]> gitweb.factorcode.org Git - factor.git/commitdiff
Add p^ to polynomial vocab for exponentiation of polynomials
authorErik Charlebois <erikcharlebois@gmail.com>
Sat, 13 Feb 2010 10:35:09 +0000 (02:35 -0800)
committerErik Charlebois <erikcharlebois@gmail.com>
Sat, 13 Feb 2010 10:35:09 +0000 (02:35 -0800)
basis/math/polynomials/polynomials-docs.factor
basis/math/polynomials/polynomials-tests.factor
basis/math/polynomials/polynomials.factor

index cb2d2a6058e91ebe2a88d2fbb1bbf1b24600355a..9c16bf8a7e7fdde59a06ef71aca362f36d3fcea8 100644 (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" } "." }
index cd88d19d1317f874301bef5421a82b80398ad29d..e0cedb9adfde8f4d356b4ea5dbf2204639bd37e0 100644 (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
index 99d77d0ce2216d66d66bad92c36592b69833f293..694b9ef542e038510af4f63c60f79c84c4fb736b 100644 (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 )