Lazy real and complex numbers

These classes are very lazy, in the sense that it doesn’t really do anything but simply sits between exact rings of characteristic 0 and the real numbers. The values are actually computed when they are cast into a field of fixed precision.

The main purpose of these classes is to provide a place for exact rings (e.g. number fields) to embed for the coercion model (as only one embedding can be specified in the forward direction).

sage.rings.real_lazy.ComplexLazyField()

Returns the lazy complex field.

EXAMPLES:

There is only one lazy complex field:

sage: ComplexLazyField() is ComplexLazyField()
True
class sage.rings.real_lazy.ComplexLazyField_class

Bases: sage.rings.real_lazy.LazyField

This class represents the set of complex numbers to unspecified precision. For the most part it simply wraps exact elements and defers evaluation until a specified precision is requested.

For more information, see the documentation of the RLF.

EXAMPLES:

sage: a = CLF(-1).sqrt()
sage: a
1*I
sage: CDF(a)
1.0*I
sage: ComplexField(200)(a)
1.0000000000000000000000000000000000000000000000000000000000*I

Note

The following TestSuite failure:

sage: CLF._test_prod()
Traceback (most recent call last):
...
AssertionError: False is not true

is due to (acceptable?) numerical noise:

sage: x = CLF.I
sage: x*x == x^2
False
sage: x*x
-1
sage: x^2
-0.9999999999999999? + 0.?e-15*I
construction()

Returns the functorial construction of self, namely, algebraic closure of the real lazy field.

EXAMPLES:

sage: c, S = CLF.construction(); S
Real Lazy Field
sage: CLF == c(S)
True
gen(i=0)

Return the \(i\)-th generator of self.

EXAMPLES:

sage: CLF.gen()
1*I
sage: ComplexField(100)(CLF.gen())
1.0000000000000000000000000000*I
interval_field(prec=None)

Returns the interval field that represents the same mathematical field as self.

EXAMPLES:

sage: CLF.interval_field()
Complex Interval Field with 53 bits of precision
sage: CLF.interval_field(333)
Complex Interval Field with 333 bits of precision
sage: CLF.interval_field() is CIF
True
class sage.rings.real_lazy.LazyAlgebraic

Bases: sage.rings.real_lazy.LazyFieldElement

This represents an algebraic number, specified by a polynomial over \(\QQ\) and a real or complex approximation.

EXAMPLES:

sage: x = polygen(QQ)
sage: from sage.rings.real_lazy import LazyAlgebraic
sage: a = LazyAlgebraic(RLF, x^2-2, 1.5)
sage: a
1.414213562373095?
eval(R)

Convert self into an element of R.

EXAMPLES:

sage: from sage.rings.real_lazy import LazyAlgebraic
sage: a = LazyAlgebraic(CLF, QQ['x'].cyclotomic_polynomial(7), 0.6+0.8*CC.0)
sage: a
0.6234898018587335? + 0.7818314824680299?*I
sage: ComplexField(150)(a) # indirect doctest
0.62348980185873353052500488400423981063227473 + 0.78183148246802980870844452667405775023233452*I

sage: a = LazyAlgebraic(CLF, QQ['x'].0^2-7, -2.0)
sage: RR(a)
-2.64575131106459
sage: RR(a)^2
7.00000000000000
class sage.rings.real_lazy.LazyBinop

Bases: sage.rings.real_lazy.LazyFieldElement

A lazy element representing a binary (usually arithmetic) operation between two other lazy elements.

EXAMPLES:

sage: from sage.rings.real_lazy import LazyBinop
sage: a = LazyBinop(RLF, 2, 1/3, operator.add)
sage: a
2.333333333333334?
sage: Reals(200)(a)
2.3333333333333333333333333333333333333333333333333333333333
depth()

Return the depth of self as an arithmetic expression.

This is the maximum number of dependent intermediate expressions when evaluating self, and is used to determine the precision needed to get the final result to the desired number of bits.

It is equal to the maximum of the right and left depths, plus one.

EXAMPLES:

sage: from sage.rings.real_lazy import LazyBinop
sage: a = LazyBinop(RLF, 6, 8, operator.mul)
sage: a.depth()
1
sage: b = LazyBinop(RLF, 2, a, operator.sub)
sage: b.depth()
2
eval(R)

Convert the operands to elements of R, then perform the operation on them.

EXAMPLES:

