changeset 275:3ea2ae2a3d15 feature/beams

Improvments to eval on grids. Added function to extrac components of a vector gf.
author Jonatan Werpers <jonatan@werpers.com>
date Tue, 06 Sep 2016 17:28:11 +0200
parents ac8e00883986
children 30321dc180e1
files +grid/evalOn.m +grid/evalOnScalar.m +grid/funcToComponents.m +grid/funcToComponentsTest.m
diffstat 4 files changed, 43 insertions(+), 11 deletions(-) [+]
line wrap: on
line diff
diff -r ac8e00883986 -r 3ea2ae2a3d15 +grid/evalOn.m
--- a/+grid/evalOn.m	Tue Sep 06 15:54:45 2016 +0200
+++ b/+grid/evalOn.m	Tue Sep 06 17:28:11 2016 +0200
@@ -17,14 +17,10 @@
     % func should now be a function_handle
 
     % Get coordinates and convert to cell array for easier use as a parameter
-    x = g.points();
-    X = {};
-    for i = 1:size(x, 2)
-        X{i} = x(:,i);
-    end
+    x = num2cell(g.points());
 
     % Find the number of components
-    x0 = num2cell(x(1,:));
+    x0 = x(1,:);
     f0 = func(x0{:});
     k = length(f0);
 
@@ -32,8 +28,11 @@
         error('grid:evalOn:VectorValuedWrongDim', 'A vector valued function must be given as a column vector')
     end
 
-    gf = func(X{:});
-    if  k > 1  % Reorder so that vectors sit together.
-        gf = reshape(reshape(gf, [g.N, k])', [g.N*k, 1]);
+    gf = zeros(g.N*k, 1);
+    % keyboard
+    for i = 1:g.N
+        % (1 + (i-1)*k):(i*k)
+        % func(x{i,:})
+        gf((1 + (i-1)*k):(i*k)) = func(x{i,:});
     end
 end
\ No newline at end of file
diff -r ac8e00883986 -r 3ea2ae2a3d15 +grid/evalOnScalar.m
--- a/+grid/evalOnScalar.m	Tue Sep 06 15:54:45 2016 +0200
+++ b/+grid/evalOnScalar.m	Tue Sep 06 17:28:11 2016 +0200
@@ -1,3 +1,5 @@
+% WHAT KIND OF A FUNCTION NAME IS THIS?!
+%  This functions send matrixa arguments to func unlike grid.evalOn()
 % Takes a funciton and evaluates it on a grid to return a grid function in the
 % form of a (n*k)x1 vector, where n is the number of grid points and k is the
 % number of components of the function.
@@ -8,7 +10,7 @@
     if ~isa(func, 'function_handle')
         % We should have a constant.
         if size(func,2) ~= 1
-            error('grid:evalOn:VectorValuedWrongDim', 'A vector valued function must be given as a column vector')
+            error('grid:evalOnScalar:VectorValuedWrongDim', 'A vector valued function must be given as a column vector')
         end
 
         gf = repmat(func,[g.N, 1]);
@@ -29,7 +31,7 @@
     k = length(f0);
 
     if size(f0,2) ~= 1
-        error('grid:evalOn:VectorValuedWrongDim', 'A vector valued function must be given as a column vector')
+        error('grid:evalOnScalar:VectorValuedWrongDim', 'A vector valued function must be given as a column vector')
     end
 
     gf = func(X{:});
diff -r ac8e00883986 -r 3ea2ae2a3d15 +grid/funcToComponents.m
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/+grid/funcToComponents.m	Tue Sep 06 17:28:11 2016 +0200
@@ -0,0 +1,8 @@
+% funcToComponents converts a grid function to a N x k matrix, where
+% k is the number of vector components of the gridfunction and N is the
+% number of points in the grid.
+%
+% Takes a grid function and and a grid.
+function F = funcToComponents(g, gf);
+    F = reshapeRowMaj(gf, [g.N, length(gf)/g.N]);
+end
\ No newline at end of file
diff -r ac8e00883986 -r 3ea2ae2a3d15 +grid/funcToComponentsTest.m
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/+grid/funcToComponentsTest.m	Tue Sep 06 17:28:11 2016 +0200
@@ -0,0 +1,23 @@
+function tests = funcToComponentsTest()
+    tests = functiontests(localfunctions);
+end
+
+
+function testScalarGf(testCase)
+    g = getTestGrid();
+    gf_in = [1; 2; 3];
+
+    testCase.verifyEqual(grid.funcToComponents(g, gf_in), gf_in);
+end
+
+function testVectorGf(testCase)
+    g = getTestGrid();
+    gf_in = [1; 2; 3; 4; 5; 6];
+    out = [1 2; 3 4; 5 6];
+
+    testCase.verifyEqual(grid.funcToComponents(g, gf_in), out);
+end
+
+function g = getTestGrid()
+    g = grid.equidistant(3,{0,2});
+end
\ No newline at end of file