Top Fields Constructors Methods
org.python.modules

public Class cmath

extends Object
Class Inheritance
Imports
org.python.core.Py, .PyComplex, .PyException, .PyFloat, .PyInstance, .PyObject, .PyTuple

Field Summary

Modifier and TypeField and Description
private static final double
ATLEAST_27LN2

For x larger than this, e-x is negligible compared with ex, or equivalently 1 is negligible compared with e2x, in IEEE-754 floating point.

public static final PyFloat
e

private static final double
private static final double
LOG10E

log10e (Ref: Abramowitz & Stegun [1972], p3).

private static final double
NEARLY_LN_DBL_MAX

ln(Double#MAX_VALUE) or a little less

public static final PyFloat
private static final double
ROOT_HALF

2-½ (Ref: Abramowitz & Stegun [1972], p2).

Constructor Summary

AccessConstructor and Description
public

Method Summary

Modifier and TypeMethod and Description
private static PyComplex

Returns:

cos-1w
_acos
(PyComplex w)

Helper to compute cos-1w.

private static PyComplex

Returns:

cosh-1w
_acosh
(PyComplex w)

Helper to compute z = cosh-1w.

public static PyComplex

Returns:

cos-1w
acos
(PyObject w)

Return the arc cosine of w.

public static PyComplex

Returns:

cosh-1w
acosh
(PyObject w)

Return the hyperbolic arc cosine of w.

public static PyComplex

Returns:

sin-1w
asin
(PyObject w)

Return the arc sine of w.

public static PyComplex

Returns:

sinh-1w
asinh
(PyObject w)

Return the hyperbolic arc sine of w.

private static PyComplex

Returns:

sinh-1w or sin-1w
asinOrAsinh
(PyComplex w, boolean
true to compute sinh-1w, false to compute sin-1w.
h
)

Helper to compute either sin-1w or sinh-1w.

public static PyComplex

Returns:

tan-1w
atan
(PyObject w)

Return the arc tangent of w.

public static PyComplex

Returns:

tanh-1w
atanh
(PyObject w)

Return the hyperbolic arc tangent of w.

private static PyComplex

Returns:

tanh-1w or tan-1w
atanOrAtanh
(PyComplex w, boolean
true to compute tanh-1w, false to compute tan-1w.
h
)

Helper to compute either tan-1w or tanh-1w.

private static PyComplex
public static PyComplex

Returns:

cos z
cos
(PyObject z)

Return the cosine of z.

public static PyComplex

Returns:

cosh z
cosh
(PyObject z)

Return the hyperbolic cosine of z.

private static PyComplex

Returns:

cos z or cosh z
cosOrCosh
(PyComplex z, boolean
true for cosh, false for cos.
h
)

Helper to compute either cos z or cosh z.

private static PyComplex

Returns:

result if arg was NaN or result was not NaN
exceptNaN
(PyComplex
to return (if we return)
result
,
PyComplex
to include in check
arg
)

Turn a NaN result into a thrown ValueError, a math domain error, if the original argument was not itself NaN.

private static PyComplex
exceptNaN(PyComplex result, double a, double b)

Raise ValueError if result is a NaN, but neither a nor b is NaN.

public static PyComplex

Returns:

ez
exp
(PyObject z)

Return the exponential value ez.

public static boolean

Returns:

true if in.real or in.imag is positive or negative infinity
isinf
(PyObject in)

public static boolean

Returns:

true if in.real or in.imag is nan.
isnan
(PyObject in)

public static PyComplex

Returns:

ln w
log
(PyObject w)

Returns the natural logarithm of w.

public static PyComplex

Returns:

logbw
log
(PyObject w, PyObject b)

Returns the logarithm of w to the given base.

public static PyComplex

Returns:

log10w
log10
(PyObject w)

Returns the common logarithm of w (base 10 logarithm).

private static double

Returns:

½ln(u2+v2)
logHypot
(double u, double v)

Helper function for the log of a complex number, dealing with the log magnitude, and without intermediate overflow or underflow.

public static double
public static PyTuple
public static PyComplex

Returns:

reiφ
rect
(double
radius
r
,
double
angle
phi
)

Return the complex number x with polar coordinates r and phi.

public static PyComplex

Returns:

sin z
sin
(PyObject z)

Return the sine of z.

public static PyComplex

Returns:

sinh z
sinh
(PyObject z)

Return the hyperbolic sine of z.

private static PyComplex

Returns:

sinh z or sin z.
sinOrSinh
(PyComplex z, boolean
true for sinh, false for sin.
h
)

Helper to compute either sin z or sinh z.

public static PyComplex

Returns:

w½
sqrt
(PyObject
to square-root
w
)

Calculate z = x+iy, such that z2 = w.

public static PyComplex

Returns:

tan z
tan
(PyObject z)

Return the tangent of z.

public static PyComplex

Returns:

tanh z
tanh
(PyObject z)

Return the hyperbolic tangent of z.

private static PyComplex

Returns:

tan or tanh z
tanOrTanh
(PyComplex z, boolean
true to compute tanh z, false to compute tan z.
h
)

Helper to compute either tan z or tanh z.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

ATLEAST_27LN2back to summary
private static final double ATLEAST_27LN2

For x larger than this, e-x is negligible compared with ex, or equivalently 1 is negligible compared with e2x, in IEEE-754 floating point. Beyond this, sinh x and cosh x are adequately approximated by 0.5ex. The smallest theoretical value is 27 ln(2).

eback to summary
public static final PyFloat e
HALF_E2back to summary
private static final double HALF_E2
LOG10Eback to summary
private static final double LOG10E

log10e (Ref: Abramowitz & Stegun [1972], p3).

NEARLY_LN_DBL_MAXback to summary
private static final double NEARLY_LN_DBL_MAX

ln(Double#MAX_VALUE) or a little less

piback to summary
public static final PyFloat pi
ROOT_HALFback to summary
private static final double ROOT_HALF

2-½ (Ref: Abramowitz & Stegun [1972], p2).

Constructor Detail

cmathback to summary
public cmath()

Method Detail

_acosback to summary
private static PyComplex _acos(PyComplex w)

Helper to compute cos-1w. The method used is as in CPython:

a = (1-w)½ = √2 sin z/2
b = (1+w)½ = √2 cos z/2

Then, with z = x+iy, a = a1+ia2, and b = b1+ib2,

a1 / b1 = tan x/2
a2b1 - a1b2 = sinh y

and we use Math#atan2(double, double) and math#asinh(double) to obtain x and y.

For w sufficiently large that w2≫1, cos-1w ≈ -i ln(2w).

Returns:PyComplex

cos-1w

_acoshback to summary
private static PyComplex _acosh(PyComplex w)

Helper to compute z = cosh-1w. The method used is as in CPython:

a = (w-1)½ = √2 sinh z/2
b = (w+1)½ = √2 cosh z/2

Then, with z = x+iy, a = a1+ia2, and b = b1+ib2,

a1b1 + a2b2 = sinh x
a2 / b1 = tan y/2

and we use math#asinh(double) and Math#atan2(double, double) to obtain x and y.

For w sufficiently large that w2≫1, cosh-1w ≈ ln(2w). We do not use this method also to compute cos-1w, because the branch cuts do not correspond.

Returns:PyComplex

cosh-1w

acosback to summary
public static PyComplex acos(PyObject w)

Return the arc cosine of w. There are two branch cuts. One extends right from 1 along the real axis to ∞, continuous from below. The other extends left from -1 along the real axis to -∞, continuous from above.

Returns:PyComplex

cos-1w

acoshback to summary
public static PyComplex acosh(PyObject w)

Return the hyperbolic arc cosine of w. There is one branch cut, extending left from 1 along the real axis to -∞, continuous from above.

Returns:PyComplex

cosh-1w

asinback to summary
public static PyComplex asin(PyObject w)

Return the arc sine of w. There are two branch cuts. One extends right from 1 along the real axis to ∞, continuous from below. The other extends left from -1 along the real axis to -∞, continuous from above.

Returns:PyComplex

sin-1w

asinhback to summary
public static PyComplex asinh(PyObject w)

Return the hyperbolic arc sine of w. There are two branch cuts. One extends from 1j along the imaginary axis to ∞j, continuous from the right. The other extends from -1j along the imaginary axis to -∞j, continuous from the left.

Returns:PyComplex

sinh-1w

asinOrAsinhback to summary
private static PyComplex asinOrAsinh(PyComplex w, boolean h)

Helper to compute either sin-1w or sinh-1w. The method used is as in CPython:

a = (1-iw)½ = √2 sin(π/4-iz/2)
b = (1+iw)½ = √2 cos(π/4-iz/2)

Then, with w = u+iv, z = x+iy, a = a1+ia2, and b = b1+ib2,

a1b2 - a2b2 = sinh x
v / (a2b1 - a1b2) = tan y

and we use math#asinh(double) and Math#atan2(double, double) to obtain x and y.

For w sufficiently large that w2≫1, sinh-1w ≈ ln(2w). When computing sin-1w, we evaluate -i sinh-1iw instead.

Parameters
h:boolean

true to compute sinh-1w, false to compute sin-1w.

Returns:PyComplex

sinh-1w or sin-1w

atanback to summary
public static PyComplex atan(PyObject w)

Return the arc tangent of w. There are two branch cuts. One extends from 1j along the imaginary axis to ∞j, continuous from the right. The other extends from -1j along the imaginary axis to -∞j, continuous from the left.

Returns:PyComplex

tan-1w

atanhback to summary
public static PyComplex atanh(PyObject w)

Return the hyperbolic arc tangent of w. There are two branch cuts. One extends from 1 along the real axis to ∞, continuous from below. The other extends from -1 along the real axis to -∞, continuous from above.

Returns:PyComplex

tanh-1w

atanOrAtanhback to summary
private static PyComplex atanOrAtanh(PyComplex w, boolean h)

Helper to compute either tan-1w or tanh-1w. The method used is close to that used in CPython. For z = tanh-1w:

z = ½ln(1 + 2w/(1-w))

Then, letting z = x+iy, and w = u+iv,

x = ¼ln(1 + 4u/((1-u)2+v2)) = -¼ln(1 - 4u/((1+u)2+v2))
y = ½tan-1(2v / ((1+u)(1-u)-v2))

We use math#log1p(double) and Math#atan2(double, double) to obtain x and y. The second expression for x is used when u<0. For w sufficiently large that w2≫1, tanh-1w ≈ 1/w ± iπ/2). For small w, tanh-1w ≈ w. When computing tan-1w, we evaluate -i tanh-1iw instead.

Parameters
h:boolean

true to compute tanh-1w, false to compute tan-1w.

Returns:PyComplex

tanh-1w or tan-1w

complexFromPyObjectback to summary
private static PyComplex complexFromPyObject(PyObject obj)
cosback to summary
public static PyComplex cos(PyObject z)

Return the cosine of z.

Returns:PyComplex

cos z

coshback to summary
public static PyComplex cosh(PyObject z)

Return the hyperbolic cosine of z.

Returns:PyComplex

cosh z

cosOrCoshback to summary
private static PyComplex cosOrCosh(PyComplex z, boolean h)

Helper to compute either cos z or cosh z.

Parameters
h:boolean

true for cosh, false for cos.

Returns:PyComplex

cos z or cosh z

exceptNaNback to summary
private static PyComplex exceptNaN(PyComplex result, PyComplex arg) throws PyException

Turn a NaN result into a thrown ValueError, a math domain error, if the original argument was not itself NaN. A PyComplex is a NaN if either component is a NaN.

Parameters
result:PyComplex

to return (if we return)

arg:PyComplex

to include in check

Returns:PyComplex

result if arg was NaN or result was not NaN

Exceptions
PyException:
ValueError if result was NaN and arg was not NaN
exceptNaNback to summary
private static PyComplex exceptNaN(PyComplex result, double a, double b) throws PyException

Raise ValueError if result is a NaN, but neither a nor b is NaN. Same as exceptNaN(PyComplex, PyComplex).

expback to summary
public static PyComplex exp(PyObject z)

Return the exponential value ez.

Returns:PyComplex

ez

isinfback to summary
public static boolean isinf(PyObject in)
Returns:boolean

true if in.real or in.imag is positive or negative infinity

isnanback to summary
public static boolean isnan(PyObject in)
Returns:boolean

true if in.real or in.imag is nan.

logback to summary
public static PyComplex log(PyObject w)

Returns the natural logarithm of w.

Returns:PyComplex

ln w

logback to summary
public static PyComplex log(PyObject w, PyObject b)

Returns the logarithm of w to the given base. If the base is not specified, returns the natural logarithm of w. There is one branch cut, from 0 along the negative real axis to -∞, continuous from above.

Returns:PyComplex

logbw

log10back to summary
public static PyComplex log10(PyObject w)

Returns the common logarithm of w (base 10 logarithm).

Returns:PyComplex

log10w

logHypotback to summary
private static double logHypot(double u, double v)

Helper function for the log of a complex number, dealing with the log magnitude, and without intermediate overflow or underflow. It returns ln r, where r2 = u2+v2. To do this it computes ½ln(u2+v2). Special cases are handled as follows:

  • if u or v is NaN, it returns NaN
  • if u or v is infinite, it returns positive infinity
  • if u and v are both zero, it raises a ValueError
We have this function instead of Math.log(Math.hypot(u,v)) because a valid result is still possible even when hypot(u,v) overflows, and because there's no point in taking a square root when a log is to follow.
Returns:double

½ln(u2+v2)

phaseback to summary
public static double phase(PyObject in)
polarback to summary
public static PyTuple polar(PyObject in)
rectback to summary
public static PyComplex rect(double r, double phi)

Return the complex number x with polar coordinates r and phi. Equivalent to r * (math.cos(phi) + math.sin(phi)*1j).

Parameters
r:double

radius

phi:double

angle

Returns:PyComplex

reiφ

sinback to summary
public static PyComplex sin(PyObject z)

Return the sine of z.

Returns:PyComplex

sin z

sinhback to summary
public static PyComplex sinh(PyObject z)

Return the hyperbolic sine of z.

Returns:PyComplex

sinh z

sinOrSinhback to summary
private static PyComplex sinOrSinh(PyComplex z, boolean h)

Helper to compute either sin z or sinh z.

Parameters
h:boolean

true for sinh, false for sin.

Returns:PyComplex

sinh z or sin z.

sqrtback to summary
public static PyComplex sqrt(PyObject w)

Calculate z = x+iy, such that z2 = w. In taking the square roots to get x and y, we choose to have x≥0 always, and y the same sign as v.

Parameters
w:PyObject

to square-root

Returns:PyComplex

w½

tanback to summary
public static PyComplex tan(PyObject z)

Return the tangent of z.

Returns:PyComplex

tan z

tanhback to summary
public static PyComplex tanh(PyObject z)

Return the hyperbolic tangent of z.

Returns:PyComplex

tanh z

tanOrTanhback to summary
private static PyComplex tanOrTanh(PyComplex z, boolean h)

Helper to compute either tan z or tanh z. The expression used is:

tanh(x+iy) = (sinh x cosh x + i sin y cos y) / (sinh2x + cos2y)

A simplification is made for x sufficiently large that e2|x|≫1 that deals satisfactorily with large or infinite x. When computing tan, we evaluate i tan iz instead, and the approximation applies to e2|y|≫1.

Parameters
h:boolean

true to compute tanh z, false to compute tan z.

Returns:PyComplex

tan or tanh z