Variables#
Variables are a core part of the cake library and offer the capability of defining expressions, and working through unknown values with symbol representation.
Variable#
- class cake.Variable(repr: str, coefficient: Any = 1, power: Any = 1)[source]#
Bases:
Variable,BasicVariableAn object which represents an Variable number/value, this class be integrated with other components within the cake library.
Note
Using comparity operators such as
>will not return a boolean value, however,==and!=will return a bool value when comparing unknowns.>>> x = Variable('x') >>> x > 9 Comparity(left=x, right=9, symbol='>') >>> (x > 9).fits(x=10) True >>> (x > 9).fits(x=5) False >>> (5 < x < 10).fits(x=10) False >>> (5 < x < 10).fits(x=7) True
Tip
Using Variables in functions
from cake import Sin, Variable f = Sin(Variable('x')) # Can be shown as f(x) = sin x # Can be extended to expressions not just limited to a single variable. print(f.evaluate(x=90)) # 1
- static is_similar(x: Variable, y: Variable) bool[source]#
Returns whether 2 Variables can interact with one another,
so
Variable.is_similar(3x, 4x)is True butVariable.is_similar(4y, 3x)is False.
- solve(value: Number | Variable | BasicExpression | Number | Function | None = None, **_v) Variable | BasicExpression | U[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
Raised Variables#
- class cake.RaisedVariable(base: Any, power: Any = 1)[source]#
Bases:
Generic[U],BasicNode,BasicVariableA raised Variable is where a literal or Variable value is raised to another Variable value, we use this class as it maintains logic within the library.
Note
Most operations which are ran on this object will more then likely return expressions!
Warning
Unlike standard variables, raised variables will always return a comparity class when comparing, to check if 2 raised variables are the same use
RaisedVariable.is_similar().>>> I = Integral(3) >>> X = Variable('x') >>> R = I ** X RaisedVariable(3 ** x) >>> R * 2 Expression(Multiply(3 ** x, 2))
- copy() RaisedVariable[source]#
Returns a shallow copy of the class
- static is_similar(x: RaisedVariable, y: RaisedVariable) bool[source]#
Checks if 2 raised variables are similar
Variable Groups#
- class cake.VariableGroup(coefficient: Any, *Variables)[source]#
Bases:
Generic[U],BasicNode,BasicVariableAn Variable group is used where multiple Variables make up a single Variable value, So,
5xis an Variable whereas5x * ywould be an VariableGroup as theres 2 values.Warning
Reading coffecients from
VariableGroup.groupswill always be one, instead useVariableGroup.coefficient.Note
Groups generally shouldn’t need to be made manually, they can easily be made by manipulating standard Variables.
>>> x, y = Variable.gen_many('x', 'y') >>> g = x * y >>> g VariableGroup(xy) >>> g.groups [Variable('x'), Variable('y')] >>> g * y VariableGroup(xy**2) # Groups == [Variable('x'), Variable('y', power=2, ...)] >>> g + y Expression(xy + y)
- as_mapping() dict[source]#
Generates a mapping of the group.
>>> g = x * y >>> g.as_mapping() { 'x': Unknown('x') 'y': Unknown('y') } >>> g *= 2 >>> g.as_mapping() { 'x': Unknown('x', coefficient=2), 'y': Unknown('y', coefficient=2) }
- copy() VariableGroup[source]#
Returns a shallow copy of the group
- classmethod is_roughly_similar(y: VariableGroup) bool[source]#
Checks if 2 variable groups are roughly similar, meaning they can broadly interact. This interaction doesn’t allow for adding and subtracting.
>>> x, y, z = Variable.many('x', 'y', 'z') >>> VariableGroup.is_roughly_similar(x * y, x * y) True >>> VariableGroup.is_roughly_similar(x * x * y, x * y) True >>> VariableGroup.is_roughly_similar(x * y, x * z) False
- Parameters:
x (
VariableGroup) – 2 Groups to be compared.y (
VariableGroup) – 2 Groups to be compared.
- static is_similar(x: VariableGroup, y: VariableGroup) bool[source]#
- Checks whether 2 variable groups are similar, meaning they can interact with each other.
This interaction includes adding, subtracting, division and more.
>>> x, y, z = Variable.many('x', 'y', 'z') >>> VariableGroup.is_similar(x * y, x * z) False >>> VariableGroup.is_similar(x * y, x * y) True >>> VariableGroup.is_similar(x * x * y, x * y) False
- Parameters:
x (
VariableGroup) – 2 Groups to be compared.y (
VariableGroup) – 2 Groups to be compared.
- property representation: str#
Returns how the group is represented as, without the group coefficient.
- solve(**values) Variable | BasicExpression | U[source]#
Generates a value for the group using inputted values.
>>> g = x * y >>> g.solve(x=2, y=2) 4 >>> g.solve(x=2) 2y >>> g.solve() xy