diff --git a/include/armadillo_bits/glue_times_meat.hpp b/include/armadillo_bits/glue_times_meat.hpp index 92d69599..e3c5834d 100644 --- a/include/armadillo_bits/glue_times_meat.hpp +++ b/include/armadillo_bits/glue_times_meat.hpp @@ -252,14 +252,14 @@ glue_times_redirect3_helper::apply(Mat& out, const arma_conform_check( (A.is_square() == false), "inv(): given matrix must be square sized" ); - const partial_unwrap tmp2(X.A.B); - const partial_unwrap tmp3(X.B ); + const partial_unwrap U2(X.A.B); + const partial_unwrap U3(X.B ); - const typename partial_unwrap::stored_type& B = tmp2.M; - const typename partial_unwrap::stored_type& C = tmp3.M; + const typename partial_unwrap::stored_type& B = U2.M; + const typename partial_unwrap::stored_type& C = U3.M; constexpr bool use_alpha = partial_unwrap::do_times || partial_unwrap::do_times; - const eT alpha = use_alpha ? (tmp2.get_val() * tmp3.get_val()) : eT(0); + const eT alpha = use_alpha ? (U2.get_val() * U3.get_val()) : eT(0); Mat BC; @@ -306,8 +306,8 @@ glue_times_redirect3_helper::apply(Mat& out, const arma_conform_check( (B.is_square() == false), "inv(): given matrix must be square sized" ); - const unwrap C_tmp(X.B); - const Mat& C = C_tmp.M; + const quasi_unwrap U3(X.B); + const Mat& C = U3.M; arma_conform_assert_mul_size(B, C, "matrix multiplication"); @@ -330,21 +330,25 @@ glue_times_redirect3_helper::apply(Mat& out, const return; } - const partial_unwrap_check tmp1(X.A.A, out); + const partial_unwrap U1(X.A.A); - const typename partial_unwrap_check::stored_type& A = tmp1.M; + const typename partial_unwrap::stored_type& A = U1.M; - constexpr bool use_alpha = partial_unwrap_check::do_times; - const eT alpha = use_alpha ? tmp1.get_val() : eT(0); + constexpr bool use_alpha = partial_unwrap::do_times; + const eT alpha = use_alpha ? U1.get_val() : eT(0); - glue_times::apply - < - eT, - partial_unwrap_check::do_trans, - false, - partial_unwrap_check::do_times - > - (out, A, solve_result, alpha); + if(U1.is_alias(out)) + { + Mat tmp; + + glue_times::apply::do_trans, false, partial_unwrap::do_times>(tmp, A, solve_result, alpha); + + out.steal_mem(tmp); + } + else + { + glue_times::apply::do_trans, false, partial_unwrap::do_times>(out, A, solve_result, alpha); + } return; }