Conjugate Gradient
Functions
cg_impls.c File Reference

Simple implementations of conjugate gradient method for solving linear systems. More...

#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include "cg_impls.h"
Include dependency graph for cg_impls.c:

Functions

int rb_dcgsv (int n, Aop A, void *ctxt, const double *b, double *sol)
 Solves a linear system using conjugate method. More...
 
void rb_dscal (int n, double a, double *X, int incx)
 Scales a vector by a constant. $x\leftarrow ax$. More...
 
void rb_daxpy (int n, double a, const double *X, int incx, double *Y, int incy)
 Scale a vector X by a constant add add it to another vector Y. $y\leftarrow y+ax$. More...
 
double rb_damax (int n, const double *X, int incx)
 Finds the element in X which has the maximum absolute value. More...
 
double rb_ddot (int n, const double *X, int incx, const double *Y, int incy)
 Perform inner product $x^Ty$. More...
 
void rb_dcopy (int n, const double *X, int incx, double *Y, int incy)
 Copy a vector to another. More...
 
void rb_dsub (int n, const double *X, int incx, const double *Y, int incy, double *Z, int incz)
 Perform vector substraction, i.e. $z=x-y$. More...
 

Detailed Description

Simple implementations of conjugate gradient method for solving linear systems.

Author
RyanBern

Function Documentation

void rb_damax ( int  n,
const double *  X,
int  incx 
)

Finds the element in X which has the maximum absolute value.

Parameters
nThe length of X.
XThe pointer to the storage of X.
incxThe increment of indices of X.
Returns
The element which has the maximum absolute value
void rb_daxpy ( int  n,
double  a,
const double *  X,
int  incx,
double *  Y,
int  incy 
)

Scale a vector X by a constant add add it to another vector Y. $y\leftarrow y+ax$.

Parameters
nThe length of X and Y.
aThe scaling constant.
XThe pointer to the storage of X.
incxThe increment of indices of X.
YInput/Output. The pointer to the storage of Y.
incyThe increment of indices of Y.
int rb_dcgsv ( int  n,
Aop  A,
void *  ctxt,
const double *  b,
double *  sol 
)

Solves a linear system using conjugate method.

Parameters
nThe dimension of A.
AThe function pointer of type Aop.
ctxtThe context needed by A.
bThe right hand side of $Ax=b$.
solOutput. The computed solution of the equation.
Returns
The number of iterations in the conjugate method.

The function rb_dcgsv solves the linear system $Ax=b$ with conjugate gradient method. It requires $A$ to be positive definite.

Conjugate gradient method doesn't need the matrix $A$ to be given explicitly. It only needs $A$ when applying it to a vector. That is, the user only has to tell how to perform $y\leftarrow Ax$, by providing the parameter of type Aop and all the context needed by the operator.

void rb_dcopy ( int  n,
const double *  X,
int  incx,
double *  Y,
int  incy 
)

Copy a vector to another.

Parameters
nThe length of X and Y.
XThe pointer to the storage of X.
incxThe increment of indices of X.
YThe pointer to the storage of Y.
incyThe increment of indices of Y.
double rb_ddot ( int  n,
const double *  X,
int  incx,
const double *  Y,
int  incy 
)

Perform inner product $x^Ty$.

Parameters
nThe length of X and Y.
XThe pointer to the storage of X.
incxThe increment of indices of X.
YThe pointer to the storage of Y.
incyThe increment of indices of Y.
Returns
The inner product.
void rb_dscal ( int  n,
double  a,
double *  X,
int  incx 
)

Scales a vector by a constant. $x\leftarrow ax$.

Parameters
nThe length of X.
aThe scaling factor.
XThe pointer to the storage of X.
incxThe increment of indices.
void rb_dsub ( int  n,
const double *  X,
int  incx,
const double *  Y,
int  incy,
double *  Z,
int  incz 
)

Perform vector substraction, i.e. $z=x-y$.

Parameters
nThe length of X and Y.
XThe pointer to the storage of X.
incxThe increment of indices of X.
YThe pointer to the storage of Y.
incyThe increment of indices of Y.
ZOutput. The pointer to the storage of Z.
incyThe increment of indices of Z.