src_c/dmatrix.c File Reference

Source file for matrix and vector manipulation functions. More...

#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/time.h>
#include "def.h"
#include "dmatrix.h"
#include "blas.h"
#include "lapack.h"

Defines

#define PROFILING   FALSE

Functions

double dmat_norm1 (const int n, const double *x)
 $ \|x\|_1 $
double dmat_norm2 (const int n, const double *x)
 $ \|x\|_2 $
double dmat_norminf (const int n, const double *x)
 $ \|x\|_{\infty} $
double dmat_dot (const int n, const double *x, const double *y)
 $ x^Ty $
void dmat_vset (int n, const double val, double *dst)
 $ \mbox{dst}_i \leftarrow \mbox{val} $
void dmat_iset (int n, const int val, int *dst)
void dmat_vcopy (const int n, const double *src, double *dst)
 $ \mbox{dst} \leftarrow \mbox{src} $
void dmat_icopy (const int n, const int *src, int *dst)
void dmat_yexpx (const int n, const double *x, double *y)
 $ y_i = \exp(x_i) $
void dmat_ysqrtx (const int n, const double *x, double *y)
 $ y_i = x_i^{1/2} $
void dmat_yinvx (const int n, const double *x, double *y)
 $ y_i = 1/x_i $
void dmat_waxpby (int n, double alpha, const double *x, double beta, const double *y, double *w)
 $ w = \alpha*x + \beta*y $
void dmat_elemprod (const int n, const double *x, const double *y, double *z)
 $ z_i = x_i*y_i $
void dmat_elemdivi (const int n, const double *x, const double *y, double *z)
 $ z_i = x_i/y_i $
void dmat_yAx (const dmatrix *A, const double *x, double *y)
 $ y = Ax $
void dmat_yATx (const dmatrix *A, const double *x, double *y)
 $ y = A^Tx $
void dmat_yAmpqx (const dmatrix *A, const double *p, const double *q, const double *x, double *y)
 $ y = (A-pq)x $
void dmat_yAmpqTx (const dmatrix *A, const double *p, const double *q, const double *x, double *y)
 $ y = (A-pq)^Tx $
void dmat_yATDAx (const dmatrix *A, const double *d, const double *x, double *y)
 $ y = A^TDAx $
void dmat_yHx_ (const dmatrix *A, const double *b, const double *d0, const double *d1, const double *d2, const double *x, double *y)
 $ y = Hx = \begin{array}{|ccc|} b^TD_0b & b^TD_0A & 0 \\ A^TD_0b & A^TD_0A+D1 & D_2 \\ 0 & D_2 & D_1 \end{array} \cdot \begin{array}{|c|} v \\ w \\ u \end{array} $
void dmat_yHx (const dmatrix *A, const double *p, const double *q, const double *b, const double *d0, const double *d1, const double *d2, const double *x, double *y)
 $ y = Hx = \begin{array}{|ccc|} b^TD_0b & b^TD_0\tilde{A} & 0 \\ \tilde{A}^TD_0b & \tilde{A}^TD_0\tilde{A}+D1 & D_2 \\ 0 & D_2 & D_1 \end{array} \cdot \begin{array}{|c|} v \\ w \\ u \end{array} \quad \mbox{ ,where } \tilde{A} = A - pq $
void dmat_elemAA (const dmatrix *A, dmatrix **B)
 $ B_{ij} = A_{ij}^2 $
void dmat_diagscale (dmatrix *M, const double *dl, const int invl, const double *dr, const int invr)
 $ M = D_L M D_R $
void dmat_diagadd (dmatrix *M, const double *d)
 $ M = M+\mbox{diag}(d) $
void dmat_colsum (const dmatrix *M, double *y)
 $ y_j = \sum_{i} M_{ij} $
void dmat_colavg (const dmatrix *M, double *y)
 $ y_j = (1/m)\sum_{i} M_{ij} $
