Ravelin
VectorN.h
1 /****************************************************************************
2  * Copyright 2013 Evan Drumwright
3  * This library is distributed under the terms of the Apache V2.0
4  * License (obtainable from http://www.apache.org/licenses/LICENSE-2.0).
5  ****************************************************************************/
6 
7 #ifndef VECTORN
8 #error This class is not to be included by the user directly. Use VectorNf.h or VectorNd.h instead.
9 #endif
10 
11 class MATRIXN;
12 class SHAREDMATRIXN;
14 
16 class VECTORN
17 {
18  public:
19  template <class ForwardIterator>
20  VECTORN(ForwardIterator begin, ForwardIterator end);
21 
22  VECTORN();
23  VECTORN(unsigned N);
24  VECTORN(const VECTORN& source);
25  VECTORN(const SHAREDVECTORN& source);
26  VECTORN(const CONST_SHAREDVECTORN& source);
27  VECTORN(const VECTOR2& v);
28  VECTORN(const VECTOR3& v);
29  VECTORN(const MATRIXN& v);
30  VECTORN(const SHAREDMATRIXN& v);
32  VECTORN(unsigned N, const REAL* array);
33  static VECTORN construct_variable(unsigned N, ...);
34  virtual ~VECTORN() {}
35  VECTORN& normalize() { assert(norm() > EPS); operator*=((REAL) 1.0/norm()); return *this; }
36  unsigned size() const { return _data.size(); }
37  static REAL norm_sq(const VECTORN& v) { return VECTORN::dot(v, v); }
38  static REAL norm(const VECTORN& v) { return std::sqrt(norm_sq(v)); }
39  REAL norm_inf() const { return norm_inf(*this); }
40  REAL norm1() const { return norm1(*this); }
41  REAL norm() const { return norm(*this); }
42  REAL norm_sq() const { return norm_sq(*this); }
43  static VECTORN one(unsigned N);
44  static VECTORN& concat(const VECTORN& v1, const VECTORN& v2, VECTORN& result);
45  VECTORN& augment(const VECTORN& v);
46  VECTORN& resize(unsigned N, bool preserve = false) { _data.resize(N, preserve); return *this; }
47  SHAREDVECTORN segment(unsigned start_idx, unsigned end_idx);
48  CONST_SHAREDVECTORN segment(unsigned start_idx, unsigned end_idx) const;
49  static VECTORN zero(unsigned n);
50  VECTORN& operator=(REAL r);
51  VECTORN& operator=(const VECTOR2& source);
52  VECTORN& operator=(const VECTOR3& source);
53  VECTORN& operator=(const VECTORN& source);
54  VECTORN& operator=(const SHAREDVECTORN& source);
55  VECTORN& operator=(const CONST_SHAREDVECTORN& source);
56  VECTORN& operator=(const MATRIXN& source);
57  VECTORN& operator=(const SHAREDMATRIXN& source);
58  VECTORN& operator=(const CONST_SHAREDMATRIXN& source);
59  VECTORN& operator/=(REAL scalar) { return operator*=((REAL) 1.0/scalar); }
60  REAL* data() { return _data.get(); }
61  const REAL* data() const { return _data.get(); }
62  static VECTORN& parse(const std::string& s, VECTORN& v);
63  static VECTORN parse(const std::string& s) { VECTORN v; parse(s, v); return v; }
64  VECTORN& resize(unsigned m, unsigned n, bool preserve = false);
65 
66  void free_memory() { resize(0); compress(); }
67  void compress() { _data.compress(); }
68  unsigned rows() const { return _data.size(); }
69  unsigned columns() const { return 1; }
70  unsigned leading_dim() const { return _data.size(); }
71  unsigned inc() const { return 1; }
72 
73  // inline code specific to VectorN
74  #include "VectorN.inl"
75 
76  // inline code for VectorN/SharedVectorN
77  #define XVECTORN VECTORN
78  #include "XVectorN.inl"
79  #undef XVECTORN
80 
81  protected:
82  SharedResizable<REAL> _data;
83 }; // end class
84 
85 std::ostream& operator<<(std::ostream& out, const VECTORN& v);
86 std::istream& operator>>(std::istream& in, VECTORN& v);
87 
SHAREDVECTORN segment(unsigned start_idx, unsigned end_idx)
Gets a subvector of this vector as a shared vector.
Definition: VectorN.cpp:245
A two-dimensional floating point vector used for computational geometry calculations and with associa...
Definition: Vector2.h:15
static VECTORN construct_variable(unsigned N,...)
Constructs a N-dimension vector from the list of double values.
Definition: VectorN.cpp:94
VECTORN()
Default constructor - constructs an empty vector.
Definition: VectorN.cpp:10
COLUMN_ITERATOR begin()
Returns the column iterator.
Definition: VectorN.h:324
A generic, possibly non-square matrix.
Definition: MatrixN.h:18
static REAL dot(const V1 &v1, const V2 &v2)
Computes the dot-product between two vectors.
Definition: VectorN.h:606
A generic N-dimensional floating point vector.
Definition: SharedVectorN.h:77
static VECTORN zero(unsigned n)
Returns a N-dimensional zero vector.
Definition: VectorN.cpp:114
A generic, possibly non-square matrix using shared data.
Definition: SharedMatrixN.h:59
XVECTORN & operator*=(REAL scalar)
Multiplies this vector in place by a scalar.
Definition: VectorN.h:393
static VECTORN & concat(const VECTORN &v1, const VECTORN &v2, VECTORN &result)
Concatenates two vectors together and stores the result in a third.
Definition: VectorN.cpp:287
A generic N-dimensional floating point vector.
Definition: SharedVectorN.h:15
COLUMN_ITERATOR end()
Returns the column iterator.
Definition: VectorN.h:330
VECTORN & operator=(REAL r)
Assigns this vector to a scalar.
Definition: VectorN.cpp:279
A generic N-dimensional floating point vector.
Definition: VectorN.h:16
static VECTORN & parse(const std::string &s, VECTORN &v)
Parses a string for a vector value.
Definition: VectorN.cpp:296
A three-dimensional floating point vector used for representing points and vectors in 3D with associa...
Definition: Vector3.h:15
A generic, possibly non-square matrix using constant shared data.
Definition: SharedMatrixN.h:19
static VECTORN one(unsigned N)
Returns a N-dimensional one vector.
Definition: VectorN.cpp:106