SMBL โ is simple package written on Python for symbolic calculations
Var()
โ You can create variables and use it in ExpressionsDomain()
โ You can create some rule for variablesExpression()
โ You can create Expression from Constants(int, float, complex) or from variables, using operation to operate by itOperation()
โ You can create operation to operate by variablesโ TODOFunction()
Imports what you need to work with SMBL
from smbl import Var # For create new variables
from smbl.domain import * # For use default domains
Imports if you want to expand functionality of some classes or want to create your own based on exist class to use it for your special calculations
from smbl.domain import Domain # For implement you own domain class
from smbl.operation import Operation # For implement you own operation
# from smbl.function import Function # For Implement you own Function
Implementations example:
This section demonstrates how to create and use variables
>>> Var(some_name, value=some_value, domain=some_domain)
>>> Var.some_name # you can acces to variable without save it to python variable
Var(name="some_name", value="some_value", domain=some_domain)
>>> Var.some_name.value = some_new_value
This section demonstrates how to create Expressions from variables
NOTE: You can also use other Expressions for variables values
>>> Var("x")
Var("x", value=None, domain=DefaultDomain)
>>> Var("y")
Var("y", value=None, domain=DefaultDomain)
>>> e = Var.x + Var.y
>>> e
Expression(operation="+", operands=[
Var("x", value=None, domain=DefaultDomain),
Var("y", value=None, domain=DefaultDomain),
])
>>> str(e)
'(x + y)'
This section demonstrates how to calculate expression, with given variables values
NOTE: You can also use other Expressions for variables values
>>> Var("x")
Var("x", value=None, domain=DefaultDomain)
>>> Var("y")
Var("y", value=None, domain=DefaultDomain)
>>> e = Var.x + Var.y
>>> e(x=4, y=5)
9
>>> e2 = e(y=5)
>>> str(e2)
'(x + 5)'
This section demonstrates how use domains
NOTE: Domain it is something like rule for you variables, for examples you can create variable the values of which can only be prime numbers
This section demonstrates how to check value in Domain
some_value in TestDomain() # return True if some_value in TestDomain else False
If you want to create your own domain you should create a class that inherits from Domain
and override the __in_domain__
method:
from smbl.domain import Domain
class OwnDomain(Domain):
def __in_domain__(self, value: any) -> bool:
"""
Write code to check value in domain
"""
pass
class DefaultDomain(Domain):
"""
Default domain which alway return True
"""
def __in_domain__(self, param: any) -> bool:
return True
class EvenDomain(Domain):
"""
Domain for even numbers
"""
def __in_domain__(self, value: int) -> bool:
return value % 2 == 0
class OddDomain(Domain):
"""
Domain for odd numbers
"""
def __in_domain__(self, value: int) -> bool:
return not value in EvenDomain()
class IntegerDomain(Domain):
"""
Domain for integer numbers
..., -3, -2, -1, 0, 1, 2, 3, ...
"""
def __in_domain__(self, value: int) -> bool:
return isinstance(value, int)
class NaturalDomain(Domain):
"""
Domain for natural numbers
0, 1, 2, 3, 4, 5, 6, 7, 8, ...
"""
def __in_domain__(self, value: int) -> bool:
return value in IntegerDomain() and value >= 0
class PrimeDomain(Domain):
"""
Domain for only positive prime numbers
2, 3, 5, 7, 11, 13, 17, ...
"""
def __is_prime__(self, value: int) -> bool:
"""
Check natural number is prime
"""
if value < 2:
return False
if value == 2:
return True
if value in EvenDomain():
return False
i = 3
while i*i <= value:
if value % i:
return False
i += 2
return True
def __in_domain__(self, value: int) -> bool:
return value in NaturalDomain() and self.__is_prime__(value)
class IntegerPrimeDomain(Domain):
"""
Domain for positive and negative prime numbers
..., -17, -13, ..., -2, 2, 3, 5, 7, 11, 13, 17, ...
"""
def __in_domain__(self, value: int) -> bool:
return value in IntegerDomain() and abs(value) in PrimeDomain()
class RealDomain(Domain):
"""
Domain for real numbers
0.(3), sqrt(2)/2, 0, 1, -pi, e, ...
"""
def __in_domain__(self, value: float | int) -> bool:
return isinstance(value, float) or value in IntegerDomain()
class ComplexDomain(Domain):
"""
Domain for complex numbers
1+i, i, 0, 1, 14+8i, ...
"""
def __in_domain__(self, value: float | int | complex):
return value in RealDomain() or isinstance(value, complex)
This section demonstrates how to use operation
NOTE: Operators is essense what take some arguments and calculate some result
This section demonstrates how to calculate value using operation
res = TestOperator(val1, val2,..., valn)
You can create UnaryOperation
and BinaryOperation
:
from smbl.operation import BinaryOperation, NewUnaryOperation
NewBinaryOperation = BinaryOperation("symbol", callback)
NewUnaryOperation = UnaryOperation("symbol", callback)
Also, you can implement operation with custom variables count:
from smbl.operation import Operation
# One
ThreeValueOperation = Operation("symbol", callback, operand_count=3)
# Two
class ThreeValueOperation(Operation):
def __init__(self,
symbol: str,
operation: callable):
super().__init__(symbol, operation, operand_count=3)
Add = BinaryOperation("+", lambda a, b: a + b)
Sub = BinaryOperation("-", lambda a, b: a - b)
Mul = BinaryOperation("*", lambda a, b: a * b)
Div = BinaryOperation("/", lambda a, b: a / b)
TrueDiv = BinaryOperation("//", lambda a, b: a // b)
Mod = BinaryOperation("%", lambda a, b: a % b)
Pow = BinaryOperation("^", lambda a, b: a**b)