-
Notifications
You must be signed in to change notification settings - Fork 1
/
generate_moments_GS.m
272 lines (257 loc) · 9.16 KB
/
generate_moments_GS.m
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
%% generate_moments_GS
% Generates the moments corresponding to the basis of DOPs \pi_k
% We follow the Gram-Schmidt procedure to compute the moments of the DOPs
%
% INPUT:
% Sample : Sample for the data points
% d : degree of exactness
% domain : domain
% weightFun : weight function
%
% OUTPUT:
% A : matrix which contains the values of \pi_k at the points Y
function m = generate_moments_GS( Sample, d, domain, weightFun )
N = Sample.N; % number of data points
X = Sample.coord; % data points
K = nchoosek(Sample.dim + d, Sample.dim); % binomial coefficient/ number of DOPs
B = zeros(K,N); % auxiliary matrix
m = zeros(K,1); % moments
%% moments of the monomials
m_monom = zeros(K,1);
% cube
if strcmp( domain, 'cube')
% omega = 1
if strcmp( weightFun, '1')
if Sample.dim == 1
for k=0:2:d
m_monom(k+1) = 2/(k+1); % moment
end
elseif Sample.dim == 2
k = 1;
for k1=0:1:d
for k2=0:1:d
if k1+k2<=d
% k1 and k2 need to be even
if mod(k1,2) == 0 && mod(k2,2) == 0
m_monom(k) = 4/( (k1+1)*(k2+1) ); % moment
end
k = k+1;
end
end
end
elseif Sample.dim == 3
k = 1;
for k1=0:d
for k2=0:d
for k3=0:d
if k1+k2+k3<=d
% k1, k2, and k3 need to be even
if mod(k1,2) == 0 && mod(k2,2) == 0 && mod(k3,2) == 0
m_monom(k) = 8/( (k1+1)*(k2+1)*(k3+1) ); % moment
end
k = k+1;
end
end
end
end
else
error('Desired dimension not yet implemented!')
end
% omega = sqrt(1-x^2) (C2k)
elseif strcmp( weightFun, 'C2k')
m_aux = zeros(d+1,1);
m_aux(1) = 0.5*pi;
for k=2:2:d
m_aux(k+1) = ( (k-1)/(k+2) )*m_aux(k-1);
end
if Sample.dim == 1
m_monom = m_aux; % moments
elseif Sample.dim == 2
k = 1;
for k1=0:1:d
for k2=0:1:d
if k1+k2<=d
m_monom(k) = m_aux(k1+1)*m_aux(k2+1); % moment
k = k+1;
end
end
end
elseif Sample.dim == 3
k = 1;
for k1=0:d
for k2=0:d
for k3=0:d
if k1+k2+k3<=d
m_monom(k) = m_aux(k1+1)*m_aux(k2+1)*m_aux(k3+1); % moment
k = k+1;
end
end
end
end
else
error('Desired dimension not yet implemented!')
end
else
error('Desired weight function not yet implemented!')
end
% ball
elseif strcmp( domain, 'ball')
% omega = 1
if strcmp( weightFun, '1')
if Sample.dim == 1
for k=0:2:d
m_monom(k+1) = 2/(k+1); % moment
end
elseif Sample.dim == 2
k = 1;
for k1=0:d
for k2=0:d
if k1+k2<=d
% k1 and k2 need to be even
if mod(k1,2) == 0 && mod(k2,2) == 0
b1 = 0.5*(k1+1);
b2 = 0.5*(k2+1);
m_monom(k) = ( 2*gamma(b1)*gamma(b2)/gamma(b1+b2) )/(k1+k2+Sample.dim); % moment
end
k = k+1;
end
end
end
elseif Sample.dim == 3
k = 1;
for k1=0:d
for k2=0:d
for k3=0:d
if k1+k2+k3<=d
% k1, k2, and k3 need to be even
if mod(k1,2) == 0 && mod(k2,2) == 0 && mod(k3,2) == 0
b1 = 0.5*(k1+1);
b2 = 0.5*(k2+1);
b3 = 0.5*(k3+1);
m_monom(k) = ( 2*gamma(b1)*gamma(b2)*gamma(b3)/gamma(b1+b2+b3) )/(k1+k2+k3+Sample.dim); % moment
end
k = k+1;
end
end
end
end
else
error('Desired dimension not yet implemented!')
end
% omega = sqrt(r)
elseif strcmp( weightFun, 'sqrt(r)')
if Sample.dim == 1
for k=0:2:d
m_monom(k+1) = 2/(k+Sample.dim+0.5); % moment
end
elseif Sample.dim == 2
k = 1;
for k1=0:d
for k2=0:d
if k1+k2<=d
% k1 and k2 need to be even
if mod(k1,2) == 0 && mod(k2,2) == 0
b1 = 0.5*(k1+1);
b2 = 0.5*(k2+1);
m_monom(k) = ( 2*gamma(b1)*gamma(b2)/gamma(b1+b2) )/(k1+k2+Sample.dim+0.5); % moment
end
k = k+1;
end
end
end
elseif Sample.dim == 3
k = 1;
for k1=0:d
for k2=0:d
for k3=0:d
if k1+k2+k3<=d
% k1, k2, and k3 need to be even
if mod(k1,2) == 0 && mod(k2,2) == 0 && mod(k3,2) == 0
b1 = 0.5*(k1+1);
b2 = 0.5*(k2+1);
b3 = 0.5*(k3+1);
m_monom(k) = ( 2*gamma(b1)*gamma(b2)*gamma(b3)/gamma(b1+b2+b3) )/(k1+k2+k3+Sample.dim+0.5); % moment
end
k = k+1;
end
end
end
end
else
error('Desired dimension not yet implemented!')
end
else
error('Desired weight function not yet implemented!')
end
% else
else
error('Desired domain not yet implemented!')
end
%% 1st DOP and moment
x_aux = ones(1,N);
x_aux_norm = sqrt( dot(x_aux.^2,Sample.r) ); % norm
B(1,:) = x_aux./x_aux_norm; % normalize
m(1) = m_monom(1)/x_aux_norm; % moment
%% dim = 1
if Sample.dim == 1
for k=2:d+1
x_aux = X.^(k-1)'; % values of e_k at the data points
m(k) = m_monom(k); % moment
% GS procedure
for i=1:k
inner_prod = dot(x_aux.*B(i,:),Sample.r); % inner product
x_aux = x_aux - inner_prod*B(i,:); % modified GM
m(k) = m(k) - inner_prod*m(i); % moment
end
x_aux_norm = sqrt( dot(x_aux.^2,Sample.r) ); % norm
B(k,:) = x_aux./x_aux_norm; % normalize
m(k) = m(k)/x_aux_norm; % moment
end
%% dim = 2
elseif Sample.dim == 2
k = 2;
for k1=0:d
for k2=0:d
if k1+k2>=1 && k1+k2<=d
x_aux = ( (X(:,1).^k1).*(X(:,2).^k2) )'; % values of e_k at the data points
m(k) = m_monom(k); % moment
% GS procedure
for i = 1:k % modified GS
inner_prod = dot(x_aux.*B(i,:),Sample.r); % inner product
x_aux = x_aux - inner_prod*B(i,:); % modified GM
m(k) = m(k) - inner_prod*m(i); % moment
end
x_aux_norm = sqrt( dot(x_aux.^2,Sample.r) ); % norm
B(k,:) = x_aux./x_aux_norm; % normalize
m(k) = m(k)/x_aux_norm; % moment
k = k+1;
end
end
end
%% dim = 3
elseif Sample.dim == 3
k = 2;
for k1=0:d
for k2=0:d
for k3=0:d
if k1+k2+k3>=1 && k1+k2+k3<=d
x_aux = ( (X(:,1).^k1).*(X(:,2).^k2).*(X(:,3).^k3) )'; % values of e_k at the data points
m(k) = m_monom(k); % moment
% GS procedure
for i = 1:k % modified GS
inner_prod = dot(x_aux.*B(i,:),Sample.r); % inner product
x_aux = x_aux - inner_prod*B(i,:); % modified GM
m(k) = m(k) - inner_prod*m(i); % moment
end
x_aux_norm = sqrt( dot(x_aux.^2,Sample.r) ); % norm
B(k,:) = x_aux./x_aux_norm; % normalize
m(k) = m(k)/x_aux_norm; % moment
k = k+1;
end
end
end
end
else
error('Desired dimension not yet implemented!')
end
end