From a864e2cf96cfdf24c461df3ff2964b6671d8ef74 Mon Sep 17 00:00:00 2001 From: Joel Falcou Date: Thu, 18 Sep 2025 20:05:33 +0200 Subject: [PATCH] Implements unary resize and conservativeResize See merge request oss/rotgen!25 --- include/rotgen/config.hpp | 12 ++++++++---- include/rotgen/dynamic/matrix.hpp | 12 ++++++++++++ include/rotgen/fixed/matrix.hpp | 14 ++++++++++++-- include/rotgen/functions.hpp | 12 ++++++++++++ test/unit/functions/qr.cpp | 6 +++--- 5 files changed, 47 insertions(+), 9 deletions(-) diff --git a/include/rotgen/config.hpp b/include/rotgen/config.hpp index 4d88d99..eb91cae 100644 --- a/include/rotgen/config.hpp +++ b/include/rotgen/config.hpp @@ -94,12 +94,16 @@ namespace rotgen inline constexpr bool storage_status = []() { - // Actual static size - if(Rows > 0 && Cols > 0) return (Rows*Cols) <= max_static_size; + // Actual static size - vector 1xN + if(Rows == 1 && Cols == -1) return true; + // Actual static size - vector Nx1 + else if(Rows == -1 && Cols == 1) return true; + // Actual static size - matrix + else if(Rows > 0 && Cols > 0) return (Rows*Cols) <= max_static_size; // Dynamic size but static Max Size - else if(MaxRows > 0 && MaxCols > 0) return (MaxRows*MaxCols) <= max_static_size; + else if(MaxRows > 0 && MaxCols > 0) return (MaxRows*MaxCols) <= max_static_size; // Everything dynamic already - else return false; + else return false; }(); } #endif diff --git a/include/rotgen/dynamic/matrix.hpp b/include/rotgen/dynamic/matrix.hpp index dd31adf..5ae98d0 100644 --- a/include/rotgen/dynamic/matrix.hpp +++ b/include/rotgen/dynamic/matrix.hpp @@ -107,11 +107,23 @@ namespace rotgen parent::resize(new_rows, new_cols); } + void resize(int new_size) requires(IsVectorAtCompileTime && (Rows == -1 || Cols == -1)) + { + if constexpr(Rows == -1) parent::resize(new_size,1); + else parent::resize(1,new_size); + } + void conservativeResize(int new_rows, int new_cols) requires(Rows == -1 && Cols == -1) { parent::conservativeResize(new_rows, new_cols); } + void conservativeResize(int new_size) requires(IsVectorAtCompileTime && (Rows == -1 || Cols == -1)) + { + if constexpr(Rows == -1) parent::conservativeResize(new_size,1); + else parent::conservativeResize(1,new_size); + } + matrix normalized() const requires(IsVectorAtCompileTime) { return matrix(base().normalized()); diff --git a/include/rotgen/fixed/matrix.hpp b/include/rotgen/fixed/matrix.hpp index 5b75e7f..6ceccfe 100644 --- a/include/rotgen/fixed/matrix.hpp +++ b/include/rotgen/fixed/matrix.hpp @@ -55,7 +55,7 @@ namespace rotgen matrix() requires(has_static_storage) {} matrix() requires(!has_static_storage) : parent(Rows > 0 ? Rows : 0, Cols > 0 ? Cols : 0){} - matrix(Index r, Index c) requires(!has_static_storage) : parent(r, c) {} + matrix(Index r, Index c) : parent(r, c) {} matrix(const matrix& other) = default; matrix(matrix&& other) = default; @@ -66,7 +66,7 @@ namespace rotgen {} matrix(Index n) requires(IsVectorAtCompileTime && (Rows != 1 || Cols != 1)) - : matrix(Rows != -1 ? 1 : n, Cols != -1 ? 1 : n) + : parent(n) {} matrix(Scalar v) requires(Rows == 1 && Cols == 1) : parent @@ -210,11 +210,21 @@ namespace rotgen else return base().cwiseSqrt(); } + void resize(int new_size) requires(IsVectorAtCompileTime && (Rows == -1 || Cols == -1)) + { + parent::resize(new_size); + } + void resize(int new_rows, int new_cols) requires(Rows == -1 && Cols == -1) { parent::resize(new_rows, new_cols); } + void conservativeResize(int new_size) requires(IsVectorAtCompileTime && (Rows == -1 || Cols == -1)) + { + parent::conservativeResize(new_size); + } + void conservativeResize(int new_rows, int new_cols) requires(Rows == -1 && Cols == -1) { parent::conservativeResize(new_rows, new_cols); diff --git a/include/rotgen/functions.hpp b/include/rotgen/functions.hpp index b55ab21..0e12ec4 100644 --- a/include/rotgen/functions.hpp +++ b/include/rotgen/functions.hpp @@ -13,6 +13,12 @@ namespace rotgen std::size_t cols(concepts::entity auto const& arg) { return arg.cols(); } std::size_t size(concepts::entity auto const& arg) { return arg.size(); } + template + void resize(E& arg, int sz) requires requires{arg.resize(sz);} + { + arg.resize(sz); + } + template void resize(E& arg, int new_rows, int new_cols) requires(E::RowsAtCompileTime == -1 && E::ColsAtCompileTime == -1) @@ -20,6 +26,12 @@ namespace rotgen arg.resize(new_rows, new_cols); } + template + void conservativeResize(E& arg, int sz) requires requires{arg.conservativeResize(sz);} + { + arg.conservativeResize(sz); + } + template void conservativeResize(E& arg, int new_rows, int new_cols) requires(E::RowsAtCompileTime == -1 && E::ColsAtCompileTime == -1) diff --git a/test/unit/functions/qr.cpp b/test/unit/functions/qr.cpp index d5bd7ec..4ffa6f5 100644 --- a/test/unit/functions/qr.cpp +++ b/test/unit/functions/qr.cpp @@ -11,16 +11,16 @@ TTS_CASE_TPL("System solver using QR", rotgen::tests::types) ( tts::type< tts::types> ) { - rotgen::matrix + rotgen::matrix a { { 2.3, -1, 0.1} , {-1.6, 2.6, -1} , { 0.3, -1, 2} }; - rotgen::matrix b(3,1); + rotgen::matrix b(3,1); b(0) = b(2) = 1; b(1) = 0; - rotgen::matrix r(3,1), error; + rotgen::matrix r(3,1), error; auto x = rotgen::extract(r,0,0,3,1); rotgen::solver::qr(x, a, b);