yat/utility/expression/MatrixBinary.h

Code
Comments
Other
Rev Date Author Line
3908 13 May 20 peter 1 #ifndef _theplu_yat_utility_expression_matrix_binary
3908 13 May 20 peter 2 #define _theplu_yat_utility_expression_matrix_binary
3908 13 May 20 peter 3
3908 13 May 20 peter 4 // $Id$
3908 13 May 20 peter 5
3908 13 May 20 peter 6 /*
3908 13 May 20 peter 7   Copyright (C) 2020 Peter Johansson
3908 13 May 20 peter 8
3908 13 May 20 peter 9   This file is part of the yat library, http://dev.thep.lu.se/yat
3908 13 May 20 peter 10
3908 13 May 20 peter 11   The yat library is free software; you can redistribute it and/or
3908 13 May 20 peter 12   modify it under the terms of the GNU General Public License as
3908 13 May 20 peter 13   published by the Free Software Foundation; either version 3 of the
3908 13 May 20 peter 14   License, or (at your option) any later version.
3908 13 May 20 peter 15
3908 13 May 20 peter 16   The yat library is distributed in the hope that it will be useful,
3908 13 May 20 peter 17   but WITHOUT ANY WARRANTY; without even the implied warranty of
3908 13 May 20 peter 18   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
3908 13 May 20 peter 19   General Public License for more details.
3908 13 May 20 peter 20
3908 13 May 20 peter 21   You should have received a copy of the GNU General Public License
3908 13 May 20 peter 22   along with yat. If not, see <http://www.gnu.org/licenses/>.
3908 13 May 20 peter 23 */
3908 13 May 20 peter 24
3908 13 May 20 peter 25 #include "yat/utility/BasicMatrix.h"
3908 13 May 20 peter 26 #include "yat/utility/BLAS_utility.h"
3908 13 May 20 peter 27 #include "yat/utility/MatrixExpression.h"
3908 13 May 20 peter 28 #include "yat/utility/yat_assert.h"
3908 13 May 20 peter 29
3908 13 May 20 peter 30 #include <gsl/gsl_matrix.h>
3908 13 May 20 peter 31
3908 13 May 20 peter 32 namespace theplu {
3908 13 May 20 peter 33 namespace yat {
3908 13 May 20 peter 34 namespace utility {
3908 13 May 20 peter 35
3908 13 May 20 peter 36   /// \cond IGNORE_DOXYGEN
3908 13 May 20 peter 37
3908 13 May 20 peter 38 namespace expression {
3908 13 May 20 peter 39
3908 13 May 20 peter 40
3908 13 May 20 peter 41
3908 13 May 20 peter 42   template<typename LHS, typename RHS, class OP>
3908 13 May 20 peter 43   class MatrixBinary
3908 13 May 20 peter 44     : public MatrixExpression<MatrixBinary<LHS, RHS, OP> >
3908 13 May 20 peter 45   {
3908 13 May 20 peter 46   public:
3908 13 May 20 peter 47     MatrixBinary(const BasicMatrix<LHS>& lhs, const BasicMatrix<RHS>& rhs)
3908 13 May 20 peter 48       : lhs_(lhs), rhs_(rhs)
3908 13 May 20 peter 49     {
3908 13 May 20 peter 50     }
3908 13 May 20 peter 51
3908 13 May 20 peter 52     size_t rows(void) const { return lhs_.rows(); }
3908 13 May 20 peter 53     size_t columns(void) const { return rhs_.columns(); }
3908 13 May 20 peter 54
3908 13 May 20 peter 55     double operator()(size_t row, size_t column) const
3908 13 May 20 peter 56     { return get(row, column, op_); }
3908 13 May 20 peter 57
3908 13 May 20 peter 58     void calculate_matrix(gsl_matrix*& result) const
3908 13 May 20 peter 59     {
3908 13 May 20 peter 60       detail::reallocate(result, this->rows(), this->columns());
3908 13 May 20 peter 61       calculate_matrix(result, op_);
3908 13 May 20 peter 62     }
3908 13 May 20 peter 63
3908 13 May 20 peter 64   private:
3908 13 May 20 peter 65     const BasicMatrix<LHS>& lhs_;
3908 13 May 20 peter 66     const BasicMatrix<RHS>& rhs_;
3908 13 May 20 peter 67     OP op_;
3908 13 May 20 peter 68
3908 13 May 20 peter 69     template<class T>
3908 13 May 20 peter 70     void calculate_matrix(gsl_matrix*& result, T) const
3908 13 May 20 peter 71     {
3908 13 May 20 peter 72       YAT_ASSERT(detail::rows(result) == this->rows());
3908 13 May 20 peter 73       YAT_ASSERT(detail::columns(result) == this->columns());
3908 13 May 20 peter 74       for (size_t i=0; i<rows(); ++i)
3908 13 May 20 peter 75         for (size_t j=0; j<columns(); ++j)
3908 13 May 20 peter 76           gsl_matrix_set(result, i, j, (*this)(i, j));
3908 13 May 20 peter 77     }
3908 13 May 20 peter 78
3908 13 May 20 peter 79
3908 13 May 20 peter 80     double get(size_t row, size_t column, Plus) const
3908 13 May 20 peter 81     {
3908 13 May 20 peter 82       return lhs_(row, column) + rhs_(row, column);
3908 13 May 20 peter 83     }
3908 13 May 20 peter 84
3908 13 May 20 peter 85
3908 13 May 20 peter 86     double get(size_t row, size_t column, Minus) const
3908 13 May 20 peter 87     {
3908 13 May 20 peter 88       return lhs_(row, column) - rhs_(row, column);
3908 13 May 20 peter 89     }
3908 13 May 20 peter 90   };
3908 13 May 20 peter 91
3908 13 May 20 peter 92
3908 13 May 20 peter 93 } // end namespace expression
3908 13 May 20 peter 94
3908 13 May 20 peter 95   /// \endcond
3908 13 May 20 peter 96
3908 13 May 20 peter 97 }}} // of namespace utility, yat, and theplu
3908 13 May 20 peter 98
3908 13 May 20 peter 99 #endif