GClasses
GClasses::GVec Class Reference

Contains some useful functions for operating on vectors. More...

#include <GVec.h>

Public Member Functions

 GVec (size_t n=0)
 
 ~GVec ()
 
void resize (size_t n)
 Resizes this vector. More...
 

Static Public Member Functions

static void add (double *pDest, const double *pSource, size_t nDims)
 Adds pSource to pDest. More...
 
static void addInterpolatedFunction (double *pOut, size_t nOutVals, double *pIn, size_t nInVals)
 Adds the function pIn to pOut after interpolating pIn to be the same size as pOut. More...
 
static void addLog (double *pDest, const double *pSource, size_t nDims)
 Adds the log of each element in pSource to pDest. More...
 
static void addScaled (double *pDest, double dMag, const double *pSource, size_t nDims)
 Adds dMag * pSource to pDest. More...
 
static void capValues (double *pVec, double cap, size_t dims)
 Sets each value, v, to MIN(cap, v) More...
 
static void copy (double *pDest, const double *pSource, size_t nDims)
 This just wraps memcpy. More...
 
static double correlation (const double *pA, const double *pB, size_t nDims)
 Computes the cosine of the angle between two vectors (the origin is the vertex) More...
 
static double correlation (const double *pOriginA, const double *pTargetA, const double *pB, size_t nDims)
 Computes the cosine of the angle between two vectors (the origin is the vertex) More...
 
static double correlation (const double *pOriginA, const double *pTargetA, const double *pOriginB, const double *pTargetB, size_t nDims)
 Computes the cosine of the angle between two vectors (the origin is the vertex) More...
 
static void deserialize (double *pVec, GDomListIterator &it)
 Load the vector from a text format. pVec must be large enough to contain all of the elements that remain in "it". More...
 
static bool doesContainUnknowns (const double *pVector, size_t nSize)
 This returns true if the vector contains any unknown values. Most of the methods in this class will give bad results if a vector contains unknown values, but for efficiency reasons, they don't check. So it's your job to check your vectors first. More...
 
static double dotProduct (const double *pA, const double *pB, size_t nSize)
 Computes the dot product of two vectors. Results are undefined if pA or pB contain unknown values. More...
 
static double dotProduct (const double *pOrigin, const double *pTarget, const double *pVector, size_t nSize)
 Computes the dot product of (pTarget - pOrigin) with pVector. More...
 
static double dotProduct (const double *pOriginA, const double *pTargetA, const double *pOriginB, const double *pTargetB, size_t nSize)
 Computes the dot product of (pTargetA - pOriginA) with (pTargetB - pOriginB). More...
 
static double dotProductIgnoringUnknowns (const double *pA, const double *pB, size_t nSize)
 Computes the dot product of two vectors, ignoring any unknown values. More...
 
static double dotProductIgnoringUnknowns (const double *pOrigin, const double *pTarget, const double *pVector, size_t nSize)
 Computes the dot product of (pTarget - pOrigin) with pVector. Unknown values in pTarget will simply be ignored. (pOrigin and pVector must not contain any unknown values.) More...
 
static double estimateSquaredDistanceWithUnknowns (const double *pA, const double *pB, size_t nDims)
 Estimates the squared distance between two points that may have some missing values. It assumes the distance in missing dimensions is approximately the same as the average distance in other dimensions. If there are no known dimensions that overlap between the two points, it returns 1e50. More...
 
static void floorValues (double *pVec, double floor, size_t dims)
 Sets each value, v, to MAX(floor, v) More...
 
static void fromImage (GImage *pImage, double *pVec, int width, int height, int channels, double range)
 Converts an image to a vector of rasterized pixel values. channels must be 1 or 3 (for grayscale or rgb) range specifies the range of channel values. Typical values are 1.0 or 255.0. Pixels are visited in reading order (left-to-right, top-to-bottom). pVec must be big enough to hold width * height * channels values. More...
 
