Add p^ to polynomial vocab for exponentiation of polynomials

db4
Erik Charlebois 2010-02-13 02:35:09 -08:00
parent ba493063fb
commit 52230d6c4d
3 changed files with 17 additions and 0 deletions

View File

@ -11,6 +11,7 @@ ARTICLE: "polynomials" "Polynomials"
p-
p*
p-sq
p^
powers
n*p
p/mod
@ -74,6 +75,11 @@ HELP: p-sq
{ $description "Squares a polynomial." }
{ $examples { $example "USING: math.polynomials prettyprint ;" "{ 1 2 0 } p-sq ." "{ 1 4 4 0 0 }" } } ;
HELP: p^
{ $values { "p" "a polynomial" } { "n" number } { "p^n" "a polynomial" } }
{ $description "Computes " { $snippet "p" } " to the power of " { $snippet "n" } "." }
{ $examples { $example "USING: math.polynomials prettyprint ;" "{ 1 2 0 } 3 p^ ." "{ 1 6 12 8 0 0 0 }" } } ;
HELP: p/mod
{ $values { "p" "a polynomial" } { "q" "a polynomial" } { "z" "a polynomial" } { "w" "a polynomial" } }
{ $description "Computes to quotient " { $snippet "z" } " and remainder " { $snippet "w" } " of dividing " { $snippet "p" } " by " { $snippet "q" } "." }

View File

@ -15,6 +15,8 @@ IN: math.polynomials.tests
[ { 0 0 0 } ] [ { 0 0 0 } { 0 0 0 } p- ] unit-test
[ { 0 0 0 } ] [ 4 { 0 0 0 } n*p ] unit-test
[ { 4 8 0 12 } ] [ 4 { 1 2 0 3 } n*p ] unit-test
[ { 1 4 4 0 0 } ] [ { 1 2 0 } p-sq ] unit-test
[ { 1 6 12 8 0 0 0 } ] [ { 1 2 0 } 3 p^ ] unit-test
[ { 1 4 7 6 0 0 0 0 0 } ] [ { 1 2 3 0 0 0 } { 1 2 0 0 } p* ] unit-test
[ V{ 7 -2 1 } V{ -20 0 0 } ] [ { 1 1 1 1 } { 3 1 } p/mod ] unit-test
[ V{ 0 0 } V{ 1 1 } ] [ { 1 1 } { 1 1 1 1 } p/mod ] unit-test

View File

@ -38,6 +38,15 @@ PRIVATE>
: p-sq ( p -- p^2 )
dup p* ;
ERROR: negative-power-polynomial p n ;
: p^ ( p n -- p^n )
{
{ [ dup 0 > ] [ 1 - dupd [ p* ] with times ] }
{ [ dup 0 = ] [ 2drop { 1 } ] }
{ [ dup 0 < ] [ negative-power-polynomial ] }
} cond ;
<PRIVATE
: p/mod-setup ( p p -- p p n )