File: smat.h

package info (click to toggle)
libitpp 4.0.4-2
  • links: PTS, VCS
  • area: main
  • in suites: lenny
  • size: 7,520 kB
  • ctags: 6,341
  • sloc: cpp: 51,608; sh: 9,248; makefile: 636; fortran: 8
file content (1074 lines) | stat: -rw-r--r-- 31,132 bytes parent folder | download
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
/*!
 * \file
 * \brief Sparse Matrix Class Definitions
 * \author Tony Ottosson and Tobias Ringstrom
 *
 * -------------------------------------------------------------------------
 *
 * IT++ - C++ library of mathematical, signal processing, speech processing,
 *        and communications classes and functions
 *
 * Copyright (C) 1995-2008  (see AUTHORS file for a list of contributors)
 *
 * This program 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 2 of the License, or
 * (at your option) any later version.
 *
 * This program 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 this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
 *
 * -------------------------------------------------------------------------
 */

#ifndef SMAT_H
#define SMAT_H

#ifndef _MSC_VER
#  include <itpp/config.h>
#else
#  include <itpp/config_msvc.h>
#endif

#include <itpp/base/svec.h>


namespace itpp {

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

  // ------------------------ Sparse_Mat Friends -------------------------------------

  //! m1+m2 where m1 and m2 are sparse matrices
  template <class T>
    Sparse_Mat<T> operator+(const Sparse_Mat<T> &m1, const Sparse_Mat<T> &m2);

  //! c*m where c is a scalar and m is a sparse matrix
  template <class T>
    Sparse_Mat<T> operator*(const T &c, const Sparse_Mat<T> &m);

  //! m1*m2 where m1 and m2 are sparse matrices
  template <class T>
    Sparse_Mat<T> operator*(const Sparse_Mat<T> &m1, const Sparse_Mat<T> &m2);

  //! m*v where m is a sparse matrix and v is a sparse vector
  template <class T>
    Sparse_Vec<T> operator*(const Sparse_Mat<T> &m, const Sparse_Vec<T> &v);

  //! m*v where m is a sparse matrix and v is a full column vector
  template <class T>
    Vec<T> operator*(const Sparse_Mat<T> &m, const Vec<T> &v);

  //! v'*m where m is a sparse matrix and v is a full column vector
  template <class T>
    Vec<T> operator*(const Vec<T> &v, const Sparse_Mat<T> &m);

  //! m'*m where m is a sparse matrix
  template <class T>
    Mat<T> trans_mult(const Sparse_Mat<T> &m);

  //! m'*m where m is a sparse matrix
  template <class T>
    Sparse_Mat<T> trans_mult_s(const Sparse_Mat<T> &m);

  //! m1'*m2 where m1 and m2 are sparse matrices
  template <class T>
    Sparse_Mat<T> trans_mult(const Sparse_Mat<T> &m1, const Sparse_Mat<T> &m2);

  //! m'*v where m is a sparse matrix and v is a full column vector
  template <class T>
    Vec<T> trans_mult(const Sparse_Mat<T> &m, const Vec<T> &v);

  //! m1*m2' where m1 and m2 are sparse matrices
  template <class T>
    Sparse_Mat<T> mult_trans(const Sparse_Mat<T> &m1, const Sparse_Mat<T> &m2);

  /*!
    \brief Templated Sparse Matrix Class
    \author Tony Ottosson and Tobias Ringstrom

    A sparse matrix is a matrix where most elements are zero. The
    maximum number of non-zero elements in each column is a parameter
    to the constructor.

    The implementation is based on representing all columns as sparse
    vectors. Thus, column access generally is much faster than row
    access. The elements in each vector are stored in random order,
    i.e. they are not sorted.
  */
  template <class T>
    class Sparse_Mat {
    public:

    //! Default constructor
    Sparse_Mat();

    /*!
      \brief Initiate an empty sparse matrix

      A Sparse_Mat consists of colums that have the type Sparse_Vec. The maximum number of non-zero elements is each column
      is denoted \c row_data_init.

      \param rows Number of rows in the matrix
      \param cols Number of columns in the matrix
      \param row_data_init The maximum number of non-zero elements in each column (default value is 200)
    */
    Sparse_Mat(int rows, int cols, int row_data_init=200);

    //! Initiate a new sparse matrix. The elements of \c m are copied into the new sparse matrix
    Sparse_Mat(const Sparse_Mat<T> &m);

