Ondrej Certik | 1 Oct 03:28 2009
Picon

Re: SymPy and Reinteract


Hi Jorn!

On Tue, Sep 29, 2009 at 3:03 PM, Jorn Baayen <jorn.baayen@...> wrote:
> Dear all,
>
> This is just a short notice to let you know about a little project of mine
> that should hopefully be useful to others. I've
> been hacking Reinteract (reinteract.org, an interactive Python shell which
> allows you to go back and edit previous
> statements) to render SymPy objects using MathML (GtkMathView) or LaTeX.
> Screenshots, links to required Python
> bindings and a link to my Reinteract git branch can be found in my blog post
> here:
>
>
> http://jbaayen.blogspot.com/2009/09/part-of-what-makes-sympy-so-useful-is.html

Very nice! Thanks also for your recent patches to sympy, great job.

Will it be possible to use reinteract over the web browser in the
future? Or do you think it would be too much work.

Ipython is nice and together with unicode pretty printing, I really
like it, I think it's the best one can get out of the terminal.
Nevertheless, as you said, sometimes I want more. I think having it in
the browser is the best way, in the long term.

William Stein recently disentangled the notebook, so it can be used as
a standalone library:
(Continue reading)

Floris van Breugel | 1 Oct 09:29 2009
Picon

error: "Invalid literal" f(x,y) not a valid variable


Hi all,

I'm completely new to sympy, and wrote some code that generated a
rather nasty expression "foo" (which is a function of four sympy
symbols), which I would like to differentiate. When I try
differentiate foo with respect to sympy symbols vx or vz, I get the
following value error:

<type 'exceptions.ValueError'>: Invalid literal: (vx*cos
(0.785398163397448) - vz*sin(0.785398163397448))*sin
(0.436332312998582) + (vx*sin(0.785398163397448) + vz*cos
(0.785398163397448))*cos(0.436332312998582) is not a valid variable

code:

vx,vz,p_ctl,freq = sympy.symbols('vx, vz, p_ctl, freq')