sage: from sage.rings.real_lazy import LazyBinop
sage: a = LazyBinop(RLF, 6, 8, operator.add)
sage: a.eval(RR)
14.0000000000000

A bit absurd:

sage: a.eval(str)
'68'
class sage.rings.real_lazy.LazyConstant

Bases: sage.rings.real_lazy.LazyFieldElement

This class represents a real or complex constant (such as pi or I).

eval(R)

Convert self into an element of R.

EXAMPLES:

sage: from sage.rings.real_lazy import LazyConstant
sage: a = LazyConstant(RLF, 'e')
sage: RDF(a) # indirect doctest
2.718281828459045
sage: a = LazyConstant(CLF, 'I')
sage: CC(a)
1.00000000000000*I
class sage.rings.real_lazy.LazyField

Bases: sage.rings.ring.Field

The base class for lazy real fields.

Warning

LazyField uses __getattr__(), to implement:

sage: CLF.pi
3.141592653589794?

I (NT, 20/04/2012) did not manage to have __getattr__ call Parent.__getattr__() in case of failure; hence we can’t use this __getattr__ trick for extension types to recover the methods from categories. Therefore, at this point, no concrete subclass of this class should be an extension type (which is probably just fine):

sage: RLF.__class__
<class 'sage.rings.real_lazy.RealLazyField_class_with_category'>
sage: CLF.__class__
<class 'sage.rings.real_lazy.ComplexLazyField_class_with_category'>
Element

alias of LazyWrapper

algebraic_closure()

Returns the algebraic closure of self, i.e., the complex lazy field.

EXAMPLES:

sage: RLF.algebraic_closure()
Complex Lazy Field

sage: CLF.algebraic_closure()
Complex Lazy Field
interval_field(prec=None)

Abstract method to create the corresponding interval field.

class sage.rings.real_lazy.LazyFieldElement

Bases: sage.structure.element.FieldElement

approx()

Returns self as an element of an interval field.

EXAMPLES:

sage: CLF(1/6).approx()
0.1666666666666667?
sage: CLF(1/6).approx().parent()
Complex Interval Field with 53 bits of precision

When the absolute value is involved, the result might be real:

sage: z = exp(CLF(1 + I/2)); z
2.38551673095914? + 1.303213729686996?*I
sage: r = z.abs(); r
2.71828182845905?
sage: parent(z.approx())
Complex Interval Field with 53 bits of precision
sage: parent(r.approx())
Real Interval Field with 53 bits of precision
continued_fraction()

Return the continued fraction of self.

EXAMPLES:

sage: a = RLF(sqrt(2)) + RLF(sqrt(3))
sage: cf = a.continued_fraction()
sage: cf
[3; 6, 1, 5, 7, 1, 1, 4, 1, 38, 43, 1, 3, 2, 1, 1, 1, 1, 2, 4, ...]
sage: cf.convergent(100)
444927297812646558239761867973501208151173610180916865469/141414466649174973335183571854340329919207428365474086063
depth()

Abstract method for returning the depth of self as an arithmetic expression.

This is the maximum number of dependent intermediate expressions when evaluating self, and is used to determine the precision needed to get the final result to the desired number of bits.

It is equal to the maximum of the right and left depths, plus one.

EXAMPLES:

sage: from sage.rings.real_lazy import LazyBinop
sage: a = LazyBinop(RLF, 6, 8, operator.mul)
sage: a.depth()
1
eval(R)

Abstract method for converting self into an element of R.

EXAMPLES:

sage: a = RLF(12)
sage: a.eval(ZZ)
12
class sage.rings.real_lazy.LazyNamedUnop

Bases: sage.rings.real_lazy.LazyUnop

This class is used to represent the many named methods attached to real numbers, and is instantiated by the __getattr__ method of LazyElements.

EXAMPLES:

sage: from sage.rings.real_lazy import LazyNamedUnop
sage: a = LazyNamedUnop(RLF, 1, 'arcsin')
sage: RR(a)
1.57079632679490
sage: a = LazyNamedUnop(RLF, 9, 'log', extra_args=(3,))
sage: RR(a)
2.00000000000000
approx()

Does something reasonable with functions that are not defined on the interval fields.

eval(R)

Convert self into an element of R.

class sage.rings.real_lazy.LazyUnop

Bases: sage.rings.real_lazy.LazyFieldElement

Represents a unevaluated single function of one variable.

EXAMPLES:

