lambdify.py 56 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592
  1. """
  2. This module provides convenient functions to transform SymPy expressions to
  3. lambda functions which can be used to calculate numerical values very fast.
  4. """
  5. from __future__ import annotations
  6. from typing import Any
  7. import builtins
  8. import inspect
  9. import keyword
  10. import textwrap
  11. import linecache
  12. import weakref
  13. # Required despite static analysis claiming it is not used
  14. from sympy.external import import_module # noqa:F401
  15. from sympy.utilities.exceptions import sympy_deprecation_warning
  16. from sympy.utilities.decorator import doctest_depends_on
  17. from sympy.utilities.iterables import (is_sequence, iterable,
  18. NotIterable, flatten)
  19. from sympy.utilities.misc import filldedent
  20. __doctest_requires__ = {('lambdify',): ['numpy', 'tensorflow']}
  21. # Default namespaces, letting us define translations that can't be defined
  22. # by simple variable maps, like I => 1j
  23. MATH_DEFAULT: dict[str, Any] = {}
  24. CMATH_DEFAULT: dict[str,Any] = {}
  25. MPMATH_DEFAULT: dict[str, Any] = {}
  26. NUMPY_DEFAULT: dict[str, Any] = {"I": 1j}
  27. SCIPY_DEFAULT: dict[str, Any] = {"I": 1j}
  28. CUPY_DEFAULT: dict[str, Any] = {"I": 1j}
  29. JAX_DEFAULT: dict[str, Any] = {"I": 1j}
  30. TENSORFLOW_DEFAULT: dict[str, Any] = {}
  31. TORCH_DEFAULT: dict[str, Any] = {"I": 1j}
  32. SYMPY_DEFAULT: dict[str, Any] = {}
  33. NUMEXPR_DEFAULT: dict[str, Any] = {}
  34. # These are the namespaces the lambda functions will use.
  35. # These are separate from the names above because they are modified
  36. # throughout this file, whereas the defaults should remain unmodified.
  37. MATH = MATH_DEFAULT.copy()
  38. CMATH = CMATH_DEFAULT.copy()
  39. MPMATH = MPMATH_DEFAULT.copy()
  40. NUMPY = NUMPY_DEFAULT.copy()
  41. SCIPY = SCIPY_DEFAULT.copy()
  42. CUPY = CUPY_DEFAULT.copy()
  43. JAX = JAX_DEFAULT.copy()
  44. TENSORFLOW = TENSORFLOW_DEFAULT.copy()
  45. TORCH = TORCH_DEFAULT.copy()
  46. SYMPY = SYMPY_DEFAULT.copy()
  47. NUMEXPR = NUMEXPR_DEFAULT.copy()
  48. # Mappings between SymPy and other modules function names.
  49. MATH_TRANSLATIONS = {
  50. "ceiling": "ceil",
  51. "E": "e",
  52. "ln": "log",
  53. }
  54. CMATH_TRANSLATIONS: dict[str, str] = {}
  55. # NOTE: This dictionary is reused in Function._eval_evalf to allow subclasses
  56. # of Function to automatically evalf.
  57. MPMATH_TRANSLATIONS = {
  58. "Abs": "fabs",
  59. "elliptic_k": "ellipk",
  60. "elliptic_f": "ellipf",
  61. "elliptic_e": "ellipe",
  62. "elliptic_pi": "ellippi",
  63. "ceiling": "ceil",
  64. "chebyshevt": "chebyt",
  65. "chebyshevu": "chebyu",
  66. "assoc_legendre": "legenp",
  67. "E": "e",
  68. "I": "j",
  69. "ln": "log",
  70. #"lowergamma":"lower_gamma",
  71. "oo": "inf",
  72. #"uppergamma":"upper_gamma",
  73. "LambertW": "lambertw",
  74. "MutableDenseMatrix": "matrix",
  75. "ImmutableDenseMatrix": "matrix",
  76. "conjugate": "conj",
  77. "dirichlet_eta": "altzeta",
  78. "Ei": "ei",
  79. "Shi": "shi",
  80. "Chi": "chi",
  81. "Si": "si",
  82. "Ci": "ci",
  83. "RisingFactorial": "rf",
  84. "FallingFactorial": "ff",
  85. "betainc_regularized": "betainc",
  86. }
  87. NUMPY_TRANSLATIONS: dict[str, str] = {
  88. "Heaviside": "heaviside",
  89. }
  90. SCIPY_TRANSLATIONS: dict[str, str] = {
  91. "jn" : "spherical_jn",
  92. "yn" : "spherical_yn"
  93. }
  94. CUPY_TRANSLATIONS: dict[str, str] = {}
  95. JAX_TRANSLATIONS: dict[str, str] = {}
  96. TENSORFLOW_TRANSLATIONS: dict[str, str] = {}
  97. TORCH_TRANSLATIONS: dict[str, str] = {}
  98. NUMEXPR_TRANSLATIONS: dict[str, str] = {}
  99. # Available modules:
  100. MODULES = {
  101. "math": (MATH, MATH_DEFAULT, MATH_TRANSLATIONS, ("from math import *",)),
  102. "cmath": (CMATH, CMATH_DEFAULT, CMATH_TRANSLATIONS, ("import cmath; from cmath import *",)),
  103. "mpmath": (MPMATH, MPMATH_DEFAULT, MPMATH_TRANSLATIONS, ("from mpmath import *",)),
  104. "numpy": (NUMPY, NUMPY_DEFAULT, NUMPY_TRANSLATIONS, ("import numpy; from numpy import *; from numpy.linalg import *",)),
  105. "scipy": (SCIPY, SCIPY_DEFAULT, SCIPY_TRANSLATIONS, ("import scipy; import numpy; from scipy.special import *",)),
  106. "cupy": (CUPY, CUPY_DEFAULT, CUPY_TRANSLATIONS, ("import cupy",)),
  107. "jax": (JAX, JAX_DEFAULT, JAX_TRANSLATIONS, ("import jax",)),
  108. "tensorflow": (TENSORFLOW, TENSORFLOW_DEFAULT, TENSORFLOW_TRANSLATIONS, ("import tensorflow",)),
  109. "torch": (TORCH, TORCH_DEFAULT, TORCH_TRANSLATIONS, ("import torch",)),
  110. "sympy": (SYMPY, SYMPY_DEFAULT, {}, (
  111. "from sympy.functions import *",
  112. "from sympy.matrices import *",
  113. "from sympy import Integral, pi, oo, nan, zoo, E, I",)),
  114. "numexpr" : (NUMEXPR, NUMEXPR_DEFAULT, NUMEXPR_TRANSLATIONS,
  115. ("import_module('numexpr')", )),
  116. }
  117. def _import(module, reload=False):
  118. """
  119. Creates a global translation dictionary for module.
  120. The argument module has to be one of the following strings: "math","cmath"
  121. "mpmath", "numpy", "sympy", "tensorflow", "jax".
  122. These dictionaries map names of Python functions to their equivalent in
  123. other modules.
  124. """
  125. try:
  126. namespace, namespace_default, translations, import_commands = MODULES[
  127. module]
  128. except KeyError:
  129. raise NameError(
  130. "'%s' module cannot be used for lambdification" % module)
  131. # Clear namespace or exit
  132. if namespace != namespace_default:
  133. # The namespace was already generated, don't do it again if not forced.
  134. if reload:
  135. namespace.clear()
  136. namespace.update(namespace_default)
  137. else:
  138. return
  139. for import_command in import_commands:
  140. if import_command.startswith('import_module'):
  141. module = eval(import_command)
  142. if module is not None:
  143. namespace.update(module.__dict__)
  144. continue
  145. else:
  146. try:
  147. exec(import_command, {}, namespace)
  148. continue
  149. except ImportError:
  150. pass
  151. raise ImportError(
  152. "Cannot import '%s' with '%s' command" % (module, import_command))
  153. # Add translated names to namespace
  154. for sympyname, translation in translations.items():
  155. namespace[sympyname] = namespace[translation]
  156. # For computing the modulus of a SymPy expression we use the builtin abs
  157. # function, instead of the previously used fabs function for all
  158. # translation modules. This is because the fabs function in the math
  159. # module does not accept complex valued arguments. (see issue 9474). The
  160. # only exception, where we don't use the builtin abs function is the
  161. # mpmath translation module, because mpmath.fabs returns mpf objects in
  162. # contrast to abs().
  163. if 'Abs' not in namespace:
  164. namespace['Abs'] = abs
  165. # Used for dynamically generated filenames that are inserted into the
  166. # linecache.
  167. _lambdify_generated_counter = 1
  168. @doctest_depends_on(modules=('numpy', 'scipy', 'tensorflow',), python_version=(3,))
  169. def lambdify(args, expr, modules=None, printer=None, use_imps=True,
  170. dummify=False, cse=False, docstring_limit=1000):
  171. """Convert a SymPy expression into a function that allows for fast
  172. numeric evaluation.
  173. .. warning::
  174. This function uses ``exec``, and thus should not be used on
  175. unsanitized input.
  176. .. deprecated:: 1.7
  177. Passing a set for the *args* parameter is deprecated as sets are
  178. unordered. Use an ordered iterable such as a list or tuple.
  179. Explanation
  180. ===========
  181. For example, to convert the SymPy expression ``sin(x) + cos(x)`` to an
  182. equivalent NumPy function that numerically evaluates it:
  183. >>> from sympy import sin, cos, symbols, lambdify
  184. >>> import numpy as np
  185. >>> x = symbols('x')
  186. >>> expr = sin(x) + cos(x)
  187. >>> expr
  188. sin(x) + cos(x)
  189. >>> f = lambdify(x, expr, 'numpy')
  190. >>> a = np.array([1, 2])
  191. >>> f(a)
  192. [1.38177329 0.49315059]
  193. The primary purpose of this function is to provide a bridge from SymPy
  194. expressions to numerical libraries such as NumPy, SciPy, NumExpr, mpmath,
  195. and tensorflow. In general, SymPy functions do not work with objects from
  196. other libraries, such as NumPy arrays, and functions from numeric
  197. libraries like NumPy or mpmath do not work on SymPy expressions.
  198. ``lambdify`` bridges the two by converting a SymPy expression to an
  199. equivalent numeric function.
  200. The basic workflow with ``lambdify`` is to first create a SymPy expression
  201. representing whatever mathematical function you wish to evaluate. This
  202. should be done using only SymPy functions and expressions. Then, use
  203. ``lambdify`` to convert this to an equivalent function for numerical
  204. evaluation. For instance, above we created ``expr`` using the SymPy symbol
  205. ``x`` and SymPy functions ``sin`` and ``cos``, then converted it to an
  206. equivalent NumPy function ``f``, and called it on a NumPy array ``a``.
  207. Parameters
  208. ==========
  209. args : List[Symbol]
  210. A variable or a list of variables whose nesting represents the
  211. nesting of the arguments that will be passed to the function.
  212. Variables can be symbols, undefined functions, or matrix symbols.
  213. >>> from sympy import Eq
  214. >>> from sympy.abc import x, y, z
  215. The list of variables should match the structure of how the
  216. arguments will be passed to the function. Simply enclose the
  217. parameters as they will be passed in a list.
  218. To call a function like ``f(x)`` then ``[x]``
  219. should be the first argument to ``lambdify``; for this
  220. case a single ``x`` can also be used:
  221. >>> f = lambdify(x, x + 1)
  222. >>> f(1)
  223. 2
  224. >>> f = lambdify([x], x + 1)
  225. >>> f(1)
  226. 2
  227. To call a function like ``f(x, y)`` then ``[x, y]`` will
  228. be the first argument of the ``lambdify``:
  229. >>> f = lambdify([x, y], x + y)
  230. >>> f(1, 1)
  231. 2
  232. To call a function with a single 3-element tuple like
  233. ``f((x, y, z))`` then ``[(x, y, z)]`` will be the first
  234. argument of the ``lambdify``:
  235. >>> f = lambdify([(x, y, z)], Eq(z**2, x**2 + y**2))
  236. >>> f((3, 4, 5))
  237. True
  238. If two args will be passed and the first is a scalar but
  239. the second is a tuple with two arguments then the items
  240. in the list should match that structure:
  241. >>> f = lambdify([x, (y, z)], x + y + z)
  242. >>> f(1, (2, 3))
  243. 6
  244. expr : Expr
  245. An expression, list of expressions, or matrix to be evaluated.
  246. Lists may be nested.
  247. If the expression is a list, the output will also be a list.
  248. >>> f = lambdify(x, [x, [x + 1, x + 2]])
  249. >>> f(1)
  250. [1, [2, 3]]
  251. If it is a matrix, an array will be returned (for the NumPy module).
  252. >>> from sympy import Matrix
  253. >>> f = lambdify(x, Matrix([x, x + 1]))
  254. >>> f(1)
  255. [[1]
  256. [2]]
  257. Note that the argument order here (variables then expression) is used
  258. to emulate the Python ``lambda`` keyword. ``lambdify(x, expr)`` works
  259. (roughly) like ``lambda x: expr``
  260. (see :ref:`lambdify-how-it-works` below).
  261. modules : str, optional
  262. Specifies the numeric library to use.
  263. If not specified, *modules* defaults to:
  264. - ``["scipy", "numpy"]`` if SciPy is installed
  265. - ``["numpy"]`` if only NumPy is installed
  266. - ``["math","cmath", "mpmath", "sympy"]`` if neither is installed.
  267. That is, SymPy functions are replaced as far as possible by
  268. either ``scipy`` or ``numpy`` functions if available, and Python's
  269. standard library ``math`` and ``cmath``, or ``mpmath`` functions otherwise.
  270. *modules* can be one of the following types:
  271. - The strings ``"math"``, ``"cmath"``, ``"mpmath"``, ``"numpy"``, ``"numexpr"``,
  272. ``"scipy"``, ``"sympy"``, or ``"tensorflow"`` or ``"jax"``. This uses the
  273. corresponding printer and namespace mapping for that module.
  274. - A module (e.g., ``math``). This uses the global namespace of the
  275. module. If the module is one of the above known modules, it will
  276. also use the corresponding printer and namespace mapping
  277. (i.e., ``modules=numpy`` is equivalent to ``modules="numpy"``).
  278. - A dictionary that maps names of SymPy functions to arbitrary
  279. functions
  280. (e.g., ``{'sin': custom_sin}``).
  281. - A list that contains a mix of the arguments above, with higher
  282. priority given to entries appearing first
  283. (e.g., to use the NumPy module but override the ``sin`` function
  284. with a custom version, you can use
  285. ``[{'sin': custom_sin}, 'numpy']``).
  286. dummify : bool, optional
  287. Whether or not the variables in the provided expression that are not
  288. valid Python identifiers are substituted with dummy symbols.
  289. This allows for undefined functions like ``Function('f')(t)`` to be
  290. supplied as arguments. By default, the variables are only dummified
  291. if they are not valid Python identifiers.
  292. Set ``dummify=True`` to replace all arguments with dummy symbols
  293. (if ``args`` is not a string) - for example, to ensure that the
  294. arguments do not redefine any built-in names.
  295. cse : bool, or callable, optional
  296. Large expressions can be computed more efficiently when
  297. common subexpressions are identified and precomputed before
  298. being used multiple time. Finding the subexpressions will make
  299. creation of the 'lambdify' function slower, however.
  300. When ``True``, ``sympy.simplify.cse`` is used, otherwise (the default)
  301. the user may pass a function matching the ``cse`` signature.
  302. docstring_limit : int or None
  303. When lambdifying large expressions, a significant proportion of the time
  304. spent inside ``lambdify`` is spent producing a string representation of
  305. the expression for use in the automatically generated docstring of the
  306. returned function. For expressions containing hundreds or more nodes the
  307. resulting docstring often becomes so long and dense that it is difficult
  308. to read. To reduce the runtime of lambdify, the rendering of the full
  309. expression inside the docstring can be disabled.
  310. When ``None``, the full expression is rendered in the docstring. When
  311. ``0`` or a negative ``int``, an ellipsis is rendering in the docstring
  312. instead of the expression. When a strictly positive ``int``, if the
  313. number of nodes in the expression exceeds ``docstring_limit`` an
  314. ellipsis is rendered in the docstring, otherwise a string representation
  315. of the expression is rendered as normal. The default is ``1000``.
  316. Examples
  317. ========
  318. >>> from sympy.utilities.lambdify import implemented_function
  319. >>> from sympy import sqrt, sin, Matrix
  320. >>> from sympy import Function
  321. >>> from sympy.abc import w, x, y, z
  322. >>> f = lambdify(x, x**2)
  323. >>> f(2)
  324. 4
  325. >>> f = lambdify((x, y, z), [z, y, x])
  326. >>> f(1,2,3)
  327. [3, 2, 1]
  328. >>> f = lambdify(x, sqrt(x))
  329. >>> f(4)
  330. 2.0
  331. >>> f = lambdify((x, y), sin(x*y)**2)
  332. >>> f(0, 5)
  333. 0.0
  334. >>> row = lambdify((x, y), Matrix((x, x + y)).T, modules='sympy')
  335. >>> row(1, 2)
  336. Matrix([[1, 3]])
  337. ``lambdify`` can be used to translate SymPy expressions into mpmath
  338. functions. This may be preferable to using ``evalf`` (which uses mpmath on
  339. the backend) in some cases.
  340. >>> f = lambdify(x, sin(x), 'mpmath')
  341. >>> f(1)
  342. 0.8414709848078965
  343. Tuple arguments are handled and the lambdified function should
  344. be called with the same type of arguments as were used to create
  345. the function:
  346. >>> f = lambdify((x, (y, z)), x + y)
  347. >>> f(1, (2, 4))
  348. 3
  349. The ``flatten`` function can be used to always work with flattened
  350. arguments:
  351. >>> from sympy.utilities.iterables import flatten
  352. >>> args = w, (x, (y, z))
  353. >>> vals = 1, (2, (3, 4))
  354. >>> f = lambdify(flatten(args), w + x + y + z)
  355. >>> f(*flatten(vals))
  356. 10
  357. Functions present in ``expr`` can also carry their own numerical
  358. implementations, in a callable attached to the ``_imp_`` attribute. This
  359. can be used with undefined functions using the ``implemented_function``
  360. factory:
  361. >>> f = implemented_function(Function('f'), lambda x: x+1)
  362. >>> func = lambdify(x, f(x))
  363. >>> func(4)
  364. 5
  365. ``lambdify`` always prefers ``_imp_`` implementations to implementations
  366. in other namespaces, unless the ``use_imps`` input parameter is False.
  367. Usage with Tensorflow:
  368. >>> import tensorflow as tf
  369. >>> from sympy import Max, sin, lambdify
  370. >>> from sympy.abc import x
  371. >>> f = Max(x, sin(x))
  372. >>> func = lambdify(x, f, 'tensorflow')
  373. After tensorflow v2, eager execution is enabled by default.
  374. If you want to get the compatible result across tensorflow v1 and v2
  375. as same as this tutorial, run this line.
  376. >>> tf.compat.v1.enable_eager_execution()
  377. If you have eager execution enabled, you can get the result out
  378. immediately as you can use numpy.
  379. If you pass tensorflow objects, you may get an ``EagerTensor``
  380. object instead of value.
  381. >>> result = func(tf.constant(1.0))
  382. >>> print(result)
  383. tf.Tensor(1.0, shape=(), dtype=float32)
  384. >>> print(result.__class__)
  385. <class 'tensorflow.python.framework.ops.EagerTensor'>
  386. You can use ``.numpy()`` to get the numpy value of the tensor.
  387. >>> result.numpy()
  388. 1.0
  389. >>> var = tf.Variable(2.0)
  390. >>> result = func(var) # also works for tf.Variable and tf.Placeholder
  391. >>> result.numpy()
  392. 2.0
  393. And it works with any shape array.
  394. >>> tensor = tf.constant([[1.0, 2.0], [3.0, 4.0]])
  395. >>> result = func(tensor)
  396. >>> result.numpy()
  397. [[1. 2.]
  398. [3. 4.]]
  399. Notes
  400. =====
  401. - For functions involving large array calculations, numexpr can provide a
  402. significant speedup over numpy. Please note that the available functions
  403. for numexpr are more limited than numpy but can be expanded with
  404. ``implemented_function`` and user defined subclasses of Function. If
  405. specified, numexpr may be the only option in modules. The official list
  406. of numexpr functions can be found at:
  407. https://numexpr.readthedocs.io/en/latest/user_guide.html#supported-functions
  408. - In the above examples, the generated functions can accept scalar
  409. values or numpy arrays as arguments. However, in some cases
  410. the generated function relies on the input being a numpy array:
  411. >>> import numpy
  412. >>> from sympy import Piecewise
  413. >>> from sympy.testing.pytest import ignore_warnings
  414. >>> f = lambdify(x, Piecewise((x, x <= 1), (1/x, x > 1)), "numpy")
  415. >>> with ignore_warnings(RuntimeWarning):
  416. ... f(numpy.array([-1, 0, 1, 2]))
  417. [-1. 0. 1. 0.5]
  418. >>> f(0)
  419. Traceback (most recent call last):
  420. ...
  421. ZeroDivisionError: division by zero
  422. In such cases, the input should be wrapped in a numpy array:
  423. >>> with ignore_warnings(RuntimeWarning):
  424. ... float(f(numpy.array([0])))
  425. 0.0
  426. Or if numpy functionality is not required another module can be used:
  427. >>> f = lambdify(x, Piecewise((x, x <= 1), (1/x, x > 1)), "math")
  428. >>> f(0)
  429. 0
  430. .. _lambdify-how-it-works:
  431. How it works
  432. ============
  433. When using this function, it helps a great deal to have an idea of what it
  434. is doing. At its core, lambdify is nothing more than a namespace
  435. translation, on top of a special printer that makes some corner cases work
  436. properly.
  437. To understand lambdify, first we must properly understand how Python
  438. namespaces work. Say we had two files. One called ``sin_cos_sympy.py``,
  439. with
  440. .. code:: python
  441. # sin_cos_sympy.py
  442. from sympy.functions.elementary.trigonometric import (cos, sin)
  443. def sin_cos(x):
  444. return sin(x) + cos(x)
  445. and one called ``sin_cos_numpy.py`` with
  446. .. code:: python
  447. # sin_cos_numpy.py
  448. from numpy import sin, cos
  449. def sin_cos(x):
  450. return sin(x) + cos(x)
  451. The two files define an identical function ``sin_cos``. However, in the
  452. first file, ``sin`` and ``cos`` are defined as the SymPy ``sin`` and
  453. ``cos``. In the second, they are defined as the NumPy versions.
  454. If we were to import the first file and use the ``sin_cos`` function, we
  455. would get something like
  456. >>> from sin_cos_sympy import sin_cos # doctest: +SKIP
  457. >>> sin_cos(1) # doctest: +SKIP
  458. cos(1) + sin(1)
  459. On the other hand, if we imported ``sin_cos`` from the second file, we
  460. would get
  461. >>> from sin_cos_numpy import sin_cos # doctest: +SKIP
  462. >>> sin_cos(1) # doctest: +SKIP
  463. 1.38177329068
  464. In the first case we got a symbolic output, because it used the symbolic
  465. ``sin`` and ``cos`` functions from SymPy. In the second, we got a numeric
  466. result, because ``sin_cos`` used the numeric ``sin`` and ``cos`` functions
  467. from NumPy. But notice that the versions of ``sin`` and ``cos`` that were
  468. used was not inherent to the ``sin_cos`` function definition. Both
  469. ``sin_cos`` definitions are exactly the same. Rather, it was based on the
  470. names defined at the module where the ``sin_cos`` function was defined.
  471. The key point here is that when function in Python references a name that
  472. is not defined in the function, that name is looked up in the "global"
  473. namespace of the module where that function is defined.
  474. Now, in Python, we can emulate this behavior without actually writing a
  475. file to disk using the ``exec`` function. ``exec`` takes a string
  476. containing a block of Python code, and a dictionary that should contain
  477. the global variables of the module. It then executes the code "in" that
  478. dictionary, as if it were the module globals. The following is equivalent
  479. to the ``sin_cos`` defined in ``sin_cos_sympy.py``:
  480. >>> import sympy
  481. >>> module_dictionary = {'sin': sympy.sin, 'cos': sympy.cos}
  482. >>> exec('''
  483. ... def sin_cos(x):
  484. ... return sin(x) + cos(x)
  485. ... ''', module_dictionary)
  486. >>> sin_cos = module_dictionary['sin_cos']
  487. >>> sin_cos(1)
  488. cos(1) + sin(1)
  489. and similarly with ``sin_cos_numpy``:
  490. >>> import numpy
  491. >>> module_dictionary = {'sin': numpy.sin, 'cos': numpy.cos}
  492. >>> exec('''
  493. ... def sin_cos(x):
  494. ... return sin(x) + cos(x)
  495. ... ''', module_dictionary)
  496. >>> sin_cos = module_dictionary['sin_cos']
  497. >>> sin_cos(1)
  498. 1.38177329068
  499. So now we can get an idea of how ``lambdify`` works. The name "lambdify"
  500. comes from the fact that we can think of something like ``lambdify(x,
  501. sin(x) + cos(x), 'numpy')`` as ``lambda x: sin(x) + cos(x)``, where
  502. ``sin`` and ``cos`` come from the ``numpy`` namespace. This is also why
  503. the symbols argument is first in ``lambdify``, as opposed to most SymPy
  504. functions where it comes after the expression: to better mimic the
  505. ``lambda`` keyword.
  506. ``lambdify`` takes the input expression (like ``sin(x) + cos(x)``) and
  507. 1. Converts it to a string
  508. 2. Creates a module globals dictionary based on the modules that are
  509. passed in (by default, it uses the NumPy module)
  510. 3. Creates the string ``"def func({vars}): return {expr}"``, where ``{vars}`` is the
  511. list of variables separated by commas, and ``{expr}`` is the string
  512. created in step 1., then ``exec``s that string with the module globals
  513. namespace and returns ``func``.
  514. In fact, functions returned by ``lambdify`` support inspection. So you can
  515. see exactly how they are defined by using ``inspect.getsource``, or ``??`` if you
  516. are using IPython or the Jupyter notebook.
  517. >>> f = lambdify(x, sin(x) + cos(x))
  518. >>> import inspect
  519. >>> print(inspect.getsource(f))
  520. def _lambdifygenerated(x):
  521. return sin(x) + cos(x)
  522. This shows us the source code of the function, but not the namespace it
  523. was defined in. We can inspect that by looking at the ``__globals__``
  524. attribute of ``f``:
  525. >>> f.__globals__['sin']
  526. <ufunc 'sin'>
  527. >>> f.__globals__['cos']
  528. <ufunc 'cos'>
  529. >>> f.__globals__['sin'] is numpy.sin
  530. True
  531. This shows us that ``sin`` and ``cos`` in the namespace of ``f`` will be
  532. ``numpy.sin`` and ``numpy.cos``.
  533. Note that there are some convenience layers in each of these steps, but at
  534. the core, this is how ``lambdify`` works. Step 1 is done using the
  535. ``LambdaPrinter`` printers defined in the printing module (see
  536. :mod:`sympy.printing.lambdarepr`). This allows different SymPy expressions
  537. to define how they should be converted to a string for different modules.
  538. You can change which printer ``lambdify`` uses by passing a custom printer
  539. in to the ``printer`` argument.
  540. Step 2 is augmented by certain translations. There are default
  541. translations for each module, but you can provide your own by passing a
  542. list to the ``modules`` argument. For instance,
  543. >>> def mysin(x):
  544. ... print('taking the sin of', x)
  545. ... return numpy.sin(x)
  546. ...
  547. >>> f = lambdify(x, sin(x), [{'sin': mysin}, 'numpy'])
  548. >>> f(1)
  549. taking the sin of 1
  550. 0.8414709848078965
  551. The globals dictionary is generated from the list by merging the
  552. dictionary ``{'sin': mysin}`` and the module dictionary for NumPy. The
  553. merging is done so that earlier items take precedence, which is why
  554. ``mysin`` is used above instead of ``numpy.sin``.
  555. If you want to modify the way ``lambdify`` works for a given function, it
  556. is usually easiest to do so by modifying the globals dictionary as such.
  557. In more complicated cases, it may be necessary to create and pass in a
  558. custom printer.
  559. Finally, step 3 is augmented with certain convenience operations, such as
  560. the addition of a docstring.
  561. Understanding how ``lambdify`` works can make it easier to avoid certain
  562. gotchas when using it. For instance, a common mistake is to create a
  563. lambdified function for one module (say, NumPy), and pass it objects from
  564. another (say, a SymPy expression).
  565. For instance, say we create
  566. >>> from sympy.abc import x
  567. >>> f = lambdify(x, x + 1, 'numpy')
  568. Now if we pass in a NumPy array, we get that array plus 1
  569. >>> import numpy
  570. >>> a = numpy.array([1, 2])
  571. >>> f(a)
  572. [2 3]
  573. But what happens if you make the mistake of passing in a SymPy expression
  574. instead of a NumPy array:
  575. >>> f(x + 1)
  576. x + 2
  577. This worked, but it was only by accident. Now take a different lambdified
  578. function:
  579. >>> from sympy import sin
  580. >>> g = lambdify(x, x + sin(x), 'numpy')
  581. This works as expected on NumPy arrays:
  582. >>> g(a)
  583. [1.84147098 2.90929743]
  584. But if we try to pass in a SymPy expression, it fails
  585. >>> g(x + 1)
  586. Traceback (most recent call last):
  587. ...
  588. TypeError: loop of ufunc does not support argument 0 of type Add which has
  589. no callable sin method
  590. Now, let's look at what happened. The reason this fails is that ``g``
  591. calls ``numpy.sin`` on the input expression, and ``numpy.sin`` does not
  592. know how to operate on a SymPy object. **As a general rule, NumPy
  593. functions do not know how to operate on SymPy expressions, and SymPy
  594. functions do not know how to operate on NumPy arrays. This is why lambdify
  595. exists: to provide a bridge between SymPy and NumPy.**
  596. However, why is it that ``f`` did work? That's because ``f`` does not call
  597. any functions, it only adds 1. So the resulting function that is created,
  598. ``def _lambdifygenerated(x): return x + 1`` does not depend on the globals
  599. namespace it is defined in. Thus it works, but only by accident. A future
  600. version of ``lambdify`` may remove this behavior.
  601. Be aware that certain implementation details described here may change in
  602. future versions of SymPy. The API of passing in custom modules and
  603. printers will not change, but the details of how a lambda function is
  604. created may change. However, the basic idea will remain the same, and
  605. understanding it will be helpful to understanding the behavior of
  606. lambdify.
  607. **In general: you should create lambdified functions for one module (say,
  608. NumPy), and only pass it input types that are compatible with that module
  609. (say, NumPy arrays).** Remember that by default, if the ``module``
  610. argument is not provided, ``lambdify`` creates functions using the NumPy
  611. and SciPy namespaces.
  612. """
  613. from sympy.core.symbol import Symbol
  614. from sympy.core.expr import Expr
  615. # If the user hasn't specified any modules, use what is available.
  616. if modules is None:
  617. try:
  618. _import("scipy")
  619. except ImportError:
  620. try:
  621. _import("numpy")
  622. except ImportError:
  623. # Use either numpy (if available) or python.math where possible.
  624. # XXX: This leads to different behaviour on different systems and
  625. # might be the reason for irreproducible errors.
  626. modules = ["math", "mpmath", "sympy"]
  627. else:
  628. modules = ["numpy"]
  629. else:
  630. modules = ["numpy", "scipy"]
  631. # Get the needed namespaces.
  632. namespaces = []
  633. # First find any function implementations
  634. if use_imps:
  635. namespaces.append(_imp_namespace(expr))
  636. # Check for dict before iterating
  637. if isinstance(modules, (dict, str)) or not hasattr(modules, '__iter__'):
  638. namespaces.append(modules)
  639. else:
  640. # consistency check
  641. if _module_present('numexpr', modules) and len(modules) > 1:
  642. raise TypeError("numexpr must be the only item in 'modules'")
  643. namespaces += list(modules)
  644. # fill namespace with first having highest priority
  645. namespace = {}
  646. for m in namespaces[::-1]:
  647. buf = _get_namespace(m)
  648. namespace.update(buf)
  649. if hasattr(expr, "atoms"):
  650. #Try if you can extract symbols from the expression.
  651. #Move on if expr.atoms in not implemented.
  652. syms = expr.atoms(Symbol)
  653. for term in syms:
  654. namespace.update({str(term): term})
  655. if printer is None:
  656. if _module_present('mpmath', namespaces):
  657. from sympy.printing.pycode import MpmathPrinter as Printer # type: ignore
  658. elif _module_present('scipy', namespaces):
  659. from sympy.printing.numpy import SciPyPrinter as Printer # type: ignore
  660. elif _module_present('numpy', namespaces):
  661. from sympy.printing.numpy import NumPyPrinter as Printer # type: ignore
  662. elif _module_present('cupy', namespaces):
  663. from sympy.printing.numpy import CuPyPrinter as Printer # type: ignore
  664. elif _module_present('jax', namespaces):
  665. from sympy.printing.numpy import JaxPrinter as Printer # type: ignore
  666. elif _module_present('numexpr', namespaces):
  667. from sympy.printing.lambdarepr import NumExprPrinter as Printer # type: ignore
  668. elif _module_present('tensorflow', namespaces):
  669. from sympy.printing.tensorflow import TensorflowPrinter as Printer # type: ignore
  670. elif _module_present('torch', namespaces):
  671. from sympy.printing.pytorch import TorchPrinter as Printer # type: ignore
  672. elif _module_present('sympy', namespaces):
  673. from sympy.printing.pycode import SymPyPrinter as Printer # type: ignore
  674. elif _module_present('cmath', namespaces):
  675. from sympy.printing.pycode import CmathPrinter as Printer # type: ignore
  676. else:
  677. from sympy.printing.pycode import PythonCodePrinter as Printer # type: ignore
  678. user_functions = {}
  679. for m in namespaces[::-1]:
  680. if isinstance(m, dict):
  681. for k in m:
  682. user_functions[k] = k
  683. printer = Printer({'fully_qualified_modules': False, 'inline': True,
  684. 'allow_unknown_functions': True,
  685. 'user_functions': user_functions})
  686. if isinstance(args, set):
  687. sympy_deprecation_warning(
  688. """
  689. Passing the function arguments to lambdify() as a set is deprecated. This
  690. leads to unpredictable results since sets are unordered. Instead, use a list
  691. or tuple for the function arguments.
  692. """,
  693. deprecated_since_version="1.6.3",
  694. active_deprecations_target="deprecated-lambdify-arguments-set",
  695. )
  696. # Get the names of the args, for creating a docstring
  697. iterable_args = (args,) if isinstance(args, Expr) else args
  698. names = []
  699. # Grab the callers frame, for getting the names by inspection (if needed)
  700. callers_local_vars = inspect.currentframe().f_back.f_locals.items() # type: ignore
  701. for n, var in enumerate(iterable_args):
  702. if hasattr(var, 'name'):
  703. names.append(var.name)
  704. else:
  705. # It's an iterable. Try to get name by inspection of calling frame.
  706. name_list = [var_name for var_name, var_val in callers_local_vars
  707. if var_val is var]
  708. if len(name_list) == 1:
  709. names.append(name_list[0])
  710. else:
  711. # Cannot infer name with certainty. arg_# will have to do.
  712. names.append('arg_' + str(n))
  713. # Create the function definition code and execute it
  714. funcname = '_lambdifygenerated'
  715. if _module_present('tensorflow', namespaces):
  716. funcprinter = _TensorflowEvaluatorPrinter(printer, dummify)
  717. else:
  718. funcprinter = _EvaluatorPrinter(printer, dummify)
  719. if cse == True:
  720. from sympy.simplify.cse_main import cse as _cse
  721. cses, _expr = _cse(expr, list=False)
  722. elif callable(cse):
  723. cses, _expr = cse(expr)
  724. else:
  725. cses, _expr = (), expr
  726. funcstr = funcprinter.doprint(funcname, iterable_args, _expr, cses=cses)
  727. # Collect the module imports from the code printers.
  728. imp_mod_lines = []
  729. for mod, keys in (getattr(printer, 'module_imports', None) or {}).items():
  730. for k in keys:
  731. if k not in namespace:
  732. ln = "from %s import %s" % (mod, k)
  733. try:
  734. exec(ln, {}, namespace)
  735. except ImportError:
  736. # Tensorflow 2.0 has issues with importing a specific
  737. # function from its submodule.
  738. # https://github.com/tensorflow/tensorflow/issues/33022
  739. ln = "%s = %s.%s" % (k, mod, k)
  740. exec(ln, {}, namespace)
  741. imp_mod_lines.append(ln)
  742. # Provide lambda expression with builtins, and compatible implementation of range
  743. namespace.update({'builtins':builtins, 'range':range})
  744. funclocals = {}
  745. global _lambdify_generated_counter
  746. filename = '<lambdifygenerated-%s>' % _lambdify_generated_counter
  747. _lambdify_generated_counter += 1
  748. c = compile(funcstr, filename, 'exec')
  749. exec(c, namespace, funclocals)
  750. # mtime has to be None or else linecache.checkcache will remove it
  751. linecache.cache[filename] = (len(funcstr), None, funcstr.splitlines(True), filename) # type: ignore
  752. # Remove the entry from the linecache when the object is garbage collected
  753. def cleanup_linecache(filename):
  754. def _cleanup():
  755. if filename in linecache.cache:
  756. del linecache.cache[filename]
  757. return _cleanup
  758. func = funclocals[funcname]
  759. weakref.finalize(func, cleanup_linecache(filename))
  760. # Apply the docstring
  761. sig = "func({})".format(", ".join(str(i) for i in names))
  762. sig = textwrap.fill(sig, subsequent_indent=' '*8)
  763. if _too_large_for_docstring(expr, docstring_limit):
  764. expr_str = "EXPRESSION REDACTED DUE TO LENGTH, (see lambdify's `docstring_limit`)"
  765. src_str = "SOURCE CODE REDACTED DUE TO LENGTH, (see lambdify's `docstring_limit`)"
  766. else:
  767. expr_str = str(expr)
  768. if len(expr_str) > 78:
  769. expr_str = textwrap.wrap(expr_str, 75)[0] + '...'
  770. src_str = funcstr
  771. func.__doc__ = (
  772. "Created with lambdify. Signature:\n\n"
  773. "{sig}\n\n"
  774. "Expression:\n\n"
  775. "{expr}\n\n"
  776. "Source code:\n\n"
  777. "{src}\n\n"
  778. "Imported modules:\n\n"
  779. "{imp_mods}"
  780. ).format(sig=sig, expr=expr_str, src=src_str, imp_mods='\n'.join(imp_mod_lines))
  781. return func
  782. def _module_present(modname, modlist):
  783. if modname in modlist:
  784. return True
  785. for m in modlist:
  786. if hasattr(m, '__name__') and m.__name__ == modname:
  787. return True
  788. return False
  789. def _get_namespace(m):
  790. """
  791. This is used by _lambdify to parse its arguments.
  792. """
  793. if isinstance(m, str):
  794. _import(m)
  795. return MODULES[m][0]
  796. elif isinstance(m, dict):
  797. return m
  798. elif hasattr(m, "__dict__"):
  799. return m.__dict__
  800. else:
  801. raise TypeError("Argument must be either a string, dict or module but it is: %s" % m)
  802. def _recursive_to_string(doprint, arg):
  803. """Functions in lambdify accept both SymPy types and non-SymPy types such as python
  804. lists and tuples. This method ensures that we only call the doprint method of the
  805. printer with SymPy types (so that the printer safely can use SymPy-methods)."""
  806. from sympy.matrices.matrixbase import MatrixBase
  807. from sympy.core.basic import Basic
  808. if isinstance(arg, (Basic, MatrixBase)):
  809. return doprint(arg)
  810. elif iterable(arg):
  811. if isinstance(arg, list):
  812. left, right = "[", "]"
  813. elif isinstance(arg, tuple):
  814. left, right = "(", ",)"
  815. if not arg:
  816. return "()"
  817. else:
  818. raise NotImplementedError("unhandled type: %s, %s" % (type(arg), arg))
  819. return left +', '.join(_recursive_to_string(doprint, e) for e in arg) + right
  820. elif isinstance(arg, str):
  821. return arg
  822. else:
  823. return doprint(arg)
  824. def lambdastr(args, expr, printer=None, dummify=None):
  825. """
  826. Returns a string that can be evaluated to a lambda function.
  827. Examples
  828. ========
  829. >>> from sympy.abc import x, y, z
  830. >>> from sympy.utilities.lambdify import lambdastr
  831. >>> lambdastr(x, x**2)
  832. 'lambda x: (x**2)'
  833. >>> lambdastr((x,y,z), [z,y,x])
  834. 'lambda x,y,z: ([z, y, x])'
  835. Although tuples may not appear as arguments to lambda in Python 3,
  836. lambdastr will create a lambda function that will unpack the original
  837. arguments so that nested arguments can be handled:
  838. >>> lambdastr((x, (y, z)), x + y)
  839. 'lambda _0,_1: (lambda x,y,z: (x + y))(_0,_1[0],_1[1])'
  840. """
  841. # Transforming everything to strings.
  842. from sympy.matrices import DeferredVector
  843. from sympy.core.basic import Basic
  844. from sympy.core.function import (Derivative, Function)
  845. from sympy.core.symbol import (Dummy, Symbol)
  846. from sympy.core.sympify import sympify
  847. if printer is not None:
  848. if inspect.isfunction(printer):
  849. lambdarepr = printer
  850. else:
  851. if inspect.isclass(printer):
  852. lambdarepr = lambda expr: printer().doprint(expr)
  853. else:
  854. lambdarepr = lambda expr: printer.doprint(expr)
  855. else:
  856. #XXX: This has to be done here because of circular imports
  857. from sympy.printing.lambdarepr import lambdarepr
  858. def sub_args(args, dummies_dict):
  859. if isinstance(args, str):
  860. return args
  861. elif isinstance(args, DeferredVector):
  862. return str(args)
  863. elif iterable(args):
  864. dummies = flatten([sub_args(a, dummies_dict) for a in args])
  865. return ",".join(str(a) for a in dummies)
  866. else:
  867. # replace these with Dummy symbols
  868. if isinstance(args, (Function, Symbol, Derivative)):
  869. dummies = Dummy()
  870. dummies_dict.update({args : dummies})
  871. return str(dummies)
  872. else:
  873. return str(args)
  874. def sub_expr(expr, dummies_dict):
  875. expr = sympify(expr)
  876. # dict/tuple are sympified to Basic
  877. if isinstance(expr, Basic):
  878. expr = expr.xreplace(dummies_dict)
  879. # list is not sympified to Basic
  880. elif isinstance(expr, list):
  881. expr = [sub_expr(a, dummies_dict) for a in expr]
  882. return expr
  883. # Transform args
  884. def isiter(l):
  885. return iterable(l, exclude=(str, DeferredVector, NotIterable))
  886. def flat_indexes(iterable):
  887. n = 0
  888. for el in iterable:
  889. if isiter(el):
  890. for ndeep in flat_indexes(el):
  891. yield (n,) + ndeep
  892. else:
  893. yield (n,)
  894. n += 1
  895. if dummify is None:
  896. dummify = any(isinstance(a, Basic) and
  897. a.atoms(Function, Derivative) for a in (
  898. args if isiter(args) else [args]))
  899. if isiter(args) and any(isiter(i) for i in args):
  900. dum_args = [str(Dummy(str(i))) for i in range(len(args))]
  901. indexed_args = ','.join([
  902. dum_args[ind[0]] + ''.join(["[%s]" % k for k in ind[1:]])
  903. for ind in flat_indexes(args)])
  904. lstr = lambdastr(flatten(args), expr, printer=printer, dummify=dummify)
  905. return 'lambda %s: (%s)(%s)' % (','.join(dum_args), lstr, indexed_args)
  906. dummies_dict = {}
  907. if dummify:
  908. args = sub_args(args, dummies_dict)
  909. else:
  910. if isinstance(args, str):
  911. pass
  912. elif iterable(args, exclude=DeferredVector):
  913. args = ",".join(str(a) for a in args)
  914. # Transform expr
  915. if dummify:
  916. if isinstance(expr, str):
  917. pass
  918. else:
  919. expr = sub_expr(expr, dummies_dict)
  920. expr = _recursive_to_string(lambdarepr, expr)
  921. return "lambda %s: (%s)" % (args, expr)
  922. class _EvaluatorPrinter:
  923. def __init__(self, printer=None, dummify=False):
  924. self._dummify = dummify
  925. #XXX: This has to be done here because of circular imports
  926. from sympy.printing.lambdarepr import LambdaPrinter
  927. if printer is None:
  928. printer = LambdaPrinter()
  929. if inspect.isfunction(printer):
  930. self._exprrepr = printer
  931. else:
  932. if inspect.isclass(printer):
  933. printer = printer()
  934. self._exprrepr = printer.doprint
  935. #if hasattr(printer, '_print_Symbol'):
  936. # symbolrepr = printer._print_Symbol
  937. #if hasattr(printer, '_print_Dummy'):
  938. # dummyrepr = printer._print_Dummy
  939. # Used to print the generated function arguments in a standard way
  940. self._argrepr = LambdaPrinter().doprint
  941. def doprint(self, funcname, args, expr, *, cses=()):
  942. """
  943. Returns the function definition code as a string.
  944. """
  945. from sympy.core.symbol import Dummy
  946. funcbody = []
  947. if not iterable(args):
  948. args = [args]
  949. if cses:
  950. cses = list(cses)
  951. subvars, subexprs = zip(*cses)
  952. exprs = [expr] + list(subexprs)
  953. argstrs, exprs = self._preprocess(args, exprs, cses=cses)
  954. expr, subexprs = exprs[0], exprs[1:]
  955. cses = zip(subvars, subexprs)
  956. else:
  957. argstrs, expr = self._preprocess(args, expr)
  958. # Generate argument unpacking and final argument list
  959. funcargs = []
  960. unpackings = []
  961. for argstr in argstrs:
  962. if iterable(argstr):
  963. funcargs.append(self._argrepr(Dummy()))
  964. unpackings.extend(self._print_unpacking(argstr, funcargs[-1]))
  965. else:
  966. funcargs.append(argstr)
  967. funcsig = 'def {}({}):'.format(funcname, ', '.join(funcargs))
  968. # Wrap input arguments before unpacking
  969. funcbody.extend(self._print_funcargwrapping(funcargs))
  970. funcbody.extend(unpackings)
  971. for s, e in cses:
  972. if e is None:
  973. funcbody.append('del {}'.format(self._exprrepr(s)))
  974. else:
  975. funcbody.append('{} = {}'.format(self._exprrepr(s), self._exprrepr(e)))
  976. # Subs may appear in expressions generated by .diff()
  977. subs_assignments = []
  978. expr = self._handle_Subs(expr, out=subs_assignments)
  979. for lhs, rhs in subs_assignments:
  980. funcbody.append('{} = {}'.format(self._exprrepr(lhs), self._exprrepr(rhs)))
  981. str_expr = _recursive_to_string(self._exprrepr, expr)
  982. if '\n' in str_expr:
  983. str_expr = '({})'.format(str_expr)
  984. funcbody.append('return {}'.format(str_expr))
  985. funclines = [funcsig]
  986. funclines.extend([' ' + line for line in funcbody])
  987. return '\n'.join(funclines) + '\n'
  988. @classmethod
  989. def _is_safe_ident(cls, ident):
  990. return isinstance(ident, str) and ident.isidentifier() \
  991. and not keyword.iskeyword(ident)
  992. def _preprocess(self, args, expr, cses=(), _dummies_dict=None):
  993. """Preprocess args, expr to replace arguments that do not map
  994. to valid Python identifiers.
  995. Returns string form of args, and updated expr.
  996. """
  997. from sympy.core.basic import Basic
  998. from sympy.core.sorting import ordered
  999. from sympy.core.function import (Derivative, Function)
  1000. from sympy.core.symbol import Dummy, uniquely_named_symbol
  1001. from sympy.matrices import DeferredVector
  1002. from sympy.core.expr import Expr
  1003. # Args of type Dummy can cause name collisions with args
  1004. # of type Symbol. Force dummify of everything in this
  1005. # situation.
  1006. dummify = self._dummify or any(
  1007. isinstance(arg, Dummy) for arg in flatten(args))
  1008. argstrs = [None]*len(args)
  1009. if _dummies_dict is None:
  1010. _dummies_dict = {}
  1011. def update_dummies(arg, dummy):
  1012. _dummies_dict[arg] = dummy
  1013. for repl, sub in cses:
  1014. arg = arg.xreplace({sub: repl})
  1015. _dummies_dict[arg] = dummy
  1016. for arg, i in reversed(list(ordered(zip(args, range(len(args)))))):
  1017. if iterable(arg):
  1018. s, expr = self._preprocess(arg, expr, cses=cses, _dummies_dict=_dummies_dict)
  1019. elif isinstance(arg, DeferredVector):
  1020. s = str(arg)
  1021. elif isinstance(arg, Basic) and arg.is_symbol:
  1022. s = str(arg)
  1023. if dummify or not self._is_safe_ident(s):
  1024. dummy = Dummy()
  1025. if isinstance(expr, Expr):
  1026. dummy = uniquely_named_symbol(
  1027. dummy.name, expr, modify=lambda s: '_' + s)
  1028. s = self._argrepr(dummy)
  1029. update_dummies(arg, dummy)
  1030. expr = self._subexpr(expr, _dummies_dict)
  1031. elif dummify or isinstance(arg, (Function, Derivative)):
  1032. dummy = Dummy()
  1033. s = self._argrepr(dummy)
  1034. update_dummies(arg, dummy)
  1035. expr = self._subexpr(expr, _dummies_dict)
  1036. else:
  1037. s = str(arg)
  1038. argstrs[i] = s
  1039. return argstrs, expr
  1040. def _subexpr(self, expr, dummies_dict):
  1041. from sympy.matrices import DeferredVector
  1042. from sympy.core.sympify import sympify
  1043. expr = sympify(expr)
  1044. xreplace = getattr(expr, 'xreplace', None)
  1045. if xreplace is not None:
  1046. expr = xreplace(dummies_dict)
  1047. else:
  1048. if isinstance(expr, DeferredVector):
  1049. pass
  1050. elif isinstance(expr, dict):
  1051. k = [self._subexpr(sympify(a), dummies_dict) for a in expr.keys()]
  1052. v = [self._subexpr(sympify(a), dummies_dict) for a in expr.values()]
  1053. expr = dict(zip(k, v))
  1054. elif isinstance(expr, tuple):
  1055. expr = tuple(self._subexpr(sympify(a), dummies_dict) for a in expr)
  1056. elif isinstance(expr, list):
  1057. expr = [self._subexpr(sympify(a), dummies_dict) for a in expr]
  1058. return expr
  1059. def _print_funcargwrapping(self, args):
  1060. """Generate argument wrapping code.
  1061. args is the argument list of the generated function (strings).
  1062. Return value is a list of lines of code that will be inserted at
  1063. the beginning of the function definition.
  1064. """
  1065. return []
  1066. def _print_unpacking(self, unpackto, arg):
  1067. """Generate argument unpacking code.
  1068. arg is the function argument to be unpacked (a string), and
  1069. unpackto is a list or nested lists of the variable names (strings) to
  1070. unpack to.
  1071. """
  1072. def unpack_lhs(lvalues):
  1073. return '[{}]'.format(', '.join(
  1074. unpack_lhs(val) if iterable(val) else val for val in lvalues))
  1075. return ['{} = {}'.format(unpack_lhs(unpackto), arg)]
  1076. def _handle_Subs(self, expr, out):
  1077. """Any instance of Subs is extracted and returned as assignment pairs."""
  1078. from sympy.core.basic import Basic
  1079. from sympy.core.function import Subs
  1080. from sympy.core.symbol import Dummy
  1081. from sympy.matrices.matrixbase import MatrixBase
  1082. def _replace(ex, variables, point):
  1083. safe = {}
  1084. for lhs, rhs in zip(variables, point):
  1085. dummy = Dummy()
  1086. safe[lhs] = dummy
  1087. out.append((dummy, rhs))
  1088. return ex.xreplace(safe)
  1089. if isinstance(expr, (Basic, MatrixBase)):
  1090. expr = expr.replace(Subs, _replace)
  1091. elif iterable(expr):
  1092. expr = type(expr)([self._handle_Subs(e, out) for e in expr])
  1093. return expr
  1094. class _TensorflowEvaluatorPrinter(_EvaluatorPrinter):
  1095. def _print_unpacking(self, lvalues, rvalue):
  1096. """Generate argument unpacking code.
  1097. This method is used when the input value is not iterable,
  1098. but can be indexed (see issue #14655).
  1099. """
  1100. def flat_indexes(elems):
  1101. n = 0
  1102. for el in elems:
  1103. if iterable(el):
  1104. for ndeep in flat_indexes(el):
  1105. yield (n,) + ndeep
  1106. else:
  1107. yield (n,)
  1108. n += 1
  1109. indexed = ', '.join('{}[{}]'.format(rvalue, ']['.join(map(str, ind)))
  1110. for ind in flat_indexes(lvalues))
  1111. return ['[{}] = [{}]'.format(', '.join(flatten(lvalues)), indexed)]
  1112. def _imp_namespace(expr, namespace=None):
  1113. """ Return namespace dict with function implementations
  1114. We need to search for functions in anything that can be thrown at
  1115. us - that is - anything that could be passed as ``expr``. Examples
  1116. include SymPy expressions, as well as tuples, lists and dicts that may
  1117. contain SymPy expressions.
  1118. Parameters
  1119. ----------
  1120. expr : object
  1121. Something passed to lambdify, that will generate valid code from
  1122. ``str(expr)``.
  1123. namespace : None or mapping
  1124. Namespace to fill. None results in new empty dict
  1125. Returns
  1126. -------
  1127. namespace : dict
  1128. dict with keys of implemented function names within ``expr`` and
  1129. corresponding values being the numerical implementation of
  1130. function
  1131. Examples
  1132. ========
  1133. >>> from sympy.abc import x
  1134. >>> from sympy.utilities.lambdify import implemented_function, _imp_namespace
  1135. >>> from sympy import Function
  1136. >>> f = implemented_function(Function('f'), lambda x: x+1)
  1137. >>> g = implemented_function(Function('g'), lambda x: x*10)
  1138. >>> namespace = _imp_namespace(f(g(x)))
  1139. >>> sorted(namespace.keys())
  1140. ['f', 'g']
  1141. """
  1142. # Delayed import to avoid circular imports
  1143. from sympy.core.function import FunctionClass
  1144. if namespace is None:
  1145. namespace = {}
  1146. # tuples, lists, dicts are valid expressions
  1147. if is_sequence(expr):
  1148. for arg in expr:
  1149. _imp_namespace(arg, namespace)
  1150. return namespace
  1151. elif isinstance(expr, dict):
  1152. for key, val in expr.items():
  1153. # functions can be in dictionary keys
  1154. _imp_namespace(key, namespace)
  1155. _imp_namespace(val, namespace)
  1156. return namespace
  1157. # SymPy expressions may be Functions themselves
  1158. func = getattr(expr, 'func', None)
  1159. if isinstance(func, FunctionClass):
  1160. imp = getattr(func, '_imp_', None)
  1161. if imp is not None:
  1162. name = expr.func.__name__
  1163. if name in namespace and namespace[name] != imp:
  1164. raise ValueError('We found more than one '
  1165. 'implementation with name '
  1166. '"%s"' % name)
  1167. namespace[name] = imp
  1168. # and / or they may take Functions as arguments
  1169. if hasattr(expr, 'args'):
  1170. for arg in expr.args:
  1171. _imp_namespace(arg, namespace)
  1172. return namespace
  1173. def implemented_function(symfunc, implementation):
  1174. """ Add numerical ``implementation`` to function ``symfunc``.
  1175. ``symfunc`` can be an ``UndefinedFunction`` instance, or a name string.
  1176. In the latter case we create an ``UndefinedFunction`` instance with that
  1177. name.
  1178. Be aware that this is a quick workaround, not a general method to create
  1179. special symbolic functions. If you want to create a symbolic function to be
  1180. used by all the machinery of SymPy you should subclass the ``Function``
  1181. class.
  1182. Parameters
  1183. ----------
  1184. symfunc : ``str`` or ``UndefinedFunction`` instance
  1185. If ``str``, then create new ``UndefinedFunction`` with this as
  1186. name. If ``symfunc`` is an Undefined function, create a new function
  1187. with the same name and the implemented function attached.
  1188. implementation : callable
  1189. numerical implementation to be called by ``evalf()`` or ``lambdify``
  1190. Returns
  1191. -------
  1192. afunc : sympy.FunctionClass instance
  1193. function with attached implementation
  1194. Examples
  1195. ========
  1196. >>> from sympy.abc import x
  1197. >>> from sympy.utilities.lambdify import implemented_function
  1198. >>> from sympy import lambdify
  1199. >>> f = implemented_function('f', lambda x: x+1)
  1200. >>> lam_f = lambdify(x, f(x))
  1201. >>> lam_f(4)
  1202. 5
  1203. """
  1204. # Delayed import to avoid circular imports
  1205. from sympy.core.function import UndefinedFunction
  1206. # if name, create function to hold implementation
  1207. kwargs = {}
  1208. if isinstance(symfunc, UndefinedFunction):
  1209. kwargs = symfunc._kwargs
  1210. symfunc = symfunc.__name__
  1211. if isinstance(symfunc, str):
  1212. # Keyword arguments to UndefinedFunction are added as attributes to
  1213. # the created class.
  1214. symfunc = UndefinedFunction(
  1215. symfunc, _imp_=staticmethod(implementation), **kwargs)
  1216. elif not isinstance(symfunc, UndefinedFunction):
  1217. raise ValueError(filldedent('''
  1218. symfunc should be either a string or
  1219. an UndefinedFunction instance.'''))
  1220. return symfunc
  1221. def _too_large_for_docstring(expr, limit):
  1222. """Decide whether an ``Expr`` is too large to be fully rendered in a
  1223. ``lambdify`` docstring.
  1224. This is a fast alternative to ``count_ops``, which can become prohibitively
  1225. slow for large expressions, because in this instance we only care whether
  1226. ``limit`` is exceeded rather than counting the exact number of nodes in the
  1227. expression.
  1228. Parameters
  1229. ==========
  1230. expr : ``Expr``, (nested) ``list`` of ``Expr``, or ``Matrix``
  1231. The same objects that can be passed to the ``expr`` argument of
  1232. ``lambdify``.
  1233. limit : ``int`` or ``None``
  1234. The threshold above which an expression contains too many nodes to be
  1235. usefully rendered in the docstring. If ``None`` then there is no limit.
  1236. Returns
  1237. =======
  1238. bool
  1239. ``True`` if the number of nodes in the expression exceeds the limit,
  1240. ``False`` otherwise.
  1241. Examples
  1242. ========
  1243. >>> from sympy.abc import x, y, z
  1244. >>> from sympy.utilities.lambdify import _too_large_for_docstring
  1245. >>> expr = x
  1246. >>> _too_large_for_docstring(expr, None)
  1247. False
  1248. >>> _too_large_for_docstring(expr, 100)
  1249. False
  1250. >>> _too_large_for_docstring(expr, 1)
  1251. False
  1252. >>> _too_large_for_docstring(expr, 0)
  1253. True
  1254. >>> _too_large_for_docstring(expr, -1)
  1255. True
  1256. Does this split it?
  1257. >>> expr = [x, y, z]
  1258. >>> _too_large_for_docstring(expr, None)
  1259. False
  1260. >>> _too_large_for_docstring(expr, 100)
  1261. False
  1262. >>> _too_large_for_docstring(expr, 1)
  1263. True
  1264. >>> _too_large_for_docstring(expr, 0)
  1265. True
  1266. >>> _too_large_for_docstring(expr, -1)
  1267. True
  1268. >>> expr = [x, [y], z, [[x+y], [x*y*z, [x+y+z]]]]
  1269. >>> _too_large_for_docstring(expr, None)
  1270. False
  1271. >>> _too_large_for_docstring(expr, 100)
  1272. False
  1273. >>> _too_large_for_docstring(expr, 1)
  1274. True
  1275. >>> _too_large_for_docstring(expr, 0)
  1276. True
  1277. >>> _too_large_for_docstring(expr, -1)
  1278. True
  1279. >>> expr = ((x + y + z)**5).expand()
  1280. >>> _too_large_for_docstring(expr, None)
  1281. False
  1282. >>> _too_large_for_docstring(expr, 100)
  1283. True
  1284. >>> _too_large_for_docstring(expr, 1)
  1285. True
  1286. >>> _too_large_for_docstring(expr, 0)
  1287. True
  1288. >>> _too_large_for_docstring(expr, -1)
  1289. True
  1290. >>> from sympy import Matrix
  1291. >>> expr = Matrix([[(x + y + z), ((x + y + z)**2).expand(),
  1292. ... ((x + y + z)**3).expand(), ((x + y + z)**4).expand()]])
  1293. >>> _too_large_for_docstring(expr, None)
  1294. False
  1295. >>> _too_large_for_docstring(expr, 1000)
  1296. False
  1297. >>> _too_large_for_docstring(expr, 100)
  1298. True
  1299. >>> _too_large_for_docstring(expr, 1)
  1300. True
  1301. >>> _too_large_for_docstring(expr, 0)
  1302. True
  1303. >>> _too_large_for_docstring(expr, -1)
  1304. True
  1305. """
  1306. # Must be imported here to avoid a circular import error
  1307. from sympy.core.traversal import postorder_traversal
  1308. if limit is None:
  1309. return False
  1310. i = 0
  1311. for _ in postorder_traversal(expr):
  1312. i += 1
  1313. if i > limit:
  1314. return True
  1315. return False