How to solve the multi object problem?
import sympy as sym
from sympy import lambdify
x = sym.Symbol('x')
n = sym.Symbol('n')
f = sym.sin(n*x)
derivative_f = f.diff(x)
derivative_f = lambdify(x, derivative_f)
x = float(input('x:'))
print(derivative_f(x))
print(derivative_f)
If I input 2, the expected result should be 2*cos(2*x).
Your code contains a few misconceptions. One problem is an important general programming rule: try to use different variable names for variables with different meanings. So, x shouldn't be assigned a float, as it was a symbolic variable before. And derivative_f being a symbolic expression, shouldn't be assigned the result of lambdify.
Note that sympy's symbolic world doesn't mix well with the numeric world of non-sympy functions. lambdify forms a bridge between these worlds, from completely symbolic to completely numeric. E.g. the function created with lambdify doesn't have access to the symbolic n anymore.
The code lambdify(x, derivative_f) contains an error. derivative_f is a symbolic expression containing two symbolic variables (x and n), so it needs to be called as derivative_f_x_n = lambdify((x, n), derivative_f) (also giving the result a different name). Afterwards, you can use numeric expressions as derivative_f_x_n(7, 8), but you can't use symbolic parameters anymore.
For what you seem to be trying to do, lambdify isn't adequate. To get the derivative with x substituted, you call .subs(x, new_value) directly on the symbolic version of derivative_f:
import sympy as sym
from sympy import lambdify
x = sym.Symbol('x')
n = sym.Symbol('n')
f = sym.sin(n * x)
derivative_f_x = f.diff(x)
x_float = 2.0
print(derivative_f_x.subs(x, x_float))
Output: n*cos(2.0*n)
Also note that sympy strongly prefers to work with exact symbolic expressions, and using floats inevitably brings in approximations. Whenever possible, integers, sympy fractions (sym.S(1)/2) or symbolic expressions (sym.sqrt(5)) are recommended.
You call also use the derivative with respect to x and then substitute n:
print(f.diff(x).subs(n, 2))
Output: 2*cos(2*x)
To use that function later on in numeric calculations, after substitution you only have one symbolic variable left(x):
g = lambdify(x, f.diff(x).subs(n, 2))
You can type help(g) to see its generated source code:
Source code:
def _lambdifygenerated(x):
return (2*cos(2*x))
Then you can use g e.g. to create matplotlib plot. After lambdify nothing is symbolic anymore.
import matplotlib.pyplot as plt
import numpy as np
xs = np.linspace(0, 10)
plt.plot(xs, g(xs))
The other answer hit the key point, that you needed to account for the symbolic n when lambdifying.
Look at the help of your function:
In [2]: help(derivative_f)
Help on function _lambdifygenerated:
_lambdifygenerated(x)
Created with lambdify. Signature:
....
Source code:
def _lambdifygenerated(x):
return (n*cos(n*x))
It's a function of x, drawing n from the global environment, here a symbol.
The full error message when given an array argument:
In [5]: derivative_f(np.array([.1,.2,.3]))
AttributeError: 'Mul' object has no attribute 'cos'
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "<ipython-input-5-e04e9f3c0a8e>", line 1, in <module>
derivative_f(np.array([.1,.2,.3]))
File "<lambdifygenerated-1>", line 2, in _lambdifygenerated
return (n*cos(n*x))
TypeError: loop of ufunc does not support argument 0 of type Mul which has no callable cos method
See the return line in the traceback. That's the 'source code' in the help, and tells you what numpy is trying to execute.
test the inner expression of that line:
In [6]: n*np.array([.1,.2,.3])
Out[6]: array([0.1*n, 0.2*n, 0.3*n], dtype=object)
This is an object dtype array. Math on such an array delegates the calculation to each of the elements. Specifically it tries
(0.1*n).cos()
0.1*n is a sympy mul expression, hence the error. symbols often don't work in numpy expressions.
lambdify is a handy way of creating numpy functions from sympy expressions, but it needs to be used with care, and full awareness of what it does, and does not, do.
Related
Suppose you have computed fu as a result of a sympy calculation:
fu= sy.cos(x)+sy.sin(y)+1
where
x,y = sy.symbols("x y")
are symbols. Now you want to turn fu to a numpy function of (obviously) two variables.
You can do this by:
fun= sy.lambdify((x,y), fu, "numpy")
and you produce fun(x,y). Is there a way that lambdify can produce fun(z) with x,y=z, i.e produce the following function:
def fun(z):
x,y=z
return np.cos(x)+np.sin(y)+1
According to the documentation of lambdify you can nest the symbols in the first argument to denote unpacking in the signature:
import sympy as sym
x,y = sym.symbols('x y')
fu = sym.cos(x) + sym.sin(y) + 1
# original: signature f1(x, y)
f1 = sym.lambdify((x,y), fu)
f1(1, 2) # returns 2.4495997326938213
# nested: signature f2(z) where x,y = z
f2 = sym.lambdify([(x,y)], fu)
f2((1, 2)) # returns 2.4495997326938213
Even if this weren't possible to do within lambdify, we could define a thin wrapper that unpacks the arguments to the lambdified function (although this would be one function call slower on each call, so for fast functions that get called a lot of times this might lead to measurable impact on the runtime):
f = sym.lambdify((x,y), fu) # signature f(x,y)
def unpacking_f(z): # signature f(z) where x,y = z
return f(*z)
Of course if the function is not for a single, throw-away use in a numerical solver (such as curve fitting or minimization), it's good practice to use functools.wraps for wrappers. This would preserve the docstring automatically generated by lambdify.
I use Sympy in my Python project, were the string definition of the expression converted to Sympy through sympify method. For example:
import sympy as sp
exp1 = sp.sympify('Add(Rational(1, 5), pi)')
While I am able to describe almost all functionality, I ran into the problem of Vector description to be understood by sympify method. Vector definition in normal way:
from sympy.vector import CoordSys3D
R = CoordSys3D('R')
v = 3*R.i + 4*R.j + 5*R.k
Next example (one of a variety) is failing:
exp2 = sp.sympify('Vector(Coord3D().i*3, Coord3D().j*4, Coord3D().k*5')
I would like to keep Vector format to support the curl, divergence and gradient functionality. Definition of 1-row matrix does not suit here for me. Documentation research did not give results.
I would be grateful for an example or suggestion of the right way for the sympy.vector definition for the sympify method.
If you pass in a locals dictionary, will that do what you want:
>>> from sympy.vector import *
>>> sympify('Vector(R.i*3, R.j*4, R.k*5)',
... locals=dict(R=CoordSys3D('R'), Vector=Vector))
Vector(3*R.i, 4*R.j, 5*R.k)
>>> type(_)
<class 'sympy.vector.vector.Vector'>
I just started learning basic parsing in sympy, and having little trouble with this.
I want to make a proc(), which the caller will pass it a symbolic function, and the argument to the function, each as separate argument. i.e. instead of calling the proc as proc( y(x) ), I wanted to call it as proc(y,x), which I thought will be easier for me to check its arguments are what should be.
Next, inside the proc, wanted to first verify that the first argument type is indeed a sympy function and the second argument is a sympy symbol before doing any processing.
What is the correct way to check that the argument is a sympy function and nothing else? This is what I tried. Created one file:
================
def process(y,x):
if not isinstance(x, sympy.Symbol):
raise Exception(x + " should be a sympy symbol")
if not isinstance(y,sympy.UndefinedFunction): #this does not work
raise Exception(y + " should be a sympy function")
import sympy
x = sympy.symbols('x')
y = sympy.Function('y')
process(y,x)
======================
But there is no sympy.UndefinedFunction. When I type
type(y)
Out[18]: sympy.core.function.UndefinedFunction
But
isinstance(y,sympy.core.function.UndefinedFunction)
does not work either. May be there is a better way to do all the above. Basically I wanted to make sure the input to the proc is what it should be.
Using 3.7 (conda)
Replace sympy.UndefinedFunction with sympy.function.UndefinedFunction.
Also, x and y in Exception function should be of type string, otherwise you will get unsupported operand type(s) for +: error in both cases.
New code:
def process(y,x):
if not isinstance(x, sympy.symbol.Symbol):
raise Exception(str(x) + " should be a sympy symbol")
if not isinstance(y,sympy.function.UndefinedFunction):
raise Exception(str(y) + " should be a sympy function")
import sympy
x = sympy.symbols('x')
y = sympy.Function('y')
process(y,x)
The function expm1 is not parsed properly in the following example:
from sympy.parsing.sympy_parser import parse_expr
print parse_expr('expm1(x)').diff('x')
gives
Derivative(expm1(x), x)
How can I get sympy identifying expm1 as symbolic function, so that I get the same result as
print parse_expr('exp(x) - 1').diff('x')
which gives exp(x)?
Since there is no built-in expm1 in SymPy, the parser does not know anything about this notation. The parameter local_dict of parse_expr can be used to explain the meaning of unfamiliar functions and symbols to SymPy.
expm1 = lambda x: exp(x)-1
parse_expr('expm1(x)', local_dict={"expm1": expm1})
This returns exp(x) - 1.
For expm1 to remain a single function with known derivative, rather than exp(x)-1, define it as a SymPy function (see tutorial for more such examples).
class expm1(Function):
def fdiff(self, argindex=1):
return exp(self.args[0])
A confirmation that this works:
e = parse_expr('expm1(x)', local_dict={"expm1": expm1})
print(e) # expm1(x)
print(e.diff(x)) # exp(x)
f = lambdify(x, e)
print(f(1)) # 1.718281828459045
print(f(1e-20)) # 1e-20, unlike exp(x)-1 which would evaluate to 0
I am writing a program that requires the user to enter an expression. This expression is entered as a string and converted to a Sympy expression using parse_expr. I then need to take the partial derivative of that expression that the user entered. However, diff is returning 0 with every expression I am testing.
For example if the user enters a*exp(-b*(x-c)**(2)), using the following code, diff returns 0 when it should (as far as I know about diff) return 2*a*b*(c - x)*exp(-b*(x - c)**2) when taking the partial derivative with respect to x:
a, b, c, x = symbols('a b c x', real=True)
str_expr = "a*exp(-b*(x-c)**(2))"
parsed_expr = parse_expr(str_expr)
result = diff(parsed_expr, x)
print(result) # prints 0
What am I doing wrong?
Bottom line: use parse_expr(str_expr,locals()).
Add global_dict=<dict of allowed entities to use>, too, if the expression may use any entities not imported into the local namespace and not accessible with the default from sympy import *.
According to Calculus — SymPy Tutorial - SymPy 1.0.1.dev documentation, you type the symbolic expression into the diff() argument as-is. Due to the fact that the letters are Symbol objects (with overridden operators), Python is tricked into constructing the SymPy object corresponding to the expression as it evaluates the argument!
Thus, if you have it as a string, you eval it to trigger the same behaviour:
<...>
>>> s="a*exp(-b*(x-c)**(2))"
>>> diff(eval(s), x)
−ab(−2c+2x)e−b(−c+x)2
But eval is a security hazard if used with untrusted input because it accepts arbitrary Python code.
This is where replacements like parse_expr come into play. However, due to the way expressions are parsed, described above, it needs access to the external entities used in the expression - like the Symbol objects for variables and function objects for the named functions used - through the local_dict and global_dict arguments.
Otherwise, it creates the Symbol objects on the fly. Which means, the Symbol object it has created for x in the expression is different from the variable x! No wonder that the derivative over it is 0!
<...>
>>> ps=parse_expr(s)
>>> ps.free_symbols
{a,b,c,x}
>>> x in _
False
>>> diff(ps,x)
0
>>> ps=parse_expr(s,locals())
>>> x in ps.free_symbols
True
>>> diff(ps,x)
-ab(−2c+2x)e−b(−c+x)2
Work is ongoing to make sympify safer than eval. Better to use something like the following:
from sympy import *
var ('a b c x')
str_expr = "a*exp(-b*(x-c)**(2))"
parsed_expr = sympify(str_expr)
result = diff(parsed_expr, x)
print(result)
Result:
-a*b*(-2*c + 2*x)*exp(-b*(-c + x)**2)
Replace a, b, c, x = symbols('a b c x', real=True) with:
a = Symbol('a')
b = Symbol('b')
c = Symbol('c')
x = Symbol('x')
Symbols with different assumptions compare unequal:
>>> Symbol('x') == Symbol('x', real=True)
False
When you use sympify or parse_expr, it parses unknown variables as symbols without assumptions. In your case, this creates Symbol('x'), which is considered distinct from the Symbol('x', real=True) you already created.
The solution is to either remove the assumptions, or include the locals() dictionary when you parse, so that it recognizes the name x as being the Symbol('x', real=True) that you already defined, like
parse_expr(str_expr,locals())
or
sympify(str_expr, locals())