SHOGUN  4.1.0
 全部  命名空间 文件 函数 变量 类型定义 枚举 枚举值 友元 宏定义  
SGMatrixList.cpp
浏览该文件的文档.
1 /*
2  * This program is free software; you can redistribute it and/or modify
3  * it under the terms of the GNU General Public License as published by
4  * the Free Software Foundation; either version 3 of the License, or
5  * (at your option) any later version.
6  *
7  * Written (W) 2012 Fernando José Iglesias García
8  * Copyright (C) 2012 Fernando José Iglesias García
9  */
10 
12 #include <shogun/lib/memory.h>
13 #include <shogun/io/SGIO.h>
14 
15 namespace shogun {
16 
17 template <class T>
19 {
20  init_data();
21 }
22 
23 template <class T>
24 SGMatrixList<T>::SGMatrixList(SGMatrix<T>* ml, int32_t nmats, bool ref_counting)
25 : SGReferencedData(ref_counting), matrix_list(ml), num_matrices(nmats)
26 {
27 }
28 
29 template <class T>
30 SGMatrixList<T>::SGMatrixList(int32_t nmats, bool ref_counting)
31 : SGReferencedData(ref_counting), num_matrices(nmats)
32 {
33  matrix_list = SG_MALLOC(SGMatrix<T>, nmats);
34 }
35 
36 template <class T>
38 {
39  copy_data(orig);
40 }
41 
42 template <class T>
44 {
45  unref();
46 }
47 
48 template <class T>
50 {
51  return matrix_list[index];
52 }
53 
54 template <class T>
56 {
57  return matrix_list[index];
58 }
59 
60 template <class T>
62 {
63  matrix_list[index] = matrix;
64 }
65 
66 template <class T>
68 {
69  matrix_list = ((SGMatrixList*) (&orig))->matrix_list;
70  num_matrices = ((SGMatrixList*) (&orig))->num_matrices;
71 }
72 
73 template <class T>
75 {
76  matrix_list = NULL;
77  num_matrices = 0;
78 }
79 
80 template <class T>
82 {
83  SG_FREE(matrix_list);
84  num_matrices = 0;
85  matrix_list = NULL;
86 }
87 
88 template <class T>
89 SGMatrixList<T> SGMatrixList<T>::split(SGMatrix<T> matrix, int32_t num_components)
90 {
91  REQUIRE((matrix.num_cols % num_components) == 0,
92  "The number of columns (%d) must be multiple of the number "
93  "of components (%d).\n",
94  matrix.num_cols, num_components);
95 
96  int32_t new_num_cols = matrix.num_cols / num_components;
97  SGMatrixList<T> out(num_components);
98 
99  for ( int32_t i = 0 ; i < num_components ; ++i )
100  {
101  SGMatrix<T> new_matrix = SGMatrix<T>(matrix.num_rows, new_num_cols);
102 
103  for ( int32_t row = 0 ; row < matrix.num_rows ; ++row )
104  {
105  for ( int32_t col = 0 ; col < new_num_cols ; ++col )
106  new_matrix(row, col) = matrix(row, int64_t(i)*new_num_cols + col);
107  }
108 
109  out.set_matrix(i, new_matrix);
110  }
111 
112  return out;
113 }
114 
115 template class SGMatrixList<bool>;
116 template class SGMatrixList<char>;
117 template class SGMatrixList<int8_t>;
118 template class SGMatrixList<uint8_t>;
119 template class SGMatrixList<int16_t>;
120 template class SGMatrixList<uint16_t>;
121 template class SGMatrixList<int32_t>;
122 template class SGMatrixList<uint32_t>;
123 template class SGMatrixList<int64_t>;
124 template class SGMatrixList<uint64_t>;
125 template class SGMatrixList<float32_t>;
126 template class SGMatrixList<float64_t>;
127 template class SGMatrixList<floatmax_t>;
128 
129 } /* namespace shogun */
int32_t index_t
Definition: common.h:62
static SGMatrixList< T > split(SGMatrix< T > matrix, int32_t num_components)
shogun matrix list
Definition: SGMatrixList.h:23
#define REQUIRE(x,...)
Definition: SGIO.h:206
index_t num_cols
Definition: SGMatrix.h:378
SGMatrix< T > * matrix_list
Definition: SGMatrixList.h:91
index_t num_rows
Definition: SGMatrix.h:376
shogun matrix
void set_matrix(index_t index, const SGMatrix< T > matrix)
shogun reference count managed data
virtual void init_data()
SGMatrix< T > operator[](index_t index) const
SGMatrix< T > get_matrix(index_t index) const
all of classes and functions are contained in the shogun namespace
Definition: class_list.h:18
virtual void free_data()
virtual void copy_data(const SGReferencedData &orig)

SHOGUN 机器学习工具包 - 项目文档