TeamBasedInquiryLearning / library

Monorepo for the TBIL library and project webpage
https://tbil.org
Other
3 stars 19 forks source link

Textbook versus Checkit notation Inconsistency for sin^n(x) #39

Open owensks opened 1 year ago

owensks commented 1 year ago

In TI3, we find integrals of the form \int \sin^4(x)\cos^3(x) \, dx but over in CheckIt the notation used is \int \sin (x)^4 \cos (x)^3 \, dx, e.g. compare Activity 5.3.3 https://teambasedinquirylearning.github.io/calculus/2023/TI3.html#activity-290 to https://teambasedinquirylearning.github.io/calculus/2023/exercises/#/bank/TI3/3/ . My preference would be for the textbook version of the notation, but I have strong feelings that the two platforms should align all notation whenever possible.

StevenClontz commented 1 year ago

This is a toughie - CheckIt problems are generated by SageMath, and it's very difficult to coerce SageMath to use a different presentation. We often use the hack \renewcommand{\log}{\ln} to get \log(x) to display as $\ln(x)$, but I'm not sure there's a workaround here without hardcoding all problems that involve trig products.

siwelwerd commented 2 months ago

Procrastinating on some other things this morning, had a half an idea to write a helper function that would handle this. Something along the lines of

def prettify_trig(expression):
    op=expression.operator()
    if op == operator.pow: 
        if expression.operands()[0].operator() in [sin, cos, tan, sec, csc, cot]:
            base, power = expression.operands()
            return f"{latex(base.operator())}^{{ {latex(power)} }}\\left({prettify_trig(base.operands()[0])}\\right)"
        else:
            return f"{{ {prettify_trig(expression.operands()[0])} }}^{{ {prettify_trig(expression.operands()[1])} }}"
    elif op in [operator.add, sage.symbolic.operators.add_vararg]:
        return " + ".join([prettify_trig(i) for i in expression.operands()])
    elif op == operator.sub:
        return " - ".join([prettify_trig(i) for i in expression.operands()])
    elif op == operator.mul or op==sage.symbolic.operators.mul_vararg:
        L=[prettify_trig(i) for i in expression.operands()]
        L.sort()
        return " ".join(L)
    #Not sure this ever gets used, division gets implemented with mul_varg and pow(,-1)
    elif op == operator.truediv:
        return f"\\dfrac{{ {prettify_trig(expression.operands()[0])} }}{{ {prettify_trig(expression.operands()[1])} }}"
    elif op in [sin, cos, tan, sec, csc, cot]:
        return f"\\{op} \\left( {expression.operands()[0]} \\right)"
    else:
        if expression.operands()==[]:
            return latex(expression)
        else:
            raise NotImplementedError(f"Operator {op} not implemented in prettify_trig()")

Sample:

print(prettify_trig(3*(sin(x))^2*(cos(x))^3))
print(prettify_trig(3*(tan(x))^5+(cos(x))^3))
print(prettify_trig(3*(csc(x))^8/(sec(x))^7))
print(prettify_trig(3*(tan(x))^5-(cos(x))^3))
3 \cos^{ 3 }\left(x\right) \sin^{ 2 }\left(x\right)
3 \tan^{ 5 }\left(x\right) + \cos^{ 3 }\left(x\right)
3 \csc^{ 8 }\left(x\right) \sec^{ -7 }\left(x\right)
3 \tan^{ 5 }\left(x\right) + -1 \cos^{ 3 }\left(x\right)

I doubt this is robust enough as is, but could be a direction to go.

StevenClontz commented 2 months ago

See also: https://github.com/sagemath/sage/blob/develop/src/sage/misc/latex.py#L886

StevenClontz commented 2 months ago

Okay after 14 minutes I cannot see exactly where SageMath is deciding how to typeset exponents... :'(

siwelwerd commented 2 months ago

I wonder if it is treating them as polynomials, and doing it in the latex() method for polynomials?

https://github.com/sagemath/sage/blob/e042294b5ed8e1cb564965079e20e4270ccde340/src/sage/rings/polynomial/polynomial_element.pyx#L3196