~ubuntu-branches/debian/squeeze/maxima/squeeze

« back to all changes in this revision

Viewing changes to share/contrib/Zeilberger/Gosper.mac

  • Committer: Bazaar Package Importer
  • Author(s): Camm Maguire
  • Date: 2006-10-18 14:52:42 UTC
  • mto: (1.1.5 upstream)
  • mto: This revision was merged to the branch mainline in revision 4.
  • Revision ID: james.westby@ubuntu.com-20061018145242-vzyrm5hmxr8kiosf
ImportĀ upstreamĀ versionĀ 5.10.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* Gosper's algorithm        */
 
2
 
 
3
/* RISC Institute, Linz, Austria */
 
4
/* by Fabrizio Caruso            */
 
5
 
 
6
 
 
7
/* Dependencies:             */
 
8
/* makeGosperForm2.macsyma   */
 
9
/*      poly2quint.macsyma   */
 
10
/* GosperEq2.macsyma         */
 
11
/*      AlgUtil2.macsyma     */
 
12
 
 
13
 
 
14
 
 
15
/* PREPROCESSING ROUTINES */
 
16
 
 
17
 
 
18
/* Preprocessing function "fRat" */
 
19
simpleFRatSlow(f,k) :=
 
20
  minfactorial(
 
21
  factor((subst(k+1,k,f)/f)));
 
22
 
 
23
simpleFRatFast(f,k) :=
 
24
  subst(k+1,k,f)/f;
 
25
 
 
26
simpleFRat(f,k) := simpleFRatFast(f,k);
 
27
 
 
28
 
 
29
fRatSlow(f,k) :=
 
30
  minfactorial(
 
31
  factor(expand(num(xthru(minfactorial(makefact(simpleFratSlow(f,k))))))/
 
32
         expand(denom(xthru(minfactorial(makefact(simpleFratSlow(f,k))))))));
 
33
 
 
34
 
 
35
fratFast(f,k) :=
 
36
  factor(expand(num(xthru(minfactorial(makefact(simpleFratFast(f,k))))))/
 
37
         expand(denom(xthru(minfactorial(makefact(simpleFratFast(f,k)))))));
 
38
 
 
39
fratVeryFast(f,k) :=
 
40
  factor(expand(num(xthru(minfactorial(makefact(simpleFratFast(f,k))))))/
 
41
         expand(denom(xthru(minfactorial(makefact(simpleFratFast(f,k)))))));
 
42
 
 
43
 
 
44
 
 
45
fRat(f,k) := fRatFast(f,k);
 
46
 
 
47
 
 
48
/* Postprocessing function that computes the rational function "certificate" */
 
49
telescope(Gsol,p,r,k) :=
 
50
   r*Gsol/p;
 
51
 
 
52
/* Postprocessing function that computes the rational function "certificate" */
 
53
telescopeSimplified(Gsol,p,r,k) :=
 
54
   r*Gsol/factor(p);
 
55
 
 
56
 
 
57
/* Postprocessing function that computes the rational function "certificate"   */
 
58
/* This is correct because we are telescoping w.r.t. a linear operator applied */
 
59
/* to the original function f , i.e. lin.op.(f) = (numNF/denNF)*f              */
 
60
/* We telescoping solution is (r/p)*Gsol*(numNF/denNF)*f =                     */
 
61
/* = (r/gfP*numNF)*Gsol*(numNF/denNF) = (r*GSol)/(gfP*denNF)                   */
 
62
zTelescope(GSol,gfP,r,denNF) :=
 
63
   r*GSol/(gfP*denNF);
 
64
 
 
65
/* Postprocessing function that computes the rational function "certificate" */
 
66
zTelescopeSimplified(GSol,gfP,r,denNF) :=
 
67
   r*Gsol/(gfP*denNF); /* BOGUS */
 
68
 
 
69
 
 
70
printGosperSummary(fRat,GosperForm,AnsatzDegree,Gpoly,GosperSol,mode) := 
 
71
  block([],
 
72
     print("--------------------------------"),
 
73
     print("Gosper-related summary"),
 
74
     print("(1) fRat : " , fRat),
 
75
     print("(2) Gosper form : ", GosperForm),
 
76
     print("(3) AnsatzDegree : ", AnsatzDegree),
 
77
     print("(4) Gosper polynomial : " , Gpoly),
 
78
     if mode>= verbose then
 
79
       print("(5) Gosper solution : ", GosperSol)
 
80
     );
 