    //! Initiate a new sparse matrix from a dense matrix. The elements of \c m are copied into the new sparse matrix
    Sparse_Mat(const Mat<T> &m);

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

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

    //! Destructor
    ~Sparse_Mat();

    /*!
      \brief Set the size of the sparse matrix

      A Sparse_Mat consists of colums that have the type Sparse_Vec. The maximum number of non-zero elements is each column
      is denoted \c row_data_init, with default value =-1 indicating that the number of data elements is not changed.

      \param rows Number of rows in the matrix
      \param cols Number of columns in the matrix
      \param row_data_init The maximum number of non-zero elements in each column (default value -1 \c => allocated size for the data is not changed)
    */
    void set_size(int rows, int cols, int row_data_init=-1);

    //! Returns the number of rows of the sparse matrix
    int rows() const { return n_rows; }

    //! Returns the number of columns of the sparse matrix
    int cols() const { return n_cols; }

    //! The number of non-zero elements in the sparse matrix
    int nnz();

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

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

    //! Returns a full, dense matrix in \c m
    void full(Mat<T> &m) const;

    //! Returns a full, dense matrix
    Mat<T> full() const;

    //! Returns element of row \c r and column \c c
    T operator()(int r, int c) const;

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

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

    //! Add the element in row \c r and column \c c with \c v
    void add_elem(const int r, const int c, const T v);

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

    //! Set the element in row \c r and column \c c to zero (i.e. clear that element if it contains a non-zero value)
    void zero_elem(const int r, const int c);

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

    //! Clear the element in row \c r and column \c c (if it contains a non-zero value)
    void clear_elem(const int r, const int c);

    //! Set submatrix defined by rows r1,r2 and columns c1,c2 to matrix m
    void set_submatrix(int r1, int r2, int c1, int c2, const Mat<T> &m);

    //! Set submatrix defined by upper-left element (\c r,\c c) and the size of matrix \c m to \c m
    void set_submatrix(int r, int c, const Mat<T>& m);

    //! Returns the sub-matrix from rows \c r1 to \c r2 and columns \c c1 to \c c2
    Sparse_Mat<T> get_submatrix(int r1, int r2, int c1, int c2) const;

    //! Returns the sub-matrix from columns \c c1 to \c c2 (all rows)
    Sparse_Mat<T> get_submatrix_cols(int c1, int c2) const;

    //! Returns column \c c of the Sparse_Mat in the Sparse_Vec \c v
    void get_col(int c, Sparse_Vec<T> &v) const;

    //! Returns column \c c of the Sparse_Mat
    Sparse_Vec<T> get_col(int c) const;

    //! Set column \c c of the Sparse_Mat
    void set_col(int c, const Sparse_Vec<T> &v);

    /*! Transpose the sparse matrix, return the result in \c m

    Note: this function can be slow for large matrices.
     */
    void transpose(Sparse_Mat<T> &m) const;

    /*! Returns the transpose of the sparse matrix

    Note: this function can be slow for large matrices.
    */
    Sparse_Mat<T> transpose() const;

    /*! Returns the transpose of the sparse matrix

    Note: this function can be slow for large matrices.
    */
    // Sparse_Mat<T> T() const { return this->transpose(); };

    //! Assign sparse matrix the value and dimensions of the sparse matrix \c m
    void operator=(const Sparse_Mat<T> &m);

    //! Assign sparse matrix the value and dimensions of the dense matrix \c m
    void operator=(const Mat<T> &m);

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

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

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

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

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

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

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

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

    //! Addition m1+m2 where m1 and m2 are sparse matrices
    friend Sparse_Mat<T> operator+<>(const Sparse_Mat<T> &m1, const Sparse_Mat<T> &m2);

    //! Multiplication c*m where c is a scalar and m is a sparse matrix
    friend Sparse_Mat<T> operator*<>(const T &c, const Sparse_Mat<T> &m);

    //! Multiplication m1*m2 where m1 and m2 are sparse matrices
    friend Sparse_Mat<T> operator*<>(const Sparse_Mat<T> &m1, const Sparse_Mat<T> &m2);

    //! Multiplication m*v where m is a sparse matrix and v is a sparse vector
    friend Sparse_Vec<T> operator*<>(const Sparse_Mat<T> &m, const Sparse_Vec<T> &v);

    //! Multiplication m*v where m is a sparse matrix and v is a full column vector
    friend Vec<T> operator*<>(const Sparse_Mat<T> &m, const Vec<T> &v);

