191 REQUIRE(idx_a >= 0 && idx_b >= 0,
"In CDistance::distance(int32_t,int32_t), idx_a and "
192 "idx_b must be positive, %d and %d given instead\n", idx_a, idx_b)
201 if (idx_a>=num_vectors)
202 idx_a=2*num_vectors-1-idx_a;
204 if (idx_b>=num_vectors)
205 idx_b=2*num_vectors-1-idx_b;
208 REQUIRE(idx_a < lhs->get_num_vectors() && idx_b < rhs->get_num_vectors(),
209 "In CDistance::distance(int32_t,int32_t), idx_a and idx_b must be less than "
210 "the number of vectors, but %d >= %d or %d >= %d\n",
231 SG_INFO(
"precomputing distance matrix (%ix%i)\n", num_left, num_right)
233 ASSERT(num_left==num_right)
235 int32_t num=num_left;
240 for (int32_t i=0; i<num; i++)
243 for (int32_t j=0; j<=i; j++)
251 void CDistance::init()
261 "Feature vectors to occur on left hand side.");
263 "Feature vectors to occur on right hand side.");
269 int32_t i_start=params->
start;
270 int32_t i_end=params->
end;
279 int64_t total=total_start;
281 for (int32_t i=i_start; i<i_end; i++)
288 for (int32_t j=j_start; j<n; j++)
293 if (symmetric && i!=j)
300 if (symmetric && i!=j)
326 int64_t total_num = int64_t(m)*n;
329 bool symmetric= (
lhs &&
lhs==
rhs && m==n);
331 SG_DEBUG(
"returning distance matrix of size %dx%d\n", m, n)
333 result=SG_MALLOC(T, total_num);
349 get_distance_matrix_helper<T>((
void*) ¶ms);
353 pthread_t* threads = SG_MALLOC(pthread_t, num_threads-1);
355 int64_t step= total_num/num_threads;
360 for (t=0; t<num_threads; t++)
363 params[t].
result = result;
373 int code=pthread_create(&threads[t], NULL,
374 CDistance::get_distance_matrix_helper<T>, (
void*)¶ms[t]);
378 SG_WARNING(
"Thread creation failed (thread %d of %d) "
379 "with error:'%s'\n",t, num_threads, strerror(code));
386 params[t].
result = result;
395 get_distance_matrix_helper<T>(¶ms[t]);
397 for (t=0; t<num_threads; t++)
399 if (pthread_join(threads[t], NULL) != 0)
400 SG_WARNING(
"pthread_join of thread %d/%d failed\n", t, num_threads)
415 template void* CDistance::get_distance_matrix_helper<float64_t>(
void* p);
416 template void* CDistance::get_distance_matrix_helper<float32_t>(
void* p);
void do_precompute_matrix()
matrix precomputation
virtual bool has_features()
Class Distance, a base class for all the distances used in the Shogun toolbox.
int32_t get_num_threads() const
virtual int32_t get_num_vec_lhs()
CFeatures * replace_lhs(CFeatures *lhs)
virtual void remove_lhs()
takes all necessary steps if the lhs is removed from distance matrix
virtual int32_t get_num_vectors() const =0
void add(bool *param, const char *name, const char *description="")
Class SGObject is the base class of all shogun objects.
#define SG_OBJ_PROGRESS(o,...)
virtual void remove_lhs_and_rhs()
A File access base class.
virtual EFeatureClass get_feature_class() const =0
virtual int32_t get_num_vec_rhs()
static bool cancel_computations()
CFeatures * replace_rhs(CFeatures *rhs)
int32_t compute_row_start(int64_t offs, int32_t n, bool symmetric)
virtual float64_t distance(int32_t idx_a, int32_t idx_b)
all of classes and functions are contained in the shogun namespace
CFeatures * lhs
feature vectors to occur on the left hand side
The class Features is the base class of all feature objects.
CFeatures * rhs
feature vectors to occur on the right hand side
SGMatrix< float64_t > get_distance_matrix()
float32_t * precomputed_matrix
virtual bool init(CFeatures *lhs, CFeatures *rhs)
virtual void remove_rhs()
takes all necessary steps if the rhs is removed from distance matrix
static void * get_distance_matrix_helper(void *p)
virtual float64_t compute(int32_t idx_a, int32_t idx_b)=0
virtual EFeatureType get_feature_type() const =0