GClasses
|
Represents a mathematical vector of doubles.
#include <GVec.h>
Public Member Functions | |
GVec (size_t n=0) | |
General-purpose constructor. n specifies the initial size of the vector. More... | |
GVec (int n) | |
General-purpose constructor. n specifies the initial size of the vector. More... | |
GVec (GDomNode *pNode) | |
Unmarshaling constructor. More... | |
GVec (const GVec &orig) | |
Copy constructor. Copies all the values in orig. More... | |
~GVec () | |
Copies all the values in orig. More... | |
void | addScaled (double scalar, const GVec &that) |
Adds scalar * that to this vector. More... | |
void | clip (double min, double max) |
Clips all values in this vector to fall in the range [min, max]. More... | |
void | copy (const GVec &orig) |
Copies all the values in orig. More... | |
double | correlation (const GVec &that) const |
Returns the cosine of the angle between this and that (with the origin as the common vertex). More... | |
double * | data () |
Returns a pointer to the raw element values. More... | |
const double * | data () const |
Returns a const pointer to the raw element values. More... | |
void | deserialize (const GDomNode *pNode) |
Unmarshals this vector from a DOM. More... | |
double | dotProduct (const GVec &that) const |
Returns the dot product of this and that. More... | |
double | dotProductIgnoringUnknowns (const GVec &that) const |
Returns the dot product of this and that, ignoring elements in which either vector has UNKNOWN_REAL_VALUE. More... | |
double | dotProductIgnoringUnknowns (const GVec &from, const GVec &to) const |
Returns the dot product of this with (to - from), ignoring elements in which any vector has UNKNOWN_REAL_VALUE. More... | |
void | erase (size_t start, size_t count=1) |
Erases the specified elements. The remaining elements are shifted over. The size of the vector is decreased, but the buffer is not reallocated (so this operation wastes some memory to save a little time). More... | |
double | estimateSquaredDistanceWithUnknowns (const GVec &that) const |
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... | |
void | fill (const double val, size_t startPos=0, size_t endPos=(size_t)-1) |
Sets all the elements in this vector to val. More... | |
void | fillNormal (GRand &rand, double deviation=1.0) |
Fills with random values from a Gaussian distribution. More... | |
void | fillSimplex (GRand &rand) |
Fills with random values uniformly distributed within a probability simplex. In other words, the values will sum to 1, will all be non-negative, and will not be biased toward or away from any of the extreme corners. More... | |
void | fillSphericalShell (GRand &rand, double radius=1.0) |
Fills with random values on the surface of a sphere. More... | |
void | fillSphericalVolume (GRand &rand) |
Fills with random values uniformly distributed within a sphere of radius 1. More... | |
void | fillUniform (GRand &rand, double min=0.0, double max=1.0) |
Fills with random values from a uniform distribution. More... | |
void | fromImage (GImage *pImage, int width, int height, int channels, double range) |
Encode an image in this vector by rastering across 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). More... | |
size_t | indexOfMax (size_t startPos=0, size_t endPos=(size_t)-1) const |
Returns the index of the max element. The returned value will be >= startPos. The returned value will be < endPos. More... | |
size_t | indexOfMin (size_t startPos=0, size_t endPos=(size_t)-1) const |
Returns the index of the min element. The returned value with be >= startPos. the returned value will be < endPos. More... | |
double | max (size_t startPos=0, size_t endPos=(size_t)-1) const |
Returns the max element. If you want the location, call indexOfMax The returned value with be >= startPos. the returned value will be < endPos. More... | |
double | mean () const |
Returns the mean of all of the elements in this vector. More... | |
double | min (size_t startPos=0, size_t endPos=(size_t)-1) const |
Returns the min element. If you want the location, call indexOfMin The returned value with be >= startPos. the returned value will be < endPos. More... | |
void | normalize () |
Scales this vector to have a magnitude of 1.0. More... | |
GVec | operator* (double scalar) const |
Makes a scaled version of this vector. More... | |
GVec & | operator*= (double scalar) |
Scales this vector. More... | |
GVec & | operator*= (const GVec &that) |
Scales this vector. More... | |
GVec | operator+ (const GVec &that) const |
Adds two vectors to make a new one. More... | |
GVec & | operator+= (const GVec &that) |
Adds another vector to this one. More... | |
GVec & | operator+= (const double scalar) |
Adds a scalar to each element in this vector. More... | |
GVec | operator- (const GVec &that) const |
Subtracts a vector from this one to make a new one. More... | |
GVec & | operator-= (const GVec &that) |
Subtracts another vector from this one. More... | |
GVec & | operator/= (double scalar) |
Scales this vector. More... | |
double & | operator[] (size_t index) |
Returns a reference to the specified element. More... | |
const double & | operator[] (size_t index) const |
Returns a const reference to the specified element. More... | |
void | perturbNormal (GRand &rand, double deviation=1.0) |
Adds Gaussian noise to each element of this vector. More... | |
void | print (std::ostream &stream=std::cout, char separator= ',') const |
Prints a representation of this vector to the specified stream. More... | |
void | put (size_t pos, const GVec &that, size_t start=0, size_t length=(size_t)-1) |
Puts a copy of that at the specified location in this. Throws an exception if it does not fit there. pos is the destination starting position. start is the source starting position. length is the source and destination length. More... | |
void | regularizeL1 (double amount) |
Applies L1 regularization to this vector. More... | |
void | resize (size_t n) |
Resizes this vector. More... | |
GDomNode * | serialize (GDom *pDoc) const |
Marshals this vector into a DOM node. More... | |
void | set (const double *pSource, size_t size) |
Sets the data in this vector. More... | |
size_t | size () const |
Returns the size of this vector. More... | |
double | squaredDistance (const GVec &that) const |
Returns the squared Euclidean distance between this and that vector. More... | |
double | squaredMagnitude () const |
Returns the squared Euclidean magnitude of this vector. More... | |
void | subtractComponent (const GVec &component) |
Subtracts a component from this vector. Uses the Gram-Schmidt approach. (Assumes component is already normalized.) More... | |
double | sum () const |
Returns the sum of the elements in this vector. More... | |
void | sumToOne () |
Scales this vector such that the elements sum to 1.0. More... | |
void | swapContents (GVec &that) |
Swaps the contents of this vector with that vector. More... | |
std::string | to_str (char separator= ',') const |
Returns a string representation of this vector. More... | |
void | toImage (GImage *pImage, int width, int height, int channels, double range) const |
Decode this vector into 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... | |
Static Public Member Functions | |
static void | fill (double *pVector, double value, size_t dims) |
static void | perturb (double *pDest, double deviation, size_t dims, GRand &rand) |
Adds Gaussian noise with the specified deviation to each element in the vector. More... | |
static void | setAll (double *pVector, double value, size_t dims) |
Sets all the elements to the specified value. More... | |
static void | test () |
Performs unit tests for this class. Throws an exception if there is a failure. More... | |
Protected Attributes | |
double * | m_data |
size_t | m_size |
Friends | |
class | GConstVecWrapper |
class | GVecWrapper |
GClasses::GVec::GVec | ( | size_t | n = 0 | ) |
General-purpose constructor. n specifies the initial size of the vector.
GClasses::GVec::GVec | ( | int | n | ) |
General-purpose constructor. n specifies the initial size of the vector.
GClasses::GVec::GVec | ( | GDomNode * | pNode | ) |
Unmarshaling constructor.
GClasses::GVec::GVec | ( | const GVec & | orig | ) |
Copy constructor. Copies all the values in orig.
GClasses::GVec::~GVec | ( | ) |
Copies all the values in orig.
void GClasses::GVec::addScaled | ( | double | scalar, |
const GVec & | that | ||
) |
Adds scalar * that to this vector.
void GClasses::GVec::clip | ( | double | min, |
double | max | ||
) |
Clips all values in this vector to fall in the range [min, max].
void GClasses::GVec::copy | ( | const GVec & | orig | ) |
Copies all the values in orig.
double GClasses::GVec::correlation | ( | const GVec & | that | ) | const |
Returns the cosine of the angle between this and that (with the origin as the common vertex).
|
inline |
Returns a pointer to the raw element values.
|
inline |
Returns a const pointer to the raw element values.
void GClasses::GVec::deserialize | ( | const GDomNode * | pNode | ) |
Unmarshals this vector from a DOM.
double GClasses::GVec::dotProduct | ( | const GVec & | that | ) | const |
Returns the dot product of this and that.
double GClasses::GVec::dotProductIgnoringUnknowns | ( | const GVec & | that | ) | const |
Returns the dot product of this and that, ignoring elements in which either vector has UNKNOWN_REAL_VALUE.
Returns the dot product of this with (to - from), ignoring elements in which any vector has UNKNOWN_REAL_VALUE.
void GClasses::GVec::erase | ( | size_t | start, |
size_t | count = 1 |
||
) |
Erases the specified elements. The remaining elements are shifted over. The size of the vector is decreased, but the buffer is not reallocated (so this operation wastes some memory to save a little time).
double GClasses::GVec::estimateSquaredDistanceWithUnknowns | ( | const GVec & | that | ) | const |
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.
void GClasses::GVec::fill | ( | const double | val, |
size_t | startPos = 0 , |
||
size_t | endPos = (size_t)-1 |
||
) |
Sets all the elements in this vector to val.
|
static |
void GClasses::GVec::fillNormal | ( | GRand & | rand, |
double | deviation = 1.0 |
||
) |
Fills with random values from a Gaussian distribution.
void GClasses::GVec::fillSimplex | ( | GRand & | rand | ) |
Fills with random values uniformly distributed within a probability simplex. In other words, the values will sum to 1, will all be non-negative, and will not be biased toward or away from any of the extreme corners.
void GClasses::GVec::fillSphericalShell | ( | GRand & | rand, |
double | radius = 1.0 |
||
) |
Fills with random values on the surface of a sphere.
void GClasses::GVec::fillSphericalVolume | ( | GRand & | rand | ) |
Fills with random values uniformly distributed within a sphere of radius 1.
void GClasses::GVec::fillUniform | ( | GRand & | rand, |
double | min = 0.0 , |
||
double | max = 1.0 |
||
) |
Fills with random values from a uniform distribution.
void GClasses::GVec::fromImage | ( | GImage * | pImage, |
int | width, | ||
int | height, | ||
int | channels, | ||
double | range | ||
) |
Encode an image in this vector by rastering across 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).
size_t GClasses::GVec::indexOfMax | ( | size_t | startPos = 0 , |
size_t | endPos = (size_t)-1 |
||
) | const |
Returns the index of the max element. The returned value will be >= startPos. The returned value will be < endPos.
size_t GClasses::GVec::indexOfMin | ( | size_t | startPos = 0 , |
size_t | endPos = (size_t)-1 |
||
) | const |
Returns the index of the min element. The returned value with be >= startPos. the returned value will be < endPos.
double GClasses::GVec::max | ( | size_t | startPos = 0 , |
size_t | endPos = (size_t)-1 |
||
) | const |
Returns the max element. If you want the location, call indexOfMax The returned value with be >= startPos. the returned value will be < endPos.
double GClasses::GVec::mean | ( | ) | const |
Returns the mean of all of the elements in this vector.
double GClasses::GVec::min | ( | size_t | startPos = 0 , |
size_t | endPos = (size_t)-1 |
||
) | const |
Returns the min element. If you want the location, call indexOfMin The returned value with be >= startPos. the returned value will be < endPos.
void GClasses::GVec::normalize | ( | ) |
Scales this vector to have a magnitude of 1.0.
GVec GClasses::GVec::operator* | ( | double | scalar | ) | const |
Makes a scaled version of this vector.
GVec& GClasses::GVec::operator*= | ( | double | scalar | ) |
Scales this vector.
GVec& GClasses::GVec::operator+= | ( | const double | scalar | ) |
Adds a scalar to each element in this vector.
Subtracts a vector from this one to make a new one.
GVec& GClasses::GVec::operator/= | ( | double | scalar | ) |
Scales this vector.
|
inline |
Returns a reference to the specified element.
|
inline |
Returns a const reference to the specified element.
|
static |
Adds Gaussian noise with the specified deviation to each element in the vector.
void GClasses::GVec::perturbNormal | ( | GRand & | rand, |
double | deviation = 1.0 |
||
) |
Adds Gaussian noise to each element of this vector.
void GClasses::GVec::print | ( | std::ostream & | stream = std::cout , |
char | separator = ',' |
||
) | const |
Prints a representation of this vector to the specified stream.
void GClasses::GVec::put | ( | size_t | pos, |
const GVec & | that, | ||
size_t | start = 0 , |
||
size_t | length = (size_t)-1 |
||
) |
Puts a copy of that at the specified location in this. Throws an exception if it does not fit there. pos is the destination starting position. start is the source starting position. length is the source and destination length.
void GClasses::GVec::regularizeL1 | ( | double | amount | ) |
Applies L1 regularization to this vector.
void GClasses::GVec::resize | ( | size_t | n | ) |
Resizes this vector.
void GClasses::GVec::set | ( | const double * | pSource, |
size_t | size | ||
) |
Sets the data in this vector.
|
static |
Sets all the elements to the specified value.
|
inline |
Returns the size of this vector.
double GClasses::GVec::squaredDistance | ( | const GVec & | that | ) | const |
Returns the squared Euclidean distance between this and that vector.
double GClasses::GVec::squaredMagnitude | ( | ) | const |
Returns the squared Euclidean magnitude of this vector.
void GClasses::GVec::subtractComponent | ( | const GVec & | component | ) |
Subtracts a component from this vector. Uses the Gram-Schmidt approach. (Assumes component is already normalized.)
double GClasses::GVec::sum | ( | ) | const |
Returns the sum of the elements in this vector.
void GClasses::GVec::sumToOne | ( | ) |
Scales this vector such that the elements sum to 1.0.
void GClasses::GVec::swapContents | ( | GVec & | that | ) |
Swaps the contents of this vector with that vector.
|
static |
Performs unit tests for this class. Throws an exception if there is a failure.
std::string GClasses::GVec::to_str | ( | char | separator = ',' | ) | const |
Returns a string representation of this vector.
void GClasses::GVec::toImage | ( | GImage * | pImage, |
int | width, | ||
int | height, | ||
int | channels, | ||
double | range | ||
) | const |
Decode this vector into 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).
|
friend |
|
friend |
|
protected |
|
protected |