My Project
 All Classes Namespaces Files Functions Variables Enumerations Enumerator Friends Macros
Macros | Functions
matrixArrayCalcs.h File Reference

matrixArrayCalcs defines various utility functions for use with Matrix objects and array objects. More...

#include "myOutput.h"
#include "math.h"
#include "string.h"
#include <fstream>
#include <newmatap.h>
#include <newmatio.h>

Go to the source code of this file.

Macros

#define isnan(a)   ((fabs(a) >= 90000) || (a != a))
 
#define sign(a)   ((a > 0) - (a < 0))
 
#define PI   3.14159265358979
 

Functions

int minVal (const int *values, const int numValues)
 
int maxVal (const int *values, const int numValues)
 
Matrix conv2 (const Matrix &A, const Matrix &H)
 
void interp2 (double *xpts, double *ypts, const Matrix Z, double *xi, double *yi, double *zi, int numPts)
 
void interp2mat (double *xpts, double *ypts, const Matrix Z, double *xi, double *yi, Matrix &zi)
 
void nearestInterp (double *xpts, double *ypts, const Matrix &zvals, double xi, double yi, double &zi, int *xIndices, int *yIndices, ColumnVector &Weights)
 
bool findNearestValid (const Matrix &zvals, int &xIndex, int &yIndex)
 
void bilinearInterp (double *xpts, double *ypts, const Matrix &zvals, double xi, double yi, double &zi, int *xIndices, int *yIndices, ColumnVector &Weights)
 
void bicubicInterp (double *xpts, double *ypts, const Matrix &zvals, double xi, double yi, double &zi, int *xIndices, int *yIndices, ColumnVector &Weights)
 
void splineInterp (double *xpts, double *ypts, const Matrix &zvals, double xi, double yi, double &zi, int *xIndices, int *yIndices, ColumnVector &Weights)
 
void nearestInterp_mat (double *xpts, double *ypts, const Matrix &zvals, double *xi, double *yi, Matrix &zi, Matrix &var)
 
void bilinearInterp_mat (double *xpts, double *ypts, const Matrix &zvals, double *xi, double *yi, Matrix &zi, Matrix &var)
 
void bicubicInterp_mat (double *xpts, double *ypts, const Matrix &zvals, double *xi, double *yi, Matrix &zi, Matrix &var)
 
void splineInterp_mat (double *xpts, double *ypts, const Matrix &zvals, double *xi, double *yi, Matrix &zi, Matrix &var)
 
int closestPt (double key, const double *base, size_t nmemb)
 
int closestPtUniformArray (double key, double firstVal, double lastVal, size_t nmemb)
 
int lowerBound (double val, const double *vec, int numVals)
 
double unif (double mean, double halfInterval)
 
double unif_zeroMean (const double &halfInterval)
 
double unif_zeroOne ()
 
double randn (double mean, double stddev)
 
double randn_zeroMean (const double &stddev)
 
char * charCat (char *dest, const char *front, const char *back)
 
double computeKLdiv_gaussian_mat (double *xpts, double *ypts, const Matrix &refPDF, double *mu, const Matrix &Cov)
 
SymmetricMatrix computeMatrixSqrt (const SymmetricMatrix &A)
 
void computeArrayCrossProd (double *a, double *b, double *result)
 

Detailed Description

matrixArrayCalcs defines various utility functions for use with Matrix objects and array objects.

Author
Debbie Meduna
Date
01/01/08

Macro Definition Documentation

#define isnan (   a)    ((fabs(a) >= 90000) || (a != a))
#define PI   3.14159265358979
#define sign (   a)    ((a > 0) - (a < 0))

Function Documentation

void bicubicInterp ( double *  xpts,
double *  ypts,
const Matrix &  zvals,
double  xi,
double  yi,
double &  zi,
int *  xIndices,
int *  yIndices,
ColumnVector &  Weights 
)

Uses bicubic interpolation to determine zi at the point (xi,yi) from the set of data xpts, ypts, and zvals. If zvals is size m x n, xpts is size m x 1 and ypts is size n x 1. The function modifies the the entries in xIndices, yIndices and W. xIndices and yIndices are px1 arrays containing the indices of xpts and ypts used to generate zi. The method used for bicubic interpolation is very similar to that described in Numerical Recipes in C++. If a 4x4 bounding box does not exist for the current (xi,yi) data point, OR the interpolated point is NaN, bilinearInterp() is used.

