40 #include "../../Matrix/Matrix.h" 41 #include "../../Matrix/MatrixTools.h" 42 #include "../../Matrix/EigenValue.h" 43 #include "../../../App/ApplicationTools.h" 52 const vector<double>& rowWeights,
53 const vector<double>& colWeights,
55 double tol,
bool verbose)
throw (
Exception) :
56 rowWeights_(rowWeights),
57 colWeights_(colWeights),
66 check_(matrix, rowWeights, colWeights, verbose);
67 compute_(matrix, tol, verbose);
72 const vector<double>& rowWeights,
73 const vector<double>& colWeights,
76 size_t rowNb = matrix.getNumberOfRows();
77 size_t colNb = matrix.getNumberOfColumns();
79 if (rowWeights.size() != rowNb)
80 throw Exception(
"DualityDiagram::check_. The number of row weigths has to be equal to the number of rows!");
81 if (colWeights.size() != colNb)
82 throw Exception(
"DualityDiagram::check_. The number of column weigths has to be equal to the number of columns!");
85 for (vector<double>::const_iterator it = rowWeights.begin(); it != rowWeights.end(); it++)
88 throw Exception(
"DualityDiagram::check_. All row weights have to be positive");
92 for (vector<double>::const_iterator it = colWeights.begin(); it != colWeights.end(); it++)
95 throw Exception(
"DualityDiagram::check_. All column weights have to be positive");
102 const vector<double>& rowWeights,
103 const vector<double>& colWeights,
105 double tol,
bool verbose)
throw (
Exception)
107 check_(matrix, rowWeights, colWeights, verbose);
108 rowWeights_ = rowWeights;
109 colWeights_ = colWeights;
111 compute_(matrix, tol, verbose);
115 double tol,
bool verbose)
121 bool transpose = (rowNb < colNb);
124 vector<double> rW(rowWeights_);
125 for (
unsigned int i = 0; i < rowWeights_.size(); i++)
127 rW[i] = sqrt(rowWeights_[i]);
134 vector<double> cW(colWeights_);
135 for (
unsigned int i = 0; i < colWeights_.size(); i++)
137 cW[i] = sqrt(colWeights_[i]);
154 throw Exception(
"DualityDiagram (constructor). The variance-covariance or correlation matrix should be symmetric...");
157 eigenVectors_ = eigen.
getV();
161 for (
size_t i = eigenValues_.size(); i > 0; i--)
163 if ((eigenValues_[i - 1] / eigenValues_[eigenValues_.size() - 1]) > tol)
169 throw Exception(
"DualityDiagram (constructor). The number of axes to keep must be positive.");
180 vector<double> tmpEigenValues(nbAxes_);
182 for (
size_t i = eigenValues_.size(); i > (eigenValues_.size() - nbAxes_); i--)
184 tmpEigenValues[cpt] = eigenValues_[i-1];
187 eigenValues_ = tmpEigenValues;
189 for (vector<double>::iterator it = rowWeights_.begin(); it != rowWeights_.end(); it++)
195 for (vector<double>::iterator it = colWeights_.begin(); it != colWeights_.end(); it++)
201 vector<double> dval(nbAxes_);
202 for (
size_t i = 0; i < dval.size(); i++)
204 dval[i] = sqrt(eigenValues_[i]);
207 vector<double> invDval(nbAxes_);
208 for (
size_t i = 0; i < invDval.size(); i++)
210 invDval[i] = 1. / sqrt(eigenValues_[i]);
216 vector<double> tmpColWeights(colNb);
217 for (
unsigned int i = 0; i < colWeights_.size(); i++)
219 tmpColWeights[i] = 1. / sqrt(colWeights_[i]);
224 tmpEigenVectors.
resize(eigenVectors_.getNumberOfRows(), nbAxes_);
226 for (
size_t i = eigenVectors_.getNumberOfColumns(); i > (eigenVectors_.getNumberOfColumns() - nbAxes_); i--)
228 for (
unsigned int j = 0; j < eigenVectors_.getNumberOfRows(); j++)
230 tmpEigenVectors(j, cpt2) = eigenVectors_(j, i-1);
239 tmpRowCoord_.
resize(rowNb, nbAxes_);
250 vector<double> tmpRowWeights(rowNb);
251 for (
unsigned int i = 0; i < rowWeights_.size(); i++)
253 tmpRowWeights[i] = 1. / sqrt(rowWeights_[i]);
258 tmpEigenVectors.
resize(eigenVectors_.getNumberOfRows(), nbAxes_);
260 for (
size_t i = eigenVectors_.getNumberOfColumns(); i > (eigenVectors_.getNumberOfColumns() - nbAxes_); i--)
262 for (
size_t j = 0; j < eigenVectors_.getNumberOfRows(); j++)
264 tmpEigenVectors(j, cpt2) = eigenVectors_(j, i-1);
273 tmpColCoord_.
resize(colNb, nbAxes_);
The matrix template interface.
void setData(const Matrix< double > &matrix, const std::vector< double > &rowWeights, const std::vector< double > &colWeights, unsigned int nbAxes, double tol=0.0000001, bool verbose=true)
Set the data and perform computations.
This class allows to perform a correspondence analysis.
virtual ~DualityDiagram()
virtual size_t getNumberOfColumns() const =0
const RowMatrix< Real > & getV() const
Return the eigenvector matrix.
void resize(size_t nRows, size_t nCols)
Resize the matrix.
void check_(const Matrix< double > &matrix, const std::vector< double > &rowWeights, const std::vector< double > &colWeights, unsigned int nbAxes)
Computes eigenvalues and eigenvectors of a real (non-complex) matrix.
const std::vector< Real > & getRealEigenValues() const
Return the real parts of the eigenvalues.
virtual size_t getNumberOfRows() const =0
DualityDiagram()
Build an empty DualityDiagram object.
void compute_(const Matrix< double > &matrix, double tol, bool verbose)