comparison +grid/evalOn.m @ 607:0546de4b31a2 feature/grids

Make grid.evalOn faster.
author Martin Almquist <malmquist@stanford.edu>
date Sat, 14 Oct 2017 22:28:27 -0700
parents d196b7cdc626
children 0813d700a816
comparison
equal deleted inserted replaced
606:c14875cf7ae6 607:0546de4b31a2
21 nargin(func) 21 nargin(func)
22 error('grid:evalOn:WrongNumberOfInputs', 'The number of inputs of the function must match the dimension of the domain.') 22 error('grid:evalOn:WrongNumberOfInputs', 'The number of inputs of the function must match the dimension of the domain.')
23 end 23 end
24 24
25 25
26 % Get coordinates and convert to cell array for easier use as a parameter 26 % Get coordinates
27 x = num2cell(g.points()); 27 x = g.points();
28 28
29 % Find the number of components 29 % Find the number of components
30 if size(x,1) ~= 0 30 if size(x,1) ~= 0
31 x0 = x(1,:); 31 x0 = x(1,:);
32 else 32 else
33 x0 = num2cell(ones(1,size(x,2))); 33 x0 = num2cell(ones(1,size(x,2)));
34 end 34 end
35 f0 = func(x0{:}); 35
36 dim = length(x0);
37 % Evaluate f0 = func(x0(1),x0(2),...,x0(dim));
38 if(dim == 1)
39 f0 = func(x0);
40 else
41 eval_str = 'f0 = func(x0(1)';
42 for i = 2:dim
43 eval_str = [eval_str, sprintf(',x0(%d)',i)];
44 end
45 eval_str = [eval_str, ');'];
46 eval(eval_str);
47 end
48
49 % k = number of components
36 k = length(f0); 50 k = length(f0);
37 51
38 if size(f0,2) ~= 1 52 if size(f0,2) ~= 1
39 error('grid:evalOn:VectorValuedWrongDim', 'A vector valued function must be given as a column vector') 53 error('grid:evalOn:VectorValuedWrongDim', 'A vector valued function must be given as a column vector')
40 end 54 end
41 55
56 % Evaluate gf = func(x(:,1),x(:,2),...,x(:,dim));
57 if(dim == 1)
58 gf = func(x);
59 else
60 eval_str = 'gf = func(x(:,1)';
61 for i = 2:dim
62 eval_str = [eval_str, sprintf(',x(:,%d)',i)];
63 end
64 eval_str = [eval_str, ');'];
65 eval(eval_str);
66 end
67
68 % Reorganize gf
69 gf_temp = gf;
42 gf = zeros(g.N*k, 1); 70 gf = zeros(g.N*k, 1);
43 for i = 1:g.N 71 for i = 1:k
44 % (1 + (i-1)*k):(i*k) 72 gf(i:k:end) = gf_temp((i-1)*g.N + 1 : i*g.N);
45 % func(x{i,:})
46 gf((1 + (i-1)*k):(i*k)) = func(x{i,:});
47 end 73 end
48 end 74 end