From c1374155b0ff51f97bd3b6f4e45175f5fc57538c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Vladim=C3=ADr=20Vondru=C5=A1?= Date: Thu, 23 Feb 2012 18:29:51 +0100 Subject: [PATCH] Math: function to compute product of all values in the vector. Is this mathematic operation at all? --- src/Math/Test/VectorTest.cpp | 6 ++++++ src/Math/Test/VectorTest.h | 1 + src/Math/Vector.h | 10 ++++++++++ 3 files changed, 17 insertions(+) diff --git a/src/Math/Test/VectorTest.cpp b/src/Math/Test/VectorTest.cpp index f5be2e503..7dff019d0 100644 --- a/src/Math/Test/VectorTest.cpp +++ b/src/Math/Test/VectorTest.cpp @@ -121,6 +121,12 @@ void VectorTest::normalized() { QVERIFY(Vector4(vec).normalized() == Vector4(normalized)); } +void VectorTest::product() { + float vec[] = { 1.0f, 2.0f, 3.0f }; + + QCOMPARE(Vector3(vec).product(), 6.0f); +} + void VectorTest::angle() { float a[] = { 2.0f, 3.0f, 4.0f }; float b[] = { 1.0f, -2.0f, 3.0f }; diff --git a/src/Math/Test/VectorTest.h b/src/Math/Test/VectorTest.h index ece0bbcca..7ec8c2edb 100644 --- a/src/Math/Test/VectorTest.h +++ b/src/Math/Test/VectorTest.h @@ -32,6 +32,7 @@ class VectorTest: public QObject { void addSubstract(); void length(); void normalized(); + void product(); void angle(); void negative(); diff --git a/src/Math/Vector.h b/src/Math/Vector.h index 2b9fb6c87..8111264fd 100644 --- a/src/Math/Vector.h +++ b/src/Math/Vector.h @@ -172,6 +172,16 @@ template class Vector { return *this/length(); } + /** @brief Product of values in the vector */ + T product() const { + T out = 1; + + for(size_t i = 0; i != size; ++i) + out *= at(i); + + return out; + } + private: T _data[size]; };