static size_t indexOfMax (const double *pVector, size_t dims, GRand *pRand=NULL)
 Returns the index of the max value in pVector. If multiple elements have have an equivalent max value, then behavior depends on the value of pRand. If pRand is NULL, it will pick the first one. If pRand is non-NULL, it will uniformly pick from all the ties. More...
 
static size_t indexOfMaxMagnitude (const double *pVector, size_t dims, GRand *pRand)
 Returns the index of the value with the largest magnitude in pVector. If multiple elements have have an equivalent magnitude, it randomly (uniformly) picks from all the ties. More...
 
static size_t indexOfMin (const double *pVector, size_t dims, GRand *pRand=NULL)
 Returns the index of the min value in pVector. If multiple elements have have an equivalent max value, then behavior depends on the value of pRand. If pRand is NULL, it will pick the first one. If pRand is non-NULL, it will uniformly pick from all the ties. More...
 
static void interpolateIndexes (size_t nIndexes, double *pInIndexes, double *pOutIndexes, float fRatio, size_t nCorrIndexes, double *pCorrIndexes1, double *pCorrIndexes2)
 Interpolates (morphs) a set of indexes from one function to another. pInIndexes, pCorrIndexes1, and pCorrIndexes2 are all expected to be in sorted order. All indexes should be >= 0 and < nDims. fRatio is the interpolation ratio such that if fRatio is zero, all indexes left unchanged, and as fRatio approaches one, the indexes are interpolated linearly such that each index in pCorrIndexes1 is interpolated linearly to the corresponding index in pCorrIndexes2. If the two extremes are not in the list of corresponding indexes, the ends may drift. More...
 
static double lNormDistance (double norm, const double *pA, const double *pB, size_t dims)
 Computes L-Norm distance (norm=1 is manhattan distance, norm=2 is Euclidean distance, norm=infinity is Chebychev, etc.) More...
 
static double lNormMagnitude (double norm, const double *pVector, size_t nSize)
 Computes the magnitude in L-Norm distance (norm=1 is manhattan distance, norm=2 is Euclidean distance, norm=infinity is Chebychev, etc.) More...
 
static void lNormNormalize (double norm, double *pVector, size_t nSize)
 Normalizes with L-Norm distance (norm=1 is manhattan distance, norm=2 is Euclidean distance, norm=infinity is Chebychev, etc.) More...
 
static void multiply (double *pVector, double dScalar, size_t nDims)
 Multiplies pVector by dScalar. More...
 
static void normalize (double *pVector, size_t nSize)
 Normalizes this vector to a magnitude of 1. Throws an exception if the magnitude is zero. More...
 
static void pairwiseDivide (double *pDest, double *pOther, size_t dims)
 Divides each element in pDest by the corresponding element in pOther. More...
 
static void pairwiseMultiply (double *pDest, double *pOther, size_t dims)
 Multiplies each element in pDest by the corresponding element in pOther. More...
 
static void perturb (double *pDest, double deviation, size_t dims, GRand &rand)
 Adds Gaussian noise with the specified deviation to each element in the matrix. More...
 
static void pow (double *pVector, double dScalar, size_t nDims)
 Raises each element of pVector to the exponent dScalar. More...
 
static void print (std::ostream &stream, int precision, const double *pVec, size_t dims)
 Prints the values in the vector separated by ", ". precision specifies the number of digits to print. More...
 
static void project (double *pDest, const double *pPoint, const double *pOrigin, const double *pBasis, size_t basisCount, size_t dims)
 Projects pPoint onto the hyperplane defined by pOrigin onto the basisCount basis vectors specified by pBasis. (The basis vectors are assumed to be chained end-to-end in a big vector.) More...
 
static double refinePoint (double *pPoint, double *pNeighbor, size_t dims, double distance, double learningRate, GRand *pRand)
 Moves "pPoint" so that it is closer to a distance of "distance" from "pNeighbor". "learningRate" specifies how much to move it (0=not at all, 1=all the way). Returns the squared distance between pPoint and pNeighbor. More...
 
