File:  [LON-CAPA] / doc / help / codingmath.tex
Revision 1.2: download - view: text, annotated - select for diffs
Fri Sep 12 02:22:23 2008 UTC (13 years, 8 months ago) by riegler
Branches: MAIN
CVS tags: version_2_9_X, version_2_9_99_0, version_2_9_1, version_2_9_0, version_2_8_X, version_2_8_99_1, version_2_8_99_0, version_2_8_2, version_2_8_1, version_2_8_0, version_2_7_X, version_2_7_99_1, version_2_7_99_0, version_2_7_1, version_2_12_X, version_2_11_X, version_2_11_4_uiuc, version_2_11_4_msu, version_2_11_4, version_2_11_3_uiuc, version_2_11_3_msu, version_2_11_3, version_2_11_2_uiuc, version_2_11_2_msu, version_2_11_2_educog, version_2_11_2, version_2_11_1, version_2_11_0_RC3, version_2_11_0_RC2, version_2_11_0_RC1, version_2_11_0, version_2_10_X, version_2_10_1, version_2_10_0_RC2, version_2_10_0_RC1, version_2_10_0, loncapaMITrelate_1, language_hyphenation_merge, language_hyphenation, bz6209-base, bz6209, HEAD, GCI_3, GCI_2, GCI_1, BZ4492-merge, BZ4492-feature_horizontal_radioresponse, BZ4492-feature_Support_horizontal_radioresponse, BZ4492-Support_horizontal_radioresponse
replaced example for mathresponse with one easier to digest

\newcommand {\LC} {LON-CAPA~}
\title{Implementing Math Problems in \LC using CAS Support}
\author{Peter Riegler\\ and hopefully other contributers}
We describe the \LC interface to computer algebra systems (CAS) which conveniently allows to implement rather sophisticated math problems. 
\section{\LC interfaces to CAS}
There are two interfaces: The first one {\tt \&cas} can be called anywhere from a perl script. Its primary use is to give access to CAS functionality from within perl. 
The second one {\tt <mathresponse>} is used for doing the complete grading of a problem exclusively be means of CAS functionality.

Both interfaces preprocess students input by the perl function {\tt \&implicit\_multiplication(\$f)}. It adds mathematical multiplication operators to the formula expression \$f where only implicit multiplication is used. Example: \&implicit\_multiplication('2(b+3c)') returns 2*(b+3*c).
\subsection{{\tt \&cas}-interface}
\&cas(\$s,\$e,\$l) Evaluates the expression \$e inside the symbolic algebra system \$s. Currently, only the Maxima
symbolic math system is implemented. \$l is an optional comma-separated list of libraries. Example: \&cas('maxima','6*7')

\subsection{{\tt <mathresponse>}-interface}
{\tt <mathresponse>} is a way to have a problem graded based on an algorithm that is executed inside of a computer algebra system. 
The documentation of \LC points out that  use of this response type is
generally discouraged, since the responses will not be analyzable by the LON-CAPA statistics tools. Yet, it can be useful.

Which computer algebra system is to be used is specified in the cas argument of the mathresponse tag; currently, only Maxima is available.
LON-CAPA sets up two arrays inside the computer algebra system: RESPONSE and LONCAPALIST. RESPONSE contains the student input by component, for example, if "3,42,17" is entered, RESPONSE[2] would be 42. LONCAPALIST contains the arguments passed in the args of mathresponse.

The answerdisplay is what is displayed when the problem is in "Show Answer" mode.

The following example illustrates this. It is a simplified version of \begin{verbatim} /res/msu/kashy/physicsLib02/02_Math_2_Trig/LinethroughPt2.problem\end{verbatim}.

<script type="loncapa/perl">
$x = &random(-1,1,2) * &random(1,5,1);
$y = &random(-1,1,2) * &random(1,4,1); 
# avoids y=x and y=-x as possible solutions
@args = ($x, $y); 
# This is passed to the CAS, where it will be 

# In the <answer> block below, RESPONSE[1] is the 
# student's submission (a scalar, e.g., 3x + 2  ).
# The two lines below provide a varying sample function, 
# y(x)=mx+b, to be displayed when a correct answer is entered.

$b = $y-$m*$x;
if($b > 0){$b = "+ " . $b;} 
elsif ($b == 0) {$yb = "";}
elsif ($b < 0) {$yb = "- " . $b;}
$example = "$m x $b is an example of the many functions that 
  meet the criteria above.";

<startouttext />
State a function y(x) which passes through the point ($x, $y) 
and which has a constant slope with absolute value > 1.<br />
<endouttext />
<mathresponse answerdisplay="$example" cas="maxima" args="@args">

thrupoint:is(abs(y(LONCAPALIST[1]) - LONCAPALIST[2]) <= 0.000000001);
islinear:is(diff(y(x),x,2) = 0);
AbsSlopeGT1:is(abs(diff(y(x),x,1)) > 1);
thrupoint and islinear and AbsSlopeGT1;
<b>y(x)</b> = <textline readonly="no" size="20" />



