libdspl-2.0
Библиотека алгоритмов цифровой обработки сигналов
sqrt_cmplx.c
1 /*
2 * Copyright (c) 2015-2023 Sergey Bakhurin
3 * Digital Signal Processing Library [http://dsplib.org]
4 *
5 * This file is part of DSPL.
6 *
7 * is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU 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 General Public License
18 * along with Foobar. If not, see <http://www.gnu.org/licenses/>.
19 */
20 
21 
22 #include <stdio.h>
23 #include <stdlib.h>
24 #include "dspl.h"
25 
26 
27 
28 #ifdef DOXYGEN_ENGLISH
29 
80 #endif
81 #ifdef DOXYGEN_RUSSIAN
82 
135 #endif
136 int DSPL_API sqrt_cmplx(complex_t* x, int n, complex_t *y)
137 {
138  int k;
139  double r, zr, at;
140  complex_t t;
141  if(!x || !y)
142  return ERROR_PTR;
143  if(n < 1)
144  return ERROR_SIZE;
145 
146  for(k = 0; k < n; k++)
147  {
148  r = ABS(x[k]);
149  if(r == 0.0)
150  {
151  RE(y[k]) = 0.0;
152  IM(y[k]) = 0.0;
153  }
154  else
155  {
156  RE(t) = RE(x[k]) + r;
157  IM(t) = IM(x[k]);
158  at = ABS(t);
159  if(at == 0.0)
160  {
161  RE(y[k]) = 0.0;
162  IM(y[k]) = sqrt(r);
163  }
164  else
165  {
166  zr = 1.0 / ABS(t);
167  r = sqrt(r);
168  RE(y[k]) = RE(t) * zr * r;
169  IM(y[k]) = IM(t) * zr * r;
170  }
171  }
172  }
173  return RES_OK;
174 }
#define RE(x)
Макрос определяющий реальную часть комплексного числа.
Definition: dspl.h:420
#define ERROR_PTR
Ошибка указателя. Данная ошибка означает, что один из обязательных указателей (память под который дол...
Definition: dspl.h:610
#define ERROR_SIZE
Ошибка при передаче размера массива. Данная ошибка возникает когда помимо указателя на массив входных...
Definition: dspl.h:618
double complex_t[2]
Описание комплексного типа данных.
Definition: dspl.h:86
#define RES_OK
Функция завершилась корректно. Ошибки отсутствуют.
Definition: dspl.h:558
int sqrt_cmplx(complex_t *x, int n, complex_t *y)
Квадратный корень из комплексного вектора x (поэлементный).
Definition: sqrt_cmplx.c:136
#define IM(x)
Макрос определяющий мнимую часть комплексного числа.
Definition: dspl.h:478