static void regularize_1 (double *pVector, double amount, size_t nDims)
 Adjusts each element in the direction toward 0 by the specified amount. More...
 
static void regularize_1_5 (double *pVector, double amount, size_t nDims)
 Apply L^(1.5) regularization to the specified vector. More...
 
static void rotate (double *pVector, size_t nDims, double dAngle, const double *pA, const double *pB)
 Rotates pVector by dAngle radians in the plane defined by the orthogonal axes pA and pB. More...
 
static void safeNormalize (double *pVector, size_t nSize, GRand *pRand)
 Normalizes this vector to a magnitude of 1. If the magnitude is zero, it returns a random vector. More...
 
static GDomNodeserialize (GDom *pDoc, const double *pVec, size_t dims)
 Write the vector to a text format. More...
 
static void setAll (double *pVector, double value, size_t dims)
 Sets all the elements to the specified value. More...
 
static void smallestToFront (double *pVec, size_t k, size_t size, double *pParallel1=NULL, size_t *pParallel2=NULL, double *pParallel3=NULL)
 Moves the smallest k values to the front of the vector, and the biggest (size - k) values to the end of the vector. (For efficiency, no other guarantees about ordering are made.) This has an average-case runtime that is linear with respect to size. pParallel1 and pParallel2 are optional arrays that should be arranged to keep their indices in sync with pVec. More...
 
static double squaredDistance (const double *pA, const double *pB, size_t nDims)
 Computes the squared distance between two vectors. More...
 
static double squaredMagnitude (const double *pVector, size_t nSize)
 Computes the squared magnitude of the vector. More...
 
static void subtract (double *pDest, const double *pSource, size_t nDims)
 Subtracts pSource from pDest. More...
 
static void subtractComponent (double *pInOut, const double *pBasis, size_t dims)
 Subtracts the component of pInOut that projects onto pBasis. (Assumes that pBasis is normalized.) This might be used, for example, to implement the modified Gram-Schmidt process. More...
 
static void subtractComponent (double *pInOut, const double *pOrigin, const double *pTarget, size_t dims)
 Subtracts the component of pInOut that projects onto (pTarget - pOrigin). This might be used, for example, to implement the modified Gram-Schmidt process. More...
 
static double sumAbsoluteValues (const double *pVec, size_t dims)
 Returns the sum of the absolute values of all the elements. More...
 
static double sumElements (const double *pVec, size_t dims)
 Returns the sum of all the elements. More...
 
static void sumToOne (double *pVector, size_t size)
 Scale the vector so that the elements sum to 1. More...
 
static void test ()
 Performs unit tests for this class. Throws an exception if there is a failure. More...
 
static void toImage (const double *pVec, GImage *pImage, int width, int height, int channels, double range)
 Converts a vector of rasterized pixel values to an image. channels must be 1 or 3 (for grayscale or rgb) range specifies the range of channel values. Typical values are 1.0 or 255.0. Pixels are visited in reading order (left-to-right, top-to-bottom). More...
 

Public Attributes

double * v
 

Detailed Description

Contains some useful functions for operating on vectors.

Constructor & Destructor Documentation

GClasses::GVec::GVec ( size_t  n = 0)
GClasses::GVec::~GVec ( )

Member Function Documentation

static void GClasses::GVec::add ( double *  pDest,
const double *  pSource,
size_t  nDims 
)
static

Adds pSource to pDest.

static void GClasses::GVec::addInterpolatedFunction ( double *  pOut,
size_t  nOutVals,
double *  pIn,
size_t  nInVals 
)
static

Adds the function pIn to pOut after interpolating pIn to be the same size as pOut.

static void GClasses::GVec::addLog ( double *  pDest,
const double *  pSource,
size_t  nDims 
)
static

Adds the log of each element in pSource to pDest.

static void GClasses::GVec::addScaled ( double *  pDest,
double  dMag,
const double *  pSource,
size_t  nDims 
)
static

Adds dMag * pSource to pDest.

