Elementary

This module implements elementary functions such as trigonometric, hyperbolic as well as functions like Abs, Max, sqrt etc.

diofant.functions.elementary.complexes

re

class diofant.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.

Examples

>>> re(2*E)
2*E
>>> re(2*I + 17)
17
>>> re(2*I)
0
>>> re(im(x) + x*I + 2)
2
as_real_imag(deep=True, **hints)[source]

Returns the real number with a zero imaginary part.

im

class diofant.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.

Examples

>>> im(2*E)
0
>>> re(2*I + 17)
17
>>> im(x*I)
re(x)
>>> im(re(x) + y)
im(y)
as_real_imag(deep=True, **hints)[source]

Return the imaginary part with a zero real part.

Examples

>>> im(2 + 3*I).as_real_imag()
(3, 0)

sign

class diofant.functions.elementary.complexes.sign(arg)[source]

Returns the complex sign of an expression.

For nonzero complex number z is an equivalent of z/abs(z). Else returns zero.

Examples

>>> sign(-1)
-1
>>> sign(0)
0
>>> sign(-3*I)
-I
>>> sign(1 + I)
sign(1 + I)
>>> _.evalf()
0.707106781186548 + 0.707106781186548*I

See also

Abs, conjugate

Abs

class diofant.functions.elementary.complexes.Abs(arg)[source]

Return the absolute value of the argument.

This is an extension of the built-in function abs() to accept symbolic values. If you pass a Diofant expression to the built-in abs(), it will pass it automatically to Abs().

Examples

>>> Abs(-1)
1
>>> x = Symbol('x', real=True)
>>> abs(-x)  # The Python built-in
Abs(x)
>>> abs(x**2)
x**2

Note that the Python built-in will return either an Expr or int depending on the argument:

>>> type(abs(-1))
<... 'int'>
>>> type(abs(Integer(-1)))
<class 'diofant.core.numbers.One'>

Abs will always return a diofant object.

fdiff(argindex=1)[source]

Get the first derivative of the argument to Abs().

Examples

>>> abs(-x).fdiff()
sign(x)

adjoint

class diofant.functions.elementary.complexes.adjoint(arg)[source]

Conjugate transpose or Hermite conjugation.

arg

class diofant.functions.elementary.complexes.arg(arg)[source]

Returns the argument (in radians) of a complex number.

For a real number, the argument is always 0.

Examples

>>> arg(2.0)
0
>>> arg(I)
pi/2
>>> arg(sqrt(2) + I*sqrt(2))
pi/4

conjugate

class diofant.functions.elementary.complexes.conjugate(arg)[source]

Returns the complex conjugate 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 + i b\) (where a and b are real numbers) is \(a - i b\)

Examples

>>> conjugate(2)
2
>>> conjugate(I)
-I

References

polar_lift

class diofant.functions.elementary.complexes.polar_lift(arg)[source]

Lift argument to the Riemann surface of the logarithm, using the standard branch.

>>> p = Symbol('p', polar=True)
>>> 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

periodic_argument

class diofant.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(P*I) == 1.

>>> unbranched_argument(exp(5*I*pi))
pi
>>> unbranched_argument(exp_polar(5*I*pi))
5*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

principal_branch

class diofant.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.

This is a function of two arguments. The first argument is a polar number \(z\), and the second one a positive real number of infinity, \(p\). The result is “z mod exp_polar(I*p)”.

>>> 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)

transpose

class diofant.functions.elementary.complexes.transpose(arg)[source]

Linear map transposition.

diofant.functions.elementary.trigonometric

Trigonometric Functions

sin

class diofant.functions.elementary.trigonometric.sin(arg)[source]

The sine function.

Returns the sine of x (measured in radians).

Notes

This function will evaluate automatically in the case x/pi is some rational number. For example, if x is a multiple of pi, pi/2, pi/3, pi/4 and pi/6.

Examples

>>> sin(x**2).diff(x)
2*x*cos(x**2)
>>> sin(pi)
0
>>> sin(pi/2)
1
>>> sin(pi/6)
1/2
>>> sin(pi/12)
-sqrt(2)/4 + sqrt(6)/4

References

cos

class diofant.functions.elementary.trigonometric.cos(arg)[source]

The cosine function.

Returns the cosine of x (measured in radians).

Notes

See sin() for notes about automatic evaluation.

Examples

>>> cos(x**2).diff(x)
-2*x*sin(x**2)
>>> cos(pi)
-1
>>> cos(pi/2)
0
>>> cos(2*pi/3)
-1/2
>>> cos(pi/12)
sqrt(2)/4 + sqrt(6)/4

References

tan

class diofant.functions.elementary.trigonometric.tan(arg)[source]