void dmat_colstd (const dmatrix *M, const double *a, double *s)
 $ s_j = \left(\sum_{i}(M_{ij}-a_j)^2/(m-1)\right)^2 $
void dmat_duplicate (const dmatrix *M, dmatrix **dst)
 Duplicates a matrix.
void dmat_copy (const dmatrix *M, dmatrix *dst)
 Copies the contents of a matrix.
void dmat_new_dense (dmatrix **M, const int m, const int n)
 Allocate memory for a dense matrix.
void dmat_free (dmatrix *M)
 Free memory for a matrix.
void dmat_vprint (const int n, const double *v)
 Shows the content of a vector.
void dmat_print (const dmatrix *M)
 Shows the content of a matrix.
void dmat_get_row (const dmatrix *M, const int rowidx, double *dst)
void dmat_summary (dmatrix *M)
 Shows the summary of a matrix.
void dmat_build_idx (dmatrix *M)
 Build row index from csr info. (jdx and rdx).
void dmat_B_AAT (dmatrix *A, dmatrix *B)
 $ B = AA^T $
void dmat_B_ATA (dmatrix *A, dmatrix *B)
 $ B = A^TA $
void dmat_A_axxTpA (double a, double *x, dmatrix *A)
 $ A = \alpha x x^T +A $
void dmat_potrs (const dmatrix *A, double *b)
 $ Ax = b $
void dmat_posv (const dmatrix *A, double *b)
 $ Ax = b $
void dmat_profile ()


Detailed Description

Source file for matrix and vector manipulation functions.


Function Documentation

void dmat_A_axxTpA ( double  a,
double *  x,
dmatrix A 
)

$ A = \alpha x x^T +A $

Computes a row-rank update of a dense matrix.

Parameters:
a scalar.
x pointer to a vector.
A pointer to a matrix.

References dmatrix::n, and dmatrix::val.

Referenced by compute_searchdir_chol_thin().

void dmat_B_AAT ( dmatrix A,
dmatrix B 
)

$ B = AA^T $

Computes dense matrix-matrix-transpose product.

Parameters:
A pointer to a matrix.
B pointer to a result matrix.

References dmatrix::m, dmatrix::n, and dmatrix::val.

Referenced by compute_searchdir_chol_fat().

void dmat_B_ATA ( dmatrix A,
dmatrix B 
)

$ B = A^TA $

Computes dense matrix-transpose-matrix product.

Parameters:
A pointer to a matrix.
B pointer to a result matrix.

References dmatrix::m, dmatrix::n, and dmatrix::val.

Referenced by compute_searchdir_chol_thin().

void dmat_build_idx ( dmatrix M  ) 

Build row index from csr info. (jdx and rdx).

Parameters:
M pointer to a matrix.

References dmatrix::idx, dmatrix::m, dmatrix::nz, and dmatrix::rdx.

Referenced by main().

void dmat_colavg ( const dmatrix M,
double *  y 
)

$ y_j = (1/m)\sum_{i} M_{ij} $

Computes column average of a matrix.

Parameters:
M pointer to a matrix.
y pointer to a result vector (column average).

References dmat_vset(), dmatrix::idx, dmatrix::jdx, dmatrix::m, dmatrix::n, dmatrix::nz, and dmatrix::val.

Referenced by standardize_data().

void dmat_colstd ( const dmatrix M,
const double *  a,
double *  s 
)

$ s_j = \left(\sum_{i}(M_{ij}-a_j)^2/(m-1)\right)^2 $

Computes column standard-deviation of a matrix.

Parameters:
M pointer to a matrix.
a pointer to a column average vector.
s pointer to a result vector (column std-dev).

References dmat_vset(), dmatrix::jdx, dmatrix::m, dmatrix::n, dmatrix::nz, and dmatrix::val.

Referenced by standardize_data().

void dmat_colsum ( const dmatrix M,
double *  y 
)

$ y_j = \sum_{i} M_{ij} $

