This file is indexed.

/usr/include/itpp/base/svec.h is in libitpp-dev 4.3.1-8.

This file is owned by root:root, with mode 0o644.

The actual contents of the file can be viewed below.

   1
   2
   3
   4
   5
   6
   7
   8
   9
  10
  11
  12
  13
  14
  15
  16
  17
  18
  19
  20
  21
  22
  23
  24
  25
  26
  27
  28
  29
  30
  31
  32
  33
  34
  35
  36
  37
  38
  39
  40
  41
  42
  43
  44
  45
  46
  47
  48
  49
  50
  51
  52
  53
  54
  55
  56
  57
  58
  59
  60
  61
  62
  63
  64
  65
  66
  67
  68
  69
  70
  71
  72
  73
  74
  75
  76
  77
  78
  79
  80
  81
  82
  83
  84
  85
  86
  87
  88
  89
  90
  91
  92
  93
  94
  95
  96
  97
  98
  99
 100
 101
 102
 103
 104
 105
 106
 107
 108
 109
 110
 111
 112
 113
 114
 115
 116
 117
 118
 119
 120
 121
 122
 123
 124
 125
 126
 127
 128
 129
 130
 131
 132
 133
 134
 135
 136
 137
 138
 139
 140
 141
 142
 143
 144
 145
 146
 147
 148
 149
 150
 151
 152
 153
 154
 155
 156
 157
 158
 159
 160
 161
 162
 163
 164
 165
 166
 167
 168
 169
 170
 171
 172
 173
 174
 175
 176
 177
 178
 179
 180
 181
 182
 183
 184
 185
 186
 187
 188
 189
 190
 191
 192
 193
 194
 195
 196
 197
 198
 199
 200
 201
 202
 203
 204
 205
 206
 207
 208
 209
 210
 211
 212
 213
 214
 215
 216
 217
 218
 219
 220
 221
 222
 223
 224
 225
 226
 227
 228
 229
 230
 231
 232
 233
 234
 235
 236
 237
 238
 239
 240
 241
 242
 243
 244
 245
 246
 247
 248
 249
 250
 251
 252
 253
 254
 255
 256
 257
 258
 259
 260
 261
 262
 263
 264
 265
 266
 267
 268
 269
 270
 271
 272
 273
 274
 275
 276
 277
 278
 279
 280
 281
 282
 283
 284
 285
 286
 287
 288
 289
 290
 291
 292
 293
 294
 295
 296
 297
 298
 299
 300
 301
 302
 303
 304
 305
 306
 307
 308
 309
 310
 311
 312
 313
 314
 315
 316
 317
 318
 319
 320
 321
 322
 323
 324
 325
 326
 327
 328
 329
 330
 331
 332
 333
 334
 335
 336
 337
 338
 339
 340
 341
 342
 343
 344
 345
 346
 347
 348
 349
 350
 351
 352
 353
 354
 355
 356
 357
 358
 359
 360
 361
 362
 363
 364
 365
 366
 367
 368
 369
 370
 371
 372
 373
 374
 375
 376
 377
 378
 379
 380
 381
 382
 383
 384
 385
 386
 387
 388
 389
 390
 391
 392
 393
 394
 395
 396
 397
 398
 399
 400
 401
 402
 403
 404
 405
 406
 407
 408
 409
 410
 411
 412
 413
 414
 415
 416
 417
 418
 419
 420
 421
 422
 423
 424
 425
 426
 427
 428
 429
 430
 431
 432
 433
 434
 435
 436
 437
 438
 439
 440
 441
 442
 443
 444
 445
 446
 447
 448
 449
 450
 451
 452
 453
 454
 455
 456
 457
 458
 459
 460
 461
 462
 463
 464
 465
 466
 467
 468
 469
 470
 471
 472
 473
 474
 475
 476
 477
 478
 479
 480
 481
 482
 483
 484
 485
 486
 487
 488
 489
 490
 491
 492
 493
 494
 495
 496
 497
 498
 499
 500
 501
 502
 503
 504
 505
 506
 507
 508
 509
 510
 511
 512
 513
 514
 515
 516
 517
 518
 519
 520
 521
 522
 523
 524
 525
 526
 527
 528
 529
 530
 531
 532
 533
 534
 535
 536
 537
 538
 539
 540
 541
 542
 543
 544
 545
 546
 547
 548
 549
 550
 551
 552
 553
 554
 555
 556
 557
 558
 559
 560
 561
 562
 563
 564
 565
 566
 567
 568
 569
 570
 571
 572
 573
 574
 575
 576
 577
 578
 579
 580
 581
 582
 583
 584
 585
 586
 587
 588
 589
 590
 591
 592
 593
 594
 595
 596
 597
 598
 599
 600
 601
 602
 603
 604
 605
 606
 607
 608
 609
 610
 611
 612
 613
 614
 615
 616
 617
 618
 619
 620
 621
 622
 623
 624
 625
 626
 627
 628
 629
 630
 631
 632
 633
 634
 635
 636
 637
 638
 639
 640
 641
 642
 643
 644
 645
 646
 647
 648
 649
 650
 651
 652
 653
 654
 655
 656
 657
 658
 659
 660
 661
 662
 663
 664
 665
 666
 667
 668
 669
 670
 671
 672
 673
 674
 675
 676
 677
 678
 679
 680
 681
 682
 683
 684
 685
 686
 687
 688
 689
 690
 691
 692
 693
 694
 695
 696
 697
 698
 699
 700
 701
 702
 703
 704
 705
 706
 707
 708
 709
 710
 711
 712
 713
 714
 715
 716
 717
 718
 719
 720
 721
 722
 723
 724
 725
 726
 727
 728
 729
 730
 731
 732
 733
 734
 735
 736
 737
 738
 739
 740
 741
 742
 743
 744
 745
 746
 747
 748
 749
 750
 751
 752
 753
 754
 755
 756
 757
 758
 759
 760
 761
 762
 763
 764
 765
 766
 767
 768
 769
 770
 771
 772
 773
 774
 775
 776
 777
 778
 779
 780
 781
 782
 783
 784
 785
 786
 787
 788
 789
 790
 791
 792
 793
 794
 795
 796
 797
 798
 799
 800
 801
 802
 803
 804
 805
 806
 807
 808
 809
 810
 811
 812
 813
 814
 815
 816
 817
 818
 819
 820
 821
 822
 823
 824
 825
 826
 827
 828
 829
 830
 831
 832
 833
 834
 835
 836
 837
 838
 839
 840
 841
 842
 843
 844
 845
 846
 847
 848
 849
 850
 851
 852
 853
 854
 855
 856
 857
 858
 859
 860
 861
 862
 863
 864
 865
 866
 867
 868
 869
 870
 871
 872
 873
 874
 875
 876
 877
 878
 879
 880
 881
 882
 883
 884
 885
 886
 887
 888
 889
 890
 891
 892
 893
 894
 895
 896
 897
 898
 899
 900
 901
 902
 903
 904
 905
 906
 907
 908
 909
 910
 911
 912
 913
 914
 915
 916
 917
 918
 919
 920
 921
 922
 923
 924
 925
 926
 927
 928
 929
 930
 931
 932
 933
 934
 935
 936
 937
 938
 939
 940
 941
 942
 943
 944
 945
 946
 947
 948
 949
 950
 951
 952
 953
 954
 955
 956
 957
 958
 959
 960
 961
 962
 963
 964
 965
 966
 967
 968
 969
 970
 971
 972
 973
 974
 975
 976
 977
 978
 979
 980
 981
 982
 983
 984
 985
 986
 987
 988
 989
 990
 991
 992
 993
 994
 995
 996
 997
 998
 999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
