-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathold_mvoperators.hpp
134 lines (89 loc) · 2.4 KB
/
old_mvoperators.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
#ifndef MVOPERATORS_H
#define MVOPERATORS_H
namespace mathq {
// outer product: vector ^ vector
template <class D, class A, class B>
inline Matrix<D>
operator^(const TensorR<D,A>& a, const TensorR<D,B>& b)
{
const size_type NC = a.size();
const size_type NR = b.size();
Matrix<D> y(NR,NC);
register index_type i=0;
for(register index_type r=0; r < NR; r++)
for(register index_type c=0; c < NC; c++,i++)
y(i) = a[c]*b[r];
return y;
}
// vector = diagonal(Matrix)
template <class D, class A>
inline Vector<D>
diag(const MorE<D,A>& a)
{
size_type Nmin;
if (a.Nrows() < a.Ncols())
Nmin=a.Nrows();
else
Nmin=a.Ncols();
const size_type N=Nmin;
Vector<D> v(N);
for (register index_type i = 0; i < N ; i++ )
v[i] = a(i,i);
return v;
}
// diagmat(vector)
template <class D, class A>
inline Matrix<D>
diagmat(const TensorR<D,A>& a)
{
const size_type N = a.size();
Matrix<D> y(N,N);
for(register index_type i=0; i < N; i++)
y(i,i) = a[i];
return y;
}
// dotproduct operator (matrix|vector)
template <class D, class A, class B>
inline Vector<D>
operator|( const MorE<D,A>& a, const TensorR<D,B>& b ) {
const size_type NR = a.Nrows();
const size_type M = a.Ncols();
const index_type C1 = NR*M;
const index_type C2 = M-1;
Vector<D> y(NR);
register index_type i = 0;
for(register index_type n=0; n < C1; n+=M, i++) {
index_type j = n;
index_type k = 0;
// using a local variable for the accumation saves a lot of CPU Time!!
D result = a(j) * b[k];
do {
result += a((j+=1)) * b[(k+=1)];
} while (k<C2);
y[i] = result;
}
return y;
}
// dotproduct operator (vector|matrix)
template <class D, class A, class B>
inline Vector<D>
operator|( const TensorR<D,A>& a, const MorE<D,B>& b ) {
const size_type NC = b.Ncols();
const size_type M = b.Nrows();
Vector<D> y(NC);
register index_type i = 0;
for(register index_type c=0; c < NC; c++,i++) {
index_type j = 0;
index_type k = c;
const index_type C2 = M-1;
// using a local variable for the accumation saves a lot of CPU Time!!
D result = a[j] * b(k);
do {
result += a[(j+=1)] * b((k+=NC));
} while (j<C2);
y[i] = result;
}
return y;
}
};
#endif