ROOT  6.07/01
Reference Guide
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
TMatrixTLazy.cxx
Go to the documentation of this file.
1 // @(#)root/matrix:$Id$
2 // Authors: Fons Rademakers, Eddy Offermann Nov 2003
3 
4 /*************************************************************************
5  * Copyright (C) 1995-2000, Rene Brun and Fons Rademakers. *
6  * All rights reserved. *
7  * *
8  * For the licensing terms see $ROOTSYS/LICENSE. *
9  * For the list of contributors see $ROOTSYS/README/CREDITS. *
10  *************************************************************************/
11 
12 //////////////////////////////////////////////////////////////////////////
13 // //
14 // Templates of Lazy Matrix classes. //
15 // //
16 // TMatrixTLazy //
17 // TMatrixTSymLazy //
18 // THaarMatrixT //
19 // THilbertMatrixT //
20 // THilbertMatrixTSym //
21 // //
22 //////////////////////////////////////////////////////////////////////////
23 
24 #include "TMatrixT.h"
25 #include "TMatrixTSym.h"
26 #include "TMatrixTLazy.h"
27 #include "TMath.h"
28 
34 
35 
36 ////////////////////////////////////////////////////////////////////////////////
37 
38 template<class Element>
39 THaarMatrixT<Element>::THaarMatrixT(Int_t order,Int_t no_cols)
40  : TMatrixTLazy<Element>(1<<order, no_cols == 0 ? 1<<order : no_cols)
41 {
42  if (order <= 0)
43  Error("THaarMatrixT","Haar order(%d) should be > 0",order);
44  if (no_cols < 0)
45  Error("THaarMatrixT","#cols(%d) in Haar should be >= 0",no_cols);
46 }
47 
48 ////////////////////////////////////////////////////////////////////////////////
49 /// Create an orthonormal (2^n)*(no_cols) Haar (sub)matrix, whose columns
50 /// are Haar functions. If no_cols is 0, create the complete matrix with
51 /// 2^n columns. Example, the complete Haar matrix of the second order is:
52 /// column 1: [ 1 1 1 1]/2
53 /// column 2: [ 1 1 -1 -1]/2
54 /// column 3: [ 1 -1 0 0]/sqrt(2)
55 /// column 4: [ 0 0 1 -1]/sqrt(2)
56 /// Matrix m is assumed to be zero originally.
57 
58 template<class Element>
60 {
61  R__ASSERT(m.IsValid());
62  const Int_t no_rows = m.GetNrows();
63  const Int_t no_cols = m.GetNcols();
64 
65  if (no_rows < no_cols) {
66  Error("MakeHaarMat","#rows(%d) should be >= #cols(%d)",no_rows,no_cols);
67  return;
68  }
69  if (no_cols <= 0) {
70  Error("MakeHaarMat","#cols(%d) should be > 0",no_cols);
71  return;
72  }
73 
74  // It is easier to calculate a Haar matrix when the elements are stored
75  // column-wise . Since we are row-wise, the transposed Haar is calculated
76 
77  TMatrixT<Element> mtr(no_cols,no_rows);
78  Element *cp = mtr.GetMatrixArray();
79  const Element *m_end = mtr.GetMatrixArray()+no_rows*no_cols;
80 
81  Element norm_factor = 1/TMath::Sqrt((Element)no_rows);
82 
83  // First row is always 1 (up to normalization)
84  Int_t j;
85  for (j = 0; j < no_rows; j++)
86  *cp++ = norm_factor;
87 
88  // The other functions are kind of steps: stretch of 1 followed by the
89  // equally long stretch of -1. The functions can be grouped in families
90  // according to their order (step size), differing only in the location
91  // of the step
92  Int_t step_length = no_rows/2;
93  while (cp < m_end && step_length > 0) {
94  for (Int_t step_position = 0; cp < m_end && step_position < no_rows;
95  step_position += 2*step_length, cp += no_rows) {
96  Element *ccp = cp+step_position;
97  for (j = 0; j < step_length; j++)
98  *ccp++ = norm_factor;
99  for (j = 0; j < step_length; j++)
100  *ccp++ = -norm_factor;
101  }
102  step_length /= 2;
103  norm_factor *= TMath::Sqrt(2.0);
104  }
105 
106  R__ASSERT(step_length != 0 || cp == m_end);
107  R__ASSERT(no_rows != no_cols || step_length == 0);
108 
109  m.Transpose(mtr);
110 }
111 
112 ////////////////////////////////////////////////////////////////////////////////
113 
114 template<class Element>
116 {
117  MakeHaarMat(m);
118 }
119 
120 ////////////////////////////////////////////////////////////////////////////////
121 
122 template<class Element>
124  : TMatrixTLazy<Element>(no_rows,no_cols)
125 {
126  if (no_rows <= 0)
127  Error("THilbertMatrixT","#rows(%d) in Hilbert should be > 0",no_rows);
128  if (no_cols <= 0)
129  Error("THilbertMatrixT","#cols(%d) in Hilbert should be > 0",no_cols);
130 }
131 
132 ////////////////////////////////////////////////////////////////////////////////
133 
134 template<class Element>
136  : TMatrixTLazy<Element>(row_lwb,row_upb,col_lwb,col_upb)
137 {
138  if (row_upb < row_lwb)
139  Error("THilbertMatrixT","row_upb(%d) in Hilbert should be >= row_lwb(%d)",row_upb,row_lwb);
140  if (col_upb < col_lwb)
141  Error("THilbertMatrixT","col_upb(%d) in Hilbert should be >= col_lwb(%d)",col_upb,col_lwb);
142 }
143 
144 ////////////////////////////////////////////////////////////////////////////////
145 /// Make a Hilbert matrix. Hilb[i,j] = 1/(i+j+1),
146 /// i,j=0...max-1 (matrix need not be a square one).
147 
148 template<class Element>
150 {
151  R__ASSERT(m.IsValid());
152  const Int_t no_rows = m.GetNrows();
153  const Int_t no_cols = m.GetNcols();
154 
155  if (no_rows <= 0) {
156  Error("MakeHilbertMat","#rows(%d) should be > 0",no_rows);
157  return;
158  }
159  if (no_cols <= 0) {
160  Error("MakeHilbertMat","#cols(%d) should be > 0",no_cols);
161  return;
162  }
163 
164  Element *cp = m.GetMatrixArray();
165  for (Int_t i = 0; i < no_rows; i++)
166  for (Int_t j = 0; j < no_cols; j++)
167  *cp++ = 1.0/(i+j+1.0);
168 }
169 
170 ////////////////////////////////////////////////////////////////////////////////
171 
172 template<class Element>
174 {
175  MakeHilbertMat(m);
176 }
177 
178 ////////////////////////////////////////////////////////////////////////////////
179 
180 template<class Element>
182  : TMatrixTSymLazy<Element>(no_rows)
183 {
184  if (no_rows <= 0)
185  Error("THilbertMatrixTSym","#rows(%d) in Hilbert should be > 0",no_rows);
186 }
187 
188 ////////////////////////////////////////////////////////////////////////////////
189 
190 template<class Element>
192  : TMatrixTSymLazy<Element>(row_lwb,row_upb)
193 {
194  if (row_upb < row_lwb)
195  Error("THilbertMatrixTSym","row_upb(%d) in Hilbert should be >= row_lwb(%d)",row_upb,row_lwb);
196 }
197 
198 ////////////////////////////////////////////////////////////////////////////////
199 /// Make a Hilbert matrix. Hilb[i,j] = 1/(i+j+1),
200 /// i,j=0...max-1 (matrix must be square).
201 
202 template<class Element>
204 {
205  R__ASSERT(m.IsValid());
206  const Int_t no_rows = m.GetNrows();
207  if (no_rows <= 0) {
208  Error("MakeHilbertMat","#rows(%d) should be > 0",no_rows);
209  return;
210  }
211 
212  Element *cp = m.GetMatrixArray();
213  for (Int_t i = 0; i < no_rows; i++)
214  for (Int_t j = 0; j < no_rows; j++)
215  *cp++ = 1.0/(i+j+1.0);
216 }
217 
218 ////////////////////////////////////////////////////////////////////////////////
219 
220 template<class Element>
222 {
223  MakeHilbertMat(m);
224 }
225 
226 template class TMatrixTLazy <Float_t>;
227 template class TMatrixTSymLazy <Float_t>;
228 template class THaarMatrixT <Float_t>;
229 template class THilbertMatrixT <Float_t>;
230 template class THilbertMatrixTSym<Float_t>;
231 
232 template class TMatrixTLazy <Double_t>;
233 template class TMatrixTSymLazy <Double_t>;
234 template class THaarMatrixT <Double_t>;
235 template class THilbertMatrixT <Double_t>;
236 template class THilbertMatrixTSym<Double_t>;
TMatrixT< Element > & Transpose(const TMatrixT< Element > &source)
Transpose matrix source.
Definition: TMatrixT.cxx:1461
Small helper to encapsulate whether to return the value pointed to by the iterator or its address...
void MakeHilbertMat(TMatrixT< Element > &m)
Make a Hilbert matrix.
#define R__ASSERT(e)
Definition: TError.h:98
int Int_t
Definition: RtypesCore.h:41
void FillIn(TMatrixT< Element > &m) const
Bool_t IsValid() const
Definition: TMatrixTBase.h:157
void FillIn(TMatrixTSym< Element > &m) const
virtual void Error(const char *method, const char *msgfmt,...) const
Issue error message.
Definition: TObject.cxx:918
void Error(const char *location, const char *msgfmt,...)
virtual const Element * GetMatrixArray() const
Definition: TMatrixTSym.h:192
void MakeHaarMat(TMatrixT< Element > &m)
Create an orthonormal (2^n)*(no_cols) Haar (sub)matrix, whose columns are Haar functions.
Int_t GetNrows() const
Definition: TMatrixTBase.h:134
TMarker * m
Definition: textangle.C:8
virtual const Element * GetMatrixArray() const
Definition: TMatrixT.h:222
templateClassImp(TMatrixTLazy) templateClassImp(TMatrixTSymLazy) templateClassImp(THaarMatrixT) templateClassImp(THilbertMatrixT) templateClassImp(THilbertMatrixTSym) template< class Element > THaarMatrixT< Element >
Int_t GetNcols() const
Definition: TMatrixTBase.h:137
void FillIn(TMatrixT< Element > &m) const
Double_t Sqrt(Double_t x)
Definition: TMath.h:464