Ask Your Question

cswiercz's profile - activity

2023-05-08 23:23:23 +0100 received badge  Famous Question (source)
2021-10-19 18:56:27 +0100 received badge  Great Answer (source)
2021-04-30 12:51:49 +0100 received badge  Notable Question (source)
2020-04-28 00:24:43 +0100 received badge  Good Answer (source)
2019-08-08 19:05:22 +0100 received badge  Nice Answer (source)
2016-11-02 22:20:04 +0100 received badge  Good Answer (source)
2016-11-02 22:20:04 +0100 received badge  Nice Answer (source)
2016-04-06 13:18:21 +0100 received badge  Good Question (source)
2015-11-11 18:46:15 +0100 received badge  Great Answer (source)
2015-08-04 14:18:28 +0100 received badge  Nice Answer (source)
2015-02-27 08:19:34 +0100 received badge  Great Answer (source)
2015-02-27 08:19:34 +0100 received badge  Guru (source)
2014-06-29 03:14:50 +0100 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[0],y=v[0])
sage: fsolve(ff,[1,1])

It seems like the naive approach but I'm receiving the error TypeError: no canonical coercion from <type 'numpy.int64'> to Symbolic Ring. Perhaps this is an issue with Sage properly dealing with Numpy/Scipy data types?

2014-06-29 03:14:49 +0100 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-29 03:14:43 +0100 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[0](conjugate)(q(x, t))*D[0](q)(x, t) + conjugate(q(x,t))*D[0](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[0](conjugate)(q(x, t))*D[0](q)(x, t)
sage print q.derivative(x,1).conjugate()
conjugate(D[0](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 09:46:28 +0100 received badge  Popular Question (source)
2013-12-24 15:51:15 +0100 received badge  Famous Question (source)
2013-12-05 16:58:02 +0100 received badge  Notable Question (source)
2013-09-17 11:03:38 +0100 received badge  Good Answer (source)
2013-05-06 20:45:13 +0100 received badge  Taxonomist
2013-04-22 21:48:12 +0100 received badge  Famous Question (source)
2012-10-18 11:03:22 +0100 received badge  Popular Question (source)
2012-06-20 16:09:28 +0100 received badge  Notable Question (source)
2012-06-07 11:26:35 +0100 received badge  Famous Question (source)
2012-02-19 00:16:58 +0100 received badge  Good Answer (source)
2012-01-29 00:50:09 +0100 received badge  Good Answer (source)
2012-01-29 00:50:09 +0100 received badge  Great Answer (source)
2012-01-29 00:50:09 +0100 received badge  Nice Answer (source)
2012-01-17 08:15:13 +0100 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[0](conjugate)(r(x))*D[0](r)(x) + conjugate(r(x))*D[0](r)(x)

sage: r.conjugate().derivative(x)
D[0](conjugate)(r(x))*D[0](r)(x)
sage: r.derivative().conjugate()
conjugate(D[0](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 08:11:54 +0100 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[0]^2 + x[1]^2 - 1,
    x[0] - x[1]^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 09:59:23 +0100 received badge  Good Answer (source)
2012-01-12 09:59:23 +0100 received badge  Nice Answer (source)
2012-01-09 04:58:38 +0100 received badge  Notable Question (source)
2011-10-26 09:35:15 +0100 received badge  Notable Question (source)
2011-09-07 16:25:16 +0100 received badge  Popular Question (source)
2011-08-01 11:42:33 +0100 received badge  Popular Question (source)
2011-07-25 15:32:42 +0100 received badge  Great Answer (source)
2011-07-11 19:51:38 +0100 received badge  Popular Question (source)
2011-07-11 13:22:46 +0100 received badge  Good Answer (source)
2011-06-30 12:15:47 +0100 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 20:05:44 +0100 received badge  Good Answer (source)
2011-06-03 16:25:06 +0100 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)
<type 'sage.symbolic.expression.Expression'>
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)
<class 'sympy.core.mul.Mul'>

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
2011-06-03 15:44:58 +0100 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 13:59:49 +0100 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 13:27:27 +0100 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!