77 m_subset_stack->remove_all_subsets();
78 free_feature_matrix();
84 m_subset_stack->remove_all_subsets();
93 int32_t real_num=m_subset_stack->subset_idx_conversion(num);
97 if (feature_matrix.matrix)
100 return &feature_matrix.matrix[real_num * int64_t(num_features)];
108 feat = feature_cache->lock_entry(real_num);
113 feat = feature_cache->set_entry(real_num);
118 feat = compute_feature_vector(num, len, feat);
120 if (get_num_preprocessors())
122 int32_t tmp_len = len;
123 ST* tmp_feat_before = feat;
124 ST* tmp_feat_after = NULL;
126 for (int32_t i = 0; i < get_num_preprocessors(); i++)
133 tmp_feat_after = applied.
vector;
137 SG_FREE(tmp_feat_before);
138 tmp_feat_before = tmp_feat_after;
144 memcpy(feat, tmp_feat_after,
sizeof(ST) * tmp_len);
145 SG_FREE(tmp_feat_after);
156 int32_t real_num=m_subset_stack->subset_idx_conversion(num);
158 if (num>=get_num_vectors())
160 SG_ERROR(
"Index out of bounds (number of vectors %d, you "
161 "requested %d)\n", get_num_vectors(), num);
164 if (!feature_matrix.matrix)
165 SG_ERROR(
"Requires a in-memory feature matrix\n")
167 if (vector.
vlen != num_features)
169 "Vector not of length %d (has %d)\n", num_features, vector.
vlen);
171 memcpy(&feature_matrix.matrix[real_num * int64_t(num_features)], vector.
vector,
172 int64_t(num_features) *
sizeof(ST));
178 int32_t real_num=m_subset_stack->subset_idx_conversion(num);
180 if (num >= get_num_vectors())
182 SG_ERROR(
"Index out of bounds (number of vectors %d, you "
183 "requested %d)\n", get_num_vectors(), real_num);
188 ST* vector= get_feature_vector(num, vlen, do_free);
195 feature_cache->unlock_entry(m_subset_stack->subset_idx_conversion(num));
203 free_feature_vector(vec.
vector, num,
false);
209 if (m_subset_stack->has_subsets())
210 SG_ERROR(
"A subset is set, cannot call vector_subset\n")
212 ASSERT(feature_matrix.matrix)
213 ASSERT(idx_len<=num_vectors)
215 int32_t num_vec = num_vectors;
216 num_vectors = idx_len;
220 for (int32_t i = 0; i < idx_len; i++)
225 if (ii < 0 || ii >= num_vec)
226 SG_ERROR(
"Index out of range: should be 0<%d<%d\n", ii, num_vec)
231 memcpy(&feature_matrix.matrix[int64_t(num_features) * i],
232 &feature_matrix.matrix[int64_t(num_features) * ii],
233 num_features *
sizeof(ST));
240 if (m_subset_stack->has_subsets())
241 SG_ERROR(
"A subset is set, cannot call feature_subset\n")
243 ASSERT(feature_matrix.matrix)
244 ASSERT(idx_len<=num_features)
245 int32_t num_feat = num_features;
246 num_features = idx_len;
248 for (int32_t i = 0; i < num_vectors; i++)
250 ST* src = &feature_matrix.matrix[int64_t(num_feat) * i];
251 ST* dst = &feature_matrix.matrix[int64_t(num_features) * i];
254 for (int32_t j = 0; j < idx_len; j++)
258 if (jj < 0 || jj >= num_feat)
260 "Index out of range: should be 0<%d<%d\n", jj, num_feat);
270 if (!m_subset_stack->has_subsets())
271 return feature_matrix;
273 SGMatrix<ST> submatrix(num_features, get_num_vectors());
276 for (int32_t i=0; i<submatrix.
num_cols; ++i)
278 int32_t real_i = m_subset_stack->subset_idx_conversion(i);
279 memcpy(&submatrix.
matrix[i*int64_t(num_features)],
280 &feature_matrix.matrix[real_i * int64_t(num_features)],
281 num_features *
sizeof(ST));
290 m_subset_stack->remove_all_subsets();
292 clean_preprocessors();
293 free_feature_matrix();
294 return st_feature_matrix;
299 m_subset_stack->remove_all_subsets();
300 free_feature_matrix();
301 feature_matrix = matrix;
308 num_feat = num_features;
309 num_vec = num_vectors;
310 return feature_matrix.
matrix;
317 ST* fm = get_transposed(num_feat, num_vec);
324 num_feat = get_num_vectors();
325 num_vec = num_features;
327 int32_t old_num_vec=get_num_vectors();
329 ST* fm = SG_MALLOC(ST, int64_t(num_feat) * num_vec);
331 for (int32_t i=0; i<old_num_vec; i++)
335 for (int32_t j=0; j<vec.
vlen; j++)
336 fm[j*int64_t(old_num_vec)+i]=vec.
vector[j];
338 free_feature_vector(vec, i);
346 if (m_subset_stack->has_subsets())
347 SG_ERROR(
"A subset is set, cannot call copy_feature_matrix\n")
349 free_feature_matrix();
350 feature_matrix = src.
clone();
358 m_subset_stack->remove_all_subsets();
363 ASSERT(num_feat>0 && num_vec>0)
365 free_feature_matrix();
368 for (int32_t i = 0; i < num_vec; i++)
373 for (int32_t j = 0; j < num_feat; j++)
374 feature_matrix.matrix[i * int64_t(num_feat) + j] = (ST) v.
vector[j];
376 num_features = num_feat;
377 num_vectors = num_vec;
382 if (m_subset_stack->has_subsets())
383 SG_ERROR(
"A subset is set, cannot call apply_preproc\n")
385 SG_DEBUG(
"force: %d\n", force_preprocessing)
387 if (feature_matrix.matrix && get_num_preprocessors())
389 for (int32_t i = 0; i < get_num_preprocessors(); i++)
391 if ((!is_preprocessed(i) || force_preprocessing))
412 if (!feature_matrix.matrix)
415 if (!get_num_preprocessors())
416 SG_ERROR(
"no preprocessors available\n")
424 return m_subset_stack->has_subsets() ? m_subset_stack->get_size() : num_vectors;
437 if (m_subset_stack->has_subsets())
438 SG_ERROR(
"A subset is set, cannot call set_num_vectors\n")
446 if (m_subset_stack->has_subsets())
447 SG_ERROR(
"A subset is set, cannot call initialize_cache\n")
449 if (num_features && num_vectors)
452 feature_cache =
new CCache<ST>(get_cache_size(), num_features,
462 if (m_subset_stack->has_subsets())
463 SG_ERROR(
"A subset is set, cannot call reshape\n")
465 if (p_num_features * p_num_vectors
466 == this->num_features * this->num_vectors)
468 num_features = p_num_features;
469 num_vectors = p_num_vectors;
488 ST* vec1 = get_feature_vector(vec_idx1, len1, free1);
489 ST* vec2 = sf->get_feature_vector(vec_idx2, len2, free2);
493 free_feature_vector(vec1, vec_idx1, free1);
494 sf->free_feature_vector(vec2, vec_idx2, free2);
500 float64_t* vec2, int32_t vec2_len,
bool abs_val)
502 ASSERT(vec2_len == num_features)
506 ST* vec1 = get_feature_vector(vec_idx1, vlen, vfree);
508 ASSERT(vlen == num_features)
512 for (int32_t i = 0; i < num_features; i++)
517 for (int32_t i = 0; i < num_features; i++)
518 vec2[i] += alpha * vec1[i];
521 free_feature_vector(vec1, vec_idx1, vfree);
526 float64_t* vec2, int32_t vec2_len,
bool abs_val)
528 ASSERT(vec2_len == num_features)
532 float64_t* vec1 = get_feature_vector(vec_idx1, vlen, vfree);
534 ASSERT(vlen == num_features)
538 for (int32_t i = 0; i < num_features; i++)
539 vec2[i] += alpha * CMath::abs(vec1[i]);
546 free_feature_vector(vec1, vec_idx1, vfree);
556 if (vector_index>=get_num_vectors())
558 SG_ERROR(
"Index out of bounds (number of vectors %d, you "
559 "requested %d)\n", get_num_vectors(), vector_index);
562 dense_feature_iterator* iterator = SG_MALLOC(dense_feature_iterator, 1);
563 iterator->vec = get_feature_vector(vector_index, iterator->vlen,
565 iterator->vidx = vector_index;
573 dense_feature_iterator* it = (dense_feature_iterator*) iterator;
574 if (!it || it->index >= it->vlen)
588 dense_feature_iterator* it = (dense_feature_iterator*) iterator;
589 free_feature_vector(it->vec, it->vidx, it->vfree);
599 index_t real_idx=m_subset_stack->subset_idx_conversion(indices.
vector[i]);
600 memcpy(&feature_matrix_copy.
matrix[i*num_features],
601 &feature_matrix.matrix[real_idx*num_features],
602 num_features*
sizeof(ST));
618 REQUIRE(max<num_features && min>=0,
619 "Provided dimensions is in the range [%d, %d] but they "
620 "have to be within [0, %d]! But it \n", min, max, num_features);
626 for (
index_t j=0; j<get_num_vectors(); ++j)
628 index_t real_idx=m_subset_stack->subset_idx_conversion(j);
629 feature_matrix_copy(i, j)=feature_matrix(dims[i], real_idx);
645 SG_SDEBUG(
"Using underlying feature matrix with %d dimensions and %d feature vectors!\n", num_features, num_vectors);
648 SG_REF(shallow_copy_features);
649 if (m_subset_stack->has_subsets())
650 shallow_copy_features->
add_subset(m_subset_stack->get_last_subset()->get_subset_idx());
652 return shallow_copy_features;
669 feature_cache = NULL;
676 SG_ADD(&feature_matrix,
"feature_matrix",
680 #define GET_FEATURE_TYPE(f_type, sg_type) \
681 template<> EFeatureType CDenseFeatures<sg_type>::get_feature_type() const \
699 #undef GET_FEATURE_TYPE
704 ASSERT(vec2_len == num_features)
708 bool* vec1 = get_feature_vector(vec_idx1, vlen, vfree);
710 ASSERT(vlen == num_features)
713 for (int32_t i = 0; i < num_features; i++)
714 result += vec1[i] ? vec2[i] : 0;
716 free_feature_vector(vec1, vec_idx1, vfree);
724 ASSERT(vec2_len == num_features)
728 char* vec1 = get_feature_vector(vec_idx1, vlen, vfree);
730 ASSERT(vlen == num_features)
733 for (int32_t i = 0; i < num_features; i++)
734 result += vec1[i] * vec2[i];
736 free_feature_vector(vec1, vec_idx1, vfree);
744 ASSERT(vec2_len == num_features)
748 int8_t* vec1 = get_feature_vector(vec_idx1, vlen, vfree);
750 ASSERT(vlen == num_features)
753 for (int32_t i = 0; i < num_features; i++)
754 result += vec1[i] * vec2[i];
756 free_feature_vector(vec1, vec_idx1, vfree);
762 int32_t vec_idx1,
const float64_t* vec2, int32_t vec2_len)
764 ASSERT(vec2_len == num_features)
768 uint8_t* vec1 = get_feature_vector(vec_idx1, vlen, vfree);
770 ASSERT(vlen == num_features)
773 for (int32_t i = 0; i < num_features; i++)
774 result += vec1[i] * vec2[i];
776 free_feature_vector(vec1, vec_idx1, vfree);
782 int32_t vec_idx1,
const float64_t* vec2, int32_t vec2_len)
784 ASSERT(vec2_len == num_features)
788 int16_t* vec1 = get_feature_vector(vec_idx1, vlen, vfree);
790 ASSERT(vlen == num_features)
793 for (int32_t i = 0; i < num_features; i++)
794 result += vec1[i] * vec2[i];
796 free_feature_vector(vec1, vec_idx1, vfree);
802 int32_t vec_idx1,
const float64_t* vec2, int32_t vec2_len)
804 ASSERT(vec2_len == num_features)
808 uint16_t* vec1 = get_feature_vector(vec_idx1, vlen, vfree);
810 ASSERT(vlen == num_features)
813 for (int32_t i = 0; i < num_features; i++)
814 result += vec1[i] * vec2[i];
816 free_feature_vector(vec1, vec_idx1, vfree);
822 int32_t vec_idx1,
const float64_t* vec2, int32_t vec2_len)
824 ASSERT(vec2_len == num_features)
828 int32_t* vec1 = get_feature_vector(vec_idx1, vlen, vfree);
830 ASSERT(vlen == num_features)
833 for (int32_t i = 0; i < num_features; i++)
834 result += vec1[i] * vec2[i];
836 free_feature_vector(vec1, vec_idx1, vfree);
842 int32_t vec_idx1,
const float64_t* vec2, int32_t vec2_len)
844 ASSERT(vec2_len == num_features)
848 uint32_t* vec1 = get_feature_vector(vec_idx1, vlen, vfree);
850 ASSERT(vlen == num_features)
853 for (int32_t i = 0; i < num_features; i++)
854 result += vec1[i] * vec2[i];
856 free_feature_vector(vec1, vec_idx1, vfree);
862 int32_t vec_idx1,
const float64_t* vec2, int32_t vec2_len)
864 ASSERT(vec2_len == num_features)
868 int64_t* vec1 = get_feature_vector(vec_idx1, vlen, vfree);
870 ASSERT(vlen == num_features)
873 for (int32_t i = 0; i < num_features; i++)
874 result += vec1[i] * vec2[i];
876 free_feature_vector(vec1, vec_idx1, vfree);
882 int32_t vec_idx1,
const float64_t* vec2, int32_t vec2_len)
884 ASSERT(vec2_len == num_features)
888 uint64_t* vec1 = get_feature_vector(vec_idx1, vlen, vfree);
890 ASSERT(vlen == num_features)
893 for (int32_t i = 0; i < num_features; i++)
894 result += vec1[i] * vec2[i];
896 free_feature_vector(vec1, vec_idx1, vfree);
902 int32_t vec_idx1,
const float64_t* vec2, int32_t vec2_len)
904 ASSERT(vec2_len == num_features)
908 float32_t* vec1 = get_feature_vector(vec_idx1, vlen, vfree);
910 ASSERT(vlen == num_features)
913 for (int32_t i = 0; i < num_features; i++)
914 result += vec1[i] * vec2[i];
916 free_feature_vector(vec1, vec_idx1, vfree);
922 int32_t vec_idx1,
const float64_t* vec2, int32_t vec2_len)
924 ASSERT(vec2_len == num_features)
928 float64_t* vec1 = get_feature_vector(vec_idx1, vlen, vfree);
930 ASSERT(vlen == num_features)
933 free_feature_vector(vec1, vec_idx1, vfree);
939 int32_t vec_idx1,
const float64_t* vec2, int32_t vec2_len)
941 ASSERT(vec2_len == num_features)
945 floatmax_t* vec1 = get_feature_vector(vec_idx1, vlen, vfree);
947 ASSERT(vlen == num_features)
950 for (int32_t i = 0; i < num_features; i++)
951 result += vec1[i] * vec2[i];
953 free_feature_vector(vec1, vec_idx1, vfree);
965 int32_t v1len, v2len;
966 bool v1free, v2free, stop =
false;
968 for (int32_t i = 0; i < num_vectors; i++)
970 vec1 = get_feature_vector(i, v1len, v1free);
976 for (int32_t j=0; j<v1len; j++)
978 if (vec1[j]!=vec2[j])
982 free_feature_vector(vec1, i, v1free);
983 free_feature_vector(vec2, i, v2free);
995 SG_DEBUG(
"entering %s::create_merged_copy()\n", get_name());
1002 index_t num_vectors_merged=num_vectors;
1009 SG_ERROR(
"%s::create_merged_copy(): Could not cast object of %s to "
1010 "same type as %s\n",get_name(), other->
get_name(), get_name());
1015 strcmp(get_name(), casted->
get_name()))
1017 SG_ERROR(
"%s::create_merged_copy(): Features are of different type!\n",
1023 SG_ERROR(
"%s::create_merged_copy(): Provided feature object has "
1024 "different dimension than this one\n");
1039 SG_DEBUG(
"copying matrix of this instance\n")
1040 memcpy(data.
matrix, feature_matrix.matrix,
1041 num_features*num_vectors*
sizeof(ST));
1044 index_t num_processed=num_vectors;
1053 SG_DEBUG(
"copying matrix of provided instance\n")
1054 memcpy(&(data.
matrix[num_processed*num_features]),
1070 SG_DEBUG(
"leaving %s::create_merged_copy()\n", get_name());
1077 SG_DEBUG(
"entering %s::create_merged_copy()\n", get_name());
1082 CFeatures* result=create_merged_copy(list);
1085 SG_DEBUG(
"leaving %s::create_merged_copy()\n", get_name());
1093 matrix.
load(loader);
1094 set_feature_matrix(matrix);
1100 feature_matrix.
save(writer);
1106 "base_features must be of dynamic type CDenseFeatures\n")
virtual const char * get_name() const =0
CSubsetStack * m_subset_stack
void set_feature_vector(SGVector< ST > vector, int32_t num)
virtual int32_t get_dim_feature_space() const
CSGObject * get_next_element()
The class DenseFeatures implements dense feature matrices.
virtual void load(CFile *loader)
virtual void copy_feature_matrix(SGMatrix< ST > src)
ST * get_feature_vector(int32_t num, int32_t &len, bool &dofree)
int32_t get_num_features() const
CDenseFeatures(int32_t size=0)
Vector::Scalar dot(Vector a, Vector b)
virtual CFeatures * duplicate() const
SGMatrix< ST > get_feature_matrix()
void set_feature_matrix(SGMatrix< ST > matrix)
virtual int32_t get_num_vectors() const =0
#define SG_NOTIMPLEMENTED
void vector_subset(int32_t *idx, int32_t idx_len)
Features that support dot products among other operations.
int32_t num_features
number of features in cache
EFeatureClass
shogun feature class
class to add subset support to another class. A CSubsetStackStack instance should be added and wrappe...
virtual int32_t get_dim_feature_space() const =0
virtual void save(CFile *saver)
CSGObject * get_first_element()
Class SGObject is the base class of all shogun objects.
virtual int32_t get_num_vectors() const
Template class DensePreprocessor, base class for preprocessors (cf. CPreprocessor) that apply to CDen...
void set_num_vectors(int32_t num)
virtual float64_t dot(int32_t vec_idx1, CDotFeatures *df, int32_t vec_idx2)
A File access base class.
CDenseFeatures< ST > * get_transposed()
virtual EFeatureClass get_feature_class() const =0
void set_num_features(int32_t num)
SGMatrix< ST > feature_matrix
static float64_t dot(const bool *v1, const bool *v2, int32_t n)
Compute dot product between v1 and v2 (blas optimized)
void feature_subset(int32_t *idx, int32_t idx_len)
all of classes and functions are contained in the shogun namespace
void free_feature_vector(ST *feat_vec, int32_t num, bool dofree)
virtual bool reshape(int32_t p_num_features, int32_t p_num_vectors)
virtual EFeatureClass get_feature_class() const
The class Features is the base class of all feature objects.
virtual ~CDenseFeatures()
virtual SGMatrix< ST > apply_to_feature_matrix(CFeatures *features)=0
bool append_element(CSGObject *data)
void obtain_from_dot(CDotFeatures *df)
int32_t num_vectors
number of vectors in cache
SGVector< float64_t > get_computed_dot_feature_vector(int32_t num)
virtual EFeatureType get_feature_type() const
virtual bool apply_preprocessor(bool force_preprocessing=false)
Matrix::Scalar max(Matrix m)
void free_feature_matrix()
SGMatrix< ST > steal_feature_matrix()
virtual SGVector< ST > apply_to_feature_vector(SGVector< ST > vector)=0
virtual const char * get_name() const
virtual void add_to_dense_vec(float64_t alpha, int32_t vec_idx1, float64_t *vec2, int32_t vec2_len, bool abs_val=false)
virtual void add_subset(SGVector< index_t > subset)
virtual EFeatureType get_feature_type() const =0
Class List implements a doubly connected list for low-level-objects.
#define GET_FEATURE_TYPE(f_type, sg_type)