Bsoft  1.9
Bernard's software package
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
Bmatrix.h
Go to the documentation of this file.
1 
9 #include <stdio.h>
10 #include <stdlib.h>
11 #include <string.h>
12 #include <ctype.h>
13 #include <math.h>
14 #include <time.h>
15 
16 #include "rwimg.h"
17 #include "Bstring.h"
18 #include "matrix_linear.h"
19 
20 #ifndef _Bmatrix_
21 /************************************************************************
22 @Object: class Bmatrix
23 @Description:
24  General matrix structure.
25 @Features:
26  An m x n matrix with m column and n row labels.
27 *************************************************************************/
28 class Bmatrix {
29 private:
30  int m, n; // Matrix size: number of columns, number of rows
31  Bstring* clabel; // Column labels linked list
32  Bstring* rlabel; // Row labels linked list
33  double* data; // Contents
34 public:
35  Bmatrix() { m=n=0; clabel = rlabel = NULL; data = NULL; }
36  Bmatrix(long columns, long rows) {
37  m=columns; n=rows;
38  data = new double[m*n];
39  for ( long i=0; i<m*n; i++ ) data[i] = 0;
40  clabel = rlabel = NULL;
41  }
42  Bmatrix(long columns, long rows, int cflag, int rflag) { // Flags: 1=numbers, 0=letters
43  m=columns; n=rows;
44  data = new double[m*n];
45  clabel = rlabel = NULL;
46  long i;
47  Bstring label("@");
48  for ( i=0; i<m*n; i++ ) data[i] = 0;
49  if ( cflag ) {
50  for ( i=1; i<=m; i++ ) {
51  if ( cflag == 1 ) label = Bstring(i, "%d");
52  else label[0]++;
53  string_add(&clabel, label);
54  }
55  }
56  if ( rflag ) {
57  label = "@";
58  for ( i=1; i<=n; i++ ) {
59  if ( rflag == 1 ) label = Bstring(i, "%d");
60  else label[0]++;
61  string_add(&rlabel, label);
62  }
63  }
64  }
66  long i, j, k, l;
67  Bmatrix mat_new(mat.m, n);
68  if ( m != mat.n ) return mat_new;
69  mat_new.column_labels(mat.clabel);
70  mat_new.row_labels(rlabel);
71  for ( i=l=0; i<n; i++ )
72  for ( j=0; j<mat.m; j++, l++ )
73  for ( k=0; k<m; k++ ) mat_new.data[l] += data[i*m+k]*mat.data[k*mat.m+j];
74  return mat_new;
75  }
76  double& operator[](long i) { if ( i < 0 ) i = 0; if ( i >= m*n ) i = m*n-1; return data[i]; }
77  long columns() { return m; }
78  long rows() { return n; }
79  long size() { return m*n; }
80  Bstring* column_labels() { return clabel; }
81  Bstring* row_labels() { return rlabel; }
82  void column_labels(Bstring* labels) {
83  string_kill(clabel);
84  clabel = NULL;
85  for ( Bstring* l=labels; l; l=l->next ) string_add(&clabel, *l);
86  }
87  void row_labels(Bstring* labels) {
88  string_kill(rlabel);
89  rlabel = NULL;
90  for ( Bstring* l=labels; l; l=l->next ) string_add(&rlabel, *l);
91  }
92  void column_add_label(Bstring& s) { string_add(&clabel, s); }
93  void row_add_label(Bstring& s) { string_add(&rlabel, s); }
94  double determinant() {
95  if ( m != n ) return(0);
96  double* mat = new double [n*n];
97  for ( long i=0; i<n*n; i++ ) mat[i] = data[i];
98  double det = matrix_LU_decomposition(n, mat, NULL);
99  delete[] mat;
100  return(det);
101  }
102  void swap_columns_rows(long rc1, long rc2) {
103  long i, i1=m*rc1, i2=m*rc2;
104  for ( i=0; i<m; i++, i1++, i2++ ) swap(data[i1], data[i2]);
105  for ( i=0, i1=rc1, i2=rc2; i<n; i++, i1+=m, i2+=m ) swap(data[i1], data[i2]);
106  Bstring *l, t, *t1, *t2;
107  if ( clabel ) {
108  for ( l=clabel, i=0; l; l=l->next ) {
109  if ( i==rc1 ) t1 = l;
110  if ( i==rc2 ) t2 = l;
111  }
112  t = *t1; *t1 = *t2; *t2 = t;
113  }
114  if ( rlabel ) {
115  for ( l=rlabel, i=0; l; l=l->next ) {
116  if ( i==rc1 ) t1 = l;
117  if ( i==rc2 ) t2 = l;
118  }
119  t = *t1; *t1 = *t2; *t2 = t;
120  }
121  }
122 };
123 #define _Bmatrix_
124 #endif
125 
126 // Function prototypes
127 int matrix_show(Bmatrix matrix);
129 Bmatrix matrix_random(long m, long n);
130 int matrix_rearrange(Bmatrix matrix, int* order);
131 
double matrix_LU_decomposition(int n, double *a, double *b)
Matrix inversion and optionally solves a set of linear equations.
Bstring * string_add(Bstring **list, const char *string)
Adds a string to a linked list.
Definition: Bstring.cpp:748
Bmatrix operator*(Bmatrix &mat)
Definition: Bmatrix.h:65
double & operator[](long i)
Definition: Bmatrix.h:76
int matrix_rearrange(Bmatrix matrix, int *order)
Rearranges a matrix with a given order.
Definition: Bmatrix.cpp:88
Bimage * img_from_matrix(Bmatrix matrix)
Converts a matrix to an image.
Definition: Bmatrix.cpp:50
void column_add_label(Bstring &s)
Definition: Bmatrix.h:92
General image parameter class.
Definition: Bimage.h:147
void column_labels(Bstring *labels)
Definition: Bmatrix.h:82
Bmatrix(long columns, long rows)
Definition: Bmatrix.h:36
Bstring * column_labels()
Definition: Bmatrix.h:80
Bstring * next
Definition: Bstring.h:27
void row_labels(Bstring *labels)
Definition: Bmatrix.h:87
double determinant()
Definition: Bmatrix.h:94
long size()
Definition: Bmatrix.h:79
long rows()
Definition: Bmatrix.h:78
int matrix_show(Bmatrix matrix)
Displays the matrix contents.
Definition: Bmatrix.cpp:21
Header file for matrix functions.
Header file for the string class.
Bmatrix()
Definition: Bmatrix.h:35
Header file for 2D and 3D image I/O.
void row_add_label(Bstring &s)
Definition: Bmatrix.h:93
Bstring * row_labels()
Definition: Bmatrix.h:81
long columns()
Definition: Bmatrix.h:77
Bmatrix matrix_random(long m, long n)
Generates a matrix with random values.
Definition: Bmatrix.cpp:67
void swap_columns_rows(long rc1, long rc2)
Definition: Bmatrix.h:102
Bmatrix(long columns, long rows, int cflag, int rflag)
Definition: Bmatrix.h:42
int string_kill(Bstring *list)
Kills a string linked list.
Definition: Bstring.cpp:782