1 /*
  2  *   Copyright (c) 2010, Michael Lehn
  3  *
  4  *   All rights reserved.
  5  *
  6  *   Redistribution and use in source and binary forms, with or without
  7  *   modification, are permitted provided that the following conditions
  8  *   are met:
  9  *
 10  *   1) Redistributions of source code must retain the above copyright
 11  *      notice, this list of conditions and the following disclaimer.
 12  *   2) Redistributions in binary form must reproduce the above copyright
 13  *      notice, this list of conditions and the following disclaimer in
 14  *      the documentation and/or other materials provided with the
 15  *      distribution.
 16  *   3) Neither the name of the FLENS development group nor the names of
 17  *      its contributors may be used to endorse or promote products derived
 18  *      from this software without specific prior written permission.
 19  *
 20  *   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 21  *   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 22  *   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
 23  *   A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
 24  *   OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 25  *   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 26  *   LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 27  *   DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 28  *   THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 29  *   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 30  *   OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 31  */
 32 
 33 #ifndef FLENS_BLAS_CLOSURES_PRUNE_H
 34 #define FLENS_BLAS_CLOSURES_PRUNE_H 1
 35 
 36 #include <cxxblas/typedefs.h>
 37 #include <flens/aux/promotion.h>
 38 #include <flens/blas/operators/operators.h>
 39 #include <flens/matrixtypes/impl/matrixclosure.h>
 40 #include <flens/typedefs.h>
 41 
 42 namespace flens {
 43 
 44 //== PruneScaling ==============================================================
 45 
 46 //-- General definition
 47 
 48 template <typename MatVec>
 49 struct PruneScaling
 50 {
 51     typedef MatVec                                  Remainder;
 52     typedef typename Remainder::Impl::ElementType   ScalingType;
 53 
 54     static const ScalingType
 55     getFactor(const MatVec &)
 56     {
 57         return ScalingType(1);
 58     }
 59 
 60     static const Remainder &
 61     getRemainder(const MatVec &x)
 62     {
 63         return x;
 64     }
 65 };
 66 
 67 //-- Specialization for scaled matrix or vector
 68 
 69 template <typename T, typename M>
 70 struct PruneScaling<MatrixClosure<OpMult, ScalarValue<T>, M> >
 71 {
 72     typedef MatrixClosure<OpMult, ScalarValue<T>, M>  MC;
 73     typedef M                                         Remainder;
 74     typedef T                                         ScalingType;
 75 
 76     static const ScalingType
 77     getFactor(const MC &x)
 78     {
 79         return x.left().value();
 80     }
 81 
 82     static const Remainder &
 83     getRemainder(const MC &x)
 84     {
 85         return x.right();
 86     }
 87 };
 88 
 89 template <typename T, typename V>
 90 struct PruneScaling<VectorClosure<OpMult, ScalarValue<T>, V> >
 91 {
 92     typedef VectorClosure<OpMult, ScalarValue<T>, V>  VC;
 93     typedef V                                         Remainder;
 94     typedef T                                         ScalingType;
 95 
 96     static const ScalingType
 97     getFactor(const VC &x)
 98     {
 99         return x.left().value();
100     }
101 
102     static const Remainder &
103     getRemainder(const VC &x)
104     {
105         return x.right();
106     }
107 };
108 
109 //== PruneConjTrans ============================================================
110 
111 //-- General definition
112 
113 template <typename Matrix>
114 struct PruneConjTrans
115 {
116     typedef Matrix Remainder;
117 
118     static const Transpose trans = NoTrans;
119 
120     static const Remainder &
121     remainder(const Matrix &matrix)
122     {
123         return matrix;
124     }
125 
126 };
127 
128 //-- Specialization for OpTrans, OpConj closures
129 
130 template <typename M>
131 struct PruneConjTrans<MatrixClosure<OpTrans, M, M> >
132 {
133     typedef MatrixClosure<OpTrans, M, M>           MC;
134     typedef typename PruneConjTrans<M>::Remainder  Remainder;
135 
136     static const Transpose trans = Transpose(Trans^PruneConjTrans<M>::trans);
137 
138     static const Remainder &
139     remainder(const MC &A)
140     {
141         return PruneConjTrans<M>::remainder(A.left());
142     }
143 
144 };
145 
146 template <typename M>
147 struct PruneConjTrans<MatrixClosure<OpConj, M, M> >
148 {
149     typedef MatrixClosure<OpConj, M, M>            MC;
150     typedef typename PruneConjTrans<M>::Remainder  Remainder;
151 
152     static const Transpose trans = Transpose(Conj^PruneConjTrans<M>::trans);
153 
154     static const Remainder &
155     remainder(const MC &A)
156     {
157         return PruneConjTrans<M>::remainder(A.left());
158     }
159 };
160 
161 // namespace flens
162 
163 #endif // FLENS_BLAS_CLOSURES_PRUNE_H