diff --git a/source/calculus/exercises/outcomes/TI/TI3/generator.sage b/source/calculus/exercises/outcomes/TI/TI3/generator.sage index e2afa39d5..c77ab7dc3 100644 --- a/source/calculus/exercises/outcomes/TI/TI3/generator.sage +++ b/source/calculus/exercises/outcomes/TI/TI3/generator.sage @@ -1,20 +1,4 @@ -#Functions to display powers of trig functions -def print_cosp(self,*args): return f"\\cos ^{{{args[1]}}}({latex(args[0])})" -def deriv_cosp(self,*args,**kwds): - if args[1]==1: - return -1*sinp(args[0],1)*args[0].derivative(args[kwds['diff_param']]) - else: - return args[1]*-1*cosp(args[0],args[1]-1)*sinp(args[0],1)*args[0].derivative(args[kwds['diff_param']]) - -cosp = function("cosp",nargs=2,print_latex_func=print_cosp,derivative_func=deriv_cosp) -def print_sinp(self,*args): return f"\\sin ^{{{args[1]}}}({latex(args[0])})" -def deriv_sinp(self,*args,**kwds): - if args[1]==1: - return cosp(args[0],1)*args[0].derivative(args[kwds['diff_param']]) - else: - return args[1]*sinp(args[0],args[1]-1)*cosp(args[0],1)*args[0].derivative(args[kwds['diff_param']]) - -sinp = function("sinp",nargs=2,print_latex_func=print_sinp, derivative_func=deriv_sinp) +load("../../../source/common/sagemath/library.sage") class Generator(BaseGenerator): @@ -30,12 +14,12 @@ class Generator(BaseGenerator): hint=f"(1-z)^{m}={latex(sum(binomial(m,k)*(-z)^k,k,0,m))}" - trigs=[sinp, cosp] + trigs=[sin, cos] shuffle(trigs) a = randint(1,6) - f = a*trigs[0](x,2*n)*trigs[1](x,2*m+1) - F = sum(a*binomial(m,k)*(-1)^k*1/(2*n+2*k+1)*trigs[0](x,2*n+2*k+1)*trigs[0](x,1).derivative(x)/trigs[1](x,1),k,0,m) + f = a*trigs[0](x)^(2*n)*trigs[1](x)^(2*m+1) + F = sum(a*binomial(m,k)*(-1)^k*1/(2*n+2*k+1)*trigs[0](x)^(2*n+2*k+1)*trigs[0](x).derivative(x)/trigs[1](x),k,0,m) # Task 2, integral with even powers @@ -44,14 +28,14 @@ class Generator(BaseGenerator): m = randrange(2,5) n = randrange(2,5) k = 2^randrange(2,5) - g = k*cosp(a*x,2*m)*sinp(a*x,2*n) + g = k*cos(a*x)^(2*m)*sin(a*x)^(2*n) also_g = k/2^(m+n)*(1+cos(2*a*x))^m*(1-cos(2*a*x))^n return { - "f": f, - "F": F, - "g": g, - "also_g": also_g, + "f": TBIL.typeset_trigpowers(f), + "F": TBIL.typeset_trigpowers(F), + "g": TBIL.typeset_trigpowers(g), + "also_g": TBIL.typeset_trigpowers(also_g), "hint": hint, } diff --git a/source/common/sagemath/library.sage b/source/common/sagemath/library.sage index 68e65d0f4..2aa4e192a 100644 --- a/source/common/sagemath/library.sage +++ b/source/common/sagemath/library.sage @@ -369,6 +369,112 @@ class TBIL: '''Returns the equation of a line from a point and slope''' return y==slope*x+point[1]-slope*point[0] + #Functions to display powers of trig functions + def print_cosp(self,*args): + if args[1]==1: + return f"\\cos({latex(args[0])})" + else: + return f"\\cos ^{{{args[1]}}}({latex(args[0])})" + + def deriv_cosp(self,*args,**kwds): + if args[1]==1: + return -1*TBIL.sinp(args[0],1)*args[0].derivative(args[kwds['diff_param']]) + else: + return args[1]*-1*TBIL.cosp(args[0],args[1]-1)*TBIL.sinp(args[0],1)*args[0].derivative(args[kwds['diff_param']]) + + def power_cosp(self,x,n,power_param): + return cosp(x,n*power_param) + + cosp = function("cosp",nargs=2,print_latex_func=print_cosp,derivative_func=deriv_cosp,power_func=power_cosp) + + def print_sinp(self,*args): + if args[1]==1: + return f"\\sin({latex(args[0])})" + else: + return f"\\sin ^{{{args[1]}}}({latex(args[0])})" + + def deriv_sinp(self,*args,**kwds): + if args[1]==1: + return TBIL.cosp(args[0],1)*args[0].derivative(args[kwds['diff_param']]) + else: + return args[1]*TBIL.sinp(args[0],args[1]-1)*TBIL.cosp(args[0],1)*args[0].derivative(args[kwds['diff_param']]) + + def power_sinp(self,x,n,power_param): + return sinp(x,n*power_param) + + sinp = function("sinp",nargs=2,print_latex_func=print_sinp, derivative_func=deriv_sinp,power_func=power_sinp) + + def print_tanp(self,*args): + if args[1]==1: + return f"\\tan({latex(args[0])})" + else: + return f"\\tan ^{{{args[1]}}}({latex(args[0])})" + + def deriv_tanp(self,*args,**kwds): + if args[1]==1: + return TBIL.secp(args[0],2)*args[0].derivative(args[kwds['diff_param']]) + else: + return args[1]*TBIL.tanp(args[0],args[1]-1)*TBIL.secp(args[0],2)*args[0].derivative(args[kwds['diff_param']]) + + def power_tanp(self,x,n,power_param): + return tanp(x,n*power_param) + + tanp = function("tanp",nargs=2,print_latex_func=print_tanp, derivative_func=deriv_tanp,power_func=power_tanp) + + def print_cotp(self,*args): + if args[1]==1: + return f"\\cot({latex(args[0])})" + else: + return f"\\cot ^{{{args[1]}}}({latex(args[0])})" + + def deriv_cotp(self,*args,**kwds): + if args[1]==1: + return -1*TBIL.cscp(args[0],2)*args[0].derivative(args[kwds['diff_param']]) + else: + return -1*args[1]*TBIL.cotp(args[0],args[1]-1)*TBIL.cscp(args[0],2)*args[0].derivative(args[kwds['diff_param']]) + + def power_cotp(self,x,n,power_param): + return cotp(x,n*power_param) + + cotp = function("cotp",nargs=2,print_latex_func=print_cotp, derivative_func=deriv_cotp,power_func=power_cotp) + + def print_secp(self,*args): + if args[1]==1: + return f"\\sec({latex(args[0])})" + else: + return f"\\sec ^{{{args[1]}}}({latex(args[0])})" + + def deriv_secp(self,*args,**kwds): + if args[1]==1: + return TBIL.secp(args[0],1)*TBIL.tanp(args[0],1)*args[0].derivative(args[kwds['diff_param']]) + else: + return args[1]*TBIL.secp(args[0],args[1])*TBIL.tanp(args[0],1)*args[0].derivative(args[kwds['diff_param']]) + + def power_secp(self,x,n,power_param): + return secp(x,n*power_param) + + secp = function("secp",nargs=2,print_latex_func=print_secp, derivative_func=deriv_secp,power_func=power_secp) + + def print_cscp(self,*args): + if args[1]==1: + return f"\\csc({latex(args[0])})" + else: + return f"\\csc ^{{{args[1]}}}({latex(args[0])})" + + def deriv_cscp(self,*args,**kwds): + if args[1]==1: + return -1*TBIL.csc(args[0],1)*TBIL.cotp(args[0],1)*args[0].derivative(args[kwds['diff_param']]) + else: + return -1*args[1]*TBIL.cscp(args[0],args[1])*TBIL.cotp(args[0],1)*args[0].derivative(args[kwds['diff_param']]) + + def power_cscp(self,x,n,power_param): + return cscp(x,n*power_param) + + cscp = function("cscp",nargs=2,print_latex_func=print_cscp, derivative_func=deriv_cscp,power_func=power_cscp) + + def typeset_trigpowers(f): + return f.substitute_function(sin(x)==TBIL.sinp(x,1)).substitute_function(cos(x)==TBIL.cosp(x,1)).substitute_function(tan(x)==TBIL.tanp(x,1)).substitute_function(sec(x)==TBIL.secp(x,1)).substitute_function(csc(x)==TBIL.cscp(x,1)) + # Linear Algebra @staticmethod diff --git a/source/precalculus/exercises/outcomes/TE/TE2/generator.sage b/source/precalculus/exercises/outcomes/TE/TE2/generator.sage index 268a9d85f..5b670b672 100644 --- a/source/precalculus/exercises/outcomes/TE/TE2/generator.sage +++ b/source/precalculus/exercises/outcomes/TE/TE2/generator.sage @@ -14,8 +14,8 @@ class Generator(BaseGenerator): shuffle(identity2) return { - "expression1A": identity1[0], - "expression1B": identity1[1], - "expression2A": identity2[0], - "expression2B": identity2[1], + "expression1A": TBIL.typeset_trigpowers(identity1[0]), + "expression1B": TBIL.typeset_trigpowers(identity1[1]), + "expression2A": TBIL.typeset_trigpowers(identity2[0]), + "expression2B": TBIL.typeset_trigpowers(identity2[1]), } \ No newline at end of file diff --git a/source/precalculus/exercises/outcomes/TE/TE3/generator.sage b/source/precalculus/exercises/outcomes/TE/TE3/generator.sage index 202dada0f..cbdd63be4 100644 --- a/source/precalculus/exercises/outcomes/TE/TE3/generator.sage +++ b/source/precalculus/exercises/outcomes/TE/TE3/generator.sage @@ -33,7 +33,7 @@ class Generator(BaseGenerator): return { "equation1": eq, "solutions1": solution_string, - "equation2": eq2, + "equation2": TBIL.typeset_trigpowers(eq2), "solutions2": solution_string2, "equation3": eq3, "solutions3": solution_string3,