| Cube_meat.hpp | | Cube_meat.hpp | |
|
| // Copyright (C) 2008-2010 NICTA (www.nicta.com.au) | | // Copyright (C) 2008-2011 NICTA (www.nicta.com.au) | |
| // Copyright (C) 2008-2010 Conrad Sanderson | | // Copyright (C) 2008-2011 Conrad Sanderson | |
| // | | // | |
| // This file is part of the Armadillo C++ library. | | // This file is part of the Armadillo C++ library. | |
| // It is provided without any warranty of fitness | | // It is provided without any warranty of fitness | |
| // for any purpose. You can redistribute this file | | // for any purpose. You can redistribute this file | |
| // and/or modify it under the terms of the GNU | | // and/or modify it under the terms of the GNU | |
| // Lesser General Public License (LGPL) as published | | // Lesser General Public License (LGPL) as published | |
| // by the Free Software Foundation, either version 3 | | // by the Free Software Foundation, either version 3 | |
| // of the License or (at your option) any later version. | | // of the License or (at your option) any later version. | |
| // (see http://www.opensource.org/licenses for more info) | | // (see http://www.opensource.org/licenses for more info) | |
| | | | |
| | | | |
| skipping to change at line 2164 | | skipping to change at line 2164 | |
| template<typename T1> | | template<typename T1> | |
| inline | | inline | |
| void | | void | |
| Cube<eT>::set_imag(const BaseCube<typename Cube<eT>::pod_type,T1>& X) | | Cube<eT>::set_imag(const BaseCube<typename Cube<eT>::pod_type,T1>& X) | |
| { | | { | |
| arma_extra_debug_sigprint(); | | arma_extra_debug_sigprint(); | |
| | | | |
| Cube_aux::set_imag(*this, X); | | Cube_aux::set_imag(*this, X); | |
| } | | } | |
| | | | |
|
| | | template<typename eT> | |
| | | inline | |
| | | eT | |
| | | Cube<eT>::min() const | |
| | | { | |
| | | arma_extra_debug_sigprint(); | |
| | | | |
| | | arma_debug_check( (n_elem == 0), "min(): object has no elements" ); | |
| | | | |
| | | return op_min::direct_min(memptr(), n_elem); | |
| | | } | |
| | | | |
| | | template<typename eT> | |
| | | inline | |
| | | eT | |
| | | Cube<eT>::max() const | |
| | | { | |
| | | arma_extra_debug_sigprint(); | |
| | | | |
| | | arma_debug_check( (n_elem == 0), "max(): object has no elements" ); | |
| | | | |
| | | return op_max::direct_max(memptr(), n_elem); | |
| | | } | |
| | | | |
| | | template<typename eT> | |
| | | inline | |
| | | eT | |
| | | Cube<eT>::min(u32& index_of_min_val) const | |
| | | { | |
| | | arma_extra_debug_sigprint(); | |
| | | | |
| | | arma_debug_check( (n_elem == 0), "min(): object has no elements" ); | |
| | | | |
| | | return op_min::direct_min(memptr(), n_elem, index_of_min_val); | |
| | | } | |
| | | | |
| | | template<typename eT> | |
| | | inline | |
| | | eT | |
| | | Cube<eT>::max(u32& index_of_max_val) const | |
| | | { | |
| | | arma_extra_debug_sigprint(); | |
| | | | |
| | | arma_debug_check( (n_elem == 0), "max(): object has no elements" ); | |
| | | | |
| | | return op_max::direct_max(memptr(), n_elem, index_of_max_val); | |
| | | } | |
| | | | |
| | | template<typename eT> | |
| | | inline | |
| | | eT | |
| | | Cube<eT>::min(u32& row_of_min_val, u32& col_of_min_val, u32& slice_of_min_v | |
| | | al) const | |
| | | { | |
| | | arma_extra_debug_sigprint(); | |
| | | | |
| | | arma_debug_check( (n_elem == 0), "min(): object has no elements" ); | |
| | | | |
| | | u32 i; | |
| | | | |
| | | eT val = op_min::direct_min(memptr(), n_elem, i); | |
| | | | |
| | | const u32 in_slice = i / n_elem_slice; | |
| | | const u32 offset = in_slice * n_elem_slice; | |
| | | const u32 j = i - offset; | |
| | | | |
| | | row_of_min_val = j % n_rows; | |
| | | col_of_min_val = j / n_rows; | |
| | | slice_of_min_val = in_slice; | |
| | | | |
| | | return val; | |
| | | } | |
| | | | |
| | | template<typename eT> | |
| | | inline | |
| | | eT | |
| | | Cube<eT>::max(u32& row_of_max_val, u32& col_of_max_val, u32& slice_of_max_v | |
| | | al) const | |
| | | { | |
| | | arma_extra_debug_sigprint(); | |
| | | | |
| | | arma_debug_check( (n_elem == 0), "max(): object has no elements" ); | |
| | | | |
| | | u32 i; | |
| | | | |
| | | eT val = op_max::direct_max(memptr(), n_elem, i); | |
| | | | |
| | | const u32 in_slice = i / n_elem_slice; | |
| | | const u32 offset = in_slice * n_elem_slice; | |
| | | const u32 j = i - offset; | |
| | | | |
| | | row_of_max_val = j % n_rows; | |
| | | col_of_max_val = j / n_rows; | |
| | | slice_of_max_val = in_slice; | |
| | | | |
| | | return val; | |
| | | } | |
| | | | |
| //! save the cube to a file | | //! save the cube to a file | |
| template<typename eT> | | template<typename eT> | |
| inline | | inline | |
| bool | | bool | |
| Cube<eT>::save(const std::string name, const file_type type, const bool pri
nt_status) const | | Cube<eT>::save(const std::string name, const file_type type, const bool pri
nt_status) const | |
| { | | { | |
| arma_extra_debug_sigprint(); | | arma_extra_debug_sigprint(); | |
| | | | |
| bool save_okay; | | bool save_okay; | |
| | | | |
| | | | |
End of changes. 2 change blocks. |
| 2 lines changed or deleted | | 100 lines changed or added | |
|
| Cube_proto.hpp | | Cube_proto.hpp | |
|
| // Copyright (C) 2008-2010 NICTA (www.nicta.com.au) | | // Copyright (C) 2008-2011 NICTA (www.nicta.com.au) | |
| // Copyright (C) 2008-2010 Conrad Sanderson | | // Copyright (C) 2008-2011 Conrad Sanderson | |
| // | | // | |
| // This file is part of the Armadillo C++ library. | | // This file is part of the Armadillo C++ library. | |
| // It is provided without any warranty of fitness | | // It is provided without any warranty of fitness | |
| // for any purpose. You can redistribute this file | | // for any purpose. You can redistribute this file | |
| // and/or modify it under the terms of the GNU | | // and/or modify it under the terms of the GNU | |
| // Lesser General Public License (LGPL) as published | | // Lesser General Public License (LGPL) as published | |
| // by the Free Software Foundation, either version 3 | | // by the Free Software Foundation, either version 3 | |
| // of the License or (at your option) any later version. | | // of the License or (at your option) any later version. | |
| // (see http://www.opensource.org/licenses for more info) | | // (see http://www.opensource.org/licenses for more info) | |
| | | | |
| | | | |
| skipping to change at line 219 | | skipping to change at line 219 | |
| inline const Cube& randu(const u32 in_rows, const u32 in_cols, const u32
in_slices); | | inline const Cube& randu(const u32 in_rows, const u32 in_cols, const u32
in_slices); | |
| | | | |
| inline const Cube& randn(); | | inline const Cube& randn(); | |
| inline const Cube& randn(const u32 in_rows, const u32 in_cols, const u32
in_slices); | | inline const Cube& randn(const u32 in_rows, const u32 in_cols, const u32
in_slices); | |
| | | | |
| inline void reset(); | | inline void reset(); | |
| | | | |
| template<typename T1> inline void set_real(const BaseCube<pod_type,T1>& X
); | | template<typename T1> inline void set_real(const BaseCube<pod_type,T1>& X
); | |
| template<typename T1> inline void set_imag(const BaseCube<pod_type,T1>& X
); | | template<typename T1> inline void set_imag(const BaseCube<pod_type,T1>& X
); | |
| | | | |
|
| | | inline eT min() const; | |
| | | inline eT max() const; | |
| | | | |
| | | inline eT min(u32& index_of_min_val) const; | |
| | | inline eT max(u32& index_of_max_val) const; | |
| | | | |
| | | inline eT min(u32& row_of_min_val, u32& col_of_min_val, u32& slice_of_min | |
| | | _val) const; | |
| | | inline eT max(u32& row_of_max_val, u32& col_of_max_val, u32& slice_of_max | |
| | | _val) const; | |
| | | | |
| inline bool save(const std::string name, const file_type type = arma_bi
nary, const bool print_status = true) const; | | inline bool save(const std::string name, const file_type type = arma_bi
nary, const bool print_status = true) const; | |
| inline bool save( std::ostream& os, const file_type type = arma_bi
nary, const bool print_status = true) const; | | inline bool save( std::ostream& os, const file_type type = arma_bi
nary, const bool print_status = true) const; | |
| | | | |
| inline bool load(const std::string name, const file_type type = auto_de
tect, const bool print_status = true); | | inline bool load(const std::string name, const file_type type = auto_de
tect, const bool print_status = true); | |
| inline bool load( std::istream& is, const file_type type = auto_de
tect, const bool print_status = true); | | inline bool load( std::istream& is, const file_type type = auto_de
tect, const bool print_status = true); | |
| | | | |
| inline bool quiet_save(const std::string name, const file_type type = a
rma_binary) const; | | inline bool quiet_save(const std::string name, const file_type type = a
rma_binary) const; | |
| inline bool quiet_save( std::ostream& os, const file_type type = a
rma_binary) const; | | inline bool quiet_save( std::ostream& os, const file_type type = a
rma_binary) const; | |
| | | | |
| inline bool quiet_load(const std::string name, const file_type type = a
uto_detect); | | inline bool quiet_load(const std::string name, const file_type type = a
uto_detect); | |
| | | | |
End of changes. 2 change blocks. |
| 2 lines changed or deleted | | 13 lines changed or added | |
|
| glue_mixed_meat.hpp | | glue_mixed_meat.hpp | |
|
| // Copyright (C) 2009-2010 NICTA (www.nicta.com.au) | | // Copyright (C) 2009-2011 NICTA (www.nicta.com.au) | |
| // Copyright (C) 2009-2010 Conrad Sanderson | | // Copyright (C) 2009-2011 Conrad Sanderson | |
| // | | // | |
| // This file is part of the Armadillo C++ library. | | // This file is part of the Armadillo C++ library. | |
| // It is provided without any warranty of fitness | | // It is provided without any warranty of fitness | |
| // for any purpose. You can redistribute this file | | // for any purpose. You can redistribute this file | |
| // and/or modify it under the terms of the GNU | | // and/or modify it under the terms of the GNU | |
| // Lesser General Public License (LGPL) as published | | // Lesser General Public License (LGPL) as published | |
| // by the Free Software Foundation, either version 3 | | // by the Free Software Foundation, either version 3 | |
| // of the License or (at your option) any later version. | | // of the License or (at your option) any later version. | |
| // (see http://www.opensource.org/licenses for more info) | | // (see http://www.opensource.org/licenses for more info) | |
| | | | |
| | | | |
| skipping to change at line 44 | | skipping to change at line 44 | |
| | | | |
| const bool A_is_alias = ( ((void *)&out) == ((void *)&A) ); | | const bool A_is_alias = ( ((void *)&out) == ((void *)&A) ); | |
| const bool B_is_alias = ( ((void *)&out) == ((void *)&B) ); | | const bool B_is_alias = ( ((void *)&out) == ((void *)&B) ); | |
| | | | |
| const Mat<eT1>* AA_ptr = A_is_alias ? new Mat<eT1>(A) : 0; | | const Mat<eT1>* AA_ptr = A_is_alias ? new Mat<eT1>(A) : 0; | |
| const Mat<eT2>* BB_ptr = B_is_alias ? new Mat<eT2>(B) : 0; | | const Mat<eT2>* BB_ptr = B_is_alias ? new Mat<eT2>(B) : 0; | |
| | | | |
| const Mat<eT1>& AA = A_is_alias ? *AA_ptr : A; | | const Mat<eT1>& AA = A_is_alias ? *AA_ptr : A; | |
| const Mat<eT2>& BB = B_is_alias ? *BB_ptr : B; | | const Mat<eT2>& BB = B_is_alias ? *BB_ptr : B; | |
| | | | |
|
| arma_debug_assert_mul_size(AA, BB, "matrix multiplication"); | | arma_debug_assert_mul_size(AA, BB, "multiplication"); | |
| | | | |
| out.set_size(AA.n_rows, BB.n_cols); | | out.set_size(AA.n_rows, BB.n_cols); | |
| | | | |
| gemm_mixed<>::apply(out, AA, BB); | | gemm_mixed<>::apply(out, AA, BB); | |
| | | | |
| if(A_is_alias == true) | | if(A_is_alias == true) | |
| { | | { | |
| delete AA_ptr; | | delete AA_ptr; | |
| } | | } | |
| | | | |
| | | | |
| skipping to change at line 79 | | skipping to change at line 79 | |
| typedef typename T1::elem_type eT1; | | typedef typename T1::elem_type eT1; | |
| typedef typename T2::elem_type eT2; | | typedef typename T2::elem_type eT2; | |
| | | | |
| typedef typename promote_type<eT1,eT2>::result out_eT; | | typedef typename promote_type<eT1,eT2>::result out_eT; | |
| | | | |
| promote_type<eT1,eT2>::check(); | | promote_type<eT1,eT2>::check(); | |
| | | | |
| const Proxy<T1> A(X.A); | | const Proxy<T1> A(X.A); | |
| const Proxy<T2> B(X.B); | | const Proxy<T2> B(X.B); | |
| | | | |
|
| arma_debug_assert_same_size(A, B, "matrix addition"); | | arma_debug_assert_same_size(A, B, "addition"); | |
| | | | |
| out.set_size(A.get_n_rows(), A.get_n_cols()); | | out.set_size(A.get_n_rows(), A.get_n_cols()); | |
| | | | |
| out_eT* out_mem = out.memptr(); | | out_eT* out_mem = out.memptr(); | |
| const u32 n_elem = out.n_elem; | | const u32 n_elem = out.n_elem; | |
| | | | |
| for(u32 i=0; i<n_elem; ++i) | | for(u32 i=0; i<n_elem; ++i) | |
| { | | { | |
| out_mem[i] = upgrade_val<eT1,eT2>::apply(A[i]) + upgrade_val<eT1,eT2>::
apply(B[i]); | | out_mem[i] = upgrade_val<eT1,eT2>::apply(A[i]) + upgrade_val<eT1,eT2>::
apply(B[i]); | |
| } | | } | |
| | | | |
| skipping to change at line 110 | | skipping to change at line 110 | |
| typedef typename T1::elem_type eT1; | | typedef typename T1::elem_type eT1; | |
| typedef typename T2::elem_type eT2; | | typedef typename T2::elem_type eT2; | |
| | | | |
| typedef typename promote_type<eT1,eT2>::result out_eT; | | typedef typename promote_type<eT1,eT2>::result out_eT; | |
| | | | |
| promote_type<eT1,eT2>::check(); | | promote_type<eT1,eT2>::check(); | |
| | | | |
| const Proxy<T1> A(X.A); | | const Proxy<T1> A(X.A); | |
| const Proxy<T2> B(X.B); | | const Proxy<T2> B(X.B); | |
| | | | |
|
| arma_debug_assert_same_size(A, B, "matrix subtraction"); | | arma_debug_assert_same_size(A, B, "subtraction"); | |
| | | | |
| out.set_size(A.get_n_rows(), A.get_n_cols()); | | out.set_size(A.get_n_rows(), A.get_n_cols()); | |
| | | | |
| out_eT* out_mem = out.memptr(); | | out_eT* out_mem = out.memptr(); | |
| const u32 n_elem = out.n_elem; | | const u32 n_elem = out.n_elem; | |
| | | | |
| for(u32 i=0; i<n_elem; ++i) | | for(u32 i=0; i<n_elem; ++i) | |
| { | | { | |
| out_mem[i] = upgrade_val<eT1,eT2>::apply(A[i]) - upgrade_val<eT1,eT2>::
apply(B[i]); | | out_mem[i] = upgrade_val<eT1,eT2>::apply(A[i]) - upgrade_val<eT1,eT2>::
apply(B[i]); | |
| } | | } | |
| | | | |
| skipping to change at line 141 | | skipping to change at line 141 | |
| typedef typename T1::elem_type eT1; | | typedef typename T1::elem_type eT1; | |
| typedef typename T2::elem_type eT2; | | typedef typename T2::elem_type eT2; | |
| | | | |
| typedef typename promote_type<eT1,eT2>::result out_eT; | | typedef typename promote_type<eT1,eT2>::result out_eT; | |
| | | | |
| promote_type<eT1,eT2>::check(); | | promote_type<eT1,eT2>::check(); | |
| | | | |
| const Proxy<T1> A(X.A); | | const Proxy<T1> A(X.A); | |
| const Proxy<T2> B(X.B); | | const Proxy<T2> B(X.B); | |
| | | | |
|
| arma_debug_assert_same_size(A, B, "element-wise matrix division"); | | arma_debug_assert_same_size(A, B, "element-wise division"); | |
| | | | |
| out.set_size(A.get_n_rows(), A.get_n_cols()); | | out.set_size(A.get_n_rows(), A.get_n_cols()); | |
| | | | |
| out_eT* out_mem = out.memptr(); | | out_eT* out_mem = out.memptr(); | |
| const u32 n_elem = out.n_elem; | | const u32 n_elem = out.n_elem; | |
| | | | |
| for(u32 i=0; i<n_elem; ++i) | | for(u32 i=0; i<n_elem; ++i) | |
| { | | { | |
| out_mem[i] = upgrade_val<eT1,eT2>::apply(A[i]) / upgrade_val<eT1,eT2>::
apply(B[i]); | | out_mem[i] = upgrade_val<eT1,eT2>::apply(A[i]) / upgrade_val<eT1,eT2>::
apply(B[i]); | |
| } | | } | |
| | | | |
| skipping to change at line 172 | | skipping to change at line 172 | |
| typedef typename T1::elem_type eT1; | | typedef typename T1::elem_type eT1; | |
| typedef typename T2::elem_type eT2; | | typedef typename T2::elem_type eT2; | |
| | | | |
| typedef typename promote_type<eT1,eT2>::result out_eT; | | typedef typename promote_type<eT1,eT2>::result out_eT; | |
| | | | |
| promote_type<eT1,eT2>::check(); | | promote_type<eT1,eT2>::check(); | |
| | | | |
| const Proxy<T1> A(X.A); | | const Proxy<T1> A(X.A); | |
| const Proxy<T2> B(X.B); | | const Proxy<T2> B(X.B); | |
| | | | |
|
| arma_debug_assert_same_size(A, B, "element-wise matrix multiplication"); | | arma_debug_assert_same_size(A, B, "element-wise multiplication"); | |
| | | | |
| out.set_size(A.get_n_rows(), A.get_n_cols()); | | out.set_size(A.get_n_rows(), A.get_n_cols()); | |
| | | | |
| out_eT* out_mem = out.memptr(); | | out_eT* out_mem = out.memptr(); | |
| const u32 n_elem = out.n_elem; | | const u32 n_elem = out.n_elem; | |
| | | | |
| for(u32 i=0; i<n_elem; ++i) | | for(u32 i=0; i<n_elem; ++i) | |
| { | | { | |
| out_mem[i] = upgrade_val<eT1,eT2>::apply(A[i]) * upgrade_val<eT1,eT2>::
apply(B[i]); | | out_mem[i] = upgrade_val<eT1,eT2>::apply(A[i]) * upgrade_val<eT1,eT2>::
apply(B[i]); | |
| } | | } | |
| | | | |
| skipping to change at line 207 | | skipping to change at line 207 | |
| typedef typename T1::elem_type eT1; | | typedef typename T1::elem_type eT1; | |
| typedef typename T2::elem_type eT2; | | typedef typename T2::elem_type eT2; | |
| | | | |
| typedef typename promote_type<eT1,eT2>::result out_eT; | | typedef typename promote_type<eT1,eT2>::result out_eT; | |
| | | | |
| promote_type<eT1,eT2>::check(); | | promote_type<eT1,eT2>::check(); | |
| | | | |
| const ProxyCube<T1> A(X.A); | | const ProxyCube<T1> A(X.A); | |
| const ProxyCube<T2> B(X.B); | | const ProxyCube<T2> B(X.B); | |
| | | | |
|
| arma_debug_assert_same_size(A, B, "cube addition"); | | arma_debug_assert_same_size(A, B, "addition"); | |
| | | | |
| out.set_size(A.get_n_rows(), A.get_n_cols(), A.get_n_slices()); | | out.set_size(A.get_n_rows(), A.get_n_cols(), A.get_n_slices()); | |
| | | | |
| out_eT* out_mem = out.memptr(); | | out_eT* out_mem = out.memptr(); | |
| const u32 n_elem = out.n_elem; | | const u32 n_elem = out.n_elem; | |
| | | | |
| for(u32 i=0; i<n_elem; ++i) | | for(u32 i=0; i<n_elem; ++i) | |
| { | | { | |
| out_mem[i] = upgrade_val<eT1,eT2>::apply(A[i]) + upgrade_val<eT1,eT2>::
apply(B[i]); | | out_mem[i] = upgrade_val<eT1,eT2>::apply(A[i]) + upgrade_val<eT1,eT2>::
apply(B[i]); | |
| } | | } | |
| | | | |
| skipping to change at line 238 | | skipping to change at line 238 | |
| typedef typename T1::elem_type eT1; | | typedef typename T1::elem_type eT1; | |
| typedef typename T2::elem_type eT2; | | typedef typename T2::elem_type eT2; | |
| | | | |
| typedef typename promote_type<eT1,eT2>::result out_eT; | | typedef typename promote_type<eT1,eT2>::result out_eT; | |
| | | | |
| promote_type<eT1,eT2>::check(); | | promote_type<eT1,eT2>::check(); | |
| | | | |
| const ProxyCube<T1> A(X.A); | | const ProxyCube<T1> A(X.A); | |
| const ProxyCube<T2> B(X.B); | | const ProxyCube<T2> B(X.B); | |
| | | | |
|
| arma_debug_assert_same_size(A, B, "cube subtraction"); | | arma_debug_assert_same_size(A, B, "subtraction"); | |
| | | | |
| out.set_size(A.get_n_rows(), A.get_n_cols(), A.get_n_slices()); | | out.set_size(A.get_n_rows(), A.get_n_cols(), A.get_n_slices()); | |
| | | | |
| out_eT* out_mem = out.memptr(); | | out_eT* out_mem = out.memptr(); | |
| const u32 n_elem = out.n_elem; | | const u32 n_elem = out.n_elem; | |
| | | | |
| for(u32 i=0; i<n_elem; ++i) | | for(u32 i=0; i<n_elem; ++i) | |
| { | | { | |
| out_mem[i] = upgrade_val<eT1,eT2>::apply(A[i]) - upgrade_val<eT1,eT2>::
apply(B[i]); | | out_mem[i] = upgrade_val<eT1,eT2>::apply(A[i]) - upgrade_val<eT1,eT2>::
apply(B[i]); | |
| } | | } | |
| | | | |
| skipping to change at line 269 | | skipping to change at line 269 | |
| typedef typename T1::elem_type eT1; | | typedef typename T1::elem_type eT1; | |
| typedef typename T2::elem_type eT2; | | typedef typename T2::elem_type eT2; | |
| | | | |
| typedef typename promote_type<eT1,eT2>::result out_eT; | | typedef typename promote_type<eT1,eT2>::result out_eT; | |
| | | | |
| promote_type<eT1,eT2>::check(); | | promote_type<eT1,eT2>::check(); | |
| | | | |
| const ProxyCube<T1> A(X.A); | | const ProxyCube<T1> A(X.A); | |
| const ProxyCube<T2> B(X.B); | | const ProxyCube<T2> B(X.B); | |
| | | | |
|
| arma_debug_assert_same_size(A, B, "element-wise cube division"); | | arma_debug_assert_same_size(A, B, "element-wise division"); | |
| | | | |
| out.set_size(A.get_n_rows(), A.get_n_cols(), A.get_n_slices()); | | out.set_size(A.get_n_rows(), A.get_n_cols(), A.get_n_slices()); | |
| | | | |
| out_eT* out_mem = out.memptr(); | | out_eT* out_mem = out.memptr(); | |
| const u32 n_elem = out.n_elem; | | const u32 n_elem = out.n_elem; | |
| | | | |
| for(u32 i=0; i<n_elem; ++i) | | for(u32 i=0; i<n_elem; ++i) | |
| { | | { | |
| out_mem[i] = upgrade_val<eT1,eT2>::apply(A[i]) / upgrade_val<eT1,eT2>::
apply(B[i]); | | out_mem[i] = upgrade_val<eT1,eT2>::apply(A[i]) / upgrade_val<eT1,eT2>::
apply(B[i]); | |
| } | | } | |
| | | | |
| skipping to change at line 300 | | skipping to change at line 300 | |
| typedef typename T1::elem_type eT1; | | typedef typename T1::elem_type eT1; | |
| typedef typename T2::elem_type eT2; | | typedef typename T2::elem_type eT2; | |
| | | | |
| typedef typename promote_type<eT1,eT2>::result out_eT; | | typedef typename promote_type<eT1,eT2>::result out_eT; | |
| | | | |
| promote_type<eT1,eT2>::check(); | | promote_type<eT1,eT2>::check(); | |
| | | | |
| const ProxyCube<T1> A(X.A); | | const ProxyCube<T1> A(X.A); | |
| const ProxyCube<T2> B(X.B); | | const ProxyCube<T2> B(X.B); | |
| | | | |
|
| arma_debug_assert_same_size(A, B, "element-wise cube multiplication"); | | arma_debug_assert_same_size(A, B, "element-wise multiplication"); | |
| | | | |
| out.set_size(A.get_n_rows(), A.get_n_cols(), A.get_n_slices()); | | out.set_size(A.get_n_rows(), A.get_n_cols(), A.get_n_slices()); | |
| | | | |
| out_eT* out_mem = out.memptr(); | | out_eT* out_mem = out.memptr(); | |
| const u32 n_elem = out.n_elem; | | const u32 n_elem = out.n_elem; | |
| | | | |
| for(u32 i=0; i<n_elem; ++i) | | for(u32 i=0; i<n_elem; ++i) | |
| { | | { | |
| out_mem[i] = upgrade_val<eT1,eT2>::apply(A[i]) * upgrade_val<eT1,eT2>::
apply(B[i]); | | out_mem[i] = upgrade_val<eT1,eT2>::apply(A[i]) * upgrade_val<eT1,eT2>::
apply(B[i]); | |
| } | | } | |
| | | | |
End of changes. 10 change blocks. |
| 11 lines changed or deleted | | 11 lines changed or added | |
|
| op_max_meat.hpp | | op_max_meat.hpp | |
| | | | |
| skipping to change at line 58 | | skipping to change at line 58 | |
| | | | |
| if(X_i > max_val) | | if(X_i > max_val) | |
| { | | { | |
| max_val = X_i; | | max_val = X_i; | |
| } | | } | |
| } | | } | |
| | | | |
| return max_val; | | return max_val; | |
| } | | } | |
| | | | |
|
| | | template<typename eT> | |
| | | inline | |
| | | eT | |
| | | op_max::direct_max(const eT* const X, const u32 n_elem, u32& index_of_max_v | |
| | | al) | |
| | | { | |
| | | arma_extra_debug_sigprint(); | |
| | | | |
| | | eT max_val = (n_elem != 1) ? priv::most_neg<eT>() : X[0]; | |
| | | | |
| | | u32 best_index = 0; | |
| | | | |
| | | u32 i,j; | |
| | | | |
| | | for(i=0, j=1; j<n_elem; i+=2, j+=2) | |
| | | { | |
| | | const eT X_i = X[i]; | |
| | | const eT X_j = X[j]; | |
| | | | |
| | | if(X_i > max_val) | |
| | | { | |
| | | max_val = X_i; | |
| | | best_index = i; | |
| | | } | |
| | | | |
| | | if(X_j > max_val) | |
| | | { | |
| | | max_val = X_j; | |
| | | best_index = j; | |
| | | } | |
| | | } | |
| | | | |
| | | if(i < n_elem) | |
| | | { | |
| | | const eT X_i = X[i]; | |
| | | | |
| | | if(X_i > max_val) | |
| | | { | |
| | | max_val = X_i; | |
| | | best_index = i; | |
| | | } | |
| | | } | |
| | | | |
| | | index_of_max_val = best_index; | |
| | | | |
| | | return max_val; | |
| | | } | |
| | | | |
| | | template<typename eT> | |
| | | inline | |
| | | eT | |
| | | op_max::direct_max(const Mat<eT>& X, const u32 row) | |
| | | { | |
| | | arma_extra_debug_sigprint(); | |
| | | | |
| | | const u32 X_n_cols = X.n_cols; | |
| | | | |
| | | eT max_val = (X_n_cols != 1) ? priv::most_neg<eT>() : X.at(row,0); | |
| | | | |
| | | for(u32 col=0; col<X_n_cols; ++col) | |
| | | { | |
| | | const eT tmp_val = X.at(row,col); | |
| | | | |
| | | if(tmp_val > max_val) | |
| | | { | |
| | | max_val = tmp_val; | |
| | | } | |
| | | } | |
| | | | |
| | | return max_val; | |
| | | } | |
| | | | |
| //! find the maximum value in a subview | | //! find the maximum value in a subview | |
| template<typename eT> | | template<typename eT> | |
| inline | | inline | |
| eT | | eT | |
| op_max::direct_max(const subview<eT>& X) | | op_max::direct_max(const subview<eT>& X) | |
| { | | { | |
| arma_extra_debug_sigprint(); | | arma_extra_debug_sigprint(); | |
| | | | |
| const u32 X_n_elem = X.n_elem; | | const u32 X_n_elem = X.n_elem; | |
| eT max_val = (X_n_elem != 1) ? priv::most_neg<eT>() : X[0]; | | eT max_val = (X_n_elem != 1) ? priv::most_neg<eT>() : X[0]; | |
| | | | |
| skipping to change at line 150 | | skipping to change at line 221 | |
| } | | } | |
| else | | else | |
| if(dim == 1) | | if(dim == 1) | |
| { | | { | |
| arma_extra_debug_print("op_max::apply(), dim = 1"); | | arma_extra_debug_print("op_max::apply(), dim = 1"); | |
| | | | |
| out.set_size(X_n_rows, 1); | | out.set_size(X_n_rows, 1); | |
| | | | |
| for(u32 row=0; row<X_n_rows; ++row) | | for(u32 row=0; row<X_n_rows; ++row) | |
| { | | { | |
|
| eT max_val = (X_n_cols != 1) ? priv::most_neg<eT>() : X.at(row,0); | | out[row] = op_max::direct_max( X, row ); | |
| | | | |
| for(u32 col=0; col<X_n_cols; ++col) | | | |
| { | | | |
| const eT tmp_val = X.at(row,col); | | | |
| | | | |
| if(tmp_val > max_val) | | | |
| { | | | |
| max_val = tmp_val; | | | |
| } | | | |
| } | | | |
| | | | |
| out[row] = max_val; | | | |
| } | | } | |
| } | | } | |
| } | | } | |
| | | | |
| //! Find the maximum value in an array (version for complex numbers) | | //! Find the maximum value in an array (version for complex numbers) | |
| template<typename T> | | template<typename T> | |
| inline | | inline | |
| std::complex<T> | | std::complex<T> | |
| op_max::direct_max(const std::complex<T>* const X, const u32 n_elem) | | op_max::direct_max(const std::complex<T>* const X, const u32 n_elem) | |
| { | | { | |
| | | | |
| skipping to change at line 192 | | skipping to change at line 251 | |
| if(tmp_val > max_val) | | if(tmp_val > max_val) | |
| { | | { | |
| max_val = tmp_val; | | max_val = tmp_val; | |
| index = i; | | index = i; | |
| } | | } | |
| } | | } | |
| | | | |
| return X[index]; | | return X[index]; | |
| } | | } | |
| | | | |
|
| //! Find the maximum value in a subview (version for complex numbers) | | | |
| template<typename T> | | template<typename T> | |
| inline | | inline | |
| std::complex<T> | | std::complex<T> | |
|
| op_max::direct_max(const subview< std::complex<T> >& X) | | op_max::direct_max(const std::complex<T>* const X, const u32 n_elem, u32& i
ndex_of_max_val) | |
| { | | { | |
| arma_extra_debug_sigprint(); | | arma_extra_debug_sigprint(); | |
| | | | |
|
| const u32 X_n_elem = X.n_elem; | | u32 index = 0; | |
| u32 index = 0; | | T max_val = (n_elem != 1) ? priv::most_neg<T>() : std::abs(X[0]); | |
| T max_val = (X_n_elem != 1) ? priv::most_neg<T>() : std::abs(X[0 | | | |
| ]); | | | |
| | | | |
|
| for(u32 i=0; i<X_n_elem; ++i) | | for(u32 i=0; i<n_elem; ++i) | |
| { | | { | |
| const T tmp_val = std::abs(X[i]); | | const T tmp_val = std::abs(X[i]); | |
| | | | |
| if(tmp_val > max_val) | | if(tmp_val > max_val) | |
| { | | { | |
| max_val = tmp_val; | | max_val = tmp_val; | |
| index = i; | | index = i; | |
| } | | } | |
| } | | } | |
| | | | |
|
| | | index_of_max_val = index; | |
| | | | |
| return X[index]; | | return X[index]; | |
| } | | } | |
| | | | |
|
| //! Find the maximum value in a diagview (version for complex numbers) | | | |
| template<typename T> | | template<typename T> | |
| inline | | inline | |
| std::complex<T> | | std::complex<T> | |
|
| op_max::direct_max(const diagview< std::complex<T> >& X) | | op_max::direct_max(const Mat< std::complex<T> >& X, const u32 row) | |
| | | { | |
| | | arma_extra_debug_sigprint(); | |
| | | | |
| | | const u32 X_n_cols = X.n_cols; | |
| | | | |
| | | u32 index = 0; | |
| | | T max_val = (X_n_cols != 1) ? priv::most_neg<T>() : std::abs(X.at(row,0 | |
| | | )); | |
| | | | |
| | | for(u32 col=0; col<X_n_cols; ++col) | |
| | | { | |
| | | const T tmp_val = std::abs(X.at(row,col)); | |
| | | | |
| | | if(tmp_val > max_val) | |
| | | { | |
| | | max_val = tmp_val; | |
| | | index = col; | |
| | | } | |
| | | } | |
| | | | |
| | | return X.at(row,index); | |
| | | } | |
| | | | |
| | | //! Find the maximum value in a subview (version for complex numbers) | |
| | | template<typename T> | |
| | | inline | |
| | | std::complex<T> | |
| | | op_max::direct_max(const subview< std::complex<T> >& X) | |
| { | | { | |
| arma_extra_debug_sigprint(); | | arma_extra_debug_sigprint(); | |
| | | | |
| const u32 X_n_elem = X.n_elem; | | const u32 X_n_elem = X.n_elem; | |
| u32 index = 0; | | u32 index = 0; | |
| T max_val = (X_n_elem != 1) ? priv::most_neg<T>() : std::abs(X[0
]); | | T max_val = (X_n_elem != 1) ? priv::most_neg<T>() : std::abs(X[0
]); | |
| | | | |
| for(u32 i=0; i<X_n_elem; ++i) | | for(u32 i=0; i<X_n_elem; ++i) | |
| { | | { | |
| const T tmp_val = std::abs(X[i]); | | const T tmp_val = std::abs(X[i]); | |
| | | | |
| skipping to change at line 244 | | skipping to change at line 329 | |
| if(tmp_val > max_val) | | if(tmp_val > max_val) | |
| { | | { | |
| max_val = tmp_val; | | max_val = tmp_val; | |
| index = i; | | index = i; | |
| } | | } | |
| } | | } | |
| | | | |
| return X[index]; | | return X[index]; | |
| } | | } | |
| | | | |
|
| //! Implementation for complex numbers | | //! Find the maximum value in a diagview (version for complex numbers) | |
| template<typename T, typename T1> | | template<typename T> | |
| inline void op_max::apply(Mat< std::complex<T> >& out, const Op<T1,op_max>& | | inline | |
| in) | | std::complex<T> | |
| | | op_max::direct_max(const diagview< std::complex<T> >& X) | |
| { | | { | |
| arma_extra_debug_sigprint(); | | arma_extra_debug_sigprint(); | |
| | | | |
|
| typedef typename std::complex<T> eT; | | const u32 X_n_elem = X.n_elem; | |
| isnt_same_type<eT, typename T1::elem_type>::check(); | | u32 index = 0; | |
| | | T max_val = (X_n_elem != 1) ? priv::most_neg<T>() : std::abs(X[0 | |
| const unwrap_check<T1> tmp(in.m, out); | | ]); | |
| const Mat<eT>& X = tmp.M; | | | |
| | | | |
| arma_debug_check( (X.n_elem == 0), "max(): given matrix has no elements" | | | |
| ); | | | |
| | | | |
| const u32 dim = in.aux_u32_a; | | | |
| arma_debug_check( (dim > 1), "max(): incorrect usage. dim must be 0 or 1" | | | |
| ); | | | |
| | | | |
| const u32 X_n_rows = X.n_rows; | | | |
| const u32 X_n_cols = X.n_cols; | | | |
| | | | |
| if(dim == 0) // column-wise max | | | |
| { | | | |
| arma_extra_debug_print("op_max::apply(), dim = 0"); | | | |
| | | | |
| out.set_size(1, X_n_cols); | | | |
| | | | |
|
| for(u32 col=0; col<X_n_cols; ++col) | | for(u32 i=0; i<X_n_elem; ++i) | |
| { | | | |
| out[col] = op_max::direct_max( X.colptr(col), X_n_rows ); | | | |
| } | | | |
| } | | | |
| else | | | |
| if(dim == 1) // row-wise max | | | |
| { | | { | |
|
| arma_extra_debug_print("op_max::apply(), dim = 1"); | | const T tmp_val = std::abs(X[i]); | |
| | | | |
| out.set_size(X_n_rows, 1); | | | |
| | | | |
|
| for(u32 row=0; row<X_n_rows; ++row) | | if(tmp_val > max_val) | |
| { | | { | |
|
| u32 index = 0; | | max_val = tmp_val; | |
| T max_val = (X_n_cols != 1) ? priv::most_neg<T>() : std::abs(X.at(r | | index = i; | |
| ow,0)); | | | |
| | | | |
| for(u32 col=0; col<X_n_cols; ++col) | | | |
| { | | | |
| const T tmp_val = std::abs(X.at(row,col)); | | | |
| | | | |
| if(tmp_val > max_val) | | | |
| { | | | |
| max_val = tmp_val; | | | |
| index = col; | | | |
| } | | | |
| } | | | |
| | | | |
| out[row] = X.at(row,index); | | | |
| } | | } | |
|
| | | | |
| } | | } | |
| | | | |
|
| | | return X[index]; | |
| } | | } | |
| | | | |
| //! @} | | //! @} | |
| | | | |
End of changes. 17 change blocks. |
| 75 lines changed or deleted | | 123 lines changed or added | |
|
| op_mean_meat.hpp | | op_mean_meat.hpp | |
| | | | |
| skipping to change at line 27 | | skipping to change at line 27 | |
| template<typename eT> | | template<typename eT> | |
| arma_pure | | arma_pure | |
| inline | | inline | |
| eT | | eT | |
| op_mean::direct_mean(const eT* const X, const u32 n_elem) | | op_mean::direct_mean(const eT* const X, const u32 n_elem) | |
| { | | { | |
| arma_extra_debug_sigprint(); | | arma_extra_debug_sigprint(); | |
| | | | |
| typedef typename get_pod_type<eT>::result T; | | typedef typename get_pod_type<eT>::result T; | |
| | | | |
|
| return arrayops::accumulate(X, n_elem) / T(n_elem); | | const eT result = arrayops::accumulate(X, n_elem) / T(n_elem); | |
| | | | |
| | | return arma_isfinite(result) ? result : op_mean::direct_mean_robust(X, n_ | |
| | | elem); | |
| | | } | |
| | | | |
| | | template<typename eT> | |
| | | inline | |
| | | eT | |
| | | op_mean::direct_mean(const Mat<eT>& X, const u32 row) | |
| | | { | |
| | | arma_extra_debug_sigprint(); | |
| | | | |
| | | typedef typename get_pod_type<eT>::result T; | |
| | | | |
| | | const u32 X_n_cols = X.n_cols; | |
| | | | |
| | | eT val = eT(0); | |
| | | | |
| | | for(u32 col=0; col<X_n_cols; ++col) | |
| | | { | |
| | | val += X.at(row,col); | |
| | | } | |
| | | | |
| | | const eT result = val / T(X_n_cols); | |
| | | | |
| | | return arma_isfinite(result) ? result : direct_mean_robust(X, row); | |
| } | | } | |
| | | | |
| //! find the mean value of a subview | | //! find the mean value of a subview | |
| template<typename eT> | | template<typename eT> | |
| inline | | inline | |
| eT | | eT | |
| op_mean::direct_mean(const subview<eT>& X) | | op_mean::direct_mean(const subview<eT>& X) | |
| { | | { | |
| arma_extra_debug_sigprint(); | | arma_extra_debug_sigprint(); | |
| | | | |
| typedef typename get_pod_type<eT>::result T; | | typedef typename get_pod_type<eT>::result T; | |
| | | | |
| const u32 X_n_elem = X.n_elem; | | const u32 X_n_elem = X.n_elem; | |
| eT val = eT(0); | | eT val = eT(0); | |
| | | | |
| for(u32 i=0; i<X_n_elem; ++i) | | for(u32 i=0; i<X_n_elem; ++i) | |
| { | | { | |
| val += X[i]; | | val += X[i]; | |
| } | | } | |
| | | | |
|
| return val / T(X_n_elem); | | const eT result = val / T(X_n_elem); | |
| | | | |
| | | return arma_isfinite(result) ? result : direct_mean_robust(X); | |
| } | | } | |
| | | | |
| //! find the mean value of a diagview | | //! find the mean value of a diagview | |
| template<typename eT> | | template<typename eT> | |
| inline | | inline | |
| eT | | eT | |
| op_mean::direct_mean(const diagview<eT>& X) | | op_mean::direct_mean(const diagview<eT>& X) | |
| { | | { | |
| arma_extra_debug_sigprint(); | | arma_extra_debug_sigprint(); | |
| | | | |
| typedef typename get_pod_type<eT>::result T; | | typedef typename get_pod_type<eT>::result T; | |
| | | | |
| const u32 X_n_elem = X.n_elem; | | const u32 X_n_elem = X.n_elem; | |
| eT val = eT(0); | | eT val = eT(0); | |
| | | | |
| for(u32 i=0; i<X_n_elem; ++i) | | for(u32 i=0; i<X_n_elem; ++i) | |
| { | | { | |
| val += X[i]; | | val += X[i]; | |
| } | | } | |
| | | | |
|
| return val / T(X_n_elem); | | const eT result = val / T(X_n_elem); | |
| | | | |
| | | return arma_isfinite(result) ? result : direct_mean_robust(X); | |
| } | | } | |
| | | | |
| //! \brief | | //! \brief | |
| //! For each row or for each column, find the mean value. | | //! For each row or for each column, find the mean value. | |
| //! The result is stored in a dense matrix that has either one column or on
e row. | | //! The result is stored in a dense matrix that has either one column or on
e row. | |
| //! The dimension, for which the means are found, is set via the mean() fun
ction. | | //! The dimension, for which the means are found, is set via the mean() fun
ction. | |
| template<typename T1> | | template<typename T1> | |
| inline | | inline | |
| void | | void | |
| op_mean::apply(Mat<typename T1::elem_type>& out, const Op<T1,op_mean>& in) | | op_mean::apply(Mat<typename T1::elem_type>& out, const Op<T1,op_mean>& in) | |
| | | | |
| skipping to change at line 117 | | skipping to change at line 146 | |
| } | | } | |
| else | | else | |
| if(dim == 1) | | if(dim == 1) | |
| { | | { | |
| arma_extra_debug_print("op_mean::apply(), dim = 1"); | | arma_extra_debug_print("op_mean::apply(), dim = 1"); | |
| | | | |
| out.set_size(X_n_rows, 1); | | out.set_size(X_n_rows, 1); | |
| | | | |
| for(u32 row=0; row<X_n_rows; ++row) | | for(u32 row=0; row<X_n_rows; ++row) | |
| { | | { | |
|
| eT val = eT(0); | | out[row] = op_mean::direct_mean( X, row ); | |
| | | } | |
| | | } | |
| | | } | |
| | | | |
|
| for(u32 col=0; col<X_n_cols; ++col) | | template<typename eT> | |
| { | | arma_pure | |
| val += X.at(row,col); | | inline | |
| } | | eT | |
| | | op_mean::direct_mean_robust(const eT* const X, const u32 n_elem) | |
| | | { | |
| | | arma_extra_debug_sigprint(); | |
| | | | |
|
| out[row] = val / T(X_n_cols); | | // use an adapted form of the mean finding algorithm from the running_sta | |
| } | | t class | |
| | | | |
| | | typedef typename get_pod_type<eT>::result T; | |
| | | | |
| | | u32 i,j; | |
| | | | |
| | | eT r_mean = eT(0); | |
| | | | |
| | | for(i=0, j=1; j<n_elem; i+=2, j+=2) | |
| | | { | |
| | | const eT Xi = X[i]; | |
| | | const eT Xj = X[j]; | |
| | | | |
| | | r_mean = r_mean + (Xi - r_mean)/T(j); // we need i+1, and j is equiv | |
| | | alent to i+1 here | |
| | | r_mean = r_mean + (Xj - r_mean)/T(j+1); | |
| | | } | |
| | | | |
| | | if(i < n_elem) | |
| | | { | |
| | | const eT Xi = X[i]; | |
| | | | |
| | | r_mean = r_mean + (Xi - r_mean)/T(i+1); | |
| | | } | |
| | | | |
| | | return r_mean; | |
| | | } | |
| | | | |
| | | template<typename eT> | |
| | | inline | |
| | | eT | |
| | | op_mean::direct_mean_robust(const Mat<eT>& X, const u32 row) | |
| | | { | |
| | | arma_extra_debug_sigprint(); | |
| | | | |
| | | typedef typename get_pod_type<eT>::result T; | |
| | | | |
| | | const u32 X_n_cols = X.n_cols; | |
| | | | |
| | | eT r_mean = eT(0); | |
| | | | |
| | | for(u32 col=0; col<X_n_cols; ++col) | |
| | | { | |
| | | r_mean = r_mean + (X.at(row,col) - r_mean)/T(col+1); | |
| | | } | |
| | | | |
| | | return r_mean; | |
| | | } | |
| | | | |
| | | template<typename eT> | |
| | | inline | |
| | | eT | |
| | | op_mean::direct_mean_robust(const subview<eT>& X) | |
| | | { | |
| | | arma_extra_debug_sigprint(); | |
| | | | |
| | | typedef typename get_pod_type<eT>::result T; | |
| | | | |
| | | const u32 X_n_elem = X.n_elem; | |
| | | | |
| | | eT r_mean = eT(0); | |
| | | | |
| | | for(u32 i=0; i<X_n_elem; ++i) | |
| | | { | |
| | | r_mean = r_mean + (X[i] - r_mean)/T(i+1); | |
| } | | } | |
|
| | | | |
| | | return r_mean; | |
| | | } | |
| | | | |
| | | template<typename eT> | |
| | | inline | |
| | | eT | |
| | | op_mean::direct_mean_robust(const diagview<eT>& X) | |
| | | { | |
| | | arma_extra_debug_sigprint(); | |
| | | | |
| | | typedef typename get_pod_type<eT>::result T; | |
| | | | |
| | | const u32 X_n_elem = X.n_elem; | |
| | | | |
| | | eT r_mean = eT(0); | |
| | | | |
| | | for(u32 i=0; i<X_n_elem; ++i) | |
| | | { | |
| | | r_mean = r_mean + (X[i] - r_mean)/T(i+1); | |
| | | } | |
| | | | |
| | | return r_mean; | |
| } | | } | |
| | | | |
| //! @} | | //! @} | |
| | | | |
End of changes. 7 change blocks. |
| 10 lines changed or deleted | | 133 lines changed or added | |
|
| op_min_meat.hpp | | op_min_meat.hpp | |
| | | | |
| skipping to change at line 58 | | skipping to change at line 58 | |
| | | | |
| if(X_i < min_val) | | if(X_i < min_val) | |
| { | | { | |
| min_val = X_i; | | min_val = X_i; | |
| } | | } | |
| } | | } | |
| | | | |
| return min_val; | | return min_val; | |
| } | | } | |
| | | | |
|
| | | template<typename eT> | |
| | | inline | |
| | | eT | |
| | | op_min::direct_min(const eT* const X, const u32 n_elem, u32& index_of_min_v | |
| | | al) | |
| | | { | |
| | | arma_extra_debug_sigprint(); | |
| | | | |
| | | eT min_val = (n_elem != 1) ? priv::most_pos<eT>() : X[0]; | |
| | | | |
| | | u32 best_index = 0; | |
| | | | |
| | | u32 i,j; | |
| | | | |
| | | for(i=0, j=1; j<n_elem; i+=2, j+=2) | |
| | | { | |
| | | const eT X_i = X[i]; | |
| | | const eT X_j = X[j]; | |
| | | | |
| | | if(X_i < min_val) | |
| | | { | |
| | | min_val = X_i; | |
| | | best_index = i; | |
| | | } | |
| | | | |
| | | if(X_j < min_val) | |
| | | { | |
| | | min_val = X_j; | |
| | | best_index = j; | |
| | | } | |
| | | } | |
| | | | |
| | | if(i < n_elem) | |
| | | { | |
| | | const eT X_i = X[i]; | |
| | | | |
| | | if(X_i < min_val) | |
| | | { | |
| | | min_val = X_i; | |
| | | best_index = i; | |
| | | } | |
| | | } | |
| | | | |
| | | index_of_min_val = best_index; | |
| | | | |
| | | return min_val; | |
| | | } | |
| | | | |
| | | template<typename eT> | |
| | | inline | |
| | | eT | |
| | | op_min::direct_min(const Mat<eT>& X, const u32 row) | |
| | | { | |
| | | arma_extra_debug_sigprint(); | |
| | | | |
| | | const u32 X_n_cols = X.n_cols; | |
| | | | |
| | | eT min_val = (X_n_cols != 1) ? priv::most_pos<eT>() : X.at(row,0); | |
| | | | |
| | | for(u32 col=0; col<X_n_cols; ++col) | |
| | | { | |
| | | const eT tmp_val = X.at(row,col); | |
| | | | |
| | | if(tmp_val < min_val) | |
| | | { | |
| | | min_val = tmp_val; | |
| | | } | |
| | | } | |
| | | | |
| | | return min_val; | |
| | | } | |
| | | | |
| //! find the minimum value in a subview | | //! find the minimum value in a subview | |
| template<typename eT> | | template<typename eT> | |
| inline | | inline | |
| eT | | eT | |
| op_min::direct_min(const subview<eT>& X) | | op_min::direct_min(const subview<eT>& X) | |
| { | | { | |
| arma_extra_debug_sigprint(); | | arma_extra_debug_sigprint(); | |
| | | | |
| const u32 X_n_elem = X.n_elem; | | const u32 X_n_elem = X.n_elem; | |
| | | | |
| | | | |
| skipping to change at line 150 | | skipping to change at line 221 | |
| } | | } | |
| else | | else | |
| if(dim == 1) // min in each row | | if(dim == 1) // min in each row | |
| { | | { | |
| arma_extra_debug_print("op_min::apply(), dim = 1"); | | arma_extra_debug_print("op_min::apply(), dim = 1"); | |
| | | | |
| out.set_size(X_n_rows, 1); | | out.set_size(X_n_rows, 1); | |
| | | | |
| for(u32 row=0; row<X_n_rows; ++row) | | for(u32 row=0; row<X_n_rows; ++row) | |
| { | | { | |
|
| eT min_val = (X_n_cols != 1) ? priv::most_pos<eT>() : X.at(row,0); | | out[row] = op_min::direct_min( X, row ); | |
| | | | |
| for(u32 col=0; col<X_n_cols; ++col) | | | |
| { | | | |
| const eT tmp_val = X.at(row,col); | | | |
| | | | |
| if(tmp_val < min_val) | | | |
| { | | | |
| min_val = tmp_val; | | | |
| } | | | |
| } | | | |
| | | | |
| out[row] = min_val; | | | |
| } | | } | |
|
| | | | |
| } | | } | |
|
| | | | |
| } | | } | |
| | | | |
| //! Find the minimum value in an array (version for complex numbers) | | //! Find the minimum value in an array (version for complex numbers) | |
| template<typename T> | | template<typename T> | |
| inline | | inline | |
| std::complex<T> | | std::complex<T> | |
| op_min::direct_min(const std::complex<T>* const X, const u32 n_elem) | | op_min::direct_min(const std::complex<T>* const X, const u32 n_elem) | |
| { | | { | |
| arma_extra_debug_sigprint(); | | arma_extra_debug_sigprint(); | |
| | | | |
| | | | |
| skipping to change at line 194 | | skipping to change at line 251 | |
| if(tmp_val < min_val) | | if(tmp_val < min_val) | |
| { | | { | |
| min_val = tmp_val; | | min_val = tmp_val; | |
| index = i; | | index = i; | |
| } | | } | |
| } | | } | |
| | | | |
| return X[index]; | | return X[index]; | |
| } | | } | |
| | | | |
|
| //! Find the minimum value in a subview (version for complex numbers) | | | |
| template<typename T> | | template<typename T> | |
| inline | | inline | |
| std::complex<T> | | std::complex<T> | |
|
| op_min::direct_min(const subview< std::complex<T> >& X) | | op_min::direct_min(const std::complex<T>* const X, const u32 n_elem, u32& i
ndex_of_min_val) | |
| { | | { | |
| arma_extra_debug_sigprint(); | | arma_extra_debug_sigprint(); | |
| | | | |
|
| const u32 X_n_elem = X.n_elem; | | u32 index = 0; | |
| u32 index = 0; | | T min_val = (n_elem != 1) ? priv::most_pos<T>() : std::abs(X[0]); | |
| T min_val = (X_n_elem != 1) ? priv::most_pos<T>() : std::abs(X[0 | | | |
| ]); | | | |
| | | | |
|
| for(u32 i=0; i<X_n_elem; ++i) | | for(u32 i=0; i<n_elem; ++i) | |
| { | | { | |
| const T tmp_val = std::abs(X[i]); | | const T tmp_val = std::abs(X[i]); | |
| | | | |
| if(tmp_val < min_val) | | if(tmp_val < min_val) | |
| { | | { | |
| min_val = tmp_val; | | min_val = tmp_val; | |
| index = i; | | index = i; | |
| } | | } | |
| } | | } | |
| | | | |
|
| | | index_of_min_val = index; | |
| | | | |
| return X[index]; | | return X[index]; | |
| } | | } | |
| | | | |
|
| //! Find the minimum value in a diagview (version for complex numbers) | | | |
| template<typename T> | | template<typename T> | |
| inline | | inline | |
| std::complex<T> | | std::complex<T> | |
|
| op_min::direct_min(const diagview< std::complex<T> >& X) | | op_min::direct_min(const Mat< std::complex<T> >& X, const u32 row) | |
| | | { | |
| | | arma_extra_debug_sigprint(); | |
| | | | |
| | | const u32 X_n_cols = X.n_cols; | |
| | | | |
| | | u32 index = 0; | |
| | | T min_val = (X_n_cols != 1) ? priv::most_pos<T>() : std::abs(X.at(row,0 | |
| | | )); | |
| | | | |
| | | for(u32 col=0; col<X_n_cols; ++col) | |
| | | { | |
| | | const T tmp_val = std::abs(X.at(row,col)); | |
| | | | |
| | | if(tmp_val < min_val) | |
| | | { | |
| | | min_val = tmp_val; | |
| | | index = col; | |
| | | } | |
| | | } | |
| | | | |
| | | return X.at(row,index); | |
| | | } | |
| | | | |
| | | //! Find the minimum value in a subview (version for complex numbers) | |
| | | template<typename T> | |
| | | inline | |
| | | std::complex<T> | |
| | | op_min::direct_min(const subview< std::complex<T> >& X) | |
| { | | { | |
| arma_extra_debug_sigprint(); | | arma_extra_debug_sigprint(); | |
| | | | |
| const u32 X_n_elem = X.n_elem; | | const u32 X_n_elem = X.n_elem; | |
| u32 index = 0; | | u32 index = 0; | |
| T min_val = (X_n_elem != 1) ? priv::most_pos<T>() : std::abs(X[0
]); | | T min_val = (X_n_elem != 1) ? priv::most_pos<T>() : std::abs(X[0
]); | |
| | | | |
| for(u32 i=0; i<X_n_elem; ++i) | | for(u32 i=0; i<X_n_elem; ++i) | |
| { | | { | |
| const T tmp_val = std::abs(X[i]); | | const T tmp_val = std::abs(X[i]); | |
| | | | |
| skipping to change at line 246 | | skipping to change at line 329 | |
| if(tmp_val < min_val) | | if(tmp_val < min_val) | |
| { | | { | |
| min_val = tmp_val; | | min_val = tmp_val; | |
| index = i; | | index = i; | |
| } | | } | |
| } | | } | |
| | | | |
| return X[index]; | | return X[index]; | |
| } | | } | |
| | | | |
|
| //! Implementation for complex numbers | | //! Find the minimum value in a diagview (version for complex numbers) | |
| template<typename T, typename T1> | | template<typename T> | |
| inline void op_min::apply(Mat< std::complex<T> >& out, const Op<T1,op_min>& | | inline | |
| in) | | std::complex<T> | |
| | | op_min::direct_min(const diagview< std::complex<T> >& X) | |
| { | | { | |
| arma_extra_debug_sigprint(); | | arma_extra_debug_sigprint(); | |
| | | | |
|
| typedef typename std::complex<T> eT; | | const u32 X_n_elem = X.n_elem; | |
| isnt_same_type<eT, typename T1::elem_type>::check(); | | u32 index = 0; | |
| | | T min_val = (X_n_elem != 1) ? priv::most_pos<T>() : std::abs(X[0 | |
| const unwrap_check<T1> tmp(in.m, out); | | ]); | |
| const Mat<eT>& X = tmp.M; | | | |
| | | | |
| arma_debug_check( (X.n_elem == 0), "min(): given matrix has no elements" | | | |
| ); | | | |
| | | | |
| const u32 dim = in.aux_u32_a; | | | |
| arma_debug_check( (dim > 1), "min(): incorrect usage. dim must be 0 or 1" | | | |
| ); | | | |
| | | | |
| const u32 X_n_rows = X.n_rows; | | | |
| const u32 X_n_cols = X.n_cols; | | | |
| | | | |
| if(dim == 0) // column-wise min | | | |
| { | | | |
| arma_extra_debug_print("op_min::apply(), dim = 0"); | | | |
| | | | |
| out.set_size(1, X_n_cols); | | | |
| | | | |
|
| for(u32 col=0; col<X_n_cols; ++col) | | for(u32 i=0; i<X_n_elem; ++i) | |
| { | | | |
| out[col] = op_min::direct_min( X.colptr(col), X_n_rows ); | | | |
| } | | | |
| } | | | |
| else | | | |
| if(dim == 1) // row-wise min | | | |
| { | | { | |
|
| arma_extra_debug_print("op_min::apply(), dim = 1"); | | const T tmp_val = std::abs(X[i]); | |
| | | | |
| out.set_size(X_n_rows, 1); | | | |
| | | | |
|
| for(u32 row=0; row<X_n_rows; ++row) | | if(tmp_val < min_val) | |
| { | | { | |
|
| u32 index = 0; | | min_val = tmp_val; | |
| T min_val = (X_n_cols != 1) ? priv::most_pos<T>() : std::abs(X.at(r | | index = i; | |
| ow,0)); | | | |
| | | | |
| for(u32 col=0; col<X_n_cols; ++col) | | | |
| { | | | |
| const T tmp_val = std::abs(X.at(row,col)); | | | |
| | | | |
| if(tmp_val < min_val) | | | |
| { | | | |
| min_val = tmp_val; | | | |
| index = col; | | | |
| } | | | |
| } | | | |
| | | | |
| out[row] = X.at(row,index); | | | |
| } | | } | |
|
| | | | |
| } | | } | |
| | | | |
|
| | | return X[index]; | |
| } | | } | |
| | | | |
| //! @} | | //! @} | |
| | | | |
End of changes. 19 change blocks. |
| 77 lines changed or deleted | | 123 lines changed or added | |
|
| op_stddev_meat.hpp | | op_stddev_meat.hpp | |
|
| // Copyright (C) 2009-2010 NICTA (www.nicta.com.au) | | // Copyright (C) 2009-2011 NICTA (www.nicta.com.au) | |
| // Copyright (C) 2009-2010 Conrad Sanderson | | // Copyright (C) 2009-2011 Conrad Sanderson | |
| // | | // | |
| // This file is part of the Armadillo C++ library. | | // This file is part of the Armadillo C++ library. | |
| // It is provided without any warranty of fitness | | // It is provided without any warranty of fitness | |
| // for any purpose. You can redistribute this file | | // for any purpose. You can redistribute this file | |
| // and/or modify it under the terms of the GNU | | // and/or modify it under the terms of the GNU | |
| // Lesser General Public License (LGPL) as published | | // Lesser General Public License (LGPL) as published | |
| // by the Free Software Foundation, either version 3 | | // by the Free Software Foundation, either version 3 | |
| // of the License or (at your option) any later version. | | // of the License or (at your option) any later version. | |
| // (see http://www.opensource.org/licenses for more info) | | // (see http://www.opensource.org/licenses for more info) | |
| | | | |
| //! \addtogroup op_stddev | | //! \addtogroup op_stddev | |
| //! @{ | | //! @{ | |
| | | | |
| //! \brief | | //! \brief | |
| //! For each row or for each column, find the standard deviation. | | //! For each row or for each column, find the standard deviation. | |
| //! The result is stored in a dense matrix that has either one column or on
e row. | | //! The result is stored in a dense matrix that has either one column or on
e row. | |
| //! The dimension for which the standard deviations are found is set via th
e stddev() function. | | //! The dimension for which the standard deviations are found is set via th
e stddev() function. | |
|
| template<typename eT> | | template<typename T1> | |
| inline | | inline | |
| void | | void | |
|
| op_stddev::apply(Mat< typename get_pod_type<eT>::result >& out, const Mat<e
T>& X, const u32 norm_type, const u32 dim) | | op_stddev::apply(Mat<typename T1::pod_type>& out, const mtOp<typename T1::p
od_type, T1, op_stddev>& in) | |
| { | | { | |
| arma_extra_debug_sigprint(); | | arma_extra_debug_sigprint(); | |
| | | | |
|
| arma_debug_check( (X.n_elem == 0), "stddev(): given matrix has no element | | typedef typename T1::elem_type in_eT; | |
| s" ); | | typedef typename T1::pod_type out_eT; | |
| | | | |
|
| | | const unwrap_check_mixed<T1> tmp(in.m, out); | |
| | | const Mat<in_eT>& X = tmp.M; | |
| | | | |
| | | const u32 norm_type = in.aux_u32_a; | |
| | | const u32 dim = in.aux_u32_b; | |
| | | | |
| | | arma_debug_check( (X.n_elem == 0), "stddev(): given matrix has no element | |
| | | s" ); | |
| arma_debug_check( (norm_type > 1), "stddev(): incorrect usage. norm_type
must be 0 or 1"); | | arma_debug_check( (norm_type > 1), "stddev(): incorrect usage. norm_type
must be 0 or 1"); | |
| arma_debug_check( (dim > 1), "stddev(): incorrect usage. dim must b
e 0 or 1" ); | | arma_debug_check( (dim > 1), "stddev(): incorrect usage. dim must b
e 0 or 1" ); | |
| | | | |
|
| | | const u32 X_n_rows = X.n_rows; | |
| | | const u32 X_n_cols = X.n_cols; | |
| | | | |
| if(dim == 0) | | if(dim == 0) | |
| { | | { | |
| arma_extra_debug_print("op_stddev::apply(), dim = 0"); | | arma_extra_debug_print("op_stddev::apply(), dim = 0"); | |
| | | | |
|
| out.set_size(1, X.n_cols); | | out.set_size(1, X_n_cols); | |
| | | | |
|
| for(u32 col=0; col<X.n_cols; ++col) | | for(u32 col=0; col<X_n_cols; ++col) | |
| { | | { | |
|
| out[col] = std::sqrt( op_var::direct_var( X.colptr(col), X.n_rows, no
rm_type ) ); | | out[col] = std::sqrt( op_var::direct_var( X.colptr(col), X_n_rows, no
rm_type ) ); | |
| } | | } | |
| } | | } | |
| else | | else | |
| if(dim == 1) | | if(dim == 1) | |
| { | | { | |
| arma_extra_debug_print("op_stddev::apply(), dim = 1"); | | arma_extra_debug_print("op_stddev::apply(), dim = 1"); | |
| | | | |
|
| const u32 n_rows = X.n_rows; | | out.set_size(X_n_rows, 1); | |
| const u32 n_cols = X.n_cols; | | | |
| | | | |
|
| out.set_size(n_rows, 1); | | podarray<in_eT> tmp(X_n_cols); | |
| | | | |
|
| podarray<eT> tmp(n_cols); | | in_eT* tmp_mem = tmp.memptr(); | |
| | | | |
|
| eT* tmp_mem = tmp.memptr(); | | for(u32 row=0; row<X_n_rows; ++row) | |
| | | | |
| for(u32 row=0; row<n_rows; ++row) | | | |
| { | | { | |
|
| for(u32 col=0; col<n_cols; ++col) | | for(u32 col=0; col<X_n_cols; ++col) | |
| { | | { | |
| tmp_mem[col] = X.at(row,col); | | tmp_mem[col] = X.at(row,col); | |
| } | | } | |
| | | | |
|
| out[row] = std::sqrt( op_var::direct_var(tmp_mem, n_cols, norm_type)
); | | out[row] = std::sqrt( op_var::direct_var(tmp_mem, X_n_cols, norm_type
) ); | |
| } | | } | |
|
| | | | |
| } | | } | |
|
| | | | |
| } | | } | |
| | | | |
| //! @} | | //! @} | |
| | | | |
End of changes. 17 change blocks. |
| 20 lines changed or deleted | | 26 lines changed or added | |
|
| op_var_meat.hpp | | op_var_meat.hpp | |
| | | | |
| skipping to change at line 24 | | skipping to change at line 24 | |
| //! @{ | | //! @{ | |
| | | | |
| //! find the variance of an array | | //! find the variance of an array | |
| template<typename eT> | | template<typename eT> | |
| inline | | inline | |
| eT | | eT | |
| op_var::direct_var(const eT* const X, const u32 n_elem, const u32 norm_type
) | | op_var::direct_var(const eT* const X, const u32 n_elem, const u32 norm_type
) | |
| { | | { | |
| arma_extra_debug_sigprint(); | | arma_extra_debug_sigprint(); | |
| | | | |
|
| const eT div_val = (n_elem > 0) ? eT(n_elem) : eT(1); | | if(n_elem > 1) | |
| | | { | |
| | | const eT acc1 = op_mean::direct_mean(X, n_elem); | |
| | | | |
|
| const eT acc1 = arrayops::accumulate(X,n_elem) / div_val; | | eT acc2 = eT(0); | |
| | | eT acc3 = eT(0); | |
| | | | |
|
| eT acc2 = eT(0); | | u32 i,j; | |
| eT acc3 = eT(0); | | | |
| | | | |
|
| for(u32 i=0; i<n_elem; ++i) | | for(i=0, j=1; j<n_elem; i+=2, j+=2) | |
| { | | { | |
| const eT tmp = acc1 - X[i]; | | const eT Xi = X[i]; | |
| | | const eT Xj = X[j]; | |
| | | | |
|
| acc2 += tmp*tmp; | | const eT tmpi = acc1 - Xi; | |
| acc3 += tmp; | | const eT tmpj = acc1 - Xj; | |
| } | | | |
| | | acc2 += tmpi*tmpi + tmpj*tmpj; | |
| | | acc3 += tmpi + tmpj; | |
| | | } | |
| | | | |
| | | if(i < n_elem) | |
| | | { | |
| | | const eT Xi = X[i]; | |
| | | | |
|
| const eT norm_val = (norm_type == 0) ? ( (n_elem > 1) ? eT(n_elem-1) : eT | | const eT tmpi = acc1 - Xi; | |
| (1) ) : eT(n_elem); | | | |
| const eT var_val = (acc2 - acc3*acc3/div_val) / norm_val; | | | |
| | | | |
|
| return var_val; | | acc2 += tmpi*tmpi; | |
| | | acc3 += tmpi; | |
| | | } | |
| | | | |
| | | const eT norm_val = (norm_type == 0) ? eT(n_elem-1) : eT(n_elem); | |
| | | const eT var_val = (acc2 - acc3*acc3/eT(n_elem)) / norm_val; | |
| | | | |
| | | return arma_isfinite(var_val) ? var_val : op_var::direct_var_robust(X, | |
| | | n_elem, norm_type); | |
| | | } | |
| | | else | |
| | | { | |
| | | return eT(0); | |
| | | } | |
| } | | } | |
| | | | |
| //! find the variance of an array (version for complex numbers) | | //! find the variance of an array (version for complex numbers) | |
| template<typename T> | | template<typename T> | |
| inline | | inline | |
| T | | T | |
| op_var::direct_var(const std::complex<T>* const X, const u32 n_elem, const
u32 norm_type) | | op_var::direct_var(const std::complex<T>* const X, const u32 n_elem, const
u32 norm_type) | |
| { | | { | |
| arma_extra_debug_sigprint(); | | arma_extra_debug_sigprint(); | |
| | | | |
| typedef typename std::complex<T> eT; | | typedef typename std::complex<T> eT; | |
| | | | |
|
| const T div_val = (n_elem > 0) ? T(n_elem) : T(1); | | if(n_elem > 1) | |
| | | { | |
| const eT acc1 = arrayops::accumulate(X,n_elem) / div_val; | | const eT acc1 = op_mean::direct_mean(X, n_elem); | |
| | | | |
|
| T acc2 = T(0); | | T acc2 = T(0); | |
| eT acc3 = eT(0); | | eT acc3 = eT(0); | |
| | | | |
|
| for(u32 i=0; i<n_elem; ++i) | | for(u32 i=0; i<n_elem; ++i) | |
| { | | { | |
| const eT tmp = acc1 - X[i]; | | const eT tmp = acc1 - X[i]; | |
| | | | |
|
| acc2 += std::norm(tmp); | | acc2 += std::norm(tmp); | |
| acc3 += tmp; | | acc3 += tmp; | |
| } | | } | |
| | | | |
|
| const T norm_val = (norm_type == 0) ? ( (n_elem > 1) ? T(n_elem-1) : T(1) | | const T norm_val = (norm_type == 0) ? T(n_elem-1) : T(n_elem); | |
| ) : T(n_elem); | | const T var_val = (acc2 - std::norm(acc3)/T(n_elem)) / norm_val; | |
| const T var_val = (acc2 - std::norm(acc3)/div_val) / norm_val; | | | |
| | | | |
|
| return var_val; | | return arma_isfinite(var_val) ? var_val : op_var::direct_var_robust(X, | |
| | | n_elem, norm_type); | |
| | | } | |
| | | else | |
| | | { | |
| | | return T(0); | |
| | | } | |
| } | | } | |
| | | | |
| //! find the variance of a subview_row | | //! find the variance of a subview_row | |
| template<typename eT> | | template<typename eT> | |
| inline | | inline | |
| typename get_pod_type<eT>::result | | typename get_pod_type<eT>::result | |
| op_var::direct_var(const subview_row<eT>& X, const u32 norm_type) | | op_var::direct_var(const subview_row<eT>& X, const u32 norm_type) | |
| { | | { | |
| arma_extra_debug_sigprint(); | | arma_extra_debug_sigprint(); | |
| | | | |
| | | | |
| skipping to change at line 135 | | skipping to change at line 161 | |
| tmp_mem[i] = X[i]; | | tmp_mem[i] = X[i]; | |
| } | | } | |
| | | | |
| return op_var::direct_var(tmp_mem, n_elem, norm_type); | | return op_var::direct_var(tmp_mem, n_elem, norm_type); | |
| } | | } | |
| | | | |
| //! \brief | | //! \brief | |
| //! For each row or for each column, find the variance. | | //! For each row or for each column, find the variance. | |
| //! The result is stored in a dense matrix that has either one column or on
e row. | | //! The result is stored in a dense matrix that has either one column or on
e row. | |
| //! The dimension, for which the variances are found, is set via the var()
function. | | //! The dimension, for which the variances are found, is set via the var()
function. | |
|
| template<typename eT> | | template<typename T1> | |
| inline | | inline | |
| void | | void | |
|
| op_var::apply(Mat< typename get_pod_type<eT>::result >& out, const Mat<eT>&
X, const u32 norm_type, const u32 dim) | | op_var::apply(Mat<typename T1::pod_type>& out, const mtOp<typename T1::pod_
type, T1, op_var>& in) | |
| { | | { | |
| arma_extra_debug_sigprint(); | | arma_extra_debug_sigprint(); | |
| | | | |
|
| arma_debug_check( (X.n_elem == 0), "var(): given matrix has no elements" | | typedef typename T1::elem_type in_eT; | |
| ); | | typedef typename T1::pod_type out_eT; | |
| | | | |
| | | const unwrap_check_mixed<T1> tmp(in.m, out); | |
| | | const Mat<in_eT>& X = tmp.M; | |
| | | | |
| | | const u32 norm_type = in.aux_u32_a; | |
| | | const u32 dim = in.aux_u32_b; | |
| | | | |
|
| | | arma_debug_check( (X.n_elem == 0), "var(): given matrix has no elements"
); | |
| arma_debug_check( (norm_type > 1), "var(): incorrect usage. norm_type mus
t be 0 or 1"); | | arma_debug_check( (norm_type > 1), "var(): incorrect usage. norm_type mus
t be 0 or 1"); | |
| arma_debug_check( (dim > 1), "var(): incorrect usage. dim must be 0
or 1" ); | | arma_debug_check( (dim > 1), "var(): incorrect usage. dim must be 0
or 1" ); | |
| | | | |
|
| | | const u32 X_n_rows = X.n_rows; | |
| | | const u32 X_n_cols = X.n_cols; | |
| | | | |
| if(dim == 0) | | if(dim == 0) | |
| { | | { | |
| arma_extra_debug_print("op_var::apply(), dim = 0"); | | arma_extra_debug_print("op_var::apply(), dim = 0"); | |
| | | | |
|
| out.set_size(1, X.n_cols); | | out.set_size(1, X_n_cols); | |
| | | | |
|
| for(u32 col=0; col<X.n_cols; ++col) | | for(u32 col=0; col<X_n_cols; ++col) | |
| { | | { | |
|
| out[col] = op_var::direct_var( X.colptr(col), X.n_rows, norm_type ); | | out[col] = op_var::direct_var( X.colptr(col), X_n_rows, norm_type ); | |
| } | | } | |
| } | | } | |
| else | | else | |
| if(dim == 1) | | if(dim == 1) | |
| { | | { | |
| arma_extra_debug_print("op_var::apply(), dim = 1"); | | arma_extra_debug_print("op_var::apply(), dim = 1"); | |
| | | | |
|
| const u32 n_rows = X.n_rows; | | out.set_size(X_n_rows, 1); | |
| const u32 n_cols = X.n_cols; | | | |
| | | | |
| out.set_size(n_rows, 1); | | | |
| | | | |
|
| podarray<eT> tmp(n_cols); | | podarray<in_eT> tmp(X_n_cols); | |
| | | | |
|
| eT* tmp_mem = tmp.memptr(); | | in_eT* tmp_mem = tmp.memptr(); | |
| | | | |
|
| for(u32 row=0; row<n_rows; ++row) | | for(u32 row=0; row<X_n_rows; ++row) | |
| { | | { | |
|
| for(u32 col=0; col<n_cols; ++col) | | for(u32 col=0; col<X_n_cols; ++col) | |
| { | | { | |
| tmp_mem[col] = X.at(row,col); | | tmp_mem[col] = X.at(row,col); | |
| } | | } | |
| | | | |
|
| out[row] = op_var::direct_var(tmp_mem, n_cols, norm_type); | | out[row] = op_var::direct_var(tmp_mem, X_n_cols, norm_type); | |
| | | } | |
| | | } | |
| | | } | |
| | | | |
| | | //! find the variance of an array (robust but slow) | |
| | | template<typename eT> | |
| | | inline | |
| | | eT | |
| | | op_var::direct_var_robust(const eT* const X, const u32 n_elem, const u32 no | |
| | | rm_type) | |
| | | { | |
| | | arma_extra_debug_sigprint(); | |
| | | | |
| | | if(n_elem > 1) | |
| | | { | |
| | | eT r_mean = X[0]; | |
| | | eT r_var = eT(0); | |
| | | | |
| | | for(u32 i=1; i<n_elem; ++i) | |
| | | { | |
| | | const eT tmp = X[i] - r_mean; | |
| | | const eT i_plus_1 = eT(i+1); | |
| | | | |
| | | r_var = eT(i-1)/eT(i) * r_var + (tmp*tmp)/i_plus_1; | |
| | | | |
| | | r_mean = r_mean + tmp/i_plus_1; | |
| | | } | |
| | | | |
| | | return (norm_type == 0) ? r_var : (eT(n_elem-1)/eT(n_elem)) * r_var; | |
| | | } | |
| | | else | |
| | | { | |
| | | return eT(0); | |
| | | } | |
| | | } | |
| | | | |
| | | //! find the variance of an array (version for complex numbers) (robust but | |
| | | slow) | |
| | | template<typename T> | |
| | | inline | |
| | | T | |
| | | op_var::direct_var_robust(const std::complex<T>* const X, const u32 n_elem, | |
| | | const u32 norm_type) | |
| | | { | |
| | | arma_extra_debug_sigprint(); | |
| | | | |
| | | typedef typename std::complex<T> eT; | |
| | | | |
| | | if(n_elem > 1) | |
| | | { | |
| | | eT r_mean = X[0]; | |
| | | T r_var = T(0); | |
| | | | |
| | | for(u32 i=1; i<n_elem; ++i) | |
| | | { | |
| | | const eT tmp = X[i] - r_mean; | |
| | | const T i_plus_1 = T(i+1); | |
| | | | |
| | | r_var = T(i-1)/T(i) * r_var + std::norm(tmp)/i_plus_1; | |
| | | | |
| | | r_mean = r_mean + tmp/i_plus_1; | |
| } | | } | |
|
| | | | |
| | | return (norm_type == 0) ? r_var : (T(n_elem-1)/T(n_elem)) * r_var; | |
| | | } | |
| | | else | |
| | | { | |
| | | return T(0); | |
| } | | } | |
| } | | } | |
| | | | |
| //! @} | | //! @} | |
| | | | |
End of changes. 28 change blocks. |
| 45 lines changed or deleted | | 145 lines changed or added | |
|