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 
00013 namespace shogun {
00014 
00015 template <class T>
00016 SGMatrixList<T>::SGMatrixList() : SGReferencedData()
00017 {
00018     init_data();
00019 }
00020 
00021 template <class T>
00022 SGMatrixList<T>::SGMatrixList(SGMatrix<T>* ml, int32_t nmats, bool ref_counting)
00023 : SGReferencedData(ref_counting), matrix_list(ml), num_matrices(nmats)
00024 {
00025 }
00026 
00027 template <class T>
00028 SGMatrixList<T>::SGMatrixList(int32_t nmats, bool ref_counting)
00029 : SGReferencedData(ref_counting), num_matrices(nmats)
00030 {
00031     matrix_list = SG_MALLOC(SGMatrix<T>, nmats);
00032     // Call to SGMatrix default constructor in-place
00033     for ( int32_t i = 0 ; i < nmats ; ++i )
00034         new (&matrix_list[i]) SGMatrix<T>();
00035 }
00036 
00037 template <class T>
00038 SGMatrixList<T>::SGMatrixList(SGMatrixList const & orig) : SGReferencedData(orig)
00039 {
00040     copy_data(orig);
00041 }
00042 
00043 template <class T>
00044 SGMatrixList<T>::~SGMatrixList()
00045 {
00046     unref();
00047 }
00048 
00049 template <class T>
00050 void SGMatrixList<T>::copy_data(SGReferencedData const & orig)
00051 {
00052     matrix_list  = ((SGMatrixList*) (&orig))->matrix_list;
00053     num_matrices = ((SGMatrixList*) (&orig))->num_matrices;
00054 }
00055 
00056 template <class T>
00057 void SGMatrixList<T>::init_data()
00058 {
00059     matrix_list  = NULL;
00060     num_matrices = 0;
00061 }
00062 
00063 template <class T>
00064 void SGMatrixList<T>::free_data()
00065 {
00066     cleanup_matrices();
00067     SG_FREE(matrix_list);
00068     num_matrices = 0;
00069     matrix_list = NULL;
00070 }
00071 
00072 template <class T>
00073 void SGMatrixList<T>::cleanup_matrices()
00074 {
00075     if ( matrix_list && num_matrices )
00076     {
00077         for ( int32_t i = 0 ; i < num_matrices ; ++i )
00078         {
00079             // Explicit call to the destructor required
00080             // due to the use of in-place constructors
00081             matrix_list[i].~SGMatrix();
00082         }
00083     }
00084 }
00085 
00086 template <class T>
00087 SGMatrixList<T> SGMatrixList<T>::split(SGMatrix<T> matrix, int32_t num_components)
00088 {
00089     REQUIRE((matrix.num_cols % num_components) == 0,
00090         "The number of columns (%d) must be multiple of the number "
00091         "of components (%d).\n",
00092         matrix.num_cols, num_components);
00093 
00094     int32_t new_num_cols = matrix.num_cols / num_components;
00095     int32_t start;
00096     SGMatrixList<T> out(num_components);
00097 
00098     for ( int32_t i = 0 ; i < num_components ; ++i )
00099     {
00100         out[i] = SGMatrix<T>(matrix.num_rows, new_num_cols);
00101         start = i*matrix.num_rows*new_num_cols;
00102 
00103         for ( int32_t row = 0 ; row < matrix.num_rows ; ++row )
00104         {
00105             for ( int32_t col = 0 ; col < new_num_cols ; ++col )
00106             {
00107                 out[i][col*matrix.num_rows + row] =
00108                     matrix[start + col*matrix.num_rows + row];
00109             }
00110         }
00111     }
00112 
00113     return out;
00114 }
00115 
00116 template class SGMatrixList<bool>;
00117 template class SGMatrixList<char>;
00118 template class SGMatrixList<int8_t>;
00119 template class SGMatrixList<uint8_t>;
00120 template class SGMatrixList<int16_t>;
00121 template class SGMatrixList<uint16_t>;
00122 template class SGMatrixList<int32_t>;
00123 template class SGMatrixList<uint32_t>;
00124 template class SGMatrixList<int64_t>;
00125 template class SGMatrixList<uint64_t>;
00126 template class SGMatrixList<float32_t>;
00127 template class SGMatrixList<float64_t>;
00128 template class SGMatrixList<floatmax_t>;
00129 
00130 } /* namespace shogun */
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines

SHOGUN Machine Learning Toolbox - Documentation