10 #ifndef __RD_VECTOR_H__ 11 #define __RD_VECTOR_H__ 21 #include <boost/random.hpp> 22 #include <boost/smart_ptr.hpp> 37 TYPE *data =
new TYPE[
N];
38 memset(static_cast<void *>(data),0,d_size*
sizeof(TYPE));
45 TYPE *data =
new TYPE[
N];
48 for (i = 0; i <
N; i++) {
67 d_size = other.
size();
68 const TYPE *otherData = other.
getData();
69 TYPE *data =
new TYPE[d_size];
71 memcpy(static_cast<void *>(data), static_cast<const void *>(otherData), d_size*
sizeof(TYPE));
79 unsigned int size()
const {
84 inline TYPE
getVal(
unsigned int i)
const {
90 inline void setVal(
unsigned int i, TYPE val) {
122 const TYPE *otherData = other.
getData();
123 memcpy(static_cast<void *>(d_data.get()), static_cast<const void *>(otherData), d_size*
sizeof(TYPE));
130 const TYPE *otherData = other.
getData();
131 TYPE *data = d_data.get();
133 for (i = 0; i < d_size; i++) {
134 data[i] += otherData[i];
141 PRECONDITION(d_size == other.
size(),
"Size mismatch in vector subtraction");
142 const TYPE *otherData = other.
getData();
143 TYPE *data = d_data.get();
145 for (i = 0; i < d_size; i++) {
146 data[i] -= otherData[i];
154 for (i = 0; i < d_size; i++) {
163 for (i = 0; i < d_size; i++) {
171 TYPE res = (TYPE)0.0;
173 TYPE *data = d_data.get();
174 for (i = 0; i < d_size; i++) {
175 res += data[i]*data[i];
187 TYPE res = (TYPE)0.0;
189 TYPE *data = d_data.get();
190 for (i = 0; i < d_size; i++) {
191 res += fabs(data[i]);
198 TYPE res = (TYPE)(-1.0);
200 TYPE *data = d_data.get();
201 for (i = 0; i < d_size; i++) {
202 if (fabs(data[i]) > res) {
212 TYPE res = (TYPE)(-1.0);
213 unsigned int i,
id=d_size;
214 TYPE *data = d_data.get();
215 for (i = 0; i < d_size; i++) {
216 if (fabs(data[i]) > res) {
226 TYPE res = (TYPE)(-1.e8);
227 unsigned int i,
id=d_size;
228 TYPE *data = d_data.get();
229 for (i = 0; i < d_size; i++) {
240 TYPE res = (TYPE)(1.e8);
241 unsigned int i,
id=d_size;
242 TYPE *data = d_data.get();
243 for (i = 0; i < d_size; i++) {
254 PRECONDITION(d_size == other.
size(),
"Size mismatch in vector doct product");
255 const TYPE *oData = other.
getData();
257 TYPE res = (TYPE)(0.0);
258 TYPE *data = d_data.get();
259 for (i = 0; i < d_size; i++) {
260 res += (data[i]*oData[i]);
267 TYPE val = this->
normL2();
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();
304 template <
typename T>
308 if(numer==0.0)
return 0.0;
310 if(denom==0.0)
return 0.0;
316 template <
typename TYPE> std::ostream &
operator<<(std::ostream& target,
318 unsigned int siz = vec.
size();
319 target <<
"Size: " << siz <<
" [";
321 for (i = 0; i < siz; i++) {
322 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...
boost::variate_generator< rng_type &, uniform_double > double_source_type
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)
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.