lambdify.py 54 KB

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