Computes column sum of a matrix.

Parameters:
M pointer to a matrix.
y pointer to a result vector (column sum).

References dmat_vset(), dmatrix::idx, dmatrix::jdx, dmatrix::m, dmatrix::n, dmatrix::nz, and dmatrix::val.

void dmat_copy ( const dmatrix M,
dmatrix dst 
)

Copies the contents of a matrix.

Only copies the contents, NOT allocate memory.

Parameters:
M pointer to a source matrix.
dst pointer to a destination matrix.

References dmatrix::idx, dmatrix::jdx, dmatrix::m, dmatrix::n, dmatrix::nz, dmatrix::rdx, and dmatrix::val.

Referenced by compute_searchdir_chol_fat(), compute_searchdir_chol_thin(), find_lambdamax(), and l1_logreg_train().

void dmat_diagadd ( dmatrix M,
const double *  d 
)

$ M = M+\mbox{diag}(d) $

Adds diagonal entries to a matrix.

Parameters:
M pointer to a matrix.
d pointer to a diagonal vector.

References dmatrix::m, dmatrix::n, dmatrix::nz, and dmatrix::val.

Referenced by compute_searchdir_chol_fat(), and compute_searchdir_chol_thin().

void dmat_diagscale ( dmatrix M,
const double *  dl,
const int  invl,
const double *  dr,
const int  invr 
)

$ M = D_L M D_R $

Computes left and right diagonal scaleing of M.

\begin{eqnarray*} M &=& D_L M \quad\mbox{ ,if dr = NULL }\\ M &=& M D_R \quad\mbox{ ,if dl = NULL }\\ M &=& D_L M D_R \quad\mbox{ ,otherwise } \end{eqnarray*}

Parameters:
M pointer to a matrix.
dl pointer to a diagonal vector.
invl inverse flag of left diagonal matrix.

\begin{eqnarray*} D_L &=& \mbox{diag}(dl) \quad\mbox{ ,if invl} = 0\\ D_L &=& \mbox{diag}(1./dl) \quad\mbox{ ,if invl} = 1 \end{eqnarray*}

dr pointer to a diagonal vector.
invr inverse flag of right diagonal matrix.

\begin{eqnarray*} D_R &=& \mbox{diag}(dr) \quad\mbox{ ,if invr} = 0\\ D_R &=& \mbox{diag}(1./dr) \quad\mbox{ ,if invr} = 1 \end{eqnarray*}

References dmatrix::idx, dmatrix::jdx, dmatrix::m, dmatrix::n, dmatrix::nz, and dmatrix::val.

Referenced by compute_searchdir_chol_fat(), compute_searchdir_chol_thin(), find_lambdamax(), l1_logreg_train(), and standardize_data().

double dmat_dot ( const int  n,
const double *  x,
const double *  y 
)

$ x^Ty $

Returns dot product of a vector x and y.

Parameters:
n length of a vector x.
x pointer to a vector x.
y pointer to a vector y.
Returns:
result.

Referenced by backtracking_linesearch(), compute_searchdir_chol_fat(), compute_searchdir_chol_thin(), and l1_logreg_train().

void dmat_duplicate ( const dmatrix M,
dmatrix **  dst 
)

Duplicates a matrix.

Allocates all the memory and set sizes as in M, but DO NOT copy the contents. To copy the contents, call dmat_copy.

Parameters:
M pointer to a source matrix.
dst pointer to a destination matrix.

References dmatrix::idx, dmatrix::jdx, dmatrix::m, dmatrix::n, dmatrix::nz, dmatrix::rdx, and dmatrix::val.

Referenced by find_lambdamax(), and l1_logreg_train().

void dmat_elemAA ( const dmatrix A,
dmatrix **  B 
)

$ B_{ij} = A_{ij}^2 $

Computes elementwise matrix square.

Parameters:
A pointer to a matrix.
B pointer to a result matrix.