The tangent function.

Returns the tangent of x (measured in radians).

Notes

See sin() for notes about automatic evaluation.

Examples

>>> tan(x**2).diff(x)
2*x*(tan(x**2)**2 + 1)
>>> tan(pi/8).expand()
-1 + sqrt(2)

References

inverse(argindex=1)[source]

Returns the inverse of this function.

cot

class diofant.functions.elementary.trigonometric.cot(arg)[source]

The cotangent function.

Returns the cotangent of x (measured in radians).

Notes

See sin() for notes about automatic evaluation.

Examples

>>> cot(x**2).diff(x)
2*x*(-cot(x**2)**2 - 1)

References

inverse(argindex=1)[source]

Returns the inverse of this function.

sec

class diofant.functions.elementary.trigonometric.sec(arg)[source]

The secant function.

Returns the secant of x (measured in radians).

Notes

See sin() for notes about automatic evaluation.

Examples

>>> sec(x**2).diff(x)
2*x*tan(x**2)*sec(x**2)

References

csc

class diofant.functions.elementary.trigonometric.csc(arg)[source]

The cosecant function.

Returns the cosecant of x (measured in radians).

Notes

See sin() for notes about automatic evaluation.

Examples

>>> csc(x**2).diff(x)
-2*x*cot(x**2)*csc(x**2)

References

Trigonometric Inverses

asin

class diofant.functions.elementary.trigonometric.asin(arg)[source]

The inverse sine function.

Returns the arcsine of x in radians.

Notes

asin(x) will evaluate automatically in the cases oo, -oo, 0, 1, -1 and for some instances when the result is a rational multiple of pi (see the eval class method).

Examples

>>> asin(1)
pi/2
>>> asin(-1)
-pi/2

References

inverse(argindex=1)[source]

Returns the inverse of this function.

acos

class diofant.functions.elementary.trigonometric.acos(arg)[source]

The inverse cosine function.

Returns the arc cosine of x (measured in radians).

Notes

acos(x) will evaluate automatically in the cases oo, -oo, 0, 1, -1.

acos(zoo) evaluates to zoo (see note in :py:class`diofant.functions.elementary.trigonometric.asec`)

Examples

>>> acos(1)
0
>>> acos(0)
pi/2
>>> acos(oo)
oo*I

References

inverse(argindex=1)[source]

Returns the inverse of this function.

asec

class diofant.functions.elementary.trigonometric.asec(arg)[source]

The inverse secant function.

Returns the arc secant of x (measured in radians).

Notes

asec(x) will evaluate automatically in the cases oo, -oo, 0, 1, -1.

asec(x) has branch cut in the interval [-1, 1]. For complex arguments, it can be defined as

\[sec^{-1}(z) = -i*(log(\sqrt{1 - z^2} + 1) / z)\]

At x = 0, for positive branch cut, the limit evaluates to zoo. For negative branch cut, the limit

\[\lim_{z \to 0}-i*(log(-\sqrt{1 - z^2} + 1) / z)\]

simplifies to \(-i*log(z/2 + O(z^3))\) which ultimately evaluates to zoo.

As asex(x) = asec(1/x), a similar argument can be given for acos(x).

Examples

>>> asec(1)
0
>>> asec(-1)
pi

References

inverse(argindex=1)[source]

Returns the inverse of this function.

atan

class diofant.functions.elementary.trigonometric.atan(arg)[source]

The inverse tangent function.

Returns the arc tangent of x (measured in radians).

Notes

atan(x) will evaluate automatically in the cases oo, -oo, 0, 1, -1.

Examples

>>> atan(0)
0
>>> atan(1)
pi/4
>>> atan(oo)
pi/2

References

inverse(argindex=1)[source]

Returns the inverse of this function.

acot

class diofant.functions.elementary.trigonometric.acot(arg)[source]

The inverse cotangent function.

Returns the arc cotangent of x (measured in radians). This function has a branch cut discontinuity in the complex plane running from \(-i\) to \(i\).

References

inverse(argindex=1)[source]

Returns the inverse of this function.

acsc

class diofant.functions.elementary.trigonometric.acsc(arg)[source]

The inverse cosecant function.

Returns the arc cosecant of x (measured in radians).

Notes

acsc(x) will evaluate automatically in the cases oo, -oo, 0, 1, -1.

Examples

>>> acsc(1)
pi/2
>>> acsc(-1)
-pi/2

References

inverse(argindex=1)[source]

Returns the inverse of this function.

atan2

class diofant.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.

Examples

Going counter-clock wise around the origin we find the following angles:

>>> 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)\)

>>> atan(Integer(1) / -1)
-pi/4
>>> atan2(1, -1)
3*pi/4

