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