Module: sage.rings.complex_double
Double Precision Complex Numbers
SAGE supports arithmetic using double-precision complex numbers. A double-precision complex number is a complex number x + I*y with x, y 64-bit (8 byte) floating point numbers (double precision).
The field ComplexDoubleField
implements the field of all
double-precision complex numbers. You can refer to this field by the
shorthand CDF. Elements of this field are of type
ComplexDoubleElement
. If x and y are coercible to doubles, you
can create a complex double element using
ComplexDoubleElement(x,y)
. You can coerce more general objects
z to complex doubles by typing either ComplexDoubleField(x)
or
CDF(x)
.
sage: ComplexDoubleField() Complex Double Field sage: CDF Complex Double Field sage: type(CDF.0) <type 'sage.rings.complex_double.ComplexDoubleElement'> sage: ComplexDoubleElement(sqrt(2),3) 1.41421356237 + 3.0*I sage: parent(CDF(-2)) Complex Double Field
sage: CC == CDF False sage: CDF is ComplexDoubleField() # CDF is the shorthand True sage: CDF == ComplexDoubleField() True
The underlying arithmetic of complex numbers is implemented using functions and macros in GSL (the GNU Scientific Library), and should be very fast. Also, all standard complex trig functions, log, exponents, etc., are implemented using GSL, and are also robust and fast. Several other special functions, e.g. eta, gamma, incomplete gamma, etc., are implemented using the PARI C library.
Author: William Stein (2006-09): first version
Module-level Functions
) |
Returns the field of double precision complex numbers.
sage: ComplexDoubleField() Complex Double Field sage: ComplexDoubleField() is CDF True
) |
Return True if x is a is_ComplexDoubleElement.
sage: is_ComplexDoubleElement(0) False sage: is_ComplexDoubleElement(CDF(0)) True
) |
Return True if x is the complex double field.
sage: from sage.rings.complex_double import is_ComplexDoubleField sage: is_ComplexDoubleField(CDF) True sage: is_ComplexDoubleField(ComplexField(53)) False
Class: ComplexDoubleElement
Functions: abs,
abs2,
agm,
algdep,
arccos,
arccosh,
arccot,
arccoth,
arccsc,
arccsch,
arcsech,
arcsin,
arcsinh,
arctan,
arctanh,
arg,
argument,
conj,
conjugate,
cos,
cosh,
cot,
coth,
csc,
csch,
dilog,
eta,
exp,
gamma,
gamma_inc,
imag,
is_square,
log,
log10,
log_b,
logabs,
norm,
nth_root,
parent,
prec,
real,
sec,
sech,
sin,
sinh,
sqrt,
tan,
tanh,
zeta
) |
This function returns the magnitude of the complex number
,
.
sage: CDF(2,3).abs() # slightly random-ish arch dependent output 3.6055512754639891
) |
This function returns the squared magnitude of the complex
number
,
.
sage: CDF(2,3).abs2() 13.0
) |
Return the arithmetic geometry mean of self and right.
The principal square root is always chosen.
sage: i = CDF(I) sage: (1+i).agm(2-i) 1.62780548487 + 0.136827548397*I
) |
Returns a polynomial of degree at most
which is approximately
satisfied by this complex number. Note that the returned polynomial
need not be irreducible, and indeed usually won't be if
is a good
approximation to an algebraic number of degree less than
.
ALGORITHM: Uses the PARI C-library algdep command.
sage: z = (1/2)*(1 + RDF(sqrt(3)) *CDF.0); z 0.5 + 0.866025403784*I sage: p = z.algdep(5); p x^5 + x^2 sage: p.factor() (x + 1) * x^2 * (x^2 - x + 1) sage: z^2 - z + 1 2.22044604925e-16...
sage: CDF(0,2).algdep(10) x^2 + 4 sage: CDF(1,5).algdep(2) x^2 - 2*x + 26
) |
This function returns the complex arccosine of the complex
number
,
. The branch cuts are on the real
axis, less than -1 and greater than 1.
sage: CDF(1,1).arccos() 0.904556894302 - 1.06127506191*I
) |
This function returns the complex hyperbolic arccosine of the
complex number
,
. The branch cut is on the real
axis, less than 1.
sage: CDF(1,1).arccosh() 1.06127506191 + 0.904556894302*I
) |
This function returns the complex arccotangent of the complex
number
,
sage: CDF(1,1).arccot() 0.553574358897 - 0.402359478109*I
) |
This function returns the complex hyperbolic arccotangent of the
complex number
,
.
sage: CDF(1,1).arccoth() 0.402359478109 - 0.553574358897*I
) |
This function returns the complex arccosecant of the complex
number
,
.
sage: CDF(1,1).arccsc() 0.452278447151 - 0.530637530953*I
) |
This function returns the complex hyperbolic arccosecant of the
complex number
,
.
sage: CDF(1,1).arccsch() 0.530637530953 - 0.452278447151*I
) |
This function returns the complex hyperbolic arcsecant of the
complex number
,
.
sage: CDF(1,1).arcsech() 0.530637530953 - 1.11851787964*I
) |
This function returns the complex arcsine of the complex
number
,
. The branch cuts are on the real axis,
less than -1 and greater than 1.
sage: CDF(1,1).arcsin() 0.666239432493 + 1.06127506191*I
) |
This function returns the complex hyperbolic arcsine of the
complex number
,
. The branch cuts are on the
imaginary axis, below -i and above i.
sage: CDF(1,1).arcsinh() 1.06127506191 + 0.666239432493*I
) |
This function returns the complex arctangent of the complex
number
,
. The branch cuts are on the imaginary
axis, below
and above
.
sage: CDF(1,1).arctan() 1.0172219679 + 0.402359478109*I
) |
This function returns the complex hyperbolic arctangent of the
complex number
,
. The branch cuts are on the real
axis, less than -1 and greater than 1.
sage: CDF(1,1).arctanh() 0.402359478109 + 1.0172219679*I
) |
This function returns the argument of the complex number
,
,
where
.
sage: CDF(1,0).arg() 0.0 sage: CDF(0,1).arg() 1.57079632679 sage: CDF(0,-1).arg() -1.57079632679 sage: CDF(-1,0).arg() 3.14159265359
) |
This function returns the argument of the self, in the interval
.
sage: CDF(6).argument() 0.0 sage: CDF(i).argument() 1.57079632679 sage: CDF(-1).argument() 3.14159265359 sage: CDF(-1 - 0.000001*i).argument() -3.14159165359
) |
This function returns the complex conjugate of the complex number
,
.
sage: z = CDF(2,3); z.conj() 2.0 - 3.0*I
) |
This function returns the complex conjugate of the complex number
,
.
sage: z = CDF(2,3); z.conjugate() 2.0 - 3.0*I
) |
This function returns the complex cosine of the complex number z,
.
sage: CDF(1,1).cos() 0.833730025131 - 0.988897705763*I
) |
This function returns the complex hyperbolic cosine of the complex
number
,
.
sage: CDF(1,1).cosh() 0.833730025131 + 0.988897705763*I
) |
This function returns the complex cotangent of the complex number
,
.
sage: CDF(1,1).cot() 0.217621561854 - 0.868014142896*I
) |
This function returns the complex hyperbolic cotangent of the
complex number
,
.
sage: CDF(1,1).coth() 0.868014142896 - 0.217621561854*I
) |
This function returns the complex cosecant of the complex number
,
.
sage: CDF(1,1).csc() 0.62151801717 - 0.303931001628*I
) |
This function returns the complex hyperbolic cosecant of the
complex number
,
.
sage: CDF(1,1).csch() 0.303931001628 - 0.62151801717*I
) |
Returns the principal branch of the dilogarithm of
, i.e.,
analytic continuation of the power series
sage: CDF(1,2).dilog() -0.0594747986738 + 2.07264797177*I sage: CDF(10000000,10000000).dilog() -134.411774491 + 38.793962999*I
) |
Return the value of the Dedekind
function on self,
intelligently computed using
transformations.
Input:
ALGORITHM: Uses the PARI C library, but with some modifications so it always works instead of failing on easy cases involving large input (like PARI does).
The
function is
We compute a few values of eta:
sage: CDF(0,1).eta() 0.768225422326 sage: CDF(1,1).eta() 0.742048775837 + 0.19883137023*I sage: CDF(25,1).eta() 0.742048775837 + 0.19883137023*I
Eta works even if the inputs are large.
sage: CDF(0,10^15).eta() 0 sage: CDF(10^15,0.1).eta() # slightly random-ish arch dependent output -0.121339721991 - 0.19619461894*I
We compute a few values of eta, but with the fractional power of e omited.
sage: CDF(0,1).eta(True) 0.998129069926
We compute eta to low precision directly from the definition.
sage: z = CDF(1,1); z.eta() 0.742048775837 + 0.19883137023*I sage: i = CDF(0,1); pi = CDF(pi) sage: exp(pi * i * z / 12) * prod([1-exp(2*pi*i*n*z) for n in range(1,10)]) 0.742048775837 + 0.19883137023*I
The optional argument allows us to omit the fractional part:
sage: z = CDF(1,1) sage: z.eta(omit_frac=True) 0.998129069926 sage: pi = CDF(pi) sage: prod([1-exp(2*pi*i*n*z) for n in range(1,10)]) # slightly random-ish arch dependent output 0.998129069926 + 4.5908467128e-19*I
We illustrate what happens when
is not in the
upper half plane.
sage: z = CDF(1) sage: z.eta() Traceback (most recent call last): ... ValueError: value must be in the upper half plane
You can also use functional notation.
sage: z = CDF(1,1) ; eta(z) 0.742048775837 + 0.19883137023*I
) |
This function returns the complex exponential of the complex
number
,
.
sage: CDF(1,1).exp() 1.46869393992 + 2.28735528718*I
We numerically verify a famous identity to the precision of a double.
sage: z = CDF(0, 2*pi); z 6.28318530718*I sage: exp(z) # somewhat random-ish output depending on platform 1.0 - 2.44921270764e-16*I
) |
Return the Gamma function evaluated at this complex number.
sage: CDF(5,0).gamma() 24.0 sage: CDF(1,1).gamma() 0.498015668118 - 0.154949828302*I sage: CDF(0).gamma() Infinity
) |
Return the incomplete Gamma function evaluated at this complex number.
sage: CDF(1,1).gamma_inc(CDF(2,3)) 0.00209691486365 - 0.0599819136554*I sage: CDF(1,1).gamma_inc(5) -0.00137813093622 + 0.00651982002312*I sage: CDF(2,0).gamma_inc(CDF(1,1)) 0.707092096346 - 0.42035364096*I
) |
Return the imaginary part of this complex double.
sage: a = CDF(3,-2) sage: a.imag() -2.0
) |
This function always returns true as
is algebraically closed.
sage: CDF(-1).is_square() True
) |
This function returns the complex natural logarithm to the
given base of the complex number
,
. The branch
cut is the negative real axis.
Input:
sage: CDF(1,1).log() 0.34657359028 + 0.785398163397*I
) |
This function returns the complex base-10 logarithm of the
complex number
,
.
The branch cut is the negative real axis.
sage: CDF(1,1).log10() 0.150514997832 + 0.34109408846*I
) |
This function returns the complex base-
logarithm of the
complex number
,
. This quantity is computed as the
ratio
.
The branch cut is the negative real axis.
sage: CDF(1,1).log_b(10) 0.150514997832 + 0.34109408846*I
) |
This function returns the natural logarithm of the magnitude of the complex
number
,
.
This allows for an accurate evaluation of
when
is close to
. The direct evaluation of
log(abs(z))
would lead to a loss of precision in this case.
sage: CDF(1.1,0.1).logabs() 0.0994254293726 sage: log(abs(CDF(1.1,0.1))) 0.0994254293726
sage: log(abs(ComplexField(200)(1.1,0.1))) 0.099425429372582675602989386713555936556752871164033127857198
) |
This function returns the squared magnitude of the complex
number
,
.
sage: CDF(2,3).norm() 13.0
) |
The n-th root function.
Input:
sage: a = CDF(125) sage: a.nth_root(3) 5.0 sage: a = CDF(10, 2) sage: [r^5 for r in a.nth_root(5, all=True)] [10.0 + 2.0*I, 10.0 + 2.0*I, 10.0 + 2.0*I, 10.0 + 2.0*I, 10.0 + 2.0*I] sage: abs(sum(a.nth_root(111, all=True))) # random but close to zero 6.00659385991e-14
) |
Return the complex double field, which is the parent of self.
sage: a = CDF(2,3) sage: a.parent() Complex Double Field sage: parent(a) Complex Double Field
) |
Returns the precision of this number (to be more similar to ComplexNumber). Always returns 53.
sage: CDF(0).prec() 53
) |
Return the real part of this complex double.
sage: a = CDF(3,-2) sage: a.real() 3.0
) |
This function returns the complex secant of the complex number
,
.
sage: CDF(1,1).sec() 0.498337030555 + 0.591083841721*I
) |
This function returns the complex hyperbolic secant of the complex
number
,
.
sage: CDF(1,1).sech() 0.498337030555 - 0.591083841721*I
) |
This function returns the complex sine of the complex number
,
.
sage: CDF(1,1).sin() 1.29845758142 + 0.634963914785*I
) |
This function returns the complex hyperbolic sine of the
complex number
,
.
sage: CDF(1,1).sinh() 0.634963914785 + 1.29845758142*I
) |
The square root function.
Input:
If all is False, the branch cut is the negative real axis. The result always lies in the right half of the complex plane.
We compute several square roots.
sage: a = CDF(2,3) sage: b = a.sqrt(); b 1.67414922804 + 0.89597747613*I sage: b^2 2.0 + 3.0*I sage: a^(1/2) 1.67414922804 + 0.89597747613*I
We compute the square root of -1.
sage: a = CDF(-1) sage: a.sqrt() 1.0*I
We compute all square roots:
sage: CDF(-2).sqrt(all=True) [1.41421356237*I, -1.41421356237*I] sage: CDF(0).sqrt(all=True) [0]
) |
This function returns the complex tangent of the complex number z,
.
sage: CDF(1,1).tan() 0.27175258532 + 1.08392332734*I
) |
This function returns the complex hyperbolic tangent of the
complex number
,
.
sage: CDF(1,1).tanh() 1.08392332734 + 0.27175258532*I
) |
Return the Riemann zeta function evaluated at this complex number.
sage: z = CDF(1, 1) sage: z.zeta() 0.582158059752 - 0.926848564331*I sage: zeta(z) 0.582158059752 - 0.926848564331*I
Special Functions: __abs__,
__complex__,
__eq__,
__float__,
__ge__,
__getitem__,
__gt__,
__init__,
__int__,
__invert__,
__le__,
__long__,
__lt__,
__ne__,
__pow__,
__reduce__,
__repr__,
__rpow__,
_interface_init_,
_latex_,
_pari_,
_pow_
) |
This function returns the magnitude of the complex number
,
.
sage: abs(CDF(1,2)) 2.2360679775 sage: abs(CDF(1,0)) 1.0 sage: abs(CDF(-2,3)) # slightly random-ish arch dependent output 3.6055512754639891
) |
sage: a = complex(2303,-3939) sage: CDF(a) 2303.0 - 3939.0*I sage: complex(CDF(a)) (2303-3939j)
) |
sage: float(CDF(1,1)) Traceback (most recent call last): ... TypeError: can't convert complex to float; use abs(z) sage: float(abs(CDF(1,1))) 1.4142135623730951
) |
Returns the real or imaginary part of self.
Input:
Raises an IndexError if n < 0 or n > 1.
sage: P = CDF(2,3) sage: P[0] 2.0 sage: P[1] 3.0 sage: P[3] Traceback (most recent call last): ... IndexError: index n must be 0 or 1
) |
sage: int(CDF(1,1)) Traceback (most recent call last): ... TypeError: can't convert complex to int; use int(abs(z)) sage: int(abs(CDF(1,1))) 1
) |
This function returns the inverse, or reciprocal, of the
complex number
,
sage: ~CDF(2,1) 0.4 - 0.2*I sage: 1/CDF(2,1) 0.4 - 0.2*I
The inverse of 0 is nan (it doesn't raise an exception):
sage: ~(0*CDF(0,1)) nan + nan*I
) |
sage: long(CDF(1,1)) Traceback (most recent call last): ... TypeError: can't convert complex to long; use long(abs(z)) sage: long(abs(CDF(1,1))) 1L
) |
sage: a = CDF(-2.7, -3) sage: loads(dumps(a)) == a True
) |
Return print version of self.
sage: a = CDF(2,-3); a 2.0 - 3.0*I sage: a^2 -5.0 - 12.0*I
) |
Returns self formatted as a string, suitable as input to another computer algebra system. (This is the default function used for exporting to other computer algebra systems.)
sage: s1 = CDF(exp(I)); s1 0.540302305868 + 0.841470984808*I sage: s1._interface_init_() '0.54030230586813977 + 0.84147098480789650*I' sage: s1 == CDF(gp(s1)) True
) |
sage: CDF(1, 2)._latex_() '1.0 + 2.0i' sage: z = CDF(1,2)^100 sage: z._latex_() '-6.44316469099 \times 10^{34} - 6.11324130776 \times 10^{34}i'
) |
Return PARI version of self.
sage: CDF(1,2)._pari_() 1.0000000000000000000 + 2.000000000000000000*I sage: pari(CDF(1,2)) 1.0000000000000000000 + 2.000000000000000000*I
) |
The function returns the complex number
raised to the
complex power
,
.
Input:
sage: a = CDF(1,1); b = CDF(2,3) sage: a._pow_(b) -0.163450932107 + 0.0960049836089*I
Class: ComplexDoubleField_class
ALGORITHMS: Arithmetic is done using GSL (the GNU Scientific Library).
Functions: characteristic,
construction,
gen,
is_exact,
ngens,
pi,
prec,
random_element,
real_double_field,
zeta
) |
Return the characteristic of this complex double field, which is 0.
sage: CDF.characteristic() 0
) |
Returns the functorial construction of self, namely, algebraic closure of the real double field.
sage: c, S = CDF.construction(); S Real Double Field sage: CDF == c(S) True
) |
Return the generator of the complex double field.
sage: CDF.0 1.0*I sage: CDF.gens() (1.0*I,)
) |
Returns whether or not this field is exact, which is always false.
sage: CDF.is_exact() False
) |
The number of generators of this complex field as an RR-algebra.
There is one generator, namely sqrt(-1).
sage: CDF.ngens() 1
) |
Returns pi as a double precision complex number.
sage: CDF.pi() 3.14159265359
) |
Return the precision of this complex double field (to be more similar to ComplexField). Always returns 53.
sage: CDF.prec() 53
) |
Return a random element this complex double field with real and imaginary part bounded by xmin, xmax, ymin, ymax.
sage: CDF.random_element() -0.436810529675 + 0.736945423566*I sage: CDF.random_element(-10,10,-10,10) -7.08874026302 - 9.54135400334*I sage: CDF.random_element(-10^20,10^20,-2,2) -7.58765473764e+19 + 0.925549022839*I
) |
The real double field, which you may view as a subfield of this complex double field.
sage: CDF.real_double_field() Real Double Field
) |
Return a primitive
-th root of unity in this CDF, for
.
Input:
sage: CDF.zeta(7) 0.623489801859 + 0.781831482468*I sage: CDF.zeta(1) 1.0 sage: CDF.zeta() -1.0 sage: CDF.zeta() == CDF.zeta(2) True
sage: CDF.zeta(0.5) Traceback (most recent call last): ... ValueError: n must be a positive integer sage: CDF.zeta(0) Traceback (most recent call last): ... ValueError: n must be a positive integer sage: CDF.zeta(-1) Traceback (most recent call last): ... ValueError: n must be a positive integer
Special Functions: __call__,
__cmp__,
__eq__,
__ge__,
__gt__,
__init__,
__le__,
__lt__,
__ne__,
__repr__
) |
Create a complex double using x and optionally an imaginary part im.
sage: CDF(0,1) 1.0*I sage: CDF(2/3) 0.666666666667 sage: CDF(5) 5.0 sage: CDF('i') 1.0*I sage: CDF(complex(2,-3)) 2.0 - 3.0*I sage: CDF(4.5) 4.5 sage: CDF(1+I) 1.0 + 1.0*I
A TypeError is raised if the coercion doesn't make sense:
sage: CDF(QQ['x'].0) Traceback (most recent call last): ... TypeError: cannot coerce nonconstant polynomial to float
One can convert back and forth between double precision complex numbers and higher-precision ones, though of course there may be loss of precision:
sage: a = ComplexField(200)(-2).sqrt(); a 1.4142135623730950488016887242096980785696718753769480731767*I sage: b = CDF(a); b 1.41421356237*I sage: a.parent()(b) 1.4142135623730951454746218587388284504413604736328125000000*I sage: a.parent()(b) == b True sage: b == CC(a) True
) |
Print out this complex double field.
sage: ComplexDoubleField() Complex Double Field sage: CDF Complex Double Field
Class: FloatToCDF
sage: f = CDF.coerce_map_from(ZZ); f Native morphism: From: Integer Ring To: Complex Double Field sage: f(4) 4.0 sage: f = CDF.coerce_map_from(QQ); f Native morphism: From: Rational Field To: Complex Double Field sage: f(1/2) 0.5 sage: f = CDF.coerce_map_from(int); f Native morphism: From: Set of Python objects of type 'int' To: Complex Double Field sage: f(3r) 3.0 sage: f = CDF.coerce_map_from(float); f Native morphism: From: Set of Python objects of type 'float' To: Complex Double Field sage: f(3.5) 3.5
Special Functions: __init__,
_repr_type
) |
See About this document... for information on suggesting changes.