| Bsoft 1.9 Bernard's software package

- Main Page
- Namespaces
- Classes
- Files

- File List
- File Members

All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
Bmatrix.cpp
Go to the documentation of this file.
1
9 #include "Bmatrix.h"
10 #include "linked_list.h"
11 #include "utilities.h"
12
13 // Declaration of global variables
14 extern int verbose; // Level of output to the screen
15
21 int matrix_show(Bmatrix matrix)
22 {
23 int i, j, k;
24 double val;
25 Bstring* label;
26
27 for ( label = matrix.column_labels(); label; label = label->next )
28 cout << tab << *label;
29 cout << endl;
30
31 for ( i=k=0, label = matrix.row_labels(); i<matrix.rows(); i++, label=(label)?label->next:NULL ) {
32 if ( label ) cout << *label;
33 for ( j=0; j<matrix.columns(); j++, k++ ) {
34 val = matrix[k];
35 if ( fabs(val) < 1e-6 ) val = 0;
36 cout << tab << val;
37 }
38 cout << endl;
39 }
40 cout << endl;
41
42 return 0;
43 }
44
50 Bimage* img_from_matrix(Bmatrix matrix)
51 {
52 Bimage* pmat = new Bimage(Float, TSimple, matrix.columns(), matrix.rows(), 1, 1);
53
54 long i, datasize = (long)matrix.columns()*matrix.rows();
55
56 for ( i=0; i<datasize; i++ ) pmat->set(i, matrix[i]);
57
58 return pmat;
59 }
60
67 Bmatrix matrix_random(long m, long n)
68 {
69 Bmatrix matrix(m, n, 2, 2);
70
71 random_seed();
72 long i, j;
73 double rm = INT_MAX/4, irm = 10.0L/INT_MAX;
74
75 for ( j=0; j<m; j++ )
76 for ( i=0; i<n; i++ )
77 matrix[j*n+i] = irm*(random() - rm);
78
79 return matrix;
80 }
81
88 int matrix_rearrange(Bmatrix matrix, int* order)
89 {
90 long i, j;
91 double* nudata = new double[matrix.rows()*matrix.columns()];
92
93 Bstring* clabel = NULL;
94 Bstring* labels = new Bstring[matrix.rows()];
95 Bstring* l;
96
97 for ( i=0, l=matrix.column_labels(); l; l=l->next, i++ ) labels[i] = *l;
98
99 for ( j=0; j<i; j++ ) string_add(&clabel, labels[order[j]]);
100
101 matrix.column_labels(clabel);
102 matrix.row_labels(clabel);
103
104 string_kill(clabel);
105 delete[] labels;
106
107 for ( i=0; i<matrix.rows(); i++ )
108 for ( j=0; j<matrix.columns(); j++ )
109 nudata[i*matrix.columns()+j] = matrix[order[i]*matrix.columns()+order[j]];
110
111 for ( i=0; i<matrix.columns()*matrix.rows(); i++ ) matrix[i] = nudata[i];
112
113 delete[] nudata;
114
115 return 0;
116 }
117
118
string_add
Bstring * string_add(Bstring **list, const char *string)
Adds a string to a linked list.
Definition: Bstring.cpp:748
Bmatrix
Definition: Bmatrix.h:28
random_seed
long random_seed()
Gets a random seed and sets the flag.
Definition: random_numbers.cpp:40
utilities.h
Header file for general utilities.
matrix_show
int matrix_show(Bmatrix matrix)
Displays the matrix contents.
Definition: Bmatrix.cpp:21
Float
Definition: Bimage.h:54
TSimple
Definition: Bimage.h:65
matrix_rearrange
int matrix_rearrange(Bmatrix matrix, int *order)
Rearranges a matrix with a given order.
Definition: Bmatrix.cpp:88
Bimage
General image parameter class.
Definition: Bimage.h:147
Bmatrix::column_labels
Bstring * column_labels()
Definition: Bmatrix.h:80
linked_list.h
Header file for generalized linked list functions.
Bstring::next
Bstring * next
Definition: Bstring.h:27
Bimage::set
void set(unsigned long j, double v)
Sets a single value at the given index.
Definition: Bimage.cpp:871
val
int val
Definition: jpeglib.h:956
Bmatrix::rows
long rows()
Definition: Bmatrix.h:78
Bstring
Definition: Bstring.h:25
Bmatrix.h
Matrix class.
Bmatrix::row_labels
Bstring * row_labels()
Definition: Bmatrix.h:81
Bmatrix::columns
long columns()
Definition: Bmatrix.h:77
matrix_random
Bmatrix matrix_random(long m, long n)
Generates a matrix with random values.
Definition: Bmatrix.cpp:67
tab
ostream & tab(ostream &out)
Definition: utilities.cpp:20
verbose
int verbose
string_kill
int string_kill(Bstring *list)
Kills a string linked list.
Definition: Bstring.cpp:782
img_from_matrix
Bimage * img_from_matrix(Bmatrix matrix)
Converts a matrix to an image.
Definition: Bmatrix.cpp:50