Skip to content

Commit

Permalink
Merge 6a1ff06 into 3f87564
Browse files Browse the repository at this point in the history
  • Loading branch information
PulkitMishra committed Apr 21, 2019
2 parents 3f87564 + 6a1ff06 commit a21e7c3
Show file tree
Hide file tree
Showing 3 changed files with 283 additions and 1 deletion.
93 changes: 93 additions & 0 deletions tests/test_functions.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from visma.functions.constant import Constant
from visma.functions.variable import Variable
from visma.functions.structure import Expression


######################
Expand All @@ -19,6 +20,57 @@ def test_Constant():
assert isinstance(constant2, Variable)
assert constant2.__str__() == "5{x}"

constant1 = Constant(2)
constant2 = Constant(7)
constant3 = constant1 + constant2
assert constant3.__str__() == "{9}"

constant1 = Constant(2)
constant2 = Constant(7)
constant3 = constant1 - constant2
assert constant3.__str__() == "{-5}"

constant1 = Constant(5)
variable1 = Variable(5, 'x', 3)
summation = constant1 + variable1
assert summation.__str__() == "{({5}+5{x}^{3})}"

constant1 = Constant(5)
variable1 = Variable(5, 'x', 3)
summation = constant1 - variable1
assert summation.__str__() == "{({5}-5{x}^{3})}"

constant1 = Constant(5)
constant2 = Constant(5)
summation = constant1 * constant2
assert summation.__str__() == "{25}"

constant1 = Constant(5)
variable1 = Variable(5, 'x', 3)
summation = constant1 * variable1
assert summation.__str__() == "25{x}^{3}"

constant1 = Constant(5)
exp1 = constant1 + Variable(5, 'x', 3)
constant2 = Constant(10)
summation = constant2 * exp1
assert summation.__str__() == "{({50}+50{x}^{3})}"

constant1 = Constant(5)
constant2 = Constant(5)
summation = constant1 / constant2
assert summation.__str__() == "{1.0}"

constant1 = Constant(5)
variable1 = Variable(5, 'x', 3)
summation = constant1 / variable1
assert summation.__str__() == "{x}^{-3}"

constant1 = Constant(5)
exp1 = constant1 + Variable(5, 'x', 3)
constant2 = Constant(10)
summation = constant2 / exp1
assert summation.__str__() == "10.0*{({5}+5{x}^{3})}^{-1}"

######################
# functions.variable #
Expand All @@ -32,6 +84,47 @@ def test_Variable():
variable1.integrate('x')
assert variable1.__str__() == "0.5{x}^{4}"

constant = Constant(3)
variable = Variable(2, 'x', 3)
add = variable + constant
assert add.__str__() == "{(2{x}^{3}+{3})}"

variable1 = Variable(2, 'x', 3)
variable2 = Variable(4, 'x', 3)
variable3 = Variable(2, 'x', 4)
add1 = variable1 + variable2
add2 = variable1 + variable3
assert add1.__str__() == "6{x}^{3}"
assert add2.__str__() == "{(6{x}^{3}+2{x}^{4})}"

variable1 = Variable(2, 'x', 3)
constant = Constant(3)
exp1 = Expression([variable1, '+', constant])
variable2 = Variable(4, 'x', 3)
add2 = variable2 + exp1
assert add2.__str__() == "{(6{x}^{3}+{3})}"

constant = Constant(3)
variable = Variable(2, 'x', 3)
add = variable - constant
assert add.__str__() == "{(2{x}^{3}-{3})}"

variable1 = Variable(2, 'x', 3)
variable2 = Variable(4, 'x', 3)
variable3 = Variable(2, 'x', 4)
variable4 = Variable(2, 'x', 3)
add1 = variable1 - variable2
add2 = variable3 - variable4
assert add1.__str__() == "-2{x}^{3}"
assert add2.__str__() == "{(2{x}^{4}-2{x}^{3})}"

variable1 = Variable(2, 'x', 3)
constant = Constant(3)
exp1 = variable1 - constant
variable2 = Variable(4, 'x', 3)
add2 = variable2 - exp1
assert add2.__str__() == "{(2{x}^{3}-{-3})}"

