STEM 隨筆︰古典力學︰運動學【六‧中】

已經拉格朗日方法談過了Kane’s method ,本想著簡寫輕描 n-鏈擺筆記。但思有助於學習者自我審查,且有許多作者筆有未到之處 ,故此去掉大半說明格子 cell ,希望讀者專心 numpy 、 scipy 、 sympy … 等等程式庫基本實務功夫也!

 

特別點出 sympy

符號運算通往快速數值計算

之 API ︰

sympy.utilities.lambdify.lambdify(args, expr, modules=None, printer=None, use_imps=True, dummify=True)

Returns a lambda function for fast calculation of numerical values.

If not specified differently by the user, modules defaults to ["numpy"] if NumPy is installed, and ["math", "mpmath", "sympy"] if it isn’t, that is, SymPy functions are replaced as far as possible by eithernumpy functions if available, and Python’s standard library math, or mpmath functions otherwise. To change this behavior, the “modules” argument can be used. It accepts:

  • the strings “math”, “mpmath”, “numpy”, “numexpr”, “sympy”, “tensorflow”
  • any modules (e.g. math)
  • dictionaries that map names of sympy functions to arbitrary functions
  • lists that contain a mix of the arguments above, with higher priority given to entries appearing first.

Warning

Note that this function uses eval, and thus shouldn’t be used on unsanitized input.

The default behavior is to substitute all arguments in the provided expression with dummy symbols. This allows for applied functions (e.g. f(t)) to be supplied as arguments. Call the function with dummify=False if dummy substitution is unwanted (and argsargs is not a string). If you want to view the lambdified function or provide “sympy” as the module, you should probably set dummify=False.

For functions involving large array calculations, numexpr can provide a significant speedup over numpy. Please note that the available functions for numexpr are more limited than numpy but can be expanded with implemented_function and user defined subclasses of Function. If specified, numexpr may be the only option in modules. The official list of numexpr functions can be found at: https://github.com/pydata/numexpr#supported-functions

In previous releases lambdify replaced Matrix with numpy.matrix by default. As of release 1.0 numpy.array is the default. To get the old default behavior you must pass in [{'ImmutableDenseMatrix':numpy.matrix}, 'numpy'] to the modules kwarg.

 

用者自己可實證的呦◎

22-lambdify.ipynb