diff --git a/ChangeLog b/ChangeLog index a77772fc..780d0342 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,12 @@ +2026-11-19 Dirk Eddelbuettel + + * DESCRIPTION (Version, Date): RcppArmadillo 15.7.91-1 + * inst/NEWS.Rd: Idem + * configure.ac: Idem + * configure: Idem + + * inst/include/armadillo*: Armadillo 15.7.91 aka 15.8-rc1 + 2026-10-16 Dirk Eddelbuettel * inst/include/armadillo_bits/*: Armadillo 15.6.2 diff --git a/DESCRIPTION b/DESCRIPTION index 8c698ef7..18cb96f5 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -1,8 +1,8 @@ Package: RcppArmadillo Type: Package Title: 'Rcpp' Integration for the 'Armadillo' Templated Linear Algebra Library -Version: 15.6.2-1 -Date: 2026-10-16 +Version: 15.7.91-1 +Date: 2026-11-19 Authors@R: c(person("Dirk", "Eddelbuettel", role = c("aut", "cre"), email = "edd@debian.org", comment = c(ORCID = "0000-0001-6419-907X")), person("Romain", "Francois", role = "aut", diff --git a/configure b/configure index 6c1600e6..4acf44f7 100755 --- a/configure +++ b/configure @@ -1,6 +1,6 @@ #! /bin/sh # Guess values for system-dependent variables and create Makefiles. -# Generated by GNU Autoconf 2.72 for RcppArmadillo 15.6.2-1. +# Generated by GNU Autoconf 2.72 for RcppArmadillo 15.7.91-1. # # Report bugs to . # @@ -603,8 +603,8 @@ MAKEFLAGS= # Identity of this package. PACKAGE_NAME='RcppArmadillo' PACKAGE_TARNAME='rcpparmadillo' -PACKAGE_VERSION='15.6.2-1' -PACKAGE_STRING='RcppArmadillo 15.6.2-1' +PACKAGE_VERSION='15.7.91-1' +PACKAGE_STRING='RcppArmadillo 15.7.91-1' PACKAGE_BUGREPORT='edd@debian.org' PACKAGE_URL='' @@ -1223,7 +1223,7 @@ if test "$ac_init_help" = "long"; then # Omit some internal or obsolete options to make the list less imposing. # This message is too long to be a string in the A/UX 3.1 sh. cat <<_ACEOF -'configure' configures RcppArmadillo 15.6.2-1 to adapt to many kinds of systems. +'configure' configures RcppArmadillo 15.7.91-1 to adapt to many kinds of systems. Usage: $0 [OPTION]... [VAR=VALUE]... @@ -1285,7 +1285,7 @@ fi if test -n "$ac_init_help"; then case $ac_init_help in - short | recursive ) echo "Configuration of RcppArmadillo 15.6.2-1:";; + short | recursive ) echo "Configuration of RcppArmadillo 15.7.91-1:";; esac cat <<\_ACEOF @@ -1366,7 +1366,7 @@ fi test -n "$ac_init_help" && exit $ac_status if $ac_init_version; then cat <<\_ACEOF -RcppArmadillo configure 15.6.2-1 +RcppArmadillo configure 15.7.91-1 generated by GNU Autoconf 2.72 Copyright (C) 2023 Free Software Foundation, Inc. @@ -1482,7 +1482,7 @@ cat >config.log <<_ACEOF This file contains any messages produced by compilers while running configure, to aid debugging if configure makes a mistake. -It was created by RcppArmadillo $as_me 15.6.2-1, which was +It was created by RcppArmadillo $as_me 15.7.91-1, which was generated by GNU Autoconf 2.72. Invocation command line was $ $0$ac_configure_args_raw @@ -3946,7 +3946,7 @@ cat >>$CONFIG_STATUS <<\_ACEOF || ac_write_fail=1 # report actual input values of CONFIG_FILES etc. instead of their # values after options handling. ac_log=" -This file was extended by RcppArmadillo $as_me 15.6.2-1, which was +This file was extended by RcppArmadillo $as_me 15.7.91-1, which was generated by GNU Autoconf 2.72. Invocation command line was CONFIG_FILES = $CONFIG_FILES @@ -4001,7 +4001,7 @@ ac_cs_config_escaped=`printf "%s\n" "$ac_cs_config" | sed "s/^ //; s/'/'\\\\\\\\ cat >>$CONFIG_STATUS <<_ACEOF || ac_write_fail=1 ac_cs_config='$ac_cs_config_escaped' ac_cs_version="\\ -RcppArmadillo config.status 15.6.2-1 +RcppArmadillo config.status 15.7.91-1 configured by $0, generated by GNU Autoconf 2.72, with options \\"\$ac_cs_config\\" diff --git a/configure.ac b/configure.ac index 196c38c2..2d0b96bb 100644 --- a/configure.ac +++ b/configure.ac @@ -11,7 +11,7 @@ AC_PREREQ([2.69]) ## Process this file with autoconf to produce a configure script. -AC_INIT([RcppArmadillo],[15.6.2-1],[edd@debian.org]) +AC_INIT([RcppArmadillo],[15.7.91-1],[edd@debian.org]) ## Set R_HOME, respecting an environment variable if one is set : ${R_HOME=$(R RHOME)} diff --git a/inst/NEWS.Rd b/inst/NEWS.Rd index 810e6bcf..8f2005d7 100644 --- a/inst/NEWS.Rd +++ b/inst/NEWS.Rd @@ -3,6 +3,20 @@ \newcommand{\ghpr}{\href{https://github.com/RcppCore/RcppArmadillo/pull/#1}{##1}} \newcommand{\ghit}{\href{https://github.com/RcppCore/RcppArmadillo/issues/#1}{##1}} +\section{Changes in RcppArmadillo version 15.7.91-1 [github-only] (2026-11-19)}{ + \itemize{ + \item Upgraded to Armadillo release 15.7.91 (15.8-rc1) + \itemize{ + \item Added \code{solve_factoriser} class for reusing matrix factorisation to + solve systems of linear equations + \item Added \code{solve_lse()} for solving linear least squares with + equality constraints + \item More efficient handling of \code{spones()} + \item Reworked detection of aliasing for cubes + } + } +} + \section{Changes in RcppArmadillo version 15.6.2-1 (2026-10-16)}{ \itemize{ \item Upgraded to Armadillo release 15.6.2 (Medium Roast Cortado) diff --git a/inst/include/armadillo b/inst/include/armadillo index d03295d8..c5cc6db0 100644 --- a/inst/include/armadillo +++ b/inst/include/armadillo @@ -384,6 +384,8 @@ namespace arma #include "armadillo_bits/glue_powext_bones.hpp" #include "armadillo_bits/glue_cubemul_bones.hpp" + #include "armadillo_bits/solve_factoriser_bones.hpp" + #include "armadillo_bits/gmm_misc_bones.hpp" #include "armadillo_bits/gmm_diag_bones.hpp" #include "armadillo_bits/gmm_full_bones.hpp" @@ -403,6 +405,7 @@ namespace arma #include "armadillo_bits/spop_relational_bones.hpp" #include "armadillo_bits/spop_omit_bones.hpp" #include "armadillo_bits/spop_accu_bones.hpp" + #include "armadillo_bits/spop_spones_bones.hpp" #include "armadillo_bits/spglue_plus_bones.hpp" #include "armadillo_bits/spglue_minus_bones.hpp" @@ -541,6 +544,7 @@ namespace arma #include "armadillo_bits/fn_qr.hpp" #include "armadillo_bits/fn_svd.hpp" #include "armadillo_bits/fn_solve.hpp" + #include "armadillo_bits/fn_solve_lse.hpp" #include "armadillo_bits/fn_repmat.hpp" #include "armadillo_bits/fn_repelem.hpp" #include "armadillo_bits/fn_reshape.hpp" @@ -846,6 +850,8 @@ namespace arma #include "armadillo_bits/glue_powext_meat.hpp" #include "armadillo_bits/glue_cubemul_meat.hpp" + #include "armadillo_bits/solve_factoriser_meat.hpp" + #include "armadillo_bits/gmm_misc_meat.hpp" #include "armadillo_bits/gmm_diag_meat.hpp" #include "armadillo_bits/gmm_full_meat.hpp" @@ -865,6 +871,7 @@ namespace arma #include "armadillo_bits/spop_relational_meat.hpp" #include "armadillo_bits/spop_omit_meat.hpp" #include "armadillo_bits/spop_accu_meat.hpp" + #include "armadillo_bits/spop_spones_meat.hpp" #include "armadillo_bits/spglue_plus_meat.hpp" #include "armadillo_bits/spglue_minus_meat.hpp" diff --git a/inst/include/armadillo_bits/Cube_bones.hpp b/inst/include/armadillo_bits/Cube_bones.hpp index fd3eb2d0..fdbb23ba 100644 --- a/inst/include/armadillo_bits/Cube_bones.hpp +++ b/inst/include/armadillo_bits/Cube_bones.hpp @@ -561,4 +561,8 @@ class Cube_aux +template class Cube_noalias : public Cube {}; + + + //! @} diff --git a/inst/include/armadillo_bits/Cube_meat.hpp b/inst/include/armadillo_bits/Cube_meat.hpp index 2a838cda..ebf9ed36 100644 --- a/inst/include/armadillo_bits/Cube_meat.hpp +++ b/inst/include/armadillo_bits/Cube_meat.hpp @@ -2772,7 +2772,7 @@ Cube::Cube(const OpCube& X) arma_type_check(( is_same_type< eT, typename T1::elem_type >::no )); - op_type::apply(*this, X); + op_type::apply(static_cast< Cube_noalias& >(*this), X); } @@ -3067,7 +3067,7 @@ Cube::Cube(const mtOpCube& X) { arma_debug_sigprint_this(this); - op_type::apply(*this, X); + op_type::apply(static_cast< Cube_noalias& >(*this), X); } @@ -3163,7 +3163,10 @@ Cube::Cube(const GlueCube& X) { arma_debug_sigprint_this(this); - (*this).operator=(X); + arma_type_check(( is_same_type< eT, typename T1::elem_type >::no )); + arma_type_check(( is_same_type< eT, typename T2::elem_type >::no )); + + glue_type::apply(static_cast< Cube_noalias& >(*this), X); } @@ -3420,7 +3423,7 @@ Cube::Cube(const mtGlueCube& X) { arma_debug_sigprint_this(this); - glue_type::apply(*this, X); + glue_type::apply(static_cast< Cube_noalias& >(*this), X); } diff --git a/inst/include/armadillo_bits/arma_str.hpp b/inst/include/armadillo_bits/arma_str.hpp index 0d7ab0f6..c107f1e3 100644 --- a/inst/include/armadillo_bits/arma_str.hpp +++ b/inst/include/armadillo_bits/arma_str.hpp @@ -81,37 +81,46 @@ namespace arma_str template - class basic_format + class format_arg { public: const T1& A; const T2& B; - inline basic_format(const T1& in_A, const T2& in_B) : A(in_A) , B(in_B) { } + inline format_arg(const T1& in_A, const T2& in_B) : A(in_A) , B(in_B) { } private: - basic_format(); + format_arg(); }; template inline - basic_format< format, T2 > + format_arg< format, T2 > operator% (const format& X, const T2& arg) { - return basic_format< format, T2 >(X, arg); + return format_arg< format, T2 >(X, arg); } template inline - basic_format< basic_format, T3 > - operator% (const basic_format& X, const T3& arg) + format_arg< format_arg, T3 > + operator% (const format_arg& X, const T3& arg) { - return basic_format< basic_format, T3 >(X, arg); + return format_arg< format_arg, T3 >(X, arg); + } + + + + inline + std::string + str(const format& X) + { + return X.fmt; } @@ -119,34 +128,38 @@ namespace arma_str template inline std::string - str(const basic_format< format, T2>& X) + str(const format_arg< format, T2>& X) { - std::string out; char_buffer buf; - bool status = false; + bool printed = false; + bool resized = false; - while(status == false) + int required_size = 0; + + while(printed == false) { - int required_size = (std::snprintf)(buf.mem, size_t(buf.n_chars), X.A.fmt.c_str(), X.B); + required_size = (std::snprintf)(buf.mem, size_t(buf.n_chars), X.A.fmt.c_str(), X.B); if(required_size < 0) { break; } - if(uword(required_size) >= buf.n_chars) + if(uword(required_size) < buf.n_chars) { - if(buf.n_chars > char_buffer::n_chars_prealloc) { break; } - - buf.set_size(1 + uword(required_size)); + printed = true; } else { - status = true; + if(resized) { break; } + + buf.set_size(uword(required_size) + uword(1)); // extra character required for the terminating null + + resized = true; } - - if(status) { out = buf.mem; } } - return out; + const std::size_t len = (printed) ? std::size_t(required_size) : std::size_t(0); + + return std::string(buf.mem, len); } @@ -154,34 +167,38 @@ namespace arma_str template inline std::string - str(const basic_format< basic_format< format, T2>, T3>& X) + str(const format_arg< format_arg< format, T2>, T3>& X) { char_buffer buf; - std::string out; - bool status = false; + bool printed = false; + bool resized = false; - while(status == false) + int required_size = 0; + + while(printed == false) { - int required_size = (std::snprintf)(buf.mem, size_t(buf.n_chars), X.A.A.fmt.c_str(), X.A.B, X.B); + required_size = (std::snprintf)(buf.mem, size_t(buf.n_chars), X.A.A.fmt.c_str(), X.A.B, X.B); if(required_size < 0) { break; } - if(uword(required_size) >= buf.n_chars) + if(uword(required_size) < buf.n_chars) { - if(buf.n_chars > char_buffer::n_chars_prealloc) { break; } - - buf.set_size(1 + uword(required_size)); + printed = true; } else { - status = true; + if(resized) { break; } + + buf.set_size(uword(required_size) + uword(1)); // extra character required for the terminating null + + resized = true; } - - if(status) { out = buf.mem; } } - return out; + const std::size_t len = (printed) ? std::size_t(required_size) : std::size_t(0); + + return std::string(buf.mem, len); } @@ -189,34 +206,38 @@ namespace arma_str template inline std::string - str(const basic_format< basic_format< basic_format< format, T2>, T3>, T4>& X) + str(const format_arg< format_arg< format_arg< format, T2>, T3>, T4>& X) { char_buffer buf; - std::string out; - bool status = false; + bool printed = false; + bool resized = false; + + int required_size = 0; - while(status == false) + while(printed == false) { - int required_size = (std::snprintf)(buf.mem, size_t(buf.n_chars), X.A.A.A.fmt.c_str(), X.A.A.B, X.A.B, X.B); + required_size = (std::snprintf)(buf.mem, size_t(buf.n_chars), X.A.A.A.fmt.c_str(), X.A.A.B, X.A.B, X.B); if(required_size < 0) { break; } - if(uword(required_size) >= buf.n_chars) + if(uword(required_size) < buf.n_chars) { - if(buf.n_chars > char_buffer::n_chars_prealloc) { break; } - - buf.set_size(1 + uword(required_size)); + printed = true; } else { - status = true; + if(resized) { break; } + + buf.set_size(uword(required_size) + uword(1)); // extra character required for the terminating null + + resized = true; } - - if(status) { out = buf.mem; } } - return out; + const std::size_t len = (printed) ? std::size_t(required_size) : std::size_t(0); + + return std::string(buf.mem, len); } @@ -224,34 +245,38 @@ namespace arma_str template inline std::string - str(const basic_format< basic_format< basic_format< basic_format< format, T2>, T3>, T4>, T5>& X) + str(const format_arg< format_arg< format_arg< format_arg< format, T2>, T3>, T4>, T5>& X) { char_buffer buf; - std::string out; - bool status = false; + bool printed = false; + bool resized = false; + + int required_size = 0; - while(status == false) + while(printed == false) { - int required_size = (std::snprintf)(buf.mem, size_t(buf.n_chars), X.A.A.A.A.fmt.c_str(), X.A.A.A.B, X.A.A.B, X.A.B, X.B); + required_size = (std::snprintf)(buf.mem, size_t(buf.n_chars), X.A.A.A.A.fmt.c_str(), X.A.A.A.B, X.A.A.B, X.A.B, X.B); if(required_size < 0) { break; } - if(uword(required_size) >= buf.n_chars) + if(uword(required_size) < buf.n_chars) { - if(buf.n_chars > char_buffer::n_chars_prealloc) { break; } - - buf.set_size(1 + uword(required_size)); + printed = true; } else { - status = true; + if(resized) { break; } + + buf.set_size(uword(required_size) + uword(1)); // extra character required for the terminating null + + resized = true; } - - if(status) { out = buf.mem; } } - return out; + const std::size_t len = (printed) ? std::size_t(required_size) : std::size_t(0); + + return std::string(buf.mem, len); } @@ -259,34 +284,38 @@ namespace arma_str template inline std::string - str(const basic_format< basic_format< basic_format< basic_format< basic_format< format, T2>, T3>, T4>, T5>, T6>& X) + str(const format_arg< format_arg< format_arg< format_arg< format_arg< format, T2>, T3>, T4>, T5>, T6>& X) { char_buffer buf; - std::string out; - bool status = false; + bool printed = false; + bool resized = false; + + int required_size = 0; - while(status == false) + while(printed == false) { - int required_size = (std::snprintf)(buf.mem, size_t(buf.n_chars), X.A.A.A.A.A.fmt.c_str(), X.A.A.A.A.B, X.A.A.A.B, X.A.A.B, X.A.B, X.B); + required_size = (std::snprintf)(buf.mem, size_t(buf.n_chars), X.A.A.A.A.A.fmt.c_str(), X.A.A.A.A.B, X.A.A.A.B, X.A.A.B, X.A.B, X.B); if(required_size < 0) { break; } - if(uword(required_size) >= buf.n_chars) + if(uword(required_size) < buf.n_chars) { - if(buf.n_chars > char_buffer::n_chars_prealloc) { break; } - - buf.set_size(1 + uword(required_size)); + printed = true; } else { - status = true; + if(resized) { break; } + + buf.set_size(uword(required_size) + uword(1)); // extra character required for the terminating null + + resized = true; } - - if(status) { out = buf.mem; } } - return out; + const std::size_t len = (printed) ? std::size_t(required_size) : std::size_t(0); + + return std::string(buf.mem, len); } @@ -294,34 +323,116 @@ namespace arma_str template inline std::string - str(const basic_format< basic_format< basic_format< basic_format< basic_format< basic_format< format, T2>, T3>, T4>, T5>, T6>, T7>& X) + str(const format_arg< format_arg< format_arg< format_arg< format_arg< format_arg< format, T2>, T3>, T4>, T5>, T6>, T7>& X) { char_buffer buf; - std::string out; - bool status = false; + bool printed = false; + bool resized = false; + + int required_size = 0; - while(status == false) + while(printed == false) { - int required_size = (std::snprintf)(buf.mem, size_t(buf.n_chars), X.A.A.A.A.A.A.fmt.c_str(), X.A.A.A.A.A.B, X.A.A.A.A.B, X.A.A.A.B, X.A.A.B, X.A.B, X.B); + required_size = (std::snprintf)(buf.mem, size_t(buf.n_chars), X.A.A.A.A.A.A.fmt.c_str(), X.A.A.A.A.A.B, X.A.A.A.A.B, X.A.A.A.B, X.A.A.B, X.A.B, X.B); if(required_size < 0) { break; } - if(uword(required_size) >= buf.n_chars) + if(uword(required_size) < buf.n_chars) + { + printed = true; + } + else { - if(buf.n_chars > char_buffer::n_chars_prealloc) { break; } + if(resized) { break; } - buf.set_size(1 + uword(required_size)); + buf.set_size(uword(required_size) + uword(1)); // extra character required for the terminating null + + resized = true; + } + } + + const std::size_t len = (printed) ? std::size_t(required_size) : std::size_t(0); + + return std::string(buf.mem, len); + } + + + + template + inline + std::string + str(const format_arg< format_arg< format_arg< format_arg< format_arg< format_arg< format_arg< format, T2>, T3>, T4>, T5>, T6>, T7>, T8>& X) + { + char_buffer buf; + + bool printed = false; + bool resized = false; + + int required_size = 0; + + while(printed == false) + { + required_size = (std::snprintf)(buf.mem, size_t(buf.n_chars), X.A.A.A.A.A.A.A.fmt.c_str(), X.A.A.A.A.A.A.B, X.A.A.A.A.A.B, X.A.A.A.A.B, X.A.A.A.B, X.A.A.B, X.A.B, X.B); + + if(required_size < 0) { break; } + + if(uword(required_size) < buf.n_chars) + { + printed = true; } else { - status = true; + if(resized) { break; } + + buf.set_size(uword(required_size) + uword(1)); // extra character required for the terminating null + + resized = true; } + } + + const std::size_t len = (printed) ? std::size_t(required_size) : std::size_t(0); + + return std::string(buf.mem, len); + } + + + + template + inline + std::string + str(const format_arg< format_arg< format_arg< format_arg< format_arg< format_arg< format_arg< format_arg< format, T2>, T3>, T4>, T5>, T6>, T7>, T8>, T9>& X) + { + char_buffer buf; + + bool printed = false; + bool resized = false; + + int required_size = 0; + + while(printed == false) + { + required_size = (std::snprintf)(buf.mem, size_t(buf.n_chars), X.A.A.A.A.A.A.A.A.fmt.c_str(), X.A.A.A.A.A.A.A.B, X.A.A.A.A.A.A.B, X.A.A.A.A.A.B, X.A.A.A.A.B, X.A.A.A.B, X.A.A.B, X.A.B, X.B); + + if(required_size < 0) { break; } - if(status) { out = buf.mem; } + if(uword(required_size) < buf.n_chars) + { + printed = true; + } + else + { + if(resized) { break; } + + buf.set_size(uword(required_size) + uword(1)); // extra character required for the terminating null + + resized = true; + } } - return out; + const std::size_t len = (printed) ? std::size_t(required_size) : std::size_t(0); + + return std::string(buf.mem, len); } @@ -344,7 +455,7 @@ namespace arma_str //template<> template - struct format_metaprog< basic_format > + struct format_metaprog< format_arg > { static constexpr uword depth = 1 + format_metaprog::depth; @@ -363,9 +474,9 @@ namespace arma_str template inline std::string - str(const basic_format& X) + str(const format_arg& X) { - return format_metaprog< basic_format >::get_fmt(X.A); + return format_metaprog< format_arg >::get_fmt(X.A); } @@ -373,7 +484,7 @@ namespace arma_str template inline std::ostream& - operator<< (std::ostream& o, const basic_format& X) + operator<< (std::ostream& o, const format_arg& X) { o << str(X); return o; @@ -387,11 +498,16 @@ namespace arma_str template<> struct char_only { typedef char result; }; template - struct basic_format_only { }; + struct format_arg_only { }; template - struct basic_format_only< basic_format > { typedef basic_format result; }; + struct format_arg_only< format_arg > { typedef format_arg result; }; + + template + struct format_only { }; + template<> + struct format_only< format > { typedef format result; }; template @@ -424,7 +540,20 @@ namespace arma_str inline static std::string - str_wrapper(const T1& x, const typename basic_format_only::result* junk = nullptr) + str_wrapper(const T1& x, const typename format_arg_only::result* junk = nullptr) + { + arma_ignore(junk); + + return str(x); + } + + + + template + inline + static + std::string + str_wrapper(const T1& x, const typename format_only::result* junk = nullptr) { arma_ignore(junk); diff --git a/inst/include/armadillo_bits/arma_version.hpp b/inst/include/armadillo_bits/arma_version.hpp index fff29c1a..69f80a29 100644 --- a/inst/include/armadillo_bits/arma_version.hpp +++ b/inst/include/armadillo_bits/arma_version.hpp @@ -22,9 +22,9 @@ #define ARMA_VERSION_MAJOR 15 -#define ARMA_VERSION_MINOR 6 -#define ARMA_VERSION_PATCH 2 -#define ARMA_VERSION_NAME "Medium Roast Cortado" +#define ARMA_VERSION_MINOR 7 +#define ARMA_VERSION_PATCH 91 +#define ARMA_VERSION_NAME "15.8-RC1" diff --git a/inst/include/armadillo_bits/auxlib_bones.hpp b/inst/include/armadillo_bits/auxlib_bones.hpp index 44857cdb..abe900fd 100644 --- a/inst/include/armadillo_bits/auxlib_bones.hpp +++ b/inst/include/armadillo_bits/auxlib_bones.hpp @@ -346,6 +346,13 @@ struct auxlib inline static bool solve_tridiag_fast(Mat& out, const Mat& A, const Base& B_expr); + // + // solve_lse + + template + inline static bool solve_lse(Mat& out, Mat& A, Mat& B, Mat& C, Mat& D); + + // // Schur decomposition diff --git a/inst/include/armadillo_bits/auxlib_meat.hpp b/inst/include/armadillo_bits/auxlib_meat.hpp index 87af2cba..4acdc91b 100644 --- a/inst/include/armadillo_bits/auxlib_meat.hpp +++ b/inst/include/armadillo_bits/auxlib_meat.hpp @@ -6113,6 +6113,69 @@ auxlib::solve_tridiag_fast(Mat& out, const Mat +inline +bool +auxlib::solve_lse(Mat& out, Mat& A, Mat& B, Mat& C, Mat& D) + { + arma_debug_sigprint(); + + // NOTE: assuming matrix sizes have already been checked for conformance + // NOTE: assuming 'out' is already set to the correct size + + #if defined(ARMA_USE_LAPACK) + { + arma_conform_assert_blas_size(out); + arma_conform_assert_blas_size(A); + arma_conform_assert_blas_size(B); + arma_conform_assert_blas_size(C); + arma_conform_assert_blas_size(D); + + blas_int m = blas_int(A.n_rows); + blas_int n = blas_int(A.n_cols); + blas_int p = blas_int(B.n_rows); + blas_int lda = blas_int(A.n_rows); + blas_int ldb = blas_int(B.n_rows); + blas_int info = 0; + + blas_int lwork_min = (std::max)( blas_int(podarray_prealloc_n_elem::val), blas_int(m + n + p) ); + + eT work_query[2] = {}; + blas_int lwork_query = -1; + + arma_debug_print("lapack::gglse()"); + lapack::gglse(&m, &n, &p, A.memptr(), &lda, B.memptr(), &ldb, C.memptr(), D.memptr(), out.memptr(), &work_query[0], &lwork_query, &info); + + if(info != 0) { return false; } + + blas_int lwork_proposed = static_cast( access::tmp_real(work_query[0]) ); + + blas_int lwork = (std::max)(lwork_min, lwork_proposed); + + podarray work( static_cast(lwork) ); + + arma_debug_print("lapack::gglse()"); + lapack::gglse(&m, &n, &p, A.memptr(), &lda, B.memptr(), &ldb, C.memptr(), D.memptr(), out.memptr(), work.memptr(), &lwork, &info); + + // NOTE: A, B, C, D are overwritten by lapack::gglse() + + return (info == 0); + } + #else + { + arma_ignore(out); + arma_ignore(A); + arma_ignore(B); + arma_ignore(C); + arma_ignore(D); + return false; + } + #endif + } + + + // // Schur decomposition diff --git a/inst/include/armadillo_bits/def_lapack.hpp b/inst/include/armadillo_bits/def_lapack.hpp index 62b966dc..7c2485f0 100644 --- a/inst/include/armadillo_bits/def_lapack.hpp +++ b/inst/include/armadillo_bits/def_lapack.hpp @@ -298,6 +298,11 @@ #define arma_cgebal cgebal #define arma_zgebal zgebal + #define arma_sgglse sgglse + #define arma_dgglse dgglse + #define arma_cgglse cgglse + #define arma_zgglse zgglse + #else #define arma_sgetrf SGETRF @@ -563,6 +568,11 @@ #define arma_cgebal CGEBAL #define arma_zgebal ZGEBAL + #define arma_sgglse SGGLSE + #define arma_dgglse DGGLSE + #define arma_cgglse CGGLSE + #define arma_zgglse ZGGLSE + #endif @@ -624,8 +634,8 @@ extern "C" void arma_fortran(arma_dgeevx)(const char* balanc, const char* jobvl, const char* jobvr, const char* sense, const blas_int* n, double* a, const blas_int* lda, double* wr, double* wi, double* vl, const blas_int* ldvl, double* vr, const blas_int* ldvr, blas_int* ilo, blas_int* ihi, double* scale, double* abnrm, double* rconde, double* rcondv, double* work, const blas_int* lwork, blas_int* iwork, blas_int* info, blas_len balanc_len, blas_len jobvl_len, blas_len jobvr_len, blas_len sense_len) ARMA_NOEXCEPT; // eigen decomposition of general matrix (complex; advanced form) - void arma_fortran(arma_cgeevx)(const char* balanc, const char* jobvl, const char* jobvr, const char* sense, const blas_int* n, blas_cxf* a, const blas_int* lda, blas_cxf* w, blas_cxf* vl, const blas_int* ldvl, blas_cxf* vr, const blas_int* ldvr, blas_int* ilo, blas_int* ihi, float* scale, float* abnrm, float* rconde, float* rcondv, blas_cxf* work, const blas_int* lwork, float* rwork, const blas_int* info, blas_len balanc_len, blas_len jobvl_len, blas_len jobvr_len, blas_len sense_len) ARMA_NOEXCEPT; - void arma_fortran(arma_zgeevx)(const char* balanc, const char* jobvl, const char* jobvr, const char* sense, const blas_int* n, blas_cxd* a, const blas_int* lda, blas_cxd* w, blas_cxd* vl, const blas_int* ldvl, blas_cxd* vr, const blas_int* ldvr, blas_int* ilo, blas_int* ihi, double* scale, double* abnrm, double* rconde, double* rcondv, blas_cxd* work, const blas_int* lwork, double* rwork, const blas_int* info, blas_len balanc_len, blas_len jobvl_len, blas_len jobvr_len, blas_len sense_len) ARMA_NOEXCEPT; + void arma_fortran(arma_cgeevx)(const char* balanc, const char* jobvl, const char* jobvr, const char* sense, const blas_int* n, blas_cxf* a, const blas_int* lda, blas_cxf* w, blas_cxf* vl, const blas_int* ldvl, blas_cxf* vr, const blas_int* ldvr, blas_int* ilo, blas_int* ihi, float* scale, float* abnrm, float* rconde, float* rcondv, blas_cxf* work, const blas_int* lwork, float* rwork, blas_int* info, blas_len balanc_len, blas_len jobvl_len, blas_len jobvr_len, blas_len sense_len) ARMA_NOEXCEPT; + void arma_fortran(arma_zgeevx)(const char* balanc, const char* jobvl, const char* jobvr, const char* sense, const blas_int* n, blas_cxd* a, const blas_int* lda, blas_cxd* w, blas_cxd* vl, const blas_int* ldvl, blas_cxd* vr, const blas_int* ldvr, blas_int* ilo, blas_int* ihi, double* scale, double* abnrm, double* rconde, double* rcondv, blas_cxd* work, const blas_int* lwork, double* rwork, blas_int* info, blas_len balanc_len, blas_len jobvl_len, blas_len jobvr_len, blas_len sense_len) ARMA_NOEXCEPT; // eigen decomposition of symmetric real matrices void arma_fortran(arma_ssyev)(const char* jobz, const char* uplo, const blas_int* n, float* a, const blas_int* lda, float* w, float* work, const blas_int* lwork, blas_int* info, blas_len jobz_len, blas_len uplo_len) ARMA_NOEXCEPT; @@ -942,6 +952,12 @@ extern "C" void arma_fortran(arma_cgebal)(const char* job, const blas_int* n, blas_cxf* a, const blas_int* lda, blas_int* ilo, blas_int* ihi, float* scale, blas_int* info, blas_len job_len) ARMA_NOEXCEPT; void arma_fortran(arma_zgebal)(const char* job, const blas_int* n, blas_cxd* a, const blas_int* lda, blas_int* ilo, blas_int* ihi, double* scale, blas_int* info, blas_len job_len) ARMA_NOEXCEPT; + // solve linear equality-constrained least squares problem + void arma_fortran(arma_sgglse)(const blas_int* m, const blas_int* n, const blas_int* p, float* a, const blas_int* lda, float* b, const blas_int* ldb, float* c, float* d, float* x, float* work, const blas_int* lwork, blas_int* info) ARMA_NOEXCEPT; + void arma_fortran(arma_dgglse)(const blas_int* m, const blas_int* n, const blas_int* p, double* a, const blas_int* lda, double* b, const blas_int* ldb, double* c, double* d, double* x, double* work, const blas_int* lwork, blas_int* info) ARMA_NOEXCEPT; + void arma_fortran(arma_cgglse)(const blas_int* m, const blas_int* n, const blas_int* p, blas_cxf* a, const blas_int* lda, blas_cxf* b, const blas_int* ldb, blas_cxf* c, blas_cxf* d, blas_cxf* x, blas_cxf* work, const blas_int* lwork, blas_int* info) ARMA_NOEXCEPT; + void arma_fortran(arma_zgglse)(const blas_int* m, const blas_int* n, const blas_int* p, blas_cxd* a, const blas_int* lda, blas_cxd* b, const blas_int* ldb, blas_cxd* c, blas_cxd* d, blas_cxd* x, blas_cxd* work, const blas_int* lwork, blas_int* info) ARMA_NOEXCEPT; + #else // prototypes without hidden arguments @@ -1304,6 +1320,12 @@ extern "C" void arma_fortran(arma_cgebal)(const char* job, const blas_int* n, blas_cxf* a, const blas_int* lda, blas_int* ilo, blas_int* ihi, float* scale, blas_int* info) ARMA_NOEXCEPT; void arma_fortran(arma_zgebal)(const char* job, const blas_int* n, blas_cxd* a, const blas_int* lda, blas_int* ilo, blas_int* ihi, double* scale, blas_int* info) ARMA_NOEXCEPT; + // solve linear equality-constrained least squares problem + void arma_fortran(arma_sgglse)(const blas_int* m, const blas_int* n, const blas_int* p, float* a, const blas_int* lda, float* b, const blas_int* ldb, float* c, float* d, float* x, float* work, const blas_int* lwork, blas_int* info) ARMA_NOEXCEPT; + void arma_fortran(arma_dgglse)(const blas_int* m, const blas_int* n, const blas_int* p, double* a, const blas_int* lda, double* b, const blas_int* ldb, double* c, double* d, double* x, double* work, const blas_int* lwork, blas_int* info) ARMA_NOEXCEPT; + void arma_fortran(arma_cgglse)(const blas_int* m, const blas_int* n, const blas_int* p, blas_cxf* a, const blas_int* lda, blas_cxf* b, const blas_int* ldb, blas_cxf* c, blas_cxf* d, blas_cxf* x, blas_cxf* work, const blas_int* lwork, blas_int* info) ARMA_NOEXCEPT; + void arma_fortran(arma_zgglse)(const blas_int* m, const blas_int* n, const blas_int* p, blas_cxd* a, const blas_int* lda, blas_cxd* b, const blas_int* ldb, blas_cxd* c, blas_cxd* d, blas_cxd* x, blas_cxd* work, const blas_int* lwork, blas_int* info) ARMA_NOEXCEPT; + #endif } diff --git a/inst/include/armadillo_bits/fn_circshift.hpp b/inst/include/armadillo_bits/fn_circshift.hpp index 28b8f47c..d6e9689c 100644 --- a/inst/include/armadillo_bits/fn_circshift.hpp +++ b/inst/include/armadillo_bits/fn_circshift.hpp @@ -23,7 +23,7 @@ template -// [[deprecated("use circshift() instead")]] +[[deprecated("use circshift() instead")]] inline typename enable_if2 @@ -73,7 +73,7 @@ circshift template -// [[deprecated("use circshift() instead")]] +[[deprecated("use circshift() instead")]] inline typename enable_if2 @@ -139,7 +139,7 @@ circshift template -// [[deprecated("use circshift() instead")]] +[[deprecated("use circshift() instead")]] inline typename enable_if2 @@ -215,7 +215,7 @@ circshift template -// [[deprecated("use circshift() instead")]] +[[deprecated("use circshift() instead")]] inline SpMat shift diff --git a/inst/include/armadillo_bits/fn_solve_lse.hpp b/inst/include/armadillo_bits/fn_solve_lse.hpp new file mode 100644 index 00000000..80515c08 --- /dev/null +++ b/inst/include/armadillo_bits/fn_solve_lse.hpp @@ -0,0 +1,122 @@ +// SPDX-License-Identifier: Apache-2.0 +// +// Copyright 2008-2016 Conrad Sanderson (https://conradsanderson.id.au) +// Copyright 2008-2016 National ICT Australia (NICTA) +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// https://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +// ------------------------------------------------------------------------ + + +//! \addtogroup fn_solve_lse +//! @{ + + + +template +inline +bool +solve_lse_worker + ( + Mat& out, + const Base& A_expr, + const Base& B_expr, + const Base& C_expr, + const Base& D_expr + ) + { + arma_debug_sigprint(); + + typedef typename T1::elem_type eT; + + // A, B, C, D are overwritten by auxlib::solve_lse() + + Mat A = A_expr.get_ref(); + Mat B = B_expr.get_ref(); + Mat C = C_expr.get_ref(); + Mat D = D_expr.get_ref(); + + arma_conform_check( (B.n_cols != uword(1)), "solve_lse(): B must be a column vector" ); + arma_conform_check( (D.n_cols != uword(1)), "solve_lse(): D must be a column vector" ); + + arma_conform_check( (A.n_rows != B.n_rows), "solve_lse(): number of rows in A and B must be the same" ); + arma_conform_check( (C.n_rows != D.n_rows), "solve_lse(): number of rows in C and D must be the same" ); + + arma_conform_check( (A.n_cols != C.n_cols), "solve_lse(): number of columns in A and C must be the same" ); + + arma_conform_check( (C.n_rows > C.n_cols), "solve_lse(): C.n_rows must be <= C.n_cols" ); + + arma_conform_check( (A.n_cols > (A.n_rows + C.n_cols)), "solve_lse(): A.n_cols must be <= (A.n_rows + C.n_cols)" ); + + out.set_size(A.n_cols, 1); + + if(out.is_empty() || A.is_empty() || B.is_empty() || C.is_empty() || D.is_empty()) { out.zeros(); return true; } + + const bool status = auxlib::solve_lse(out, A, C, B, D); // B and C are deliberately swapped + + if(status == false) { out.soft_reset(); } + + return status; + } + + + +template +inline +typename enable_if2< is_blas_type::value, bool >::result +solve_lse + ( + Mat& out, + const Base& A_expr, + const Base& B_expr, + const Base& C_expr, + const Base& D_expr + ) + { + arma_debug_sigprint(); + + const bool status = solve_lse_worker(out, A_expr, B_expr, C_expr, D_expr); + + if(status == false) { arma_warn(3, "solve_lse(): solution not found"); } + + return status; + } + + + +template +arma_warn_unused +inline +Col +solve_lse + ( + const Base& A_expr, + const Base& B_expr, + const Base& C_expr, + const Base& D_expr + ) + { + arma_debug_sigprint(); + + typedef typename T1::elem_type eT; + + Col out; + + const bool status = solve_lse_worker(out, A_expr, B_expr, C_expr, D_expr); + + if(status == false) { arma_stop_runtime_error("solve_lse(): solution not found"); } + + return out; + } + + + +//! @} diff --git a/inst/include/armadillo_bits/fn_spones.hpp b/inst/include/armadillo_bits/fn_spones.hpp index f34c9936..013f26c1 100644 --- a/inst/include/armadillo_bits/fn_spones.hpp +++ b/inst/include/armadillo_bits/fn_spones.hpp @@ -26,20 +26,12 @@ template arma_warn_unused inline -SpMat +const SpOp spones(const SpBase& X) { arma_debug_sigprint(); - typedef typename T1::elem_type eT; - - const unwrap_spmat U(X.get_ref()); - - SpMat out(arma_layout_indicator(), U.M); - - arrayops::inplace_set( access::rwp(out.values), eT(1), out.n_nonzero ); - - return out; + return SpOp(X.get_ref()); } diff --git a/inst/include/armadillo_bits/glue_cubemul_bones.hpp b/inst/include/armadillo_bits/glue_cubemul_bones.hpp index 5056c197..32b50fd0 100644 --- a/inst/include/armadillo_bits/glue_cubemul_bones.hpp +++ b/inst/include/armadillo_bits/glue_cubemul_bones.hpp @@ -27,6 +27,9 @@ struct glue_cubemul template inline static void apply(Cube& out, const GlueCube& X); + template + inline static void apply(Cube_noalias& out, const GlueCube& X); + template inline static void apply_noalias(Cube& out, const Cube& A, const Cube& B); diff --git a/inst/include/armadillo_bits/glue_cubemul_meat.hpp b/inst/include/armadillo_bits/glue_cubemul_meat.hpp index fe284a95..575a13d1 100644 --- a/inst/include/armadillo_bits/glue_cubemul_meat.hpp +++ b/inst/include/armadillo_bits/glue_cubemul_meat.hpp @@ -155,6 +155,96 @@ glue_cubemul::apply(Cube& out, const GlueCube +inline +void +glue_cubemul::apply(Cube_noalias& out, const GlueCube& X) + { + arma_debug_sigprint(); + + typedef typename T1::elem_type eT; + + const bool XA_is_permute_equiv_to_strans = is_permute_equiv_to_strans_expr::eval(X.A); + const bool XB_is_permute_equiv_to_strans = is_permute_equiv_to_strans_expr::eval(X.B); + + if(XA_is_permute_equiv_to_strans && XB_is_permute_equiv_to_strans) + { + arma_debug_print("case: X.A and X.B permute is equivalent to strans"); + + const strip_permute SA(X.A); + const strip_permute SB(X.B); + + constexpr bool do_strans_A = true; + constexpr bool do_htrans_A = false; + + constexpr bool do_strans_B = true; + constexpr bool do_htrans_B = false; + + const unwrap_cube::stored_type> UA(SA.M); + const unwrap_cube::stored_type> UB(SB.M); + + glue_cubemul::apply_noalias(out, UA.M, UB.M); + } + else + if(XA_is_permute_equiv_to_strans) + { + arma_debug_print("case: X.A permute is equivalent to strans"); + + const strip_permute SA(X.A); + const strip_xtrans SB(X.B); + + constexpr bool do_strans_A = true; + constexpr bool do_htrans_A = false; + + constexpr bool do_strans_B = strip_xtrans::do_strans; + constexpr bool do_htrans_B = strip_xtrans::do_htrans; + + const unwrap_cube::stored_type> UA(SA.M); + const unwrap_cube::stored_type> UB(SB.M); + + glue_cubemul::apply_noalias(out, UA.M, UB.M); + } + else + if(XB_is_permute_equiv_to_strans) + { + arma_debug_print("case: X.B permute is equivalent to strans"); + + const strip_xtrans SA(X.A); + const strip_permute SB(X.B); + + constexpr bool do_strans_A = strip_xtrans::do_strans; + constexpr bool do_htrans_A = strip_xtrans::do_htrans; + + constexpr bool do_strans_B = true; + constexpr bool do_htrans_B = false; + + const unwrap_cube::stored_type> UA(SA.M); + const unwrap_cube::stored_type> UB(SB.M); + + glue_cubemul::apply_noalias(out, UA.M, UB.M); + } + else + { + arma_debug_print("case: default operation"); + + const strip_xtrans SA(X.A); + const strip_xtrans SB(X.B); + + constexpr bool do_strans_A = strip_xtrans::do_strans; + constexpr bool do_htrans_A = strip_xtrans::do_htrans; + + constexpr bool do_strans_B = strip_xtrans::do_strans; + constexpr bool do_htrans_B = strip_xtrans::do_htrans; + + const unwrap_cube::stored_type> UA(SA.M); + const unwrap_cube::stored_type> UB(SB.M); + + glue_cubemul::apply_noalias(out, UA.M, UB.M); + } + } + + + template inline void diff --git a/inst/include/armadillo_bits/glue_join_bones.hpp b/inst/include/armadillo_bits/glue_join_bones.hpp index d5910d57..47f8cee5 100644 --- a/inst/include/armadillo_bits/glue_join_bones.hpp +++ b/inst/include/armadillo_bits/glue_join_bones.hpp @@ -82,6 +82,9 @@ struct glue_join_slices { template inline static void apply(Cube& out, const GlueCube& X); + + template + inline static void apply(Cube_noalias& out_noalias, const GlueCube& X); }; diff --git a/inst/include/armadillo_bits/glue_join_meat.hpp b/inst/include/armadillo_bits/glue_join_meat.hpp index 3af809c7..209e1a06 100644 --- a/inst/include/armadillo_bits/glue_join_meat.hpp +++ b/inst/include/armadillo_bits/glue_join_meat.hpp @@ -369,7 +369,7 @@ glue_join_slices::apply(Cube& out, const GlueCube A_tmp(X.A); const unwrap_cube B_tmp(X.B); @@ -385,7 +385,7 @@ glue_join_slices::apply(Cube& out, const GlueCube& out, const GlueCube +inline +void +glue_join_slices::apply(Cube_noalias& out_noalias, const GlueCube& X) + { + arma_debug_sigprint(); + + typedef typename T1::elem_type eT; + + Cube& out = static_cast< Cube& >(out_noalias); + + const unwrap_cube A_tmp(X.A); + const unwrap_cube B_tmp(X.B); + + const Cube& A = A_tmp.M; + const Cube& B = B_tmp.M; + + if(A.n_elem == 0) { out = B; return; } + if(B.n_elem == 0) { out = A; return; } + + arma_conform_check( ( (A.n_rows != B.n_rows) || (A.n_cols != B.n_cols) ), "join_slices(): size of slices must be the same" ); + + out.set_size(A.n_rows, A.n_cols, A.n_slices + B.n_slices); + out.slices(0, A.n_slices-1) = A; + out.slices(A.n_slices, out.n_slices-1) = B; } diff --git a/inst/include/armadillo_bits/op_clamp_bones.hpp b/inst/include/armadillo_bits/op_clamp_bones.hpp index ebe37d8d..157a9d0e 100644 --- a/inst/include/armadillo_bits/op_clamp_bones.hpp +++ b/inst/include/armadillo_bits/op_clamp_bones.hpp @@ -39,7 +39,9 @@ struct op_clamp template inline static void apply(Cube& out, const mtOpCube& in); - template inline static void apply_direct(Cube& out, const Cube& X, const eT min_val, const eT max_val); + template inline static void apply(Cube_noalias& out, const mtOpCube& in); + + template inline static void apply_cube_noalias(Cube& out, const Cube& X, const eT min_val, const eT max_val); template inline static void apply_proxy_noalias(Cube& out, const ProxyCube& P, const typename T1::elem_type min_val, const typename T1::elem_type max_val); }; diff --git a/inst/include/armadillo_bits/op_clamp_meat.hpp b/inst/include/armadillo_bits/op_clamp_meat.hpp index 915f2640..62d827af 100644 --- a/inst/include/armadillo_bits/op_clamp_meat.hpp +++ b/inst/include/armadillo_bits/op_clamp_meat.hpp @@ -192,7 +192,15 @@ op_clamp::apply(Cube& out, const mtOpCube U(in.m); - op_clamp::apply_direct(out, U.M, min_val, max_val); + // detect in-place operation + if(&out == &(U.M)) + { + arrayops::clamp(out.memptr(), out.n_elem, min_val, max_val); + } + else + { + op_clamp::apply_cube_noalias(out, U.M, min_val, max_val); + } } else { @@ -215,34 +223,55 @@ op_clamp::apply(Cube& out, const mtOpCube +template inline void -op_clamp::apply_direct(Cube& out, const Cube& X, const eT min_val, const eT max_val) +op_clamp::apply(Cube_noalias& out, const mtOpCube& in) { arma_debug_sigprint(); - if(&out != &X) + typedef typename T1::elem_type eT; + + const eT min_val = in.aux; + const eT max_val = in.aux_out_eT; + + arma_conform_check( ((min_val <= max_val) == false), "clamp(): min_val must be less than max_val" ); + + if(is_Cube::value) { - out.set_size(X.n_rows, X.n_cols, X.n_slices); - - const uword N = out.n_elem; - - const eT* X_mem = X.memptr(); - eT* out_mem = out.memptr(); + const unwrap_cube U(in.m); - for(uword i=0; i max_val) ? max_val : val); - } + op_clamp::apply_cube_noalias(out, U.M, min_val, max_val); } else { - arma_debug_print("op_clamp::apply_direct(): inplace operation"); + const ProxyCube P(in.m); - arrayops::clamp(out.memptr(), out.n_elem, min_val, max_val); + op_clamp::apply_proxy_noalias(out, P, min_val, max_val); + } + } + + + +template +inline +void +op_clamp::apply_cube_noalias(Cube& out, const Cube& X, const eT min_val, const eT max_val) + { + arma_debug_sigprint(); + + out.set_size(X.n_rows, X.n_cols, X.n_slices); + + const uword N = out.n_elem; + + const eT* X_mem = X.memptr(); + eT* out_mem = out.memptr(); + + for(uword i=0; i max_val) ? max_val : val); } } diff --git a/inst/include/armadillo_bits/op_htrans_bones.hpp b/inst/include/armadillo_bits/op_htrans_bones.hpp index 0147c328..5da857e7 100644 --- a/inst/include/armadillo_bits/op_htrans_bones.hpp +++ b/inst/include/armadillo_bits/op_htrans_bones.hpp @@ -94,6 +94,12 @@ struct op_htrans template inline static void apply(Cube& out, const OpCube& in, const typename arma_cx_only::result* junk = nullptr); + + template + inline static void apply(Cube_noalias& out, const OpCube& in, const typename arma_not_cx::result* junk = nullptr); + + template + inline static void apply(Cube_noalias& out, const OpCube& in, const typename arma_cx_only::result* junk = nullptr); }; diff --git a/inst/include/armadillo_bits/op_htrans_meat.hpp b/inst/include/armadillo_bits/op_htrans_meat.hpp index 7037afdc..f5f7defb 100644 --- a/inst/include/armadillo_bits/op_htrans_meat.hpp +++ b/inst/include/armadillo_bits/op_htrans_meat.hpp @@ -501,6 +501,36 @@ op_htrans::apply(Cube& out, const OpCube& +template +inline +void +op_htrans::apply(Cube_noalias& out, const OpCube& in, const typename arma_not_cx::result* junk) + { + arma_debug_sigprint(); + arma_ignore(junk); + + const unwrap_cube U(in.m); + + op_strans::apply_cube_noalias(out, U.M); + } + + + +template +inline +void +op_htrans::apply(Cube_noalias& out, const OpCube& in, const typename arma_cx_only::result* junk) + { + arma_debug_sigprint(); + arma_ignore(junk); + + const unwrap_cube U(in.m); + + op_htrans::apply_cube_noalias(out, U.M); + } + + + // // op_htrans2 diff --git a/inst/include/armadillo_bits/op_max_bones.hpp b/inst/include/armadillo_bits/op_max_bones.hpp index af4c4964..0e07ce0d 100644 --- a/inst/include/armadillo_bits/op_max_bones.hpp +++ b/inst/include/armadillo_bits/op_max_bones.hpp @@ -45,6 +45,9 @@ struct op_max template inline static void apply(Cube& out, const OpCube& in); + template + inline static void apply(Cube_noalias& out, const OpCube& in); + template inline static void apply_noalias(Cube& out, const Cube& X, const uword dim, const typename arma_not_cx::result* junk = nullptr); diff --git a/inst/include/armadillo_bits/op_max_meat.hpp b/inst/include/armadillo_bits/op_max_meat.hpp index 62cac1a5..bd63c9b2 100644 --- a/inst/include/armadillo_bits/op_max_meat.hpp +++ b/inst/include/armadillo_bits/op_max_meat.hpp @@ -181,6 +181,7 @@ op_max::apply(Cube& out, const OpCube& in) typedef typename T1::elem_type eT; const uword dim = in.aux_uword_a; + arma_conform_check( (dim > 2), "max(): parameter 'dim' must be 0 or 1 or 2" ); const unwrap_cube U(in.m); @@ -201,6 +202,24 @@ op_max::apply(Cube& out, const OpCube& in) +template +inline +void +op_max::apply(Cube_noalias& out, const OpCube& in) + { + arma_debug_sigprint(); + + const uword dim = in.aux_uword_a; + + arma_conform_check( (dim > 2), "max(): parameter 'dim' must be 0 or 1 or 2" ); + + const unwrap_cube U(in.m); + + op_max::apply_noalias(out, U.M, dim); + } + + + template inline void diff --git a/inst/include/armadillo_bits/op_mean_bones.hpp b/inst/include/armadillo_bits/op_mean_bones.hpp index 2b736537..e0d3b5aa 100644 --- a/inst/include/armadillo_bits/op_mean_bones.hpp +++ b/inst/include/armadillo_bits/op_mean_bones.hpp @@ -43,6 +43,9 @@ struct op_mean template inline static void apply(Cube& out, const OpCube& in); + template + inline static void apply(Cube_noalias& out, const OpCube& in); + template inline static void apply_noalias(Cube& out, const Cube& X, const uword dim); diff --git a/inst/include/armadillo_bits/op_mean_meat.hpp b/inst/include/armadillo_bits/op_mean_meat.hpp index 00ae24e0..aa9649a7 100644 --- a/inst/include/armadillo_bits/op_mean_meat.hpp +++ b/inst/include/armadillo_bits/op_mean_meat.hpp @@ -214,6 +214,24 @@ op_mean::apply(Cube& out, const OpCube& in) +template +inline +void +op_mean::apply(Cube_noalias& out, const OpCube& in) + { + arma_debug_sigprint(); + + const uword dim = in.aux_uword_a; + + arma_conform_check( (dim > 2), "mean(): parameter 'dim' must be 0 or 1 or 2" ); + + const unwrap_cube U(in.m); + + op_mean::apply_noalias(out, U.M, dim); + } + + + template inline void diff --git a/inst/include/armadillo_bits/op_min_bones.hpp b/inst/include/armadillo_bits/op_min_bones.hpp index a30f25b7..a9f6ae90 100644 --- a/inst/include/armadillo_bits/op_min_bones.hpp +++ b/inst/include/armadillo_bits/op_min_bones.hpp @@ -45,6 +45,9 @@ struct op_min template inline static void apply(Cube& out, const OpCube& in); + template + inline static void apply(Cube_noalias& out, const OpCube& in); + template inline static void apply_noalias(Cube& out, const Cube& X, const uword dim, const typename arma_not_cx::result* junk = nullptr); diff --git a/inst/include/armadillo_bits/op_min_meat.hpp b/inst/include/armadillo_bits/op_min_meat.hpp index 36d0f3ac..698a7f87 100644 --- a/inst/include/armadillo_bits/op_min_meat.hpp +++ b/inst/include/armadillo_bits/op_min_meat.hpp @@ -181,6 +181,7 @@ op_min::apply(Cube& out, const OpCube& in) typedef typename T1::elem_type eT; const uword dim = in.aux_uword_a; + arma_conform_check( (dim > 2), "min(): parameter 'dim' must be 0 or 1 or 2" ); const unwrap_cube U(in.m); @@ -201,6 +202,24 @@ op_min::apply(Cube& out, const OpCube& in) +template +inline +void +op_min::apply(Cube_noalias& out, const OpCube& in) + { + arma_debug_sigprint(); + + const uword dim = in.aux_uword_a; + + arma_conform_check( (dim > 2), "min(): parameter 'dim' must be 0 or 1 or 2" ); + + const unwrap_cube U(in.m); + + op_min::apply_noalias(out, U.M, dim); + } + + + template inline void diff --git a/inst/include/armadillo_bits/op_permute_bones.hpp b/inst/include/armadillo_bits/op_permute_bones.hpp index bc91b916..fc32f256 100644 --- a/inst/include/armadillo_bits/op_permute_bones.hpp +++ b/inst/include/armadillo_bits/op_permute_bones.hpp @@ -26,6 +26,8 @@ struct op_permute { template inline static void apply(Cube& out, const OpCube& in); + template inline static void apply(Cube_noalias& out, const OpCube& in); + template inline static void apply_noalias(Cube& Y, const Cube& X, const uword d0, const uword d1, const uword d2); }; diff --git a/inst/include/armadillo_bits/op_permute_meat.hpp b/inst/include/armadillo_bits/op_permute_meat.hpp index 321f621e..7a47f999 100644 --- a/inst/include/armadillo_bits/op_permute_meat.hpp +++ b/inst/include/armadillo_bits/op_permute_meat.hpp @@ -58,6 +58,31 @@ op_permute::apply(Cube& out, const OpCube +template +inline +void +op_permute::apply(Cube_noalias& out, const OpCube& in) + { + arma_debug_sigprint(); + + typedef typename T1::elem_type eT; + + const unwrap_cube U(in.m); + const Cube& X = U.M; + + const uword d0 = in.aux_uword_a; + const uword d1 = in.aux_uword_b; + const uword d2 = in.aux_uword_c; + + const bool mapping_ok = (d0 <= uword(2)) && (d1 <= uword(2)) && (d2 <= uword(2)) && (d0 != d1) && (d0 != d2) && (d1 != d2); + + arma_conform_check( (mapping_ok == false), "permute(): each mapping dimension must be unique and one of: 0, 1, 2" ); + + op_permute::apply_noalias(out, X, d0, d1, d2); + } + + + template inline void diff --git a/inst/include/armadillo_bits/op_repmat_bones.hpp b/inst/include/armadillo_bits/op_repmat_bones.hpp index 706096c1..0ae195a5 100644 --- a/inst/include/armadillo_bits/op_repmat_bones.hpp +++ b/inst/include/armadillo_bits/op_repmat_bones.hpp @@ -39,6 +39,8 @@ struct op_repcube template inline static void apply_noalias(Cube& out, const Cube& X, const uword copies_per_row, const uword copies_per_col, const uword copies_per_slice); template inline static void apply(Cube& out, const OpCube& in); + + template inline static void apply(Cube_noalias& out, const OpCube& in); }; diff --git a/inst/include/armadillo_bits/op_repmat_meat.hpp b/inst/include/armadillo_bits/op_repmat_meat.hpp index e031a266..eaf83e1b 100644 --- a/inst/include/armadillo_bits/op_repmat_meat.hpp +++ b/inst/include/armadillo_bits/op_repmat_meat.hpp @@ -202,4 +202,22 @@ op_repcube::apply(Cube& out, const OpCube +template +inline +void +op_repcube::apply(Cube_noalias& out, const OpCube& in) + { + arma_debug_sigprint(); + + const uword copies_per_row = in.aux_uword_a; + const uword copies_per_col = in.aux_uword_b; + const uword copies_per_slice = in.aux_uword_c; + + const unwrap_cube U(in.m); + + op_repcube::apply_noalias(out, U.M, copies_per_row, copies_per_col, copies_per_slice); + } + + + //! @} diff --git a/inst/include/armadillo_bits/op_reshape_bones.hpp b/inst/include/armadillo_bits/op_reshape_bones.hpp index 0e0f3bcf..7d27df11 100644 --- a/inst/include/armadillo_bits/op_reshape_bones.hpp +++ b/inst/include/armadillo_bits/op_reshape_bones.hpp @@ -39,6 +39,8 @@ struct op_reshape template inline static void apply(Cube& out, const OpCube& in); + template inline static void apply(Cube_noalias& out, const OpCube& in); + template inline static void apply_cube_inplace(Cube& A, const uword new_n_rows, const uword new_n_cols, const uword new_n_slices); template inline static void apply_cube_noalias(Cube& out, const Cube& A, const uword new_n_rows, const uword new_n_cols, const uword new_n_slices); diff --git a/inst/include/armadillo_bits/op_reshape_meat.hpp b/inst/include/armadillo_bits/op_reshape_meat.hpp index 7cec2c2f..6e5c3795 100644 --- a/inst/include/armadillo_bits/op_reshape_meat.hpp +++ b/inst/include/armadillo_bits/op_reshape_meat.hpp @@ -219,6 +219,10 @@ op_reshape::apply_proxy_noalias(Mat& out, const Proxy inline void @@ -247,6 +251,27 @@ op_reshape::apply(Cube& out, const OpCube +template +inline +void +op_reshape::apply(Cube_noalias& out, const OpCube& in) + { + arma_debug_sigprint(); + + typedef typename T1::elem_type eT; + + const unwrap_cube U(in.m); + const Cube& A = U.M; + + const uword new_n_rows = in.aux_uword_a; + const uword new_n_cols = in.aux_uword_b; + const uword new_n_slices = in.aux_uword_c; + + op_reshape::apply_cube_noalias(out, A, new_n_rows, new_n_cols, new_n_slices); + } + + + template inline void diff --git a/inst/include/armadillo_bits/op_resize_bones.hpp b/inst/include/armadillo_bits/op_resize_bones.hpp index 830e89ae..07ea0bea 100644 --- a/inst/include/armadillo_bits/op_resize_bones.hpp +++ b/inst/include/armadillo_bits/op_resize_bones.hpp @@ -27,6 +27,8 @@ struct op_resize { template inline static void apply(Mat& out, const Op& in); + template inline static void apply(Mat_noalias& out, const Op& in); + template inline static void apply_mat_inplace(Mat& A, const uword new_n_rows, const uword new_n_cols); template inline static void apply_mat_noalias(Mat& out, const Mat& A, const uword new_n_rows, const uword new_n_cols); @@ -35,6 +37,8 @@ struct op_resize template inline static void apply(Cube& out, const OpCube& in); + template inline static void apply(Cube_noalias& out, const OpCube& in); + template inline static void apply_cube_inplace(Cube& A, const uword new_n_rows, const uword new_n_cols, const uword new_n_slices); template inline static void apply_cube_noalias(Cube& out, const Cube& A, const uword new_n_rows, const uword new_n_cols, const uword new_n_slices); diff --git a/inst/include/armadillo_bits/op_resize_meat.hpp b/inst/include/armadillo_bits/op_resize_meat.hpp index caabf9da..24cedc68 100644 --- a/inst/include/armadillo_bits/op_resize_meat.hpp +++ b/inst/include/armadillo_bits/op_resize_meat.hpp @@ -69,6 +69,23 @@ op_resize::apply(Mat& out, const Op& in) +template +inline +void +op_resize::apply(Mat_noalias& out, const Op& in) + { + arma_debug_sigprint(); + + const uword new_n_rows = in.aux_uword_a; + const uword new_n_cols = in.aux_uword_b; + + const quasi_unwrap U(in.m); + + op_resize::apply_mat_noalias(out, U.M, new_n_rows, new_n_cols); + } + + + template inline void @@ -146,6 +163,24 @@ op_resize::apply(Cube& out, const OpCube& +template +inline +void +op_resize::apply(Cube_noalias& out, const OpCube& in) + { + arma_debug_sigprint(); + + const uword new_n_rows = in.aux_uword_a; + const uword new_n_cols = in.aux_uword_b; + const uword new_n_slices = in.aux_uword_c; + + const unwrap_cube U(in.m); + + op_resize::apply_cube_noalias(out, U.M, new_n_rows, new_n_cols, new_n_slices); + } + + + template inline void diff --git a/inst/include/armadillo_bits/op_strans_bones.hpp b/inst/include/armadillo_bits/op_strans_bones.hpp index 682e8b14..b9f65672 100644 --- a/inst/include/armadillo_bits/op_strans_bones.hpp +++ b/inst/include/armadillo_bits/op_strans_bones.hpp @@ -80,6 +80,9 @@ struct op_strans template inline static void apply(Cube& out, const OpCube& in); + + template + inline static void apply(Cube_noalias& out, const OpCube& in); }; diff --git a/inst/include/armadillo_bits/op_strans_meat.hpp b/inst/include/armadillo_bits/op_strans_meat.hpp index a9ca7302..25036404 100644 --- a/inst/include/armadillo_bits/op_strans_meat.hpp +++ b/inst/include/armadillo_bits/op_strans_meat.hpp @@ -536,4 +536,18 @@ op_strans::apply(Cube& out, const OpCube& +template +inline +void +op_strans::apply(Cube_noalias& out, const OpCube& in) + { + arma_debug_sigprint(); + + const unwrap_cube U(in.m); + + op_strans::apply_cube_noalias(out, U.M); + } + + + //! @} diff --git a/inst/include/armadillo_bits/op_sum_bones.hpp b/inst/include/armadillo_bits/op_sum_bones.hpp index 209248bb..bb4ab8cd 100644 --- a/inst/include/armadillo_bits/op_sum_bones.hpp +++ b/inst/include/armadillo_bits/op_sum_bones.hpp @@ -64,6 +64,9 @@ struct op_sum template inline static void apply(Cube& out, const OpCube& in); + template + inline static void apply(Cube_noalias& out, const OpCube& in); + template inline static void apply_cube_noalias(Cube& out, const Cube& X, const uword dim); diff --git a/inst/include/armadillo_bits/op_sum_meat.hpp b/inst/include/armadillo_bits/op_sum_meat.hpp index 91a4c9aa..5d2282f4 100644 --- a/inst/include/armadillo_bits/op_sum_meat.hpp +++ b/inst/include/armadillo_bits/op_sum_meat.hpp @@ -511,6 +511,33 @@ op_sum::apply(Cube& out, const OpCube& in) +template +inline +void +op_sum::apply(Cube_noalias& out, const OpCube& in) + { + arma_debug_sigprint(); + + const uword dim = in.aux_uword_a; + + arma_conform_check( (dim > 2), "sum(): parameter 'dim' must be 0 or 1 or 2" ); + + if((is_Cube::value) || (is_Cube::stored_type>::value) || (arma_config::openmp && ProxyCube::use_mp)) + { + const unwrap_cube U(in.m); + + op_sum::apply_cube_noalias(out, U.M, dim); + } + else + { + const ProxyCube P(in.m); + + op_sum::apply_proxy_noalias(out, P, dim); + } + } + + + template inline void diff --git a/inst/include/armadillo_bits/solve_factoriser_bones.hpp b/inst/include/armadillo_bits/solve_factoriser_bones.hpp new file mode 100644 index 00000000..3585b41b --- /dev/null +++ b/inst/include/armadillo_bits/solve_factoriser_bones.hpp @@ -0,0 +1,127 @@ +// SPDX-License-Identifier: Apache-2.0 +// +// Copyright 2008-2016 Conrad Sanderson (https://conradsanderson.id.au) +// Copyright 2008-2016 National ICT Australia (NICTA) +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// https://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +// ------------------------------------------------------------------------ + + +//! \addtogroup solve_factoriser +//! @{ + + + +// internal use only +template +struct solve_factoriser_trimat_helper + { + inline static bool train(Mat& AA, typename get_pod_type::result& out_rcond, const Mat& A, const uword layout); + + inline static bool solve(Mat& out, const Mat& B, const Mat& AA, const uword layout); + }; + + + +// internal use only +template +struct solve_factoriser_sympd_helper + { + inline static bool train(Mat& AA, typename get_pod_type::result& out_rcond, const Mat< typename get_pod_type::result >& A); + inline static bool train(Mat& AA, typename get_pod_type::result& out_rcond, const Mat::result> >& A); + + inline static bool solve(Mat& out, const Mat& B, const Mat& AA); + }; + + + +// internal use only +template +struct solve_factoriser_square_helper + { + inline static bool train(Mat& AA, podarray& ipiv, typename get_pod_type::result& out_rcond, const Mat& A); + + inline static bool solve(Mat& out, const Mat& B, const Mat& AA, const podarray& ipiv); + }; + + + +// internal use only +template +struct solve_factoriser_rect_helper + { + inline static bool train(Mat& AA, typename get_pod_type::result& out_rcond, const Mat& A); + + inline static bool solve(Mat& out, const Mat& B, const Mat& AA); + }; + + + +// internal use only +template +struct solve_factoriser_approx_helper + { + inline static bool train(Mat& AA, const Mat& A); + + inline static bool solve(Mat& out, const Mat& B, const Mat& AA); + }; + + + +// + + + +class solve_factoriser + { + private: + + enum class elem_id_enum : uword { elem_none, elem_float, elem_double, elem_cx_float, elem_cx_double }; + enum class fact_id_enum : uword { fact_none, fact_trimat, fact_sympd, fact_square, fact_rect, fact_approx }; + + template inline void set_elem_id(); + template inline bool check_elem_id() const; + + template inline bool acquire_AA_ptr(); + template inline void release_AA_ptr(); + + void_ptr AA_ptr_raw = nullptr; + double stored_rcond_val = double(0); + uword layout = 0; // 0: upper triangular; 1: lower triangular + uword n_rows = 0; // number of rows in given A matrix + + elem_id_enum elem_id = elem_id_enum::elem_none; + fact_id_enum fact_id = fact_id_enum::fact_none; + + podarray ipiv; + + + public: + + inline ~solve_factoriser(); + inline solve_factoriser(); + + inline void reset(); + + inline double rcond() const; + + template inline bool factorise(const Base& A_expr, const solve_opts::opts& opts = solve_opts::none, const typename arma_blas_real_or_cx_only::result* junk = nullptr); + + template inline bool solve(Mat& X, const Base& B_expr, const typename arma_blas_real_or_cx_only::result* junk = nullptr) const; + + inline solve_factoriser(const solve_factoriser&) = delete; + inline void operator= (const solve_factoriser&) = delete; + }; + + + +//! @} diff --git a/inst/include/armadillo_bits/solve_factoriser_meat.hpp b/inst/include/armadillo_bits/solve_factoriser_meat.hpp new file mode 100644 index 00000000..3fd69b66 --- /dev/null +++ b/inst/include/armadillo_bits/solve_factoriser_meat.hpp @@ -0,0 +1,818 @@ +// SPDX-License-Identifier: Apache-2.0 +// +// Copyright 2008-2016 Conrad Sanderson (https://conradsanderson.id.au) +// Copyright 2008-2016 National ICT Australia (NICTA) +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// https://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +// ------------------------------------------------------------------------ + + +//! \addtogroup solve_factoriser +//! @{ + + + +template +inline +bool +solve_factoriser_trimat_helper::train(Mat& AA, typename get_pod_type::result& out_rcond, const Mat& A, const uword layout) + { + arma_debug_sigprint(); + + #if defined(ARMA_USE_LAPACK) + { + typedef typename get_pod_type::result T; + + out_rcond = T(0); + + AA = A; + + if(AA.is_empty()) { return true; } + + out_rcond = auxlib::rcond_trimat(AA, layout); + + return true; + } + #else + { + arma_ignore(AA); + arma_ignore(out_rcond); + arma_ignore(A); + arma_ignore(layout); + arma_stop_logic_error("use of LAPACK must be enabled"); + return false; + } + #endif + } + + + +template +inline +bool +solve_factoriser_trimat_helper::solve(Mat& out, const Mat& B, const Mat& AA, const uword layout) + { + arma_debug_sigprint(); + + #if defined(ARMA_USE_LAPACK) + { + if(AA.is_empty() || B.is_empty()) { out.zeros(AA.n_rows, B.n_cols); return true; } + + out = B; + + arma_conform_assert_blas_size(out); + + char uplo = (layout == 0) ? 'U' : 'L'; + char trans = 'N'; + char diag = 'N'; + blas_int n = blas_int(AA.n_rows); + blas_int nrhs = blas_int( B.n_cols); + blas_int info = blas_int(0); + + arma_debug_print("lapack::trtrs()"); + lapack::trtrs(&uplo, &trans, &diag, &n, &nrhs, AA.memptr(), &n, out.memptr(), &n, &info); + + return (info == 0); + } + #else + { + arma_ignore(out); + arma_ignore(B); + arma_ignore(AA); + arma_ignore(layout); + arma_stop_logic_error("use of LAPACK must be enabled"); + return false; + } + #endif + } + + + +// + + + +template +inline +bool +solve_factoriser_sympd_helper::train(Mat& AA, typename get_pod_type::result& out_rcond, const Mat::result>& A) + { + arma_debug_sigprint(); + + #if defined(ARMA_USE_LAPACK) + { + typedef typename get_pod_type::result T; + + out_rcond = T(0); + + AA = A; + + if(AA.is_empty()) { return true; } + + arma_conform_assert_blas_size(AA); + + char norm_id = '1'; + char uplo = 'L'; + blas_int n = blas_int(AA.n_rows); // assuming AA is square + blas_int info = blas_int(0); + T norm_val = T(0); + + podarray work(AA.n_rows); + + arma_debug_print("lapack::lansy()"); + norm_val = (has_blas_float_bug::value) ? auxlib::norm1_sym(AA) : lapack::lansy(&norm_id, &uplo, &n, AA.memptr(), &n, work.memptr()); + + arma_debug_print("lapack::potrf()"); + lapack::potrf(&uplo, &n, AA.memptr(), &n, &info); + + if(info != 0) { AA.reset(); return false; } + + out_rcond = auxlib::lu_rcond_sympd(AA, norm_val); + + return true; + } + #else + { + arma_ignore(AA); + arma_ignore(out_rcond); + arma_ignore(A); + arma_stop_logic_error("use of LAPACK must be enabled"); + return false; + } + #endif + } + + + +template +inline +bool +solve_factoriser_sympd_helper::train(Mat& AA, typename get_pod_type::result& out_rcond, const Mat< std::complex::result> >& A) + { + arma_debug_sigprint(); + + #if defined(ARMA_USE_LAPACK) + { + typedef typename get_pod_type::result T; + + out_rcond = T(0); + + AA = A; + + if(AA.is_empty()) { return true; } + + arma_conform_assert_blas_size(AA); + + char norm_id = '1'; + char uplo = 'L'; + blas_int n = blas_int(AA.n_rows); // assuming AA is square + blas_int info = blas_int(0); + T norm_val = T(0); + + podarray work(AA.n_rows); + + arma_debug_print("lapack::lanhe()"); + norm_val = (has_blas_float_bug::value) ? auxlib::norm1_sym(AA) : lapack::lanhe(&norm_id, &uplo, &n, AA.memptr(), &n, work.memptr()); + + arma_debug_print("lapack::potrf()"); + lapack::potrf(&uplo, &n, AA.memptr(), &n, &info); + + if(info != 0) { AA.reset(); return false; } + + out_rcond = auxlib::lu_rcond_sympd(AA, norm_val); + + return true; + } + #else + { + arma_ignore(AA); + arma_ignore(out_rcond); + arma_ignore(A); + arma_stop_logic_error("use of LAPACK must be enabled"); + return false; + } + #endif + } + + + +template +inline +bool +solve_factoriser_sympd_helper::solve(Mat& out, const Mat& B, const Mat& AA) + { + arma_debug_sigprint(); + + #if defined(ARMA_USE_LAPACK) + { + if(AA.is_empty() || B.is_empty()) { out.zeros(AA.n_rows, B.n_cols); return true; } + + out = B; + + arma_conform_assert_blas_size(out); + + char uplo = 'L'; + blas_int n = blas_int(AA.n_rows); // assuming AA is square + blas_int nrhs = blas_int( B.n_cols); + blas_int info = blas_int(0); + + arma_debug_print("lapack::potrs()"); + lapack::potrs(&uplo, &n, &nrhs, AA.memptr(), &n, out.memptr(), &n, &info); + + return (info == 0); + } + #else + { + arma_ignore(out); + arma_ignore(B); + arma_ignore(AA); + arma_stop_logic_error("use of LAPACK must be enabled"); + return false; + } + #endif + } + + + +// + + + +template +inline +bool +solve_factoriser_square_helper::train(Mat& AA, podarray& ipiv, typename get_pod_type::result& out_rcond, const Mat& A) + { + arma_debug_sigprint(); + + #if defined(ARMA_USE_LAPACK) + { + typedef typename get_pod_type::result T; + + out_rcond = T(0); + + AA = A; + + if(AA.is_empty()) { return true; } + + arma_conform_assert_blas_size(AA); + + char norm_id = '1'; + blas_int n = blas_int(AA.n_rows); // assuming AA is square + blas_int lda = blas_int(AA.n_rows); + blas_int info = blas_int(0); + T norm_val = T(0); + + podarray junk(1); + + ipiv.set_size(AA.n_rows + 2); // +2 for paranoia + + arma_debug_print("lapack::lange()"); + norm_val = (has_blas_float_bug::value) ? auxlib::norm1_gen(A) : lapack::lange(&norm_id, &n, &n, AA.memptr(), &lda, junk.memptr()); + + arma_debug_print("lapack::getrf()"); + lapack::getrf(&n, &n, AA.memptr(), &n, ipiv.memptr(), &info); + + if(info != blas_int(0)) { AA.reset(); ipiv.reset(); return false; } + + out_rcond = auxlib::lu_rcond(AA, norm_val); + + return true; + } + #else + { + arma_ignore(AA); + arma_ignore(ipiv); + arma_ignore(out_rcond); + arma_ignore(A); + arma_stop_logic_error("use of LAPACK must be enabled"); + return false; + } + #endif + } + + + +template +inline +bool +solve_factoriser_square_helper::solve(Mat& out, const Mat& B, const Mat& AA, const podarray& ipiv) + { + arma_debug_sigprint(); + + #if defined(ARMA_USE_LAPACK) + { + if(AA.is_empty() || B.is_empty()) { out.zeros(AA.n_rows, B.n_cols); return true; } + + out = B; + + arma_conform_assert_blas_size(out); + + char trans = 'N'; + blas_int n = blas_int(AA.n_rows); // assuming AA is square + blas_int lda = blas_int(AA.n_rows); + blas_int ldb = blas_int( B.n_rows); + blas_int nrhs = blas_int( B.n_cols); + blas_int info = blas_int(0); + + arma_debug_print("lapack::getrs()"); + lapack::getrs(&trans, &n, &nrhs, AA.memptr(), &lda, ipiv.memptr(), out.memptr(), &ldb, &info); + + return (info == 0); + } + #else + { + arma_ignore(out); + arma_ignore(B); + arma_ignore(AA); + arma_ignore(ipiv); + arma_stop_logic_error("use of LAPACK must be enabled"); + return false; + } + #endif + } + + + +// + + + +template +inline +bool +solve_factoriser_rect_helper::train(Mat& AA, typename get_pod_type::result& out_rcond, const Mat& A) + { + arma_debug_sigprint(); + + Mat A_copy = A; + + // obtain the pseudo-inverse of A, under the assumption of full rank + + const bool status = auxlib::solve_rect_rcond(AA, out_rcond, A_copy, eye>(A.n_rows, A.n_rows)); // A_copy is overwritten + + if(status == false) { AA.reset(); return false; } + + return true; + } + + + +template +inline +bool +solve_factoriser_rect_helper::solve(Mat& out, const Mat& B, const Mat& AA) + { + arma_debug_sigprint(); + + if(AA.n_cols != B.n_rows) { return false; } + + out = AA * B; + + return true; + } + + + +// + + + +template +inline +bool +solve_factoriser_approx_helper::train(Mat& AA, const Mat& A) + { + arma_debug_sigprint(); + + Mat A_copy = A; + + // obtain the pseudo-inverse of A; not using pinv() to ensure the processing is the same as standard arma::solve() + + const bool status = auxlib::solve_approx_svd(AA, A_copy, eye>(A.n_rows, A.n_rows)); // A_copy is overwritten + + if(status == false) { AA.reset(); return false; } + + return true; + } + + + +template +inline +bool +solve_factoriser_approx_helper::solve(Mat& out, const Mat& B, const Mat& AA) + { + arma_debug_sigprint(); + + if(AA.n_cols != B.n_rows) { return false; } + + out = AA * B; + + return true; + } + + + + +// +// +// + + + +inline +solve_factoriser::~solve_factoriser() + { + arma_debug_sigprint(); + + reset(); + } + + + +inline +solve_factoriser::solve_factoriser() + { + arma_debug_sigprint_this(this); + } + + + +template +inline +bool +solve_factoriser::acquire_AA_ptr() + { + arma_debug_sigprint(); + + AA_ptr_raw = new(std::nothrow) Mat; + + return (AA_ptr_raw != nullptr); + } + + + +template +inline +void +solve_factoriser::release_AA_ptr() + { + arma_debug_sigprint(); + + if(AA_ptr_raw != nullptr) { delete static_cast< Mat* >(AA_ptr_raw); AA_ptr_raw = nullptr; } + } + + + +inline +void +solve_factoriser::reset() + { + arma_debug_sigprint(); + + if(elem_id == elem_id_enum::elem_float ) { (*this).release_AA_ptr< float>(); } + else if(elem_id == elem_id_enum::elem_double ) { (*this).release_AA_ptr< double>(); } + else if(elem_id == elem_id_enum::elem_cx_float ) { (*this).release_AA_ptr< cx_float>(); } + else if(elem_id == elem_id_enum::elem_cx_double) { (*this).release_AA_ptr(); } + + stored_rcond_val = double(0); + layout = 0; + n_rows = 0; + + elem_id = elem_id_enum::elem_none; + fact_id = fact_id_enum::fact_none; + + ipiv.reset(); + } + + + +template +inline +void +solve_factoriser::set_elem_id() + { + arma_debug_sigprint(); + + if( is_float::value) { elem_id = elem_id_enum::elem_float; } + else if( is_double::value) { elem_id = elem_id_enum::elem_double; } + else if( is_cx_float::value) { elem_id = elem_id_enum::elem_cx_float; } + else if(is_cx_double::value) { elem_id = elem_id_enum::elem_cx_double; } + else { elem_id = elem_id_enum::elem_none; } + } + + + +template +inline +bool +solve_factoriser::check_elem_id() const + { + arma_debug_sigprint(); + + bool ok = false; + + if( (is_float::value) && (elem_id == elem_id_enum::elem_float )) { ok = true; } + else if( (is_double::value) && (elem_id == elem_id_enum::elem_double )) { ok = true; } + else if( (is_cx_float::value) && (elem_id == elem_id_enum::elem_cx_float )) { ok = true; } + else if((is_cx_double::value) && (elem_id == elem_id_enum::elem_cx_double)) { ok = true; } + + return ok; + } + + + +inline +double +solve_factoriser::rcond() const + { + arma_debug_sigprint(); + + return stored_rcond_val; + } + + + +template +inline +bool +solve_factoriser::factorise + ( + const Base& A_expr, + const solve_opts::opts& opts, + const typename arma_blas_real_or_cx_only::result* junk + ) + { + arma_debug_sigprint(); + arma_ignore(junk); + + typedef typename T1::elem_type eT; + typedef typename get_pod_type::result T; + + if(arma_config::lapack == false) { arma_stop_logic_error("solve_factoriser::factorise(): use of LAPACK must be enabled"); return false; } + + reset(); + + set_elem_id(); + + const bool AA_ptr_ok = acquire_AA_ptr(); + + if(AA_ptr_ok == false) { arma_warn(3, "solve_factoriser::factorise(): initialisation failed" ); return false; } + + Mat* AA_ptr_cooked = static_cast< Mat* >(AA_ptr_raw); + Mat& AA = (*AA_ptr_cooked); + + const uword flags = opts.flags; + + const bool fast = bool(flags & solve_opts::flag_fast ); + const bool equilibrate = bool(flags & solve_opts::flag_equilibrate ); + const bool no_approx = bool(flags & solve_opts::flag_no_approx ); + const bool no_band = bool(flags & solve_opts::flag_no_band ); + const bool no_sympd = bool(flags & solve_opts::flag_no_sympd ); + const bool allow_ugly = bool(flags & solve_opts::flag_allow_ugly ); + const bool likely_sympd = bool(flags & solve_opts::flag_likely_sympd); + const bool refine = bool(flags & solve_opts::flag_refine ); + const bool no_trimat = bool(flags & solve_opts::flag_no_trimat ); + const bool force_approx = bool(flags & solve_opts::flag_force_approx); + const bool force_sym = bool(flags & solve_opts::flag_force_sym ); + const bool scale_thresh = bool(flags & solve_opts::flag_scale_thresh); + + arma_debug_print("solve_factoriser::factorise(): enabled flags:"); + + if(fast ) { arma_debug_print("fast"); } + if(equilibrate ) { arma_debug_print("equilibrate"); } + if(no_approx ) { arma_debug_print("no_approx"); } + if(no_band ) { arma_debug_print("no_band"); } + if(no_sympd ) { arma_debug_print("no_sympd"); } + if(allow_ugly ) { arma_debug_print("allow_ugly"); } + if(likely_sympd) { arma_debug_print("likely_sympd"); } + if(refine ) { arma_debug_print("refine"); } + if(no_trimat ) { arma_debug_print("no_trimat"); } + if(force_approx) { arma_debug_print("force_approx"); } + if(force_sym ) { arma_debug_print("force_sym"); } + if(scale_thresh) { arma_debug_print("scale_thresh"); } + + arma_conform_check( fast, "solve_factoriser::factorise(): option 'fast' is not supported" ); + arma_conform_check( equilibrate, "solve_factoriser::factorise(): option 'equilibrate' is not supported" ); + arma_conform_check( refine, "solve_factoriser::factorise(): option 'refine' is not supported" ); + arma_conform_check( force_sym, "solve_factoriser::factorise(): option 'force_sym' is not supported" ); + arma_conform_check( (no_sympd && likely_sympd), "solve_factoriser::factorise(): options 'no_sympd' and 'likely_sympd' are mutually exclusive" ); + arma_conform_check( (no_approx && force_approx), "solve_factoriser::factorise(): options 'no_approx' and 'force_approx' are mutually exclusive" ); + + const bool is_triu_expr = is_trimatu_expr::eval(A_expr.get_ref()); + const bool is_tril_expr = is_trimatl_expr::eval(A_expr.get_ref()); + + arma_debug_print("is_triu_expr: ", is_triu_expr); + arma_debug_print("is_tril_expr: ", is_tril_expr); + + const plain_unwrap U(A_expr.get_ref()); + const Mat& A = U.M; + + if(arma_config::check_nonfinite && A.internal_has_nonfinite()) { arma_warn(3, "solve_factoriser::factorise(): given matrix has non-finite elements" ); return false; } + + n_rows = A.n_rows; + + if(A.is_empty()) { AA.copy_size(A); return true; } + + T rcond_val = T(0); + bool status = false; + + if(force_approx) + { + arma_debug_print("solve_factoriser::factorise(): forced approximate factorisation"); + + if(likely_sympd) { arma_warn(2, "solve_factoriser::factorise(): option 'likely_sympd' ignored for forced approximate factorisation" ); } + if(scale_thresh) { arma_warn(2, "solve_factoriser::factorise(): option 'scale_thresh' ignored for forced approximate factorisation" ); } + + status = solve_factoriser_approx_helper::train(AA, A); + + if(status) { fact_id = fact_id_enum::fact_approx; } + + return status; + } + + const T rcond_threshold = (scale_thresh) ? T(std::numeric_limits::epsilon() * (std::max)(uword(1), (std::max)(A.n_rows, A.n_cols))) : T(std::numeric_limits::epsilon()); + + arma_debug_print("solve_factoriser::factorise(): rcond_threshold: ", rcond_threshold); + + if(A.is_square()) + { + arma_debug_print("solve_factoriser::factorise(): detected square system"); + + const bool is_triu = (no_trimat || likely_sympd ) ? false : ((is_triu_expr) ? true : trimat_helper::is_triu(A)); + const bool is_tril = (no_trimat || likely_sympd || is_triu) ? false : ((is_tril_expr) ? true : trimat_helper::is_tril(A)); + + const bool try_sympd = arma_config::optimise_sym && ( (no_sympd || is_triu || is_tril) ? false : (likely_sympd ? true : sym_helper::guess_sympd(A, uword(16))) ); + + arma_debug_print("solve_factoriser::factorise(): internal flags:"); + arma_debug_print("is_triu: ", is_triu ); + arma_debug_print("is_tril: ", is_tril ); + arma_debug_print("try_sympd: ", try_sympd); + + if(is_triu || is_tril) + { + if(is_triu) { arma_debug_print("solve_factoriser::factorise(): upper triangular"); } + if(is_tril) { arma_debug_print("solve_factoriser::factorise(): lower triangular"); } + + layout = (is_triu) ? uword(0) : uword(1); + + status = solve_factoriser_trimat_helper::train(AA, rcond_val, A, layout); + + if(status) { fact_id = fact_id_enum::fact_trimat; } + } + else + if(try_sympd) + { + arma_debug_print("solve_factoriser::factorise(): sympd"); + + status = solve_factoriser_sympd_helper::train(AA, rcond_val, A); + + if(status) + { + fact_id = fact_id_enum::fact_sympd; + } + else + { + arma_debug_print("solve_factoriser::factorise(): sympd failed; retrying with standard square solver"); + + status = solve_factoriser_square_helper::train(AA, ipiv, rcond_val, A); + + if(status) { fact_id = fact_id_enum::fact_square; } + } + } + else + { + arma_debug_print("solve_factoriser::factorise(): square"); + + status = solve_factoriser_square_helper::train(AA, ipiv, rcond_val, A); + + if(status) { fact_id = fact_id_enum::fact_square; } + } + } + else + { + arma_debug_print("solve_factoriser::factorise(): detected non-square system"); + + if(likely_sympd) { arma_warn(2, "solve_factoriser::factorise(): option 'likely_sympd' ignored for non-square matrix" ); } + + status = solve_factoriser_rect_helper::train(AA, rcond_val, A); + + if(status) { fact_id = fact_id_enum::fact_rect; } + } + + + if( (status == true) && (allow_ugly == false) && ((rcond_val < rcond_threshold) || arma_isnan(rcond_val)) ) + { + status = false; + } + + + stored_rcond_val = double(rcond_val); + + + if( (status == false) && (no_approx == false) ) + { + arma_debug_print("solve_factoriser::factorise(): approx"); + + if(rcond_val == T(0)) + { + arma_warn(2, "solve_factoriser::factorise(): matrix is singular; attempting approx factorisation"); + } + else + { + arma_warn(2, "solve_factoriser::factorise(): matrix is close to singular; rcond: ", rcond_val, "; attempting approx factorisation"); + } + + status = solve_factoriser_approx_helper::train(AA, A); + + if(status) { fact_id = fact_id_enum::fact_approx; } + } + + + if(status == false) + { + AA.reset(); + ipiv.reset(); + + fact_id = fact_id_enum::fact_none; + + arma_warn(3, "solve_factoriser::factorise(): factorisation failed" ); + } + + return status; + } + + + +template +inline +bool +solve_factoriser::solve + ( + Mat& X, + const Base& B_expr, + const typename arma_blas_real_or_cx_only::result* junk + ) + const + { + arma_debug_sigprint(); + arma_ignore(junk); + + typedef typename T1::elem_type eT; + + if(arma_config::lapack == false) { arma_stop_logic_error("solve_factoriser::solve(): use of LAPACK must be enabled"); return false; } + if(check_elem_id() == false) { arma_stop_logic_error("solve_factoriser::solve(): element type mismatch"); return false; } + + if(fact_id == fact_id_enum::fact_none) { arma_warn(2, "solve_factoriser::solve(): no factorisation available"); X.soft_reset(); return false; } + + const Mat* AA_ptr_cooked = static_cast< const Mat* >(AA_ptr_raw); + const Mat& AA = (*AA_ptr_cooked); + + const quasi_unwrap U(B_expr.get_ref()); + const Mat& B = U.M; + + if(n_rows != B.n_rows) { arma_warn(1, "solve_factoriser::solve(): size mismatch between factorisation and given matrix" ); X.soft_reset(); return false; } + + if(AA.is_empty() || B.is_empty()) { X.zeros(AA.n_rows, B.n_cols); return true; } + + bool status = false; + + if(U.is_alias(X)) + { + Mat tmp; + + if(fact_id == fact_id_enum::fact_trimat) { status = solve_factoriser_trimat_helper::solve(tmp, B, AA, layout); } + else if(fact_id == fact_id_enum::fact_sympd ) { status = solve_factoriser_sympd_helper::solve(tmp, B, AA ); } + else if(fact_id == fact_id_enum::fact_square) { status = solve_factoriser_square_helper::solve(tmp, B, AA, ipiv ); } + else if(fact_id == fact_id_enum::fact_rect ) { status = solve_factoriser_rect_helper::solve(tmp, B, AA ); } + else if(fact_id == fact_id_enum::fact_approx) { status = solve_factoriser_approx_helper::solve(tmp, B, AA ); } + + X.steal_mem(tmp); + } + else + { + if(fact_id == fact_id_enum::fact_trimat) { status = solve_factoriser_trimat_helper::solve(X, B, AA, layout); } + else if(fact_id == fact_id_enum::fact_sympd ) { status = solve_factoriser_sympd_helper::solve(X, B, AA ); } + else if(fact_id == fact_id_enum::fact_square) { status = solve_factoriser_square_helper::solve(X, B, AA, ipiv ); } + else if(fact_id == fact_id_enum::fact_rect ) { status = solve_factoriser_rect_helper::solve(X, B, AA ); } + else if(fact_id == fact_id_enum::fact_approx) { status = solve_factoriser_approx_helper::solve(X, B, AA ); } + } + + if(status == false) { arma_warn(3, "solve_factoriser::solve(): solution not found"); X.soft_reset(); } + + return status; + } + + + +//! @} diff --git a/inst/include/armadillo_bits/span.hpp b/inst/include/armadillo_bits/span.hpp index 09866832..86365bfe 100644 --- a/inst/include/armadillo_bits/span.hpp +++ b/inst/include/armadillo_bits/span.hpp @@ -21,67 +21,28 @@ //! @{ -struct span_alt {}; +template struct span_base; -template +template struct span_base { - static const span_alt all; - }; - - -template -const span_alt span_base::all = span_alt(); - - -struct span : public span_base<> - { - uword a; - uword b; - bool whole; - - inline - span() - : a(0) - , b(0) - , whole(true) - { - } + const uword a; + const uword b; + const bool whole; + constexpr span_base() : a(0), b(0), whole(true) {} + constexpr explicit span_base(const uword in_a) : a(in_a), b(in_a), whole(false) {} + constexpr explicit span_base(const uword in_a, const uword in_b) : a(in_a), b(in_b), whole(false) {} - inline - span(const span_alt&) - : a(0) - , b(0) - , whole(true) - { - } - - - inline - explicit - span(const uword in_a) - : a(in_a) - , b(in_a) - , whole(false) - { - } - - - // the "explicit" keyword is required here to prevent automatic conversion of {a,b} - // into an instance of span() when submatrices are specified - inline - explicit - span(const uword in_a, const uword in_b) - : a(in_a) - , b(in_b) - , whole(false) - { - } - + static const span_base all; }; +template +const span_base span_base::all = span_base(); + +typedef span_base span; + //! @} diff --git a/inst/include/armadillo_bits/spop_spones_bones.hpp b/inst/include/armadillo_bits/spop_spones_bones.hpp new file mode 100644 index 00000000..f5240b57 --- /dev/null +++ b/inst/include/armadillo_bits/spop_spones_bones.hpp @@ -0,0 +1,31 @@ +// SPDX-License-Identifier: Apache-2.0 +// +// Copyright 2008-2016 Conrad Sanderson (https://conradsanderson.id.au) +// Copyright 2008-2016 National ICT Australia (NICTA) +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// https://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +// ------------------------------------------------------------------------ + + +//! \addtogroup spop_spones +//! @{ + + +struct spop_spones + : public traits_op_passthru + { + template + inline static void apply(SpMat& out, const SpOp& expr); + }; + + +//! @} diff --git a/inst/include/armadillo_bits/spop_spones_meat.hpp b/inst/include/armadillo_bits/spop_spones_meat.hpp new file mode 100644 index 00000000..6e29d090 --- /dev/null +++ b/inst/include/armadillo_bits/spop_spones_meat.hpp @@ -0,0 +1,51 @@ +// SPDX-License-Identifier: Apache-2.0 +// +// Copyright 2008-2016 Conrad Sanderson (https://conradsanderson.id.au) +// Copyright 2008-2016 National ICT Australia (NICTA) +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// https://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +// ------------------------------------------------------------------------ + + +//! \addtogroup spop_spones +//! @{ + + + +template +inline +void +spop_spones::apply(SpMat& out, const SpOp& expr) + { + arma_debug_sigprint(); + + typedef typename T1::elem_type eT; + + const unwrap_spmat U(expr.m); + + if(U.is_alias(out)) + { + arrayops::inplace_set( access::rwp(out.values), eT(1), out.n_nonzero ); + } + else + { + SpMat tmp(arma_layout_indicator(), U.M); + + arrayops::inplace_set( access::rwp(tmp.values), eT(1), tmp.n_nonzero ); + + out.steal_mem(tmp); + } + } + + + +//! @} diff --git a/inst/include/armadillo_bits/traits.hpp b/inst/include/armadillo_bits/traits.hpp index 46e21eaf..58adf9bd 100644 --- a/inst/include/armadillo_bits/traits.hpp +++ b/inst/include/armadillo_bits/traits.hpp @@ -1474,6 +1474,35 @@ struct is_sym_expr< Op > // +template +struct is_trimatu_expr + { + static constexpr bool eval(const T1&) { return false; } + }; + +template +struct is_trimatu_expr< Op > + { + static arma_inline bool eval(const Op& expr) { return (expr.aux_uword_a == uword(0)); } + }; + + +template +struct is_trimatl_expr + { + static constexpr bool eval(const T1&) { return false; } + }; + +template +struct is_trimatl_expr< Op > + { + static arma_inline bool eval(const Op& expr) { return (expr.aux_uword_a == uword(1)); } + }; + + +// + + template struct is_permute_equiv_to_strans_expr { diff --git a/inst/include/armadillo_bits/translate_lapack.hpp b/inst/include/armadillo_bits/translate_lapack.hpp index 4098f471..bbf12168 100644 --- a/inst/include/armadillo_bits/translate_lapack.hpp +++ b/inst/include/armadillo_bits/translate_lapack.hpp @@ -27,7 +27,7 @@ namespace lapack template inline void - getrf(blas_int* m, blas_int* n, eT* a, blas_int* lda, blas_int* ipiv, blas_int* info) + getrf(const blas_int* m, const blas_int* n, eT* a, const blas_int* lda, blas_int* ipiv, blas_int* info) { arma_type_check(( is_blas_type::value == false )); @@ -42,20 +42,20 @@ namespace lapack template inline void - getrs(char* trans, blas_int* n, blas_int* nrhs, eT* a, blas_int* lda, blas_int* ipiv, eT* b, blas_int* ldb, blas_int* info) + getrs(const char* trans, const blas_int* n, const blas_int* nrhs, const eT* a, const blas_int* lda, const blas_int* ipiv, eT* b, const blas_int* ldb, blas_int* info) { arma_type_check(( is_blas_type::value == false )); #if defined(ARMA_USE_FORTRAN_HIDDEN_ARGS) - if( is_float::value) { typedef float T; arma_fortran(arma_sgetrs)(trans, n, nrhs, (T*)a, lda, ipiv, (T*)b, ldb, info, 1); } - else if( is_double::value) { typedef double T; arma_fortran(arma_dgetrs)(trans, n, nrhs, (T*)a, lda, ipiv, (T*)b, ldb, info, 1); } - else if( is_cx_float::value) { typedef blas_cxf T; arma_fortran(arma_cgetrs)(trans, n, nrhs, (T*)a, lda, ipiv, (T*)b, ldb, info, 1); } - else if(is_cx_double::value) { typedef blas_cxd T; arma_fortran(arma_zgetrs)(trans, n, nrhs, (T*)a, lda, ipiv, (T*)b, ldb, info, 1); } + if( is_float::value) { typedef float T; arma_fortran(arma_sgetrs)(trans, n, nrhs, (const T*)a, lda, ipiv, (T*)b, ldb, info, 1); } + else if( is_double::value) { typedef double T; arma_fortran(arma_dgetrs)(trans, n, nrhs, (const T*)a, lda, ipiv, (T*)b, ldb, info, 1); } + else if( is_cx_float::value) { typedef blas_cxf T; arma_fortran(arma_cgetrs)(trans, n, nrhs, (const T*)a, lda, ipiv, (T*)b, ldb, info, 1); } + else if(is_cx_double::value) { typedef blas_cxd T; arma_fortran(arma_zgetrs)(trans, n, nrhs, (const T*)a, lda, ipiv, (T*)b, ldb, info, 1); } #else - if( is_float::value) { typedef float T; arma_fortran(arma_sgetrs)(trans, n, nrhs, (T*)a, lda, ipiv, (T*)b, ldb, info); } - else if( is_double::value) { typedef double T; arma_fortran(arma_dgetrs)(trans, n, nrhs, (T*)a, lda, ipiv, (T*)b, ldb, info); } - else if( is_cx_float::value) { typedef blas_cxf T; arma_fortran(arma_cgetrs)(trans, n, nrhs, (T*)a, lda, ipiv, (T*)b, ldb, info); } - else if(is_cx_double::value) { typedef blas_cxd T; arma_fortran(arma_zgetrs)(trans, n, nrhs, (T*)a, lda, ipiv, (T*)b, ldb, info); } + if( is_float::value) { typedef float T; arma_fortran(arma_sgetrs)(trans, n, nrhs, (const T*)a, lda, ipiv, (T*)b, ldb, info); } + else if( is_double::value) { typedef double T; arma_fortran(arma_dgetrs)(trans, n, nrhs, (const T*)a, lda, ipiv, (T*)b, ldb, info); } + else if( is_cx_float::value) { typedef blas_cxf T; arma_fortran(arma_cgetrs)(trans, n, nrhs, (const T*)a, lda, ipiv, (T*)b, ldb, info); } + else if(is_cx_double::value) { typedef blas_cxd T; arma_fortran(arma_zgetrs)(trans, n, nrhs, (const T*)a, lda, ipiv, (T*)b, ldb, info); } #endif } @@ -310,7 +310,7 @@ namespace lapack template inline void - potrf(char* uplo, blas_int* n, eT* a, blas_int* lda, blas_int* info) + potrf(const char* uplo, const blas_int* n, eT* a, const blas_int* lda, blas_int* info) { arma_type_check(( is_blas_type::value == false )); @@ -332,20 +332,20 @@ namespace lapack template inline void - potrs(char* uplo, blas_int* n, const blas_int* nrhs, eT* a, blas_int* lda, eT* b, blas_int* ldb, blas_int* info) + potrs(char* uplo, const blas_int* n, const blas_int* nrhs, const eT* a, const blas_int* lda, eT* b, const blas_int* ldb, blas_int* info) { arma_type_check(( is_blas_type::value == false )); #if defined(ARMA_USE_FORTRAN_HIDDEN_ARGS) - if( is_float::value) { typedef float T; arma_fortran(arma_spotrs)(uplo, n, nrhs, (T*)a, lda, (T*)b, ldb, info, 1); } - else if( is_double::value) { typedef double T; arma_fortran(arma_dpotrs)(uplo, n, nrhs, (T*)a, lda, (T*)b, ldb, info, 1); } - else if( is_cx_float::value) { typedef blas_cxf T; arma_fortran(arma_cpotrs)(uplo, n, nrhs, (T*)a, lda, (T*)b, ldb, info, 1); } - else if(is_cx_double::value) { typedef blas_cxd T; arma_fortran(arma_zpotrs)(uplo, n, nrhs, (T*)a, lda, (T*)b, ldb, info, 1); } + if( is_float::value) { typedef float T; arma_fortran(arma_spotrs)(uplo, n, nrhs, (const T*)a, lda, (T*)b, ldb, info, 1); } + else if( is_double::value) { typedef double T; arma_fortran(arma_dpotrs)(uplo, n, nrhs, (const T*)a, lda, (T*)b, ldb, info, 1); } + else if( is_cx_float::value) { typedef blas_cxf T; arma_fortran(arma_cpotrs)(uplo, n, nrhs, (const T*)a, lda, (T*)b, ldb, info, 1); } + else if(is_cx_double::value) { typedef blas_cxd T; arma_fortran(arma_zpotrs)(uplo, n, nrhs, (const T*)a, lda, (T*)b, ldb, info, 1); } #else - if( is_float::value) { typedef float T; arma_fortran(arma_spotrs)(uplo, n, nrhs, (T*)a, lda, (T*)b, ldb, info); } - else if( is_double::value) { typedef double T; arma_fortran(arma_dpotrs)(uplo, n, nrhs, (T*)a, lda, (T*)b, ldb, info); } - else if( is_cx_float::value) { typedef blas_cxf T; arma_fortran(arma_cpotrs)(uplo, n, nrhs, (T*)a, lda, (T*)b, ldb, info); } - else if(is_cx_double::value) { typedef blas_cxd T; arma_fortran(arma_zpotrs)(uplo, n, nrhs, (T*)a, lda, (T*)b, ldb, info); } + if( is_float::value) { typedef float T; arma_fortran(arma_spotrs)(uplo, n, nrhs, (const T*)a, lda, (T*)b, ldb, info); } + else if( is_double::value) { typedef double T; arma_fortran(arma_dpotrs)(uplo, n, nrhs, (const T*)a, lda, (T*)b, ldb, info); } + else if( is_cx_float::value) { typedef blas_cxf T; arma_fortran(arma_cpotrs)(uplo, n, nrhs, (const T*)a, lda, (T*)b, ldb, info); } + else if(is_cx_double::value) { typedef blas_cxd T; arma_fortran(arma_zpotrs)(uplo, n, nrhs, (const T*)a, lda, (T*)b, ldb, info); } #endif } @@ -718,20 +718,20 @@ namespace lapack template inline void - trtrs(char* uplo, char* trans, char* diag, blas_int* n, blas_int* nrhs, const eT* a, blas_int* lda, eT* b, blas_int* ldb, blas_int* info) + trtrs(const char* uplo, const char* trans, const char* diag, const blas_int* n, const blas_int* nrhs, const eT* a, const blas_int* lda, eT* b, const blas_int* ldb, blas_int* info) { arma_type_check(( is_blas_type::value == false )); #if defined(ARMA_USE_FORTRAN_HIDDEN_ARGS) - if( is_float::value) { typedef float T; arma_fortran(arma_strtrs)(uplo, trans, diag, n, nrhs, (T*)a, lda, (T*)b, ldb, info, 1, 1, 1); } - else if( is_double::value) { typedef double T; arma_fortran(arma_dtrtrs)(uplo, trans, diag, n, nrhs, (T*)a, lda, (T*)b, ldb, info, 1, 1, 1); } - else if( is_cx_float::value) { typedef blas_cxf T; arma_fortran(arma_ctrtrs)(uplo, trans, diag, n, nrhs, (T*)a, lda, (T*)b, ldb, info, 1, 1, 1); } - else if(is_cx_double::value) { typedef blas_cxd T; arma_fortran(arma_ztrtrs)(uplo, trans, diag, n, nrhs, (T*)a, lda, (T*)b, ldb, info, 1, 1, 1); } + if( is_float::value) { typedef float T; arma_fortran(arma_strtrs)(uplo, trans, diag, n, nrhs, (const T*)a, lda, (T*)b, ldb, info, 1, 1, 1); } + else if( is_double::value) { typedef double T; arma_fortran(arma_dtrtrs)(uplo, trans, diag, n, nrhs, (const T*)a, lda, (T*)b, ldb, info, 1, 1, 1); } + else if( is_cx_float::value) { typedef blas_cxf T; arma_fortran(arma_ctrtrs)(uplo, trans, diag, n, nrhs, (const T*)a, lda, (T*)b, ldb, info, 1, 1, 1); } + else if(is_cx_double::value) { typedef blas_cxd T; arma_fortran(arma_ztrtrs)(uplo, trans, diag, n, nrhs, (const T*)a, lda, (T*)b, ldb, info, 1, 1, 1); } #else - if( is_float::value) { typedef float T; arma_fortran(arma_strtrs)(uplo, trans, diag, n, nrhs, (T*)a, lda, (T*)b, ldb, info); } - else if( is_double::value) { typedef double T; arma_fortran(arma_dtrtrs)(uplo, trans, diag, n, nrhs, (T*)a, lda, (T*)b, ldb, info); } - else if( is_cx_float::value) { typedef blas_cxf T; arma_fortran(arma_ctrtrs)(uplo, trans, diag, n, nrhs, (T*)a, lda, (T*)b, ldb, info); } - else if(is_cx_double::value) { typedef blas_cxd T; arma_fortran(arma_ztrtrs)(uplo, trans, diag, n, nrhs, (T*)a, lda, (T*)b, ldb, info); } + if( is_float::value) { typedef float T; arma_fortran(arma_strtrs)(uplo, trans, diag, n, nrhs, (const T*)a, lda, (T*)b, ldb, info); } + else if( is_double::value) { typedef double T; arma_fortran(arma_dtrtrs)(uplo, trans, diag, n, nrhs, (const T*)a, lda, (T*)b, ldb, info); } + else if( is_cx_float::value) { typedef blas_cxf T; arma_fortran(arma_ctrtrs)(uplo, trans, diag, n, nrhs, (const T*)a, lda, (T*)b, ldb, info); } + else if(is_cx_double::value) { typedef blas_cxd T; arma_fortran(arma_ztrtrs)(uplo, trans, diag, n, nrhs, (const T*)a, lda, (T*)b, ldb, info); } #endif } @@ -992,22 +992,22 @@ namespace lapack template inline typename get_pod_type::result - lange(char* norm, blas_int* m, blas_int* n, eT* a, blas_int* lda, typename get_pod_type::result* work) + lange(const char* norm, const blas_int* m, const blas_int* n, const eT* a, const blas_int* lda, typename get_pod_type::result* work) { arma_type_check(( is_blas_type::value == false )); typedef typename get_pod_type::result out_T; #if defined(ARMA_USE_FORTRAN_HIDDEN_ARGS) - if( is_float::value) { typedef float pod_T; typedef float T; return out_T( arma_fortran(arma_slange)(norm, m, n, (T*)a, lda, (pod_T*)work, 1) ); } - else if( is_double::value) { typedef double pod_T; typedef double T; return out_T( arma_fortran(arma_dlange)(norm, m, n, (T*)a, lda, (pod_T*)work, 1) ); } - else if( is_cx_float::value) { typedef float pod_T; typedef blas_cxf T; return out_T( arma_fortran(arma_clange)(norm, m, n, (T*)a, lda, (pod_T*)work, 1) ); } - else if(is_cx_double::value) { typedef double pod_T; typedef blas_cxd T; return out_T( arma_fortran(arma_zlange)(norm, m, n, (T*)a, lda, (pod_T*)work, 1) ); } + if( is_float::value) { typedef float pod_T; typedef float T; return out_T( arma_fortran(arma_slange)(norm, m, n, (const T*)a, lda, (pod_T*)work, 1) ); } + else if( is_double::value) { typedef double pod_T; typedef double T; return out_T( arma_fortran(arma_dlange)(norm, m, n, (const T*)a, lda, (pod_T*)work, 1) ); } + else if( is_cx_float::value) { typedef float pod_T; typedef blas_cxf T; return out_T( arma_fortran(arma_clange)(norm, m, n, (const T*)a, lda, (pod_T*)work, 1) ); } + else if(is_cx_double::value) { typedef double pod_T; typedef blas_cxd T; return out_T( arma_fortran(arma_zlange)(norm, m, n, (const T*)a, lda, (pod_T*)work, 1) ); } #else - if( is_float::value) { typedef float pod_T; typedef float T; return out_T( arma_fortran(arma_slange)(norm, m, n, (T*)a, lda, (pod_T*)work) ); } - else if( is_double::value) { typedef double pod_T; typedef double T; return out_T( arma_fortran(arma_dlange)(norm, m, n, (T*)a, lda, (pod_T*)work) ); } - else if( is_cx_float::value) { typedef float pod_T; typedef blas_cxf T; return out_T( arma_fortran(arma_clange)(norm, m, n, (T*)a, lda, (pod_T*)work) ); } - else if(is_cx_double::value) { typedef double pod_T; typedef blas_cxd T; return out_T( arma_fortran(arma_zlange)(norm, m, n, (T*)a, lda, (pod_T*)work) ); } + if( is_float::value) { typedef float pod_T; typedef float T; return out_T( arma_fortran(arma_slange)(norm, m, n, (const T*)a, lda, (pod_T*)work) ); } + else if( is_double::value) { typedef double pod_T; typedef double T; return out_T( arma_fortran(arma_dlange)(norm, m, n, (const T*)a, lda, (pod_T*)work) ); } + else if( is_cx_float::value) { typedef float pod_T; typedef blas_cxf T; return out_T( arma_fortran(arma_clange)(norm, m, n, (const T*)a, lda, (pod_T*)work) ); } + else if(is_cx_double::value) { typedef double pod_T; typedef blas_cxd T; return out_T( arma_fortran(arma_zlange)(norm, m, n, (const T*)a, lda, (pod_T*)work) ); } #endif return out_T(0); @@ -1018,22 +1018,22 @@ namespace lapack template inline typename get_pod_type::result - lansy(char* norm, char* uplo, blas_int* n, eT* a, blas_int* lda, typename get_pod_type::result* work) + lansy(const char* norm, const char* uplo, const blas_int* n, const eT* a, const blas_int* lda, typename get_pod_type::result* work) { arma_type_check(( is_blas_type::value == false )); typedef typename get_pod_type::result out_T; #if defined(ARMA_USE_FORTRAN_HIDDEN_ARGS) - if( is_float::value) { typedef float pod_T; typedef float T; return out_T( arma_fortran(arma_slansy)(norm, uplo, n, (T*)a, lda, (pod_T*)work, 1, 1) ); } - else if( is_double::value) { typedef double pod_T; typedef double T; return out_T( arma_fortran(arma_dlansy)(norm, uplo, n, (T*)a, lda, (pod_T*)work, 1, 1) ); } - else if( is_cx_float::value) { typedef float pod_T; typedef blas_cxf T; return out_T( arma_fortran(arma_clansy)(norm, uplo, n, (T*)a, lda, (pod_T*)work, 1, 1) ); } - else if(is_cx_double::value) { typedef double pod_T; typedef blas_cxd T; return out_T( arma_fortran(arma_zlansy)(norm, uplo, n, (T*)a, lda, (pod_T*)work, 1, 1) ); } + if( is_float::value) { typedef float pod_T; typedef float T; return out_T( arma_fortran(arma_slansy)(norm, uplo, n, (const T*)a, lda, (pod_T*)work, 1, 1) ); } + else if( is_double::value) { typedef double pod_T; typedef double T; return out_T( arma_fortran(arma_dlansy)(norm, uplo, n, (const T*)a, lda, (pod_T*)work, 1, 1) ); } + else if( is_cx_float::value) { typedef float pod_T; typedef blas_cxf T; return out_T( arma_fortran(arma_clansy)(norm, uplo, n, (const T*)a, lda, (pod_T*)work, 1, 1) ); } + else if(is_cx_double::value) { typedef double pod_T; typedef blas_cxd T; return out_T( arma_fortran(arma_zlansy)(norm, uplo, n, (const T*)a, lda, (pod_T*)work, 1, 1) ); } #else - if( is_float::value) { typedef float pod_T; typedef float T; return out_T( arma_fortran(arma_slansy)(norm, uplo, n, (T*)a, lda, (pod_T*)work) ); } - else if( is_double::value) { typedef double pod_T; typedef double T; return out_T( arma_fortran(arma_dlansy)(norm, uplo, n, (T*)a, lda, (pod_T*)work) ); } - else if( is_cx_float::value) { typedef float pod_T; typedef blas_cxf T; return out_T( arma_fortran(arma_clansy)(norm, uplo, n, (T*)a, lda, (pod_T*)work) ); } - else if(is_cx_double::value) { typedef double pod_T; typedef blas_cxd T; return out_T( arma_fortran(arma_zlansy)(norm, uplo, n, (T*)a, lda, (pod_T*)work) ); } + if( is_float::value) { typedef float pod_T; typedef float T; return out_T( arma_fortran(arma_slansy)(norm, uplo, n, (const T*)a, lda, (pod_T*)work) ); } + else if( is_double::value) { typedef double pod_T; typedef double T; return out_T( arma_fortran(arma_dlansy)(norm, uplo, n, (const T*)a, lda, (pod_T*)work) ); } + else if( is_cx_float::value) { typedef float pod_T; typedef blas_cxf T; return out_T( arma_fortran(arma_clansy)(norm, uplo, n, (const T*)a, lda, (pod_T*)work) ); } + else if(is_cx_double::value) { typedef double pod_T; typedef blas_cxd T; return out_T( arma_fortran(arma_zlansy)(norm, uplo, n, (const T*)a, lda, (pod_T*)work) ); } #endif return out_T(0); @@ -1044,18 +1044,18 @@ namespace lapack template inline typename get_pod_type::result - lanhe(char* norm, char* uplo, blas_int* n, eT* a, blas_int* lda, typename get_pod_type::result* work) + lanhe(const char* norm, const char* uplo, const blas_int* n, const eT* a, const blas_int* lda, typename get_pod_type::result* work) { arma_type_check(( is_blas_type::value == false )); typedef typename get_pod_type::result out_T; #if defined(ARMA_USE_FORTRAN_HIDDEN_ARGS) - if( is_cx_float::value) { typedef float pod_T; typedef blas_cxf T; return out_T( arma_fortran(arma_clanhe)(norm, uplo, n, (T*)a, lda, (pod_T*)work, 1, 1) ); } - else if(is_cx_double::value) { typedef double pod_T; typedef blas_cxd T; return out_T( arma_fortran(arma_zlanhe)(norm, uplo, n, (T*)a, lda, (pod_T*)work, 1, 1) ); } + if( is_cx_float::value) { typedef float pod_T; typedef blas_cxf T; return out_T( arma_fortran(arma_clanhe)(norm, uplo, n, (const T*)a, lda, (pod_T*)work, 1, 1) ); } + else if(is_cx_double::value) { typedef double pod_T; typedef blas_cxd T; return out_T( arma_fortran(arma_zlanhe)(norm, uplo, n, (const T*)a, lda, (pod_T*)work, 1, 1) ); } #else - if( is_cx_float::value) { typedef float pod_T; typedef blas_cxf T; return out_T( arma_fortran(arma_clanhe)(norm, uplo, n, (T*)a, lda, (pod_T*)work) ); } - else if(is_cx_double::value) { typedef double pod_T; typedef blas_cxd T; return out_T( arma_fortran(arma_zlanhe)(norm, uplo, n, (T*)a, lda, (pod_T*)work) ); } + if( is_cx_float::value) { typedef float pod_T; typedef blas_cxf T; return out_T( arma_fortran(arma_clanhe)(norm, uplo, n, (const T*)a, lda, (pod_T*)work) ); } + else if(is_cx_double::value) { typedef double pod_T; typedef blas_cxd T; return out_T( arma_fortran(arma_zlanhe)(norm, uplo, n, (const T*)a, lda, (pod_T*)work) ); } #endif return out_T(0); @@ -1507,6 +1507,21 @@ namespace lapack #endif } + + + template + inline + void + gglse(const blas_int* m, const blas_int* n, const blas_int* p, eT* a, const blas_int* lda, eT* b, const blas_int* ldb, eT* c, eT* d, eT* x, eT* work, const blas_int* lwork, blas_int* info) + { + arma_type_check(( is_blas_type::value == false )); + + if( is_float::value) { typedef float T; arma_fortran(arma_sgglse)(m, n, p, (T*)a, lda, (T*)b, ldb, (T*)c, (T*)d, (T*)x, (T*)work, lwork, info); } + else if( is_double::value) { typedef double T; arma_fortran(arma_dgglse)(m, n, p, (T*)a, lda, (T*)b, ldb, (T*)c, (T*)d, (T*)x, (T*)work, lwork, info); } + else if( is_cx_float::value) { typedef blas_cxf T; arma_fortran(arma_cgglse)(m, n, p, (T*)a, lda, (T*)b, ldb, (T*)c, (T*)d, (T*)x, (T*)work, lwork, info); } + else if(is_cx_double::value) { typedef blas_cxd T; arma_fortran(arma_zgglse)(m, n, p, (T*)a, lda, (T*)b, ldb, (T*)c, (T*)d, (T*)x, (T*)work, lwork, info); } + } + }