def doMath(op, op1, op2): if op == "*": return Mathlibrary.mul(op1, op2) elif op == "/": return Mathlibrary.div(op1, op2) elif op == "+": return Mathlibrary.add(op1, op2) elif op == "√": return Mathlibrary.root(op2, op1) elif op == "^": return Mathlibrary.pow(op1, op2) else: return Mathlibrary.sub(op1, op2)
inputNumbers = inputNumbers.replace('\n', ' ') #Count the numbers for x in inputNumbers.split(): N += 1 #Profiling on one single string if oneString: subSum = createSubSum(inputNumbers) nextSum = createSum(inputNumbers) evalString = "2 √ ( ( 1 / ( {} - 1 ) ) * ( {} - {} ) )".format( N, nextSum, subSum) with PyCallGraph(output=GraphvizOutput()): print(resolve(evalString)) #Profiling using side calculations else: firstSum = 0 secondSum = 0 with PyCallGraph(output=GraphvizOutput()): #Calculate SUM of numbers and SUM of numbers^2 for x in inputNumbers.split(): firstSum = Mathlibrary.add(firstSum, float(x)) #sum of Xi secondSum = Mathlibrary.add(secondSum, Mathlibrary.pow(float(x), 2)) #N*(x')^2 firstSum = Mathlibrary.pow(Mathlibrary.div(firstSum, N), 2) firstSum = Mathlibrary.mul(firstSum, N) finalSum = Mathlibrary.sub(secondSum, firstSum) #final formula print(resolve("2 √ ( 1 / ( {} - 1 ) * {} )".format(N, finalSum)))
class MathLibraryTestMul(unittest.TestCase): def setUp(self): self.mathlib = Mathlibrary() def testMulPositive(self): self.assertEqual(self.mathlib.mul(5, 8), 40) self.assertEqual(self.mathlib.mul(19247, 0), 0) self.assertEqual(self.mathlib.mul(0, 701134), 0) self.assertEqual(self.mathlib.mul(12442, 823), 10239766) def testMulNegative(self): self.assertEqual(self.mathlib.mul(-5, 8), -40) self.assertEqual(self.mathlib.mul(-5, -8), 40) self.assertEqual(self.mathlib.mul(-9313, 7053170), -65686172210) self.assertEqual(self.mathlib.mul(0, -701134), 0) def testMulFloat(self): self.assertAlmostEqual(self.mathlib.mul(1.5132, 8.85130), 13.39378716) self.assertAlmostEqual(self.mathlib.mul(5.841021, 0.843103), 4.9245823281) self.assertAlmostEqual(self.mathlib.mul(-34.4104921, 913.83191), -31445.405719782911) self.assertAlmostEqual(self.mathlib.mul(-794.7415124, -0.05923), 47.072539779452)