Isis 3 Programmer Reference
LinearAlgebra.h
1#ifndef LinearAlgebra_h
2#define LinearAlgebra_h
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/vector.hpp>
15#include <boost/numeric/ublas/vector_sparse.hpp>
16#include <boost/numeric/ublas/io.hpp>
17
18// Qt Library
19#include <QDebug>
20#include <QPair>
21
22// Prevent Boost from outputing debug information to
23// standard error when throwing exceptions.
24#define BOOST_UBLAS_NO_STD_CERR
25
26namespace Isis {
27 class Angle;
28 class Distance;
29 class Latitude;
30 class Longitude;
92 public:
102 typedef boost::numeric::ublas::matrix<double> Matrix;
112 typedef boost::numeric::ublas::symmetric_matrix<double, boost::numeric::ublas::upper> SymmetricMatrix;
122 typedef boost::numeric::ublas::symmetric_matrix<double, boost::numeric::ublas::upper> MatrixUpperTriangular;
132 typedef boost::numeric::ublas::vector<double> Vector;
142 typedef boost::numeric::ublas::compressed_vector<double> VectorCompressed;
143
144 // define AxisAngle and EulerAngle
151 typedef QPair<Vector, Angle> AxisAngle;
158 typedef QPair<Angle, int> EulerAngle;
159
160 // check type of matrix/vector
161 static bool isIdentity(const Matrix &matrix);
162 static bool isOrthogonal(const Matrix &matrix);
163 static bool isRotationMatrix(const Matrix &matrix);
164 static bool isZero(const Matrix &matrix);
165 static bool isZero(const Vector &vector);
166 static bool isEmpty(const Vector &vector);
167 static bool isUnit(const Vector &vector);
168
169 // create special matrices
170 static Matrix identity(int size);
171 static Matrix transpose(const Matrix &matrix);
172 static Matrix inverse(const Matrix &matrix);
173 static Matrix pseudoinverse(const Matrix &matrix);
174 static Matrix zeroMatrix(int rows, int columns);
175 static Vector zeroVector(int size);
176
177 //
178 static double determinant(const Matrix &matrix);
179
180 // magnitude/norm based calculations
181 static Vector normalize(const Vector &vector);
182 static double magnitude(const Vector &vector);
183 static double absoluteMaximum(const Vector &vector);
184
185 // arithmetic operations
186 static Matrix multiply(const Matrix &matrix1, const Matrix &matrix2);
187 static Vector multiply(const Matrix &matrix, const Vector &vector);
188 static Vector multiply(double scalar, const Vector &vector);
189 static Matrix multiply(double scalar, const Matrix &matrix);
190 static Vector add(const Vector &vector1, const Vector &vector2);
191 static Vector subtract(const Vector &vector1, const Vector &vector2);
192
193 // vector products
194 static Vector crossProduct(const Vector &vector1, const Vector &vector2);
195 static Vector normalizedCrossProduct(const Vector &vector1, const Vector &vector2);
196 static Matrix outerProduct(const Vector &vector1, const Vector &vector2);
197 static double dotProduct(const Vector &vector1, const Vector &vector2);
198 static double innerProduct(const Vector &vector1, const Vector &vector2);
199
200 // vector movements (projections, rotations)
201 static Vector project(const Vector &vector1, const Vector &vector2);
202 static Vector rotate(const Vector &vector, const Vector &axis, Angle angle);
203
204 //
205 static Vector perpendicular(const Vector &vector1, const Vector &vector2);
206
207 // converters
208 static AxisAngle toAxisAngle(const Matrix &rotationMatrix);//raxisa
209 static Matrix toMatrix(const AxisAngle &axisAngle); // axisar
210 static Matrix toMatrix(const Vector &axis, Angle angle); // axisar
211
212 static QList<EulerAngle> toEulerAngles(const Matrix &rotationMatrix, const QList<int> axes);// m2eul
213 static Matrix toMatrix(const QList<EulerAngle> &eulerAngles); // eul2m
214 static Matrix toMatrix(const EulerAngle &angle3, const EulerAngle &angle2, const EulerAngle &angle1); // eul2m
215
216 static Vector toQuaternion(const Matrix &rotationMatrix);// m2q
217 static Matrix toMatrix(const Vector &quaternion);// q2m
218
219 //Matrix row/col to vector and vector to row/columns
220 static void setRow(Matrix &matrix, const Vector &vector, int rowIndex);
221 static void setColumn(Matrix &matrix, const Vector &vector, int columnIndex);
222 static Vector row(const Matrix &matrix, int rowIndex);
223 static Vector column(const Matrix &matrix, int columnIndex);
224
225 static void setVec3(Vector *v, double v0, double v1, double v2);
226 static void setVec4(Vector *v, double v0, double v1, double v2, double v3);
227 static Vector vector(double v0, double v1, double v2);
228 static Vector vector(double v0, double v1, double v2, double v3);
229
230 static Vector subVector(const Vector &v, int start, int size);
231
232 // No need to implement the std::cout operator<< since this is already
233 // done in boost/numeric/ublas/io.hpp, which is included above
234 // The print format for a size 3 vector is
235 // [3] (1, 2, 3)
236 //
237 // friend ostream &operator<<(ostream &os, LinearAlgebra::Vector &vector);
238
239 protected:
242
243 private:
244 };
245
246 // these must be declared outside of the class (at the end since they must be
247 // declared after the typedefs)
248 QDebug operator<<(QDebug dbg, const LinearAlgebra::Vector &vector);
249 QDebug operator<<(QDebug dbg, const LinearAlgebra::Matrix &matrix);
250 QString toString(const LinearAlgebra::Vector &vector, int precision=15);
251};
252
253#endif
Defines an angle and provides unit conversions.
Definition Angle.h:45
This class holds all static methods to perform linear algebra operations on vectors and matrices.
static AxisAngle toAxisAngle(const Matrix &rotationMatrix)
Converts a rotation's representation from a matrix to a axis of rotation and its corresponding rotati...
static Matrix identity(int size)
Returns the identity matrix of size NxN.
~LinearAlgebra()
Destructor for a LinearAlgebra object.
static Vector subtract(const Vector &vector1, const Vector &vector2)
Subtracts the right vector from the left vector.
static Vector crossProduct(const Vector &vector1, const Vector &vector2)
Returns the cross product of two vectors.
static Vector zeroVector(int size)
Returns a vector of given length that is filled with zeroes.
boost::numeric::ublas::compressed_vector< double > VectorCompressed
Definition for an Isis::LinearAlgebra::VectorCompressed of doubles.
static Vector vector(double v0, double v1, double v2)
Constructs a 3 dimensional vector with the given component values.
LinearAlgebra()
Default constructor for a LinearAlgebra object.
static Vector add(const Vector &vector1, const Vector &vector2)
Adds the two given vectors.
static Vector row(const Matrix &matrix, int rowIndex)
Returns a vector whose components match those of the given matrix row.
static Vector subVector(const Vector &v, int start, int size)
Constructs a vector of given size using the given vector and starting index.
static Matrix toMatrix(const AxisAngle &axisAngle)
Converts a rotation's representation from an axis of rotation and its corresponding rotation angle to...
static bool isUnit(const Vector &vector)
Determines whether the given vector is a unit vector.
static Vector column(const Matrix &matrix, int columnIndex)
Returns a vector whose components match those of the given matrix column.
static bool isEmpty(const Vector &vector)
Determines whether the given vector is empty (i.e.
static Vector project(const Vector &vector1, const Vector &vector2)
Compute the vector projection of vector1 onto vector2.
static void setRow(Matrix &matrix, const Vector &vector, int rowIndex)
Sets the row of the given matrix to the values of the given vector.
static void setColumn(Matrix &matrix, const Vector &vector, int columnIndex)
Sets the column of the given matrix to the values of the given vector.
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.
static Matrix multiply(const Matrix &matrix1, const Matrix &matrix2)
Returns the product of two matrices.
static double innerProduct(const Vector &vector1, const Vector &vector2)
Computes the inner product of the given vectors.
static Matrix zeroMatrix(int rows, int columns)
Returns a matrix with given dimensions that is filled with zeroes.
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...
boost::numeric::ublas::symmetric_matrix< double, boost::numeric::ublas::upper > SymmetricMatrix
Definition for an Isis::LinearAlgebra::SymmetrixMatrix of doubles with an upper configuration.
static Vector rotate(const Vector &vector, const Vector &axis, Angle angle)
Rotates a vector about an axis vector given a specified angle.
QPair< Vector, Angle > AxisAngle
Definition for an Axis-Angle pair.
static double magnitude(const Vector &vector)
Computes the magnitude (i.e., the length) of the given vector using the Euclidean norm (L2 norm).
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,...
static Matrix pseudoinverse(const Matrix &matrix)
Returns the pseudoinverse of a matrix.
static double determinant(const Matrix &matrix)
Returns the determinant of the given 3x3 matrix.
static Matrix inverse(const Matrix &matrix)
Returns the inverse of a 2x2 or 3x3 matrix.
boost::numeric::ublas::vector< double > Vector
Definition for an Isis::LinearAlgebra::Vector of doubles.
static bool isIdentity(const Matrix &matrix)
Determines whether the given matrix is the identity.
static void setVec3(Vector *v, double v0, double v1, double v2)
Fills the first three elements of the given vector with the given values.
static bool isZero(const Matrix &matrix)
Determines whether the given matrix is filled with zereos.
QPair< Angle, int > EulerAngle
Definition for an EulerAngle pair.
static Matrix outerProduct(const Vector &vector1, const Vector &vector2)
Computes the outer product of the given vectors.
boost::numeric::ublas::matrix< double > Matrix
Definition for an Isis::LinearAlgebra::Matrix of doubles.
static bool isOrthogonal(const Matrix &matrix)
Determines whether the given matrix is orthogonal by verifying that the matrix and its tranpose are i...
boost::numeric::ublas::symmetric_matrix< double, boost::numeric::ublas::upper > MatrixUpperTriangular
Definition for an Isis::LinearAlgebra::MatrixUpperTriangular of doubles with an upper configuration.
static double absoluteMaximum(const Vector &vector)
Returns the maximum norm (L-infinity norm) for the given vector.
static Vector toQuaternion(const Matrix &rotationMatrix)
Converts a rotation's representation from a matrix to a unit quaternion.
static Matrix transpose(const Matrix &matrix)
Returns the transpose of the given matrix.
static double dotProduct(const Vector &vector1, const Vector &vector2)
Computes the dot product of the given vectors.
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.
static Vector normalize(const Vector &vector)
Returns a unit vector that is codirectional with the given vector by dividing each component of the v...
static bool isRotationMatrix(const Matrix &matrix)
Determines whether the given matrix is a rotation matrix.
This is free and unencumbered software released into the public domain.
Definition Apollo.h:16
QString toString(bool boolToConvert)
Global function to convert a boolean to a string.
Definition IString.cpp:211
QDebug operator<<(QDebug debug, const Hillshade &hillshade)
Print this class out to a QDebug object.
This is free and unencumbered software released into the public domain.