Skip to content
This repository was archived by the owner on Mar 29, 2023. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions Equation/equation_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,9 @@ def sumargs(*args):
addUnaryOp('-',"-{0:s}","-{0:s}",op.neg)
addFn('abs',"abs({0:s})","\\left|{0:s}\\right|",1,op.abs)
addFn('sum',"sum({0:s})","\\sum\\left({0:s}\\right)",'+',sumargs)
def last_of_list(args):
return args[-1]
addFn('last',"min({0:s})","\\text{last}\\left({0:s}\\right)",'+',last_of_list)
addFn('prod',"prod({0:s})","\\prod\\left({0:s}\\right)",'+',product)
if has_numpy:
addFn('floor',"floor({0:s})","\\lfloor {0:s} \\rfloor",1,np.floor)
Expand All @@ -83,6 +86,7 @@ def sumargs(*args):
addFn('re',"re({0:s})","\\Re\\left({0:s}\\right)",1,np.real)
addFn('im',"re({0:s})","\\Im\\left({0:s}\\right)",1,np.imag)
addFn('sqrt',"sqrt({0:s})","\\sqrt{{{0:s}}}",1,np.sqrt)
addFn('mean',"mean({0:s})","\\text{mean}\\left({0:s}\\right)",'+',np.mean)
addConst("pi",np.pi)
addConst("e",np.e)
addConst("Inf",np.Inf)
Expand All @@ -97,6 +101,9 @@ def sumargs(*args):
addFn('re',"re({0:s})","\\Re\\left({0:s}\\right)",1,complex.real)
addFn('im',"re({0:s})","\\Im\\left({0:s}\\right)",1,complex.imag)
addFn('sqrt',"sqrt({0:s})","\\sqrt{{{0:s}}}",1,math.sqrt)
def mean(args):
return float(sum(args)) / len(args)
addFn('mean',"mean({0:s})","\\text{mean}\\left({0:s}\\right)",'+',mean)
addConst("pi",math.pi)
addConst("e",math.e)
addConst("Inf",float("Inf"))
Expand Down
17 changes: 17 additions & 0 deletions tests/test_Equation.py
Original file line number Diff line number Diff line change
Expand Up @@ -457,6 +457,23 @@ def testCall(self):
def tearDown(self):
pass

class TestLastEquation(unittest.TestCase):
def setUp(self):
self.fn = Expression("last(x)")

def testCall(self):
self.assertEqual(self.fn([1]), 1)
self.assertEqual(self.fn([1,2,8,4,5]),5)

class TestMeanEquation(unittest.TestCase):
def setUp(self):
self.fn = Expression("mean(x)")

def testCall(self):
self.assertEqual(self.fn([42]), 42)
self.assertEqual(self.fn(42), 42)
self.assertEqual(self.fn([1,2,3,4,5]),3)

class TestEqualsEquation(unittest.TestCase):
def setUp(self):
self.fn = Expression("x == y")
Expand Down