    //! Multiplication v'*m where m is a sparse matrix and v is a full column vector
    friend Vec<T> operator*<>(const Vec<T> &v, const Sparse_Mat<T> &m);

    //! Multiplication m'*m where m is a sparse matrix. Returns a full, dense matrix
    friend Mat<T> trans_mult <>(const Sparse_Mat<T> &m);

    //! Multiplication m'*m where m is a sparse matrix, Returns a sparse matrix
    friend Sparse_Mat<T> trans_mult_s <>(const Sparse_Mat<T> &m);

    //! Multiplication m1'*m2 where m1 and m2 are sparse matrices
    friend Sparse_Mat<T> trans_mult <>(const Sparse_Mat<T> &m1, const Sparse_Mat<T> &m2);

    //! Multiplication m'*v where m is a sparse matrix and v is a full column vector
    friend Vec<T> trans_mult <>(const Sparse_Mat<T> &m, const Vec<T> &v);

    //! Multiplication m1*m2' where m1 and m2 are sparse matrices
    friend Sparse_Mat<T> mult_trans <>(const Sparse_Mat<T> &m1, const Sparse_Mat<T> &m2);

    private:
    void init();
    void alloc_empty();
    void alloc(int row_data_size=200);
    void free();

    int n_rows, n_cols;
    Sparse_Vec<T> *col;
  };

  /*!
    \relates Sparse_Mat
    \brief Sparse integer matrix
  */
  typedef Sparse_Mat<int> sparse_imat;

  /*!
    \relates Sparse_Mat
    \brief Sparse double matrix
  */
  typedef Sparse_Mat<double> sparse_mat;

  /*!
    \relates Sparse_Mat
    \brief Sparse complex<double> matrix
  */
  typedef Sparse_Mat<std::complex<double> > sparse_cmat;

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

  template <class T>
    void Sparse_Mat<T>::init()
    {
      n_rows = 0;
      n_cols = 0;
      col = 0;
    }

  template <class T>
    void Sparse_Mat<T>::alloc_empty()
    {
      if (n_cols == 0)
	col = 0;
      else
	col = new Sparse_Vec<T>[n_cols];
    }

  template <class T>
    void Sparse_Mat<T>::alloc(int row_data_init)
    {
      if (n_cols == 0)
	col = 0;
      else
	col = new Sparse_Vec<T>[n_cols];
      for (int c=0; c<n_cols; c++)
	col[c].set_size(n_rows, row_data_init);
    }

  template <class T>
    void Sparse_Mat<T>::free()
    {
      delete [] col;
      col = 0;
    }

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

  template <class T>
    Sparse_Mat<T>::Sparse_Mat(int rows, int cols, int row_data_init)
    {
      init();
      n_rows = rows;
      n_cols = cols;
      alloc(row_data_init);
    }

  template <class T>
    Sparse_Mat<T>::Sparse_Mat(const Sparse_Mat<T> &m)
    {
      init();
      n_rows = m.n_rows;
      n_cols = m.n_cols;
      alloc_empty();

      for (int c=0; c<n_cols; c++)
	col[c] = m.col[c];
    }

  template <class T>
    Sparse_Mat<T>::Sparse_Mat(const Mat<T> &m)
    {
      init();
      n_rows = m.rows();
      n_cols = m.cols();
      alloc();

      for (int c=0; c<n_cols; c++) {
	for (int r=0; r<n_rows; r++) {
	  //if (abs(m(r,c)) != T(0))
	  if (m(r,c) != T(0))
	    col[c].set_new(r, m(r,c));
	}
	col[c].compact();
      }
    }

  template <class T>
    Sparse_Mat<T>::Sparse_Mat(const Mat<T> &m, T epsilon)
    {
      init();
      n_rows = m.rows();
      n_cols = m.cols();
      alloc();

      for (int c=0; c<n_cols; c++) {
	for (int r=0; r<n_rows; r++) {
		if (std::abs(m(r,c)) > std::abs(epsilon))
	    col[c].set_new(r, m(r,c));
	}
	col[c].compact();
      }
    }

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

  template <class T>
    void Sparse_Mat<T>::set_size(int rows, int cols, int row_data_init)
    {
      n_rows = rows;

      //Allocate new memory for data if the number of columns has changed or if row_data_init != -1
      if (cols!=n_cols||row_data_init!=-1) {
	n_cols = cols;
	free();
	alloc(row_data_init);
      }
    }