/*!
 * \file
 * \brief Sparse Vector Class definitions
 * \author Tony Ottosson and Tobias Ringstrom
 *
 * -------------------------------------------------------------------------
 *
 * Copyright (C) 1995-2010  (see AUTHORS file for a list of contributors)
 *
 * This file is part of IT++ - a C++ library of mathematical, signal
 * processing, speech processing, and communications classes and functions.
 *
 * IT++ is free software: you can redistribute it and/or modify it under the
 * terms of the GNU General Public License as published by the Free Software
 * Foundation, either version 3 of the License, or (at your option) any
 * later version.
 *
 * IT++ is distributed in the hope that it will be useful, but WITHOUT ANY
 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
 * details.
 *
 * You should have received a copy of the GNU General Public License along
 * with IT++.  If not, see <http://www.gnu.org/licenses/>.
 *
 * -------------------------------------------------------------------------
 */

#ifndef SVEC_H
#define SVEC_H

#include <itpp/base/vec.h>
#include <itpp/base/math/min_max.h>
#include <cstdlib>
#include <itpp/itexports.h>

namespace itpp
{

// Declaration of class Vec
template <class T> class Vec;
// Declaration of class Sparse_Vec
template <class T> class Sparse_Vec;

// ----------------------- Sparse_Vec Friends -------------------------------

//! v1+v2 where v1 and v2 are sparse vector
template <class T>
Sparse_Vec<T> operator+(const Sparse_Vec<T> &v1, const Sparse_Vec<T> &v2);

//! v1*v2 where v1 and v2 are sparse vectors
template <class T>
T operator*(const Sparse_Vec<T> &v1, const Sparse_Vec<T> &v2);

//! v1*v2 where v1 is a sparse vector and v2 is a dense vector
template <class T>
T operator*(const Sparse_Vec<T> &v1, const Vec<T> &v2);

//! v1*v2 where v1 is a dense vector and v2 is a sparse vector
template <class T>
T operator*(const Vec<T> &v1, const Sparse_Vec<T> &v2);

//! Elementwise multiplication of two sparse vectors returning a sparse vector
template <class T>
Sparse_Vec<T> elem_mult(const Sparse_Vec<T> &v1, const Sparse_Vec<T> &v2);

//! Elementwise multiplication of a sparse vector and a dense vector returning a dense vector
template <class T>
Vec<T> elem_mult(const Sparse_Vec<T> &v1, const Vec<T> &v2);

//! Elementwise multiplication of a sparse vector and a dense vector returning a sparse vector
template <class T>
Sparse_Vec<T> elem_mult_s(const Sparse_Vec<T> &v1, const Vec<T> &v2);

//! Elementwise multiplication of a dense vector and a sparse vector returning a dense vector
template <class T>
Vec<T> elem_mult(const Vec<T> &v1, const Sparse_Vec<T> &v2);

//! Elementwise multiplication of a dense vector and a sparse vector returning a sparse vector
template <class T>
Sparse_Vec<T> elem_mult_s(const Vec<T> &v1, const Sparse_Vec<T> &v2);

namespace details
{
	//this template selects appropriate type for Eps value used to remove small elements from
	//sparse containers
	template <typename NumT> struct Sparse_Eps_Type_Selector;
	template <> struct Sparse_Eps_Type_Selector<double> {typedef double eps_type;};
	template <> struct Sparse_Eps_Type_Selector<std::complex<double> > {typedef double eps_type;};
	template <> struct Sparse_Eps_Type_Selector<int> {typedef int eps_type;};
	template <> struct Sparse_Eps_Type_Selector<short> {typedef short eps_type;};
	template <> struct Sparse_Eps_Type_Selector<itpp::bin> {typedef int eps_type;};
}


/*!
  \brief Templated sparse vector class
  \author Tony Ottosson and Tobias Ringstrom

  A sparse vector is a vector where most elements are zero. The
  maximum number of none-zero elements is a parameter to the
  constructor. The elements are stored in random order, i.e. they
  are not sorted.

*/
template <class T>
class Sparse_Vec
{
public:

