#! /usr/bin/env python3 # def sympy_evaluate ( ): from sympy import cos from sympy import lambdify from sympy import symbols import numpy import numpy as np import sympy print ( '' ) print ( 'sympy_evaluate():' ) print ( ' sympy version: ' + sympy.__version__ ) print ( ' Show ways of evaluating the symbolic humps() function.' ) print ( '' ) x = symbols ( 'x' ) humps = 100 / ( ( 10 * x - 3 )**2 + 1 ) \ + 100 / ( ( 10 * x - 9 )**2 + 4 ) \ - 6 # # subs(x,y) replaces "x" by "y". # "y" can be a symbol, an expression, or a number. # y = symbols ( 'y' ) humps_y = humps.subs ( x, y ) print ( ' Replace "x" by "y" in the formula.' ) print ( ' humps.subs(x,y) = ', humps_y ) humps_cosy = humps.subs ( x, cos(y) ) print ( ' Replace "x" by "cos(y)" in the formula.' ) print ( ' humps.subs(x,cos(y)) = ', humps_cosy ) humps_1 = humps.subs ( x, 1 ) print ( ' Replace "x" by "1" in the formula.' ) print ( ' humps.subs(x,1) = ', humps_1 ) # # lambdify() creates a numpy function from a symbolic expression. # print ( '' ) print ( ' Create a numpy function from the symbolic function.' ) humps_lambda = lambdify ( x, humps, 'numpy' ) z = np.linspace ( 0.0, 2.0, 5 ) humps_lambda ( z ) print ( '' ) print ( ' Evaluate the numpy function at equally spaced points in [0,2].' ) print ( ' humps(z) = ', humps_lambda ( z ) ) return if ( __name__ == "__main__" ): sympy_evaluate ( )