From de6a2d31ffcd1ce1adcc3e273dc6f6459b318f79 Mon Sep 17 00:00:00 2001 From: kritz Date: Wed, 4 Dec 2019 14:33:33 +0100 Subject: [PATCH] Slice assign value (#111) * Assign value to slice * Readme for formatting --- README.md | 10 ++++++++++ matrix/Slice.hpp | 11 +++++++++++ test/slice.cpp | 12 ++++++++++++ 3 files changed, 33 insertions(+) diff --git a/README.md b/README.md index 72f553665b..1cc31a5e2f 100644 --- a/README.md +++ b/README.md @@ -40,6 +40,16 @@ cmake -DTESTING=ON .. make check ``` +## Formatting +The format can be checked as following: +``` +mkdir build +cd build +cmake -DFORMAT=ON .. +make check_format +``` + + ## Example See the test directory for detailed examples. Some simple examples are included below: diff --git a/matrix/Slice.hpp b/matrix/Slice.hpp index 3a1008c21a..fc5c942264 100644 --- a/matrix/Slice.hpp +++ b/matrix/Slice.hpp @@ -63,6 +63,17 @@ public: return self; } + Slice& operator=(const Type& other) + { + Slice& self = *this; + for (size_t i = 0; i < P; i++) { + for (size_t j = 0; j < Q; j++) { + self(i, j) = other; + } + } + return self; + } + // allow assigning vectors to a slice that are in the axis template // make this a template function since it only exists for some instantiations Slice& operator=(const Vector& other) diff --git a/test/slice.cpp b/test/slice.cpp index 36c252a742..2e2baab1b8 100644 --- a/test/slice.cpp +++ b/test/slice.cpp @@ -125,6 +125,18 @@ int main() TEST(!v5.xy().longerThan(5.f)); TEST(isEqualF(5.f, v5.xy().norm())); + // assign scalar value to slice + Matrix L; + L(0,0) = -1; + L(1,0) = 1; + L(2,0) = 3; + + L.slice<2,1>(0,0) = 0.0f; + + float data_5_check[3] = {0, 0, 3}; + Matrix M (data_5_check); + TEST(isEqual(L, M)); + return 0; }