References dmatrix::idx, dmatrix::jdx, dmatrix::m, dmatrix::n, dmatrix::nz, dmatrix::rdx, and dmatrix::val.

Referenced by l1_logreg_train().

void dmat_elemdivi ( const int  n,
const double *  x,
const double *  y,
double *  z 
)

$ z_i = x_i/y_i $

Computes elementwise division of vectors.

NOTE: y = x./y is not allowed, i.e., w should not be the same with y.

Parameters:
n length of a vector x.
x pointer to a vector x.
y pointer to a vector y.
z pointer to a result vector z.

Referenced by l1_logreg_train(), and standardize_data().

void dmat_elemprod ( const int  n,
const double *  x,
const double *  y,
double *  z 
)

$ z_i = x_i*y_i $

Computes elementwise product of vectors.

NOTE: x = x.*y is not allowed, i.e., w should not be the same with x.

Parameters:
n length of a vector x.
x pointer to a vector x.
y pointer to a vector y.
z pointer to a result vector z.

Referenced by compute_searchdir_chol_fat(), and compute_searchdir_pcg().

void dmat_free ( dmatrix M  ) 

Free memory for a matrix.

Parameters:
M pointer to a matrix to be freed.

References dmatrix::idx, dmatrix::jdx, dmatrix::rdx, and dmatrix::val.

Referenced by find_lambdamax(), and main().

void dmat_new_dense ( dmatrix **  M,
const int  m,
const int  n 
)

Allocate memory for a dense matrix.

Parameters:
M pointer to an allocated matrix.
m size of matrix (row).
n size of matrix (column).

References dmatrix::idx, dmatrix::jdx, dmatrix::m, dmatrix::n, dmatrix::nz, dmatrix::rdx, and dmatrix::val.

Referenced by l1_logreg_train().

double dmat_norm1 ( const int  n,
const double *  x 
)

$ \|x\|_1 $

Returns 1-norm of a vector x.

Parameters:
n length of a vector x.
x pointer to a vector x.
Returns:
result.

Referenced by l1_logreg_train().

double dmat_norm2 ( const int  n,
const double *  x 
)

$ \|x\|_2 $

Returns 2-norm of a vector x.

Parameters:
n length of a vector x.
x pointer to a vector x.
Returns:
result.

Referenced by compute_searchdir_pcg().

double dmat_norminf ( const int  n,
const double *  x 
)

$ \|x\|_{\infty} $

Returns infinity-norm of a vector x

Parameters:
n length of a vector x
x pointer to a vector x
Returns:
result.

Referenced by find_lambdamax(), and l1_logreg_train().

void dmat_posv ( const dmatrix A,
double *  b 
)

$ Ax = b $

Computes the solution of a linear system Ax = b via Cholesky method. Wrapper function to dposv. That is, A should be a Cholesky factor computed by potrs.

Parameters:
A pointer to a matrix (Cholesky factor).
b pointer to a vector.

References dmatrix::m, and dmatrix::val.

Referenced by compute_searchdir_chol_fat(), and compute_searchdir_chol_thin().

void dmat_potrs ( const dmatrix A,
double *  b 
)

$ Ax = b $

Computes the solution of a linear system Ax = b via Cholesky method. Wrapper function to dports. That is, the cholesky factor is stored in A after computation.

Parameters:
A pointer to a matrix.
b pointer to a vector.

References dmatrix::m, and dmatrix::val.

Referenced by compute_searchdir_chol_fat().

void dmat_print ( const dmatrix M  ) 

Shows the content of a matrix.

It only shows at most 10 x 10 elements of a matrix.

Parameters:
M pointer to a matrix.

References dmatrix::idx, dmatrix::jdx, dmatrix::m, dmatrix::n, dmatrix::nz, dmatrix::rdx, and dmatrix::val.

void dmat_summary ( dmatrix M  ) 

Shows the summary of a matrix.

  • sparsity.
  • size.
  • number of non-zero elements.

