libdspl-2.0
Digital Signal Processing Algorithm Library
Polynomial functions and analysis.

Functions

int DSPL_API cheby_poly1 (double *x, int n, int ord, double *y)
 Chebyshev polynomial of the first kind order ord More...
 
int DSPL_API cheby_poly2 (double *x, int n, int ord, double *y)
 Chebyshev polynomial of the second kind order ord More...
 
int DSPL_API polyroots (double *a, int ord, complex_t *r, int *info)
 Function calculates real polynomial roots. More...
 

Detailed Description

Function Documentation

◆ cheby_poly1()

int cheby_poly1 ( double *  x,
int  n,
int  ord,
double *  y 
)

Chebyshev polynomial of the first kind order ord


Function calculates Chebyshev polynomial \( C_{ord}(x)\) of the first kind order ord for the real vector x (length n) by recurrent equation:

\[ C_{ord}(x) = 2 x C_{ord-1}(x) - C_{ord-2}(x), \]

where \( C_0(x) = 1 \), \( C_1(x) = x\)

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

[in]nSize of vectors x and y.

[in]ordChebyshev polynomial order.

[out]yPointer to the Chebyshev polynomial values, corresponds to the argument x.
Vector size is [n x 1].
Memory must be allocated.

Returns
RES_OK if Chebyshev polynomial is calculated successfully.
Else code error.
Example:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "dspl.h"
#define N 250
int main(int argc, char* argv[])
{
void* hdspl; /* DSPL handle */
void* hplot; /* GNUPLOT handle */
double x[N], y[N];
int ord;
char fn[64];
hdspl = dspl_load(); /* Load DSPL function */
linspace(-1.0, 1.0, N, DSPL_SYMMETRIC, x);
for(ord = 1; ord < 5; ord++)
{
cheby_poly1(x, N, ord, y);
sprintf(fn, "dat/cheby_poly1_ord%d.txt", ord);
writetxt(x,y,N,fn);
}
/* plotting by GNUPLOT */
gnuplot_create(argc, argv, 560, 380, "img/cheby_poly1.png", &hplot);
gnuplot_cmd(hplot, "set grid");
gnuplot_cmd(hplot, "set key left top");
gnuplot_cmd(hplot, "set xlabel 'x'");
gnuplot_cmd(hplot, "set ylabel 'C_N(x)'");
gnuplot_cmd(hplot, "set yrange [-1.5:1.5]");
gnuplot_cmd(hplot, "plot 'dat/cheby_poly1_ord1.txt' with lines, \\");
gnuplot_cmd(hplot, " 'dat/cheby_poly1_ord2.txt' with lines, \\");
gnuplot_cmd(hplot, " 'dat/cheby_poly1_ord3.txt' with lines, \\");
gnuplot_cmd(hplot, " 'dat/cheby_poly1_ord4.txt' with lines");
gnuplot_close(hplot);
dspl_free(hdspl); /* free dspl handle */
return 0;
}
int DSPL_API linspace(double x0, double x1, int n, int type, double *x)
Function fills a vector with n linearly spaced elements between x0 and x1.
Definition: linspace.c:169
int DSPL_API writetxt(double *x, double *y, int n, char *fn)
Save real data to the text file fn. .
Definition: writetxt.c:122
void DSPL_API gnuplot_close(void *h)
Close GNUPLOT handle.
Definition: gnuplot_close.c:80
int DSPL_API gnuplot_create(int argc, char *argv[], int w, int h, char *fn_png, void **hplot)
Create GNUPLOT chart.
void DSPL_API gnuplot_cmd(void *h, char *cmd)
Function sends cmd command to GNUPLOT corresponds to h handle.
Definition: gnuplot_cmd.c:82
int DSPL_API cheby_poly1(double *x, int n, int ord, double *y)
Chebyshev polynomial of the first kind order ord
Definition: cheby_poly1.c:136
void * dspl_load()
Perform dynamic linking and load libdspl-2.0 functions.
void dspl_free(void *handle)
Cleans up the previously linked DSPL-2.0 dynamic library.

