WeBWorK Problems

self-defined CIS function issue

self-defined CIS function issue

by Carl Yao -
Number of replies: 2

Hello:

I defined the function cis( ), so students can enter 2*cis(pi) to represent 2*(cos(pi)+sin(pi)). However when the answer is r*cis(0), I get the error:

Can't generate enough valid points for comparison

Please help. Thank you!

Carl Yao

Portland Community College



DOCUMENT();


loadMacros(

  "PGstandard.pl",

  "MathObjects.pl",

  "PGML.pl",

  "PGcourse.pl",,

);


##############################################



package my::Complex;

our @ISA = ('Value::Complex');


sub cis {

  my $z = shift;

  return cos($z) + main::i * sin($z);

}


package main;


Context("Complex");


Context()->{value}{Complex} = 'my::Complex';

Context()->constants->set(i => {value => my::Complex->new(0,1)});

Context()->functions->add(cis => {

  class => 'Parser::Function::complex', TeX => '\operatorname{cis}',

  complex => 1, isCommand => 1

});


sub cis {Parser::Function->call("cis", my::Complex->new(@_))}


Context()->flags->set(reduceConstantFunctions => 0);


$ans = Formula("2*cis(0)");


##############################################


TEXT(beginproblem());

BEGIN_PGML


Correct answer is [` 2*cis(0)= `]


[________________________]{$ans}


END_PGML


##############################################


ENDDOCUMENT();


In reply to Carl Yao

Re: self-defined CIS function issue

by Danny Glin -
After a bit of trial and error, it looks like your code has trouble with constant functions using cis in the correct answer.  You are declaring "2*cis(0)" as a Formula, which means that WeBWorK is treating it as a function of z where the output is constant.  I don't know why that isn't working, but if you change that line to
$ans = Compute("2*cis(0)");
then it works for me, but it is now expecting a complex number as the student's answer rather than a complex function, and will give the student a warning message if they enter a function rather than a complex number.

I suspect that this has something to do with the fact that you have created cis as a perl function, and then shoehorned it into a MathObject.  I was able to get this working by using parserFunction.pl (https://webwork.maa.org/wiki/AddingFunctions):

DOCUMENT();

loadMacros(

  "PGstandard.pl",

  "MathObjects.pl",

  "PGML.pl",

  "parserFunction.pl",

  "PGcourse.pl",

);


##############################################


Context("Complex");


parserFunction("cis(z)" => "cos(z) + i*sin(z)");


Context()->flags->set(reduceConstantFunctions => 0);


$ans = Formula("2*cis(0)");


##############################################


TEXT(beginproblem());


BEGIN_PGML

Correct answer is [` 2*cis(0)= `]

[________________________]{$ans}

END_PGML


##############################################


ENDDOCUMENT();