/usr/include/tulip/cxx/Matrix.cxx is in libtulip-dev 3.1.2-2.3ubuntu3.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 | //-*-c++-*-
/**
Authors: David Auber, Patrick Mary, Morgan Mathiaut
from the LaBRI Visualization Team
Email : auber@tulip-software.org
Last modification : 13/03/2009
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
*/
#include <cmath>
#define MATRIXTLPGEO tlp::Matrix<Obj,SIZE>
//===================================================================
//Specialisation
namespace tlp {
template<typename Obj>
class Matrix<Obj,1>:public Vector<Vector<Obj,1>,1> {
public:
Obj determinant() {return (*this)[0][0];}
// Matrix<Obj,1>& fill(Obj obj) {return *this;}
Matrix<Obj,1>& inverse() {(*this)[0][0] = 1.0 / (*this)[0][0]; return *this;}
Matrix<Obj,1>& transpose() {return *this;}
Matrix<Obj,1>& operator*=(const Matrix<Obj,1> &mat) {(*this)[0][0] *= mat[0][0]; return *this;}
// Matrix<Obj,1>& operator/=(const Obj &obj){return *this;}
Matrix<Obj,1> cofactor() {return *this;}
};
}
//===================================================================
template<typename Obj,unsigned int SIZE>
MATRIXTLPGEO::Matrix(const std::vector<std::vector<Obj> > &covarianceMatrix)
{
for(unsigned int i=0; i < SIZE; i++)
for(unsigned int j=0; j < SIZE; j++)
(*this)[i][j] = covarianceMatrix[i][j] / (sqrt(covarianceMatrix[i][i] * covarianceMatrix[j][j]));
}
//===================================================================
template<typename Obj,unsigned int SIZE>
MATRIXTLPGEO & MATRIXTLPGEO::fill(Obj obj) {
for (unsigned int i=0; i<SIZE; ++i)
(*this)[i].fill(obj);
return (*this);
}
//===================================================================
template<typename Obj,unsigned int SIZE>
MATRIXTLPGEO & MATRIXTLPGEO::operator*=(const MATRIXTLPGEO &mat) {
MATRIXTLPGEO tmpMat(*this);
MATRIXTLPGEO *pMat = (MATRIXTLPGEO *) &mat;
if (pMat == this)
pMat = new MATRIXTLPGEO(*this);
for (unsigned int i=0;i<SIZE;++i)
for (unsigned int j=0;j<SIZE;++j) {
Obj tmpObj = (Obj) 0;
for (unsigned int k=0;k<SIZE;++k)
tmpObj+=tmpMat[i][k]*(*pMat)[k][j];
(*this)[i][j] = tmpObj;
}
if (pMat != &mat)
delete pMat;
return (*this);
}
//=====================================================================================
template<typename Obj,unsigned int SIZE>
MATRIXTLPGEO & MATRIXTLPGEO::operator*=(const Obj &obj) {
for (unsigned int i=0;i<SIZE;++i)
(*this)[i] *= obj;
return (*this);
}
//=====================================================================================
template<typename Obj,unsigned int SIZE>
MATRIXTLPGEO & MATRIXTLPGEO::operator/=(const MATRIXTLPGEO &mat) {
MATRIXTLPGEO tmpMat(mat);
tmpMat.inverse();
(*this) *= tmpMat;
return (*this);
}
//=====================================================================================
template<typename Obj,unsigned int SIZE>
MATRIXTLPGEO & MATRIXTLPGEO::operator/=(const Obj &obj) {
for (unsigned int i=0;i<SIZE;++i)
(*this)[i] /= obj;
return (*this);
}
//=====================================================================================
template<typename Obj,unsigned int SIZE>
Obj MATRIXTLPGEO::determinant() const {
switch (SIZE) {
case 2:
return (*this)[0][0] * (*this)[1][1] - (*this)[1][0] * (*this)[0][1];
break;
case 3:
return (*this)[0][0] * ((*this)[1][1]*(*this)[2][2] - (*this)[1][2] * (*this)[2][1])
- (*this)[0][1] * ((*this)[1][0]*(*this)[2][2] - (*this)[1][2] * (*this)[2][0])
+ (*this)[0][2] * ((*this)[1][0]*(*this)[2][1] - (*this)[1][1] * (*this)[2][0]) ;
break;
default:
int j2;
Obj det = 0;
for (unsigned int j1=0; j1<SIZE; ++j1) {
tlp::Matrix<Obj, SIZE - 1> m;
for (unsigned int i=1; i<SIZE; i++) {
j2 = 0;
for (unsigned int j=0; j<SIZE; ++j) {
if (j == j1)
continue;
m[i-1][j2] = (*this)[i][j];
++j2;
}
}
if (j1 & 1)
det += (*this)[0][j1] * m.determinant();
else
det -= (*this)[0][j1] * m.determinant();
}
return(det);
}
}
//=====================================================================================
template<typename Obj,unsigned int SIZE>
MATRIXTLPGEO MATRIXTLPGEO::cofactor() const{
MATRIXTLPGEO result;
switch (SIZE){
case 2:
(result)[0][0] = (*this)[1][1];
(result)[0][1] = - (*this)[1][0];
(result)[1][0] = - (*this)[0][1];
(result)[1][1] = (*this)[0][0];
break;
case 3:
(result)[0][0] = (*this)[1][1]*(*this)[2][2] - (*this)[1][2]*(*this)[2][1];
(result)[0][1] = - ((*this)[1][0]*(*this)[2][2] - (*this)[2][0]*(*this)[1][2]);
(result)[0][2] = (*this)[1][0]*(*this)[2][1] - (*this)[1][1]*(*this)[2][0];
(result)[1][0] = - ((*this)[0][1]*(*this)[2][2] - (*this)[0][2]*(*this)[2][1]);
(result)[1][1] = (*this)[0][0]*(*this)[2][2] - (*this)[0][2]*(*this)[2][0];
(result)[1][2] = - ((*this)[0][0]*(*this)[2][1] - (*this)[0][1]*(*this)[2][0]);
(result)[2][0] = (*this)[0][1]*(*this)[1][2] - (*this)[0][2]*(*this)[1][1];
(result)[2][1] = - ((*this)[0][0]*(*this)[1][2] - (*this)[0][2]*(*this)[1][0]);
(result)[2][2] = (*this)[0][0]*(*this)[1][1] - (*this)[0][1]*(*this)[1][0];
break;
default :
int i1,j1;
tlp::Matrix<Obj,SIZE - 1> c;
for (unsigned int j=0;j<SIZE;++j) {
for (unsigned int i=0;i<SIZE;++i) {
i1 = 0;
for (unsigned int ii=0;ii<SIZE;++ii) {
if (ii == i)
continue;
j1 = 0;
for (unsigned int jj=0;jj<SIZE;jj++) {
if (jj == j)
continue;
c[i1][j1] = (*this)[ii][jj];
++j1;
}
++i1;
}
if ((i+j) & 1) result[i][j]=c.determinant(); else result[i][j]=-c.determinant();
}
}
break;
}
return result;
}
//=====================================================================================
template<typename Obj,unsigned int SIZE>
MATRIXTLPGEO & MATRIXTLPGEO::transpose() {
register Obj tmp;
for (unsigned int i=1; i<SIZE; ++i) {
for (unsigned int j=0; j<i; ++j) {
tmp = (*this)[i][j];
(*this)[i][j] = (*this)[j][i];
(*this)[j][i] = tmp;
}
}
return (*this);
}
//=====================================================================================
template<typename Obj,unsigned int SIZE>
MATRIXTLPGEO & MATRIXTLPGEO::inverse() {
(*this) = (*this).cofactor().transpose() /= (*this).determinant();
return (*this);
}
//=====================================================================================
template<typename Obj,unsigned int SIZE>
MATRIXTLPGEO tlp::operator*(const MATRIXTLPGEO &mat1 ,const MATRIXTLPGEO &mat2) {
return MATRIXTLPGEO(mat1)*=mat2;
}
//=====================================================================================
template<typename Obj,unsigned int SIZE>
MATRIXTLPGEO MATRIXTLPGEO::operator/(const MATRIXTLPGEO &mat2) const{
return MATRIXTLPGEO(*this)/=mat2;
}
//=====================================================================================
template<typename Obj,unsigned int SIZE>
MATRIXTLPGEO MATRIXTLPGEO::operator/(const Obj &obj) const{
return MATRIXTLPGEO(*this) /= obj;
}
//=====================================================================================
template<typename Obj,unsigned int SIZE>
MATRIXTLPGEO tlp::operator*(const MATRIXTLPGEO &mat ,const Obj &obj) {
return MATRIXTLPGEO(mat) *= obj;
}
//=====================================================================================
template<typename Obj, unsigned int SIZE>
tlp::Vector<Obj, SIZE> tlp::operator*( const MATRIXTLPGEO &mat , const tlp::Vector<Obj, SIZE> &vec) {
tlp::Vector<Obj,SIZE> result;
for (unsigned int row=0; row<SIZE; ++row) {
result[row] = mat[row][0] * vec[0];
}
for (unsigned int col=1; col<SIZE; ++col) {
for (unsigned int row=0; row<SIZE; ++row) {
result[row] += mat[row][col] * vec[col];
}
}
return result;
}
//=====================================================================================
template<typename Obj,unsigned int SIZE>
tlp::Vector<Obj,SIZE> tlp::operator*( const tlp::Vector<Obj,SIZE> &vec, const MATRIXTLPGEO &mat) {
tlp::Vector<Obj,SIZE> result;
for (unsigned int row=0; row<SIZE; ++row) {
result[row] = mat[0][row] * vec[0];
}
for (unsigned int col=1; col<SIZE; ++col) {
for (unsigned int row=0; row<SIZE; ++row) {
result[row] += mat[col][row] * vec[col];
}
}
return result;
}
//=====================================================================================
template<typename Obj, unsigned int SIZE>
tlp::Vector<Obj, SIZE> MATRIXTLPGEO::powerIteration(const int nIterations) const
{
tlp::Vector<Obj, SIZE> iteration;
for(unsigned int i=0; i < SIZE; i++)
iteration[i] = 1;
for(unsigned int i=0; i < nIterations; i++)
{
iteration = (*this) * iteration;
iteration /= iteration.norm();
}
return iteration;
}
//=====================================================================================
template<typename Obj, unsigned int SIZE>
bool MATRIXTLPGEO::simplify(tlp::Matrix<Obj, 2> &simplifiedMatrix) const
{
if (SIZE != 3)
{
std::cerr << "Computation allowed only for 3x3 Matrices. Yours sizes : " << SIZE << "x" << SIZE << std::endl;
return false;
}
// We start with a matrix representing an equation system under the following form :
//
// ax + by + cz = 0
// dx + ey + fz = 0
// gx + hy + iz = 0
//
// So M looks like :
//
// ( ax by cz ) *(e1)*
// M = ( dx ey fz ) *(e2)*
// ( gx hy iz ) *(e3)*
//
// What we want is something like that :
//
// jx + ky = 0
// lx + mz = 0
//
// So to reduce the matrix, we will use the Gaussian Elimination.
// For the first line we will apply a Gaussian Elimination between (e1) and (e2)
// For the second line we will apply a Gaussian Elimination between (e1) and (e3)
float coeff;
// First Gaussian Elimination :
// The pivot is z
coeff = (*this)[1][2] / (*this)[0][2]; // fz / cz
// After that:
// jx = dx - (coeff * ax)
// ky = ey - (coeff * by)
simplifiedMatrix[0][0] = (*this)[1][0] - (coeff * (*this)[0][0]);
simplifiedMatrix[0][1] = (*this)[1][1] - (coeff * (*this)[0][1]);
// Second Gaussian Elimination :
// The pivot is y
coeff = (*this)[2][1] / (*this)[0][1]; // hy / by
// Idem :
// lx = gx - (coeff * ax)
// mz = iz - (coeff * cz)
simplifiedMatrix[1][0] = (*this)[2][0] - (coeff * (*this)[0][0]);
simplifiedMatrix[1][1] = (*this)[2][2] - (coeff * (*this)[0][2]);
return true;
}
//=====================================================================================
template<typename Obj, unsigned int SIZE>
bool MATRIXTLPGEO::computeEigenVector(const float x, tlp::Vector<Obj, 3> &eigenVector) const
{
if (SIZE != 2)
{
std::cerr << "Computation allowed only for 2x2 Matrices. Yours sizes : " << SIZE << "x" << SIZE << std::endl;
return false;
}
eigenVector[0] = x; // Fixed by user
// We know that the matrix we are using is under that form :
//
// ( ax by )
// M = ( )
// ( cx dz )
//
// Since we have a fixed x, we can compute y and z :
//
// y = -a / b
// z = -c / d
float a, b, c, d;
a = (*this)[0][0];
b = (*this)[0][1];
c = (*this)[1][0];
d = (*this)[1][1];
eigenVector[1] = (-a * x) / b;
eigenVector[2] = (-c * x) / d;
return true;
}
|