  //! Default constructor
  Sparse_Vec();

  /*!
    \brief Initiate an empty sparse vector

    \param sz Size of the sparse vector (i.e. maximum index is (\c sz - 1))
    \param data_init Maximum number of non-zero elements in the sparse vector (default value 200)
  */
  Sparse_Vec(int sz, int data_init = 200);

  /*!
    \brief Initiate a new sparse vector.

    \param v The elements of \c v are copied into the new sparse vector
  */
  Sparse_Vec(const Sparse_Vec<T> &v);

  /*!
    \brief Initiate a new sparse vector from a dense vector.

    \param v The elements of \c v are copied into the new sparse vector
  */
  Sparse_Vec(const Vec<T> &v);

  /*!
    \brief Initiate a new sparse vector from a dense vector. Elements of \c v larger than \c epsilon are copied into the new sparse vector.

    \note If the type T is complex<double>, then the elements of \c v larger than \c abs(epsilon) are copied into the new sparse vector.
  */
  Sparse_Vec(const Vec<T> &v, T epsilon);

  //! Destructor
  ~Sparse_Vec();

  /*!
    \brief Set the size \c sz of the sparse vector. Default value \c data_init=-1 \c => allocated size for the data is not changed.

    \param sz Size of the sparse vector (i.e. maximum index is (\c sz - 1))
    \param data_init Maximum number of non-zero elements in the sparse vector (default value -1 \c => allocated size for the data is not changed)
  */
  void set_size(int sz, int data_init = -1);

  //! Returns the size of the sparse vector
  int size() const { return v_size; }

  //! Number of non-zero elements in the sparse vector
  inline int nnz() {
    if (check_small_elems_flag) {
      remove_small_elements();
    }
    return used_size;
  }

  //! Returns the density of the sparse vector: (number of non-zero elements)/(size of the vector)
  double density();

  //! Set that all elements smaller than \a epsilon should be set to zero.
  void set_small_element(const T& epsilon);

  /*!
    Removes all elements that are smaller than \a epsilon from the non-zero elements.

    \note The small element \a epsilon can be set by the member function set_small_element. If no small value is set, the default value is always \c epsilon=0.
  */
  void remove_small_elements();

  /*!
    \brief Set the maximum number of non-zero elements to \c new_size

    \param new_size The new maximum number of non-zero elements.
  */
  void resize_data(int new_size);

  //! Set the maximum number of non-zero elements equal to the actual number of non-zero elements
  void compact();

  //! Returns a full, dense vector in \c v
  void full(Vec<T> &v) const;

  //! Returns a full, dense vector
  Vec<T> full() const;

  //! Returns the element with index \c i
  T operator()(int i) const;

  //! Set element \c i equal to \c v
  void set(int i, T v);

  //! Set the elements of the sparse vector with indices \c index_vec to the values in \c v
  void set(const ivec &index_vec, const Vec<T> &v);

  //! Set a new element with index \c i equal to \c v
  void set_new(int i, T v);

  //! Set new elements with indices \c index_vec equal to the values in \c v (no check whether the same index is used several times)
  void set_new(const ivec &index_vec, const Vec<T> &v);

  //! Add element \c i with \c v
  void add_elem(const int i, const T v);

  //! Add \c v to the elements specified by \c index_vec with \c v
  void add(const ivec &index_vec, const Vec<T> &v);

  //! Set the sparse vector to the all zero vector (removes all non-zero elements)
  void zeros();

  //! Set the i-th element to zero (i.e. clear that element if it contains a non-zero value)
  void zero_elem(const int i);

  //! Clear all non-zero elements of the sparse vector
  void clear();

  //! Clear the i-th element (if it contains a non-zero value)
  void clear_elem(const int i);

  /*!
    \brief Extract the reference to the p-th non-zero data element
  */
  inline void get_nz_data(int p, T& data_out) {
    if (check_small_elems_flag) {
      remove_small_elements();
    }
    data_out = data[p];
  }

  //! Returns the p-th non-zero data element
  inline T get_nz_data(int p) {
    if (check_small_elems_flag) {
      remove_small_elements();
    }
    return data[p];
  }

  //! Returns the vector index of the p-th non-zero element
  inline int get_nz_index(int p) {
    if (check_small_elems_flag) {
      remove_small_elements();
    }
    return index[p];
  }

  //! Returns the p-th non-zero value in \c dat and the corresponding vector index in \c idx
  inline void get_nz(int p, int &idx, T &dat) {
    if (check_small_elems_flag) {
      remove_small_elements();
    }
    idx = index[p];
    dat = data[p];
  }

  //! Return the indices of non-zero values
  ivec get_nz_indices();

  //! Return sparse subvector from index \c i1 to index \c i2
  Sparse_Vec<T> get_subvector(int i1, int i2) const;

  //! Returns the sum of all values squared
  T sqr() const;

  //! Assign sparse vector the value and length of the sparse vector \c v
  void operator=(const Sparse_Vec<T> &v);
  //! Assign sparse vector the value and length of the dense vector \c v
  void operator=(const Vec<T> &v);

  //! Returns the sign inverse of all elements in the sparse vector
  Sparse_Vec<T> operator-() const;

  //! Compare two sparse vectors. False if wrong sizes or different values
  bool operator==(const Sparse_Vec<T> &v);

  //! Add sparse vector \c v to all non-zero elements of the sparse vector
  void operator+=(const Sparse_Vec<T> &v);

  //! Add vector \c v to all non-zero elements of the sparse vector
  void operator+=(const Vec<T> &v);

  //! Subtract sparse vector \c v from all non-zero elements of the sparse vector
  void operator-=(const Sparse_Vec<T> &v);

  //! Subtract vector \c v from all non-zero elements of the sparse vector
  void operator-=(const Vec<T> &v);

  //! Multiply the scalar \c v to all non-zero elements of the sparse vector
  void operator*=(const T &v);