void bicubicInterp_mat ( double *  xpts,
double *  ypts,
const Matrix &  zvals,
double *  xi,
double *  yi,
Matrix &  zi,
Matrix &  var 
)

Interpolates all points in the vectors xi, yi to fill the matrix zi using the data in xpts, ypts and Z. Calls bicubicInterp() for each point in the xi and yi arrays to perform this interpolation.

void bilinearInterp ( double *  xpts,
double *  ypts,
const Matrix &  zvals,
double  xi,
double  yi,
double &  zi,
int *  xIndices,
int *  yIndices,
ColumnVector &  Weights 
)

Uses bilinear interpolation to determine zi at the point (xi,yi) from the set of data xpts, ypts, and zvals. If zvals is size m x n, xpts is size m x 1 and ypts is size n x 1. The function modifies the the entries in xIndices, yIndices and W. xIndices and yIndices are 4x1 arrays containing the indices of xpts and ypts used to generate zi. W is a 4x1 column vector of corresponding weights where: zi = sum_i W(i,1)*zvals(xIndices[i]+1,yIndices[i]+1). If a 2x2 bounding box does not exist for the current (xi,yi) data point (i.e., it is out of the bounds of Z), OR the interpolated point is NaN, nearestInterp() is used.

void bilinearInterp_mat ( double *  xpts,
double *  ypts,
const Matrix &  zvals,
double *  xi,
double *  yi,
Matrix &  zi,
Matrix &  var 
)

Interpolates all points in the vectors xi, yi to fill the matrix zi using the data in xpts, ypts and Z. Calls bilinearInterp() for each point in the xi and yi arrays to perform this interpolation.

char* charCat ( char *  dest,
const char *  front,
const char *  back 
)

This function is used to concatenate two character arrays (front and back) into a new character array, dest. This differs from the C standard function strcat() in that the returned character array is an entirely new location in memory, separate from the front and back arrays. The length of the dest char array must be greater than or equal to the sum of the lengths of the front and back arrays.

int closestPt ( double  key,
const double *  base,
size_t  nmemb 
)

Finds the index into the array "pts" which is closest to "value". This function is copied from the function nearest() in mapio.cpp.

int closestPtUniformArray ( double  key,
double  firstVal,
double  lastVal,
size_t  nmemb 
)
inline

Finds the index closest to "value" for the array whose first element is "firstVal", last element is "lastVal", and has size "nmemb". This function assumes that the array specified is uniformly spaced and takes advantage of this to perform a faster search than closestPt().

void computeArrayCrossProd ( double *  a,
double *  b,
double *  result 
)
inline

This function computes the cross product: array1 x array2, and returns the result in result. The arrays are assumed to have 3 elements.

double computeKLdiv_gaussian_mat ( double *  xpts,
double *  ypts,
const Matrix &  refPDF,
double *  mu,
const Matrix &  Cov 
)

This function computes the KL divergence between a proposal distribution and a Gaussian distribution with mean vector mu and covariance C. The proposal distribution is described by the matrix P, with corresponding (x,y) values given in the vectors xpts and ypts.

SymmetricMatrix computeMatrixSqrt ( const SymmetricMatrix &  A)

This function computes the matrix square root of the given matrix, A. The given matrix and the returned matrix are both symmetric matrices. The sqrt is computed by first performing the eigenvalue decomposition of the matrix A, square rooting the resulting eigenvalues, and reconstructing the matrix to get Asqrt.

Matrix conv2 ( const Matrix &  A,
const Matrix &  H 
)

Computes the 2D convolution of A with filter matrix H. The returned matrix B is the same size as A. H is assumed to be smaller than A.

bool findNearestValid ( const Matrix &  zvals,
int &  xIndex,
int &  yIndex 
)

Finds the nearest valid (non-NaN) point in the grid zvals to the point (xIndex, yIndex). Replaces xIndex and yIndex with the indices corresponding to this found location. Returns a boolean indicating if a non-NaN point was successfully found.

void interp2 ( double *  xpts,
double *  ypts,
const Matrix  Z,
double *  xi,
double *  yi,
double *  zi,
int  numPts 
)

