Skip to content

Display powers of trig functions in usual way #496

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Dec 18, 2024
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
50 changes: 34 additions & 16 deletions source/calculus/exercises/outcomes/TI/TI3/generator.sage
Original file line number Diff line number Diff line change
@@ -1,32 +1,50 @@
#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)

class Generator(BaseGenerator):

def data(self):
# integral with odd power
# Task 1, integral with odd power
x=var("x")

even=2*randint(1,6)

odd=choice([5,7])
n=randint(1,6)
m=randint(2,3)

z = var("z")
if odd==5:
hint = "hint_2"
else:
hint = "hint_3"
k=var('k')
z=var('z')
hint=f"(1-z)^{m}={latex(sum(binomial(m,k)*(-z)^k,k,0,m))}"

trigs=[sin, cos]
shuffle(trigs)

f=randint(1,5)*(trigs[0](x))^even*(trigs[1](x))^odd
trigs=[sinp, cosp]
shuffle(trigs)

F=f.integral(x)
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)


# integral with even powers
# Task 2, integral with even powers

a = randrange(2,6)
m = randrange(2,5)
n = randrange(2,5)
k = 2^randrange(2,5)
g = k*cos(a*x)^(2*m)*sin(a*x)^(2*n)
g = k*cosp(a*x,2*m)*sinp(a*x,2*n)
also_g = k/2^(m+n)*(1+cos(2*a*x))^m*(1-cos(2*a*x))^n
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
also_g = k/2^(m+n)*(1+cos(2*a*x))^m*(1-cos(2*a*x))^n
also_g = k/2^(m+n)*(1+cos(2*a*x))^m*(1-cos(2*a*x))^n
assert f.diff() == also_g

Or something like that to ensure no errors were made? Or something that's more clearly the same as g?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I don't think that will work, the way cosp is created sage really doesn't know that it's a power of cos. We lose a lot of that kind of thing by creating our own function.

Copy link
Contributor

Choose a reason for hiding this comment

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

Hmm, it might be quicker to demo than explain what I'm trying to say, one sec

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Maybe there is a way to use assuming to do something. Maybe that's a separate issue t hough to go back and add checks like these wherever we are doing something by hand (across exercises)

Copy link
Contributor

Choose a reason for hiding this comment

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

It was not quicker, so I think this is okay to use as-is.



Expand All @@ -35,5 +53,5 @@ class Generator(BaseGenerator):
"F": F,
"g": g,
"also_g": also_g,
hint: True,
"hint": hint,
}
10 changes: 2 additions & 8 deletions source/calculus/exercises/outcomes/TI/TI3/template.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,8 @@
<knowl>
<content>
<p>
Use trigonometric identities and
{{#hint_2}}
<m>(1-z)^2=1-2z+z^2</m>
{{/hint_2}}
{{#hint_3}}
<m>(1-z)^3=1-3z+3z^2-z^3</m>
{{/hint_3}}
to explain and demonstrate how to find
Using trigonometric identities and the fact that <m>{{hint}}</m>,
explain and demonstrate how to find
<m>\displaystyle \int {{f}} dx</m>.
</p>
</content>
Expand Down
Loading