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 COMPLEX128_ERROR_ONEARG(function) \
39 void SGVector<complex128_t>::function(complex128_t a) \
41 SG_SERROR("SGVector::%s():: Not supported for complex128_t\n",\
45 #define COMPLEX128_ERROR_TWOARGS(function) \
47 void SGVector<complex128_t>::function(complex128_t a, complex128_t b) \
49 SG_SERROR("SGVector::%s():: Not supported for complex128_t\n",\
53 #define COMPLEX128_ERROR_THREEARGS(function) \
55 void SGVector<complex128_t>::function(complex128_t a, complex128_t b,\
58 SG_SERROR("SGVector::%s():: Not supported for complex128_t\n",\
119 vector[i]=const_elem ;
126 catlas_dset(vlen, const_elem, vector, 1);
130 void SGVector<float32_t>::set_const(
float32_t const_elem)
132 catlas_sset(vlen, const_elem, vector, 1);
134 #endif // HAVE_CATLAS
139 range_fill_vector(vector, vlen, start);
147 random_vector(vector, vlen, min_value, max_value);
155 randperm(vector, vlen);
163 CMath::qsort<T>(vector, vlen);
178 return data[i] < data[j];
190 for (
index_t i=0; i < vlen; ++i)
201 SG_SERROR(
"SGVector::argsort():: Not supported for complex128_t\n");
212 for(int32_t i=1; i<vlen; i++)
214 if (vector[i-1] > vector[i])
224 SG_SERROR(
"SGVector::is_sorted():: Not supported for complex128_t\n");
232 for (i=0; i<vlen; ++i)
234 if (vector[i]>element)
243 SG_SERROR(
"SGVector::find_position_to_insert():: \
244 Not supported for complex128_t\n");
251 return SGVector<T>(clone_vector(vector, vlen), vlen);
257 T* result = SG_MALLOC(T, len);
258 memcpy(result, vec,
sizeof(T)*len);
265 for (int32_t i=0; i<len; i++)
272 for (int32_t i=0; i<len; i++)
280 SG_SERROR(
"SGVector::range_fill_vector():: \
281 Not supported for complex128_t\n");
287 ASSERT(vector && (index>=0) && (index<vlen))
288 return vector[index];
294 ASSERT(vector && (index>=0) && (index<vlen))
295 vector[index]=p_element;
301 vector=SG_REALLOC(T, vector, vlen, n);
304 memset(&vector[vlen], 0, (n-vlen)*
sizeof(T));
326 for (int32_t i=0; i<vlen; i++)
335 for (int32_t i=0; i<vlen; i++)
356 SG_SPRINT(
"SGVector '%p' of size: %d\n", vector, vlen)
362 vector=((
SGVector*)(&orig))->vector;
384 if (other.
vlen!=vlen)
389 if (other.
vector[i]!=vector[i])
398 const char* prefix)
const
400 display_vector(vector, vlen, name, prefix);
416 for (int32_t i=0; i<n; i++)
417 SG_SPRINT(
"%s%d%s", prefix, vector[i] ? 1 : 0, i==n-1?
"" :
",")
427 for (int32_t i=0; i<n; i++)
428 SG_SPRINT(
"%s%c%s", prefix, vector[i], i==n-1?
"" :
",")
438 for (int32_t i=0; i<n; i++)
439 SG_SPRINT(
"%s%u%s", prefix, vector[i], i==n-1?
"" :
",")
449 for (int32_t i=0; i<n; i++)
450 SG_SPRINT(
"%s%d%s", prefix, vector[i], i==n-1?
"" :
",")
460 for (int32_t i=0; i<n; i++)
461 SG_SPRINT(
"%s%u%s", prefix, vector[i], i==n-1?
"" :
",")
471 for (int32_t i=0; i<n; i++)
472 SG_SPRINT(
"%s%d%s", prefix, vector[i], i==n-1?
"" :
",")
482 for (int32_t i=0; i<n; i++)
483 SG_SPRINT(
"%s%d%s", prefix, vector[i], i==n-1?
"" :
",")
493 for (int32_t i=0; i<n; i++)
494 SG_SPRINT(
"%s%u%s", prefix, vector[i], i==n-1?
"" :
",")
505 for (int32_t i=0; i<n; i++)
506 SG_SPRINT(
"%s%lld%s", prefix, vector[i], i==n-1?
"" :
",")
516 for (int32_t i=0; i<n; i++)
517 SG_SPRINT(
"%s%llu%s", prefix, vector[i], i==n-1?
"" :
",")
527 for (int32_t i=0; i<n; i++)
528 SG_SPRINT(
"%s%g%s", prefix, vector[i], i==n-1?
"" :
",")
538 for (int32_t i=0; i<n; i++)
539 SG_SPRINT(
"%s%.18g%s", prefix, vector[i], i==n-1?
"" :
",")
545 const char* name,
const char* prefix)
549 for (int32_t i=0; i<n; i++)
551 SG_SPRINT(
"%s%.36Lg%s", prefix, (
long double) vector[i],
559 const char* name,
const char* prefix)
563 for (int32_t i=0; i<n; i++)
565 SG_SPRINT(
"%s(%.36lg+i%.36lg)%s", prefix, vector[i].real(),
566 vector[i].imag(), i==n-1?
"" :
",");
573 const T scalar,
const T* vec2, int32_t n)
575 for (int32_t i=0; i<n; i++)
576 vec1[i]+=scalar*vec2[i];
585 cblas_daxpy(n, scalar, vec2, skip, vec1, skip);
587 for (int32_t i=0; i<n; i++)
588 vec1[i]+=scalar*vec2[i];
598 cblas_saxpy(n, scalar, vec2, skip, vec1, skip);
600 for (int32_t i=0; i<n; i++)
601 vec1[i]+=scalar*vec2[i];
610 Eigen::Map<const Eigen::VectorXd> ev1(v1,n);
611 Eigen::Map<const Eigen::VectorXd> ev2(v2,n);
615 r = cblas_ddot(n, v1, skip, v2, skip);
617 for (int32_t i=0; i<n; i++)
628 Eigen::Map<const Eigen::VectorXf> ev1(v1,n);
629 Eigen::Map<const Eigen::VectorXf> ev2(v2,n);
633 r = cblas_sdot(n, v1, skip, v2, skip);
635 for (int32_t i=0; i<n; i++)
644 for (int32_t i=0; i<len; i++)
645 vec[i]=CMath::random(min_value, max_value);
672 T* perm = SG_MALLOC(T, n);
689 for (int32_t i = 0; i < n; i++)
704 for (int32_t i = 0; i < n; i++)
705 CMath::swap(vec[i], vec[CMath::random(i, n-1)]);
711 for (int32_t i = 0; i < n; i++)
712 CMath::swap(vec[i], vec[rand->
random(i, n-1)]);
732 CMath::swap(vec.
vector[i],
755 for (int32_t i=0; i<len; i++)
758 return CMath::sqrt(result);
765 for (int32_t i=0; i<len; i++)
768 return CMath::sqrt(result);
775 for (int32_t i=0; i<len; i++)
778 return CMath::sqrt(result);
785 for (int32_t i=0; i<len; i++)
788 return CMath::sqrt(result);
795 for (int32_t i=0; i<len; i++)
798 return CMath::sqrt(result);
805 for (int32_t i=0; i<len; i++)
808 return CMath::sqrt(result);
815 for (int32_t i=0; i<len; i++)
818 return CMath::sqrt(result);
825 for (int32_t i=0; i<len; i++)
828 return CMath::sqrt(result);
835 for (int32_t i=0; i<len; i++)
838 return CMath::sqrt(result);
846 norm = cblas_dnrm2(n, v, 1);
848 norm = CMath::sqrt(SGVector::dot(v, v, n));
857 for (int32_t i=0; i<len; i++)
860 return CMath::sqrt(result);
867 for (int32_t i=0; i<len; i++)
870 return CMath::sqrt(result);
877 for (int32_t i=0;i<len; ++i)
878 result+=CMath::abs(x[i]);
888 for (int32_t i=0; i<len; i++)
889 result+=CMath::pow(fabs(x[i]), q);
906 return CMath::pow((
float64_t) qsq(x, len, q), 1.0/q);
923 for (int32_t i=1; i<len; i++)
924 minv=CMath::min(vec[i], minv);
935 int32_t idx = cblas_idamax(len, vec, skip);
937 return CMath::abs(vec[idx]);
945 int32_t idx = cblas_isamax(len, vec, skip);
947 return CMath::abs(vec[idx]);
956 T maxv=CMath::abs(vec[0]);
958 for (int32_t i=1; i<len; i++)
959 maxv=CMath::max(CMath::abs(vec[i]), maxv);
978 for (int32_t i=1; i<len; i++)
979 maxv=CMath::max(vec[i], maxv);
989 int32_t idx = cblas_idamax(len, vec, inc);
991 if (maxv_ptr != NULL)
992 *maxv_ptr = CMath::abs(vec[idx*inc]);
1001 int32_t idx = cblas_isamax(len, vec, inc);
1003 if (maxv_ptr != NULL)
1004 *maxv_ptr = CMath::abs(vec[idx*inc]);
1013 ASSERT(len > 0 || inc > 0)
1015 T maxv = CMath::abs(vec[0]);
1018 for (int32_t i = 1, j = inc ; i < len ; i++, j += inc)
1020 if (CMath::abs(vec[j]) > maxv)
1021 maxv = CMath::abs(vec[j]), maxIdx = i;
1024 if (maxv_ptr != NULL)
1035 SG_SERROR(
"SGVector::arg_max_abs():: Not supported for complex128_t\n");
1042 ASSERT(len > 0 || inc > 0)
1047 for (int32_t i = 1, j = inc ; i < len ; i++, j += inc)
1050 maxv = vec[j], maxIdx = i;
1053 if (maxv_ptr != NULL)
1064 SG_SERROR(
"SGVector::arg_max():: Not supported for complex128_t\n");
1073 ASSERT(len > 0 || inc > 0)
1078 for (int32_t i = 1, j = inc ; i < len ; i++, j += inc)
1081 minv = vec[j], minIdx = i;
1084 if (minv_ptr != NULL)
1095 SG_SERROR(
"SGVector::arg_min():: Not supported for complex128_t\n");
1104 for (int32_t i=0; i<len; i++)
1105 result+=CMath::abs(vec[i]);
1115 result = cblas_dasum(len, vec, 1);
1123 result = cblas_sasum(len, vec, 1);
1132 return CMath::abs(x-y)<precision;
1138 CMath::qsort<T>(output, size);
1141 for (int32_t i=0; i<size; i++)
1143 if (i==0 || output[i]!=output[i-1])
1144 output[j++]=output[i];
1153 SG_SERROR(
"SGVector::unique():: Not supported for complex128_t\n");
1163 for (
index_t i=0; i < vlen; ++i)
1164 if (vector[i] == elem)
1173 for (int32_t i=0; i<len; i++)
1181 cblas_dscal(len, alpha, vec, 1);
1187 cblas_sscal(len, alpha, vec, 1);
1194 scale_vector(alpha, vector, vlen);
1201 for (
index_t i = 0 ; i < vlen ; ++i )
1231 SG_SERROR(
"SGVector::load():: Not supported for complex128_t\n");
1246 SG_SERROR(
"SGVector::save():: Not supported for complex128_t\n");
1250 #define MATHOP(op) \
1251 template <class T> void SGVector<T>::op() \
1253 for (int32_t i=0; i<vlen; i++) \
1254 vector[i]=(T) CMath::op((double) vector[i]); \
1273 #define COMPLEX128_MATHOP(op) \
1275 void SGVector<complex128_t>::op() \
1277 for (int32_t i=0; i<vlen; i++) \
1278 vector[i]=complex128_t(CMath::op(vector[i])); \
1292 #undef COMPLEX128_MATHOP
1294 #define COMPLEX128_MATHOP_NOTIMPLEMENTED(op) \
1296 void SGVector<complex128_t>::op() \
1298 SG_SERROR("SGVector::%s():: Not supported for complex128_t\n",#op);\
1304 #undef COMPLEX128_MATHOP_NOTIMPLEMENTED
1308 for (int32_t i=0; i<vlen; i++)
1309 vector[i]=CMath::atan2(vector[i], x);
1316 for (int32_t i=0; i<vlen; i++)
1317 vector[i]=(T) CMath::pow((
double) vector[i], (
double) q);
1330 CMath::linspace(output, start, end, n);
1339 SG_SERROR(
"SGVector::linspace():: Not supported for complex128_t\n");
1346 for (int32_t i=0; i<vlen; i++)
1347 vector[i]=CMath::pow(vector[i], q);
1353 for (int32_t i=0; i<vlen; i++)
1354 real[i]=CMath::real(vector[i]);
1361 for (int32_t i=0; i<vlen; i++)
1362 imag[i]=CMath::imag(vector[i]);
1370 if (nrows*ncols>vector.
size())
1371 SG_SERROR(
"SGVector::convert_to_matrix():: Dimensions mismatch\n");
1383 if (nrows*ncols>vlen)
1384 SG_SERROR(
"SGVector::convert_to_matrix():: Dimensions mismatch\n");
1388 matrix=SG_MALLOC(T, nrows*ncols);
1392 for (
index_t i=0; i<ncols*nrows; i++)
1393 matrix[i]=vector[i];
1397 for (
index_t i=0; i<nrows; i++)
1399 for (
index_t j=0; j<ncols; j++)
1400 matrix[i+j*nrows]=vector[j+i*ncols];
1405 #define UNDEFINED(function, type) \
1407 SGVector<float64_t> SGVector<type>::function() \
1409 SG_SERROR("SGVector::%s():: Not supported for %s\n", \
1410 #function, #type); \
1411 SGVector<float64_t> ret(vlen); \
1443 template class SGVector<bool>;
1444 template class SGVector<char>;
1445 template class SGVector<int8_t>;
1446 template class SGVector<uint8_t>;
1447 template class SGVector<int16_t>;
1448 template class SGVector<uint16_t>;
1449 template class SGVector<int32_t>;
1450 template class SGVector<uint32_t>;
1451 template class SGVector<int64_t>;
1452 template class SGVector<uint64_t>;
1453 template class SGVector<float32_t>;
1454 template class SGVector<float64_t>;
1455 template class SGVector<floatmax_t>;
1456 template class SGVector<complex128_t>;
1459 #undef COMPLEX128_ERROR_NOARG
1460 #undef COMPLEX128_ERROR_ONEARG
1461 #undef COMPLEX128_ERROR_TWOARGS
1462 #undef COMPLEX128_ERROR_THREEARGS