WeBWorK Problems

Q: random polynomial

Q: random polynomial

by Dick Lane -
Number of replies: 2
I am interested in creating a forum/blog where we can discuss various ways to structure tasks for part of a problem, together with suitable programming.  Perhaps a "better than this" or "instead of this" blog would be useful --- what do you think?  [FWiW: I tilt toward the latter because slick stuff is not always optimal for learning.]

In the meantime, consider various ways to generate a random polynomial (without structural constraints).  Here is my current code for a problem where y-intercept is the only feature of this function needed by a student [full problem involves other stuff].  I request suggestions for improvements or slicker ways to generate a random polynomial.

####    integer coefficients (upto sign)
@a = (1 .. 9)[NchooseK(9,4)] ;

####    integer powers for non-constant terms
@p = (1 .. 9)[NchooseK(9,3)] ;
####     powers WITH  a constant term
@p = ( 0 , @p )[NchooseK(4,4)] ;

$poly = '0' ;
foreach  my $i  (0 .. 3)  {
    my  $s = list_random(-1,1);
    $poly .= "+ ($s) * $a[$i] * x^($p[$i])" ;
} ;
$poly = Compute( "$poly" ) -> reduce ;
$y0 = $poly->eval( x => 0 ) ;

?? Q for Davide:  should  reduce  be  reduce()
In reply to Dick Lane

Re: Q: random polynomial

by Dick Lane -
FWiW: A blog posting might comment about a programming technique to arrange terms by magnitude of coefficient (or in some other order) to require extra processing by a learner.
In reply to Dick Lane

Re: Q: random polynomial

by Dick Lane -
Two minor revisions:

## inhibit rearrangement
Context() -> reduction -> set( '(-x)+y' => 0, '(-x)-y' => 0 ) ;

## repeat reduction of $poly to remove parentheses
## around constant if it is the last term
$poly = Compute( "$poly" ) -> reduce ;

I suspect the first use of reduce would replace "+ (-5 x^0)" with "+ (-5)" and the second reduce yields "- 5".  It is unclear why the second step is not needed if the constant is not the last term.