Parameters:
M pointer to a matrix.

References dmatrix::m, dmatrix::n, and dmatrix::nz.

void dmat_vcopy ( const int  n,
const double *  src,
double *  dst 
)

$ \mbox{dst} \leftarrow \mbox{src} $

Copies a vector.

Parameters:
n length of vectors.
src pointer to a source vector.
dst pointer to a destination vector.

Referenced by backtracking_linesearch(), l1_logreg_train(), and standardize_data().

void dmat_vprint ( const int  n,
const double *  v 
)

Shows the content of a vector.

It only shows at most 10 elements of a vector.

Parameters:
n size of a vector.
v pointer to a vector.

void dmat_vset ( int  n,
const double  val,
double *  dst 
)

$ \mbox{dst}_i \leftarrow \mbox{val} $

Sets all the elements of a vector with a constant value.

Parameters:
n length of a vector.
val constant value to set.
dst pointer to a vector.

Referenced by compute_searchdir_pcg(), dmat_colavg(), dmat_colstd(), dmat_colsum(), dmat_waxpby(), dmat_yAmpqTx(), dmat_yATx(), dmat_yHx(), dmat_yHx_(), l1_logreg_train(), and standardize_data().

void dmat_waxpby ( int  n,
double  alpha,
const double *  x,
double  beta,
const double *  y,
double *  w 
)

$ w = \alpha*x + \beta*y $

Computes weighted vector sum.

  • w = -x
  • w = alpha*x
  • w = -x + y
  • w = alpha*x + y
  • w = -x - y
  • w = alpha*x - y
  • w = alpha*x + beta*y

Parameters:
n length of vectors.
alpha constant
x pointer to a vector.
beta constant
y pointer to a vector.
w pointer to a result vector.

References dmat_vset().

Referenced by backtracking_linesearch(), compute_searchdir_chol_fat(), compute_searchdir_pcg(), l1_logreg_train(), and optimize_intercept().

void dmat_yAmpqTx ( const dmatrix A,
const double *  p,
const double *  q,
const double *  x,
double *  y 
)

$ y = (A-pq)^Tx $

Computes transposed row-rank updated matrix-vector product.

Parameters:
A pointer to a matrix.
p pointer to a column vector.
q pointer to a row vector.
x pointer to a vector x.
y pointer to a result vector y.

References dmat_vset(), dmat_yATx(), dmatrix::jdx, dmatrix::m, dmatrix::n, dmatrix::nz, dmatrix::rdx, and dmatrix::val.

Referenced by compute_searchdir_pcg(), find_lambdamax(), and l1_logreg_train().

void dmat_yAmpqx ( const dmatrix A,
const double *  p,
const double *  q,
const double *  x,
double *  y 
)

$ y = (A-pq)x $

Computes row-rank updated matrix-vector product.

Parameters:
A pointer to a matrix.
p pointer to a column vector.
q pointer to a row vector.
x pointer to a vector x.
y pointer to a result vector y.

References dmat_yAx(), dmatrix::jdx, dmatrix::m, dmatrix::n, dmatrix::nz, dmatrix::rdx, and dmatrix::val.

Referenced by backtracking_linesearch(), and l1_logreg_train().

void dmat_yATDAx ( const dmatrix A,
const double *  d,
const double *  x,
double *  y 
)

$ y = A^TDAx $

Computes triple-matrix product.

Parameters:
A pointer to a matrix.
d pointer to a diagonal vector $ D = diag(d) $
x pointer to a vector x.
y pointer to a result vector y.

References dmatrix::jdx, dmatrix::m, dmatrix::n, dmatrix::nz, dmatrix::rdx, and dmatrix::val.

void dmat_yATx ( const dmatrix A,
const double *  x,
double *  y 
)

$ y = A^Tx $

Computes transposed matrixr-vector product.

Parameters:
A pointer to a matrix.
x pointer to a vector x.
y pointer to a result vector y.

