Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Rebased SGNDArray on SGReferencedData. Closes #771
  • Loading branch information
lisitsyn committed Sep 13, 2012
1 parent 89373e4 commit af52af3
Show file tree
Hide file tree
Showing 4 changed files with 142 additions and 75 deletions.
113 changes: 113 additions & 0 deletions src/shogun/lib/SGNDArray.cpp
@@ -0,0 +1,113 @@
/*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* Written (W) 2012 Fernando José Iglesias García
* Written (W) 2010,2012 Soeren Sonnenburg
* Copyright (C) 2010 Berlin Institute of Technology
* Copyright (C) 2012 Soeren Sonnenburg
*/

#include <shogun/lib/SGNDArray.h>
#include <shogun/lib/SGReferencedData.h>

namespace shogun
{

template<class T> SGNDArray<T>::SGNDArray() :
SGReferencedData()
{
init_data();
}

template<class T> SGNDArray<T>::SGNDArray(T* a, index_t* d, index_t nd, bool ref_counting) :
SGReferencedData(ref_counting)
{
array = a;
dims = d;
num_dims = nd;
}

template<class T> SGNDArray<T>::SGNDArray(index_t* d, index_t nd, bool ref_counting) :
SGReferencedData(ref_counting), dims(d), num_dims(nd)
{
index_t total = 1;
for (int32_t i=0; i<num_dims; i++)
total *= dims[i];
ASSERT(total>0);
array = SG_MALLOC(T, total);
}

template<class T> SGNDArray<T>::SGNDArray(const SGNDArray &orig) :
SGReferencedData(orig)
{
copy_data(orig);
}

template<class T> SGNDArray<T>::~SGNDArray()
{
unref();
}

template<class T> void SGNDArray<T>::copy_data(const SGReferencedData &orig)
{
array = ((SGNDArray*)(&orig))->array;
dims = ((SGNDArray*)(&orig))->dims;
num_dims = ((SGNDArray*)(&orig))->num_dims;
}

template<class T> void SGNDArray<T>::init_data()
{
array = NULL;
dims = NULL;
num_dims = 0;
}

template<class T> void SGNDArray<T>::free_data()
{
SG_FREE(array);
SG_FREE(dims);

array = NULL;
dims = NULL;
num_dims = 0;
}

template<class T> void SGNDArray<T>::transpose_matrix(index_t matIdx) const
{
ASSERT(array && dims && num_dims > 2 && dims[2] > matIdx);

T aux;
// Index to acces directly the elements of the matrix of interest
int32_t idx = matIdx*dims[0]*dims[1];

for (int32_t i=0; i<dims[0]; i++)
for (int32_t j=0; j<i-1; j++)
{
aux = array[idx + i + j*dims[0]];
array[idx + i + j*dims[0]] = array[idx + j + i*dims[0]];
array[idx + j + i*dims[1]] = aux;
}

// Swap the sizes of the two first dimensions
index_t auxDim = dims[0];
dims[0] = dims[1];
dims[1] = auxDim;
}

template class SGNDArray<bool>;
template class SGNDArray<char>;
template class SGNDArray<int8_t>;
template class SGNDArray<uint8_t>;
template class SGNDArray<int16_t>;
template class SGNDArray<uint16_t>;
template class SGNDArray<int32_t>;
template class SGNDArray<uint32_t>;
template class SGNDArray<int64_t>;
template class SGNDArray<uint64_t>;
template class SGNDArray<float32_t>;
template class SGNDArray<float64_t>;
template class SGNDArray<floatmax_t>;
}
92 changes: 26 additions & 66 deletions src/shogun/lib/SGNDArray.h
Expand Up @@ -15,60 +15,28 @@

#include <shogun/lib/config.h>
#include <shogun/lib/DataType.h>
#include <shogun/lib/SGReferencedData.h>

