22#include <boost/random.hpp>
35 constexpr explicit Vector(
unsigned int N) {
37 TYPE *data =
new TYPE[N];
38 memset(
static_cast<void *
>(data), 0, d_size *
sizeof(TYPE));
43 constexpr Vector(
unsigned int N, TYPE val) {
45 d_data.reset(
new TYPE[N]);
46 for (
auto i = 0u; i < N; i++) {
64 d_size = other.
size();
65 const TYPE *otherData = other.
getData();
66 d_data.reset(
new TYPE[d_size]);
68 memcpy(
static_cast<void *
>(d_data.get()),
69 static_cast<const void *
>(otherData), d_size *
sizeof(TYPE));
75 constexpr unsigned int size()
const {
return d_size; }
84 void setVal(
unsigned int i, TYPE val) {
100 constexpr TYPE *
getData() {
return d_data.get(); }
114 const TYPE *otherData = other.
getData();
115 memcpy(
static_cast<void *
>(d_data.get()),
116 static_cast<const void *
>(otherData), d_size *
sizeof(TYPE));
123 const TYPE *otherData = other.
getData();
124 TYPE *data = d_data.get();
126 for (i = 0; i < d_size; i++) {
127 data[i] += otherData[i];
134 PRECONDITION(d_size == other.
size(),
"Size mismatch in vector subtraction");
135 const TYPE *otherData = other.
getData();
136 TYPE *data = d_data.get();
138 for (i = 0; i < d_size; i++) {
139 data[i] -= otherData[i];
147 for (i = 0; i < d_size; i++) {
156 for (i = 0; i < d_size; i++) {
164 TYPE res = (TYPE)0.0;
166 TYPE *data = d_data.get();
167 for (i = 0; i < d_size; i++) {
168 res += data[i] * data[i];
178 TYPE res = (TYPE)0.0;
180 TYPE *data = d_data.get();
181 for (i = 0; i < d_size; i++) {
182 res += fabs(data[i]);
189 TYPE res = (TYPE)(-1.0);
191 TYPE *data = d_data.get();
192 for (i = 0; i < d_size; i++) {
193 if (fabs(data[i]) > res) {
203 TYPE res = (TYPE)(-1.0);
204 unsigned int i,
id = d_size;
205 TYPE *data = d_data.get();
206 for (i = 0; i < d_size; i++) {
207 if (fabs(data[i]) > res) {
217 TYPE res = (TYPE)(-1.e8);
218 unsigned int i,
id = d_size;
219 TYPE *data = d_data.get();
220 for (i = 0; i < d_size; i++) {
231 TYPE res = (TYPE)(1.e8);
232 unsigned int i,
id = d_size;
233 TYPE *data = d_data.get();
234 for (i = 0; i < d_size; i++) {
246 "Size mismatch in vector doct product");
247 const TYPE *oData = other.
getData();
249 TYPE res = (TYPE)(0.0);
250 TYPE *data = d_data.get();
251 for (i = 0; i < d_size; i++) {
252 res += (data[i] * oData[i]);
259 TYPE val = this->
normL2();
261 throw std::runtime_error(
"Cannot normalize a zero length vector");
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)
static constexpr double zero_tolerance
constexpr std::ostream & operator<<(std::ostream &target, const RDNumeric::Vector< TYPE > &vec)
ostream operator for Vectors
A class to represent vectors of numbers.
constexpr void normalize()
Normalize the vector using the L2 norm.
constexpr TYPE * getData()
returns a pointer to our data array
Vector< TYPE > & assign(const Vector< TYPE > &other)
Copy operator.
TYPE & operator[](unsigned int i)
std::shared_ptr< TYPE[]> DATA_SPTR
constexpr ~Vector()=default
void setVal(unsigned int i, TYPE val)
sets the index at a particular value
constexpr Vector(unsigned int N, TYPE val)
Initialize with a size and default value.
constexpr TYPE normL1() const
L1 norm.
constexpr const TYPE * getData() const
returns a const pointer to our data array
constexpr Vector(unsigned int N, DATA_SPTR data)
Initialize from a smart pointer.
constexpr 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...
Vector< TYPE > & operator-=(const Vector< TYPE > &other)
elementwise subtraction, vectors must be the same size.
constexpr TYPE normL2Sq() const
L2 norm squared.
constexpr TYPE normLinfinity() const
L-infinity norm.
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.
constexpr unsigned int largestValId() const
Gets the ID of the entry that has the largest value.
constexpr unsigned int size() const
return the size (dimension) of the vector
constexpr unsigned int smallestValId() const
Gets the ID of the entry that has the smallest value.
constexpr Vector< TYPE > & operator*=(TYPE scale)
multiplication by a scalar
TYPE dotProduct(const Vector< TYPE > other) const
returns the dot product between two Vectors
TYPE getVal(unsigned int i) const
returns the value at a particular index
TYPE operator[](unsigned int i) const
constexpr Vector(unsigned int N)
Initialize with only a size.
constexpr Vector(const Vector &other)
copy constructor
constexpr TYPE normL2() const
L2 norm.
constexpr Vector< TYPE > & operator/=(TYPE scale)
division by a scalar
boost::minstd_rand rng_type
boost::variate_generator< rng_type &, uniform_double > double_source_type
boost::uniform_real uniform_double
Vector< double > DoubleVector
constexpr double TanimotoSimilarity(const Vector< T > &v1, const Vector< T > &v2)
returns the algebraic tanimoto similarity [defn' from JCIM 46:587-96 (2006)]