1 | // emacs edit mode for this file is -*- C++ -*- |
---|
2 | // $Id: cf_binom.cc,v 1.1 1996-07-08 08:13:08 stobbe Exp $ |
---|
3 | |
---|
4 | /* |
---|
5 | $Log: not supported by cvs2svn $ |
---|
6 | Revision 1.0 1996/05/17 10:59:43 stobbe |
---|
7 | Initial revision |
---|
8 | |
---|
9 | */ |
---|
10 | |
---|
11 | #include "assert.h" |
---|
12 | #include "cf_defs.h" |
---|
13 | #include "canonicalform.h" |
---|
14 | #include "cf_binom.h" |
---|
15 | |
---|
16 | #define MAXPT 40 |
---|
17 | |
---|
18 | #define INITPT 10 |
---|
19 | |
---|
20 | CFArray * ptZ = 0; |
---|
21 | CFArray * ptF = 0; |
---|
22 | |
---|
23 | int charac = 0; |
---|
24 | int gfdeg = 0; |
---|
25 | int ptZmax = INITPT; |
---|
26 | int ptFmax = 0; |
---|
27 | |
---|
28 | void |
---|
29 | resetFPT() |
---|
30 | { |
---|
31 | ptFmax = 0; |
---|
32 | } |
---|
33 | |
---|
34 | void |
---|
35 | initPT ( ) |
---|
36 | { |
---|
37 | static bool initialized = false; |
---|
38 | |
---|
39 | if ( ! initialized ) { |
---|
40 | initialized = true; |
---|
41 | ptZ = new CFArray[MAXPT+1]; |
---|
42 | ptF = new CFArray[MAXPT+1]; |
---|
43 | int i, j; |
---|
44 | ptZ[0] = CFArray(1); ptZ[0][0] = 1; |
---|
45 | ptF[0] = CFArray(1); |
---|
46 | for ( i = 1; i <= INITPT; i++ ) { |
---|
47 | ptF[i] = CFArray(i+1); |
---|
48 | ptZ[i] = CFArray(i+1); |
---|
49 | (ptZ[i])[0] = 1; |
---|
50 | for ( j = 1; j < i; j++ ) |
---|
51 | (ptZ[i])[j] = (ptZ[i-1])[j-1] + (ptZ[i-1])[j]; |
---|
52 | (ptZ[i])[i] = 1; |
---|
53 | } |
---|
54 | for ( i = INITPT+1; i <= MAXPT; i++ ) { |
---|
55 | ptF[i] = CFArray(i+1); |
---|
56 | ptZ[i] = CFArray(i+1); |
---|
57 | } |
---|
58 | ptZmax = INITPT; |
---|
59 | ptFmax = 0; |
---|
60 | } |
---|
61 | } |
---|
62 | |
---|
63 | CanonicalForm |
---|
64 | binomialpower ( const Variable & x, const CanonicalForm & a, int n ) |
---|
65 | { |
---|
66 | if ( n == 0 ) |
---|
67 | return 1; |
---|
68 | else if ( n == 1 ) |
---|
69 | return x + a; |
---|
70 | else if ( getCharacteristic() == 0 ) { |
---|
71 | if ( n <= MAXPT ) { |
---|
72 | if ( n > ptZmax ) { |
---|
73 | int i, j; |
---|
74 | for ( i = ptZmax+1; i <= n; i++ ) { |
---|
75 | (ptZ[i])[0] = 1; |
---|
76 | for ( j = 1; j < i; j++ ) |
---|
77 | (ptZ[i])[j] = (ptZ[i-1])[j-1] + (ptZ[i-1])[j]; |
---|
78 | (ptZ[i])[i] = 1; |
---|
79 | } |
---|
80 | ptZmax = n; |
---|
81 | } |
---|
82 | CanonicalForm result = 0, apower = 1; |
---|
83 | int k; |
---|
84 | for ( k = n; k >= 0; k-- ) { |
---|
85 | result += power( x, k ) * apower * (ptZ[n])[k]; |
---|
86 | if ( k != 0 ) |
---|
87 | apower *= a; |
---|
88 | } |
---|
89 | return result; |
---|
90 | } |
---|
91 | else { |
---|
92 | CanonicalForm result = binomialpower( x, a, MAXPT ); |
---|
93 | CanonicalForm xa = x + a; |
---|
94 | int i; |
---|
95 | for ( i = MAXPT; i < n; i++ ) |
---|
96 | result *= xa; |
---|
97 | return result; |
---|
98 | } |
---|
99 | } |
---|
100 | else { |
---|
101 | if ( getCharacteristic() != charac || gfdeg != getGFDegree() ) { |
---|
102 | ptFmax = 0; |
---|
103 | charac = getCharacteristic(); |
---|
104 | gfdeg = getGFDegree(); |
---|
105 | (ptF[0])[0] = 1; |
---|
106 | } |
---|
107 | if ( n <= MAXPT ) { |
---|
108 | if ( n > ptFmax ) { |
---|
109 | int i, j; |
---|
110 | for ( i = ptFmax+1; i <= n; i++ ) { |
---|
111 | (ptF[i])[0] = 1; |
---|
112 | for ( j = 1; j < i; j++ ) |
---|
113 | (ptF[i])[j] = (ptF[i-1])[j-1] + (ptF[i-1])[j]; |
---|
114 | (ptF[i])[i] = 1; |
---|
115 | } |
---|
116 | ptFmax = n; |
---|
117 | } |
---|
118 | CanonicalForm result = 0, apower = 1; |
---|
119 | int k; |
---|
120 | for ( k = n; k >= 0; k-- ) { |
---|
121 | result += power( x, k ) * apower * (ptF[n])[k]; |
---|
122 | if ( k != 0 ) |
---|
123 | apower *= a; |
---|
124 | } |
---|
125 | return result; |
---|
126 | } |
---|
127 | else { |
---|
128 | CanonicalForm result = binomialpower( x, a, MAXPT ); |
---|
129 | CanonicalForm xa = x + a; |
---|
130 | int i; |
---|
131 | for ( i = MAXPT; i < n; i++ ) |
---|
132 | result *= xa; |
---|
133 | return result; |
---|
134 | } |
---|
135 | } |
---|
136 | } |
---|
137 | |
---|