This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import math | |
from functools import reduce | |
class Calculator: | |
def __init__(self): | |
pass | |
def __str__(self): | |
return "Basic calculator" | |
def add(self,*args): | |
''' | |
*args - list of arguments | |
returns the sum of items in the list | |
''' | |
return sum(list(*args)) | |
def sub(self, x, y): | |
sub = x - y | |
return sub | |
def div(self, x, y): | |
return x / y | |
def mult_math_lib(self, *args): | |
return math.prod(*args) | |
def mult_norm(self, *args): | |
result = 1 | |
for x in list(*args): | |
result = result * x | |
return result | |
def mult_using_lambda(self, *args): | |
return reduce((lambda x, y: x * y), *args) | |
class SciComp(Calculator): | |
def __init__(self): | |
Polygon.__init__(self,3) | |
def __init__(self): | |
Calculator.__init__(self) | |
def first_order(self, y, t, po): | |
''' | |
this might not be the ideal way to implement this tho, lols | |
formula : F(t,y,y˙)=y˙−t2−1 | |
po - raised to the power | |
''' | |
array = [t] * po | |
power_of_t = self.mult_norm(array) | |
print("POWER", power_of_t) | |
return y - power_of_t - 1 | |
if __name__ == "__main__": | |
calc = Calculator() | |
## test add method | |
list_of_numbers = [1,2,3,4] | |
print("########### ADDITION ############") | |
print("Sum of",','.join(str(i) for i in list_of_numbers), 'is',calc.add(list_of_numbers)) | |
print("######### SUBSTRACTION ##########") | |
num1 = 10 | |
num2 = 5 | |
subtract = num1 - num2 | |
print(f"{num1} - {num2} = {subtract}") | |
print("######### SUBSTRACTION ##########") | |
print("####### multiplication using math library #########") | |
list_of_numbers2 = [1,2,3,4,5] | |
print("Multiplication of",','.join(str(i) for i in list_of_numbers2), 'is',calc.mult_math_lib(list_of_numbers2)) | |
print("####### multiplication using math library #########") | |
print("##### normal multiplication #####") | |
list_of_numbers3 = [1,2,3,4,5, 6] | |
print("Multiplication of",','.join(str(i) for i in list_of_numbers3), 'is',calc.mult_norm(list_of_numbers3)) | |
print("##### normal multiplication #####") | |
# mult_norm | |
print("######## multiplication with lambda ##########") | |
list_of_numbers4 = [1,2,3,4,5,6] | |
print("Multiplication of",','.join(str(i) for i in list_of_numbers4), 'is',calc.mult_using_lambda(list_of_numbers4)) | |
print("######## multiplication with lambda ##########") | |
print("##### scientific computing ########") | |
sci = SciComp() | |
y = 16 | |
t = 2 | |
print(sci.first_order(y, t, 2)) | |
print("##### scientific computing ########") | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment