2020-04-27 17:24:43 -0600 received badge ● Good Answer (source) 2019-08-08 12:05:22 -0600 received badge ● Nice Answer (source) 2016-11-02 16:20:04 -0600 received badge ● Good Answer (source) 2016-11-02 16:20:04 -0600 received badge ● Nice Answer (source) 2016-04-06 06:18:21 -0600 received badge ● Good Question (source) 2015-11-11 11:46:15 -0600 received badge ● Great Answer (source) 2015-08-04 07:18:28 -0600 received badge ● Nice Answer (source) 2015-02-27 01:19:34 -0600 received badge ● Great Answer (source) 2015-02-27 01:19:34 -0600 received badge ● Guru (source) 2014-06-28 20:14:50 -0600 marked best answer Using Sage Symbolic Functions in Scipy fsolve I need to find the roots of a system of multivariate non-linear (algebraic) functions and Sage's solve function is running indefinitely. (Where "indefinitely" means "more than 30 mins".) I only require numerical solutions so I was hoping to use Scipy's fsolve function instead. However, the functions that I'm generating are Sage symbolic functions, which have been mightly convenient to work with, and I'm having trouble getting them into a format that fsolve will understand. Does anyone have any suggestions? The documentation for Scipy's fsolve can be found here. It looks like a Python function is sufficient. As a toy example, I've tried the following: sage: from scipy.optimize import fsolve sage: var('x,y') sage: f = x^2 + y^2 sage: def ff(v): return f(x=v,y=v) sage: fsolve(ff,[1,1])  It seems like the naive approach but I'm receiving the error TypeError: no canonical coercion from to Symbolic Ring. Perhaps this is an issue with Sage properly dealing with Numpy/Scipy data types? 2014-06-28 20:14:49 -0600 marked best answer Linking to ATLAS/BLAS in Cython I've been using GSL's CBLAS for performing "fast" matrix-vector arithmetic in a Cython script but I've been told that ATLAS' CBLAS is faster by a couple of factors. However, I'm having trouble locating the ATLAS libraries and headers in $SAGE_ROOT/local. Could someone provide tips on how to link to CBLAS in my Cython code? Thanks. Edit: I found in module_list.py how different implementations of BLAS are detected. However, I haven't found any example codes that cdef extern that functionality directly! Anyone have any examples of using Sage's detected version of ATLAS? 2014-06-28 20:14:43 -0600 marked best answer Differentiating Complex Conjugated Functions This is primarily a question of understanding the syntax of some output although there might be a bug hidden underneath. Consider the following code: sage: var('x,t') sage: q = function('q',x,t) sage: f = q*q.conjugate() sage: print f.derivative(x,1) q(x, t)*D(conjugate)(q(x, t))*D(q)(x, t) + conjugate(q(x,t))*D(q)(x, t)  The answer is supposed to be$d/dx(q\bar{q}) = q_x \bar{q} + q \bar{q}_x$. The second term in the Sage output is correct but I'm having trouble deciphering the first term. Any thoughts? I think I can narrow down the differences even further. Check it out: sage: print q.conjugate().derivative(x,1) D(conjugate)(q(x, t))*D(q)(x, t) sage print q.derivative(x,1).conjugate() conjugate(D(q)(x, t))  The independence of order isn't the issue:$q = u + iv$means that$q_x = u_x + iv_x$,$\bar{q} = u - iv$. So$\bar{q_x} = u_x - iv_x$and$(\bar{q})_x = (u - iv)_x = u_x - iv_x\$. 2014-05-04 02:46:28 -0600 received badge ● Popular Question (source) 2013-12-24 08:51:15 -0600 received badge ● Famous Question (source) 2013-12-05 09:58:02 -0600 received badge ● Notable Question (source) 2013-09-17 04:03:38 -0600 received badge ● Good Answer (source) 2013-05-06 13:45:13 -0600 received badge ● Taxonomist 2013-04-22 14:48:12 -0600 received badge ● Famous Question (source) 2012-10-18 04:03:22 -0600 received badge ● Popular Question (source) 2012-06-20 09:09:28 -0600 received badge ● Notable Question (source) 2012-06-07 04:26:35 -0600 received badge ● Famous Question (source) 2012-02-18 17:16:58 -0600 received badge ● Good Answer (source) 2012-01-28 17:50:09 -0600 received badge ● Great Answer (source) 2012-01-28 17:50:09 -0600 received badge ● Good Answer (source) 2012-01-28 17:50:09 -0600 received badge ● Nice Answer (source) 2012-01-17 01:15:13 -0600 marked best answer Differentiating Complex Conjugated Functions It looks like Sage is (incorrectly? naively?) applying the chain rule to conjugate(q(x,t)), since conjugate is a pure symbolic thing in Sage a priori. In fact, the multiple variables are irrelevant: sage: r = function('r',x) sage: g = r*r.conjugate() sage: g.derivative(x) r(x)*D(conjugate)(r(x))*D(r)(x) + conjugate(r(x))*D(r)(x) sage: r.conjugate().derivative(x) D(conjugate)(r(x))*D(r)(x) sage: r.derivative().conjugate() conjugate(D(r)(x))  Ginac (and hence Pynac) does not appear to support doing this - see this. However, presumably one could implement a trivial conjugation to the fderivative or tderivative ... the problem is that I'm not sure whether we should automatically assume that this makes sense. See, for instance, the convoluted discussion at The Maple equivalent of ask.sagemath. On the other hand, Sympy allows this. If there is a consensus, a ticket should be opened, though! 2012-01-17 01:11:54 -0600 marked best answer Using Sage Symbolic Functions in Scipy fsolve The original scenario involves two variables and only one equation. Because ff accepts two variables but does not return a list (or tuple, or array, ...) of length 2, the "shape mismatch" error is thrown. Here is a working example (with two equations and minimal modifications): import scipy.optimize var('x,y') f = x^2 + y^2 - 1 g = x - y^2 def ff(v): x, y = map(float, v) return [f(x=x, y=y), g(x=x, y=y)] print scipy.optimize.fsolve(ff, [1, 1])  Here is a more compact version using lambda functions and the trick described link:here import scipy.optimize var('x,y') f(x,y) = (x^2 + y^2 - 1, x - y^2) print scipy.optimize.fsolve(lambda v: f(*map(float, v)), (1, 1))  Since you said you have a large number of variables and equations, here is a slightly more "scale-friendly" approach (but somewhat kludgy): import scipy.optimize # Create symbolic variables "x_0" and "x_1". # This is a pretty messy kludge x = tuple( SR.var( "".join(['x_', str(t)]) ) for t in range(2) ) # Represent system of equations using "x" as defined above. # I use the intermediate "exprs" variable to allow breaking up the tuple # over multiple lines. exprs = ( x^2 + x^2 - 1, x - x^2 ) f(*x) = exprs # Guess values guess = (1, 1) print scipy.optimize.fsolve(lambda v: f(*map(float, v)), guess)  In all cases, the result should look like: [ 0.61803399 0.78615138] 2012-01-12 02:59:23 -0600 received badge ● Nice Answer (source) 2012-01-12 02:59:23 -0600 received badge ● Good Answer (source) 2012-01-08 21:58:38 -0600 received badge ● Notable Question (source) 2011-10-26 02:35:15 -0600 received badge ● Notable Question (source) 2011-09-07 09:25:16 -0600 received badge ● Popular Question (source) 2011-08-01 04:42:33 -0600 received badge ● Popular Question (source) 2011-07-25 08:32:42 -0600 received badge ● Great Answer (source) 2011-07-11 12:51:38 -0600 received badge ● Popular Question (source) 2011-07-11 06:22:46 -0600 received badge ● Good Answer (source) 2011-06-30 05:15:47 -0600 answered a question Running PARI/GP and Sage Running for i in range(n): pari('a[i]')  won't work since 'a[i]' is a string. Try something like this instead: for i in range(n): pari('a[%d]' %(i))  This will actually substitute the values i takes on into the string expressions you want to evaluate. 2011-06-24 13:05:44 -0600 received badge ● Good Answer (source) 2011-06-03 08:44:58 -0600 commented answer How to change _latex_ of log to \ln ? No worries. I think kcrisman's answer is at least starting to head in the direction of what you're looking for. 2011-06-03 06:59:49 -0600 answered a question How to change _latex_ of log to \ln ? I'm not exactly sure how this is a Sage question but in your LaTeX preamble you can write \DeclareMathOperator*{\tg}{tan}  so that whenever you write "\tg" in the LaTeX code is will appear as "tan" in the document. 2011-06-03 06:27:27 -0600 commented answer Export to C code Very interesting. I know that some of the Sympy folks have been involved with f2py, a Fortran to Python wrapping utility. Quite interesting that they have a C codegen as well! 2011-06-02 12:48:44 -0600 commented answer Export to C code Which link are you talking about? They work fine for me. 2011-05-28 11:20:12 -0600 received badge ● Enlightened (source) 2011-05-28 10:55:26 -0600 edited answer Replace part of expression Alternatively, you can write your expression out using Sympy, a symbolics package that's included with Sage. (Sympy Documentation) sage: from sympy import * sage: T = Symbol('T') sage: p = Symbol('p') sage: z = Symbol('z') sage: X = (5/(exp(T*p) - 1) + 1/(exp(T*p) - exp(2*T)))*exp(T*p)/(80*exp(T*p) + exp(2*T*p)) sage: X.subs(exp(T*p),z) z*(1/(z - exp(2*T)) - 5/(1 - z))/(80*z + z**2)  Note that Sympy correctly identifies that exp(2*T*p) == z^2. In some cases, Sympy can work better than the core of most of Sage's symbolics: Maxima. You should try both and see which one performs to your liking. You can easily switch from Sage's symbolic expressions to Sympy's by doing the following: sage: import sympy sage: var('T,p,z') sage: X = (5/(e^(T*p) - 1) + 1/(e^(T*p) - e^(2*T)))*e^(T*p)/(80*e^(T*p) + e^(2*T*p)) sage: type(X) sage: Y = sympy.sympify(X) sage: Y (1/(-exp(2*T) + exp(T*p)) - 5/(1 - exp(T*p)))*exp(T*p)/(80*exp(T*p) + exp(2*T*p)) sage: type(Y)  You can now treat Y similarly as above. sage: Y.subs(exp(T*p),z) z*(1/(z - exp(2*T)) - 5/(1 - z))/(80*z + z**2)  Note that we didn't have to convert T,p,z, nor exp to Sympy data types. Isn't that great? EDIT: You can convert a Sympy expression back to a Sage symbolic expression by doing the following: sage: Z = SR(Y.subs(exp(T*p),z)) sage: Z (5/(z - 1) + 1/(z - e^(2*T)))*z/(z^2 + 80*z) sage: Z(z=-1) -1/79/(e^(2*T) + 1) - 5/158