References dmat_vset(), dmatrix::jdx, dmatrix::m, dmatrix::n, dmatrix::nz, dmatrix::rdx, and dmatrix::val.

Referenced by compute_searchdir_chol_fat(), compute_searchdir_chol_thin(), compute_searchdir_pcg(), and dmat_yAmpqTx().

void dmat_yAx ( const dmatrix A,
const double *  x,
double *  y 
)

$ y = Ax $

Computes matrix-vector product.

Parameters:
A pointer to a matrix.
x pointer to a vector x.
y pointer to a result vector y.

References dmatrix::jdx, dmatrix::m, dmatrix::n, dmatrix::nz, dmatrix::rdx, and dmatrix::val.

Referenced by compute_searchdir_chol_fat(), dmat_yAmpqx(), and l1_logreg_classify().

void dmat_yexpx ( const int  n,
const double *  x,
double *  y 
)

$ y_i = \exp(x_i) $

Computes elementwise exp() of a vector.

Parameters:
n length of vectors.
x pointer to a source vector.
y pointer to a destination vector.

Referenced by backtracking_linesearch(), and optimize_intercept().

void dmat_yHx ( const dmatrix A,
const double *  p,
const double *  q,
const double *  b,
const double *  d0,
const double *  d1,
const double *  d2,
const double *  x,
double *  y 
)

$ y = Hx = \begin{array}{|ccc|} b^TD_0b & b^TD_0\tilde{A} & 0 \\ \tilde{A}^TD_0b & \tilde{A}^TD_0\tilde{A}+D1 & D_2 \\ 0 & D_2 & D_1 \end{array} \cdot \begin{array}{|c|} v \\ w \\ u \end{array} \quad \mbox{ ,where } \tilde{A} = A - pq $

Computes complex matrix-vector product.

Parameters:
A pointer to a matrix.
p pointer to a column vector.
q pointer to a row vector.
b pointer to a vector.
d0 pointer to a vector.
d1 pointer to a vector.
d2 pointer to a vector.
x pointer to a vector.
y pointer to a result vector.

References dmat_vset(), dmat_yHx_(), dmatrix::jdx, dmatrix::m, dmatrix::n, dmatrix::nz, dmatrix::rdx, and dmatrix::val.

void dmat_yHx_ ( const dmatrix A,
const double *  b,
const double *  d0,
const double *  d1,
const double *  d2,
const double *  x,
double *  y 
)

$ y = Hx = \begin{array}{|ccc|} b^TD_0b & b^TD_0A & 0 \\ A^TD_0b & A^TD_0A+D1 & D_2 \\ 0 & D_2 & D_1 \end{array} \cdot \begin{array}{|c|} v \\ w \\ u \end{array} $

Computes complex matrix-vector product.

Parameters:
A pointer to a matrix.
b pointer to a vector.
d0 pointer to a vector.
d1 pointer to a vector.
d2 pointer to a vector.
x pointer to a vector.
y pointer to a result vector.

References dmat_vset(), dmatrix::jdx, dmatrix::m, dmatrix::n, dmatrix::nz, dmatrix::rdx, and dmatrix::val.

Referenced by dmat_yHx().

void dmat_yinvx ( const int  n,
const double *  x,
double *  y 
)

$ y_i = 1/x_i $

Computes elementwise inv() of a vector.

Parameters:
n length of vectors.
x pointer to a source vector.
y pointer to a destination vector.

Referenced by backtracking_linesearch(), compute_searchdir_chol_fat(), and optimize_intercept().

void dmat_ysqrtx ( const int  n,
const double *  x,
double *  y 
)

$ y_i = x_i^{1/2} $

Computes elementwise sqrt() of a vector.

Parameters:
n length of vectors.
x pointer to a source vector.
y pointer to a destination vector.

Referenced by compute_searchdir_chol_thin().


Generated on Mon May 25 19:15:19 2009 for l1_logreg by Doxygen 1.5.5