comparison +grid/evalOn.m @ 626:da30d3bbeea6 feature/grids

Avoid eval
author Jonatan Werpers <jonatan@werpers.com>
date Fri, 20 Oct 2017 23:13:43 +0200
parents 55e6267be117
children c602fe0a778c
comparison
equal deleted inserted replaced
625:55e6267be117 626:da30d3bbeea6
13 return 13 return
14 end 14 end
15 % func should now be a function_handle 15 % func should now be a function_handle
16 assert(g.D == nargin(func),'grid:evalOn:WrongNumberOfInputs', 'The number of inputs of the function must match the dimension of the domain.') 16 assert(g.D == nargin(func),'grid:evalOn:WrongNumberOfInputs', 'The number of inputs of the function must match the dimension of the domain.')
17 17
18 x = g.points(); 18 x = num2cell(g.points(),1);
19 k = numberOfComponents(func, x); 19 k = numberOfComponents(func, x);
20 20
21 % Evaluate gf = func(x(:,1),x(:,2),...,x(:,dim)); 21 gf = func(x{:});
22 if(g.D == 1)
23 gf = func(x);
24 else
25 eval_str = 'gf = func(x(:,1)';
26 for i = 2:g.D
27 eval_str = [eval_str, sprintf(',x(:,%d)',i)];
28 end
29 eval_str = [eval_str, ');'];
30 eval(eval_str);
31 end
32 22
33 % Reorganize gf 23 % Reorganize gf
34 gf_temp = gf; 24 gf_temp = gf;
35 gf = zeros(g.N*k, 1); 25 gf = zeros(g.N*k, 1);
36 for i = 1:k 26 for i = 1:k
38 end 28 end
39 end 29 end
40 30
41 % Find the number of vector components of func 31 % Find the number of vector components of func
42 function k = numberOfComponents(func, x) 32 function k = numberOfComponents(func, x)
43 if size(x,1) ~= 0 33 x0 = num2cell(ones(1,size(x,2)));
44 x0 = x(1,:);
45 else
46 x0 = num2cell(ones(1,size(x,2)));
47 end
48 34
49 dim = length(x0); 35 f0 = func(x0{:});
50 % Evaluate f0 = func(x0(1),x0(2),...,x0(dim));
51 if(dim == 1)
52 f0 = func(x0);
53 else
54 eval_str = 'f0 = func(x0(1)';
55 for i = 2:dim
56 eval_str = [eval_str, sprintf(',x0(%d)',i)];
57 end
58 eval_str = [eval_str, ');'];
59 eval(eval_str);
60 end
61
62 % k = number of components 36 % k = number of components
63 k = length(f0); 37 k = length(f0);
64 38
65 assert(size(f0,2) == 1, 'grid:evalOn:VectorValuedWrongDim', 'A vector valued function must be given as a column vector'); 39 assert(size(f0,2) == 1, 'grid:evalOn:VectorValuedWrongDim', 'A vector valued function must be given as a column vector');
66 end 40 end