Ian Jauslin
summaryrefslogtreecommitdiff
blob: 1b4137d4c1f0d14a899c5271c61edbf8975bf6d4 (plain)
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
/*
Copyright 2016 Ian Jauslin

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

/*
  Base functions for real polynomials

  see polynomial_*.h for the values taken by POLYNOMIAL_FUNC, etc...
*/


// init
int POLYNOMIAL_FUNC(init) (POLYNOMIAL_TYPENAME* poly, unsigned int memory){
  POLYNOMIAL_COEFSARRAY_FUNC(init) (&(poly->coefficients), memory);
  POLYNOMIAL_ORDERSARRAY_FUNC(init) (&(poly->orders), memory);
  return(0);
}
int POLYNOMIAL_FUNC(free) (POLYNOMIAL_TYPENAME poly){
  POLYNOMIAL_COEFSARRAY_FUNC(free) (poly.coefficients);
  POLYNOMIAL_ORDERSARRAY_FUNC(free) (poly.orders);
  return(0);
}

// resize memory
int POLYNOMIAL_FUNC(resize) (POLYNOMIAL_TYPENAME* poly, unsigned int newsize){
  POLYNOMIAL_COEFSARRAY_FUNC(resize) (&(poly->coefficients), newsize);
  POLYNOMIAL_ORDERSARRAY_FUNC(resize) (&(poly->orders), newsize);
  return(0);
}

// add a monomial
int POLYNOMIAL_FUNC(add_monomial) (POLYNOMIAL_COEF_TYPE val, POLYNOMIAL_ORDER_TYPE order, POLYNOMIAL_TYPENAME* output){
  unsigned int i;
  if((output->coefficients.length != output->orders.length) || (output->coefficients.memory != output->orders.memory)){
    return(LIBINUM_ERROR_SIZE_MISMATCH);
  }
  // check whether the order already exists in the polynomial
  for(i=0;i< output->coefficients.length;i++){
    if(POLYNOMIAL_ORDER_CMP(order, output->orders.values[i])==0){
      POLYNOMIAL_COEF_ADD(output->coefficients.values[i], output->coefficients.values[i], val);
      return(0);
    }
  }
  // if the order does not already exist
  if(output->coefficients.length >= output->coefficients.memory){
    POLYNOMIAL_FUNC(resize) (output,2*output->coefficients.memory+1);
  }
  POLYNOMIAL_COEF_CPY(val, output->coefficients.values[output->coefficients.length]);
  POLYNOMIAL_ORDER_CPY(order, output->orders.values[output->coefficients.length]);
  output->coefficients.length++;
  output->orders.length++;
  return(0);
}
// from a double-valued coefficient and an unsigned int order
int POLYNOMIAL_FUNC(add_monomial_dui) (double val, unsigned int order, POLYNOMIAL_TYPENAME* output){
  unsigned int i;
  if((output->coefficients.length != output->orders.length) || (output->coefficients.memory != output->orders.memory)){
    return(LIBINUM_ERROR_SIZE_MISMATCH);
  }
  // check whether the order already exists in the polynomial
  for(i=0;i< output->coefficients.length;i++){
    if(POLYNOMIAL_ORDER_CMP_UI(output->orders.values[i], order)==0){
      POLYNOMIAL_COEF_ADD_D(output->coefficients.values[i], output->coefficients.values[i], val);
      return(0);
    }
  }
  // if the order does not already exist
  if(output->coefficients.length >= output->coefficients.memory){
    POLYNOMIAL_FUNC(resize) (output,2*output->coefficients.memory+1);
  }
  POLYNOMIAL_COEF_CPY_D(val, output->coefficients.values[output->coefficients.length]);
  POLYNOMIAL_ORDER_CPY_UI(order, output->orders.values[output->coefficients.length]);
  output->coefficients.length++;
  output->orders.length++;
  return(0);
}

