def __init__(self, cop, plus_operation): ''' cop: COP_Instance, Constraint Optimization Problem plus_operation: Sum operator report_path: location where saving the MaxSum report ''' #self.reportpath = reportpath self.cop = cop self.mfactory = MessageFactory() self.ps = MailMan(self.mfactory) self.sum = Sum(self.mfactory) if plus_operation == 'max': self.op = Max(self.mfactory) self.latestValue_start = float('-Infinity') elif plus_operation == 'min': self.op = Min(self.mfactory) self.latestValue_start = float('+Infinity') ''' create MaxSumOperator: Max - Sum ''' self.ms = MSumOperator(self.sum, self.op) self.values = list()
def __init__(self, cop, plus_operation, run): ''' cop: COP_Instance, Constraint Optimization Problem plus_operation: Sum operator reportpath: location where saving the MaxSum report ''' self.reportpath = "MaxSumReport_RUN_" + str(run) + ".txt" self.cop = cop self.mfactory = MessageFactory() self.ps = MailMan(self.mfactory) self.sum = Sum(self.mfactory) if plus_operation == 'max': self.op = Max(self.mfactory) self.latestValue_start = float('-inf') elif plus_operation == 'min': self.op = Min(self.mfactory) self.latestValue_start = float('+inf') ''' create MaxSumOperator: Max - Sum ''' self.ms = MSumOperator(self.sum, self.op) self.values = list() self.lastAverage = -1 self.count = 0 self.howMany = ''
def testSumPlusMoney(self): fiveBucks = Money.dollar(5) tenFrancs = Money.franc(10) bank = Bank() bank.addRate("CHF", "USD", 2) sum = Sum(fiveBucks, tenFrancs).plus(fiveBucks) result = bank.reduce(sum, "USD") self.assertEqual(Money.dollar(15), result)
def testSumTimes(self): fiveBucks = Money.dollar(5) tenFrancs = Money.franc(10) bank = Bank() bank.addRate("CHF", "USD", 2) sum = Sum(fiveBucks, tenFrancs).times(2) result = bank.reduce(sum, "USD") self.assertEqual(Money.dollar(20), result)
def Integral(func, low, high, dx): if not callable(func): print(str(deal), "不可调用.") return None if high < low: print(high, "<", low) return None rst = Sum(lambda x: func(x + dx / 2), low, lambda x: x + dx, high) * dx return rst
def test_k(ks): """ Runs the test to compare series which are aborted after different count of addends. :param ks: The k's to use for the maximum 10^k. :return: Nothing. """ ks.sort() print("Test about different k's started...") print() print("The sums will be calculated for i=1 to i=10^k") print("k = {0}".format(ks)) print() lst = [] last_sum = None diffs = [] s = Sum(lambda x: Decimal(1) / Decimal(str(x))) s.start_value = 1 for k in ks: s.end_value = 10**k res = s.calculate() if last_sum is not None: diff = res - last_sum print(" + {0} = ...".format(diff)) diffs.append(diff) last_sum = res print(" k = {0:<2} | result = {1}".format(k, res)) lst.append(res) diff = max(lst) - min(lst) avg = average(diffs) print("Maximum difference between the sums is {0}".format(diff)) print("The average difference between the results is {0}".format(avg)) print() print("Test about different k's finished")
def testReduceSum(self): s = Sum(Money.dollar(3), Money.dollar(4)) bank = Bank() result = bank.reduce(s, "USD") self.assertEqual(Money.dollar(7), result)
#!/usr/bin/python from Operation import Operation from Sum import Sum Ernesto = Sum(4, 2) Ernesto.calculateOperation() Ernesto.displayResult()
def test(self): s = Sum(lambda x: x*x) s.start_value = 2 s.end_value = 4 self.assertEqual(s.calculate(), 2*2 + 3*3 + 4*4) self.assertTrue(2 in s._cache) self.assertEqual(len(s._cache), 1) self.assertTrue(4 in s._cache[2]) self.assertEqual(len(s._cache[2]), 1) self.assertEqual(s._cache[2][4], 2*2 + 3*3 + 4*4) s.end_value = 6 self.assertEqual(s.calculate(), 2*2 + 3*3 + 4*4 + 5*5 + 6*6) self.assertTrue(2 in s._cache) self.assertEqual(len(s._cache), 1) self.assertTrue(4 in s._cache[2]) self.assertTrue(6 in s._cache[2]) self.assertEqual(len(s._cache[2]), 2) self.assertEqual(s._cache[2][6], 2*2 + 3*3 + 4*4 + 5*5 + 6*6) s.end_value = 5 self.assertEqual(s.calculate(), 2*2 + 3*3 + 4*4 + 5*5) self.assertTrue(2 in s._cache) self.assertEqual(len(s._cache), 1) self.assertTrue(4 in s._cache[2]) self.assertTrue(5 in s._cache[2]) self.assertTrue(6 in s._cache[2]) self.assertEqual(len(s._cache[2]), 3) self.assertEqual(s._cache[2][5], 2*2 + 3*3 + 4*4 + 5*5) s.start_value = 3 s.end_value = 4 self.assertEqual(s.calculate(), 3*3 + 4*4) self.assertTrue(2 in s._cache) self.assertTrue(3 in s._cache) self.assertEqual(len(s._cache), 2) self.assertTrue(4 in s._cache[3]) self.assertEqual(len(s._cache[3]), 1) self.assertEqual(s._cache[3][4], 3*3 + 4*4)
#!/usr/bin/python "Calculadora basica" "Importar la clase Sum del archivo Sum.py" from Sum import Sum from Multiplication import Multiplication from Subtract import Subtract from Division import Division print("Ingrese un numero:") numeroUno = input() print("Ingrese otro numero:") numeroDos = input() sumar = Sum() print sumar.calculateSum(numeroUno, numeroDos) restar = Subtract() print restar.calculateSubtract(numeroUno, numeroDos) dividir = Division() print dividir.calculateDivision(numeroUno, numeroDos) multiplicar = Multiplication() print multiplicar.calculateMultiplication(numeroUno, numeroDos)
def test_getSum_emptylist(self): testList = Sum([]) self.assertEqual(testList.getSum(), sum([]))
def plus(self, addend): return Sum(self, addend)
def test_getSum(self): List = [1, 2, 3, 4, 5, 6, 7] testList = Sum([1, 2, 3, 4, 5, 6, 7]) self.assertEqual(testList.getSum(), sum(List))
def test_getSum_None(self): testList = Sum(None) self.assertIsNone(testList.getSum(), None)
def test_getSum_multiElist(self): testList = Sum([1, 2, 3, 4.5, 7.7]) self.assertEqual(testList.getSum(), sum([1, 2, 3, 4.5, 7.7]))
def test_getSum_oneElist(self): testList = Sum([43]) self.assertEqual(testList.getSum(), sum([43]))
''' THIS PROGRAM IS A SIMPLE TEST TO LEARN HOW MODULE WORKS. THERE ARE 5 .py FILES THAT ARE CREATED IN "MathModules_1" FOLDER NAMELY : Square.py, Cube.py, Sum.py, Multiply.py, SquareRoot.py WE ARE IMPORTING FUNCTIONS FROM EACH OF THE FILE INDIVIDUALLY AND THEN PERFORMING THE CALCULATIONS. ''' from Sum import Sum from Cube import Cube from Multiply import Multiply from SquareRoot import SquareRoot from Square import Square res = Sum(10, 20, 30, 40, 50) print('Sqaure of Numbers are : ', Square(10, 20, 30, 40, 50)) print('Sqaure Root of Numbers are : ', SquareRoot(10, 20, 30, 40, 50)) print('Cube of Numbers are : ', Cube(10, 20, 30, 40, 50)) print('Product of Numbers are : ', Multiply(10, 20, 30, 40, 50))
def test_fx(): s = Sum(lambda x: Decimal(1) / Decimal(str(x))) s.start_value = 1 s.end_value = 10**k return s.calculate()