IVT
LinearAlgebra.h
Go to the documentation of this file.
1 // ****************************************************************************
2 // This file is part of the Integrating Vision Toolkit (IVT).
3 //
4 // The IVT is maintained by the Karlsruhe Institute of Technology (KIT)
5 // (www.kit.edu) in cooperation with the company Keyetech (www.keyetech.de).
6 //
7 // Copyright (C) 2014 Karlsruhe Institute of Technology (KIT).
8 // All rights reserved.
9 //
10 // Redistribution and use in source and binary forms, with or without
11 // modification, are permitted provided that the following conditions are met:
12 //
13 // 1. Redistributions of source code must retain the above copyright
14 // notice, this list of conditions and the following disclaimer.
15 //
16 // 2. Redistributions in binary form must reproduce the above copyright
17 // notice, this list of conditions and the following disclaimer in the
18 // documentation and/or other materials provided with the distribution.
19 //
20 // 3. Neither the name of the KIT nor the names of its contributors may be
21 // used to endorse or promote products derived from this software
22 // without specific prior written permission.
23 //
24 // THIS SOFTWARE IS PROVIDED BY THE KIT AND CONTRIBUTORS “AS IS” AND ANY
25 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
26 // WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
27 // DISCLAIMED. IN NO EVENT SHALL THE KIT OR CONTRIBUTORS BE LIABLE FOR ANY
28 // DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
29 // (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
30 // LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
31 // ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
32 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
33 // THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34 // ****************************************************************************
35 // ****************************************************************************
36 // Filename: LinearAlgebra.h
37 // Author: Pedram Azad
38 // Date: 23.01.2008
39 // ****************************************************************************
40 
44 #ifndef _LINEAR_ALGEBRA_H_
45 #define _LINEAR_ALGEBRA_H_
46 
47 
48 // ****************************************************************************
49 // Forward declarations
50 // ****************************************************************************
51 
52 class CFloatMatrix;
53 class CFloatVector;
54 class CDoubleMatrix;
55 class CDoubleVector;
56 struct Mat3d;
57 struct Vec2d;
58 
59 
60 
61 // ****************************************************************************
62 // LinearAlgebra
63 // ****************************************************************************
64 
69 namespace LinearAlgebra
70 {
71  // operating on CFloatMatrix and CFloatVector
72  void Zero(CFloatMatrix *pMatrix);
73  void Zero(CFloatVector *pVector);
74 
75  void SelfProduct(const CFloatMatrix *pMatrix, CFloatMatrix *pResultMatrix, bool AAT = false);
76  void MulMatMat(const CFloatMatrix *A, const CFloatMatrix *B, CFloatMatrix *pResultMatrix, bool bTransposeB = false);
77  void Transpose(const CFloatMatrix *pMatrix, CFloatMatrix *pResultMatrix);
78 
79  void MulMatVec(const CFloatMatrix *pMatrix, const CFloatVector *pVector, CFloatVector *pResultVector);
80  void MulMatVec(const CFloatMatrix *pMatrix, const CFloatVector *pVector1, const CFloatVector *pVector2, CFloatVector *pResultVector);
81 
82  void AddMatMat(const CFloatMatrix *pMatrix1, const CFloatMatrix *pMatrix2, CFloatMatrix *pResultMatrix);
83  void SubtractMatMat(const CFloatMatrix *pMatrix1, const CFloatMatrix *pMatrix2, CFloatMatrix *pResultMatrix);
84  void AddVecVec(const CFloatVector *pVector1, const CFloatVector *pVector2, CFloatVector *pResultVector);
85  void SubtractVecVec(const CFloatVector *pVector1, const CFloatVector *pVector2, CFloatVector *pResultVector);
86 
87  void AddToMat(CFloatMatrix *pMatrix, const CFloatMatrix *pMatrixToAdd);
88  void SubtractFromMat(CFloatMatrix *pMatrix, const CFloatMatrix *pMatrixToSubtract);
89  void AddToVec(CFloatVector *pVector, const CFloatVector *pVectorToAdd);
90  void SubtractFromVec(CFloatVector *pVector, const CFloatVector *pVectorToSubtract);
91 
92  void SubtractMeanFromColumns(const CFloatMatrix *pMatrix, CFloatMatrix *pResultMatrix);
93  void SubtractMeanFromRows(const CFloatMatrix *pMatrix, CFloatMatrix *pResultMatrix);
94 
95  extern void SVD(const CFloatMatrix *A, CFloatMatrix *W, CFloatMatrix *U = 0, CFloatMatrix *V = 0,
96  bool bAllowModifyA = false, bool bReturnUTransposed = false, bool bReturnVTransposed = false);
97 
98  // linear least squares
102 
103  // calculation of pseudoinverse
104  void CalculatePseudoInverseSVD(const CFloatMatrix *pInputMatrix, CFloatMatrix *pOutputMatrix);
105  bool CalculatePseudoInverseSimple(const CFloatMatrix *pInputMatrix, CFloatMatrix *pResultMatrix);
106 
107  // calculation of inverse
108  bool Invert(const CFloatMatrix *pInputMatrix, CFloatMatrix *pResultMatrix);
109 
110  // PCA
111  void PCA(const CFloatMatrix *pData, CFloatMatrix *pTransformationMatrix, CFloatMatrix *pTransformedData, int nTargetDimension);
112  void PCA(const CFloatMatrix *pData, CFloatMatrix *pTransformationMatrix, CFloatMatrix *pEigenValues);
113 
114 
115  // operating on CDoubleMatrix and CDoubleVector
116  void Zero(CDoubleMatrix *pMatrix);
117  void Zero(CDoubleVector *pVector);
118 
119  void SelfProduct(const CDoubleMatrix *pMatrix, CDoubleMatrix *pResultMatrix, bool AAT = false);
120  void MulMatMat(const CDoubleMatrix *pMatrix1, const CDoubleMatrix *pMatrix2, CDoubleMatrix *pResultMatrix, bool bTransposeB = false);
121  void Transpose(const CDoubleMatrix *pMatrix, CDoubleMatrix *pResultMatrix);
122 
123  void MulMatVec(const CDoubleMatrix *pMatrix, const CDoubleVector *pVector, CDoubleVector *pResultVector);
124  void MulMatVec(const CDoubleMatrix *pMatrix, const CDoubleVector *pVector1, const CDoubleVector *pVector2, CDoubleVector *pResultVector);
125 
126  void AddMatMat(const CDoubleMatrix *pMatrix1, const CDoubleMatrix *pMatrix2, CDoubleMatrix *pResultMatrix);
127  void SubtractMatMat(const CDoubleMatrix *pMatrix1, const CDoubleMatrix *pMatrix2, CDoubleMatrix *pResultMatrix);
128  void AddVecVec(const CDoubleVector *pVector1, const CDoubleVector *pVector2, CDoubleVector *pResultVector);
129  void SubtractVecVec(const CDoubleVector *pVector1, const CDoubleVector *pVector2, CDoubleVector *pResultVector);
130 
131  void AddToMat(CDoubleMatrix *pMatrix, const CDoubleMatrix *pMatrixToAdd);
132  void SubtractFromMat(CDoubleMatrix *pMatrix, const CDoubleMatrix *pMatrixToSubtract);
133  void AddToVec(CDoubleVector *pVector, const CDoubleVector *pVectorToAdd);
134  void SubtractFromVec(CDoubleVector *pVector, const CDoubleVector *pVectorToSubtract);
135 
136  void SubtractMeanFromColumns(const CDoubleMatrix *pMatrix, CDoubleMatrix *pResultMatrix);
137  void SubtractMeanFromRows(const CDoubleMatrix *pMatrix, CDoubleMatrix *pResultMatrix);
138 
139  extern void SVD(const CDoubleMatrix *A, CDoubleMatrix *W, CDoubleMatrix *U = 0, CDoubleMatrix *V = 0,
140  bool bAllowModifyA = false, bool bReturnUTransposed = false, bool bReturnVTransposed = false);
141 
142  // linear least squares
146 
147  // calculation of pseudoinverse
148  void CalculatePseudoInverseSVD(const CDoubleMatrix *pInputMatrix, CDoubleMatrix *pOutputMatrix);
149  bool CalculatePseudoInverseSimple(const CDoubleMatrix *pInputMatrix, CDoubleMatrix *pResultMatrix);
150 
151  // calculation of inverse
152  bool Invert(const CDoubleMatrix *pInputMatrix, CDoubleMatrix *pResultMatrix);
153 
154 
155 
156  // 2d homographies
157 
185  bool DetermineAffineTransformation(const Vec2d *pSourcePoints, const Vec2d *pTargetPoints, int nPoints, Mat3d &A, bool bUseSVD = false);
186 
214  bool DetermineHomography(const Vec2d *pSourcePoints, const Vec2d *pTargetPoints, int nPoints, Mat3d &A, bool bUseSVD = false);
215 }
216 
217 
218 
219 #endif /* _LINEAR_ALGEBRA_H_ */
void SVD(const CFloatMatrix *A, CFloatMatrix *W, CFloatMatrix *U=0, CFloatMatrix *V=0, bool bAllowModifyA=false, bool bReturnUTransposed=false, bool bReturnVTransposed=false)
Definition: SVD.cpp:1660
void SelfProduct(const CFloatMatrix *pMatrix, CFloatMatrix *pResultMatrix, bool AAT=false)
Data structure for the representation of a 2D vector.
Definition: Math2d.h:82
bool SolveLinearLeastSquaresSimple(const CFloatMatrix *A, const CFloatVector *b, CFloatVector *x)
void MulMatMat(const CFloatMatrix *A, const CFloatMatrix *B, CFloatMatrix *pResultMatrix, bool bTransposeB=false)
void PCA(const CFloatMatrix *pData, CFloatMatrix *pTransformationMatrix, CFloatMatrix *pTransformedData, int nTargetDimension)
void SubtractMeanFromColumns(const CFloatMatrix *pMatrix, CFloatMatrix *pResultMatrix)
Data structure for the representation of a matrix of values of the data type double.
Definition: DoubleMatrix.h:54
void SubtractFromMat(CFloatMatrix *pMatrix, const CFloatMatrix *pMatrixToSubtract)
Data structure for the representation of a vector of values of the data type double.
Definition: DoubleVector.h:54
void SubtractFromVec(CFloatVector *pVector, const CFloatVector *pVectorToSubtract)
bool Invert(const CFloatMatrix *pInputMatrix, CFloatMatrix *pResultMatrix)
bool DetermineAffineTransformation(const Vec2d *pSourcePoints, const Vec2d *pTargetPoints, int nPoints, Mat3d &A, bool bUseSVD=false)
Determines an affine transformation based on a set of 2d-2d point correspondences.
void SolveLinearLeastSquaresSVD(const CFloatMatrix *A, const CFloatVector *b, CFloatVector *x)
void AddToVec(CFloatVector *pVector, const CFloatVector *pVectorToAdd)
void AddToMat(CFloatMatrix *pMatrix, const CFloatMatrix *pMatrixToAdd)
void SubtractVecVec(const CFloatVector *pVector1, const CFloatVector *pVector2, CFloatVector *pResultVector)
void Zero(CFloatMatrix *pMatrix)
Data structure for the representation of a matrix of values of the data type float.
Definition: FloatMatrix.h:56
void AddMatMat(const CFloatMatrix *pMatrix1, const CFloatMatrix *pMatrix2, CFloatMatrix *pResultMatrix)
void Transpose(const CFloatMatrix *pMatrix, CFloatMatrix *pResultMatrix)
void AddVecVec(const CFloatVector *pVector1, const CFloatVector *pVector2, CFloatVector *pResultVector)
bool CalculatePseudoInverseSimple(const CFloatMatrix *pInputMatrix, CFloatMatrix *pResultMatrix)
bool DetermineHomography(const Vec2d *pSourcePoints, const Vec2d *pTargetPoints, int nPoints, Mat3d &A, bool bUseSVD=false)
Determines a homography based on a set of 2d-2d point correspondences.
void CalculatePseudoInverseSVD(const CFloatMatrix *pInputMatrix, CFloatMatrix *pOutputMatrix)
void SubtractMeanFromRows(const CFloatMatrix *pMatrix, CFloatMatrix *pResultMatrix)
void SolveLinearLeastSquaresHomogeneousSVD(const CFloatMatrix *A, CFloatVector *x)
Data structure for the representation of a vector of values of the data type float.
Definition: FloatVector.h:53
Data structure for the representation of a 3x3 matrix.
Definition: Math3d.h:93
void SubtractMatMat(const CFloatMatrix *pMatrix1, const CFloatMatrix *pMatrix2, CFloatMatrix *pResultMatrix)
void MulMatVec(const CFloatMatrix *pMatrix, const CFloatVector *pVector, CFloatVector *pResultVector)