The following (hart_ws1 Problem 3) works, but if there is a better approach,
$a = random(50, 90, 5);
#$f = Formula( "x^4 - ($a^2-1) x^2 - $a^2" ); #problem not accepted with x
$f = Formula( "z^4 - ($a^2-1) z^2 - $a^2" );
$ans0 = Compute( "$a" );
$ans1 = Compute( "-$a" );
$ans2 = Complex("i");
$ans3 = Complex("-1*i");
I don't see anything that I would do fundamentally different than what you have there. In the Complex Context, the default variable is z, not x, which is why the Formula in x is rejected. One could do the following in place of what you have.
Context("Complex"); $a = random(50, 90, 5); $f = Compute( "z^4 - ($a^2-1) z^2 - $a^2" ); $ans0 = Compute( "$a" ); $ans1 = Compute( "-$a" ); $ans2 = Compute( "i" ); $ans3 = Compute( "-i" );
Compute in each case should correctly determine the type of the object to create for you. A possible advantage of using Compute instead of Formula for the variable
$f is that the former preserves the string used to create the formula and displays it as the correct answer when the variable is used to check a student's submission. Thus it is possible to illustrate the structure of the correct answer once students can view the coded correct answer.
Two things to think about. I noticed you comment about the formula using x. The reason that doesn't work is that the Complex context doesn't have a variable x in it (just z). If you want to use x, you need to add it to the context. (I see Gavin has already answered this -- I had to step away for a bit, and he beat me to it.)
Your real question, however, is how to make the list of complex numbers that you want in the end. It is easiest to use
$answer = Compute("$a,-$a,i,-i");and later use
ANS($answer->cmp);Frequently, it is best to use
Compute()with whatever answer you want the students to type, rather than the individual class constructors like
List(). This makes sure the students actually can type what you want them to, and makes it easier on you, since you don't have to be concerned with the details of the classes involved.
Finally, note that inside a string used to create a MathObject, you don't have to do
-i is sufficient. It is only in perl expressions that there is a problem with
Hope that clears things up.