Mercurial > repos > public > sbplib
comparison +grid/evalOn.m @ 886:8894e9c49e40 feature/timesteppers
Merge with default for latest changes
author | Vidar Stiernström <vidar.stiernstrom@it.uu.se> |
---|---|
date | Thu, 15 Nov 2018 16:36:21 -0800 |
parents | 77451445955f |
children |
comparison
equal
deleted
inserted
replaced
816:b5e5b195da1e | 886:8894e9c49e40 |
---|---|
1 % Takes a function and evaluates it on a grid to return a grid function in the | |
2 % form of a (n*k)x1 vector, where n is the number of grid points and k is the | |
3 % number of components of the function. | |
4 % g -- Grid to evaluate on. | |
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, | |
7 function gf = evalOn(g, func) | |
8 if ~isa(func, 'function_handle') | |
9 % We should have a constant. | |
10 assert(size(func,2) == 1,'grid:evalOn:VectorValuedWrongDim', 'A vector valued function must be given as a column vector'); | |
11 | |
12 gf = repmat(func,[g.N, 1]); | |
13 return | |
14 end | |
15 % func should now be a function_handle | |
16 assert(g.D == nargin(func) || nargin(func) < 0,'grid:evalOn:WrongNumberOfInputs', 'The number of inputs of the function must match the dimension of the domain.') | |
17 | |
18 x = num2cell(g.points(),1); | |
19 k = numberOfComponents(func, g.D); | |
20 | |
21 gf = func(x{:}); | |
22 gf = reorderComponents(gf, k); | |
23 end | |
24 | |
25 % Find the number of vector components of func | |
26 function k = numberOfComponents(func, dim) | |
27 x0 = num2cell(ones(1, dim)); | |
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 | |
32 | |
33 % Reorder the components of the function to sit together | |
34 function gf = reorderComponents(a, k) | |
35 N = length(a)/k; | |
36 gf = zeros(N*k, 1); | |
37 for i = 1:k | |
38 gf(i:k:end) = a((i-1)*N + 1 : i*N); | |
39 end | |
40 end |