Ask Your Question
1

how to convert a named function to sympy

asked 2014-09-11 09:16:19 +0100

Jannik gravatar image

updated 2016-03-08 21:02:01 +0100

FrédéricC gravatar image

i know from another question, that

sage: x = var('x')
sage: y = 2 * x + Ei(x)
sage: import sympy
sage: sympy.sympify(y)
2*x + Ei(x)
sage: type(_)
<class 'sympy.core.add.Add'>

or

sage: x = var('x')
sage: y = 2 * x + Ei(x)
sage: y._sympy_()
2*x + Ei(x)

works. But how can i convert a equation named for example d to sympy.

var('r h s phi theta d')

s_v=vector([s*cos(theta),-s*sin(theta)])
h_v=vector([h*sin(phi),-h*cos(phi)])
r_v=vector([r*cos(phi),r*sin(phi)])

f_v=r_v+h_v-s_v

def f(r,h,s,phi,theta): return norm(f_v(r=r,h=h,s=s,phi=phi,theta=theta))

d=f(r,h,s,-phi,theta)-f(r,h,s,0,theta)

import sympy
sympy.sympify(d)
d

i also tried to do

from sympy import sympify
from sympy.solvers import solve
solve(sympify(d),r)

but than he is telling me

Traceback (click to the left of this block for traceback)
...
NotImplementedError: SymPy function 'abs' doesn't exist

:'(

thank you a lot


Thank you guys. I can´t find a Sage 6.4 Beta3 Version vor VM. I downloaded this . Is it somehow possible, to use this folder to run in through a VM. I have no experience with that and as far as i know until yet do i need a *.ova file.

I now tried in sage 6.3

d = abs(f_v.subs(phi=-phi))-abs(f_v.subs(phi=0))


from sympy import sympify
from sympy.solvers import solve
#sympy.sympify(d)
solve(sympify(d),r)

solve it for theta works now, wich is a big step forward, thank you for that! But for any other variable for example r is the result: [] for d it is : [0] should there just come the equation for d?

Does this mean, that sage can´t solve this equation or, that i still do something wrong?

edit retag flag offensive close merge delete

Comments

The 6.4.beta3 tarball you downloaded is Sage's source code, which must be compiled, so you need to run GNU/Linux or some other POSIX compliant system to do this. If you are using the VM through windows, it is much less easy, so it is preferable to wait for the official Sage 6.4, that will propose .ova images. It is never too late to switch to GNU/Linux :)

tmonteil gravatar imagetmonteil ( 2014-09-14 19:38:09 +0100 )edit

2 Answers

Sort by » oldest newest most voted
1

answered 2014-09-11 13:33:43 +0100

FrédéricC gravatar image

Something like that maybe ?

sage: d = abs(f_v.subs(phi=-phi))-abs(f_v.subs(phi=0))
sage: sympy.sympify(d)
-sqrt((-h + s*sin(theta))**2 + (-r + s*cos(theta))**2) + sqrt((-h*sin(phi) + r*cos(phi) - s*cos(theta))**2 + (h*cos(phi) + r*sin(phi) - s*sin(theta))**2)
edit flag offensive delete link more
1

answered 2014-09-11 17:59:01 +0100

tmonteil gravatar image

The problem comes from the fact that Sage does not know that its own abs should be translated into sympy.Abs (see the upper case). This has been fixed in trac ticket 15057, and merged today in Sage 6.4.beta3. Your options are:

  • compile Sage 6.4.beta3
  • wait for Sage 6.4 official release
  • let sympy know about abs

For this, just add the following before your code:

sage: import sympy
sympy.abs = sympy.Abs

In you last line, you will get (i did not check it it the expected result though):

sage: sympy.sympify(d)
-sqrt(Abs(-h + s*sin(theta))**2 + Abs(r - s*cos(theta))**2) + sqrt(Abs(-h*sin(phi) + r*cos(phi) - s*cos(theta))**2 + Abs(-h*cos(phi) - r*sin(phi) + s*sin(theta))**2)
edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

1 follower

Stats

Asked: 2014-09-11 09:16:19 +0100

Seen: 895 times

Last updated: Sep 14 '14