libdspl-2.0
Digital Signal Processing Algorithm Library
Linear algebra and matrix operations.

Functions

int DSPL_API matrix_eig_cmplx (complex_t *a, int n, complex_t *v, int *info)
 Eigenvalues calculation of the complex square matrix a. More...
 
int DSPL_API matrix_eye (double *a, int n, int m)
 The real identity matrix size n x m generation. More...
 
int DSPL_API matrix_eye_cmplx (complex_t *a, int n, int m)
 The complex identity matrix size n x m generation. More...
 
int DSPL_API matrix_mul (double *a, int na, int ma, double *b, int nb, int mb, double *c)
 Matrix multiplication. More...
 

Detailed Description

This group describes the linear algebra algorithms and matrix operations. DSPL-2.0 using internal interface for BLAS and LAPACK packages.

Function Documentation

◆ matrix_eig_cmplx()

int matrix_eig_cmplx ( complex_t a,
int  n,
complex_t v,
int *  info 
)

Eigenvalues calculation of the complex square matrix a.


Function calculates n eigenvalues of the matrix a size n x n.

Parameters
[in]aPointer to the complex matrix a size n x n.
Matrix is stored in the memory as column-major array.

[in]nSize of matrix n x n.

[out]vPointer to the eigenvalues vector.
Vector size is ]n x 1].
Memory must be allocated.

[out]infoPointer to the zgees LAPACK subroutine output parameter.
If an error occurs while calculating the vector of eigenvalues, the LAPACK subroutine returns an error code that can be read from this pointer.

Returns
RES_OK — function is calculated successfully.
Else code error.
If an ERROR_LAPACK error occurs, the LAPACK package error code will be written to the info address.

Eigenvalues calculation example:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "dspl.h"
#define N 3
int main()
{
void* handle; /* DSPL handle */
handle = dspl_load(); /* Load DSPL function */
complex_t a[N*N] = {{1.0, 0.0}, {1.0, 0.0}, {0.0, 0.0},
{2.0, 0.0}, {0.0, 0.0}, {1.0, 0.0},
{3.0, 0.0}, {0.0, 0.0}, {0.0, 0.0}};
complex_t v[N];
int err, info;
matrix_print_cmplx(a, N, N, "A", "%8.2f%+8.2fi");
err = matrix_eig_cmplx(a, N, v, &info);
if(err!=RES_OK)
{
printf("ERROR CODE: 0x%.8x, info = %d\n", err, info);
}
matrix_print_cmplx(v, N, 1, "v", "%10.6f%+10.6fi");
dspl_free(handle); /* free dspl handle */
return 0;
}

This program calculates eigenvalues of the matrix size 3 x 3 and print its to display.
Result:

A = [ % size [3 x 3] type: complex
1.00   +0.00i,     2.00   +0.00i,     3.00   +0.00i;
1.00   +0.00i,     0.00   +0.00i,     0.00   +0.00i;
0.00   +0.00i,     1.00   +0.00i,     0.00   +0.00i;];

v = [ % size [3 x 1] type: complex
 2.374424 -0.000000i;    
-0.687212 +0.889497i;
-0.687212 -0.889497i;];
Author
Sergey Bakhurin www.dsplib.org

Definition at line 144 of file matrix.c.

Referenced by polyroots().

◆ matrix_eye()

int matrix_eye ( double *  a,
int  n,
int  m 
)

The real identity matrix size n x m generation.


Function fills matrix a by zeros and sets main diagonal as ones.

Parameters
[in]aPointer to the real matrix size n x m.
Matrix is stored in the memory as column-major array.

[in]nMatrix a rows number.

[in]mMatrix a columns number..

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

Definition at line 229 of file matrix.c.

◆ matrix_eye_cmplx()

int matrix_eye_cmplx ( complex_t a,
int  n,
int  m 
)

The complex identity matrix size n x m generation.


Function fills matrix a by zeros and sets main diagonal as ones.

Parameters
[in]aPointer to the complex matrix size n x m.
Matrix is stored in the memory as column-major array.

[in]nMatrix a rows number.

[in]mMatrix a columns number..

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

Definition at line 300 of file matrix.c.

◆ matrix_mul()

int matrix_mul ( double *  a,
int  na,
int  ma,
double *  b,
int  nb,
int  mb,
double *  c 
)

Matrix multiplication.


The function calculates the product of matrices \(\mathbf{C} = \mathbf{AB}\), here \(\mathbf{A}\) – matrix contains \(N_A\) rows and \(M_A\) columns, \(\mathbf{B}\) – matrix contains \(N_B\) rows and \(M_B\) columns, product matrix \(\mathbf{C} = \mathbf{AB}\) contains \(N_A\) rows and \(M_B\) columns.

Note
Matrix multiplication requires the equality \(M_A = N_B\).
Function uses BLAS subroutine dgemm.
Parameters
[in]aPointer to the input matrix \(\mathbf{A}\) size na x ma.
Matrix must be located in memory as column-major array.

[in]naMatrix a rows number.

[in]maMatrix a columns number.

[in]bPointer to the input matrix \(\mathbf{B}\) size nb x mb.
Matrix must be located in memory as column-major array.

[in]nbMatrix b rows number.
Necessary equality ma = nb.

[in]mbMatrix a columns number.

[out]cPointer to the output matrix \(\mathbf{С} = \mathbf{AB}\).
Matrix size is na x mb.
Memory must be allocated.

Returns
RES_OK — function is calculated successfully.
Else error code.
Example:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "dspl.h"
#define N 4
#define M 3
#define K 5
int main()
{
void* handle; /* DSPL handle */
handle = dspl_load(); /* Load DSPL function */
double a[N*K], b[K*M], c[N*M];
linspace(0, N*K, N*K, DSPL_PERIODIC, a);
matrix_print(a, N, K, "A", "%8.2f");
linspace(0, K*M, K*M, DSPL_PERIODIC, b);
matrix_print(b, K, M, "B", "%8.2f");
matrix_mul(a, N, K, b, K, M, c);
matrix_print(c, N, M, "C", "%8.2f");
dspl_free(handle); /* free dspl handle */
return 0;
}

The program forms and multiplies two matrices. The original matrices and their product are printed.

The result of the program:

A = [ % size [4 x 5] type: real
0.00,     4.00,     8.00,    12.00,    16.00;
1.00,     5.00,     9.00,    13.00,    17.00;
2.00,     6.00,    10.00,    14.00,    18.00;
3.00,     7.00,    11.00,    15.00,    19.00;];

B = [ % size [5 x 3] type: real
0.00,     5.00,    10.00;
1.00,     6.00,    11.00;
2.00,     7.00,    12.00;
3.00,     8.00,    13.00;
4.00,     9.00,    14.00;];

C = [ % size [4 x 3] type: real
120.00,   320.00,   520.00;
130.00,   355.00,   580.00;
140.00,   390.00,   640.00;
150.00,   425.00,   700.00;]; 
Author
Sergey Bakhurin www.dsplib.org

Definition at line 486 of file matrix.c.

void * dspl_load()
Perform dynamic linking and load libdspl-2.0 functions.
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
void dspl_free(void *handle)
Cleans up the previously linked DSPL-2.0 dynamic library.
#define RES_OK
The function completed correctly. No errors.
Definition: dspl.h:558
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: array.c:1041
int DSPL_API matrix_mul(double *a, int na, int ma, double *b, int nb, int mb, double *c)
Matrix multiplication.
Definition: matrix.c:486