Basic Image AlgorithmS Library  2.8.0
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
triang.h
1 /*
2 This file is distributed as part of the BIAS library (Basic ImageAlgorithmS)
3 but it has not been developed by the authors of BIAS.
4 
5 For copyright, author and license information see below.
6 */
7 
8 
9 /*
10 *
11 * Template Numerical Toolkit (TNT): Linear Algebra Module
12 *
13 * Mathematical and Computational Sciences Division
14 * National Institute of Technology,
15 * Gaithersburg, MD USA
16 *
17 *
18 * This software was developed at the National Institute of Standards and
19 * Technology (NIST) by employees of the Federal Government in the course
20 * of their official duties. Pursuant to title 17 Section 105 of the
21 * United States Code, this software is not subject to copyright protection
22 * and is in the public domain. The Template Numerical Toolkit (TNT) is
23 * an experimental system. NIST assumes no responsibility whatsoever for
24 * its use by other parties, and makes no guarantees, expressed or implied,
25 * about its quality, reliability, or any other characteristic.
26 *
27 * BETA VERSION INCOMPLETE AND SUBJECT TO CHANGE
28 * see http://math.nist.gov/tnt for latest updates.
29 *
30 */
31 
32 
33 
34 // Triangular Matrices (Views and Adpators)
35 
36 #ifndef TRIANG_H
37 #define TRIANG_H
38 
39 // default to use lower-triangular portions of arrays
40 // for symmetric matrices.
41 
42 namespace TNT
43 {
44 
45  template <class MaTRiX>
47  {
48  protected:
49 
50 
51  const MaTRiX &A_;
52  const typename MaTRiX::element_type zero_;
53 
54  public:
55 
56 
57  typedef typename MaTRiX::const_reference const_reference;
58  typedef const typename MaTRiX::element_type element_type;
59  typedef const typename MaTRiX::element_type value_type;
60  typedef element_type T;
61 
62  Subscript dim(Subscript d) const { return A_.dim(d); }
63  Subscript lbound() const { return A_.lbound(); }
64  Subscript num_rows() const { return A_.num_rows(); }
65  Subscript num_cols() const { return A_.num_cols(); }
66 
67 
68  // constructors
69 
70  LowerTriangularView(/*const*/ MaTRiX &A) : A_(A), zero_(0) {}
71 
72 
73  inline const_reference get(Subscript i, Subscript j) const
74  {
75 #ifdef TNT_BOUNDS_CHECK
76  BIASASSERT(lbound()<=i);
77  BIASASSERT(i<=A_.num_rows() + lbound() - 1);
78  BIASASSERT(lbound()<=j);
79  BIASASSERT(j<=A_.num_cols() + lbound() - 1);
80 #endif
81  if (i<j)
82  return zero_;
83  else
84  return A_(i,j);
85  }
86 
87 
89  {
90 #ifdef TNT_BOUNDS_CHECK
91  BIASASSERT(lbound()<=i);
92  BIASASSERT(i<=A_.num_rows() + lbound() - 1);
93  BIASASSERT(lbound()<=j);
94  BIASASSERT(j<=A_.num_cols() + lbound() - 1);
95 #endif
96  if (i<j)
97  return zero_;
98  else
99  return A_(i,j);
100  }
101 
102 #ifdef TNT_USE_REGIONS
103 
105  const_Region;
106 
107  const_Region operator()(/*const*/ Index1D &I,
108  /*const*/ Index1D &J) const
109  {
110  return const_Region(*this, I, J);
111  }
112 
113  const_Region operator()(Subscript i1, Subscript i2,
114  Subscript j1, Subscript j2) const
115  {
116  return const_Region(*this, i1, i2, j1, j2);
117  }
118 
119 
120 
121 #endif
122  // TNT_USE_REGIONS
123 
124  };
125 
126 
127  /* *********** Lower_triangular_view() algorithms ****************** */
128 
129  template <class MaTRiX, class VecToR>
130  VecToR matmult(/*const*/ LowerTriangularView<MaTRiX> &A, VecToR &x)
131  {
132  Subscript M = A.num_rows();
133  Subscript N = A.num_cols();
134 
135  BIASASSERT(N == x.dim());
136 
137  Subscript i, j;
138  typename MaTRiX::element_type sum=0.0;
139  VecToR result(M);
140 
141  Subscript start = A.lbound();
142  Subscript Mend = M + A.lbound() -1 ;
143 
144  for (i=start; i<=Mend; i++)
145  {
146  sum = 0.0;
147  for (j=start; j<=i; j++)
148  sum = sum + A(i,j)*x(j);
149  result(i) = sum;
150  }
151 
152  return result;
153  }
154 
155  template <class MaTRiX, class VecToR>
156  inline VecToR operator*(/*const*/ LowerTriangularView<MaTRiX> &A, VecToR &x)
157  {
158  return matmult(A,x);
159  }
160 
161  template <class MaTRiX>
163  {
164  protected:
165 
166  const MaTRiX &A_;
167  const typename MaTRiX::element_type zero;
168  const typename MaTRiX::element_type one;
169 
170  public:
171 
172  typedef typename MaTRiX::const_reference const_reference;
173  typedef typename MaTRiX::element_type element_type;
174  typedef typename MaTRiX::element_type value_type;
175  typedef element_type T;
176 
177  Subscript lbound() const { return 1; }
178  Subscript dim(Subscript d) const { return A_.dim(d); }
179  Subscript num_rows() const { return A_.num_rows(); }
180  Subscript num_cols() const { return A_.num_cols(); }
181 
182 
183  // constructors
184 
185  UnitLowerTriangularView(/*const*/ MaTRiX &A) : A_(A), zero(0), one(1) {}
186 
187 
188  inline const_reference get(Subscript i, Subscript j) const
189  {
190 #ifdef TNT_BOUNDS_CHECK
191  BIASASSERT(1<=i);
192  BIASASSERT(i<=A_.dim(1));
193  BIASASSERT(1<=j);
194  BIASASSERT(j<=A_.dim(2));
195  BIASASSERT(0<=i && i<A_.dim(0) && 0<=j && j<A_.dim(1));
196 #endif
197  if (i>j)
198  return A_(i,j);
199  else if (i==j)
200  return one;
201  else
202  return zero;
203  }
204 
205 
207  {
208 #ifdef TNT_BOUNDS_CHECK
209  BIASASSERT(1<=i);
210  BIASASSERT(i<=A_.dim(1));
211  BIASASSERT(1<=j);
212  BIASASSERT(j<=A_.dim(2));
213 #endif
214  if (i>j)
215  return A_(i,j);
216  else if (i==j)
217  return one;
218  else
219  return zero;
220  }
221 
222 
223 #ifdef TNT_USE_REGIONS
224  // These are the "index-aware" features
225 
227  const_Region;
228 
229  const_Region operator()(/*const*/ Index1D &I,
230  /*const*/ Index1D &J) const
231  {
232  return const_Region(*this, I, J);
233  }
234 
235  const_Region operator()(Subscript i1, Subscript i2,
236  Subscript j1, Subscript j2) const
237  {
238  return const_Region(*this, i1, i2, j1, j2);
239  }
240 #endif
241  // TNT_USE_REGIONS
242  };
243 
244  template <class MaTRiX>
246  /*const*/ MaTRiX &A)
247  {
248  return LowerTriangularView<MaTRiX>(A);
249  }
250 
251 
252  template <class MaTRiX>
254  /*const*/ MaTRiX &A)
255  {
257  }
258 
259  template <class MaTRiX, class VecToR>
260  VecToR matmult(/*const*/ UnitLowerTriangularView<MaTRiX> &A, VecToR &x)
261  {
262  Subscript M = A.num_rows();
263  Subscript N = A.num_cols();
264 
265  BIASASSERT(N == x.dim());
266 
267  Subscript i, j;
268  typename MaTRiX::element_type sum=0.0;
269  VecToR result(M);
270 
271  Subscript start = A.lbound();
272  Subscript Mend = M + A.lbound() -1 ;
273 
274  for (i=start; i<=Mend; i++)
275  {
276  sum = 0.0;
277  for (j=start; j<i; j++)
278  sum = sum + A(i,j)*x(j);
279  result(i) = sum + x(i);
280  }
281 
282  return result;
283  }
284 
285  template <class MaTRiX, class VecToR>
286  inline VecToR operator*(/*const*/ UnitLowerTriangularView<MaTRiX> &A, VecToR &x)
287  {
288  return matmult(A,x);
289  }
290 
291 
292  //********************** Algorithms *************************************
293 
294 
295 
296  template <class MaTRiX>
297  std::ostream& operator<<(std::ostream &s, const LowerTriangularView<MaTRiX>&A)
298  {
299  Subscript M=A.num_rows();
300  Subscript N=A.num_cols();
301 
302  s << M << " " << N << endl;
303 
304  for (Subscript i=1; i<=M; i++)
305  {
306  for (Subscript j=1; j<=N; j++)
307  {
308  s << A(i,j) << " ";
309  }
310  s << endl;
311  }
312 
313 
314  return s;
315  }
316 
317  template <class MaTRiX>
318  std::ostream& operator<<(std::ostream &s,
320  {
321  Subscript M=A.num_rows();
322  Subscript N=A.num_cols();
323 
324  s << M << " " << N << endl;
325 
326  for (Subscript i=1; i<=M; i++)
327  {
328  for (Subscript j=1; j<=N; j++)
329  {
330  s << A(i,j) << " ";
331  }
332  s << endl;
333  }
334 
335 
336  return s;
337  }
338 
339 
340 
341  // ******************* Upper Triangular Section **************************
342 
343  template <class MaTRiX>
345  {
346  protected:
347 
348 
349  /*const*/ MaTRiX &A_;
350  /*const*/ typename MaTRiX::element_type zero_;
351 
352  public:
353 
354 
355  typedef typename MaTRiX::const_reference const_reference;
356  typedef /*const*/ typename MaTRiX::element_type element_type;
357  typedef /*const*/ typename MaTRiX::element_type value_type;
358  typedef element_type T;
359 
360  Subscript dim(Subscript d) const { return A_.dim(d); }
361  Subscript lbound() const { return A_.lbound(); }
362  Subscript num_rows() const { return A_.num_rows(); }
363  Subscript num_cols() const { return A_.num_cols(); }
364 
365 
366  // constructors
367 
368  UpperTriangularView(/*const*/ MaTRiX &A) : A_(A), zero_(0) {}
369 
370 
371  inline const_reference get(Subscript i, Subscript j) const
372  {
373 #ifdef TNT_BOUNDS_CHECK
374  BIASASSERT(lbound()<=i);
375  BIASASSERT(i<=A_.num_rows() + lbound() - 1);
376  BIASASSERT(lbound()<=j);
377  BIASASSERT(j<=A_.num_cols() + lbound() - 1);
378 #endif
379  if (i>j)
380  return zero_;
381  else
382  return A_(i,j);
383  }
384 
385 
387  {
388 #ifdef TNT_BOUNDS_CHECK
389  BIASASSERT(lbound()<=i);
390  BIASASSERT(i<=A_.num_rows() + lbound() - 1);
391  BIASASSERT(lbound()<=j);
392  BIASASSERT(j<=A_.num_cols() + lbound() - 1);
393 #endif
394  if (i>j)
395  return zero_;
396  else
397  return A_(i,j);
398  }
399 
400 #ifdef TNT_USE_REGIONS
401 
403  const_Region;
404 
405  const_Region operator()(const Index1D &I,
406  const Index1D &J) const
407  {
408  return const_Region(*this, I, J);
409  }
410 
411  const_Region operator()(Subscript i1, Subscript i2,
412  Subscript j1, Subscript j2) const
413  {
414  return const_Region(*this, i1, i2, j1, j2);
415  }
416 
417 
418 
419 #endif
420  // TNT_USE_REGIONS
421 
422  };
423 
424 
425  /* *********** Upper_triangular_view() algorithms ****************** */
426 
427  template <class MaTRiX, class VecToR>
428  VecToR matmult(/*const*/ UpperTriangularView<MaTRiX> &A, VecToR &x)
429  {
430  Subscript M = A.num_rows();
431  Subscript N = A.num_cols();
432 
433  BIASASSERT(N == x.dim());
434 
435  Subscript i, j;
436  typename VecToR::element_type sum=0.0;
437  VecToR result(M);
438 
439  Subscript start = A.lbound();
440  Subscript Mend = M + A.lbound() -1 ;
441 
442  for (i=start; i<=Mend; i++)
443  {
444  sum = 0.0;
445  for (j=i; j<=N; j++)
446  sum = sum + A(i,j)*x(j);
447  result(i) = sum;
448  }
449 
450  return result;
451  }
452 
453  template <class MaTRiX, class VecToR>
454  inline VecToR operator*(/*const*/ UpperTriangularView<MaTRiX> &A, VecToR &x)
455  {
456  return matmult(A,x);
457  }
458 
459  template <class MaTRiX>
461  {
462  protected:
463 
464  const MaTRiX &A_;
465  const typename MaTRiX::element_type zero;
466  const typename MaTRiX::element_type one;
467 
468  public:
469 
470  typedef typename MaTRiX::const_reference const_reference;
471  typedef typename MaTRiX::element_type element_type;
472  typedef typename MaTRiX::element_type value_type;
473  typedef element_type T;
474 
475  Subscript lbound() const { return 1; }
476  Subscript dim(Subscript d) const { return A_.dim(d); }
477  Subscript num_rows() const { return A_.num_rows(); }
478  Subscript num_cols() const { return A_.num_cols(); }
479 
480 
481  // constructors
482 
483  UnitUpperTriangularView(/*const*/ MaTRiX &A) : A_(A), zero(0), one(1) {}
484 
485 
486  inline const_reference get(Subscript i, Subscript j) const
487  {
488 #ifdef TNT_BOUNDS_CHECK
489  BIASASSERT(1<=i);
490  BIASASSERT(i<=A_.dim(1));
491  BIASASSERT(1<=j);
492  BIASASSERT(j<=A_.dim(2));
493  BIASASSERT(0<=i && i<A_.dim(0) && 0<=j && j<A_.dim(1));
494 #endif
495  if (i<j)
496  return A_(i,j);
497  else if (i==j)
498  return one;
499  else
500  return zero;
501  }
502 
503 
505  {
506 #ifdef TNT_BOUNDS_CHECK
507  BIASASSERT(1<=i);
508  BIASASSERT(i<=A_.dim(1));
509  BIASASSERT(1<=j);
510  BIASASSERT(j<=A_.dim(2));
511 #endif
512  if (i<j)
513  return A_(i,j);
514  else if (i==j)
515  return one;
516  else
517  return zero;
518  }
519 
520 
521 #ifdef TNT_USE_REGIONS
522  // These are the "index-aware" features
523 
525  const_Region;
526 
527  const_Region operator()(const Index1D &I,
528  const Index1D &J) const
529  {
530  return const_Region(*this, I, J);
531  }
532 
533  const_Region operator()(Subscript i1, Subscript i2,
534  Subscript j1, Subscript j2) const
535  {
536  return const_Region(*this, i1, i2, j1, j2);
537  }
538 #endif
539  // TNT_USE_REGIONS
540  };
541 
542  template <class MaTRiX>
544  /*const*/ MaTRiX &A)
545  {
546  return UpperTriangularView<MaTRiX>(A);
547  }
548 
549 
550  template <class MaTRiX>
552  /*const*/ MaTRiX &A)
553  {
555  }
556 
557  template <class MaTRiX, class VecToR>
558  VecToR matmult(/*const*/ UnitUpperTriangularView<MaTRiX> &A, VecToR &x)
559  {
560  Subscript M = A.num_rows();
561  Subscript N = A.num_cols();
562 
563  BIASASSERT(N == x.dim());
564 
565  Subscript i, j;
566  typename VecToR::element_type sum=0.0;
567  VecToR result(M);
568 
569  Subscript start = A.lbound();
570  Subscript Mend = M + A.lbound() -1 ;
571 
572  for (i=start; i<=Mend; i++)
573  {
574  sum = x(i);
575  for (j=i+1; j<=N; j++)
576  sum = sum + A(i,j)*x(j);
577  result(i) = sum + x(i);
578  }
579 
580  return result;
581  }
582 
583  template <class MaTRiX, class VecToR>
584  inline VecToR operator*(/*const*/ UnitUpperTriangularView<MaTRiX> &A, VecToR &x)
585  {
586  return matmult(A,x);
587  }
588 
589 
590  //********************** Algorithms *************************************
591 
592 
593 
594  template <class MaTRiX>
595  std::ostream& operator<<(std::ostream &s,
596  /*const*/ UpperTriangularView<MaTRiX>&A)
597  {
598  Subscript M=A.num_rows();
599  Subscript N=A.num_cols();
600 
601  s << M << " " << N << endl;
602 
603  for (Subscript i=1; i<=M; i++)
604  {
605  for (Subscript j=1; j<=N; j++)
606  {
607  s << A(i,j) << " ";
608  }
609  s << endl;
610  }
611 
612 
613  return s;
614  }
615 
616  template <class MaTRiX>
617  std::ostream& operator<<(std::ostream &s,
619  {
620  Subscript M=A.num_rows();
621  Subscript N=A.num_cols();
622 
623  s << M << " " << N << endl;
624 
625  for (Subscript i=1; i<=M; i++)
626  {
627  for (Subscript j=1; j<=N; j++)
628  {
629  s << A(i,j) << " ";
630  }
631  s << endl;
632  }
633 
634 
635  return s;
636  }
637 
638 } // namespace TNT
639 
640 
641 
642 
643 
644 #endif
645 //TRIANG_H
646 
const_reference operator()(Subscript i, Subscript j) const
Definition: triang.h:88
Subscript dim(Subscript d) const
Definition: triang.h:476
Subscript dim(Subscript d) const
Definition: triang.h:62
MaTRiX::const_reference const_reference
Definition: triang.h:355
UnitLowerTriangularView< MaTRiX > Unit_lower_triangular_view(MaTRiX &A)
Definition: triang.h:253
MaTRiX::element_type element_type
Definition: triang.h:173
Subscript num_rows() const
Definition: triang.h:64
const MaTRiX::element_type value_type
Definition: triang.h:59
element_type T
Definition: triang.h:60
Subscript dim(Subscript d) const
Definition: triang.h:178
MaTRiX::const_reference const_reference
Definition: triang.h:172
MaTRiX::element_type value_type
Definition: triang.h:472
Subscript lbound() const
Definition: triang.h:475
MaTRiX::const_reference const_reference
Definition: triang.h:470
Subscript num_cols() const
Definition: triang.h:478
const MaTRiX::element_type one
Definition: triang.h:168
Subscript num_cols() const
Definition: triang.h:180
UnitUpperTriangularView< MaTRiX > Unit_upper_triangular_view(MaTRiX &A)
Definition: triang.h:551
TNT_SUBSCRIPT_TYPE Subscript
Definition: subscript.h:55
ostream & operator<<(ostream &s, const Fortran_Sparse_Col_Matrix< T > &A)
Definition: fcscmat.h:144
element_type T
Definition: triang.h:358
const MaTRiX::element_type one
Definition: triang.h:466
const MaTRiX & A_
Definition: triang.h:51
Matrix< T > matmult(const Matrix< T > &A, const Matrix< T > &B)
Definition: cmat.h:811
UnitLowerTriangularView(MaTRiX &A)
Definition: triang.h:185
MaTRiX::element_type element_type
Definition: triang.h:356
Fortran_Matrix< T > operator*(const Fortran_Matrix< T > &A, const Fortran_Matrix< T > &B)
Definition: fmat.h:484
UpperTriangularView< MaTRiX > Upper_triangular_view(MaTRiX &A)
Definition: triang.h:543
const_reference operator()(Subscript i, Subscript j) const
Definition: triang.h:206
Subscript num_rows() const
Definition: triang.h:179
Subscript lbound() const
Definition: triang.h:177
MaTRiX::element_type element_type
Definition: triang.h:471
const MaTRiX::element_type zero
Definition: triang.h:167
Subscript lbound() const
Definition: triang.h:361
LowerTriangularView(MaTRiX &A)
Definition: triang.h:70
MaTRiX::element_type value_type
Definition: triang.h:357
MaTRiX::element_type value_type
Definition: triang.h:174
const MaTRiX::element_type zero
Definition: triang.h:465
Subscript lbound() const
Definition: triang.h:63
Subscript dim(Subscript d) const
Definition: triang.h:360
UpperTriangularView(MaTRiX &A)
Definition: triang.h:368
Subscript num_cols() const
Definition: triang.h:65
Subscript num_rows() const
Definition: triang.h:477
Subscript num_rows() const
Definition: triang.h:362
const MaTRiX::element_type element_type
Definition: triang.h:58
const_reference operator()(Subscript i, Subscript j) const
Definition: triang.h:386
Subscript num_cols() const
Definition: triang.h:363
MaTRiX::element_type zero_
Definition: triang.h:350
UnitUpperTriangularView(MaTRiX &A)
Definition: triang.h:483
const_reference operator()(Subscript i, Subscript j) const
Definition: triang.h:504
MaTRiX::const_reference const_reference
Definition: triang.h:57
const MaTRiX::element_type zero_
Definition: triang.h:52
LowerTriangularView< MaTRiX > Lower_triangular_view(MaTRiX &A)
Definition: triang.h:245