81
 
 
82
 
 
83
 
 
84
/* GOSPER'S ALGORITHM - Main routine - verbose */
 
85
GosperVerboseOpt(f,k,mode) :=
 
86
  block(
 
87
  [fRat,
 
88
   signFlag,
 
89
   GosperForm,
 
90
   p,q,r,
 
91
   AnsatzDegree,
 
92
   Gpoly,
 
93
   sysSol,
 
94
   GosperSol,
 
95
   tlscope,
 
96
   constantPart],
 
97
  
 
98
 
 
99
 
 
100
  /* Computation of the shift quotient */
 
101
  fRat : shiftQuoOnlyHyp(factor(f),k), /* factor might not be necessary */
 
102
  if fRat = [] then
 
103
    (
 
104
    print(f, " is not hypergeometric in ", k),
 
105
    return(NON_HYPERGEOMETRIC)
 
106
    ),
 
107
 
 
108
  if mode>=verbose then
 
109
     print("Shift quotient computed! : " , fRat),
 
110
 
 
111
 
 
112
  /* Computation of the Gosper form */
 
113
  GosperForm: makeGosperFormVerboseOpt(factor(fRat),k,mode-1), /* factor ? */
 
114
 
 
115
  if GosperForm = NON_HYPERGEOMETRIC then
 
116
    (
 
117
    print(f, " is not proper hypergeometric in ", k),
 
118
    return(NON_HYPERGEOMETRIC)
 
119
    ),
 
120
 
 
121
  p: takeP(GosperForm),
 
122
  q: takeQ(GosperForm),
 
123
  r: takeR(GosperForm),
 
124
  if mode>=verbose then
 
125
     print("p,q,r have been extracted! p: ", p, ", q: ", q, ", r: ", r),
 
126
 
 
127
  /* Solution of the Gosper equation */
 
128
  AnsatzDegree: AnsatzDegreeVerboseOpt(p,q,r,k,mode-1),
 
129
 
 
130
  if mode>=verbose then
 
131
     print("Ansatz degree computed ! : " , AnsatzDegree),
 
132
 
 
133
  Gpoly : 0,
 
134
 
 
135
  if AnsatzDegree >= 0 then
 
136
    Gpoly: expand(GosperPoly(p,q,r,k,"c",AnsatzDegree)),
 
137
  if mode>=verbose then 
 
138
     print("Gosper polynomial created! : " , Gpoly),
 
139
  if Gpoly = 0 then
 
140
     return(NO_HYP_SOL),
 
141
  
 
142
  sysSol: SolveSysVerboseOpt(Gpoly,"c",AnsatzDegree+1,k,mode-1),
 
143
  
 
144
  if mode>=very_verbose then
 
145
     print("System of equations solved! :  " , sysSol),
 
146
  GosperSol: Gsol2Poly(first(sysSol),k),  
 
147
  if mode>=verbose then
 
148
     print("Gosper solution created! : " , GosperSol),
 
149
 
 
150
  if mode>=summary then 
 
151
     printGosperSummary(fRat,GosperForm,AnsatzDegree,Gpoly,GosperSol,mode-1),
 
152
 
 
153
 
 
154
  /* Postprocessing : building the sol. to the telesc. probl. */    
 
155
  if GosperSol = 0 then 
 
156
    tlscope: NO_HYP_SOL
 
157
  else
 
158
    tlscope: telescope(GosperSol,p,r,k),
 
159
 
 
160
 
 
161
 if simplified_output then
 
162
    return(ratsimp(tlscope))
 
163
 else
 
164
    return(tlscope)
 
165
  );
 
166
 
 
167
 
 
168
/* Macro for the normal verbose Gosper algorithm */
 
169
GosperVerbose(f,k) ::=
 
170
  buildq([f,k],
 
171
         GosperVerboseOpt(f,k,verbose));
 
172
 
 
173
/* Macro for the very verbose Gosper algorithm */
 
174
GosperVeryVerbose(f,k) ::=
 
175
  buildq([f,k],
 
176
         GosperVerboseOpt(f,k,very_verbose));
 
177
 
 
178
 
 
179
GosperExtra(f,k) ::=
 
