max@0: // Copyright (C) 2010-2011 NICTA (www.nicta.com.au) max@0: // Copyright (C) 2010-2011 Conrad Sanderson max@0: // max@0: // This file is part of the Armadillo C++ library. max@0: // It is provided without any warranty of fitness max@0: // for any purpose. You can redistribute this file max@0: // and/or modify it under the terms of the GNU max@0: // Lesser General Public License (LGPL) as published max@0: // by the Free Software Foundation, either version 3 max@0: // of the License or (at your option) any later version. max@0: // (see http://www.opensource.org/licenses for more info) max@0: max@0: max@0: //! \addtogroup eglue_core max@0: //! @{ max@0: max@0: max@0: max@0: class eglue_plus : public eglue_core max@0: { max@0: public: max@0: max@0: inline static const char* text() { return "addition"; } max@0: }; max@0: max@0: max@0: max@0: class eglue_minus : public eglue_core max@0: { max@0: public: max@0: max@0: inline static const char* text() { return "subtraction"; } max@0: }; max@0: max@0: max@0: max@0: class eglue_div : public eglue_core max@0: { max@0: public: max@0: max@0: inline static const char* text() { return "element-wise division"; } max@0: }; max@0: max@0: max@0: max@0: class eglue_schur : public eglue_core max@0: { max@0: public: max@0: max@0: inline static const char* text() { return "element-wise multiplication"; } max@0: }; max@0: max@0: max@0: max@0: #undef arma_applier_1 max@0: #undef arma_applier_2 max@0: #undef arma_applier_3 max@0: #undef operatorA max@0: #undef operatorB max@0: max@0: #define arma_applier_1(operatorA, operatorB) \ max@0: {\ max@0: uword i,j;\ max@0: \ max@0: for(i=0, j=1; j max@0: template max@0: arma_hot max@0: inline max@0: void max@0: eglue_core::apply(Mat& out, const eGlue& x) max@0: { max@0: arma_extra_debug_sigprint(); max@0: max@0: typedef typename T1::elem_type eT; max@0: max@0: const bool prefer_at_accessor = (Proxy::prefer_at_accessor || Proxy::prefer_at_accessor); max@0: max@0: // NOTE: we're assuming that the matrix has already been set to the correct size and there is no aliasing; max@0: // size setting and alias checking is done by either the Mat contructor or operator=() max@0: max@0: eT* out_mem = out.memptr(); max@0: max@0: if(prefer_at_accessor == false) max@0: { max@0: const uword n_elem = out.n_elem; max@0: max@0: typename Proxy::ea_type P1 = x.P1.get_ea(); max@0: typename Proxy::ea_type P2 = x.P2.get_ea(); max@0: max@0: if(is_same_type::value == true) { arma_applier_1(=, +); } max@0: else if(is_same_type::value == true) { arma_applier_1(=, -); } max@0: else if(is_same_type::value == true) { arma_applier_1(=, /); } max@0: else if(is_same_type::value == true) { arma_applier_1(=, *); } max@0: } max@0: else max@0: { max@0: const uword n_rows = out.n_rows; max@0: const uword n_cols = out.n_cols; max@0: max@0: const Proxy& P1 = x.P1; max@0: const Proxy& P2 = x.P2; max@0: max@0: if(is_same_type::value == true) { arma_applier_2(=, +); } max@0: else if(is_same_type::value == true) { arma_applier_2(=, -); } max@0: else if(is_same_type::value == true) { arma_applier_2(=, /); } max@0: else if(is_same_type::value == true) { arma_applier_2(=, *); } max@0: } max@0: } max@0: max@0: max@0: max@0: template max@0: template max@0: arma_hot max@0: inline max@0: void max@0: eglue_core::apply_inplace_plus(Mat& out, const eGlue& x) max@0: { max@0: arma_extra_debug_sigprint(); max@0: max@0: arma_debug_assert_same_size(out, x.P1, "addition"); max@0: max@0: typedef typename T1::elem_type eT; max@0: max@0: eT* out_mem = out.memptr(); max@0: max@0: const bool prefer_at_accessor = (Proxy::prefer_at_accessor || Proxy::prefer_at_accessor); max@0: max@0: if(prefer_at_accessor == false) max@0: { max@0: const uword n_elem = out.n_elem; max@0: max@0: typename Proxy::ea_type P1 = x.P1.get_ea(); max@0: typename Proxy::ea_type P2 = x.P2.get_ea(); max@0: max@0: if(is_same_type::value == true) { arma_applier_1(+=, +); } max@0: else if(is_same_type::value == true) { arma_applier_1(+=, -); } max@0: else if(is_same_type::value == true) { arma_applier_1(+=, /); } max@0: else if(is_same_type::value == true) { arma_applier_1(+=, *); } max@0: } max@0: else max@0: { max@0: const uword n_rows = out.n_rows; max@0: const uword n_cols = out.n_cols; max@0: max@0: const Proxy& P1 = x.P1; max@0: const Proxy& P2 = x.P2; max@0: max@0: if(is_same_type::value == true) { arma_applier_2(+=, +); } max@0: else if(is_same_type::value == true) { arma_applier_2(+=, -); } max@0: else if(is_same_type::value == true) { arma_applier_2(+=, /); } max@0: else if(is_same_type::value == true) { arma_applier_2(+=, *); } max@0: } max@0: } max@0: max@0: max@0: max@0: template max@0: template max@0: arma_hot max@0: inline max@0: void max@0: eglue_core::apply_inplace_minus(Mat& out, const eGlue& x) max@0: { max@0: arma_extra_debug_sigprint(); max@0: max@0: arma_debug_assert_same_size(out, x.P1, "subtraction"); max@0: max@0: typedef typename T1::elem_type eT; max@0: max@0: eT* out_mem = out.memptr(); max@0: max@0: const bool prefer_at_accessor = (Proxy::prefer_at_accessor || Proxy::prefer_at_accessor); max@0: max@0: if(prefer_at_accessor == false) max@0: { max@0: const uword n_elem = out.n_elem; max@0: max@0: typename Proxy::ea_type P1 = x.P1.get_ea(); max@0: typename Proxy::ea_type P2 = x.P2.get_ea(); max@0: max@0: if(is_same_type::value == true) { arma_applier_1(-=, +); } max@0: else if(is_same_type::value == true) { arma_applier_1(-=, -); } max@0: else if(is_same_type::value == true) { arma_applier_1(-=, /); } max@0: else if(is_same_type::value == true) { arma_applier_1(-=, *); } max@0: } max@0: else max@0: { max@0: const uword n_rows = out.n_rows; max@0: const uword n_cols = out.n_cols; max@0: max@0: const Proxy& P1 = x.P1; max@0: const Proxy& P2 = x.P2; max@0: max@0: if(is_same_type::value == true) { arma_applier_2(-=, +); } max@0: else if(is_same_type::value == true) { arma_applier_2(-=, -); } max@0: else if(is_same_type::value == true) { arma_applier_2(-=, /); } max@0: else if(is_same_type::value == true) { arma_applier_2(-=, *); } max@0: } max@0: } max@0: max@0: max@0: max@0: template max@0: template max@0: arma_hot max@0: inline max@0: void max@0: eglue_core::apply_inplace_schur(Mat& out, const eGlue& x) max@0: { max@0: arma_extra_debug_sigprint(); max@0: max@0: arma_debug_assert_same_size(out, x.P1, "element-wise multiplication"); max@0: max@0: typedef typename T1::elem_type eT; max@0: max@0: eT* out_mem = out.memptr(); max@0: max@0: const bool prefer_at_accessor = (Proxy::prefer_at_accessor || Proxy::prefer_at_accessor); max@0: max@0: if(prefer_at_accessor == false) max@0: { max@0: const uword n_elem = out.n_elem; max@0: max@0: typename Proxy::ea_type P1 = x.P1.get_ea(); max@0: typename Proxy::ea_type P2 = x.P2.get_ea(); max@0: max@0: if(is_same_type::value == true) { arma_applier_1(*=, +); } max@0: else if(is_same_type::value == true) { arma_applier_1(*=, -); } max@0: else if(is_same_type::value == true) { arma_applier_1(*=, /); } max@0: else if(is_same_type::value == true) { arma_applier_1(*=, *); } max@0: } max@0: else max@0: { max@0: const uword n_rows = out.n_rows; max@0: const uword n_cols = out.n_cols; max@0: max@0: const Proxy& P1 = x.P1; max@0: const Proxy& P2 = x.P2; max@0: max@0: if(is_same_type::value == true) { arma_applier_2(*=, +); } max@0: else if(is_same_type::value == true) { arma_applier_2(*=, -); } max@0: else if(is_same_type::value == true) { arma_applier_2(*=, /); } max@0: else if(is_same_type::value == true) { arma_applier_2(*=, *); } max@0: } max@0: } max@0: max@0: max@0: max@0: template max@0: template max@0: arma_hot max@0: inline max@0: void max@0: eglue_core::apply_inplace_div(Mat& out, const eGlue& x) max@0: { max@0: arma_extra_debug_sigprint(); max@0: max@0: arma_debug_assert_same_size(out, x.P1, "element-wise division"); max@0: max@0: typedef typename T1::elem_type eT; max@0: max@0: eT* out_mem = out.memptr(); max@0: max@0: const bool prefer_at_accessor = (Proxy::prefer_at_accessor || Proxy::prefer_at_accessor); max@0: max@0: if(prefer_at_accessor == false) max@0: { max@0: const uword n_elem = out.n_elem; max@0: max@0: typename Proxy::ea_type P1 = x.P1.get_ea(); max@0: typename Proxy::ea_type P2 = x.P2.get_ea(); max@0: max@0: if(is_same_type::value == true) { arma_applier_1(/=, +); } max@0: else if(is_same_type::value == true) { arma_applier_1(/=, -); } max@0: else if(is_same_type::value == true) { arma_applier_1(/=, /); } max@0: else if(is_same_type::value == true) { arma_applier_1(/=, *); } max@0: } max@0: else max@0: { max@0: const uword n_rows = out.n_rows; max@0: const uword n_cols = out.n_cols; max@0: max@0: const Proxy& P1 = x.P1; max@0: const Proxy& P2 = x.P2; max@0: max@0: if(is_same_type::value == true) { arma_applier_2(*=, +); } max@0: else if(is_same_type::value == true) { arma_applier_2(*=, -); } max@0: else if(is_same_type::value == true) { arma_applier_2(*=, /); } max@0: else if(is_same_type::value == true) { arma_applier_2(*=, *); } max@0: } max@0: } max@0: max@0: max@0: max@0: // max@0: // cubes max@0: max@0: max@0: max@0: template max@0: template max@0: arma_hot max@0: inline max@0: void max@0: eglue_core::apply(Cube& out, const eGlueCube& x) max@0: { max@0: arma_extra_debug_sigprint(); max@0: max@0: typedef typename T1::elem_type eT; max@0: max@0: const bool prefer_at_accessor = (ProxyCube::prefer_at_accessor || ProxyCube::prefer_at_accessor); max@0: max@0: // NOTE: we're assuming that the cube has already been set to the correct size and there is no aliasing; max@0: // size setting and alias checking is done by either the Cube contructor or operator=() max@0: max@0: max@0: eT* out_mem = out.memptr(); max@0: max@0: if(prefer_at_accessor == false) max@0: { max@0: const uword n_elem = out.n_elem; max@0: max@0: typename ProxyCube::ea_type P1 = x.P1.get_ea(); max@0: typename ProxyCube::ea_type P2 = x.P2.get_ea(); max@0: max@0: if(is_same_type::value == true) { arma_applier_1(=, +); } max@0: else if(is_same_type::value == true) { arma_applier_1(=, -); } max@0: else if(is_same_type::value == true) { arma_applier_1(=, /); } max@0: else if(is_same_type::value == true) { arma_applier_1(=, *); } max@0: } max@0: else max@0: { max@0: const uword n_rows = x.get_n_rows(); max@0: const uword n_cols = x.get_n_cols(); max@0: const uword n_slices = x.get_n_slices(); max@0: max@0: const ProxyCube& P1 = x.P1; max@0: const ProxyCube& P2 = x.P2; max@0: max@0: if(is_same_type::value == true) { arma_applier_3(=, +); } max@0: else if(is_same_type::value == true) { arma_applier_3(=, -); } max@0: else if(is_same_type::value == true) { arma_applier_3(=, /); } max@0: else if(is_same_type::value == true) { arma_applier_3(=, *); } max@0: } max@0: } max@0: max@0: max@0: max@0: template max@0: template max@0: arma_hot max@0: inline max@0: void max@0: eglue_core::apply_inplace_plus(Cube& out, const eGlueCube& x) max@0: { max@0: arma_extra_debug_sigprint(); max@0: max@0: const uword n_rows = x.get_n_rows(); max@0: const uword n_cols = x.get_n_cols(); max@0: const uword n_slices = x.get_n_slices(); max@0: max@0: arma_debug_assert_same_size(out.n_rows, out.n_cols, out.n_slices, n_rows, n_cols, n_slices, "addition"); max@0: max@0: typedef typename T1::elem_type eT; max@0: max@0: eT* out_mem = out.memptr(); max@0: max@0: const bool prefer_at_accessor = (ProxyCube::prefer_at_accessor || ProxyCube::prefer_at_accessor); max@0: max@0: if(prefer_at_accessor == false) max@0: { max@0: const uword n_elem = out.n_elem; max@0: max@0: typename ProxyCube::ea_type P1 = x.P1.get_ea(); max@0: typename ProxyCube::ea_type P2 = x.P2.get_ea(); max@0: max@0: if(is_same_type::value == true) { arma_applier_1(+=, +); } max@0: else if(is_same_type::value == true) { arma_applier_1(+=, -); } max@0: else if(is_same_type::value == true) { arma_applier_1(+=, /); } max@0: else if(is_same_type::value == true) { arma_applier_1(+=, *); } max@0: } max@0: else max@0: { max@0: const ProxyCube& P1 = x.P1; max@0: const ProxyCube& P2 = x.P2; max@0: max@0: if(is_same_type::value == true) { arma_applier_3(+=, +); } max@0: else if(is_same_type::value == true) { arma_applier_3(+=, -); } max@0: else if(is_same_type::value == true) { arma_applier_3(+=, /); } max@0: else if(is_same_type::value == true) { arma_applier_3(+=, *); } max@0: } max@0: } max@0: max@0: max@0: max@0: template max@0: template max@0: arma_hot max@0: inline max@0: void max@0: eglue_core::apply_inplace_minus(Cube& out, const eGlueCube& x) max@0: { max@0: arma_extra_debug_sigprint(); max@0: max@0: const uword n_rows = x.get_n_rows(); max@0: const uword n_cols = x.get_n_cols(); max@0: const uword n_slices = x.get_n_slices(); max@0: max@0: arma_debug_assert_same_size(out.n_rows, out.n_cols, out.n_slices, n_rows, n_cols, n_slices, "subtraction"); max@0: max@0: typedef typename T1::elem_type eT; max@0: max@0: eT* out_mem = out.memptr(); max@0: max@0: const bool prefer_at_accessor = (ProxyCube::prefer_at_accessor || ProxyCube::prefer_at_accessor); max@0: max@0: if(prefer_at_accessor == false) max@0: { max@0: const uword n_elem = out.n_elem; max@0: max@0: typename ProxyCube::ea_type P1 = x.P1.get_ea(); max@0: typename ProxyCube::ea_type P2 = x.P2.get_ea(); max@0: max@0: if(is_same_type::value == true) { arma_applier_1(-=, +); } max@0: else if(is_same_type::value == true) { arma_applier_1(-=, -); } max@0: else if(is_same_type::value == true) { arma_applier_1(-=, /); } max@0: else if(is_same_type::value == true) { arma_applier_1(-=, *); } max@0: } max@0: else max@0: { max@0: const ProxyCube& P1 = x.P1; max@0: const ProxyCube& P2 = x.P2; max@0: max@0: if(is_same_type::value == true) { arma_applier_3(-=, +); } max@0: else if(is_same_type::value == true) { arma_applier_3(-=, -); } max@0: else if(is_same_type::value == true) { arma_applier_3(-=, /); } max@0: else if(is_same_type::value == true) { arma_applier_3(-=, *); } max@0: } max@0: } max@0: max@0: max@0: max@0: template max@0: template max@0: arma_hot max@0: inline max@0: void max@0: eglue_core::apply_inplace_schur(Cube& out, const eGlueCube& x) max@0: { max@0: arma_extra_debug_sigprint(); max@0: max@0: const uword n_rows = x.get_n_rows(); max@0: const uword n_cols = x.get_n_cols(); max@0: const uword n_slices = x.get_n_slices(); max@0: max@0: arma_debug_assert_same_size(out.n_rows, out.n_cols, out.n_slices, n_rows, n_cols, n_slices, "element-wise multiplication"); max@0: max@0: typedef typename T1::elem_type eT; max@0: max@0: eT* out_mem = out.memptr(); max@0: max@0: const bool prefer_at_accessor = (ProxyCube::prefer_at_accessor || ProxyCube::prefer_at_accessor); max@0: max@0: if(prefer_at_accessor == false) max@0: { max@0: const uword n_elem = out.n_elem; max@0: max@0: typename ProxyCube::ea_type P1 = x.P1.get_ea(); max@0: typename ProxyCube::ea_type P2 = x.P2.get_ea(); max@0: max@0: if(is_same_type::value == true) { arma_applier_1(*=, +); } max@0: else if(is_same_type::value == true) { arma_applier_1(*=, -); } max@0: else if(is_same_type::value == true) { arma_applier_1(*=, /); } max@0: else if(is_same_type::value == true) { arma_applier_1(*=, *); } max@0: } max@0: else max@0: { max@0: const ProxyCube& P1 = x.P1; max@0: const ProxyCube& P2 = x.P2; max@0: max@0: if(is_same_type::value == true) { arma_applier_3(*=, +); } max@0: else if(is_same_type::value == true) { arma_applier_3(*=, -); } max@0: else if(is_same_type::value == true) { arma_applier_3(*=, /); } max@0: else if(is_same_type::value == true) { arma_applier_3(*=, *); } max@0: } max@0: } max@0: max@0: max@0: max@0: template max@0: template max@0: arma_hot max@0: inline max@0: void max@0: eglue_core::apply_inplace_div(Cube& out, const eGlueCube& x) max@0: { max@0: arma_extra_debug_sigprint(); max@0: max@0: const uword n_rows = x.get_n_rows(); max@0: const uword n_cols = x.get_n_cols(); max@0: const uword n_slices = x.get_n_slices(); max@0: max@0: arma_debug_assert_same_size(out.n_rows, out.n_cols, out.n_slices, n_rows, n_cols, n_slices, "element-wise division"); max@0: max@0: typedef typename T1::elem_type eT; max@0: max@0: eT* out_mem = out.memptr(); max@0: max@0: const bool prefer_at_accessor = (ProxyCube::prefer_at_accessor || ProxyCube::prefer_at_accessor); max@0: max@0: if(prefer_at_accessor == false) max@0: { max@0: const uword n_elem = out.n_elem; max@0: max@0: typename ProxyCube::ea_type P1 = x.P1.get_ea(); max@0: typename ProxyCube::ea_type P2 = x.P2.get_ea(); max@0: max@0: if(is_same_type::value == true) { arma_applier_1(/=, +); } max@0: else if(is_same_type::value == true) { arma_applier_1(/=, -); } max@0: else if(is_same_type::value == true) { arma_applier_1(/=, /); } max@0: else if(is_same_type::value == true) { arma_applier_1(/=, *); } max@0: } max@0: else max@0: { max@0: const ProxyCube& P1 = x.P1; max@0: const ProxyCube& P2 = x.P2; max@0: max@0: if(is_same_type::value == true) { arma_applier_3(/=, +); } max@0: else if(is_same_type::value == true) { arma_applier_3(/=, -); } max@0: else if(is_same_type::value == true) { arma_applier_3(/=, /); } max@0: else if(is_same_type::value == true) { arma_applier_3(/=, *); } max@0: } max@0: } max@0: max@0: max@0: max@0: #undef arma_applier_1 max@0: #undef arma_applier_2 max@0: #undef arma_applier_3 max@0: max@0: max@0: max@0: //! @}