Add laff_copy

This commit is contained in:
Julien Lengrand-Lambert
2018-05-02 14:23:00 +02:00
parent d4dce71599
commit bd4b76e1a7
3 changed files with 152 additions and 0 deletions

1
.gitignore vendored
View File

@@ -0,0 +1 @@
Homework/Week1/octave-workspace

View File

@@ -0,0 +1,56 @@
## Copyright (C) 2018 julien Lengrand-Lambert
##
## This program is free software; you can redistribute it and/or modify it
## under the terms of the GNU General Public License as published by
## the Free Software Foundation; either version 3 of the License, or
## (at your option) any later version.
##
## This program is distributed in the hope that it will be useful,
## but WITHOUT ANY WARRANTY; without even the implied warranty of
## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
## GNU General Public License for more details.
##
## You should have received a copy of the GNU General Public License
## along with this program. If not, see <http://www.gnu.org/licenses/>.
## Author: julien Lengrand-Lambert <jlengrand@juliens-MacBook-Pro.local>
## Created: 2018-04-30
function [ y_out ] = laff_copy (x, y)
y_out = y;
function isVector = isVector(x)
isVector = (size(x, 1) == 1 || size(x, 2) == 1);
endfunction
function isCompatible = isCompatible(x, y)
isCompatible = (isequal(size(x), size(y)) || isequal(size(x), fliplr(size(y))));
endfunction
function needsTranspose = needsTranspose(x, y)
needsTranspose = isequal(size(x) ,(size(y')));
endfunction
if !isVector(x)
disp('Error : x not a vector');
y_out = 'FAILED';
return
end
if !isVector(y)
disp('Error : y not a vector');
y_out = 'FAILED';
return
end
if !isCompatible(x, y)
disp('Error : x and y are not compatible');
y_out = 'FAILED';
return
end
for i = 1:max(size(x))
y_out(i) = x(i);
end
endfunction

95
Homework/Week1/test_copy.m Executable file
View File

@@ -0,0 +1,95 @@
% Create some vectors
x = [
1
2
3
]
y = [
0
-1
-2
]
z = [
4
3
2
1
]
% test column -> column copy
disp( 'column -> column copy' )
if ( isequal( laff_copy( x, y ), x ) )
disp( 'TEST PASSED' )
else
disp( 'TEST FAILED' )
end
disp( ' ' )
% test column -> row copy
disp( 'column -> row copy' )
if ( isequal( laff_copy( x, y' ), x' ) )
disp( 'TEST PASSED' )
else
disp( 'TEST FAILED' )
end
disp( ' ' )
% test row -> column copy
disp( 'row -> column copy' )
if ( isequal( laff_copy( x', y ), x ) )
disp( 'TEST PASSED' )
else
disp( 'TEST FAILED' )
end
disp( ' ' )
% test row -> row copy
disp( 'row -> row copy' )
if ( isequal( laff_copy( x', y' ), x' ) )
disp( 'TEST PASSED' )
else
disp( 'TEST FAILED' )
end
disp( ' ' )
% test column -> column copy (wrong size)
disp( 'column -> column copy (wrong size)' )
if ( isequal( laff_copy( x, z ), 'FAILED' ) )
disp( 'TEST PASSED' )
else
disp( 'TEST FAILED' )
end
disp( ' ' )
% test column -> row copy (wrong size)
disp( 'column -> row copy (wrong size)' )
if ( isequal( laff_copy( x, z' ), 'FAILED' ) )
disp( 'TEST PASSED' )
else
disp( 'TEST FAILED' )
end
disp( ' ' )
% test row -> column copy (wrong size)
disp( 'row -> column copy (wrong size)' )
if ( isequal( laff_copy( x', z ), 'FAILED' ) )
disp( 'TEST PASSED' )
else
disp( 'TEST FAILED' )
end
disp( ' ' )
% test row -> row copy (wrong size)
disp( 'row -> row copy (wrong size)' )
if ( isequal( laff_copy( x', z' ), 'FAILED' ) )
disp( 'TEST PASSED' )
else
disp( 'TEST FAILED' )
end