where only the \(\operatorname{atan2}\) function returns what we expect. We can differentiate the function with respect to both arguments:

>>> 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:

>>> atan2(y, x).rewrite(log)
-I*log((x + I*y)/sqrt(x**2 + y**2))

and in terms of \(\operatorname(atan)\):

>>> atan2(y, x).rewrite(atan)
2*atan(y/(x + sqrt(x**2 + y**2)))

but note that this form is undefined on the negative real axis.

References

diofant.functions.elementary.hyperbolic

Hyperbolic Functions

HyperbolicFunction

class diofant.functions.elementary.hyperbolic.HyperbolicFunction(*args)[source]

Base class for hyperbolic functions.

sinh

class diofant.functions.elementary.hyperbolic.sinh(arg)[source]

The hyperbolic sine function, \(\frac{e^x - e^{-x}}{2}\).

  • sinh(x) -> Returns the hyperbolic sine of x

as_real_imag(deep=True, **hints)[source]

Returns this function as a complex coordinate.

fdiff(argindex=1)[source]

Returns the first derivative of this function.

inverse(argindex=1)[source]

Returns the inverse of this function.

static taylor_term(n, x, *previous_terms)[source]

Returns the next term in the Taylor series expansion.

cosh

class diofant.functions.elementary.hyperbolic.cosh(arg)[source]

The hyperbolic cosine function, \(\frac{e^x + e^{-x}}{2}\).

  • cosh(x) -> Returns the hyperbolic cosine of x

tanh

class diofant.functions.elementary.hyperbolic.tanh(arg)[source]

The hyperbolic tangent function, \(\frac{\sinh(x)}{\cosh(x)}\).

  • tanh(x) -> Returns the hyperbolic tangent of x

inverse(argindex=1)[source]

Returns the inverse of this function.

coth

class diofant.functions.elementary.hyperbolic.coth(arg)[source]

The hyperbolic cotangent function, \(\frac{\cosh(x)}{\sinh(x)}\).

  • coth(x) -> Returns the hyperbolic cotangent of x

inverse(argindex=1)[source]

Returns the inverse of this function.

sech

class diofant.functions.elementary.hyperbolic.sech(arg)[source]

The hyperbolic secant function, \(\frac{2}{e^x + e^{-x}}\)

  • sech(x) -> Returns the hyperbolic secant of x

csch

class diofant.functions.elementary.hyperbolic.csch(arg)[source]

The hyperbolic cosecant function, \(\frac{2}{e^x - e^{-x}}\)

  • csch(x) -> Returns the hyperbolic cosecant of x

fdiff(argindex=1)[source]

Returns the first derivative of this function.

static taylor_term(n, x, *previous_terms)[source]

Returns the next term in the Taylor series expansion.

Hyperbolic Inverses

asinh

class diofant.functions.elementary.hyperbolic.asinh(arg)[source]

The inverse hyperbolic sine function.

  • asinh(x) -> Returns the inverse hyperbolic sine of x

inverse(argindex=1)[source]

Returns the inverse of this function.

acosh

class diofant.functions.elementary.hyperbolic.acosh(arg)[source]

The inverse hyperbolic cosine function.

  • acosh(x) -> Returns the inverse hyperbolic cosine of x

inverse(argindex=1)[source]

Returns the inverse of this function.

atanh

class diofant.functions.elementary.hyperbolic.atanh(arg)[source]

The inverse hyperbolic tangent function.

  • atanh(x) -> Returns the inverse hyperbolic tangent of x

inverse(argindex=1)[source]

Returns the inverse of this function.

acoth

class diofant.functions.elementary.hyperbolic.acoth(arg)[source]

The inverse hyperbolic cotangent function.

  • acoth(x) -> Returns the inverse hyperbolic cotangent of x

inverse(argindex=1)[source]

Returns the inverse of this function.

diofant.functions.elementary.integers

ceiling

class diofant.functions.elementary.integers.ceiling(arg)[source]

Ceiling is a univariate function which returns the smallest integer value not less than its argument. Ceiling function is generalized in this implementation to complex numbers.

Examples

>>> ceiling(17)
17
>>> ceiling(Rational(23, 10))
3
>>> ceiling(2*E)
6
>>> ceiling(-Float(0.567))
0
>>> ceiling(I/2)
I

References

floor

class diofant.functions.elementary.integers.floor(arg)[source]

Floor is a univariate function which returns the largest integer value not greater than its argument. However this implementation generalizes floor to complex numbers.

Examples

>>> floor(17)
17
>>> floor(Rational(23, 10))
2
>>> floor(2*E)
5
>>> floor(-Float(0.567))
-1
>>> floor(-I/2)
-I