# FIXME: Optimize integrate
'''
variable2 = Variable(3, 'x', -1)
Expand Down
113 changes: 112 additions & 1 deletion visma/functions/constant.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import math
from visma.functions.structure import Function
from visma.functions.structure import Function, Expression
from visma.functions.variable import Variable

############
Expand Down Expand Up @@ -41,6 +41,117 @@ def integrate(self, intwrt):
def calculate(self):
return self.coefficient * ((self.value**(self.power)))

def __radd__(self, other):
return self + other

def __add__(self, other):
from visma.functions.constant import Constant
if isinstance(other, Constant):
self = Constant(self.calculate() + other.calculate())
return self
elif isinstance(other, Expression):
expression = Expression()
expression.tokens = [self]
for i, token in enumerate(other.tokens):
if isinstance(token, Constant):
self = Constant(self.calculate() + other.calculate())
elif isinstance(token, Variable):
expression.tokens.extend(['+', Variable(token)])
expression.tokens[0] = self
self.type = 'Expression'
self = expression
return expression
elif isinstance(other, Variable):
expression = Expression()
expression.tokens = [self]
expression.tokens.extend(['+', other])
self.type = 'Expression'
self = expression
return expression

def __rsub__(self, other):
return self - other

def __sub__(self, other):
from visma.functions.constant import Constant
if isinstance(other, Constant):
self = Constant(self.calculate() - other.calculate())
return self
elif isinstance(other, Expression):
expression = Expression()
expression.tokens = [self]
for i, token in enumerate(other.tokens):
if isinstance(token, Constant):
self = Constant(self.calculate() - other.calculate())
elif isinstance(token, Variable):
expression.tokens.extend(['-', Variable(token)])
expression.tokens[0] = self
self.type = 'Expression'
self = expression
return expression
elif isinstance(other, Variable):
expression = Expression()
expression.tokens = [self]
expression.tokens.extend(['-', other])
self.type = 'Expression'
self = expression
return expression

def __rmul__(self, other):
return self - other

def __mul__(self, other):
from visma.functions.constant import Constant
if isinstance(other, Constant):
self = Constant(self.calculate() * other.calculate())
return self
elif isinstance(other, Expression):
expression = Expression()
for i, token in enumerate(other.tokens):
if isinstance(token, Constant):
expression.tokens.append(Constant(self.calculate() * token.calculate()))
elif isinstance(token, Variable):
variable = Variable()
variable.coefficient = self.calculate() * token.coefficient
variable.value.extend(token.value)
variable.power.extend(token.power)
expression.tokens.extend(['+', variable])
self.type = 'Expression'
self = expression
return expression
elif isinstance(other, Variable):
variable = Variable()
variable.coefficient = self.calculate() * other.coefficient
variable.value.extend(other.value)
variable.power.extend(other.power)
self.type = 'Variable'
self = variable
return variable

def __rtruediv__(self, other):
return self - other

def __truediv__(self, other):
from visma.functions.constant import Constant
if isinstance(other, Constant):
self = Constant(self.calculate() / other.calculate())
return self
elif isinstance(other, Expression):
expression = Expression(other.tokens)
expression.coefficient = self.calculate()/other.coefficient
expression.power = -1*other.power
self.type = 'Expression'
self = expression
return expression
elif isinstance(other, Variable):
variable = Variable(other)
variable.coefficient = self.calculate() / other.coefficient
variable.value.extend(other.value)
variable.power.extend([other.power[0]*-1])
self.type = 'Variable'
self = variable
return variable

def functionOf(self):
return []

Expand Down
78 changes: 78 additions & 0 deletions visma/functions/variable.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,3 +77,81 @@ def integrate(self, wrtVar):

def calculate(self, val):
return self.coefficient * ((val**(self.power)))

def __radd__(self, other):
return self + other

def __add__(self, other):
from visma.functions.constant import Constant
if isinstance(other, Constant):
expression = Expression()
expression.tokens = [self]
expression.tokens.extend(['+', other])
self.type = 'Expression'
self = expression
return expression
elif isinstance(other, Expression):
expression = Expression()
expression.tokens = [self]
for i, token in enumerate(other.tokens):
if isinstance(token, Variable):
if token.power == self.power:
self.coefficient += other.tokens[i].coefficient
else:
expression.tokens.extend(['+', Variable(token)])
elif isinstance(token, Constant):
expression.tokens.extend(['+', Constant(token.calculate()*1)])
expression.tokens[0] = self
self.type = 'Expression'
self = expression
return expression
elif isinstance(other, Variable):
if other.power == self.power:
self.coefficient += other.coefficient
return self
else:
expression = Expression()
expression.tokens = [self]
expression.tokens.extend(['+', other])
self.type = 'Expression'
self = expression
return expression

def __rsub__(self, other):
return self - other

def __sub__(self, other):
from visma.functions.constant import Constant
if isinstance(other, Constant):
expression = Expression()
expression.tokens = [self]
expression.tokens.extend(['-', other])
self.type = 'Expression'
self = expression
return expression
elif isinstance(other, Expression):
expression = Expression()
expression.tokens = [self]
for i, token in enumerate(other.tokens):
if isinstance(token, Variable):
if token.power == self.power:
self.coefficient -= other.tokens[i].coefficient
else:
expression.tokens.extend(['-', Variable(token)])
elif isinstance(token, Constant):
expression.tokens.extend(['-', Constant(token.calculate()*-1)])
expression.tokens[0] = self
self.type = 'Expression'
self = expression
return expression
elif isinstance(other, Variable):
if other.power == self.power:
self.coefficient -= other.coefficient
return self
else:
expression = Expression()
expression.tokens = [self]
expression.tokens.extend(['-', other])
self.type = 'Expression'
self = expression
return expression

0 comments on commit a21e7c3

Please sign in to comment.