7 #include <boost/algorithm/string.hpp>
9 #include <gsl/gsl_math.h>
10 #include <gsl/gsl_eigen.h>
11 #include <gsl/gsl_linalg.h>
20 void eigenDecomposition(Matrix3 &eVectors, Vector3& eValues,
const Matrix3& sym)
23 for (
int i = 0; i < 3; ++i)
for (
int j = 0; j < 3; ++j)
24 data[i*3+j] = sym(i,j);
27 = gsl_matrix_view_array (data, 3, 3);
29 gsl_vector *eval = gsl_vector_alloc (3);
30 gsl_matrix *evec = gsl_matrix_alloc (3, 3);
32 gsl_eigen_symmv_workspace * w =
33 gsl_eigen_symmv_alloc (3);
35 gsl_eigen_symmv (&m.matrix, eval, evec, w);
37 gsl_eigen_symmv_free (w);
39 gsl_eigen_symmv_sort (eval, evec,
40 GSL_EIGEN_SORT_ABS_DESC);
42 for (
int i = 0; i < 3; i++)
45 = gsl_vector_get (eval, i);
47 gsl_vector_view evec_i
48 = gsl_matrix_column (evec, i);
49 for (
int j = 0; j < 3; ++j)
50 eVectors(j, i) = gsl_vector_get(&evec_i.vector, j);
54 if (Vector3(eVectors.GetColumn(0)).Cross(eVectors.GetColumn(1)).Dot(eVectors.GetColumn(2)) < 0)
55 eVectors.SetColumn(2, -Vector3(eVectors.GetColumn(2)));
57 gsl_vector_free (eval);
58 gsl_matrix_free (evec);
66 double determinant(
const GMatrix& m)
68 NUKLEI_FAST_ASSERT(m.GetRows() == m.GetColumns());
69 const int dim = m.GetRows();
72 gsl_permutation *perm;
75 lu = gsl_matrix_alloc(dim, dim);
76 perm = gsl_permutation_alloc(dim);
78 for (
int i = 0; i < dim; ++i)
79 for (
int j = 0; j < dim; ++j)
81 gsl_matrix_set(lu, i, j, m(i,j));
84 gsl_linalg_LU_decomp(lu, perm, &signum);
85 double det = gsl_linalg_LU_det(lu, signum);
88 gsl_permutation_free(perm);
93 GMatrix inverse(
const GMatrix& m)
95 NUKLEI_FAST_ASSERT(m.GetRows() == m.GetColumns());
96 const int dim = m.GetRows();
98 gsl_matrix *lu, *inverse;
99 gsl_permutation *perm;
102 lu = gsl_matrix_alloc(dim, dim);
103 inverse = gsl_matrix_alloc(dim, dim);
104 perm = gsl_permutation_alloc(dim);
106 for (
int i = 0; i < dim; ++i)
107 for (
int j = 0; j < dim; ++j)
109 gsl_matrix_set(lu, i, j, m(i,j));
112 gsl_linalg_LU_decomp(lu, perm, &signum);
113 gsl_linalg_LU_invert(lu, perm, inverse);
115 GMatrix inv(dim, dim);
116 for (
int i = 0; i < dim; ++i)
117 for (
int j = 0; j < dim; ++j)
119 inv(i,j) = gsl_matrix_get(inverse, i, j);
123 gsl_matrix_free(inverse);
124 gsl_permutation_free(perm);
131 std::ostream& operator<<(std::ostream &out,
const GMatrix &m)
133 for (
int i = 0; i < m.GetRows(); ++i)
135 for (
int j = 0; j < m.GetColumns(); ++j)
143 std::istream& operator>>(std::istream &in, GMatrix &m)
145 std::vector< std::vector<double> > rows;
148 while (std::getline(in, line))
151 std::vector<std::string> tokens;
152 boost::split(tokens, line, boost::is_any_of(
" "), boost::token_compress_on);
153 if (tokens.front() ==
"")
154 tokens.erase(tokens.begin());
155 if (tokens.back() ==
"")
159 ntok = tokens.size();
163 if (tokens.size() == 0)
166 rows.push_back(std::vector<double>());
167 for (std::vector<std::string>::const_iterator i = tokens.begin();
168 i != tokens.end(); ++i)
169 rows.back().push_back(numify<double>(*i));
172 m.SetSize(rows.size(), ntok);
173 for (
int i = 0; i < m.GetRows(); ++i)
174 for (
int j = 0; j < m.GetColumns(); ++j)
175 m(i,j) = rows.at(i).at(j);