mirror of
https://github.moeyy.xyz/https://github.com/TheAlgorithms/C.git
synced 2023-10-11 15:56:24 +08:00
document QR algorithms
This commit is contained in:
parent
dd40af2736
commit
0c4be6aaba
@ -1,8 +1,9 @@
|
|||||||
/**
|
/**
|
||||||
* @file
|
* @file
|
||||||
*
|
*
|
||||||
* Library functions to compute QR decomposition of a
|
* \brief Library functions to compute [QR
|
||||||
* given matrix.
|
* decomposition](https://en.wikipedia.org/wiki/QR_decomposition) of a given
|
||||||
|
* matrix.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
#ifndef QR_DECOMPOSE_H
|
#ifndef QR_DECOMPOSE_H
|
||||||
@ -11,6 +12,9 @@
|
|||||||
#include <math.h>
|
#include <math.h>
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
|
#ifdef _OPENMP
|
||||||
|
#include <omp.h>
|
||||||
|
#endif
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* function to display matrix on stdout
|
* function to display matrix on stdout
|
||||||
@ -36,11 +40,16 @@ void print_matrix(double **A, /**< matrix to print */
|
|||||||
* \f$\vec{a}\cdot\vec{b}=\displaystyle\sum_{i=0}^L a_i\times b_i\f$
|
* \f$\vec{a}\cdot\vec{b}=\displaystyle\sum_{i=0}^L a_i\times b_i\f$
|
||||||
*
|
*
|
||||||
* \returns \f$\vec{a}\cdot\vec{b}\f$
|
* \returns \f$\vec{a}\cdot\vec{b}\f$
|
||||||
**/
|
*/
|
||||||
double vector_dot(double *a, double *b, int L)
|
double vector_dot(double *a, double *b, int L)
|
||||||
{
|
{
|
||||||
double mag = 0.f;
|
double mag = 0.f;
|
||||||
for (int i = 0; i < L; i++)
|
int i;
|
||||||
|
#ifdef _OPENMP
|
||||||
|
// parallelize on threads
|
||||||
|
#pragma omp parallel for reduction(+ : mag)
|
||||||
|
#endif
|
||||||
|
for (i = 0; i < L; i++)
|
||||||
mag += a[i] * b[i];
|
mag += a[i] * b[i];
|
||||||
|
|
||||||
return mag;
|
return mag;
|
||||||
@ -53,7 +62,7 @@ double vector_dot(double *a, double *b, int L)
|
|||||||
* \f$\left|\vec{a}\right|=\sqrt{\displaystyle\sum_{i=0}^L a_i^2}\f$
|
* \f$\left|\vec{a}\right|=\sqrt{\displaystyle\sum_{i=0}^L a_i^2}\f$
|
||||||
*
|
*
|
||||||
* \returns \f$\left|\vec{a}\right|\f$
|
* \returns \f$\left|\vec{a}\right|\f$
|
||||||
**/
|
*/
|
||||||
double vector_mag(double *vector, int L)
|
double vector_mag(double *vector, int L)
|
||||||
{
|
{
|
||||||
double dot = vector_dot(vector, vector, L);
|
double dot = vector_dot(vector, vector, L);
|
||||||
@ -65,7 +74,7 @@ double vector_mag(double *vector, int L)
|
|||||||
* \f[\text{proj}_\vec{b}\vec{a}=\frac{\vec{a}\cdot\vec{b}}{\left|\vec{b}\right|^2}\vec{b}\f]
|
* \f[\text{proj}_\vec{b}\vec{a}=\frac{\vec{a}\cdot\vec{b}}{\left|\vec{b}\right|^2}\vec{b}\f]
|
||||||
*
|
*
|
||||||
* \returns NULL if error, otherwise pointer to output
|
* \returns NULL if error, otherwise pointer to output
|
||||||
**/
|
*/
|
||||||
double *vector_proj(double *a, double *b, double *out, int L)
|
double *vector_proj(double *a, double *b, double *out, int L)
|
||||||
{
|
{
|
||||||
const double num = vector_dot(a, b, L);
|
const double num = vector_dot(a, b, L);
|
||||||
@ -74,7 +83,12 @@ double *vector_proj(double *a, double *b, double *out, int L)
|
|||||||
return NULL;
|
return NULL;
|
||||||
|
|
||||||
const double scalar = num / deno;
|
const double scalar = num / deno;
|
||||||
for (int i = 0; i < L; i++)
|
int i;
|
||||||
|
#ifdef _OPENMP
|
||||||
|
// parallelize on threads
|
||||||
|
#pragma omp for
|
||||||
|
#endif
|
||||||
|
for (i = 0; i < L; i++)
|
||||||
out[i] = scalar * b[i];
|
out[i] = scalar * b[i];
|
||||||
|
|
||||||
return out;
|
return out;
|
||||||
@ -86,14 +100,19 @@ double *vector_proj(double *a, double *b, double *out, int L)
|
|||||||
* \f$\vec{c}=\vec{a}-\vec{b}\f$
|
* \f$\vec{c}=\vec{a}-\vec{b}\f$
|
||||||
*
|
*
|
||||||
* \returns pointer to output vector
|
* \returns pointer to output vector
|
||||||
**/
|
*/
|
||||||
double *vector_sub(double *a, /**< minuend */
|
double *vector_sub(double *a, /**< minuend */
|
||||||
double *b, /**< subtrahend */
|
double *b, /**< subtrahend */
|
||||||
double *out, /**< resultant vector */
|
double *out, /**< resultant vector */
|
||||||
int L /**< length of vectors */
|
int L /**< length of vectors */
|
||||||
)
|
)
|
||||||
{
|
{
|
||||||
for (int i = 0; i < L; i++)
|
int i;
|
||||||
|
#ifdef _OPENMP
|
||||||
|
// parallelize on threads
|
||||||
|
#pragma omp for
|
||||||
|
#endif
|
||||||
|
for (i = 0; i < L; i++)
|
||||||
out[i] = a[i] - b[i];
|
out[i] = a[i] - b[i];
|
||||||
|
|
||||||
return out;
|
return out;
|
||||||
@ -123,7 +142,7 @@ double *vector_sub(double *a, /**< minuend */
|
|||||||
* \vdots & \vdots & \vdots & \ddots
|
* \vdots & \vdots & \vdots & \ddots
|
||||||
* \end{bmatrix}\\
|
* \end{bmatrix}\\
|
||||||
* \f}
|
* \f}
|
||||||
**/
|
*/
|
||||||
void qr_decompose(double **A, /**< input matrix to decompose */
|
void qr_decompose(double **A, /**< input matrix to decompose */
|
||||||
double **Q, /**< output decomposed matrix */
|
double **Q, /**< output decomposed matrix */
|
||||||
double **R, /**< output decomposed matrix */
|
double **R, /**< output decomposed matrix */
|
||||||
@ -137,16 +156,25 @@ void qr_decompose(double **A, /**< input matrix to decompose */
|
|||||||
for (int i = 0; i < N;
|
for (int i = 0; i < N;
|
||||||
i++) /* for each column => R is a square matrix of NxN */
|
i++) /* for each column => R is a square matrix of NxN */
|
||||||
{
|
{
|
||||||
for (int j = 0; j < i; j++) /* second dimension of column */
|
int j;
|
||||||
R[i][j] = 0.; /* make R upper triangular */
|
#ifdef _OPENMP
|
||||||
|
// parallelize on threads
|
||||||
|
#pragma omp for
|
||||||
|
#endif
|
||||||
|
for (j = 0; j < i; j++) /* second dimension of column */
|
||||||
|
R[i][j] = 0.; /* make R upper triangular */
|
||||||
|
|
||||||
/* get corresponding Q vector */
|
/* get corresponding Q vector */
|
||||||
for (int j = 0; j < M; j++)
|
#ifdef _OPENMP
|
||||||
|
// parallelize on threads
|
||||||
|
#pragma omp for
|
||||||
|
#endif
|
||||||
|
for (j = 0; j < M; j++)
|
||||||
{
|
{
|
||||||
tmp_vector[j] = A[j][i]; /* accumulator for uk */
|
tmp_vector[j] = A[j][i]; /* accumulator for uk */
|
||||||
col_vector[j] = A[j][i];
|
col_vector[j] = A[j][i];
|
||||||
}
|
}
|
||||||
for (int j = 0; j < i; j++)
|
for (j = 0; j < i; j++)
|
||||||
{
|
{
|
||||||
for (int k = 0; k < M; k++)
|
for (int k = 0; k < M; k++)
|
||||||
col_vector2[k] = Q[k][j];
|
col_vector2[k] = Q[k][j];
|
||||||
@ -154,7 +182,12 @@ void qr_decompose(double **A, /**< input matrix to decompose */
|
|||||||
vector_sub(tmp_vector, col_vector2, tmp_vector, M);
|
vector_sub(tmp_vector, col_vector2, tmp_vector, M);
|
||||||
}
|
}
|
||||||
double mag = vector_mag(tmp_vector, M);
|
double mag = vector_mag(tmp_vector, M);
|
||||||
for (int j = 0; j < M; j++)
|
|
||||||
|
#ifdef _OPENMP
|
||||||
|
// parallelize on threads
|
||||||
|
#pragma omp for
|
||||||
|
#endif
|
||||||
|
for (j = 0; j < M; j++)
|
||||||
Q[j][i] = tmp_vector[j] / mag;
|
Q[j][i] = tmp_vector[j] / mag;
|
||||||
|
|
||||||
/* compute upper triangular values of R */
|
/* compute upper triangular values of R */
|
||||||
|
@ -1,7 +1,8 @@
|
|||||||
/**
|
/**
|
||||||
* @file
|
* @file
|
||||||
* Program to compute the QR decomposition of a
|
* \brief Program to compute the [QR
|
||||||
* given matrix.
|
* decomposition](https://en.wikipedia.org/wiki/QR_decomposition) of a given
|
||||||
|
* matrix.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
#include "qr_decompose.h"
|
#include "qr_decompose.h"
|
||||||
|
@ -1,7 +1,8 @@
|
|||||||
/**
|
/**
|
||||||
* @file
|
* @file
|
||||||
* Compute real eigen values and eigen vectors of a symmetric matrix using QR
|
* \brief Compute real eigen values and eigen vectors of a symmetric matrix
|
||||||
* decomposition method.
|
* using [QR decomposition](https://en.wikipedia.org/wiki/QR_decomposition)
|
||||||
|
* method.
|
||||||
*/
|
*/
|
||||||
#include "qr_decompose.h"
|
#include "qr_decompose.h"
|
||||||
#include <math.h>
|
#include <math.h>
|
||||||
|
Loading…
Reference in New Issue
Block a user