1

After a working with SyMpy library I receive an expression (in yprime variable).

from sympy import *
x = Symbol('x')
y = 1 - (0.1 * coeff1) / (x + 2) - sin(x) * (2 * x + coeff1)
yprime = y.diff(x)

Then I try to use yprime in calculations, but Python interpret it as a text: -(2*x + 1.0)*cos(x) - 2*sin(x) + 0.1/(x + 2)**2

How may I calculate -(2*x + 1.0)*cos(x) - 2*sin(x) + 0.1/(x + 2)**2 ?

After some manipulations according to mtadd:

from sympy import *
x, coeff1 = symbols('x coeff1')
y = 1 - (0.1 * coeff1) / (x + 2) - sin(x) * (2 * x + coeff1)
yprime = y.diff(x).evalf(subs={x:0,coeff1:1})

I receive a digital result, but still it's unable to operate with further logic. It says:

TypeError: can't convert expression to float

Community
  • 1
  • 1
Alderven
  • 7,569
  • 5
  • 26
  • 38

2 Answers2

2

Call the method subs on your symbolic expression to calculate an analytic expression. You can call evalf on the analytic expression to a numeric solution in a Python float. Below is some example output from isympy after entering your input from above (coeff1 = 1.0).

In [18]: yprime
Out[18]:
                                   0.1
-(2⋅x + 1.0)⋅cos(x) - 2⋅sin(x) + ────────
                                        2
                                 (x + 2)

In [19]: yprime.subs(x,2*pi)
Out[19]:
                0.1
-4⋅π - 1.0 + ──────────
                      2
             (2 + 2⋅π)

In [20]: yprime.subs(x,2*pi).evalf()
Out[20]: -13.5649131254944

The following program:

from sympy import *
x, C1 = symbols('x C1')
y = 1-(0.1*C1)/(x+2)-sin(x)*(2*x+C1)
print y.diff(x).evalf(subs={x:pi/2,C1:1})

prints -1.99215722345589 with sympy version 0.7.2 and python 2.7.3.

mtadd
  • 2,495
  • 15
  • 18
  • Another valid expression is `yprime.evalf(subs={x:2*pi})` – mtadd Apr 08 '13 at 00:57
  • Unfortunately, methods `subs` and `evalf` returns the same expression: **-(2*x + 1.0)*cos(x) - 2*sin(x) + 0.1/(x + 2)**2** – Alderven Apr 08 '13 at 01:00
  • I'm working with sympy version 0.7.2, check your version with `import sympy; print sympy.__version__` – mtadd Apr 08 '13 at 01:18
  • Mine too 0.7.2 and Python 2.7. – Alderven Apr 08 '13 at 01:24
  • Try the 4 line script I added to the end of my answer above. – mtadd Apr 08 '13 at 01:27
  • I can't even save **yprime** to file. It says: `TypeError: expected a character buffer object` – Alderven Apr 08 '13 at 01:31
  • If you're using `write`, it expects a string. Try write(str(yprime)) – mtadd Apr 08 '13 at 01:34
  • It may be due to a distinction between using the `Symbol` class constructor, and the `symbols` function that returns a tuple of multiple instantiations of said class. (note case of first letter). – mtadd Apr 08 '13 at 01:36
  • In your original question, what is the type of `coeff1`? – mtadd Apr 08 '13 at 01:49
  • after line 4 of your sample code, try to `print type(yprime)` and verify that its `sympy.core.add.Add` (basically its the root node of an AST). If `type(yprime) == str`, then you'll need to convert it to a symbolic expression using `eval`: `eval(yprime).evalf(subs={x:1.5})` – mtadd Apr 08 '13 at 02:09
  • I checked. It's class is ``. – Alderven Apr 08 '13 at 02:15
  • Somewhere along the way `yprime` has been assigned with the `y.diff(x)` expression calculated for a particular value of x. – mtadd Apr 08 '13 at 02:26
  • I updated description of the problem. Now I can't change 'digit' to real number. – Alderven Apr 08 '13 at 02:29
  • I assume you're trying to cast something via the `float` function. What is the object's type before attempting to cast it? – mtadd Apr 08 '13 at 02:33
  • 'X' - int, 'coeff1' - float. That's all - I didn't used any other variables in this part of program. – Alderven Apr 08 '13 at 02:37
  • try wrapping right hand side of last line with a call to `float`: e.g. `yprime = float(y.diff(x).evalf(subs={x:0,coeff1:1}))` afterwards, `type(yprime)` should be `float` – mtadd Apr 08 '13 at 02:41
  • Sadly, the same error: **TypeError: can't convert expression to float** – Alderven Apr 08 '13 at 02:49
  • That exception raises in `sympy.core.expr.Expr` class `__float__` method. Unfortunately, I can't figure out why from here with the information I have. try running the script with the python debugger enabled (_python -m pdb script.py_), and also look at the relevant python code in **site-packages/sympy/core/expr.py** (line 221) – mtadd Apr 08 '13 at 03:05
  • Thanks for help. I'll try to figure it out by myself. – Alderven Apr 08 '13 at 05:36
0

I think this might help Evaluating a mathematical expression in a string I think yprime is being interpreted as a string and this first answer of the post describes how to evaluate a string as an arithmetic expression.

Community
  • 1
  • 1
Jake Schievink
  • 409
  • 4
  • 16