12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097 |
- #include "FeatureMatrixT.h"
- namespace NICE {
-
-
-
-
- template <typename T>
- FeatureMatrixT<T>::FeatureMatrixT()
- {
- this->ui_n = 0;
- this->ui_d = 0;
- this->features.clear();
- this->b_verbose = false;
- this->b_debug = false;
- }
-
- template <typename T>
- FeatureMatrixT<T>::FeatureMatrixT(const std::vector<std::vector<T> > & _features,
- const uint & _dim
- )
- {
-
- if (_dim == 0)
- this->set_d( (*_features.begin()).size() );
- else
- this->set_d( _dim );
- for (typename std::vector<std::vector<T> >::const_iterator it = _features.begin(); it != _features.end(); it++)
- {
- add_feature(*it);
- }
- this->b_verbose = false;
- this->b_debug = false;
- }
-
- template <typename T>
- FeatureMatrixT<T>::
- FeatureMatrixT(const std::vector< const NICE::SparseVector * > & _features,
- const bool _dimensionsOverExamples,
- const uint & _dim
- )
- {
- this->features.clear();
-
- if (_dim == 0)
- this->set_d( (*_features.begin())->getDim() );
- else
- this->set_d( _dim );
-
- if ( this->ui_d > 0 )
- {
- if (_dimensionsOverExamples)
- this->ui_n = _features[0]->getDim();
- else
- this->ui_n = _features.size();
- }
-
- if (_dimensionsOverExamples)
- {
- for (uint dim = 0; dim < this->ui_d; dim++)
- {
- this->features[dim].insert( _features[dim] );
- }
- }
- else
- {
-
- for (uint nr = 0; nr < this->ui_n; nr++)
- {
-
- for (NICE::SparseVector::const_iterator elemIt = _features[nr]->begin(); elemIt != _features[nr]->end(); elemIt++)
- {
-
- this->features[elemIt->first].insert( (T) elemIt->second, nr);
- }
- }
- }
-
- for (typename std::vector<NICE::SortedVectorSparse<T> >::iterator it = this->features.begin(); it != this->features.end(); it++)
- (*it).setN( this->ui_n );
- }
- #ifdef NICE_USELIB_MATIO
-
- template <typename T>
- FeatureMatrixT<T>::
- FeatureMatrixT(const sparse_t & _features,
- const uint & _dim
- )
- {
- if (_dim < 0)
- set_d( _features.njc -1 );
- else
- set_d( _dim );
- uint nMax(0);
- for ( uint i = 0; i < _features.njc-1; i++ )
- {
- for ( uint j = _features.jc[i]; j < _features.jc[i+1] && j < _features.ndata; j++ )
- {
- this->features[i].insert(((T*)_features.data)[j], true , _features.ir[ j]);
- if ((_features.ir[ j])>nMax)
- nMax = _features.ir[ j];
- }
- }
- for (typename std::vector<NICE::SortedVectorSparse<T> >::iterator it = this->features.begin(); it != this->features.end(); it++)
- {
- (*it).setN(nMax+1);
- }
- this->ui_n = nMax+1;
- this->b_verbose = false;
- }
-
- template <typename T>
- FeatureMatrixT<T>::
- FeatureMatrixT(const sparse_t & _features,
- const std::map<uint, uint> & _examples,
- const uint & _dim)
- {
- if (_dim < 0)
- set_d(_features.njc -1);
- else
- set_d(_dim);
- uint nMax(0);
- for ( uint i = 0; i < _features.njc-1; i++ )
- {
- for ( uint j = _features.jc[i]; j < _features.jc[i+1] && j < _features.ndata; j++ )
- {
- uint example_index = _features.ir[ j];
- std::map<uint, uint>::const_iterator it = _examples.find(example_index);
- if ( it != _examples.end() ) {
- this->features[i].insert(((T*)_features.data)[j], true , it->second );
- if (it->second > nMax)
- nMax = it->second;
- }
- }
- }
- for (typename std::vector<NICE::SortedVectorSparse<T> >::iterator it = this->features.begin(); it != this->features.end(); it++)
- (*it).setN(nMax+1);
- this->ui_n = nMax+1;
- this->b_verbose = false;
- }
- #endif
-
- template <typename T>
- FeatureMatrixT<T>::~FeatureMatrixT()
- {
- }
-
-
-
-
- template <typename T>
- uint FeatureMatrixT<T>::get_n() const
- {
- return this->ui_n;
- }
-
- template <typename T>
- uint FeatureMatrixT<T>::get_d() const
- {
- return this->ui_d;
- }
-
- template <typename T>
- void FeatureMatrixT<T>::set_d(const uint & _d)
- {
- this->ui_d = _d;
- this->features.resize( this->ui_d );
- }
- template <typename T>
- void FeatureMatrixT<T>::setVerbose( const bool & _verbose)
- {
- this->b_verbose = _verbose;
- }
- template <typename T>
- bool FeatureMatrixT<T>::getVerbose( ) const
- {
- return this->b_verbose;
- }
- template <typename T>
- void FeatureMatrixT<T>::setDebug( const bool & _debug)
- {
- this->b_debug = _debug;
- }
- template <typename T>
- bool FeatureMatrixT<T>::getDebug( ) const
- {
- return this->b_debug;
- }
-
- template <typename T>
- inline T FeatureMatrixT<T>::operator()(const uint _row,
- const uint _col
- ) const
- {
- if ( (_row < 0) || (_col < 0) || (_row > this->ui_d) || (_col > this->ui_n) )
- {
- fthrow(Exception, "FeatureMatrixT: out of bounds");
- }
- else
- return ( this->features[_row]).access(_col);
- }
- template<class T>
- inline bool
- FeatureMatrixT<T>::operator==(const FeatureMatrixT<T> & _F) const
- {
- if ( ( this->get_n() != _F.get_n()) || (this->get_d() != _F.get_d()) )
- {
- fthrow(Exception, "FeatureMatrixT<T>::operator== : (n != F.get_n()) || (d != F.get_d()) -- number of dimensions does not fit");
- }
- else if ((this->ui_n == 0) || (this->ui_d == 0))
- {
- return true;
- }
- for (uint i = 0; i < this->ui_d; i++)
- {
- for (uint j = 0; j < this->ui_n; j++)
- {
-
- if(!((*this)(i,j) == _F(i,j)))
- return false;
- }
- }
- return true;
- }
- template<class T>
- inline bool
- FeatureMatrixT<T>::operator!=(const FeatureMatrixT<T> & _F) const
- {
- if ( ( (*this).get_n() != _F.get_n()) ||
- ( (*this).get_d() != _F.get_d())
- )
- {
- fthrow(Exception, "FeatureMatrixT::operator!=(): (n != F.get_n()) || (d != F.get_d()) -- number of dimensions does not fit");
- }
- else if ((this->ui_n == 0) || (this->ui_d == 0))
- {
- return false;
- }
- for (uint i = 0; i < this->ui_d; i++)
- {
- for (uint j = 0; j < this->ui_n; j++)
- {
- if(!((*this)(i,j) == _F(i,j)))
- return true;
- }
- }
- return false;
- }
- template<typename T>
- inline FeatureMatrixT<T>&
- FeatureMatrixT<T>::operator=(const FeatureMatrixT<T> & _F)
- {
- this->set_d(_F.get_d());
- this->ui_n = _F.get_n();
- for (uint i = 0; i < (*this).get_d(); i++)
- {
-
- features[i] = _F[i];
- }
- return *this;
- }
-
- template <typename T>
- inline T FeatureMatrixT<T>::getUnsafe(const uint _row,
- const uint _col
- ) const
- {
- return (this->features[_row]).access(_col);
- }
-
- template <typename T>
- inline T FeatureMatrixT<T>::getOriginal(const uint _row,
- const uint _col
- ) const
- {
- return (this->features[_row]).accessOriginal(_col);
- }
-
- template <typename T>
- inline void FeatureMatrixT<T>::set (const uint _row,
- const uint _col,
- const T & _newElement,
- bool _setTransformedValue
- )
- {
- if ( (_row < 0) || (_col < 0) || (_row > this->ui_d) || (_col > this->ui_n) )
- {
- return;
- }
- else
- (this->features[_row]).set ( _col, _newElement, _setTransformedValue );
- }
-
- template <typename T>
- inline void FeatureMatrixT<T>::setUnsafe (const uint _row,
- const uint _col,
- const T & _newElement,
- bool _setTransformedValue
- )
- {
- (this->features[_row]).set ( _col, _newElement, _setTransformedValue );
- }
-
- template <typename T>
- void FeatureMatrixT<T>::getDimension(const uint & _dim,
- NICE::SortedVectorSparse<T> & _dimension
- ) const
- {
- if ( (_dim < 0) || (_dim > this->ui_d) )
- {
- return;
- }
- else
- _dimension = this->features[_dim];
- }
-
- template <typename T>
- void FeatureMatrixT<T>::getDimensionUnsafe(const uint & _dim,
- NICE::SortedVectorSparse<T> & _dimension
- ) const
- {
- _dimension = this->features[_dim];
- }
-
- template <typename T>
- void FeatureMatrixT<T>::findFirstInDimension(const uint & _dim,
- const T & _elem,
- uint & _position
- ) const
- {
- _position = 0;
- if ( _dim > this->ui_d )
- return;
- std::pair< typename SortedVectorSparse<T>::elementpointer, typename SortedVectorSparse<T>::elementpointer > eit;
- eit = this->features[_dim].nonzeroElements().equal_range ( _elem );
- _position = distance( this->features[_dim].nonzeroElements().begin(), eit.first );
- if ( _elem > this->features[_dim].getTolerance() )
- _position += this->features[_dim].getZeros();
- }
-
- template <typename T>
- void FeatureMatrixT<T>::findLastInDimension(const uint & _dim,
- const T & _elem,
- uint & _position
- ) const
- {
- _position = 0;
- if ( _dim > this->ui_d )
- return;
- std::pair< typename SortedVectorSparse<T>::const_elementpointer, typename SortedVectorSparse<T>::const_elementpointer > eit = this->features[_dim].nonzeroElements().equal_range ( _elem );
- _position = distance( this->features[_dim].nonzeroElements().begin(), eit.second );
- if ( _elem > this->features[_dim].getTolerance() )
- _position += this->features[_dim].getZeros();
- }
-
- template <typename T>
- void FeatureMatrixT<T>::findFirstLargerInDimension(const uint & _dim,
- const T & _elem,
- uint & _position
- ) const
- {
-
- uint nz = this->ui_n - this->features[_dim].getNonZeros();
-
- uint nnz = this->features[_dim].getNonZeros();
- _position = 0;
-
-
- typename SortedVectorSparse<T>::const_elementpointer it = this->features[_dim].nonzeroElements().end();
-
- it = this->features[_dim].nonzeroElements().upper_bound ( _elem );
- _position = distance( this->features[_dim].nonzeroElements().begin(), it );
-
- if ( _elem >= this->features[_dim].getTolerance() )
- {
- _position += nz;
- }
- }
-
- template <typename T>
- void FeatureMatrixT<T>::findLastSmallerInDimension(const uint & _dim,
- const T & _elem,
- uint & _position
- ) const
- {
- _position = 0;
- if ( (_dim < 0) || (_dim > this->ui_d))
- return;
- typename SortedVectorSparse<T>::const_elementpointer it = this->features[_dim].nonzeroElements().lower_bound ( _elem );
- _position = distance( this->features[_dim].nonzeroElements().begin(), it );
- if ( it->first > this->features[_dim].getTolerance() )
- _position += this->features[_dim].getZeros();
- }
- template <typename T>
- T FeatureMatrixT<T>::getLargestValue ( const bool & _getTransformedValue ) const
- {
- T vmax = (T) 0;
- T vtmp = (T) 0;
- uint tmp ( 0 );
- for ( typename std::vector<NICE::SortedVectorSparse<T> >::const_iterator it = this->features.begin();
- it != this->features.end();
- it++, tmp++
- )
- {
- vtmp = it->getLargestValueUnsafe( 1.0 , _getTransformedValue );
- if ( vtmp > vmax )
- {
- vmax = vtmp;
- }
- }
- return vmax;
- }
- template <typename T>
- NICE::VectorT<T> FeatureMatrixT<T>::getLargestValuePerDimension ( const double & _quantile,
- const bool & _getTransformedValue
- ) const
- {
- NICE::VectorT<T> vmax ( this->get_d() );
- uint tmp ( 0 );
- typename NICE::VectorT<T>::iterator vmaxIt = vmax.begin();
- for ( typename std::vector<NICE::SortedVectorSparse<T> >::const_iterator it = this->features.begin();
- it != this->features.end();
- it++, vmaxIt++, tmp++
- )
- {
- *vmaxIt = it->getLargestValueUnsafe( _quantile, _getTransformedValue );
- }
- return vmax;
- }
-
-
-
- template <typename T>
- void FeatureMatrixT<T>::applyFunctionToFeatureMatrix ( const NICE::ParameterizedFunction *_pf )
- {
- if (_pf != NULL)
- {
-
- if ( !_pf->isOrderPreserving() )
- fthrow(Exception, "ParameterizedFunction::applyFunctionToFeatureMatrix: this function is optimized for order preserving transformations");
- uint d = this->get_d();
- for (uint dim = 0; dim < d; dim++)
- {
- std::multimap< double, typename SortedVectorSparse<double>::dataelement> & nonzeroElements = this->getFeatureValues(dim).nonzeroElements();
- for ( SortedVectorSparse<double>::elementpointer i = nonzeroElements.begin(); i != nonzeroElements.end(); i++ )
- {
- SortedVectorSparse<double>::dataelement & de = i->second;
-
- de.second = _pf->f( dim, i->first );
- }
- }
-
- }
- else
- {
-
- }
- }
-
- template <typename T>
- double FeatureMatrixT<T>:: computeSparsityRatio() const
- {
- double ratio(0.0);
- for (typename std::vector<NICE::SortedVectorSparse<T> >::const_iterator it = this->features.begin(); it != this->features.end(); it++)
- {
- ratio += (*it).getZeros() / (double) (*it).getN();
- }
- if (this->features.size() != 0)
- ratio /= double(this->features.size());
- return ratio;
- }
-
- template <typename T>
- void FeatureMatrixT<T>::add_feature( const std::vector<T> & _feature,
- const NICE::ParameterizedFunction *_pf
- )
- {
- if (this->ui_n == 0)
- {
- this->set_d( _feature.size() );
- }
- if ( _feature.size() != this->ui_d)
- {
- fthrow(Exception, "FeatureMatrixT<T>::add_feature - number of dimensions does not fit");
- return;
- }
- for (uint dimension = 0; dimension < this->features.size(); dimension++)
- {
- if (_pf != NULL)
- this->features[dimension].insert( _feature[dimension], _pf->f( dimension, _feature[dimension]) );
- else
- this->features[dimension].insert( _feature[dimension] );
- }
- this->ui_n++;
- }
-
- template <typename T>
- void FeatureMatrixT<T>::add_feature(const NICE::SparseVector & _feature,
- const ParameterizedFunction *_pf
- )
- {
- if (this->ui_n == 0)
- {
- this->set_d( _feature.size() );
- }
- if ( _feature.getDim() > this->ui_d)
- {
- fthrow(Exception, "FeatureMatrixT<T>::add_feature - number of dimensions does not fit");
- return;
- }
- for (NICE::SparseVector::const_iterator it = _feature.begin(); it != _feature.end(); it++)
- {
- if (_pf != NULL)
- this->features[it->first].insert( (T) it->second, _pf->f( it->first, (T) it->second), true , this->ui_n );
- else
- this->features[it->first].insert( (T) it->second, true , this->ui_n );
- }
- this->ui_n++;
- }
-
- template <typename T>
- void FeatureMatrixT<T>::add_features(const std::vector<std::vector<T> > & _features )
- {
-
-
-
- if (this->ui_n == 0)
- {
- this->set_d(_features.size());
- }
-
- for (uint dim = 0; dim < this->ui_d; dim++)
- {
- this->features[dim].insert( _features[dim] );
- }
-
- this->ui_n += _features[0].size();
- }
- template <typename T>
- void FeatureMatrixT<T>::set_features(const std::vector<std::vector<T> > & _features,
- std::vector<std::vector<uint> > & _permutations,
- const uint & _dim
- )
- {
- this->features.clear();
- this->set_d( std::max ( _dim, (const uint) _features.size() ) );
- if ( this->ui_d > 0 )
- this->ui_n = _features[0].size();
-
- for (uint dim = 0; dim < this->ui_d; dim++)
- {
- this->features[dim].insert( _features[dim] );
- }
- this->getPermutations( _permutations );
- }
- template <typename T>
- void FeatureMatrixT<T>::set_features(const std::vector<std::vector<T> > & _features,
- std::vector<std::map<uint,uint> > & _permutations,
- const uint & _dim
- )
- {
- this->features.clear();
- this->set_d( std::max ( _dim, _features.size() ) );
- if ( this->ui_d > 0 )
- this->ui_n = _features[0].size();
-
- for (uint dim = 0; dim < this->ui_d; dim++)
- {
- this->features[dim].insert( _features[dim] );
- }
- this->getPermutations( _permutations );
- }
- template <typename T>
- void FeatureMatrixT<T>::set_features(const std::vector<std::vector<T> > & _features,
- const uint & _dim
- )
- {
- this->features.clear();
- this->set_d( std::max ( _dim, (const uint) _features.size() ) );
- if ( this->ui_d > 0 )
- this->ui_n = _features[0].size();
-
- for (uint dim = 0; dim < this->ui_d; dim++)
- {
- if ( this->b_debug )
- {
- std::cerr << " dim: " << dim << " add " << _features[dim].size() << " examples " << std::endl;
- }
- this->features[dim].insert( _features[dim] );
- }
- }
- template <typename T>
- void FeatureMatrixT<T>::set_features(const std::vector< const NICE::SparseVector * > & _features,
- const bool _dimensionsOverExamples,
- const uint & _dim
- )
- {
- this->features.clear();
- if (_features.size() == 0)
- {
- std::cerr << "set_features without features" << std::endl;
- }
-
-
- uint dimTmp ( _dim );
- if (_dimensionsOverExamples)
- {
- if ( _features.size() > dimTmp )
- {
- dimTmp = _features.size();
- }
- }
- else
- {
- if (_features.size() > 0)
- {
- try{
- if ( _features[0]->getDim() > dimTmp )
- {
- dimTmp = _features[0]->getDim();
- }
- }
- catch(...)
- {
- std::cerr << "FeatureMatrixT<T>::set_features -- something went wrong using getDim() of SparseVectors" << std::endl;
- }
- }
- }
- this->set_d( dimTmp );
-
- if ( this->ui_d > 0 )
- {
- if ( _dimensionsOverExamples )
- this->ui_n = _features[0]->getDim();
- else
- this->ui_n = _features.size();
- }
-
- if ( _dimensionsOverExamples )
- {
- for (uint dim = 0; dim < this->ui_d; dim++)
- {
- this->features[dim].insert( _features[dim] );
- }
- }
- else
- {
- if ( this->b_debug )
- std::cerr << "FeatureMatrixT<T>::set_features " << this->ui_n << " new examples" << std::endl;
-
- for (uint nr = 0; nr < this->ui_n; nr++)
- {
- if ( this->b_debug )
- std::cerr << "add feature nr. " << nr << " / " << _features.size() << " ";
-
- for (NICE::SparseVector::const_iterator elemIt = _features[nr]->begin(); elemIt != _features[nr]->end(); elemIt++)
- {
- if ( this->b_debug )
- std::cerr << elemIt->first << "-" << elemIt->second << " ";
-
- this->features[elemIt->first].insert( (T) elemIt->second, true , nr);
- }
- if ( this->b_debug )
- std::cerr << std::endl;
- }
- if ( this->b_debug )
- std::cerr << "FeatureMatrixT<T>::set_features done" << std::endl;
- }
-
- for (typename std::vector<NICE::SortedVectorSparse<T> >::iterator it = this->features.begin(); it != this->features.end(); it++)
- (*it).setN( this->ui_n );
- }
- template <typename T>
- void FeatureMatrixT<T>::getPermutations( std::vector<std::vector<uint> > & _permutations) const
- {
- for (uint dim = 0; dim < this->ui_d; dim++)
- {
- std::vector<uint> perm ( (this->features[dim]).getPermutation() );
- _permutations.push_back(perm);
- }
- }
- template <typename T>
- void FeatureMatrixT<T>::getPermutations( std::vector<std::map<uint,uint> > & _permutations) const
- {
- for (uint dim = 0; dim < this->ui_d; dim++)
- {
- std::map<uint,uint> perm ( (this->features[dim]).getPermutationNonZeroReal() );
- _permutations.push_back(perm);
- }
- }
-
- template <typename T>
- void FeatureMatrixT<T>::print(std::ostream & _os) const
- {
- if (_os.good())
- {
- for (uint dim = 0; dim < this->ui_d; dim++)
- {
- this->features[dim].print(_os);
- }
- }
- }
-
- template <typename T>
- void FeatureMatrixT<T>::computeNonSparseMatrix(NICE::MatrixT<T> & _matrix,
- bool _transpose
- ) const
- {
- if ( _transpose )
- _matrix.resize(this->get_n(),this->get_d());
- else
- _matrix.resize(this->get_d(),this->get_n());
- _matrix.set((T)0.0);
- uint dimIdx(0);
- for (typename std::vector<NICE::SortedVectorSparse<T> >::const_iterator it = this->features.begin(); it != this->features.end(); it++, dimIdx++)
- {
- std::map< uint, typename NICE::SortedVectorSparse<T>::elementpointer> nonzeroIndices= (*it).nonzeroIndices();
- for (typename std::map< uint, typename NICE::SortedVectorSparse<T>::elementpointer>::const_iterator inIt = nonzeroIndices.begin(); inIt != nonzeroIndices.end(); inIt++)
- {
- uint featIndex = ((*inIt).second)->second.first;
- if ( _transpose )
- _matrix(featIndex,dimIdx) =((*inIt).second)->second.second;
- else
- _matrix(dimIdx,featIndex) =((*inIt).second)->second.second;
- }
- }
- }
-
- template <typename T>
- void FeatureMatrixT<T>::computeNonSparseMatrix(std::vector<std::vector<T> > & _matrix,
- bool _transpose
- ) const
- {
- if ( _transpose )
- _matrix.resize(this->get_n());
- else
- _matrix.resize(this->get_d());
-
- for ( uint i = 0 ; i < _matrix.size(); i++ )
- if ( _transpose )
- _matrix[i] = std::vector<T>(this->get_d(), 0.0);
- else
- _matrix[i] = std::vector<T>(this->get_n(), 0.0);
- uint dimIdx(0);
- for (typename std::vector<NICE::SortedVectorSparse<T> >::const_iterator it = this->features.begin(); it != this->features.end(); it++, dimIdx++)
- {
- std::map< uint, typename NICE::SortedVectorSparse<T>::elementpointer> nonzeroIndices= (*it).nonzeroIndices();
- for (typename std::map< uint, typename NICE::SortedVectorSparse<T>::elementpointer>::const_iterator inIt = nonzeroIndices.begin(); inIt != nonzeroIndices.end(); inIt++)
- {
- uint featIndex = ((*inIt).second)->second.first;
- if ( _transpose )
- _matrix[featIndex][dimIdx] =((*inIt).second)->second.second;
- else
- _matrix[dimIdx][featIndex] =((*inIt).second)->second.second;
- }
- }
- }
-
- template <typename T>
- void FeatureMatrixT<T>::swap(const uint & _row1,
- const uint & _col1,
- const uint & _row2,
- const uint & _col2
- )
- {
- if ( (_row1 < 0) || (_col1 < 0) || (_row1 > this->ui_d) || (_col1 > this->ui_n) ||
- (_row2 < 0) || (_col2 < 0) || (_row2 > this->ui_d) || (_col2 > this->ui_n)
- )
- {
- return;
- }
- else
- {
-
- T tmp = (*this)(_row1, _col1);
- (*this).set(_row1, _col1, (*this)(_row2,_col2));
- (*this).set(_row2, _col2, tmp);
- }
- }
-
- template <typename T>
- void FeatureMatrixT<T>::swapUnsafe(const uint & _row1,
- const uint & _col1,
- const uint & _row2,
- const uint & _col2
- )
- {
-
- T tmp = (*this)(_row1, _col1);
- (*this).set(_row1, _col1, (*this)(_row2,_col2));
- (*this).set(_row2, _col2, tmp);
- }
- template <typename T>
- void FeatureMatrixT<T>::hikDiagonalElements( Vector & _diagonalElements ) const
- {
- uint dimIdx = 0;
-
- _diagonalElements.resize(this->ui_n);
- _diagonalElements.set(0.0);
-
- for (typename std::vector<NICE::SortedVectorSparse<T> >::const_iterator it = this->features.begin(); it != this->features.end(); it++, dimIdx++)
- {
- std::map< uint, typename NICE::SortedVectorSparse<T>::elementpointer> nonzeroIndices= (*it).nonzeroIndices();
-
- for (typename std::map< uint, typename NICE::SortedVectorSparse<T>::elementpointer>::const_iterator inIt = nonzeroIndices.begin(); inIt != nonzeroIndices.end(); inIt++)
- {
- uint index = inIt->first;
- typename NICE::SortedVectorSparse<T>::elementpointer p = inIt->second;
- typename NICE::SortedVectorSparse<T>::dataelement de = p->second;
- _diagonalElements[index] += de.second;
- }
- }
- }
- template <typename T>
- double FeatureMatrixT<T>::hikTrace() const
- {
- Vector diagonalElements;
- this->hikDiagonalElements( diagonalElements );
- return diagonalElements.Sum();
- }
- template <typename T>
- uint FeatureMatrixT<T>::getNumberOfNonZeroElementsPerDimension(const uint & dim) const
- {
-
- if ( (dim < 0) || (dim > this->ui_d))
- return 0;
- return this->features[dim].getNonZeros();
- }
- template <typename T>
- uint FeatureMatrixT<T>::getNumberOfZeroElementsPerDimension(const uint & dim) const
- {
- if ( (dim < 0) || (dim > this->ui_d))
- return 0;
- return this->ui_n - this-> features[dim].getNonZeros();
- }
- template <typename T>
- void FeatureMatrixT<T>::restore ( std::istream & _is,
- int _format
- )
- {
- bool b_restoreVerbose ( false );
- if ( _is.good() )
- {
- if ( b_restoreVerbose )
- std::cerr << " restore FeatureMatrixT" << std::endl;
- std::string tmp;
- _is >> tmp;
- if ( ! this->isStartTag( tmp, "FeatureMatrixT" ) )
- {
- std::cerr << " WARNING - attempt to restore FeatureMatrixT, but start flag " << tmp << " does not match! Aborting... " << std::endl;
- throw;
- }
- _is.precision ( std::numeric_limits<double>::digits10 + 1);
- bool b_endOfBlock ( false ) ;
- while ( !b_endOfBlock )
- {
- _is >> tmp;
- if ( this->isEndTag( tmp, "FeatureMatrixT" ) )
- {
- b_endOfBlock = true;
- continue;
- }
- tmp = this->removeStartTag ( tmp );
- if ( b_restoreVerbose )
- std::cerr << " currently restore section " << tmp << " in FeatureMatrixT" << std::endl;
- if ( tmp.compare("ui_n") == 0 )
- {
- _is >> this->ui_n;
- _is >> tmp;
- tmp = this->removeEndTag ( tmp );
- }
- else if ( tmp.compare("ui_d") == 0 )
- {
- _is >> this->ui_d;
- _is >> tmp;
- tmp = this->removeEndTag ( tmp );
- }
- else if ( tmp.compare("features") == 0 )
- {
-
- this->features.resize( this->ui_d);
-
- for (uint dim = 0; dim < this->ui_d; dim++)
- {
- NICE::SortedVectorSparse<T> svs;
- this->features[dim] = svs;
- this->features[dim].restore(_is, _format);
- }
- _is >> tmp;
- tmp = this->removeEndTag ( tmp );
- }
- else
- {
- std::cerr << "WARNING -- unexpected FeatureMatrixT object -- " << tmp << " -- for restoration... aborting" << std::endl;
- throw;
- }
- }
- }
- else
- {
- std::cerr << "FeatureMatrixT<T>::restore -- InStream not initialized - restoring not possible!" << std::endl;
- throw;
- }
- }
- template <typename T>
- void FeatureMatrixT<T>::store ( std::ostream & _os,
- int _format
- ) const
- {
- if (_os.good())
- {
-
- _os << this->createStartTag( "FeatureMatrixT" ) << std::endl;
- _os.precision (std::numeric_limits<double>::digits10 + 1);
- _os << this->createStartTag( "ui_n" ) << std::endl;
- _os << this->ui_n << std::endl;
- _os << this->createEndTag( "ui_n" ) << std::endl;
- _os << this->createStartTag( "ui_d" ) << std::endl;
- _os << this->ui_d << std::endl;
- _os << this->createEndTag( "ui_d" ) << std::endl;
-
- _os << this->createStartTag( "features" ) << std::endl;
- for (uint dim = 0; dim < this->ui_d; dim++)
- {
- this->features[dim].store(_os,_format);
- }
- _os << this->createEndTag( "features" ) << std::endl;
-
- _os << this->createEndTag( "FeatureMatrixT" ) << std::endl;
- }
- else
- {
- std::cerr << "FeatureMatrixT<T>::store -- OutStream not initialized - storing not possible!" << std::endl;
- }
- }
- template <typename T>
- void FeatureMatrixT<T>::clear ()
- {}
- }
|