Text files will be created in dat directory:

cheby_poly1_ord1.txt
cheby_poly1_ord2.txt
cheby_poly1_ord3.txt
cheby_poly1_ord4.txt

GNUPLOT package will create Chebyshev polynomials plot from saved text-files:

Author
Sergey Bakhurin www.dsplib.org

Definition at line 136 of file cheby_poly1.c.

◆ cheby_poly2()

int cheby_poly2 ( double *  x,
int  n,
int  ord,
double *  y 
)

Chebyshev polynomial of the second kind order ord


Function calculates Chebyshev polynomial \( U_ord(x)\) of the first kind order ord for the real vector x (length n) by recurrent equation:

\[ U_{ord}(x) = 2 x U_{ord-1}(x) - U_{ord-2}(x), \]

where \( U_0(x) = 1 \), \( U_1(x) = 2x\)

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

[in]nSize of vectors x and y.

[in]ordChebyshev polynomial order.

[out]yPointer to the Chebyshev polynomial values, corresponds to the argument x.
Vector size is [n x 1].
Memory must be allocated.

Returns
RES_OK if Chebyshev polynomial is calculated successfully.
Else code error.
Example:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "dspl.h"
#define N 250
int main(int argc, char* argv[])
{
void* hdspl; /* DSPL handle */
void* hplot; /* GNUPLOT handle */
double x[N], y[N];
int ord;
char fn[64];
hdspl = dspl_load(); /* Load DSPL function */
linspace(-1.0, 1.0, N, DSPL_SYMMETRIC, x);
for(ord = 1; ord < 5; ord++)
{
cheby_poly2(x, N, ord, y);
sprintf(fn, "dat/cheby_poly2_ord%d.txt", ord);
writetxt(x,y,N,fn);
}
/* plotting by GNUPLOT */
gnuplot_create(argc, argv, 560, 380, "img/cheby_poly2.png", &hplot);
gnuplot_cmd(hplot, "set grid");
gnuplot_cmd(hplot, "set key left top");
gnuplot_cmd(hplot, "set xlabel 'x'");
gnuplot_cmd(hplot, "set ylabel 'U_N (x)'");
gnuplot_cmd(hplot, "set yrange [-3.5:3.5]");
gnuplot_cmd(hplot, "plot 'dat/cheby_poly2_ord1.txt' with lines, \\");
gnuplot_cmd(hplot, " 'dat/cheby_poly2_ord2.txt' with lines, \\");
gnuplot_cmd(hplot, " 'dat/cheby_poly2_ord3.txt' with lines, \\");
gnuplot_cmd(hplot, " 'dat/cheby_poly2_ord4.txt' with lines");
gnuplot_close(hplot);
dspl_free(hdspl); /* free dspl handle */
return 0;
}
int DSPL_API cheby_poly2(double *x, int n, int ord, double *y)
Chebyshev polynomial of the second kind order ord
Definition: cheby_poly2.c:133

Text files will be created in dat directory:

cheby_poly2_ord1.txt
cheby_poly2_ord2.txt
cheby_poly2_ord3.txt
cheby_poly2_ord4.txt

GNUPLOT package will create Chebyshev polynomials plot from saved text-files:

Author
Sergey Bakhurin www.dsplib.org

Definition at line 133 of file cheby_poly2.c.

◆ 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;
}
int DSPL_API polyroots(double *a, int ord, complex_t *r, int *info)
Function calculates real polynomial roots.
Definition: polyroots.c:157
#define RE(x)
Macro sets real part of the complex number.
Definition: dspl.h:420
double complex_t[2]
Complex data type.
Definition: dspl.h:86
#define IM(x)
Macro sets imaginary part of the complex number.
Definition: dspl.h:478

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 157 of file polyroots.c.