10#include "jama/jama_svd.h"
11#include "jama/jama_eig.h"
12#include "jama/jama_lu.h"
15#include "IException.h"
26 std::string m =
"Invalid matrix dimensions";
29 p_matrix = TNT::Array2D<double>(n, m, value);
36 p_matrix = matrix.copy();
47 std::string m =
"Invalid matrix dimensions";
51 for(
int i = 0; i < identity.Rows(); i++) {
61 if(Rows() != Columns()) {
62 std::string m =
"Unable to calculate the determinant, the matrix is not square.";
65 JAMA::LU<double> lu(p_matrix);
73 if(Rows() != Columns()) {
74 std::string m =
"Unable to calculate the trace, the matrix is not square.";
78 for(
int i = 0; i < Rows(); i++) {
79 trace += p_matrix[i][i];
88 if(Columns() != matrix.Rows()) {
89 std::string m =
"Incompatible matrix dimensions, cannot multiply the matrices.";
92 TNT::Array2D<double> m(matrix.Rows(), matrix.Columns());
93 for(
int i = 0; i < m.dim1(); i++) {
94 for(
int j = 0; j < m.dim2(); j++) {
95 m[i][j] = matrix[i][j];
98 return Matrix(TNT::matmult(p_matrix, m));
105 if(Rows() != matrix.Rows() || Columns() != matrix.Columns()) {
106 std::string m =
"Incompatible matrix dimensions, cannot add the matrices.";
109 TNT::Array2D<double> m(matrix.Rows(), matrix.Columns());
110 for(
int i = 0; i < m.dim1(); i++) {
111 for(
int j = 0; j < m.dim2(); j++) {
112 m[i][j] = matrix[i][j];
115 return Matrix(p_matrix + m);
122 if(Rows() != matrix.Rows() || Columns() != matrix.Columns()) {
123 std::string m =
"Incompatible matrix dimensions, cannot subtract the matrices.";
126 TNT::Array2D<double> m(matrix.Rows(), matrix.Columns());
127 for(
int i = 0; i < m.dim1(); i++) {
128 for(
int j = 0; j < m.dim2(); j++) {
129 m[i][j] = matrix[i][j];
133 return Matrix(p_matrix - m);
141 if(Rows() != matrix.Rows() || Columns() != matrix.Columns()) {
142 std::string m =
"Incompatible matrix dimensions, cannot multiply the matrices.";
145 TNT::Array2D<double> m(matrix.Rows(), matrix.Columns());
146 for(
int i = 0; i < m.dim1(); i++) {
147 for(
int j = 0; j < m.dim2(); j++) {
148 m[i][j] = matrix[i][j];
151 return Matrix(p_matrix * m);
158 Matrix product(Rows(), Columns());
159 for(
int i = 0; i < Rows(); i++) {
160 for(
int j = 0; j < Columns(); j++) {
161 product[i][j] = p_matrix[i][j] * scalar;
171 TNT::Array2D<double> transpose(p_matrix.dim2(), p_matrix.dim1());
172 for(
int i = 0; i < transpose.dim1(); i++) {
173 for(
int j = 0; j < transpose.dim2(); j++) {
174 transpose[i][j] = p_matrix[j][i];
184 if(Rows() != Columns()) {
185 std::string m =
"Unable to calculate the inverse, the matrix is not square.";
188 TNT::Array2D<double> id(p_matrix.dim1(), p_matrix.dim2(), 0.0);
189 for(
int i = 0; i < p_matrix.dim1(); i++)
id[i][i] = 1;
191 JAMA::LU<double> lu(p_matrix);
192 if(lu.det() == 0.0) {
193 std::string m =
"Cannot take the inverse of the matrix";
197 return Matrix(lu.solve(
id));
204 if(Rows() != Columns()) {
205 std::string m =
"Unable to calculate eigenvalues, the matrix is not square.";
208 JAMA::Eigenvalue<double>
E(p_matrix);
209 TNT::Array2D<double> D;
212 std::vector<double> eigenvalues(D.dim1());
213 for(
int i = 0; i < D.dim1(); i++) {
214 eigenvalues[i] = D[i][i];
225 if(Rows() != Columns()) {
226 std::string m =
"Unable to calculate eigenvectors, the matrix is not square.";
229 JAMA::Eigenvalue<double>
E(p_matrix);
230 TNT::Array2D<double> V;
240 for(
int i = 0; i < matrix.Rows(); i++) {
241 for(
int j = 0; j < matrix.Columns(); j++) {
243 if(j < matrix.Columns() - 1) os <<
" ";
245 if(i < matrix.Rows() - 1) os << endl;
@ Programmer
This error is for when a programmer made an API call that was illegal.
double Determinant()
Compute the determinant of the matrix.
std::vector< double > Eigenvalues()
Compute the eigenvalues of the matrix.
Matrix Transpose()
Compute the transpose of the matrix.
Matrix Subtract(Matrix &matrix)
Subtract the input matrix from this matrix.
Matrix Eigenvectors()
Compute the eigenvectors of the matrix and return them as columns of a matrix in ascending order.
Matrix(const int n, const int m, const double value=0.0)
Constructs an n x m Matrix containing the specified default value.
double Trace()
Compute the determinant of the matrix.
~Matrix()
Destroys the Matrix object.
Matrix Inverse()
Compute the inverse of the matrix.
Matrix Add(Matrix &matrix)
Add the two matrices.
static Matrix Identity(const int n)
Create an n x n identity matrix.
Matrix MultiplyElementWise(Matrix &matrix)
Multiply the two matrices element-wise (ie compute C such that C[i][j] = A[i][j]*B[i][j])
Matrix Multiply(Matrix &matrix)
Multiply the two matrices.
const double E
Sets some basic constants for use in ISIS programming.
This is free and unencumbered software released into the public domain.
QDebug operator<<(QDebug debug, const Hillshade &hillshade)
Print this class out to a QDebug object.