  template <class T>
    int Sparse_Mat<T>::nnz()
    {
      int n=0;
      for (int c=0; c<n_cols; c++)
	n += col[c].nnz();

      return n;
    }

  template <class T>
    double Sparse_Mat<T>::density()
    {
      //return static_cast<double>(nnz())/(n_rows*n_cols);
      return double(nnz())/(n_rows*n_cols);
    }

  template <class T>
    void Sparse_Mat<T>::compact()
    {
      for (int c=0; c<n_cols; c++)
	col[c].compact();
    }

  template <class T>
    void Sparse_Mat<T>::full(Mat<T> &m) const
    {
      m.set_size(n_rows, n_cols);
      m = T(0);
      for (int c=0; c<n_cols; c++) {
	for (int p=0; p<col[c].nnz(); p++)
	  m(col[c].get_nz_index(p),c) = col[c].get_nz_data(p);
      }
    }

  template <class T>
    Mat<T> Sparse_Mat<T>::full() const
    {
      Mat<T> r(n_rows, n_cols);
      full(r);
      return r;
    }

  template <class T>
    T Sparse_Mat<T>::operator()(int r, int c) const
    {
      it_assert_debug(r>=0&&r<n_rows&&c>=0&&c<n_cols, "Incorrect input indexes given");
      return col[c](r);
    }

  template <class T>
    void Sparse_Mat<T>::set(int r, int c, T v)
    {
      it_assert_debug(r>=0&&r<n_rows&&c>=0&&c<n_cols, "Incorrect input indexes given");
      col[c].set(r, v);
    }

  template <class T>
    void Sparse_Mat<T>::set_new(int r, int c, T v)
    {
      it_assert_debug(r>=0&&r<n_rows&&c>=0&&c<n_cols, "Incorrect input indexes given");
      col[c].set_new(r, v);
    }

  template <class T>
    void Sparse_Mat<T>::add_elem(int r, int c, T v)
    {
      it_assert_debug(r>=0&&r<n_rows&&c>=0&&c<n_cols, "Incorrect input indexes given");
      col[c].add_elem(r, v);
    }

  template <class T>
    void Sparse_Mat<T>::zeros()
    {
      for (int c=0; c<n_cols; c++)
	col[c].zeros();
    }

  template <class T>
    void Sparse_Mat<T>::zero_elem(const int r, const int c)
    {
      it_assert_debug(r>=0&&r<n_rows&&c>=0&&c<n_cols, "Incorrect input indexes given");
      col[c].zero_elem(r);
    }

  template <class T>
    void Sparse_Mat<T>::clear()
    {
      for (int c=0; c<n_cols; c++)
	col[c].clear();
    }

  template <class T>
    void Sparse_Mat<T>::clear_elem(const int r, const int c)
    {
      it_assert_debug(r>=0&&r<n_rows&&c>=0&&c<n_cols, "Incorrect input indexes given");
      col[c].clear_elem(r);
    }

  template <class T>
    void Sparse_Mat<T>::set_submatrix(int r1, int r2, int c1, int c2, const Mat<T>& m)
  {
    if (r1 == -1) r1 = n_rows-1;
    if (r2 == -1) r2 = n_rows-1;
    if (c1 == -1) c1 = n_cols-1;
    if (c2 == -1) c2 = n_cols-1;

    it_assert_debug(r1>=0 && r2>=0 && r1<n_rows && r2<n_rows &&
	       c1>=0 && c2>=0 && c1<n_cols && c2<n_cols, "Sparse_Mat<Num_T>::set_submatrix(): index out of range");

    it_assert_debug(r2>=r1 && c2>=c1, "Sparse_Mat<Num_T>::set_submatrix: r2<r1 or c2<c1");
    it_assert_debug(m.rows() == r2-r1+1 && m.cols() == c2-c1+1, "Mat<Num_T>::set_submatrix(): sizes don't match");

    for (int i=0 ; i<m.rows() ; i++) {
      for (int j=0 ; j<m.cols() ; j++) {
        set(r1+i, c1+j, m(i,j));
      }
    }
  }

  template <class T>
    void Sparse_Mat<T>::set_submatrix(int r, int c, const Mat<T>& m)
  {
    it_assert_debug(r>=0 && r+m.rows()<=n_rows &&
               c>=0 && c+m.cols()<=n_cols, "Sparse_Mat<Num_T>::set_submatrix(): index out of range");

    for (int i=0 ; i<m.rows() ; i++) {
      for (int j=0 ; j<m.cols() ; j++) {
        set(r+i, c+j, m(i,j));
      }
    }
  }

