11#ifndef __RD_VECTOR_H__
12#define __RD_VECTOR_H__
22#include <boost/random.hpp>
23#include <boost/smart_ptr.hpp>
36 TYPE *data =
new TYPE[N];
37 memset(
static_cast<void *
>(data), 0, d_size *
sizeof(TYPE));
44 TYPE *data =
new TYPE[N];
47 for (i = 0; i < N; i++) {
66 d_size = other.
size();
67 const TYPE *otherData = other.
getData();
68 TYPE *data =
new TYPE[d_size];
70 memcpy(
static_cast<void *
>(data),
static_cast<const void *
>(otherData),
71 d_size *
sizeof(TYPE));
78 unsigned int size()
const {
return d_size; }
81 inline TYPE
getVal(
unsigned int i)
const {
87 inline void setVal(
unsigned int i, TYPE val) {
103 inline TYPE *
getData() {
return d_data.get(); }
117 const TYPE *otherData = other.
getData();
118 memcpy(
static_cast<void *
>(d_data.get()),
119 static_cast<const void *
>(otherData), d_size *
sizeof(TYPE));
126 const TYPE *otherData = other.
getData();
127 TYPE *data = d_data.get();
129 for (i = 0; i < d_size; i++) {
130 data[i] += otherData[i];
137 PRECONDITION(d_size == other.
size(),
"Size mismatch in vector subtraction");
138 const TYPE *otherData = other.
getData();
139 TYPE *data = d_data.get();
141 for (i = 0; i < d_size; i++) {
142 data[i] -= otherData[i];
150 for (i = 0; i < d_size; i++) {
159 for (i = 0; i < d_size; i++) {
167 TYPE res = (TYPE)0.0;
169 TYPE *data = d_data.get();
170 for (i = 0; i < d_size; i++) {
171 res += data[i] * data[i];
181 TYPE res = (TYPE)0.0;
183 TYPE *data = d_data.get();
184 for (i = 0; i < d_size; i++) {
185 res += fabs(data[i]);
192 TYPE res = (TYPE)(-1.0);
194 TYPE *data = d_data.get();
195 for (i = 0; i < d_size; i++) {
196 if (fabs(data[i]) > res) {
206 TYPE res = (TYPE)(-1.0);
207 unsigned int i,
id = d_size;
208 TYPE *data = d_data.get();
209 for (i = 0; i < d_size; i++) {
210 if (fabs(data[i]) > res) {
220 TYPE res = (TYPE)(-1.e8);
221 unsigned int i,
id = d_size;
222 TYPE *data = d_data.get();
223 for (i = 0; i < d_size; i++) {
234 TYPE res = (TYPE)(1.e8);
235 unsigned int i,
id = d_size;
236 TYPE *data = d_data.get();
237 for (i = 0; i < d_size; i++) {
249 "Size mismatch in vector doct product");
250 const TYPE *oData = other.
getData();
252 TYPE res = (TYPE)(0.0);
253 TYPE *data = d_data.get();
254 for (i = 0; i < d_size; i++) {
255 res += (data[i] * oData[i]);
262 TYPE val = this->
normL2();
274 generator.seed(seed);
279 generator.seed(clock() + 1);
283 TYPE *data = d_data.get();
284 for (i = 0; i < d_size; i++) {
285 data[i] = randSource();
309 return numer / denom;
314template <
typename TYPE>
317 unsigned int siz = vec.
size();
318 target <<
"Size: " << siz <<
" [";
320 for (i = 0; i < siz; i++) {
321 target << std::setw(7) << std::setprecision(3) << vec.
getVal(i) <<
", ";
#define PRECONDITION(expr, mess)
std::ostream & operator<<(std::ostream &target, const RDNumeric::Vector< TYPE > &vec)
ostream operator for Vectors
A class to represent vectors of numbers.
TYPE normL2() const
L2 norm.
Vector< TYPE > & operator*=(TYPE scale)
multiplication by a scalar
Vector(unsigned int N)
Initialize with only a size.
Vector(const Vector &other)
copy constructor
Vector< TYPE > & assign(const Vector< TYPE > &other)
Copy operator.
TYPE & operator[](unsigned int i)
void normalize()
Normalize the vector using the L2 norm.
void setVal(unsigned int i, TYPE val)
sets the index at a particular value
Vector(unsigned int N, TYPE val)
Initialize with a size and default value.
Vector< TYPE > & operator-=(const Vector< TYPE > &other)
elementwise subtraction, vectors must be the same size.
Vector< TYPE > & operator+=(const Vector< TYPE > &other)
elementwise addition, vectors must be the same size.
void setToRandom(unsigned int seed=0)
Set to a random unit vector.
unsigned int size() const
return the size (dimension) of the vector
TYPE normL1() const
L1 norm.
Vector< TYPE > & operator/=(TYPE scale)
division by a scalar
TYPE normLinfinity() const
L-infinity norm.
boost::shared_array< TYPE > DATA_SPTR
TYPE normL2Sq() const
L2 norm squared.
TYPE dotProduct(const Vector< TYPE > other) const
returns the dot product between two Vectors
unsigned int largestValId() const
Gets the ID of the entry that has the largest value.
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 * getData()
returns a pointer to our data array
unsigned int smallestValId() const
Gets the ID of the entry that has the smallest value.
TYPE getVal(unsigned int i) const
returns the value at a particular index
const TYPE * getData() const
returns a const pointer to our data array
TYPE operator[](unsigned int i) const
Vector(unsigned int N, DATA_SPTR data)
Initialize from a smart pointer.
boost::minstd_rand rng_type
boost::variate_generator< rng_type &, uniform_double > double_source_type
boost::uniform_real uniform_double
double TanimotoSimilarity(const Vector< T > &v1, const Vector< T > &v2)
returns the algebraic tanimoto similarity [defn' from JCIM 46:587-96 (2006)]
Vector< double > DoubleVector