Browse Source

Math: generalize Gram-Schmidt algorithm for non-square matrices.

pull/7/head
Vladimír Vondruš 13 years ago
parent
commit
ede0d9b503
  1. 15
      src/Math/Algorithms/GramSchmidt.h
  2. 2
      src/Math/Algorithms/Test/GramSchmidtTest.cpp

15
src/Math/Algorithms/GramSchmidt.h

@ -19,29 +19,30 @@
* @brief Function Magnum::Math::Algorithms::gramSchmidtInPlace(), Magnum::Math::Algorithms::gramSchmidt() * @brief Function Magnum::Math::Algorithms::gramSchmidtInPlace(), Magnum::Math::Algorithms::gramSchmidt()
*/ */
#include "Math/Matrix.h" #include "Math/RectangularMatrix.h"
namespace Magnum { namespace Math { namespace Algorithms { namespace Magnum { namespace Math { namespace Algorithms {
/** /**
@brief Gram-schmidt matrix orthonormalization @brief Gram-Schmidt matrix orthonormalization
@param[in,out] matrix Matrix to perform orthonormalization on @param[in,out] matrix Matrix to perform orthonormalization on
*/ */
template<std::size_t size, class T> void gramSchmidtInPlace(Matrix<size, T>& matrix) { template<std::size_t cols, std::size_t rows, class T> void gramSchmidtInPlace(RectangularMatrix<cols, rows, T>& matrix) {
for(std::size_t i = 0; i != size; ++i) { static_assert(cols <= rows, "Unsupported matrix aspect ratio");
for(std::size_t i = 0; i != cols; ++i) {
matrix[i] = matrix[i].normalized(); matrix[i] = matrix[i].normalized();
for(std::size_t j = i+1; j != size; ++j) for(std::size_t j = i+1; j != cols; ++j)
matrix[j] -= matrix[j].projected(matrix[i]); matrix[j] -= matrix[j].projected(matrix[i]);
} }
} }
/** /**
@brief Gram-schmidt matrix orthonormalization @brief Gram-Schmidt matrix orthonormalization
Unlike gramSchmidtInPlace() returns the modified matrix instead of performing Unlike gramSchmidtInPlace() returns the modified matrix instead of performing
the orthonormalization in-place. the orthonormalization in-place.
*/ */
template<std::size_t size, class T> Matrix<size, T> gramSchmidt(Matrix<size, T> matrix) { template<std::size_t cols, std::size_t rows, class T> RectangularMatrix<cols, rows, T> gramSchmidt(RectangularMatrix<cols, rows, T> matrix) {
gramSchmidtInPlace(matrix); gramSchmidtInPlace(matrix);
return matrix; return matrix;
} }

2
src/Math/Algorithms/Test/GramSchmidtTest.cpp

@ -26,7 +26,7 @@ class GramSchmidtTest: public Corrade::TestSuite::Tester {
void test(); void test();
}; };
typedef Matrix<3, float> Matrix3; typedef RectangularMatrix<3, 3, float> Matrix3;
typedef Vector<3, float> Vector3; typedef Vector<3, float> Vector3;
GramSchmidtTest::GramSchmidtTest() { GramSchmidtTest::GramSchmidtTest() {

Loading…
Cancel
Save