File: tnt_matrix.h

package info (click to toggle)
pymol 2.5.0%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 42,288 kB
  • sloc: cpp: 476,472; python: 76,538; ansic: 29,510; javascript: 6,792; sh: 47; makefile: 24
file content (1084 lines) | stat: -rw-r--r-- 22,607 bytes parent folder | download | duplicates (3)
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
/*
*
* Template Numerical Toolkit (TNT)
*
* Mathematical and Computational Sciences Division
* National Institute of Technology,
* Gaithersburg, MD USA
*
*
* This software was developed at the National Institute of Standards and
* Technology (NIST) by employees of the Federal Government in the course
* of their official duties. Pursuant to title 17 Section 105 of the
* United States Code, this software is not subject to copyright protection
* and is in the public domain. NIST assumes no responsibility whatsoever for
* its use by other parties, and makes no guarantees, expressed or implied,
* about its quality, reliability, or any other characteristic.
*
*/


// C compatible matrix: row-oriented, 0-based [i][j] and 1-based (i,j) indexing
//

#ifndef TNT_MATRIX_H
#define TNT_MATRIX_H

#include "tnt_subscript.h"
#include "tnt_vector.h"
#include <cstdlib>
#include <cassert>
#include <iostream>
#include <sstream>
#include <fstream>

namespace TNT
{

//namespace Linear_Algebra {


/**
		Dense matrix class for basic linear algebra operations.

		Ordering: row major.
		<p>
		Elements begin at (1,1) or [0][0].
		<p>
		Can be interfaced with C multidimentionsal arrays (e.g. double **)
		<p>
		copy-by-value semantics.
		<p>
		Optional range checking at compile time via TNT_BOUNDS_CHECK macro.

*/
template <class T>
class Matrix 
{

  private:
    Subscript m_;
    Subscript n_;
    Subscript mn_;      // total size
    T* v_;                  
    T** row_;           
    T* vm1_ ;       // these point to the same data, but are 1-based 
    T** rowm1_;

    // internal helper function to create the array
    // of row pointers

    void initialize(Subscript M, Subscript N)
    {
        mn_ = M*N;
        m_ = M;
        n_ = N;

        v_ = new T[mn_]; 
        row_ = new T*[M];
        rowm1_ = new T*[M];

        assert(v_  != NULL);
        assert(row_  != NULL);
        assert(rowm1_ != NULL);

        T* p = v_;              
        vm1_ = v_ - 1;
        for (Subscript i=0; i<M; i++)
        {
            row_[i] = p;
            rowm1_[i] = p-1;
            p += N ;
            
        }

        rowm1_ -- ;     // compensate for 1-based offset
    }
   
    void copy(const T*  v)
    {
        Subscript N = m_ * n_;
        Subscript i;

#ifdef TNT_UNROLL_LOOPS
        Subscript Nmod4 = N & 3;
        Subscript N4 = N - Nmod4;

        for (i=0; i<N4; i+=4)
        {
            v_[i] = v[i];
            v_[i+1] = v[i+1];
            v_[i+2] = v[i+2];
            v_[i+3] = v[i+3];
        }

        for (i=N4; i< N; i++)
            v_[i] = v[i];
#else

        for (i=0; i< N; i++)
            v_[i] = v[i];
#endif      
    }

    void set(const T& val)
    {
        Subscript N = m_ * n_;
        Subscript i;

#ifdef TNT_UNROLL_LOOPS
        Subscript Nmod4 = N & 3;
        Subscript N4 = N - Nmod4;

        for (i=0; i<N4; i+=4)
        {
            v_[i] = val;
            v_[i+1] = val;
            v_[i+2] = val;
            v_[i+3] = val; 
        }

        for (i=N4; i< N; i++)
            v_[i] = val;
#else

        for (i=0; i< N; i++)
            v_[i] = val;
        
#endif      
    }
    

    
    void destroy()
    {     
        /* do nothing, if no memory has been previously allocated */
        if (v_ == NULL) return ;

        /* if we are here, then matrix was previously allocated */
        if (v_ != NULL) delete [] (v_);     
        if (row_ != NULL) delete [] (row_);

        /* return rowm1_ back to original value */
        rowm1_ ++;
        if (rowm1_ != NULL ) delete [] (rowm1_);
    }