Interpolates the matrix Z to the (x,y) point pairs in arrays xi and yi. N indicates the number of point pairs to be interpolated. The arrays xpts and ypts contain the (x,y) values corresponding to entries in matrix Z. If Z is size m x n, xpts is size m x 1 and ypts is size n x 1. Bilinear interpolation is used for this function.

void interp2mat ( double *  xpts,
double *  ypts,
const Matrix  Z,
double *  xi,
double *  yi,
Matrix &  zi 
)

Interpolates the matrix Z to a new matrix zi over the (x,y) points in the arrays xi and yi. Nx and Ny indicates the length of these arrays. The arrays xpts and ypts contain the (x,y) values corresponding to entries in matrix Z. If Z is size m x n, xpts is size m x 1 and ypts is size n x 1. This function calls interp2() for performing interpolations.

int lowerBound ( double  val,
const double *  vec,
int  numVals 
)

Finds the lowest index into the array "vec" which bounds the value "val". This function does NOT check that the returned index is a valid index into the array.

int maxVal ( const int *  values,
const int  numValues 
)

Finds and returns the maximum number in the integer array values.

int minVal ( const int *  values,
const int  numValues 
)

Rounds the number given to the nearest integer and returns the integer.

Finds and returns the minimum number in the integer array values.

void nearestInterp ( double *  xpts,
double *  ypts,
const Matrix &  zvals,
double  xi,
double  yi,
double &  zi,
int *  xIndices,
int *  yIndices,
ColumnVector &  Weights 
)

Uses nearest-neighbor interpolation to determine zi at the point (xi,yi) from the set of data xpts, ypts, and zvals. If zvals is size m x n, xpts is size m x 1 and ypts is size n x 1. The function modifies the the entries in xIndices, yIndices and W. xIndices and yIndices are px1 arrays containing the indices of xpts and ypts used to generate zi. W is a px1 matrix of corresponding weights where: zi = sum_i W(i,1)*zvals(xIndices[i]+1,yIndices[i]+1). If the nearest point is NaN, the closest non-NaN point is found.

void nearestInterp_mat ( double *  xpts,
double *  ypts,
const Matrix &  zvals,
double *  xi,
double *  yi,
Matrix &  zi,
Matrix &  var 
)

Interpolates all points in the vectors xi, yi to fill the matrix zi using the data in xpts, ypts and Z. Calls nearestInterp() for each point in the xi and yi arrays to perform this interpolation.

double randn ( double  mean,
double  stddev 
)

Uses a polar form Box-Muller Transform to generate a pseudorandom number from a gaussian distribution with specificied mean and standard deviation. Generates two random numbers for each call and stores the unused number for the next call to this function.

double randn_zeroMean ( const double &  stddev)
inline

Uses a polar form Box-Muller Transform to generate a pseudorandom number from a gaussian distribution with zero mean and specified standard deviation. Generates two random numbers for each call and stores the unused number for the next call to this function.

void splineInterp ( double *  xpts,
double *  ypts,
const Matrix &  zvals,
double  xi,
double  yi,
double &  zi,
int *  xIndices,
int *  yIndices,
ColumnVector &  Weights 
)

Uses spline interpolation to determine zi at the point (xi,yi) from the set of data xpts, ypts, and zvals. If zvals is size m x n, xvals is size m x 1 and ypts is size n x 1. The function modifies the the entries in xIndices, yIndices and W. xIndices and yIndices are px1 arrays containing the indices of xpts and ypts used to generate zi. Currently, this function is only implemented if a Matlab engine is running. Otherwise, this function calls nearestInterp().

void splineInterp_mat ( double *  xpts,
double *  ypts,
const Matrix &  zvals,
double *  xi,
double *  yi,
Matrix &  zi,
Matrix &  var 
)

Interpolates all points in the vectors xi, yi to fill the matrix zi using the data in xpts, ypts and Z. Currently, this function is only implemented if a Matlab engine is running. Otherwise, this function calls nearestInterp_mat().

double unif ( double  mean,
double  halfInterval 
)
inline

Generates a pseudorandom number from a uniform distribution with given mean and halfInterval width.

double unif_zeroMean ( const double &  halfInterval)
inline

Generates a pseudorandom number from a uniform distribution with zero mean and given halfInterval width.

double unif_zeroOne ( )
inline

Generates a pseudorandom number from a uniform distribution with zero mean and halfInterval width of 1.