180
  buildq([f,k],
 
181
         GosperVerboseOpt(f,k,extra));
 
182
 
 
183
/* Macro for the summary version of the Gosper algorithm */
 
184
GosperSummary(f,k) ::=
 
185
  buildq([f,k],
 
186
         GosperVerboseOpt(f,k,summary));
 
187
 
 
188
 
 
189
/* Short name for the non verbose mode */
 
190
Gosper(f,k) ::=
 
191
   buildq([f,k],
 
192
          GosperVerboseOpt(f,k,nonverbose));
 
193
 
 
194
/* ----------------------------------------------------------------------- */
 
195
/* Anti-difference : It finds the anti-difference of a hypergeometric term */
 
196
 
 
197
AntiDifferenceVerboseOpt(f,k,mode) :=
 
198
  block([gSol],
 
199
  gSol : GosperVerboseOpt(f,k,mode),
 
200
  if gSol=NO_HYP_SOL then
 
201
    return(NO_HYP_ANTIDIFFERENCE)
 
202
  else
 
203
    if gSol=NON_HYPERGEOMETRIC then
 
204
      return(NON_HYPERGEOMETRIC)
 
205
    else
 
206
      if simplified_output then
 
207
         return(factor(f*gSol))
 
208
      else
 
209
         return(f*gSol)
 
210
  );
 
211
       
 
212
 
 
213
AntiDifference(f,k) :=
 
214
  AntiDifferenceVerboseOpt(f,k,nonverbose);
 
215
 
 
216
AntiDifferenceSummary(f,k) :=
 
217
  AntiDifferenceVerboseOpt(f,k,summary);
 
218
 
 
219
AntiDifferenceVerbose(f,k) :=
 
220
  AntiDifferenceVerboseOpt(f,k,verbose);
 
221
 
 
222
AntiDifferenceVeryVerbose(f,k) :=
 
223
  AntiDifferenceVerboseOpt(f,k,very_verbose);
 
224
 
 
225
AntiDifferenceExtra(f,k) :=
 
226
  AntiDifferenceVerboseOpt(f,k,extra);
 
227
 
 
228
 
 
229
/* ------------------------------------------------------------------ */
 
230
/* Gosper Sum : It sums Gosper-summable hypergeometric terms          */
 
231
 
 
232
/* Gosper summation function - Summary mode */
 
233
GosperSumVerboseOpt(f,k,a,b,mode) :=
 
234
  block(
 
235
        [antiDiff],
 
236
        
 
237
        antiDiff:AntiDifferenceVerboseOpt(f,k,mode),
 
238
        if antiDiff=NO_HYP_ANTIDIFFERENCE then
 
239
           return(NON_GOSPER_SUMMABLE)
 
240
        else
 
241
          if antiDiff=NON_HYPERGEOMETRIC then
 
242
             return(NON_HYPERGEOMETRIC)
 
243
          else
 
244
            (
 
245
            if mode>= verbose then              
 
246
              print("Anti-difference : ", antiDiff),
 
247
            return(subst(b+1,k,antiDiff)-subst(a,k,antiDiff))
 
248
            )
 
249
       );
 
250
 
 
251
/* Gosper summation function */
 
252
GosperSum(f,k,a,b) :=
 
253
  GosperSumVerboseOpt(f,k,a,b,nonverbose);
 
254
 
 
255
 
 
256
/* Gosper summation function - Summary mode */
 
257
GosperSumSummary(f,k,a,b) :=
 
258
  GosperSumVerboseOpt(f,k,a,b,summary);
 
259
 
 
260
GosperSumVerbose(f,k,a,b) :=
 
261
  GosperSumVerboseOpt(f,k,a,b,verbose);
 
262
 
 
263
GosperSumVeryVerbose(f,k,a,b) :=
 
264
  GosperSumVerboseOpt(f,k,a,b,very_verbose);
 
265
 
 
266
GosperSumExtra(f,k,a,b) :=
 
267
  GosperSumVerboseOpt(f,k,a,b,extra);
 
268
 
 
269
 
 
270
 
 
271
 
 
272
 
 
273
 
 
274
 
 
275
 
 
276
 
 
277
 
 
278
 
 
279
 
 
280
 
 
281
 
 
282
 
 
283
 
 
284
 
 
285
 
 
286