cdb.sympy.solvers
Access to Sympy solver functions.
These are simple wrappers to make Sympy solvers available from Cadabra, using
Cadabra expressions. There are some differences in the input and output of these
functions with respect to Sympy, mainly to make things more consistent.dsolve(Ex: equation(s), Ex: function(s)) -> Ex
Solve (system of) ordinary differential equation(s).
Given a single differential equation or a system of differential equations, return the solution. The second argument
is optional; if not given, dsolve
will try to figure out the unknown(s). Functions can
have implicit dependence on the independent variable, as in the example below, where $f$ depends on $x$
but that dependence is not spelled out again explicitly in the equation.
Note that since this is backed by sympy.dsolve
, and the latter is rather buggy for systems
of differential equations, do not expect miracles in that case.import sympy
x::Coordinate;
{f,g}::Depends(x);
\partial{#}::PartialDerivative;
ex:= \partial_{x x}{f} + \partial_{x}{f} = \sin(x);
sol=dsolve(ex, $f$);
\(\displaystyle{}\text{Attached property Coordinate to }x.\)
\(\displaystyle{}\text{Attached property Depends to }\left[f, g\right].\)
\(\displaystyle{}\text{Attached property PartialDerivative to }\partial{\#}.\)
\(\displaystyle{}\partial_{x x}{f}+\partial_{x}{f} = \sin{x}\)
\partial_{x x}(f) + \partial_{x}(f) = \sin(x)
\(\displaystyle{}f = {C_{1}}+{C_{2}} \exp\left(-x\right) - \frac{1}{2}\sin{x} - \frac{1}{2}\cos{x}\)
f = C1 + C2 \exp(-x) - 1/2 \sin(x) - 1/2 \cos(x)
substitute(ex, sol);
\(\displaystyle{}\partial_{x x}\left({C_{1}}+{C_{2}} \exp\left(-x\right) - \frac{1}{2}\sin{x} - \frac{1}{2}\cos{x}\right)+\partial_{x}\left({C_{1}}+{C_{2}} \exp\left(-x\right) - \frac{1}{2}\sin{x} - \frac{1}{2}\cos{x}\right) = \sin{x}\)
\partial_{x x}((C1 + C2 \exp(-x) - 1/2 \sin(x) - 1/2 \cos(x))) + \partial_{x}((C1 + C2 \exp(-x) - 1/2 \sin(x) - 1/2 \cos(x))) = \sin(x)
map_sympy(ex, "simplify");
\(\displaystyle{}\sin{x} = \sin{x}\)
\sin(x) = \sin(x)
st:= { \partial_{x}{f} = f g \sin(x), \partial_{x}{g} = g**2 \sin(x) };
\(\displaystyle{}\left[\partial_{x}{f} = f g \sin{x}, \partial_{x}{g} = {g}^{2} \sin{x}\right]\)
{\partial_{x}(f) = f g \sin(x), \partial_{x}(g) = (g)**2 \sin(x)}
dsolve(st);
\(\displaystyle{}\left[f = -\exp\left({C_{1}}\right) {\left({C_{2}} \exp\left({C_{1}}\right)-\cos{x}\right)}^{-1}, g = -{\left({C_{1}}-\cos{x}\right)}^{-1}\right]\)
{f = -\exp(C1) (C2 \exp(C1)-\cos(x))**(-1), g = -(C1-\cos(x))**(-1)}
linsolve(Ex: equations, Ex: objects) -> Ex
Solve a linear system for unknowns.
Given an expression containing one or more linear equations, and an second expression containing
the variables to solve for, determine the solutions. Returns list with one element, which is a
list of substitution rules for the variables that have been solved for (the output is a nested
list to ensure that it is similar to the output of nonlinsolve
).ex:= x + a = 0;
sol=linsolve(ex, $x$);
\(\displaystyle{}x+a = 0\)
x + a = 0
\(\displaystyle{}\left[\left[x \rightarrow -a\right]\right]\)
{{x -> -a}}
substitute(ex, sol[0]);
\(\displaystyle{}0 = 0\)
0 = 0
ex:= x + 1 = 0, y + 4/3 x + 2 a = 0;
sol=linsolve(ex, $x,y$);
\(\displaystyle{}\left[x+1 = 0, y+\frac{4}{3}x+2a = 0\right]\)
{x + 1 = 0, y + 4/3 x + 2a = 0}
\(\displaystyle{}\left[\left[x \rightarrow -1, y \rightarrow \frac{4}{3}-2a\right]\right]\)
{{x -> -1, y -> 4/3 -2a}}
substitute(ex, sol[0]);
\(\displaystyle{}\left[0 = 0, 0 = 0\right]\)
{0 = 0, 0 = 0}
ex:= x A_{m} C^{m} + b D_{n p} G^{n p} = 0;
linsolve(ex, $x$);
\(\displaystyle{}x A_{m} C^{m}+b D_{n p} G^{n p} = 0\)
x A_{m} C^{m} + b D_{n p} G^{n p} = 0
\(\displaystyle{}\left[\left[x \rightarrow -b D_{n p} G^{n p} {\left(A_{m} C^{m}\right)}^{-1}\right]\right]\)
{{x -> -b D_{n p} G^{n p} (A_{m} C^{m})**(-1)}}
nonlinsolve
Solve a system of non-linear equations.
Given an expression containing one or more linear equations, and an second expression containing
the variables to solve for, determine the solutions. Returns a substitution rule or a list of
substitution rules for the variables that have been solved for.ex:= x**2-1;
sol=nonlinsolve(ex, $x$);
\(\displaystyle{}{x}^{2}-1\)
(x)**2-1
\(\displaystyle{}\left[\left[x \rightarrow -1\right], \left[x \rightarrow 1\right]\right]\)
{{x -> -1}, {x -> 1}}
substitute(ex, sol[0])
simplify(ex);
\(\displaystyle{}0\)
0
ex:= x**2 + A_{m} B^{m} = 0;
sol=nonlinsolve(ex, $x$);
\(\displaystyle{}{x}^{2}+A_{m} B^{m} = 0\)
(x)**2 + A_{m} B^{m} = 0
\(\displaystyle{}\left[\left[x \rightarrow \sqrt{-A_{m} B^{m}}\right], \left[x \rightarrow -\sqrt{-A_{m} B^{m}}\right]\right]\)
{{x -> (-A_{m} B^{m})**( 1/2 )}, {x -> -(-A_{m} B^{m})**( 1/2 )}}
ex:= { x**2 - 1 = 0, y**2 - 2 =0 };
nonlinsolve(ex, $x,y$);
\(\displaystyle{}\left[{x}^{2}-1 = 0, {y}^{2}-2 = 0\right]\)
{(x)**2-1 = 0, (y)**2-2 = 0}
\(\displaystyle{}\left[\left[x \rightarrow -1, y \rightarrow \sqrt{2}\right], \left[x \rightarrow 1, y \rightarrow \sqrt{2}\right], \left[x \rightarrow -1, y \rightarrow -\sqrt{2}\right], \left[x \rightarrow 1, y \rightarrow -\sqrt{2}\right]\right]\)
{{x -> -1, y -> 2**( 1/2 )}, {x -> 1, y -> 2**( 1/2 )}, {x -> -1, y -> -2**( 1/2 )}, {x -> 1, y -> -2**( 1/2 )}}
solve
Solve an equation or system of equations.
Given an expression containing one or more equations, and an second expression containing
the variables to solve for, determine the solutions. Returns a list of substitution rules for the
variables that have been solved for.solve($x**2=y$, $x$);
\(\displaystyle{}\left[\left[x \rightarrow \left(-\sqrt{y}\right)\right], \left[x \rightarrow \left(\sqrt{y}\right)\right]\right]\)
{{x -> (-(y)**( 1/2 ))}, {x -> ((y)**( 1/2 ))}}