Added differentiation generator

This commit is contained in:
strikeraryu
2020-10-20 10:59:10 +05:30
parent 82a0e1433a
commit d73c4c5678
2 changed files with 54 additions and 0 deletions

View File

@@ -90,3 +90,4 @@ from .decimalToOctalFunc import *
from .decimalToRomanNumeralsFunc import * from .decimalToRomanNumeralsFunc import *
from .degreeToRadFunc import * from .degreeToRadFunc import *
from .radianToDegFunc import * from .radianToDegFunc import *
from .differentiationFunc import *

View File

@@ -0,0 +1,53 @@
from .__init__ import *
def genDifferentiationProblem(diff_lvl):
problem = ''
types = {
'Logrithmic': ['ln'],
'Trigonometric': ['sin', 'cos', 'tan', 'cot', 'sec'],
'Exponentional': ['exp']
}
if diff_lvl == 1:
coeff = random.randrange(2, 10)
power = random.randint(2, 4)
flag = random.random()
if flag > 0.5:
power *= -1
problem += str(coeff) + '*x^' + '(' + str(power) + ')'
else:
problem += str(coeff) + '*x^' + str(power)
if diff_lvl == 2:
func_type = random.choices(list(types.keys()), weights=(1, 4, 1))[0]
func = random.choice(types[func_type])
problem += func + '(x)' + '+' + genDifferentiationProblem(1)
if diff_lvl == 3:
func_type = random.choices(list(types.keys()), weights=(1, 4, 1))[0]
func = random.choice(types[func_type])
problem += func + '(' + genDifferentiationProblem(1) + ')'
if diff_lvl == 4:
operator = random.choice(('/', '*'))
problem = '(' + genDifferentiationProblem(2) + ')' + \
operator + '(' + genDifferentiationProblem(3) + ')'
return problem
def differentiationFunc(diff_lvl=2):
if diff_lvl < 1 or diff_lvl > 4:
print("diff_lvl not supported")
return None
problem = genDifferentiationProblem(diff_lvl)
x = sympy.symbols('x')
solution = str(sympy.diff(problem.replace('^', '**'), x))
solution = solution.replace('**', '^')
problem = f"differentiate w.r.t x : d({problem})/dx"
return problem, solution
differentiation = Generator(
"Differentiation", 88, "differentiate w.r.t x : d(f(x))/dx", "g(x)", differentiationFunc)