minor regularization of vectors and matrices

This commit is contained in:
Mathew Sutcliffe
2013-08-19 17:44:33 +01:00
parent f9f35f27d5
commit 5f267e8245
9 changed files with 72 additions and 177 deletions

View File

@@ -10,13 +10,11 @@ int BlackMiscTest::CSamplesVectorMatrix::samples()
CVector3D v1;
CVector3D v2(1, 2, 3);
qDebug() << v1 << "value:" << v2[2] << v2.length();
v2 *= v2; // v2 * v2
qDebug() << v2 << 2 * v1 << v1 *v1;
qDebug() << v2 << (2 * v1);
CMatrix3x3 m;
CMatrix3x3 mr = m;
qDebug() << m << mr;
mr.setRandom();
bool im;
CMatrix3x3 mi = mr.inverse(im);
CMatrix3x3 mid = mr * mi;

View File

@@ -15,7 +15,7 @@ namespace Math
{
/*!
* \brief 3D matrix
* \brief Matrix 1x3
*/
class CMatrix1x3 : public CMatrixBase<CMatrix1x3, 1, 3>
{

View File

@@ -14,12 +14,14 @@ namespace BlackMisc
namespace Math
{
class CMatrix3x3;
/*!
* \brief Matrix 3x1
*/
class CMatrix3x1 : public CMatrixBase<CMatrix3x1, 3, 1>
{
friend class CMatrix3x3; // for matrix multiplicaion to access m_matrix
friend class CMatrix3x3;
public:
/*!

View File

@@ -30,8 +30,8 @@ double CMatrix3x3::determinant() const
CMatrix3x3 CMatrix3x3::inverse(bool &o_invertible) const
{
CMatrix3x3 inverse;
double det;
if (this->allValuesEqual() || (det = this->determinant()) == 0)
double det = this->determinant();
if (det == 0)
{
o_invertible = false;
inverse.setZero();

View File

@@ -87,6 +87,17 @@ public:
this->m_matrix = this->m_matrix * other.m_matrix;
return *this;
}
/*!
* \brief Operator *
* \param other
* \return
*/
CMatrix3x3 operator *(const CMatrix3x3 &other) const
{
CMatrix3x3 m(*this);
m *= other;
return m;
}
/*!
* \brief Operator *
@@ -101,27 +112,24 @@ public:
}
/*!
* \brief Operator *
* \param other
* \return
*/
CMatrix3x3 operator *(const CMatrix3x3 &other) const
{
CMatrix3x3 m(*this);
m *= other;
return m;
}
/*!
* \brief Multiply vector with this 3x3 matrix
* \brief Multiply this matrix with vector
* \param vector
* \return
*/
CVector3D operator *(const CVector3D &vector) const
{
CVector3D v(vector);
v.matrixMultiplication(*this);
return v;
return ((*this) * CMatrix3x1(vector.toMatrix3x1())).toVector3D();
}
/*!
* \brief Multiply with factor
* \param factor
* \return
*/
CMatrix3x3 &operator *=(double factor)
{
this->CMatrixBase::operator *=(factor);
return *this;
}
/*!
@@ -136,17 +144,6 @@ public:
return m;
}
/*!
* \brief Multiply with factor
* \param factor
* \return
*/
CMatrix3x3 &operator *=(double factor)
{
CMatrixBase::operator *=(factor);
return *this;
}
/*!
* \brief Transposed matrix
* \return
@@ -171,7 +168,6 @@ public:
* \return
*/
CMatrix1x3 getRow(int row) const;
};
} // namespace

View File

@@ -40,20 +40,6 @@ template<class ImplMatrix, int Rows, int Columns> void CMatrixBase<ImplMatrix, R
if (!valid) throw std::range_error("Row or column invalid");
}
/*
* All values to random value
*/
template<class ImplMatrix, int Rows, int Columns> void CMatrixBase<ImplMatrix, Rows, Columns>::setRandom()
{
for (int r = 0; r < Rows; r++)
{
for (int c = 0; c < Columns; c++)
{
this->m_matrix(r, c) = (qrand() % 101); // 0...100
}
}
}
/*
* All values zero?
*/
@@ -87,7 +73,7 @@ template<class ImplMatrix, int Rows, int Columns> void CMatrixBase<ImplMatrix, R
/*
* To list
*/
template<class ImplMatrix, int Rows, int Columns> const QList<double> CMatrixBase<ImplMatrix, Rows, Columns>::toList() const
template<class ImplMatrix, int Rows, int Columns> QList<double> CMatrixBase<ImplMatrix, Rows, Columns>::toList() const
{
QList<double> list;
for (int r = 0; r < Rows; r++)
@@ -116,22 +102,6 @@ template<class ImplMatrix, int Rows, int Columns> void CMatrixBase<ImplMatrix, R
}
}
/*
* All values equal?
*/
template<class ImplMatrix, int Rows, int Columns> bool CMatrixBase<ImplMatrix, Rows, Columns>::allValuesEqual() const
{
double v = this->getElement(0, 0);
for (int r = 0; r < Rows; r++)
{
for (int c = 0; c < Columns; c++)
{
if (this->m_matrix(r, c) != v) return false;
}
}
return true;
}
/*
* Round all values
*/

View File