  public:

    typedef Subscript   size_type;
    typedef         T   value_type;
    typedef         T   element_type;
    typedef         T*  pointer;
    typedef         T*  iterator;
    typedef         T&  reference;
    typedef const   T*  const_iterator;
    typedef const   T&  const_reference;

    Subscript lbound() const { return 1;}
 



    operator T**(){ return  row_; }
    operator const T**() const { return row_; }


		/**
			@return the total number of items in matrix (M*N).
		*/
    Subscript size() const { return mn_; }

    // constructors

    Matrix() : m_(0), n_(0), mn_(0), v_(0), row_(0), vm1_(0), rowm1_(0) {};

    Matrix(const Matrix<T> &A)
    {
        initialize(A.m_, A.n_);
        copy(A.v_);
    }

		/**
			Create a MxN matrix, with each element assigned to the value 0.

			@param M the number of rows
			@param N the number of columns
			@param value (optional default value: 0 if not specified.

		*/                                              
    Matrix(Subscript M, Subscript N, const T& value = T(0))
    {
        initialize(M,N);
        set(value);
    }

		/**
			Create an MxN matrix, filling in values (row-major order) from
			the list (C array) provided.

			@param M the number of rows
			@param N the number of columns
			@param v  list (C array) of M*N values used to initialize matrix.
		*/
    Matrix(Subscript M, Subscript N, const T* v)
    {
        initialize(M,N);
        copy(v);
    }

		/**
			Create an MxN matrix, filling in values (row-major order) from
			a character string.

			@param M the number of rows
			@param N the number of columns
			@param s  string of M*N values used to initialize matrix.
		*/
    Matrix(Subscript M, Subscript N, const char *s)
    {
        initialize(M,N);
        //std::istrstream ins(s);
        std::istringstream ins(s);

        Subscript i, j;

        for (i=0; i<M; i++)
            for (j=0; j<N; j++)
                ins >> row_[i][j];
    }

    // destructor
    //
    ~Matrix()
    {
        destroy();
    }


    /** 
				Change size of matrix to MxN, reallocating memory if necessary.
				<p>
				NOTE: This operations occurs in place, i.e. when resizing to 
				a new matrix, original matrix elements
				are <b>NOT</b> retained.  Instead, one must explicit create
				a new matrix of this size and manually copy the elements, e.g.
				<pre>

				Matrix double B(M, N);

				int 	min_M = M < A.num_rows() ? M : A.num_rows();
				int 	min_N = N < A.num_cols() ? N : A.num_cols();
				for (int i=1; i<=min_M; i++)
					for (int j=1; j<=min_N; j++)
						B(i,j) = A(i,j);

				A.destroy();
				</pre>

				@param M  the number of rows of new size.
				@param N	the number of columns of new size.
    */
    Matrix<T>& newsize(Subscript M, Subscript N)
    {
        if (num_rows() == M && num_cols() == N)
            return *this;

        destroy();
        initialize(M,N);
        
        return *this;
    }




    /**
			Assign (copy) one matrix to another, e.g. A=B.  The
			contents of A are lost, and a new copy of B is created.  

			@param B to matrix to be copied.

    */
    Matrix<T>& operator=(const Matrix<T> &B)
    {
        if (v_ == B.v_)
            return *this;

        if (m_ == B.m_  && n_ == B.n_)      // no need to re-alloc
            copy(B.v_);

        else
        {
            destroy();
            initialize(B.m_, B.n_);
            copy(B.v_);
        }

        return *this;
    }
        
    Matrix<T>& operator=(const T& scalar)
    { 
        set(scalar); 
        return *this;
    }


    Subscript dim(Subscript d) const 
    {
#ifdef TNT_BOUNDS_CHECK
        assert( d >= 1);
        assert( d <= 2);
#endif
        return (d==1) ? m_ : ((d==2) ? n_ : 0); 
    }

    Subscript num_rows() const { return m_; }
    Subscript num_cols() const { return n_; }




    inline T* operator[](Subscript i)
    {
#ifdef TNT_BOUNDS_CHECK
        assert(0<=i);
        assert(i < m_) ;
#endif
        return row_[i];
    }

    inline const T* operator[](Subscript i) const
    {
#ifdef TNT_BOUNDS_CHECK
        assert(0<=i);
        assert(i < m_) ;
#endif
        return row_[i];
    }