static void GClasses::GVec::capValues ( double *  pVec,
double  cap,
size_t  dims 
)
static

Sets each value, v, to MIN(cap, v)

static void GClasses::GVec::copy ( double *  pDest,
const double *  pSource,
size_t  nDims 
)
static

This just wraps memcpy.

static double GClasses::GVec::correlation ( const double *  pA,
const double *  pB,
size_t  nDims 
)
static

Computes the cosine of the angle between two vectors (the origin is the vertex)

static double GClasses::GVec::correlation ( const double *  pOriginA,
const double *  pTargetA,
const double *  pB,
size_t  nDims 
)
static

Computes the cosine of the angle between two vectors (the origin is the vertex)

static double GClasses::GVec::correlation ( const double *  pOriginA,
const double *  pTargetA,
const double *  pOriginB,
const double *  pTargetB,
size_t  nDims 
)
static

Computes the cosine of the angle between two vectors (the origin is the vertex)

static void GClasses::GVec::deserialize ( double *  pVec,
GDomListIterator it 
)
static

Load the vector from a text format. pVec must be large enough to contain all of the elements that remain in "it".

static bool GClasses::GVec::doesContainUnknowns ( const double *  pVector,
size_t  nSize 
)
static

This returns true if the vector contains any unknown values. Most of the methods in this class will give bad results if a vector contains unknown values, but for efficiency reasons, they don't check. So it's your job to check your vectors first.

static double GClasses::GVec::dotProduct ( const double *  pA,
const double *  pB,
size_t  nSize 
)
static

Computes the dot product of two vectors. Results are undefined if pA or pB contain unknown values.

static double GClasses::GVec::dotProduct ( const double *  pOrigin,
const double *  pTarget,
const double *  pVector,
size_t  nSize 
)
static

Computes the dot product of (pTarget - pOrigin) with pVector.

static double GClasses::GVec::dotProduct ( const double *  pOriginA,
const double *  pTargetA,
const double *  pOriginB,
const double *  pTargetB,
size_t  nSize 
)
static

Computes the dot product of (pTargetA - pOriginA) with (pTargetB - pOriginB).

static double GClasses::GVec::dotProductIgnoringUnknowns ( const double *  pA,
const double *  pB,
size_t  nSize 
)
static

Computes the dot product of two vectors, ignoring any unknown values.

static double GClasses::GVec::dotProductIgnoringUnknowns ( const double *  pOrigin,
const double *  pTarget,
const double *  pVector,
size_t  nSize 
)
static

Computes the dot product of (pTarget - pOrigin) with pVector. Unknown values in pTarget will simply be ignored. (pOrigin and pVector must not contain any unknown values.)

static double GClasses::GVec::estimateSquaredDistanceWithUnknowns ( const double *  pA,
const double *  pB,
size_t  nDims 
)
static

Estimates the squared distance between two points that may have some missing values. It assumes the distance in missing dimensions is approximately the same as the average distance in other dimensions. If there are no known dimensions that overlap between the two points, it returns 1e50.

static void GClasses::GVec::floorValues ( double *  pVec,
double  floor,
size_t  dims 
)
static

Sets each value, v, to MAX(floor, v)

static void GClasses::GVec::fromImage ( GImage pImage,
double *  pVec,
int  width,
int  height,
int  channels,
double  range 
)
static

Converts an image to a vector of rasterized pixel values. channels must be 1 or 3 (for grayscale or rgb) range specifies the range of channel values. Typical values are 1.0 or 255.0. Pixels are visited in reading order (left-to-right, top-to-bottom). pVec must be big enough to hold width * height * channels values.

static size_t GClasses::GVec::indexOfMax ( const double *  pVector,
size_t  dims,
GRand pRand = NULL 
)
static

Returns the index of the max value in pVector. If multiple elements have have an equivalent max value, then behavior depends on the value of pRand. If pRand is NULL, it will pick the first one. If pRand is non-NULL, it will uniformly pick from all the ties.

