ARB
AP_matrix.cxx
Go to the documentation of this file.
1 // =============================================================== //
2 // //
3 // File : AP_matrix.cxx //
4 // Purpose : //
5 // //
6 // Institute of Microbiology (Technical University Munich) //
7 // http://www.arb-home.de/ //
8 // //
9 // =============================================================== //
10 
11 #include "AP_matrix.hxx"
12 
13 #include <arbdbt.h>
14 #include <aw_window.hxx>
15 #include <aw_root.hxx>
16 #include <aw_awar.hxx>
17 #include <cfloat>
18 
19 #define ap_assert(cond) arb_assert(cond)
20 
21 // --------------------
22 // AP_smatrix
23 
25  : Size(si)
26 {
27  size_t elements = Size*(Size+1)/2;
28  size_t headsize = Size * sizeof(*m);
29  size_t datasize = elements * sizeof(*(m[0]));
30 
31  m = (AP_FLOAT**)ARB_calloc<char>(headsize+datasize);
32  m[0] = (AP_FLOAT*)(((char*)m)+headsize);
33 
34  for (size_t i=1; i<si; i++) {
35  m[i] = m[i-1]+i;
36  }
37 }
38 
40  free(m);
41 }
42 
44  AP_FLOAT max = -FLT_MAX;
45  for (size_t i=0; i<Size; i++) {
46  for (size_t j=0; j<i; j++) {
47  if (m[i][j] > max) max = m[i][j];
48  }
49  }
50  return max;
51 }
52 
53 // ------------------
54 // AP_matrix
55 
57  ARB_calloc(m, si);
58  for (long i=0; i<si; i++) {
59  ARB_calloc(m[i], si);
60  }
61  size = si;
62 }
63 
65  for (long i=0; i<size; i++) {
66  free(m[i]);
67  }
68  free(m);
69 }
70 
71 // ---------------------------
72 // AP_userdef_matrix
73 
74 void AP_userdef_matrix::set_desc(char**& which_desc, int idx, const char *desc) {
75  if (!which_desc) ARB_calloc(which_desc, get_size());
76  which_desc[idx] = strdup(desc);
77 }
78 
80  char buffer[1024];
81  int x, y;
82  for (x = 0; x<get_size(); x++) {
83  if (x_description[x]) {
84  for (y = 0; y<get_size(); y++) {
85  if (y_description[y]) {
86  sprintf(buffer, "%s/B%s/B%s", awar_prefix, x_description[x], y_description[y]);
87  if (x==y) {
88  awr->awar_float(buffer, 0)->set_minmax(0.0, 2.0);
89  }
90  else {
91  awr->awar_float(buffer, 1.0)->set_minmax(0.0, 2.0);
92  }
93  }
94 
95  }
96  }
97  }
98 }
100  char buffer[1024];
101  int x, y;
102  for (x = 0; x<get_size(); x++) {
103  if (x_description[x]) {
104  for (y = 0; y<get_size(); y++) {
105  if (y_description[y]) {
106  sprintf(buffer, "%s/B%s/B%s", awar_prefix, x_description[x], y_description[y]);
107  this->set(x, y, awr->awar(buffer)->read_float());
108  }
109  }
110  }
111  }
112 }
113 
115  char buffer[1024];
116  int x, y;
117  aww->create_button(NULp, " ");
118  for (x = 0; x<get_size(); x++) {
119  if (x_description[x]) {
120  aww->create_button(NULp, x_description[x]);
121  }
122  }
123  aww->at_newline();
124  for (x = 0; x<get_size(); x++) {
125  if (x_description[x]) {
126  aww->create_button(NULp, x_description[x]);
127  for (y = 0; y<get_size(); y++) {
128  if (y_description[y]) {
129  sprintf(buffer, "%s/B%s/B%s", awar_prefix, x_description[x], y_description[y]);
130  aww->create_input_field(buffer, 4);
131  }
132  }
133  aww->at_newline();
134  }
135  }
136 }
137 
138 void AP_userdef_matrix::normize() { // set values so that average of non diag elems == 1.0
139  int x, y;
140  double sum = 0.0;
141  double elems = 0.0;
142  for (x = 0; x<get_size(); x++) {
143  if (x_description[x]) {
144  for (y = 0; y<get_size(); y++) {
145  if (y!=x && y_description[y]) {
146  sum += this->get(x, y);
147  elems += 1.0;
148  }
149  }
150  }
151  }
152  if (sum == 0.0) return;
153  sum /= elems;
154  for (x = 0; x<get_size(); x++) {
155  for (y = 0; y<get_size(); y++) { // LOOP_VECTORIZED
156  this->set(x, y, get(x, y)/sum);
157  }
158  }
159 }
160 
162  for (long i=0; i<get_size(); i++) {
163  if (x_description) free(x_description[i]);
164  if (y_description) free(y_description[i]);
165  }
166  free(x_description);
167  free(y_description);
168  free(awar_prefix);
169 }
170 
AP_FLOAT get_max_value() const
Definition: AP_matrix.cxx:43
static char * y[maxsp+1]
AW_awar * set_minmax(float min, float max)
Definition: AW_awar.cxx:532
void set(int i, int j, AP_FLOAT val)
Definition: AP_matrix.hxx:60
void create_input_fields(AW_window *aww)
Definition: AP_matrix.cxx:114
char buffer[MESSAGE_BUFFERSIZE]
Definition: seq_search.cxx:34
double AP_FLOAT
Definition: AP_matrix.hxx:27
AW_awar * awar_float(const char *var_name, float default_value=0.0, AW_default default_file=AW_ROOT_DEFAULT)
Definition: AW_root.cxx:560
AP_smatrix(size_t si)
Definition: AP_matrix.cxx:24
AW_awar * awar(const char *awar)
Definition: AW_root.cxx:554
float read_float() const
Definition: AW_awar.cxx:180
void create_input_field(const char *awar_name, int columns=0)
Definition: AW_button.cxx:855
TYPE * ARB_calloc(size_t nelem)
Definition: arb_mem.h:81
AP_matrix(long si)
Definition: AP_matrix.cxx:56
void update_from_awars(AW_root *awr)
Definition: AP_matrix.cxx:99
void create_awars(AW_root *awr)
Definition: AP_matrix.cxx:79
#define NULp
Definition: cxxforward.h:97
long get_size() const
Definition: AP_matrix.hxx:63
void at_newline()
Definition: AW_at.cxx:119
void create_button(const char *macro_name, AW_label label, const char *mnemonic=NULp, const char *color=NULp)
Definition: AW_button.cxx:446
#define max(a, b)
Definition: f2c.h:154