    inline reference operator()(Subscript i)
    { 
#ifdef TNT_BOUNDS_CHECK
        assert(1<=i);
        assert(i <= mn_) ;
#endif
        return vm1_[i]; 
    }

    inline const_reference operator()(Subscript i) const
    { 
#ifdef TNT_BOUNDS_CHECK
        assert(1<=i);
        assert(i <= mn_) ;
#endif
        return vm1_[i]; 
    }



    inline reference operator()(Subscript i, Subscript j)
    { 
#ifdef TNT_BOUNDS_CHECK
        assert(1<=i);
        assert(i <= m_) ;
        assert(1<=j);
        assert(j <= n_);
#endif
        return  rowm1_[i][j]; 
    }


    
    inline const_reference operator() (Subscript i, Subscript j) const
    {
#ifdef TNT_BOUNDS_CHECK
        assert(1<=i);
        assert(i <= m_) ;
        assert(1<=j);
        assert(j <= n_);
#endif
        return rowm1_[i][j]; 
    }



		Vector<T> diag() const
		{
			Subscript N = n_ < m_ ? n_ : m_; 
			Vector<T> d(N);

			for (int i=0; i<N; i++)
				d[i] = row_[i][i];

		  return d;
		}

	  Matrix<T> upper_triangular() const;
		Matrix<T> lower_triangular() const;

