Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

I have a more complete solution that not only allows SymPy to play nicely with Sage, but can even rope in NumPy. Here's my example (my system is an older MacOS, but one which runs the SageManifolds binary package):

import sympy as smp
import numpy as np
import matplotlib.pyplot as plt

x, a, b, c = smp.symbols('x a b c', real=True)
expr = smp.exp(-a*smp.sin(x**2)) * smp.sin(b**x) * smp.log(c*smp.sin(x)**2/x)

dydx = smp.diff(expr, x)
fx = smp.lambdify((x, a, b, c), dydx, 'numpy') 
# including third argument 'numpy' (otherwise optional) is key
fx(2, 1, 2, 3)      # produces a number
fx.subs([(x, 2), (a, 1), (b, 2), (c, 3)]).evalf()     # also produces a number

xx = np.linspace(1, 2, 100)
yy = fx(xx, 1, 2, 3)

plt.ylabel('$df / dx$', fontsize=24)
plt.xlabel('$x$', fontsize=24)
P = plt.plot(xx, yy)       # more research may show how to use native plotting without importing matplotlib

I have a more complete solution that not only allows SymPy to play nicely with Sage, but can even rope in NumPy. Here's my example (my system is an older MacOS, but one which runs the SageManifolds binary package):package @10.7):

import sympy as smp
import numpy as np
import matplotlib.pyplot as plt

x, a, b, c = smp.symbols('x a b c', real=True)
expr = smp.exp(-a*smp.sin(x**2)) * smp.sin(b**x) * smp.log(c*smp.sin(x)**2/x)

dydx = smp.diff(expr, x)
fx = smp.lambdify((x, a, b, c), dydx, 'numpy') 
# including third argument 'numpy' (otherwise optional) is key
fx(2, 1, 2, 3)      # produces a number
fx.subs([(x, 2), (a, 1), (b, 2), (c, 3)]).evalf()     # also produces a number

xx = np.linspace(1, 2, 100)
yy = fx(xx, 1, 2, 3)

plt.ylabel('$df / dx$', fontsize=24)
plt.xlabel('$x$', fontsize=24)
P = plt.plot(xx, yy)       # more research may show how to use native plotting without importing matplotlib

I have a more complete solution that not only allows SymPy to play nicely with Sage, but can even rope in NumPy. Here's my example (my system is an older MacOS, but one which runs the SageManifolds binary package @10.7):

import sympy as smp
import numpy as np
import matplotlib.pyplot as plt

x, a, b, c = smp.symbols('x a b c', real=True)
expr = smp.exp(-a*smp.sin(x**2)) * smp.sin(b**x) * smp.log(c*smp.sin(x)**2/x)

dydx = smp.diff(expr, x)
fx = smp.lambdify((x, a, b, c), dydx, 'numpy') 
# including third argument 'numpy' (otherwise optional) is key
fx(2, 1, 2, 3)      # produces a number
fx.subs([(x, 2), (a, 1), (b, 2), (c, 3)]).evalf()     # also produces a number

xx = np.linspace(1, 2, 100)
yy = fx(xx, 1, 2, 3)

plt.ylabel('$df / dx$', fontsize=24)
plt.xlabel('$x$', fontsize=24)
P = plt.plot(xx, yy)   # more research may show how to use native plotting without importing matplotlib

I have a more complete solution that not only allows SymPy to play nicely with Sage, but can even rope in NumPy. NumPy and plotting with linspace. Here's my example (my system is an older MacOS, but one which runs the SageManifolds binary package @10.7):

import sympy as smp
import numpy as np
import matplotlib.pyplot as plt

x, a, b, c = smp.symbols('x a b c', real=True)
expr = smp.exp(-a*smp.sin(x**2)) * smp.sin(b**x) * smp.log(c*smp.sin(x)**2/x)

dydx = smp.diff(expr, x)
fx = smp.lambdify((x, a, b, c), dydx, 'numpy') 
# including third argument 'numpy' (otherwise optional) is key
fx(2, 1, 2, 3)      # produces a number
fx.subs([(x, 2), (a, 1), (b, 2), (c, 3)]).evalf()     # also produces a number

xx = np.linspace(1, 2, 100)
yy = fx(xx, 1, 2, 3)

plt.ylabel('$df / dx$', fontsize=24)
plt.xlabel('$x$', fontsize=24)
P = plt.plot(xx, yy) 
# more research may show how to use native plotting without importing matplotlib