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 | |
---|
24 | AP_smatrix::AP_smatrix(size_t si) |
---|
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**)calloc(1, 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 | |
---|
39 | AP_smatrix::~AP_smatrix() { |
---|
40 | free(m); |
---|
41 | } |
---|
42 | |
---|
43 | AP_FLOAT AP_smatrix::get_max_value() const { // O(n*2) |
---|
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 | |
---|
56 | void AP_matrix::set_desc(char**& which_desc, int idx, const char *desc) { |
---|
57 | if (!which_desc) which_desc = (char**)GB_calloc(sizeof(char*), size); |
---|
58 | which_desc[idx] = strdup(desc); |
---|
59 | } |
---|
60 | |
---|
61 | void AP_matrix::create_awars(AW_root *awr, const char *awar_prefix) { |
---|
62 | char buffer[1024]; |
---|
63 | int x, y; |
---|
64 | for (x = 0; x<size; x++) { |
---|
65 | if (x_description[x]) { |
---|
66 | for (y = 0; y<size; y++) { |
---|
67 | if (y_description[y]) { |
---|
68 | sprintf(buffer, "%s/B%s/B%s", awar_prefix, x_description[x], y_description[y]); |
---|
69 | if (x==y) { |
---|
70 | awr->awar_float(buffer, 0)->set_minmax(0.0, 2.0); |
---|
71 | } |
---|
72 | else { |
---|
73 | awr->awar_float(buffer, 1.0)->set_minmax(0.0, 2.0); |
---|
74 | } |
---|
75 | } |
---|
76 | |
---|
77 | } |
---|
78 | } |
---|
79 | } |
---|
80 | } |
---|
81 | void AP_matrix::read_awars(AW_root *awr, const char *awar_prefix) { |
---|
82 | char buffer[1024]; |
---|
83 | int x, y; |
---|
84 | for (x = 0; x<size; x++) { |
---|
85 | if (x_description[x]) { |
---|
86 | for (y = 0; y<size; y++) { |
---|
87 | if (y_description[y]) { |
---|
88 | sprintf(buffer, "%s/B%s/B%s", awar_prefix, x_description[x], y_description[y]); |
---|
89 | this->set(x, y, awr->awar(buffer)->read_float()); |
---|
90 | } |
---|
91 | } |
---|
92 | } |
---|
93 | } |
---|
94 | } |
---|
95 | |
---|
96 | void AP_matrix::create_input_fields(AW_window *aww, const char *awar_prefix) { |
---|
97 | char buffer[1024]; |
---|
98 | int x, y; |
---|
99 | aww->create_button(0, " "); |
---|
100 | for (x = 0; x<size; x++) { |
---|
101 | if (x_description[x]) { |
---|
102 | aww->create_button(0, x_description[x]); |
---|
103 | } |
---|
104 | } |
---|
105 | aww->at_newline(); |
---|
106 | for (x = 0; x<size; x++) { |
---|
107 | if (x_description[x]) { |
---|
108 | aww->create_button(0, x_description[x]); |
---|
109 | for (y = 0; y<size; y++) { |
---|
110 | if (y_description[y]) { |
---|
111 | sprintf(buffer, "%s/B%s/B%s", awar_prefix, x_description[x], y_description[y]); |
---|
112 | aww->create_input_field(buffer, 4); |
---|
113 | } |
---|
114 | } |
---|
115 | aww->at_newline(); |
---|
116 | } |
---|
117 | } |
---|
118 | } |
---|
119 | |
---|
120 | void AP_matrix::normize() { // set values so that average of non diag elems == 1.0 |
---|
121 | int x, y; |
---|
122 | double sum = 0.0; |
---|
123 | double elems = 0.0; |
---|
124 | for (x = 0; x<size; x++) { |
---|
125 | if (x_description[x]) { |
---|
126 | for (y = 0; y<size; y++) { |
---|
127 | if (y!=x && y_description[y]) { |
---|
128 | sum += this->get(x, y); |
---|
129 | elems += 1.0; |
---|
130 | } |
---|
131 | } |
---|
132 | } |
---|
133 | } |
---|
134 | if (sum == 0.0) return; |
---|
135 | sum /= elems; |
---|
136 | for (x = 0; x<size; x++) { |
---|
137 | for (y = 0; y<size; y++) { |
---|
138 | this->set(x, y, get(x, y)/sum); |
---|
139 | } |
---|
140 | } |
---|
141 | } |
---|
142 | |
---|
143 | AP_matrix::AP_matrix(long si) |
---|
144 | : x_description(NULL), |
---|
145 | y_description(NULL) |
---|
146 | { |
---|
147 | m = (AP_FLOAT **)calloc(sizeof(AP_FLOAT *), (size_t)si); |
---|
148 | for (long i=0; i<si; i++) { |
---|
149 | m[i] = (AP_FLOAT *)calloc(sizeof(AP_FLOAT), (size_t)(si)); |
---|
150 | } |
---|
151 | size = si; |
---|
152 | } |
---|
153 | |
---|
154 | AP_matrix::~AP_matrix() { |
---|
155 | for (long i=0; i<size; i++) { |
---|
156 | free(m[i]); |
---|
157 | if (x_description) free(x_description[i]); |
---|
158 | if (y_description) free(y_description[i]); |
---|
159 | } |
---|
160 | free(x_description); |
---|
161 | free(y_description); |
---|
162 | free(m); |
---|
163 | } |
---|
164 | |
---|