mirror of
https://github.com/jlengrand/LAFF.git
synced 2026-03-10 08:31:21 +00:00
Create laff_scal
This commit is contained in:
42
Homework/Week1/laff_scal.m
Normal file
42
Homework/Week1/laff_scal.m
Normal 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
54
Homework/Week1/test_scal.m
Executable 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
|
||||
Reference in New Issue
Block a user