10 #ifndef __RD_VECTOR_H__
11 #define __RD_VECTOR_H__
21 #include <boost/random.hpp>
22 #include <boost/smart_ptr.hpp>
35 TYPE *data =
new TYPE[
N];
36 memset(static_cast<void *>(data), 0, d_size *
sizeof(TYPE));
43 TYPE *data =
new TYPE[
N];
46 for (i = 0; i <
N; i++) {
65 d_size = other.
size();
66 const TYPE *otherData = other.
getData();
67 TYPE *data =
new TYPE[d_size];
69 memcpy(static_cast<void *>(data), static_cast<const void *>(otherData),
70 d_size *
sizeof(TYPE));
77 unsigned int size()
const {
return d_size; }
80 inline TYPE
getVal(
unsigned int i)
const {
86 inline void setVal(
unsigned int i, TYPE val) {
102 inline TYPE *
getData() {
return d_data.get(); }
116 const TYPE *otherData = other.
getData();
117 memcpy(static_cast<void *>(d_data.get()),
118 static_cast<const void *>(otherData), d_size *
sizeof(TYPE));
125 const TYPE *otherData = other.
getData();
126 TYPE *data = d_data.get();
128 for (i = 0; i < d_size; i++) {
129 data[i] += otherData[i];
136 PRECONDITION(d_size == other.
size(),
"Size mismatch in vector subtraction");
137 const TYPE *otherData = other.
getData();
138 TYPE *data = d_data.get();
140 for (i = 0; i < d_size; i++) {
141 data[i] -= otherData[i];
149 for (i = 0; i < d_size; i++) {
158 for (i = 0; i < d_size; i++) {
166 TYPE res = (TYPE)0.0;
168 TYPE *data = d_data.get();
169 for (i = 0; i < d_size; i++) {
170 res += data[i] * data[i];
180 TYPE res = (TYPE)0.0;
182 TYPE *data = d_data.get();
183 for (i = 0; i < d_size; i++) {
184 res += fabs(data[i]);
191 TYPE res = (TYPE)(-1.0);
193 TYPE *data = d_data.get();
194 for (i = 0; i < d_size; i++) {
195 if (fabs(data[i]) > res) {
205 TYPE res = (TYPE)(-1.0);
206 unsigned int i,
id = d_size;
207 TYPE *data = d_data.get();
208 for (i = 0; i < d_size; i++) {
209 if (fabs(data[i]) > res) {
219 TYPE res = (TYPE)(-1.e8);
220 unsigned int i,
id = d_size;
221 TYPE *data = d_data.get();
222 for (i = 0; i < d_size; i++) {
233 TYPE res = (TYPE)(1.e8);
234 unsigned int i,
id = d_size;
235 TYPE *data = d_data.get();
236 for (i = 0; i < d_size; i++) {
248 "Size mismatch in vector doct product");
249 const TYPE *oData = other.
getData();
251 TYPE res = (TYPE)(0.0);
252 TYPE *data = d_data.get();
253 for (i = 0; i < d_size; i++) {
254 res += (data[i] * oData[i]);
261 TYPE val = this->
normL2();
273 generator.seed(seed);
278 generator.seed(clock() + 1);
282 TYPE *data = d_data.get();
283 for (i = 0; i < d_size; i++) {
284 data[i] = randSource();
298 template <
typename T>
301 if (numer == 0.0)
return 0.0;
303 if (denom == 0.0)
return 0.0;
304 return numer / denom;
309 template <
typename TYPE>
312 unsigned int siz = vec.
size();
313 target <<
"Size: " << siz <<
" [";
315 for (i = 0; i < siz; i++) {
316 target << std::setw(7) << std::setprecision(3) << vec.
getVal(i) <<
", ";
void normalize()
Normalize the vector using the L2 norm.
unsigned int largestValId() const
Gets the ID of the entry that has the largest value.
TYPE normL2() const
L2 norm.
boost::shared_array< TYPE > DATA_SPTR
boost::minstd_rand rng_type
std::ostream & operator<<(std::ostream &target, const RDNumeric::Vector< TYPE > &vec)
ostream operator for Vectors
double TanimotoSimilarity(const Vector< T > &v1, const Vector< T > &v2)
returns the algebraic tanimoto similarity [defn' from JCIM 46:587-96 (2006)]
Vector< TYPE > & operator-=(const Vector< TYPE > &other)
elementwise subtraction, vectors must be the same size.
TYPE & operator[](unsigned int i)
Vector< double > DoubleVector
unsigned int smallestValId() const
Gets the ID of the entry that has the smallest value.
Vector< TYPE > & assign(const Vector< TYPE > &other)
Copy operator.
unsigned int size() const
return the size (dimension) of the vector
TYPE normLinfinity() const
L-infinity norm.
void setVal(unsigned int i, TYPE val)
sets the index at a particular value
Vector< TYPE > & operator/=(TYPE scale)
division by a scalar
TYPE normL2Sq() const
L2 norm squared.
Vector(const Vector &other)
copy constructor
TYPE normL1() const
L1 norm.
boost::uniform_real uniform_double
const TYPE * getData() const
returns a const pointer to our data array
TYPE operator[](unsigned int i) const
Vector< TYPE > & operator+=(const Vector< TYPE > &other)
elementwise addition, vectors must be the same size.
unsigned int largestAbsValId() const
Gets the ID of the entry that has the largest absolute value i.e. the entry being used for the L-infi...
TYPE dotProduct(const Vector< TYPE > other) const
returns the dot product between two Vectors
void setToRandom(unsigned int seed=0)
Set to a random unit vector.
Vector(unsigned int N, DATA_SPTR data)
Initialize from a smart pointer.
Vector< TYPE > & operator*=(TYPE scale)
multiplication by a scalar
Vector(unsigned int N, TYPE val)
Initialize with a size and default value.
TYPE getVal(unsigned int i) const
returns the value at a particular index
#define PRECONDITION(expr, mess)
boost::variate_generator< rng_type &, uniform_double > double_source_type
TYPE * getData()
returns a pointer to our data array
A class to represent vectors of numbers.
Vector(unsigned int N)
Initialize with only a size.