comparison +grid/evalOn.m @ 704:111fcbcff2e9 feature/optim

merg with featuew grids
author Ylva Rydin <ylva.rydin@telia.com>
date Fri, 03 Nov 2017 10:53:15 +0100
parents 0609a72dcdfe
children 94f0f0b0d721
comparison
equal deleted inserted replaced
703:027f606fa691 704:111fcbcff2e9
5 % func -- Function to evaluate. May be a function handle or a constant. If 5 % func -- Function to evaluate. May be a function handle or a constant. If
6 % it is a vector value it has to be provided as a column vector, 6 % it is a vector value it has to be provided as a column vector,
7 function gf = evalOn(g, func) 7 function gf = evalOn(g, func)
8 if ~isa(func, 'function_handle') 8 if ~isa(func, 'function_handle')
9 % We should have a constant. 9 % We should have a constant.
10 if size(func,2) ~= 1 10 assert(size(func,2) == 1,'grid:evalOn:VectorValuedWrongDim', 'A vector valued function must be given as a column vector');
11 error('grid:evalOn:VectorValuedWrongDim', 'A vector valued function must be given as a column vector')
12 end
13 11
14 gf = repmat(func,[g.N, 1]); 12 gf = repmat(func,[g.N, 1]);
15 return 13 return
16 end 14 end
17 % 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.')
18 17
19 if g.D ~= nargin(func) 18 x = num2cell(g.points(),1);
20 g.D 19 k = numberOfComponents(func);
21 nargin(func)
22 error('grid:evalOn:WrongNumberOfInputs', 'The number of inputs of the function must match the dimension of the domain.')
23 end
24 20
21 gf = func(x{:});
22 gf = reorderComponents(gf, k);
23 end
25 24
26 % Get coordinates and convert to cell array for easier use as a parameter 25 % Find the number of vector components of func
27 x = num2cell(g.points()); 26 function k = numberOfComponents(func)
27 x0 = num2cell(ones(1,nargin(func)));
28 f0 = func(x0{:});
29 assert(size(f0,2) == 1, 'grid:evalOn:VectorValuedWrongDim', 'A vector valued function must be given as a column vector');
30 k = length(f0);
31 end
28 32
29 % Find the number of components 33 % Reorder the components of the function to sit together
30 x0 = x(1,:); 34 function gf = reorderComponents(a, k)
31 f0 = func(x0{:}); 35 N = length(a)/k;
32 k = length(f0); 36 gf = zeros(N*k, 1);
33 37 for i = 1:k
34 if size(f0,2) ~= 1 38 gf(i:k:end) = a((i-1)*N + 1 : i*N);
35 error('grid:evalOn:VectorValuedWrongDim', 'A vector valued function must be given as a column vector')
36 end
37
38 gf = zeros(g.N*k, 1);
39 % keyboard
40 for i = 1:g.N
41 % (1 + (i-1)*k):(i*k)
42 % func(x{i,:})
43 gf((1 + (i-1)*k):(i*k)) = func(x{i,:});
44 end 39 end
45 end 40 end