CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
MatRepSparse.h
Go to the documentation of this file.
1 #ifndef MatRepSparse_H
2 #define MatRepSparse_H
3 #include<type_traits>
4 #include<algorithm>
5 
10 template <typename T, unsigned int D1, unsigned int D2, unsigned int S, typename F=int(*)(int)>
11 class MatRepSparse {
12 
13 public:
15  template<typename FI>
16  explicit MatRepSparse(FI fi) : f(fi) { }
17 
18  typedef T value_type;
19 
20  static T & sink() {
21  static T t=0; // this should throw...
22  return t;
23  }
24 
25  static T const & csink() {
26  static const T t=0;
27  return t;
28  }
29 
30 
31  inline T const & operator()(unsigned int i, unsigned int j) const {
32  int k = f(i*D2+j);
33  return k<0 ? csink() : fArray[k];
34  }
35 
36  inline T& operator()(unsigned int i, unsigned int j) {
37  int k = f(i*D2+j);
38  return k<0 ? sink() : fArray[k];
39  }
40 
41  inline T& operator[](unsigned int i) {
42  int k = f(i);
43  return k<0 ? sink() : fArray[k];
44  }
45 
46  inline T const & operator[](unsigned int i) const {
47  int k = f(i);
48  return k<0 ? csink() : fArray[k];
49  }
50 
51  inline T apply(unsigned int i) const {
52  int k = f(i);
53  return k<0 ? 0 : fArray[k];
54  }
55 
56  inline T* Array() { return fArray; }
57 
58  inline const T* Array() const { return fArray; }
59 
63  template <class R>
64  inline MatRepSparse & operator=(const R&) {
65  static_assert(std::is_same<R,MatRepSparse<T,D1,D2,S,F>>::value,
66  "Cannot_assign_general_to_sparse_matrix_representation");
67  return *this;
68  }
69 
70  inline MatRepSparse & operator=(const MatRepSparse& rhs) {
71  for(unsigned int i=0; i<kSize; ++i) fArray[i] = rhs.Array()[i];
72  return *this;
73  }
74 
78  template <class R>
79  inline MatRepSparse & operator+=(const R&) {
80  static_assert(std::is_same<R,MatRepSparse<T,D1,D2,S,F>>::value,
81  "Cannot_add_general_to_sparse_matrix_representation");
82  return *this;
83  }
84  inline MatRepSparse & operator+=(const MatRepSparse& rhs) {
85  for(unsigned int i=0; i<kSize; ++i) fArray[i] += rhs.Array()[i];
86  return *this;
87  }
88 
92  template <class R>
93  inline MatRepSparse & operator-=(const R&) {
94  static_assert(std::is_same<R,MatRepSparse<T,D1,D2,S,F>>::value,
95  "Cannot_substract_general_to_sparse_matrix_representation");
96  return *this;
97  }
98 
99  inline MatRepSparse & operator-=(const MatRepSparse& rhs) {
100  for(unsigned int i=0; i<kSize; ++i) fArray[i] -= rhs.Array()[i];
101  return *this;
102  }
103  template <class R>
104  inline bool operator==(const R& rhs) const {
105  bool rc = true;
106  for(unsigned int i=0; i<D1*D2; ++i) {
107  rc = rc && (operator[](i) == rhs[i]);
108  }
109  return rc;
110  }
111 
112  enum {
119  };
120 
121 
122 public:
123  T fArray[kSize]={0};
124  F f;
125 };
126 
127 #endif //
Divides< B, C > D2
Definition: Factorize.h:145
int i
Definition: DBlmapReader.cc:9
bool operator==(const R &rhs) const
Definition: MatRepSparse.h:104
MatRepSparse & operator=(const R &)
Definition: MatRepSparse.h:64
const T * Array() const
Definition: MatRepSparse.h:58
MatRepSparse & operator=(const MatRepSparse &rhs)
Definition: MatRepSparse.h:70
T const & operator()(unsigned int i, unsigned int j) const
Definition: MatRepSparse.h:31
return no. of matrix rows
Definition: MatRepSparse.h:114
Divides< A, C > D1
Definition: Factorize.h:144
MatRepSparse & operator-=(const R &)
Definition: MatRepSparse.h:93
static T & sink()
Definition: MatRepSparse.h:20
int j
Definition: DBlmapReader.cc:9
return no. of matrix columns
Definition: MatRepSparse.h:116
static T const & csink()
Definition: MatRepSparse.h:25
double S(const TLorentzVector &, const TLorentzVector &)
Definition: Particle.cc:99
T & operator[](unsigned int i)
Definition: MatRepSparse.h:41
MatRepSparse & operator+=(const R &)
Definition: MatRepSparse.h:79
T const & operator[](unsigned int i) const
Definition: MatRepSparse.h:46
MatRepSparse & operator+=(const MatRepSparse &rhs)
Definition: MatRepSparse.h:84
return no of elements: rows*columns
Definition: MatRepSparse.h:118
static uInt32 F(BLOWFISH_CTX *ctx, uInt32 x)
Definition: blowfish.cc:281
T fArray[kSize]
Definition: MatRepSparse.h:123
long double T
MatRepSparse(FI fi)
Definition: MatRepSparse.h:16
T apply(unsigned int i) const
Definition: MatRepSparse.h:51
T & operator()(unsigned int i, unsigned int j)
Definition: MatRepSparse.h:36
MatRepSparse & operator-=(const MatRepSparse &rhs)
Definition: MatRepSparse.h:99