namespace shogun
{
/** @brief shogun n-dimensional array */
template<class T> class SGNDArray
template<class T> class SGNDArray : public SGReferencedData
{
public:
/** default constructor */
SGNDArray() : array(NULL), dims(NULL), num_dims(0), do_free(false) { }
SGNDArray();

/** constructor for setting params */
SGNDArray(T* a, index_t* d, index_t nd, bool do_free_ndarray = false)
: array(a), dims(d), num_dims(nd), do_free(do_free_ndarray) { }
SGNDArray(T* a, index_t* d, index_t nd, bool ref_counting=true);

/** constructor to create new ndarray in memory */
SGNDArray(index_t* d, index_t nd, bool do_free_ndarray = false)
: dims(d), num_dims(nd), do_free(do_free_ndarray)
{
index_t tot = 1;
for (int32_t i=0; i<nd; i++)
tot *= dims[i];
array=SG_MALLOC(T, tot);
}

SGNDArray(index_t* d, index_t nd, bool ref_counting=true);

/** copy constructor */
SGNDArray(const SGNDArray &orig)
: array(orig.array), dims(orig.dims), num_dims(orig.num_dims),
do_free(orig.do_free) { }
SGNDArray(const SGNDArray &orig);

/** empty destructor */
virtual ~SGNDArray()
{
}

/** free ndarray */
virtual void free_ndarray()
{
if (do_free)
SG_FREE(array);

SG_FREE(dims);

array = NULL;
dims = NULL;
num_dims = 0;
}


/** destroy ndarray */
virtual void destroy_ndarray()
{
do_free = true;
free_ndarray();
}
virtual ~SGNDArray();

/** get a matrix formed by the two first dimensions
*
Expand All @@ -81,6 +49,12 @@ template<class T> class SGNDArray
return &array[matIdx*dims[0]*dims[1]];
}

/** transposes a matrix formed by the two first dimensions
*
* @param matIdx matrix index
*/
void transpose_matrix(index_t matIdx) const;

/** operator overload for ndarray read only access
*
* @param index to access
Expand All @@ -99,41 +73,27 @@ template<class T> class SGNDArray
return array[index];
}

/** transposes a matrix formed by the two first dimensions
*
* @param matIdx matrix index
*/
void transpose_matrix(index_t matIdx) const
{
ASSERT(array && dims && num_dims > 2 && dims[2] > matIdx);
protected:

T aux;
// Index to acces directly the elements of the matrix of interest
int32_t idx = matIdx*dims[0]*dims[1];

for (int32_t i=0; i<dims[0]; i++)
for (int32_t j=0; j<i-1; j++)
{
aux = array[idx + i + j*dims[0]];
array[idx + i + j*dims[0]] = array[idx + j + i*dims[0]];
array[idx + j + i*dims[1]] = aux;
}

// Swap the sizes of the two first dimensions
index_t auxDim = dims[0];
dims[0] = dims[1];
dims[1] = auxDim;
}
/** copy data */
virtual void copy_data(const SGReferencedData &orig);

/** init data */
virtual void init_data();

/** free data */
virtual void free_data();

public:

/** array */
T* array;

/** dimension sizes */
index_t* dims;

/** number of dimensions */
index_t num_dims;
/** whether ndarry needs to be freed */
bool do_free;
};
}
#endif // __SGNDARRAY_H__
10 changes: 2 additions & 8 deletions src/shogun/multiclass/QDA.cpp
Expand Up @@ -59,11 +59,6 @@ void CQDA::init()

void CQDA::cleanup()
{
if ( m_store_covs )
m_covs.destroy_ndarray();

m_covs.free_ndarray();
m_M.free_ndarray();
m_means=SGMatrix<float64_t>();

m_num_classes = 0;
Expand Down Expand Up @@ -206,7 +201,7 @@ bool CQDA::train_machine(CFeatures* data)
cov_dims[0] = m_dim;
cov_dims[1] = m_dim;
cov_dims[2] = m_num_classes;
m_covs = SGNDArray< float64_t >(cov_dims, 3, true);
m_covs = SGNDArray< float64_t >(cov_dims, 3);
}

m_means = SGMatrix< float64_t >(m_dim, m_num_classes, true);
Expand Down Expand Up @@ -290,7 +285,7 @@ bool CQDA::train_machine(CFeatures* data)
M_dims[0] = m_dim;
M_dims[1] = m_dim;
M_dims[2] = m_num_classes;
m_M = SGNDArray< float64_t >(M_dims, 3, true);
m_M = SGNDArray< float64_t >(M_dims, 3);

m_slog = SGVector< float32_t >(m_num_classes);
m_slog.zero();
Expand Down Expand Up @@ -335,7 +330,6 @@ bool CQDA::train_machine(CFeatures* data)
SG_PRINT("\n>>> Exit DEBUG_QDA\n");
#endif

rotations.destroy_ndarray();
SG_FREE(class_idxs);
SG_FREE(class_nums);
return true;
Expand Down
2 changes: 1 addition & 1 deletion src/shogun/ui/SGInterface.cpp
Expand Up @@ -6372,7 +6372,7 @@ bool CSGInterface::cmd_set_model()
ASSERT(numDim==3);
ASSERT(Dim[0]==Dim[1]);

if (!pm->compute_plif_matrix(SGNDArray<float64_t>(penalties_array, Dim, numDim)))
if (!pm->compute_plif_matrix(SGNDArray<float64_t>(penalties_array, Dim, numDim, false)))
SG_ERROR("error computing plif matrix\n");
ui_structure->set_num_states(Dim[0]);
SG_FREE(penalties_array);
Expand Down

0 comments on commit af52af3

Please sign in to comment.