  //! Divide all non-zero elements of the sparse vector with the scalar \c v
  void operator/=(const T &v);

  //! Addition v1+v2 where v1 and v2 are sparse vector
  friend Sparse_Vec<T> operator+<>(const Sparse_Vec<T> &v1, const Sparse_Vec<T> &v2);
  //! Scalar product v1*v2 where v1 and v2 are sparse vectors
  friend T operator*<>(const Sparse_Vec<T> &v1, const Sparse_Vec<T> &v2);
  //! Scalar product v1*v2 where v1 is a sparse vector and v2 is a dense vector
  friend T operator*<>(const Sparse_Vec<T> &v1, const Vec<T> &v2);
  //! Scalar product v1*v2 where v1 is a dense vector and v2 is a sparse vector
  friend T operator*<>(const Vec<T> &v1, const Sparse_Vec<T> &v2);

  //! Element wise multiplication of two sparse vectors
  friend Sparse_Vec<T> elem_mult <>(const Sparse_Vec<T> &v1, const Sparse_Vec<T> &v2);

  //! Element wise multiplication of a sparse vector and a dense vector
  friend Vec<T> elem_mult <>(const Sparse_Vec<T> &v1, const Vec<T> &v2);

  //! Element wise multiplication of a sparse vector and a dense vector returning a sparse vector
  friend Sparse_Vec<T> elem_mult_s <>(const Sparse_Vec<T> &v1, const Vec<T> &v2);

  //! Element wise multiplication of a a dense vector and a sparse vector
  friend Vec<T> elem_mult <>(const Vec<T> &v1, const Sparse_Vec<T> &v2);

  //! Element wise multiplication of a a dense vector and a sparse vector returning a sparse vector
  friend Sparse_Vec<T> elem_mult_s <>(const Vec<T> &v1, const Sparse_Vec<T> &v2);

private:
  void init();
  void alloc();
  void free();

