Auryn simulator  v0.8.1-206-gb56e451
Plastic Spiking Neural Network Simulator
ComplexMatrix.h
Go to the documentation of this file.
1 /*
2 * Copyright 2014-2018 Friedemann Zenke
3 *
4 * This file is part of Auryn, a simulation package for plastic
5 * spiking neural networks.
6 *
7 * Auryn is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
11 *
12 * Auryn is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with Auryn. If not, see <http://www.gnu.org/licenses/>.
19 *
20 * If you are using Auryn or parts of it for your work please cite:
21 * Zenke, F. and Gerstner, W., 2014. Limits to high-speed simulations
22 * of spiking neural networks using general-purpose computers.
23 * Front Neuroinform 8, 76. doi: 10.3389/fninf.2014.00076
24 */
25 
26 #ifndef COMPLEXMATRIX_H_
27 #define COMPLEXMATRIX_H_
28 
29 #include "auryn_global.h"
30 #include "auryn_definitions.h"
31 
32 #include <boost/serialization/utility.hpp>
33 #include <boost/serialization/split_member.hpp>
34 
35 namespace auryn {
36 
74 template <typename T>
76 {
77 private:
78 
80  template<class Archive>
81  void save(Archive & ar, const unsigned int version) const
82  {
83  ar & m_rows;
84  ar & n_cols;
85  ar & n_z_values;
86  ar & current_row;
87  ar & current_col;
88  ar & statesize;
89  ar & n_nonzero;
90 
91  // rowpointers -- translate in elements per row
92  for ( NeuronID i = 0 ; i < m_rows+1 ; ++i ) {
93  NeuronID num_elements = rowptrs[i]-rowptrs[0];
94  ar & num_elements;
95  }
96  // colindices
97  for (AurynLong i = 0 ; i < n_nonzero ; ++i) {
98  ar & colinds[i];
99  }
100  // data
101  for (StateID z = 0; z < n_z_values ; ++z ) {
102  ar & *(statevectors[z]);
103  }
104  }
105  template<class Archive>
106  void load(Archive & ar, const unsigned int version)
107  {
108  const StateID cur_num_syn_states = get_num_synaptic_states();
109  AurynLong load_statesize;
110  AurynLong load_n_nonzero;
111 
112  ar & m_rows;
113  ar & n_cols;
114  ar & n_z_values;
115  ar & current_row;
116  ar & current_col;
117  ar & load_statesize;
118  ar & load_n_nonzero;
119 
120  if ( n_z_values != cur_num_syn_states ) { // check if we have the same number of tensor modes
122  }
123 
124  // allocate necessary memory and set statesize
125  resize_buffers(load_statesize);
126 
127  // rowpointers -- translate in elements per row
128  for ( NeuronID i = 0 ; i < m_rows+1 ; ++i ) {
129  NeuronID num_elements;
130  ar & num_elements;
131  rowptrs[i] = rowptrs[0]+num_elements;
132  }
133  // colindices
134  for (AurynLong i = 0 ; i < load_n_nonzero ; ++i) {
135  ar & colinds[i];
136  }
137  // data
138  for (StateID z = 0; z < n_z_values ; ++z ) {
139  ar & *(statevectors[z]);
140  }
141  // set the nonzero value to the actual number
142  n_nonzero = load_n_nonzero;
143  }
144  BOOST_SERIALIZATION_SPLIT_MEMBER()
145 
146 
147  AurynLong data_index_error_value;
148 
150  NeuronID m_rows,n_cols;
152  NeuronID n_z_values;
154  NeuronID current_row, current_col;
156  AurynLong n_nonzero;
158  AurynLong statesize;
159 
160 protected:
163 
166 
169 
171  void prepare_state_vectors();
172 
174  void init(NeuronID m, NeuronID n, AurynLong size, NeuronID z );
175  void free();
176 
177 public:
180 
182  ComplexMatrix();
183 
186 
188  ComplexMatrix(NeuronID rows, NeuronID cols, AurynLong size=256, StateID n_values=1 );
189 
191  virtual ~ComplexMatrix();
192 
194  void clear();
195 
198 
201 
206  void resize_buffers(AurynLong size);
207 
211 
215  void prune();
216 
222  void push_back(const NeuronID i, const NeuronID j, const T value);
223 
225  void copy(ComplexMatrix * mat);
226 
227 
228  /* \brief Sets element identified by the data index to value */
229  void set_element(AurynLong data_index, T value, StateID z=0);
230 
231  /* Deprecated: TODO Remove this function */
232  void set_data(AurynLong i, T value, StateID z=0);
233 
234 
235  /* \brief Scales element identified by the data index by value */
236  void scale_element(AurynLong data_index, T value, StateID z=0);
237 
238  /* Deprecated: TODO Remove this function */
239  void scale_data(AurynLong i, T value, StateID z=0);
240 
242  T get_data(AurynLong i, StateID z=0);
243 
245  T * get_data_ptr(const AurynLong data_index, const StateID z=0);
247  T * get_data_ptr(const NeuronID * ind_ptr, const StateID z=0);
248 
250  T get_data(const NeuronID * ind_ptr, StateID z=0);
251 
257  void fill_na();
258 
259 
264  void fill_zeros();
265 
266 
272 
274  T get(const NeuronID i, const NeuronID j, const NeuronID z=0);
275 
277  bool exists(const NeuronID i, const NeuronID j, const StateID z=0);
278 
280  T * get_ptr(const NeuronID i, const NeuronID j, const StateID z=0);
281 
287  T * get_ptr(const AurynLong data_index, const StateID z=0);
288 
291  T get_element(const AurynLong data_index, const StateID z);
292 
294  AurynLong ind_ptr_to_didx(const NeuronID * ind_ptr);
296  AurynLong get_data_index(const NeuronID * ind_ptr);
298  AurynLong get_data_index(const NeuronID i, const NeuronID j);
300  AurynLong data_ptr_to_didx(const T * ptr);
302  AurynLong get_data_index(const T * ptr) { return data_ptr_to_didx(ptr); };
303 
304 
305  /* Methods concerning synaptic state vectors. */
306 
308  void set_num_synaptic_states(const StateID zsize);
310  void set_num_synapse_states(const StateID zsize);
317 
319  T * get_state_begin(const StateID z=0);
321  T * get_state_end(const StateID z=0);
323  void state_set_all(T * x, const T value);
325  void state_saxpy(const T a, T * x, T * y);
327  void state_mul(T * x, T * y);
329  void state_add(T * x, T * y);
331  void state_sub(T * x, T * y);
333  void state_sub(T * x, T * y, T * res);
335  void state_scale(const T a, T * x);
337  void state_add_const(const T a, T * x);
339  void state_clip(T * x, const T a, const T b);
341  T * state_get_data_ptr(T * x, NeuronID i);
342 
343 
344  void add_value(const AurynLong data_index, T value);
345  NeuronID get_colind(const AurynLong data_index);
346 
347  bool set(const NeuronID i, const NeuronID j, T value);
349  void set_all(const T value, const StateID z=0);
351  void set_row(const NeuronID i, const T value);
353  void scale_row(const NeuronID i, const T value);
355  void scale_all(const T value);
357  void set_col(const NeuronID j, const T value);
359  void scale_col(const NeuronID j, const T value);
360  double sum_col(const NeuronID j);
370  void print();
371 
376  double mean();
377 
385  NeuronID ** get_rowptrs();
387  T * get_data_begin(const StateID z=0);
389  T * get_data_end(const StateID z=0);
391  T get_value(const AurynLong data_index);
393  T get_value(NeuronID * r);
395  T * get_value_ptr(const NeuronID i);
397  T * get_value_ptr(NeuronID * r);
399 };
400 
401 template <typename T>
402 T * ComplexMatrix<T>::get_data_ptr(const AurynLong data_index, const StateID z)
403 {
404  return statevectors[z]->data+data_index;
405 }
406 
407 template <typename T>
409 {
410  return *get_data_ptr(i,z);
411 }
412 
413 template <typename T>
414 T * ComplexMatrix<T>::get_data_ptr(const NeuronID * ind_ptr, const StateID z)
415 {
416  size_t ptr_offset = ind_ptr-get_ind_begin();
417  return statevectors[z]->data+ptr_offset;
418 }
419 
420 template <typename T>
422 {
423  return *(get_data_ptr(ind_ptr,z));
424 }
425 
426 template <typename T>
427 T ComplexMatrix<T>::get_element(const AurynLong data_index, const StateID z)
428 {
429  return *get_data_ptr(data_index);
430 }
431 
432 template <typename T>
433 void ComplexMatrix<T>::set_element(AurynLong data_index, T value, StateID z)
434 {
435  if (data_index<statesize)
436  statevectors[z]->data[data_index] = value;
437 }
438 
439 
440 template <typename T>
442 {
443  set_element(i,value,z);
444 }
445 
446 template <typename T>
448 {
449  if (data_index<statesize)
450  statevectors[z]->data[data_index] *= value;
451 }
452 
453 template <typename T>
455 {
456  scale_element(i, value, z);
457 }
458 
459 template <typename T>
461 {
462  current_row = 0;
463  current_col = 0;
464  n_nonzero = 0;
465  rowptrs[0] = colinds;
466  rowptrs[1] = colinds;
467 }
468 
469 template <typename T>
471 {
472  T * vec = new AurynVector<T,AurynLong>(get_statesize());
473  return vec;
474 }
475 
476 template <typename T>
478 {
479  if ( z >= get_num_synaptic_states() ) {
480  logger->error("Trying to access a complex state larger than number of states in the tensor");
482  }
483  return statevectors[z];
484 }
485 
486 template <typename T>
488 {
489  // add synaptic state vectors until we have enough
490  while ( statevectors.size() < get_num_synaptic_states() ) {
492  statevectors.push_back(vec);
493  }
494 
495  // remove state vectors from the end until we have the right amount
496  // if the above code ran this will not run
497  while ( statevectors.size() > get_num_synaptic_states() ) {
498  delete [] *(statevectors.end());
499  statevectors.pop_back();
500  }
501 }
502 
503 template <typename T>
505 {
506  m_rows = m;
507  n_cols = n;
508 
509  statesize = size; // assumed maximum number of connections
510  n_z_values = z;
511 
512  data_index_error_value = std::numeric_limits<AurynTime>::max();
513 
514 
515  rowptrs = new NeuronID * [m_rows+1];
516  colinds = new NeuronID [get_datasize()];
518  clear();
519 }
520 
521 template <typename T>
523 {
524  const AurynLong old_statesize = get_statesize();
525  const AurynLong new_statesize = std::min(new_size,(AurynLong)m_rows*n_cols);
526 
527  if ( old_statesize == new_statesize ) return; // nothing to be done
528 
529  // allocate new mem
530  NeuronID * new_colinds = new NeuronID [new_statesize];
531 
532  // copy column indices
533  const AurynLong copysize = std::min(old_statesize,new_statesize);
534  std::copy(colinds, colinds+copysize, new_colinds);
535 
536  // update rowpointers
537  ptrdiff_t offset = new_colinds-colinds;
538  for ( NeuronID i = 0 ; i < m_rows+1 ; ++i ) {
539  rowptrs[i] += offset;
540  }
541 
542  // free old memory
543  delete [] colinds;
544  colinds = new_colinds;
545 
546  // resize state vectors individually
547  for ( StateID i = 0 ; i < get_num_synaptic_states() ; ++i ) {
548  statevectors[i]->resize(new_statesize);
549  }
550 
551  statesize = new_statesize;
552 }
553 
554 template <typename T>
556 {
557  free();
558  init(m_rows, n_cols, size, n_z_values );
559 }
560 
561 template <typename T>
563 {
564  if ( get_datasize() > get_nonzero() )
566 }
567 
568 template <typename T>
570 {
571  init(1,1,2,1);
572 }
573 
574 template <typename T>
576 {
577  init(mat->get_m_rows(), mat->get_n_cols(), mat->get_nonzero(), mat->get_num_z_values() );
578  copy(mat);
579 }
580 
581 template <typename T>
583 {
584  init(rows, cols, statesize, n_values);
585 }
586 
587 template <typename T>
589 {
590  delete [] rowptrs;
591  delete [] colinds;
592  while ( statevectors.size() ) {
593  AurynVector<T,AurynLong> * vec = statevectors.back();
594  delete vec;
595  statevectors.pop_back();
596  }
597 }
598 
599 template <typename T>
601 {
602  if ( get_m_rows() != mat->get_m_rows() || get_n_cols() != mat->get_n_cols() )
604 
605  clear();
606 
607  // copy sparse strcture and first state
608  for ( NeuronID i = 0 ; i < mat->get_m_rows() ; ++i ) {
609  for ( NeuronID * r = mat->get_row_begin(i) ; r != mat->get_row_end(i) ; ++r ) {
610  push_back(i,*r,mat->get_value(r));
611  }
612  }
613 
614  // copy the other states
615  for ( StateID z = 1 ; z < get_num_synaptic_states() ; ++z ) {
616  statevectors[z]->copy(mat->statevectors[z]);
617  }
618 }
619 
620 template <typename T>
622 {
623  free();
624 }
625 
626 template <typename T>
628 {
629  if ( i >= m_rows || j >= n_cols ) throw AurynMatrixDimensionalityException();
630  while ( i > current_row )
631  {
632  rowptrs[current_row+2] = rowptrs[current_row+1]; // save value of last element
633  current_col = 0;
634  current_row++;
635  }
636  current_col = j;
637  T * data_z0 = get_synaptic_state_vector(0)->data;
638  if (i >= current_row && j >= current_col) {
639  if ( n_nonzero >= get_datasize() ) throw AurynMatrixBufferException();
640  *(rowptrs[i+1]) = j; // write last j to end of index array
641  data_z0[rowptrs[i+1]-colinds] = value; // write value to end of data array
642  ++rowptrs[i+1]; //increment end by one
643  rowptrs[m_rows] = rowptrs[i+1]; // last (m_row+1) marks end of last row
644  n_nonzero++;
645  } else {
647  }
648 }
649 
650 template <typename T>
652 {
653  return get_statesize();
654 }
655 
656 template <typename T>
658 {
659  return statesize;
660 }
661 
662 
663 template <typename T>
665 {
666  return statesize*n_z_values;
667 }
668 
669 template <typename T>
671 {
672  return n_nonzero;
673 }
674 
675 template <typename T>
677 {
678  for ( NeuronID i = current_row ; i < m_rows-1 ; ++i )
679  {
680  rowptrs[i+2] = rowptrs[i+1]; // save value of last element
681  }
682  current_row = get_m_rows();
683 }
684 
685 template <typename T>
687 {
688  fill_na();
689 }
690 
691 template <typename T>
693 {
694  if ( get_data_index(i,j) == data_index_error_value || z >= get_num_synaptic_states() )
695  return false;
696  else
697  return true;
698 }
699 
700 template <typename T>
702 {
703  n_z_values = zsize;
705  resize_buffers(statesize);
706 }
707 
708 template <typename T>
710 {
712 }
713 
714 template <typename T>
716 {
717  return n_z_values;
718 }
719 
720 template <typename T>
722 {
723  return get_num_synaptic_states();
724 }
725 
726 template <typename T>
728 {
729  return get_num_synaptic_states();
730 }
731 
732 template <typename T>
734 {
735  return get_synaptic_state_vector(z);
736 }
737 
738 template <typename T>
740 {
741  return statevectors[z]->data;
742 }
743 
744 
745 template <typename T>
747 {
748  return get_state_begin(z)+get_datasize();
749 }
750 
751 
752 template <typename T>
754 {
755 #ifdef DEBUG
756  std::cout << "cm: starting bisect " << i << ":" << j << std::endl;
757 #endif // DEBUG
758 
759  // check bounds
760  if ( !(i < m_rows && j < n_cols) ) return data_index_error_value;
761 
762  // perform binary search
763  NeuronID * lo = rowptrs[i];
764  NeuronID * hi = rowptrs[i+1];
765 
766  if ( lo >= hi ) // no elements/targets in this row
767  return data_index_error_value;
768 
769  while ( lo < hi ) {
770  NeuronID * c = lo + (hi-lo)/2;
771  if ( *c < j ) lo = c+1;
772  else hi = c;
773 #ifdef DEBUG
774  std::cout << "cm: " << i << ":" << j << " " << *lo << ":" << *hi << std::endl;
775 #endif // DEBUG
776  }
777 
778  if ( *lo == j ) {
779 #ifdef DEBUG
780  std::cout << "cm: " << "found element at data array position "
781  << (lo-colinds) << std::endl;
782 #endif // DEBUG
783  return (lo-colinds);
784  }
785 
786 #ifdef DEBUG
787  std::cout << "cm: " << "element not found" << std::endl;
788 #endif // DEBUG
789 
790  return data_index_error_value;
791 }
792 
793 
794 
795 template <typename T>
797 {
798  return ind_ptr - get_ind_begin();
799 }
800 
801 template <typename T>
803 {
804  return ind_ptr_to_didx(ind_ptr);
805 }
806 
807 template <typename T>
809 {
810  return ptr - get_data_begin();
811 }
812 
813 template <typename T>
815 {
816  AurynLong data_index = get_data_index(i,j);
817  if ( data_index != data_index_error_value )
818  return statevectors[z]->data+data_index;
819  else
820  return NULL;
821 }
822 
823 template <typename T>
825 {
826  AurynLong data_index = get_data_index(i,j);
827  if ( data_index != data_index_error_value )
828  return *(statevectors[z]->data+get_data_index(i,j));
829  else
830  return 0;
831 }
832 
833 template <typename T>
834 T * ComplexMatrix<T>::get_ptr(const AurynLong data_index, const StateID z)
835 {
836  return get_data_ptr(data_index, z);
837 }
838 
839 
840 template <typename T>
842 {
843  return *get_ptr(data_index);
844 }
845 
846 template <typename T>
847 void ComplexMatrix<T>::add_value(AurynLong data_index, T value)
848 {
849  *get_ptr(data_index) += value;
850 }
851 
852 template <typename T>
854 {
855  return colinds[data_index];
856 }
857 
858 template <typename T>
860 {
861  T * ptr = get_ptr(i,j);
862  if ( ptr != NULL) {
863  *ptr = value;
864  return true;
865  }
866  else
867  return false;
868 }
869 
870 template <typename T>
872 {
873  NeuronID * rowbegin = rowptrs[i];
874  NeuronID * rowend = rowptrs[i+1]--;
875 
876  for (NeuronID * c = rowbegin ; c <= rowend ; ++c)
877  {
878  *get_ptr(c-colinds) *= value;
879  }
880 }
881 
882 template <typename T>
884 {
886 }
887 
888 template <typename T>
890 {
891  NeuronID * rowbegin = rowptrs[i];
892  NeuronID * rowend = rowptrs[i+1]--;
893 
894  for (NeuronID * c = rowbegin ; c <= rowend ; ++c)
895  {
896  *get_ptr(c-colinds) = value;
897  }
898 }
899 
900 template <typename T>
902 {
903  get_state_vector(z)->set_all(value);
904 }
905 
906 template <typename T>
908 {
909  for ( AurynLong i = 0 ; i < n_nonzero ; ++i ) {
910  if ( colinds[i] == j ) scale_data(i,value);
911  }
912 }
913 
914 template <typename T>
916 {
917  double sum = 0;
918  for ( AurynLong i = 0 ; i < n_nonzero ; ++i ) {
919  if ( colinds[i] == j )
920  sum += *(get_data_begin()+i);
921  }
922  return sum;
923 }
924 
925 template <typename T>
927 {
928  for ( AurynLong i = 0 ; i < n_nonzero ; ++i ) {
929  if ( colinds[i] == j ) set_data(i,value);
930  }
931 }
932 
933 template <typename T>
935 {
936  return rowptrs[0];
937 }
938 
939 template <typename T>
941 {
942  return rowptrs[i];
943 }
944 
945 template <typename T>
947 {
948  return rowptrs[i]-rowptrs[0];
949 }
950 
951 template <typename T>
953 {
954  return rowptrs[i+1];
955 }
956 
957 template <typename T>
959 {
960  return rowptrs[i+1]-rowptrs[0];
961 }
962 
963 
964 template <typename T>
966 {
967  return rowptrs;
968 }
969 
970 template <typename T>
972 {
973  return get_data_ptr((AurynLong)0,z);
974 }
975 
976 template <typename T>
978 {
979  return get_data_ptr(get_nonzero(),z);
980 }
981 
982 
983 template <typename T>
985 {
986  return 1.*n_nonzero/get_datasize();
987 }
988 
989 template <typename T>
991 {
992  return m_rows;
993 }
994 
995 template <typename T>
997 {
998  return n_cols;
999 }
1000 
1001 
1002 template <typename T>
1004 {
1005  std::cout << get_nonzero() << " elements in sparse matrix:" << std::endl;
1006  for (NeuronID i = 0 ; i < m_rows ; ++i) {
1007  for (NeuronID * r = get_row_begin(i) ; r != get_row_end(i) ; ++r ) {
1008  std::cout << i << " " << *r << " " << *get_ptr(r-colinds) << "\n";
1009  // TODO not dumping the other states yet
1010  }
1011  }
1012 }
1013 
1014 template <typename T>
1016 {
1017  double sum = 0;
1018  for (NeuronID i = 0 ; i < get_nonzero() ; ++i) {
1019  sum += *get_ptr(i);
1020  }
1021  return sum/get_nonzero();
1022 }
1023 
1024 template <typename T>
1026 {
1027  for (T * iter=x;
1028  iter!=x+get_nonzero();
1029  ++iter ) {
1030  *iter = value;
1031  }
1032 }
1033 
1034 template <typename T>
1035 void ComplexMatrix<T>::state_saxpy(T a, T * x, T * y)
1036 {
1037  for (AurynLong i = 0 ; i < get_nonzero() ; ++i ) {
1038  y[i] = a*x[i]+y[i];
1039  }
1040 }
1041 
1042 template <typename T>
1044 {
1045  for (AurynLong i = 0 ; i < get_nonzero() ; ++i ) {
1046  y[i] = x[i]*y[i];
1047  }
1048 }
1049 
1050 template <typename T>
1052 {
1053  for (AurynLong i = 0 ; i < get_nonzero() ; ++i ) {
1054  y[i] = x[i]+y[i];
1055  }
1056 }
1057 
1058 template <typename T>
1059 void ComplexMatrix<T>::state_sub(T * x, T * y, T * res)
1060 {
1061  for (AurynLong i = 0 ; i < get_nonzero() ; ++i ) {
1062  res[i] = x[i]-y[i];
1063  }
1064 }
1065 
1066 template <typename T>
1068 {
1069  state_sub(x,y,y);
1070 }
1071 
1072 template <typename T>
1074 {
1075  for (AurynLong i = 0 ; i < get_nonzero() ; ++i ) {
1076  y[i] = a*y[i];
1077  }
1078 }
1079 
1080 template <typename T>
1082 {
1083  for (AurynLong i = 0 ; i < get_nonzero() ; ++i ) {
1084  y[i] = a+y[i];
1085  }
1086 }
1087 
1088 template <typename T>
1089 void ComplexMatrix<T>::state_clip(T * x, T a, T b)
1090 {
1091  for (AurynLong i = 0 ; i < get_nonzero() ; ++i ) {
1092  if ( x[i] < a ) x[i] = a;
1093  else if ( x[i] > b ) x[i] = b;
1094  }
1095 }
1096 
1097 template <typename T>
1099 {
1100  return x+i;
1101 }
1102 
1103 template <typename T>
1105 {
1106  return get_data_begin()[r-get_ind_begin()];
1107 }
1108 
1109 template <typename T>
1111 {
1112  return &get_data_begin()[i];
1113 }
1114 
1115 template <typename T>
1117 {
1118  return &get_data_begin()[r-get_ind_begin()];
1119 }
1120 
1121 template <typename T>
1123 {
1124  return r-get_ind_begin();
1125 }
1126 
1127 }
1128 
1129 #endif /*COMPLEXMATRIX_H_*/
1130 
AurynVector< T, AurynLong > * get_state_vector(StateID z=0)
Sames as get_synaptic_state_vector(StateID z)
Definition: ComplexMatrix.h:733
Default Auryn vector template.
Definition: auryn_definitions.h:327
void print()
stdout dump of all elements – for testing only.
Definition: ComplexMatrix.h:1003
r
Definition: mkpat.py:9
NeuronID get_n_cols()
Definition: ComplexMatrix.h:996
AurynLong ind_ptr_to_didx(const NeuronID *ind_ptr)
Returns data index to a particular element specifed by an index pointer.
Definition: ComplexMatrix.h:796
NeuronID * colinds
Array that holds the column indices of non-zero elements.
Definition: ComplexMatrix.h:165
T * get_ptr(const NeuronID i, const NeuronID j, const StateID z=0)
Returns the pointer to a particular element.
Definition: ComplexMatrix.h:814
T get_element(const AurynLong data_index, const StateID z)
Returns a particular element given its position in the data array.
Definition: ComplexMatrix.h:427
T get(const NeuronID i, const NeuronID j, const NeuronID z=0)
Value of synaptic state variable i,j,z returns zero if the element is zero or does not exist...
Definition: ComplexMatrix.h:824
AurynLong get_row_begin_index(NeuronID i)
Definition: ComplexMatrix.h:946
void copy(ComplexMatrix *mat)
Copies complex matrix mat.
Definition: ComplexMatrix.h:600
AurynLong get_memsize()
Returns statesize multiplied by number of states.
Definition: ComplexMatrix.h:664
void set_num_synapse_states(const StateID zsize)
Sets number of synaptic states (z-value)
Definition: ComplexMatrix.h:709
StateID get_num_synapse_states()
Synonymous to get_num_synaptic_states.
Definition: ComplexMatrix.h:727
AurynDouble get_fill_level()
Returns the fill level of the matrix element buffer.
Definition: ComplexMatrix.h:984
NeuronID * get_row_end(NeuronID i)
Definition: ComplexMatrix.h:952
T * data
Pointer to the array housing the data.
Definition: AurynVector.h:154
void prune()
Prunes the reserved memory such that datasize=get_nonzero() Note that this is an expensive operation ...
Definition: ComplexMatrix.h:562
void set_all(const T v)
Set all elements to value v.
Definition: AurynVector.h:232
AurynLong get_datasize()
Returns datasize: number of possible entries.
Definition: ComplexMatrix.h:651
void state_sub(T *x, T *y)
Computes x-y and stores result in y.
Definition: ComplexMatrix.h:1067
void set_all(const T value, const StateID z=0)
Sets all non-zero elements to value.
Definition: ComplexMatrix.h:901
STL namespace.
NeuronID * get_ind_begin()
Definition: ComplexMatrix.h:934
T get_value(const AurynLong data_index)
Returns the data value to an item that is i-th in the colindex array.
Definition: ComplexMatrix.h:841
AurynVector< T, AurynLong > * get_synaptic_state_vector(StateID z=0)
Returns pointer to statevector which is an AurynVector of specified synaptic state.
Definition: ComplexMatrix.h:477
double AurynDouble
Higher precision floating point datatype.
Definition: auryn_definitions.h:158
void state_set_all(T *x, const T value)
Sets all values in state x to value.
Definition: ComplexMatrix.h:1025
std::vector< AurynVector< T, AurynLong > *> statevectors
Vector that holds pointers to the state vectors storing the synaptic states.
Definition: ComplexMatrix.h:179
void state_clip(T *x, const T a, const T b)
Clips state values to interval [a,b].
Definition: ComplexMatrix.h:1089
bool set(const NeuronID i, const NeuronID j, T value)
Definition: ComplexMatrix.h:859
int n
Definition: mkpat.py:5
void scale_data(AurynLong i, T value, StateID z=0)
Definition: ComplexMatrix.h:454
double mean()
Return mean value of elements for the first complex state (z=0).
Definition: ComplexMatrix.h:1015
NeuronID get_m_rows()
Definition: ComplexMatrix.h:990
Definition: auryn_definitions.h:232
AurynLong get_nonzero()
Returns number of non-zero elements.
Definition: ComplexMatrix.h:670
Logger * logger
Global pointer to instance of Logger which needs to be initialized in every simulation main program...
Definition: auryn_global.cpp:36
AurynLong get_row_end_index(NeuronID i)
Definition: ComplexMatrix.h:958
Template for a sparse matrix with row major ordering and fast access of rows and capability to store ...
Definition: ComplexMatrix.h:75
void prepare_state_vectors()
Matches size of statevectors to number of synaptic states.
Definition: ComplexMatrix.h:487
NeuronID * get_row_begin(NeuronID i)
Definition: ComplexMatrix.h:940
void state_scale(const T a, T *x)
Scale state x by a.
Definition: ComplexMatrix.h:1073
T get_data(AurynLong i, StateID z=0)
Definition: ComplexMatrix.h:408
void fill_na()
Pads non-existing elements for the remaining elements to a matrix.
Definition: ComplexMatrix.h:676
virtual ~ComplexMatrix()
Default destructor.
Definition: ComplexMatrix.h:621
Definition: ABSConnection.h:38
StateID get_num_z_values()
Returns number of synaptic states (z-value)
Definition: ComplexMatrix.h:721
unsigned long AurynLong
An unsigned long type used to count synapses or similar.
Definition: auryn_definitions.h:154
void state_mul(T *x, T *y)
Multiplies x and y and stores result in y.
Definition: ComplexMatrix.h:1043
T * get_data_begin(const StateID z=0)
Returns pointer to data value corresponding to the first element.
Definition: ComplexMatrix.h:971
void scale_row(const NeuronID i, const T value)
Scales all non-zero elements in row i to value.
Definition: ComplexMatrix.h:871
void resize_buffers(AurynLong size)
Resize buffer.
Definition: ComplexMatrix.h:522
void clear()
Clears matrix.
Definition: ComplexMatrix.h:460
T * get_state_end(const StateID z=0)
Gets pointer for the element behind the last of a given synaptic state vector.
Definition: ComplexMatrix.h:746
void fill_zeros()
Same as fill_na but deprecated because of bad name.
Definition: ComplexMatrix.h:686
T * get_state_begin(const StateID z=0)
Gets pointer for the first element of a given synaptic state vector.
Definition: ComplexMatrix.h:739
void scale_all(const T value)
Scales all non-zero elements.
Definition: ComplexMatrix.h:883
void set_data(AurynLong i, T value, StateID z=0)
Definition: ComplexMatrix.h:441
bool exists(const NeuronID i, const NeuronID j, const StateID z=0)
Returns true if the matrix element exists.
Definition: ComplexMatrix.h:692
void set_col(const NeuronID j, const T value)
Sets all non-zero elements in col j to value. Due to ordering this is slow and the use of this functi...
Definition: ComplexMatrix.h:926
void set_element(AurynLong data_index, T value, StateID z=0)
Definition: ComplexMatrix.h:433
void resize_buffer_and_clear(AurynLong size)
Resizes buffer and clears the matrix. This saves to copy all the data.
Definition: ComplexMatrix.h:555
void free()
Definition: ComplexMatrix.h:588
Definition: auryn_definitions.h:240
void push_back(const NeuronID i, const NeuronID j, const T value)
Definition: ComplexMatrix.h:627
NeuronID get_data_offset(NeuronID *r)
Definition: ComplexMatrix.h:1122
NeuronID ** get_rowptrs()
Definition: ComplexMatrix.h:965
AurynLong get_data_index(const NeuronID *ind_ptr)
Returns data index to a particular element specifed by an index pointer.
Definition: ComplexMatrix.h:802
friend class boost::serialization::access
Definition: ComplexMatrix.h:79
AurynVector< T, AurynLong > * alloc_synaptic_state_vector()
Returns a synaptic state vector.
Definition: ComplexMatrix.h:470
T * get_value_ptr(const NeuronID i)
Returns pointer to the the data value to an item that is i-th in the colindex array.
Definition: ComplexMatrix.h:1110
void set_row(const NeuronID i, const T value)
Sets all non-zero elements in row i to value.
Definition: ComplexMatrix.h:889
void error(std::string text)
Definition: Logger.cpp:141
void scale_col(const NeuronID j, const T value)
Scales all non-zero elements in col j to value. Due to ordering this is slow and the use of this func...
Definition: ComplexMatrix.h:907
void state_add_const(const T a, T *x)
Adds constant a to all values in x.
Definition: ComplexMatrix.h:1081
void add_value(const AurynLong data_index, T value)
Definition: ComplexMatrix.h:847
void init(NeuronID m, NeuronID n, AurynLong size, NeuronID z)
Default initializiation called by the constructor.
Definition: ComplexMatrix.h:504
void state_add(T *x, T *y)
Adds x and y and stores result in y.
Definition: ComplexMatrix.h:1051
Definition: auryn_definitions.h:224
double sum_col(const NeuronID j)
Definition: ComplexMatrix.h:915
StateID get_num_synaptic_states()
Returns number of synaptic states (z-value)
Definition: ComplexMatrix.h:715
Definition: auryn_definitions.h:248
T * state_get_data_ptr(T *x, NeuronID i)
Get data pointer for that state.
Definition: ComplexMatrix.h:1098
unsigned int StateID
StateID is an unsigned integeger type used to index synaptic states in Auryn.
Definition: auryn_definitions.h:153
T * get_data_end(const StateID z=0)
Returns pointer to data value corresponding to the element behind the last nonzero element...
Definition: ComplexMatrix.h:977
AurynLong get_statesize()
Same as datasize : number of possible entries.
Definition: ComplexMatrix.h:657
T * get_data_ptr(const AurynLong data_index, const StateID z=0)
Gets the matching data ptr for a given index i and state z.
Definition: ComplexMatrix.h:402
void state_saxpy(const T a, T *x, T *y)
Computes a*x + y and stores result in y.
Definition: ComplexMatrix.h:1035
NeuronID get_colind(const AurynLong data_index)
Definition: ComplexMatrix.h:853
void scale_element(AurynLong data_index, T value, StateID z=0)
Definition: ComplexMatrix.h:447
unsigned int NeuronID
NeuronID is an unsigned integeger type used to index neurons in Auryn.
Definition: auryn_definitions.h:151
AurynLong data_ptr_to_didx(const T *ptr)
Returns data index to a particular element specifed by a data pointer.
Definition: ComplexMatrix.h:808
ComplexMatrix()
Empty constructor.
Definition: ComplexMatrix.h:569
void set_num_synaptic_states(const StateID zsize)
Sets number of synaptic states (z-value)
Definition: ComplexMatrix.h:701
void scale(const T a)
Scales all vector elements by a.
Definition: AurynVector.h:246
NeuronID ** rowptrs
Array that holds the begin addresses of column indices.
Definition: ComplexMatrix.h:162