foo = 0.0159434228515462 - 0.253132498196857*freq -
0.0169245565654875*p_ctl + 0.00981133713941305*vx*sin
(0.785398163397448) + 0.00981133713941305*vz*cos(0.785398163397448) +
0.181019170222171*freq*p_ctl + 0.00809435314001577*vz*sin
(0.785398163397448) - 0.00717454028319579*abs(vx*cos
(0.785398163397448) - vz*sin(0.785398163397448))**2*(0.0494 +
0.0063*sin(3*atan2((vx*cos(0.785398163397448) - vz*sin
(0.785398163397448))*cos(0.436332312998582) - (vx*sin
(0.785398163397448) + vz*cos(0.785398163397448))*sin
(0.436332312998582), (vx*cos(0.785398163397448) - vz*sin
(0.785398163397448))*sin(0.436332312998582) + (vx*sin
(0.785398163397448) + vz*cos(0.785398163397448))*cos
(Continue reading)

Renato Coutinho | 1 Oct 10:21 2009
Picon

Re: error: "Invalid literal" f(x,y) not a valid variable


Hello Floris,

I'll bet the problem is when you try to differentiate atan2 in the
line 7. Trying this:

>>> atan2(f(x), f(x)).diff(x)
   2*D(atan2(f(x), f(x)), f(x))*D(f(x), x)

>>> atan2(f(x), f(x)).diff(x).doit()

I get the same error. This is because atan2 doesn't have a derivative
implemented, so sympy relies on the derivative of the compose
function, which leads to a derivative in terms of a function, not a
symbol, and that triggers the error.

I don't know why diff() evaluates to a derivative in terms of a
function, I was thinking of reporting this as a bug. It's possible to
look if the derivative of the inner function evaluates to a symbol; if
not, leave it as it is. If people agree, I can provide a patch.

To have your calculation really done, you'll have to implement the
derivative of atan2, defined in
sympy/functions/elementary/trigonometric.py.

Someone please correct me if I'm wrong, I'm new around here too. It
just happens I've been bumping in exactly this kind of problem in the
last days.

Renato
(Continue reading)

Ondrej Certik | 1 Oct 10:37 2009
Picon

Re: error: "Invalid literal" f(x,y) not a valid variable


Hi Renato and Floris,

On Thu, Oct 1, 2009 at 1:21 AM, Renato Coutinho
<renato.coutinho@...> wrote:
>
> Hello Floris,
>
> I'll bet the problem is when you try to differentiate atan2 in the
> line 7. Trying this:
>
>>>> atan2(f(x), f(x)).diff(x)
>   2*D(atan2(f(x), f(x)), f(x))*D(f(x), x)
>
>>>> atan2(f(x), f(x)).diff(x).doit()
>
> I get the same error. This is because atan2 doesn't have a derivative
> implemented, so sympy relies on the derivative of the compose
> function, which leads to a derivative in terms of a function, not a
> symbol, and that triggers the error.
>
> I don't know why diff() evaluates to a derivative in terms of a
> function, I was thinking of reporting this as a bug. It's possible to
> look if the derivative of the inner function evaluates to a symbol; if
> not, leave it as it is. If people agree, I can provide a patch.
>
> To have your calculation really done, you'll have to implement the
> derivative of atan2, defined in
> sympy/functions/elementary/trigonometric.py.
>
(Continue reading)

Vinzent Steinberg | 1 Oct 11:18 2009

Re: Symbolic matrix inversion


On Sep 29, 9:49 pm, Luke <hazelnu...@...> wrote:
> I was thinking that it might be nice to have pre-computed matrix inverses
> for n x n matrices.  Matrix inversion is O(n^3), so it would be nice to have
> all this precomputed symbolically, and this would greatly speed up Sympy's
> matrix capabilities.  Inverses up to say 100x100 could be computed (or maybe
> something smaller), and then when you need an inverse, everything would be
> fast.  This could also be used behind the scenes (by introduction of
> symbolic substitution dictionaries) for inverting a matrix full of sympy
> expressions.

I think there won't be a huge speed advantage, because in the end you
get a lazily evaluated version of the result of the inversion
algorithm. If nothing simplifies somehow (and I think it doesn't for
the general dense case), you only avoid some sympy overhead, if at
all.

Vinzent
Renato Coutinho | 1 Oct 12:27 2009
Picon

Re: error: "Invalid literal" f(x,y) not a valid variable


Hi Ondrej,

On Thu, Oct 1, 2009 at 5:37 AM, Ondrej Certik <ondrej@...> wrote:
> I think you are exactly right. If you could send a patch fixing this,
> it's be really awesome. Let me know if you need any help with it, or
> to explain anything in sympy.

Posted issue and patch here:

http://code.google.com/p/sympy/issues/detail?id=1660

It's strange that there were tests expecting exactly the old
behaviour. I don't know if this could be potentially useful, like
having a non-evaluated D(f(x), f(x)) * D(f(x), x) to substitute
D(f(x), f(x)) later?

Also, is there any way to represent a partial derivative, or a
derivative calculated at a specific point? When there's some function
f(g(x), x, x) and I do .diff(x), I had to keep it as D(f(g(x), x, x),
x), although it's possible to expand the derivative in the two last
args, but then how could I represent D_{x1}(f(x1, x, x) |x1=g(x) ?

Renato

bblais | 1 Oct 11:37 2009
Picon

sympy returns a dictionary sometimes,and sometimes a list of tuples...why?


Hello,

I wrote a very simple script using sympy, and things were working
fine, except for one problem.  So I have:

from sympy import *

x, y = symbols('x','y',real=True)
alpha,beta,gamma=symbols('alpha','beta','gamma',real=True)
alpha_p,beta_p,gamma_p=symbols('alpha_p','beta_p','gamma_p',real=True)

L = symbols('L',real=True)

and then I look at solutions to some equations, like:

solution=solve([beta*y - alpha*(1+y/L) ,
                -beta_p*x + alpha_p ], [x, y])

print solution

which prints (correctly):

{x: alpha_p/beta_p, y: L*alpha/(-alpha + L*beta)}

now, if I do:

solution=solve([beta*y - alpha*(1+y/L) - gamma*x*(1+y/L),
                -beta_p*x + alpha_p - gamma_p*y], [x, y])

(Continue reading)

Ondrej Certik | 1 Oct 16:51 2009
Picon

Re: error: "Invalid literal" f(x,y) not a valid variable


On Thu, Oct 1, 2009 at 3:27 AM, Renato Coutinho
<renato.coutinho@...> wrote:
>
> Hi Ondrej,
>
> On Thu, Oct 1, 2009 at 5:37 AM, Ondrej Certik <ondrej@...> wrote:
>> I think you are exactly right. If you could send a patch fixing this,
>> it's be really awesome. Let me know if you need any help with it, or
>> to explain anything in sympy.
>
> Posted issue and patch here:
>
> http://code.google.com/p/sympy/issues/detail?id=1660
>
> It's strange that there were tests expecting exactly the old
> behaviour. I don't know if this could be potentially useful, like
> having a non-evaluated D(f(x), f(x)) * D(f(x), x) to substitute
> D(f(x), f(x)) later?

Why not to just fix atan2, so that it's derivatives work? I thought
the main bug is in there.

>
> Also, is there any way to represent a partial derivative, or a
> derivative calculated at a specific point? When there's some function
> f(g(x), x, x) and I do .diff(x), I had to keep it as D(f(g(x), x, x),
> x), although it's possible to expand the derivative in the two last
> args, but then how could I represent D_{x1}(f(x1, x, x) |x1=g(x) ?

(Continue reading)

Aaron S. Meurer | 1 Oct 16:54 2009
Picon

Re: error: "Invalid literal" f(x,y) not a valid variable


See issue 1620.

Aaron  Meurer
On Oct 1, 2009, at 8:51 AM, Ondrej Certik wrote:

>
> On Thu, Oct 1, 2009 at 3:27 AM, Renato Coutinho
> <renato.coutinho@...> wrote:
>>
>> Hi Ondrej,
>>
>> On Thu, Oct 1, 2009 at 5:37 AM, Ondrej Certik <ondrej@...>  
>> wrote:
>>> I think you are exactly right. If you could send a patch fixing  
>>> this,
>>> it's be really awesome. Let me know if you need any help with it, or
>>> to explain anything in sympy.
>>
>> Posted issue and patch here:
>>
>> http://code.google.com/p/sympy/issues/detail?id=1660
>>
>> It's strange that there were tests expecting exactly the old
>> behaviour. I don't know if this could be potentially useful, like
>> having a non-evaluated D(f(x), f(x)) * D(f(x), x) to substitute
>> D(f(x), f(x)) later?
>
> Why not to just fix atan2, so that it's derivatives work? I thought
> the main bug is in there.
(Continue reading)

Andrew Straw | 1 Oct 23:36 2009
Picon

Re: error: "Invalid literal" f(x,y) not a valid variable


Aaron S. Meurer wrote:
> See issue 1620.
>
> Aaron  Meurer
> On Oct 1, 2009, at 8:51 AM, Ondrej Certik wrote:
>
>   
>> Why not to just fix atan2, so that it's derivatives work? I thought
>> the main bug is in there.
>>     
FWIW, I have used this in the past, and it's worked for me:

def my_atan2(y,x):
    """take arctangent of y/x preserving angle"""
    # from http://en.wikipedia.org/wiki/Atan2
    return 2*sympy.atan( y/(sympy.sqrt(x**2+y**2) + x) )

--

-- 
Andrew D. Straw, Ph.D.
California Institute of Technology
http://www.its.caltech.edu/~astraw/


Gmane