From 067b51f18ec4eaabce46d6723c18352df615fff4 Mon Sep 17 00:00:00 2001 From: Ben Reifler Date: Thu, 11 Jul 2024 22:18:28 -0600 Subject: [PATCH] Makes it easier to call `cross`. --- PAZ_Math | 2 +- matrix.cpp | 11 +++++++++++ test/test.cpp | 2 +- vector.cpp | 11 ----------- 4 files changed, 13 insertions(+), 13 deletions(-) diff --git a/PAZ_Math b/PAZ_Math index 2b4d5f3..3720dc9 100644 --- a/PAZ_Math +++ b/PAZ_Math @@ -195,6 +195,7 @@ namespace paz Mat operator/(double rhs) const; Mat operator-() const; double dot(const MatRef& rhs) const; + Vec cross(const Vec& rhs) const; MatRef block(std::size_t startRow, std::size_t startCol, std::size_t numRows, std::size_t numCols) const; void setBlock(std::size_t startRow, std::size_t startCol, std::size_t @@ -237,7 +238,6 @@ namespace paz MatRef tail(std::size_t n) const; void setTail(std::size_t n, const Vec& rhs); void resize(std::size_t newRows); - Vec cross(const Vec& rhs) const; }; class MatRef diff --git a/matrix.cpp b/matrix.cpp index d878042..a6b02e1 100644 --- a/matrix.cpp +++ b/matrix.cpp @@ -686,6 +686,17 @@ double paz::Mat::dot(const MatRef& rhs) const return res; } +paz::Vec paz::Mat::cross(const Vec& rhs) const +{ + if(rows() != 3 || cols() != 1 || rhs.rows() != 3 || rhs.cols() != 1) + { + throw std::runtime_error("Not a 3-vector."); + } + return {{operator()(1)*rhs(2) - operator()(2)*rhs(1), + operator()(2)*rhs(0) - operator()(0)*rhs(2), + operator()(0)*rhs(1) - operator()(1)*rhs(0)}}; +} + paz::Mat paz::Mat::operator-(const MatRef& rhs) const { auto res = *this; diff --git a/test/test.cpp b/test/test.cpp index 7c76419..d7ea01d 100644 --- a/test/test.cpp +++ b/test/test.cpp @@ -75,7 +75,7 @@ int main() z.setHead(2, z.tail(2)); PRINT(z) - PRINT(static_cast(m.row(0).trans()).cross(m.row(1).trans())) + PRINT(m.row(0).trans().cross(m.row(1).trans())) PRINT(paz::Mat::Cross(m.row(0).trans())*m.row(1).trans()) const paz::Mat x = paz::Mat::Cross(m.row(0).trans())*m.row(1).trans(); diff --git a/vector.cpp b/vector.cpp index 1b6b955..0f435ab 100644 --- a/vector.cpp +++ b/vector.cpp @@ -110,14 +110,3 @@ void paz::Vec::resize(std::size_t newRows) { resizeRows(newRows); } - -paz::Vec paz::Vec::cross(const Vec& rhs) const -{ - if(size() != 3 || rhs.size() != 3) - { - throw std::runtime_error("Not a 3-vector."); - } - return {{(*this)(1)*rhs(2) - (*this)(2)*rhs(1), - (*this)(2)*rhs(0) - (*this)(0)*rhs(2), - (*this)(0)*rhs(1) - (*this)(1)*rhs(0)}}; -}