-
Notifications
You must be signed in to change notification settings - Fork 81
/
Copy pathexpr.hpp
1655 lines (1241 loc) · 44.1 KB
/
expr.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
#ifndef FILE_NGBLA_EXPR
#define FILE_NGBLA_EXPR
/**************************************************************************/
/* File: expr.hpp */
/* Author: Joachim Schoeberl */
/* Date: 01. Jan. 02 */
/**************************************************************************/
#include <core/array.hpp>
#include <core/localheap.hpp>
#include <core/exception.hpp>
#include <cstddef>
#include <ngs_stdcpp_include.hpp> // for INLINE
#include "complex_wrapper.hpp"
#if defined(NETGEN_ENABLE_CHECK_RANGE) && !defined(__CUDA_ARCH__)
#define NETGEN_CHECK_SHAPE(a,b) \
{ if(a.Shape() != b.Shape()) \
ngcore::ThrowException(__FILE__ ":" NETGEN_CORE_NGEXEPTION_STR(__LINE__) "\t: shapes don't match"); }
#else // defined(NETGEN_ENABLE_CHECK_RANGE) && !defined(__CUDA_ARCH__)
#define NETGEN_CHECK_SHAPE(a,b)
#endif // defined(NETGEN_ENABLE_CHECK_RANGE) && !defined(__CUDA_ARCH__)
template <typename T>
struct SafeIndex
{
T i;
INLINE SafeIndex(T ai) : i(ai) { };
INLINE operator T() const { return i; }
INLINE auto operator++() { return ++i; }
INLINE auto operator++(int) { return i++; }
};
namespace ngcore
{
template <typename T>
struct IsSafe<SafeIndex<T>> {
constexpr operator bool() const { return true; } };
} // namespace ngcore
/*
namespace std {
template <typename T>
struct is_integral<SafeIndex<T>> {
static constexpr bool value = true;
};
}
*/
namespace ngbla
{
using namespace std;
using namespace ngcore;
using namespace ngstd;
enum ORDERING { ColMajor, RowMajor };
struct unused_dist
{
unused_dist () = default;
unused_dist (size_t d) { };
template <int S>
unused_dist (IC<S> d) { };
};
template <typename T = double, ORDERING ORD = RowMajor, typename TH=size_t, typename TW=size_t, typename TDIST=size_t>
class MatrixView;
template <typename T = double, ORDERING ORD = RowMajor>
using FlatMatrix = MatrixView<T,ORD,size_t, size_t, unused_dist>;
// template <typename T = double, ORDERING ORD = RowMajor> class FlatMatrix;
template <typename T = double, ORDERING ORD = RowMajor> class Matrix;
template <int H, int W, typename T> class Mat;
template <int H, typename T> class DiagMat;
template <int S, typename T> class Vec;
template <typename T>
struct is_scalar_type { static constexpr bool value = false; };
template <typename T>
constexpr bool IsScalar ()
{
return is_scalar_type<T>::value;
}
/*
Matrix expression templates
*/
template <typename TM, enable_if_t<!IsScalar<TM>(),bool> = true>
inline auto Access (const TM & mat, int i, int j)
{
return mat(i,j);
}
template <typename TM, enable_if_t<IsScalar<TM>(),bool> = true>
inline auto Access (const TM & mat, int i, int j)
{
return mat;
}
template <typename T> struct is_scalar_type;
template<> struct is_scalar_type<int> { static constexpr bool value = true; };
template<> struct is_scalar_type<double> { static constexpr bool value = true; };
template<> struct is_scalar_type<Complex> { static constexpr bool value = true; };
/**
Trait to obtain vector and scalar types for given matrix types.
Is specified for double, Complex, AutoDiff<doube>, AutoDiff<Complex>
*/
template <class T> class mat_traits;
template <class T>
class mat_traits
{
public:
/// matrix element
typedef T TELEM;
/// field of matrix element
typedef T TSCAL;
/// type of column vector
typedef T TV_COL;
/// type of row vector
typedef T TV_ROW;
/// matrix height
// enum { HEIGHT = 1 };
/// matrix with
// enum { WIDTH = 1 };
// static constexpr int HEIGHT = 1;
// static constexpr int WIDTH = 1;
///
// enum { IS_COMPLEX = 0 };
};
template <class T>
class mat_traits<const T> : public mat_traits<T> { };
template <class T>
class mat_traits<T&> : public mat_traits<T> { };
/*
template <int D>
class mat_traits<ngcore::INT<D> >
{
public:
typedef int TELEM;
typedef int TSCAL;
typedef int TV_COL;
typedef int TV_ROW;
// enum { HEIGHT = D };
// enum { WIDTH = 1 };
// enum { IS_COMPLEX = 0 };
};
*/
template <>
class mat_traits<Complex>
{
public:
typedef Complex TELEM;
typedef Complex TSCAL;
typedef Complex TV_COL;
typedef Complex TV_ROW;
// static constexpr int HEIGHT = 1;
// static constexpr int WIDTH = 1;
// enum { IS_COMPLEX = 1 };
};
/// Height of matrix
template <class TM>
inline auto Height (const TM & m)
{
return m.Height();
}
/// Width of matrix
template <class TM>
inline auto Width (const TM & m)
{
return m.Width();
}
template <> inline constexpr auto Height<double> (const double&) { return 1; }
template <> inline constexpr auto Height<Complex> (const Complex&) { return 1; }
template <> inline constexpr auto Width<double> (const double&) { return 1; }
template <> inline constexpr auto Width<Complex> (const Complex&) { return 1; }
/*
template <class TM>
inline constexpr size_t Height () { return Height(TM()); }
template <class TM>
inline constexpr size_t Width () { return Width(TM()); }
*/
template <class TM>
inline constexpr auto Height () { return TM::Height(); }
template <class TM>
inline constexpr auto Width () { return TM::Width(); }
template <> inline constexpr auto Height<double> () { return 1; }
template <> inline constexpr auto Height<Complex> () { return 1; }
template <> inline constexpr auto Width<double> () { return 1; }
template <> inline constexpr auto Width<Complex> () { return 1; }
template <class TM>
inline constexpr bool IsComplex () { return IsComplex<typename mat_traits<TM>::TSCAL>(); }
template <> inline constexpr bool IsComplex<double> () { return false; }
template <> inline constexpr bool IsComplex<Complex> () { return true; }
template <class TA> class RowsArrayExpr;
template <class TA> class ColsArrayExpr;
template <class TA> class SubMatrixExpr;
template <class TA> class RowExpr;
template <class TA> class ColExpr;
/**
Expr is the base class for all matrix template expressions.
Barton and Nackman Trick for template polymorphism, function Spec.
provides Height and Width of matrix.
IsLinear allows linear matrix element access.
*/
#ifdef NETGEN_ENABLE_CHECK_RANGE
struct undefined_size
{
size_t size;
undefined_size() = default;
constexpr undefined_size(size_t s) : size(s) { }
template <int S>
explicit constexpr undefined_size(IC<S> s) : size(s) { }
explicit constexpr operator size_t() const { return size; }
explicit constexpr operator int() const { return size; }
explicit constexpr operator ptrdiff_t() const { return size; }
};
inline ostream & operator<< (ostream & ost, undefined_size s) { ost << "undefined("<<size_t(s)<<")"; return ost; }
inline constexpr auto operator/ (undefined_size ud, size_t i) { return undefined_size(size_t(ud)/i); }
inline constexpr auto operator- (undefined_size ud, size_t i) { return undefined_size(size_t(ud)-i); }
inline constexpr auto operator+ (undefined_size ud, size_t i) { return undefined_size(size_t(ud)+i); }
inline constexpr auto operator* (undefined_size ud, size_t i) { return undefined_size(size_t(ud)*i); }
inline constexpr auto operator* (size_t i, undefined_size ud) { return undefined_size(size_t(ud)*i); }
inline constexpr bool operator< (size_t i, undefined_size ud) { return i < size_t(ud); }
inline constexpr bool operator< (undefined_size ud, size_t i) { return size_t(ud) < i; }
inline constexpr bool operator>= (size_t i, undefined_size ud) { return i >= size_t(ud); }
inline constexpr bool operator>= (undefined_size ud, size_t i) { return size_t(ud) >= i; }
inline constexpr bool operator== (size_t i, undefined_size ud) { return i == size_t(ud); }
inline constexpr bool operator== (undefined_size ud, size_t i) { return size_t(ud) == i; }
inline constexpr bool operator== (undefined_size ud, undefined_size ud2) { return size_t(ud) == size_t(ud2); }
inline constexpr bool operator!= (size_t i, undefined_size ud) { return i != size_t(ud); }
inline constexpr bool operator!= (undefined_size ud, size_t i) { return size_t(ud) != i; }
inline constexpr bool operator!= (undefined_size ud, undefined_size ud2) { return size_t(ud) != size_t(ud2); }
#else
struct undefined_size
{
undefined_size() = default;
undefined_size(size_t s) { }
template <int S>
explicit constexpr undefined_size(IC<S> s) { }
};
inline ostream & operator<< (ostream & ost, undefined_size s) { ost << "undefined"; return ost; }
inline auto operator/ (undefined_size ud, size_t i) { return ud; }
inline auto operator- (undefined_size ud, size_t i) { return ud; }
inline auto operator+ (undefined_size ud, size_t i) { return ud; }
#endif
INLINE constexpr auto CombinedSize(undefined_size s1, undefined_size s2) {
NETGEN_CHECK_SAME(size_t(s1), size_t(s2)); return s1; }
INLINE constexpr auto CombinedSize(undefined_size s1, size_t s2) {
NETGEN_CHECK_SAME(size_t(s1), size_t(s2)); return s2; }
INLINE constexpr auto CombinedSize(size_t s1, undefined_size s2) {
NETGEN_CHECK_SAME(size_t(s1), size_t(s2)); return s1; }
INLINE constexpr auto CombinedSize(size_t s1, size_t s2) {
NETGEN_CHECK_SAME(size_t(s1), size_t(s2)); return s1; }
template <int S1> INLINE constexpr auto CombinedSize(IC<S1> s1, undefined_size s2) {
NETGEN_CHECK_SAME(size_t(s1), size_t(s2)); return s1; }
template <int S1> INLINE constexpr auto CombinedSize(IC<S1> s1, size_t s2) {
NETGEN_CHECK_SAME(size_t(s1), size_t(s2)); return s1; }
template <int S1, int S2> INLINE constexpr auto CombinedSize(IC<S1> s1, IC<S2> s2) {
NETGEN_CHECK_SAME(size_t(s1), size_t(s2)); return s1; }
template <int S2> INLINE constexpr auto CombinedSize(undefined_size s1, IC<S2> s2) {
NETGEN_CHECK_SAME(size_t(s1), size_t(s2)); return s2; }
template <int S2> INLINE constexpr auto CombinedSize(size_t s1, IC<S2> s2) {
NETGEN_CHECK_SAME(size_t(s1), size_t(s2)); return s2; }
template <typename T1, typename T2>
INLINE constexpr auto CombinedSize(tuple<T1> tup1, tuple<T2> tup2)
{ return tuple(CombinedSize(get<0>(tup1), get<0>(tup2))); }
template <typename T11, typename T12, typename T21, typename T22>
INLINE constexpr auto CombinedSize(tuple<T11,T12> tup1, tuple<T21,T22> tup2)
{ return tuple(CombinedSize(get<0>(tup1), get<0>(tup2)),
CombinedSize(get<1>(tup1), get<1>(tup2))); }
template <typename T>
class Expr
{
public:
constexpr Expr () = default;
/// cast to specific type
INLINE T & Spec() { return static_cast<T&> (*this); }
/// cast to specific type
INLINE const T & Spec() const { return static_cast<const T&> (*this); }
INLINE auto View() const { return static_cast<const T&> (*this).View(); }
INLINE decltype(auto) ViewRW() { return static_cast<T&>(*this).ViewRW(); }
INLINE auto Shape() const { return Spec().T::Shape(); }
INLINE auto Height() const { return Spec().T::Height(); }
INLINE auto Width() const { return Spec().T::Width(); }
void Dump (ostream & ost) const { Spec().T::Dump(ost); }
INLINE auto Row (size_t r) const
{
// return RowExpr<const T> (static_cast<const T&> (*this), r);
return RowExpr<const T> (this->View(), r);
}
INLINE auto Col (size_t r) const
{
// return ColExpr<const T> (static_cast<const T&> (*this), r);
return ColExpr<const T> (this->View(), r);
}
INLINE SubMatrixExpr<T>
Rows (size_t first, size_t next) const
{
// return SubMatrixExpr<T> (static_cast<T&> (*this), first, 0, next-first, Width());
return SubMatrixExpr<T> (this->View(), first, 0, next-first, Width());
}
INLINE SubMatrixExpr<T>
Cols (size_t first, size_t next) const
{
// return SubMatrixExpr<T> (static_cast<T&> (*this), 0, first, Height(), next-first);
return SubMatrixExpr<T> (this->View(), 0, first, Height(), next-first);
}
INLINE SubMatrixExpr<T>
Rows (IntRange range) const
{
return Rows (range.First(), range.Next());
}
INLINE SubMatrixExpr<T>
Cols (IntRange range) const
{
return Cols (range.First(), range.Next());
}
INLINE RowsArrayExpr<T>
Rows (FlatArray<int> rows) const
{
return RowsArrayExpr<T> (static_cast<const T&> (*this), rows);
}
INLINE ColsArrayExpr<T>
Cols (FlatArray<int> cols) const
{
return ColsArrayExpr<T> (static_cast<const T&> (*this), cols);
}
};
/**
Caller knows that matrix expression is a symmetric matrix.
Thus, only one half of the matrix needs to be computed.
*/
template <typename T>
class SymExpr : public Expr<SymExpr<T> >
{
T a;
public:
SymExpr (T aa) : a(aa) { ; }
INLINE auto operator() (size_t i) const { return a(i); }
INLINE auto operator() (size_t i, size_t j) const { return a(i,j); }
INLINE auto Height() const { return a.Height(); }
INLINE auto Width() const { return a.Width(); }
auto View() const { return *this; }
auto Shape() const { return a.Shape(); }
static constexpr bool IsLinear() { return T::IsLinear(); }
void Dump (ostream & ost) const
{ ost << "Sym ("; a.Dump(ost); ost << ")"; }
};
/**
Declare that matrix expression is symmetric
*/
template <typename T>
inline SymExpr<T> Symmetric (const Expr<T> & a)
{
return SymExpr<T> (a.View());
}
template <typename TA>
class LocalHeapExpr : public Expr<LocalHeapExpr<TA> >
{
const TA & a;
LocalHeap * lh;
public:
INLINE LocalHeapExpr (const TA & aa, LocalHeap & alh) : a(aa), lh(&alh) { ; }
INLINE const TA & A() const { return a; }
INLINE auto Height() const { return a.Height(); }
INLINE auto Width() const { return a.Width(); }
INLINE LocalHeap & GetLocalHeap() const { return *lh; }
};
template <typename TA>
INLINE LocalHeapExpr<TA> operator| (const Expr<TA> & a, LocalHeap & lh)
{
return LocalHeapExpr<TA> (a.Spec(), lh);
}
template <class TA> class MatExpr;
template <class TA, class TB> class MultExpr;
template <class TA> class MinusExpr;
template <class TA> class TransExpr;
template <class TA, class TS> class ScaleExpr;
template <typename TOP, typename T, typename TB, typename Enable=int>
class assign_trait
{
public:
static INLINE T & Assign (MatExpr<T> & self, const Expr<TB> & v)
{
// NETGEN_CHECK_RANGE(self.Height(), v.Height(), v.Height()+1);
// NETGEN_CHECK_RANGE(self.Width(), v.Width(), v.Width()+1);
// NETGEN_CHECK_SHAPE(self.Spec(), v);
auto src = v.View();
decltype(auto) dest = self.ViewRW();
auto h = CombinedSize (src.Height(), dest.Height()); // checks if same
auto w = CombinedSize (src.Width(), dest.Width()); // checks if same
if (T::COL_MAJOR)
{
if (h > 0)
for (size_t j = 0; j < w; j++)
for (size_t i = 0; i < h; i++)
TOP()(dest(i,j), src(i,j));
return self.Spec();
}
if (TB::IsLinear())
{
if (T::IsLinear())
{
auto hw = h*w;
for (SafeIndex<size_t> i : Range(hw))
TOP()(dest(i), src(i));
}
else
{
if (w > 0)
for (SafeIndex<size_t> i = 0, k = 0; i < h; i++)
for (SafeIndex<size_t> j = 0; j < w; j++, k++)
TOP() (dest(i,j), src(k));
}
}
else
{
if (w > 0)
{
if (T::IsLinear())
for (SafeIndex<size_t> i = 0, k = 0; i < h; i++)
for (SafeIndex<size_t> j = 0; j < w; j++, k++)
TOP() (dest(k), src(i,j));
else
{
for (SafeIndex<size_t> i = 0; i < h; i++)
for (SafeIndex<size_t> j = 0; j < w; j++)
TOP() (dest(i,j), src(i,j));
}
}
}
return self.Spec();
}
};
/**
The base class for matrices.
*/
template <class T>
class MatExpr : public Expr<T>
{
public:
constexpr MatExpr () = default;
using Expr<T>::Spec;
using Expr<T>::Height;
using Expr<T>::Width;
enum { COL_MAJOR = 0 }; // matrix is stored col-major
void Dump (ostream & ost) const { ost << "Matrix"; }
template<typename TOP, typename TB>
INLINE auto & Assign (const Expr<TB> & v)
{
return assign_trait<TOP, T, TB>::Assign (*this, v);
}
class As
{
public:
template <typename T1, typename T2>
INLINE void operator() (T1 && v1, const T2 & v2) { v1 = v2; }
static constexpr bool IsPos() { return true; }
static constexpr bool IsAdd() { return false; }
};
class AsAdd
{
public:
template <typename T1, typename T2>
INLINE void operator() (T1 && v1, const T2 & v2) { v1 += v2; }
static constexpr bool IsPos() { return true; }
static constexpr bool IsAdd() { return true; }
};
class AsSub
{
public:
template <typename T1, typename T2>
INLINE void operator() (T1 && v1, const T2 & v2) { v1 -= v2; }
static constexpr bool IsPos() { return false; }
static constexpr bool IsAdd() { return true; }
};
/*
TODO: move to traits ...
// x += s*y
template <typename OP, typename TA,
enable_if_t<std::is_same<OP,AsAdd>::value,bool> = true,
enable_if_t<is_constructible_v<SliceVector<double>,typename pair<T,TA>::first_type>,bool> = true,
enable_if_t<is_constructible_v<SliceVector<double>,TA>,bool> = true>
INLINE T & Assign (const Expr<ScaleExpr<TA,double>> & scaled)
{
AddVector (scaled.View().S(),
make_SliceVector(scaled.View().A()),
make_SliceVector(this->Spec()));
return Spec();
}
// x += s*(m*y)
template <typename OP, typename TA, typename TB,
enable_if_t<std::is_same_v<OP,AsAdd>,bool> = true,
enable_if_t<IsConvertibleToSliceMatrix<TA,double>(),bool> = true,
enable_if_t<is_convertible_v<TB,FlatVector<double>>,bool> = true,
enable_if_t<is_convertible<typename pair<T,TB>::first_type,FlatVector<double>>::value,bool> = true>
INLINE T & Assign (const Expr<ScaleExpr<MultExpr<TA, TB>,double>> & prod)
{
MultAddMatVec (prod.Spec().S(),
make_SliceMatrix(prod.Spec().A().A()),
prod.Spec().A().B(),
Spec());
return Spec();
}
// x += (s*m)*y
template <typename OP, typename TA, typename TB,
typename enable_if<std::is_same<OP,AsAdd>::value,int>::type = 0,
typename enable_if<IsConvertibleToSliceMatrix<TA,double>(),int>::type = 0,
typename enable_if<is_convertible<TB,FlatVector<double>>::value,int>::type = 0,
typename enable_if<is_convertible<typename pair<T,TB>::first_type,FlatVector<double>>::value,int>::type = 0>
INLINE T & Assign (const Expr<MultExpr<ScaleExpr<TA,double>, TB>> & prod)
{
MultAddMatVec (prod.Spec().A().S(),
make_SliceMatrix(prod.Spec().A().A()),
prod.Spec().B(),
Spec());
return Spec();
}
*/
template<typename TB>
INLINE T & operator= (const Expr<TB> & v)
{
Assign<As> (v);
return Spec();
}
INLINE T & operator= (const T & v)
{
Assign<As> (v);
return Spec();
}
template<typename TB>
INLINE T & operator+= (const Expr<TB> & v)
{
Assign<AsAdd> (v);
return Spec();
}
template<typename TB>
INLINE MatExpr<T> & operator-= (const Expr<TB> & v)
{
Assign<AsSub> (v);
return Spec();
}
template<typename TB>
INLINE T & operator+= (const Expr<SymExpr<TB> > & v)
{
NETGEN_CHECK_RANGE(Height(), v.Height(), v.Height()+1);
NETGEN_CHECK_RANGE(Width(), v.Width(), v.Width()+1);
size_t h = Height();
for (size_t i = 0; i < h; i++)
{
for (size_t j = 0; j < i; j++)
{
double val = v.Spec()(i,j);
Spec()(i,j) += val;
Spec()(j,i) += val;
}
Spec()(i,i) += v.Spec()(i,i);
}
return Spec();
}
template <class SCAL2>
INLINE T & operator*= (SCAL2 s)
{
if (T::IsLinear())
{
size_t hw = Height() * Width();
for (size_t i = 0; i < hw; i++)
Spec()(i) *= s;
}
else
for (size_t i = 0; i < Height(); i++)
for (size_t j = 0; j < Width(); j++)
Spec()(i,j) *= s;
return Spec();
}
template <class SCAL2>
INLINE T & operator/= (SCAL2 s)
{
return (*this) *= (1./s);
}
};
/* *************************** SumExpr **************************** */
/**
Sum of 2 matrix expressions
*/
template <class TA, class TB>
class SumExpr : public Expr<SumExpr<TA,TB> >
{
TA a;
TB b;
public:
static constexpr bool IsLinear() { return TA::IsLinear() && TB::IsLinear(); }
INLINE SumExpr (TA aa, TB ab) : a(aa), b(ab) { ; }
template <typename ...I>
INLINE auto operator() (I... i) const { return a(i...)+b(i...); }
INLINE auto Height() const { return CombinedSize(a.Height(), b.Height()); }
INLINE auto Width() const { return CombinedSize(a.Width(), b.Width()); }
INLINE auto View() const { return SumExpr(a,b); }
INLINE auto Shape() const { return CombinedSize(a.Shape(), b.Shape()); }
void Dump (ostream & ost) const
{ ost << "("; a.Dump(ost); ost << ") + ("; b.Dump(ost); ost << ")"; }
};
template <typename TA, typename TB>
INLINE auto
operator+ (const Expr<TA> & a, const Expr<TB> & b)
{
NETGEN_CHECK_SHAPE(a, b);
return SumExpr(a.View(), b.View());
}
/* *************************** SubExpr **************************** */
/**
Matrix-expr minus Matrix-expr
*/
template <class TA, class TB>
class SubExpr : public Expr<SubExpr<TA,TB> >
{
TA a;
TB b;
public:
static constexpr bool IsLinear() { return TA::IsLinear() && TB::IsLinear(); }
INLINE SubExpr (TA aa, TB ab) : a(aa), b(ab) { ; }
template <typename ...I>
INLINE auto operator() (I... i) const { return a(i...)-b(i...); }
INLINE auto View() const { return SubExpr(a,b); }
INLINE auto Shape() const { return CombinedSize(a.Shape(), b.Shape()); }
INLINE auto Height() const { return CombinedSize(a.Height(), b.Height()); }
INLINE auto Width() const { return CombinedSize(a.Width(), b.Width()); }
};
template <typename TA, typename TB>
inline auto operator- (const Expr<TA> & a, const Expr<TB> & b)
{
NETGEN_CHECK_SHAPE(a, b);
return SubExpr(a.View(), b.View());
}
/* *************************** MinusExpr **************************** */
/**
minus Matrix-expr
*/
template <class TA>
class MinusExpr : public Expr<MinusExpr<TA> >
{
TA a;
public:
MinusExpr (TA aa) : a(aa) { ; }
template <typename ...I>
INLINE auto operator() (I... i) const { return -a(i...); }
INLINE auto View() const { return MinusExpr(a); }
INLINE auto Shape() const { return a.Shape(); }
INLINE auto Height() const { return a.Height(); }
INLINE auto Width() const { return a.Width(); }
INLINE TA A() const { return a; }
static constexpr bool IsLinear() { return TA::IsLinear(); }
};
template <typename TA>
INLINE auto operator- (const Expr<TA> & a)
{
return MinusExpr (a.View());
}
/* *************************** PW_Mult_Expr **************************** */
template <class TA, class TB>
class PW_Mult_Expr : public Expr<PW_Mult_Expr<TA,TB> >
{
TA a;
TB b;
public:
static constexpr bool IsLinear() { return TA::IsLinear() && TB::IsLinear(); }
INLINE PW_Mult_Expr (TA aa, TB ab) : a(aa), b(ab) { ; }
INLINE auto operator() (size_t i) const { return a(i)*b(i); }
INLINE auto operator() (size_t i, size_t j) const { return a(i,j)*b(i,j); }
INLINE auto Height() const { return a.Height(); }
INLINE auto Width() const { return a.Width(); }
INLINE auto View() const { return *this; }
INLINE auto Shape() const { return CombinedSize(a.Shape(), b.Shape()); }
void Dump (ostream & ost) const
{ ost << "("; a.Dump(ost); ost << ") + ("; b.Dump(ost); ost << ")"; }
};
template <typename TA, typename TB>
INLINE auto pw_mult (const Expr<TA> & a, const Expr<TB> & b)
{
NETGEN_CHECK_SHAPE(a, b);
return PW_Mult_Expr (a.View(), b.View());
}
/* *************************** PW_Inv_Expr **************************** */
template <class TA>
class PW_Inv_Expr : public Expr<PW_Inv_Expr<TA> >
{
TA a;
public:
static constexpr bool IsLinear() { return TA::IsLinear(); }
INLINE PW_Inv_Expr (TA aa) : a(aa) { ; }
INLINE auto operator() (size_t i) const { return 1.0/a(i); }
INLINE auto operator() (size_t i, size_t j) const { return 1.0/a(i,j); }
INLINE size_t Height() const { return a.Height(); }
INLINE size_t Width() const { return a.Width(); }
INLINE auto View () const { return *this; }
void Dump (ostream & ost) const
{ ost << "1/("; a.Dump(ost); ost << ")"; }
};
template <typename TA>
INLINE auto pw_inv (const Expr<TA> & a)
{
return PW_Inv_Expr (a.View());
}
/* *************************** ScaleExpr **************************** */
/**
Scalar times Matrix-expr
*/
template <class TA, class TS>
class ScaleExpr : public Expr<ScaleExpr<TA,TS> >
{
TA a;
TS s;
public:
static constexpr bool IsLinear() { return TA::IsLinear(); }
INLINE ScaleExpr (TA aa, TS as) : a(aa), s(as) { ; }
// INLINE auto operator() (size_t i) const { return s * a(i); }
// INLINE auto operator() (size_t i, size_t j) const { return s * a(i,j); }
template <typename ...I>
INLINE auto operator() (I... i) const { return s*a(i...); }
INLINE auto Height() const { return a.Height(); }
INLINE auto Width() const { return a.Width(); }
INLINE auto View() const { return *this; }
INLINE auto Shape() const { return a.Shape(); }
INLINE TA A() const { return a; }
INLINE TS S() const { return s; }
void Dump (ostream & ost) const
{ ost << "Scale, s=" << s << " * "; a.Dump(ost); }
};
template <typename TS, typename TA,
typename enable_if<IsScalar<TS>(),int>::type = 0>
INLINE auto operator* (TS s, const Expr<TA> & a)
{
return ScaleExpr (a.View(), s);
}
/* ************************* MultExpr ************************* */
/**
Matrix-expr timex Matrix-expr
*/
template <class TA, class TB> class MultExpr : public Expr<MultExpr<TA,TB> >
{
TA a;
TB b;
public:
INLINE MultExpr (TA aa, TB ab) : a(aa), b(ab) { ; }
/*
INLINE auto operator() (size_t i) const
{ return operator()(i,0); }
INLINE auto operator() (size_t i, size_t j) const // -> decltype (a(0,0)*b(0,0))
{
size_t wa = a.Width();
if (wa >= 1)
{
auto sum = a(i,0) * b(0,j);
for (size_t k = 1; k < wa; k++)
sum += a(i,k) * b(k,j);
return sum;
}
decltype (a(0,0)*b(0,0)) sum (0);
return sum;
}