Create laff_scal

This commit is contained in:
Julien Lengrand-Lambert
2018-05-03 08:01:22 +02:00
parent bd4b76e1a7
commit d5645eaa68
2 changed files with 96 additions and 0 deletions

View File

@@ -0,0 +1,42 @@
## 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-05-03
function [ x_out ] = laff_scal (alpha, x)
x_out = x;
function isVector = isVector(x)
isVector = (size(x, 1) == 1 || size(x, 2) == 1);
endfunction
if !isVector(x)
disp('Error : x not a vector');
x_out = 'FAILED';
return
end
if !isscalar(alpha)
disp('Error : alpha not a scalar');
x_out = 'FAILED';
return
end
for i = 1:max(size(x))
x_out(i) = alpha * x(i);
end
endfunction

54
Homework/Week1/test_scal.m Executable file
View File

@@ -0,0 +1,54 @@
% Create a vector and a scalar
x = [
1
2
3
]
alpha = -2
% test with x column vector
disp( 'scale column vector' )
if ( isequal( laff_scal( alpha, x ), alpha * x ) )
disp( 'TEST PASSED' )
else
disp( 'TEST FAILED' )
end
disp( ' ' )
% test with x row vector
disp( 'scale row vector' )
if ( isequal( laff_scal( alpha, x' ), alpha * x' ) )
disp( 'TEST PASSED' )
else
disp( 'TEST FAILED' )
end
disp( ' ' )
% test with illegal alpha
alpha = [
1 2
3 4
];
disp( 'illegal alpha' )
if ( isequal( laff_scal( alpha, x ), 'FAILED' ) )
disp( 'TEST PASSED' )
else
disp( 'TEST FAILED' )
end
disp( ' ' )
% test with illegal x
x = [
2 3
-1 -2
];
alpha = 2;
disp( 'illegal x' )
if ( isequal( laff_scal( alpha, x ), 'FAILED' ) )
disp( 'TEST PASSED' )
else
disp( 'TEST FAILED' )
end