Elementary¶
This module implements elementary functions such as trigonometric, hyperbolic, and
sqrt, as well as functions like Abs
, Max
, Min
etc.
Complex Functions¶
- class sympy.functions.elementary.complexes.re(arg)[source]¶
Returns real part of expression. This function performs only elementary analysis and so it will fail to decompose properly more complicated expressions. If completely simplified result is needed then use
Basic.as_real_imag()
or perform complex expansion on instance of this function.- Parameters:
arg : Expr
Real or complex expression.
- Returns:
expr : Expr
Real part of expression.
Examples
>>> from sympy import re, im, I, E, symbols >>> x, y = symbols('x y', real=True) >>> re(2*E) 2*E >>> re(2*I + 17) 17 >>> re(2*I) 0 >>> re(im(x) + x*I + 2) 2 >>> re(5 + I + 2) 7
See also
- class sympy.functions.elementary.complexes.im(arg)[source]¶
Returns imaginary part of expression. This function performs only elementary analysis and so it will fail to decompose properly more complicated expressions. If completely simplified result is needed then use
Basic.as_real_imag()
or perform complex expansion on instance of this function.- Parameters:
arg : Expr
Real or complex expression.
- Returns:
expr : Expr
Imaginary part of expression.
Examples
>>> from sympy import re, im, E, I >>> from sympy.abc import x, y >>> im(2*E) 0 >>> im(2*I + 17) 2 >>> im(x*I) re(x) >>> im(re(x) + y) im(y) >>> im(2 + 3*I) 3
See also
- class sympy.functions.elementary.complexes.sign(arg)[source]¶
Returns the complex sign of an expression:
- Parameters:
arg : Expr
Real or imaginary expression.
- Returns:
expr : Expr
Complex sign of expression.
Explanation
If the expression is real the sign will be:
\(1\) if expression is positive
\(0\) if expression is equal to zero
\(-1\) if expression is negative
If the expression is imaginary the sign will be:
\(I\) if im(expression) is positive
\(-I\) if im(expression) is negative
Otherwise an unevaluated expression will be returned. When evaluated, the result (in general) will be
cos(arg(expr)) + I*sin(arg(expr))
.Examples
>>> from sympy import sign, I
>>> sign(-1) -1 >>> sign(0) 0 >>> sign(-3*I) -I >>> sign(1 + I) sign(1 + I) >>> _.evalf() 0.707106781186548 + 0.707106781186548*I
- class sympy.functions.elementary.complexes.Abs(arg)[source]¶
Return the absolute value of the argument.
- Parameters:
arg : Expr
Real or complex expression.
- Returns:
expr : Expr
Absolute value returned can be an expression or integer depending on input arg.
Explanation
This is an extension of the built-in function
abs()
to accept symbolic values. If you pass a SymPy expression to the built-inabs()
, it will pass it automatically toAbs()
.Examples
>>> from sympy import Abs, Symbol, S, I >>> Abs(-1) 1 >>> x = Symbol('x', real=True) >>> Abs(-x) Abs(x) >>> Abs(x**2) x**2 >>> abs(-x) # The Python built-in Abs(x) >>> Abs(3*x + 2*I) sqrt(9*x**2 + 4) >>> Abs(8*I) 8
Note that the Python built-in will return either an Expr or int depending on the argument:
>>> type(abs(-1)) <... 'int'> >>> type(abs(S.NegativeOne)) <class 'sympy.core.numbers.One'>
Abs will always return a SymPy object.
- class sympy.functions.elementary.complexes.arg(arg)[source]¶
Returns the argument (in radians) of a complex number. The argument is evaluated in consistent convention with
atan2
where the branch-cut is taken along the negative real axis andarg(z)
is in the interval \((-\pi,\pi]\). For a positive number, the argument is always 0; the argument of a negative number is \(\pi\); and the argument of 0 is undefined and returnsnan
. So thearg
function will never nest greater than 3 levels since at the 4th application, the result must be nan; for a real number, nan is returned on the 3rd application.- Parameters:
arg : Expr
Real or complex expression.
- Returns:
value : Expr
Returns arc tangent of arg measured in radians.
Examples
>>> from sympy import arg, I, sqrt, Dummy >>> from sympy.abc import x >>> arg(2.0) 0 >>> arg(I) pi/2 >>> arg(sqrt(2) + I*sqrt(2)) pi/4 >>> arg(sqrt(3)/2 + I/2) pi/6 >>> arg(4 + 3*I) atan(3/4) >>> arg(0.8 + 0.6*I) 0.643501108793284 >>> arg(arg(arg(arg(x)))) nan >>> real = Dummy(real=True) >>> arg(arg(arg(real))) nan
- class sympy.functions.elementary.complexes.conjugate(arg)[source]¶
Returns the complex conjugate [R276] of an argument. In mathematics, the complex conjugate of a complex number is given by changing the sign of the imaginary part.
Thus, the conjugate of the complex number \(a + ib\) (where \(a\) and \(b\) are real numbers) is \(a - ib\)
- Parameters:
arg : Expr
Real or complex expression.
- Returns:
arg : Expr
Complex conjugate of arg as real, imaginary or mixed expression.
Examples
>>> from sympy import conjugate, I >>> conjugate(2) 2 >>> conjugate(I) -I >>> conjugate(3 + 2*I) 3 - 2*I >>> conjugate(5 - I) 5 + I
References
- class sympy.functions.elementary.complexes.polar_lift(arg)[source]¶
Lift argument to the Riemann surface of the logarithm, using the standard branch.
- Parameters:
arg : Expr
Real or complex expression.
Examples
>>> from sympy import Symbol, polar_lift, I >>> p = Symbol('p', polar=True) >>> x = Symbol('x') >>> polar_lift(4) 4*exp_polar(0) >>> polar_lift(-4) 4*exp_polar(I*pi) >>> polar_lift(-I) exp_polar(-I*pi/2) >>> polar_lift(I + 2) polar_lift(2 + I)
>>> polar_lift(4*x) 4*polar_lift(x) >>> polar_lift(4*p) 4*p
- class sympy.functions.elementary.complexes.periodic_argument(ar, period)[source]¶
Represent the argument on a quotient of the Riemann surface of the logarithm. That is, given a period \(P\), always return a value in \((-P/2, P/2]\), by using \(\exp(PI) = 1\).
- Parameters:
ar : Expr
A polar number.
period : Expr
The period \(P\).
Examples
>>> from sympy import exp_polar, periodic_argument >>> from sympy import I, pi >>> periodic_argument(exp_polar(10*I*pi), 2*pi) 0 >>> periodic_argument(exp_polar(5*I*pi), 4*pi) pi >>> from sympy import exp_polar, periodic_argument >>> from sympy import I, pi >>> periodic_argument(exp_polar(5*I*pi), 2*pi) pi >>> periodic_argument(exp_polar(5*I*pi), 3*pi) -pi >>> periodic_argument(exp_polar(5*I*pi), pi) 0
See also
sympy.functions.elementary.exponential.exp_polar
polar_lift
Lift argument to the Riemann surface of the logarithm
- class sympy.functions.elementary.complexes.principal_branch(x, period)[source]¶
Represent a polar number reduced to its principal branch on a quotient of the Riemann surface of the logarithm.
- Parameters:
x : Expr
A polar number.
period : Expr
Positive real number or infinity.
Explanation
This is a function of two arguments. The first argument is a polar number \(z\), and the second one a positive real number or infinity, \(p\). The result is
z mod exp_polar(I*p)
.Examples
>>> from sympy import exp_polar, principal_branch, oo, I, pi >>> from sympy.abc import z >>> principal_branch(z, oo) z >>> principal_branch(exp_polar(2*pi*I)*3, 2*pi) 3*exp_polar(0) >>> principal_branch(exp_polar(2*pi*I)*3*z, 2*pi) 3*principal_branch(z, 2*pi)
See also
sympy.functions.elementary.exponential.exp_polar
polar_lift
Lift argument to the Riemann surface of the logarithm
Trigonometric¶
Trigonometric Functions¶
- class sympy.functions.elementary.trigonometric.sin(arg)[source]¶
The sine function.
Returns the sine of x (measured in radians).
Explanation
This function will evaluate automatically in the case \(x/\pi\) is some rational number [R280]. For example, if \(x\) is a multiple of \(\pi\), \(\pi/2\), \(\pi/3\), \(\pi/4\), and \(\pi/6\).
Examples
>>> from sympy import sin, pi >>> from sympy.abc import x >>> sin(x**2).diff(x) 2*x*cos(x**2) >>> sin(1).diff(x) 0 >>> sin(pi) 0 >>> sin(pi/2) 1 >>> sin(pi/6) 1/2 >>> sin(pi/12) -sqrt(2)/4 + sqrt(6)/4
References
- class sympy.functions.elementary.trigonometric.cos(arg)[source]¶
The cosine function.
Returns the cosine of x (measured in radians).
Explanation
See
sin()
for notes about automatic evaluation.Examples
>>> from sympy import cos, pi >>> from sympy.abc import x >>> cos(x**2).diff(x) -2*x*sin(x**2) >>> cos(1).diff(x) 0 >>> cos(pi) -1 >>> cos(pi/2) 0 >>> cos(2*pi/3) -1/2 >>> cos(pi/12) sqrt(2)/4 + sqrt(6)/4
References
- class sympy.functions.elementary.trigonometric.tan(arg)[source]¶
The tangent function.
Returns the tangent of x (measured in radians).
Explanation
See
sin
for notes about automatic evaluation.Examples
>>> from sympy import tan, pi >>> from sympy.abc import x >>> tan(x**2).diff(x) 2*x*(tan(x**2)**2 + 1) >>> tan(1).diff(x) 0 >>> tan(pi/8).expand() -1 + sqrt(2)
References
- class sympy.functions.elementary.trigonometric.cot(arg)[source]¶
The cotangent function.
Returns the cotangent of x (measured in radians).
Explanation
See
sin
for notes about automatic evaluation.Examples
>>> from sympy import cot, pi >>> from sympy.abc import x >>> cot(x**2).diff(x) 2*x*(-cot(x**2)**2 - 1) >>> cot(1).diff(x) 0 >>> cot(pi/12) sqrt(3) + 2
References
- class sympy.functions.elementary.trigonometric.sec(arg)[source]¶
The secant function.
Returns the secant of x (measured in radians).
Explanation
See
sin
for notes about automatic evaluation.Examples
>>> from sympy import sec >>> from sympy.abc import x >>> sec(x**2).diff(x) 2*x*tan(x**2)*sec(x**2) >>> sec(1).diff(x) 0
References
- class sympy.functions.elementary.trigonometric.csc(arg)[source]¶
The cosecant function.
Returns the cosecant of x (measured in radians).
Explanation
See
sin()
for notes about automatic evaluation.Examples
>>> from sympy import csc >>> from sympy.abc import x >>> csc(x**2).diff(x) -2*x*cot(x**2)*csc(x**2) >>> csc(1).diff(x) 0
References
- class sympy.functions.elementary.trigonometric.sinc(arg)[source]¶
Represents an unnormalized sinc function:
\[\begin{split}\operatorname{sinc}(x) = \begin{cases} \frac{\sin x}{x} & \qquad x \neq 0 \\ 1 & \qquad x = 0 \end{cases}\end{split}\]Examples
>>> from sympy import sinc, oo, jn >>> from sympy.abc import x >>> sinc(x) sinc(x)
Automated Evaluation
>>> sinc(0) 1 >>> sinc(oo) 0
Differentiation
>>> sinc(x).diff() cos(x)/x - sin(x)/x**2
Series Expansion
>>> sinc(x).series() 1 - x**2/6 + x**4/120 + O(x**6)
As zero’th order spherical Bessel Function
>>> sinc(x).rewrite(jn) jn(0, x)
See also
References
Trigonometric Inverses¶
- class sympy.functions.elementary.trigonometric.asin(arg)[source]¶
The inverse sine function.
Returns the arcsine of x in radians.
Explanation
asin(x)
will evaluate automatically in the cases \(x \in \{\infty, -\infty, 0, 1, -1\}\) and for some instances when the result is a rational multiple of \(\pi\) (see theeval
class method).A purely imaginary argument will lead to an asinh expression.
Examples
>>> from sympy import asin, oo >>> asin(1) pi/2 >>> asin(-1) -pi/2 >>> asin(-oo) oo*I >>> asin(oo) -oo*I
References
- class sympy.functions.elementary.trigonometric.acos(arg)[source]¶
The inverse cosine function.
Explanation
Returns the arc cosine of x (measured in radians).
acos(x)
will evaluate automatically in the cases \(x \in \{\infty, -\infty, 0, 1, -1\}\) and for some instances when the result is a rational multiple of \(\pi\) (see the eval class method).acos(zoo)
evaluates tozoo
(see note insympy.functions.elementary.trigonometric.asec
)A purely imaginary argument will be rewritten to asinh.
Examples
>>> from sympy import acos, oo >>> acos(1) 0 >>> acos(0) pi/2 >>> acos(oo) oo*I
References
- class sympy.functions.elementary.trigonometric.atan(arg)[source]¶
The inverse tangent function.
Returns the arc tangent of x (measured in radians).
Explanation
atan(x)
will evaluate automatically in the cases \(x \in \{\infty, -\infty, 0, 1, -1\}\) and for some instances when the result is a rational multiple of \(\pi\) (see the eval class method).Examples
>>> from sympy import atan, oo >>> atan(0) 0 >>> atan(1) pi/4 >>> atan(oo) pi/2
References
- class sympy.functions.elementary.trigonometric.acot(arg)[source]¶
The inverse cotangent function.
Returns the arc cotangent of x (measured in radians).
Explanation
acot(x)
will evaluate automatically in the cases \(x \in \{\infty, -\infty, \tilde{\infty}, 0, 1, -1\}\) and for some instances when the result is a rational multiple of \(\pi\) (see the eval class method).A purely imaginary argument will lead to an
acoth
expression.acot(x)
has a branch cut along \((-i, i)\), hence it is discontinuous at 0. Its range for real \(x\) is \((-\frac{\pi}{2}, \frac{\pi}{2}]\).Examples
>>> from sympy import acot, sqrt >>> acot(0) pi/2 >>> acot(1) pi/4 >>> acot(sqrt(3) - 2) -5*pi/12
References
- class sympy.functions.elementary.trigonometric.asec(arg)[source]¶
The inverse secant function.
Returns the arc secant of x (measured in radians).
Explanation
asec(x)
will evaluate automatically in the cases \(x \in \{\infty, -\infty, 0, 1, -1\}\) and for some instances when the result is a rational multiple of \(\pi\) (see the eval class method).asec(x)
has branch cut in the interval \([-1, 1]\). For complex arguments, it can be defined [R311] as\[\operatorname{sec^{-1}}(z) = -i\frac{\log\left(\sqrt{1 - z^2} + 1\right)}{z}\]At
x = 0
, for positive branch cut, the limit evaluates tozoo
. For negative branch cut, the limit\[\lim_{z \to 0}-i\frac{\log\left(-\sqrt{1 - z^2} + 1\right)}{z}\]simplifies to \(-i\log\left(z/2 + O\left(z^3\right)\right)\) which ultimately evaluates to
zoo
.As
acos(x) = asec(1/x)
, a similar argument can be given foracos(x)
.Examples
>>> from sympy import asec, oo >>> asec(1) 0 >>> asec(-1) pi >>> asec(0) zoo >>> asec(-oo) pi/2
References
- class sympy.functions.elementary.trigonometric.acsc(arg)[source]¶
The inverse cosecant function.
Returns the arc cosecant of x (measured in radians).
Explanation
acsc(x)
will evaluate automatically in the cases \(x \in \{\infty, -\infty, 0, 1, -1\}\)` and for some instances when the result is a rational multiple of \(\pi\) (see theeval
class method).Examples
>>> from sympy import acsc, oo >>> acsc(1) pi/2 >>> acsc(-1) -pi/2 >>> acsc(oo) 0 >>> acsc(-oo) == acsc(oo) True >>> acsc(0) zoo
References
- class sympy.functions.elementary.trigonometric.atan2(y, x)[source]¶
The function
atan2(y, x)
computes \(\operatorname{atan}(y/x)\) taking two arguments \(y\) and \(x\). Signs of both \(y\) and \(x\) are considered to determine the appropriate quadrant of \(\operatorname{atan}(y/x)\). The range is \((-\pi, \pi]\). The complete definition reads as follows:\[\begin{split}\operatorname{atan2}(y, x) = \begin{cases} \arctan\left(\frac y x\right) & \qquad x > 0 \\ \arctan\left(\frac y x\right) + \pi& \qquad y \ge 0, x < 0 \\ \arctan\left(\frac y x\right) - \pi& \qquad y < 0, x < 0 \\ +\frac{\pi}{2} & \qquad y > 0, x = 0 \\ -\frac{\pi}{2} & \qquad y < 0, x = 0 \\ \text{undefined} & \qquad y = 0, x = 0 \end{cases}\end{split}\]Attention: Note the role reversal of both arguments. The \(y\)-coordinate is the first argument and the \(x\)-coordinate the second.
If either \(x\) or \(y\) is complex:
\[\operatorname{atan2}(y, x) = -i\log\left(\frac{x + iy}{\sqrt{x^2 + y^2}}\right)\]Examples
Going counter-clock wise around the origin we find the following angles:
>>> from sympy import atan2 >>> atan2(0, 1) 0 >>> atan2(1, 1) pi/4 >>> atan2(1, 0) pi/2 >>> atan2(1, -1) 3*pi/4 >>> atan2(0, -1) pi >>> atan2(-1, -1) -3*pi/4 >>> atan2(-1, 0) -pi/2 >>> atan2(-1, 1) -pi/4
which are all correct. Compare this to the results of the ordinary \(\operatorname{atan}\) function for the point \((x, y) = (-1, 1)\)
>>> from sympy import atan, S >>> atan(S(1)/-1) -pi/4 >>> atan2(1, -1) 3*pi/4
where only the \(\operatorname{atan2}\) function reurns what we expect. We can differentiate the function with respect to both arguments:
>>> from sympy import diff >>> from sympy.abc import x, y >>> diff(atan2(y, x), x) -y/(x**2 + y**2)
>>> diff(atan2(y, x), y) x/(x**2 + y**2)
We can express the \(\operatorname{atan2}\) function in terms of complex logarithms:
>>> from sympy import log >>> atan2(y, x).rewrite(log) -I*log((x + I*y)/sqrt(x**2 + y**2))
and in terms of \(\operatorname(atan)\):
>>> from sympy import atan >>> atan2(y, x).rewrite(atan) Piecewise((2*atan(y/(x + sqrt(x**2 + y**2))), Ne(y, 0)), (pi, re(x) < 0), (0, Ne(x, 0)), (nan, True))
but note that this form is undefined on the negative real axis.
References
Hyperbolic¶
Hyperbolic Functions¶
- class sympy.functions.elementary.hyperbolic.HyperbolicFunction(*args)[source]¶
Base class for hyperbolic functions.
- class sympy.functions.elementary.hyperbolic.sinh(arg)[source]¶
sinh(x)
is the hyperbolic sine ofx
.The hyperbolic sine function is \(\frac{e^x - e^{-x}}{2}\).
Examples
>>> from sympy import sinh >>> from sympy.abc import x >>> sinh(x) sinh(x)
- class sympy.functions.elementary.hyperbolic.cosh(arg)[source]¶
cosh(x)
is the hyperbolic cosine ofx
.The hyperbolic cosine function is \(\frac{e^x + e^{-x}}{2}\).
Examples
>>> from sympy import cosh >>> from sympy.abc import x >>> cosh(x) cosh(x)
- class sympy.functions.elementary.hyperbolic.tanh(arg)[source]¶
tanh(x)
is the hyperbolic tangent ofx
.The hyperbolic tangent function is \(\frac{\sinh(x)}{\cosh(x)}\).
Examples
>>> from sympy import tanh >>> from sympy.abc import x >>> tanh(x) tanh(x)
- class sympy.functions.elementary.hyperbolic.coth(arg)[source]¶
coth(x)
is the hyperbolic cotangent ofx
.The hyperbolic cotangent function is \(\frac{\cosh(x)}{\sinh(x)}\).
Examples
>>> from sympy import coth >>> from sympy.abc import x >>> coth(x) coth(x)
- class sympy.functions.elementary.hyperbolic.sech(arg)[source]¶
sech(x)
is the hyperbolic secant ofx
.The hyperbolic secant function is \(\frac{2}{e^x + e^{-x}}\)
Examples
>>> from sympy import sech >>> from sympy.abc import x >>> sech(x) sech(x)
Hyperbolic Inverses¶
- class sympy.functions.elementary.hyperbolic.asinh(arg)[source]¶
asinh(x)
is the inverse hyperbolic sine ofx
.The inverse hyperbolic sine function.
Examples
>>> from sympy import asinh >>> from sympy.abc import x >>> asinh(x).diff(x) 1/sqrt(x**2 + 1) >>> asinh(1) log(1 + sqrt(2))
- class sympy.functions.elementary.hyperbolic.acosh(arg)[source]¶
acosh(x)
is the inverse hyperbolic cosine ofx
.The inverse hyperbolic cosine function.
Examples
>>> from sympy import acosh >>> from sympy.abc import x >>> acosh(x).diff(x) 1/(sqrt(x - 1)*sqrt(x + 1)) >>> acosh(1) 0
- class sympy.functions.elementary.hyperbolic.atanh(arg)[source]¶
atanh(x)
is the inverse hyperbolic tangent ofx
.The inverse hyperbolic tangent function.
Examples
>>> from sympy import atanh >>> from sympy.abc import x >>> atanh(x).diff(x) 1/(1 - x**2)
- class sympy.functions.elementary.hyperbolic.acoth(arg)[source]¶
acoth(x)
is the inverse hyperbolic cotangent ofx
.The inverse hyperbolic cotangent function.
Examples
>>> from sympy import acoth >>> from sympy.abc import x >>> acoth(x).diff(x) 1/(1 - x**2)
- class sympy.functions.elementary.hyperbolic.asech(arg)[source]¶
asech(x)
is the inverse hyperbolic secant ofx
.The inverse hyperbolic secant function.
Examples
>>> from sympy import asech, sqrt, S >>> from sympy.abc import x >>> asech(x).diff(x) -1/(x*sqrt(1 - x**2)) >>> asech(1).diff(x) 0 >>> asech(1) 0 >>> asech(S(2)) I*pi/3 >>> asech(-sqrt(2)) 3*I*pi/4 >>> asech((sqrt(6) - sqrt(2))) I*pi/12
References
- class sympy.functions.elementary.hyperbolic.acsch(arg)[source]¶
acsch(x)
is the inverse hyperbolic cosecant ofx
.The inverse hyperbolic cosecant function.
Examples
>>> from sympy import acsch, sqrt, I >>> from sympy.abc import x >>> acsch(x).diff(x) -1/(x**2*sqrt(1 + x**(-2))) >>> acsch(1).diff(x) 0 >>> acsch(1) log(1 + sqrt(2)) >>> acsch(I) -I*pi/2 >>> acsch(-2*I) I*pi/6 >>> acsch(I*(sqrt(6) - sqrt(2))) -5*I*pi/12
See also
References
Integer Functions¶
- class sympy.functions.elementary.integers.ceiling(arg)[source]¶
Ceiling is a univariate function which returns the smallest integer value not less than its argument. This implementation generalizes ceiling to complex numbers by taking the ceiling of the real and imaginary parts separately.
Examples
>>> from sympy import ceiling, E, I, S, Float, Rational >>> ceiling(17) 17 >>> ceiling(Rational(23, 10)) 3 >>> ceiling(2*E) 6 >>> ceiling(-Float(0.567)) 0 >>> ceiling(I/2) I >>> ceiling(S(5)/2 + 5*I/2) 3 + 3*I
References
[R324]“Concrete mathematics” by Graham, pp. 87
- class sympy.functions.elementary.integers.floor(arg)[source]¶
Floor is a univariate function which returns the largest integer value not greater than its argument. This implementation generalizes floor to complex numbers by taking the floor of the real and imaginary parts separately.
Examples
>>> from sympy import floor, E, I, S, Float, Rational >>> floor(17) 17 >>> floor(Rational(23, 10)) 2 >>> floor(2*E) 5 >>> floor(-Float(0.567)) -1 >>> floor(-I/2) -I >>> floor(S(5)/2 + 5*I/2) 2 + 2*I
References
[R326]“Concrete mathematics” by Graham, pp. 87
- class sympy.functions.elementary.integers.RoundFunction(arg)[source]¶
Abstract base class for rounding functions.
- class sympy.functions.elementary.integers.frac(arg)[source]¶
Represents the fractional part of x
For real numbers it is defined [R328] as
\[x - \left\lfloor{x}\right\rfloor\]Examples
>>> from sympy import Symbol, frac, Rational, floor, I >>> frac(Rational(4, 3)) 1/3 >>> frac(-Rational(4, 3)) 2/3
returns zero for integer arguments
>>> n = Symbol('n', integer=True) >>> frac(n) 0
rewrite as floor
>>> x = Symbol('x') >>> frac(x).rewrite(floor) x - floor(x)
for complex arguments
>>> r = Symbol('r', real=True) >>> t = Symbol('t', real=True) >>> frac(t + I*r) I*frac(r) + frac(t)
References
Exponential¶
- class sympy.functions.elementary.exponential.exp(arg)[source]¶
The exponential function, \(e^x\).
- Parameters:
arg : Expr
Examples
>>> from sympy import exp, I, pi >>> from sympy.abc import x >>> exp(x) exp(x) >>> exp(x).diff(x) exp(x) >>> exp(I*pi) -1
See also
- as_real_imag(deep=True, **hints)[source]¶
Returns this function as a 2-tuple representing a complex number.
Examples
>>> from sympy import exp, I >>> from sympy.abc import x >>> exp(x).as_real_imag() (exp(re(x))*cos(im(x)), exp(re(x))*sin(im(x))) >>> exp(1).as_real_imag() (E, 0) >>> exp(I).as_real_imag() (cos(1), sin(1)) >>> exp(1+I).as_real_imag() (E*cos(1), E*sin(1))
- property base¶
Returns the base of the exponential function.
- class sympy.functions.elementary.exponential.LambertW(x, k=None)[source]¶
The Lambert W function \(W(z)\) is defined as the inverse function of \(w \exp(w)\) [R330].
Explanation
In other words, the value of \(W(z)\) is such that \(z = W(z) \exp(W(z))\) for any complex number \(z\). The Lambert W function is a multivalued function with infinitely many branches \(W_k(z)\), indexed by \(k \in \mathbb{Z}\). Each branch gives a different solution \(w\) of the equation \(z = w \exp(w)\).
The Lambert W function has two partially real branches: the principal branch (\(k = 0\)) is real for real \(z > -1/e\), and the \(k = -1\) branch is real for \(-1/e < z < 0\). All branches except \(k = 0\) have a logarithmic singularity at \(z = 0\).
Examples
>>> from sympy import LambertW >>> LambertW(1.2) 0.635564016364870 >>> LambertW(1.2, -1).n() -1.34747534407696 - 4.41624341514535*I >>> LambertW(-1).is_real False
References
- class sympy.functions.elementary.exponential.log(arg, base=None)[source]¶
The natural logarithm function \(\ln(x)\) or \(\log(x)\).
Explanation
Logarithms are taken with the natural base, \(e\). To get a logarithm of a different base
b
, uselog(x, b)
, which is essentially short-hand forlog(x)/log(b)
.log
represents the principal branch of the natural logarithm. As such it has a branch cut along the negative real axis and returns values having a complex argument in \((-\pi, \pi]\).Examples
>>> from sympy import log, sqrt, S, I >>> log(8, 2) 3 >>> log(S(8)/3, 2) -log(3)/log(2) + 3 >>> log(-1 + I*sqrt(3)) log(2) + 2*I*pi/3
See also
- as_real_imag(deep=True, **hints)[source]¶
Returns this function as a complex coordinate.
Examples
>>> from sympy import I, log >>> from sympy.abc import x >>> log(x).as_real_imag() (log(Abs(x)), arg(x)) >>> log(I).as_real_imag() (0, pi/2) >>> log(1 + I).as_real_imag() (log(sqrt(2)), pi/4) >>> log(I*x).as_real_imag() (log(Abs(x)), arg(I*x))
- class sympy.functions.elementary.exponential.exp_polar(*args)[source]¶
Represent a polar number (see g-function Sphinx documentation).
Explanation
exp_polar
represents the function \(Exp: \mathbb{C} \rightarrow \mathcal{S}\), sending the complex number \(z = a + bi\) to the polar number \(r = exp(a), \theta = b\). It is one of the main functions to construct polar numbers.Examples
>>> from sympy import exp_polar, pi, I, exp
The main difference is that polar numbers do not “wrap around” at \(2 \pi\):
>>> exp(2*pi*I) 1 >>> exp_polar(2*pi*I) exp_polar(2*I*pi)
apart from that they behave mostly like classical complex numbers:
>>> exp_polar(2)*exp_polar(3) exp_polar(5)
Piecewise¶
- class sympy.functions.elementary.piecewise.ExprCondPair(expr, cond)[source]¶
Represents an expression, condition pair.
- property cond¶
Returns the condition of this pair.
- property expr¶
Returns the expression of this pair.
- class sympy.functions.elementary.piecewise.Piecewise(*_args)[source]¶
Represents a piecewise function.
Usage:
- Piecewise( (expr,cond), (expr,cond), … )
Each argument is a 2-tuple defining an expression and condition
The conds are evaluated in turn returning the first that is True. If any of the evaluated conds are not explicitly False, e.g.
x < 1
, the function is returned in symbolic form.If the function is evaluated at a place where all conditions are False, nan will be returned.
Pairs where the cond is explicitly False, will be removed and no pair appearing after a True condition will ever be retained. If a single pair with a True condition remains, it will be returned, even when evaluation is False.
Examples
>>> from sympy import Piecewise, log, piecewise_fold >>> from sympy.abc import x, y >>> f = x**2 >>> g = log(x) >>> p = Piecewise((0, x < -1), (f, x <= 1), (g, True)) >>> p.subs(x,1) 1 >>> p.subs(x,5) log(5)
Booleans can contain Piecewise elements:
>>> cond = (x < y).subs(x, Piecewise((2, x < 0), (3, True))); cond Piecewise((2, x < 0), (3, True)) < y
The folded version of this results in a Piecewise whose expressions are Booleans:
>>> folded_cond = piecewise_fold(cond); folded_cond Piecewise((2 < y, x < 0), (3 < y, True))
When a Boolean containing Piecewise (like cond) or a Piecewise with Boolean expressions (like folded_cond) is used as a condition, it is converted to an equivalent
ITE
object:>>> Piecewise((1, folded_cond)) Piecewise((1, ITE(x < 0, y > 2, y > 3)))
When a condition is an
ITE
, it will be converted to a simplified Boolean expression:>>> piecewise_fold(_) Piecewise((1, ((x >= 0) | (y > 2)) & ((y > 3) | (x < 0))))
See also
- _eval_integral(x, _first=True, **kwargs)[source]¶
Return the indefinite integral of the Piecewise such that subsequent substitution of x with a value will give the value of the integral (not including the constant of integration) up to that point. To only integrate the individual parts of Piecewise, use the
piecewise_integrate
method.Examples
>>> from sympy import Piecewise >>> from sympy.abc import x >>> p = Piecewise((0, x < 0), (1, x < 1), (2, True)) >>> p.integrate(x) Piecewise((0, x < 0), (x, x < 1), (2*x - 1, True)) >>> p.piecewise_integrate(x) Piecewise((0, x < 0), (x, x < 1), (2*x, True))
See also
- as_expr_set_pairs(domain=None)[source]¶
Return tuples for each argument of self that give the expression and the interval in which it is valid which is contained within the given domain. If a condition cannot be converted to a set, an error will be raised. The variable of the conditions is assumed to be real; sets of real values are returned.
Examples
>>> from sympy import Piecewise, Interval >>> from sympy.abc import x >>> p = Piecewise( ... (1, x < 2), ... (2,(x > 0) & (x < 4)), ... (3, True)) >>> p.as_expr_set_pairs() [(1, Interval.open(-oo, 2)), (2, Interval.Ropen(2, 4)), (3, Interval(4, oo))] >>> p.as_expr_set_pairs(Interval(0, 3)) [(1, Interval.Ropen(0, 2)), (2, Interval(2, 3))]
- classmethod eval(*_args)[source]¶
Either return a modified version of the args or, if no modifications were made, return None.
Modifications that are made here:
relationals are made canonical
any False conditions are dropped
any repeat of a previous condition is ignored
any args past one with a true condition are dropped
If there are no args left, nan will be returned. If there is a single arg with a True condition, its corresponding expression will be returned.
Examples
>>> from sympy import Piecewise >>> from sympy.abc import x >>> cond = -x < -1 >>> args = [(1, cond), (4, cond), (3, False), (2, True), (5, x < 1)] >>> Piecewise(*args, evaluate=False) Piecewise((1, -x < -1), (4, -x < -1), (2, True)) >>> Piecewise(*args) Piecewise((1, x > 1), (2, True))
- piecewise_integrate(x, **kwargs)[source]¶
Return the Piecewise with each expression being replaced with its antiderivative. To obtain a continuous antiderivative, use the
integrate()
function or method.Examples
>>> from sympy import Piecewise >>> from sympy.abc import x >>> p = Piecewise((0, x < 0), (1, x < 1), (2, True)) >>> p.piecewise_integrate(x) Piecewise((0, x < 0), (x, x < 1), (2*x, True))
Note that this does not give a continuous function, e.g. at x = 1 the 3rd condition applies and the antiderivative there is 2*x so the value of the antiderivative is 2:
>>> anti = _ >>> anti.subs(x, 1) 2
The continuous derivative accounts for the integral up to the point of interest, however:
>>> p.integrate(x) Piecewise((0, x < 0), (x, x < 1), (2*x - 1, True)) >>> _.subs(x, 1) 1
See also
- sympy.functions.elementary.piecewise.piecewise_exclusive(
- expr,
- *,
- skip_nan=False,
- deep=True,
Rewrite
Piecewise
with mutually exclusive conditions.- Parameters:
expr: a SymPy expression.
Any
Piecewise
in the expression will be rewritten.skip_nan: ``bool`` (default ``False``)
If
skip_nan
is set toTrue
then a finalNaN
case will not be included.deep: ``bool`` (default ``True``)
If
deep
isTrue
thenpiecewise_exclusive()
will rewrite anyPiecewise
subexpressions inexpr
rather than just rewritingexpr
itself.- Returns:
An expression equivalent to
expr
but where allPiecewise
havebeen rewritten with mutually exclusive conditions.
Explanation
SymPy represents the conditions of a
Piecewise
in an “if-elif”-fashion, allowing more than one condition to be simultaneously True. The interpretation is that the first condition that is True is the case that holds. While this is a useful representation computationally it is not how a piecewise formula is typically shown in a mathematical text. Thepiecewise_exclusive()
function can be used to rewrite anyPiecewise
with more typical mutually exclusive conditions.Note that further manipulation of the resulting
Piecewise
, e.g. simplifying it, will most likely make it non-exclusive. Hence, this is primarily a function to be used in conjunction with printing the Piecewise or if one would like to reorder the expression-condition pairs.If it is not possible to determine that all possibilities are covered by the different cases of the
Piecewise
then a finalNaN
case will be included explicitly. This can be prevented by passingskip_nan=True
.Examples
>>> from sympy import piecewise_exclusive, Symbol, Piecewise, S >>> x = Symbol('x', real=True) >>> p = Piecewise((0, x < 0), (S.Half, x <= 0), (1, True)) >>> piecewise_exclusive(p) Piecewise((0, x < 0), (1/2, Eq(x, 0)), (1, x > 0)) >>> piecewise_exclusive(Piecewise((2, x > 1))) Piecewise((2, x > 1), (nan, x <= 1)) >>> piecewise_exclusive(Piecewise((2, x > 1)), skip_nan=True) Piecewise((2, x > 1))
See also
- sympy.functions.elementary.piecewise.piecewise_fold(expr, evaluate=True)[source]¶
Takes an expression containing a piecewise function and returns the expression in piecewise form. In addition, any ITE conditions are rewritten in negation normal form and simplified.
The final Piecewise is evaluated (default) but if the raw form is desired, send
evaluate=False
; if trivial evaluation is desired, sendevaluate=None
and duplicate conditions and processing of True and False will be handled.Examples
>>> from sympy import Piecewise, piecewise_fold, S >>> from sympy.abc import x >>> p = Piecewise((x, x < 1), (1, S(1) <= x)) >>> piecewise_fold(x*p) Piecewise((x**2, x < 1), (x, True))
See also
Miscellaneous¶
- class sympy.functions.elementary.miscellaneous.IdentityFunction[source]¶
The identity function
Examples
>>> from sympy import Id, Symbol >>> x = Symbol('x') >>> Id(x) x
- class sympy.functions.elementary.miscellaneous.Min(*args)[source]¶
Return, if possible, the minimum value of the list. It is named
Min
and notmin
to avoid conflicts with the built-in functionmin
.Examples
>>> from sympy import Min, Symbol, oo >>> from sympy.abc import x, y >>> p = Symbol('p', positive=True) >>> n = Symbol('n', negative=True)
>>> Min(x, -2) Min(-2, x) >>> Min(x, -2).subs(x, 3) -2 >>> Min(p, -3) -3 >>> Min(x, y) Min(x, y) >>> Min(n, 8, p, -7, p, oo) Min(-7, n)
See also
Max
find maximum values
- class sympy.functions.elementary.miscellaneous.Max(*args)[source]¶
Return, if possible, the maximum value of the list.
When number of arguments is equal one, then return this argument.
When number of arguments is equal two, then return, if possible, the value from (a, b) that is \(\ge\) the other.
In common case, when the length of list greater than 2, the task is more complicated. Return only the arguments, which are greater than others, if it is possible to determine directional relation.
If is not possible to determine such a relation, return a partially evaluated result.
Assumptions are used to make the decision too.
Also, only comparable arguments are permitted.
It is named
Max
and notmax
to avoid conflicts with the built-in functionmax
.Examples
>>> from sympy import Max, Symbol, oo >>> from sympy.abc import x, y, z >>> p = Symbol('p', positive=True) >>> n = Symbol('n', negative=True)
>>> Max(x, -2) Max(-2, x) >>> Max(x, -2).subs(x, 3) 3 >>> Max(p, -2) p >>> Max(x, y) Max(x, y) >>> Max(x, y) == Max(y, x) True >>> Max(x, Max(y, z)) Max(x, y, z) >>> Max(n, 8, p, 7, -oo) Max(8, p) >>> Max (1, x, oo) oo
Algorithm
The task can be considered as searching of supremums in the directed complete partial orders [R331].
The source values are sequentially allocated by the isolated subsets in which supremums are searched and result as Max arguments.
If the resulted supremum is single, then it is returned.
The isolated subsets are the sets of values which are only the comparable with each other in the current set. E.g. natural numbers are comparable with each other, but not comparable with the \(x\) symbol. Another example: the symbol \(x\) with negative assumption is comparable with a natural number.
Also there are “least” elements, which are comparable with all others, and have a zero property (maximum or minimum for all elements). For example, in case of \(\infty\), the allocation operation is terminated and only this value is returned.
- Assumption:
if \(A > B > C\) then \(A > C\)
if \(A = B\) then \(B\) can be removed
See also
Min
find minimum values
References
- sympy.functions.elementary.miscellaneous.root(arg, n, k=0, evaluate=None)[source]¶
Returns the k-th n-th root of
arg
.- Parameters:
k : int, optional
Should be an integer in \(\{0, 1, ..., n-1\}\). Defaults to the principal root if \(0\).
evaluate : bool, optional
The parameter determines if the expression should be evaluated. If
None
, its value is taken fromglobal_parameters.evaluate
.
Examples
>>> from sympy import root, Rational >>> from sympy.abc import x, n
>>> root(x, 2) sqrt(x)
>>> root(x, 3) x**(1/3)
>>> root(x, n) x**(1/n)
>>> root(x, -Rational(2, 3)) x**(-3/2)
To get the k-th n-th root, specify k:
>>> root(-2, 3, 2) -(-1)**(2/3)*2**(1/3)
To get all n n-th roots you can use the rootof function. The following examples show the roots of unity for n equal 2, 3 and 4:
>>> from sympy import rootof
>>> [rootof(x**2 - 1, i) for i in range(2)] [-1, 1]
>>> [rootof(x**3 - 1,i) for i in range(3)] [1, -1/2 - sqrt(3)*I/2, -1/2 + sqrt(3)*I/2]
>>> [rootof(x**4 - 1,i) for i in range(4)] [-1, 1, -I, I]
SymPy, like other symbolic algebra systems, returns the complex root of negative numbers. This is the principal root and differs from the text-book result that one might be expecting. For example, the cube root of -8 does not come back as -2:
>>> root(-8, 3) 2*(-1)**(1/3)
The real_root function can be used to either make the principal result real (or simply to return the real root directly):
>>> from sympy import real_root >>> real_root(_) -2 >>> real_root(-32, 5) -2
Alternatively, the n//2-th n-th root of a negative number can be computed with root:
>>> root(-32, 5, 5//2) -2
References
- sympy.functions.elementary.miscellaneous.sqrt(arg, evaluate=None)[source]¶
Returns the principal square root.
- Parameters:
evaluate : bool, optional
The parameter determines if the expression should be evaluated. If
None
, its value is taken fromglobal_parameters.evaluate
.
Examples
>>> from sympy import sqrt, Symbol, S >>> x = Symbol('x')
>>> sqrt(x) sqrt(x)
>>> sqrt(x)**2 x
Note that sqrt(x**2) does not simplify to x.
>>> sqrt(x**2) sqrt(x**2)
This is because the two are not equal to each other in general. For example, consider x == -1:
>>> from sympy import Eq >>> Eq(sqrt(x**2), x).subs(x, -1) False
This is because sqrt computes the principal square root, so the square may put the argument in a different branch. This identity does hold if x is positive:
>>> y = Symbol('y', positive=True) >>> sqrt(y**2) y
You can force this simplification by using the powdenest() function with the force option set to True:
>>> from sympy import powdenest >>> sqrt(x**2) sqrt(x**2) >>> powdenest(sqrt(x**2), force=True) x
To get both branches of the square root you can use the rootof function:
>>> from sympy import rootof
>>> [rootof(x**2-3,i) for i in (0,1)] [-sqrt(3), sqrt(3)]
Although
sqrt
is printed, there is nosqrt
function so looking forsqrt
in an expression will fail:>>> from sympy.utilities.misc import func_name >>> func_name(sqrt(x)) 'Pow' >>> sqrt(x).has(sqrt) False
To find
sqrt
look forPow
with an exponent of1/2
:>>> (x + 1/sqrt(x)).find(lambda i: i.is_Pow and abs(i.exp) is S.Half) {1/sqrt(x)}
See also
References
- sympy.functions.elementary.miscellaneous.cbrt(arg, evaluate=None)[source]¶
Returns the principal cube root.
- Parameters:
evaluate : bool, optional
The parameter determines if the expression should be evaluated. If
None
, its value is taken fromglobal_parameters.evaluate
.
Examples
>>> from sympy import cbrt, Symbol >>> x = Symbol('x')
>>> cbrt(x) x**(1/3)
>>> cbrt(x)**3 x
Note that cbrt(x**3) does not simplify to x.
>>> cbrt(x**3) (x**3)**(1/3)
This is because the two are not equal to each other in general. For example, consider \(x == -1\):
>>> from sympy import Eq >>> Eq(cbrt(x**3), x).subs(x, -1) False
This is because cbrt computes the principal cube root, this identity does hold if \(x\) is positive:
>>> y = Symbol('y', positive=True) >>> cbrt(y**3) y
See also
References
- sympy.functions.elementary.miscellaneous.real_root(arg, n=None, evaluate=None)[source]¶
Return the real n’th-root of arg if possible.
- Parameters:
n : int or None, optional
If n is
None
, then all instances of \((-n)^{1/\text{odd}}\) will be changed to \(-n^{1/\text{odd}}\). This will only create a real root of a principal root. The presence of other factors may cause the result to not be real.evaluate : bool, optional
The parameter determines if the expression should be evaluated. If
None
, its value is taken fromglobal_parameters.evaluate
.
Examples
>>> from sympy import root, real_root
>>> real_root(-8, 3) -2 >>> root(-8, 3) 2*(-1)**(1/3) >>> real_root(_) -2
If one creates a non-principal root and applies real_root, the result will not be real (so use with caution):
>>> root(-8, 3, 2) -2*(-1)**(2/3) >>> real_root(_) -2*(-1)**(2/3)