Bsoft  2.1.0
Bernard's software package
matrix_util.cpp
Go to the documentation of this file.
1 
9 #include "matrix_util.h"
10 #include "Matrix.h"
11 #include "matrix_linear.h"
12 #include "math_util.h"
13 #include "utilities.h"
14 
15 // Declaration of global variables
16 extern int verbose; // Level of output to the screen
17 
28 {
29  long i, r, c;
30  double err(1);
31  double* rw = new double[m.rows()];
32  double* cw = new double[m.columns()];
33 
34  if ( verbose & VERB_FULL )
35  cout << "Cycle\tError" << endl;
36  for ( i=0; i<100 && err > 1e-20; i++ ) {
37  for ( r=0; r<m.rows(); r++ ) rw[r] = 0; // Row scaling
38  for ( r=0; r<m.rows(); r++ )
39  for ( c=0; c<m.columns(); c++ )
40  rw[r] += m[r][c];
41  for ( r=0; r<m.rows(); r++ ) rw[r] = 1/rw[r];
42  for ( r=0; r<m.rows(); r++ )
43  for ( c=0; c<m.columns(); c++ )
44  m[r][c] *= rw[r];
45 
46  for ( c=0; c<m.columns(); c++ ) cw[c] = 0; // Column scaling
47  for ( r=0; r<m.rows(); r++ )
48  for ( c=0; c<m.columns(); c++ )
49  cw[c] += m[r][c];
50  for ( c=0; c<m.columns(); c++ ) cw[c] = 1/cw[c];
51  for ( r=0; r<m.rows(); r++ )
52  for ( c=0; c<m.columns(); c++ )
53  m[r][c] *= cw[c];
54 
55  for ( err=0, r=0; r<m.rows(); r++ ) err += rw[r];
56  for ( c=0; c<m.columns(); c++ ) err += cw[c];
57  err = fabs(err/(m.rows() + m.columns()) - 1);
58  if ( verbose & VERB_FULL )
59  cout << i+1 << tab << err << endl;
60  }
61 
62  delete[] rw;
63  delete[] cw;
64 
65  return 0;
66 }
67 
79 {
80  n=2;
81  long j, k, c, r;
82  double dcut(0);
83  double* d = new double[n];
84 
85  for ( c=0; c<m.columns(); c++ ) {
86  for ( j=0; j<n; j++ ) d[j] = 1e30;
87  for ( r=0; r<m.rows(); r++ ) if ( c != r ) {
88  for ( j=0; j<n && d[j] < m[r][c]; j++ ) ;
89  if ( j < n ) {
90  for ( k=n-1; k>j; k-- ) d[k] = d[k-1];
91  d[j] = m[r][c];
92  }
93  }
94  if ( dcut < d[n-1] ) dcut = d[n-1];
95  }
96 
97  delete[] d;
98 
99  return dcut;
100 }
101 
108 {
109  long i, j;
110 
111  for ( i=0; i<matrix.rows(); i++ ) {
112  for ( j=0; j<matrix.columns(); j++ ) {
113  if ( matrix[i][j] < 1 )
114  matrix[i][j] = -log(1 - matrix[i][j]);
115  else
116  matrix[i][j] = 1e30;
117  }
118  }
119 
120  return 0;
121 }
122 
129 int* matrix_find_linear_sequence(Matrix matrix, int window)
130 {
131  if ( matrix.columns() != matrix.rows() ) {
132  cerr << "Error: the matrix is not square! (" << matrix.columns() << "x" << matrix.rows() << ")" << endl;
133  return NULL;
134  }
135 
136  int i, j, n = matrix.columns();
137  double best_R = 1e30;
138  int* order = new int[n];
139  int* best_order = new int[n];
140 
141  if ( window < 2 ) window = 2;
142  if ( window > n ) window = n;
143 
144  if ( verbose & VERB_PROCESS ) {
145  cout << "Finding the best sequence from " << n << " entities:" << endl;
146  cout << "Window: " << window << endl;
147  cout << "Number of permutations: " << (n-window+1)*factorial(window) << endl;
148  } else if ( verbose & VERB_LABEL )
149  cout << "Finding the best sequence from " << n << " entities with a window of " << window << endl;
150 
151  for ( j=0; j<n; j++ ) best_order[j] = j; // Set up the initial order
152  for ( i=0; i<n-window+1; i++ ) {
153  for ( j=0; j<n; j++ ) order[j] = best_order[j]; // Keep the best order
154  matrix_permute(i+1, i+window, matrix, order, &best_R, best_order);
155  }
156 
157  if ( verbose & VERB_RESULT ) {
158  cout << "Best R = " << best_R << endl << "Best order:";
159  for ( j=0; j<n; j++ ) cout << " " << best_order[j]+1;
160  cout << endl;
161  }
162 
163  delete[] order;
164 
165  return best_order;
166 }
167 
182 int matrix_permute(int i, int j, Matrix matrix, int* order,
183  double* best_R, int* best_order)
184 {
185  long k, m, n(matrix.columns());
186  int* this_order;
187  double R;
188 
189  if( i == j ) {
190  R = matrix_calc_dist_fit(matrix, order);
191  if ( *best_R > R ) {
192  *best_R = R;
193  for ( m=0; m<n; m++ ) best_order[m] = order[m];
194  }
195  } else {
196  this_order = new int[n];
197  for ( m=0; m<n; m++ ) this_order[m] = order[m];
198  for( k=i; k<=j; k++) {
199  matrix_permute(i+1, j, matrix, order, best_R, best_order);
200  if( k != j ) {
201  swap(this_order[i-1], this_order[k]);
202  for ( m=0; m<n; m++ ) order[m] = this_order[m];
203  }
204  }
205  delete[] this_order;
206  }
207 
208  return 0;
209 }
210 
211 
232 double matrix_calc_dist_fit(Matrix matrix, int* order)
233 {
234  long i, j, k, n = matrix.columns();
235  double min(1), max(0), new_min(1), new_max(0);
236  Matrix new_mat(n,n);
237 
238  // Set up the lower triangle with the original distances
239  for ( i=1; i<n; i++ ) {
240  for ( j=0; j<i; j++ ) {
241  new_mat[i][j] = matrix[order[i]][order[j]];
242  if ( min > new_mat[i][j] ) min = new_mat[i][j];
243  if ( max < new_mat[i][j] ) max = new_mat[i][j];
244  }
245  }
246 
247  // Set up the second upper diagonal
248  for ( j=1; j<n; j++ )
249  new_mat[j-1][j] = new_mat[j][j-1];
250 
251  // Calculate the other upper diagonals
252  for ( i=0; i<n-1; i++ ) {
253  for ( j=i+2; j<n; j++ ) {
254  for ( k=i+1; k<=j; k++ )
255  new_mat[i][j] += new_mat[k][k-1];
256  if ( new_min > new_mat[i][j] ) new_min = new_mat[i][j];
257  if ( new_max < new_mat[i][j] ) new_max = new_mat[i][j];
258  }
259  }
260 
261  if ( verbose & VERB_DEBUG ) {
262  cout << "DEBUG matrix_calc_dist_fit: Order:" << endl;
263  for ( i=0; i<n; i++ ) cout << " " << order[i];
264  cout << endl << "DEBUG matrix_calc_dist_fit: New matrix:" << endl;
265  cout << new_mat << endl;
266  }
267 
268  // Calculate a figure-of-merit weighted by diagonal
269  double diff, weight, wsum(0), R(0);
270  double scale = 1/(max - min);
271  double new_scale = 1/(new_max - new_min);
272  double offset = min/(max - min) - new_min/(new_max - new_min);
273  for ( i=2; i<n; i++ ) {
274  for ( j=0; j<i-1; j++ ) {
275  diff = scale*new_mat[i][j] - new_scale*new_mat[j][i] - offset;
276  weight = 1.0/(i - j);
277  R += weight*diff*diff;
278  wsum += weight;
279  }
280  }
281  R = sqrt(R/wsum);
282 
283  if ( verbose & VERB_PROCESS ) {
284  cout << "R = " << R << endl;
285  for ( j=0; j<n; j++ ) cout << " " << order[j]+1;
286  cout << endl;
287  }
288 
289  return R;
290 }
291 
298 /*Bimage* img_from_matrix(Matrix& matrix, long scale)
299 {
300  if ( scale < 1 ) scale = 1;
301  if ( scale > 10 ) scale = 10;
302 
303  Bimage* pmat = new Bimage(Float, TSimple, scale*matrix.columns(), scale*matrix.rows(), 1, 1);
304 
305  long i, j, k, x, y;
306 
307  for ( k=y=0; y<pmat->sizeY(); ++y ) {
308  i = y/scale;
309  for ( x=0; x<pmat->sizeX(); ++x, ++k ) {
310  j = x/scale;
311  pmat->set(k, matrix[i][j]);
312  }
313  }
314 
315  return pmat;
316 }
317 */
318 
matrix_linear.h
Header file for matrix functions.
matrix_util.h
Utility functions for matrices.
VERB_DEBUG
#define VERB_DEBUG
Definition: utilities.h:116
Matrix::columns
long columns()
Definition: Matrix.h:154
VERB_PROCESS
#define VERB_PROCESS
Definition: utilities.h:111
factorial
double factorial(int n)
Calculates the factorial of n.
Definition: math_util.cpp:66
Matrix::rows
long rows()
Definition: Matrix.h:153
VERB_RESULT
#define VERB_RESULT
Definition: utilities.h:109
matrix_find_linear_sequence
int * matrix_find_linear_sequence(Matrix matrix, int window)
Finds a linear sequence of elements based on a pairwise matrix.
Definition: matrix_util.cpp:129
utilities.h
Header file for general utilities.
tab
ostream & tab(ostream &out)
Definition: utilities.cpp:22
matrix_log_1_R
int matrix_log_1_R(Matrix matrix)
Calculates the logarithmic derivative of an R-factor in a matrix.
Definition: matrix_util.cpp:107
Matrix.h
Generalized matrix class.
math_util.h
Header file for general utilities.
VERB_FULL
#define VERB_FULL
Definition: utilities.h:113
Matrix
Definition: Matrix.h:22
matrix_normalize
int matrix_normalize(Matrix &m)
Normalizes a matrix.
Definition: matrix_util.cpp:27
VERB_LABEL
#define VERB_LABEL
Definition: utilities.h:110
matrix_permute
int matrix_permute(int i, int j, Matrix matrix, int *order, double *best_R, int *best_order)
Permutes the order of a pairwise matrix and calculate a distance criterion.
Definition: matrix_util.cpp:182
verbose
int verbose
matrix_find_cutoff_for_number
double matrix_find_cutoff_for_number(Matrix m, int n)
Calculates a cutoff to ensure that the given number of elements fall below it.
Definition: matrix_util.cpp:78
matrix_calc_dist_fit
double matrix_calc_dist_fit(Matrix matrix, int *order)
Calculates an R-factor for a specific order of elements in a pairwise matrix.
Definition: matrix_util.cpp:232