初级#
这个模块实现一些基本函数,比如三角函数、双曲函数和sqrt函数,以及像 Abs
, Max
, Min
等。
Complex Functions#
- class sympy.functions.elementary.complexes.re(arg)[源代码]#
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.- 参数:
arg :表达式
真实的或复杂的表达。
- 返回:
expr :表达式
表达的真实部分。
实例
>>> 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
参见
- class sympy.functions.elementary.complexes.im(arg)[源代码]#
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.- 参数:
arg :表达式
真实的或复杂的表达。
- 返回:
expr :表达式
表达的想象部分。
实例
>>> 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
参见
- class sympy.functions.elementary.complexes.sign(arg)[源代码]#
返回表达式的复数符号:
- 参数:
arg :表达式
真实的或想象的表达。
- 返回:
expr :表达式
复杂的表情符号。
解释
如果表达式为真,则符号为:
\(1\) if expression is positive
\(0\) if expression is equal to zero
\(-1\) if expression is negative
如果表达式是虚构的,则符号为:
\(I\) if im(expression) is positive
\(-I\) if im(expression) is negative
否则将返回未赋值的表达式。当评估时,结果(通常)将是
cos(arg(expr)) + I*sin(arg(expr))
.实例
>>> 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)[源代码]#
返回参数的绝对值。
- 参数:
arg :表达式
真实的或复杂的表达。
- 返回:
expr :表达式
返回的绝对值可以是表达式或整数,具体取决于输入参数。
解释
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()
.实例
>>> 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
请注意,Python内置函数将根据参数返回Expr或int::
>>> 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)[源代码]#
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.- 参数:
arg :表达式
真实的或复杂的表达。
- 返回:
价值 :表达式
返回以弧度度量的参数的反正切值。
实例
>>> 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)[源代码]#
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\)
- 参数:
arg :表达式
真实的或复杂的表达。
- 返回:
arg :表达式
作为实数、虚数或混合表达式的arg的复共轭。
实例
>>> from sympy import conjugate, I >>> conjugate(2) 2 >>> conjugate(I) -I >>> conjugate(3 + 2*I) 3 - 2*I >>> conjugate(5 - I) 5 + I
工具书类
- class sympy.functions.elementary.complexes.polar_lift(arg)[源代码]#
将参数提升到对数的黎曼曲面,使用标准分支。
- 参数:
arg :表达式
真实的或复杂的表达。
实例
>>> 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)[源代码]#
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\).
- 参数:
ar :表达式
极数。
时期 :表达式
期间\(P\)。
实例
>>> 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
- class sympy.functions.elementary.complexes.principal_branch(x, period)[源代码]#
在对数的黎曼曲面的商上表示一个极数降为其主支的极数。
- 参数:
x :表达式
极数。
时期 :表达式
正实数或无穷大。
解释
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)
.实例
>>> 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)
Trigonometric#
三角函数#
- class sympy.functions.elementary.trigonometric.sin(arg)[源代码]#
正弦函数。
返回x的正弦值(以弧度度量)。
解释
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\).
实例
>>> 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
工具书类
- class sympy.functions.elementary.trigonometric.cos(arg)[源代码]#
余弦函数。
返回x的余弦(以弧度度量)。
解释
见
sin()
有关自动评估的说明。实例
>>> 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
工具书类
- class sympy.functions.elementary.trigonometric.tan(arg)[源代码]#
切线函数。
返回x的正切值(以弧度度量)。
解释
See
sin
for notes about automatic evaluation.实例
>>> 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)
工具书类
- class sympy.functions.elementary.trigonometric.cot(arg)[源代码]#
余切函数。
返回x的余切(以弧度度量)。
解释
See
sin
for notes about automatic evaluation.实例
>>> 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
工具书类
- class sympy.functions.elementary.trigonometric.sec(arg)[源代码]#
割线函数。
返回x的正割值(以弧度度量)。
解释
See
sin
for notes about automatic evaluation.实例
>>> 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
工具书类
- class sympy.functions.elementary.trigonometric.csc(arg)[源代码]#
余割函数。
返回x的余割(以弧度度量)。
解释
见
sin()
有关自动评估的说明。实例
>>> 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
工具书类
- class sympy.functions.elementary.trigonometric.sinc(arg)[源代码]#
表示未规范化的sinc函数:
\[\运算符名称{sinc}(x)=\]实例
>>> from sympy import sinc, oo, jn >>> from sympy.abc import x >>> sinc(x) sinc(x)
自动评估
>>> sinc(0) 1 >>> sinc(oo) 0
区别
>>> sinc(x).diff() cos(x)/x - sin(x)/x**2
级数展开
>>> sinc(x).series() 1 - x**2/6 + x**4/120 + O(x**6)
作为零阶球面贝塞尔函数
>>> sinc(x).rewrite(jn) jn(0, x)
参见
工具书类
三角逆#
- class sympy.functions.elementary.trigonometric.asin(arg)[源代码]#
反正弦函数。
返回x的反正弦(以弧度为单位)。
解释
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).一个纯粹虚构的论点将导致一个asinh表达式。
实例
>>> from sympy import asin, oo >>> asin(1) pi/2 >>> asin(-1) -pi/2 >>> asin(-oo) oo*I >>> asin(oo) -oo*I
工具书类
- class sympy.functions.elementary.trigonometric.acos(arg)[源代码]#
反余弦函数。
解释
返回x的反余弦(以弧度度量)。
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.
实例
>>> from sympy import acos, oo >>> acos(1) 0 >>> acos(0) pi/2 >>> acos(oo) oo*I
工具书类
- class sympy.functions.elementary.trigonometric.atan(arg)[源代码]#
反切函数。
返回x的反正切值(以弧度度量)。
解释
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).实例
>>> from sympy import atan, oo >>> atan(0) 0 >>> atan(1) pi/4 >>> atan(oo) pi/2
工具书类
- class sympy.functions.elementary.trigonometric.acot(arg)[源代码]#
逆余切函数。
返回x的弧余切(以弧度度量)。
解释
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).一个纯粹虚构的论点会导致
acoth
表达式。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}]\).实例
>>> from sympy import acot, sqrt >>> acot(0) pi/2 >>> acot(1) pi/4 >>> acot(sqrt(3) - 2) -5*pi/12
工具书类
- class sympy.functions.elementary.trigonometric.asec(arg)[源代码]#
反割线函数。
返回x的弧正割(以弧度度量)。
解释
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\[\运算符名称{sec^{-1}}(z)=-i\frac{\log\left(\sqrt{1-z^2}+1\右)}{z}\]AT
x = 0
,对于正分支切割,限制计算为zoo
. 对于负分支切割,极限\[\lim{z\到0}-i\frac{\log\left(-\sqrt{1-z^2}+1\右)}{z}\]简化为 \(-i\log\left(z/2 + O\left(z^3\right)\right)\) 最终评估为
zoo
.As
acos(x) = asec(1/x)
, a similar argument can be given foracos(x)
.实例
>>> from sympy import asec, oo >>> asec(1) 0 >>> asec(-1) pi >>> asec(0) zoo >>> asec(-oo) pi/2
工具书类
- class sympy.functions.elementary.trigonometric.acsc(arg)[源代码]#
逆余割函数。
返回x的弧余割(以弧度度量)。
解释
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).实例
>>> from sympy import acsc, oo >>> acsc(1) pi/2 >>> acsc(-1) -pi/2 >>> acsc(oo) 0 >>> acsc(-oo) == acsc(oo) True >>> acsc(0) zoo
工具书类
- class sympy.functions.elementary.trigonometric.atan2(y, x)[源代码]#
函数
atan2(y, x)
计算 \(\operatorname{{atan}}(y/x)\) 接受两个论点 \(y\) 和 \(x\) . 两者的迹象 \(y\) 和 \(x\) 考虑确定适当的象限 \(\operatorname{{atan}}(y/x)\) . 范围是 \((-\pi, \pi]\) . 完整定义如下:\[\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}\]注意:注意两个论点的角色颠倒。这个 \(y\) -坐标是第一个参数 \(x\) -协调第二个。
如果任一 \(x\) 或 \(y\) 很复杂:
\[\operatorname{atan2}(y, x) = -i\log\left(\frac{x + iy}{\sqrt{x^2 + y^2}}\right)\]实例
沿着原点逆时针方向,我们发现以下角度:
>>> 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
这些都是正确的。将此结果与普通 \(\operatorname{{atan}}\) 点的函数 \((x, y) = (-1, 1)\)
>>> from sympy import atan, S >>> atan(S(1)/-1) -pi/4 >>> atan2(1, -1) 3*pi/4
只有 \(\operatorname{{atan2}}\) 函数符合我们的期望。我们可以根据两个参数区分函数:
>>> 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)
我们可以表达 \(\operatorname{{atan2}}\) 复对数函数:
>>> from sympy import log >>> atan2(y, x).rewrite(log) -I*log((x + I*y)/sqrt(x**2 + y**2))
就 \(\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))
但是请注意,这个形式在负实轴上是未定义的。
工具书类
Hyperbolic#
双曲函数#
- class sympy.functions.elementary.hyperbolic.sinh(arg)[源代码]#
sinh(x)
is the hyperbolic sine ofx
.双曲正弦函数是\(\frac{e^x-e^{-x}}{2}\)。
实例
>>> from sympy import sinh >>> from sympy.abc import x >>> sinh(x) sinh(x)
- class sympy.functions.elementary.hyperbolic.cosh(arg)[源代码]#
cosh(x)
is the hyperbolic cosine ofx
.双曲余弦函数是\(\frac{e^x+e^{-x}}{2}\)。
实例
>>> from sympy import cosh >>> from sympy.abc import x >>> cosh(x) cosh(x)
- class sympy.functions.elementary.hyperbolic.tanh(arg)[源代码]#
tanh(x)
is the hyperbolic tangent ofx
.双曲正切函数是\(\frac{\sinh(x)}{\cosh(x)}\)。
实例
>>> from sympy import tanh >>> from sympy.abc import x >>> tanh(x) tanh(x)
- class sympy.functions.elementary.hyperbolic.coth(arg)[源代码]#
coth(x)
is the hyperbolic cotangent ofx
.双曲余切函数是\(\frac{\cosh(x)}{\sinh(x)}\)。
实例
>>> from sympy import coth >>> from sympy.abc import x >>> coth(x) coth(x)
- class sympy.functions.elementary.hyperbolic.sech(arg)[源代码]#
sech(x)
is the hyperbolic secant ofx
.双曲正割函数是\(\frac{2}{e^x+e^{-x}}\)
实例
>>> from sympy import sech >>> from sympy.abc import x >>> sech(x) sech(x)
双曲逆#
- class sympy.functions.elementary.hyperbolic.asinh(arg)[源代码]#
asinh(x)
is the inverse hyperbolic sine ofx
.反双曲正弦函数。
实例
>>> 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)[源代码]#
acosh(x)
is the inverse hyperbolic cosine ofx
.反双曲余弦函数。
实例
>>> 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)[源代码]#
atanh(x)
is the inverse hyperbolic tangent ofx
.反双曲正切函数。
实例
>>> from sympy import atanh >>> from sympy.abc import x >>> atanh(x).diff(x) 1/(1 - x**2)
- class sympy.functions.elementary.hyperbolic.acoth(arg)[源代码]#
acoth(x)
is the inverse hyperbolic cotangent ofx
.反双曲余切函数。
实例
>>> from sympy import acoth >>> from sympy.abc import x >>> acoth(x).diff(x) 1/(1 - x**2)
- class sympy.functions.elementary.hyperbolic.asech(arg)[源代码]#
asech(x)
is the inverse hyperbolic secant ofx
.反双曲正割函数。
实例
>>> 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
工具书类
- class sympy.functions.elementary.hyperbolic.acsch(arg)[源代码]#
acsch(x)
is the inverse hyperbolic cosecant ofx
.反双曲余割函数。
实例
>>> 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
参见
工具书类
Integer Functions#
- class sympy.functions.elementary.integers.ceiling(arg)[源代码]#
Ceiling是一个单变量函数,它返回的最小整数值不小于其参数。这个实现通过分别取实部和虚部的上限将上限推广到复数。
实例
>>> 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
工具书类
[R324]《具体数学》,格雷厄姆著,第87页
- class sympy.functions.elementary.integers.floor(arg)[源代码]#
Floor是一个单变量函数,它返回不大于其参数的最大整数值。这个实现通过分别取实数和虚部的底,将底数推广到复数。
实例
>>> 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
工具书类
[R326]《具体数学》,格雷厄姆著,第87页
- class sympy.functions.elementary.integers.RoundFunction(arg)[源代码]#
Abstract base class for rounding functions.
- class sympy.functions.elementary.integers.frac(arg)[源代码]#
表示x的小数部分
For real numbers it is defined [R328] as
\[x-\left\lfloor{x}\right\r层\]实例
>>> from sympy import Symbol, frac, Rational, floor, I >>> frac(Rational(4, 3)) 1/3 >>> frac(-Rational(4, 3)) 2/3
对于整型参数返回零
>>> n = Symbol('n', integer=True) >>> frac(n) 0
重写为楼层
>>> x = Symbol('x') >>> frac(x).rewrite(floor) x - floor(x)
对于复杂的参数
>>> r = Symbol('r', real=True) >>> t = Symbol('t', real=True) >>> frac(t + I*r) I*frac(r) + frac(t)
工具书类
Exponential#
- class sympy.functions.elementary.exponential.exp(arg)[源代码]#
指数函数, \(e^x\) .
- 参数:
arg :表达式
实例
>>> 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
参见
- as_real_imag(deep=True, **hints)[源代码]#
Returns this function as a 2-tuple representing a complex number.
实例
>>> 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)[源代码]#
The Lambert W function \(W(z)\) is defined as the inverse function of \(w \exp(w)\) [R330].
解释
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\).
实例
>>> from sympy import LambertW >>> LambertW(1.2) 0.635564016364870 >>> LambertW(1.2, -1).n() -1.34747534407696 - 4.41624341514535*I >>> LambertW(-1).is_real False
工具书类
- class sympy.functions.elementary.exponential.log(arg, base=None)[源代码]#
自然对数函数 \(\ln(x)\) 或 \(\log(x)\) .
解释
对数取自然基, \(e\) . 得到不同基的对数
b
使用log(x, b)
,基本上是log(x)/log(b)
.log
表示自然对数的主分支。因此,它有一个沿着负实数轴的分支切割,并返回在中具有复杂参数的值 \((-\pi, \pi]\) .实例
>>> 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
参见
- as_real_imag(deep=True, **hints)[源代码]#
Returns this function as a complex coordinate.
实例
>>> 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)[源代码]#
Represent a polar number (see g-function Sphinx documentation).
解释
exp_polar
表示函数 \(Exp: \mathbb{{C}} \rightarrow \mathcal{{S}}\) ,发送复数 \(z = a + bi\) 对极数 \(r = exp(a), \theta = b\) . 它是构造极数的主要函数之一。实例
>>> 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)
除此之外,它们的行为大多类似于经典复数:
>>> exp_polar(2)*exp_polar(3) exp_polar(5)
分段#
- class sympy.functions.elementary.piecewise.ExprCondPair(expr, cond)[源代码]#
表示表达式、条件对。
- property cond#
Returns the condition of this pair.
- property expr#
Returns the expression of this pair.
- class sympy.functions.elementary.piecewise.Piecewise(*_args)[源代码]#
表示分段函数。
用途:
- 分段((expr,cond),(expr,cond),…)
每个参数都是一个定义表达式和条件的2元组
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.如果函数在所有条件都为False的地方求值,则返回nan。
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.
实例
>>> 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)
布尔值可以包含分段元素:
>>> cond = (x < y).subs(x, Piecewise((2, x < 0), (3, True))); cond Piecewise((2, x < 0), (3, True)) < y
这种折叠式的结果是分段的,其表达式是布尔型:
>>> 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))))
- _eval_integral(x, _first=True, **kwargs)[源代码]#
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.实例
>>> 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))
- as_expr_set_pairs(domain=None)[源代码]#
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.
实例
>>> 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)[源代码]#
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.
实例
>>> 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)[源代码]#
Return the Piecewise with each expression being replaced with its antiderivative. To obtain a continuous antiderivative, use the
integrate()
function or method.实例
>>> 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
- sympy.functions.elementary.piecewise.piecewise_exclusive(expr, *, skip_nan=False, deep=True)[源代码]#
Rewrite
Piecewise
with mutually exclusive conditions.- 参数:
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.- 返回:
An expression equivalent to
expr
but where allPiecewise
havebeen rewritten with mutually exclusive conditions.
解释
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
.实例
>>> 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))
- sympy.functions.elementary.piecewise.piecewise_fold(expr, evaluate=True)[源代码]#
接受包含分段函数的表达式,并以分段形式返回表达式。此外,任何ITE条件都被改写成否定范式并简化。
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.实例
>>> 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))
其他#
- class sympy.functions.elementary.miscellaneous.IdentityFunction[源代码]#
恒等函数
实例
>>> from sympy import Id, Symbol >>> x = Symbol('x') >>> Id(x) x
- class sympy.functions.elementary.miscellaneous.Min(*args)[源代码]#
如果可能,返回列表的最小值。它被命名为
Min
而不是min
避免与内置函数冲突min
.实例
>>> 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)
参见
Max
查找最大值
- class sympy.functions.elementary.miscellaneous.Max(*args)[源代码]#
如果可能,返回列表的最大值。
当参数数等于1时,则返回此参数。
When number of arguments is equal two, then return, if possible, the value from (a, b) that is \(\ge\) the other.
一般情况下,当列表长度大于2时,任务就比较复杂。如果可以确定方向关系,则只返回比其他参数大的参数。
如果无法确定这种关系,则返回部分计算结果。
假设也被用来做决定。
此外,只允许使用可比较的参数。
它被命名为
Max
而不是max
避免与内置函数冲突max
.实例
>>> 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
算法
The task can be considered as searching of supremums in the directed complete partial orders [R331].
源值由孤立的子集按顺序分配,在这些子集中搜索supremus并作为Max参数。
如果得到的上确界是单的,则返回它。
孤立的子集是在当前集合中只能相互比较的一组值。E、 自然数可以相互比较,但不能与 \(x\) 符号。另一个例子:符号 \(x\) 负假设与自然数相当。
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.
- 假设:
if \(A > B > C\) then \(A > C\)
if \(A = B\) then \(B\) can be removed
参见
Min
查找最小值
工具书类
- sympy.functions.elementary.miscellaneous.root(arg, n, k=0, evaluate=None)[源代码]#
返回 k -第 n -根
arg
.- 参数:
k :int,可选
应为\(\{0,1,…,n-1\}\)中的整数。如果\(0\),则默认为主体根。
评价 :bool,可选
参数确定是否应计算表达式。如果
None
,其值取自global_parameters.evaluate
.
实例
>>> 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)
要获得第k个n个根,请指定k:
>>> root(-2, 3, 2) -(-1)**(2/3)*2**(1/3)
要得到所有n个根,可以使用rootof函数。以下示例显示了n等于2、3和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返回负数的复数根。这是主根,它与人们可能期望的教科书结果不同。例如,-8的立方根不返回为-2:
>>> root(-8, 3) 2*(-1)**(1/3)
real_root函数可用于使主要结果为实数(或直接返回实数根):
>>> from sympy import real_root >>> real_root(_) -2 >>> real_root(-32, 5) -2
或者,负数的n//2第n个根可以用根计算:
>>> root(-32, 5, 5//2) -2
工具书类
- sympy.functions.elementary.miscellaneous.sqrt(arg, evaluate=None)[源代码]#
返回主平方根。
- 参数:
评价 :bool,可选
参数确定是否应计算表达式。如果
None
,其值取自global_parameters.evaluate
.
实例
>>> from sympy import sqrt, Symbol, S >>> x = Symbol('x')
>>> sqrt(x) sqrt(x)
>>> sqrt(x)**2 x
注意,sqrt(x**2)并没有简化为x。
>>> sqrt(x**2) sqrt(x**2)
这是因为两者在总体上是不平等的。例如,考虑x==-1:
>>> from sympy import Eq >>> Eq(sqrt(x**2), x).subs(x, -1) False
这是因为sqrt计算主平方根,所以平方可以将参数放在不同的分支中。如果x为正,则此恒等式成立:
>>> y = Symbol('y', positive=True) >>> sqrt(y**2) y
您可以通过使用powdenest()函数将force选项设置为True来强制简化:
>>> from sympy import powdenest >>> sqrt(x**2) sqrt(x**2) >>> powdenest(sqrt(x**2), force=True) x
要获得平方根的两个分支,可以使用rootof函数:
>>> from sympy import rootof
>>> [rootof(x**2-3,i) for i in (0,1)] [-sqrt(3), sqrt(3)]
虽然
sqrt
没有,没有sqrt
如此寻找的功能sqrt
在表达式中将失败:>>> from sympy.utilities.misc import func_name >>> func_name(sqrt(x)) 'Pow' >>> sqrt(x).has(sqrt) False
寻找
sqrt
寻找Pow
指数为1/2
:>>> (x + 1/sqrt(x)).find(lambda i: i.is_Pow and abs(i.exp) is S.Half) {1/sqrt(x)}
工具书类
- sympy.functions.elementary.miscellaneous.cbrt(arg, evaluate=None)[源代码]#
返回主立方根。
- 参数:
评价 :bool,可选
参数确定是否应计算表达式。如果
None
,其值取自global_parameters.evaluate
.
实例
>>> from sympy import cbrt, Symbol >>> x = Symbol('x')
>>> cbrt(x) x**(1/3)
>>> cbrt(x)**3 x
注意,cbrt(x**3)并没有简化为x。
>>> cbrt(x**3) (x**3)**(1/3)
这是因为两者在总体上是不平等的。例如,考虑 \(x == -1\) :
>>> from sympy import Eq >>> Eq(cbrt(x**3), x).subs(x, -1) False
这是因为cbrt计算主立方根,如果 \(x\) 是积极的:
>>> y = Symbol('y', positive=True) >>> cbrt(y**3) y
工具书类
- sympy.functions.elementary.miscellaneous.real_root(arg, n=None, evaluate=None)[源代码]#
归还真实的 n 'th根 arg 如果可能的话。
- 参数:
n :int或None,可选
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.评价 :bool,可选
参数确定是否应计算表达式。如果
None
,其值取自global_parameters.evaluate
.
实例
>>> from sympy import root, real_root
>>> real_root(-8, 3) -2 >>> root(-8, 3) 2*(-1)**(1/3) >>> real_root(_) -2
如果创建一个非主体根并应用realu根,则结果将不是real(因此请谨慎使用):
>>> root(-8, 3, 2) -2*(-1)**(2/3) >>> real_root(_) -2*(-1)**(2/3)