libdspl-2.0
Digital Signal Processing Algorithm Library
polyval.c
1 /*
2 * Copyright (c) 2015-2019 Sergey Bakhurin
3 * Digital Signal Processing Library [http://dsplib.org]
4 *
5 * This file is part of libdspl-2.0.
6 *
7 * is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU Lesser General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
11 *
12 * DSPL is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public License
18 * along with Foobar. If not, see <http://www.gnu.org/licenses/>.
19 */
20 
21 
22 #include <stdlib.h>
23 #include <string.h>
24 #include "dspl.h"
25 
26 
27 
28 #ifdef DOXYGEN_ENGLISH
29 
30 #endif
31 #ifdef DOXYGEN_RUSSIAN
32 
33 #endif
34 int DSPL_API poly_z2a_cmplx(complex_t* z, int nz, int ord, complex_t* a)
35 {
36  int k, ind, res;
37  complex_t x[2];
38 
39  if(!z || !a)
40  return ERROR_PTR;
41  if(nz < 0)
42  return ERROR_SIZE;
43  if(nz > ord || ord < 1)
44  return ERROR_POLY_ORD;
45 
46  RE(x[1]) = 1.0;
47  IM(x[1]) = 0.0;
48 
49  memset(a, 0, (ord+1) * sizeof(complex_t));
50 
51  RE(a[0]) = 1.0;
52  ind = 1;
53  for(k = 0; k < nz; k++)
54  {
55  RE(x[0]) = -RE(z[k]);
56  IM(x[0]) = -IM(z[k]);
57  res = conv_cmplx(a, ind, x, 2, a);
58  if(res!=RES_OK)
59  return res;
60  ind++;
61  }
62 
63  return RES_OK;
64 }
65 
66 
67 
68 
69 
70 #ifdef DOXYGEN_ENGLISH
71 
132 #endif
133 #ifdef DOXYGEN_RUSSIAN
134 
198 #endif
199 int DSPL_API polyroots(double* a, int ord, complex_t* r, int* info)
200 {
201  complex_t *t = NULL;
202  int m;
203  int err;
204 
205  if(!a || !r)
206  return ERROR_PTR;
207  if(ord<0)
208  return ERROR_POLY_ORD;
209  if(a[ord] == 0.0)
210  return ERROR_POLY_AN;
211 
212  t = (complex_t*)malloc(ord * ord * sizeof(complex_t));
213  if(!t)
214  return ERROR_MALLOC;
215 
216  for(m = 0; m < ord-1; m++)
217  {
218  RE(t[m * (ord+1) + 1]) = 1.0;
219  RE(t[m + ord * (ord - 1)]) = -a[m] / a[ord];
220  }
221  RE(t[ord * ord - 1]) = -a[ord-1] / a[ord];
222 
223  err = matrix_eig_cmplx(t, ord, r, info);
224 
225  if(t)
226  free(t);
227  return err;
228 }
229 
230 
231 
232 
233 #ifdef DOXYGEN_ENGLISH
234 
235 #endif
236 #ifdef DOXYGEN_RUSSIAN
237 
282 #endif
283 int DSPL_API polyval(double* a, int ord, double* x, int n, double* y)
284 {
285  int k, m;
286 
287  if(!a || !x || !y)
288  return ERROR_PTR;
289  if(ord<0)
290  return ERROR_POLY_ORD;
291  if(n<1)
292  return ERROR_SIZE;
293 
294  for(k = 0; k < n; k++)
295  {
296  y[k] = a[ord];
297  for(m = ord-1; m>-1; m--)
298  y[k] = y[k]*x[k] + a[m];
299  }
300  return RES_OK;
301 }
302 
303 
304 
305 
306 
307 #ifdef DOXYGEN_ENGLISH
308 
309 #endif
310 #ifdef DOXYGEN_RUSSIAN
311 
356 #endif
357 int DSPL_API polyval_cmplx(complex_t* a, int ord,
358  complex_t* x, int n, complex_t* y)
359 {
360  int k, m;
361  complex_t t;
362 
363  if(!a || !x || !y)
364  return ERROR_PTR;
365  if(ord<0)
366  return ERROR_POLY_ORD;
367  if(n<1)
368  return ERROR_SIZE;
369 
370  for(k = 0; k < n; k++)
371  {
372  RE(y[k]) = RE(a[ord]);
373  IM(y[k]) = IM(a[ord]);
374  for(m = ord-1; m>-1; m--)
375  {
376  RE(t) = CMRE(y[k], x[k]);
377  IM(t) = CMIM(y[k], x[k]);
378  RE(y[k]) = RE(t) + RE(a[m]);
379  IM(y[k]) = IM(t) + IM(a[m]);
380  }
381  }
382  return RES_OK;
383 }
384 
#define ERROR_POLY_ORD
The polynomial order is incorrect. The order of the polynomial must be a positive integer.
Definition: dspl.h:609
#define RE(x)
Macro sets real part of the complex number.
Definition: dspl.h:420
#define ERROR_PTR
Pointer error. This error means that one of the required pointers (memory to be allocated for) is tra...
Definition: dspl.h:610
int DSPL_API polyroots(double *a, int ord, complex_t *r, int *info)
Function calculates real polynomial roots.
Definition: polyval.c:199
#define ERROR_SIZE
Error array size. This error occurs when in addition to the pointer the wrong input is passed to the ...
Definition: dspl.h:618
int DSPL_API conv_cmplx(complex_t *a, int na, complex_t *b, int nb, complex_t *c)
Complex vectors linear convolution.
Definition: conv.c:327
int DSPL_API matrix_eig_cmplx(complex_t *a, int n, complex_t *v, int *info)
Eigenvalues calculation of the complex square matrix a.
Definition: matrix.c:144
double complex_t[2]
Complex data type.
Definition: dspl.h:86
#define RES_OK
The function completed correctly. No errors.
Definition: dspl.h:558
#define IM(x)
Macro sets imaginary part of the complex number.
Definition: dspl.h:478
#define ERROR_POLY_AN
The senior coefficient of the polynomial is incorrectly set. For example, when calculating the roots ...
Definition: dspl.h:608
#define ERROR_MALLOC
Dynamic memory allocation error. This error means that an error occurred while dynamically allocating...
Definition: dspl.h:599