  template <class T>
    Sparse_Mat<T> Sparse_Mat<T>::get_submatrix(int r1, int r2, int c1, int c2) const
    {
      it_assert_debug(r1<=r2 && r1>=0 && r1<n_rows && c1<=c2 && c1>=0 && c1<n_cols,
		"Sparse_Mat<T>::get_submatrix(): illegal input variables");

      Sparse_Mat<T> r(r2-r1+1, c2-c1+1);

      for (int c=c1; c<=c2; c++)
	r.col[c-c1] = col[c].get_subvector(r1, r2);
      r.compact();

      return r;
    }

  template <class T>
    Sparse_Mat<T> Sparse_Mat<T>::get_submatrix_cols(int c1, int c2) const
    {
      it_assert_debug(c1<=c2 && c1>=0 && c1<n_cols, "Sparse_Mat<T>::get_submatrix_cols()");
      Sparse_Mat<T> r(n_rows, c2-c1+1, 0);

      for (int c=c1; c<=c2; c++)
	r.col[c-c1] = col[c];
      r.compact();

      return r;
    }

  template <class T>
    void Sparse_Mat<T>::get_col(int c, Sparse_Vec<T> &v) const
    {
      it_assert(c>=0 && c<n_cols, "Sparse_Mat<T>::get_col()");
      v = col[c];
    }

  template <class T>
    Sparse_Vec<T> Sparse_Mat<T>::get_col(int c) const
    {
      it_assert(c>=0 && c<n_cols, "Sparse_Mat<T>::get_col()");
      return col[c];
    }

  template <class T>
    void Sparse_Mat<T>::set_col(int c, const Sparse_Vec<T> &v)
    {
      it_assert(c>=0 && c<n_cols, "Sparse_Mat<T>::set_col()");
      col[c] = v;
    }

  template <class T>
    void Sparse_Mat<T>::transpose(Sparse_Mat<T> &m) const
  {
    m.set_size(n_cols, n_rows);
    for (int c=0; c<n_cols; c++) {
      for (int p=0; p<col[c].nnz(); p++)
        m.col[col[c].get_nz_index(p)].set_new(c, col[c].get_nz_data(p));
    }
  }

  template <class T>
    Sparse_Mat<T> Sparse_Mat<T>::transpose() const
  {
    Sparse_Mat<T> m;
    transpose(m);
    return m;
  }

  template <class T>
    void Sparse_Mat<T>::operator=(const Sparse_Mat<T> &m)
    {
      free();
      n_rows = m.n_rows;
      n_cols = m.n_cols;
      alloc_empty();

      for (int c=0; c<n_cols; c++)
	col[c] = m.col[c];
    }

  template <class T>
    void Sparse_Mat<T>::operator=(const Mat<T> &m)
    {
      free();
      n_rows = m.rows();
      n_cols = m.cols();
      alloc();

      for (int c=0; c<n_cols; c++) {
	for (int r=0; r<n_rows; r++) {
	  if (m(r,c) != T(0))
	    col[c].set_new(r, m(r,c));
	}
	col[c].compact();
      }
    }

  template <class T>
    Sparse_Mat<T> Sparse_Mat<T>::operator-() const
    {
      Sparse_Mat r(n_rows, n_cols, 0);

      for (int c=0; c<n_cols; c++) {
	r.col[c].resize_data(col[c].nnz());
	for (int p=0; p<col[c].nnz(); p++)
	  r.col[c].set_new(col[c].get_nz_index(p), -col[c].get_nz_data(p));
      }

      return r;
    }

  template <class T>
    bool Sparse_Mat<T>::operator==(const Sparse_Mat<T> &m) const
    {
      if (n_rows!=m.n_rows || n_cols!=m.n_cols)
	return false;
      for (int c=0; c<n_cols; c++) {
	if (!(col[c] == m.col[c]))
	  return false;
      }
      // If they passed all tests, they must be equal
      return true;
    }

  template <class T>
    void Sparse_Mat<T>::operator+=(const Sparse_Mat<T> &m)
    {
      it_assert_debug(m.rows()==n_rows&&m.cols()==n_cols, "Addition of unequal sized matrices is not allowed");

      Sparse_Vec<T> v;
      for (int c=0; c<n_cols; c++) {
	m.get_col(c,v);
	col[c]+=v;
      }
    }

