# orange / source / orange / symmatrix.hpp

 ``` 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79``` ```#ifndef __SYMMATRIX_HPP #define __SYMMATRIX_HPP #include "root.hpp" #include #include #include "stdlib.h" #include using namespace std; class ORANGE_API TSymMatrix : public TOrange { public: __REGISTER_CLASS CLASSCONSTANTS(Shape) enum { Lower, Upper, Symmetric, LowerFilled, UpperFilled }; int dim; //PR matrix dimension int matrixType; //P(&SymMatrix_Shape) 0 lower, 1 upper, 2 symmetric, 3 lower_filled, 4 upper_filled float *elements; TSymMatrix(const int &adim, const float &init = 0) : dim(adim), matrixType(Symmetric), elements(mlnew float[((adim+1)*(adim+2))>>1]) { for(float *pi = elements, *pe = elements+(((adim+1)*(adim+2))>>1); pi!=pe; *(pi++) = init); } ~TSymMatrix() { mldelete elements; } int getindex(const int &i, const int &j, bool raiseExceptions = true) const; inline float &getref(const int &i, const int &j) { return elements[getindex(i, j)]; } inline const float &getref(const int &i, const int &j) const { return elements[getindex(i, j)]; } inline const float getitem(const int &i, const int &j) const { const int index = getindex(i, j, false); return index<0 ? float(0.0) : elements[getindex(i, j)]; } typedef std::pair coord_t; struct pkt_less { bool operator ()(const coord_t &e1, const coord_t &e2) const { return (e1.second < e2.second); } }; void getknn(const int &i, const int &k, vector &knn) { vector knn_tmp; int j; for (j=0; j < dim; j++) if (j != i) knn_tmp.push_back(coord_t(j, elements[getindex(i, j)])); sort(knn_tmp.begin(), knn_tmp.end(), pkt_less()); for (j=0; j < k; j++) { knn.push_back(knn_tmp[j].first); } } void index2coordinates(const float *f, int &x, int &y) const { index2coordinates(f-elements, x, y); } static void index2coordinates(const int &index, int &x, int &y) { x = int(floor( (sqrt(float(1+8*index)) -1) / 2)); y = index - (x*(x+1))/2; } }; WRAPPER(SymMatrix) #endif ```