/usr/include/visp/vpMatrix.h is in libvisp-dev 2.8.0-4.
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 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 | /****************************************************************************
*
* $Id: vpMatrix.h 4056 2013-01-05 13:04:42Z fspindle $
*
* This file is part of the ViSP software.
* Copyright (C) 2005 - 2013 by INRIA. All rights reserved.
*
* This software is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* ("GPL") version 2 as published by the Free Software Foundation.
* See the file LICENSE.txt at the root directory of this source
* distribution for additional information about the GNU GPL.
*
* For using ViSP with software that can not be combined with the GNU
* GPL, please contact INRIA about acquiring a ViSP Professional
* Edition License.
*
* See http://www.irisa.fr/lagadic/visp/visp.html for more information.
*
* This software was developed at:
* INRIA Rennes - Bretagne Atlantique
* Campus Universitaire de Beaulieu
* 35042 Rennes Cedex
* France
* http://www.irisa.fr/lagadic
*
* If you have questions regarding the use of this file, please contact
* INRIA at visp@inria.fr
*
* This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
* WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
*
*
* Description:
* Matrix manipulation.
*
* Authors:
* Eric Marchand
*
*****************************************************************************/
#ifndef vpMatrix_H
#define vpMatrix_H
#include <visp/vpConfig.h>
#include <visp/vpTime.h>
#ifdef VISP_HAVE_GSL
# include <gsl/gsl_math.h>
# include <gsl/gsl_eigen.h>
#endif
#include <iostream>
#include <math.h>
class vpRowVector;
class vpColVector;
class vpTranslationVector;
class vpColVector;
class vpTranslationVector;
class vpRowVector;
/*!
\file vpMatrix.h
\brief Definition of matrix class as well as a set of operations on
these matrices.
*/
/*!
\class vpMatrix
\ingroup Matrix
\brief Definition of the vpMatrix class.
vpMatrix class provides a data structure for the matrices as well
as a set of operations on these matrices
\author Eric Marchand (IRISA - INRIA Rennes)
\warning Note the matrix in the class (*this) will be noted A in the comment
\ingroup libmath
\sa vpRowVector, vpColVector, vpHomogeneousMatrix, vpRotationMatrix,
vpTwistMatrix, vpHomography
*/
class VISP_EXPORT vpMatrix
{
public:
/*!
Method used to compute the determinant of a square matrix.
\sa det()
*/
typedef enum {
LU_DECOMPOSITION /*!< LU decomposition method. */
} vpDetMethod;
protected:
//! number of rows
unsigned int rowNum;
//! number of columns
unsigned int colNum;
public:
//! address of the first element of the data array
double *data;
protected:
//! address of the first element of each rows
double **rowPtrs;
//! Current size (rowNum * colNum)
unsigned int dsize;
//! Total row space
unsigned int trsize;
public:
//! Basic constructor
vpMatrix() ;
//! Constructor. Initialization of A as an r x c matrix with 0.
vpMatrix(unsigned int r, unsigned int c) ;
//! sub vpMatrix constructor
vpMatrix(const vpMatrix &m, unsigned int r, unsigned int c,
unsigned int nrows, unsigned int ncols) ;
//! Destructor (Memory de-allocation)
virtual ~vpMatrix();
//! Initialization of the object matrix
void init() ;
//! Destruction of the matrix (Memory de-allocation)
void kill() ;
// Initialize an identity matrix n-by-n
void eye(unsigned int n) ;
// Initialize an identity matrix m-by-n
void eye(unsigned int m, unsigned int n) ;
void setIdentity(const double & val=1.0) ;
//---------------------------------
// Set/get Matrix size
//---------------------------------
/** @name Set/get Matrix size */
//@{
//! Return the number of rows of the matrix
inline unsigned int getRows() const { return rowNum ;}
//! Return the number of columns of the matrix
inline unsigned int getCols() const { return colNum; }
// Set the size of the matrix A, initialization with a zero matrix
void resize(const unsigned int nrows, const unsigned int ncols,
const bool nullify = true);
double getMinValue() const;
double getMaxValue() const;
//@}
//---------------------------------
// Printing
//---------------------------------
friend VISP_EXPORT std::ostream &operator << (std::ostream &s,const vpMatrix &m);
/** @name Printing */
//@{
int print(std::ostream& s, unsigned int length, char const* intro=0);
std::ostream & matlabPrint(std::ostream & os);
std::ostream & maplePrint(std::ostream & os);
std::ostream & cppPrint(std::ostream & os, const char * matrixName = NULL, bool octet = false);
void printSize() { std::cout << getRows() <<" x " << getCols() <<" " ; }
//@}
static bool saveMatrix(const char *filename, const vpMatrix &M, const bool binary = false, const char *Header = "");
static bool loadMatrix(const char *filename, vpMatrix &M, const bool binary = false, char *Header = NULL);
/*!
Save a matrix to a file.
\param filename : absolute file name
\param M : matrix to be saved
\param binary :If true the matrix is save in a binary file, else a text file.
\param Header : optional line that will be saved at the beginning of the file
\return Returns true if no problem appends.
Warning : If you save the matrix as in a text file the precision is less than if you save it in a binary file.
*/
static inline bool saveMatrix(std::string filename, const vpMatrix &M,
const bool binary = false,
const char *Header = "")
{
return vpMatrix::saveMatrix(filename.c_str(), M, binary, Header);
}
/*!
Load a matrix to a file.
\param filename : absolute file name
\param M : matrix to be loaded
\param binary :If true the matrix is load from a binary file, else from a text file.
\param Header : Header of the file is load in this parameter
\return Returns true if no problem appends.
*/
static inline bool loadMatrix(std::string filename, vpMatrix &M,
const bool binary = false, char *Header = NULL)
{
return vpMatrix::loadMatrix(filename.c_str(), M, binary, Header);
}
//---------------------------------
// Copy / assignment
//---------------------------------
/** @name Copy / assignment */
//@{
//! Copy constructor
vpMatrix (const vpMatrix& m);
// Assignment from an array
vpMatrix &operator<<(double*);
//! Copy operator. Allow operation such as A = B
vpMatrix &operator=(const vpMatrix &B);
//! Set all the element of the matrix A to x
vpMatrix &operator=(const double x);
void diag(const vpColVector &A);
//@}
//---------------------------------
// Access/modification operators
//---------------------------------
/** @name Access/modification operators */
//@{
//! write elements Aij (usage : A[i][j] = x )
inline double *operator[](unsigned int n) { return rowPtrs[n]; }
//! read elements Aij (usage : x = A[i][j] )
inline double *operator[](unsigned int n) const {return rowPtrs[n];}
//@}
//---------------------------------
// Matrix operations (Static).
//---------------------------------
static void mult2Matrices(const vpMatrix &A, const vpMatrix &B, vpMatrix &C);
static void add2Matrices(const vpMatrix &A, const vpMatrix &B, vpMatrix &C);
static void add2WeightedMatrices(const vpMatrix &A, const double &wA, const vpMatrix &B,const double &wB, vpMatrix &C);
static void sub2Matrices(const vpMatrix &A, const vpMatrix &B, vpMatrix &C);
static void negateMatrix(const vpMatrix &A, vpMatrix &C);
static void multMatrixVector(const vpMatrix &A, const vpColVector &b, vpColVector &c);
static vpMatrix computeCovarianceMatrix(const vpMatrix &A, const vpColVector &x, const vpColVector &b);
static vpMatrix computeCovarianceMatrix(const vpMatrix &A, const vpColVector &x, const vpColVector &b, const vpMatrix &w);
//---------------------------------
// Matrix operations.
//---------------------------------
/** @name Matrix operations */
//@{
// operation A = A + B
vpMatrix &operator+=(const vpMatrix &B);
// operation A = A - B
vpMatrix &operator-=(const vpMatrix &B);
vpMatrix operator*(const vpMatrix &B) const;
vpMatrix operator+(const vpMatrix &B) const;
vpMatrix operator-(const vpMatrix &B) const;
vpMatrix operator-() const;
//---------------------------------
// Matrix/vector operations.
//---------------------------------
vpColVector operator*(const vpColVector &b) const;
// operation c = A * b (A is unchanged, c and b are translation vectors)
vpTranslationVector operator*(const vpTranslationVector &b) const;
//---------------------------------
// Matrix/real operations.
//---------------------------------
//! Add x to all the element of the matrix : Aij = Aij + x
vpMatrix &operator+=(const double x);
//! Substract x to all the element of the matrix : Aij = Aij - x
vpMatrix &operator-=(const double x);
//! Multiply all the element of the matrix by x : Aij = Aij * x
vpMatrix &operator*=(const double x);
//! Divide all the element of the matrix by x : Aij = Aij / x
vpMatrix &operator/=(double x);
// Cij = Aij * x (A is unchanged)
vpMatrix operator*(const double x) const;
// Cij = Aij / x (A is unchanged)
vpMatrix operator/(const double x) const;
//!return sum of the Aij^2 (for all i, for all j)
double sumSquare() const;
// return the determinant of the matrix.
double det(vpDetMethod method = LU_DECOMPOSITION) const;
//Compute the exponential matrix of a square matrix
vpMatrix expm();
//-------------------------------------------------
// Columns, Rows extraction, SubMatrix
//-------------------------------------------------
/** @name Columns, Rows extraction, Submatrix */
//@{
//! Row extraction
vpRowVector row(const unsigned int i);
//! Column extraction
vpColVector column(const unsigned int j);
//! subvpMatrix extraction
void init(const vpMatrix &m, unsigned int r, unsigned int c,
unsigned int nrows, unsigned int ncols);
//@}
//-------------------------------------------------
// transpose
//-------------------------------------------------
/** @name Transpose, Identity */
//@{
// Compute the transpose C = A^T
vpMatrix t() const;
// Compute the transpose C = A^T
vpMatrix transpose()const;
void transpose(vpMatrix & C )const;
vpMatrix AAt() const;
void AAt(vpMatrix &B) const;
vpMatrix AtA() const;
void AtA(vpMatrix &B) const;
//@}
//-------------------------------------------------
// Kronecker product
//-------------------------------------------------
/** @name Kronecker product */
//@{
// Stacks columns of a matrix in a vector
void stackColumns(vpColVector &out );
// Stacks columns of a matrix in a vector
vpColVector stackColumns();
// Stacks columns of a matrix in a vector
void stackRows(vpRowVector &out );
// Stacks columns of a matrix in a vector
vpRowVector stackRows();
// Compute Kronecker product matrix
void kron(const vpMatrix &m1 , vpMatrix &out);
// Compute Kronecker product matrix
vpMatrix kron(const vpMatrix &m1);
//@}
// Compute Kronecker product matrix
static void kron(const vpMatrix &m1, const vpMatrix &m2 , vpMatrix &out);
// Compute Kronecker product matrix
static vpMatrix kron(const vpMatrix &m1, const vpMatrix &m2 );
//-------------------------------------------------
// Matrix inversion
//-------------------------------------------------
/** @name Matrix inversion */
//@{
#ifndef DOXYGEN_SHOULD_SKIP_THIS
//! LU Decomposition
void LUDcmp(unsigned int* perm, int& d);
//! solve AX = B using the LU Decomposition
void LUBksb(unsigned int* perm, vpColVector& b);
#endif // doxygen should skip this
// inverse matrix A using the LU decomposition
vpMatrix inverseByLU() const;
#if defined(VISP_HAVE_LAPACK)
// inverse matrix A using the Cholesky decomposition (only for real symmetric matrices)
vpMatrix inverseByCholesky() const;
//lapack implementation of inverse by Cholesky
vpMatrix inverseByCholeskyLapack() const;
// inverse matrix A using the QR decomposition
vpMatrix inverseByQR() const;
//lapack implementation of inverse by QR
vpMatrix inverseByQRLapack() const;
#endif
//! Compute the pseudo inverse of the matrix using the SVD.
vpMatrix pseudoInverse(double svThreshold=1e-6) const;
//! Compute the pseudo inverse of the matrix using the SVD.
//! return the rank
unsigned int pseudoInverse(vpMatrix &Ap, double svThreshold=1e-6) const;
//! Compute the pseudo inverse of the matrix using the SVD.
//! return the rank and the singular value
unsigned int pseudoInverse(vpMatrix &Ap, vpColVector &sv, double svThreshold=1e-6) const ;
//! Compute the pseudo inverse of the matrix using the SVD.
//! return the rank and the singular value, image
unsigned int pseudoInverse(vpMatrix &Ap,
vpColVector &sv, double svThreshold,
vpMatrix &ImA,
vpMatrix &ImAt) const ;
//! Compute the pseudo inverse of the matrix using the SVD.
//! return the rank and the singular value, image, kernel.
unsigned int pseudoInverse(vpMatrix &Ap,
vpColVector &sv, double svThreshold,
vpMatrix &ImA,
vpMatrix &ImAt,
vpMatrix &kerA) const ;
//@}
//-------------------------------------------------
// SVD decomposition
//-------------------------------------------------
#ifndef DOXYGEN_SHOULD_SKIP_THIS
void svdFlake(vpColVector& w, vpMatrix& v);
void svdNr(vpColVector& w, vpMatrix& v);
#ifdef VISP_HAVE_GSL
void svdGsl(vpColVector& w, vpMatrix& v);
#endif
#if (VISP_HAVE_OPENCV_VERSION >= 0x020101) // Require opencv >= 2.1.1
void svdOpenCV(vpColVector& w, vpMatrix& v);
#endif
#ifdef VISP_HAVE_LAPACK
void svdLapack(vpColVector& W, vpMatrix& V);
#endif
//! solve AX=B using the SVD decomposition
void SVBksb(const vpColVector& w,
const vpMatrix& v,
const vpColVector& b, vpColVector& x);
#endif
/** @name SVD decomposition */
//@{
// singular value decomposition SVD
void svd(vpColVector& w, vpMatrix& v);
// solve Ax=B using the SVD decomposition (usage A = solveBySVD(B,x) )
void solveBySVD(const vpColVector &B, vpColVector &x) const ;
// solve Ax=B using the SVD decomposition (usage x=A.solveBySVD(B))
vpColVector solveBySVD(const vpColVector &B) const ;
unsigned int kernel(vpMatrix &KerA, double svThreshold=1e-6);
//@}
//-------------------------------------------------
// Eigen values and vectors
//-------------------------------------------------
/** @name Eigen values */
//@{
// compute the eigen values using the Gnu Scientific library
vpColVector eigenValues();
void eigenValues(vpColVector &evalue, vpMatrix &evector);
//@}
//! Stack two Matrices C = [ A B ]^T
static vpMatrix stackMatrices(const vpMatrix &A,const vpMatrix &B) ;
//! Stack two Matrices C = [ A B ]^T
static void stackMatrices(const vpMatrix &A,const vpMatrix &B, vpMatrix &C) ;
// Juxtapose to matrices C = [ A B ]
static vpMatrix juxtaposeMatrices(const vpMatrix &A,const vpMatrix &B) ;
// Juxtapose to matrices C = [ A B ]
static void juxtaposeMatrices(const vpMatrix &A,const vpMatrix &B, vpMatrix &C) ;
// Create a diagonal matrix with the element of a vector DAii = Ai
static void createDiagonalMatrix(const vpColVector &A, vpMatrix &DA) ;
// Stack the matrix A below the current one, copy if not initialized this = [ this A ]^T
void stackMatrices(const vpMatrix &A);
// Insert matrix A in the current matrix at the given position (r, c).
void insert(const vpMatrix&A, const unsigned int r, const unsigned int c);
// Insert matrix B in matrix A at the given position (r, c).
static vpMatrix insert(const vpMatrix &A,const vpMatrix &B, const unsigned int r, const unsigned int c) ;
// Insert matrix B in matrix A (not modified) at the given position (r, c), the result is given in matrix C.
static void insert(const vpMatrix &A, const vpMatrix &B, vpMatrix &C, const unsigned int r, const unsigned int c) ;
// -------------------------
// Norms
// -------------------------
/** @name Norms */
//@{
// Euclidean norm ||x||=sqrt(sum(x_i^2))
double euclideanNorm () const;
// Infinity norm ||x||=max(sum(fabs(x_i)))
double infinityNorm () const;
//@}
private:
double detByLU() const;
};
//////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////
//! multiplication by a scalar C = x*A
VISP_EXPORT vpMatrix operator*(const double &x, const vpMatrix &A) ;
//! multiplication by a scalar C = x*A
VISP_EXPORT vpColVector operator*(const double &x, const vpColVector &A) ;
#endif
/*
* Local variables:
* c-basic-offset: 2
* End:
*/
|