  template <class T>
    void Sparse_Mat<T>::operator+=(const Mat<T> &m)
    {
      it_assert_debug(m.rows()==n_rows&&m.cols()==n_cols, "Addition of unequal sized matrices is not allowed");

      for (int c=0; c<n_cols; c++)
	col[c]+=(m.get_col(c));
    }

  template <class T>
    void Sparse_Mat<T>::operator-=(const Sparse_Mat<T> &m)
    {
      it_assert_debug(m.rows()==n_rows&&m.cols()==n_cols, "Subtraction of unequal sized matrices is not allowed");

      Sparse_Vec<T> v;
      for (int c=0; c<n_cols; c++) {
	m.get_col(c,v);
	col[c]-=v;
      }
    }

  template <class T>
    void Sparse_Mat<T>::operator-=(const Mat<T> &m)
    {
      it_assert_debug(m.rows()==n_rows&&m.cols()==n_cols, "Subtraction of unequal sized matrices is not allowed");

      for (int c=0; c<n_cols; c++)
	col[c]-=(m.get_col(c));
    }

  template <class T>
    void Sparse_Mat<T>::operator*=(const T &m)
    {
      for (int c=0; c<n_cols; c++)
	col[c]*=m;
    }

  template <class T>
    void Sparse_Mat<T>::operator/=(const T &m)
    {
      for (int c=0; c<n_cols; c++)
	col[c]/=m;
    }

  template <class T>
    Sparse_Mat<T> operator+(const Sparse_Mat<T> &m1, const Sparse_Mat<T> &m2)
    {
      it_assert_debug(m1.n_cols == m2.n_cols && m1.n_rows == m2.n_rows , "Sparse_Mat<T> + Sparse_Mat<T>");

      Sparse_Mat<T> m(m1.n_rows, m1.n_cols, 0);

      for (int c=0; c<m.n_cols; c++)
	m.col[c] = m1.col[c] + m2.col[c];

      return m;
    }

  // This function added by EGL, May'05
  template <class T>
    Sparse_Mat<T> operator*(const T &c, const Sparse_Mat<T> &m)
    {
      int i,j;
      Sparse_Mat<T> ret(m.n_rows,m.n_cols);
      for (j=0; j<m.n_cols; j++) {
	for (i=0; i<m.col[j].nnz(); i++) {
	  T x = c*m.col[j].get_nz_data(i);
	  int k = m.col[j].get_nz_index(i);
	  ret.set_new(k,j,x);
	}
      }
      return ret;
    }

  template <class T>
    Sparse_Mat<T> operator*(const Sparse_Mat<T> &m1, const Sparse_Mat<T> &m2)
    {
      it_assert_debug(m1.n_cols == m2.n_rows, "Sparse_Mat<T> * Sparse_Mat<T>");

      Sparse_Mat<T> ret(m1.n_rows, m2.n_cols);

      for (int c=0; c<m2.n_cols; c++) {
	Sparse_Vec<T> &m2colc=m2.col[c];
	for (int p2=0; p2<m2colc.nnz(); p2++) {
	  Sparse_Vec<T> &mcol = m1.col[m2colc.get_nz_index(p2)];
	  T x = m2colc.get_nz_data(p2);
	  for (int p1=0; p1<mcol.nnz(); p1++) {
	    int r = mcol.get_nz_index(p1);
	    T inc = mcol.get_nz_data(p1) *x;
	    ret.col[c].add_elem(r,inc);
	  }
	}
      }
      // old code
/*       for (int c=0; c<m2.n_cols; c++) { */
/* 	for (int p2=0; p2<m2.col[c].nnz(); p2++) { */
/* 	  Sparse_Vec<T> &mcol = m1.col[m2.col[c].get_nz_index(p2)]; */
/* 	  for (int p1=0; p1<mcol.nnz(); p1++) { */
/* 	    int r = mcol.get_nz_index(p1); */
/* 	    T inc = mcol.get_nz_data(p1) * m2.col[c].get_nz_data(p2); */
/* 	    ret.col[c].add_elem(r,inc); */
/* 	  } */
/* 	} */
/*       } */
      ret.compact();
      return ret;
    }


