-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmatrix.h
940 lines (775 loc) · 26.7 KB
/
matrix.h
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
//
// Created by liuxi on 2018/12/28.
//
#ifndef NUMOPT3_MATRIX_H
#define NUMOPT3_MATRIX_H
#include <algorithm>
#include <cassert>
#include <ostream>
#include <string>
#include <typeinfo>
#include <type_traits>
struct Size;
struct Range;
struct HasSize {
virtual Size getSize() const = 0;
virtual ~HasSize() = default;
};
struct Point {
int x,y;
Point(int x, int y) : x(x), y(y) {}
Point swap() const { return {y,x}; }
Range operator,(const Size& size) const;
Range operator,(const Point& other) const;
Size operator-(const Point& other) const;
operator Size() const;
Point operator+(const Size& size) const;
Point operator-(const Size& size) const;
Point operator+() const { return {x,y}; };
Point operator-() const { return {-x,-y}; };
bool operator==(const Point &rhs) const {
return x == rhs.x &&
y == rhs.y;
}
bool operator!=(const Point &rhs) const {
return !(rhs == *this);
}
friend std::ostream &operator<<(std::ostream &os, const Point &point) {
os << "x: " << point.x << " y: " << point.y;
return os;
}
};
struct Size {
int w,h;
Size(int w, int h) : w(w), h(h) {}
operator Point() const {
return {w,h};
}
int count() const { return w*h; }
int index(const Point& pt) const { return w*pt.y+pt.x; }
int index(int row, int col) const { return w*row+col; }
Size swap() const { return {h,w}; }
bool operator==(const Size &rhs) const {
return w == rhs.w &&
h == rhs.h;
}
bool operator!=(const Size &rhs) const {
return !(rhs == *this);
}
friend std::ostream &operator<<(std::ostream &os, const Size &size) {
os << "w: " << size.w << " h: " << size.h;
return os;
}
};
struct Range : HasSize {
Point beg;
Size size;
Range(const Point &beg, const Size &size) : beg(beg), size(size) {}
Point beg_point() const {return beg;}
Point end_point() const {return beg+size;}
int left() const {return beg.x;}
int right() const {return beg.x+size.w-1;}
int top() const {return beg.y;}
int bottom() const {return beg.y+size.h-1;}
Range background(const Range& bk) const {
return Range{{bk.beg.x+this->beg.x,bk.beg.y+this->beg.y},
this->size};
}
int count() const {return size.count();}
bool operator==(const Range &rhs) const {
return beg == rhs.beg &&
size == rhs.size;
}
bool operator!=(const Range &rhs) const {
return !(rhs == *this);
}
friend std::ostream &operator<<(std::ostream &os, const Range &range) {
os << "beg: " << range.beg << " size: " << range.size;
return os;
}
virtual Size getSize() const {
return size;
}
};
Range Point::operator,(const Size &size) const {
return {*this,size};
}
Point::operator Size() const {
return {x,y};
}
Point Point::operator+(const Size &size) const {
return {x+size.w,y+size.h};
}
Point Point::operator-(const Size &size) const {
return {x-size.w,y-size.h};
}
Range Point::operator,(const Point &other) const {
return {*this,Point(other)-(*this)};
}
Size Point::operator-(const Point &other) const {
return {this->x-other.x,this->y-other.y};
}
template <typename T>
struct Matrix;
template <typename T>
struct MatrixView;
template <typename T>
struct MatrixInputer;
template <typename T>
struct MatrixIterator;
template <typename T>
struct MatrixVectorWiseView;
struct IsMatrix {
virtual ~IsMatrix() = default;
//private:
// IsMatrix() = default;
};
template <typename T>
struct IMatrix : HasSize,IsMatrix {
virtual ~IMatrix() = default;
virtual Matrix<T> copy() const;
virtual int count() const {return getSize().count();}
virtual std::string classname() const { return "IMatrix"; }
virtual T& operator()(const Point& idx2d) const {
return (*this)(this->getSize().index(idx2d));
}
virtual T& operator()(int row, int col) const {
return (*this)(this->getSize().index(row,col));
}
virtual T& operator()(int idx) const = 0;
virtual Size getSize() const = 0 ;
virtual T& at(const Point& idx2d) const { return (*this)(idx2d); }
virtual T& at(int row, int col) const { return (*this)(row,col); }
virtual T& at(int idx) const { return (*this)(idx); }
virtual int rows() const { return getSize().h; }
virtual int cols() const { return getSize().w; }
virtual MatrixView<T> operator()(const Range& rng) const = 0;
virtual MatrixView<T> view(Range rng) const { return (*this)(rng); }
virtual MatrixView<T> view() const { return (*this)(Range{Point{0,0},getSize()}); }
virtual MatrixView<T> col(int c) const = 0;
virtual MatrixView<T> row(int r) const = 0;
virtual MatrixVectorWiseView<T> colwise() const;
virtual MatrixVectorWiseView<T> rowwise() const;
virtual MatrixIterator<T> coliter() const;
virtual MatrixIterator<T> rowiter() const;
template <typename A>
IMatrix<T>& operator=(const IMatrix<A>& _mat) const {
assert(this->getSize()==_mat.getSize());
int cnt = this->count();
for(int i=0;i<cnt;i++) {
(*this)(i)=_mat(i);
}
return (*this);
}
virtual void setValue(const T& t) {
int n = this->count();
for(int i=0;i<n;i++) (*this)(i)=t;
}
template <typename A=T> A atc(int row, int col) const {
return (A)((*this)(row,col));
}
template <typename A=T> A atc(Point idx2d) const {
return (A)((*this)(idx2d));
}
template <typename A=T> A atc(int idx) const {
return (A)((*this)(idx));
}
template <typename A> Matrix<A> cast() const;
// template <typename A> Matrix<T> cross(const IMatrix<A>& _mat) const {
// //todo
// }
//
// template <typename A> Matrix<T> dot(const IMatrix<A>& _mat) const {
// //todo
// }
//
// template <typename A> Matrix<T> matmul(const IMatrix<A>& _mat) const {
// //todo
//
// }
virtual Matrix<T> t() const {
Matrix<T> r(this->getSize().swap());
int rn = this->rows();
int cn = this->cols();
for(int y=0;y<rn;y++)
for(int x=0;x<cn;x++)
r(x,y) = (*this)(y,x);
return r;
}
friend std::ostream &operator<<(std::ostream &os, const IMatrix<T> &mat) {
Size sz = mat.getSize();
os << mat.classname() << ": " << sz.h << "x" << sz.w << "\n";
for(int row = 0; row<sz.h; row++) {
os << " ";
for(int col = 0; col<sz.w; col++)
os << mat(row,col) << (col!=sz.w-1 ? ",\t" : "\n");
}
return os;
}
virtual MatrixInputer<T> operator<<(T t) const;
static Matrix<T> ones(int _rows, int _cols) {
return Matrix<T>(_rows, _cols, T(1));
}
static Matrix<T> zeros(int _rows, int _cols) {
return Matrix<T>(_rows, _cols);
}
static Matrix<T> eye(int _rows, int _cols) {
auto mat = Matrix<T>::zeros(_rows, _cols);
auto m = std::min(_rows,_cols);
for(int i=0;i<m;i++) mat(i,i)=T(1);
return mat;
}
static Matrix<T> eye(int _n) {
return Matrix<T>::eye(_n, _n);
}
static Matrix<T> ones_like(const HasSize& a) {
return Matrix<T>(a.getSize(), T(1));
}
static Matrix<T> zeros_like(const HasSize& a) {
return Matrix<T>(a.getSize());
}
static Matrix<T> eye_like(const HasSize& a) {
auto mat = Matrix<T>::zeros(a.getSize());
auto m = std::min(mat.getSize().w,mat.getSize().h);
for(int i=0;i<m;i++) mat(i,i)=T(1);
return mat;
}
};
template <typename T>
struct MatrixInputer{
int input_idx;
IMatrix<T>& mat;
MatrixInputer<T>&operator,(T t) {
assert(input_idx<mat.count());
mat(input_idx)=t;
input_idx++;
return *this;
};
friend struct IMatrix<T>;
private:
MatrixInputer(int input_idx, const IMatrix<T> &mat)
: input_idx(input_idx), mat(const_cast<IMatrix<T> &>(mat)) {}
MatrixInputer(const IMatrix<T> &mat, T first_value)
: input_idx(1), mat(const_cast<IMatrix<T> &>(mat)) {
mat(0)=first_value;
}
};
template<typename T>
MatrixInputer<T> IMatrix<T>::operator<<(T t) const {
return MatrixInputer<T>(const_cast<IMatrix<T> &>(*this),t);
}
template <typename T>
struct MatrixIterator {
Matrix<T> & _mat;
Range _cur_rng;
Range _iter_rng;
Size _offset;
MatrixIterator(const IMatrix<T> &_mat, const Range &_cur_rng, const Size &_offset)
: _mat(const_cast<IMatrix<T> &>(_mat).view()._mat),
_cur_rng(_cur_rng.background(_mat.view()._rng)),
_iter_rng(_mat.view()._rng), _offset(_offset) {}
virtual ~MatrixIterator() = default;
virtual MatrixView<T> get() const;
virtual void forward() {
this->_cur_rng.beg = this->_cur_rng.beg+this->offset();
}
virtual bool valid() const {
return _cur_rng.beg.x>=0
&& _cur_rng.beg.y>=0
&& _cur_rng.beg.x+_cur_rng.size.w<=_iter_rng.size.w
&& _cur_rng.beg.y+_cur_rng.size.h<=_iter_rng.size.h;
}
virtual Size offset() const { return _offset; }
friend std::ostream &operator<<(std::ostream &os, const MatrixIterator &iterator) {
os << "_mat: " << iterator._mat << " _cur_rng: " << iterator._cur_rng << " _iter_rng: " << iterator._iter_rng
<< " _offset: " << iterator._offset;
return os;
}
};
template <typename T>
struct MatrixView: IMatrix<T> {
Matrix<T> & _mat;
Range _rng;
using IMatrix<T>::operator<<;
using IMatrix<T>::operator();
virtual ~MatrixView() = default;
virtual std::string classname() const { return "MatrixView"; }
virtual Matrix<T> & refmat() const { return this->_mat; };
virtual T& operator()(int row, int col) const;
virtual T& operator()(int idx) const;
virtual MatrixView<T> operator()(const Range& rng) const {
return MatrixView<T>(this->_mat,Range{this->_rng.beg+rng.beg,rng.size});
}
virtual MatrixView<T> col(int c) const {
return {_mat,Range{{c+_rng.beg.x,_rng.beg.y},{1,_rng.size.h}}};
}
virtual MatrixView<T> row(int r) const {
return {_mat,Range{{_rng.beg.x,r+_rng.beg.y},{_rng.size.w,1}}};
}
virtual Size getSize() const { return this->_rng.size; }
friend struct IMatrix<T>;
friend struct Matrix<T>;
MatrixView<T>& operator=(const MatrixView<T>& mv){
assert(this->getSize()==mv.getSize());
int cnt = this->count();
for(int i=0;i<cnt;i++) {
(*this)(i)=mv(i);
}
return (*this);
}
// MatrixView<T>& operator=(const Matrix<T>& mv){
// assert(this->getSize()==mv.getSize());
// int cnt = this->count();
// for(int i=0;i<cnt;i++) {
// (*this)(i)=mv(i);
// }
// return (*this);
// }
template <typename A>
MatrixView<T>& operator=(const IMatrix<A>& mv) {
assert(this->getSize()==mv.getSize());
int cnt = this->count();
for(int i=0;i<cnt;i++) {
(*this)(i)=mv(i);
}
return (*this);
}
MatrixView(const MatrixView<T>& mv):_mat(mv._mat),_rng(mv._rng) {
}
MatrixView(const Matrix<T> &_mat, const Range &_rng)
: _mat(const_cast<Matrix<T>&>(_mat)), _rng(_rng) {}
};
template<typename T>
MatrixView<T> MatrixIterator<T>::get() const {
return _mat.view(_cur_rng);
}
template <typename T>
struct MatrixVectorWiseView: HasSize {
Matrix<T> & _mat;
Range _rng;
Size _offset;
Range _init_iter_rng;
virtual ~MatrixVectorWiseView() = default;
virtual MatrixIterator<T> iterator() const {
return MatrixIterator<T>(_mat,_init_iter_rng,_offset);
}
friend struct IMatrix<T>;
friend struct Matrix<T>;
virtual Size getSize() const {
return _rng.getSize();
}
private:
MatrixVectorWiseView(Matrix<T> &_mat, const Range &_rng,
const Size &_offset, const Range &_init_iter_rng) :
_mat(_mat), _rng(_rng), _offset(_offset), _init_iter_rng(_init_iter_rng) {}
//MatrixVectorWiseView(const MatrixVectorWiseView<T>& mv): _mat(mv._mat)
};
template <typename T>
struct Matrix:IMatrix<T>{
Size _size;
T * _data;
using IMatrix<T>::operator<<;
using IMatrix<T>::operator=;
using IMatrix<T>::operator();
T& operator()(int idx) const {
return _data[idx];
}
Matrix()
: _size(1,1),
_data(new T[_size.count()]{T{0}}) {}
Matrix(Size size)
: _size(size),
_data(new T[size.count()]{T{0}}) {}
Matrix(int _rows, int _cols)
: _size(_cols,_rows),
_data(new T[_rows*_cols]{T{0}}) {}
Matrix(int _rows, int _cols, const T& value)
: _size(_cols,_rows),
_data(new T[_rows*_cols]{T{0}}) {
std::fill_n(_data,_rows*_cols,value);
}
Matrix(const Size& size, const T& value)
: _size(size),
_data(new T[size.count()]{T(0)}) {
std::fill_n(_data,this->count(),value);
}
Matrix(Matrix<T>&& _mat)
: _size(_mat.getSize()),
_data(_mat._data) {
_mat._data = nullptr;
}
Matrix(const Matrix<T>& _mat)
: _size(_mat.getSize()),
_data(new T[_mat.count()]{T{0}}) {
std::copy(_mat._data,_mat._data+this->count(),_data);
}
Matrix(const IMatrix<T>& _mat)
: _size(_mat.getSize()),
_data(new T[_mat.count()]{T(0)}) {
int cnt = _mat.count();
for(int i=0; i<cnt; i++)
(*this)(i)=_mat(i);
}
virtual std::string classname() const { return "Matrix"; }
virtual MatrixView<T> col(int c) const {
return {*this,Range{{c,0},{1,_size.h}}};
}
virtual MatrixView<T> row(int r) const {
return {*this,Range{{0,r},{_size.w,1}}};
}
T * data() const {
return _data;
}
Size getSize() const override {
return _size;
}
MatrixView<T> operator()(const Range& rng) const override {
return MatrixView<T>(*this, rng);
}
Matrix<T>& operator=(Matrix<T>&& mat) {
if (_data!= nullptr) {
delete[] _data;
_data = nullptr;
}
this->_size = mat._size;
this->_data = mat._data;
mat._data = nullptr;
return *this;
}
Matrix<T>& operator=(const Matrix<T>& _mat){
if (this->_size != _mat._size) {
this->_size = _mat._size;
if (_data!= nullptr) {
delete[] _data;
_data = nullptr;
}
_data = new T[_mat.count()]{T(0)};
}
int cnt = _mat.count();
for(int i=0; i<cnt; i++)
(*this)(i)=_mat(i);
//*this = _mat;
return *this;
}
Matrix<T>& operator=(const T& a){
if(_data!= nullptr) {
int cnt = this->count();
for(int i=0;i<cnt;i++) (*this)(i)=(a);
}
return *this;
}
virtual ~Matrix() {
if (_data!= nullptr) {
delete[] _data;
_data = nullptr;
}
}
};
template<typename T>
template<typename A>
Matrix<A> IMatrix<T>::cast() const {
Matrix<A> mat(this->getSize());
int cnt = this->count();
for(int i=0;i<cnt;i++) {
mat(i)=(*this)(i);
}
return mat;
}
template<typename T>
Matrix<T> IMatrix<T>::copy() const {
return Matrix<T>(*this);
}
template<typename T>
MatrixVectorWiseView<T> IMatrix<T>::colwise() const {
auto v = this->view();
return MatrixVectorWiseView<T>(v._mat,v._rng,{1,0},{{0,0},{1,v.rows()}});
}
template<typename T>
MatrixVectorWiseView<T> IMatrix<T>::rowwise() const {
auto v = this->view();
return MatrixVectorWiseView<T>(v._mat,v._rng,{0,1},{{0,0},{v.cols(),1}});
}
template<typename T>
MatrixIterator<T> IMatrix<T>::coliter() const {
return MatrixIterator<T>(this->view(), this->col(0)._rng, Size(1,0));
}
template<typename T>
MatrixIterator<T> IMatrix<T>::rowiter() const {
return MatrixIterator<T>(this->view(), this->row(0)._rng, Size(0,1));
}
template<typename T>
T &MatrixView<T>::operator()(int idx) const {
return (*this)(idx/this->_rng.size.w, idx%this->_rng.size.w);
}
template<typename T>
T &MatrixView<T>::operator()(int row, int col) const {
return this->_mat(row+this->_rng.beg.y, col+this->_rng.beg.x);
}
template<typename T1, typename T2, typename T3>
inline void mat_apply1(const IMatrix<T1>& a, IMatrix<T2>& dst,
T3 func) {
assert(a.getSize()==dst.getSize());
int cnt = a.count();
for(int i=0;i<cnt;i++) dst(i) = func(a(i));
}
template<typename T1, typename T2, typename T3, typename T4>
inline void mat_apply2(const IMatrix<T1>& a, const IMatrix<T2>& b, IMatrix<T3>& dst,
T4 func) {
assert(a.getSize()==b.getSize());
assert(a.getSize()==dst.getSize());
int cnt = a.count();
for(int i=0;i<cnt;i++) dst(i) = func(a(i),b(i));
}
template<typename T1, typename T2, typename T3>
inline void mat_apply_inplace(IMatrix<T1>& a, const IMatrix<T2>& b,
T3 func) {
assert(a.getSize()==b.getSize());
int cnt = a.count();
for(int i=0;i<cnt;i++) func(a(i),b(i));
}
template<typename T1, typename T2, typename T3, typename T4>
inline void mat_apply_with_right_number(const IMatrix<T1>& a, T2 b,
IMatrix<T3>& dst, T4 func) {
assert(a.getSize()==dst.getSize());
int cnt = a.count();
for(int i=0;i<cnt;i++) dst(i) = func(a(i),b);
}
template<typename T1, typename T2, typename T3>
inline void mat_apply_with_right_number_inplace(IMatrix<T1>& a, T2 b, T3 func) {
int cnt = a.count();
for(int i=0;i<cnt;i++) func(a(i),b);
}
template<typename T1, typename T2, typename T3, typename T4>
inline void mat_apply_with_left_number(T1 a, const IMatrix<T2>& b,
IMatrix<T3>& dst, T4 func) {
assert(b.getSize()==dst.getSize());
int cnt = b.count();
for(int i=0;i<cnt;i++) dst(i) = func(a, b(i));
}
template<typename T1, typename T2, typename T3, typename T4>
inline void mat_apply_left_vectorwise(const MatrixVectorWiseView<T1>& a, const IMatrix<T2>& b, Matrix<T3>& c, T4 func) {
assert(a.getSize()==c.getSize());
auto ita = a.iterator();
auto itc = MatrixIterator<T3>(c,{{0,0},ita._cur_rng.size},a._offset);
assert(ita.valid() && ita._cur_rng.size == b.getSize());
for(;ita.valid()&&itc.valid();ita.forward(),itc.forward()) {
itc.get() = func(ita.get(),b);
}
}
template<typename T1, typename T2, typename T3, typename T4>
inline void mat_apply_right_vectorwise(const IMatrix<T2>& a, const MatrixVectorWiseView<T1>& b, Matrix<T3>& c, T4 func) {
assert(b.getSize()==c.getSize());
auto itb = b.iterator();
auto itc = MatrixIterator<T3>(c,{{0,0},itb._cur_rng.size},a._offset);
assert(itb.valid() && itb._cur_rng.size == a.getSize());
for(;itb.valid()&&itc.valid();itb.forward(),itc.forward()) {
itc.get() = func(a,itb.get());
}
}
#define SINGLE_MATRIX_OPERATOR(OP) \
template <template <typename> typename M1, \
typename T1, \
typename = std::enable_if_t< \
(!std::is_base_of_v<IsMatrix, T1>) \
&& (std::is_base_of_v<IMatrix<T1>, M1<T1>>),int>> \
Matrix<decltype(OP T1())> operator OP(const M1<T1>& a) { \
Matrix<decltype(OP T1())> c(a.getSize()); \
mat_apply1(a,c,[](const T1&t1){ return OP t1; }); \
return c; \
}
#define MATRIX_MATRIX_OPERATOR(OP) \
template <template <typename> typename M1,template <typename> typename M2, \
typename T1, typename T2, \
typename = std::enable_if_t< \
(!std::is_base_of_v<IsMatrix, T1>) \
&& (!std::is_base_of_v<IsMatrix, T2>) \
&& (std::is_base_of_v<IMatrix<T1>, M1<T1>>) \
&& (std::is_base_of_v<IMatrix<T2>, M2<T2>>),int>> \
Matrix<decltype(T1() OP T2())> operator OP(const M1<T1>& a, const M2<T2>& b) { \
Matrix<decltype(T1() OP T2())> c(a.getSize()); \
mat_apply2(a,b,c,[](const T1&t1,const T2&t2){ return t1 OP t2; }); \
return c; \
}
#define MATRIX_MATRIX_ASSIGN_OPERATOR(OP) \
template <template <typename> typename M1,template <typename> typename M2, \
typename T1, typename T2, \
typename = std::enable_if_t< \
(!std::is_base_of_v<IsMatrix, T1>) \
&& (!std::is_base_of_v<IsMatrix, T2>) \
&& (std::is_base_of_v<IMatrix<T1>, M1<T1>>) \
&& (std::is_base_of_v<IMatrix<T2>, M2<T2>>),int>> \
M1<T1>& operator OP(M1<T1>& a, const M2<T2>& b) { \
mat_apply_inplace(a,b,[](T1&t1,const T2&t2){t1 OP t2;}); \
return a; \
}
#define MATRIX_ELEMENT_OPERATOR(OP) \
template <template <typename> typename M, typename T1, typename T2, \
typename = std::enable_if_t< \
(!std::is_base_of_v<IsMatrix, T1>) \
&& (!std::is_base_of_v<IsMatrix, T2>) \
&& (std::is_base_of_v<IMatrix<T1>, M<T1>>),int>> \
Matrix<decltype(T1() OP T2())> operator OP(const M<T1>& a, const T2& b) { \
Matrix<decltype(T1() OP T2())> c(a.getSize()); \
mat_apply_with_right_number(a,b,c,[](const T1&t1, const T2&t2){return t1 OP t2;});\
return c; \
}
#define ELEMENT_MATRIX_OPERATOR(OP) \
template <template <typename> typename M, typename T1, typename T2, \
typename = std::enable_if_t< \
(!std::is_base_of_v<IsMatrix, T1>) \
&& (!std::is_base_of_v<IsMatrix, T2>) \
&& (std::is_base_of_v<IMatrix<T2>, M<T2>>),int>> \
Matrix<decltype(T1() OP T2())> operator OP(const T1& a, const M<T2>& b) { \
Matrix<decltype(T1() OP T2())> c(b.getSize()); \
mat_apply_with_left_number(a,b,c,[](const T1&t1, const T2&t2){return t1 OP t2;});\
return c; \
}
#define MATRIX_ELEMENT_ASSIGN_OPERATOR(OP) \
template <template <typename> typename M, typename T1, typename T2, \
typename = std::enable_if_t< \
(!std::is_base_of_v<IsMatrix, T1>) \
&& (!std::is_base_of_v<IsMatrix, T2>) \
&& (std::is_base_of_v<IMatrix<T1>, M<T1>>),int>> \
M<T1>& operator OP(M<T1>& a, const T2& b) { \
mat_apply_with_right_number_inplace(a,b,[](T1&t1, const T2&t2){t1 OP t2;});\
return a; \
}
#define VECTORWISE_MATRIX_OPERATOR(OP) \
template <template <typename> typename M2, \
typename T1, typename T2, \
typename = std::enable_if_t< \
(!std::is_base_of_v<IsMatrix, T1>) \
&& (!std::is_base_of_v<IsMatrix, T2>) \
&& (std::is_base_of_v<IMatrix<T2>, M2<T2>>),int>> \
Matrix<decltype(T1() OP T2())> operator OP(const MatrixVectorWiseView<T1>& a, const M2<T2>& b) { \
Matrix<decltype(T1() OP T2())> c(a.getSize());\
mat_apply_left_vectorwise(a,b,c,[](const auto& t1, const auto& t2){ return t1 OP t2;});\
return c; \
}
#define MATRIX_VECTORWISE_OPERATOR(OP) \
template <template <typename> typename M1, \
typename T1, typename T2, \
typename = std::enable_if_t< \
(!std::is_base_of_v<IsMatrix, T1>) \
&& (!std::is_base_of_v<IsMatrix, T2>) \
&& (std::is_base_of_v<IMatrix<T1>, M1<T1>>),int>> \
Matrix<decltype(T1() OP T2())> operator OP(const M1<T1>& a, const MatrixVectorWiseView<T2>& b) { \
Matrix<decltype(T1() OP T2())> c(b.getSize());\
mat_apply_right_vectorwise(a,b,c,[](const auto& t1, const auto& t2){ return t1 OP t2;});\
return c; \
}
SINGLE_MATRIX_OPERATOR(+)
SINGLE_MATRIX_OPERATOR(-)
SINGLE_MATRIX_OPERATOR(~)
SINGLE_MATRIX_OPERATOR(!)
MATRIX_MATRIX_OPERATOR(+ )
MATRIX_MATRIX_OPERATOR(- )
MATRIX_MATRIX_OPERATOR(* )
MATRIX_MATRIX_OPERATOR(/ )
MATRIX_MATRIX_OPERATOR(% )
MATRIX_MATRIX_OPERATOR(& )
MATRIX_MATRIX_OPERATOR(| )
MATRIX_MATRIX_OPERATOR(^ )
MATRIX_MATRIX_OPERATOR(> )
MATRIX_MATRIX_OPERATOR(>=)
MATRIX_MATRIX_OPERATOR(< )
MATRIX_MATRIX_OPERATOR(<=)
MATRIX_MATRIX_OPERATOR(==)
MATRIX_MATRIX_OPERATOR(!=)
MATRIX_MATRIX_OPERATOR(&&)
MATRIX_MATRIX_OPERATOR(||)
MATRIX_MATRIX_OPERATOR(<<)
MATRIX_MATRIX_OPERATOR(>>)
MATRIX_MATRIX_ASSIGN_OPERATOR(+=)
MATRIX_MATRIX_ASSIGN_OPERATOR(-=)
MATRIX_MATRIX_ASSIGN_OPERATOR(*=)
MATRIX_MATRIX_ASSIGN_OPERATOR(/=)
MATRIX_MATRIX_ASSIGN_OPERATOR(%=)
MATRIX_MATRIX_ASSIGN_OPERATOR(&=)
MATRIX_MATRIX_ASSIGN_OPERATOR(|=)
MATRIX_MATRIX_ASSIGN_OPERATOR(^=)
MATRIX_MATRIX_ASSIGN_OPERATOR(<<=)
MATRIX_MATRIX_ASSIGN_OPERATOR(>>=)
MATRIX_ELEMENT_OPERATOR(+ )
MATRIX_ELEMENT_OPERATOR(- )
MATRIX_ELEMENT_OPERATOR(* )
MATRIX_ELEMENT_OPERATOR(/ )
MATRIX_ELEMENT_OPERATOR(% )
MATRIX_ELEMENT_OPERATOR(& )
MATRIX_ELEMENT_OPERATOR(| )
MATRIX_ELEMENT_OPERATOR(^ )
MATRIX_ELEMENT_OPERATOR(> )
MATRIX_ELEMENT_OPERATOR(>=)
MATRIX_ELEMENT_OPERATOR(< )
MATRIX_ELEMENT_OPERATOR(<=)
MATRIX_ELEMENT_OPERATOR(==)
MATRIX_ELEMENT_OPERATOR(!=)
MATRIX_ELEMENT_OPERATOR(&&)
MATRIX_ELEMENT_OPERATOR(||)
//MATRIX_ELEMENT_OPERATOR(<<)
//MATRIX_ELEMENT_OPERATOR(>>)
ELEMENT_MATRIX_OPERATOR(+ )
ELEMENT_MATRIX_OPERATOR(- )
ELEMENT_MATRIX_OPERATOR(* )
ELEMENT_MATRIX_OPERATOR(/ )
ELEMENT_MATRIX_OPERATOR(% )
ELEMENT_MATRIX_OPERATOR(& )
ELEMENT_MATRIX_OPERATOR(| )
ELEMENT_MATRIX_OPERATOR(^ )
ELEMENT_MATRIX_OPERATOR(> )
ELEMENT_MATRIX_OPERATOR(>=)
ELEMENT_MATRIX_OPERATOR(< )
ELEMENT_MATRIX_OPERATOR(<=)
ELEMENT_MATRIX_OPERATOR(==)
ELEMENT_MATRIX_OPERATOR(!=)
ELEMENT_MATRIX_OPERATOR(&&)
ELEMENT_MATRIX_OPERATOR(||)
//ELEMENT_MATRIX_OPERATOR(<<)
//ELEMENT_MATRIX_OPERATOR(>>)
MATRIX_ELEMENT_ASSIGN_OPERATOR(+=)
MATRIX_ELEMENT_ASSIGN_OPERATOR(-=)
MATRIX_ELEMENT_ASSIGN_OPERATOR(*=)
MATRIX_ELEMENT_ASSIGN_OPERATOR(/=)
MATRIX_ELEMENT_ASSIGN_OPERATOR(%=)
MATRIX_ELEMENT_ASSIGN_OPERATOR(&=)
MATRIX_ELEMENT_ASSIGN_OPERATOR(|=)
MATRIX_ELEMENT_ASSIGN_OPERATOR(^=)
//MATRIX_ELEMENT_ASSIGN_OPERATOR(<<=)
//MATRIX_ELEMENT_ASSIGN_OPERATOR(>>=)
VECTORWISE_MATRIX_OPERATOR(+ )
VECTORWISE_MATRIX_OPERATOR(- )
VECTORWISE_MATRIX_OPERATOR(* )
VECTORWISE_MATRIX_OPERATOR(/ )
VECTORWISE_MATRIX_OPERATOR(% )
VECTORWISE_MATRIX_OPERATOR(& )
VECTORWISE_MATRIX_OPERATOR(| )
VECTORWISE_MATRIX_OPERATOR(^ )
VECTORWISE_MATRIX_OPERATOR(> )
VECTORWISE_MATRIX_OPERATOR(>=)
VECTORWISE_MATRIX_OPERATOR(< )
VECTORWISE_MATRIX_OPERATOR(<=)
VECTORWISE_MATRIX_OPERATOR(==)
VECTORWISE_MATRIX_OPERATOR(!=)
VECTORWISE_MATRIX_OPERATOR(&&)
VECTORWISE_MATRIX_OPERATOR(||)
VECTORWISE_MATRIX_OPERATOR(<<)
VECTORWISE_MATRIX_OPERATOR(>>)
MATRIX_VECTORWISE_OPERATOR(+ )
MATRIX_VECTORWISE_OPERATOR(- )
MATRIX_VECTORWISE_OPERATOR(* )
MATRIX_VECTORWISE_OPERATOR(/ )
MATRIX_VECTORWISE_OPERATOR(% )
MATRIX_VECTORWISE_OPERATOR(& )
MATRIX_VECTORWISE_OPERATOR(| )
MATRIX_VECTORWISE_OPERATOR(^ )
MATRIX_VECTORWISE_OPERATOR(> )
MATRIX_VECTORWISE_OPERATOR(>=)
MATRIX_VECTORWISE_OPERATOR(< )
MATRIX_VECTORWISE_OPERATOR(<=)
MATRIX_VECTORWISE_OPERATOR(==)
MATRIX_VECTORWISE_OPERATOR(!=)
MATRIX_VECTORWISE_OPERATOR(&&)
MATRIX_VECTORWISE_OPERATOR(||)
MATRIX_VECTORWISE_OPERATOR(<<)
MATRIX_VECTORWISE_OPERATOR(>>)
#endif //NUMOPT3_MATRIX_H