static size_t GClasses::GVec::indexOfMaxMagnitude ( const double *  pVector,
size_t  dims,
GRand pRand 
)
static

Returns the index of the value with the largest magnitude in pVector. If multiple elements have have an equivalent magnitude, it randomly (uniformly) picks from all the ties.

static size_t GClasses::GVec::indexOfMin ( const double *  pVector,
size_t  dims,
GRand pRand = NULL 
)
static

Returns the index of the min value in pVector. If multiple elements have have an equivalent max value, then behavior depends on the value of pRand. If pRand is NULL, it will pick the first one. If pRand is non-NULL, it will uniformly pick from all the ties.

static void GClasses::GVec::interpolateIndexes ( size_t  nIndexes,
double *  pInIndexes,
double *  pOutIndexes,
float  fRatio,
size_t  nCorrIndexes,
double *  pCorrIndexes1,
double *  pCorrIndexes2 
)
static

Interpolates (morphs) a set of indexes from one function to another. pInIndexes, pCorrIndexes1, and pCorrIndexes2 are all expected to be in sorted order. All indexes should be >= 0 and < nDims. fRatio is the interpolation ratio such that if fRatio is zero, all indexes left unchanged, and as fRatio approaches one, the indexes are interpolated linearly such that each index in pCorrIndexes1 is interpolated linearly to the corresponding index in pCorrIndexes2. If the two extremes are not in the list of corresponding indexes, the ends may drift.

static double GClasses::GVec::lNormDistance ( double  norm,
const double *  pA,
const double *  pB,
size_t  dims 
)
static

Computes L-Norm distance (norm=1 is manhattan distance, norm=2 is Euclidean distance, norm=infinity is Chebychev, etc.)

static double GClasses::GVec::lNormMagnitude ( double  norm,
const double *  pVector,
size_t  nSize 
)
static

Computes the magnitude in L-Norm distance (norm=1 is manhattan distance, norm=2 is Euclidean distance, norm=infinity is Chebychev, etc.)

static void GClasses::GVec::lNormNormalize ( double  norm,
double *  pVector,
size_t  nSize 
)
static

Normalizes with L-Norm distance (norm=1 is manhattan distance, norm=2 is Euclidean distance, norm=infinity is Chebychev, etc.)

static void GClasses::GVec::multiply ( double *  pVector,
double  dScalar,
size_t  nDims 
)
static

Multiplies pVector by dScalar.

static void GClasses::GVec::normalize ( double *  pVector,
size_t  nSize 
)
static

Normalizes this vector to a magnitude of 1. Throws an exception if the magnitude is zero.

static void GClasses::GVec::pairwiseDivide ( double *  pDest,
double *  pOther,
size_t  dims 
)
static

Divides each element in pDest by the corresponding element in pOther.

static void GClasses::GVec::pairwiseMultiply ( double *  pDest,
double *  pOther,
size_t  dims 
)
static

Multiplies each element in pDest by the corresponding element in pOther.

static void GClasses::GVec::perturb ( double *  pDest,
double  deviation,
size_t  dims,
GRand rand 
)
static

Adds Gaussian noise with the specified deviation to each element in the matrix.

static void GClasses::GVec::pow ( double *  pVector,
double  dScalar,
size_t  nDims 
)
static

Raises each element of pVector to the exponent dScalar.

static void GClasses::GVec::print ( std::ostream &  stream,
int  precision,
const double *  pVec,
size_t  dims 
)
static

Prints the values in the vector separated by ", ". precision specifies the number of digits to print.

static void GClasses::GVec::project ( double *  pDest,
const double *  pPoint,
const double *  pOrigin,
const double *  pBasis,
size_t  basisCount,
size_t  dims 
)
static

Projects pPoint onto the hyperplane defined by pOrigin onto the basisCount basis vectors specified by pBasis. (The basis vectors are assumed to be chained end-to-end in a big vector.)

static double GClasses::GVec::refinePoint ( double *  pPoint,
double *  pNeighbor,
size_t  dims,
double  distance,
double  learningRate,
GRand pRand 
)
static

