Field of Arbitrary Precision Complex Numbers

AUTHORS:

  • William Stein (2006-01-26): complete rewrite
  • Niles Johnson (2010-08): trac ticket #3893: random_element() should pass on *args and **kwds.
  • Travis Scrimshaw (2012-10-18): Added documentation for full coverage.
sage.rings.complex_field.ComplexField(prec=53, names=None)

Return the complex field with real and imaginary parts having prec bits of precision.

EXAMPLES:

sage: ComplexField()
Complex Field with 53 bits of precision
sage: ComplexField(100)
Complex Field with 100 bits of precision
sage: ComplexField(100).base_ring()
Real Field with 100 bits of precision
sage: i = ComplexField(200).gen()
sage: i^2
-1.0000000000000000000000000000000000000000000000000000000000
class sage.rings.complex_field.ComplexField_class(prec=53)

Bases: sage.rings.ring.Field

An approximation to the field of complex numbers using floating point numbers with any specified precision. Answers derived from calculations in this approximation may differ from what they would be if those calculations were performed in the true field of complex numbers. This is due to the rounding errors inherent to finite precision calculations.

EXAMPLES:

sage: C = ComplexField(); C
Complex Field with 53 bits of precision
sage: Q = RationalField()
sage: C(1/3)
0.333333333333333
sage: C(1/3, 2)
0.333333333333333 + 2.00000000000000*I
sage: C(RR.pi())
3.14159265358979
sage: C(RR.log2(), RR.pi())
0.693147180559945 + 3.14159265358979*I

We can also coerce rational numbers and integers into C, but coercing a polynomial will raise an exception:

sage: Q = RationalField()
sage: C(1/3)
0.333333333333333
sage: S = PolynomialRing(Q, 'x')
sage: C(S.gen())
Traceback (most recent call last):
...
TypeError: unable to coerce to a ComplexNumber: <type 'sage.rings.polynomial.polynomial_rational_flint.Polynomial_rational_flint'>

This illustrates precision:

sage: CC = ComplexField(10); CC(1/3, 2/3)
0.33 + 0.67*I
sage: CC
Complex Field with 10 bits of precision
sage: CC = ComplexField(100); CC
Complex Field with 100 bits of precision
sage: z = CC(1/3, 2/3); z
0.33333333333333333333333333333 + 0.66666666666666666666666666667*I

We can load and save complex numbers and the complex field:

sage: loads(z.dumps()) == z
True
sage: loads(CC.dumps()) == CC
True
sage: k = ComplexField(100)
sage: loads(dumps(k)) == k
True

This illustrates basic properties of a complex field:

sage: CC = ComplexField(200)
sage: CC.is_field()
True
sage: CC.characteristic()
0
sage: CC.precision()
200
sage: CC.variable_name()
'I'
sage: CC == ComplexField(200)
True
sage: CC == ComplexField(53)
False
sage: CC == 1.1
False
algebraic_closure()

Return the algebraic closure of self (which is itself).

EXAMPLES:

sage: CC
Complex Field with 53 bits of precision
sage: CC.algebraic_closure()
Complex Field with 53 bits of precision
sage: CC = ComplexField(1000)
sage: CC.algebraic_closure() is CC
True
characteristic()

Return the characteristic of \(\CC\), which is 0.

EXAMPLES:

sage: ComplexField().characteristic()
0
construction()

Returns the functorial construction of self, namely the algebraic closure of the real field with the same precision.

EXAMPLES:

sage: c, S = CC.construction(); S
Real Field with 53 bits of precision
sage: CC == c(S)
True
gen(n=0)

Return the generator of the complex field.

EXAMPLES:

sage: ComplexField().gen(0)
1.00000000000000*I
is_exact()

Return whether or not this field is exact, which is always False.

EXAMPLES:

sage: ComplexField().is_exact()
False
ngens()

The number of generators of this complex field as an \(\RR\)-algebra.

There is one generator, namely sqrt(-1).

EXAMPLES:

sage: ComplexField().ngens()
1
pi()

Returns \(\pi\) as a complex number.

EXAMPLES:

sage: ComplexField().pi()
3.14159265358979
sage: ComplexField(100).pi()
3.1415926535897932384626433833
prec()

Return the precision of this complex field.

EXAMPLES:

sage: ComplexField().prec()
53
sage: ComplexField(15).prec()
15
precision()

Return the precision of this complex field.

EXAMPLES:

sage: ComplexField().prec()
53
sage: ComplexField(15).prec()
15
random_element(component_max=1, *args, **kwds)

Returns a uniformly distributed random number inside a square centered on the origin (by default, the square \([-1,1] \times [-1,1]\)).

Passes additional arguments and keywords to underlying real field.

EXAMPLES:

sage: [CC.random_element() for _ in range(5)]
[0.153636193785613 - 0.502987375247518*I,
 0.609589964322241 - 0.948854594338216*I,
 0.968393085385764 - 0.148483595843485*I,
 -0.908976099636549 + 0.126219184235123*I,
 0.461226845462901 - 0.0420335212948924*I]
sage: CC6 = ComplexField(6)
sage: [CC6.random_element(2^-20) for _ in range(5)]
[-5.4e-7 - 3.3e-7*I, 2.1e-7 + 8.0e-7*I, -4.8e-7 - 8.6e-7*I, -6.0e-8 + 2.7e-7*I, 6.0e-8 + 1.8e-7*I]
sage: [CC6.random_element(pi^20) for _ in range(5)]
[6.7e8 - 5.4e8*I, -9.4e8 + 5.0e9*I, 1.2e9 - 2.7e8*I, -2.3e9 - 4.0e9*I, 7.7e9 + 1.2e9*I]

Passes extra positional or keyword arguments through:

sage: [CC.random_element(distribution='1/n') for _ in range(5)]
[-0.900931453455899 - 0.932172283929307*I,
 0.327862582226912 + 0.828104487111727*I,
 0.246299162813240 + 0.588214960163442*I,
 0.892970599589521 - 0.266744694790704*I,
 0.878458776600692 - 0.905641181799996*I]
scientific_notation(status=None)

Set or return the scientific notation printing flag.

If this flag is True then complex numbers with this space as parent print using scientific notation.

EXAMPLES:

sage: C = ComplexField()
sage: C((0.025, 2))
0.0250000000000000 + 2.00000000000000*I
sage: C.scientific_notation(True)
sage: C((0.025, 2))
2.50000000000000e-2 + 2.00000000000000e0*I
sage: C.scientific_notation(False)
sage: C((0.025, 2))
0.0250000000000000 + 2.00000000000000*I
to_prec(prec)

Returns the complex field to the specified precision.

EXAMPLES:

sage: CC.to_prec(10)
Complex Field with 10 bits of precision
sage: CC.to_prec(100)
Complex Field with 100 bits of precision
zeta(n=2)

Return a primitive \(n\)-th root of unity.

INPUT:

  • n - an integer (default: 2)

OUTPUT: a complex \(n\)-th root of unity.

EXAMPLES:

sage: C = ComplexField()
sage: C.zeta(2)
-1.00000000000000
sage: C.zeta(5)
0.309016994374947 + 0.951056516295154*I
sage.rings.complex_field.is_ComplexField(x)

Check if x is a complex field.

EXAMPLES:

sage: from sage.rings.complex_field import is_ComplexField as is_CF
sage: is_CF(ComplexField())
True
sage: is_CF(ComplexField(12))
True
sage: is_CF(CC)
True
sage.rings.complex_field.late_import()

Import the objects/modules after build (when needed).