35 #include <itpp/itexports.h>
41 template <
class T>
class Vec;
43 template <
class T>
class Sparse_Vec;
49 Sparse_Vec<T>
operator+(
const Sparse_Vec<T> &v1,
const Sparse_Vec<T> &v2);
53 T
operator*(
const Sparse_Vec<T> &v1,
const Sparse_Vec<T> &v2);
57 T
operator*(
const Sparse_Vec<T> &v1,
const Vec<T> &v2);
61 T
operator*(
const Vec<T> &v1,
const Sparse_Vec<T> &v2);
65 Sparse_Vec<T>
elem_mult(
const Sparse_Vec<T> &v1,
const Sparse_Vec<T> &v2);
69 Vec<T>
elem_mult(
const Sparse_Vec<T> &v1,
const Vec<T> &v2);
73 Sparse_Vec<T>
elem_mult_s(
const Sparse_Vec<T> &v1,
const Vec<T> &v2);
77 Vec<T>
elem_mult(
const Vec<T> &v1,
const Sparse_Vec<T> &v2);
81 Sparse_Vec<T>
elem_mult_s(
const Vec<T> &v1,
const Sparse_Vec<T> &v2);
87 template <
typename NumT>
struct Sparse_Eps_Type_Selector;
88 template <>
struct Sparse_Eps_Type_Selector<double> {
typedef double eps_type;};
89 template <>
struct Sparse_Eps_Type_Selector<std::complex<double> > {
typedef double eps_type;};
90 template <>
struct Sparse_Eps_Type_Selector<int> {
typedef int eps_type;};
91 template <>
struct Sparse_Eps_Type_Selector<short> {
typedef short eps_type;};
92 template <>
struct Sparse_Eps_Type_Selector<itpp::bin> {
typedef int eps_type;};
152 void set_size(
int sz,
int data_init = -1);
155 int size()
const {
return v_size; }
159 if (check_small_elems_flag) {
198 void set(
int i, T v);
201 void set(
const ivec &index_vec,
const Vec<T> &v);
210 void add_elem(
const int i,
const T v);
213 void add(
const ivec &index_vec,
const Vec<T> &v);
231 if (check_small_elems_flag) {
239 if (check_small_elems_flag) {
247 if (check_small_elems_flag) {
254 inline void get_nz(
int p,
int &idx, T &dat) {
255 if (check_small_elems_flag) {
329 int v_size, used_size, data_size;
332 typename details::Sparse_Eps_Type_Selector<T>::eps_type eps;
333 bool check_small_elems_flag;
341 typedef Sparse_Vec<int> sparse_ivec;
347 typedef Sparse_Vec<double> sparse_vec;
353 typedef Sparse_Vec<std::complex<double> > sparse_cvec;
358 void Sparse_Vec<T>::init()
366 check_small_elems_flag =
true;
370 void Sparse_Vec<T>::alloc()
372 if (data_size != 0) {
373 data =
new T[data_size];
374 index =
new int[data_size];
379 void Sparse_Vec<T>::free()
399 data_size = data_init;
408 used_size = v.used_size;
409 data_size = v.data_size;
411 check_small_elems_flag = v.check_small_elems_flag;
414 for (
int i = 0; i < used_size; i++) {
416 index[i] = v.index[i];
429 for (
int i = 0; i < v_size; i++) {
431 if (used_size == data_size)
432 resize_data(data_size*2);
433 data[used_size] = v(i);
434 index[used_size] = i;
451 for (
int i = 0; i < v_size; i++) {
453 if (used_size == data_size)
454 resize_data(data_size*2);
455 data[used_size] = v(i);
456 index[used_size] = i;
474 if (data_init != -1) {
476 data_size = data_init;
484 if (check_small_elems_flag) {
485 remove_small_elements();
488 return double(used_size) / v_size;
495 remove_small_elements();
502 int nrof_removed_elements = 0;
505 for (i = 0;i < used_size;i++) {
507 nrof_removed_elements++;
509 else if (nrof_removed_elements > 0) {
510 data[i-nrof_removed_elements] = data[i];
511 index[i-nrof_removed_elements] = index[i];
516 used_size -= nrof_removed_elements;
519 check_small_elems_flag =
false;
526 it_assert(new_size >= used_size,
"Sparse_Vec<T>::resize_data(int new_size): New size is to small");
528 if (new_size != data_size) {
533 int *tmp_pos = index;
534 data_size = new_size;
536 for (
int p = 0; p < used_size; p++) {
537 data[p] = tmp_data[p];
538 index[p] = tmp_pos[p];
549 if (check_small_elems_flag) {
550 remove_small_elements();
552 resize_data(used_size);
561 for (
int p = 0; p < used_size; p++)
562 v(index[p]) = data[p];
577 it_assert_debug(i >= 0 && i < v_size,
"The index of the element is out of range");
581 for (p = 0; p < used_size; p++) {
587 return found ? data[p] : T(0);
593 it_assert_debug(i >= 0 && i < v_size,
"The index of the element is out of range");
596 bool larger_than_eps;
599 for (p = 0; p < used_size; p++) {
608 if (found && larger_than_eps)
610 else if (larger_than_eps) {
611 if (used_size == data_size)
612 resize_data(data_size*2 + 100);
614 index[used_size] = i;
620 remove_small_elements();
628 it_assert_debug(v_size > i,
"The index of the element exceeds the size of the sparse vector");
632 if (used_size == data_size)
633 resize_data(data_size*2 + 100);
635 index[used_size] = i;
646 it_assert_debug(v_size > i,
"The index of the element exceeds the size of the sparse vector");
648 for (p = 0; p < used_size; p++) {
657 if (used_size == data_size)
658 resize_data(data_size*2 + 100);
660 index[used_size] = i;
664 check_small_elems_flag =
true;
673 int nrof_nz = v.
size();
675 it_assert_debug(v_size >
max(index_vec),
"The indices exceeds the size of the sparse vector");
678 for (q = 0; q < nrof_nz; q++) {
680 for (p = 0; p < used_size; p++) {
689 if (used_size == data_size)
690 resize_data(data_size*2 + 100);
691 data[used_size] = v(q);
692 index[used_size] = i;
698 check_small_elems_flag =
true;
706 check_small_elems_flag =
false;
715 it_assert_debug(v_size > i,
"The index of the element exceeds the size of the sparse vector");
717 for (p = 0; p < used_size; p++) {
724 data[p] = data[used_size-1];
725 index[p] = index[used_size-1];
734 check_small_elems_flag =
false;
743 it_assert_debug(v_size > i,
"The index of the element exceeds the size of the sparse vector");
745 for (p = 0; p < used_size; p++) {
752 data[p] = data[used_size-1];
753 index[p] = index[used_size-1];
761 it_assert_debug(v_size >
max(index_vec),
"The indices exceeds the size of the sparse vector");
774 int nrof_nz = v.
size();
776 it_assert_debug(v_size >
max(index_vec),
"The indices exceeds the size of the sparse vector");
781 for (q = 0; q < nrof_nz; q++) {
783 if (used_size == data_size)
784 resize_data(data_size*2 + 100);
785 data[used_size] = v(q);
786 index[used_size] = index_vec(q);
797 for (
int i = 0; i < n; i++) {
798 r(i) = get_nz_index(i);
806 it_assert_debug(v_size > i1 && v_size > i2 && i1 <= i2 && i1 >= 0,
"The index of the element exceeds the size of the sparse vector");
810 for (
int p = 0; p < used_size; p++) {
811 if (index[p] >= i1 && index[p] <= i2) {
812 if (r.used_size == r.data_size)
814 r.data[r.used_size] = data[p];
815 r.index[r.used_size] = index[p] - i1;
820 r.check_small_elems_flag = check_small_elems_flag;
830 for (
int p = 0; p < used_size; p++)
831 sum += data[p] * data[p];
841 used_size = v.used_size;
842 data_size = v.data_size;
844 check_small_elems_flag = v.check_small_elems_flag;
847 for (
int i = 0; i < used_size; i++) {
849 index[i] = v.index[i];
861 check_small_elems_flag =
false;
864 for (
int i = 0; i < v_size; i++) {
866 if (used_size == data_size)
867 resize_data(data_size*2);
868 data[used_size] = v(i);
869 index[used_size] = i;
881 for (
int p = 0; p < used_size; p++) {
882 r.data[p] = -data[p];
883 r.index[p] = index[p];
885 r.used_size = used_size;
897 if (check_small_elems_flag)
898 remove_small_elements();
900 if (v_size != v.v_size) {
905 for (p = 0;p < used_size;p++) {
906 for (q = 0;q < v.used_size;q++) {
907 if (index[p] == v.index[q]) {
915 else if (data[p] != v.data[q])
926 if (used_size != v.used_size) {
927 if (used_size > v.used_size) {
933 int nrof_small_elems = 0;
934 for (q = 0;q < v.used_size;q++) {
938 if (v.used_size - nrof_small_elems != used_size)
953 int nrof_nz_v = v.used_size;
957 for (p = 0; p < nrof_nz_v; p++) {
959 tmp_data = v.data[p];
961 add_elem(i, tmp_data);
964 check_small_elems_flag =
true;
974 for (i = 0; i < v.
size(); i++)
978 check_small_elems_flag =
true;
987 int nrof_nz_v = v.used_size;
989 it_assert_debug(v_size == v.
size(),
"Attempted subtraction of unequal sized sparse vectors");
991 for (p = 0; p < nrof_nz_v; p++) {
993 tmp_data = v.data[p];
995 add_elem(i, -tmp_data);
998 check_small_elems_flag =
true;
1006 it_assert_debug(v_size == v.
size(),
"Attempted subtraction of unequal sized sparse vectors");
1008 for (i = 0; i < v.
size(); i++)
1012 check_small_elems_flag =
true;
1020 for (p = 0; p < used_size; p++) {
1024 check_small_elems_flag =
true;
1031 for (p = 0; p < used_size; p++) {
1036 check_small_elems_flag =
true;
1043 it_assert_debug(v1.v_size == v2.v_size,
"Sparse_Vec<T> * Sparse_Vec<T>");
1048 for (
int p = 0; p < v2.used_size; p++) {
1049 if (v1f[v2.index[p]] != T(0))
1050 sum += v1f[v2.index[p]] * v2.data[p];
1062 for (
int p1 = 0; p1 < v1.used_size; p1++)
1063 sum += v1.data[p1] * v2[v1.index[p1]];
1074 for (
int p2 = 0; p2 < v2.used_size; p2++)
1075 sum += v1[v2.index[p2]] * v2.data[p2];
1083 it_assert_debug(v1.v_size == v2.v_size,
"elem_mult(Sparse_Vec<T>, Sparse_Vec<T>)");
1086 ivec pos(v1.v_size);
1088 for (
int p1 = 0; p1 < v1.used_size; p1++)
1089 pos[v1.index[p1]] = p1;
1090 for (
int p2 = 0; p2 < v2.used_size; p2++) {
1091 if (pos[v2.index[p2]] != -1) {
1092 if (r.used_size == r.data_size)
1094 r.data[r.used_size] = v1.data[pos[v2.index[p2]]] * v2.data[p2];
1095 r.index[r.used_size] = v2.index[p2];
1111 for (
int p1 = 0; p1 < v1.used_size; p1++)
1112 r[v1.index[p1]] = v1.data[p1] * v2[v1.index[p1]];
1123 for (
int p1 = 0; p1 < v1.used_size; p1++) {
1124 if (v2[v1.index[p1]] != T(0)) {
1125 if (r.used_size == r.data_size)
1126 r.resize_data(r.used_size*2 + 100);
1127 r.data[r.used_size] = v1.data[p1] * v2[v1.index[p1]];
1128 r.index[r.used_size] = v1.index[p1];
1144 for (
int p2 = 0; p2 < v2.used_size; p2++)
1145 r[v2.index[p2]] = v1[v2.index[p2]] * v2.data[p2];
1156 for (
int p2 = 0; p2 < v2.used_size; p2++) {
1157 if (v1[v2.index[p2]] != T(0)) {
1158 if (r.used_size == r.data_size)
1159 r.resize_data(r.used_size*2 + 100);
1160 r.data[r.used_size] = v1[v2.index[p2]] * v2.data[p2];
1161 r.index[r.used_size] = v2.index[p2];
1173 it_assert_debug(v1.v_size == v2.v_size,
"Sparse_Vec<T> + Sparse_Vec<T>");
1176 ivec pos(v1.v_size);
1178 for (
int p1 = 0; p1 < v1.used_size; p1++)
1179 pos[v1.index[p1]] = p1;
1180 for (
int p2 = 0; p2 < v2.used_size; p2++) {
1181 if (pos[v2.index[p2]] == -1) {
1182 if (r.used_size == r.data_size)
1184 r.data[r.used_size] = v2.data[p2];
1185 r.index[r.used_size] = v2.index[p2];
1189 r.data[pos[v2.index[p2]]] += v2.data[p2];
1191 r.check_small_elems_flag =
true;
1228 ITPP_EXPORT_TEMPLATE
template class ITPP_EXPORT Sparse_Vec<int>;
1229 ITPP_EXPORT_TEMPLATE
template class ITPP_EXPORT Sparse_Vec<double>;
1230 ITPP_EXPORT_TEMPLATE
template class ITPP_EXPORT Sparse_Vec<std::complex<double> >;
1232 ITPP_EXPORT_TEMPLATE
template ITPP_EXPORT sparse_ivec
operator+(
const sparse_ivec &,
1233 const sparse_ivec &);
1234 ITPP_EXPORT_TEMPLATE
template ITPP_EXPORT sparse_vec
operator+(
const sparse_vec &,
1235 const sparse_vec &);
1236 ITPP_EXPORT_TEMPLATE
template ITPP_EXPORT sparse_cvec
operator+(
const sparse_cvec &,
1237 const sparse_cvec &);
1239 ITPP_EXPORT_TEMPLATE
template ITPP_EXPORT
int operator*(
const sparse_ivec &,
const sparse_ivec &);
1240 ITPP_EXPORT_TEMPLATE
template ITPP_EXPORT
double operator*(
const sparse_vec &,
const sparse_vec &);
1241 ITPP_EXPORT_TEMPLATE
template ITPP_EXPORT std::complex<double>
operator*(
const sparse_cvec &,
1242 const sparse_cvec &);
1244 ITPP_EXPORT_TEMPLATE
template ITPP_EXPORT
int operator*(
const sparse_ivec &,
const ivec &);
1245 ITPP_EXPORT_TEMPLATE
template ITPP_EXPORT
double operator*(
const sparse_vec &,
const vec &);
1246 ITPP_EXPORT_TEMPLATE
template ITPP_EXPORT std::complex<double>
operator*(
const sparse_cvec &,
1249 ITPP_EXPORT_TEMPLATE
template ITPP_EXPORT
int operator*(
const ivec &,
const sparse_ivec &);
1250 ITPP_EXPORT_TEMPLATE
template ITPP_EXPORT
double operator*(
const vec &,
const sparse_vec &);
1251 ITPP_EXPORT_TEMPLATE
template ITPP_EXPORT std::complex<double>
operator*(
const cvec &,
1252 const sparse_cvec &);
1254 ITPP_EXPORT_TEMPLATE
template ITPP_EXPORT sparse_ivec
elem_mult(
const sparse_ivec &,
1255 const sparse_ivec &);
1256 ITPP_EXPORT_TEMPLATE
template ITPP_EXPORT sparse_vec
elem_mult(
const sparse_vec &,
const sparse_vec &);
1257 ITPP_EXPORT_TEMPLATE
template ITPP_EXPORT sparse_cvec
elem_mult(
const sparse_cvec &,
1258 const sparse_cvec &);
1260 ITPP_EXPORT_TEMPLATE
template ITPP_EXPORT ivec
elem_mult(
const sparse_ivec &,
const ivec &);
1261 ITPP_EXPORT_TEMPLATE
template ITPP_EXPORT vec
elem_mult(
const sparse_vec &,
const vec &);
1262 ITPP_EXPORT_TEMPLATE
template ITPP_EXPORT cvec
elem_mult(
const sparse_cvec &,
const cvec &);
1264 ITPP_EXPORT_TEMPLATE
template ITPP_EXPORT sparse_ivec
elem_mult_s(
const sparse_ivec &,
const ivec &);
1265 ITPP_EXPORT_TEMPLATE
template ITPP_EXPORT sparse_vec
elem_mult_s(
const sparse_vec &,
const vec &);
1266 ITPP_EXPORT_TEMPLATE
template ITPP_EXPORT sparse_cvec
elem_mult_s(
const sparse_cvec &,
const cvec &);
1268 ITPP_EXPORT_TEMPLATE
template ITPP_EXPORT ivec
elem_mult(
const ivec &,
const sparse_ivec &);
1269 ITPP_EXPORT_TEMPLATE
template ITPP_EXPORT vec
elem_mult(
const vec &,
const sparse_vec &);
1270 ITPP_EXPORT_TEMPLATE
template ITPP_EXPORT cvec
elem_mult(
const cvec &,
const sparse_cvec &);
1272 ITPP_EXPORT_TEMPLATE
template ITPP_EXPORT sparse_ivec
elem_mult_s(
const ivec &,
const sparse_ivec &);
1273 ITPP_EXPORT_TEMPLATE
template ITPP_EXPORT sparse_vec
elem_mult_s(
const vec &,
const sparse_vec &);
1274 ITPP_EXPORT_TEMPLATE
template ITPP_EXPORT sparse_cvec
elem_mult_s(
const cvec &,
const sparse_cvec &);
1280 #endif // #ifndef SVEC_H