11#ifndef __RD_VECTOR_H__
12#define __RD_VECTOR_H__
22#include <boost/random.hpp>
23#include <boost/smart_ptr.hpp>
38 TYPE *data =
new TYPE[N];
39 memset(
static_cast<void *
>(data), 0, d_size *
sizeof(TYPE));
46 TYPE *data =
new TYPE[N];
49 for (i = 0; i < N; i++) {
68 d_size = other.
size();
69 const TYPE *otherData = other.
getData();
70 TYPE *data =
new TYPE[d_size];
72 memcpy(
static_cast<void *
>(data),
static_cast<const void *
>(otherData),
73 d_size *
sizeof(TYPE));
80 unsigned int size()
const {
return d_size; }
83 inline TYPE
getVal(
unsigned int i)
const {
89 inline void setVal(
unsigned int i, TYPE val) {
105 inline TYPE *
getData() {
return d_data.get(); }
119 const TYPE *otherData = other.
getData();
120 memcpy(
static_cast<void *
>(d_data.get()),
121 static_cast<const void *
>(otherData), d_size *
sizeof(TYPE));
128 const TYPE *otherData = other.
getData();
129 TYPE *data = d_data.get();
131 for (i = 0; i < d_size; i++) {
132 data[i] += otherData[i];
139 PRECONDITION(d_size == other.
size(),
"Size mismatch in vector subtraction");
140 const TYPE *otherData = other.
getData();
141 TYPE *data = d_data.get();
143 for (i = 0; i < d_size; i++) {
144 data[i] -= otherData[i];
152 for (i = 0; i < d_size; i++) {
161 for (i = 0; i < d_size; i++) {
169 TYPE res = (TYPE)0.0;
171 TYPE *data = d_data.get();
172 for (i = 0; i < d_size; i++) {
173 res += data[i] * data[i];
183 TYPE res = (TYPE)0.0;
185 TYPE *data = d_data.get();
186 for (i = 0; i < d_size; i++) {
187 res += fabs(data[i]);
194 TYPE res = (TYPE)(-1.0);
196 TYPE *data = d_data.get();
197 for (i = 0; i < d_size; i++) {
198 if (fabs(data[i]) > res) {
208 TYPE res = (TYPE)(-1.0);
209 unsigned int i,
id = d_size;
210 TYPE *data = d_data.get();
211 for (i = 0; i < d_size; i++) {
212 if (fabs(data[i]) > res) {
222 TYPE res = (TYPE)(-1.e8);
223 unsigned int i,
id = d_size;
224 TYPE *data = d_data.get();
225 for (i = 0; i < d_size; i++) {
236 TYPE res = (TYPE)(1.e8);
237 unsigned int i,
id = d_size;
238 TYPE *data = d_data.get();
239 for (i = 0; i < d_size; i++) {
251 "Size mismatch in vector doct product");
252 const TYPE *oData = other.
getData();
254 TYPE res = (TYPE)(0.0);
255 TYPE *data = d_data.get();
256 for (i = 0; i < d_size; i++) {
257 res += (data[i] * oData[i]);
264 TYPE val = this->
normL2();
266 throw std::runtime_error(
"Cannot normalize a zero length vector");
279 generator.seed(seed);
284 generator.seed(clock() + 1);
288 TYPE *data = d_data.get();
289 for (i = 0; i < d_size; i++) {
290 data[i] = randSource();
314 return numer / denom;
319template <
typename TYPE>
322 unsigned int siz = vec.
size();
323 target <<
"Size: " << siz <<
" [";
325 for (i = 0; i < siz; i++) {
326 target << std::setw(7) << std::setprecision(3) << vec.
getVal(i) <<
", ";
#define PRECONDITION(expr, mess)
static constexpr double zero_tolerance
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