Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 9 additions & 25 deletions source/calculus/exercises/outcomes/TI/TI3/generator.sage
Original file line number Diff line number Diff line change
@@ -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):

Expand All @@ -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
Expand All @@ -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,
}
106 changes: 106 additions & 0 deletions source/common/sagemath/library.sage
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Comment on lines +475 to +476
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of using TBIL.sinp,etc. directly, this allows users to do whatever they want with built in trig functions, and then right before displaying call this on the expression they want to typeset. So users should hopefully never need the TBIL.sinp functions.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like the solution + implementation, but I'm hanging on the naming of variables a bit. E.g. I'd expect TBIL.typeset_trigpowers to return a string or LatexExpr. Since I think you have another PR depending on this, I'll do the renaming in a separate PR.


# Linear Algebra

@staticmethod
Expand Down
8 changes: 4 additions & 4 deletions source/precalculus/exercises/outcomes/TE/TE2/generator.sage
Original file line number Diff line number Diff line change
Expand Up @@ -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]),
}
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down