References

RoundFunction

class diofant.functions.elementary.integers.RoundFunction(arg)[source]

The base class for rounding functions.

diofant.functions.elementary.exponential

exp

diofant.functions.elementary.exponential.exp(arg, **kwargs)[source]

The exponential function, \(e^x\).

exp_polar

class diofant.functions.elementary.exponential.exp_polar(*args)[source]

Represent a ‘polar number’ (see g-function Sphinx documentation).

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.

The main difference is that polar numbers don’t “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)
property exp

Returns the exponent of the function.

LambertW

class diofant.functions.elementary.exponential.LambertW(x, k=None)[source]

The Lambert W function \(W(z)\) is defined as the inverse function of \(w \exp(w)\).

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

>>> LambertW(1.2)
0.635564016364870
>>> LambertW(1.2, -1).evalf()
-1.34747534407696 - 4.41624341514535*I
>>> LambertW(-1).is_real
False

References

fdiff(argindex=1)[source]

Return the first derivative of this function.

log

class diofant.functions.elementary.exponential.log(arg, base=None)[source]

The natural logarithm function \(\ln(x)\) or \(\log(x)\). Logarithms are taken with the natural base, \(e\). To get a logarithm of a different base b, use log(x, b), which is essentially short-hand for log(x)/log(b).

as_real_imag(deep=True, **hints)[source]

Returns this function as a complex coordinate.

Examples

>>> 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))
fdiff(argindex=1)[source]

Returns the first derivative of the function.

inverse(argindex=1)[source]

Returns \(e^x\), the inverse function of \(\log(x)\).

diofant.functions.elementary.piecewise

ExprCondPair

class diofant.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.

Piecewise

class diofant.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 determined explicitly False, e.g. x < 1, the function is returned in symbolic form.

  • Pairs where the cond is explicitly False, will be removed.

  • The last condition is explicitely True. It’s value is a default value for the function (nan if not specified othewise).

Examples

>>> 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)
doit(**hints)[source]

Evaluate this piecewise function.

diofant.functions.elementary.piecewise.piecewise_fold(expr)[source]

Takes an expression containing a piecewise function and returns the expression in piecewise form.

Examples

>>> p = Piecewise((x, x < 1), (1, True))
>>> piecewise_fold(x*p)
Piecewise((x**2, x < 1), (x, true))

diofant.functions.elementary.miscellaneous

IdentityFunction

class diofant.functions.elementary.miscellaneous.IdentityFunction(*args, **kwargs)[source]

The identity function

Examples

>>> Id(x)
x

Min

class diofant.functions.elementary.miscellaneous.Min(*args)[source]

Return, if possible, the minimum value of the list.

It is named Min and not min to avoid conflicts with the built-in function min.

Examples

>>> 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)

Max

class diofant.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 >= 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 not max to avoid conflicts with the built-in function max.

Examples

>>> 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

Notes

The task can be considered as searching of supremums in the directed complete partial orders.

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). E.g. \(oo\). In case of it 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

References

root

diofant.functions.elementary.miscellaneous.root(arg, n, k=0, **kwargs)[source]

Returns the k-th n-th root of arg, defaulting to the principle root.

Examples

>>> root(x, 2)
sqrt(x)
>>> root(x, 3)
root(x, 3)
>>> root(x, n)
x**(1/n)
>>> root(x, -Rational(2, 3))
1/sqrt(x)**3

To get the k-th n-th root, specify k:

>>> root(-2, 3, 2)
-root(-1, 3)**2*root(2, 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:

>>> [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]

Diofant, 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*root(-1, 3)

The real_root function can be used to either make the principle result real (or simply to return the real root directly):

>>> 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

real_root

diofant.functions.elementary.miscellaneous.real_root(arg, n=None)[source]

Return the real nth-root of arg if possible. If n is omitted then all instances of (-n)**(1/odd) will be changed to -n**(1/odd); this will only create a real root of a principle root – the presence of other factors may cause the result to not be real.

Examples

>>> real_root(-8, 3)
-2
>>> root(-8, 3)
2*root(-1, 3)
>>> real_root(_)
-2

If one creates a non-principle root and applies real_root, the result will not be real (so use with caution):

>>> root(-8, 3, 2)
-2*root(-1, 3)**2
>>> real_root(_)
-2*root(-1, 3)**2

sqrt

diofant.functions.elementary.miscellaneous.sqrt(arg, **kwargs)[source]

The square root function

sqrt(x) -> Returns the principal square root of x.

Examples

>>> 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:

>>> 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:

>>> 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:

>>> [RootOf(x**2 - 3, i) for i in (0, 1)]
[-sqrt(3), sqrt(3)]

References