Several submodules in SymPy allow one to generate directly compilable and executable code in a variety of different programming languages from SymPy expressions. In addition, there are functions that generate Python importable objects that can evaluate SymPy expressions very efficiently.
We will start with a brief introduction to the components that make up the code generation capabilities of SymPy.
Introduction
There are four main levels of abstractions:
expression
|
code printers
|
code generators
|
autowrap
sympy.utilities.autowrap
uses codegen, and codegen uses the code
printers. sympy.utilities.autowrap
does everything: it lets you go
from SymPy expression to numerical function in the same Python process in one
step. Codegen is actual code generation, i.e., to compile and use later, or to
include in some larger project.
The code printers translate the SymPy objects into actual code, like abs(x)
-> fabs(x)
(for C).
The code printers don’t print optimal code in many cases. An example of this is
powers in C. x**2
prints as pow(x, 2)
instead of x*x
. Other
optimizations (like mathematical simplifications) should happen before the code
printers.
Currently, sympy.simplify.cse_main.cse()
is not applied automatically anywhere in this
chain. It ideally happens at the codegen level, or somewhere above it.
We will iterate through the levels below.
The following three lines will be used to setup each example:
>>> from sympy import *
>>> init_printing(use_unicode=True)
>>> from sympy.abc import a, e, k, n, r, t, x, y, z, T, Z
>>> from sympy.abc import beta, omega, tau
>>> f, g = symbols('f, g', cls=Function)
Code printers (sympy.printing)
This is where the meat of code generation is; the translation of SymPy
actually more like a lightweight version of codegen for Python, and
Python (sympy.printing.pycode.pycode()
), and
sympy.printing.lambdarepr.lambdarepr()
, which supports many libraries
(like NumPy), and Aesara
(sympy.printing.aesaracode.aesara_function()
). The code printers are
special cases of the other prints in SymPy (str printer, pretty printer, etc.).
An important distinction is that the code printer has to deal with assignments
(using the sympy.codegen.ast.Assignment
object). This serves as
building blocks for the code printers and hence the codegen
module. An
example that shows the use of Assignment
in C code:
>>> from sympy.codegen.ast import Assignment
>>> print(ccode(Assignment(x, y + 1)))
x = y + 1;
Here is another simple example of printing a C version of a SymPy expression:
>>> expr = (Rational(-1, 2) * Z * k * (e**2) / r)
>>> expr
2
-Z⋅e ⋅k
────────
2⋅r
>>> ccode(expr)
-1.0/2.0*Z*pow(e, 2)*k/r
>>> from sympy.codegen.ast import real, float80
>>> ccode(expr, assign_to="E", type_aliases={real: float80})
E = -1.0L/2.0L*Z*powl(e, 2)*k/r;
To generate code with some math functions provided by e.g. the C99 standard we need
to import functions from sympy.codegen.cfunctions
:
>>> from sympy.codegen.cfunctions import expm1
>>> ccode(expm1(x), standard='C99')
expm1(x)
Piecewise
expressions are converted into conditionals. If an assign_to
variable is provided an if statement is created, otherwise the ternary operator
is used. Note that if the Piecewise
lacks a default term, represented by
(expr, True)
then an error will be thrown. This is to prevent generating
an expression that may not evaluate to anything. A use case for Piecewise
:
>>> expr = Piecewise((x + 1, x > 0), (x, True))
>>> print(fcode(expr, tau))
if (x > 0) then
tau = x + 1
else
tau = x
end if
The various printers also tend to support Indexed
objects well. With
contract=True
these expressions will be turned into loops, whereas
contract=False
will just print the assignment expression that should be
looped over:
>>> len_y = 5
>>> mat_1 = IndexedBase('mat_1', shape=(len_y,))
>>> mat_2 = IndexedBase('mat_2', shape=(len_y,))
>>> Dy = IndexedBase('Dy', shape=(len_y-1,))
>>> i = Idx('i', len_y-1)
>>> eq = Eq(Dy[i], (mat_1[i+1] - mat_1[i]) / (mat_2[i+1] - mat_2[i]))
>>> print(jscode(eq.rhs, assign_to=eq.lhs, contract=False))
Dy[i] = (mat_1[i + 1] - mat_1[i])/(mat_2[i + 1] - mat_2[i]);
>>> Res = IndexedBase('Res', shape=(len_y,))
>>> j = Idx('j', len_y)
>>> eq = Eq(Res[j], mat_1[j]*mat_2[j])
>>> print(jscode(eq.rhs, assign_to=eq.lhs, contract=True))
for (var j=0; j<5; j++){
Res[j] = 0;
}
for (var j=0; j<5; j++){
for (var j=0; j<5; j++){
Res[j] = Res[j] + mat_1[j]*mat_2[j];
}
}
>>> print(jscode(eq.rhs, assign_to=eq.lhs, contract=False))
Res[j] = mat_1[j]*mat_2[j];
Custom printing can be defined for certain types by passing a dictionary of
“type” : “function” to the user_functions
kwarg. Alternatively, the
dictionary value can be a list of tuples i.e., [(argument_test,
cfunction_string)]
. This can be used to call a custom Octave function:
>>> custom_functions = {
... "f": "existing_octave_fcn",
... "g": [(lambda x: x.is_Matrix, "my_mat_fcn"),
... (lambda x: not x.is_Matrix, "my_fcn")]
... }
>>> mat = Matrix([[1, x]])
>>> octave_code(f(x) + g(x) + g(mat), user_functions=custom_functions)
existing_octave_fcn(x) + my_fcn(x) + my_mat_fcn([1 x])
An example of Mathematica code printer:
>>> x_ = Function('x')
>>> expr = x_(n*T) * sin((t - n*T) / T)
>>> expr = expr / ((-T*n + t) / T)
>>> expr
⎛-T⋅n + t⎞
T⋅x(T⋅n)⋅sin⎜────────⎟
⎝ T ⎠
──────────────────────
-T⋅n + t
>>> expr = summation(expr, (n, -1, 1))
>>> mathematica_code(expr)
T*(x[-T]*Sin[(T + t)/T]/(T + t) + x[T]*Sin[(-T + t)/T]/(-T + t) + x[0]*Sin[t/T
]/t)
We can go through a common expression in different languages we support and see how it works:
>>> k, g1, g2, r, I, S = symbols("k, gamma_1, gamma_2, r, I, S")
>>> expr = k * g1 * g2 / (r**3)
>>> expr = expr * 2 * I * S * (3 * (cos(beta))**2 - 1) / 2
>>> expr
⎛ 2 ⎞
I⋅S⋅γ₁⋅γ₂⋅k⋅⎝3⋅cos (β) - 1⎠
───────────────────────────
3
r
>>> print(jscode(expr, assign_to="H_is"))
H_is = I*S*gamma_1*gamma_2*k*(3*Math.pow(Math.cos(beta), 2) - 1)/Math.pow(r, 3);
>>> print(ccode(expr, assign_to="H_is", standard='C89'))
H_is = I*S*gamma_1*gamma_2*k*(3*pow(cos(beta), 2) - 1)/pow(r, 3);
>>> print(fcode(expr, assign_to="H_is"))
H_is = I*S*gamma_1*gamma_2*k*(3*cos(beta)**2 - 1)/r**3
>>> print(julia_code(expr, assign_to="H_is"))
H_is = I.*S.*gamma_1.*gamma_2.*k.*(3*cos(beta).^2 - 1)./r.^3
>>> print(octave_code(expr, assign_to="H_is"))
H_is = I.*S.*gamma_1.*gamma_2.*k.*(3*cos(beta).^2 - 1)./r.^3;
>>> print(rust_code(expr, assign_to="H_is"))
H_is = I*S*gamma_1*gamma_2*k*(3*beta.cos().powi(2) - 1)/r.powi(3);
>>> print(mathematica_code(expr))
I*S*gamma_1*gamma_2*k*(3*Cos[beta]^2 - 1)/r^3
Codegen (sympy.utilities.codegen)
This module deals with creating compilable code from SymPy expressions. This is lower level than autowrap, as it doesn’t actually attempt to compile the code, but higher level than the printers, as it generates compilable files (including header files), rather than just code snippets.
The user friendly functions, here, are codegen
and make_routine
.
codegen
takes a list of (variable, expression)
pairs and a language (C,
F95, and Octave/Matlab are supported). It returns, as strings, a code file and
a header file (for relevant languages). The variables are created as functions
that return the value of the expression as output.
Note
The codegen
callable is not in the sympy namespace automatically,
to use it you must first import codegen
from sympy.utilities.codegen
For instance:
>>> from sympy.utilities.codegen import codegen
>>> length, breadth, height = symbols('length, breadth, height')
>>> [(c_name, c_code), (h_name, c_header)] = \
... codegen(('volume', length*breadth*height), "C99", "test",
... header=False, empty=False)
>>> print(c_name)
test.c
>>> print(c_code)
#include "test.h"
#include <math.h>
double volume(double breadth, double height, double length) {
double volume_result;
volume_result = breadth*height*length;
return volume_result;
}
>>> print(h_name)
test.h
>>> print(c_header)
#ifndef PROJECT__TEST__H
#define PROJECT__TEST__H
double volume(double breadth, double height, double length);
#endif
Various flags to codegen
let you modify things. The project name for
preprocessor instructions can be varied using project
. Variables listed as
global variables in arg global_vars
will not show up as function arguments.
language
is a case-insensitive string that indicates the source code
language. Currently, C
, F95
and Octave
are supported. Octave
generates code compatible with both Octave and Matlab.
header
when True, a header is written on top of each source file. empty
when True, empty lines are used to structure the code. With
argument_sequence
a sequence of arguments for the routine can be defined in
a preferred order.
prefix
defines a prefix for the names of the files that contain the source
code. If omitted, the name of the first name_expr tuple is used.
to_files
when True, the code will be written to one or more files with the
given prefix.
Here is an example:
>>> [(f_name, f_code), header] = codegen(("volume", length*breadth*height),
... "F95", header=False, empty=False, argument_sequence=(breadth, length),
... global_vars=(height,))
>>> print(f_code)
REAL*8 function volume(breadth, length)
implicit none
REAL*8, intent(in) :: breadth
REAL*8, intent(in) :: length
volume = breadth*height*length
end function
The method make_routine
creates a Routine
object, which represents an
evaluation routine for a set of expressions. This is only good for internal use
by the CodeGen objects, as an intermediate representation from SymPy expression
to generated code. It is not recommended to make a Routine
object
yourself. You should instead use make_routine
method. make_routine
in
turn calls the routine
method of the CodeGen object depending upon the
language of choice. This creates the internal objects representing assignments
and so on, and creates the Routine
class with them.
The various codegen objects such as Routine
and Variable
aren’t SymPy
objects (they don’t subclass from Basic).
For example:
>>> from sympy.utilities.codegen import make_routine
>>> from sympy.physics.hydrogen import R_nl
>>> expr = R_nl(3, y, x, 6)
>>> routine = make_routine('my_routine', expr)
>>> [arg.result_var for arg in routine.results]
[result₅₁₄₂₃₄₁₆₈₁₃₉₇₇₁₉₄₂₈]
>>> [arg.expr for arg in routine.results]
⎡ __________ ⎤
⎢ y ╱ (2 - y)! -2⋅x ⎥
⎢4⋅√6⋅(4⋅x) ⋅ ╱ ──────── ⋅ℯ ⋅assoc_laguerre(2 - y, 2⋅y + 1, 4⋅x)⎥
⎢ ╲╱ (y + 3)! ⎥
⎢────────────────────────────────────────────────────────────────────⎥
⎣ 3 ⎦
>>> [arg.name for arg in routine.arguments]
[x, y]
Another more complicated example with a mixture of specified and automatically-assigned names. Also has Matrix output:
>>> routine = make_routine('fcn', [x*y, Eq(a, 1), Eq(r, x + r), Matrix([[x, 2]])])
>>> [arg.result_var for arg in routine.results]
[result_5397460570204848505]
>>> [arg.expr for arg in routine.results]
[x⋅y]
>>> [arg.name for arg in routine.arguments]
[x, y, a, r, out_8598435338387848786]
We can examine the various arguments more closely:
>>> from sympy.utilities.codegen import (InputArgument, OutputArgument,
... InOutArgument)
>>> [a.name for a in routine.arguments if isinstance(a, InputArgument)]
[x, y]
>>> [a.name for a in routine.arguments if isinstance(a, OutputArgument)]
[a, out_8598435338387848786]
>>> [a.expr for a in routine.arguments if isinstance(a, OutputArgument)]
[1, [x 2]]
>>> [a.name for a in routine.arguments if isinstance(a, InOutArgument)]
[r]
>>> [a.expr for a in routine.arguments if isinstance(a, InOutArgument)]
[r + x]
The full API reference can be viewed here.
Autowrap
Autowrap automatically generates code, writes it to disk, compiles it, and
imports it into the current session. Main functions of this module are
autowrap
, binary_function
, and ufuncify
.
It also automatically converts expressions containing Indexed
objects into
summations. The classes IndexedBase, Indexed and Idx represent a matrix element
M[i, j]. See Tensor for more on this.
autowrap
creates a wrapper using f2py or Cython and creates a numerical function.
Note
The autowrap
callable is not in the sympy namespace automatically,
to use it you must first import autowrap
from sympy.utilities.autowrap
The callable returned from autowrap() is a binary Python function, not a SymPy object. For example:
>>> from sympy.utilities.autowrap import autowrap
>>> expr = ((x - y + z)**(13)).expand()
>>> binary_func = autowrap(expr)
>>> binary_func(1, 4, 2)
-1.0
The various flags available with autowrap() help to modify the services
provided by the method. The argument tempdir
tells autowrap to compile the
code in a specific directory, and leave the files intact when finished. For
instance:
>>> from sympy.utilities.autowrap import autowrap
>>> from sympy.physics.qho_1d import psi_n
>>> x_ = IndexedBase('x')
>>> y_ = IndexedBase('y')
>>> m = symbols('m', integer=True)
>>> i = Idx('i', m)
>>> qho = autowrap(Eq(y_[i], psi_n(0, x_[i], m, omega)), tempdir='/tmp')
Checking the Fortran source code in the directory specified reveals this:
subroutine autofunc(m, omega, x, y)
implicit none
INTEGER*4, intent(in) :: m
REAL*8, intent(in) :: omega
REAL*8, intent(in), dimension(1:m) :: x
REAL*8, intent(out), dimension(1:m) :: y
INTEGER*4 :: i
REAL*8, parameter :: hbar = 1.05457162d-34
REAL*8, parameter :: pi = 3.14159265358979d0
do i = 1, m
y(i) = (m*omega)**(1.0d0/4.0d0)*exp(-4.74126166983329d+33*m*omega*x(i &
)**2)/(hbar**(1.0d0/4.0d0)*pi**(1.0d0/4.0d0))
end do
end subroutine
Using the argument args
along with it changes argument sequence:
>>> eq = Eq(y_[i], psi_n(0, x_[i], m, omega))
>>> qho = autowrap(eq, tempdir='/tmp', args=[y, x, m, omega])
yields:
subroutine autofunc(y, x, m, omega)
implicit none
INTEGER*4, intent(in) :: m
REAL*8, intent(in) :: omega
REAL*8, intent(out), dimension(1:m) :: y
REAL*8, intent(in), dimension(1:m) :: x
INTEGER*4 :: i
REAL*8, parameter :: hbar = 1.05457162d-34
REAL*8, parameter :: pi = 3.14159265358979d0
do i = 1, m
y(i) = (m*omega)**(1.0d0/4.0d0)*exp(-4.74126166983329d+33*m*omega*x(i &
)**2)/(hbar**(1.0d0/4.0d0)*pi**(1.0d0/4.0d0))
end do
end subroutine
The argument verbose
is boolean, optional and if True, autowrap will not
mute the command line backends. This can be helpful for debugging.
The argument language
and backend
are used to change defaults:
Fortran
and f2py
to C
and Cython
. The argument helpers is used
to define auxiliary expressions needed for the main expression. If the main
expression needs to call a specialized function it should be put in the
helpers
iterable. Autowrap will then make sure that the compiled main
expression can link to the helper routine. Items should be tuples with
(<function_name>, <sympy_expression>, <arguments>)
. It is mandatory to
supply an argument sequence to helper routines.
Another method available at the autowrap
level is binary_function
. It
returns a sympy function. The advantage is that we can have very fast functions
as compared to SymPy speeds. This is because we will be using compiled
functions with SymPy attributes and methods. An illustration:
>>> from sympy.utilities.autowrap import binary_function
>>> from sympy.physics.hydrogen import R_nl
>>> psi_nl = R_nl(1, 0, a, r)
>>> f = binary_function('f', psi_nl)
>>> f(a, r).evalf(3, subs={a: 1, r: 2})
0.766
While NumPy operations are very efficient for vectorized data but they sometimes incur unnecessary costs when chained together. Consider the following operation
>>> x = get_numpy_array(...)
>>> y = sin(x) / x
The operators sin
and /
call routines that execute tight for loops in
C
. The resulting computation looks something like this
for(int i = 0; i < n; i++)
{
temp[i] = sin(x[i]);
}
for(int i = i; i < n; i++)
{
y[i] = temp[i] / x[i];
}
This is slightly sub-optimal because
We allocate an extra
temp
arrayWe walk over
x
memory twice when once would have been sufficient
A better solution would fuse both element-wise operations into a single for loop
for(int i = i; i < n; i++)
{
y[i] = sin(x[i]) / x[i];
}
Statically compiled projects like NumPy are unable to take advantage of such
optimizations. Fortunately, SymPy is able to generate efficient low-level C
or Fortran code. It can then depend on projects like Cython
or f2py
to
compile and reconnect that code back up to Python. Fortunately this process is
well automated and a SymPy user wishing to make use of this code generation
should call the ufuncify
function.
ufuncify
is the third method available with Autowrap module. It basically
implies ‘Universal functions’ and follows an ideology set by NumPy. The main
point of ufuncify as compared to autowrap is that it allows arrays as arguments
and can operate in an element-by-element fashion. The core operation done
element-wise is in accordance to Numpy’s array broadcasting rules. See this for more.
>>> from sympy import *
>>> from sympy.abc import x
>>> expr = sin(x)/x
>>> from sympy.utilities.autowrap import ufuncify
>>> f = ufuncify([x], expr)
This function f
consumes and returns a NumPy array. Generally ufuncify
performs at least as well as lambdify
. If the expression is complicated
then ufuncify
often significantly outperforms the NumPy backed solution.
Jensen has a good blog post on this topic.
Let us see an example for some quantitative analysis:
>>> from sympy.physics.hydrogen import R_nl
>>> expr = R_nl(3, 1, x, 6)
>>> expr
-2⋅x
8⋅x⋅(4 - 4⋅x)⋅ℯ
───────────────────
3
The lambdify function translates SymPy expressions into Python functions,
leveraging a variety of numerical libraries. By default lambdify relies on
implementations in the math
standard library. Naturally, Raw Python is
faster than SymPy. However it also supports mpmath
and most notably,
numpy
. Using the NumPy library gives the generated function access to
powerful vectorized ufuncs that are backed by compiled C code.
Let us compare the speeds:
>>> from sympy.utilities.autowrap import ufuncify
>>> from sympy.utilities.lambdify import lambdify
>>> fn_numpy = lambdify(x, expr, 'numpy')
>>> fn_fortran = ufuncify([x], expr, backend='f2py')
>>> from numpy import linspace
>>> xx = linspace(0, 1, 5)
>>> fn_numpy(xx)
[ 0. 1.21306132 0.98101184 0.44626032 0. ]
>>> fn_fortran(xx)
[ 0. 1.21306132 0.98101184 0.44626032 0. ]
>>> import timeit
>>> timeit.timeit('fn_numpy(xx)', 'from __main__ import fn_numpy, xx', number=10000)
0.18891601900395472
>>> timeit.timeit('fn_fortran(xx)', 'from __main__ import fn_fortran, xx', number=10000)
0.004707066000264604
The options available with ufuncify are more or less the same as those
available with autowrap
.
There are other facilities available with SymPy to do efficient numeric computation. See this page for a comparison among them.
Classes and functions for rewriting expressions (sympy.codegen.rewriting)
Classes and functions useful for rewriting expressions for optimized code generation. Some languages (or standards thereof), e.g. C99, offer specialized math functions for better performance and/or precision.
Using the optimize
function in this module, together with a collection of
rules (represented as instances of Optimization
), one can rewrite the
expressions for this purpose:
>>> from sympy import Symbol, exp, log
>>> from sympy.codegen.rewriting import optimize, optims_c99
>>> x = Symbol('x')
>>> optimize(3*exp(2*x) - 3, optims_c99)
3*expm1(2*x)
>>> optimize(exp(2*x) - 1 - exp(-33), optims_c99)
expm1(2*x) - exp(-33)
>>> optimize(log(3*x + 3), optims_c99)
log1p(x) + log(3)
>>> optimize(log(2*x + 3), optims_c99)
log(2*x + 3)
The optims_c99
imported above is tuple containing the following instances
(which may be imported from sympy.codegen.rewriting
):
expm1_opt
log1p_opt
exp2_opt
log2_opt
log2const_opt
- class sympy.codegen.rewriting.FuncMinusOneOptim(func, func_m_1, opportunistic=True)[source]
Specialization of ReplaceOptim for functions evaluating “f(x) - 1”.
- Parameters
func :
The function which is subtracted by one.
func_m_1 :
The specialized function evaluating
func(x) - 1
.opportunistic : bool
When
True
, apply the transformation as long as the magnitude of the remaining number terms decreases. WhenFalse
, only apply the transformation if it completely eliminates the number term.
Explanation
Numerical functions which go toward one as x go toward zero is often best implemented by a dedicated function in order to avoid catastrophic cancellation. One such example is
expm1(x)
in the C standard library which evaluatesexp(x) - 1
. Such functions preserves many more significant digits when its argument is much smaller than one, compared to subtracting one afterwards.Examples
>>> from sympy import symbols, exp >>> from sympy.codegen.rewriting import FuncMinusOneOptim >>> from sympy.codegen.cfunctions import expm1 >>> x, y = symbols('x y') >>> expm1_opt = FuncMinusOneOptim(exp, expm1) >>> expm1_opt(exp(x) + 2*exp(5*y) - 3) expm1(x) + 2*expm1(5*y)
- class sympy.codegen.rewriting.Optimization(cost_function=None, priority=1)[source]
Abstract base class for rewriting optimization.
Subclasses should implement
__call__
taking an expression as argument.- Parameters
cost_function : callable returning number
priority : number
- class sympy.codegen.rewriting.ReplaceOptim(query, value, **kwargs)[source]
Rewriting optimization calling replace on expressions.
- Parameters
query :
First argument passed to replace.
value :
Second argument passed to replace.
Explanation
The instance can be used as a function on expressions for which it will apply the
replace
method (seesympy.core.basic.Basic.replace()
).Examples
>>> from sympy import Symbol >>> from sympy.codegen.rewriting import ReplaceOptim >>> from sympy.codegen.cfunctions import exp2 >>> x = Symbol('x') >>> exp2_opt = ReplaceOptim(lambda p: p.is_Pow and p.base == 2, ... lambda p: exp2(p.exp)) >>> exp2_opt(2**x) exp2(x)
- sympy.codegen.rewriting.create_expand_pow_optimization(limit, *, base_req=<function <lambda>>)[source]
Creates an instance of
ReplaceOptim
for expandingPow
.- Parameters
limit : int
The highest power which is expanded into multiplication.
base_req : function returning bool
Requirement on base for expansion to happen, default is to return the
is_symbol
attribute of the base.
Explanation
The requirements for expansions are that the base needs to be a symbol and the exponent needs to be an Integer (and be less than or equal to
limit
).Examples
>>> from sympy import Symbol, sin >>> from sympy.codegen.rewriting import create_expand_pow_optimization >>> x = Symbol('x') >>> expand_opt = create_expand_pow_optimization(3) >>> expand_opt(x**5 + x**3) x**5 + x*x*x >>> expand_opt(x**5 + x**3 + sin(x)**3) x**5 + sin(x)**3 + x*x*x >>> opt2 = create_expand_pow_optimization(3, base_req=lambda b: not b.is_Function) >>> opt2((x+1)**2 + sin(x)**2) sin(x)**2 + (x + 1)*(x + 1)
- sympy.codegen.rewriting.optimize(expr, optimizations)[source]
Apply optimizations to an expression.
- Parameters
expr : expression
optimizations : iterable of
Optimization
instancesThe optimizations will be sorted with respect to
priority
(highest first).
Examples
>>> from sympy import log, Symbol >>> from sympy.codegen.rewriting import optims_c99, optimize >>> x = Symbol('x') >>> optimize(log(x+3)/log(2) + log(x**2 + 1), optims_c99) log1p(x**2) + log2(x + 3)
Additional AST nodes for operations on matrices. The nodes in this module are meant to represent optimization of matrix expressions within codegen’s target languages that cannot be represented by SymPy expressions.
As an example, we can use sympy.codegen.rewriting.optimize()
and the
matin_opt
optimization provided in sympy.codegen.rewriting
to
transform matrix multiplication under certain assumptions:
>>> from sympy import symbols, MatrixSymbol
>>> n = symbols('n', integer=True)
>>> A = MatrixSymbol('A', n, n)
>>> x = MatrixSymbol('x', n, 1)
>>> expr = A**(-1) * x
>>> from sympy import assuming, Q
>>> from sympy.codegen.rewriting import matinv_opt, optimize
>>> with assuming(Q.fullrank(A)):
... optimize(expr, [matinv_opt])
MatrixSolve(A, vector=x)
- class sympy.codegen.matrix_nodes.MatrixSolve(*args, **kwargs)[source]
Represents an operation to solve a linear matrix equation.
- Parameters
matrix : MatrixSymbol
Matrix representing the coefficients of variables in the linear equation. This matrix must be square and full-rank (i.e. all columns must be linearly independent) for the solving operation to be valid.
vector : MatrixSymbol
One-column matrix representing the solutions to the equations represented in
matrix
.
Examples
>>> from sympy import symbols, MatrixSymbol >>> from sympy.codegen.matrix_nodes import MatrixSolve >>> n = symbols('n', integer=True) >>> A = MatrixSymbol('A', n, n) >>> x = MatrixSymbol('x', n, 1) >>> from sympy.printing.numpy import NumPyPrinter >>> NumPyPrinter().doprint(MatrixSolve(A, x)) 'numpy.linalg.solve(A, x)' >>> from sympy import octave_code >>> octave_code(MatrixSolve(A, x)) 'A \\ x'
Tools for simplifying expressions using approximations (sympy.codegen.approximations)
- class sympy.codegen.approximations.SeriesApprox(bounds, reltol, max_order=4, n_point_checks=4, **kwargs)[source]
Approximates functions by expanding them as a series.
- Parameters
bounds : dict
Mapping expressions to length 2 tuple of bounds (low, high).
reltol : number
Threshold for when to ignore a term. Taken relative to the largest lower bound among bounds.
max_order : int
Largest order to include in series expansion
n_point_checks : int (even)
The validity of an expansion (with respect to reltol) is checked at discrete points (linearly spaced over the bounds of the variable). The number of points used in this numerical check is given by this number.
Examples
>>> from sympy import sin, pi >>> from sympy.abc import x, y >>> from sympy.codegen.rewriting import optimize >>> from sympy.codegen.approximations import SeriesApprox >>> bounds = {x: (-.1, .1), y: (pi-1, pi+1)} >>> series_approx2 = SeriesApprox(bounds, reltol=1e-2) >>> series_approx3 = SeriesApprox(bounds, reltol=1e-3) >>> series_approx8 = SeriesApprox(bounds, reltol=1e-8) >>> expr = sin(x)*sin(y) >>> optimize(expr, [series_approx2]) x*(-y + (y - pi)**3/6 + pi) >>> optimize(expr, [series_approx3]) (-x**3/6 + x)*sin(y) >>> optimize(expr, [series_approx8]) sin(x)*sin(y)
- class sympy.codegen.approximations.SumApprox(bounds, reltol, **kwargs)[source]
Approximates sum by neglecting small terms.
- Parameters
bounds : dict
Mapping expressions to length 2 tuple of bounds (low, high).
reltol : number
Threshold for when to ignore a term. Taken relative to the largest lower bound among bounds.
Explanation
If terms are expressions which can be determined to be monotonic, then bounds for those expressions are added.
Examples
>>> from sympy import exp >>> from sympy.abc import x, y, z >>> from sympy.codegen.rewriting import optimize >>> from sympy.codegen.approximations import SumApprox >>> bounds = {x: (-1, 1), y: (1000, 2000), z: (-10, 3)} >>> sum_approx3 = SumApprox(bounds, reltol=1e-3) >>> sum_approx2 = SumApprox(bounds, reltol=1e-2) >>> sum_approx1 = SumApprox(bounds, reltol=1e-1) >>> expr = 3*(x + y + exp(z)) >>> optimize(expr, [sum_approx3]) 3*(x + y + exp(z)) >>> optimize(expr, [sum_approx2]) 3*y + 3*exp(z) >>> optimize(expr, [sum_approx1]) 3*y
Classes for abstract syntax trees (sympy.codegen.ast)
Types used to represent a full function/module as an Abstract Syntax Tree.
Most types are small, and are merely used as tokens in the AST. A tree diagram has been included below to illustrate the relationships between the AST types.
AST Type Tree
*Basic*
|
|
CodegenAST
|
|--->AssignmentBase
| |--->Assignment
| |--->AugmentedAssignment
| |--->AddAugmentedAssignment
| |--->SubAugmentedAssignment
| |--->MulAugmentedAssignment
| |--->DivAugmentedAssignment
| |--->ModAugmentedAssignment
|
|--->CodeBlock
|
|
|--->Token
|--->Attribute
|--->For
|--->String
| |--->QuotedString
| |--->Comment
|--->Type
| |--->IntBaseType
| | |--->_SizedIntType
| | |--->SignedIntType
| | |--->UnsignedIntType
| |--->FloatBaseType
| |--->FloatType
| |--->ComplexBaseType
| |--->ComplexType
|--->Node
| |--->Variable
| | |---> Pointer
| |--->FunctionPrototype
| |--->FunctionDefinition
|--->Element
|--->Declaration
|--->While
|--->Scope
|--->Stream
|--->Print
|--->FunctionCall
|--->BreakToken
|--->ContinueToken
|--->NoneToken
|--->Return
Predefined types
A number of Type
instances are provided in the sympy.codegen.ast
module
for convenience. Perhaps the two most common ones for code-generation (of numeric
codes) are float32
and float64
(known as single and double precision respectively).
There are also precision generic versions of Types (for which the codeprinters selects the
underlying data type at time of printing): real
, integer
, complex_
, bool_
.
The other Type
instances defined are:
intc
: Integer type used by C’s “int”.intp
: Integer type used by C’s “unsigned”.int8
,int16
,int32
,int64
: n-bit integers.uint8
,uint16
,uint32
,uint64
: n-bit unsigned integers.float80
: known as “extended precision” on modern x86/amd64 hardware.complex64
: Complex number represented by twofloat32
numberscomplex128
: Complex number represented by twofloat64
numbers
Using the nodes
It is possible to construct simple algorithms using the AST nodes. Let’s construct a loop applying Newton’s method:
>>> from sympy import symbols, cos
>>> from sympy.codegen.ast import While, Assignment, aug_assign, Print
>>> t, dx, x = symbols('tol delta val')
>>> expr = cos(x) - x**3
>>> whl = While(abs(dx) > t, [
... Assignment(dx, -expr/expr.diff(x)),
... aug_assign(x, '+', dx),
... Print([x])
... ])
>>> from sympy import pycode
>>> py_str = pycode(whl)
>>> print(py_str)
while (abs(delta) > tol):
delta = (val**3 - math.cos(val))/(-3*val**2 - math.sin(val))
val += delta
print(val)
>>> import math
>>> tol, val, delta = 1e-5, 0.5, float('inf')
>>> exec(py_str)
1.1121416371
0.909672693737
0.867263818209
0.865477135298
0.865474033111
>>> print('%3.1g' % (math.cos(val) - val**3))
-3e-11
If we want to generate Fortran code for the same while loop we simple call fcode
:
>>> from sympy import fcode
>>> print(fcode(whl, standard=2003, source_format='free'))
do while (abs(delta) > tol)
delta = (val**3 - cos(val))/(-3*val**2 - sin(val))
val = val + delta
print *, val
end do
There is a function constructing a loop (or a complete function) like this in
sympy.codegen.algorithms
.
- class sympy.codegen.ast.Assignment(lhs, rhs)[source]
Represents variable assignment for code generation.
- Parameters
lhs : Expr
SymPy object representing the lhs of the expression. These should be singular objects, such as one would use in writing code. Notable types include Symbol, MatrixSymbol, MatrixElement, and Indexed. Types that subclass these types are also supported.
rhs : Expr
SymPy object representing the rhs of the expression. This can be any type, provided its shape corresponds to that of the lhs. For example, a Matrix type can be assigned to MatrixSymbol, but not to Symbol, as the dimensions will not align.
Examples
>>> from sympy import symbols, MatrixSymbol, Matrix >>> from sympy.codegen.ast import Assignment >>> x, y, z = symbols('x, y, z') >>> Assignment(x, y) Assignment(x, y) >>> Assignment(x, 0) Assignment(x, 0) >>> A = MatrixSymbol('A', 1, 3) >>> mat = Matrix([x, y, z]).T >>> Assignment(A, mat) Assignment(A, Matrix([[x, y, z]])) >>> Assignment(A[0, 1], x) Assignment(A[0, 1], x)
- class sympy.codegen.ast.AssignmentBase(lhs, rhs)[source]
Abstract base class for Assignment and AugmentedAssignment.
Attributes:
- opstr
Symbol for assignment operator, e.g. “=”, “+=”, etc.
- class sympy.codegen.ast.Attribute(possibly parametrized)[source]
For use with
sympy.codegen.ast.Node
(which takes instances ofAttribute
asattrs
).- Parameters
name : str
parameters : Tuple
Examples
>>> from sympy.codegen.ast import Attribute >>> volatile = Attribute('volatile') >>> volatile volatile >>> print(repr(volatile)) Attribute(String('volatile')) >>> a = Attribute('foo', [1, 2, 3]) >>> a foo(1, 2, 3) >>> a.parameters == (1, 2, 3) True
- class sympy.codegen.ast.AugmentedAssignment(lhs, rhs)[source]
Base class for augmented assignments.
Attributes:
- binopstr
Symbol for binary operation being applied in the assignment, such as “+”, “*”, etc.
- class sympy.codegen.ast.BreakToken(*args, **kwargs)[source]
Represents ‘break’ in C/Python (‘exit’ in Fortran).
Use the premade instance
break_
or instantiate manually.Examples
>>> from sympy import ccode, fcode >>> from sympy.codegen.ast import break_ >>> ccode(break_) 'break' >>> fcode(break_, source_format='free') 'exit'
- class sympy.codegen.ast.CodeBlock(*args)[source]
Represents a block of code.
Explanation
For now only assignments are supported. This restriction will be lifted in the future.
Useful attributes on this object are:
left_hand_sides
:Tuple of left-hand sides of assignments, in order.
left_hand_sides
:Tuple of right-hand sides of assignments, in order.
free_symbols
: Free symbols of the expressions in the right-hand sideswhich do not appear in the left-hand side of an assignment.
Useful methods on this object are:
topological_sort
:Class method. Return a CodeBlock with assignments sorted so that variables are assigned before they are used.
cse
:Return a new CodeBlock with common subexpressions eliminated and pulled out as assignments.
Examples
>>> from sympy import symbols, ccode >>> from sympy.codegen.ast import CodeBlock, Assignment >>> x, y = symbols('x y') >>> c = CodeBlock(Assignment(x, 1), Assignment(y, x + 1)) >>> print(ccode(c)) x = 1; y = x + 1;
- cse(symbols=None, optimizations=None, postprocess=None, order='canonical')[source]
Return a new code block with common subexpressions eliminated.
Explanation
See the docstring of
sympy.simplify.cse_main.cse()
for more information.Examples
>>> from sympy import symbols, sin >>> from sympy.codegen.ast import CodeBlock, Assignment >>> x, y, z = symbols('x y z')
>>> c = CodeBlock( ... Assignment(x, 1), ... Assignment(y, sin(x) + 1), ... Assignment(z, sin(x) - 1), ... ) ... >>> c.cse() CodeBlock( Assignment(x, 1), Assignment(x0, sin(x)), Assignment(y, x0 + 1), Assignment(z, x0 - 1) )
- classmethod topological_sort(assignments)[source]
Return a CodeBlock with topologically sorted assignments so that variables are assigned before they are used.
Examples
>>> from sympy import symbols >>> from sympy.codegen.ast import CodeBlock, Assignment >>> x, y, z = symbols('x y z')
>>> assignments = [ ... Assignment(x, y + z), ... Assignment(y, z + 1), ... Assignment(z, 2), ... ] >>> CodeBlock.topological_sort(assignments) CodeBlock( Assignment(z, 2), Assignment(y, z + 1), Assignment(x, y + z) )
- class sympy.codegen.ast.ComplexType(*args, **kwargs)[source]
Represents a complex floating point number.
- class sympy.codegen.ast.ContinueToken(*args, **kwargs)[source]
Represents ‘continue’ in C/Python (‘cycle’ in Fortran)
Use the premade instance
continue_
or instantiate manually.Examples
>>> from sympy import ccode, fcode >>> from sympy.codegen.ast import continue_ >>> ccode(continue_) 'continue' >>> fcode(continue_, source_format='free') 'cycle'
- class sympy.codegen.ast.Declaration(*args, **kwargs)[source]
Represents a variable declaration
- Parameters
variable : Variable
Examples
>>> from sympy.codegen.ast import Declaration, NoneToken, untyped >>> z = Declaration('z') >>> z.variable.type == untyped True >>> # value is special NoneToken() which must be tested with == operator >>> z.variable.value is None # won't work False >>> z.variable.value == None # not PEP-8 compliant True >>> z.variable.value == NoneToken() # OK True
- class sympy.codegen.ast.Element(*args, **kwargs)[source]
Element in (a possibly N-dimensional) array.
Examples
>>> from sympy.codegen.ast import Element >>> elem = Element('x', 'ijk') >>> elem.symbol.name == 'x' True >>> elem.indices (i, j, k) >>> from sympy import ccode >>> ccode(elem) 'x[i][j][k]' >>> ccode(Element('x', 'ijk', strides='lmn', offset='o')) 'x[i*l + j*m + k*n + o]'
- class sympy.codegen.ast.FloatBaseType(*args, **kwargs)[source]
Represents a floating point number type.
- cast_nocheck[source]
alias of
sympy.core.numbers.Float
- class sympy.codegen.ast.FloatType(*args, **kwargs)[source]
Represents a floating point type with fixed bit width.
Base 2 & one sign bit is assumed.
- Parameters
name : str
Name of the type.
nbits : integer
Number of bits used (storage).
nmant : integer
Number of bits used to represent the mantissa.
nexp : integer
Number of bits used to represent the mantissa.
Examples
>>> from sympy import S >>> from sympy.codegen.ast import FloatType >>> half_precision = FloatType('f16', nbits=16, nmant=10, nexp=5) >>> half_precision.max 65504 >>> half_precision.tiny == S(2)**-14 True >>> half_precision.eps == S(2)**-10 True >>> half_precision.dig == 3 True >>> half_precision.decimal_dig == 5 True >>> half_precision.cast_check(1.0) 1.0 >>> half_precision.cast_check(1e5) Traceback (most recent call last): ... ValueError: Maximum value for data type smaller than new value.
- property decimal_dig
Number of digits needed to store & load without loss.
Explanation
Number of decimal digits needed to guarantee that two consecutive conversions (float -> text -> float) to be idempotent. This is useful when one do not want to loose precision due to rounding errors when storing a floating point value as text.
- property dig
Number of decimal digits that are guaranteed to be preserved in text.
When converting text -> float -> text, you are guaranteed that at least
dig
number of digits are preserved with respect to rounding or overflow.
- property eps
Difference between 1.0 and the next representable value.
- property max
Maximum value representable.
- property max_exponent
The largest positive number n, such that 2**(n - 1) is a representable finite value.
- property min_exponent
The lowest negative number n, such that 2**(n - 1) is a valid normalized number.
- property tiny
The minimum positive normalized value.
- class sympy.codegen.ast.For(*args, **kwargs)[source]
Represents a ‘for-loop’ in the code.
- Expressions are of the form:
- “for target in iter:
body…”
- Parameters
target : symbol
iter : iterable body : CodeBlock or iterable
! When passed an iterable it is used to instantiate a CodeBlock.
Examples
>>> from sympy import symbols, Range >>> from sympy.codegen.ast import aug_assign, For >>> x, i, j, k = symbols('x i j k') >>> for_i = For(i, Range(10), [aug_assign(x, '+', i*j*k)]) >>> for_i For(i, iterable=Range(0, 10, 1), body=CodeBlock( AddAugmentedAssignment(x, i*j*k) )) >>> for_ji = For(j, Range(7), [for_i]) >>> for_ji For(j, iterable=Range(0, 7, 1), body=CodeBlock( For(i, iterable=Range(0, 10, 1), body=CodeBlock( AddAugmentedAssignment(x, i*j*k) )) )) >>> for_kji =For(k, Range(5), [for_ji]) >>> for_kji For(k, iterable=Range(0, 5, 1), body=CodeBlock( For(j, iterable=Range(0, 7, 1), body=CodeBlock( For(i, iterable=Range(0, 10, 1), body=CodeBlock( AddAugmentedAssignment(x, i*j*k) )) )) ))
- class sympy.codegen.ast.FunctionCall(*args, **kwargs)[source]
Represents a call to a function in the code.
- Parameters
name : str
function_args : Tuple
Examples
>>> from sympy.codegen.ast import FunctionCall >>> from sympy import pycode >>> fcall = FunctionCall('foo', 'bar baz'.split()) >>> print(pycode(fcall)) foo(bar, baz)
- class sympy.codegen.ast.FunctionDefinition(*args, **kwargs)[source]
Represents a function definition in the code.
- Parameters
return_type : Type
name : str
parameters: iterable of Variable instances
body : CodeBlock or iterable
attrs : iterable of Attribute instances
Examples
>>> from sympy import ccode, symbols >>> from sympy.codegen.ast import real, FunctionPrototype >>> x, y = symbols('x y', real=True) >>> fp = FunctionPrototype(real, 'foo', [x, y]) >>> ccode(fp) 'double foo(double x, double y)' >>> from sympy.codegen.ast import FunctionDefinition, Return >>> body = [Return(x*y)] >>> fd = FunctionDefinition.from_FunctionPrototype(fp, body) >>> print(ccode(fd)) double foo(double x, double y){ return x*y; }
- class sympy.codegen.ast.FunctionPrototype(*args, **kwargs)[source]
Represents a function prototype
Allows the user to generate forward declaration in e.g. C/C++.
- Parameters
return_type : Type
name : str
parameters: iterable of Variable instances
attrs : iterable of Attribute instances
Examples
>>> from sympy import ccode, symbols >>> from sympy.codegen.ast import real, FunctionPrototype >>> x, y = symbols('x y', real=True) >>> fp = FunctionPrototype(real, 'foo', [x, y]) >>> ccode(fp) 'double foo(double x, double y)'
- class sympy.codegen.ast.IntBaseType(*args, **kwargs)[source]
Integer base type, contains no size information.
- class sympy.codegen.ast.Node(*args, **kwargs)[source]
Subclass of Token, carrying the attribute ‘attrs’ (Tuple)
Examples
>>> from sympy.codegen.ast import Node, value_const, pointer_const >>> n1 = Node([value_const]) >>> n1.attr_params('value_const') # get the parameters of attribute (by name) () >>> from sympy.codegen.fnodes import dimension >>> n2 = Node([value_const, dimension(5, 3)]) >>> n2.attr_params(value_const) # get the parameters of attribute (by Attribute instance) () >>> n2.attr_params('dimension') # get the parameters of attribute (by name) (5, 3) >>> n2.attr_params(pointer_const) is None True
- class sympy.codegen.ast.NoneToken(*args, **kwargs)[source]
The AST equivalence of Python’s NoneType
The corresponding instance of Python’s
None
isnone
.Examples
>>> from sympy.codegen.ast import none, Variable >>> from sympy import pycode >>> print(pycode(Variable('x').as_Declaration(value=none))) x = None
- class sympy.codegen.ast.Pointer(*args, **kwargs)[source]
Represents a pointer. See
Variable
.Examples
Can create instances of
Element
:>>> from sympy import Symbol >>> from sympy.codegen.ast import Pointer >>> i = Symbol('i', integer=True) >>> p = Pointer('x') >>> p[i+1] Element(x, indices=(i + 1,))
- class sympy.codegen.ast.Print(*args, **kwargs)[source]
Represents print command in the code.
- Parameters
formatstring : str
*args : Basic instances (or convertible to such through sympify)
Examples
>>> from sympy.codegen.ast import Print >>> from sympy import pycode >>> print(pycode(Print('x y'.split(), "coordinate: %12.5g %12.5g"))) print("coordinate: %12.5g %12.5g" % (x, y))
- class sympy.codegen.ast.QuotedString(*args, **kwargs)[source]
Represents a string which should be printed with quotes.
- class sympy.codegen.ast.Return(*args, **kwargs)[source]
Represents a return command in the code.
- Parameters
return : Basic
Examples
>>> from sympy.codegen.ast import Return >>> from sympy.printing.pycode import pycode >>> from sympy import Symbol >>> x = Symbol('x') >>> print(pycode(Return(x))) return x
- class sympy.codegen.ast.Scope(*args, **kwargs)[source]
Represents a scope in the code.
- Parameters
body : CodeBlock or iterable
When passed an iterable it is used to instantiate a CodeBlock.
- class sympy.codegen.ast.Stream(*args, **kwargs)[source]
Represents a stream.
There are two predefined Stream instances
stdout
&stderr
.- Parameters
name : str
Examples
>>> from sympy import pycode, Symbol >>> from sympy.codegen.ast import Print, stderr, QuotedString >>> print(pycode(Print(['x'], file=stderr))) print(x, file=sys.stderr) >>> x = Symbol('x') >>> print(pycode(Print([QuotedString('x')], file=stderr))) # print literally "x" print("x", file=sys.stderr)
- class sympy.codegen.ast.String(*args, **kwargs)[source]
SymPy object representing a string.
Atomic object which is not an expression (as opposed to Symbol).
- Parameters
text : str
Examples
>>> from sympy.codegen.ast import String >>> f = String('foo') >>> f foo >>> str(f) 'foo' >>> f.text 'foo' >>> print(repr(f)) String('foo')
- class sympy.codegen.ast.Token(*args, **kwargs)[source]
Base class for the AST types.
Explanation
Defining fields are set in
__slots__
. Attributes (defined in __slots__) are only allowed to contain instances of Basic (unless atomic, seeString
). The arguments to__new__()
correspond to the attributes in the order defined in__slots__`. The ``defaults
class attribute is a dictionary mapping attribute names to their default values.Subclasses should not need to override the
__new__()
method. They may define a class or static method named_construct_<attr>
for each attribute to process the value passed to__new__()
. Attributes listed in the class attributenot_in_args
are not passed toBasic
.
- class sympy.codegen.ast.Type(*args, **kwargs)[source]
Represents a type.
- Parameters
name : str
Name of the type, e.g.
object
,int16
,float16
(where the latter two would use theType
sub-classesIntType
andFloatType
respectively). If aType
instance is given, the said instance is returned.
Explanation
The naming is a super-set of NumPy naming. Type has a classmethod
from_expr
which offer type deduction. It also has a methodcast_check
which casts the argument to its type, possibly raising an exception if rounding error is not within tolerances, or if the value is not representable by the underlying data type (e.g. unsigned integers).Examples
>>> from sympy.codegen.ast import Type >>> t = Type.from_expr(42) >>> t integer >>> print(repr(t)) IntBaseType(String('integer')) >>> from sympy.codegen.ast import uint8 >>> uint8.cast_check(-1) Traceback (most recent call last): ... ValueError: Minimum value for data type bigger than new value. >>> from sympy.codegen.ast import float32 >>> v6 = 0.123456 >>> float32.cast_check(v6) 0.123456 >>> v10 = 12345.67894 >>> float32.cast_check(v10) Traceback (most recent call last): ... ValueError: Casting gives a significantly different value. >>> boost_mp50 = Type('boost::multiprecision::cpp_dec_float_50') >>> from sympy import cxxcode >>> from sympy.codegen.ast import Declaration, Variable >>> cxxcode(Declaration(Variable('x', type=boost_mp50))) 'boost::multiprecision::cpp_dec_float_50 x'
References
- cast_check(value, rtol=None, atol=0, precision_targets=None)[source]
Casts a value to the data type of the instance.
- Parameters
value : number
rtol : floating point number
Relative tolerance. (will be deduced if not given).
atol : floating point number
Absolute tolerance (in addition to
rtol
).type_aliases : dict
Maps substitutions for Type, e.g. {integer: int64, real: float32}
Examples
>>> from sympy.codegen.ast import integer, float32, int8 >>> integer.cast_check(3.0) == 3 True >>> float32.cast_check(1e-40) Traceback (most recent call last): ... ValueError: Minimum value for data type bigger than new value. >>> int8.cast_check(256) Traceback (most recent call last): ... ValueError: Maximum value for data type smaller than new value. >>> v10 = 12345.67894 >>> float32.cast_check(v10) Traceback (most recent call last): ... ValueError: Casting gives a significantly different value. >>> from sympy.codegen.ast import float64 >>> float64.cast_check(v10) 12345.67894 >>> from sympy import Float >>> v18 = Float('0.123456789012345646') >>> float64.cast_check(v18) Traceback (most recent call last): ... ValueError: Casting gives a significantly different value. >>> from sympy.codegen.ast import float80 >>> float80.cast_check(v18) 0.123456789012345649
- classmethod from_expr(expr)[source]
Deduces type from an expression or a
Symbol
.- Parameters
expr : number or SymPy object
The type will be deduced from type or properties.
- Raises
ValueError when type deduction fails.
Examples
>>> from sympy.codegen.ast import Type, integer, complex_ >>> Type.from_expr(2) == integer True >>> from sympy import Symbol >>> Type.from_expr(Symbol('z', complex=True)) == complex_ True >>> Type.from_expr(sum) Traceback (most recent call last): ... ValueError: Could not deduce type from expr.
- class sympy.codegen.ast.UnsignedIntType(*args, **kwargs)[source]
Represents an unsigned integer type.
- class sympy.codegen.ast.Variable(*args, **kwargs)[source]
Represents a variable.
- Parameters
symbol : Symbol
type : Type (optional)
Type of the variable.
attrs : iterable of Attribute instances
Will be stored as a Tuple.
Examples
>>> from sympy import Symbol >>> from sympy.codegen.ast import Variable, float32, integer >>> x = Symbol('x') >>> v = Variable(x, type=float32) >>> v.attrs () >>> v == Variable('x') False >>> v == Variable('x', type=float32) True >>> v Variable(x, type=float32)
One may also construct a
Variable
instance with the type deduced from assumptions about the symbol using thededuced
classmethod:>>> i = Symbol('i', integer=True) >>> v = Variable.deduced(i) >>> v.type == integer True >>> v == Variable('i') False >>> from sympy.codegen.ast import value_const >>> value_const in v.attrs False >>> w = Variable('w', attrs=[value_const]) >>> w Variable(w, attrs=(value_const,)) >>> value_const in w.attrs True >>> w.as_Declaration(value=42) Declaration(Variable(w, value=42, attrs=(value_const,)))
- as_Declaration(**kwargs)[source]
Convenience method for creating a Declaration instance.
Explanation
If the variable of the Declaration need to wrap a modified variable keyword arguments may be passed (overriding e.g. the
value
of the Variable instance).Examples
>>> from sympy.codegen.ast import Variable, NoneToken >>> x = Variable('x') >>> decl1 = x.as_Declaration() >>> # value is special NoneToken() which must be tested with == operator >>> decl1.variable.value is None # won't work False >>> decl1.variable.value == None # not PEP-8 compliant True >>> decl1.variable.value == NoneToken() # OK True >>> decl2 = x.as_Declaration(value=42.0) >>> decl2.variable.value == 42 True
- classmethod deduced(symbol, value=None, attrs=(), cast_check=True)[source]
Alt. constructor with type deduction from
Type.from_expr
.Deduces type primarily from
symbol
, secondarily fromvalue
.- Parameters
symbol : Symbol
value : expr
(optional) value of the variable.
attrs : iterable of Attribute instances
cast_check : bool
Whether to apply
Type.cast_check
onvalue
.
Examples
>>> from sympy import Symbol >>> from sympy.codegen.ast import Variable, complex_ >>> n = Symbol('n', integer=True) >>> str(Variable.deduced(n).type) 'integer' >>> x = Symbol('x', real=True) >>> v = Variable.deduced(x) >>> v.type real >>> z = Symbol('z', complex=True) >>> Variable.deduced(z).type == complex_ True
- class sympy.codegen.ast.While(*args, **kwargs)[source]
Represents a ‘for-loop’ in the code.
- Expressions are of the form:
- “while condition:
body…”
- Parameters
condition : expression convertible to Boolean
body : CodeBlock or iterable
When passed an iterable it is used to instantiate a CodeBlock.
Examples
>>> from sympy import symbols, Gt, Abs >>> from sympy.codegen import aug_assign, Assignment, While >>> x, dx = symbols('x dx') >>> expr = 1 - x**2 >>> whl = While(Gt(Abs(dx), 1e-9), [ ... Assignment(dx, -expr/expr.diff(x)), ... aug_assign(x, '+', dx) ... ])
- sympy.codegen.ast.aug_assign(lhs, op, rhs)[source]
Create ‘lhs op= rhs’.
- Parameters
lhs : Expr
SymPy object representing the lhs of the expression. These should be singular objects, such as one would use in writing code. Notable types include Symbol, MatrixSymbol, MatrixElement, and Indexed. Types that subclass these types are also supported.
op : str
Operator (+, -, /, *, %).
rhs : Expr
SymPy object representing the rhs of the expression. This can be any type, provided its shape corresponds to that of the lhs. For example, a Matrix type can be assigned to MatrixSymbol, but not to Symbol, as the dimensions will not align.
Explanation
Represents augmented variable assignment for code generation. This is a convenience function. You can also use the AugmentedAssignment classes directly, like AddAugmentedAssignment(x, y).
Examples
>>> from sympy import symbols >>> from sympy.codegen.ast import aug_assign >>> x, y = symbols('x, y') >>> aug_assign(x, '+', y) AddAugmentedAssignment(x, y)
Special C math functions (sympy.codegen.cfunctions)
This module contains SymPy functions mathcin corresponding to special math functions in the C standard library (since C99, also available in C++11).
The functions defined in this module allows the user to express functions such as expm1
as a SymPy function for symbolic manipulation.
- class sympy.codegen.cfunctions.Cbrt(*args)[source]
Represents the cube root function.
Explanation
The reason why one would use
Cbrt(x)
overcbrt(x)
is that the latter is internally represented asPow(x, Rational(1, 3))
which may not be what one wants when doing code-generation.Examples
>>> from sympy.abc import x >>> from sympy.codegen.cfunctions import Cbrt >>> Cbrt(x) Cbrt(x) >>> Cbrt(x).diff(x) 1/(3*x**(2/3))
See also
- class sympy.codegen.cfunctions.Sqrt(*args)[source]
Represents the square root function.
Explanation
The reason why one would use
Sqrt(x)
oversqrt(x)
is that the latter is internally represented asPow(x, S.Half)
which may not be what one wants when doing code-generation.Examples
>>> from sympy.abc import x >>> from sympy.codegen.cfunctions import Sqrt >>> Sqrt(x) Sqrt(x) >>> Sqrt(x).diff(x) 1/(2*sqrt(x))
See also
- class sympy.codegen.cfunctions.exp2(arg)[source]
Represents the exponential function with base two.
Explanation
The benefit of using
exp2(x)
over2**x
is that the latter is not as efficient under finite precision arithmetic.Examples
>>> from sympy.abc import x >>> from sympy.codegen.cfunctions import exp2 >>> exp2(2).evalf() == 4 True >>> exp2(x).diff(x) log(2)*exp2(x)
See also
- class sympy.codegen.cfunctions.expm1(arg)[source]
Represents the exponential function minus one.
Explanation
The benefit of using
expm1(x)
overexp(x) - 1
is that the latter is prone to cancellation under finite precision arithmetic when x is close to zero.Examples
>>> from sympy.abc import x >>> from sympy.codegen.cfunctions import expm1 >>> '%.0e' % expm1(1e-99).evalf() '1e-99' >>> from math import exp >>> exp(1e-99) - 1 0.0 >>> expm1(x).diff(x) exp(x)
See also
- class sympy.codegen.cfunctions.fma(*args)[source]
Represents “fused multiply add”.
Explanation
The benefit of using
fma(x, y, z)
overx*y + z
is that, under finite precision arithmetic, the former is supported by special instructions on some CPUs.Examples
>>> from sympy.abc import x, y, z >>> from sympy.codegen.cfunctions import fma >>> fma(x, y, z).diff(x) y
- class sympy.codegen.cfunctions.hypot(*args)[source]
Represents the hypotenuse function.
Explanation
The hypotenuse function is provided by e.g. the math library in the C99 standard, hence one may want to represent the function symbolically when doing code-generation.
Examples
>>> from sympy.abc import x, y >>> from sympy.codegen.cfunctions import hypot >>> hypot(3, 4).evalf() == 5 True >>> hypot(x, y) hypot(x, y) >>> hypot(x, y).diff(x) x/hypot(x, y)
- class sympy.codegen.cfunctions.log10(arg)[source]
Represents the logarithm function with base ten.
Examples
>>> from sympy.abc import x >>> from sympy.codegen.cfunctions import log10 >>> log10(100).evalf() == 2 True >>> log10(x).diff(x) 1/(x*log(10))
See also
- class sympy.codegen.cfunctions.log1p(arg)[source]
Represents the natural logarithm of a number plus one.
Explanation
The benefit of using
log1p(x)
overlog(x + 1)
is that the latter is prone to cancellation under finite precision arithmetic when x is close to zero.Examples
>>> from sympy.abc import x >>> from sympy.codegen.cfunctions import log1p >>> from sympy import expand_log >>> '%.0e' % expand_log(log1p(1e-99)).evalf() '1e-99' >>> from math import log >>> log(1 + 1e-99) 0.0 >>> log1p(x).diff(x) 1/(x + 1)
See also
- class sympy.codegen.cfunctions.log2(arg)[source]
Represents the logarithm function with base two.
Explanation
The benefit of using
log2(x)
overlog(x)/log(2)
is that the latter is not as efficient under finite precision arithmetic.Examples
>>> from sympy.abc import x >>> from sympy.codegen.cfunctions import log2 >>> log2(4).evalf() == 2 True >>> log2(x).diff(x) 1/(x*log(2))
C specific AST nodes (sympy.codegen.cnodes)
AST nodes specific to the C family of languages
- class sympy.codegen.cnodes.Label(*args, **kwargs)[source]
Label for use with e.g. goto statement.
Examples
>>> from sympy import ccode, Symbol >>> from sympy.codegen.cnodes import Label, PreIncrement >>> print(ccode(Label('foo'))) foo: >>> print(ccode(Label('bar', [PreIncrement(Symbol('a'))]))) bar: ++(a);
- class sympy.codegen.cnodes.PreDecrement(*args)[source]
Represents the pre-decrement operator
Examples
>>> from sympy.abc import x >>> from sympy.codegen.cnodes import PreDecrement >>> from sympy import ccode >>> ccode(PreDecrement(x)) '--(x)'
C++ specific AST nodes (sympy.codegen.cxxnodes)
AST nodes specific to C++.
Fortran specific AST nodes (sympy.codegen.fnodes)
AST nodes specific to Fortran.
The functions defined in this module allows the user to express functions such as dsign
as a SymPy function for symbolic manipulation.
- class sympy.codegen.fnodes.ArrayConstructor(*args, **kwargs)[source]
Represents an array constructor.
Examples
>>> from sympy import fcode >>> from sympy.codegen.fnodes import ArrayConstructor >>> ac = ArrayConstructor([1, 2, 3]) >>> fcode(ac, standard=95, source_format='free') '(/1, 2, 3/)' >>> fcode(ac, standard=2003, source_format='free') '[1, 2, 3]'
- class sympy.codegen.fnodes.Do(*args, **kwargs)[source]
Represents a Do loop in in Fortran.
Examples
>>> from sympy import fcode, symbols >>> from sympy.codegen.ast import aug_assign, Print >>> from sympy.codegen.fnodes import Do >>> i, n = symbols('i n', integer=True) >>> r = symbols('r', real=True) >>> body = [aug_assign(r, '+', 1/i), Print([i, r])] >>> do1 = Do(body, i, 1, n) >>> print(fcode(do1, source_format='free')) do i = 1, n r = r + 1d0/i print *, i, r end do >>> do2 = Do(body, i, 1, n, 2) >>> print(fcode(do2, source_format='free')) do i = 1, n, 2 r = r + 1d0/i print *, i, r end do
- class sympy.codegen.fnodes.Extent(*args)[source]
Represents a dimension extent.
Examples
>>> from sympy.codegen.fnodes import Extent >>> e = Extent(-3, 3) # -3, -2, -1, 0, 1, 2, 3 >>> from sympy import fcode >>> fcode(e, source_format='free') '-3:3' >>> from sympy.codegen.ast import Variable, real >>> from sympy.codegen.fnodes import dimension, intent_out >>> dim = dimension(e, e) >>> arr = Variable('x', real, attrs=[dim, intent_out]) >>> fcode(arr.as_Declaration(), source_format='free', standard=2003) 'real*8, dimension(-3:3, -3:3), intent(out) :: x'
- class sympy.codegen.fnodes.FortranReturn(*args, **kwargs)[source]
AST node explicitly mapped to a fortran “return”.
Explanation
Because a return statement in fortran is different from C, and in order to aid reuse of our codegen ASTs the ordinary
.codegen.ast.Return
is interpreted as assignment to the result variable of the function. If one for some reason needs to generate a fortran RETURN statement, this node should be used.Examples
>>> from sympy.codegen.fnodes import FortranReturn >>> from sympy import fcode >>> fcode(FortranReturn('x')) ' return x'
- class sympy.codegen.fnodes.GoTo(*args, **kwargs)[source]
Represents a goto statement in Fortran
Examples
>>> from sympy.codegen.fnodes import GoTo >>> go = GoTo([10, 20, 30], 'i') >>> from sympy import fcode >>> fcode(go, source_format='free') 'go to (10, 20, 30), i'
- class sympy.codegen.fnodes.ImpliedDoLoop(*args, **kwargs)[source]
Represents an implied do loop in Fortran.
Examples
>>> from sympy import Symbol, fcode >>> from sympy.codegen.fnodes import ImpliedDoLoop, ArrayConstructor >>> i = Symbol('i', integer=True) >>> idl = ImpliedDoLoop(i**3, i, -3, 3, 2) # -27, -1, 1, 27 >>> ac = ArrayConstructor([-28, idl, 28]) # -28, -27, -1, 1, 27, 28 >>> fcode(ac, standard=2003, source_format='free') '[-28, (i**3, i = -3, 3, 2), 28]'
- class sympy.codegen.fnodes.Module(*args, **kwargs)[source]
Represents a module in Fortran.
Examples
>>> from sympy.codegen.fnodes import Module >>> from sympy import fcode >>> print(fcode(Module('signallib', ['implicit none'], []), source_format='free')) module signallib implicit none contains end module
- class sympy.codegen.fnodes.Program(*args, **kwargs)[source]
Represents a ‘program’ block in Fortran.
Examples
>>> from sympy.codegen.ast import Print >>> from sympy.codegen.fnodes import Program >>> prog = Program('myprogram', [Print([42])]) >>> from sympy import fcode >>> print(fcode(prog, source_format='free')) program myprogram print *, 42 end program
- class sympy.codegen.fnodes.Subroutine(*args, **kwargs)[source]
Represents a subroutine in Fortran.
Examples
>>> from sympy import fcode, symbols >>> from sympy.codegen.ast import Print >>> from sympy.codegen.fnodes import Subroutine >>> x, y = symbols('x y', real=True) >>> sub = Subroutine('mysub', [x, y], [Print([x**2 + y**2, x*y])]) >>> print(fcode(sub, source_format='free', standard=2003)) subroutine mysub(x, y) real*8 :: x real*8 :: y print *, x**2 + y**2, x*y end subroutine
- class sympy.codegen.fnodes.SubroutineCall(*args, **kwargs)[source]
Represents a call to a subroutine in Fortran.
Examples
>>> from sympy.codegen.fnodes import SubroutineCall >>> from sympy import fcode >>> fcode(SubroutineCall('mysub', 'x y'.split())) ' call mysub(x, y)'
- sympy.codegen.fnodes.allocated(array)[source]
Creates an AST node for a function call to Fortran’s “allocated(…)”
Examples
>>> from sympy import fcode >>> from sympy.codegen.fnodes import allocated >>> alloc = allocated('x') >>> fcode(alloc, source_format='free') 'allocated(x)'
- sympy.codegen.fnodes.array(symbol, dim, intent=None, *, attrs=(), value=None, type=None)[source]
Convenience function for creating a Variable instance for a Fortran array.
- Parameters
symbol : symbol
dim : Attribute or iterable
If dim is an
Attribute
it need to have the name ‘dimension’. If it is not anAttribute
, then it is passsed todimension()
as*dim
intent : str
One of: ‘in’, ‘out’, ‘inout’ or None
**kwargs:
Keyword arguments for
Variable
(‘type’ & ‘value’)
Examples
>>> from sympy import fcode >>> from sympy.codegen.ast import integer, real >>> from sympy.codegen.fnodes import array >>> arr = array('a', '*', 'in', type=integer) >>> print(fcode(arr.as_Declaration(), source_format='free', standard=2003)) integer*4, dimension(*), intent(in) :: a >>> x = array('x', [3, ':', ':'], intent='out', type=real) >>> print(fcode(x.as_Declaration(value=1), source_format='free', standard=2003)) real*8, dimension(3, :, :), intent(out) :: x = 1
- sympy.codegen.fnodes.bind_C(name=None)[source]
Creates an Attribute
bind_C
with a name.- Parameters
name : str
Examples
>>> from sympy import fcode, Symbol >>> from sympy.codegen.ast import FunctionDefinition, real, Return >>> from sympy.codegen.fnodes import array, sum_, bind_C >>> a = Symbol('a', real=True) >>> s = Symbol('s', integer=True) >>> arr = array(a, dim=[s], intent='in') >>> body = [Return((sum_(a**2)/s)**.5)] >>> fd = FunctionDefinition(real, 'rms', [arr, s], body, attrs=[bind_C('rms')]) >>> print(fcode(fd, source_format='free', standard=2003)) real*8 function rms(a, s) bind(C, name="rms") real*8, dimension(s), intent(in) :: a integer*4 :: s rms = sqrt(sum(a**2)/s) end function
- sympy.codegen.fnodes.dimension(*args)[source]
Creates a ‘dimension’ Attribute with (up to 7) extents.
Examples
>>> from sympy import fcode >>> from sympy.codegen.fnodes import dimension, intent_in >>> dim = dimension('2', ':') # 2 rows, runtime determined number of columns >>> from sympy.codegen.ast import Variable, integer >>> arr = Variable('a', integer, attrs=[dim, intent_in]) >>> fcode(arr.as_Declaration(), source_format='free', standard=2003) 'integer*4, dimension(2, :), intent(in) :: a'
- class sympy.codegen.fnodes.dsign(*args)[source]
Fortran sign intrinsic for double precision arguments.
- sympy.codegen.fnodes.lbound(array, dim=None, kind=None)[source]
Creates an AST node for a function call to Fortran’s “lbound(…)”
- Parameters
array : Symbol or String
dim : expr
kind : expr
Examples
>>> from sympy import fcode >>> from sympy.codegen.fnodes import lbound >>> lb = lbound('arr', dim=2) >>> fcode(lb, source_format='free') 'lbound(arr, 2)'
- class sympy.codegen.fnodes.literal_dp(num, dps=None, precision=None)[source]
Fortran double precision real literal
- class sympy.codegen.fnodes.literal_sp(num, dps=None, precision=None)[source]
Fortran single precision real literal
- sympy.codegen.fnodes.reshape(source, shape, pad=None, order=None)[source]
Creates an AST node for a function call to Fortran’s “reshape(…)”
- Parameters
source : Symbol or String
shape : ArrayExpr
- sympy.codegen.fnodes.shape(source, kind=None)[source]
Creates an AST node for a function call to Fortran’s “shape(…)”
- Parameters
source : Symbol or String
kind : expr
Examples
>>> from sympy import fcode >>> from sympy.codegen.fnodes import shape >>> shp = shape('x') >>> fcode(shp, source_format='free') 'shape(x)'
- sympy.codegen.fnodes.size(array, dim=None, kind=None)[source]
Creates an AST node for a function call to Fortran’s “size(…)”
Examples
>>> from sympy import fcode, Symbol >>> from sympy.codegen.ast import FunctionDefinition, real, Return >>> from sympy.codegen.fnodes import array, sum_, size >>> a = Symbol('a', real=True) >>> body = [Return((sum_(a**2)/size(a))**.5)] >>> arr = array(a, dim=[':'], intent='in') >>> fd = FunctionDefinition(real, 'rms', [arr], body) >>> print(fcode(fd, source_format='free', standard=2003)) real*8 function rms(a) real*8, dimension(:), intent(in) :: a rms = sqrt(sum(a**2)*1d0/size(a)) end function
- class sympy.codegen.fnodes.use(*args, **kwargs)[source]
Represents a use statement in Fortran.
Examples
>>> from sympy.codegen.fnodes import use >>> from sympy import fcode >>> fcode(use('signallib'), source_format='free') 'use signallib' >>> fcode(use('signallib', [('metric', 'snr')]), source_format='free') 'use signallib, metric => snr' >>> fcode(use('signallib', only=['snr', 'convolution2d']), source_format='free') 'use signallib, only: snr, convolution2d'
- class sympy.codegen.fnodes.use_rename(*args, **kwargs)[source]
Represents a renaming in a use statement in Fortran.
Examples
>>> from sympy.codegen.fnodes import use_rename, use >>> from sympy import fcode >>> ren = use_rename("thingy", "convolution2d") >>> print(fcode(ren, source_format='free')) thingy => convolution2d >>> full = use('signallib', only=['snr', ren]) >>> print(fcode(full, source_format='free')) use signallib, only: snr, thingy => convolution2d
Algorithms (sympy.codegen.algorithms)
- sympy.codegen.algorithms.newtons_method(expr, wrt, atol=1e-12, delta=None, debug=False, itermax=None, counter=None)[source]
Generates an AST for Newton-Raphson method (a root-finding algorithm).
- Parameters
expr : expression
wrt : Symbol
With respect to, i.e. what is the variable.
atol : number or expr
Absolute tolerance (stopping criterion)
delta : Symbol
Will be a
Dummy
ifNone
.debug : bool
Whether to print convergence information during iterations
itermax : number or expr
Maximum number of iterations.
counter : Symbol
Will be a
Dummy
ifNone
.
Explanation
Returns an abstract syntax tree (AST) based on
sympy.codegen.ast
for Netwon’s method of root-finding.Examples
>>> from sympy import symbols, cos >>> from sympy.codegen.ast import Assignment >>> from sympy.codegen.algorithms import newtons_method >>> x, dx, atol = symbols('x dx atol') >>> expr = cos(x) - x**3 >>> algo = newtons_method(expr, x, atol, dx) >>> algo.has(Assignment(dx, -expr/expr.diff(x))) True
References
- sympy.codegen.algorithms.newtons_method_function(expr, wrt, params=None, func_name='newton', attrs=(), *, delta=None, **kwargs)[source]
Generates an AST for a function implementing the Newton-Raphson method.
- Parameters
expr : expression
wrt : Symbol
With respect to, i.e. what is the variable
params : iterable of symbols
Symbols appearing in expr that are taken as constants during the iterations (these will be accepted as parameters to the generated function).
func_name : str
Name of the generated function.
attrs : Tuple
Attribute instances passed as
attrs
toFunctionDefinition
.**kwargs :
Keyword arguments passed to
sympy.codegen.algorithms.newtons_method()
.
Examples
>>> from sympy import symbols, cos >>> from sympy.codegen.algorithms import newtons_method_function >>> from sympy.codegen.pyutils import render_as_module >>> x = symbols('x') >>> expr = cos(x) - x**3 >>> func = newtons_method_function(expr, x) >>> py_mod = render_as_module(func) # source code as string >>> namespace = {} >>> exec(py_mod, namespace, namespace) >>> res = eval('newton(0.5)', namespace) >>> abs(res - 0.865474033102) < 1e-12 True
Python utilities (sympy.codegen.pyutils)
- sympy.codegen.pyutils.render_as_module(content, standard='python3')[source]
Renders Python code as a module (with the required imports).
- Parameters
standard :
See the parameter
standard
insympy.printing.pycode.pycode()
C utilities (sympy.codegen.cutils)
Fortran utilities (sympy.codegen.futils)
- sympy.codegen.futils.render_as_module(definitions, name, declarations=(), printer_settings=None)[source]
Creates a
Module
instance and renders it as a string.This generates Fortran source code for a module with the correct
use
statements.- Parameters
definitions : iterable
Passed to
sympy.codegen.fnodes.Module
.name : str
Passed to
sympy.codegen.fnodes.Module
.declarations : iterable
Passed to
sympy.codegen.fnodes.Module
. It will be extended with use statements, ‘implicit none’ and public list generated fromdefinitions
.printer_settings : dict
Passed to
FCodePrinter
(default:{'standard': 2003, 'source_format': 'free'}
).