source: tags/ms_r18q1/SL/MATRIX/AP_matrix.cxx

Last change on this file was 16763, checked in by westram, 6 years ago
  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 4.7 KB
Line 
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
24AP_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**)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
39AP_smatrix::~AP_smatrix() {
40    free(m);
41}
42
43AP_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
56AP_matrix::AP_matrix(long si) {
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
64AP_matrix::~AP_matrix() {
65    for (long i=0; i<size; i++) {
66        free(m[i]);
67    }
68    free(m);
69}
70
71// ---------------------------
72//      AP_userdef_matrix
73
74void 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
79void AP_userdef_matrix::create_awars(AW_root *awr) {
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}
99void AP_userdef_matrix::update_from_awars(AW_root *awr) {
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
114void AP_userdef_matrix::create_input_fields(AW_window *aww) {
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
138void 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
161AP_userdef_matrix::~AP_userdef_matrix() {
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
Note: See TracBrowser for help on using the repository browser.