Mercurial > repos > public > sbplib
comparison +grid/evalOn.m @ 624:0e20f4c9a94e feature/grids
Factor out function for calculating the number of components
author | Jonatan Werpers <jonatan@werpers.com> |
---|---|
date | Fri, 20 Oct 2017 22:59:30 +0200 |
parents | 190941ec12d8 |
children | 55e6267be117 |
comparison
equal
deleted
inserted
replaced
623:190941ec12d8 | 624:0e20f4c9a94e |
---|---|
18 | 18 |
19 if g.D ~= nargin(func) | 19 if g.D ~= nargin(func) |
20 error('grid:evalOn:WrongNumberOfInputs', 'The number of inputs of the function must match the dimension of the domain.') | 20 error('grid:evalOn:WrongNumberOfInputs', 'The number of inputs of the function must match the dimension of the domain.') |
21 end | 21 end |
22 | 22 |
23 x = g.points(); | |
24 k = numberOfComponents(func, x); | |
23 | 25 |
24 % Get coordinates | 26 % Evaluate gf = func(x(:,1),x(:,2),...,x(:,dim)); |
25 x = g.points(); | 27 if(g.D == 1) |
28 gf = func(x); | |
29 else | |
30 eval_str = 'gf = func(x(:,1)'; | |
31 for i = 2:g.D | |
32 eval_str = [eval_str, sprintf(',x(:,%d)',i)]; | |
33 end | |
34 eval_str = [eval_str, ');']; | |
35 eval(eval_str); | |
36 end | |
26 | 37 |
27 % Find the number of components | 38 % Reorganize gf |
39 gf_temp = gf; | |
40 gf = zeros(g.N*k, 1); | |
41 for i = 1:k | |
42 gf(i:k:end) = gf_temp((i-1)*g.N + 1 : i*g.N); | |
43 end | |
44 end | |
45 | |
46 % Find the number of vector components of func | |
47 function k = numberOfComponents(func, x) | |
28 if size(x,1) ~= 0 | 48 if size(x,1) ~= 0 |
29 x0 = x(1,:); | 49 x0 = x(1,:); |
30 else | 50 else |
31 x0 = num2cell(ones(1,size(x,2))); | 51 x0 = num2cell(ones(1,size(x,2))); |
32 end | 52 end |
48 k = length(f0); | 68 k = length(f0); |
49 | 69 |
50 if size(f0,2) ~= 1 | 70 if size(f0,2) ~= 1 |
51 error('grid:evalOn:VectorValuedWrongDim', 'A vector valued function must be given as a column vector') | 71 error('grid:evalOn:VectorValuedWrongDim', 'A vector valued function must be given as a column vector') |
52 end | 72 end |
53 | |
54 % Evaluate gf = func(x(:,1),x(:,2),...,x(:,dim)); | |
55 if(dim == 1) | |
56 gf = func(x); | |
57 else | |
58 eval_str = 'gf = func(x(:,1)'; | |
59 for i = 2:dim | |
60 eval_str = [eval_str, sprintf(',x(:,%d)',i)]; | |
61 end | |
62 eval_str = [eval_str, ');']; | |
63 eval(eval_str); | |
64 end | |
65 | |
66 % Reorganize gf | |
67 gf_temp = gf; | |
68 gf = zeros(g.N*k, 1); | |
69 for i = 1:k | |
70 gf(i:k:end) = gf_temp((i-1)*g.N + 1 : i*g.N); | |
71 end | |
72 end | 73 end |