KJB
|
This class implements vectors, in the linear-algebra sense, with real-valued elements. More...
#include <m_vector.h>
Public Types | |
typedef double | Value_type |
data type of the elements More... | |
typedef kjb_c::Vector | Impl_type |
the underlying implementation More... | |
typedef Matrix | Mat_type |
the associated matrix type More... | |
typedef Generic_matrix_view < Matrix > | Mat_view_type |
view type of the associated matrix type More... | |
typedef Vector | Vec_type |
the associated vector type More... | |
typedef Value_type(* | Mapper )(Value_type) |
element transformer fun More... | |
typedef double | value_type |
typedef double * | pointer |
typedef const double * | const_pointer |
typedef double & | reference |
typedef double | const_reference |
typedef value_type * | iterator |
typedef const value_type * | const_iterator |
typedef std::reverse_iterator < iterator > | reverse_iterator |
typedef std::reverse_iterator < const_iterator > | const_reverse_iterator |
const Iterator type More... | |
typedef int | size_type |
typedef int | difference_type |
Public Member Functions | |
Vector (int length=0) | |
Allocate vector of given length; contents are uninitialized. More... | |
Vector (unsigned length) | |
Allocate vector of given length; contents are uninitialized. More... | |
Vector (unsigned long length) | |
Allocate vector of given length; contents are uninitialized. More... | |
Vector (int length, Value_type num) | |
Allocate vector of given length; initialize all elts to 'num'. More... | |
Vector (int length, const Value_type *data) | |
Initialize from an array of doubles, of given length. More... | |
Vector (const Int_vector &) | |
Ctor builds a vector from one with integer values. More... | |
Vector (int length, const float *data) | |
Initialize from an array of floats, of given length. More... | |
Vector (Impl_type *vec_ptr) | |
Conversion ctor: claim ownership of an existing vector pointer (i.e., make a shallow copy). More... | |
Vector (const std::vector< double > &src) | |
Conversion ctor: Create from stl-style vector. More... | |
Vector (Value_type val) | |
Create a single-element vector. More... | |
Vector (const Vector_view &view) | |
Create a vector from a view of another vector. More... | |
Vector (const Const_vector_view &view) | |
Create a vector from a view of another vector. More... | |
Vector (Value_type val1, Value_type val2) | |
Create a two-vector. More... | |
Vector (Value_type val1, Value_type val2, Value_type val3) | |
Create a three-vector. More... | |
Vector (Value_type val1, Value_type val2, Value_type val3, Value_type val4) | |
Create a four-vector. More... | |
Vector (const Impl_type &vec_ref) | |
Ctor copies contents (i.e., deep copy) of an existing vector. More... | |
Vector (const Mat_type &src) | |
Ctor builds from a one-row or one-column Matrix (making a deep copy). More... | |
Vector (const Mat_view_type &src) | |
Ctor builds from a one-row or one-column view of a Matrix (making a deep copy). More... | |
Vector (const std::string &file_name) | |
Construct a vector by reading contents from a named file. More... | |
Vector (const Vector &vec_ref) | |
Copy ctor – calls the kjb_c function to copy a vector. More... | |
template<typename InputIterator_ > | |
Vector (InputIterator_ begin_, InputIterator_ end_) | |
"Range constructor" More... | |
~Vector () | |
Destructor – which just calls the KJB destructor. More... | |
int | get_length () const |
Return the length of the vector. More... | |
size_type | size () const |
Alias to get_length(). Required to comply with stl Container concept. More... | |
size_type | max_size () const |
Maximum size vector can ever have. Currently defined as INT_MAX. More... | |
bool | empty () const |
Returns true iff size is zero. Required to comply with stl Container concept. More... | |
iterator | begin () |
const_iterator | begin () const |
iterator | end () |
const_iterator | end () const |
reverse_iterator | rbegin () |
const_reverse_iterator | rbegin () const |
reverse_iterator | rend () |
const_reverse_iterator | rend () const |
void | reserve (int capacity) |
Vector & | randomize (int length) |
Clobber current vector; resize and fill with random values. More... | |
Vector & | randomize () |
Clobber current vector contents with random values. More... | |
Vector & | zero_out (int length) |
Clone of randomize(int) More... | |
Vector & | zero_out () |
Clobber current vector contents with zeroes. More... | |
Vector & | set (Value_type val) |
Clone of zero_out(int) More... | |
Vector & | set (Value_type val1, Value_type val2) |
Convert to a 2-vector and set its values to the arguments. More... | |
Vector & | set (Value_type val1, Value_type val2, Value_type val3) |
Convert to a 3-vector and set its values to the arguments. More... | |
Vector & | set (Value_type val1, Value_type val2, Value_type val3, Value_type val4) |
Convert to a 4-vector and set its values to the arguments. More... | |
Vector & | operator= (const Mat_view_type &vec_ref) |
Assignment operator: assign from a single-row matrix-view. More... | |
Vector & | operator= (const Impl_type &vec_ref) |
Assignment operator: assign from a kjb_c::Vector, a C struct. More... | |
Vector & | operator= (const Vector &src) |
Assignment operator: assign from a kjb::Vector, a C++ object. More... | |
Vector & | replace (const Vector &iv, int offset, int begin, int length) |
Copy elements from input vector iv into this vector, starting at some offset, leaving the rest of this vector unchanged. More... | |
const Impl_type * | get_c_vector () const |
Get const pointer to the underlying kjb_c::Vector C struct. More... | |
Impl_type *& | get_underlying_representation_with_guilt () |
Get pointer to the underlying kjb_c::Vector C struct. More... | |
Impl_type * | get_underlying_representation_unsafe () |
Get pointer to the underlying kjb_c::Vector C struct. More... | |
void | swap (Vector &other) |
Swap the representations of two vectors. More... | |
Vector & | resize (int new_length, Value_type pad=Value_type(0)) |
Resize vector, retaining previous values. More... | |
Value_type & | operator[] (int i) |
Subscript vector like a C array, e.g., A[10], returning an lvalue. More... | |
const Value_type & | operator[] (int i) const |
Subscript vector like a C array, e.g., A[10], returning an rvalue. More... | |
Value_type & | operator() (int i) |
Fortran-style vector subscript, e.g., A(10), to get an lvalue. More... | |
const Value_type & | operator() (int i) const |
Fortran-style vector subscript, e.g., A(10), to get an rvalue. More... | |
Vector_view | operator[] (const Index_range &idx) |
Const_vector_view | operator[] (const Index_range &idx) const |
void | check_bounds (int i) const |
Test whether a subscript is valid, throw an exception if not. More... | |
Value_type & | at (int i) |
Safely subscript vector, e.g., A.at(10), to get an lvalue. More... | |
const Value_type & | at (int i) const |
Safely subscript vector, e.g., A.at(10), to get an rvalue. More... | |
iterator | insert (iterator position, value_type t) |
A copy of t is inserted before position. More... | |
void | insert (iterator position, size_type N, value_type t) |
Insert N copies of t before position. More... | |
template<typename InputIterator > | |
void | insert (iterator position, InputIterator begin_, InputIterator end_) |
Insert range [begin, end) before position. More... | |
void | assign (size_type N, value_type t) |
Resizes to N, filling all values with t. More... | |
template<typename InputIterator > | |
void | assign (InputIterator begin_, InputIterator end_) |
Assign this vector to the values in sequence (begin_, and_) More... | |
iterator | erase (iterator position) |
erase element at position and shift all elements after p up by 1. More... | |
void | erase (iterator begin_, iterator end_) |
erase all elements beginning with begin_ up to but not including end_ More... | |
void | clear () |
delete all elements from vector More... | |
reference | front () |
Returns the first element of the vector. More... | |
const_reference | front () const |
Returns the first element of the vector. More... | |
reference | back () |
Returns the last element of the vector. More... | |
const_reference | back () const |
Returns the last element of the vector. More... | |
void | push_back (Value_type x) |
inserts an element at the back of the vector in amortized constant time. More... | |
void | pop_back () |
Returns the last element of the vector. More... | |
void | write_row (const char *filename=0) const |
Write vector as a row to a file, or to standard output. More... | |
void | write_col (const char *filename=0) const |
Write vector as a column to a file, or to standard output. More... | |
void | read (const char *filename=0) |
Read vector from a file, or from standard input. More... | |
void | write (const char *filename=0) const |
Write vector to a file, or to standard output. More... | |
Vector & | operator*= (Value_type op2) |
Scalar multiply self, in-place, e.g., v *= 6. More... | |
Vector & | multiply (Value_type op2) |
Scalar multiply self, in-place, just like v *= 6. More... | |
Vector & | ew_multiply (const Vector &op2) |
Element wise multiplication of two vectors. More... | |
Vector & | operator/= (Value_type op2) |
Scalar division of self, in-place. More... | |
Vector & | divide (Value_type op2) |
Scalar division of self, in-place, just like v /= 2. More... | |
Vector & | ew_divide (const Vector &op2) |
Element wise division of two vectors; result is stored in first. More... | |
Vector & | operator+= (const Vector &op2) |
Add vector to self, in-place, e.g., v += delta_v. More... | |
Vector & | operator+= (double x) |
Add scalar to self, in-place, e.g., v -= delta_v. More... | |
Vector & | add (const Vector &op2) |
Add vector to self, in-place, just like v += delta_v. More... | |
Vector & | operator-= (const Vector &op2) |
Subtract vector from self, in-place, e.g., v -= delta_v. More... | |
Vector & | operator-= (double x) |
Subtract scalar from self, in-place, e.g., v -= delta_v. More... | |
Vector & | subtract (const Vector &op2) |
Subtract vector from self, in-place, just like v -= delta_v. More... | |
Vector & | negate () |
Negate self, in-place, just like v *= (-1). More... | |
Vec_type & | mapcar (Mapper) |
Transform the elements of a vector. More... | |
Value_type | min (int *min_index) const |
Find minimum element in the vector, both its value and index. More... | |
Value_type | max (int *max_index) const |
Find maximum element in the vector, both its value and index. More... | |
double | get_max_abs_difference (const Vector &op2) const |
returns the sum of the element-wise differences between this vector and the input vector More... | |
kjb::Matrix | hat () const |
Construct an "equivalent" skew-symmetric matrix from 3-vector. More... | |
Vector & | cross_with (const Vector &op2) |
Compute cross product of op1 and op2. More... | |
Value_type | magnitude () const |
Return this vector's magnitude. More... | |
Value_type | magnitude_squared () const |
Return this vector's squared magnitude. More... | |
Value_type | sum_vector_elements () const |
Return this vector's elements sum. More... | |
Vector & | normalize (kjb_c::Norm_method method=kjb_c::NORMALIZE_BY_MAGNITUDE) |
Normalize vector in place by choice of method (default by magnitude). More... | |
Vector | normalized (kjb_c::Norm_method method=kjb_c::NORMALIZE_BY_MAGNITUDE) const |
non=mutating version of "normalize()" More... | |
This class implements vectors, in the linear-algebra sense, with real-valued elements.
For better maintainability, refer to the element type using this class's Value_type typedef, instead of referring to 'double' directly.
Most methods of this class are implemented in the C language portion of the KJB library, with this class forming a thin (usually inlined) layer.
typedef const value_type* kjb::Vector::const_iterator |
typedef const double* kjb::Vector::const_pointer |
typedef double kjb::Vector::const_reference |
typedef std::reverse_iterator<const_iterator> kjb::Vector::const_reverse_iterator |
const Iterator type
typedef int kjb::Vector::difference_type |
typedef kjb_c::Vector kjb::Vector::Impl_type |
the underlying implementation
typedef value_type* kjb::Vector::iterator |
typedef Value_type(* kjb::Vector::Mapper)(Value_type) |
element transformer fun
typedef Matrix kjb::Vector::Mat_type |
the associated matrix type
view type of the associated matrix type
typedef double* kjb::Vector::pointer |
typedef double& kjb::Vector::reference |
typedef std::reverse_iterator<iterator> kjb::Vector::reverse_iterator |
typedef int kjb::Vector::size_type |
typedef double kjb::Vector::Value_type |
data type of the elements
typedef double kjb::Vector::value_type |
typedef Vector kjb::Vector::Vec_type |
the associated vector type
|
inlineexplicit |
Allocate vector of given length; contents are uninitialized.
This also serves as a default ctor
|
inlineexplicit |
Allocate vector of given length; contents are uninitialized.
This ctor is almost mandatory since we want to construct from a size_t, which on most platforms is unsigned int or unsigned long int.
|
inlineexplicit |
Allocate vector of given length; contents are uninitialized.
This ctor is almost mandatory since we want to construct from a size_t, which on most platforms is unsigned int or unsigned long int.
|
inline |
Allocate vector of given length; initialize all elts to 'num'.
|
inline |
Initialize from an array of doubles, of given length.
kjb::Vector::Vector | ( | const Int_vector & | ivec | ) |
Ctor builds a vector from one with integer values.
|
inline |
Initialize from an array of floats, of given length.
|
inlineexplicit |
Conversion ctor: claim ownership of an existing vector pointer (i.e., make a shallow copy).
This method is the proper way to say, ''Here is a kjb_c::Vector struct that I am responsible for deleting, and I must make sure that it gets destroyed when it goes out of scope.'' This is a good way to wrap a vector "dynamically," after it has already been created.
Anyplace you find yourself using free_vector() in your C++ code, you should consider using instead this Vector class with this ctor.
If the input pointer equals NULL then a zero length vector is constructed.
kjb::Vector::Vector | ( | const std::vector< double > & | src | ) |
Conversion ctor: Create from stl-style vector.
|
inlineexplicit |
Create a single-element vector.
kjb::Vector::Vector | ( | const Vector_view & | view | ) |
Create a vector from a view of another vector.
kjb::Vector::Vector | ( | const Const_vector_view & | view | ) |
Create a vector from a view of another vector.
|
inline |
Create a two-vector.
|
inline |
Create a three-vector.
|
inline |
Create a four-vector.
|
inlineexplicit |
Ctor copies contents (i.e., deep copy) of an existing vector.
This kind of conversion is relatively expensive, thus we restrict its use only to explicit invocation.
|
explicit |
Ctor builds from a one-row or one-column Matrix (making a deep copy).
|
explicit |
kjb::Vector::Vector | ( | const std::string & | file_name | ) |
Construct a vector by reading contents from a named file.
|
inline |
Copy ctor – calls the kjb_c function to copy a vector.
Although this method is essential, I hope it is seldom actually used. Most of the time it should be unnecessary or optimized away somehow.
|
inline |
"Range constructor"
|
inline |
Destructor – which just calls the KJB destructor.
Add vector to self, in-place, just like v += delta_v.
void kjb::Vector::assign | ( | size_type | N, |
value_type | t | ||
) |
Resizes to N, filling all values with t.
|
inline |
Safely subscript vector, e.g., A.at(10), to get an lvalue.
"Safely" here means we do bounds checking and throw if out of bounds.
|
inline |
Safely subscript vector, e.g., A.at(10), to get an rvalue.
"Safely" here means we do bounds checking and throw if out of bounds.
|
inline |
Returns the last element of the vector.
|
inline |
Returns the last element of the vector.
|
inline |
Returns a read/write iterator that points to the first element in the Vector. Iteration is done in ordinary element order.
|
inline |
Returns a read-only (constant) iterator that points to the first element in the Vector. Iteration is done in ordinary element order.
|
inline |
Test whether a subscript is valid, throw an exception if not.
Index_out_of_bounds | with a message, if the index is bad. |
|
inline |
delete all elements from vector
Compute cross product of op1 and op2.
Store result in this vector. Only defined for dimension = 3.
|
inline |
Scalar division of self, in-place, just like v /= 2.
|
inline |
Returns true iff size is zero. Required to comply with stl Container concept.
|
inline |
Returns a read/write iterator that points one past the last element in the Vector. Iteration is done in ordinary element order.
|
inline |
Returns a read-only (constant) iterator that points one past the last element in the Vector. Iteration is done in ordinary element order.
Vector::iterator kjb::Vector::erase | ( | iterator | position | ) |
erase element at position and shift all elements after p up by 1.
erase all elements beginning with begin_ up to but not including end_
Element wise division of two vectors; result is stored in first.
Element wise multiplication of two vectors.
|
inline |
Returns the first element of the vector.
|
inline |
Returns the first element of the vector.
|
inline |
Get const pointer to the underlying kjb_c::Vector C struct.
|
inline |
Return the length of the vector.
double kjb::Vector::get_max_abs_difference | ( | const Vector & | op2 | ) | const |
returns the sum of the element-wise differences between this vector and the input vector
op2 | the input vector |
|
inline |
Get pointer to the underlying kjb_c::Vector C struct.
|
inline |
Get pointer to the underlying kjb_c::Vector C struct.
Matrix kjb::Vector::hat | ( | ) | const |
Construct an "equivalent" skew-symmetric matrix from 3-vector.
The result is useful for computing a cross product. Only implemented for dimension = 3. The formula is as follows:
( 0 -c b ) Hat of (a b c)' := ( c 0 -a ) ( -b a 0 ).
Vector::iterator kjb::Vector::insert | ( | iterator | position, |
value_type | t | ||
) |
A copy of t is inserted before position.
void kjb::Vector::insert | ( | iterator | position, |
size_type | N, | ||
value_type | t | ||
) |
Insert N copies of t before position.
|
inline |
Return this vector's magnitude.
|
inline |
Return this vector's squared magnitude.
Transform the elements of a vector.
Apply a transformation fun to each element of the vector, in place, and return an lvalue of the modified vector.
|
inline |
Find maximum element in the vector, both its value and index.
This will search for the maximum value in the vector, and return that value. Also this method can work like an argmax function, and emit the index of a location containing that value.
[out] | max_index | Pointer to a location to store the index of the maximum value. If it equals null, then this method behaves the same as max(). |
Result_error | if the vector has size of zero |
The vector might store the maximum value in more than one location. In that case, the value in *max_index will store one of the locations, but it is undefined which one.
|
inline |
Maximum size vector can ever have. Currently defined as INT_MAX.
|
inline |
Find minimum element in the vector, both its value and index.
This will search for the minimum value in the vector, and return that value. Also this method can work like an argmin function, and emit the index of a location containing that value.
[out] | min_index | Pointer to a location to store the index of the minimum value. If it equals null, then this method behaves the same as min(). |
Result_error | if the vector has size of zero |
The vector might store the minimum value in more than one location. In that case, the value in *min_index will store one of the locations, but it is undefined which one.
|
inline |
Scalar multiply self, in-place, just like v *= 6.
|
inline |
Negate self, in-place, just like v *= (-1).
|
inline |
Normalize vector in place by choice of method (default by magnitude).
|
inline |
non=mutating version of "normalize()"
|
inline |
Fortran-style vector subscript, e.g., A(10), to get an lvalue.
|
inline |
Fortran-style vector subscript, e.g., A(10), to get an rvalue.
|
inline |
Scalar multiply self, in-place, e.g., v *= 6.
Add vector to self, in-place, e.g., v += delta_v.
Vector & kjb::Vector::operator+= | ( | double | x | ) |
Add scalar to self, in-place, e.g., v -= delta_v.
Subtract vector from self, in-place, e.g., v -= delta_v.
Vector & kjb::Vector::operator-= | ( | double | x | ) |
Subtract scalar from self, in-place, e.g., v -= delta_v.
|
inline |
Scalar division of self, in-place.
Vector & kjb::Vector::operator= | ( | const Mat_view_type & | vec_ref | ) |
Assignment operator: assign from a single-row matrix-view.
Assignment operator: assign from a kjb_c::Vector, a C struct.
Assignment operator: assign from a kjb::Vector, a C++ object.
|
inline |
Subscript vector like a C array, e.g., A[10], returning an lvalue.
|
inline |
Subscript vector like a C array, e.g., A[10], returning an rvalue.
Vector_view kjb::Vector::operator[] | ( | const Index_range & | idx | ) |
Access a subvector using Matlab-like syntax. Returned object has Vector semantics, but operates on the elements of this vector indexed by the idx parameter.
Const_vector_view kjb::Vector::operator[] | ( | const Index_range & | idx | ) | const |
|
inline |
Returns the last element of the vector.
|
inline |
inserts an element at the back of the vector in amortized constant time.
|
inline |
Clobber current vector; resize and fill with random values.
Random values are uniformly distributed between 0 and 1.
|
inline |
Clobber current vector contents with random values.
Random values are uniformly distributed between 0 and 1.
|
inline |
Returns a read/write reverse iterator that points to the last element in the Vector. Iteration is done in reverse element order.
|
inline |
Returns a read-only (constant) reverse iterator that points to the last element in the Vector. Iteration is done in reverse element order.
|
inline |
Read vector from a file, or from standard input.
If filename equals NULL or filename[0] is the null character then the output is directed to standard output.
|
inline |
Returns a read/write reverse iterator that points to one before the first element in the Vector. Iteration is done in reverse element order.
|
inline |
Returns a read-only (constant) reverse iterator that points to one before the first element in the Vector. Iteration is done in reverse element order.
Copy elements from input vector iv into this vector, starting at some offset, leaving the rest of this vector unchanged.
iv | The input vector |
offset | The index of the first target location of this vector. |
begin | The index of the first element to copy from iv. |
length | The number of elements to be copied from iv |
Illegal_argument | if the source or destination is too small to perform the specified copy, or if an argument is negative. |
Just to be absolutely explicit, it works like so, provided all these locations below are valid.
| (*this)[ offset ] = iv[ begin ]; | : | : | (*this)[ offset + length - 1 ] = iv[ begin + length - 1 ];
|
inline |
After calling, the vector will be able to contain capacity elements without needing to resize itself.
Vector & kjb::Vector::resize | ( | int | new_length, |
Value_type | pad = Value_type(0) |
||
) |
Resize vector, retaining previous values.
|
inline |
Clone of zero_out(int)
Convert to a 1-vector and set its value to the argument.
|
inline |
Convert to a 2-vector and set its values to the arguments.
|
inline |
Convert to a 3-vector and set its values to the arguments.
|
inline |
Convert to a 4-vector and set its values to the arguments.
|
inline |
Alias to get_length(). Required to comply with stl Container concept.
Subtract vector from self, in-place, just like v -= delta_v.
|
inline |
Return this vector's elements sum.
|
inline |
Swap the representations of two vectors.
|
inline |
Write vector to a file, or to standard output.
If filename equals NULL or filename[0] is the null character then the output is directed to standard output.
|
inline |
Write vector as a column to a file, or to standard output.
If filename equals NULL or filename[0] is the null character then the output is directed to standard output.
|
inline |
Write vector as a row to a file, or to standard output.
If filename equals NULL or filename[0] is a null character then the output is directed to standard output.
|
inline |
Clone of randomize(int)
Clobber current vector; resize and fill with zeroes.
|
inline |
Clobber current vector contents with zeroes.