29 #define COMPLEX128_ERROR_NOARG(function) \
31 void SGVector<complex128_t>::function() \
33 SG_SERROR("SGVector::%s():: Not supported for complex128_t\n",\
37 #define BOOL_ERROR_ONEARG(function) \
39 void SGVector<bool>::function(bool a) \
41 SG_SERROR("SGVector::%s():: Not supported for bool\n",\
45 #define COMPLEX128_ERROR_ONEARG(function) \
47 void SGVector<complex128_t>::function(complex128_t a) \
49 SG_SERROR("SGVector::%s():: Not supported for complex128_t\n",\
53 #define COMPLEX128_ERROR_TWOARGS(function) \
55 void SGVector<complex128_t>::function(complex128_t a, complex128_t b) \
57 SG_SERROR("SGVector::%s():: Not supported for complex128_t\n",\
61 #define COMPLEX128_ERROR_THREEARGS(function) \
63 void SGVector<complex128_t>::function(complex128_t a, complex128_t b,\
66 SG_SERROR("SGVector::%s():: Not supported for complex128_t\n",\
127 vector[i]=const_elem ;
134 catlas_dset(vlen, const_elem, vector, 1);
140 catlas_sset(vlen, const_elem, vector, 1);
142 #endif // HAVE_CATLAS
147 range_fill_vector(vector, vlen, start);
155 random_vector(vector, vlen, min_value, max_value);
163 randperm(vector, vlen);
171 CMath::qsort<T>(vector, vlen);
186 return data[i] < data[j];
198 for (
index_t i=0; i < vlen; ++i)
209 SG_SERROR(
"SGVector::argsort():: Not supported for complex128_t\n");
220 for(int32_t i=1; i<vlen; i++)
222 if (vector[i-1] > vector[i])
232 SG_SERROR(
"SGVector::is_sorted():: Not supported for complex128_t\n");
240 for (i=0; i<vlen; ++i)
242 if (vector[i]>element)
251 SG_SERROR(
"SGVector::find_position_to_insert():: \
252 Not supported for complex128_t\n");
259 return SGVector<T>(clone_vector(vector, vlen), vlen);
265 T* result = SG_MALLOC(T, len);
266 memcpy(result, vec,
sizeof(T)*len);
273 for (int32_t i=0; i<len; i++)
280 for (int32_t i=0; i<len; i++)
288 SG_SERROR(
"SGVector::range_fill_vector():: \
289 Not supported for complex128_t\n");
295 ASSERT(vector && (index>=0) && (index<vlen))
296 return vector[index];
302 ASSERT(vector && (index>=0) && (index<vlen))
303 vector[index]=p_element;
309 vector=SG_REALLOC(T, vector, vlen, n);
312 memset(&vector[vlen], 0, (n-vlen)*
sizeof(T));
334 for (int32_t i=0; i<vlen; i++)
343 for (int32_t i=0; i<vlen; i++)
364 SG_SPRINT(
"SGVector '%p' of size: %d\n", vector, vlen)
370 vector=((
SGVector*)(&orig))->vector;
392 if (other.
vlen!=vlen)
397 if (other.
vector[i]!=vector[i])
406 const char* prefix)
const
408 display_vector(vector, vlen, name, prefix);
424 for (int32_t i=0; i<n; i++)
425 SG_SPRINT(
"%s%d%s", prefix, vector[i] ? 1 : 0, i==n-1?
"" :
",")
435 for (int32_t i=0; i<n; i++)
436 SG_SPRINT(
"%s%c%s", prefix, vector[i], i==n-1?
"" :
",")
446 for (int32_t i=0; i<n; i++)
447 SG_SPRINT(
"%s%u%s", prefix, vector[i], i==n-1?
"" :
",")
457 for (int32_t i=0; i<n; i++)
458 SG_SPRINT(
"%s%d%s", prefix, vector[i], i==n-1?
"" :
",")
468 for (int32_t i=0; i<n; i++)
469 SG_SPRINT(
"%s%u%s", prefix, vector[i], i==n-1?
"" :
",")
479 for (int32_t i=0; i<n; i++)
480 SG_SPRINT(
"%s%d%s", prefix, vector[i], i==n-1?
"" :
",")
490 for (int32_t i=0; i<n; i++)
491 SG_SPRINT(
"%s%d%s", prefix, vector[i], i==n-1?
"" :
",")
501 for (int32_t i=0; i<n; i++)
502 SG_SPRINT(
"%s%u%s", prefix, vector[i], i==n-1?
"" :
",")
513 for (int32_t i=0; i<n; i++)
514 SG_SPRINT(
"%s%lld%s", prefix, vector[i], i==n-1?
"" :
",")
524 for (int32_t i=0; i<n; i++)
525 SG_SPRINT(
"%s%llu%s", prefix, vector[i], i==n-1?
"" :
",")
535 for (int32_t i=0; i<n; i++)
536 SG_SPRINT(
"%s%g%s", prefix, vector[i], i==n-1?
"" :
",")
546 for (int32_t i=0; i<n; i++)
547 SG_SPRINT(
"%s%.18g%s", prefix, vector[i], i==n-1?
"" :
",")
553 const char* name,
const char* prefix)
557 for (int32_t i=0; i<n; i++)
559 SG_SPRINT(
"%s%.36Lg%s", prefix, (
long double) vector[i],
567 const char* name,
const char* prefix)
571 for (int32_t i=0; i<n; i++)
573 SG_SPRINT(
"%s(%.36lg+i%.36lg)%s", prefix, vector[i].real(),
574 vector[i].imag(), i==n-1?
"" :
",");
581 const T scalar,
const T* vec2, int32_t n)
583 for (int32_t i=0; i<n; i++)
584 vec1[i]+=scalar*vec2[i];
593 cblas_daxpy(n, scalar, vec2, skip, vec1, skip);
595 for (int32_t i=0; i<n; i++)
596 vec1[i]+=scalar*vec2[i];
606 cblas_saxpy(n, scalar, vec2, skip, vec1, skip);
608 for (int32_t i=0; i<n; i++)
609 vec1[i]+=scalar*vec2[i];
618 Eigen::Map<const Eigen::VectorXd> ev1(v1,n);
619 Eigen::Map<const Eigen::VectorXd> ev2(v2,n);
623 r = cblas_ddot(n, v1, skip, v2, skip);
625 for (int32_t i=0; i<n; i++)
636 Eigen::Map<const Eigen::VectorXf> ev1(v1,n);
637 Eigen::Map<const Eigen::VectorXf> ev2(v2,n);
641 r = cblas_sdot(n, v1, skip, v2, skip);
643 for (int32_t i=0; i<n; i++)
652 for (int32_t i=0; i<len; i++)
653 vec[i]=CMath::random(min_value, max_value);
680 T* perm = SG_MALLOC(T, n);
697 for (int32_t i = 0; i < n; i++)
712 for (int32_t i = 0; i < n; i++)
713 CMath::swap(vec[i], vec[CMath::random(i, n-1)]);
719 for (int32_t i = 0; i < n; i++)
720 CMath::swap(vec[i], vec[rand->
random(i, n-1)]);
740 CMath::swap(vec.
vector[i],
763 for (int32_t i=0; i<len; i++)
766 return CMath::sqrt(result);
773 for (int32_t i=0; i<len; i++)
776 return CMath::sqrt(result);
783 for (int32_t i=0; i<len; i++)
786 return CMath::sqrt(result);
793 for (int32_t i=0; i<len; i++)
796 return CMath::sqrt(result);
803 for (int32_t i=0; i<len; i++)
806 return CMath::sqrt(result);
813 for (int32_t i=0; i<len; i++)
816 return CMath::sqrt(result);
823 for (int32_t i=0; i<len; i++)
826 return CMath::sqrt(result);
833 for (int32_t i=0; i<len; i++)
836 return CMath::sqrt(result);
843 for (int32_t i=0; i<len; i++)
846 return CMath::sqrt(result);
854 norm = cblas_dnrm2(n, v, 1);
856 norm = CMath::sqrt(SGVector::dot(v, v, n));
865 for (int32_t i=0; i<len; i++)
868 return CMath::sqrt(result);
875 for (int32_t i=0; i<len; i++)
878 return CMath::sqrt(result);
885 for (int32_t i=0;i<len; ++i)
886 result+=CMath::abs(x[i]);
896 for (int32_t i=0; i<len; i++)
897 result+=CMath::pow(fabs(x[i]), q);
914 return CMath::pow((
float64_t) qsq(x, len, q), 1.0/q);
931 for (int32_t i=1; i<len; i++)
932 minv=CMath::min(vec[i], minv);
943 int32_t idx = cblas_idamax(len, vec, skip);
945 return CMath::abs(vec[idx]);
953 int32_t idx = cblas_isamax(len, vec, skip);
955 return CMath::abs(vec[idx]);
964 T maxv=CMath::abs(vec[0]);
966 for (int32_t i=1; i<len; i++)
967 maxv=CMath::max(CMath::abs(vec[i]), maxv);
986 for (int32_t i=1; i<len; i++)
987 maxv=CMath::max(vec[i], maxv);
997 int32_t idx = cblas_idamax(len, vec, inc);
999 if (maxv_ptr != NULL)
1000 *maxv_ptr = CMath::abs(vec[idx*inc]);
1009 int32_t idx = cblas_isamax(len, vec, inc);
1011 if (maxv_ptr != NULL)
1012 *maxv_ptr = CMath::abs(vec[idx*inc]);
1021 ASSERT(len > 0 || inc > 0)
1023 T maxv = CMath::abs(vec[0]);
1026 for (int32_t i = 1, j = inc ; i < len ; i++, j += inc)
1028 if (CMath::abs(vec[j]) > maxv)
1029 maxv = CMath::abs(vec[j]), maxIdx = i;
1032 if (maxv_ptr != NULL)
1043 SG_SERROR(
"SGVector::arg_max_abs():: Not supported for complex128_t\n");
1050 ASSERT(len > 0 || inc > 0)
1055 for (int32_t i = 1, j = inc ; i < len ; i++, j += inc)
1058 maxv = vec[j], maxIdx = i;
1061 if (maxv_ptr != NULL)
1072 SG_SERROR(
"SGVector::arg_max():: Not supported for complex128_t\n");
1081 ASSERT(len > 0 || inc > 0)
1086 for (int32_t i = 1, j = inc ; i < len ; i++, j += inc)
1089 minv = vec[j], minIdx = i;
1092 if (minv_ptr != NULL)
1103 SG_SERROR(
"SGVector::arg_min():: Not supported for complex128_t\n");
1112 for (int32_t i=0; i<len; i++)
1113 result+=CMath::abs(vec[i]);
1123 result = cblas_dasum(len, vec, 1);
1131 result = cblas_sasum(len, vec, 1);
1140 return CMath::abs(x-y)<precision;
1146 CMath::qsort<T>(output, size);
1149 for (int32_t i=0; i<size; i++)
1151 if (i==0 || output[i]!=output[i-1])
1152 output[j++]=output[i];
1161 SG_SERROR(
"SGVector::unique():: Not supported for complex128_t\n");
1171 for (
index_t i=0; i < vlen; ++i)
1172 if (vector[i] == elem)
1181 for (int32_t i=0; i<len; i++)
1189 cblas_dscal(len, alpha, vec, 1);
1195 cblas_sscal(len, alpha, vec, 1);
1202 scale_vector(alpha, vector, vlen);
1209 for (
index_t i = 0 ; i < vlen ; ++i )
1239 SG_SERROR(
"SGVector::load():: Not supported for complex128_t\n");
1254 SG_SERROR(
"SGVector::save():: Not supported for complex128_t\n");
1258 #define MATHOP(op) \
1259 template <class T> void SGVector<T>::op() \
1261 for (int32_t i=0; i<vlen; i++) \
1262 vector[i]=(T) CMath::op((double) vector[i]); \
1281 #define COMPLEX128_MATHOP(op) \
1283 void SGVector<complex128_t>::op() \
1285 for (int32_t i=0; i<vlen; i++) \
1286 vector[i]=complex128_t(CMath::op(vector[i])); \
1300 #undef COMPLEX128_MATHOP
1302 #define COMPLEX128_MATHOP_NOTIMPLEMENTED(op) \
1304 void SGVector<complex128_t>::op() \
1306 SG_SERROR("SGVector::%s():: Not supported for complex128_t\n",#op);\
1312 #undef COMPLEX128_MATHOP_NOTIMPLEMENTED
1316 for (int32_t i=0; i<vlen; i++)
1317 vector[i]=CMath::atan2(vector[i], x);
1325 for (int32_t i=0; i<vlen; i++)
1326 vector[i]=(T) CMath::pow((
double) vector[i], (
double) q);
1339 CMath::linspace(output, start, end, n);
1348 SG_SERROR(
"SGVector::linspace():: Not supported for complex128_t\n");
1355 for (int32_t i=0; i<vlen; i++)
1356 vector[i]=CMath::pow(vector[i], q);
1362 for (int32_t i=0; i<vlen; i++)
1363 real[i]=CMath::real(vector[i]);
1370 for (int32_t i=0; i<vlen; i++)
1371 imag[i]=CMath::imag(vector[i]);
1379 if (nrows*ncols>vector.
size())
1380 SG_SERROR(
"SGVector::convert_to_matrix():: Dimensions mismatch\n");
1392 if (nrows*ncols>vlen)
1393 SG_SERROR(
"SGVector::convert_to_matrix():: Dimensions mismatch\n");
1397 matrix=SG_MALLOC(T, nrows*ncols);
1401 for (
index_t i=0; i<ncols*nrows; i++)
1402 matrix[i]=vector[i];
1406 for (
index_t i=0; i<nrows; i++)
1408 for (
index_t j=0; j<ncols; j++)
1409 matrix[i+j*nrows]=vector[j+i*ncols];
1414 #define UNDEFINED(function, type) \
1416 SGVector<float64_t> SGVector<type>::function() \
1418 SG_SERROR("SGVector::%s():: Not supported for %s\n", \
1419 #function, #type); \
1420 SGVector<float64_t> ret(vlen); \
1452 template class SGVector<bool>;
1453 template class SGVector<char>;
1454 template class SGVector<int8_t>;
1455 template class SGVector<uint8_t>;
1456 template class SGVector<int16_t>;
1457 template class SGVector<uint16_t>;
1458 template class SGVector<int32_t>;
1459 template class SGVector<uint32_t>;
1460 template class SGVector<int64_t>;
1461 template class SGVector<uint64_t>;
1462 template class SGVector<float32_t>;
1463 template class SGVector<float64_t>;
1464 template class SGVector<floatmax_t>;
1465 template class SGVector<complex128_t>;
1468 #undef COMPLEX128_ERROR_NOARG
1469 #undef COMPLEX128_ERROR_ONEARG
1470 #undef COMPLEX128_ERROR_TWOARGS
1471 #undef COMPLEX128_ERROR_THREEARGS
#define BOOL_ERROR_ONEARG(function)
double norm(double *v, double p, int n)
IndexSorter(const SGVector< T > *vec)
std::complex< float64_t > complex128_t
uint64_t random(uint64_t min_value, uint64_t max_value)
#define COMPLEX128_ERROR_TWOARGS(function)
void set(SGVector< T > orig)
#define SG_SNOTIMPLEMENTED
void display_vector(const char *name="vector", const char *prefix="") const
#define UNDEFINED(function, type)
virtual void get_vector(bool *&vector, int32_t &len)
shogun reference count managed data
A File access base class.
SGSparseVectorEntry< T > * features
: Pseudo random number geneartor
#define COMPLEX128_ERROR_ONEARG(function)
all of classes and functions are contained in the shogun namespace
template class SGSparseVector The assumtion is that the stored SGSparseVectorEntry* vector is orde...
virtual void copy_data(const SGReferencedData &orig)
#define COMPLEX128_ERROR_NOARG(function)
virtual void set_vector(const bool *vector, int32_t len)
#define COMPLEX128_MATHOP(op)
#define COMPLEX128_MATHOP_NOTIMPLEMENTED(op)
void set_const(float32_tconst_elem)
void add(const SGVector< T > x)