@@ -111,7 +111,7 @@ public:
* \brief List of values
* \return
*/
const QList<double> toList() const;
QList<double> toList() const;
/*!
* \brief List of values
@@ -164,7 +164,7 @@ public:
}
/*!
* \brief Operator to support commutative multiplication
* \brief Operator to support commutative scalar multiplication
* \param factor
* \param other
* \return
@@ -174,13 +174,6 @@ public:
return other * factor;
}
/*!
* \brief Multiply with 3D vector operator *
* \param matrix
* \return
*/
template<class ImplVector> ImplVector operator*(const ImplVector matrix) const;
/*!
* \brief Operator /=
* \param factor
@@ -279,11 +272,6 @@ public:
this->m_matrix.setToIdentity();
}
/*!
* \brief Fills the matrix with random elements
*/
void setRandom();
/*!
* \brief All values to zero
*/
@@ -312,13 +300,7 @@ public:
}
/*!
* \brief All values equal, if so matirx is not invertible
* \return
*/
bool allValuesEqual() const;
/*!
* \brief Set a dedicated value
* \brief Set all elements the same
* \param value
*/
void fill(double value) { this->m_matrix.fill(value); }

View File

@@ -43,29 +43,31 @@ template <class ImplVector> void CVector3DBase<ImplVector>::fill(double value)
this->m_k = value;
}
/*
* Element (return by reference)
*/
template <class ImplVector> double &CVector3DBase<ImplVector>::getElement(size_t row)
{
switch (row)
{
case 0:
return this->m_i;
case 1:
return this->m_j;
case 2:
return this->m_k;
default:
Q_ASSERT_X(true, "getElement", "Detected invalid index in 3D vector");
throw std::range_error("Detected invalid index in 3D vector");
}
}
/*
* Element
*/
template <class ImplVector> double CVector3DBase<ImplVector>::getElement(size_t row) const
{
double d;
switch (row)
{
case 0:
d = this->m_i;
break;
case 1:
d = this->m_j;
break;
case 2:
d = this->m_k;
break;
default:
Q_ASSERT_X(true, "getElement", "Detected invalid index in 3D vector");
throw std::range_error("Detected invalid index in 3D vector");
break;
}
return d;
return const_cast<CVector3DBase<ImplVector>*>(this)->getElement(row);
}
/*
@@ -112,17 +114,6 @@ template <class ImplVector> double CVector3DBase<ImplVector>::dotProduct(const I
}
/*
* Multiply with matrix
*/
template <class ImplVector> void CVector3DBase<ImplVector>::matrixMultiplication(const CMatrix3x3 &matrix)
{
CMatrix3x1 m = matrix * this->toMatrix3x1();
this->m_i = m(0, 0);
this->m_j = m(1, 0);
this->m_k = m(2, 0);
}
/*
* Convert to matrix
*/

View File

@@ -14,9 +14,7 @@ namespace BlackMisc
namespace Math
{
class CMatrix3x3; // forward declaration
class CMatrix3x1; // forward declaration
class CMatrix3x1;
/*!
* \brief 3D vector base (x, y, z)
@@ -101,6 +99,13 @@ protected:
*/
CVector3DBase(const CVector3DBase &other) : m_i(other.m_i), m_j(other.m_j), m_k(other.m_k) {}
/*!
* \brief Get element
* \param row
* \return Mutable reference
*/
double &getElement(size_t row);
/*!
* \brief String for converter
* \param i18n
@@ -155,7 +160,8 @@ public:
}
/*!
* \brief Set zeros
* \brief Set all elements the same
* \param value
*/
void fill(double value);
@@ -180,6 +186,13 @@ public:
*/
double operator[](size_t row) const { return this->getElement(row); }
/*!
* \brief Operator []
* \param row
* \return Mutable reference
*/
double &operator[](size_t row) { return this->getElement(row); }
/*!
* \brief Equal operator ==
* \param other
@@ -253,31 +266,6 @@ public:
return v;
}
/*!
* \brief Operator *=, just x*x, y*y, z*z neither vector nor dot product (like a matrix produc)
* \param other
* \return
*/
CVector3DBase &operator *=(const CVector3DBase &other)
{
this->m_i *= other.m_i;
this->m_j *= other.m_j;
this->m_k *= other.m_k;
return *this;
}
/*!
* \brief Operator, just x*x, y*y, z*z neither vector nor dot product, (like a matrix produc)
* \param other
* \return
*/
ImplVector operator *(const ImplVector &other) const
{
ImplVector v = *derived();
v *= other;
return v;
}
/*!
* \brief Multiply with scalar
* \param factor
@@ -339,31 +327,6 @@ public:
return v;
}
/*!
* \brief Operator /=, just x/x, y/y, z/z
* \param other
* \return
*/
CVector3DBase &operator /=(const CVector3DBase &other)
{
this->m_i /= other.m_i;
this->m_j /= other.m_j;
this->m_k /= other.m_k;
return *this;
}
/*!
* \brief Operator, just x/x, y/y, z/z
* \param other
* \return
*/
ImplVector operator /(const ImplVector &other) const
{
ImplVector v = *derived();
v /= other;
return v;
}
/*!
* \brief Dot product
* \param other
@@ -378,13 +341,6 @@ public:
*/
ImplVector crossProduct(const ImplVector &other) const;
/*!
* \brief Matrix * this vector
* \param matrix
* \return
*/
void matrixMultiplication(const CMatrix3x3 &matrix);
/*!
* \brief Reciprocal value
* \return