  // This is apparently buggy.
/*   template <class T> */
/*     Sparse_Mat<T> operator*(const Sparse_Mat<T> &m1, const Sparse_Mat<T> &m2) */
/*     { */
/*       it_assert_debug(m1.n_cols == m2.n_rows, "Sparse_Mat<T> * Sparse_Mat<T>"); */

/*       Sparse_Mat<T> ret(m1.n_rows, m2.n_cols); */
/*       ivec occupied_by(ret.n_rows), pos(ret.n_rows); */
/*       for (int rp=0; rp<m1.n_rows; rp++) */
/* 	occupied_by[rp] = -1; */
/*       for (int c=0; c<ret.n_cols; c++) { */
/* 	Sparse_Vec<T> &m2col = m2.col[c]; */
/* 	for (int p2=0; p2<m2col.nnz(); p2++) { */
/* 	  Sparse_Vec<T> &m1col = m1.col[m2col.get_nz_index(p2)]; */
/* 	  for (int p1=0; p1<m1col.nnz(); p1++) { */
/* 	    int r = m1col.get_nz_index(p1); */
/* 	    T inc = m1col.get_nz_data(p1) * m2col.get_nz_data(p2); */
/* 	    if (occupied_by[r] == c) { */
/* 	      int index=ret.col[c].get_nz_index(pos[r]); */
/* 	      ret.col[c].add_elem(index,inc); */
/* 	    } */
/* 	    else { */
/* 	      occupied_by[r] = c; */
/* 	      pos[r] = ret.col[c].nnz(); */
/* 	      ret.col[c].set_new(r, inc); */
/* 	    } */
/* 	  } */
/* 	} */
/*       } */
/*       ret.compact(); */

/*       return ret; */
/*     } */


  // This function added by EGL, May'05
  template <class T>
    Sparse_Vec<T> operator*(const Sparse_Mat<T> &m, const Sparse_Vec<T> &v)
    {
      it_assert_debug(m.n_cols == v.size(), "Sparse_Mat<T> * Sparse_Vec<T>");

      Sparse_Vec<T> ret(m.n_rows);

      /* The two lines below added because the input parameter "v" is
	 declared const, but the some functions (e.g., nnz()) change
	 the vector... Is there a better workaround? */
      Sparse_Vec<T> vv = v;

      for (int p2=0; p2<vv.nnz(); p2++) {
	Sparse_Vec<T> &mcol = m.col[vv.get_nz_index(p2)];
	T x = vv.get_nz_data(p2);
	for (int p1=0; p1<mcol.nnz(); p1++) {
	  int r = mcol.get_nz_index(p1);
	  T inc = mcol.get_nz_data(p1) * x;
	  ret.add_elem(r,inc);
	}
      }
      ret.compact();
      return ret;
    }


  template <class T>
    Vec<T> operator*(const Sparse_Mat<T> &m, const Vec<T> &v)
    {
      it_assert_debug(m.n_cols == v.size(), "Sparse_Mat<T> * Vec<T>");

      Vec<T> r(m.n_rows);
      r.clear();

      for (int c=0; c<m.n_cols; c++) {
	for (int p=0; p<m.col[c].nnz(); p++)
	  r(m.col[c].get_nz_index(p)) += m.col[c].get_nz_data(p) * v(c);
      }

      return r;
    }

  template <class T>
    Vec<T> operator*(const Vec<T> &v, const Sparse_Mat<T> &m)
    {
      it_assert_debug(v.size() == m.n_rows, "Vec<T> * Sparse_Mat<T>");

      Vec<T> r(m.n_cols);
      r.clear();

      for (int c=0; c<m.n_cols; c++)
	r[c] = v * m.col[c];

      return r;
    }

  template <class T>
    Mat<T> trans_mult(const Sparse_Mat<T> &m)
    {
      Mat<T> ret(m.n_cols, m.n_cols);
      Vec<T> col;
      for (int c=0; c<ret.cols(); c++) {
	m.col[c].full(col);
	for (int r=0; r<c; r++) {
	  T tmp = m.col[r] * col;
	  ret(r,c) = tmp;
	  ret(c,r) = tmp;
	}
	ret(c,c) = m.col[c].sqr();
      }

      return ret;
    }

  template <class T>
    Sparse_Mat<T> trans_mult_s(const Sparse_Mat<T> &m)
    {
      Sparse_Mat<T> ret(m.n_cols, m.n_cols);
      Vec<T> col;
      T tmp;
      for (int c=0; c<ret.n_cols; c++) {
	m.col[c].full(col);
	for (int r=0; r<c; r++) {
	  tmp = m.col[r] * col;
	  if (tmp != T(0)) {
	    ret.col[c].set_new(r, tmp);
	    ret.col[r].set_new(c, tmp);
	  }
	}
	tmp = m.col[c].sqr();
	if (tmp != T(0))
	  ret.col[c].set_new(c, tmp);
      }

      return ret;
    }

