Mercurial > repos > public > sbplib
changeset 162:c75c03f692b3 feature/grids
Moved function for resizing vectors out of grid.
author | Jonatan Werpers <jonatan@werpers.com> |
---|---|
date | Mon, 22 Feb 2016 13:34:50 +0100 |
parents | 73bc43c7379e |
children | 51aaf67a7df5 |
files | +grid/funcToMatrix.m +grid/funcToMatrixTest.m +grid/funcToPlotMatrix.m +grid/funcToPlotMatrixTest.m reshapeKronVector.m reshapeKronVectorTest.m reshapeToPlotMatrix.m reshapeToPlotMatrixTest.m |
diffstat | 8 files changed, 176 insertions(+), 176 deletions(-) [+] |
line wrap: on
line diff
diff -r 73bc43c7379e -r c75c03f692b3 +grid/funcToMatrix.m --- a/+grid/funcToMatrix.m Mon Feb 22 13:20:55 2016 +0100 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,13 +0,0 @@ -% Takes a grid function and reshapes it into a matrix of shape m. -% Called by class methods. -function F = funcToMatrix(gf, m) - D = length(m); - - if D == 1 - F = gf; - return - end - - % Reshape and reverse order of indecies - F = permute(reshape(gf, rot90(m,2)), D:-1:1); -end \ No newline at end of file
diff -r 73bc43c7379e -r c75c03f692b3 +grid/funcToMatrixTest.m --- a/+grid/funcToMatrixTest.m Mon Feb 22 13:20:55 2016 +0100 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,95 +0,0 @@ -function tests = funcToMatrixTest() - tests = functiontests(localfunctions); -end - -function test1D(testCase) - inGf = [1 2 3 4 5]'; - inM = 5; - out = [1 2 3 4 5]'; - testCase.verifyEqual(grid.funcToMatrix(inGf, inM),out); -end - -function test2D(testCase) - inGf = [11; 12; 21; 22]; - inM = [2, 2]; - - out(1,1) = 11; - out(1,2) = 12; - out(2,1) = 21; - out(2,2) = 22; - - testCase.verifyEqual(grid.funcToMatrix(inGf, inM),out); -end - -function test3D(testCase) - inGf = [111; 112; 121; 122; 211; 212; 221; 222]; - inM = [2, 2, 2]; - - out(1,1,1) = 111; - out(1,1,2) = 112; - out(1,2,1) = 121; - out(1,2,2) = 122; - out(2,1,1) = 211; - out(2,1,2) = 212; - out(2,2,1) = 221; - out(2,2,2) = 222; - - testCase.verifyEqual(grid.funcToMatrix(inGf, inM),out); -end - -function testNonSquare(testCase) - inGf = [ - 111; - 112; - 113; - 114; - 121; - 122; - 123; - 124; - 131; - 132; - 133; - 134; - 211; - 212; - 213; - 214; - 221; - 222; - 223; - 224; - 231; - 232; - 233; - 234; - ]; - inM = [2, 3, 4]; - - out(1,1,1) = 111; - out(1,1,2) = 112; - out(1,1,3) = 113; - out(1,1,4) = 114; - out(1,2,1) = 121; - out(1,2,2) = 122; - out(1,2,3) = 123; - out(1,2,4) = 124; - out(1,3,1) = 131; - out(1,3,2) = 132; - out(1,3,3) = 133; - out(1,3,4) = 134; - out(2,1,1) = 211; - out(2,1,2) = 212; - out(2,1,3) = 213; - out(2,1,4) = 214; - out(2,2,1) = 221; - out(2,2,2) = 222; - out(2,2,3) = 223; - out(2,2,4) = 224; - out(2,3,1) = 231; - out(2,3,2) = 232; - out(2,3,3) = 233; - out(2,3,4) = 234; - - testCase.verifyEqual(grid.funcToMatrix(inGf, inM), out); -end \ No newline at end of file
diff -r 73bc43c7379e -r c75c03f692b3 +grid/funcToPlotMatrix.m --- a/+grid/funcToPlotMatrix.m Mon Feb 22 13:20:55 2016 +0100 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,20 +0,0 @@ -% Takes a grid function and reshapes it into a matrix of shape m for plotting. -% Called by class methods. -function F = funcToPlotMatrix(gf, m) - D = length(m); - - - - switch D - case 1 - F = gf; - case 2 - F = reshape(gf, rot90(m,2)); - case 3 - % After the reshape the indecies will be M(z,y,x). Plot need them to be M(y,x,z) - p = [2 3 1]; % Permuation - F = permute(reshape(gf,rot90(m,2)), p); - otherwise - error('grid:funcToMatrix:NotImplemented','Grid function to matrix is not implemented for dimension = %d', length(m)); - end -end \ No newline at end of file
diff -r 73bc43c7379e -r c75c03f692b3 +grid/funcToPlotMatrixTest.m --- a/+grid/funcToPlotMatrixTest.m Mon Feb 22 13:20:55 2016 +0100 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,48 +0,0 @@ -function tests = funcToPlotMatrixTest() - tests = functiontests(localfunctions); -end - -function test1D(testCase) - inGf = [1 2 3 4 5]'; - inM = 5; - out = [1 2 3 4 5]'; - testCase.verifyEqual(grid.funcToPlotMatrix(inGf, inM),out); -end - -function test2D(testCase) - x = 1:2; - y = 1:3; - - f = @(x,y) x + y*10; - - xx = [1; 1; 1; 2; 2; 2]; - yy = [1; 2; 3; 1; 2; 3]; - inGf = f(xx,yy); - - [X,Y] = meshgrid(x,y); - out = f(X,Y); - - inM = [2, 3]; - - testCase.verifyEqual(grid.funcToPlotMatrix(inGf, inM),out); -end - -function test3D(testCase) - x = 1:2; - y = 1:3; - z = 1:4; - - f = @(x,y,z) x + y*10 + z*100; - - xx = [repmat(1, [12, 1]); repmat(2, [12, 1])]; - yy = repmat([1; 1; 1; 1; 2; 2; 2; 2; 3; 3; 3; 3], [2, 1]); - zz = repmat([1; 2; 3; 4], [6, 1]); - inGf = f(xx,yy,zz); - - [X,Y,Z] = meshgrid(x,y,z); - out = f(X,Y,Z); - - inM = [2, 3, 4]; - - testCase.verifyEqual(grid.funcToPlotMatrix(inGf, inM),out); -end \ No newline at end of file
diff -r 73bc43c7379e -r c75c03f692b3 reshapeKronVector.m --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/reshapeKronVector.m Mon Feb 22 13:34:50 2016 +0100 @@ -0,0 +1,13 @@ +% Takes a grid function and reshapes it into a matrix of shape m. +% Called by class methods. +function F = funcToMatrix(gf, m) + D = length(m); + + if D == 1 + F = gf; + return + end + + % Reshape and reverse order of indecies + F = permute(reshape(gf, rot90(m,2)), D:-1:1); +end \ No newline at end of file
diff -r 73bc43c7379e -r c75c03f692b3 reshapeKronVectorTest.m --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/reshapeKronVectorTest.m Mon Feb 22 13:34:50 2016 +0100 @@ -0,0 +1,95 @@ +function tests = funcToMatrixTest() + tests = functiontests(localfunctions); +end + +function test1D(testCase) + inGf = [1 2 3 4 5]'; + inM = 5; + out = [1 2 3 4 5]'; + testCase.verifyEqual(grid.funcToMatrix(inGf, inM),out); +end + +function test2D(testCase) + inGf = [11; 12; 21; 22]; + inM = [2, 2]; + + out(1,1) = 11; + out(1,2) = 12; + out(2,1) = 21; + out(2,2) = 22; + + testCase.verifyEqual(grid.funcToMatrix(inGf, inM),out); +end + +function test3D(testCase) + inGf = [111; 112; 121; 122; 211; 212; 221; 222]; + inM = [2, 2, 2]; + + out(1,1,1) = 111; + out(1,1,2) = 112; + out(1,2,1) = 121; + out(1,2,2) = 122; + out(2,1,1) = 211; + out(2,1,2) = 212; + out(2,2,1) = 221; + out(2,2,2) = 222; + + testCase.verifyEqual(grid.funcToMatrix(inGf, inM),out); +end + +function testNonSquare(testCase) + inGf = [ + 111; + 112; + 113; + 114; + 121; + 122; + 123; + 124; + 131; + 132; + 133; + 134; + 211; + 212; + 213; + 214; + 221; + 222; + 223; + 224; + 231; + 232; + 233; + 234; + ]; + inM = [2, 3, 4]; + + out(1,1,1) = 111; + out(1,1,2) = 112; + out(1,1,3) = 113; + out(1,1,4) = 114; + out(1,2,1) = 121; + out(1,2,2) = 122; + out(1,2,3) = 123; + out(1,2,4) = 124; + out(1,3,1) = 131; + out(1,3,2) = 132; + out(1,3,3) = 133; + out(1,3,4) = 134; + out(2,1,1) = 211; + out(2,1,2) = 212; + out(2,1,3) = 213; + out(2,1,4) = 214; + out(2,2,1) = 221; + out(2,2,2) = 222; + out(2,2,3) = 223; + out(2,2,4) = 224; + out(2,3,1) = 231; + out(2,3,2) = 232; + out(2,3,3) = 233; + out(2,3,4) = 234; + + testCase.verifyEqual(grid.funcToMatrix(inGf, inM), out); +end \ No newline at end of file
diff -r 73bc43c7379e -r c75c03f692b3 reshapeToPlotMatrix.m --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/reshapeToPlotMatrix.m Mon Feb 22 13:34:50 2016 +0100 @@ -0,0 +1,20 @@ +% Takes a grid function and reshapes it into a matrix of shape m for plotting. +% Called by class methods. +function F = funcToPlotMatrix(gf, m) + D = length(m); + + + + switch D + case 1 + F = gf; + case 2 + F = reshape(gf, rot90(m,2)); + case 3 + % After the reshape the indecies will be M(z,y,x). Plot need them to be M(y,x,z) + p = [2 3 1]; % Permuation + F = permute(reshape(gf,rot90(m,2)), p); + otherwise + error('grid:funcToMatrix:NotImplemented','Grid function to matrix is not implemented for dimension = %d', length(m)); + end +end \ No newline at end of file
diff -r 73bc43c7379e -r c75c03f692b3 reshapeToPlotMatrixTest.m --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/reshapeToPlotMatrixTest.m Mon Feb 22 13:34:50 2016 +0100 @@ -0,0 +1,48 @@ +function tests = funcToPlotMatrixTest() + tests = functiontests(localfunctions); +end + +function test1D(testCase) + inGf = [1 2 3 4 5]'; + inM = 5; + out = [1 2 3 4 5]'; + testCase.verifyEqual(grid.funcToPlotMatrix(inGf, inM),out); +end + +function test2D(testCase) + x = 1:2; + y = 1:3; + + f = @(x,y) x + y*10; + + xx = [1; 1; 1; 2; 2; 2]; + yy = [1; 2; 3; 1; 2; 3]; + inGf = f(xx,yy); + + [X,Y] = meshgrid(x,y); + out = f(X,Y); + + inM = [2, 3]; + + testCase.verifyEqual(grid.funcToPlotMatrix(inGf, inM),out); +end + +function test3D(testCase) + x = 1:2; + y = 1:3; + z = 1:4; + + f = @(x,y,z) x + y*10 + z*100; + + xx = [repmat(1, [12, 1]); repmat(2, [12, 1])]; + yy = repmat([1; 1; 1; 1; 2; 2; 2; 2; 3; 3; 3; 3], [2, 1]); + zz = repmat([1; 2; 3; 4], [6, 1]); + inGf = f(xx,yy,zz); + + [X,Y,Z] = meshgrid(x,y,z); + out = f(X,Y,Z); + + inM = [2, 3, 4]; + + testCase.verifyEqual(grid.funcToPlotMatrix(inGf, inM),out); +end \ No newline at end of file