9 #ifndef REDUKT_FastMDS_H
10 #define REDUKT_FastMDS_H
15 #include <boost/numeric/ublas/matrix_proxy.hpp>
16 #include <ext/algorithm>
30 template<
class ValueType>
34 typedef ublas::matrix<ValueType>
Matrix;
35 typedef ublas::vector<ValueType>
Vector;
49 using namespace ublas;
50 using namespace ublasTools;
55 assert( D.size1() == D.size2() );
61 cout <<
"FastMDS with n=" << n <<
", p=" << p <<
", dim_low=k=" << k <<
", s=" << s << endl;
64 cout <<
"Computing sample indices, taking " << s <<
" samples from each range, total " << s*p << endl;
65 std::vector<int> indices(n);
for(
int i=0; i < n; ++i ) indices[i]=i;
66 std::vector<int> sample( s*p );
67 std::vector<int>::iterator it = sample.begin();
68 for(
int i=0; i < p; ++i )
73 it = __gnu_cxx::random_sample_n( indices.begin() + i *(n/p),
74 indices.begin() + (i+1)*(n/p),
78 cout <<
"Sample indices are: " << endl;
79 for(
int i=0; i < s*p; ++i )
80 cout << sample[i] <<
" ";
85 cout <<
"Build align " << M.size1() <<
"x" << M.size2() <<
" matrix M..." << flush;
86 for(
int row=0; row < s*p; ++row )
87 for(
int col=0; col < s*p; ++col )
89 M(row,col) = D(sample[row],sample[col]);
91 cout <<
"done" << endl;
94 cout <<
"Solve CMDS for align matrix M..." << flush;
97 cout <<
"done (result is a " << M.size1() <<
"x" << M.size2() <<
" matrix)" << endl;
100 for(
int i=0; i < p; ++i )
103 int first = i *(n/p),
106 cout <<
"Submatrix D_" << i <<
" from point " << first <<
" to " << last << endl;
107 Matrix D_i = matrix_range<Matrix>( D,
112 cout <<
"Solve CMDS for D_" << i <<
"..." << flush;
115 cout <<
"done (result is a " << D_i.size1() <<
"x" << D_i.size2() <<
" matrix)" << endl;
120 cout <<
" Build " << k <<
"x" << s <<
" matrices mdsDs and mdsMs" << endl;
123 for(
int row=0; row < s; ++row )
127 int idx = sample[ofs] - i*(n/p);
130 for(
int col=0; col < k; ++col )
132 mdsDs(col,row) = D_i( idx , col );
133 mdsMs(col,row) = M ( ofs, col );
138 cout <<
" Find affine mapping A_" << i << endl;
140 if( !affine_fit<T>( mdsDs, mdsMs, A, b ) )
142 cout <<
"BOGUS!" << endl;
148 cout <<
" mdsDs = " << mdsDs << endl;
149 cout <<
" mdsMs = " << mdsMs << endl;
150 Matrix estimate = prod( A, mdsDs );
151 for(
unsigned int j=0; j < estimate.size2(); ++j )
152 column(estimate,j) += b;
153 cout <<
" A*mdsDs+b1' = " << estimate << endl;
162 cout <<
"Align CMDS(D_" << i <<
") with CMDS(M)..." << flush;
166 D_i = prod( A, D_i );
167 cout <<
"done (result is a " << D_i.size1() <<
"x" << D_i.size2() <<
" matrix)" << endl;
169 cout <<
"Copy resulting points into solution matrix" << endl;
173 for(
unsigned int row=0; row < D_i.size1(); ++row )
174 for(
int col=0; col < k; ++col )
175 m_points(row+first,col) = D_i(row,col);
ublas::matrix< ValueType > Matrix
Definition: FastMDS.h:34
const Matrix & getPoints() const
Definition: FastMDS.h:39
ublas::vector< ValueType > Vector
Definition: FastMDS.h:35
Classical Multidimensional Scaling (suited for small instances)
Definition: CMDS.h:36
void project(int dim=2)
Project referenced matrix D to dim dimensions.
Definition: CMDS.h:119
FastMDS(Matrix &D, int k, int p, int s)
Definition: FastMDS.h:47