-
Notifications
You must be signed in to change notification settings - Fork 0
/
matrix.hpp
1190 lines (1052 loc) · 24.7 KB
/
matrix.hpp
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
//////////////////////////////////
// Jiayao's Math Toolbox //
// Linear Algebra Library //
// Matrix Class //
// //
// //
// (C) 2017 Jiayao Zhang //
// //
// v0.1 27-Apr-2017 //
// Basic functionality //
// for real matrices. //
// //
// v0.2 08-May-2017 //
// Complex support on //
// - Cholesky //
// - Inverse //
// - QR //
// - SVD //
//////////////////////////////////
// matrix.hpp
#include <cmath>
#include <cstdio>
#include <random>
#include "complex.hpp"
#ifndef MATRIX_HPP
#define MATRIX_HPP
#ifndef STDERR
#define STDERR stderr
#endif
#ifndef FLOAT_WIDTH
#define FLOAT_WIDTH 8
#endif
#ifndef FLOAT_PRE
#define FLOAT_PRE 3
#endif
#ifndef SEP_SPACE
#define SEP_SPACE 2
#endif
namespace jmt
{
// typedef double mat_type;
typedef unsigned size_type;
enum mat_style
{
C, MATLAB, NUMPY, UNFORMATTED
};
// forward declarations
template <class mat_type>
class matrix;
template <typename mat_type>
matrix<mat_type> operator+(const matrix<mat_type> &m1, const matrix<mat_type> &m2);
template <typename mat_type>
matrix<mat_type> operator-(const matrix<mat_type> &m1, const matrix<mat_type> &m2);
template <typename mat_type>
matrix<mat_type> operator*(const matrix<mat_type> &m1, const matrix<mat_type> &m2);
template <typename mat_type>
matrix<mat_type> operator+(const matrix<mat_type> &mat, mat_type p);
template <typename mat_type>
matrix<mat_type> operator+(mat_type p, const matrix<mat_type> &mat);
template <typename mat_type>
matrix<mat_type> operator-(const matrix<mat_type> &mat, mat_type p);
template <typename mat_type>
matrix<mat_type> operator-(mat_type p, const matrix<mat_type> &mat);
template <typename mat_type>
matrix<mat_type> operator*(const matrix<mat_type> &mat, mat_type p);
template <typename mat_type>
matrix<mat_type> operator*(mat_type p, const matrix<mat_type> &mat);
template <typename mat_type>
matrix<mat_type> operator/(const matrix<mat_type> &mat, mat_type p);
template <typename mat_type>
matrix<mat_type> operator/(mat_type p, const matrix<mat_type> &mat);
template <typename mat_type>
mat_type inner_prod(const matrix<mat_type> &m1, const matrix<mat_type> &m2);
template <class mat_type>
class matrix
{
public:
mat_type *data;
size_type nrow, ncol;
private:
size_type *ref;
protected:
inline virtual void clean_data()
{ if(data) { delete [] data; } data = nullptr; }
inline size_type idx(size_type r, size_type c) const
{ return r * ncol + c; }
private:
inline bool bound_check(size_type r, size_type c) const
{ return (r < nrow) && (c < ncol); }
/**
* recycle
*
* deallocate dynamic memory,
* reset reference counter
*/
void recycle()
{
if( !ref || !(*ref) )
{
this->clean_data();
if(ref) { delete ref; }
ref = nullptr;
ncol = nrow = 0;
} else if(*ref) {
--(*ref);
if(!(*ref))
{
this->recycle();
}
}
}
public:
/**
* fill(r1, c1, r2, c2, src)
*
* fill (r1, c1) to (r2, c2)
* in row-major order from array src
*/
void fill(size_type r1, size_type c1, size_type r2, size_type c2, mat_type *src)
{
size_type k = 0;
for(size_type i = r1; i <= r2; ++i)
{
for(size_type j = c1; j <= c2; ++j)
{
data[idx(i, j)] = mat_type(src[k++]);
}
}
}
/**
* fill(idx, n, src)
*
* fill n elements from the idx-th
* entry of data from array src
*/
void fill(size_type idx, size_type n, mat_type *src)
{
for(size_type i = 0; i < n; ++i)
{
data[idx + i] = mat_type(src[i]);
}
}
/**
* fill(r1, c1, r2, c2, mat)
*
* fill from begining of this matrix
* from (r1, c1) to (r2, c2) of mat
* in row-major order from matrix mat
*/
void fill(size_type r1, size_type c1, size_type r2, size_type c2, const matrix &mat)
{
size_type k = 0;
for(size_type i = r1; i <= r2; ++i)
{
for(size_type j = c1; j <= c2; ++j)
{
data[k++] = mat_type(mat.data[mat.idx(i, j)]);
}
}
}
/**
* fill(idx, n, mat)
*
* fill n elements to idx-th entry of this matrix
* from the idx-th entry of mat
*/
void fill(size_type idx, size_type n, const matrix &mat)
{
for(size_type i = 0; i < n; ++i)
{
data[idx + i] = mat_type(mat.data[idx + i]);
}
}
/**
* elem_set(e)
*
* set all element to e
*/
void elem_set(const mat_type &e)
{
for(size_type i = 0; i < nrow * ncol; ++i)
{ data[i] = mat_type(e); }
}
public:
/* constructor, copy, move and destructor */
// constructor
matrix() : data(nullptr), nrow(0), ncol(0), ref(nullptr) {}
matrix(size_type row, size_type col, mat_type e) : data(new mat_type[row * col]), nrow(row), ncol(col), ref(new size_type(1))
{ elem_set(e); }
matrix(size_type row, size_type col) : matrix(row, col, mat_type(0.0)) {}
matrix(size_type row, size_type col, mat_type *ee) : data(new mat_type[row * col]), nrow(row), ncol(col), ref(new size_type(1))
{ fill(0, nrow * ncol, ee); }
// destructor
~matrix()
{ this->recycle(); }
// copy constructor, shallow copy
matrix(const matrix &mat) : data(mat.data), nrow(mat.nrow), ncol(mat.ncol), ref(mat.ref)
{
if(!ref)
{
fprintf(STDERR, "copy constructor: copy from uninitilized matrix\n");
return;
}
++(*ref);
}
// copy assignment
virtual matrix& operator=(const matrix &mat)
{
this->recycle();
this->data = mat.data;
this->nrow = mat.nrow; this->ncol = mat.ncol;
this->ref = mat.ref;
if(ref)
{ ++(*(this->ref)); }
else {
fprintf(STDERR, "copy assignment: assign from uninitilized matrix\n");
}
return *this;
}
// move construcor, shallow copy
matrix(matrix &&mat) : data(mat.data), nrow(mat.nrow), ncol(mat.ncol), ref(mat.ref)
{
mat.data = nullptr;
mat.recycle();
}
// move assignment
virtual matrix& operator=(matrix &&mat)
{
this->recycle();
this->data = mat.data;
this->nrow = mat.nrow; this->ncol = mat.ncol;
if(mat.ref)
{ this->ref = new size_type(*(mat.ref)); }
else { this->ref = nullptr; }
mat.data = nullptr;
mat.recycle();
return *this;
}
/* matrix access & common matrices */
/**
* clone()
*
* return a clone of the matrix
*/
inline virtual matrix clone() const
{
matrix ret(nrow, ncol);
ret.fill(0, nrow * ncol, *this);
return ret;
}
/**
* clone(mat)
*
* set mat to a clone of the matrix
*/
inline matrix clone(matrix &mat) const
{
mat = this->clone();
return mat;
}
/**
* set(nrow, ncol)
*
* clean the matrix and reset it with
* dimension nrow * ncol
*/
virtual void set(size_type nrow, size_type ncol)
{
this->recycle();
this->nrow = nrow; this->ncol = ncol;
this->ref = new size_type(1);
this->data = new mat_type[nrow * ncol];
}
/**
* fillRow(r, row)
*
* fill the r-th row from a row matrix
* bound checking is performed
*/
virtual matrix& fillRow(size_type r, const matrix& row)
{
if(( (row.ncol == 1 && row.nrow == this->ncol)
|| (row.nrow == 1 && row.ncol == this->ncol))
&& bound_check(r, 0))
{
this->fill(r * ncol, ncol, row.data);
}
return *this;
}
/**
* fillRow(r, row)
*
* fill the r-th row from a row array
* bound checking is performed
*/
virtual matrix& fillRow(size_type r, mat_type *row)
{
if(bound_check(r, 0))
{
this->fill(r * ncol, ncol, row);
}
return *this;
}
/**
* fillCol(c, col)
*
* fill the c-th column from a column matrix
* bound checking is performed
*/
virtual matrix& fillCol(size_type c, const matrix &col)
{
if(( (col.ncol == 1 && col.nrow == this->nrow)
|| (col.nrow == 1 && col.ncol == this->nrow))
&& bound_check(0, c))
{
for(size_type i = 0; i < nrow; ++i)
{
data[idx(i,c)] = col.data[i];
}
}
return *this;
}
/**
* fillCol(c, col)
*
* fill the c-th column from a column array
* bound checking is performed
*/
virtual matrix& fillCol(size_type c, mat_type *col)
{
if(bound_check(0, c))
{
for(size_type i = 0; i < nrow; ++i)
{
data[idx(i,c)] = col[i];
}
}
return *this;
}
/**
* zeros()
*
* set a matrix with zeros
* matrix should be non-null
*/
inline virtual matrix& zeros()
{ elem_set(mat_type(0)); return *this; }
/**
* zeros(nrow, ncol)
*
* replace this matrix
* with a nrow * ncol zero matrix
*/
inline virtual matrix& zeros(size_type nrow, size_type ncol)
{
this->set(nrow, ncol);
return this->zeros();
}
/**
* diag(nrow, ncol, dat)
*
* replace this matrix with a diagonal matrix
* filling from array dat
*/
virtual matrix& diag(size_type nrow, size_type ncol, mat_type *dat)
{
this->zeros(nrow, ncol);
size_type n = nrow < ncol ? nrow : ncol;
for(size_type i = 0; i < n; ++i)
{
data[idx(i, i)] = dat[i];
}
return *this;
}
/**
* diag(nrow, ncol, mat)
*
* replace this matrix with a diagonal matrix
* filling from matrix mat in row major order
*/
inline virtual matrix& diag(size_type nrow, size_type ncol, const matrix &mat)
{ return this->diag(nrow, ncol, mat.data); }
/**
* ones()
*
* set a matrix with ones
* matrix should be non-null
*/
inline virtual matrix& ones()
{ elem_set(mat_type(1)); return *this; }
/**
* ones(nrow, ncol)
*
* replace this matrix
* with a nrow * ncol one-matrix
*/
inline virtual matrix& ones(size_type nrow, size_type ncol)
{
this->set(nrow, ncol);
return this->ones();
}
/**
* eye()
*
* set a matrix with identity
* matrix should be non-null
* but not necessarily be square
*/
virtual matrix& eye()
{
for(size_type i = 0; i < nrow; ++i)
{
for(size_type j = 0; j < ncol; ++j)
{
if(i == j)
{ data[idx(i,j)] = mat_type(1.0); }
else { data[idx(i,j)] = mat_type(0.0); }
}
}
return *this;
}
/**
* eye(nrow, ncol)
*
* replace this matrix
* with a nrow * ncol identity matrix
*/
inline virtual matrix& eye(size_type nrow, size_type ncol)
{
this->set(nrow, ncol);
return this->eye();
}
/**
* seq(init = 0, step = 1)
*
* filling the matrix in row major
* order, from init with step
*/
virtual matrix& seq(mat_type init = mat_type(), mat_type step = double(1))
{
for(size_type i = 0; i < nrow * ncol; ++i)
{
data[i] = init + step * i;
}
return *this;
}
/**
* seq(nrow, ncol, init = 0, step = 1)
*
* replace this matrix with one nrow * ncol
* matrix then invoke seq(init, step)
*/
inline virtual matrix& seq(size_type nrow, size_type ncol, mat_type init = mat_type(), mat_type step = double(1))
{
this->set(nrow, ncol);
return this->seq(init, step);
}
virtual matrix& nrand(double mu = 0.0, double sigma = 1.0)
{
std::default_random_engine gen;
std::normal_distribution<double> normal(mu, sigma);
for(size_type i = 0; i < ncol * nrow; ++i)
{
data[i] = mat_type(normal(gen));
}
return *this;
}
inline virtual matrix& nrand(size_type nrow, size_type ncol, double mu = 0.0, double sigma = 1.0)
{
this->set(nrow, ncol);
return this->nrand();
}
inline static matrix getNRand(size_type nrow, size_type ncol, double mu = 0.0, double sigma = 1.0)
{
matrix ret(nrow, ncol);
return ret.nrand(mu, sigma);
}
inline static matrix getSeq(size_type nrow, size_type ncol, mat_type init = mat_type())
{
matrix ret(nrow, ncol);
return ret.seq(init);
}
inline static matrix getOnes(size_type nrow, size_type ncol)
{
matrix ret(nrow, ncol);
return ret.ones();
}
inline static matrix getZeros(size_type nrow, size_type ncol)
{
matrix ret(nrow, ncol);
return ret.zeros();
}
inline static matrix getEye(size_type nrow, size_type ncol)
{
matrix ret(nrow, ncol);
ret.eye();
return ret;
}
// IOs
// protected:
// to be overriden / specialized
virtual void read(FILE *stream, size_type i) {}
public:
void read(FILE *stream = stdin)
{
if(ref && *ref)
{
for(size_type i = 0; i < nrow * ncol; ++i)
{
this->read(stream, i);
}
} else {
if(!ref) { ref = new size_type; }
*ref = 1;
if(!(ncol && nrow))
{ fscanf(stream, "%u%u", &nrow, &ncol); }
this->read(stream);
}
}
// protected:
// to be overriden / specialized
virtual void print(FILE *stream, size_type r, size_type c, int precision) const {}
public:
void print(FILE *stream, mat_style style, int precision = FLOAT_PRE) const
{
char lb = '[', rb = ']', sep = ' ', emp = ' ';
if(style == C)
{
lb = '{'; rb = '}'; sep = ',';
} else if(style == NUMPY) {
lb = '['; rb = ']'; sep = ',';
} else if(style == UNFORMATTED) {
lb = rb = sep = ' ';
}
if(ref && (*ref))
{
for(size_type i = 0; i < nrow; ++i)
{
fprintf(stream, "%c%c ", (i == 0 ? lb : emp), lb);
for(size_type j = 0; j < ncol; ++j)
{
print(stream, i, j, precision);
fprintf(stream, "%c", (j == ncol - 1 ? emp : sep) );
}
fprintf(stream, " %c%c\n", rb, i == nrow - 1 ? rb : sep);
}
} else {
fprintf(STDERR, "matrix not printable\n");
}
}
inline virtual void print(mat_style style, int precision = FLOAT_PRE) const
{ this->print(stdout, style, precision); }
inline virtual void print(FILE *stream) const
{ this->print(stream, MATLAB); }
inline virtual void print() const
{ this->print(stdout, MATLAB); }
/* element access */
virtual const mat_type& operator()(size_type r, size_type c) const
{ return data[idx(r, c)]; }
virtual mat_type& operator()(size_type r, size_type c)
{ return data[idx(r, c)]; }
virtual const mat_type* operator[](size_type r) const
{ return &data[r * ncol]; }
virtual mat_type* operator[](size_type r)
{ return &data[r * ncol]; }
/* submatrix / row / col */
virtual matrix& exRow(size_type i, size_type j)
{
mat_type tmp;
for(size_type n = 0; n < ncol; ++n)
{
tmp = data[idx(i ,n)];
data[idx(i, n)] = data[idx(j, n)];
data[idx(j ,n)] = tmp;
}
return *this;
}
virtual matrix& exCol(size_type i, size_type j)
{
mat_type tmp;
for(size_type n = 0; n < nrow; ++n)
{
tmp = data[idx(n ,i)];
data[idx(n, i)] = data[idx(n, j)];
data[idx(n ,j)] = tmp;
}
return *this;
}
virtual matrix& operator*=(double d)
{
for(size_type i = 0; i < nrow * ncol; ++i)
{
data[i] *= d;
}
return *this;
}
// return new matrix
virtual matrix getCol(size_type col) const
{
matrix res(nrow, 1);
if(bound_check(0, col))
{
for(size_type i = 0; i < nrow; ++i)
{
res(i, 0) = data[idx(i, col)];
}
} else {
fscanf(STDERR, "failed obtain column: index out of bound\n");
}
return res;
}
// return new matrix
virtual matrix getRow(size_type r) const
{
matrix res(1, ncol);
if(bound_check(r, 0))
{
res.fill(0, ncol, (data + r * ncol));
} else {
fscanf(STDERR, "failed obtain row: index out of bound\n");
}
return res;
}
// return new matrix
virtual matrix subMat(size_type r1, size_type c1, size_type r2, size_type c2) const
{
matrix res;
if(bound_check(r1, c1) && bound_check(r2, c2) && (r1 <= r2) && (c1 <= c2))
{
res.set(r2 - r1 + 1, c2 - c1 + 1);
size_type k = 0;
for(size_type i = r1; i <= r2; ++i)
{
for(size_type j = c1; j <= c2; ++j)
{
res.data[k++] = data[idx(i, j)];
}
}
} else {
fscanf(STDERR, "failed obtain submatrix: invalid indices\n");
}
return res;
}
/* arithmatics */
// to be specialized
virtual double sq_norm(size_type r1, size_type c1, size_type r2, size_type c2) const
{}
// to be specialized
virtual double sq_norm() const
{}
virtual double norm(size_type r1, size_type c1, size_type r2, size_type c2) const
{ return std::sqrt(this->sq_norm(r1, c1, r2, c2)); }
virtual double norm() const
{ return std::sqrt(this->sq_norm()); }
virtual matrix& add(const matrix &mat)
{
if(this->nrow == mat.nrow && this->ncol == mat.ncol)
{
for(size_type i = 0; i < nrow; ++i)
{
for(size_type j = 0; j < ncol; ++j)
{
data[idx(i,j)] += mat(i,j);
}
}
} else { fprintf(STDERR, "operation add failed, dimension not match\n"); }
return *this;
}
virtual matrix& operator+=(const matrix &mat)
{ return this->add(mat); }
virtual matrix& sub(const matrix &mat)
{
if(this->nrow == mat.nrow && this->ncol == mat.ncol)
{
for(size_type i = 0; i < nrow; ++i)
{
for(size_type j = 0; j < ncol; ++j)
{
data[idx(i,j)] -= mat(i,j);
}
}
} else { fprintf(STDERR, "operation sub failed, dimension not match\n"); }
return *this;
}
virtual matrix& operator-=(const matrix &mat)
{ return this->sub(mat); }
/* transformations */
virtual matrix transpose() const
{
matrix res(this->ncol, this->nrow);
for(size_type i = 0; i < this->nrow; ++i)
{
for(size_type j = 0; j < this->ncol; ++j)
{
res(j,i) = data[idx(i,j)];
}
}
return res;
}
// to be specialized for Complex
virtual matrix hermitian() const
{ }
// protected:
virtual matrix& clear_zero()
{
for(size_type i = 0; i < nrow * ncol; ++i)
{
if(feq(data[i], mat_type(0.0))) { data[i] = mat_type(0.0); }
}
return *this;
}
public:
virtual matrix& normalizeCol()
{
for(size_type i = 0; i < ncol; ++i)
{
matrix tmp = this->getCol(i);
double coef = tmp.norm();
for(size_type j = 0; j < nrow; ++j)
{
if(!feq(coef, 0.0) && !feq(data[idx(i,j)], mat_type(0.0)))
{ data[idx(j,i)] /= coef; }
else { data[idx(j,i)] = 0.0; }
}
}
return *this;
}
inline virtual matrix& normalize()
{
double coef = this->norm();
for(size_type i = 0; i < nrow * ncol; ++i)
{
data[i] /= coef;
}
return *this;
}
inline virtual mat_type trace() const
{
size_type n = fmin(nrow, ncol);
mat_type res(0);
for(size_type i = 0; i < n; ++i)
{
res += data[idx(i,i)];
}
return res;
}
// use Cholesky decomposition
inline virtual matrix inverse() const
{
matrix res = this->cholesky(),
inv = res.linv();
res = inv.hermitian() * inv;
res.clear_zero();
return res;
}
// use Gram-Schmit Process
virtual void basis(matrix &B) const
{
const size_type &r = this->nrow, &c = this->nrow;
B.set(r, c);
for(size_type i = 0; i < c; ++i)
{
matrix vi = this->getCol(i),
sum(r, 1);
for(size_type j = 0; j < i; ++j)
{
matrix tmp = B.getCol(j);
if(!feq(tmp.sq_norm(), 0))
{ sum += (inner_prod(vi, tmp) / tmp.sq_norm()) * tmp; }
else { sum = vi; break; }
}
vi -= sum;
B.fillCol(i, vi);
}
}
virtual void norm_basis(matrix &B) const
{
this->basis(B);
B.normalizeCol();
}
// use Householder Relection
virtual void QR(matrix &Q, matrix &R, bool _clear_zero_ = true) const
{ /* specialized */}
// m * n matrix =>
// U: m * n orthogonal
// S: n * n diagonal
// V: n * n orthogonal
virtual void SVD(matrix &U, matrix &S, matrix &V, double criterion=1e-8) const
{
bool trans = false;
matrix res, q, r;
if(nrow < ncol) { trans = true; res = this->hermitian(); }
else { res = this->clone(); }
jacobi_svd(res, U, S, V, criterion);
if(trans)
{
res = U;
U = V;
V = res;
}
}
virtual void sym_svd(matrix &U, matrix &S, double criterion=1e-8)
{ /* specialized */ }
virtual matrix cholesky() const
{
matrix L;
if(nrow != ncol)
{
fprintf(STDERR, "cholesky failed: matrix not square\n");
} else { matrix::cholesky(*this, L); }
return L;
}
virtual matrix linv() const
{
matrix res;
matrix::linv(*this, res);
return res;
}
private:
// inverse of a lower triangular matrix L
// S = L^-1
static void linv(const matrix &L, matrix &S)
{
S.zeros(L.nrow, L.ncol);
mat_type tmp(0);
for(size_type j = 0; j < L.ncol; ++j)
{
S(j, j) = 1.0 / L(j, j);
for(size_type i = j + 1; i < L.nrow; ++i)
{
// compute S(i, j)
tmp = mat_type(0);
for(size_type k = j; k < i; ++k)
{
tmp += L(i, k) * S(k,j);
}
S(i, j) = (- tmp) / L(i, i);
}
}
}
// single-sided Jacobi SVD algorithm
// for m * n matrix with m >= n
static void jacobi_svd(const matrix &A, matrix &U,
matrix &S, matrix &V, double criterion)
{ /* specialized */ }
// A is hermitian
// L is lower triangular
// A = L * (L*)
static void cholesky(const matrix &A, matrix &L)
{
size_type n = A.nrow;
L.zeros(n ,n);
mat_type tmp(0);
double res = 0.0;
for(size_type i = 0; i < n; ++i)
{
for(size_type j = 0; j < i; ++j)
{
tmp = mat_type(0.0);
// compute L(i, j)
for(size_type k = 0; k < j; ++k)
{
tmp += L(i, k) * conjugate(L(j, k));
}
L(i, j) = (A(i, j) - tmp) / L(j, j);
// printf("L(%u,%u) = %lf\n", i, j, L(i, j));
}
// compute L(i, i)
res = 0.0;
for(size_type k = 0; k < i; ++k)
{
res += jmt::sq_norm(L(i, k));
}
res = real(A(i, i)) - res;
if(res < 0 || !feq(im(A(i,i)), 0.0) )
{
fprintf(STDERR, "cholesky failed: matrix must be hermitian and positive definite\n");
L.zeros();
return;
}
L(i , i) = mat_type(::sqrt(res));
// printf("L(%u,%u) = %lf\n", i, i, L(i, i));