// copy
int POLYNOMIAL_FUNC(cpy) (POLYNOMIAL_TYPENAME input, POLYNOMIAL_TYPENAME* output){
  POLYNOMIAL_COEFSARRAY_FUNC(cpy) (input.coefficients, &(output->coefficients));
  POLYNOMIAL_ORDERSARRAY_FUNC(cpy) (input.orders, &(output->orders));
  return(0);
}
int POLYNOMIAL_FUNC(cpy_noinit) (POLYNOMIAL_TYPENAME input, POLYNOMIAL_TYPENAME* output){
  int ret;
  ret=POLYNOMIAL_COEFSARRAY_FUNC(cpy_noinit) (input.coefficients, &(output->coefficients));
  if(ret<0){
    return(ret);
  }
  POLYNOMIAL_ORDERSARRAY_FUNC(cpy_noinit) (input.orders, &(output->orders));
  return(ret);
}

// add
int POLYNOMIAL_FUNC(add_inplace) (POLYNOMIAL_TYPENAME poly1, POLYNOMIAL_TYPENAME* poly2){
  unsigned int i;
  if(poly1.coefficients.length != poly1.orders.length){
    return(LIBINUM_ERROR_SIZE_MISMATCH);
  }
  for(i=0;i<poly1.coefficients.length;i++){
    POLYNOMIAL_FUNC(add_monomial) (poly1.coefficients.values[i], poly1.orders.values[i], poly2);
  }
  return(0);
}
int POLYNOMIAL_FUNC(add) (POLYNOMIAL_TYPENAME poly1, POLYNOMIAL_TYPENAME poly2, POLYNOMIAL_TYPENAME* output){
  int ret;
  POLYNOMIAL_FUNC(cpy) (poly2, output);
  ret=POLYNOMIAL_FUNC(add_inplace) (poly1, output);
  return(ret);
}

// multiply by a scalar
int POLYNOMIAL_FUNC(mul_scalar_inplace) (POLYNOMIAL_COEF_TYPE x, POLYNOMIAL_TYPENAME* poly){
  unsigned int i;
  if(poly->coefficients.length != poly->orders.length){
    return(LIBINUM_ERROR_SIZE_MISMATCH);
  }
  for(i=0;i<poly->coefficients.length;i++){
    POLYNOMIAL_COEF_MUL(poly->coefficients.values[i], poly->coefficients.values[i], x);
  }
  return(0);
}
int POLYNOMIAL_FUNC(mul_scalar) (POLYNOMIAL_COEF_TYPE x, POLYNOMIAL_TYPENAME poly, POLYNOMIAL_TYPENAME* output){
  int ret;
  POLYNOMIAL_FUNC(cpy) (poly, output);
  ret=POLYNOMIAL_FUNC(mul_scalar_inplace) (x, output);
  return(ret);
}

// multiply by a monomial
int POLYNOMIAL_FUNC(mul_monomial_inplace) (POLYNOMIAL_COEF_TYPE x, POLYNOMIAL_ORDER_TYPE order, POLYNOMIAL_TYPENAME* poly){
  unsigned int i;
  if(poly->coefficients.length != poly->orders.length){
    return(LIBINUM_ERROR_SIZE_MISMATCH);
  }
  for(i=0;i<poly->coefficients.length;i++){
    POLYNOMIAL_COEF_MUL(poly->coefficients.values[i], poly->coefficients.values[i], x);
    POLYNOMIAL_ORDER_ADD(poly->orders.values[i], poly->orders.values[i], order);
  }
  return(0);
}
int POLYNOMIAL_FUNC(mul_monomial) (POLYNOMIAL_COEF_TYPE x, POLYNOMIAL_ORDER_TYPE order, POLYNOMIAL_TYPENAME poly, POLYNOMIAL_TYPENAME* output){
  int ret;
  POLYNOMIAL_FUNC(cpy) (poly,output);
  ret=POLYNOMIAL_FUNC(mul_monomial_inplace) (x, order, output);
  return(ret);
}

