From 1cc13611ccd9b26201d4033589cbee02f4369b4c Mon Sep 17 00:00:00 2001 From: Julien Lengrand-Lambert Date: Thu, 3 May 2018 14:01:33 +0200 Subject: [PATCH] Implment laff_norm2 * Still rounding issues --- Homework/Week1/laff_dot.m | 2 +- Homework/Week1/laff_norm2.m | 44 ++++++++++++++++++++++++++ Homework/Week1/test_norm2.m | 47 ++++++++++++++++++++++++++++ Programming/laff/vecvec/laff_norm2.m | 2 +- 4 files changed, 93 insertions(+), 2 deletions(-) create mode 100644 Homework/Week1/laff_norm2.m create mode 100755 Homework/Week1/test_norm2.m diff --git a/Homework/Week1/laff_dot.m b/Homework/Week1/laff_dot.m index e0864ae..85209a2 100644 --- a/Homework/Week1/laff_dot.m +++ b/Homework/Week1/laff_dot.m @@ -47,7 +47,7 @@ y_out = y; alpha = 0; for i = 1:max(size(x)) - alpha += y(i) * x(i) + alpha += y(i) * x(i); end endfunction \ No newline at end of file diff --git a/Homework/Week1/laff_norm2.m b/Homework/Week1/laff_norm2.m new file mode 100644 index 0000000..62cae79 --- /dev/null +++ b/Homework/Week1/laff_norm2.m @@ -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 . + +## Author: julien Lengrand-Lambert +## 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 \ No newline at end of file diff --git a/Homework/Week1/test_norm2.m b/Homework/Week1/test_norm2.m new file mode 100755 index 0000000..f114c67 --- /dev/null +++ b/Homework/Week1/test_norm2.m @@ -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 diff --git a/Programming/laff/vecvec/laff_norm2.m b/Programming/laff/vecvec/laff_norm2.m index 23c6372..d3fddbe 100755 --- a/Programming/laff/vecvec/laff_norm2.m +++ b/Programming/laff/vecvec/laff_norm2.m @@ -17,7 +17,7 @@ if ( m_x ~= 1 & n_x ~= 1 ) return end -xmax = 0.0 +xmax = 0.0; for i=1:m_x if abs( x( i ) > xmax ) xmax = abs( x( i ) );