\section{Interface to maxima}
\LC servers run several maxima sessions in parallel. There is a queue which distributes CAS calls to these sessions. When processing a new CAS call one has to be sure that maxima is reset to some default state. In particular functions, variables etc.\ defined in previous calls should be removed. LON-CAPA automatically takes care of that by means of the following sequence of commands which is executed every time before a maxima code snippet supplied by an author will be executed:
Authors should be aware of this, because {\tt kill(all)} 
does {\em not} delete all previously defined 
stuff.\footnote{In fact, there seems to be no maxima command which 
does the desired job. If the cherished reader finds out about one, any \LC developer will be more than happy to change the above sequence accordingly.} One known issue is, that previously loaded maxima packages will not be removed by {\tt kill(all)}.

The two commands in front of {\tt kill(all)} make sure that the maxima session renders output as expressions contained in a single line and that maxima's elementary simplification functionality is turned on. (More on the {\tt simp}-flag and its usefulnes in \ref{SEC:simp}.)
\section{Primer on maxima}
This sections serves as a short tutorial on maxima. It is intended for readers who either have not worked with maxima before or are not familiar with CAS at all. In the long run when authoring \LC problems you might wish to consult more advanced material provided e.g.\ on

To start with it is a good idea to have access to maxima either on a mainframe or on your personal computer. This will also be helpful if you author problems using maxima. Maxima runs on almost any platform and can be downloaded at the above mentioned URL. In the following we will use the terminal interface to maxima. The graphical user interfaces basically behave the same way, except for wxmaxima where you do not have a prompt but have to enter your expressions at the buttom.

Let's start with some survival basics: exiting a maxima session and getting help. To quit maxima type {\tt quit();}
Maxima 5.9.2
Using Lisp GNU Common Lisp (GCL) GCL 2.6.7 (aka GCL)
Distributed under the GNU Public License. See the file COPYING.
Dedicated to the memory of William Schelter.
This is a development version of Maxima. The function bug_report()
provides bug reporting information.
(%i1) quit();
To search for help use {\tt ?} followed by blank and an appropriate keyword
and follow the instructions.
Note that the blank after {\tt ?} is essential.
(%i1) ? integrate

 0: integrate :( for Integration.
 1: integrate_use_rootsof :Definitions for Integration.
Enter space-separated numbers, `all' or `none': 
Entering 1 would give you all the details on how to integrate functions.

Obviously you won't use neither {\tt quit()} nor {\tt ?} when authoring LON-CAPA problems. To assure that you won't do that accidentily \LC will block these and a number of other commands.

Commands have to end either with {\tt ;} or {\tt \$}. The effect of {\tt \$} is that no output will be displayed:
(%i2) 1+1;

(%o2)                                  2
(%i3) 1+1$

By now you might have noticed that inputs are precedented by 
{\tt (\%i<number>)} 
and the corresponding outputs by
{\tt (\%o<number>)}, 
where {\tt <number>} is a consecutive number. You can refer back to previous outputs via {\tt \%o<number>}:
(%i4) (2*%o2)^20;

(%o4)                            1099511627776
Note that refering back via {\tt \%o<number>} does not make sense when authoring problems, as you will not know {\tt <number>}. Instead you will use named variables of course. The assignment operator for variables is {\tt :}
(%i5) a: sin(%pi);

(%o5)                                  0

(%i6) b: cos(%pi);

(%o6)                                 - 1
Mathematical constants such as $\pi$ and Euler's number are precedented by {\tt \%}:
(%i7) log(%e);

(%o7)                                  1
The assignment operator for functions is {\tt :=}
(%i8) f(x):=b*x^2;

(%o8)                            f(x) := b x
(%i9) f(2);

(%o9)                                 - 4
(%i10) integrate(f(x),x);

(%o10)                               - --
Here we see maxima's two-dimensional rendering. Setting the flag {\tt display2d} to false will turn it off:
(%i11) display2d:false;

(%o11) false
(%i12) f(x);

(%o12) -x^2

When authoring {\tt <mathresponse>}-problems you will have to make sure that the last value your maxima code snippet will return is either true or false. Most often {\tt is} will do the job. For instance, 
(%i13) RESPONSE[1]: -x^3/3 + 5;

(%o13) 5-x^3/3
(%i14) is ( diff(RESPONSE[1],x) = f(x) );

(%o14) true
checks whether the students response {\tt RESPONSE[1]} is the antiderivative of $f(x)$. Note that by differentiating {\tt RESPONSE[1]} via the {\tt diff}-command we make sure that any integration constant will be accepted:
(%i15) RESPONSE[1]: -x^3/3 + C;

(%o15) C-x^3/3
(%i12) is ( diff(RESPONSE[1],x) = f(x) );

(%o16) true
(%i16) RESPONSE[1]: -x^3/3 + C*x;

(%o17) x*C-x^3/3
(%i18) is ( diff(RESPONSE[1],x) = f(x) );

(%o18) false

more on delayed assignment, exact numbers, ...

\section{Simplify the following expression ...\label{SEC:simp}}
Many text book examples start with this phrase. Giving it a moment of thought reveals that these are somewhat ill-posed problems. For what is simple depends to a large extent on what you {\em define} to be simple and on what you want to do next.

more to be written by Zhoujing Wang (my student working on the issue)


FreeBSD-CVSweb <>