sage: from sage.rings.real_lazy import LazyUnop
sage: a = LazyUnop(RLF, 3, sqrt); a
1.732050807568878?
sage: a._arg
3
sage: a._op
<function sqrt at ...>
sage: Reals(100)(a)
1.7320508075688772935274463415
sage: Reals(100)(a)^2
3.0000000000000000000000000000
depth()

Return the depth of self as an arithmetic expression.

This is the maximum number of dependent intermediate expressions when evaluating self, and is used to determine the precision needed to get the final result to the desired number of bits.

It is equal to one more than the depth of its operand.

EXAMPLES:

sage: from sage.rings.real_lazy import LazyUnop
sage: a = LazyUnop(RLF, 3, sqrt)
sage: a.depth()
1
sage: b = LazyUnop(RLF, a, sin)
sage: b.depth()
2
eval(R)

Convert self into an element of R.

EXAMPLES:

sage: from sage.rings.real_lazy import LazyUnop
sage: a = LazyUnop(RLF, 3, sqrt)
sage: a.eval(ZZ)
sqrt(3)
class sage.rings.real_lazy.LazyWrapper

Bases: sage.rings.real_lazy.LazyFieldElement

A lazy element that simply wraps an element of another ring.

EXAMPLES:

sage: from sage.rings.real_lazy import LazyWrapper
sage: a = LazyWrapper(RLF, 3)
sage: a._value
3
continued_fraction()

Return the continued fraction of self.

EXAMPLES:

sage: a = RLF(sqrt(2))
sage: a.continued_fraction()
[1; 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, ...]
depth()

Returns the depth of self as an expression, which is always 0.

EXAMPLES:

sage: RLF(4).depth()
0
eval(R)

Convert self into an element of R.

EXAMPLES:

sage: a = RLF(12)
sage: a.eval(ZZ)
12
sage: a.eval(ZZ).parent()
Integer Ring
class sage.rings.real_lazy.LazyWrapperMorphism

Bases: sage.categories.morphism.Morphism

This morphism coerces elements from anywhere into lazy rings by creating a wrapper element (as fast as possible).

EXAMPLES:

sage: from sage.rings.real_lazy import LazyWrapperMorphism
sage: f = LazyWrapperMorphism(QQ, RLF)
sage: a = f(3); a
3
sage: type(a)
<type 'sage.rings.real_lazy.LazyWrapper'>
sage: a._value
3
sage: a._value.parent()
Rational Field
sage.rings.real_lazy.RealLazyField()

Return the lazy real field.

EXAMPLES:

There is only one lazy real field:

sage: RealLazyField() is RealLazyField()
True
class sage.rings.real_lazy.RealLazyField_class

Bases: sage.rings.real_lazy.LazyField

This class represents the set of real numbers to unspecified precision. For the most part it simply wraps exact elements and defers evaluation until a specified precision is requested.

Its primary use is to connect the exact rings (such as number fields) to fixed precision real numbers. For example, to specify an embedding of a number field \(K\) into \(\RR\) one can map into this field and the coercion will then be able to carry the mapping to real fields of any precision.

EXAMPLES:

sage: a = RLF(1/3)
sage: a
0.3333333333333334?
sage: a + 1/5
0.5333333333333334?
sage: a = RLF(1/3)
sage: a
0.3333333333333334?
sage: a + 5
5.333333333333334?
sage: RealField(100)(a+5)
5.3333333333333333333333333333
sage: CC.0 + RLF(1/3)
0.333333333333333 + 1.00000000000000*I
sage: ComplexField(200).0 + RLF(1/3)
0.33333333333333333333333333333333333333333333333333333333333 + 1.0000000000000000000000000000000000000000000000000000000000*I
construction()

Returns the functorial construction of self, namely, the completion of the rationals at infinity to infinite precision.

EXAMPLES:

sage: c, S = RLF.construction(); S
Rational Field
sage: RLF == c(S)
True
gen(i=0)

Return the \(i\)-th generator of self.

EXAMPLES:

sage: RLF.gen()
1
interval_field(prec=None)

Returns the interval field that represents the same mathematical field as self.

EXAMPLES:

sage: RLF.interval_field()
Real Interval Field with 53 bits of precision
sage: RLF.interval_field(200)
Real Interval Field with 200 bits of precision
sage.rings.real_lazy.make_element(parent, *args)

Create an element of parent.

EXAMPLES:

sage: a = RLF(pi) + RLF(sqrt(1/2)) # indirect doctest
sage: bool(loads(dumps(a)) == a)
True