]> gitweb.factorcode.org Git - factor.git/blob - basis/math/polynomials/polynomials-docs.factor
merge project-euler.factor
[factor.git] / basis / math / polynomials / polynomials-docs.factor
1 USING: help.markup help.syntax math sequences ;
2 IN: math.polynomials
3
4 ARTICLE: "polynomials" "Polynomials"
5 "A polynomial is a vector with the highest powers on the right:"
6 { $code "{ 1 1 0 1 } -> 1 + x + x^3" "{ } -> 0" }
7 "Numerous words are defined to help with polynomial arithmetic:"
8 { $subsections
9     p=
10     p+
11     p-
12     p*
13     p-sq
14     powers
15     n*p
16     p/mod
17     pgcd
18     polyval
19     pdiff
20     pextend-conv
21     ptrim
22     2ptrim
23 } ;
24
25 ABOUT: "polynomials"
26
27 HELP: powers
28 { $values { "n" integer } { "x" number } { "seq" sequence } }
29 { $description "Output a sequence having " { $snippet "n" } " elements in the format: " { $snippet "{ 1 x x^2 x^3 ... }" } "." }
30 { $examples { $example "USING: math.polynomials prettyprint ;" "4 2 powers ." "{ 1 2 4 8 }" } } ;
31
32 HELP: p=
33 { $values { "p" "a polynomial" } { "q" "a polynomial" } { "?" "a boolean" } }
34 { $description "Tests if two polynomials are equal." }
35 { $examples { $example "USING: math.polynomials prettyprint ;" "{ 0 1 } { 0 1 0 } p= ." "t" } } ;
36
37 HELP: ptrim
38 { $values { "p" "a polynomial" } { "p" "a polynomial" } }
39 { $description "Trims excess zeros from a polynomial." }
40 { $examples { $example "USING: math.polynomials prettyprint ;" "{ 0 1 0 0 } ptrim ." "{ 0 1 }" } } ;
41
42 HELP: 2ptrim
43 { $values { "p" "a polynomial" } { "q" "a polynomial" } { "p" "a polynomial" } { "q" "a polynomial" } }
44 { $description "Trims excess zeros from two polynomials." }
45 { $examples { $example "USING: kernel math.polynomials prettyprint ;" "{ 0 1 0 0 } { 1 0 0 } 2ptrim [ . ] bi@" "{ 0 1 }\n{ 1 }" } } ;
46
47 HELP: p+
48 { $values { "p" "a polynomial" } { "q" "a polynomial" } { "r" "a polynomial" } }
49 { $description "Adds " { $snippet "p" } " and " { $snippet "q" } " component-wise." }
50 { $examples { $example "USING: math.polynomials prettyprint ;" "{ 1 0 1 } { 0 1 } p+ ." "{ 1 1 1 }" } } ;
51
52 HELP: p-
53 { $values { "p" "a polynomial" } { "q" "a polynomial" } { "r" "a polynomial" } }
54 { $description "Subtracts " { $snippet "q" } " from " { $snippet "p" } " component-wise." }
55 { $examples { $example "USING: math.polynomials prettyprint ;" "{ 1 1 1 } { 0 1 } p- ." "{ 1 0 1 }" } } ;
56
57 HELP: n*p
58 { $values { "n" number } { "p" "a polynomial" } { "n*p" "a polynomial" } }
59 { $description "Multiplies each element of " { $snippet "p" } " by " { $snippet "n" } "." }
60 { $examples { $example "USING: math.polynomials prettyprint ;" "4 { 3 0 1 } n*p ." "{ 12 0 4 }" } } ;
61
62 HELP: pextend-conv
63 { $values { "p" "a polynomial" } { "q" "a polynomial" } { "p" "a polynomial" } { "q" "a polynomial" } }
64 { $description "Convulution, extending to " { $snippet "p_m + q_n - 1" } "." }
65 { $examples { $example "USING: kernel math.polynomials prettyprint ;" "{ 1 0 1 } { 0 1 } pextend-conv [ . ] bi@" "V{ 1 0 1 0 }\nV{ 0 1 0 0 }" } } ;
66
67 HELP: p*
68 { $values { "p" "a polynomial" } { "q" "a polynomial" } { "r" "a polynomial" } }
69 { $description "Multiplies two polynomials." }
70 { $examples { $example "USING: math.polynomials prettyprint ;" "{ 1 2 3 0 0 0 } { 1 2 0 0 } p* ." "{ 1 4 7 6 0 0 0 0 0 }" } } ;
71
72 HELP: p-sq
73 { $values { "p" "a polynomial" } { "p^2" "a polynomial" } }
74 { $description "Squares a polynomial." }
75 { $examples { $example "USING: math.polynomials prettyprint ;" "{ 1 2 0 } p-sq ." "{ 1 4 4 0 0 }" } } ;
76
77 HELP: p/mod
78 { $values { "p" "a polynomial" } { "q" "a polynomial" } { "z" "a polynomial" } { "w" "a polynomial" } }
79 { $description "Computes to quotient " { $snippet "z" } " and remainder " { $snippet "w" } " of dividing " { $snippet "p" } " by " { $snippet "q" } "." }
80 { $examples { $example "USING: kernel math.polynomials prettyprint ;" "{ 1 1 1 1 } { 3 1 } p/mod [ . ] bi@" "V{ 7 -2 1 }\nV{ -20 0 0 }" } } ;
81
82 HELP: pgcd
83 { $values { "p" "a polynomial" } { "q" "a polynomial" } { "a" "a polynomial" } { "d" "a polynomial" } }
84 { $description "Computes the greatest common divisor " { $snippet "d" } " of " { $snippet "p" } " and " { $snippet "q" } ", and another value " { $snippet "a" } " satisfying:" { $code "a*q = d mod p" } }
85 { $notes "GCD in the case of polynomials is a monic polynomial of the highest possible degree that divides into both " { $snippet "p" } " and " { $snippet "q" } "." }
86 { $examples
87     { $example "USING: kernel math.polynomials prettyprint ;"
88                "{ 1 1 1 1 } { 1 1 } pgcd [ . ] bi@"
89                "{ 0 0 }\n{ 1 1 }"
90     }
91 } ;
92
93 HELP: pdiff
94 { $values { "p" "a polynomial" } { "p'" "a polynomial" } }
95 { $description "Finds the derivative of " { $snippet "p" } "." } ;
96
97 HELP: polyval
98 { $values { "x" number } { "p" "a polynomial" } { "p[x]" number } }
99 { $description "Evaluate " { $snippet "p" } " with the input " { $snippet "x" } "." }
100 { $examples { $example "USING: math.polynomials prettyprint ;" "2 { 1 0 1 } polyval ." "5" } } ;
101
102 HELP: polyval*
103 { $values { "p" "a literal polynomial" } }
104 { $description "Macro version of " { $link polyval } ". Evaluates the literal polynomial " { $snippet "p" } " at the value off the top of the stack." }
105 { $examples { $example "USING: math.polynomials prettyprint ;" "2 { 1 0 1 } polyval* ." "5" } } ;
106
107 { polyval polyval* } related-words