Moves "pPoint" so that it is closer to a distance of "distance" from "pNeighbor". "learningRate" specifies how much to move it (0=not at all, 1=all the way). Returns the squared distance between pPoint and pNeighbor.

static void GClasses::GVec::regularize_1 ( double *  pVector,
double  amount,
size_t  nDims 
)
static

Adjusts each element in the direction toward 0 by the specified amount.

static void GClasses::GVec::regularize_1_5 ( double *  pVector,
double  amount,
size_t  nDims 
)
static

Apply L^(1.5) regularization to the specified vector.

void GClasses::GVec::resize ( size_t  n)

Resizes this vector.

static void GClasses::GVec::rotate ( double *  pVector,
size_t  nDims,
double  dAngle,
const double *  pA,
const double *  pB 
)
static

Rotates pVector by dAngle radians in the plane defined by the orthogonal axes pA and pB.

static void GClasses::GVec::safeNormalize ( double *  pVector,
size_t  nSize,
GRand pRand 
)
static

Normalizes this vector to a magnitude of 1. If the magnitude is zero, it returns a random vector.

static GDomNode* GClasses::GVec::serialize ( GDom pDoc,
const double *  pVec,
size_t  dims 
)
static

Write the vector to a text format.

static void GClasses::GVec::setAll ( double *  pVector,
double  value,
size_t  dims 
)
static

Sets all the elements to the specified value.

static void GClasses::GVec::smallestToFront ( double *  pVec,
size_t  k,
size_t  size,
double *  pParallel1 = NULL,
size_t *  pParallel2 = NULL,
double *  pParallel3 = NULL 
)
static

Moves the smallest k values to the front of the vector, and the biggest (size - k) values to the end of the vector. (For efficiency, no other guarantees about ordering are made.) This has an average-case runtime that is linear with respect to size. pParallel1 and pParallel2 are optional arrays that should be arranged to keep their indices in sync with pVec.

static double GClasses::GVec::squaredDistance ( const double *  pA,
const double *  pB,
size_t  nDims 
)
static

Computes the squared distance between two vectors.

static double GClasses::GVec::squaredMagnitude ( const double *  pVector,
size_t  nSize 
)
static

Computes the squared magnitude of the vector.

static void GClasses::GVec::subtract ( double *  pDest,
const double *  pSource,
size_t  nDims 
)
static

Subtracts pSource from pDest.

static void GClasses::GVec::subtractComponent ( double *  pInOut,
const double *  pBasis,
size_t  dims 
)
static

Subtracts the component of pInOut that projects onto pBasis. (Assumes that pBasis is normalized.) This might be used, for example, to implement the modified Gram-Schmidt process.

static void GClasses::GVec::subtractComponent ( double *  pInOut,
const double *  pOrigin,
const double *  pTarget,
size_t  dims 
)
static

Subtracts the component of pInOut that projects onto (pTarget - pOrigin). This might be used, for example, to implement the modified Gram-Schmidt process.

static double GClasses::GVec::sumAbsoluteValues ( const double *  pVec,
size_t  dims 
)
static

Returns the sum of the absolute values of all the elements.

static double GClasses::GVec::sumElements ( const double *  pVec,
size_t  dims 
)
static

Returns the sum of all the elements.

static void GClasses::GVec::sumToOne ( double *  pVector,
size_t  size 
)
static

Scale the vector so that the elements sum to 1.

static void GClasses::GVec::test ( )
static

Performs unit tests for this class. Throws an exception if there is a failure.

static void GClasses::GVec::toImage ( const double *  pVec,
GImage pImage,
int  width,
int  height,
int  channels,
double  range 
)
static

Converts a vector of rasterized pixel values to an image. channels must be 1 or 3 (for grayscale or rgb) range specifies the range of channel values. Typical values are 1.0 or 255.0. Pixels are visited in reading order (left-to-right, top-to-bottom).

Member Data Documentation

double* GClasses::GVec::v