	/* basic computations */

};


/* ***************************  I/O  ********************************/


template <class T>
std::ostream& operator<<(std::ostream &s, const Matrix<T> &A)
{
    Subscript M=A.num_rows();
    Subscript N=A.num_cols();

    s << M << " " << N << "\n";
    for (Subscript i=0; i<M; i++)
    {
        for (Subscript j=0; j<N; j++)
        {
            s << A[i][j] << " ";
        }
        s << "\n";
    }


    return s;
}


template <class T>
std::istream& operator>>(std::istream &s, Matrix<T> &A)
{

    Subscript M, N;

    s >> M >> N;

    if ( !(M == A.num_rows() && N == A.num_cols() ))
    {
        A.newsize(M,N);
    }


    for (Subscript i=0; i<M; i++)
        for (Subscript j=0; j<N; j++)
        {
            s >>  A[i][j];
        }


    return s;
}



// *******************[ basic matrix algorithms ]***************************

/**
		Matrix-Matrix multiplication:  C = A * B.

		<p>
		This is an optimizied (trinary) version of matrix multiply, where 
		the destination matrix has already been allocated.

		@param A	matrix of size M x N.
		@param B	matrix of size N x K.
		@param C the result A*B, of size M x K.

		@return a reference to C, after multiplication.
*/
template <class T>
Matrix<T> & mult(Matrix<T>& C, const Matrix<T>  &A, const Matrix<T> &B)
{

#ifdef TNT_BOUNDS_CHECK
    assert(A.num_cols() == B.num_rows());
#endif

    Subscript M = A.num_rows();
    Subscript N = A.num_cols();
    Subscript K = B.num_cols();

#ifdef TNT_BOUNDS_CHECK
		assert(C.num_rows() == M);
		assert(C.num_cols() == K);
#endif	

    T sum;

    const T* row_i;
    const T* col_k;

    for (Subscript i=0; i<M; i++)
    for (Subscript k=0; k<K; k++)
    {
        row_i  = &(A[i][0]);
        col_k  = &(B[0][k]);
        sum = 0;
        for (Subscript j=0; j<N; j++)
        {
            sum  += *row_i * *col_k;
            row_i++;
            col_k += K;
        }
        C[i][k] = sum; 
    }

    return C;
}


/**
	Matrix/matrix multiplication: compute A * B.

	@param A matrix: left side operand  (size M x N).
	@param B matrix: right side operand  (size N x K).

	@return A*B  (a new matirx of size M x K).
*/
template <class T>
inline Matrix<T> mult(const Matrix<T>  &A, const Matrix<T> &B)
{

#ifdef TNT_BOUNDS_CHECK
    assert(A.num_cols() == B.num_rows());
#endif

    Subscript M = A.num_rows();
    Subscript K = B.num_cols();

    Matrix<T> tmp(M,K);

		mult(tmp, A, B);		// tmp = A*B

    return tmp;
}

/**
	Matrix/matrix multiplication: compute A * B.

	@param A matrix: left side operand  (size M x N).
	@param B matrix: right side operand  (size N x K).

	@return A*B  (a new matirx of size M x K).
*/
template <class T>
inline Matrix<T> operator*(const Matrix<T>  &A, const Matrix<T> &B)
{
	return mult(A,B);
}

/**
	Matrix/vector multiplication: compute A * b.

	@param A matrix: left side operand  (number of columns of A, must match 
			the number of elements in b.)
	@param b vector: right side operand.
	@return A*b (a new vector of size M.)
*/
	
template <class T>
inline Vector<T> mult(const Matrix<T>  &A, const Vector<T> &b)
{

#ifdef TNT_BOUNDS_CHECK
    assert(A.num_cols() == b.dim());
#endif

    Subscript M = A.num_rows();
    Subscript N = A.num_cols();

    Vector<T> tmp(M);
    T sum;

    for (Subscript i=0; i<M; i++)
    {
        sum = 0;
        for (Subscript j=0; j<N; j++)
            sum = sum +  A[i][j] * b[j];

        tmp[i] = sum; 
    }

    return tmp;
}

/**
	Matrix/vector multiplication: compute A * b.

	@param A matrix: left side operand  (number of columns of A, must match 
			the number of elements in b.)
	@param b vector: right side operand.
	@return A*b (a new vector of size M.)
*/
	
template <class T>
inline Vector<T> operator*(const Matrix<T>  &A, const Vector<T> &b)
{
	return mult(A,b);
}


/**
	Matrix scaling: multiply each element of A by scalar s.

	<p>NOTE: this creates a new copy of A.  To scale "in place",
	use *= or mult_eq().


	@param A matrix: to be scaled. 
	@param s scalar: multiplier.
	@return s*A, a new matrix with same size of A.
*/
template <class T>
inline Matrix<T> mult(const T& s, const Matrix<T> &A)
{
    Subscript M = A.num_rows();
    Subscript N = A.num_cols();

		Matrix<T> R(M,N);
		for (int i=0; i<M; i++)
			for (int j=0; j<N; j++)
				R[i][j] = s * A[i][j];
				 
		return R;
}

/**
	Matrix scaling: multiply each element of A by scalar s.

	<p>
	Same as mult(A,s), as this is a commutative operation.

	<p>NOTE: this creates a new copy of A.  To scale "in place",
	use *= or mult_eq().


	@param A matrix: to be scaled. 
	@param s scalar: multiplier.
	@return s*A, a new matrix with same size of A.
*/
template <class T>
inline Matrix<T> mult(const Matrix<T> &A, const T& s)
{
	return mult(s, A);
}


/**
	Matrix scale in-place, i.e. compute A *= s, where each element
	of A is multiplied (scaled) by the value s.

	<p>NOTE: this creates a new copy of A.  To scale "in place",
	use *= or mult_eq().


	@param A matrix: to be scaled. 
	@param s scalar: multiplier.
	@return A, after scaling.
*/
template <class T>
inline Matrix<T> mult_eq(const T& s, Matrix<T> &A)
{
    Subscript M = A.num_rows();
    Subscript N = A.num_cols();

		for (int i=0; i<M; i++)
			for (int j=0; j<N; j++)
				A[i][j] *= s;
}

template <class T>
inline Matrix<T> mult_eq(Matrix<T> &A, const T&a)
{
	return mult_eq(a, A);
}


/**
	Matrix-Matrix tranpose multiplication, i.e. compute tranpose(A)*B.

	<p>
	NOTE: this is more efficient than computing the tranpose(A) explicitly,
	and then multiplying, as the tranpose of A is never really constructed.

	@param A  matrix: size M x N.
	@param B	matrix: size M x K.
	@return a new matrix of size N x K.
*/
template <class T>
inline Matrix<T> transpose_mult(const Matrix<T>  &A, const Matrix<T> &B)
{

#ifdef TNT_BOUNDS_CHECK
    assert(A.num_rows() == B.num_rows());
#endif

    Subscript M = A.num_cols();
    Subscript N = A.num_rows();
    Subscript K = B.num_cols();

    Matrix<T> tmp(M,K);
    T sum;

    for (Subscript i=0; i<N; i++)
    for (Subscript k=0; k<K; k++)
    {
        sum = 0;
        for (Subscript j=0; j<M; j++)
            sum = sum +  A[j][i] * B[j][k];

        tmp[i][k] = sum; 
    }

    return tmp;
}

/**
	Matrix-Vector tranpose multiplication, i.e. compute tranpose(A)*b.

	<p>
	NOTE: this is more efficient than computing the tranpose(A) explicitly,
	and then multiplying, as the tranpose of A is not explicitly constructed.

	@param A  Matrix: size M x N.
	@param b	Vector: size M.
	@return a new vector of size N.
*/
template <class T>
inline Vector<T> transpose_mult(const Matrix<T>  &A, const Vector<T> &b)
{

#ifdef TNT_BOUNDS_CHECK
    assert(A.num_rows() == b.dim());
#endif

    Subscript M = A.num_cols();
    Subscript N = A.num_rows();
	
    Vector<T> tmp(M);

    for (Subscript i=0; i<M; i++)
    {
        T sum = 0;
        for (Subscript j=0; j<N; j++)
            sum = sum +  A[j][i] * b[j];

        tmp[i] = sum; 
    }

    return tmp;
}

 
/**
		Matrix addition: compute A + B

		@param A	matrix of size M x N.
		@param B	matrix of size M x N.

		@param the sum A+B.
*/
template <class T>
Matrix<T> add(const Matrix<T> &A, const Matrix<T> &B)
{
    Subscript M = A.num_rows();
    Subscript N = A.num_cols();

    assert(M==B.num_rows());
    assert(N==B.num_cols());

    Matrix<T> tmp(M,N);
    Subscript i,j;

    for (i=0; i<M; i++)
        for (j=0; j<N; j++)
            tmp[i][j] = A[i][j] + B[i][j];

    return tmp;
}

/**
		Matrix addition: compute A + B.

		<p>
		NOTE: this is shorthand notation for add(A,B).
		
		@param A	matrix of size M x N.
		@param B	matrix of size M x N.

		@param the sum A+B.
*/
template <class T>
inline Matrix<T> operator+(const Matrix<T> &A, const Matrix<T> &B)
{
	return add(A,B);
}



/**
		Matrix addition, in place : compute A = A + B.


		@param A	matrix of size M x N.
		@param B	matrix of size M x N.

		@param the sum A+B.
*/
template <class T>
Matrix<T>& add_eq(Matrix<T> &A, const Matrix<T> &B)
{
    Subscript M = A.num_rows();
    Subscript N = A.num_cols();

    assert(M==B.num_rows());
    assert(N==B.num_cols());

    Matrix<T> tmp(M,N);
    Subscript i,j;

    for (i=0; i<M; i++)
        for (j=0; j<N; j++)
            tmp[i][j] = A[i][j] + B[i][j];

    return A += tmp;
}

/**
		Matrix addition, in place: compute A = A + B.

		<p>
		NOTE: this is shorthand notation for add_eq(A,B).
		
		@param A	matrix of size M x N.
		@param B	matrix of size M x N.

		@return the sum A+B.
*/
template <class T> 
inline Matrix<T> operator+=(Matrix<T> &A, const Matrix<T> &B)
{
	return add_eq(A,B);
}

/**
		Matrix subtraction : compute A - B.


		@param A	matrix of size M x N.
		@param B	matrix of size M x N.

		@return the result A-B.
*/
template <class T>
Matrix<T> minus(const Matrix <T>& A, const Matrix<T> &B)
{
    Subscript M = A.num_rows();
    Subscript N = A.num_cols();

    assert(M==B.num_rows());
    assert(N==B.num_cols());

    Matrix<T> tmp(M,N);
    Subscript i,j;

    for (i=0; i<M; i++)
        for (j=0; j<N; j++)
            tmp[i][j] = A[i][j] - B[i][j];

    return tmp;

}

/**
		Matrix subtraction : compute A - B.

		<p>
		This is shorthand notation for minus(A,B).

		@param A	matrix of size M x N.
		@param B	matrix of size M x N.

		@return the result A-B.
*/
template <class T>
inline Matrix<T> operator-(const Matrix<T> &A, 
    const Matrix<T> &B)
{
	return minus(A,B);
}


/**

		Matrix element-by-elment multiplication: for each (i,j)
		compute A(i,j) * B(i,j).

	@param A matrix of size M x N.
	@param B matrix of size M x N.
	@return new matrix, where each (i,j) is A(i,j) * B(i,j);

*/
template <class T>
Matrix<T> mult_element(const Matrix<T> &A, const Matrix<T> &B)
{
    Subscript M = A.num_rows();
    Subscript N = A.num_cols();

    assert(M==B.num_rows());
    assert(N==B.num_cols());

    Matrix<T> tmp(M,N);
    Subscript i,j;

    for (i=0; i<M; i++)
        for (j=0; j<N; j++)
            tmp[i][j] = A[i][j] * B[i][j];

    return tmp;
}

/**

		Matrix element-by-elment multiplication, in place: for each (i,j)
		compute A(i,j) = A(i,j) * B(i,j).

	@param A matrix of size M x N.
	@param B matrix of size M x N.
	@return the resultant A, where A(i,j) *= B(i,j);

*/
template <class T>
Matrix<T>&  mult_element_eq(Matrix<T> &A, const Matrix<T> &B)
{
    Subscript M = A.num_rows();
    Subscript N = A.num_cols();

    assert(M==B.num_rows());
    assert(N==B.num_cols());

    Subscript i,j;

    for (i=0; i<M; i++)
        for (j=0; j<N; j++)
            A[i][j] *= B[i][j];

		return A;
}


/**
	Compute Frobenius norm of matrix.  This is the
	square root of the sum of squares of each matrix entry, i.e.
	<pre>
			$$ \\sqrt{ \\sum_{i=1}{N} \\sum_{j=1}{N} A_{i,j}^{2} } $$.
	</pre>.  

	@param A the matrix to compute its Frobeinus norm.
	@return the Frobenius norm of A.
*/
template <class T>
double norm(const Matrix<T> &A)
{
	Subscript M = A.num_rows();
	Subscript N = A.num_cols();

	T sum = 0.0;
	for (int i=1; i<=M; i++)
		for (int j=1; j<=N; j++)
					sum += A(i,j) * A(i,j);
	return sqrt(sum);

}

/**

		Matrix tranpose: return a new matrix B, where B(i,j)
		is A(j,i).

		@param A matrix MxN
		@return new matrix of size N x M, where each (i,j) is 
		A(j,i).
*/
template <class T>
Matrix<T> transpose(const Matrix<T> &A)
{
    Subscript M = A.num_rows();
    Subscript N = A.num_cols();

    Matrix<T> S(N,M);
    Subscript i, j;

    for (i=0; i<M; i++)
        for (j=0; j<N; j++)
            S[j][i] = A[i][j];

    return S;
}







/**

	Solve the triangular system A_u *x=b, where A_u is the upper triangular
		portion (including the diagonal) of A.

	@param A 	a square matrix of size N x N.
	@param b	the right-hand-side (solution vector) of size N.

	@return x, such that A_u * x = b.

*/
template <class T>
Vector<T> upper_triangular_solve(const Matrix<T> &A, const Vector<T> &b) 
{
	int n = A.num_rows() < A.num_cols() ? A.num_rows() : A.num_cols();
	Vector<T> x(b);
	for (int k=n; k >= 1; k--)
	{
		x(k) /= A(k,k);
		for (int i=1; i< k; i++ )
			x(i) -= x(k) * A(i,k);
		}

		return x;
}
		
/**

	Solve the triangular system A_L *x=b, where A_L is the lower triangular
		portion (including the diagonal) of A.

	@param A 	a square matrix of size N x N.
	@param b	the right-hand-side (solution vector) of size N.

	@return x, such that A_u * x = b.

*/
template <class T>
Vector<T> lower_triangular_solve(const Matrix<T> &A, const Vector<T> &b) 
{
	int n = A.num_rows() < A.num_cols() ? A.num_rows() : A.num_cols();
	Vector<T> x(b);
	for (int k=1; k <= n; k++)
	{
		x(k) /= A(k,k);
		for (int i=k+1; i<= n; i++ )
			x(i) -= x(k) * A(i,k);
		}

		return x;
}
		

//}  	/*  namespace TNT::Linear_Algebra; */
} 	/* namespace TNT  */

#endif
// TNT_MATRIX_H