Isis 3 Programmer Reference
LinearAlgebra.h
1 #ifndef LinearAlgebra_h
2 #define LinearAlgebra_h
3 
8 /* SPDX-License-Identifier: CC0-1.0 */
9 // std library
10 #include <iostream>
11 
12 // boost library
13 #include <boost/numeric/ublas/matrix.hpp>
14 #include <boost/numeric/ublas/io.hpp>
15 
16 // Qt Library
17 #include <QDebug>
18 #include <QPair>
19 
20 // Prevent Boost from outputing debug information to
21 // standard error when throwing exceptions.
22 #define BOOST_UBLAS_NO_STD_CERR
23 
24 namespace Isis {
25  class Angle;
26  class Distance;
27  class Latitude;
28  class Longitude;
89  class LinearAlgebra {
90  public:
100  typedef boost::numeric::ublas::matrix<double> Matrix;
110  typedef boost::numeric::ublas::symmetric_matrix<double, boost::numeric::ublas::upper> SymmetricMatrix;
120  typedef boost::numeric::ublas::vector<double> Vector;
121 
122  // define AxisAngle and EulerAngle
137 
138  // check type of matrix/vector
139  static bool isIdentity(const Matrix &matrix);
140  static bool isOrthogonal(const Matrix &matrix);
141  static bool isRotationMatrix(const Matrix &matrix);
142  static bool isZero(const Matrix &matrix);
143  static bool isZero(const Vector &vector);
144  static bool isEmpty(const Vector &vector);
145  static bool isUnit(const Vector &vector);
146 
147  // create special matrices
148  static Matrix identity(int size);
149  static Matrix transpose(const Matrix &matrix);
150  static Matrix inverse(const Matrix &matrix);
151  static Matrix pseudoinverse(const Matrix &matrix);
152  static Matrix zeroMatrix(int rows, int columns);
153  static Vector zeroVector(int size);
154 
155  //
156  static double determinant(const Matrix &matrix);
157 
158  // magnitude/norm based calculations
159  static Vector normalize(const Vector &vector);
160  static double magnitude(const Vector &vector);
161  static double absoluteMaximum(const Vector &vector);
162 
163  // arithmetic operations
164  static Matrix multiply(const Matrix &matrix1, const Matrix &matrix2);
165  static Vector multiply(const Matrix &matrix, const Vector &vector);
166  static Vector multiply(double scalar, const Vector &vector);
167  static Matrix multiply(double scalar, const Matrix &matrix);
168  static Vector add(const Vector &vector1, const Vector &vector2);
169  static Vector subtract(const Vector &vector1, const Vector &vector2);
170 
171  // vector products
172  static Vector crossProduct(const Vector &vector1, const Vector &vector2);
173  static Vector normalizedCrossProduct(const Vector &vector1, const Vector &vector2);
174  static Matrix outerProduct(const Vector &vector1, const Vector &vector2);
175  static double dotProduct(const Vector &vector1, const Vector &vector2);
176  static double innerProduct(const Vector &vector1, const Vector &vector2);
177 
178  // vector movements (projections, rotations)
179  static Vector project(const Vector &vector1, const Vector &vector2);
180  static Vector rotate(const Vector &vector, const Vector &axis, Angle angle);
181 
182  //
183  static Vector perpendicular(const Vector &vector1, const Vector &vector2);
184 
185  // converters
186  static AxisAngle toAxisAngle(const Matrix &rotationMatrix);//raxisa
187  static Matrix toMatrix(const AxisAngle &axisAngle); // axisar
188  static Matrix toMatrix(const Vector &axis, Angle angle); // axisar
189 
190  static QList<EulerAngle> toEulerAngles(const Matrix &rotationMatrix, const QList<int> axes);// m2eul
191  static Matrix toMatrix(const QList<EulerAngle> &eulerAngles); // eul2m
192  static Matrix toMatrix(const EulerAngle &angle3, const EulerAngle &angle2, const EulerAngle &angle1); // eul2m
193 
194  static Vector toQuaternion(const Matrix &rotationMatrix);// m2q
195  static Matrix toMatrix(const Vector &quaternion);// q2m
196 
197  //Matrix row/col to vector and vector to row/columns
198  static void setRow(Matrix &matrix, const Vector &vector, int rowIndex);
199  static void setColumn(Matrix &matrix, const Vector &vector, int columnIndex);
200  static Vector row(const Matrix &matrix, int rowIndex);
201  static Vector column(const Matrix &matrix, int columnIndex);
202 
203  static void setVec3(Vector *v, double v0, double v1, double v2);
204  static void setVec4(Vector *v, double v0, double v1, double v2, double v3);
205  static Vector vector(double v0, double v1, double v2);
206  static Vector vector(double v0, double v1, double v2, double v3);
207 
208  static Vector subVector(const Vector &v, int start, int size);
209 
210  // No need to implement the std::cout operator<< since this is already
211  // done in boost/numeric/ublas/io.hpp, which is included above
212  // The print format for a size 3 vector is
213  // [3] (1, 2, 3)
214  //
215  // friend ostream &operator<<(ostream &os, LinearAlgebra::Vector &vector);
216 
217  protected:
218  LinearAlgebra();
219  ~LinearAlgebra();
220 
221  private:
222  };
223 
224  // these must be declared outside of the class (at the end since they must be
225  // declared after the typedefs)
226  QDebug operator<<(QDebug dbg, const LinearAlgebra::Vector &vector);
227  QDebug operator<<(QDebug dbg, const LinearAlgebra::Matrix &matrix);
228  QString toString(const LinearAlgebra::Vector &vector, int precision=15);
229 };
230 
231 #endif
Isis::operator<<
QDebug operator<<(QDebug debug, const Hillshade &hillshade)
Print this class out to a QDebug object.
Definition: Hillshade.cpp:314
Isis::LinearAlgebra::multiply
static Matrix multiply(const Matrix &matrix1, const Matrix &matrix2)
Returns the product of two matrices.
Definition: LinearAlgebra.cpp:549
Isis::LinearAlgebra::add
static Vector add(const Vector &vector1, const Vector &vector2)
Adds the two given vectors.
Definition: LinearAlgebra.cpp:634
Isis::LinearAlgebra::project
static Vector project(const Vector &vector1, const Vector &vector2)
Compute the vector projection of vector1 onto vector2.
Definition: LinearAlgebra.cpp:833
QList
This is free and unencumbered software released into the public domain.
Definition: BoxcarCachingAlgorithm.h:13
Isis::LinearAlgebra::isIdentity
static bool isIdentity(const Matrix &matrix)
Determines whether the given matrix is the identity.
Definition: LinearAlgebra.cpp:61
Isis::LinearAlgebra::isOrthogonal
static bool isOrthogonal(const Matrix &matrix)
Determines whether the given matrix is orthogonal by verifying that the matrix and its tranpose are i...
Definition: LinearAlgebra.cpp:89
Isis::LinearAlgebra::subtract
static Vector subtract(const Vector &vector1, const Vector &vector2)
Subtracts the right vector from the left vector.
Definition: LinearAlgebra.cpp:660
Isis::LinearAlgebra::~LinearAlgebra
~LinearAlgebra()
Destructor for a LinearAlgebra object.
Definition: LinearAlgebra.cpp:46
Isis::LinearAlgebra::AxisAngle
QPair< Vector, Angle > AxisAngle
Definition for an Axis-Angle pair.
Definition: LinearAlgebra.h:129
Isis::LinearAlgebra::determinant
static double determinant(const Matrix &matrix)
Returns the determinant of the given 3x3 matrix.
Definition: LinearAlgebra.cpp:440
Isis::LinearAlgebra::perpendicular
static Vector perpendicular(const Vector &vector1, const Vector &vector2)
Finds the unique vector P such that A = V + P, V is parallel to B and P is perpendicular to B,...
Definition: LinearAlgebra.cpp:932
Isis::LinearAlgebra::isZero
static bool isZero(const Matrix &matrix)
Determines whether the given matrix is filled with zereos.
Definition: LinearAlgebra.cpp:211
Isis::LinearAlgebra::absoluteMaximum
static double absoluteMaximum(const Vector &vector)
Returns the maximum norm (L-infinity norm) for the given vector.
Definition: LinearAlgebra.cpp:531
Isis::LinearAlgebra::zeroMatrix
static Matrix zeroMatrix(int rows, int columns)
Returns a matrix with given dimensions that is filled with zeroes.
Definition: LinearAlgebra.cpp:408
Isis::LinearAlgebra::innerProduct
static double innerProduct(const Vector &vector1, const Vector &vector2)
Computes the inner product of the given vectors.
Definition: LinearAlgebra.cpp:801
Isis::LinearAlgebra::rotate
static Vector rotate(const Vector &vector, const Vector &axis, Angle angle)
Rotates a vector about an axis vector given a specified angle.
Definition: LinearAlgebra.cpp:872
Isis::LinearAlgebra::magnitude
static double magnitude(const Vector &vector)
Computes the magnitude (i.e., the length) of the given vector using the Euclidean norm (L2 norm).
Definition: LinearAlgebra.cpp:504
Isis::LinearAlgebra::transpose
static Matrix transpose(const Matrix &matrix)
Returns the transpose of the given matrix.
Definition: LinearAlgebra.cpp:369
Isis::LinearAlgebra::vector
static Vector vector(double v0, double v1, double v2)
Constructs a 3 dimensional vector with the given component values.
Definition: LinearAlgebra.cpp:1714
Isis::LinearAlgebra::isEmpty
static bool isEmpty(const Vector &vector)
Determines whether the given vector is empty (i.e.
Definition: LinearAlgebra.cpp:250
Isis::LinearAlgebra::setRow
static void setRow(Matrix &matrix, const Vector &vector, int rowIndex)
Sets the row of the given matrix to the values of the given vector.
Definition: LinearAlgebra.cpp:1626
Isis::LinearAlgebra::Matrix
boost::numeric::ublas::matrix< double > Matrix
Definition for an Isis::LinearAlgebra::Matrix of doubles.
Definition: LinearAlgebra.h:100
Isis::LinearAlgebra::row
static Vector row(const Matrix &matrix, int rowIndex)
Returns a vector whose components match those of the given matrix row.
Definition: LinearAlgebra.cpp:1670
Isis::toString
QString toString(bool boolToConvert)
Global function to convert a boolean to a string.
Definition: IString.cpp:211
Isis::LinearAlgebra::LinearAlgebra
LinearAlgebra()
Default constructor for a LinearAlgebra object.
Definition: LinearAlgebra.cpp:39
Isis::LinearAlgebra::inverse
static Matrix inverse(const Matrix &matrix)
Returns the inverse of a 2x2 or 3x3 matrix.
Definition: LinearAlgebra.cpp:282
Isis::LinearAlgebra::outerProduct
static Matrix outerProduct(const Vector &vector1, const Vector &vector2)
Computes the outer product of the given vectors.
Definition: LinearAlgebra.cpp:757
Isis::LinearAlgebra::isRotationMatrix
static bool isRotationMatrix(const Matrix &matrix)
Determines whether the given matrix is a rotation matrix.
Definition: LinearAlgebra.cpp:113
Isis::LinearAlgebra::dotProduct
static double dotProduct(const Vector &vector1, const Vector &vector2)
Computes the dot product of the given vectors.
Definition: LinearAlgebra.cpp:781
Isis::LinearAlgebra::Vector
boost::numeric::ublas::vector< double > Vector
Definition for an Isis::LinearAlgebra::Vector of doubles.
Definition: LinearAlgebra.h:120
Isis::LinearAlgebra::column
static Vector column(const Matrix &matrix, int columnIndex)
Returns a vector whose components match those of the given matrix column.
Definition: LinearAlgebra.cpp:1693
Isis::LinearAlgebra::toAxisAngle
static AxisAngle toAxisAngle(const Matrix &rotationMatrix)
Converts a rotation's representation from a matrix to a axis of rotation and its corresponding rotati...
Definition: LinearAlgebra.cpp:973
Isis::LinearAlgebra::pseudoinverse
static Matrix pseudoinverse(const Matrix &matrix)
Returns the pseudoinverse of a matrix.
Definition: LinearAlgebra.cpp:339
Isis::LinearAlgebra::SymmetricMatrix
boost::numeric::ublas::symmetric_matrix< double, boost::numeric::ublas::upper > SymmetricMatrix
Definition for an Isis::LinearAlgebra::SymmetrixMatrix of doubles with an upper configuration.
Definition: LinearAlgebra.h:110
Isis::LinearAlgebra::zeroVector
static Vector zeroVector(int size)
Returns a vector of given length that is filled with zeroes.
Definition: LinearAlgebra.cpp:421
Isis::LinearAlgebra::normalize
static Vector normalize(const Vector &vector)
Returns a unit vector that is codirectional with the given vector by dividing each component of the v...
Definition: LinearAlgebra.cpp:477
Isis::LinearAlgebra::toMatrix
static Matrix toMatrix(const AxisAngle &axisAngle)
Converts a rotation's representation from an axis of rotation and its corresponding rotation angle to...
Definition: LinearAlgebra.cpp:1442
Isis::Angle
Defines an angle and provides unit conversions.
Definition: Angle.h:45
Isis::LinearAlgebra::toEulerAngles
static QList< EulerAngle > toEulerAngles(const Matrix &rotationMatrix, const QList< int > axes)
Converts a rotation's representation from a matrix to a set of Euler angles with corresponding axes.
Definition: LinearAlgebra.cpp:1051
Isis::LinearAlgebra::setVec4
static void setVec4(Vector *v, double v0, double v1, double v2, double v3)
Fills the first four elements of the given vector with the given values.
Definition: LinearAlgebra.cpp:1768
QPair
This is free and unencumbered software released into the public domain.
Definition: CubeIoHandler.h:23
Isis::LinearAlgebra::setVec3
static void setVec3(Vector *v, double v0, double v1, double v2)
Fills the first three elements of the given vector with the given values.
Definition: LinearAlgebra.cpp:1752
Isis::LinearAlgebra::identity
static Matrix identity(int size)
Returns the identity matrix of size NxN.
Definition: LinearAlgebra.cpp:384
Isis::LinearAlgebra::crossProduct
static Vector crossProduct(const Vector &vector1, const Vector &vector2)
Returns the cross product of two vectors.
Definition: LinearAlgebra.cpp:688
Isis::LinearAlgebra::EulerAngle
QPair< Angle, int > EulerAngle
Definition for an EulerAngle pair.
Definition: LinearAlgebra.h:136
Isis::LinearAlgebra::normalizedCrossProduct
static Vector normalizedCrossProduct(const Vector &vector1, const Vector &vector2)
Divides each vector by its corresponding absolute maximum, computes the cross product of the new vect...
Definition: LinearAlgebra.cpp:721
Isis::LinearAlgebra::isUnit
static bool isUnit(const Vector &vector)
Determines whether the given vector is a unit vector.
Definition: LinearAlgebra.cpp:262
Isis::LinearAlgebra
This class holds all static methods to perform linear algebra operations on vectors and matrices.
Definition: LinearAlgebra.h:89
Isis::LinearAlgebra::subVector
static Vector subVector(const Vector &v, int start, int size)
Constructs a vector of given size using the given vector and starting index.
Definition: LinearAlgebra.cpp:1789
Isis::LinearAlgebra::setColumn
static void setColumn(Matrix &matrix, const Vector &vector, int columnIndex)
Sets the column of the given matrix to the values of the given vector.
Definition: LinearAlgebra.cpp:1647
Isis
This is free and unencumbered software released into the public domain.
Definition: Apollo.h:16
Isis::LinearAlgebra::toQuaternion
static Vector toQuaternion(const Matrix &rotationMatrix)
Converts a rotation's representation from a matrix to a unit quaternion.
Definition: LinearAlgebra.cpp:1211