source: git/factory/cf_binom.cc @ 362fc67

spielwiese
Last change on this file since 362fc67 was 362fc67, checked in by Martin Lee <martinlee84@…>, 12 years ago
chg: remove $Id$
  • Property mode set to 100644
File size: 3.3 KB
Line 
1/* emacs edit mode for this file is -*- C++ -*- */
2
3#include "config.h"
4
5#include "cf_assert.h"
6
7#include "cf_defs.h"
8#include "canonicalform.h"
9#include "cf_binom.h"
10
11#define MAXPT 40
12
13#define INITPT 10
14
15CFArray * ptZ = 0;
16CFArray * ptF = 0;
17
18int charac = 0;
19int gfdeg = 0;
20int ptZmax = INITPT;
21int ptFmax = 0;
22
23void
24resetFPT()
25{
26    ptFmax = 0;
27}
28
29void
30initPT ( )
31{
32    static bool initialized = false;
33
34    if ( ! initialized ) {
35        initialized = true;
36        ptZ = new CFArray[MAXPT+1];
37        ptF = new CFArray[MAXPT+1];
38        int i, j;
39        ptZ[0] = CFArray(1); ptZ[0][0] = 1;
40        ptF[0] = CFArray(1);
41        for ( i = 1; i <= INITPT; i++ ) {
42            ptF[i] = CFArray(i+1);
43            ptZ[i] = CFArray(i+1);
44            (ptZ[i])[0] = 1;
45            for ( j = 1; j < i; j++ )
46                (ptZ[i])[j] = (ptZ[i-1])[j-1] + (ptZ[i-1])[j];
47            (ptZ[i])[i] = 1;
48        }
49        for ( i = INITPT+1; i <= MAXPT; i++ ) {
50            ptF[i] = CFArray(i+1);
51            ptZ[i] = CFArray(i+1);
52        }
53        ptZmax = INITPT;
54        ptFmax = 0;
55    }
56}
57
58CanonicalForm
59binomialpower ( const Variable & x, const CanonicalForm & a, int n )
60{
61    if ( n == 0 )
62        return 1;
63    else if ( n == 1 )
64        return x + a;
65    else if ( getCharacteristic() == 0 ) {
66        if ( n <= MAXPT ) {
67            if ( n > ptZmax ) {
68                int i, j;
69                for ( i = ptZmax+1; i <= n; i++ ) {
70                    (ptZ[i])[0] = 1;
71                    for ( j = 1; j < i; j++ )
72                        (ptZ[i])[j] = (ptZ[i-1])[j-1] + (ptZ[i-1])[j];
73                    (ptZ[i])[i] = 1;
74                }
75                ptZmax = n;
76            }
77            CanonicalForm result = 0, apower = 1;
78            int k;
79            for ( k = n; k >= 0; k-- ) {
80                result += power( x, k ) * apower * (ptZ[n])[k];
81                if ( k != 0 )
82                    apower *= a;
83            }
84            return result;
85        }
86        else {
87            CanonicalForm result = binomialpower( x, a, MAXPT );
88            CanonicalForm xa = x + a;
89            int i;
90            for ( i = MAXPT; i < n; i++ )
91                result *= xa;
92            return result;
93        }
94    }
95    else {
96        if ( getCharacteristic() != charac || gfdeg != getGFDegree() ) {
97            ptFmax = 0;
98            charac = getCharacteristic();
99            gfdeg = getGFDegree();
100            (ptF[0])[0] = 1;
101        }
102        if ( n <= MAXPT ) {
103            if ( n > ptFmax ) {
104                int i, j;
105                for ( i = ptFmax+1; i <= n; i++ ) {
106                    (ptF[i])[0] = 1;
107                    for ( j = 1; j < i; j++ )
108                        (ptF[i])[j] = (ptF[i-1])[j-1] + (ptF[i-1])[j];
109                    (ptF[i])[i] = 1;
110                }
111                ptFmax = n;
112            }
113            CanonicalForm result = 0, apower = 1;
114            int k;
115            for ( k = n; k >= 0; k-- ) {
116                result += power( x, k ) * apower * (ptF[n])[k];
117                if ( k != 0 )
118                    apower *= a;
119            }
120            return result;
121        }
122        else {
123            CanonicalForm result = binomialpower( x, a, MAXPT );
124            CanonicalForm xa = x + a;
125            int i;
126            for ( i = MAXPT; i < n; i++ )
127                result *= xa;
128            return result;
129        }
130    }
131}
Note: See TracBrowser for help on using the repository browser.