AskSage

Make a call to a Sage cell server from within a problem.

Complete Code

Download file: AskSage.pg

PG problem file

Explanation

DOCUMENT();
loadMacros('PGstandard.pl', 'PGML.pl', 'PGcourse.pl');

Preamble

These standard macros need to be loaded.
Context('Matrix');

my $rows    = random(3, 4);
my $columns = random(4, 5);
my $rank    = random(2, 3);

$sageReply = AskSage(<<"END_CODE", { seed => $problemSeed });
WEBWORK['M'] = random_matrix(
    QQ, $rows, $columns, algorithm = 'echelonizable', rank = $rank
).rows()
WEBWORK['RREF'] = matrix(QQ, WEBWORK['M']).rref().rows()
END_CODE

if (sageReturnedFail($sageReply)) {
    # Fallback values for $M and $RREF in case the AskSage call fails.
    $M = Matrix([ [ -4, 8, -12, 15 ], [ 5, -10, 15, -19 ], [ 2, -4, 6, -5 ] ]);
    $RREF = Matrix([ [ 1, -2, 3, 0 ], [ 0, 0, 0, 1 ], [ 0, 0, 0, 0 ] ]);
} else {
    $M    = Matrix($sageReply->{webwork}{M});
    $RREF = Matrix($sageReply->{webwork}{RREF});
}

Setup

Determine a random number of rows, columns, and rank for the random matrix that will be generated by Sage.

Send the code to generate the random matrix and its reduced row echelon form to the Sage cell service by calling the AskSage method. The first argument to the AskSage method must be the code for Sage to evaluate. The results are saved in keys of the WEBWORK dictionary. This is a special variable that the AskSage method looks for in the returned result and JSON decodes. Use this to save the data that is needed for the problem. The second optional argument is a reference to a hash of options. Generally, the only option is the seed. This can be used to pass the problem seed to Sage so that any random calls it makes also use that seed.

After the AskSage method is returned check to see if the call failed with the sageReturnedFail method. If that is true, then the Sage cell server query failed. In that case set fallback values for the variables needed in the problem so that a workable problem is rendered in this case. If the call succeeded the Sage WEBWORK dictionary will be decoded into the webwork hash key of the reply.

Note that by default AskSage queries the public Sage cell service at https://sagecell.sagemath.org/service. However, that site no longer accepts public service requests. The service endpoint has been shut down due to security concerns. Thus the AskSage method will always fail with that URL. To fix this the WeBWorK administrator of your server can set up a local Sage cell service and configure AskSage to use that.

BEGIN_PGML
Give the reduced row echelon form of the following matrix.

[```[$M]```]

The reduced row echelon form is

[_]*{$RREF}
END_PGML

Statement

This is the problem statement in PGML.
BEGIN_PGML_SOLUTION
Solution explanation goes here.
END_PGML_SOLUTION

ENDDOCUMENT();

Solution

A solution should be provided here.