libdspl-2.0
Digital Signal Processing Algorithm Library
Basic math functions of the real and complex arguments.

Functions

int DSPL_API log_cmplx (complex_t *x, int n, complex_t *y)
 The logarithm function the complex vector argument x. More...
 
int DSPL_API sinc (double *x, int n, double a, double *y)
 Function \( \textrm{sinc}(x,a) = \frac{\sin(ax)}{ax}\) for the real vector x. More...
 
int DSPL_API sqrt_cmplx (complex_t *x, int n, complex_t *y)
 Square root of the complex vector argguument x. More...
 
int DSPL_API polyroots (double *a, int ord, complex_t *r, int *info)
 Function calculates real polynomial roots. More...
 

Detailed Description

Function Documentation

◆ log_cmplx()

int log_cmplx ( complex_t x,
int  n,
complex_t y 
)

The logarithm function the complex vector argument x.


Function calculates the logarithm function as:

\[ \textrm{Ln}(x) = j \varphi + \ln(|x|), \]

here \(\varphi\) - the complex number phase.

Parameters
[in]xPointer to the argument vector x.
Vector size is [n x 1].

[in]nInput vector x and the logarithm vector y size.

[out]yPointer to the output complex vector y, corresponds to the input vector x.
Vector size is [n x 1].
Memory must be allocated.

Returns
RES_OK if function calculated successfully.
Else code error.
Example:
complex_t x[3] = {{1.0, 2.0}, {3.0, 4.0}, {5.0, 6.0}};
complex_t y[3];
int k;
log_cmplx(x, 3, y);
for(k = 0; k < 3; k++)
printf("log_cmplx(%.1f%+.1fj) = %.3f%+.3fj\n",
RE(x[k]), IM(x[k]), RE(y[k]), IM(y[k]));

Output is:
log_cmplx(1.0+2.0j) = 0.805+1.107j
log_cmplx(3.0+4.0j) = 1.609+0.927j
log_cmplx(5.0+6.0j) = 2.055+0.876j
Author
Sergey Bakhurin www.dsplib.org

Definition at line 718 of file math.c.

Referenced by asin_cmplx().

◆ polyroots()

int polyroots ( double *  a,
int  ord,
complex_t r,
int *  info 
)

Function calculates real polynomial roots.


Function calculates roots of the real polynomial \(P_N(x)\) order \(N\) with a coefficient vector size [(N+1) x 1].

\[ P_N(x) = a_0 + a_1 x + a_2 x^2 + a_3 x^3 + ... a_N x^N. \]

The roots of the polynomial are calculated as eigenvalues of the polynomial companion matrix. To calculate the eigenvalues, a subroutine of the LAPACK package is used.

Parameters
[in]aPointer to the vector of coefficients.
Vector size is [ord+1 x 1].
Coefficient a[0] corresponds to the \(a_0\) polynomial coefficient.
Coefficient a[ord] cannot be zero.

[in]ordPolynomial order \(N\).

[out]rPointer to the polynomial roots vector.
Vector size is [ord x 1].
Memory must be allocated.
The roots of a real polynomial can be either real or form simple or multiple complex conjugate pairs of roots. Therefore, the output root vector is of a complex data type.

[out]infoPointer to the LAPACK subroutine error code.
This code is returned by the LAPACK subroutine and translated through this variable for analysis..

Returns
RES_OK — roots are calculated successfully.

Else code error.

Example:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "dspl.h"
#define N 2
int main(int argc, char* argv[])
{
void* hdspl; /* DSPL handle */
double a[N+1] = {2.0, 2.0, 1.0}; /* P(x) = 2 + 2x + x^2 */
complex_t r[N] = {0}; /* roots */
int err, n, info;
hdspl = dspl_load(); /* Load DSPL functions */
if(!hdspl)
{
printf("libdspl loading error!\n");
return -1;
}
/* roots calculation */
err = polyroots(a, N, r, &info);
printf("Error code: 0x%.8x\n", err);
/* print roots */
for(n = 0; n < N; n++)
printf("r[%d] = % -8.5f% -8.5f j\n", n, RE(r[n]), IM(r[n]));
/* free dspl handle */
dspl_free(hdspl);
return 0;
}

This program calculates the roots of the polynomial

\[ P(x) = 2 + 2x + x^2 \]

and prints the calculated roots. The result of the program:

Error code: 0x00000000
r[0] = -1.00000 1.00000 j
r[1] = -1.00000-1.00000 j
Author
Sergey Bakhurin. www.dsplib.org

Definition at line 199 of file polyval.c.

◆ sinc()

int sinc ( double *  x,
int  n,
double  a,
double *  y 
)

Function \( \textrm{sinc}(x,a) = \frac{\sin(ax)}{ax}\) for the real vector x.


Parameters
[in]xPointer to the input vector \( x \).
Vector size is [n x 1].

[in]nInput and output vectors size.

[in]aFunction parameter \( \textrm{sinc}(x,a) = \frac{\sin(ax)}{ax}\).

[out]yPointer to the sinc function output vector.
Vector size is [n x 1].
Memory must be allocated.

Returns
RES_OK if function calculated successfully.
Else code error.
Author
Sergey Bakhurin www.dsplib.org

Definition at line 936 of file math.c.

◆ sqrt_cmplx()

int sqrt_cmplx ( complex_t x,
int  n,
complex_t y 
)

Square root of the complex vector argguument x.


Function calculates square root value of vector x length n:

\[ y(k) = \sqrt{x(k)}, \qquad k = 0 \ldots n-1. \]

Parameters
[in]xPointer to the input complex vector x.
Vector size is [n x 1].

[in]nSize of input and output vectors x and y.

[out]yPointer to the square root vector y.
Vector size is [n x 1].
Memory must be allocated.

Returns
RES_OK if function is calculated successfully.
Else code error.
Example
complex_t x[3] = {{1.0, 2.0}, {3.0, 4.0}, {5.0, 6.0}};
int k;
sqrt_cmplx(x, 3, y);
for(k = 0; k < 3; k++)
printf("sqrt_cmplx(%.1f%+.1fj) = %.3f%+.3fj\n",
RE(x[k]), IM(x[k]), RE(y[k]), IM(y[k]));

Result:
sqrt_cmplx(1.0+2.0j) = 1.272+0.786j
sqrt_cmplx(3.0+4.0j) = 2.000+1.000j
sqrt_cmplx(5.0+6.0j) = 2.531+1.185j
Author
Sergey Bakhurin www.dsplib.org

Definition at line 1307 of file math.c.

Referenced by asin_cmplx(), ellip_acd_cmplx(), and ellip_asn_cmplx().

#define RE(x)
Macro sets real part of the complex number.
Definition: dspl.h:420
int DSPL_API log_cmplx(complex_t *x, int n, complex_t *y)
The logarithm function the complex vector argument x.
Definition: math.c:718
int DSPL_API polyroots(double *a, int ord, complex_t *r, int *info)
Function calculates real polynomial roots.
Definition: polyval.c:199
void * dspl_load()
Perform dynamic linking and load libdspl-2.0 functions.
double complex_t[2]
Complex data type.
Definition: dspl.h:86
void dspl_free(void *handle)
Cleans up the previously linked DSPL-2.0 dynamic library.
int DSPL_API sqrt_cmplx(complex_t *x, int n, complex_t *y)
Square root of the complex vector argguument x.
Definition: math.c:1307
#define IM(x)
Macro sets imaginary part of the complex number.
Definition: dspl.h:478