libdspl-2.0
Библиотека алгоритмов цифровой обработки сигналов
polyroots.c
1 /*
2 * Copyright (c) 2015-2024 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 #include <stdlib.h>
22 #include <string.h>
23 #include <math.h>
24 #include "dspl.h"
25 
26 
27 
28 #ifdef DOXYGEN_ENGLISH
29 
90 #endif
91 #ifdef DOXYGEN_RUSSIAN
92 
156 #endif
157 int DSPL_API polyroots(double* a, int ord, complex_t* r, int* info)
158 {
159  complex_t *t = NULL;
160  int m;
161  int err;
162 
163  if(!a || !r)
164  return ERROR_PTR;
165  if(ord<0)
166  return ERROR_POLY_ORD;
167  if(a[ord] == 0.0)
168  return ERROR_POLY_AN;
169 
170  t = (complex_t*)malloc(ord * ord * sizeof(complex_t));
171  if(!t)
172  return ERROR_MALLOC;
173 
174  for(m = 0; m < ord-1; m++)
175  {
176  RE(t[m * (ord+1) + 1]) = 1.0;
177  RE(t[m + ord * (ord - 1)]) = -a[m] / a[ord];
178  }
179  RE(t[ord * ord - 1]) = -a[ord-1] / a[ord];
180 
181  err = matrix_eig_cmplx(t, ord, r, info);
182 
183  if(t)
184  free(t);
185  return err;
186 }
#define ERROR_POLY_ORD
Неверно задан порядок полинома. Порядок полинома должен быть положительным целым числом.
Definition: dspl.h:609
#define RE(x)
Макрос определяющий реальную часть комплексного числа.
Definition: dspl.h:420
#define ERROR_PTR
Ошибка указателя. Данная ошибка означает, что один из обязательных указателей (память под который дол...
Definition: dspl.h:610
int polyroots(double *a, int ord, complex_t *r, int *info)
Расчет корней вещественного полинома
Definition: polyroots.c:157
int matrix_eig_cmplx(complex_t *a, int n, complex_t *v, int *info)
Расчет собственных значений квадратной комплексной матрицы.
double complex_t[2]
Описание комплексного типа данных.
Definition: dspl.h:86
#define ERROR_POLY_AN
Неверно задан старший коэффициент полинома. Например при вычислении кореней полинома степени .
Definition: dspl.h:608
#define ERROR_MALLOC
Ошибка динамического выделения памяти. Данная ошибка означает, что при динамическом выделении памяти ...
Definition: dspl.h:599