mirror of
https://github.com/jlengrand/LAFF.git
synced 2026-03-10 08:31:21 +00:00
Implment laff_norm2
* Still rounding issues
This commit is contained in:
@@ -47,7 +47,7 @@ y_out = y;
|
|||||||
|
|
||||||
alpha = 0;
|
alpha = 0;
|
||||||
for i = 1:max(size(x))
|
for i = 1:max(size(x))
|
||||||
alpha += y(i) * x(i)
|
alpha += y(i) * x(i);
|
||||||
end
|
end
|
||||||
|
|
||||||
endfunction
|
endfunction
|
||||||
44
Homework/Week1/laff_norm2.m
Normal file
44
Homework/Week1/laff_norm2.m
Normal file
@@ -0,0 +1,44 @@
|
|||||||
|
## 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 [ alpha ] = laff_norm2 (x)
|
||||||
|
|
||||||
|
function isVector = isVector(x)
|
||||||
|
isVector = (size(x, 1) == 1 || size(x, 2) == 1);
|
||||||
|
endfunction
|
||||||
|
|
||||||
|
if !isVector(x)
|
||||||
|
disp('Error : x not a vector');
|
||||||
|
alpha = 'FAILED';
|
||||||
|
return
|
||||||
|
end
|
||||||
|
|
||||||
|
xmax = 0.0
|
||||||
|
for i=1:size(x, 1)
|
||||||
|
if abs( x( i ) > xmax )
|
||||||
|
xmax = abs( x( i ) );
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
x = laff_scal( 1/xmax, x );
|
||||||
|
|
||||||
|
% Use laff_dot to compute the length of x as sqrt( x' * x )
|
||||||
|
alpha = sqrt( laff_dot( x, x ) );
|
||||||
|
|
||||||
|
alpha *= xmax;
|
||||||
|
|
||||||
|
endfunction
|
||||||
47
Homework/Week1/test_norm2.m
Executable file
47
Homework/Week1/test_norm2.m
Executable file
@@ -0,0 +1,47 @@
|
|||||||
|
% Create a vector
|
||||||
|
x = [
|
||||||
|
1
|
||||||
|
2
|
||||||
|
3
|
||||||
|
]
|
||||||
|
|
||||||
|
% test with x column vector, comparing against matlab's norm function
|
||||||
|
disp( 'compute length of column vector' )
|
||||||
|
if ( isequal( laff_norm2( x ), norm( x ) ) )
|
||||||
|
disp( 'TEST PASSED' )
|
||||||
|
else
|
||||||
|
disp( 'TEST FAILED. But could this be due to roundoff? Lets check:' )
|
||||||
|
disp( 'laff_norm2( x ):' )
|
||||||
|
disp( laff_norm2( x ) )
|
||||||
|
disp( 'norm( x, 2 ):' )
|
||||||
|
disp( norm( x, 2 ) )
|
||||||
|
end
|
||||||
|
|
||||||
|
disp( ' ' )
|
||||||
|
|
||||||
|
% test with x row vector, comparing against matlab's norm function
|
||||||
|
disp( 'compute length of row vector' )
|
||||||
|
if ( isequal( laff_norm2( x' ), norm( x ) ) )
|
||||||
|
disp( 'TEST PASSED' )
|
||||||
|
else
|
||||||
|
disp( 'TEST FAILED But could this be due to roundoff? Lets check:' )
|
||||||
|
disp( 'laff_norm2( trans(x) ):' )
|
||||||
|
disp( laff_norm2( x' ) )
|
||||||
|
disp( 'norm( x, 2 ):' )
|
||||||
|
disp( norm( x, 2 ) )
|
||||||
|
end
|
||||||
|
|
||||||
|
disp( ' ' )
|
||||||
|
|
||||||
|
% test with illegal x
|
||||||
|
x = [
|
||||||
|
2 3
|
||||||
|
-1 -2
|
||||||
|
];
|
||||||
|
|
||||||
|
disp( 'illegal x' )
|
||||||
|
if ( isequal( laff_norm2( x ), 'FAILED' ) )
|
||||||
|
disp( 'TEST PASSED' )
|
||||||
|
else
|
||||||
|
disp( 'TEST FAILED' )
|
||||||
|
end
|
||||||
@@ -17,7 +17,7 @@ if ( m_x ~= 1 & n_x ~= 1 )
|
|||||||
return
|
return
|
||||||
end
|
end
|
||||||
|
|
||||||
xmax = 0.0
|
xmax = 0.0;
|
||||||
for i=1:m_x
|
for i=1:m_x
|
||||||
if abs( x( i ) > xmax )
|
if abs( x( i ) > xmax )
|
||||||
xmax = abs( x( i ) );
|
xmax = abs( x( i ) );
|
||||||
|
|||||||
Reference in New Issue
Block a user