Bsoft  2.1.0
Bernard's software package
bmatrix.cpp
Go to the documentation of this file.
1 
9 #include "cluster.h"
10 #include "Matrix.h"
11 #include "matrix_linear.h"
12 #include "matrix_util.h"
13 #include "math_util.h"
14 #include "Bimage.h"
15 #include "utilities.h"
16 #include "options.h"
17 #include "timer.h"
18 
19 // Declaration of global variables
20 extern int verbose; // Level of output to the screen
21 
22 // Usage assistance
23 const char* use[] = {
24 " ",
25 "Usage: bmatrix [options] matrix_in.dat matrix_out.dat",
26 "-----------------------------------------------------",
27 "Processes matrices.",
28 " ",
29 "Actions:",
30 "-random 4,7 Create a random matrix of the given size (rows & columns).",
31 "-negate Change the sign of the matrix.",
32 "-invert Invert by LU decomposition.",
33 "-svd Invert by singular value decomposition.",
34 "-window 5 Window over which to do permutations for linear sorting.",
35 "-preference -10 Homogenous preference for clustering.",
36 " ",
37 "Parameters:",
38 "-verbose 7 Verbosity of output.",
39 "-ln_1_R Use the -ln(1-R) conversion of the input matrix.",
40 "-lambda 0.6 Damping factor for clustering (default 0.5).",
41 " ",
42 "Parameters for image output:",
43 "-datatype b Force writing of a new data type.",
44 "-scale 3 Scale the image (must be an integer).",
45 "-color Convert to a heat map.",
46 " ",
47 "Input:",
48 "-vector file.dat Input a vector to multiply with the matrix.",
49 " ",
50 "Output:",
51 "-outvector file.dat Output the vector.",
52 "-outimage file.img Output the matrix as an image.",
53 " ",
54 NULL
55 };
56 
57 int main(int argc, char** argv)
58 {
59  // Initialize variables
60  long m(0), n(0); // Dimensions for random matrix
61  int negate(0); // Flag to negate
62  int invert(0); // Flag for LU decomp inversion
63  int svd(0); // Flag for singular value decomposition
64  int window(0); // Window for linear sorting
65  double pref(-1e37); // Preference value for clustering
66  double lambda(0.5); // Damping factor for clustering
67  int log_1_R(0);
68  Bstring vector_file; // Vector input file name
69  Bstring vecout_file; // Vector output file name
70  DataType nudatatype(Unknown_Type); // Conversion to new type
71  long img_scale(0); // Image enlargement
72  int img_color(0); // Flag to color the image
73  Bstring img_file; // Output image file name
74 
75  int i, optind;
76  Boption* option = get_option_list(use, argc, argv, optind);
77  Boption* curropt;
78  for ( curropt = option; curropt; curropt = curropt->next ) {
79  if ( curropt->tag == "random" ) {
80  if ( ( i = curropt->values(m, n) ) < 1 )
81  cerr << "-random: A matrix size must be specified!" << endl;
82  else
83  if ( i < 2 ) n = m;
84  }
85  if ( curropt->tag == "negate" )
86  negate = 1;
87  if ( curropt->tag == "invert" )
88  invert = 1;
89  if ( curropt->tag == "svd" )
90  svd = 1;
91  if ( curropt->tag == "window" ) {
92  if ( ( window = curropt->value.integer() ) < 1 )
93  cerr << "-window: A window size must be specified!" << endl;
94  else
95  if ( window < 2 ) window = 2;
96  }
97  if ( curropt->tag == "preference" )
98  if ( ( pref = curropt->value.real() ) < -1e36 )
99  cerr << "-preference: A preference value must be specified!" << endl;
100  if ( curropt->tag == "ln_1_R" )
101  log_1_R = 1;
102  if ( curropt->tag == "lambda" )
103  if ( ( lambda = curropt->value.real() ) < 0.001 )
104  cerr << "-lambda: A damping factor must be specified!" << endl;
105  if ( curropt->tag == "datatype" )
106  nudatatype = curropt->datatype();
107  if ( curropt->tag == "scale" )
108  if ( ( img_scale = curropt->value.integer() ) < 2 )
109  cerr << "-scale: An integer must be specified!" << endl;
110  if ( curropt->tag == "color" ) img_color = 1;
111  if ( curropt->tag == "vector" )
112  vector_file = curropt->filename();
113  if ( curropt->tag == "outvector" )
114  vecout_file = curropt->filename();
115  if ( curropt->tag == "outimage" )
116  img_file = curropt->filename();
117  }
118  option_kill(option);
119 
120  double ti = timer_start();
121 
122  Matrix matrix, vector;
123  Bstring filename;
124 
125  if ( n > 0 ) {
126  matrix = Matrix(m, n);
127  matrix.randomize();
128  } else {
129  filename = argv[optind++];
130  matrix = Matrix(filename);
131  }
132 
133  if ( verbose )
134  cout << "Matrix size:" << tab << matrix.rows() << " x " << matrix.columns() << endl;
135 
136  if ( verbose & VERB_PROCESS )
137  cout << "Input matrix:" << endl << matrix << endl;
138 
139  if ( vector_file.length() )
140  vector = Matrix(vector_file);
141 
142  if ( negate )
143  matrix = -matrix;
144 
145  double det;
146  if ( invert ) {
147  det = matrix.LU_decomposition();
148  if ( verbose & VERB_PROCESS ) {
149  cout << "Determinant: " << det << endl;
150  cout << "Inverse matrix:" << endl << matrix << endl;
151  }
152  }
153 
154  if ( svd ) {
156  if ( verbose & VERB_PROCESS ) {
157  cout << "Inverse matrix:" << endl << matrix << endl;
158  }
159  }
160 
161  if ( log_1_R ) matrix_log_1_R(matrix);
162 
163  int* order = NULL;
164  if ( window ) {
165  order = matrix_find_linear_sequence(matrix, window);
166 // matrix_rearrange(matrix, order);
167  delete[] order;
168  }
169 
170  long ncluster(0);
171  if ( pref > -1e36 ) {
172  for ( i=0; i<matrix.rows(); i++ )
173  matrix[i][i] = pref;
174  affin_prop_clustering(matrix, 500, 50, lambda, ncluster);
175  }
176 
177  if ( vector.rows() )
178  vector = matrix * vector;
179 
180  if ( verbose && vector.rows() ) {
181  cout << "Output vector:" << endl << vector << endl;
182  }
183 
184  if ( verbose & VERB_FULL ) {
185  cout << "Output matrix:" << endl << matrix << endl;
186  }
187 
188  if ( vecout_file.length() )
189  vector.write(vecout_file);
190 
191  if ( optind < argc ) {
192  filename = argv[optind];
193  matrix.write(filename);
194  }
195 
196 // Bimage* pmat = NULL;
197 // Bimage* pt = NULL;
198  if ( img_file.length() ) {
199 // pmat = img_from_matrix(matrix, img_scale);
200  Bimage* pmat = new Bimage(matrix, img_scale);
201  if ( img_color ) {
202  pmat->change_type(UCharacter);
203  Bimage* pt = pmat->color_spectrum(pmat->minimum(), pmat->maximum());
204  delete pt;
205  }
206  if ( nudatatype > Unknown_Type ) pmat->change_type(nudatatype);
207  write_img(img_file, pmat, 0);
208  delete pmat;
209  }
210 
211  if ( verbose & VERB_TIME )
212  timer_report(ti);
213 
214  bexit(0);
215 }
216 
217 
218 
Boption
A class for a command line input option.
Definition: options.h:26
matrix_linear.h
Header file for matrix functions.
matrix_util.h
Utility functions for matrices.
cluster.h
Header file for clustering functions.
UCharacter
@ UCharacter
Definition: utilities.h:153
Bstring
Definition: Bstring.h:29
Matrix::randomize
void randomize()
Definition: Matrix.h:237
use
const char * use[]
Definition: bmatrix.cpp:23
Bstring::integer
long integer()
Definition: Bstring.cpp:547
Boption::datatype
DataType datatype()
Definition: options.h:93
Matrix::columns
long columns()
Definition: Matrix.h:154
Boption::next
Boption * next
Definition: options.h:28
bexit
int bexit(int value)
Exit function for cleanup and debugging.
Definition: utilities.cpp:280
get_option_list
Boption * get_option_list(const char *use[], int argc, char *argv[], int &optind)
Parses command line arguments based on a template.
Definition: options.cpp:40
Bstring::real
double real()
Definition: Bstring.cpp:553
options.h
Header file for option handlers.
VERB_PROCESS
#define VERB_PROCESS
Definition: utilities.h:111
Bimage::maximum
double maximum()
Definition: Bimage.h:542
Bimage::color_spectrum
Bimage * color_spectrum(double cmin, double cmax)
Colorizes an image to a spectrum.
Definition: Bimage_color.cpp:415
affin_prop_clustering
long * affin_prop_clustering(Matrix s, long maxit, long convit, double lambda, long &ncluster)
Generate clusters from a similarity matrix using affinity propagation.
Definition: cluster.cpp:102
Boption::tag
Bstring tag
Definition: options.h:29
Boption::values
long values(T1 &v1, T2 &v2)
Definition: options.h:115
Matrix::singular_value_decomposition
double singular_value_decomposition()
Singular value decomposition of a matrix .
Definition: matrix.cpp:177
verbose
int verbose
write_img
int write_img(const char *filename, Bimage *p, int compression)
General driver function to write multiple image formats.
Definition: rwimg.cpp:281
Matrix::rows
long rows()
Definition: Matrix.h:153
Boption::value
Bstring value
Definition: options.h:30
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
Bimage
General image parameter class.
Definition: Bimage.h:177
timer.h
Utilities for timing functions.
Unknown_Type
@ Unknown_Type
Definition: utilities.h:151
Matrix::LU_decomposition
double LU_decomposition()
Matrix inversion by LU decomposition.
Definition: matrix.cpp:78
VERB_TIME
#define VERB_TIME
Definition: utilities.h:114
Bimage.h
Header file for image class.
main
int main(int argc, char **argv)
Definition: bmatrix.cpp:57
timer_start
double timer_start()
Starts timer and prints the time.
Definition: timer.cpp:61
utilities.h
Header file for general utilities.
tab
ostream & tab(ostream &out)
Definition: utilities.cpp:22
Bstring::length
long length() const
Definition: Bstring.cpp:263
timer_report
double timer_report(double lasttime)
Reports the time elapsed since a given time.
Definition: timer.cpp:76
Bimage::change_type
void change_type(char letter)
Get the data type indicated by a single letter code.
Definition: Bimage.cpp:1765
Matrix.h
Generalized matrix class.
math_util.h
Header file for general utilities.
Boption::filename
Bstring filename()
Definition: options.h:85
option_kill
int option_kill(Boption *option)
Deallocates a linked list of option structures.
Definition: options.cpp:111
VERB_FULL
#define VERB_FULL
Definition: utilities.h:113
Matrix::write
void write(Bstring &filename)
Definition: Matrix.h:95
Bimage::minimum
double minimum()
Definition: Bimage.h:541
matrix_find_linear_sequence
int * matrix_find_linear_sequence(Matrix, int window)
Finds a linear sequence of elements based on a pairwise matrix.
Definition: matrix_util.cpp:129
DataType
DataType
Base data type specifier.
Definition: utilities.h:150
Matrix
Definition: Matrix.h:22