SHOGUN  v3.2.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines
SGMatrixList.cpp
Go to the documentation of this file.
00001 /*
00002  * This program is free software; you can redistribute it and/or modify
00003  * it under the terms of the GNU General Public License as published by
00004  * the Free Software Foundation; either version 3 of the License, or
00005  * (at your option) any later version.
00006  *
00007  * Written (W) 2012 Fernando José Iglesias García
00008  * Copyright (C) 2012 Fernando José Iglesias García
00009  */
00010 
00011 #include <shogun/lib/SGMatrixList.h>
00012 #include <shogun/lib/memory.h>
00013 
00014 namespace shogun {
00015 
00016 template <class T>
00017 SGMatrixList<T>::SGMatrixList() : SGReferencedData()
00018 {
00019     init_data();
00020 }
00021 
00022 template <class T>
00023 SGMatrixList<T>::SGMatrixList(SGMatrix<T>* ml, int32_t nmats, bool ref_counting)
00024 : SGReferencedData(ref_counting), matrix_list(ml), num_matrices(nmats)
00025 {
00026 }
00027 
00028 template <class T>
00029 SGMatrixList<T>::SGMatrixList(int32_t nmats, bool ref_counting)
00030 : SGReferencedData(ref_counting), num_matrices(nmats)
00031 {
00032     matrix_list = SG_MALLOC(SGMatrix<T>, nmats);
00033 }
00034 
00035 template <class T>
00036 SGMatrixList<T>::SGMatrixList(SGMatrixList const & orig) : SGReferencedData(orig)
00037 {
00038     copy_data(orig);
00039 }
00040 
00041 template <class T>
00042 SGMatrixList<T>::~SGMatrixList()
00043 {
00044     unref();
00045 }
00046 
00047 template <class T>
00048 SGMatrix<T> SGMatrixList<T>::get_matrix(index_t index) const
00049 {
00050     return matrix_list[index];
00051 }
00052 
00053 template <class T>
00054 SGMatrix<T> SGMatrixList<T>::operator[](index_t index) const
00055 {
00056     return matrix_list[index];
00057 }
00058 
00059 template <class T>
00060 void SGMatrixList<T>::set_matrix(index_t index, const SGMatrix<T> matrix)
00061 {
00062     matrix_list[index] = matrix;
00063 }
00064 
00065 template <class T>
00066 void SGMatrixList<T>::copy_data(const SGReferencedData &orig)
00067 {
00068     matrix_list  = ((SGMatrixList*) (&orig))->matrix_list;
00069     num_matrices = ((SGMatrixList*) (&orig))->num_matrices;
00070 }
00071 
00072 template <class T>
00073 void SGMatrixList<T>::init_data()
00074 {
00075     matrix_list  = NULL;
00076     num_matrices = 0;
00077 }
00078 
00079 template <class T>
00080 void SGMatrixList<T>::free_data()
00081 {
00082     SG_FREE(matrix_list);
00083     num_matrices = 0;
00084     matrix_list = NULL;
00085 }
00086 
00087 template <class T>
00088 SGMatrixList<T> SGMatrixList<T>::split(SGMatrix<T> matrix, int32_t num_components)
00089 {
00090     REQUIRE((matrix.num_cols % num_components) == 0,
00091         "The number of columns (%d) must be multiple of the number "
00092         "of components (%d).\n",
00093         matrix.num_cols, num_components);
00094 
00095     int32_t new_num_cols = matrix.num_cols / num_components;
00096     SGMatrixList<T> out(num_components);
00097 
00098     for ( int32_t i = 0 ; i < num_components ; ++i )
00099     {
00100         SGMatrix<T> new_matrix = SGMatrix<T>(matrix.num_rows, new_num_cols);
00101 
00102         for ( int32_t row = 0 ; row < matrix.num_rows ; ++row )
00103         {
00104             for ( int32_t col = 0 ; col < new_num_cols ; ++col )
00105                 new_matrix(row, col) = matrix(row, int64_t(i)*new_num_cols + col);
00106         }
00107 
00108         out.set_matrix(i, new_matrix);
00109     }
00110 
00111     return out;
00112 }
00113 
00114 template class SGMatrixList<bool>;
00115 template class SGMatrixList<char>;
00116 template class SGMatrixList<int8_t>;
00117 template class SGMatrixList<uint8_t>;
00118 template class SGMatrixList<int16_t>;
00119 template class SGMatrixList<uint16_t>;
00120 template class SGMatrixList<int32_t>;
00121 template class SGMatrixList<uint32_t>;
00122 template class SGMatrixList<int64_t>;
00123 template class SGMatrixList<uint64_t>;
00124 template class SGMatrixList<float32_t>;
00125 template class SGMatrixList<float64_t>;
00126 template class SGMatrixList<floatmax_t>;
00127 
00128 } /* namespace shogun */
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines

SHOGUN Machine Learning Toolbox - Documentation