  int v_size, used_size, data_size;
  T *data;
  int *index;
  typename details::Sparse_Eps_Type_Selector<T>::eps_type eps;
  bool check_small_elems_flag;
};


/*!
  \relates Sparse_Vec
  \brief Type definition of an integer sparse vector
*/
typedef Sparse_Vec<int> sparse_ivec;

/*!
  \relates Sparse_Vec
  \brief Type definition of a double sparse vector
*/
typedef Sparse_Vec<double> sparse_vec;

/*!
  \relates Sparse_Vec
  \brief Type definition of a complex<double> sparse vector
*/
typedef Sparse_Vec<std::complex<double> > sparse_cvec;

// ----------------------- Implementation starts here --------------------------------

template <class T>
void Sparse_Vec<T>::init()
{
  v_size = 0;
  used_size = 0;
  data_size = 0;
  data = 0;
  index = 0;
  eps = 0;
  check_small_elems_flag = true;
}

template <class T>
void Sparse_Vec<T>::alloc()
{
  if (data_size != 0) {
    data = new T[data_size];
    index = new int[data_size];
  }
}

template <class T>
void Sparse_Vec<T>::free()
{
  delete [] data;
  data = 0;
  delete [] index;
  index = 0;
}

template <class T>
Sparse_Vec<T>::Sparse_Vec()
{
  init();
}

template <class T>
Sparse_Vec<T>::Sparse_Vec(int sz, int data_init)
{
  init();
  v_size = sz;
  used_size = 0;
  data_size = data_init;
  alloc();
}

template <class T>
Sparse_Vec<T>::Sparse_Vec(const Sparse_Vec<T> &v)
{
  init();
  v_size = v.v_size;
  used_size = v.used_size;
  data_size = v.data_size;
  eps = v.eps;
  check_small_elems_flag = v.check_small_elems_flag;
  alloc();

  for (int i = 0; i < used_size; i++) {
    data[i] = v.data[i];
    index[i] = v.index[i];
  }
}

template <class T>
Sparse_Vec<T>::Sparse_Vec(const Vec<T> &v)
{
  init();
  v_size = v.size();
  used_size = 0;
  data_size = std::min(v.size(), 10000);
  alloc();

  for (int i = 0; i < v_size; i++) {
    if (v(i) != T(0)) {
      if (used_size == data_size)
        resize_data(data_size*2);
      data[used_size] = v(i);
      index[used_size] = i;
      used_size++;
    }
  }
  compact();
}

template <class T>
Sparse_Vec<T>::Sparse_Vec(const Vec<T> &v, T epsilon)
{
  init();
  v_size = v.size();
  used_size = 0;
  data_size = std::min(v.size(), 10000);
  eps = std::abs(epsilon);
  alloc();

  for (int i = 0; i < v_size; i++) {
    if (std::abs(v(i)) > eps) {
      if (used_size == data_size)
        resize_data(data_size*2);
      data[used_size] = v(i);
      index[used_size] = i;
      used_size++;
    }
  }
  compact();
}

template <class T>
Sparse_Vec<T>::~Sparse_Vec()
{
  free();
}

template <class T>
void Sparse_Vec<T>::set_size(int new_size, int data_init)
{
  v_size = new_size;
  used_size = 0;
  if (data_init != -1) {
    free();
    data_size = data_init;
    alloc();
  }
}

template <class T>
double Sparse_Vec<T>::density()
{
  if (check_small_elems_flag) {
    remove_small_elements();
  }
  //return static_cast<double>(used_size) / v_size;
  return double(used_size) / v_size;
}

template <class T>
void Sparse_Vec<T>::set_small_element(const T& epsilon)
{
  eps = std::abs(epsilon);
  remove_small_elements();
}

template <class T>
void Sparse_Vec<T>::remove_small_elements()
{
  int i;
  int nrof_removed_elements = 0;

  //Remove small elements
  for (i = 0;i < used_size;i++) {
    if (std::abs(data[i]) <= eps) {
      nrof_removed_elements++;
    }
    else if (nrof_removed_elements > 0) {
      data[i-nrof_removed_elements] = data[i];
      index[i-nrof_removed_elements] = index[i];
    }
  }

  //Set new size after small elements have been removed
  used_size -= nrof_removed_elements;

  //Set the flag to indicate that all small elements have been removed
  check_small_elems_flag = false;
}


template <class T>
void Sparse_Vec<T>::resize_data(int new_size)
{
  it_assert(new_size >= used_size, "Sparse_Vec<T>::resize_data(int new_size): New size is to small");

  if (new_size != data_size) {
    if (new_size == 0)
      free();
    else {
      T *tmp_data = data;
      int *tmp_pos = index;
      data_size = new_size;
      alloc();
      for (int p = 0; p < used_size; p++) {
        data[p] = tmp_data[p];
        index[p] = tmp_pos[p];
      }
      delete [] tmp_data;
      delete [] tmp_pos;
    }
  }
}

template <class T>
void Sparse_Vec<T>::compact()
{
  if (check_small_elems_flag) {
    remove_small_elements();
  }
  resize_data(used_size);
}

template <class T>
void Sparse_Vec<T>::full(Vec<T> &v) const
{
  v.set_size(v_size);

  v = T(0);
  for (int p = 0; p < used_size; p++)
    v(index[p]) = data[p];
}

template <class T>
Vec<T> Sparse_Vec<T>::full() const
{
  Vec<T> r(v_size);
  full(r);
  return r;
}

// This is slow. Implement a better search
template <class T>
T Sparse_Vec<T>::operator()(int i) const
{
  it_assert_debug(i >= 0 && i < v_size, "The index of the element is out of range");

  bool found = false;
  int p;
  for (p = 0; p < used_size; p++) {
    if (index[p] == i) {
      found = true;
      break;
    }
  }
  return found ? data[p] : T(0);
}

template <class T>
void Sparse_Vec<T>::set(int i, T v)
{
  it_assert_debug(i >= 0 && i < v_size, "The index of the element is out of range");

  bool found = false;
  bool larger_than_eps;
  int p;

  for (p = 0; p < used_size; p++) {
    if (index[p] == i) {
      found = true;
      break;
    }
  }

  larger_than_eps = (std::abs(v) > eps);

  if (found && larger_than_eps)
    data[p] = v;
  else if (larger_than_eps) {
    if (used_size == data_size)
      resize_data(data_size*2 + 100);
    data[used_size] = v;
    index[used_size] = i;
    used_size++;
  }

  //Check if the stored element is smaller than eps. In that case it should be removed.
  if (std::abs(v) <= eps) {
    remove_small_elements();
  }

}

template <class T>
void Sparse_Vec<T>::set_new(int i, T v)
{
  it_assert_debug(v_size > i, "The index of the element exceeds the size of the sparse vector");

  //Check that the new element is larger than eps!
  if (std::abs(v) > eps) {
    if (used_size == data_size)
      resize_data(data_size*2 + 100);
    data[used_size] = v;
    index[used_size] = i;
    used_size++;
  }
}

template <class T>
void Sparse_Vec<T>::add_elem(const int i, const T v)
{
  bool found = false;
  int p;

  it_assert_debug(v_size > i, "The index of the element exceeds the size of the sparse vector");

  for (p = 0; p < used_size; p++) {
    if (index[p] == i) {
      found = true;
      break;
    }
  }
  if (found)
    data[p] += v;
  else {
    if (used_size == data_size)
      resize_data(data_size*2 + 100);
    data[used_size] = v;
    index[used_size] = i;
    used_size++;
  }

  check_small_elems_flag = true;

}

template <class T>
void Sparse_Vec<T>::add(const ivec& index_vec, const Vec<T>& v)
{
  bool found = false;
  int i, p, q;
  int nrof_nz = v.size();

  it_assert_debug(v_size > max(index_vec), "The indices exceeds the size of the sparse vector");

  //Elements are added if they have identical indices
  for (q = 0; q < nrof_nz; q++) {
    i = index_vec(q);
    for (p = 0; p < used_size; p++) {
      if (index[p] == i) {
        found = true;
        break;
      }
    }
    if (found)
      data[p] += v(q);
    else {
      if (used_size == data_size)
        resize_data(data_size*2 + 100);
      data[used_size] = v(q);
      index[used_size] = i;
      used_size++;
    }
    found = false;
  }

  check_small_elems_flag = true;

}

template <class T>
void Sparse_Vec<T>::zeros()
{
  used_size = 0;
  check_small_elems_flag = false;
}

template <class T>
void Sparse_Vec<T>::zero_elem(const int i)
{
  bool found = false;
  int p;

  it_assert_debug(v_size > i, "The index of the element exceeds the size of the sparse vector");

  for (p = 0; p < used_size; p++) {
    if (index[p] == i) {
      found = true;
      break;
    }
  }
  if (found) {
    data[p] = data[used_size-1];
    index[p] = index[used_size-1];
    used_size--;
  }
}

template <class T>
void Sparse_Vec<T>::clear()
{
  used_size = 0;
  check_small_elems_flag = false;
}

template <class T>
void Sparse_Vec<T>::clear_elem(const int i)
{
  bool found = false;
  int p;

  it_assert_debug(v_size > i, "The index of the element exceeds the size of the sparse vector");

  for (p = 0; p < used_size; p++) {
    if (index[p] == i) {
      found = true;
      break;
    }
  }
  if (found) {
    data[p] = data[used_size-1];
    index[p] = index[used_size-1];
    used_size--;
  }
}

template <class T>
void Sparse_Vec<T>::set(const ivec& index_vec, const Vec<T>& v)
{
  it_assert_debug(v_size > max(index_vec), "The indices exceeds the size of the sparse vector");

  //Clear all old non-zero elements
  clear();

  //Add the new non-zero elements
  add(index_vec, v);
}

template <class T>
void Sparse_Vec<T>::set_new(const ivec& index_vec, const Vec<T>& v)
{
  int q;
  int nrof_nz = v.size();

  it_assert_debug(v_size > max(index_vec), "The indices exceeds the size of the sparse vector");

  //Clear all old non-zero elements
  clear();

  for (q = 0; q < nrof_nz; q++) {
    if (std::abs(v[q]) > eps) {
      if (used_size == data_size)
        resize_data(data_size*2 + 100);
      data[used_size] = v(q);
      index[used_size] = index_vec(q);
      used_size++;
    }
  }
}

template <class T>
ivec Sparse_Vec<T>::get_nz_indices()
{
  int n = nnz();
  ivec r(n);
  for (int i = 0; i < n; i++) {
    r(i) = get_nz_index(i);
  }
  return r;
}

template <class T>
Sparse_Vec<T> Sparse_Vec<T>::get_subvector(int i1, int i2) const
{
  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");

  Sparse_Vec<T> r(i2 - i1 + 1);

  for (int p = 0; p < used_size; p++) {
    if (index[p] >= i1 && index[p] <= i2) {
      if (r.used_size == r.data_size)
        r.resize_data(r.data_size*2 + 100);
      r.data[r.used_size] = data[p];
      r.index[r.used_size] = index[p] - i1;
      r.used_size++;
    }
  }
  r.eps = eps;
  r.check_small_elems_flag = check_small_elems_flag;
  r.compact();

  return r;
}

template <class T>
T Sparse_Vec<T>::sqr() const
{
  T sum(0);
  for (int p = 0; p < used_size; p++)
    sum += data[p] * data[p];

  return sum;
}

template <class T>
void Sparse_Vec<T>::operator=(const Sparse_Vec<T> &v)
{
  free();
  v_size = v.v_size;
  used_size = v.used_size;
  data_size = v.data_size;
  eps = v.eps;
  check_small_elems_flag = v.check_small_elems_flag;
  alloc();

  for (int i = 0; i < used_size; i++) {
    data[i] = v.data[i];
    index[i] = v.index[i];
  }
}

template <class T>
void Sparse_Vec<T>::operator=(const Vec<T> &v)
{
  free();
  v_size = v.size();
  used_size = 0;
  data_size = std::min(v.size(), 10000);
  eps = std::abs(T(0));
  check_small_elems_flag = false;
  alloc();

  for (int i = 0; i < v_size; i++) {
    if (v(i) != T(0)) {
      if (used_size == data_size)
        resize_data(data_size*2);
      data[used_size] = v(i);
      index[used_size] = i;
      used_size++;
    }
  }
  compact();
}

template <class T>
Sparse_Vec<T> Sparse_Vec<T>::operator-() const
{
  Sparse_Vec r(v_size, used_size);

  for (int p = 0; p < used_size; p++) {
    r.data[p] = -data[p];
    r.index[p] = index[p];
  }
  r.used_size = used_size;

  return r;
}

template <class T>
bool Sparse_Vec<T>::operator==(const Sparse_Vec<T> &v)
{
  int p, q;
  bool found = false;

  //Remove small elements before comparing the two sparse_vectors
  if (check_small_elems_flag)
    remove_small_elements();

  if (v_size != v.v_size) {
    //Return false if vector sizes are unequal
    return false;
  }
  else {
    for (p = 0;p < used_size;p++) {
      for (q = 0;q < v.used_size;q++) {
        if (index[p] == v.index[q]) {
          found = true;
          break;
        }
      }
      if (found == false)
        //Return false if non-zero element not found, or if elements are unequal
        return false;
      else if (data[p] != v.data[q])
        //Return false if non-zero element not found, or if elements are unequal
        return false;
      else
        //Check next non-zero element
        found = false;
    }
  }

  /*Special handling if sizes do not match.
  Required since v may need to do remove_small_elements() for true comparison*/
  if (used_size != v.used_size) {
    if (used_size > v.used_size) {
      //Return false if number of non-zero elements is less in v
      return false;
    }
    else {
      //Ensure that the remaining non-zero elements in v are smaller than v.eps
      int nrof_small_elems = 0;
      for (q = 0;q < v.used_size;q++) {
        if (std::abs(v.data[q]) <= v.eps)
          nrof_small_elems++;
      }
      if (v.used_size - nrof_small_elems != used_size)
        //Return false if the number of "true" non-zero elements are unequal
        return false;
    }
  }

  //All elements checks => return true
  return true;
}

template <class T>
void Sparse_Vec<T>::operator+=(const Sparse_Vec<T> &v)
{
  int i, p;
  T tmp_data;
  int nrof_nz_v = v.used_size;

  it_assert_debug(v_size == v.size(), "Attempted addition of unequal sized sparse vectors");

  for (p = 0; p < nrof_nz_v; p++) {
    i = v.index[p];
    tmp_data = v.data[p];
    //get_nz(p,i,tmp_data);
    add_elem(i, tmp_data);
  }

  check_small_elems_flag = true;
}

template <class T>
void Sparse_Vec<T>::operator+=(const Vec<T> &v)
{
  int i;

  it_assert_debug(v_size == v.size(), "Attempted addition of unequal sized sparse vectors");

  for (i = 0; i < v.size(); i++)
    if (v(i) != T(0))
      add_elem(i, v(i));

  check_small_elems_flag = true;
}


template <class T>
void Sparse_Vec<T>::operator-=(const Sparse_Vec<T> &v)
{
  int i, p;
  T tmp_data;
  int nrof_nz_v = v.used_size;

  it_assert_debug(v_size == v.size(), "Attempted subtraction of unequal sized sparse vectors");

  for (p = 0; p < nrof_nz_v; p++) {
    i = v.index[p];
    tmp_data = v.data[p];
    //v.get_nz(p,i,tmp_data);
    add_elem(i, -tmp_data);
  }

  check_small_elems_flag = true;
}

template <class T>
void Sparse_Vec<T>::operator-=(const Vec<T> &v)
{
  int i;

  it_assert_debug(v_size == v.size(), "Attempted subtraction of unequal sized sparse vectors");

  for (i = 0; i < v.size(); i++)
    if (v(i) != T(0))
      add_elem(i, -v(i));

  check_small_elems_flag = true;
}

template <class T>
void Sparse_Vec<T>::operator*=(const T &v)
{
  int p;

  for (p = 0; p < used_size; p++) {
    data[p] *= v;
  }

  check_small_elems_flag = true;
}

template <class T>
void Sparse_Vec<T>::operator/=(const T &v)
{
  int p;
  for (p = 0; p < used_size; p++) {
    data[p] /= v;
  }

  if (eps > 0) {
    check_small_elems_flag = true;
  }
}

template <class T>
T operator*(const Sparse_Vec<T> &v1, const Sparse_Vec<T> &v2)
{
  it_assert_debug(v1.v_size == v2.v_size, "Sparse_Vec<T> * Sparse_Vec<T>");

  T sum(0);
  Vec<T> v1f(v1.v_size);
  v1.full(v1f);
  for (int p = 0; p < v2.used_size; p++) {
    if (v1f[v2.index[p]] != T(0))
      sum += v1f[v2.index[p]] * v2.data[p];
  }

  return sum;
}

template <class T>
T operator*(const Sparse_Vec<T> &v1, const Vec<T> &v2)
{
  it_assert_debug(v1.size() == v2.size(), "Multiplication of unequal sized vectors attempted");

  T sum(0);
  for (int p1 = 0; p1 < v1.used_size; p1++)
    sum += v1.data[p1] * v2[v1.index[p1]];

  return sum;
}

template <class T>
T operator*(const Vec<T> &v1, const Sparse_Vec<T> &v2)
{
  it_assert_debug(v1.size() == v2.size(), "Multiplication of unequal sized vectors attempted");

  T sum(0);
  for (int p2 = 0; p2 < v2.used_size; p2++)
    sum += v1[v2.index[p2]] * v2.data[p2];

  return sum;
}

template <class T>
Sparse_Vec<T> elem_mult(const Sparse_Vec<T> &v1, const Sparse_Vec<T> &v2)
{
  it_assert_debug(v1.v_size == v2.v_size, "elem_mult(Sparse_Vec<T>, Sparse_Vec<T>)");

  Sparse_Vec<T> r(v1.v_size);
  ivec pos(v1.v_size);
  pos = -1;
  for (int p1 = 0; p1 < v1.used_size; p1++)
    pos[v1.index[p1]] = p1;
  for (int p2 = 0; p2 < v2.used_size; p2++) {
    if (pos[v2.index[p2]] != -1) {
      if (r.used_size == r.data_size)
        r.resize_data(r.used_size*2 + 100);
      r.data[r.used_size] = v1.data[pos[v2.index[p2]]] * v2.data[p2];
      r.index[r.used_size] = v2.index[p2];
      r.used_size++;
    }
  }
  r.compact();

  return r;
}

template <class T>
Vec<T> elem_mult(const Sparse_Vec<T> &v1, const Vec<T> &v2)
{
  it_assert_debug(v1.v_size == v2.size(), "elem_mult(Sparse_Vec<T>, Vec<T>)");

  Vec<T> r(v1.v_size);
  r = T(0);
  for (int p1 = 0; p1 < v1.used_size; p1++)
    r[v1.index[p1]] = v1.data[p1] * v2[v1.index[p1]];

  return r;
}

template <class T>
Sparse_Vec<T> elem_mult_s(const Sparse_Vec<T> &v1, const Vec<T> &v2)
{
  it_assert_debug(v1.v_size == v2.size(), "elem_mult(Sparse_Vec<T>, Vec<T>)");

  Sparse_Vec<T> r(v1.v_size);
  for (int p1 = 0; p1 < v1.used_size; p1++) {
    if (v2[v1.index[p1]] != T(0)) {
      if (r.used_size == r.data_size)
        r.resize_data(r.used_size*2 + 100);
      r.data[r.used_size] = v1.data[p1] * v2[v1.index[p1]];
      r.index[r.used_size] = v1.index[p1];
      r.used_size++;
    }
  }
  r.compact();

  return r;
}

template <class T>
Vec<T> elem_mult(const Vec<T> &v1, const Sparse_Vec<T> &v2)
{
  it_assert_debug(v1.size() == v2.v_size, "elem_mult(Vec<T>, Sparse_Vec<T>)");

  Vec<T> r(v2.v_size);
  r = T(0);
  for (int p2 = 0; p2 < v2.used_size; p2++)
    r[v2.index[p2]] = v1[v2.index[p2]] * v2.data[p2];

  return r;
}

template <class T>
Sparse_Vec<T> elem_mult_s(const Vec<T> &v1, const Sparse_Vec<T> &v2)
{
  it_assert_debug(v1.size() == v2.v_size, "elem_mult(Vec<T>, Sparse_Vec<T>)");

  Sparse_Vec<T> r(v2.v_size);
  for (int p2 = 0; p2 < v2.used_size; p2++) {
    if (v1[v2.index[p2]] != T(0)) {
      if (r.used_size == r.data_size)
        r.resize_data(r.used_size*2 + 100);
      r.data[r.used_size] = v1[v2.index[p2]] * v2.data[p2];
      r.index[r.used_size] = v2.index[p2];
      r.used_size++;
    }
  }
  r.compact();

  return r;
}

template <class T>
Sparse_Vec<T> operator+(const Sparse_Vec<T> &v1, const Sparse_Vec<T> &v2)
{
  it_assert_debug(v1.v_size == v2.v_size, "Sparse_Vec<T> + Sparse_Vec<T>");

  Sparse_Vec<T> r(v1);
  ivec pos(v1.v_size);
  pos = -1;
  for (int p1 = 0; p1 < v1.used_size; p1++)
    pos[v1.index[p1]] = p1;
  for (int p2 = 0; p2 < v2.used_size; p2++) {
    if (pos[v2.index[p2]] == -1) {// A new entry
      if (r.used_size == r.data_size)
        r.resize_data(r.used_size*2 + 100);
      r.data[r.used_size] = v2.data[p2];
      r.index[r.used_size] = v2.index[p2];
      r.used_size++;
    }
    else
      r.data[pos[v2.index[p2]]] += v2.data[p2];
  }
  r.check_small_elems_flag = true;  // added dec 7, 2006
  r.compact();

  return r;
}

//! Convert a dense vector \c v into its sparse representation
template <class T>
inline Sparse_Vec<T> sparse(const Vec<T> &v)
{
  Sparse_Vec<T> s(v);
  return s;
}

//! Convert a dense vector \c v into its sparse representation
template <class T>
inline Sparse_Vec<T> sparse(const Vec<T> &v, T epsilon)
{
  Sparse_Vec<T> s(v, epsilon);
  return s;
}

//! Convert a sparse vector \c s into its dense representation
template <class T>
inline Vec<T> full(const Sparse_Vec<T> &s)
{
  Vec<T> v;
  s.full(v);
  return v;
}

//! \cond

// ---------------------------------------------------------------------
// Instantiations
// ---------------------------------------------------------------------

ITPP_EXPORT_TEMPLATE template class ITPP_EXPORT Sparse_Vec<int>;
ITPP_EXPORT_TEMPLATE template class ITPP_EXPORT Sparse_Vec<double>;
ITPP_EXPORT_TEMPLATE template class ITPP_EXPORT Sparse_Vec<std::complex<double> >;

ITPP_EXPORT_TEMPLATE template ITPP_EXPORT sparse_ivec operator+(const sparse_ivec &,
                                        const sparse_ivec &);
ITPP_EXPORT_TEMPLATE template ITPP_EXPORT sparse_vec operator+(const sparse_vec &,
                                       const sparse_vec &);
ITPP_EXPORT_TEMPLATE template ITPP_EXPORT sparse_cvec operator+(const sparse_cvec &,
                                        const sparse_cvec &);

ITPP_EXPORT_TEMPLATE template ITPP_EXPORT int operator*(const sparse_ivec &, const sparse_ivec &);
ITPP_EXPORT_TEMPLATE template ITPP_EXPORT double operator*(const sparse_vec &, const sparse_vec &);
ITPP_EXPORT_TEMPLATE template ITPP_EXPORT std::complex<double> operator*(const sparse_cvec &,
    const sparse_cvec &);

ITPP_EXPORT_TEMPLATE template ITPP_EXPORT int operator*(const sparse_ivec &, const ivec &);
ITPP_EXPORT_TEMPLATE template ITPP_EXPORT double operator*(const sparse_vec &, const vec &);
ITPP_EXPORT_TEMPLATE template ITPP_EXPORT std::complex<double> operator*(const sparse_cvec &,
    const cvec &);

ITPP_EXPORT_TEMPLATE template ITPP_EXPORT int operator*(const ivec &, const sparse_ivec &);
ITPP_EXPORT_TEMPLATE template ITPP_EXPORT double operator*(const vec &, const sparse_vec &);
ITPP_EXPORT_TEMPLATE template ITPP_EXPORT std::complex<double> operator*(const cvec &,
    const sparse_cvec &);

ITPP_EXPORT_TEMPLATE template ITPP_EXPORT sparse_ivec elem_mult(const sparse_ivec &,
                                        const sparse_ivec &);
ITPP_EXPORT_TEMPLATE template ITPP_EXPORT sparse_vec elem_mult(const sparse_vec &, const sparse_vec &);
ITPP_EXPORT_TEMPLATE template ITPP_EXPORT sparse_cvec elem_mult(const sparse_cvec &,
                                        const sparse_cvec &);

ITPP_EXPORT_TEMPLATE template ITPP_EXPORT ivec elem_mult(const sparse_ivec &, const ivec &);
ITPP_EXPORT_TEMPLATE template ITPP_EXPORT vec elem_mult(const sparse_vec &, const vec &);
ITPP_EXPORT_TEMPLATE template ITPP_EXPORT cvec elem_mult(const sparse_cvec &, const cvec &);

ITPP_EXPORT_TEMPLATE template ITPP_EXPORT sparse_ivec elem_mult_s(const sparse_ivec &, const ivec &);
ITPP_EXPORT_TEMPLATE template ITPP_EXPORT sparse_vec elem_mult_s(const sparse_vec &, const vec &);
ITPP_EXPORT_TEMPLATE template ITPP_EXPORT sparse_cvec elem_mult_s(const sparse_cvec &, const cvec &);

ITPP_EXPORT_TEMPLATE template ITPP_EXPORT ivec elem_mult(const ivec &, const sparse_ivec &);
ITPP_EXPORT_TEMPLATE template ITPP_EXPORT vec elem_mult(const vec &, const sparse_vec &);
ITPP_EXPORT_TEMPLATE template ITPP_EXPORT cvec elem_mult(const cvec &, const sparse_cvec &);

ITPP_EXPORT_TEMPLATE template ITPP_EXPORT sparse_ivec elem_mult_s(const ivec &, const sparse_ivec &);
ITPP_EXPORT_TEMPLATE template ITPP_EXPORT sparse_vec elem_mult_s(const vec &, const sparse_vec &);
ITPP_EXPORT_TEMPLATE template ITPP_EXPORT sparse_cvec elem_mult_s(const cvec &, const sparse_cvec &);

//! \endcond

} // namespace itpp

#endif // #ifndef SVEC_H