# [LON-CAPA-users] math rendering engine

Stefan Bisitz lon-capa-users@mail.lon-capa.org
Tue, 19 Jan 2010 11:47:46 +0100

Hi Justin and other math authors,

Once again another optimization:
You might want to put your functions into an array and the answer
examples, too. Then dynamically choose from these lists by using
$n = &random(1,$#functions,1);
$function =$functions[$n];$example = $examples[$n];

This way, you can easily add (or remove) functions by just adding them
to the functions array and add an example to the examples array. No

The following problem source code respects all of this and the other
optimizations discussed before. It's not fully tested, but should work.

Hope this helps,
Stefan Bisitz

----------------------------------------------------------------
<problem>
<script type="loncapa/perl">
@functions = (
"(x^2 - 1)^2",
"sqrt(x^2 - 1)",
"3/(2 + x^2)",
"2/(3 + sqrt(1 + x))",
);
@examples = (
&xmlparse('Many answers are possible. One example would be <m>$f(x) = x^2, \quad g(x) = x^2 - 1$</m>'),
&xmlparse('Many answers are possible. One example would be <m>$f(x) = \sqrt{x}, \quad g(x) = x^2 - 1$</m>'),
&xmlparse('Many answers are possible. One example would be <m>$f(x) = 3/x, \quad g(x) = 2 + x^2$</m>'),
&xmlparse('Many answers are possible. One example would be <m>$f(x) = 2/(3 + x), \quad g(x) = \sqrt{1 + x}$</m>'),
);
$n = &random(1,$#functions,1);
$function =$functions[$n];$example = $examples[$n];
</script>

<startouttext />
<p>Give an example of two nontrivial functions <m>$f$</m> and <m>$g$</m> such that <m eval="on">$(f \circ g)(x) =$</m>
<algebra>$function</algebra>.</p>Enter your answer in the form <p><b>expression1,expression2</b></p> where <m>$ f(x) =  $</m> <b>expression1</b> and <m>$ g(x) =  $</m> <b>expression2</b>.<p></p> <endouttext /> <mathresponse answerdisplay="$example" cas="maxima" args="$function" id="11"> <answer>f(x) := RESPONSE[1]; g(x) := RESPONSE[2]; h(x) := LONCAPALIST[1]; composition:is(trigsimp(f(g(x)) - h(x)) = 0); fnottrivial:is(not(f(x) = x)); gnottrivial:is(not(g(x) = x)); composition and fnottrivial and gnottrivial;</answer> <textline readonly="no" size="20" /> <hintgroup showoncorrect="no"> <mathhint name="composition not equal" cas="maxima" args="$function" id="12">
g(x) := RESPONSE[2];
h(x) := LONCAPALIST[1];
</mathhint>
<hintpart on="composition not equal">
<startouttext /><p>Your example does not satisfy <m eval="on">$(f \circ g)(x) = f(g(x)) =$</m>
<algebra>$function</algebra>.</p><endouttext /> </hintpart> </hintgroup> <hintgroup showoncorrect="no"> <mathhint name="f trivial" cas="maxima" id="13"> <answer>f(x) := RESPONSE[1]; is(f(x) = x);</answer> </mathhint> <hintpart on="f trivial"> <startouttext /><p>The question asks for nontrivial functions, and so you cannot use <m>$ f(x) = x $</m>.</p><endouttext /> </hintpart> </hintgroup> <hintgroup showoncorrect="no"> <mathhint name="g trivial" cas="maxima" id="14"> <answer>g(x) := RESPONSE[2]; is(g(x) = x);</answer> </mathhint> <hintpart on="g trivial"> <startouttext /><p>The question asks for nontrivial functions, and so you cannot use <m>$ g(x) = x $</m>.</p><endouttext /> </hintpart> </hintgroup> </mathresponse> </problem> ---------------------------------------------------------------- On 19.01.2010 11:13, Stefan Bisitz wrote: > And again... ;-) > > Much better (the best?): > > Don't use two variables, one for calculations and one for display. It's > simpler, shorter and less error-prone to have only one variable: > > <script type="loncapa/perl"> >$function1 = "(x^2 - 1)^2";
> $function2 = "sqrt(x^2 - 1)"; >$function3 = "3/(2 + x^2)";
> $function4 = "2/(3 + sqrt(1 + x))"; >$example1=[...]
> </script>
>
> <startouttext />
> <p>Give an example of two nontrivial functions <m>$f$</m> and <m>$g >$</m> such that <m eval="on">$(f \circ g)(x) =$</m>
> <algebra>$displayfunction</algebra>.</p> > [...] > <endouttext /> > > > In general, store or calculate your formula in one variable using the > 1-dimensional "pocket calculator" notation. Reuse the same variable to > display it by using the <algebra> tag (without any "$") which
> automatically renders in the preferred method.
>
> Stefan Bisitz
>
>
> On 19.01.2010 11:04, Stefan Bisitz wrote:
>> Hi again,
>>
>> Why "pre-parse" anyway?
>>
>> Even better:
>>
>> 1)
>> <script type="loncapa/perl">
>> [...]
>> $displayfunction1 = '(x^2 - 1)^2'; >> [...] >> >> and 2) >> [...] >> such that <m eval="on">$ (f \circ g)(x) = $displayfunction$</m>.
>> [...]
>>
>>
>> Stefan Bisitz
>>
>>
>> On 19.01.2010 10:32, Stefan Bisitz wrote:
>>> Hi Justin,
>>>
>>> It's quite simple to solve your display issues. You parse the formula
>>> twice:
>>> 1) $displayfunction1 = &xmlparse('<m>$(x^2 - 1)^2$</m>'); >>> [...] >>> >>> 2) <m eval="on">$(f \circ g)(x) = $displayfunction$</m>
>>>
>>> Just change to
>>> <m>$(f \circ g)(x) =$</m> $displayfunction >>> >>>$displayfunction is already parsed and must not be included again in
>>> the <m> tag.
>>>
>>> tth as well as mimetex works now (tested on bleeding edge machine).
>>>
>>> And yes, please avoid to use jsmath hardcoded in the problem. Let the
>>> CCs or students decide.
>>>
>>> Stefan Bisitz
>>>
>>>
>>>
>>> On 19.01.2010 09:59, Justin Gray wrote:
>>>> When displaying equations, setting the display attribute within the
>>>> <m> tag to "jsMath" is generally discouraged as it requires users to
>>>> have
>>>> installed jsmath software on their computer and will override their
>>>> preferences on how math equations are displayed.
>>>>
>>>> I have tried viewing the problem below on several computers and
>>>> jsMath seems to be the only math rendering engine that lets me view
>>>> the equations properly. (With tth the equations do not show up at
>>>> all and with mimetex the equations are incomprehensible.) I am
>>>> reluctant to force users to jsMath for this problem. Perhaps there
>>>> is another way to remedy this problem?
>>>>
>>>> Any suggestions are appreciated.
>>>>
>>>> Thanks,
>>>> Justin
>>>>
>>>> Justin Gray | Senior Lecturer
>>>> Department of Mathematics | Simon Fraser University
>>>> 8888 University Drive, Burnaby | V5A 1S6 | Canada
>>>> Tel: +1 778.782.4237
>>>>
>>>> <problem>
>>>> <script type="loncapa/perl">
>>>> $function1 = "(x^2 - 1)^2"; >>>>$function2 = "sqrt(x^2 - 1)";
>>>> $function3 = "3/(2 + x^2)"; >>>>$function4 = "2/(3 + sqrt(1 + x))";
>>>> $displayfunction1 = &xmlparse('<m>$(x^2 - 1)^2$</m>'); >>>>$displayfunction2 = &xmlparse('<m>$\sqrt{x^2 - 1}$</m>');
>>>> $displayfunction3 = &xmlparse('<m>$\displaystyle \frac{3}{2 +
>>>> x^2}$</m>'); >>>>$displayfunction4 = &xmlparse('<m>$\displaystyle \frac{2}{3 + >>>> \sqrt{1 + x}}$</m>');
>>>> $example1=&xmlparse('Many answers are possible. One example would be >>>> <m>$f(x) = x^2, \quad g(x) = x^2 - 1$</m>'); >>>>$example2=&xmlparse('Many answers are possible. One example would be
>>>> <m>$f(x) = \sqrt{x}, \quad g(x) = x^2 - 1$</m>');
>>>> $example3=&xmlparse('Many answers are possible. One example would be >>>> <m>$f(x) = 3/x, \quad g(x) = 2 + x^2$</m>'); >>>>$example4=&xmlparse('Many answers are possible. One example would be
>>>> <m>$f(x) = 2/(3 + x), \quad g(x) = \sqrt{1 + x}$</m>');
>>>> $n = &random(1,4,1); >>>>$function =
>>>> &choose($n,"$function1","$function2","$function3","$function4"); >>>>$displayfunction =
>>>> &choose($n,"$displayfunction1","$displayfunction2","$displayfunction3","$displayfunction4"); >>>> >>>>$example = &choose($n,"$example1","$example2","$example3","$example4"); >>>> </script> >>>> >>>> <startouttext /><p>Give an example of two nontrivial functions >>>> <m>$f$</m> and <m>$g$</m> such that <m eval="on">$(f \circ g)(x) =
>>>> $displayfunction$</m>.</p>Enter your answer in the form
>>>> <p><b>expression1,expression2</b></p> where <m>$f(x) =$</m>
>>>> <b>expression1</b> and <m>$g(x) =$</m>
>>>> <b>expression2</b>.<p></p><endouttext />
>>>>
>>>> <mathresponse answerdisplay="$example" cas="maxima" args="$function"
>>>> id="11">
>>>> g(x) := RESPONSE[2];
>>>> h(x) := LONCAPALIST[1];
>>>> composition:is(trigsimp(f(g(x)) - h(x)) = 0);
>>>> fnottrivial:is(not(f(x) = x));
>>>> gnottrivial:is(not(g(x) = x));
>>>> composition and fnottrivial and gnottrivial;</answer>
>>>>     <hintgroup showoncorrect="no">
>>>>         <mathhint name="composition not equal" cas="maxima"
>>>> args="$function" id="12"> >>>> <answer>f(x) := RESPONSE[1]; >>>> g(x) := RESPONSE[2]; >>>> h(x) := LONCAPALIST[1]; >>>> is(not(f(g(x)) = h(x)));</answer> >>>> </mathhint> >>>> <hintpart on="composition not equal"> >>>> <startouttext /><p>Your example does not satisfy <m >>>> eval="on">$(f \circ g)(x) = f(g(x)) = $displayfunction >>>>$</m>.</p><endouttext />
>>>> </hintpart>
>>>>     </hintgroup>
>>>>     <hintgroup showoncorrect="no">
>>>>         <mathhint name="f trivial" cas="maxima" id="13">
>>>>         </mathhint>
>>>> <hintpart on="f trivial">
>>>>     <startouttext /><p>The question asks for nontrivial functions,
>>>> and so you cannot use <m>$f(x) = x$</m>.</p><endouttext />
>>>> </hintpart>
>>>>     </hintgroup>
>>>>     <hintgroup showoncorrect="no">
>>>>         <mathhint name="g trivial" cas="maxima" id="14">
>>>>         </mathhint>
>>>> <hintpart on="g trivial">
>>>>     <startouttext /><p>The question asks for nontrivial functions,
>>>> and so you cannot use <m>$g(x) = x$</m>.</p><endouttext />
>>>> </hintpart>
>>>>     </hintgroup>
>>>> </mathresponse>
>>>> </problem>
>>>>
>>>>
>>>>
>>>
>>> _______________________________________________
>>> LON-CAPA-users mailing list
>>> LON-CAPA-users@mail.lon-capa.org
>>> http://mail.lon-capa.org/mailman/listinfo/lon-capa-users
>>
>>
>
> _______________________________________________
> LON-CAPA-users mailing list
> LON-CAPA-users@mail.lon-capa.org
> http://mail.lon-capa.org/mailman/listinfo/lon-capa-users