  template <class T>
    Sparse_Mat<T> trans_mult(const Sparse_Mat<T> &m1, const Sparse_Mat<T> &m2)
    {
      it_assert_debug(m1.n_rows == m2.n_rows, "trans_mult()");

      Sparse_Mat<T> ret(m1.n_cols, m2.n_cols);
      Vec<T> col;
      for (int c=0; c<ret.n_cols; c++) {
        m2.col[c].full(col);
        for (int r=0; r<ret.n_rows; r++)
          ret.col[c].set_new(r, m1.col[r] * col);
      }

      return ret;
    }

  template <class T>
    Vec<T> trans_mult(const Sparse_Mat<T> &m, const Vec<T> &v)
    {
      Vec<T> r(m.n_cols);
      for (int c=0; c<m.n_cols; c++)
	r(c) = m.col[c] * v;

      return r;
    }

  template <class T>
    Sparse_Mat<T> mult_trans(const Sparse_Mat<T> &m1, const Sparse_Mat<T> &m2)
    {
      return trans_mult(m1.transpose(),m2.transpose());
    }

  //! Convert a dense matrix \c m into its sparse representation
  template <class T>
    inline Sparse_Mat<T> sparse(const Mat<T> &m, T epsilon)
    {
      Sparse_Mat<T> s(m, epsilon);
      return s;
    }

  //! Convert a sparse matrix \c s into its dense representation
  template <class T>
    inline Mat<T> full(const Sparse_Mat<T> &s)
    {
      Mat<T> m;
      s.full(m);
      return m;
    }

  //! Transpose a sparse matrix \c s
  template <class T>
    inline Sparse_Mat<T> transpose(const Sparse_Mat<T> &s)
    {
      Sparse_Mat<T> m;
      s.transpose(m);
      return m;
    }

  //! \cond

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

#ifdef HAVE_EXTERN_TEMPLATE

  extern template class Sparse_Mat<int>;
  extern template class Sparse_Mat<double>;
  extern template class Sparse_Mat<std::complex<double> >;

  extern template sparse_imat operator+(const sparse_imat &, const sparse_imat &);
  extern template sparse_mat operator+(const sparse_mat &, const sparse_mat &);
  extern template sparse_cmat operator+(const sparse_cmat &, const sparse_cmat &);

  extern template sparse_imat operator*(const sparse_imat &, const sparse_imat &);
  extern template sparse_mat operator*(const sparse_mat &, const sparse_mat &);
  extern template sparse_cmat operator*(const sparse_cmat &, const sparse_cmat &);

  extern template ivec operator*(const ivec &, const sparse_imat &);
  extern template vec operator*(const vec &, const sparse_mat &);
  extern template cvec operator*(const cvec &, const sparse_cmat &);

  extern template ivec operator*(const sparse_imat &, const ivec &);
  extern template vec operator*(const sparse_mat &, const vec &);
  extern template cvec operator*(const sparse_cmat &, const cvec &);

  extern template imat trans_mult(const sparse_imat &);
  extern template mat trans_mult(const sparse_mat &);
  extern template cmat trans_mult(const sparse_cmat &);

  extern template sparse_imat trans_mult_s(const sparse_imat &);
  extern template sparse_mat trans_mult_s(const sparse_mat &);
  extern template sparse_cmat trans_mult_s(const sparse_cmat &);

  extern template sparse_imat trans_mult(const sparse_imat &, const sparse_imat &);
  extern template sparse_mat trans_mult(const sparse_mat &, const sparse_mat &);
  extern template sparse_cmat trans_mult(const sparse_cmat &, const sparse_cmat &);

  extern template ivec trans_mult(const sparse_imat &, const ivec &);
  extern template vec trans_mult(const sparse_mat &, const vec &);
  extern template cvec trans_mult(const sparse_cmat &, const cvec &);

  extern template sparse_imat mult_trans(const sparse_imat &, const sparse_imat &);
  extern template sparse_mat mult_trans(const sparse_mat &, const sparse_mat &);
  extern template sparse_cmat mult_trans(const sparse_cmat &, const sparse_cmat &);

#endif // HAVE_EXTERN_TEMPLATE

  //! \endcond

} // namespace itpp

#endif // #ifndef SMAT_H