// Copyright (C) 2008-2015 National ICT Australia (NICTA) // // This Source Code Form is subject to the terms of the Mozilla Public // License, v. 2.0. If a copy of the MPL was not distributed with this // file, You can obtain one at http://mozilla.org/MPL/2.0/. // ------------------------------------------------------------------- // // Written by Conrad Sanderson - http://conradsanderson.id.au //! \addtogroup op_misc //! @{ template inline void op_real::apply( Mat& out, const mtOp& X ) { arma_extra_debug_sigprint(); typedef typename T1::pod_type T; const Proxy P(X.m); const uword n_rows = P.get_n_rows(); const uword n_cols = P.get_n_cols(); out.set_size(n_rows, n_cols); T* out_mem = out.memptr(); if(Proxy::prefer_at_accessor == false) { typedef typename Proxy::ea_type ea_type; const uword n_elem = P.get_n_elem(); ea_type A = P.get_ea(); for(uword i=0; i < n_elem; ++i) { out_mem[i] = std::real( A[i] ); } } else { for(uword col=0; col < n_cols; ++col) for(uword row=0; row < n_rows; ++row) { *out_mem = std::real( P.at(row,col) ); out_mem++; } } } template inline void op_real::apply( Cube& out, const mtOpCube& X ) { arma_extra_debug_sigprint(); typedef typename T1::pod_type T; const ProxyCube P(X.m); const uword n_rows = P.get_n_rows(); const uword n_cols = P.get_n_cols(); const uword n_slices = P.get_n_slices(); out.set_size(n_rows, n_cols, n_slices); T* out_mem = out.memptr(); if(ProxyCube::prefer_at_accessor == false) { typedef typename ProxyCube::ea_type ea_type; const uword n_elem = P.get_n_elem(); ea_type A = P.get_ea(); for(uword i=0; i < n_elem; ++i) { out_mem[i] = std::real( A[i] ); } } else { for(uword slice=0; slice < n_slices; ++slice) for(uword col=0; col < n_cols; ++col ) for(uword row=0; row < n_rows; ++row ) { *out_mem = std::real( P.at(row,col,slice) ); out_mem++; } } } template inline void op_imag::apply( Mat& out, const mtOp& X ) { arma_extra_debug_sigprint(); typedef typename T1::pod_type T; const Proxy P(X.m); const uword n_rows = P.get_n_rows(); const uword n_cols = P.get_n_cols(); out.set_size(n_rows, n_cols); T* out_mem = out.memptr(); if(Proxy::prefer_at_accessor == false) { typedef typename Proxy::ea_type ea_type; const uword n_elem = P.get_n_elem(); ea_type A = P.get_ea(); for(uword i=0; i < n_elem; ++i) { out_mem[i] = std::imag( A[i] ); } } else { for(uword col=0; col < n_cols; ++col) for(uword row=0; row < n_rows; ++row) { *out_mem = std::imag( P.at(row,col) ); out_mem++; } } } template inline void op_imag::apply( Cube& out, const mtOpCube& X ) { arma_extra_debug_sigprint(); typedef typename T1::pod_type T; const ProxyCube P(X.m); const uword n_rows = P.get_n_rows(); const uword n_cols = P.get_n_cols(); const uword n_slices = P.get_n_slices(); out.set_size(n_rows, n_cols, n_slices); T* out_mem = out.memptr(); if(ProxyCube::prefer_at_accessor == false) { typedef typename ProxyCube::ea_type ea_type; const uword n_elem = P.get_n_elem(); ea_type A = P.get_ea(); for(uword i=0; i < n_elem; ++i) { out_mem[i] = std::imag( A[i] ); } } else { for(uword slice=0; slice < n_slices; ++slice) for(uword col=0; col < n_cols; ++col ) for(uword row=0; row < n_rows; ++row ) { *out_mem = std::imag( P.at(row,col,slice) ); out_mem++; } } } template inline void op_abs::apply( Mat& out, const mtOp& X ) { arma_extra_debug_sigprint(); typedef typename T1::pod_type T; const Proxy P(X.m); const uword n_rows = P.get_n_rows(); const uword n_cols = P.get_n_cols(); out.set_size(n_rows, n_cols); T* out_mem = out.memptr(); if(Proxy::prefer_at_accessor == false) { typedef typename Proxy::ea_type ea_type; const uword n_elem = P.get_n_elem(); ea_type A = P.get_ea(); for(uword i=0; i < n_elem; ++i) { out_mem[i] = std::abs( A[i] ); } } else { for(uword col=0; col < n_cols; ++col) for(uword row=0; row < n_rows; ++row) { *out_mem = std::abs( P.at(row,col) ); out_mem++; } } } template inline void op_abs::apply( Cube& out, const mtOpCube& X ) { arma_extra_debug_sigprint(); typedef typename T1::pod_type T; const ProxyCube P(X.m); const uword n_rows = P.get_n_rows(); const uword n_cols = P.get_n_cols(); const uword n_slices = P.get_n_slices(); out.set_size(n_rows, n_cols, n_slices); T* out_mem = out.memptr(); if(ProxyCube::prefer_at_accessor == false) { typedef typename ProxyCube::ea_type ea_type; const uword n_elem = P.get_n_elem(); ea_type A = P.get_ea(); for(uword i=0; i < n_elem; ++i) { out_mem[i] = std::abs( A[i] ); } } else { for(uword slice=0; slice < n_slices; ++slice) for(uword col=0; col < n_cols; ++col ) for(uword row=0; row < n_rows; ++row ) { *out_mem = std::abs( P.at(row,col,slice) ); out_mem++; } } } template inline void op_orth::apply( Mat& out, const Op& expr ) { arma_extra_debug_sigprint(); typedef typename T1::elem_type eT; typedef typename T1::pod_type T; T tol = access::tmp_real(expr.aux); arma_debug_check((tol < T(0)), "orth(): tolerance must be >= 0"); const unwrap tmp(expr.m); const Mat& X = tmp.M; Mat U; Col< T> s; Mat V; const bool status = auxlib::svd_dc(U, s, V, X); V.reset(); if(status == false) { out.reset(); arma_bad("orth(): svd failed"); return; } if(s.is_empty()) { out.reset(); return; } const uword s_n_elem = s.n_elem; const T* s_mem = s.memptr(); // set tolerance to default if it hasn't been specified if(tol == T(0)) { tol = (std::max)(X.n_rows, X.n_cols) * s_mem[0] * std::numeric_limits::epsilon(); } uword count = 0; for(uword i=0; i < s_n_elem; ++i) { count += (s_mem[i] > tol) ? uword(1) : uword(0); } if(count > 0) { out = U.head_cols(count); // out *= eT(-1); } else { out.set_size(X.n_rows, 0); } } template inline void op_null::apply( Mat& out, const Op& expr ) { arma_extra_debug_sigprint(); typedef typename T1::elem_type eT; typedef typename T1::pod_type T; T tol = access::tmp_real(expr.aux); arma_debug_check((tol < T(0)), "null(): tolerance must be >= 0"); const unwrap tmp(expr.m); const Mat& X = tmp.M; Mat U; Col< T> s; Mat V; const bool status = auxlib::svd_dc(U, s, V, X); U.reset(); if(status == false) { out.reset(); arma_bad("null(): svd failed"); return; } if(s.is_empty()) { out.reset(); return; } const uword s_n_elem = s.n_elem; const T* s_mem = s.memptr(); // set tolerance to default if it hasn't been specified if(tol == T(0)) { tol = (std::max)(X.n_rows, X.n_cols) * s_mem[0] * std::numeric_limits::epsilon(); } uword count = 0; for(uword i=0; i < s_n_elem; ++i) { count += (s_mem[i] > tol) ? uword(1) : uword(0); } if(count < X.n_cols) { out = V.tail_cols(X.n_cols - count); const uword out_n_elem = out.n_elem; eT* out_mem = out.memptr(); for(uword i=0; i::epsilon()) { out_mem[i] = eT(0); } } } else { out.set_size(X.n_cols, 0); } } //! @}