Being excited about the possibility of using Sage in WW, found this page:
https://webwork.maa.org/wiki/AskSage
I copied the code into a .pg file and tried to create it as a problem in WW. I've included the text of the problem below for convenience.
(A simplified version of what I want to accomplish is this. If I have $a
= 1 and $b = 2 in my .pg file, I want Sage to compute $a + $b and
return the answer as $c. Obviously I wouldn't use Sage to do this
specifically, but if I could understand how to do this then it would
help me better understand how AskSage works.)
But, the code doesn't work for me. I get this error:
1. ERROR caught by Translator while processing problem file:Linear_Algebra/HW2/b.pg **************** ERRORS from evaluating PG file:line 23 is:
'HASH' is not defined in this context; see position 1 of formula at line 23 of (eval 1805) Died within main::Matrix called at line 23 of (eval 1805)
$M = Matrix($sageReply1)
WeBWorK Warnings
Warning messages
Can't locate package Types::Serialiser::BooleanBase for @JSON::PP::Boolean::ISA at [PG]/lib/WeBWorK/PG/IO.pm line 414
Can't locate package Types::Serialiser::BooleanBase for @JSON::PP::Boolean::ISA at [PG]/lib/WeBWorK/PG/IO.pm line 414
Use of uninitialized value $sage_WEBWORK_data in substitution (s///) at [PG]/lib/WeBWorK/PG/IO.pm line 431
Use of uninitialized value $sage_WEBWORK_data in substitution (s///) at [PG]/lib/WeBWorK/PG/IO.pm line 432
IO.pm: ERROR trapped during JSON call to sage:
Error receiving JSON output from sage:
{"success": false, "execute_reply": {"status": "error", "traceback": ["\u001b[0;36m File \u001b[0;32m\"\"\u001b[0;36m, line \u001b[0;32m27\u001b[0m\n\u001b[0;31m print random_matrix(QQ,Integer(4),Integer(4),algorithm='echelonizable',rank=Integer(2)).rows()\u001b[0m\n\u001b[0m ^\u001b[0m\n\u001b[0;31mSyntaxError\u001b[0m\u001b[0;31m:\u001b[0m invalid syntax\n"], "ename": "SyntaxError", "evalue": "invalid syntax (, line 27)", "engine_info": {"engine_uuid": "21685e14-fbe7-4bbd-b364-8c0de1f07688", "engine_id": -1, "method": "execute"}, "execution_count": 1, "user_expressions": {}, "payload": []}}
at [PG]/lib/WeBWorK/PG/IO.pm line 445
Died within WeBWorK::PG::IO::AskSage called at line 769 of [PG]/lib/PGcore.pm
from within PGcore::AskSage called at line 213 of [PG]/macros/PG.pl
from within main::AskSage called at line 22 of (eval 1357)
at [PG]/lib/WeBWorK/PG/IO.pm line 456
Processing of this PG problem was not completed. Probably because of a syntax error.
The translator died prematurely and no PG warning messages were transmitted. at /opt/webwork/webwork2/lib/WeBWorK/ContentGenerator/Problem.pm line 778.
########################################################
DOCUMENT();
loadMacros(
"PGstandard.pl",
"MathObjects.pl",
);
###############################################
##
## pg initializations and regular WeBWorK code
Context("Matrix");
my $rows = random(3,4);
my $columns = random(4,5);
my $rank = random(2,3);
$SageCode1=<<END;
print random_matrix(QQ,$rows,$columns,algorithm='echelonizable',rank=$rank).rows()
END
$sageReply1 = AskSage($SageCode1,{accepted_tos=>true,seed=>$problemSeed});
$M = Matrix($sageReply1);
if (sageReturnedFail($sageReply1) ) {
$sageReply1 = "[1]"; # default value for $M;
}
$SageCode2 =<<END;
print matrix(QQ,$sageReply1).rref().rows()
END
$sageReply2 = AskSage($SageCode2,{accepted_tos=>true});
if (sageReturnedFail($sageReply2) ) {
$sageReply2 = "[5]"; # default value for $ans;
}
$ans = Matrix($sageReply2);
Context()->texStrings;
BEGIN_TEXT
Row reduce \( $M \) $BR
\{ $ans->ans_array\}
END_TEXT
Context()->normalStrings;
ANS($ans->cmp());
ENDDOCUMENT();