// multiply two polynomials
int POLYNOMIAL_FUNC(mul_inplace) (POLYNOMIAL_TYPENAME poly1, POLYNOMIAL_TYPENAME* poly2){
  unsigned int i;
  int ret;
  if(poly1.coefficients.length != poly1.orders.length){
    return(LIBINUM_ERROR_SIZE_MISMATCH);
  }
  for(i=0;i<poly1.coefficients.length;i++){
    ret=POLYNOMIAL_FUNC(mul_monomial_inplace) (poly1.coefficients.values[i], poly1.orders.values[i], poly2);
    if(ret<0){
      return(ret);
    }
  }
  return(0);
}
int POLYNOMIAL_FUNC(mul) (POLYNOMIAL_TYPENAME poly1, POLYNOMIAL_TYPENAME poly2, POLYNOMIAL_TYPENAME* output){
  int ret;
  POLYNOMIAL_FUNC(cpy) (poly2, output);
  ret=POLYNOMIAL_FUNC(mul_inplace) (poly1, output);
  return(ret);
}

// derive
int POLYNOMIAL_FUNC(derive_inplace) (POLYNOMIAL_TYPENAME* poly){
  unsigned int i;
  if(poly->coefficients.length != poly->orders.length){
    return(LIBINUM_ERROR_SIZE_MISMATCH);
  }
  for(i=0;i<poly->coefficients.length;i++){
    if(POLYNOMIAL_ORDER_CMP_UI(poly->orders.values[i], 0)>0){
      POLYNOMIAL_COEF_MUL_ORDER(poly->coefficients.values[i], poly->coefficients.values[i], poly->orders.values[i]);
      POLYNOMIAL_ORDER_SUB_UI(poly->orders.values[i], poly->orders.values[i], 1);
    }
    else{
      // remove the term by setting it to the last term and reducing the length
      #ifdef POLYNOMIAL_COEF_FREE
	POLYNOMIAL_COEF_FREE(poly->coefficients.values[i]);
      #endif
      #ifdef POLYNOMIAL_ORDER_FREE
	POLYNOMIAL_ORDER_FREE(poly->orders.values[i]);
      #endif
      if(i<poly->coefficients.length-1){
	POLYNOMIAL_COEF_SET(poly->coefficients.values[i], poly->coefficients.values[poly->coefficients.length-1]);
	POLYNOMIAL_ORDER_SET(poly->orders.values[i], poly->orders.values[poly->coefficients.length-1]);
	i--;
      }
      poly->coefficients.length--;
      poly->orders.length--;
    }
  }
  return(0);
}
int POLYNOMIAL_FUNC(derive) (POLYNOMIAL_TYPENAME poly, POLYNOMIAL_TYPENAME* output){
  int ret;
  POLYNOMIAL_FUNC(cpy) (poly, output);
  ret=POLYNOMIAL_FUNC(derive_inplace) (output);
  return(ret);
}

// evaluate
int POLYNOMIAL_FUNC(evaluate) (POLYNOMIAL_COEF_TYPE* out, POLYNOMIAL_COEF_TYPE x, POLYNOMIAL_TYPENAME poly){
  unsigned int i;
  POLYNOMIAL_COEF_TYPE tmp;

  #ifdef POLYNOMIAL_COEF_INIT
    POLYNOMIAL_COEF_INIT(tmp);
  #endif

  if(poly.coefficients.length != poly.orders.length){
    return(LIBINUM_ERROR_SIZE_MISMATCH);
  }

  POLYNOMIAL_COEF_SET_UI(*out, 0);
  for(i=0;i<poly.coefficients.length;i++){
    POLYNOMIAL_COEF_POW_UI(tmp, x, poly.orders.values[i]);
    POLYNOMIAL_COEF_MUL(tmp, tmp, poly.coefficients.values[i]);
    POLYNOMIAL_COEF_ADD(*out, *out, tmp);
  }
  #ifdef POLYNOMIAL_COEF_FREE
    POLYNOMIAL_COEF_FREE(tmp);
  #endif
  return(0);
}

