From 389d4e523df404761262fd84ab73c5dcec539322 Mon Sep 17 00:00:00 2001 From: Drew Lewis Date: Fri, 28 Mar 2025 13:46:35 -0700 Subject: [PATCH 1/5] Factor sinp and cosp into library.sage --- .../exercises/outcomes/TI/TI3/generator.sage | 22 +++---------------- source/common/sagemath/library.sage | 21 ++++++++++++++++++ 2 files changed, 24 insertions(+), 19 deletions(-) diff --git a/source/calculus/exercises/outcomes/TI/TI3/generator.sage b/source/calculus/exercises/outcomes/TI/TI3/generator.sage index e2afa39d5..ca8ad962c 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,7 +14,7 @@ class Generator(BaseGenerator): hint=f"(1-z)^{m}={latex(sum(binomial(m,k)*(-z)^k,k,0,m))}" - trigs=[sinp, cosp] + trigs=[TBIL.sinp, TBIL.cosp] shuffle(trigs) a = randint(1,6) @@ -44,7 +28,7 @@ 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*TBIL.cosp(a*x,2*m)*TBIL.sinp(a*x,2*n) also_g = k/2^(m+n)*(1+cos(2*a*x))^m*(1-cos(2*a*x))^n diff --git a/source/common/sagemath/library.sage b/source/common/sagemath/library.sage index 08bd70c1a..d35b9fa54 100644 --- a/source/common/sagemath/library.sage +++ b/source/common/sagemath/library.sage @@ -334,6 +334,27 @@ 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): 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']]) + + 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 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']]) + + sinp = function("sinp",nargs=2,print_latex_func=print_sinp, derivative_func=deriv_sinp) + # Linear Algebra @staticmethod From e2ec345cdd1c015cd3fc9105d8ba44edf25498ae Mon Sep 17 00:00:00 2001 From: Drew Lewis Date: Tue, 24 Jun 2025 10:50:16 -0700 Subject: [PATCH 2/5] Typeset TE2 --- source/common/sagemath/library.sage | 93 ++++++++++++++++++- .../exercises/outcomes/TE/TE2/generator.sage | 8 +- 2 files changed, 93 insertions(+), 8 deletions(-) diff --git a/source/common/sagemath/library.sage b/source/common/sagemath/library.sage index 132608e49..4f1b4e15c 100644 --- a/source/common/sagemath/library.sage +++ b/source/common/sagemath/library.sage @@ -370,25 +370,110 @@ class TBIL: return y==slope*x+point[1]-slope*point[0] #Functions to display powers of trig functions - def print_cosp(self,*args): return f"\\cos ^{{{args[1]}}}({latex(args[0])})" + 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) + cosp = function("cosp",nargs=2,print_latex_func=print_cosp,derivative_func=deriv_cosp,power_func=power_cosp) - def print_sinp(self,*args): return f"\\sin ^{{{args[1]}}}({latex(args[0])})" + 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) - sinp = function("sinp",nargs=2,print_latex_func=print_sinp, derivative_func=deriv_sinp) + def trig_print(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 diff --git a/source/precalculus/exercises/outcomes/TE/TE2/generator.sage b/source/precalculus/exercises/outcomes/TE/TE2/generator.sage index 268a9d85f..a99d26859 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.trig_print(identity1[0]), + "expression1B": TBIL.trig_print(identity1[1]), + "expression2A": TBIL.trig_print(identity2[0]), + "expression2B": TBIL.trig_print(identity2[1]), } \ No newline at end of file From df83faf81abd00ac3fb15041de00f394862f13db Mon Sep 17 00:00:00 2001 From: Drew Lewis Date: Tue, 24 Jun 2025 10:52:33 -0700 Subject: [PATCH 3/5] Rename function --- source/common/sagemath/library.sage | 2 +- .../precalculus/exercises/outcomes/TE/TE2/generator.sage | 8 ++++---- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/source/common/sagemath/library.sage b/source/common/sagemath/library.sage index 4f1b4e15c..2aa4e192a 100644 --- a/source/common/sagemath/library.sage +++ b/source/common/sagemath/library.sage @@ -472,7 +472,7 @@ class TBIL: cscp = function("cscp",nargs=2,print_latex_func=print_cscp, derivative_func=deriv_cscp,power_func=power_cscp) - def trig_print(f): + 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 diff --git a/source/precalculus/exercises/outcomes/TE/TE2/generator.sage b/source/precalculus/exercises/outcomes/TE/TE2/generator.sage index a99d26859..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": TBIL.trig_print(identity1[0]), - "expression1B": TBIL.trig_print(identity1[1]), - "expression2A": TBIL.trig_print(identity2[0]), - "expression2B": TBIL.trig_print(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 From 0a9909858d192e3b1b34c55db05b58d9b7ddc8a7 Mon Sep 17 00:00:00 2001 From: Drew Lewis Date: Tue, 24 Jun 2025 10:53:13 -0700 Subject: [PATCH 4/5] Typest TE3 --- source/precalculus/exercises/outcomes/TE/TE3/generator.sage | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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, From b3e1142a703c8f752f8c6f76b8671fbb6d4f0b38 Mon Sep 17 00:00:00 2001 From: Drew Lewis Date: Tue, 24 Jun 2025 11:08:56 -0700 Subject: [PATCH 5/5] Refactor TI3 --- .../exercises/outcomes/TI/TI3/generator.sage | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/source/calculus/exercises/outcomes/TI/TI3/generator.sage b/source/calculus/exercises/outcomes/TI/TI3/generator.sage index ca8ad962c..c77ab7dc3 100644 --- a/source/calculus/exercises/outcomes/TI/TI3/generator.sage +++ b/source/calculus/exercises/outcomes/TI/TI3/generator.sage @@ -14,12 +14,12 @@ class Generator(BaseGenerator): hint=f"(1-z)^{m}={latex(sum(binomial(m,k)*(-z)^k,k,0,m))}" - trigs=[TBIL.sinp, TBIL.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 @@ -28,14 +28,14 @@ class Generator(BaseGenerator): m = randrange(2,5) n = randrange(2,5) k = 2^randrange(2,5) - g = k*TBIL.cosp(a*x,2*m)*TBIL.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, }