MyraMath
MrhsReshaper.h
Go to the documentation of this file.
1 // ========================================================================= //
2 // This file is part of MyraMath, copyright (c) 2014-2019 by Ryan A Chilton //
3 // and distributed by MyraCore, LLC. See LICENSE.txt for license terms. //
4 // ========================================================================= //
5 
6 #ifndef MYRAMATH_ITERATIVE_MRHSRESHAPER_H
7 #define MYRAMATH_ITERATIVE_MRHSRESHAPER_H
8 
16 #include <myramath/dense/Matrix.h>
17 
18 // The multiple-rhs (Matrix B/X) iterative solvers just delegate to the single-rhs (Vector b/x) ones,
19 // by reshaping/concatenating the columns of the Matrix's B/X into (taller) Vector's b/x. In the lucky
20 // event that B and X are already column-contiguious (LD = I), all this reshaping can be done in-place.
21 // Otherwise, must make deep copies of B and X that are contiguous, delegate, then copy the solution back.
22 //
23 // These two classes encapsulate those steps/decisions for B->b and X->x, to reduce code duplication/errors.
24 
25 namespace myra {
26 namespace detail {
27 
28 // Captures a Matrix-valued forcing data (B), presents it as a Vector (b), either by reshaping or deep copying.
29 template<class Number> class MrhsReshaperB
30  {
31  public:
32 
33  // Constructor, requires forcing data B.
35  : B(in_B)
36  {
37  // Is B column-contiguous? No copy is needed..
38  if (B.I == B.LD)
39  b = CVectorRange<Number>(B.begin,B.I*B.J);
40  // .. otherwise, need to make a copy
41  else
42  {
43  B_copy = B;
44  b = CVectorRange<Number>(B_copy.range().begin,B.I*B.J);
45  }
46  }
47 
48  // Presents the columns of B as a (taller) Vector b.
49  CVectorRange<Number> range() const
50  { return b; }
51 
52  private:
53 
54  // Original forcing data.
56 
57  // A column-contiguous deep copy of B, if necessary.
58  Matrix<Number> B_copy;
59 
60  // A Vector view of B, aliases either B or B_copy.
62 
63  };
64 
65 // Captures a Matrix-valued solution data (X), presents it as a Vector (b), either by reshaping or deep copying.
66 template<class Number> class MrhsReshaperX
67  {
68  public:
69 
70  // Constructor, requires solution data X.
72  : X(in_X)
73  {
74  // Is X column-contiguous? No copy is needed..
75  if (X.I == X.LD)
76  x = VectorRange<Number>(X.begin,X.I*X.J);
77  // .. otherwise, need to make a copy
78  else
79  {
80  X_copy = X;
81  x = VectorRange<Number>(X_copy.range().begin,X.I*X.J);
82  }
83  }
84 
85  // Presents the columns of X as a (taller) Vector x.
86  VectorRange<Number> range() const
87  { return x; }
88 
89  // After linear solution is done, we might need to copy from X_copy back into the original X.
90  ~MrhsReshaperX()
91  {
92  if (X.I != X.LD)
93  X.assign(X_copy);
94  }
95 
96  private:
97 
98  // Original solution data.
100 
101  // A column-contiguous deep copy of X, if necessary.
102  Matrix<Number> X_copy;
103 
104  // A Vector view of X, aliases either X or X_copy.
106 
107  };
108 
109 } // namespace detail
110 } // namespace
111 
112 #endif
Definition: MrhsReshaper.h:29
Interface class for representing subranges of dense Matrix&#39;s.
Interface class for representing subranges of dense Vector&#39;s.
Tabulates an IxJ matrix. Allows random access, has column major layout to be compatible with BLAS/LAP...
Definition: bdsqr.h:20
Represents a mutable VectorRange.
Definition: axpy.h:21
Definition: syntax.dox:1
Represents a const MatrixRange.
Definition: bothcat.h:22
Definition: MrhsReshaper.h:66
Definition: random.cpp:45
Represents a mutable MatrixRange.
Definition: conjugate.h:26
General purpose dense matrix container, O(i*j) storage.
Represents a const VectorRange.
Definition: axpy.h:20