Numbers#

The cake library provides basic number classes which interact and function the same as regular python number classes.

Number#

class cake.Number(_Number__value: N, /)[source]#

Bases: Number

Basic class for creating different types of numbers, this class can be treated as a normal number like in python.

Tip

To retrieve the actual value of the class, use Number.value

from cake import Number, Variable

x = 5
x += Number(10)
## x is now an Integral not a Number

a = Variable('a')
x = x + a
## x is now an Expression
## x = Expr(Integral(15), Variable('a'))
## which can be represented as f(x) = 15 + a
static convert(x: Number | Number | BasicExpression) Number[source]#

Used to convert generic python types into types of the cake library

>>> Number.convert(1)
Integral(1)
>>> Number.convert(1.4)
Real(1.4)
Parameters:

x (Any[Like[Number]]) – Value to convert, can be any value, if value cannot be converted the original value is returned.

Complex#

class cake.Complex(real: N, imag: N = 0, /)[source]#

Bases: Number, Complex, Complex

Represents a generic complex number

>>> Complex(1, 2) == 1+2j
True
>>> Complex(1, 2j) == 1+2j
True
conjugate() Any[source]#

(x+y*i).conjugate() returns (x-y*i).

property imag: Any#

Retrieve the imaginary component of this number.

This should subclass Real.

property real: Any#

Retrieve the real component of this number.

This should subclass Real.

Real#

class cake.Real(value: N, /)[source]#

Bases: Complex, Real, Real

Represents a generic real number

>>> Real(1.5) == 1.5
True
>>> Real(Integral(5)) = 5
True
as_integer_ratio()[source]#

Return integer ratio.

Return a pair of integers, whose ratio is exactly equal to the original float and with a positive denominator.

Raise OverflowError on infinities and a ValueError on NaNs.

>>> (10.0).as_integer_ratio()
(10, 1)
>>> (0.0).as_integer_ratio()
(0, 1)
>>> (-.25).as_integer_ratio()
(-1, 4)

Rational#

class cake.Rational(value_or_numerator, denominator=None)[source]#

Bases: Real, Rational, Rational

Represents a generic rational number

>>> Rational(1, 3) == (1/3)
True
>>> Rational(1.5) == 1.5
True

Integral#

class cake.Integral(value: int)[source]#

Bases: Rational, Integral, Integral

Represents a generic integer

>>> Integral(10) == 10
True