Constants#

Constants are a unique type of variable were the value is known.

class cake.Constant(coefficient: Any = 1, power: Any = 1)[source]#

Bases: Variable, ABC

Generic base class for defining constants, behaves similar to Variable.

class MyConstant(Constant):
    @classmethod
    def _to_type(cls, coefficient: Any = 1, power: Any = 1) -> MyConstant:
        return cls(coefficient, power)

    @property
    def c_value(self) -> Any:
        return 5.5  ## Value of the constant

## Lets use our constant
mc = MyConstant()
x = Variable(x)
expr = x + mc(2)
print(expr)
### x + 2MyConstant
print(expr.solve(x=3))
### 14
Parameters:
  • coefficient (Any[Like[cake.BasicNode]]) – Coefficient of constant

  • power (Any[Like[cake.BasicNode]]) – Power the constant is raised to

abstract property c_value: Any#

Returns default value of the constant

copy() Constant[source]#

Returns a shallow copy of the variable

static is_similar(x: Variable, y: Variable) bool#

Returns whether 2 Variables can interact with one another,

so Variable.is_similar(3x, 4x) is True but Variable.is_similar(4y, 3x) is False.

classmethod many(*symbols) List[Variable]#

Returns a list of Variables from the input given

>>> Variable.many('x', 'y')
[Variable('x'), Variable('y')]
>>> Variable.many(('x', 5), ('y', 1, 2), 'z')
[Variable('x', coefficient=5), Variable('y', coefficient=1, power=2), Variable('z')]
solve(*_, **kwds) Any[source]#

Solves the variable with provided values

>>> x = Variable('x')
>>> x *= 3
>>> x **= 2
>>> x
3x**2
>>> x.solve(5)
75
>>> x.solve(x=5)
75
>>> x = Variable('x', coefficient=Variable('y', power=2))
>>> x
y**2x
>>> x.solve(2, y=3)
18
>>> x.solve(x=2, y=3)
18