26 #ifndef AURYNVECTOR_H_ 27 #define AURYNVECTOR_H_ 35 #include <boost/serialization/utility.hpp> 36 #include <boost/serialization/split_member.hpp> 38 #include <boost/random/mersenne_twister.hpp> 39 #include <boost/random/variate_generator.hpp> 40 #include <boost/random/normal_distribution.hpp> 58 template <
typename T,
typename IndexType = NeuronID >
65 friend class boost::serialization::access;
66 template<
class Archive>
67 void serialize(Archive & ar,
const unsigned int version)
70 for ( IndexType i = 0 ; i <
size ; ++i )
93 if ( v->size != size ) {
101 #if defined(CODE_USE_SIMD_INSTRUCTIONS_EXPLICITLY) && defined(CODE_ALIGNED_SIMD_INSTRUCTIONS) 103 std::size_t mem_size =
sizeof(T)*n;
104 mem = malloc(mem_size+mem_alignment-1);
106 if ( (
unsigned long)mem%mem_alignment ) ptr = (T*)(((
unsigned long)mem/mem_alignment+1)*mem_alignment);
113 assert(((
unsigned long)ptr % mem_alignment) == 0);
123 #if defined(CODE_USE_SIMD_INSTRUCTIONS_EXPLICITLY) && defined(CODE_ALIGNED_SIMD_INSTRUCTIONS) 136 x *= x; x *= x; x *= x; x *= x;
137 x *= x; x *= x; x *= x; x *= x;
186 if ( size != new_size ) {
188 IndexType old_size =
size;
191 const size_t copy_size = std::min(old_size,new_size) *
sizeof(T);
192 memcpy(data, old_data, copy_size);
203 std::copy(v->data, v->data+v->size, data);
224 void set(IndexType i, T value)
234 for ( IndexType i = 0 ; i <
size ; ++i ) {
248 for ( IndexType i = 0 ; i <
size ; ++i ) {
257 for ( IndexType i = 0 ; i <
size ; ++i ) {
282 for ( IndexType i = 0 ; i <
size ; ++i ) {
283 data[i] += v->data[i];
297 for ( IndexType i = 0 ; i <
size ; ++i ) {
298 data[i] -= v->data[i];
314 for ( IndexType i = 0 ; i <
size ; ++i ) {
315 data[i] *= v->data[i];
333 for ( IndexType i = 0 ; i <
size ; ++i ) {
334 data[i] /= v->data[i];
346 for ( IndexType i = 0 ; i <
size ; ++i ) {
347 data[i] = a->data[i]/b->data[i];
360 for ( IndexType i = 0 ; i <
size ; ++i ) {
361 data[i] += a * x->data[i];
374 for ( IndexType i = 0 ; i <
size ; ++i ) {
375 data[i] += rate*(v->data[i]-data[i]);
382 for ( IndexType i = 0 ; i <
size ; ++i ) {
383 data[i] += rate*(a-data[i]);
392 for ( IndexType i = 0 ; i <
size ; ++i ) {
393 data[i] = std::max(v1->data[i],v2->data[i]);
408 for ( IndexType i = 0 ; i <
size ; ++i ) {
409 data[i] = std::pow(data[i],n);
424 for ( IndexType i = 0 ; i <
size ; ++i ) {
432 for ( IndexType i = 0 ; i <
size ; ++i ) {
433 data[i] = std::exp(data[i]);
455 for ( IndexType i = 0 ; i <
size ; ++i ) {
456 data[i] = std::sqrt(data[i]);
463 for ( IndexType i = 0 ; i <
size ; ++i ) {
471 for ( IndexType i = 0 ; i <
size ; ++i ) {
472 data[i] = 1.0/data[i];
483 for ( IndexType i = 0 ; i <
size ; ++i ) {
484 data[i] = a->data[i]+b->data[i];
494 for ( IndexType i = 0 ; i <
size ; ++i ) {
495 data[i] = a->data[i]+b;
506 for ( IndexType i = 0 ; i <
size ; ++i ) {
507 data[i] = a->data[i]-b->data[i];
551 for ( IndexType i = 0 ; i <
size ; ++i ) {
552 if ( data[i] < 0.0 ) {
562 for ( IndexType i = 0 ; i <
size ; ++i ) {
563 if ( data[i] > 0.0 ) {
576 for ( IndexType i = 0 ; i <
size ; ++i ) {
577 if ( data[i] < min ) {
597 for ( IndexType i = 0 ; i <
size ; ++i ) {
598 double elem =
get(i);
600 sum2 += std::pow(elem,2);
602 double var = (sum2-(sum*
sum)/size)/(size-1);
614 return std::sqrt(
var());
633 for ( IndexType i = 0 ; i <
size ; ++i ) {
645 for ( IndexType i = 0 ; i <
size ; ++i ) {
659 for ( IndexType i = 0 ; i <
size ; ++i ) {
663 return std::sqrt(sum);
672 for ( IndexType i = 0 ; i <
size ; ++i ) {
674 if ( el > max ) max = el;
685 for ( IndexType i = 0 ; i <
size ; ++i ) {
687 if ( el < min ) min = el;
700 for ( IndexType i = 0 ; i <
size ; ++i ) {
701 if (
get(i) != 0 ) ++
sum;
711 for ( IndexType i = 0 ; i <
size ; ++i ) {
712 if ( std::abs(
get(i)) < epsilon )
set(i, 0.0);
719 seed =
static_cast<unsigned int>(std::time(0));
720 boost::mt19937 randgen(seed);
721 boost::normal_distribution<> dist((
double)
mean, (
double)sigma);
722 boost::variate_generator<boost::mt19937&, boost::normal_distribution<> > die(randgen, dist);
724 for ( IndexType i = 0 ; i<
size ; ++i ) {
745 for ( IndexType i = 0 ; i <
size ; ++i ) {
746 if (
get(i) )
return true;
753 for ( IndexType i = 0 ; i <
size ; ++i ) {
754 if ( std::abs(
get(i))>eps )
return true;
761 for ( IndexType i = 0 ; i <
size ; ++i ) {
762 std::cout <<
get(i) <<
" ";
764 std::cout << std::endl;
770 std::ofstream outfile;
771 outfile.open(filename.c_str(),std::ios::out);
773 std::stringstream oss;
777 outfile << std::setprecision(7);
779 for ( IndexType i = 0 ; i <
size ; ++i ) {
780 outfile <<
get(i) <<
"\n";
812 void scale(
const float a);
815 void add(
const float c);
Default Auryn vector template.
Definition: auryn_definitions.h:327
void zero_effective_zeros(const T epsilon=1e-3)
Sets all values whose absolute value is smaller than epsilon to zero.
Definition: AurynVector.h:709
void check_size(AurynVector *v)
Checks if vector size matches to this instance.
Definition: AurynVector.h:90
void sum(AurynVector *a, AurynVector *b)
Computes the sum a+b and stores the result in this instance.
Definition: AurynVector.h:479
double l2norm()
Computes the l2 norm of the vector.
Definition: AurynVector.h:656
T fast_exp256(T x)
Computes approximation of exp(x) via fast series approximation up to n=256.
Definition: AurynVector.h:133
void sqr()
Squares each element.
Definition: AurynVector.h:533
void set_random(unsigned int seed=0)
Initializes vector elements with Gaussian of unit varince and a seed derived from system time if no s...
Definition: AurynVector.h:738
void elementwise_max(AurynVector *v1, AurynVector *v2)
Elementwise max operation.
Definition: AurynVector.h:388
void print()
Print vector elements to stdout for debugging.
Definition: AurynVector.h:760
void mul(AurynVector *v)
Element-wise vector multiply.
Definition: AurynVector.h:311
virtual ~AurynVector()
Default destructor.
Definition: AurynVector.h:173
void sum(AurynVector *a, const T b)
Computes the sum a+b and stores the result in this instance.
Definition: AurynVector.h:491
void check_size(IndexType x)
Checks if argument is larger than size and throws and exception if so.
Definition: AurynVector.h:78
T * data
Pointer to the array housing the data.
Definition: AurynVector.h:154
void set_all(const T v)
Set all elements to value v.
Definition: AurynVector.h:232
void sub(const T c)
Subtract constant c to each vector element.
Definition: AurynVector.h:288
void set_zero()
Set all elements to zero.
Definition: AurynVector.h:240
void allocate(const NeuronID n)
Implements aligned memory allocation.
Definition: AurynVector.h:100
double mean()
Computes the mean of the vector elements on this rank.
Definition: AurynVector.h:622
void mul(const float a)
Definition: AurynVector.h:819
int n
Definition: mkpat.py:5
void follow(AurynVector< T, IndexType > *v, const T rate)
Follows target vector v with rate.
Definition: AurynVector.h:372
IndexType size
Size of the vector.
Definition: AurynVector.h:151
double element_sum()
Computes the sum of the vector elements.
Definition: AurynVector.h:630
void fast_exp()
Computes an approximation of exp(x) for each vector element.
Definition: AurynVector.h:416
Definition: auryn_definitions.h:265
void diff(AurynVector *a, AurynVector *b)
Computes the difference a-b and stores the result in this instance.
Definition: AurynVector.h:502
void mul_specific(const IndexType i, const T c)
Multiply to specific vector element with data index i with the constant c.
Definition: AurynVector.h:270
Definition: auryn_definitions.h:208
void inv()
Computes 1./x of each element.
Definition: AurynVector.h:469
double min()
Returns the min of the vector elements.
Definition: AurynVector.h:682
void freebuf()
Definition: AurynVector.h:122
bool any()
Returns true if any element is nonzero.
Definition: AurynVector.h:744
double var()
Computes the variance of the vector elements on this rank.
Definition: AurynVector.h:593
AurynVector(AurynVector *vec)
Copy constructor.
Definition: AurynVector.h:165
void exp()
Computes exp(x) for each vector element.
Definition: AurynVector.h:430
void pow(const unsigned int n)
Takes each element to the n-th power.
Definition: AurynVector.h:406
Definition: ABSConnection.h:38
void neg()
Flips the sign of all elements.
Definition: AurynVector.h:461
IndexType nonzero()
Computes number of nonzero elements on this rank.
Definition: AurynVector.h:697
void sub(AurynVector *v)
Elementwise subtraction.
Definition: AurynVector.h:294
AurynFloat AurynState
Type for Auryn state variables (default single precision since it needs to be compatible with auryn_v...
Definition: auryn_definitions.h:160
void div(const T a)
Element-wise division.
Definition: AurynVector.h:322
void diff(AurynVector *a, const T b)
Computes the difference a-b and stores the result in this instance.
Definition: AurynVector.h:514
void abs()
Takes absolute value of each element.
Definition: AurynVector.h:541
void sigmoid(AurynVector *x, const T beta, const T thr)
Computes sigmoid(beta*(x-thr)) for each vector element and stores result in this instance.
Definition: AurynVector.h:440
void add(AurynVector *v)
Adds a vector v to the vector.
Definition: AurynVector.h:279
void set_random_normal(AurynState mean=0.0, AurynState sigma=1.0, unsigned int seed=8721)
Definition: AurynVector.h:730
void rect()
Rectifies all elements.
Definition: AurynVector.h:549
bool any(T eps)
Returns true if any element is nonzero.
Definition: AurynVector.h:752
Definition: auryn_definitions.h:306
void neg_rect()
Negatively rectifies all elements.
Definition: AurynVector.h:560
#define SIMD_NUM_OF_PARALLEL_FLOAT_OPERATIONS
Use Intel Cilk Plus – only has an effect when CODE_USE_SIMD_INSTRUCTIONS_EXPLICITLY is enabled...
Definition: auryn_definitions.h:64
void add_random_normal(AurynState mean=0.0, AurynState sigma=1.0, unsigned int seed=8721)
Definition: AurynVector.h:716
AurynVector(IndexType n)
Default constructor.
Definition: AurynVector.h:157
void copy(AurynVector *v)
Copies vector v.
Definition: AurynVector.h:200
void add(const T c)
Adds constant c to each vector element.
Definition: AurynVector.h:255
void diff(const T a, AurynVector *b)
Computes the difference a-b and stores the result in this instance.
Definition: AurynVector.h:522
~AurynVectorFloat()
Default destructor.
Definition: AurynVector.h:806
T * ptr(IndexType i=0)
Gets pointer to element i from vector.
Definition: AurynVector.h:217
double max()
Returns the max of the vector elements.
Definition: AurynVector.h:669
void div(AurynVector *v)
Element-wise vector division.
Definition: AurynVector.h:330
void sqrt()
Takes the square root of each element.
Definition: AurynVector.h:453
void follow_scalar(const T a, const T rate)
Like follow but with a scalar target value a.
Definition: AurynVector.h:380
void write_to_file(std::string filename)
Print vector elements to a text file for debugging.
Definition: AurynVector.h:768
double std()
Computes the standard deviation of all elements on this rank.
Definition: AurynVector.h:612
void add_specific(const IndexType i, const T c)
Adds the value c to specific vector element i.
Definition: AurynVector.h:263
Default AurynVectorFloat class for performance computation.
Definition: AurynVector.h:796
void elementwise_max(AurynVector *v1)
Elementwise max operation with another vector.
Definition: AurynVector.h:398
virtual void resize(IndexType new_size)
resize data array to new_size
Definition: AurynVector.h:184
void clip(T min, T max)
Clips all vector elements to the range min max.
Definition: AurynVector.h:574
double l1norm()
Computes the l1 norm of the vector.
Definition: AurynVector.h:642
void div(AurynVector *a, AurynVector *b)
Element-wise vector division which stores the result in this.
Definition: AurynVector.h:343
void saxpy(const T a, AurynVector *x)
SAXPY operation as in GSL.
Definition: AurynVector.h:357
std::string string
Standard library string type which is imported into Auryn namespace.
Definition: auryn_definitions.h:156
void mul(const T a)
Multiply all vector elements by constant.
Definition: AurynVector.h:303
unsigned int NeuronID
NeuronID is an unsigned integeger type used to index neurons in Auryn.
Definition: auryn_definitions.h:151
void scale(const T a)
Scales all vector elements by a.
Definition: AurynVector.h:246