-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmulti_float.c
More file actions
367 lines (313 loc) · 8.84 KB
/
multi_float.c
File metadata and controls
367 lines (313 loc) · 8.84 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
/********************************************************************************
* *
* The purpose of this file is basic manipulation of power series. These are infinite *
* series as opposed to multivariate polynomials of finite degree. I use the same memory *
* allocation and all degrees start at zero, so offset to a block corresponds to coefficient of x to *
* that power. *
* Algorithms from Knuth page 506 (section 4.7) *
* *
* Author = Mike Rosing *
* date = April 11, 2000 *
* *
********************************************************************************/
#include "bigfloat.h"
#include "multipoly.h"
extern RAMDATA ram_block[];
/* add two power series. C = A + B
Creates new output space. takes care of memory management,
but ASSUMES A and B were previously allocated.
Returns 1 on success, 0 on failure to create C.
*/
int mbf_power_add( MULTIPOLY A, MULTIPOLY B, MULTIPOLY *C)
{
ELEMENT i, j, big, small;
MULTIPOLY result;
FLOAT *Result, *Aptr, *Bptr;
/* find out which polynomial is larger and allocate that much space */
if (A.degree > B.degree)
{
small = B.degree;
big = A.degree;
}
else
{
small = A.degree;
big = B.degree;
}
result.degree = big;
if (!mbf_get_space( &result)) return 0;
/* now add the shorter length amounts together */
Result = Address(result);
Aptr = Address( A);
Bptr = Address( B);
for( i=0; i<= small; i++)
{
mbf_add( Aptr, Bptr, Result);
Result++;
Aptr++;
Bptr++;
}
if( A.degree == big)
mbf_multi_copy( big - small, Aptr, Result);
else
mbf_multi_copy( big - small, Bptr, Result);
/* take care of memory management */
if ( A.memdex == C->memdex ) mbf_free_space( &A);
if ( B.memdex == C->memdex ) mbf_free_space( &B);
C->degree = result.degree;
C->memdex = result.memdex;
return 1;
}
/* Multiply two power series. Uses Knuth's construction
from page 506. of Semi Numerical Algorithms (sect. 4.7)
computes C = A*B
returns 0 if C can't be allocated.
*/
int mbf_power_mul( MULTIPOLY A, MULTIPOLY B, MULTIPOLY *C)
{
ELEMENT i, k;
MULTIPOLY result;
FLOAT *Aptr, *Bptr, *Result;
FLOAT temp;
/* create space for result */
if (A.degree > B.degree)
result.degree = A.degree;
else
result.degree = B.degree;
if ( !mbf_get_space( &result) ) return 0;
for( i=0; i<=result.degree; i++)
{
Result = Address(result) + i;
mbf_null( Result);
for( k=0; k<=i; k++)
{
if( k <= A.degree) Aptr = Address( A) + k;
else continue;
if( i-k < B.degree) Bptr = Address( B) + i - k;
else continue;
mbf_multiply( Aptr, Bptr, &temp);
mbf_add( &temp, Result, Result);
}
}
/* take care of memory management */
if (A.memdex == C->memdex) mbf_free_space( &A);
if (B.memdex == C->memdex) mbf_free_space( &B);
C->memdex = result.memdex;
C->degree = result.degree;
return 1;
}
/* Subroutine to test if a FLOAT value is 0.
returns 0 if all bits in field are 0, first non-zero ELEMENT
otherwise.
*/
ELEMENT mbf_zero_check( FLOAT *z)
{
INDEX i;
if( z->expnt) return z->expnt;
for( i= MS_MNTSA; i >= 0; i++)
if( z->mntsa.e[i] ) return z->mntsa.e[i];
return 0;
}
/* Power series division. Resulting degree is minimum
degree of two source polynomials.
C = A/B
returns 1 if ok, 0 if no space for C
*/
int mbf_power_div( MULTIPOLY A, MULTIPOLY B, MULTIPOLY *C)
{
ELEMENT n, k;
FLOAT temp, *Result, *Aptr, *Bptr;
MULTIPOLY result;
/* check to see if attempting to divide by 0 */
Bptr = Address( B);
if (!mbf_zero_check(Bptr)) return (0);
/* create space for result */
if( A.degree < B.degree) result.degree = A.degree;
else result.degree = B.degree;
if( !mbf_get_space(&result)) return 0;
/* do first term */
Aptr = Address(A);
Bptr = Address( B);
Result = Address( result);
mbf_divide( Aptr, Bptr, Result);
/* do rest of terms */
for( n=1; n<= result.degree; n++)
{
Result = Address( result) + n;
for( k=0; k<n; k++)
{
Aptr = Address( result) +k;
Bptr = Address( B) + n - k;
mbf_multiply( Aptr, Bptr, &temp);
mbf_add( &temp, Result, Result);
}
Aptr = Address( A) + n;
mbf_subtract( Aptr, Result, Result);
Bptr = Address( B);
mbf_divide( Result, Bptr, Result);
}
/* take care of memory management */
if (A.memdex == C->memdex) mbf_free_space( &A);
if (B.memdex == C->memdex) mbf_free_space( &B);
C->memdex = result.memdex;
C->degree = result.degree;
return 1;
}
/* NOTE: the following routines work with *polynomials*, which are similar
to but not the same as *power series* above. The main difference is
that in a power series we don't care about higher order terms and let
them fall off the end because they are too small. With polynomials
we keep every term and assume things get larger.
*/
/* add two multivariate polynomials. C = A + B
Creates new output space. takes care of memory management,
but ASSUMES A and B were previously allocated.
Returns 1 on success, 0 on failure to create C.
*/
int mbf_multi_add( MULTIPOLY A, MULTIPOLY B, MULTIPOLY *C)
{
ELEMENT i, j;
MULTIPOLY shortpoly, result, longpoly;
FLOAT *Result, *Aptr, *Bptr;
/* find out which polynomial is larger and allocate that much space */
if (A.degree > B.degree)
{
shortpoly.degree = B.degree;
shortpoly.memdex = B.memdex;
longpoly.degree = A.degree;
longpoly.memdex = A.memdex;
}
else
{
shortpoly.degree = A.degree;
shortpoly.memdex = A.memdex;
longpoly.degree = B.degree;
longpoly.memdex = B.memdex;
}
result.degree = longpoly.degree;
if (!mbf_get_space( &result)) return 0;
/* now add the shorter length amounts together */
Result = Address(result);
Aptr = Address( A);
Bptr = Address( B);
for( i=0; i<= shortpoly.degree; i++)
{
mbf_add( Aptr, Bptr, Result);
Result++;
Aptr++;
Bptr++;
}
/* and copy the rest */
Result = Address(result) + shortpoly.degree + 1;
Aptr = Address(longpoly) + shortpoly.degree + 1;
mbf_multi_copy( longpoly.degree - shortpoly.degree, Aptr, Result);
Result = Address(result);
while( result.degree)
if( !mbf_zero_check( &Result[result.degree])) result.degree--;
else break;
/* take care of memory management */
if ( A.memdex == C->memdex ) mbf_free_space( &A);
if ( B.memdex == C->memdex ) mbf_free_space( &B);
C->degree = result.degree;
C->memdex = result.memdex;
return 1;
}
/* subtract two multivariate polynomials. C = A - B
Negate all components of B and then call add.
Simplest way to deal with it.
*/
int mbf_multi_sub( MULTIPOLY A, MULTIPOLY B, MULTIPOLY *C)
{
INDEX i;
FLOAT *ptr;
MULTIPOLY temp;
mbf_multi_dup( B, &temp);
for( i=0; i<=temp.degree; i++)
{
ptr = Address(temp) + i;
mbf_negate( ptr);
}
mbf_multi_add( A, temp, C);
mbf_free_space( &temp);
}
/* Multiply two multivariate polynomials. Uses Knuth's construction
from page 399 of Semi Numerical Algorithms (sect. 4.6)
computes C = A*B
returns 0 if C can't be allocated.
*/
int mbf_multi_mul( MULTIPOLY A, MULTIPOLY B, MULTIPOLY *C)
{
ELEMENT i, j, k;
MULTIPOLY shortmulti, longmulti, result;
FLOAT *Short, *Long, *Result;
FLOAT temp;
/* create space for result using sum of degrees of source polynomials */
result.degree = A.degree + B.degree;
if ( !mbf_get_space( &result) ) return 0;
if (A.degree > B.degree)
{
longmulti.memdex = A.memdex;
longmulti.degree = A.degree;
shortmulti.memdex = B.memdex;
shortmulti.degree = B.degree;
}
else
{
longmulti.memdex = B.memdex;
longmulti.degree = B.degree;
shortmulti.memdex = A.memdex;
shortmulti.degree = A.degree;
}
for( k=0; k<shortmulti.degree; k++)
{
Result = Address(result) + k;
mbf_null( Result);
for( i=0; i<=k; i++)
{
j = k - i;
Short = Address( shortmulti) + i;
Long = Address( longmulti) + j;
mbf_multiply( Short, Long, &temp);
Result = Address( result) + k;
mbf_add( Result, &temp, Result);
}
}
for( k=shortmulti.degree; k<longmulti.degree; k++)
{
Result = Address(result) + k;
mbf_null( Result);
for( i=0; i<=shortmulti.degree; i++)
{
j = k - i;
Short = Address(shortmulti) + i;
Long = Address( longmulti) + j;
mbf_multiply( Short, Long, &temp);
Result = Address( result) + k;
mbf_add( Result, &temp, Result);
}
}
for( k=longmulti.degree; k<=result.degree; k++)
{
Result = Address( result) + k;
mbf_null( Result);
for( i = k-longmulti.degree; i <= shortmulti.degree; i++)
{
j = k - i;
Short = Address( shortmulti) + i;
Long = Address( longmulti) + j;
mbf_multiply( Short, Long, &temp);
Result = Address( result) + k;
mbf_add( Result, &temp, Result);
}
}
Result = Address( result);
while( result.degree)
if( !mbf_zero_check( &Result[result.degree])) result.degree--;
else break;
/* take care of memory management */
if (A.memdex == C->memdex) mbf_free_space( &A);
if (B.memdex == C->memdex) mbf_free_space( &B);
C->memdex = result.memdex;
C->degree = result.degree;
return 1;
}