The keyword “math_funcs” is used to declare which names should be treated as functions instead of variables for the current question. For example after the statement math_funcs(f, g), the parser will treat f(x) and g(x) as function calls, instead of multiplication by x. This applies in the authoring environment as well as in the student environment. Additionally, we allow the apostrophe character inside names, so you can say math_funcs(f, f’).
After f is declared as a function, f(x) is a function application, unless it is on the left side of an equal sign. Then it means function definition. For consistency with our pattern of not simplifying automatically, the function application f(5) simply typesets f(5). You can use the function "expand" to get the fully expanded form:
math_funcs(f)
f(x) = sin(x)
s1 = f(pi) # f(pi)
s2 = expand(s1) # sin(pi)
You can also embed functions into other expressions, including function composition. This allows you to use other keywords to compute or simplify expressions:
math_funcs(f, g, h)
f(x) = sin(x)
g(x) = cos(x)
h(x) = x^2
s1 = h(f(y)) + h(g(y)) # squaring both terms via function composition = hotness
s2 = compute(s1) # sin(y)^2 + cos(y)^2
s3 = simplify(s1) # 1
Things are slightly different for students entering function definitions. For example, say we add sf = input_expr() to this program. The student enters f(y) = y + 1. We cannot apply the student’s definition using sf(5): sf is a variable that holds the input box. It is not a function. Use the "apply" keyword to return the application of a student defined function with specific arguments:
math_funcs(f)
sf = input_expr()
sf5 = apply(sf, 5) # f(5)
t = expand(sf5) # 5 + 1
c = compute(sf5) # 6