// print
int POLYNOMIAL_FUNC(print) (POLYNOMIAL_TYPENAME poly){
  unsigned int j;

  if(poly.coefficients.length != poly.orders.length){
    return(LIBINUM_ERROR_SIZE_MISMATCH);
  }

  printf("  ");
  for(j=0;j<poly.coefficients.length;j++){
    if(j>0){
      printf("+ ");
    }
    POLYNOMIAL_COEF_PRINT(poly.coefficients.values[j]);
    printf(" x^");
    POLYNOMIAL_ORDER_PRINT(poly.orders.values[j]);
    printf("\n");
  }

  return(0);
}


// n-th Legendre polynomial
int POLYNOMIAL_FUNC(legendre) (unsigned int n, POLYNOMIAL_TYPENAME* output){
  POLYNOMIAL_TYPENAME prevpoly, tmppoly;
  unsigned int i;
  int j;
  POLYNOMIAL_COEF_TYPE tmp;
  POLYNOMIAL_ORDER_TYPE tmp_o;
  
  if(n==0){
    POLYNOMIAL_FUNC(init) (output, 1);
    POLYNOMIAL_FUNC(add_monomial_dui) (1., 0, output);
    return(0);
  }
  else if(n==1){
    POLYNOMIAL_FUNC(init) (output, n);
    POLYNOMIAL_FUNC(add_monomial_dui) (1., 1, output);
    return(0);
  }

  #ifdef POLYNOMIAL_COEF_INIT
    POLYNOMIAL_COEF_INIT(tmp);
  #endif
  #ifdef POLYNOMIAL_ORDER_INIT
    POLYNOMIAL_ORDER_INIT(tmp_o);
  #endif

  // init: prevpoly=1
  POLYNOMIAL_FUNC(init) (&prevpoly, n-1);
  POLYNOMIAL_FUNC(add_monomial_dui) (1., 0, &prevpoly);
  // init: output=x
  POLYNOMIAL_FUNC(init) (output, n);
  POLYNOMIAL_FUNC(add_monomial_dui) (1., 1, output);

  // implement i*p_i=(2*i-1)*p_{i-1}-(i-1)*p_{i-2}
  for(i=2;i<=n;i++){
    // save current poly to copy it to prevpoly at the end of the loop
    POLYNOMIAL_FUNC(cpy) (*output, &tmppoly);

    // (2*i-1)/i*p_{i-1}
    POLYNOMIAL_COEF_SET_UI(tmp, 2*i-1);
    POLYNOMIAL_COEF_DIV_UI(tmp, tmp, i);
    POLYNOMIAL_ORDER_SET_UI(tmp_o, 1);
    POLYNOMIAL_FUNC(mul_monomial_inplace) (tmp, tmp_o, output);

    // -(i-1)/i*p_{i-2}
    // copy i to a signed int
    j=i;
    POLYNOMIAL_COEF_SET_SI(tmp, -j+1);
    POLYNOMIAL_COEF_DIV_UI(tmp, tmp, i);
    POLYNOMIAL_FUNC(mul_scalar_inplace) (tmp, &prevpoly);

    // add it to p_n
    POLYNOMIAL_FUNC(add_inplace) (prevpoly, output);

    // replace prevpoly
    POLYNOMIAL_FUNC(free) (prevpoly);
    prevpoly=tmppoly;
  }

  POLYNOMIAL_FUNC(free) (prevpoly);
  #ifdef POLYNOMIAL_COEF_FREE
    POLYNOMIAL_COEF_FREE(tmp);
  #endif
  #ifdef POLYNOMIAL_ORDER_FREE
    POLYNOMIAL_ORDER_FREE(tmp_o);
  #endif

  return(0);
}