Create repo

This commit is contained in:
Julien Lengrand-Lambert
2018-05-02 09:13:08 +02:00
commit d4dce71599
243 changed files with 19406 additions and 0 deletions

View File

@@ -0,0 +1,57 @@
function [ y_out ] = laff_copy( x, y )
% y = copy( x, y ) copies vector x into vector y
% Vectors x and y can be a mixture of column and/or row vector. In other
% words, x and y can be n x 1 or 1 x n arrays. However, one size must
% equal 1 and the other size equal n.
%
% The reason y is an input parameter is that the input y indicates
% whether the output, y_out, is a column or row vector, and then also
% indicates whether x must be transposed (e.g., if x is a row vector and
% y is a column vector).
% Extract the row and column sizes of x and y
[ m_x, n_x ] = size( x );
[ m_y, n_y ] = size( y );
% Make sure x and y are (row or column) vectors of equal length
if ( m_x ~= 1 & n_x ~= 1 ) | ( m_y ~= 1 & n_y ~= 1 )
y_out = 'FAILED';
return
end
if ( m_x * n_x ~= m_y * n_y )
y_out = 'FAILED';
return
end
if ( n_x == 1 ) % x is a column vector
if ( n_y == 1 ) % y is a column vector
% Copy the elements of x into the elements of y
for i=1:m_x
y( i,1 ) = x( i,1 );
end
else % y is a row vector
% Copy the elements of x into the elements of y
for i=1:m_x
y( 1,i ) = x( i,1 );
end
end
else % x is a row vector
if ( n_y == 1 ) % y is a column vector
% Copy the elements of x into the elements of y
for i=1:n_x
y( i,1 ) = x( 1,i );
end
else % y is a row vector
% Copy the elements of x into the elements of y
for i=1:n_x
y( 1,i ) = x( 1,i );
end
end
end
% Return the updated y in y_out
y_out = y;
return
end

98
Programming/Week01/test_axpy.m Executable file
View File

@@ -0,0 +1,98 @@
% Create some vectors
x = [
1
2
3
]
y = [
0
-1
-2
]
z = [
4
3
2
1
]
% Create a scalar
alpha = -2
% test column - column axpy
disp( 'column - column axpy' )
if ( isequal( laff_axpy( alpha, x, y ), alpha * x + y ) )
disp( 'TEST PASSED' )
else
disp( 'TEST FAILED' )
end
disp( ' ' )
% test column - row axpy
disp( 'column - row axpy' )
if ( isequal( laff_axpy( alpha, x, y' ), alpha * x' + y' ) )
disp( 'TEST PASSED' )
else
disp( 'TEST FAILED' )
end
disp( ' ' )
% test row - column axpy
disp( 'row - column axpy' )
if ( isequal( laff_axpy( alpha, x', y ), alpha * x + y ) )
disp( 'TEST PASSED' )
else
disp( 'TEST FAILED' )
end
disp( ' ' )
% test row -> row axpy
disp( 'row -> row axpy' )
if ( isequal( laff_axpy( alpha, x', y' ), alpha * x' + y' ) )
disp( 'TEST PASSED' )
else
disp( 'TEST FAILED' )
end
disp( ' ' )
% test column - column axpy (wrong size)
disp( 'column - column axpy (wrong size)' )
if ( isequal( laff_axpy( alpha, x, z ), 'FAILED' ) )
disp( 'TEST PASSED' )
else
disp( 'TEST FAILED' )
end
disp( ' ' )
% test column - row axpy (wrong size)
disp( 'column - row axpy (wrong size)' )
if ( isequal( laff_axpy( alpha, x, z' ), 'FAILED' ) )
disp( 'TEST PASSED' )
else
disp( 'TEST FAILED' )
end
disp( ' ' )
% test row -> column (wrong size)
disp( 'row -> column (wrong size)' )
if ( isequal( laff_axpy( alpha, x', z ), 'FAILED' ) )
disp( 'TEST PASSED' )
else
disp( 'TEST FAILED' )
end
disp( ' ' )
% test row -> row axpy (wrong size)
disp( 'row -> row axpy (wrong size)' )
if ( isequal( laff_axpy( alpha, x', z' ), 'FAILED' ) )
disp( 'TEST PASSED' )
else
disp( 'TEST FAILED' )
end

95
Programming/Week01/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

95
Programming/Week01/test_dot.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 dot product of column vector with column vector
disp( 'dot product of column vector with column vector' )
if ( isequal( laff_dot( x, y ), x' * y ) )
disp( 'TEST PASSED' )
else
disp( 'TEST FAILED' )
end
disp( ' ' )
% test dot product of column vector with row vector
disp( 'dot product of column vector with row vector' )
if ( isequal( laff_dot( x, y' ), x' * y ) )
disp( 'TEST PASSED' )
else
disp( 'TEST FAILED' )
end
disp( ' ' )
% test dot product of row vector with column vector
disp( 'dot product of row vector with column vector' )
if ( isequal( laff_dot( x', y ), x' * y ) )
disp( 'TEST PASSED' )
else
disp( 'TEST FAILED' )
end
disp( ' ' )
% test dot product of row vector with row vector
disp( 'dot product of row vector with row vector' )
if ( isequal( laff_dot( x', y' ), x' * y ) )
disp( 'TEST PASSED' )
else
disp( 'TEST FAILED' )
end
disp( ' ' )
% test dot product of column vector with column vector (wrong size)
disp( 'dot product of column vector with column vector (wrong size)' )
if ( isequal( laff_dot( x, z ), 'FAILED' ) )
disp( 'TEST PASSED' )
else
disp( 'TEST FAILED' )
end
disp( ' ' )
% test dot product of column vector with row vector (wrong size)
disp( 'dot product of column vector with row vector (wrong size)' )
if ( isequal( laff_dot( x, z' ), 'FAILED' ) )
disp( 'TEST PASSED' )
else
disp( 'TEST FAILED' )
end
disp( ' ' )
% test dot product of row vector with column vector (wrong size)
disp( 'dot product of row vector with column vector (wrong size)' )
if ( isequal( laff_dot( x', z ), 'FAILED' ) )
disp( 'TEST PASSED' )
else
disp( 'TEST FAILED' )
end
disp( ' ' )
% test dot product of row vector with row vector (wrong size)
disp( 'dot product of row vector with row vector (wrong size)' )
if ( isequal( laff_dot( x', z' ), 'FAILED' ) )
disp( 'TEST PASSED' )
else
disp( 'TEST FAILED' )
end

47
Programming/Week01/test_norm2.m Executable file
View 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

54
Programming/Week01/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

View File

@@ -0,0 +1,40 @@
disp( 'To end practice, enter Control-C' )
for i=1:10
size_A = randi( [1,3], 2,1 );
A = randi( [-2,2], size_A(1), size_A(2) );
x = randi( [-2,2], size_A(2), 1 );
m_A = size( A, 1 );
for i=1:5
disp( 'evaluate' )
PrintMVProblem( A, x );
answ = A * x;
correct = 1;
for j=1:m_A
y( j,1 ) = input('');
if y( j, 1 ) ~= answ( j )
correct = 0;
end
end
if correct
disp('Correct!')
disp(' ')
break
else
disp('try again!' )
disp(' ')
end
if i == 5
disp( 'the answer is' )
PrintMVProblem( A, x );
disp( A * x )
end
end
end

View File

@@ -0,0 +1,73 @@
function [ error ] = PrintMVProblem( A, x )
output_string = '';
col = 1;
[ m_A, n_A ] = size( A );
[ m_x, n_x ] = size( x );
for i=1:max( m_A, m_x )
if m_A == 1 & i== 1
outstring = '< ';
elseif i == 1
outstring = '/ ';
elseif i==m_A
outstring = '\ ';
elseif i<m_A
outstring = '| ';
else
outstring = ' ';
end
for j=1:n_A
if i<=m_A
outstring = [ outstring sprintf( '%2d ', A( i,j ) ) ];
else
outstring = [ outstring ' ' ];
end
end
if m_A == 1 & i == 1
outstring = [ outstring '> ' ];
elseif i == 1
outstring = [ outstring '\ '];
elseif i==m_A
outstring = [ outstring '/ '];
elseif i<m_A
outstring = [ outstring '| '];
else
outstring = [ outstring ' '];
end
if m_x == 1 & i == 1
outstring = [ outstring '< ' ];
elseif i == 1
outstring = [ outstring '/ '];
elseif i==m_x
outstring = [ outstring '\ '];
elseif i<m_x
outstring = [ outstring '| '];
else
outstring = [ outstring ' '];
end
if i <= m_x
outstring = [ outstring sprintf( '%d ', x( i ) ) ];
end
if m_x == 1 & i == 1
outstring = [ outstring '>' ];
elseif i == 1
outstring = [ outstring '\ ' ];
elseif i==m_x
outstring = [ outstring '/ '];
elseif i<m_x
outstring = [ outstring '| ' ];
end
disp( outstring )
end
disp( '=' )
end

View File

@@ -0,0 +1,42 @@
disp( 'Practice as many matrix-matrix multiplications as you want!' )
disp( 'We suggest you get a piece of paper out rather than trying to' )
disp( 'do it in your head.' )
disp( 'To end practice, enter Control-C' )
for i=1:50
size_A = randi( [1,3], 2,1 );
size_B = randi( [1,3], 2,1 );
A = randi( [-2,2], size_A(1), size_A(2) );
B = randi( [-2,2], size_A(2), size_B(2) );
m_A = size( A, 1 );
for i=1:3
disp( 'evaluate' )
PrintMMProblem( A, B );
answ = A * B;
C = input('');
if isequal( C, answ )
disp('Correct!')
disp(' ')
break
else
disp('try again!' )
disp(' ')
end
if i == 3
disp( 'the answer is' )
PrintMMProblem( A, B );
disp( A * B )
end
end
disp( 'To end practice, enter Control-C' )
disp( ' ' )
end

View File

@@ -0,0 +1,79 @@
function [ error ] = PrintMMProblem( A, B )
output_string = '';
col = 1;
[ m_A, n_A ] = size( A );
[ m_B, n_B ] = size( B );
for i=1:max( m_A, m_B )
if m_A == 1 & i== 1
outstring = '< ';
elseif i == 1
outstring = '/ ';
elseif i==m_A
outstring = '\ ';
elseif i<m_A
outstring = '| ';
else
outstring = ' ';
end
for j=1:n_A
if i<=m_A
outstring = [ outstring sprintf( '%2d ', A( i,j ) ) ];
else
outstring = [ outstring ' ' ];
end
end
if m_A == 1 & i == 1
outstring = [ outstring '> ' ];
elseif i == 1
outstring = [ outstring '\ '];
elseif i==m_A
outstring = [ outstring '/ '];
elseif i<m_A
outstring = [ outstring '| '];
else
outstring = [ outstring ' '];
end
if m_B == 1 & i == 1
outstring = [ outstring '< ' ];
elseif i == 1
outstring = [ outstring '/ '];
elseif i==m_B
outstring = [ outstring '\ '];
elseif i<m_B
outstring = [ outstring '| '];
else
outstring = [ outstring ' '];
end
for j=1:n_B
if i<=m_B
outstring = [ outstring sprintf( '%2d ', B( i,j ) ) ];
else
outstring = [ outstring ' ' ];
end
end
if m_B == 1 & i == 1
outstring = [ outstring '>' ];
elseif i == 1
outstring = [ outstring '\ ' ];
elseif i==m_B
outstring = [ outstring '/ '];
elseif i<m_B
outstring = [ outstring '| ' ];
end
disp( outstring )
end
disp( ' ' )
disp( '=' )
disp( ' ' )
end

View File

@@ -0,0 +1,10 @@
A = randi( [ -2, 2 ], 4, 4 )
x = randi( [ -2, 2 ], 4, 1 )
y = randi( [ -2, 2 ], 4, 1 )
if ( isequal( Mvmult_t_unb_var1( A, x, y ), A' * x + y ) )
disp( 'Mvmult_t_unb_var1 appears to be correct' )
else
disp( 'Mvmult_t_unb_var1 has a problem' )
end

View File

@@ -0,0 +1,10 @@
A = randi( [ -2, 2 ], 4, 4 )
x = randi( [ -2, 2 ], 4, 1 )
y = randi( [ -2, 2 ], 4, 1 )
if ( isequal( Mvmult_t_unb_var2( A, x, y ), A' * x + y ) )
disp( 'Mvmult_t_unb_var2 appears to be correct' )
else
disp( 'Mvmult_t_unb_var2 has a problem' )
end

View File

@@ -0,0 +1,10 @@
A = randi( [ -2, 2 ], 4, 4 )
x = randi( [ -2, 2 ], 4, 1 )
y = randi( [ -2, 2 ], 4, 1 )
if ( isequal( Mvmult_n_unb_var1B( A, x, y ), A * x + y ) )
disp( 'Mvmult_unb_var1B appears to be correct' )
else
disp( 'Mvmult_unb_var1B has a problem' )
end

View File

@@ -0,0 +1,10 @@
A = randi( [ -2, 2 ], 4, 4 )
x = randi( [ -2, 2 ], 4, 1 )
y = randi( [ -2, 2 ], 4, 1 )
if ( isequal( Mvmult_n_unb_var2B( A, x, y ), A * x + y ) )
disp( 'Mvmult_unb_var2B appears to be correct' )
else
disp( 'Mvmult_unb_var2B has a problem' )
end

View File

@@ -0,0 +1,12 @@
A = randi( [ -2, 2 ], 4, 4 )
x = randi( [ -2, 2 ], 4, 1 )
y = randi( [ -2, 2 ], 4, 1 )
Asymm = tril( A ) + tril( A, -1 )';
if ( isequal( Symv_l_unb_var1( A, x, y ), Asymm * x + y ) )
disp( 'Symv_l_unb_var1 appears to be correct' )
else
disp( 'Symv_l_unb_var1 has a problem' )
end

View File

@@ -0,0 +1,12 @@
A = randi( [ -2, 2 ], 4, 4 )
x = randi( [ -2, 2 ], 4, 1 )
y = randi( [ -2, 2 ], 4, 1 )
Asymm = tril( A ) + tril( A, -1 )';
if ( isequal( Symv_l_unb_var2( A, x, y ), Asymm * x + y ) )
disp( 'Symv_l_unb_var2 appears to be correct' )
else
disp( 'Symv_l_unb_var2 has a problem' )
end

View File

@@ -0,0 +1,12 @@
A = randi( [ -2, 2 ], 4, 4 )
x = randi( [ -2, 2 ], 4, 1 )
y = randi( [ -2, 2 ], 4, 1 )
Asymm = triu( A ) + triu( A, 1 )';
if ( isequal( Symv_u_unb_var1( A, x, y ), Asymm * x + y ) )
disp( 'Symv_u_unb_var1 appears to be correct' )
else
disp( 'Symv_u_unb_var1 has a problem' )
end

View File

@@ -0,0 +1,12 @@
A = randi( [ -2, 2 ], 4, 4 )
x = randi( [ -2, 2 ], 4, 1 )
y = randi( [ -2, 2 ], 4, 1 )
Asymm = triu( A ) + triu( A, 1 )';
if ( isequal( Symv_u_unb_var2( A, x, y ), Asymm * x + y ) )
disp( 'Symv_u_unb_var2 appears to be correct' )
else
disp( 'Symv_u_unb_var2 has a problem' )
end

View File

@@ -0,0 +1,12 @@
A = randi( [ -2, 2 ], 4, 4 )
x = randi( [ -2, 2 ], 4, 1 )
y = randi( [ -2, 2 ], 4, 1 )
Asymm = triu( A ) + triu( A, 1 )';
if ( isequal( Symv_u_unb_var3( A, x, y ), Asymm * x + y ) )
disp( 'Symv_u_unb_var3 appears to be correct' )
else
disp( 'Symv_u_unb_var3 has a problem' )
end

View File

@@ -0,0 +1,9 @@
A = randi( [ -2, 2 ], 4, 4 )
x = randi( [ -2, 2 ], 4, 1 )
if ( isequal( Trmv_ln_unb_var1( A, x ), tril( A ) * x ) )
disp( 'Trmv_ln_unb_var1 appears to be correct' )
else
disp( 'Trmv_ln_unb_var1 has a problem' )
end

View File

@@ -0,0 +1,9 @@
A = randi( [ -2, 2 ], 4, 4 )
x = randi( [ -2, 2 ], 4, 1 )
if ( isequal( Trmv_ln_unb_var2( A, x ), tril( A ) * x ) )
disp( 'Trmv_ln_unb_var2 appears to be correct' )
else
disp( 'Trmv_ln_unb_var2 has a problem' )
end

View File

@@ -0,0 +1,9 @@
A = randi( [ -2, 2 ], 4, 4 )
x = randi( [ -2, 2 ], 4, 1 )
if ( isequal( Trmv_un_unb_var1( A, x ), triu( A ) * x ) )
disp( 'Trmv_un_unb_var1 appears to be correct' )
else
disp( 'Trmv_un_unb_var1 has a problem' )
end

View File

@@ -0,0 +1,9 @@
A = randi( [ -2, 2 ], 4, 4 )
x = randi( [ -2, 2 ], 4, 1 )
if ( isequal( Trmv_un_unb_var2( A, x ), triu( A ) * x ) )
disp( 'Trmv_un_unb_var2 appears to be correct' )
else
disp( 'Trmv_un_unb_var2 has a problem' )
end

View File

@@ -0,0 +1,10 @@
A = randi( [ -2, 2 ], 4, 4 )
x = randi( [ -2, 2 ], 4, 1 )
y = randi( [ -2, 2 ], 4, 1 )
if ( isequal( Trmvp_ln_unb_var1( A, x, y ), tril( A ) * x + y ) )
disp( 'Trmvp_ln_unb_var1 appears to be correct' )
else
disp( 'Trmvp_ln_unb_var1 has a problem' )
end

View File

@@ -0,0 +1,10 @@
A = randi( [ -2, 2 ], 4, 4 )
x = randi( [ -2, 2 ], 4, 1 )
y = randi( [ -2, 2 ], 4, 1 )
if ( isequal( Trmvp_ln_unb_var2( A, x, y ), tril( A ) * x + y ) )
disp( 'Trmvp_ln_unb_var2 appears to be correct' )
else
disp( 'Trmvp_ln_unb_var2 has a problem' )
end

View File

@@ -0,0 +1,10 @@
A = randi( [ -2, 2 ], 4, 4 )
x = randi( [ -2, 2 ], 4, 1 )
y = randi( [ -2, 2 ], 4, 1 )
if ( isequal( Trmvp_un_unb_var1( A, x, y ), triu( A ) * x + y ) )
disp( 'Trmvp_un_unb_var1 appears to be correct' )
else
disp( 'Trmvp_un_unb_var1 has a problem' )
end

View File

@@ -0,0 +1,10 @@
A = randi( [ -2, 2 ], 4, 4 )
x = randi( [ -2, 2 ], 4, 1 )
y = randi( [ -2, 2 ], 4, 1 )
if ( isequal( Trmvp_un_unb_var2( A, x, y ), triu( A ) * x + y ) )
disp( 'Trmvp_un_unb_var2 appears to be correct' )
else
disp( 'Trmvp_un_unb_var2 has a problem' )
end

15
Programming/Week05/MatMatMult.m Executable file
View File

@@ -0,0 +1,15 @@
function [ C_out ] = MatMatMult( A, B, C )
[ m, n ] = size( C );
[ m_A, k ] = size( A );
[ m_B, n_B ] = size( B );
for j = 1:n
for i = 1:m
for p = 1:k
C( i,j ) = A( i, p ) * B( p, j ) + C( i, j );
end
end
end
C_out = C;

View File

@@ -0,0 +1,21 @@
m = 5;
n = 3;
k = 4;
C = randi( [-2,2], m, n )
A = randi( [-2,2], m, k )
B = randi( [-2,2], k, n )
% Compute A * B + C via MatMatMult
Gemm_unb_var1( A, B, C )
% Compute A * B + C
A * B + C
% Check if they compute the same
if ( isequal( Gemm_unb_var1( A, B, C ), A * B + C ) )
disp( 'it appears Gemm_unb_var1 computes correctly' )
else
disp( 'Gemm_unb_var1 has a problem' )
end

View File

@@ -0,0 +1,21 @@
m = 5;
n = 3;
k = 4;
C = randi( [-2,2], m, n )
A = randi( [-2,2], m, k )
B = randi( [-2,2], k, n )
% Compute A * B + C via MatMatMult
Gemm_unb_var2( A, B, C )
% Compute A * B + C
A * B + C
% Check if they compute the same
if ( isequal( Gemm_unb_var2( A, B, C ), A * B + C ) )
disp( 'it appears Gemm_unb_var2 computes correctly' )
else
disp( 'Gemm_unb_var2 has a problem' )
end

View File

@@ -0,0 +1,21 @@
m = 5;
n = 3;
k = 4;
C = randi( [-2,2], m, n )
A = randi( [-2,2], m, k )
B = randi( [-2,2], k, n )
% Compute A * B + C via MatMatMult
Gemm_unb_var3( A, B, C )
% Compute A * B + C
A * B + C
% Check if they compute the same
if ( isequal( Gemm_unb_var3( A, B, C ), A * B + C ) )
disp( 'it appears Gemm_unb_var3 computes correctly' )
else
disp( 'Gemm_unb_var3 has a problem' )
end

View File

@@ -0,0 +1,21 @@
m = 5;
n = 3;
k = 4;
C = randi( [-2,2], m, n )
A = randi( [-2,2], m, k )
B = randi( [-2,2], k, n )
% Compute A * B + C via MatMatMult
MatMatMult( A, B, C )
% Compute A * B + C
A * B + C
% Check if they compute the same
if ( isequal( MatMatMult( A, B, C ), A * B + C ) )
disp( 'it appears MatMatMult computes correctly' )
else
disp( 'MatMatMult has a problem' )
end

View File

@@ -0,0 +1,23 @@
U = randi( [-2,2], 4, 4 );
R = randi( [-2,2], 4, 4 );
C = randi( [-2,2], 4, 4 );
Cout = Trtrmm_unb_var1( U, R, C );
% Now, remember that only the upper triangular part changed. So,
% tril( Cout,-1 ) should equal tril( C,-1 ) and triu( Cout ) should equal
% triu( U ) * triu( R )
if ~isequal( tril( Cout, -1 ), tril( C, -1 ) )
disp( 'TROUBLE: The lower triangular part of C was corrupted' )
elseif ~isequal( triu( Cout ), triu( U ) * triu( R ) )
disp( 'TROUBLE: The lower triangular part of C was corrupted' )
else
disp( 'Trtrmm_unb_var1 appears to give the right answer' )
end

View File

@@ -0,0 +1,40 @@
% Create a matrix. (This matrix was carefully chosen so that only integers
% are encountered during the process.)
A = [
2 0 1 2
-2 -1 1 -1
4 -1 5 4
-4 1 -3 -8
]
% Perform Gaussian elimination
LU = GaussianElimination( A )
% Create a right-hand side. We are going to solve A x = b
b = [
2
2
11
-3
]
% Perform forward substitution
bhat = ForwardSubstitution( LU, b )
% Extract the upper triangular matrix from the matrix that resulted from
% Gaussian elminination:
U = triu( LU )
% Solve U x = bhat
x = U \ bhat
% Check the answer
b - A * x

View File

@@ -0,0 +1,25 @@
% Create a matrix. (This matrix was carefully chosen so that only integers
% are encountered during the process.)
A = [
2 0 1 2
-2 -1 1 -1
4 -1 5 4
-4 1 -3 -8
]
% Perform LU factorization
LU = LU_unb_var5( A )
% Extract the unit lower triangular matrix
L = tril( LU, -1 ) + eye( size( LU ) )
% Extract the upper triangular matrix
U = triu( LU )
% Check that A = L * U
A - L * U

View File

@@ -0,0 +1,28 @@
% Create a unit lower triangular matrixmatrix.
% (This matrix was carefully chosen so that only integers
% are encountered during the process.)
L = [
1 0 0 0
-1 1 0 0
2 1 1 0
-2 -1 1 1
]
% Create a right-hand side. We are going to solve L x = b
b = [
2
2
11
-3
]
% Solve L x = b
x = Ltrsv_unb_var1( L, b )
% Check that L x = b
b - L * x

26
Programming/Week06/test_Solve.m Executable file
View File

@@ -0,0 +1,26 @@
% Create a matrix. (This matrix was carefully chosen so that only integers
% are encountered during the process.)
A = [
2 0 1 2
-2 -1 1 -1
4 -1 5 4
-4 1 -3 -8
]
% Create a right-hand size vector b
b = [
2
2
11
-3
]
% Solve A x = b
[ LU, x ] = Solve( A, b )
% Check that A x = b
b - A * x

View File

@@ -0,0 +1,30 @@
% Create an upper triangular matrixmatrix.
% (This matrix was carefully chosen so that only integers
% are encountered during the process.)
U = [
2 0 1 2
0 -1 2 1
0 0 1 -1
0 0 0 -2
]
% Create a right-hand side. We are going to solve U x = b
b = [
2
4
3
2
]
% Solve U x = b
x = Utrsv_unb_var1( U, b )
% Check that U x = b
b - U * x

View File

@@ -0,0 +1,28 @@
% Create a unit lower triangular matrixmatrix.
% (This matrix was carefully chosen so that only integers
% are encountered during the process.)
L = [
1 0 0 0
-1 1 0 0
2 1 1 0
-2 -1 1 1
]
% Create a right-hand side. We are going to solve L x = b
b = [
2
2
11
-3
]
% Solve L x = b
x = Ltrsv_unb_var2( L, b )
% Check that L x = b
b - L * x

33
Programming/Week08/LU_unb.m Executable file
View File

@@ -0,0 +1,33 @@
function [ A_out ] = LU_unb( A )
[ ATL, ATR, ...
ABL, ABR ] = FLA_Part_2x2( A, ...
0, 0, 'FLA_TL' );
while ( size( ATL, 1 ) < size( A, 1 ) )
[ A00, a01, A02, ...
a10t, alpha11, a12t, ...
A20, a21, A22 ] = FLA_Repart_2x2_to_3x3( ATL, ATR, ...
ABL, ABR, ...
1, 1, 'FLA_BR' );
%------------------------------------------------------------%
a21 = a21 / alpha11;
A22 = A22 - a21 * a12t;
%------------------------------------------------------------%
[ ATL, ATR, ...
ABL, ABR ] = FLA_Cont_with_3x3_to_2x2( A00, a01, A02, ...
a10t, alpha11, a12t, ...
A20, a21, A22, ...
'FLA_TL' );
end
A_out = [ ATL, ATR
ABL, ABR ];
return

View File

@@ -0,0 +1,19 @@
% We are going to take a bit of a risk, and start with a random matrix A
% with integer values... There is no guarantee that it is invertible
% without pivoting, or invertible at all...
A = randi( [-9,9], 4, 4 );
B = zeros( size( A ) );
Ainv = GJ_Inverse_alt_unb( A, B );
disp( 'The below should equal approximately the identity:' )
Ainv * A
disp( 'Note: if the above shows "NaN", this means that the Gauss-Jordan' )
disp( 'algorithm encountered a divide by zero' )
disp( 'Just execute this script again, and see if you get luckier...' )

View File

@@ -0,0 +1,17 @@
% We are going to take a bit of a risk, and start with a random matrix A
% with integer values... There is no guarantee that it is invertible
% without pivoting, or invertible at all...
A = randi( [-9,9], 4, 4 );
Ainv = GJ_Inverse_inplace_unb( A );
disp( 'The below should equal approximately the identity:' )
Ainv * A
disp( 'Note: if the above shows "NaN", this means that the Gauss-Jordan' )
disp( 'algorithm encountered a divide by zero, resulting in a "Not a Number"' )
disp( 'Just execute this script again, and see if you get luckier...' )

View File

@@ -0,0 +1,17 @@
% We are going to take a bit of a risk, and start with a random matrix A
% with integer values... There is no guarantee that it is invertible
% without pivoting, or invertible at all...
A = randi( [-9,9], 4, 4 );
Ainv = GJ_Inverse_inplace_unb_var2( A );
disp( 'The below should equal approximately the identity:' )
Ainv * A
disp( 'Note: if the above shows "NaN", this means that the Gauss-Jordan' )
disp( 'algorithm encountered a divide by zero, resulting in a "Not a Number"' )
disp( 'Just execute this script again, and see if you get luckier...' )

View File

@@ -0,0 +1,34 @@
function [ A_out ] = Cholesky_unb_var3( A )
[ ATL, ATR, ...
ABL, ABR ] = FLA_Part_2x2( A, ...
0, 0, 'FLA_TL' );
while ( size( ATL, 1 ) < size( A, 1 ) )
[ A00, a01, A02, ...
a10t, alpha11, a12t, ...
A20, a21, A22 ] = FLA_Repart_2x2_to_3x3( ATL, ATR, ...
ABL, ABR, ...
1, 1, 'FLA_BR' );
%------------------------------------------------------------%
alpha11 = sqrt( alpha11 );
a21 = laff_invscal( alpha11, a21 );
A22 = laff_syr( 'Lower triangular', -1, a21, A22 );
%------------------------------------------------------------%
[ ATL, ATR, ...
ABL, ABR ] = FLA_Cont_with_3x3_to_2x2( A00, a01, A02, ...
a10t, alpha11, a12t, ...
A20, a21, A22, ...
'FLA_TL' );
end
A_out = [ ATL, ATR
ABL, ABR ];
return

View File

@@ -0,0 +1,56 @@
% Close all figures
close all
% Read the picture in building.png into matrix B
[ B, map ] = imread( 'building.png' );
% Show the picture in a figure
imshow( B );
% Choose the (approximate) number of columns you want to use for the
% aproximation.
k = 30;
% Extract the number of rows and columns in B
[ m, n ] = size( B )
% B starts as an integer array. To do the computations, we need to
% make it into floating point numbers.
B = double( B );
% Pick A as the matrix consisting of about k equally spaced columns of
% B.
stride = round( n / k );
A = B( :, 1:stride:n );
% Replace the comments below with their respective operations from the notebook
% C = A^T A
% V = A^T B
% Overwrite C with its LU factorization
% Extract the unit lower triangular matrix L and upper triangular matrix U.
L = tril( C, -1 ) + eye( size( C ) );
U = triu( C );
% Solve L(UX) = V, overwriting V with X
V = L \ V;
V = U \ V;
% Create a new figure in which you show the approximated picture. Notice
% that A and V are matrices of doubles, which much be converted to unsigned
% 8 byte integers for imshow to work.
figure
imshow( uint8( A * V ) );
% The two figures may be on top of each other. You may have to move the
% second one next to the first one.
% Play around by changing the number of columns you use for the
% approximation!

View File

@@ -0,0 +1,68 @@
% Close all figures
close all
% Read the picture in building.png into matrix B
[ B, map ] = imread( 'building.png' );
% Show the picture in a figure
imshow( B );
% Choose the (approximate) number of columns you want to use for the
% aproximation.
k = 30;
% Extract the number of rows and columns in B
[ m, n ] = size( B )
% B starts as an integer array. To do the computations, we need to
% make it into floating point numbers.
B = double( B );
% Pick A as the matrix consisting of about k equally spaced columns of
% B.
stride = round( n / k );
A = B( :, 1:stride:n );
% Replace the comments below with their respective operations from the notebook
% C = A^T A
C = A' * A;
% V = A^T B
V = A' * B;
% Overwrite C with its LU factorization
C = LU_unb_var5( C );
% Extract the unit lower triangular matrix L and upper triangular matrix U.
L = tril( C, -1 ) + eye( size( C ) );
U = triu( C );
% Solve L(UX) = V, overwriting V with X
V = L \ V;
V = U \ V;
% Create a new figure in which you show the approximated picture. Notice
% that A and V are matrices of doubles, which much be converted to unsigned
% 8 byte integers for imshow to work.
figure
imshow( uint8( A * V ) );
% Now let's use the SVD
% Compute the SVD
[ U, Sigma, V ] = svd( B );
% Use the first k columns of U and V and the first k singular value to
% approximate
figure
imshow( uint8( U( :, 1:k ) * Sigma( 1:k, 1:k ) * V( :, 1:k )' ) );
% The three figures may be on top of each other. You may have to move the
% second one next to the first one.
% Play around by changing the number of columns you use for the
% approximation!

View File

@@ -0,0 +1,56 @@
% Close all figures
close all
% Read the picture in building.png into matrix B
[ B, map ] = imread( 'building.png' );
% Show the picture in a figure
imshow( B );
% Choose the (approximate) number of columns you want to use for the
% aproximation.
k = 30;
% Extract the number of rows and columns in B
[ m, n ] = size( B )
% B starts as an integer array. To do the computations, we need to
% make it into floating point numbers.
B = double( B );
% Pick A as the matrix consisting of about k equally spaced columns of
% B.
stride = round( n / k );
A = B( :, 1:stride:n );
% Replace the comments below with their respective operations from the notebook
% C = A^T A
C = A' * A;
% V = A^T B
V = A' * B;
% Overwrite C with its LU factorization
C = LU_unb_var5( C );
% Extract the unit lower triangular matrix L and upper triangular matrix U.
L = tril( C, -1 ) + eye( size( C ) );
U = triu( C );
% Solve L(UX) = V, overwriting V with X
V = L \ V;
V = U \ V;
% Create a new figure in which you show the approximated picture. Notice
% that A and V are matrices of doubles, which much be converted to unsigned
% 8 byte integers for imshow to work.
figure
imshow( uint8( A * V ) );
% The two figures may be on top of each other. You may have to move the
% second one next to the first one.
% Play around by changing the number of columns you use for the
% approximation!

View File

@@ -0,0 +1,34 @@
function [ A_out ] = LU_unb_var5( A )
[ ATL, ATR, ...
ABL, ABR ] = FLA_Part_2x2( A, ...
0, 0, 'FLA_TL' );
while ( size( ATL, 1 ) < size( A, 1 ) )
[ A00, a01, A02, ...
a10t, alpha11, a12t, ...
A20, a21, A22 ] = FLA_Repart_2x2_to_3x3( ATL, ATR, ...
ABL, ABR, ...
1, 1, 'FLA_BR' );
%------------------------------------------------------------%
a21 = laff_invscal( alpha11, a21 );
A22 = laff_ger( -1, a21, a12t, A22 );
%------------------------------------------------------------%
[ ATL, ATR, ...
ABL, ABR ] = FLA_Cont_with_3x3_to_2x2( A00, a01, A02, ...
a10t, alpha11, a12t, ...
A20, a21, A22, ...
'FLA_TL' );
end
A_out = [ ATL, ATR
ABL, ABR ];
return

View File

@@ -0,0 +1,57 @@
function [ Q_out, R_out ] = QR_unb( A, Q, R )
[ AL, AR ] = FLA_Part_1x2( A, ...
0, 'FLA_LEFT' );
[ QL, QR ] = FLA_Part_1x2( Q, ...
0, 'FLA_LEFT' );
[ RTL, RTR, ...
RBL, RBR ] = FLA_Part_2x2( R, ...
0, 0, 'FLA_TL' );
while ( size( AL, 2 ) < size( A, 2 ) )
[ A0, a1, A2 ]= FLA_Repart_1x2_to_1x3( AL, AR, ...
1, 'FLA_RIGHT' );
[ Q0, q1, Q2 ]= FLA_Repart_1x2_to_1x3( QL, QR, ...
1, 'FLA_RIGHT' );
[ R00, r01, R02, ...
r10t, rho11, r12t, ...
R20, r21, R22 ] = FLA_Repart_2x2_to_3x3( RTL, RTR, ...
RBL, RBR, ...
1, 1, 'FLA_BR' );
%------------------------------------------------------------%
%r01 = Q0' * a1;
r01 = laff_gemv( 'Transpose', 1, Q0, a1, 0, r01 );
a1perp = a1 - Q0 * r01;
rho11 = norm( a1perp );
q1 = a1perp / rho11;
%------------------------------------------------------------%
[ AL, AR ] = FLA_Cont_with_1x3_to_1x2( A0, a1, A2, ...
'FLA_LEFT' );
[ QL, QR ] = FLA_Cont_with_1x3_to_1x2( Q0, q1, Q2, ...
'FLA_LEFT' );
[ RTL, RTR, ...
RBL, RBR ] = FLA_Cont_with_3x3_to_2x2( R00, r01, R02, ...
r10t, rho11, r12t, ...
R20, r21, R22, ...
'FLA_TL' );
end
Q_out = [ QL, QR ];
R_out = [ RTL, RTR
RBL, RBR ];
return

BIN
Programming/Week11/building.png Executable file

Binary file not shown.

After

Width:  |  Height:  |  Size: 180 KiB

View File

@@ -0,0 +1,23 @@
function [ A, V ] = CreateMatrixForEigenvalueProblem( eigs )
% Given a column vector eigs with desired eigenvalues, this routine creates
% a diagonalizable matrix A with eigenvalues eigs, and matrix V the columns
% of which equal normalized eigenvectors corresponding to the given
% eigenvalues, in order.
% extract the number of eigenvalues
n = size( eigs, 1 );
% create a random matrix that is n x n. Almost surely, it is nonsingular.
V = rand( n, n );
% normalize the columns of V to have norm 1
for i=1:n
V( :, i ) = V( :, i ) / norm( V ( :, i ) );
end
% Set A = V * diag( x ) * inv( V ) so that the columns of V equal the
% eigenvectors of A corresponding to the eigenvalues, in order.
A = V * diag( eigs ) / V;
end

View File

@@ -0,0 +1,94 @@
format long
% Choose the maximum number of iterations of the Inverse Power Method
% that will be performed.
k = 20;
% Choose the eigenvalues
disp( 'input a vector of eigenvalues. e.g.: [ 4; 3; 2; 1 ] ' );
eigs = input('');
n = size( eigs,1);
% Make sure that the last entry is equal to smallest in absolute value
[ eig_min ] = min( abs( eigs( 1:n-1 ) ) );
if eig_min < abs( eigs( n ) )
abort( 'The last entry should be equal to smallest in absolute value.' );
end
% extract the number of eigenvalues
n = size( eigs, 1 );
[ A, V ] = CreateMatrixForEigenvalueProblem( eigs );
disp( 'Matrix A:' )
disp( A );
% Print V
disp( 'Matrix of eigenvectors:' );
disp( V );
% Print eigenvalues
disp( 'Eigenvalues:' );
disp( eigs );
% Create a random initial vector x, and normalize it to have unit length
x = rand( n, 1 );
x = x / norm( x );
disp( 'Initial random vector:' )
disp( x )
disp( 'iteration' );
disp( 0 );
% Print the length of the component of x orthogonal to the eigenvector
% associated with the smallest eigenvalue (in magnitude)
disp( 'The length of the component of x orthogonal to V( :, n ) is ' );
disp( norm( x - x' * V( :, n ) * V( :, n ) ) );
% Compute the Rayleigh quotient (no need to divide by x' * x since x is
% of unit length)
disp( 'Rayleigh quotient:' );
disp( x' * A * x );
% Compute the LU factorization (with pivoting) of A
[ L, U, P ] = lu( A );
cont = 1;
% Perform at most k steps of the Inverse Power Method
for i=1:k
cont = input( 'continue? (0=NO, return = YES)' );
if cont == 0
break;
end
disp( 'iteration' );
disp( i );
% Next step of Inverse Power Method
% x = A \ x; Let's use the LU factorization instead.
x = P * x;
x = L \ x;
x = U \ x;
x = x / norm( x );
% Print the length of the component of x orthogonal to the eigenvector
% associated with the smallest eigenvalue (in magnitude)
disp( 'The length of the component of x orthogonal to V( :, n ) is ' );
disp( norm( x - x' * V( :, n ) * V( :, n ) ) );
% Compute the Rayleigh quotient (no need to divide by x' * x since x is
% of unit length)
sigma = x' * A * x;
disp( 'sigma (Rayleigh quotient):' );
disp( sigma );
end
disp( 'Final vector x:' );
disp( x );
disp( 'A * x - sigma * x (should equal, approximately, the zero vector)' );
disp( A * x - sigma * x );

View File

@@ -0,0 +1,92 @@
format long
% Choose the maximum number of iterations of the power method that will be performed.
k = 20;
% Choose the eigenvalues
disp( 'input a vector of eigenvalues. e.g.: [ 4; 3; 2; 1 ] ' );
eigs = input('');
n = size( eigs,1);
% Make sure that the first entry is equal to largest in absolute value
[ eig_max ] = max( abs( eigs( 2:n ) ) );
if eig_max > abs( eigs( 1 ) )
abort( 'The first entry should be equal to largest in absolute value.' );
end
% Create a diagonalizable matrix A with eigenvalues given by eigs and
% matrix V the columns of which are the corresponding eigenvectors,
% normalized to have length 1.
[ A, V ] = CreateMatrixForEigenvalueProblem( eigs )
% extract the number of eigenvalues
n = size( eigs, 1 );
% Print A
disp( 'Matrix A:' )
disp( A );
% Print V
disp( 'Matrix of eigenvectors:' );
disp( V );
% Print eigenvalues
disp( 'Eigenvalues:' );
disp( eigs );
% Create a random initial vector x, and normalize it to have unit length
x = rand( n, 1 );
x = x / norm( x );
disp( 'Initial random vector:' )
disp( x )
disp( 'iteration' );
disp( 0 );
% Print the length of the component of x orthogonal to the eigenvector
% associated with the largest eigenvalue (in magnitude)
disp( 'The length of the component of x orthogonal to V( :, 1 ) is ' );
disp( norm( x - x' * V( :, 1 ) * V( :, 1 ) ) );
% Compute the Rayleigh quotient (no need to divide by x' * x since x is
% of unit length)
disp( 'Rayleigh quotient:' );
disp( x' * A * x );
cont = 1;
% Perform at most k steps of the power method
for i=1:k
cont = input( 'continue? (0=NO, return = YES)' );
if cont == 0
break;
end
disp( 'iteration' );
disp( i );
% Next step of Power Method
x = A * x;
x = x / norm( x );
% Print the length of the component of x orthogonal to the eigenvector
% associated with the largest eigenvalue (in magnitude)
disp( 'The length of the component of x orthogonal to V( :, 1 ) is ' );
disp( norm( x - x' * V( :, 1 ) * V( :, 1 ) ) );
% Compute the Rayleigh quotient (no need to divide by x' * x since x is
% of unit length)
sigma = x' * A * x;
disp( 'sigma (Rayleigh quotient):' );
disp( sigma );
end
disp( 'Final vector x:' );
disp( x );
disp( 'A * x - sigma * x (should equal, approximately, the zero vector)' );
disp( A * x - sigma * x );

View File

@@ -0,0 +1,91 @@
format long
% Choose the maximum number of iterations of the power method that will be performed.
k = 20;
% Choose the eigenvalues
disp( 'input a vector of eigenvalues. e.g.: [ 4; 3; 2; 1 ] ' );
eigs = input('');
n = size( eigs,1);
% Make sure that the first entry is equal to largest in absolute value
[ eig_max ] = max( abs( eigs( 2:n ) ) );
if eig_max > abs( eigs( 1 ) )
abort( 'The first entry should be equal to largest in absolute value.' );
end
% extract the number of eigenvalues
n = size( eigs, 1 );
disp( 'Now we set the second eigenvalue equal to the negative of the ' );
disp( 'first one' );
eigs( 2 ) = -eigs( 1 );
[ A, V ] = CreateMatrixForEigenvalueProblem( eigs );
disp( 'Matrix A:' )
disp( A );
% Print V
disp( 'Matrix of eigenvectors:' );
disp( V );
% Print eigenvalues
disp( 'Eigenvalues:' );
disp( eigs );
input( 'hit return to continue' );
% Create a random initial vector x, and normalize it to have unit length
x = rand( n, 1 );
x = x / norm( x );
disp( 'Initial random vector:' )
disp( x )
disp( 'iteration' );
disp( 0 );
% Compute the Rayleigh quotient (no need to divide by x' * x since x is
% of unit length)
disp( 'Rayleigh quotient:' );
disp( x' * A * x );
cont = 1; % (continue = YES)
% Perform at most k steps of the power method
for i=1:k
cont = input( 'continue? (0=NO, return = YES)' );
if cont == 0
break;
end
disp( 'iteration' );
disp( i );
% Next step of Power Method
x = A * x;
x = x / norm( x );
% Print the length of the component of x orthogonal to the eigenvector
% associated with the largest eigenvalue (in magnitude)
disp( 'The length of the component of x orthogonal to V( :, 1 ) is ' );
disp( norm( x - x' * V( :, 1 ) * V( :, 1 ) ) );
% Compute the Rayleigh quotient (no need to divide by x' * x since x is
% of unit length)
disp( 'Rayleigh quotient: (notice that this doesnt converge)' );
disp( x' * A * x );
% Print the length of the component of x orthogonal to the eigenvector
% associated with the largest eigenvalue (in magnitude)
disp( 'The length of the component of x orthogonal to the spaces ' );
disp( ' spanned by V( :, 1 ) and V( :, 2 ) is ' );
disp( norm( x - ...
V( :, 1:2 ) * inv( V( :, 1:2 )' * V( :, 1:2 ) ) * V( :, 1:2 )' * x ) );
end

View File

@@ -0,0 +1,96 @@
format long
% Choose the maximum number of iterations of the Inverse Power Method
% that will be performed.
k = 20;
% Choose the eigenvalues
disp( 'input a vector of eigenvalues. e.g.: [ 4; 3; 2; 1 ] ' );
eigs = input('');
n = size( eigs,1);
% Make sure that the last entry is equal to smallest in absolute value
[ eig_min ] = min( abs( eigs( 1:n-1 ) ) );
if eig_min < abs( eigs( n ) )
abort( 'The last entry should be equal to smallest in absolute value.' );
end
% extract the number of eigenvalues
n = size( eigs, 1 );
[ A, V ] = CreateMatrixForEigenvalueProblem( eigs );
disp( 'Matrix A:' )
disp( A );
% Print V
disp( 'Matrix of eigenvectors:' );
disp( V );
% Print eigenvalues
disp( 'Eigenvalues:' );
disp( eigs );
% Create a random initial vector x, and normalize it to have unit length
x = rand( n, 1 );
x = x / norm( x );
disp( 'Initial random vector:' )
disp( x )
disp( 'iteration' );
disp( 0 );
% Print the length of the component of x orthogonal to the eigenvector
% associated with the smallest eigenvalue (in magnitude)
disp( 'The length of the component of x orthogonal to V( :, n ) is ' );
disp( norm( x - x' * V( :, n ) * V( :, n ) ) );
% Compute the Rayleigh quotient (no need to divide by x' * x since x is
% of unit length)
disp( 'initial Rayleigh quotient (shift):' );
sigma = x' * A * x;
disp( sigma );
% Compute the LU factorization (with pivoting) of A - \sigma I
% [ L, U, P ] = lu( A - sigma * ( eye( size( A ) ) ));
cont = 1;
% Perform at most k steps of the Rayleigh Quotient
for i=1:k
cont = input( 'continue? (0=NO, return = YES)' );
if cont == 0
break;
end
disp( 'iteration' );
disp( i );
% Next step of Rayleigh Quotient Iteration
x = ( A - sigma * eye( size( A ) ) ) \ x;
x = x / norm( x );
% Since you don't know which eigenvalue and vector the method is
% homing in on, showing the length of the component orthogonal to
% the last eigenvector is not meaningful.
% disp( 'The length of the component of x orthogonal to V( :, n ) is ' );
% disp( norm( x - x' * V( :, n ) * V( :, n ) ) );
% Compute the next Rayleigh quotient (no need to divide by x' * x since x is
% of unit length)
sigma = x' * A * x;
disp( 'Rayleigh quotient:' );
disp( sigma );
end
disp( 'Final vector x:' );
disp( x );
disp( 'A * x - sigma * x (should equal, approximately, the zero vector)' );
disp( A * x - sigma * x );

View File

@@ -0,0 +1,93 @@
format long
% Choose the maximum number of iterations of the Inverse Power Method
% that will be performed.
k = 20;
% Choose the eigenvalues
disp( 'input a vector of eigenvalues. e.g.: [ 4; 3; 2; 1 ] ' );
eigs = input('');
n = size( eigs, 1 );
% Make sure that the last entry is equal to smallest in absolute value
[ eig_min ] = min( abs( eigs( 1:n-1 ) ) );
if eig_min < abs( eigs( n ) )
abort( 'The last entry should be equal to smallest in absolute value.' );
end
[ A, V ] = CreateMatrixForEigenvalueProblem( eigs );
disp( 'Matrix A:' )
disp( A );
% Print V
disp( 'Matrix of eigenvectors:' );
disp( V );
% Print eigenvalues
disp( 'Eigenvalues:' );
disp( eigs );
% Ask for a shift
sigma = input( 'enter a shift to use: (a number close to the smallest eigenvalue) ' );
% Create a random initial vector x, and normalize it to have unit length
x = rand( n, 1 );
x = x / norm( x );
disp( 'Initial random vector:' )
disp( x )
disp( 'iteration' );
disp( 0 );
% Print the length of the component of x orthogonal to the eigenvector
% associated with the smallest eigenvalue (in magnitude)
disp( 'The length of the component of x orthogonal to V( :, n ) is ' );
disp( norm( x - x' * V( :, n ) * V( :, n ) ) );
% Compute the Rayleigh quotient (no need to divide by x' * x since x is
% of unit length)
disp( 'Rayleigh quotient:' );
disp( x' * A * x );
% Compute the LU factorization (with pivoting) of A - \sigma I
[ L, U, P ] = lu( A - sigma * ( eye( size( A ) ) ));
cont = 1;
% Perform at most k steps of the Shifted Inverse Power Method
for i=1:k
cont = input( 'continue? (0=NO, return = YES)' );
if cont == 0
break;
end
disp( 'iteration' );
disp( i );
% Next step of Inverse Power Method
% x = ( A - sigma I ) \ x; Let's use the LU factorization instead.
x = P * x;
x = L \ x;
x = U \ x;
x = x / norm( x );
% Print the length of the component of x orthogonal to the eigenvector
% associated with the smallest eigenvalue (in magnitude)
disp( 'The length of the component of x orthogonal to V( :, n ) is ' );
disp( norm( x - x' * V( :, n ) * V( :, n ) ) );
% Compute the Rayleigh quotient (no need to divide by x' * x since x is
% of unit length)
disp( 'sigma (Rayleigh quotient):' );
disp( x' * A * x );
end
disp( 'Final vector x:' );
disp( x );
sigma = x' * A * x;
disp( 'A * x - sigma * x (should equal, approximately, the zero vector)' );
disp( A * x - sigma * x );

View File

@@ -0,0 +1,88 @@
function [ y_out ] = laff_gemv( trans, alpha, A, x, beta, y )
% laff_gemv( trans, alpha, A, x, beta, y )
% if trans = 'No transpose' returns alpha * A * x + beta * y
% if trans = 'Transpose' returns alpha * A' * x + beta * y
% Check parameters
% Check if trans is 'No transpose' or 'Transpose'
if (~isequal( trans, 'No transpose' )) & (~isequal( trans, 'Transpose' ))
disp( 'gemv: illegal trans parameter' )
y_out = 'FAILED';
return
end
% Check if alpha is a scalar
if ~isscalar( alpha )
disp( 'gemv: alpha is not a scalar' )
y_out = 'FAILED'
return
end
% Check if beta is a scalar
if ~isscalar( beta )
disp( 'gemv: beta is not a scalar' )
y_out = 'FAILED'
return
end
% Check if x is a (row or column) vector
if ~isvector( x )
disp( 'gemv: x is not a vector' )
y_out = 'FAILED'
return
end
% Check if y is a (row or column) vector
if ~isvector( y )
disp( 'gemv: y is not a vector' )
y_out = 'FAILED'
return
end
[ m_A, n_A ] = size( A );
[ m_x, n_x ] = size( x );
[ m_y, n_y ] = size( y );
% Now we cheat a little:
% if x is a row vector, we make it a column vector
if n_x ~= 1
x = x';
m_x = n_x;
end
% if y is a row vector, we make it a column vector (but remember what it
% was so that y_out is set to be a row or column vector, at the end)
if n_y ~= 1
y = y';
m_y = n_y;
y_is_a_row = 1;
else
y_is_a_row = 0;
end
if isequal( trans, 'No transpose' )
if n_A ~= m_x | m_A ~= m_y
disp( 'gemv: sizes of A, x, and/or y do not match' )
y_out = 'FAILED';
return
else
y_out = alpha * A * x + beta * y;
end
else
if m_A ~= m_x | n_A ~= m_y
disp( 'gemv: sizes of A, x, and/or y do not match' )
y_out = 'FAILED';
return
else
y_out = alpha * A' * x + beta * y;
end
end
if y_is_a_row
y_out = y_out';
end
return
end

View File

@@ -0,0 +1,55 @@
function [ A_out ] = laff_ger( alpha, x, y, A )
% laff_ger( alpha, x, y, A )
% computes alpha * x * y' + A
% but it is a bit tricky: x and y can be row or column vectors, in any
% combination.
% Check parameters
if ~isscalar( alpha )
disp( 'ger: alpha is not a scalar' )
A_out = 'FAILED'
return
end
% Check if x is a (row or column) vector
if ~isvector( x )
disp( 'ger: x is not a vector' )
A_out = 'FAILED'
return
end
% Check if y is a (row or column) vector
if ~isvector( y )
disp( 'ger: y is not a vector' )
A_out = 'FAILED'
return
end
[ m_A, n_A ] = size( A );
[ m_x, n_x ] = size( x );
[ m_y, n_y ] = size( y );
% Now we cheat a little:
% if x is a row vector, we make it a column vector
if n_x ~= 1
x = x';
m_x = n_x;
end
% if y is a row vector, we make it a column vector
if n_y ~= 1
y = y';
m_y = n_y;
end
if m_A ~= m_x | n_A ~= m_y
disp( 'ger: sizes of A, x, and/or y do not match' )
A_out = 'FAILED';
return
else
A_out = alpha * x * y' + A;
end
return
end

View File

@@ -0,0 +1,58 @@
function [ A_out ] = laff_syr( uplo, alpha, x, A )
% laff_syr( uplo, alpha, x, A )
% computes alpha * x * x' + A
% but it is a bit tricky: x be a row vector.
%
% if uplo = 'Lower triangular', the lower triangular part of A is
% updated.
% if uplo = 'Upper triangular', the upper triangular part of A is
% updated.
% Check parameters
% Check if uplo is 'Lower triangular' or 'Upper triangular'
if (~isequal( uplo, 'Lower triangular' )) & (~isequal( uplo, 'Upper triangular'))
disp( 'syr: illegal uplo parameter' )
A_out = 'FAILED';
return
end
if ~isscalar( alpha )
disp( 'syr: alpha is not a scalar' )
A_out = 'FAILED'
return
end
% Check if x is a (row or column) vector
if ~isvector( x )
disp( 'syr: x is not a vector' )
A_out = 'FAILED'
return
end
[ m_A, n_A ] = size( A );
[ m_x, n_x ] = size( x );
% Now we cheat a little:
% if x is a row vector, we make it a column vector
if n_x ~= 1
x = x';
m_x = n_x;
end
if m_A ~= n_A | n_A ~= m_x
disp( 'syr: sizes of A and/or x do not match' )
A_out = 'FAILED';
return
else
if isequal( uplo, 'Lower triangular' )
A_out = alpha * tril( x * x') + A;
else
A_out = alpha * triu( x * x') + A;
end
end
return
end

View File

@@ -0,0 +1,93 @@
function [ y ] = laff_trmv( uplo, trans, diag, A, x )
% laff_trmv( uplo, trans, diag, A, y )
% laff_trmv( 'Lower triangular', 'No transpose', 'Nonunit diag', A, y )
% computes y := tril( A ) * x
% laff_trmv( 'Upper triangular', 'Transpose', 'Nonunit diag', A, y )
% computes y = triu( A )' * x
% laff_trmv( 'Lower triangular', 'No transpose', 'Unit diag', A, y )
% computes y = tril( A ) * x except with (implicitly) ones on the
% diagonal.
% Check parameters
% Check if uplo is 'Lower triangular' or 'Upper triangular'
if (~isequal( uplo, 'Lower triangular' )) & (~isequal( uplo, 'Upper triangular'))
disp( 'trmv: illegal uplo parameter' )
y = 'FAILED';
return
end
% Check if trans is 'No transpose' or 'Transpose'
if (~isequal( trans, 'No transpose' )) & (~isequal( trans, 'Transpose' ))
disp( 'trmv: illegal trans parameter' )
y = 'FAILED';
return
end
% Check if diag is 'Nonunit diag' or 'Unit diag'
if (~isequal( diag, 'Nonunit diag' )) & (~isequal( diag, 'Unit diag'))
disp( 'trmv: illegal diag parameter' )
y = 'FAILED';
return
end
% Check if x is a (row or column) vector
if ~isvector( x )
disp( 'trmv: x is not a vector' )
y = 'FAILED'
return
end
[ m_A, n_A ] = size( A );
[ m_x, n_x ] = size( x );
% Now we cheat a little:
% if x is a row vector, we make it a column vector (but remember what it
% was so that x is set to be a column or row vector, at the end)
if n_x ~= 1
x = x';
m_x = n_x;
x_is_a_row = 1;
else
x_is_a_row = 0;
end
% Check that A is square and th size of (the updated) x is the same as the
% size of A.
if m_A ~= n_A | m_A ~= m_x
disp( 'trmv: A and/or x do not have same size' )
y = 'FAILED';
return
end
% Now lets fix A
if isequal( uplo, 'Lower triangular' )
if isequal( diag, 'Nonunit diag' )
A = tril( A );
else
A = tril( A, -1 ) + eye( size( A ) );
end
else
if isequal( diag, 'Nonunit diag' )
A = triu( A );
else
A = triu( A, 1 ) + eye( size( A ) );
end
end
if isequal( trans, 'Transpose' )
A = A';
end
% Now use matlab's intrinsic method for computing y = A x
y = A * x;
% And fix the return to be consistent with y
if x_is_a_row
y = y';
end
return
end

View File

@@ -0,0 +1,94 @@
function [ x ] = laff_trsv( uplo, trans, diag, A, y )
% laff_trsv( uplo, trans, diag, A, y )
% laff_trsv( 'Lower triangular', 'No transpose', 'Nonunit diag', A, y )
% solves tril( A ) x = y
% laff_trsv( 'Upper triangular', 'Transpose', 'Nonunit diag', A, y )
% solves triu( A )' x = y
% laff_trsv( 'Lower triangular', 'No transpose', 'Unit diag', A, y )
% solves tril( A ) x = y except with (implicitly) ones on the
% diagonal. In other words, it solves
% ( tril( A,-1 ) + eye( size( A ) ) x = y
% Check parameters
% Check if uplo is 'Lower triangular' or 'Upper triangular'
if (~isequal( uplo, 'Lower triangular' )) & (~isequal( uplo, 'Upper triangular'))
disp( 'trsv: illegal uplo parameter' )
x = 'FAILED';
return
end
% Check if trans is 'No transpose' or 'Transpose'
if (~isequal( trans, 'No transpose' )) & (~isequal( trans, 'Transpose' ))
disp( 'trsv: illegal trans parameter' )
x = 'FAILED';
return
end
% Check if diag is 'Nonunit diag' or 'Unit diag'
if (~isequal( diag, 'Nonunit diag' )) & (~isequal( diag, 'Unit diag'))
disp( 'trsv: illegal diag parameter' )
x = 'FAILED';
return
end
% Check if y is a (row or column) vector
if ~isvector( y )
disp( 'trsv: y is not a vector' )
x = 'FAILED'
return
end
[ m_A, n_A ] = size( A );
[ m_y, n_y ] = size( y );
% Now we cheat a little:
% if y is a row vector, we make it a column vector (but remember what it
% was so that x is set to be a column or row vector, at the end)
if n_y ~= 1
y = y';
m_y = n_y;
y_is_a_row = 1;
else
y_is_a_row = 0;
end
% Check that A is square and th size of (the updated) y is the same as the
% size of A.
if m_A ~= n_A | m_A ~= m_y
disp( 'trsv: A and/or y do not have same size' )
x = 'FAILED';
return
end
% Now lets fix A
if isequal( uplo, 'Lower triangular' )
if isequal( diag, 'Nonunit diag' )
A = tril( A );
else
A = tril( A, -1 ) + eye( size( A ) );
end
else
if isequal( diag, 'Nonunit diag' )
A = triu( A );
else
A = triu( A, 1 ) + eye( size( A ) );
end
end
if isequal( trans, 'Transpose' )
A = A';
end
% Now use matlab's intrinsic method for solving A x = y
x = A \ y;
% And fix the return to be consistent with y
if y_is_a_row
x = x';
end
return
end

View File

@@ -0,0 +1,50 @@
function [ AL, AR ] = FLA_Cont_with_1x3_to_1x2( A0, A1, A2,...
side )
%
% function [ AL, AR ] = FLA_Cont_with_1x3_to_1x2( A0, A1, A2,...
% side )
%
% Purpose: Update the 1 x 2 partitioning of matrix A by moving the
% boundaries so that A1 is added to the side indicated by side
%
[ m0, n0 ] = size( A0 );
[ m1, n1 ] = size( A1 );
[ m2, n2 ] = size( A2 );
[ mside, nside ] = size( side );
%
% Check input parameters
%
if( ( m0 ~= m1 )|( m1 ~= m2 ) )
error('input matrices must have the same number of rows');
elseif( ( mside ~= 1 )|( nside < 8 )|( nside > 9 ) )
error('side must be a string with contents equal to FLA_LEFT or FLA_RIGHT');
elseif( ( nside == 8 )&( ~strcmp( side(1:8), 'FLA_LEFT' ) ) )
error('side must be a string with contents equal to FLA_LEFT or FLA_RIGHT');
elseif( nside == 9 )
if ( ~strcmp( side(1:9), 'FLA_RIGHT' ) )
error('side must be a string with contents equal to FLA_LEFT or FLA_RIGHT');
end
end
%
% Continue with...
%
if( strcmp( side(1:8), 'FLA_LEFT' ) )
if( ( m0 == 0 )|( n0+n1 == 0 ) )
AL = zeros( m0, n0+n1 );
else
AL = [ A0, A1 ];
end
AR = A2;
else
AL = A0;
if( ( m0 == 0 )|( n1+n2 == 0 ) )
AR = zeros( m0, n1+n2 );
else
AR = [ A1, A2 ];
end
end
%
return;
%
% End of FLA_Cont_with_1x3_to_1x2
%

View File

@@ -0,0 +1,58 @@
function [ AT,...
AB ] = FLA_Cont_with_3x1_to_2x1( A0,...
A1,...
A2,...
side )
%
% function [ AT,...
% AB ] = FLA_Cont_with_3x1_to_2x1( A0,...
% A1,...
% A2,...
% side )
%
% Purpose: Update the 2 x 1 partitioning of matrix A by moving the
% boundaries so that A1 is added to the side indicated by side
%
[ m0, n0 ] = size( A0 );
[ m1, n1 ] = size( A1 );
[ m2, n2 ] = size( A2 );
[ mside, nside ] = size( side );
%
% Check input parameters
%
if( ( n0 ~= n1 )|( n1 ~= n2 ) )
error('input matrices must have the same number of columns');
elseif( ( mside ~= 1 )|( ( nside ~= 7 )&( nside ~= 10 ) ) )
error('side must be a string with contents equal to FLA_TOP or FLA_BOTTOM');
elseif( ( nside == 7 )&( ~strcmp( side(1:7), 'FLA_TOP' ) ) )
error('side must be a string with contents equal to FLA_TOP or FLA_BOTTOM');
elseif( nside == 10 )
if ( ~strcmp( side(1:10), 'FLA_BOTTOM' ) )
error('side must be a string with contents equal to FLA_TOP or FLA_BOTTOM');
end
end
%
% Continue with...
%
if( strcmp( side(1:7), 'FLA_TOP' ) )
if( ( m0+m1 == 0 )|( n0 == 0 ) )
AT = zeros( m0+m1, n0 );
else
AT = [ A0;...
A1 ];
end
AB = A2;
else
AT = A0;
if( ( m1+m2 == 0 )|( n0 == 0 ) )
AB = zeros( m1+m2, n0 );
else
AB = [ A1;...
A2 ];
end
end
%
return;
%
% End of FLA_Cont_with_3x1_to_2x1
%

View File

@@ -0,0 +1,134 @@
function [ ATL, ATR,...
ABL, ABR ] = FLA_Cont_with_3x3_to_2x2( A00, A01, A02,...
A10, A11, A12,...
A20, A21, A22, ...
quadrant )
%
% function [ ATL, ATR,...
% ABL, ABR ] = FLA_Cont_with_3x3_to_2x2( A00, A01, A02,...
% A10, A11, A12,...
% A20, A21, A22, ...
% quadrant )
%
% Purpose: Update the 2 x 2 partitioning of matrix A by
% moving the boundaries so that A11 is added to
% the quadrant indicated by quadrant
%
[ m00, n00 ] = size( A00 );
[ m01, n01 ] = size( A01 );
[ m02, n02 ] = size( A02 );
[ m10, n10 ] = size( A10 );
[ m11, n11 ] = size( A11 );
[ m12, n12 ] = size( A12 );
[ m20, n20 ] = size( A20 );
[ m21, n21 ] = size( A21 );
[ m22, n22 ] = size( A22 );
[ mquadrant, nquadrant ] = size( quadrant );
%
% Check input parameters
%
if( ( m00 ~= m01 )|( m01 ~= m02 ) )
error('input matrices in first row must have the same number of rows');
elseif( ( m10 ~= m11 )|( m11 ~= m12 ) )
error('input matrices in second row must have the same number of rows');
elseif( ( m20 ~= m21 )|( m21 ~= m22 ) )
error('input matrices in third row must have the same number of rows');
elseif( ( n00 ~= n10 )|( n10 ~= n20 ) )
error('input matrices in first column must have the same number of columns');
elseif( ( n01 ~= n11 )|( n11 ~= n21 ) )
error('input matrices in second column must have the same number of columns');
elseif( ( n02 ~= n12 )|( n12 ~= n22 ) )
error('input matrices in third column must have the same number of columns');
elseif( ( mquadrant ~= 1 )|( nquadrant ~= 6 ) )
error('quadrant must be a string with contents equal to FLA_TL, FLA_TR, FLA_BL, or FLA_BR');
elseif( ( ~strcmp( quadrant(1:6), 'FLA_TL' ) )&...
( ~strcmp( quadrant(1:6), 'FLA_TR' ) )&...
( ~strcmp( quadrant(1:6), 'FLA_BL' ) )&...
( ~strcmp( quadrant(1:6), 'FLA_BR' ) ) )
error('quadrant must be a string with contents equal to FLA_TL, FLA_TR, FLA_BL, or FLA_BR');
end
%
% Continue with...
%
if( strcmp( quadrant(1:6), 'FLA_TL' ) )
if( ( m00+m10 == 0 )|( n00+n01 == 0 ) )
ATL = zeros( m00+m10, n00+n01 );
else
ATL = [ A00, A01;...
A10 A11 ];
end
if( ( m02+m12 == 0 )|( n02 == 0 ) )
ATR = zeros( m02+m12, n02 );
else
ATR = [ A02;...
A12 ];
end
if( ( m20 == 0 )|( n20+n21 == 0 ) )
ABL = zeros( m20, n20+n21 );
else
ABL = [ A20, A21 ];
end
ABR = A22;
elseif( strcmp( quadrant(1:6), 'FLA_TR' ) )
if( ( m00+m10 == 0 )|( n00 == 0 ) )
ATL = zeros( m00+m10, n00 );
else
ATL = [ A00;...
A10 ];
end
if( ( m01+m11 == 0 )|( n01+n02 == 0 ) )
ATR = zeros( m01+m11, n01+n02 );
else
ATR = [ A01, A02;...
A11, A12 ];
end
ABL = A20;
if( ( m21 == 0 )|( n21+n22 == 0 ) )
ABR = zeros( m21, n21+n22 );
else
ABR = [ A21, A22 ];
end
elseif( strcmp( quadrant(1:6), 'FLA_BL' ) )
if( ( m00 == 0 )|( n00+n01 == 0 ) )
ATL = zeros( m00, n00+n01 );
else
ATL = [ A00, A01 ];
end
ATR = A02;
if( ( m10+m20 == 0 )|( n10+n11 == 0 ) )
ABL = zeros( m10+m20, n10+n11 );
else
ABL = [ A10, A11;...
A20, A21 ];
end
if( ( m12+m22 == 0 )|( n12 == 0 ) )
ABR = zeros( m12+m22, n12 );
else
ABR = [ A12;...
A22 ];
end
else
ATL = A00;
if( ( m01 == 0 )|( n01+n02 == 0 ) )
ATR = zeros( m01, n01+n02 );
else
ATR = [ A01, A02 ];
end
if( ( m10+m20 == 0 )|( n10 == 0 ) )
ABL = zeros( m10+m20, n10 );
else
ABL = [ A10;...
A20 ];
end
if( ( m11+m21 == 0 )|( n11+n12 == 0 ) )
ABR = zeros( m11+m21, n11+n12 );
else
ABR = [ A11, A12;...
A21, A22 ];
end
end
%
return;
%
% End of FLA_Cont_with_3x3_to_2x2
%

View File

@@ -0,0 +1,37 @@
function [ AL, AR ] = FLA_Part_1x2( A,...
nb, side )
%
% function [ AL, AR ] = FLA_Part_1x2( A,...
% nb, side )
% Purpose: Partition matrix A into a left and a right side
% where the side indicated by side has nb columns
%
n = size( A, 2 );
[ mside, nside ] = size( side );
%
% Check input parameters
%
if( ( mside ~= 1 )|( nside < 8 )|( nside > 9 ) )
error('side must be a string with contents equal to FLA_LEFT or FLA_RIGHT');
elseif( ( nside == 8 )&( ~strcmp( side(1:8), 'FLA_LEFT' ) ) )
error('side must be a string with contents equal to FLA_LEFT or FLA_RIGHT');
elseif( nside == 9 )
if ( ~strcmp( side(1:9), 'FLA_RIGHT' ) )
error('side must be a string with contents equal to FLA_LEFT or FLA_RIGHT');
end
end
%
% Partitioning...
%
if( strcmp( side(1:8), 'FLA_LEFT' ) )
AL = A(:,1:nb);
AR = A(:,nb+1:n);
else
AL = A(:,1:n-nb);
AR = A(:,n-nb+1:n);
end
%
return;
%
% End of FLA_Part_1x2
%

View File

@@ -0,0 +1,40 @@
function [ AT,...
AB ] = FLA_Part_2x1( A,...
mb, side )
%
% function [ AT,...
% AB ] = FLA_Part_2x1( A,...
% mb, side )
%
% Purpose: Partition matrix A into a top and a bottom side
% where the side indicated by {\tt side} has $ {\tt mb} $ rows
%
m = size( A, 1 );
[ mside, nside ] = size( side );
%
% Check input parameters
%
if( ( mside ~= 1 )|( ( nside ~= 7 )&( nside ~= 10 ) ) )
error('side must be a string with contents equal to FLA_TOP or FLA_BOTTOM');
elseif( ( nside == 7 )&( ~strcmp( side(1:7), 'FLA_TOP' ) ) )
error('side must be a string with contents equal to FLA_TOP or FLA_BOTTOM');
elseif( nside == 10 )
if ( ~strcmp( side(1:10), 'FLA_BOTTOM' ) )
error('side must be a string with contents equal to FLA_TOP or FLA_BOTTOM');
end
end
%
% Partitioning...
%
if( strcmp( side(1:7), 'FLA_TOP' ) )
AT = A(1:mb,:);
AB = A(mb+1:m,:);
else
AT = A(1:m-mb,:);
AB = A(m-mb+1:m,:);
end
%
return;
%
% End of FLA_Part_2x1
%

View File

@@ -0,0 +1,52 @@
function [ ATL, ATR,...
ABL, ABR ] = FLA_Part_2x2( A,...
mb, nb, quadrant )
%
% function [ ATL, ATR,...
% ABL, ABR ] = FLA_Part_2x2( A,...
% mb, nb, quadrant )
% Purpose: Partition matrix A into four quadrants
% where the quadrant indicated by quadrant is mb x nb
%
[ m, n ] = size( A );
[ mquadrant, nquadrant ] = size( quadrant );
%
% Check input parameters
%
if( ( mquadrant ~= 1 )|( nquadrant ~= 6 ) )
error('quadrant must be a string with contents equal to FLA_TL, FLA_TR, FLA_BL, or FLA_BR');
elseif( ( ~strcmp( quadrant(1:6), 'FLA_TL' ) )&...
( ~strcmp( quadrant(1:6), 'FLA_TR' ) )&...
( ~strcmp( quadrant(1:6), 'FLA_BL' ) )&...
( ~strcmp( quadrant(1:6), 'FLA_BR' ) ) )
error('quadrant must be a string with contents equal to FLA_TL, FLA_TR, FLA_BL, or FLA_BR');
end
%
% Partitioning...
%
if( strcmp( quadrant(1:6), 'FLA_TL' ) )
ATL = A(1:mb,1:nb);
ATR = A(1:mb,nb+1:n);
ABL = A(mb+1:m,1:nb);
ABR = A(mb+1:m,nb+1:n);
elseif( strcmp( quadrant(1:6), 'FLA_TR' ) )
ATL = A(1:mb,1:n-nb);
ATR = A(1:mb,n-nb+1:n);
ABL = A(mb+1:m,1:n-nb);
ABR = A(mb+1:m,n-nb+1:n);
elseif( strcmp( quadrant(1:6), 'FLA_BL' ) )
ATL = A(1:m-mb,1:nb);
ATR = A(1:m-mb,nb+1:n);
ABL = A(m-mb+1:m,1:nb);
ABR = A(m-mb+1:m,nb+1:n);
else
ATL = A(1:m-mb,1:n-nb);
ATR = A(1:m-mb,n-nb+1:n);
ABL = A(m-mb+1:m,1:n-nb);
ABR = A(m-mb+1:m,n-nb+1:n);
end
%
return;
%
% End of FLA_Part_2x2
%

View File

@@ -0,0 +1,44 @@
function [ A0, A1, A2 ] = FLA_Repart_1x2_to_1x3( AL, AR,...
nb, side )
%
% function [ A0, A1, A2 ] = FLA_Repart_1x2_to_1x3( AL, AR,...
% nb, side )
%
% Purpose: Repartition a 1 x 2 partitioning of matrix A into
% a 1 x 3 partitioning where submatrix A1 with nb columns is split from
% the side indicated by side
%
[ ml, nl ] = size( AL );
[ mr, nr ] = size( AR );
[ mside, nside ] = size( side );
%
% Check input parameters
%
if( ml ~= mr )
error('input matrices must have the same number of rows');
elseif( ( mside ~= 1 )|( nside < 8 )|( nside > 9 ) )
error('side must be a string with contents equal to FLA_LEFT or FLA_RIGHT');
elseif( ( nside == 8 )&( ~strcmp( side(1:8), 'FLA_LEFT' ) ) )
error('side must be a string with contents equal to FLA_LEFT or FLA_RIGHT');
elseif( nside == 9 )
if ( ~strcmp( side(1:9), 'FLA_RIGHT' ) )
error('side must be a string with contents equal to FLA_LEFT or FLA_RIGHT');
end
end
%
% Repartitioning...
%
if( strcmp( side(1:8), 'FLA_LEFT' ) )
A0 = AL( :, 1:nl-nb );
A1 = AL( :, nl-nb+1:nl );
A2 = AR;
else
A0 = AL;
A1 = AR( :, 1:nb );
A2 = AR( :, nb+1:nr );
end
%
return;
%
% End of FLA_Repart_1x2_to_1x3
%

View File

@@ -0,0 +1,45 @@
function [ A0,...
A1,...
A2 ] = FLA_Repart_2x1_to_3x1( AT,...
AB,...
mb, side )
%
% Purpose: Repartition a 2 x 1 partitioning of matrix A into
% a 3 x 1 partitioning where submatrix A1 with mb rows is split from
% the side indicated by side
%
[ mt, nt ] = size( AT );
[ mbt, nbt ] = size( AB );
[ mside, nside ] = size( side );
%
% Check input parameters
%
if( nt ~= nbt )
error('input matrices must have the same number of columns');
elseif( ( mside ~= 1 )|( ( nside ~= 7 )&( nside ~= 10 ) ) )
error('side must be a string with contents equal to FLA_TOP or FLA_BOTTOM');
elseif( ( nside == 7 )&( ~strcmp( side(1:7), 'FLA_TOP' ) ) )
error('side must be a string with contents equal to FLA_TOP or FLA_BOTTOM');
elseif( nside == 10 )
if ( ~strcmp( side(1:10), 'FLA_BOTTOM' ) )
error('side must be a string with contents equal to FLA_TOP or FLA_BOTTOM');
end
end
%
% Repartitioning...
%
if( strcmp( side(1:7), 'FLA_TOP' ) )
A0 = AT( 1:mt-mb, : );
A1 = AT( mt-mb+1:mt, : );
A2 = AB;
else
A0 = AT;
A1 = AB( 1:mb, : );
A2 = AB( mb+1:mbt, : );
end
%
return;
%
% End of FLA_Repart_2x1_to_3x1
%

View File

@@ -0,0 +1,86 @@
function [ A00, A01, A02,...
A10, A11, A12,...
A20, A21, A22 ] = FLA_Repart_2x2_to_3x3( ATL, ATR,...
ABL, ABR,...
bm, bn, quadrant )
%
% function [ A00, A01, A02,...
% A10, A11, A12,...
% A20, A21, A22 ] = FLA_Repart_2x2_to_3x3( ATL, ATR,...
% ABL, ABR,...
% bm, bn, quadrant )
%
% Purpose: Repartition a 2 x 2 partitioning of matrix A into
% a 3 x 3 partitioning where mb x nb submatrix A11 is split from
% the quadrant indicated by quadrant
%
[ mtl, ntl ] = size( ATL );
[ mtr, ntr ] = size( ATR );
[ mbl, nbl ] = size( ABL );
[ mbr, nbr ] = size( ABR );
%
% Check input parameters
%
if( mtl ~= mtr )
error('input matrices in top row must have the same number of rows');
elseif( mbl ~= mbr )
error('input matrices in bottom row must have the same number of rows');
elseif( ntl ~= nbl )
error('input matrices in left column must have the same number of columns');
elseif( ntr ~= nbr )
error('input matrices in right column must have the same number of columns');
elseif( ( ~strcmp( quadrant(1:6), 'FLA_TL' ) )&...
( ~strcmp( quadrant(1:6), 'FLA_TR' ) )&...
( ~strcmp( quadrant(1:6), 'FLA_BL' ) )&...
( ~strcmp( quadrant(1:6), 'FLA_BR' ) ) )
error('quadrant must be a string with contents equal to FLA_TL, FLA_TR, FLA_BL, or FLA_BR');
end
%
% Repartitioning...
%
if( strcmp( quadrant(1:6), 'FLA_TL' ) )
A00 = ATL( 1:mtl-bm, 1:ntl-bn );
A01 = ATL( 1:mtl-bm, ntl-bn+1:ntl );
A02 = ATR( 1:mtl-bm, : );
A10 = ATL( mtl-bm+1:mtl, 1:ntl-bn );
A11 = ATL( mtl-bm+1:mtl, ntl-bn+1:ntl );
A12 = ATR( mtl-bm+1:mtl, : );
A20 = ABL( :, 1:ntl-bn );
A21 = ABL( :, ntl-bn+1:ntl );
A22 = ABR;
elseif( strcmp( quadrant(1:6), 'FLA_TR' ) )
A00 = ATL( 1:mtr-bm, : );
A01 = ATR( 1:mtr-bm, 1:bn );
A02 = ATR( 1:mtr-bm, bn+1:ntr );
A10 = ATL( mtr-bm+1:mtr, : );
A11 = ATR( mtr-bm+1:mtr, 1:bn );
A12 = ATR( mtr-bm+1:mtr, bn+1:ntr );
A20 = ABL;
A21 = ABR( :, 1:bn );
A22 = ABR( :, bn+1:ntr );
elseif( strcmp( quadrant(1:6), 'FLA_BL' ) )
A00 = ATL( :, 1:nbl-bn );
A01 = ATL( :, nbl-bn+1:nbl );
A02 = ATR;
A10 = ABL( 1:bm, 1:nbl-bn );
A11 = ABL( 1:bm, nbl-bn+1:nbl );
A12 = ABR( 1:bm, : );
A20 = ABL( bm+1:mbl, 1:nbl-bn );
A21 = ABL( bm+1:mbl, nbl-bn+1:nbl );
A22 = ABR( bm+1:mbl, : );
else
A00 = ATL;
A01 = ATR( :, 1:bn );
A02 = ATR( :, bn+1:nbr );
A10 = ABL( 1:bm, : );
A11 = ABR( 1:bm, 1:bn );
A12 = ABR( 1:bm, bn+1:nbr );
A20 = ABL( bm+1:mbr, : );
A21 = ABR( bm+1:mbr, 1:bn );
A22 = ABR( bm+1:mbr, bn+1:nbr );
end
%
return;
%
% End of FLA_Repart_2x2_to_3x3
%

View File

@@ -0,0 +1,117 @@
function [ y_out ] = laff_axpy( alpha, x, y )
% y_out = laff_axpy( alpha, x, y ) computes y_out = alpha * x + y.
% Vectors x and y can be a mixture of column and/or row vector. In other
% words, x and y can be n x 1 or 1 x n arrays. However, one size must
% equal 1 and the other size equal n.
%
% The reason y is an input parameter is that the input y indicates
% whether the output, y_out, is a column or row vector.
% For convenience, we treat each vector as a m x n matrix where either m
% or n equals 1, making it a row or column vector.
% Extract the row and column sizes of alpha, x, and y
[ m_alpha, n_alpha ] = size( alpha );
[ m_x, n_x ] = size( x );
[ m_y, n_y ] = size( y );
% check if alpha is a scalar (is 1 x 1 )
if ( m_alpha ~= 1 | n_alpha ~= 1 ) % alpha is not 1 x 1
disp( 'error in laff_scal: alpha is not a scalar' )
return
end
if ( n_x == 1 ) % x is a column vector
if ( n_y == 1 ) % y is a column vector
if ( m_x == m_y ) % the sizes of the vectors match
% Scale the elements of x and add to corresponding element of y
for i=1:m_x
y( i,1 ) = alpha * x( i,1 ) + y( i,1 );
end
else % The sizes of the vectors don't match
y_out = 'failed';
return
end
elseif ( m_y == 1 ) % y is a row vector
if ( m_x == n_y ) % the sizes of the vectors match
% Scale the elements of x and add to corresponding element of y
for i=1:m_x
y( 1,i ) = alpha * x( i,1 ) + y( 1,i );
end
else % The sizes of the vectors don't match
y_out = 'failed';
return
end
else % One of the sizes of y must equal 1. Display error and return.
y_out = 'failed';
return
end
elseif ( m_x == 1 ) % x is a row vector
if ( n_y == 1 ) % y is a column vector
if ( n_x == m_y ) % the sizes of the vectors match
% Scale the elements of x and add to corresponding element of y
for i=1:n_x
y( i,1 ) = alpha * x( 1,i ) + y( i,1 );
end
else % The sizes of the vectors don't match
y_out = 'failed';
return
end
elseif ( m_y == 1 ) % y is a row vector
if ( n_x == n_y ) % the sizes of the vectors match
% Scale the elements of x and add to corresponding element of y
for i=1:n_x
y( 1,i ) = alpha * x( 1,i ) + y( 1,i );
end
else % The sizes of the vectors don't match
y_out = 'failed';
return
end
else % One of the sizes of y must equal 1. Display error and return.
y_out = 'failed';
return
end
else % One of the sizes of x must equal 1. Display error and return.
y_out = 'failed';
return
end
y_out = y;
return
end

View File

@@ -0,0 +1,57 @@
function [ y_out ] = laff_copy( x, y )
% y = copy( x, y ) copies vector x into vector y
% Vectors x and y can be a mixture of column and/or row vector. In other
% words, x and y can be n x 1 or 1 x n arrays. However, one size must
% equal 1 and the other size equal n.
%
% The reason y is an input parameter is that the input y indicates
% whether the output, y_out, is a column or row vector, and then also
% indicates whether x must be transposed (e.g., if x is a row vector and
% y is a column vector).
% Extract the row and column sizes of x and y
[ m_x, n_x ] = size( x );
[ m_y, n_y ] = size( y );
% Make sure x and y are (row or column) vectors of equal length
if ( m_x ~= 1 & n_x ~= 1 ) | ( m_y ~= 1 & n_y ~= 1 )
y_out = 'FAILED';
return
end
if ( m_x * n_x ~= m_y * n_y )
y_out = 'FAILED';
return
end
if ( n_x == 1 ) % x is a column vector
if ( n_y == 1 ) % y is a column vector
% Copy the elements of x into the elements of y
for i=1:m_x
y( i,1 ) = x( i,1 );
end
else % y is a row vector
% Copy the elements of x into the elements of y
for i=1:m_x
y( 1,i ) = x( i,1 );
end
end
else % x is a row vector
if ( n_y == 1 ) % y is a column vector
% Copy the elements of x into the elements of y
for i=1:n_x
y( i,1 ) = x( 1,i );
end
else % y is a row vector
% Copy the elements of x into the elements of y
for i=1:n_x
y( 1,i ) = x( 1,i );
end
end
end
% Return the updated y in y_out
y_out = y;
return
end

View File

@@ -0,0 +1,52 @@
function [ alpha ] = laff_dot( x, y )
% y = laff_dot( x, y ) computes the dot product of vectors x and y
% Vectors x and y can be a mixture of column and/or row vector. In other
% words, x and y can be n x 1 or 1 x n arrays. However, one size must
% equal 1 and the other size equal n.
% Extract the row and column sizes of x and y
[ m_x, n_x ] = size( x );
[ m_y, n_y ] = size( y );
% Make sure x and y are (row or column) vectors of equal length
if ( m_x ~= 1 & n_x ~= 1 ) | ( m_y ~= 1 & n_y ~= 1 )
alpha = 'FAILED';
return
end
if ( m_x * n_x ~= m_y * n_y )
alpha = 'FAILED';
return
end
alpha = 0;
if ( n_x == 1 ) % x is a column vector
if ( n_y == 1 ) % y is a column vector
% Copy the elements of x into the elements of y
for i=1:m_x
alpha = alpha + x( i,1 ) * y( i,1 );
end
else % y is a row vector
% Copy the elements of x into the elements of y
for i=1:m_x
alpha = alpha + x( i,1 ) * y( 1,i );
end
end
else % x is a row vector
if ( n_y == 1 ) % y is a column vector
% Copy the elements of x into the elements of y
for i=1:n_x
alpha = alpha + x( 1,i ) * y( i,1 );
end
else % y is a row vector
% Copy the elements of x into the elements of y
for i=1:n_x
alpha = alpha + x( 1,i ) * y( 1,i );
end
end
end
return
end

View File

@@ -0,0 +1,51 @@
function [ alpha_out ] = laff_dots( x, y, alpha )
% y = laff_dot( x, y ) computes the dot product of vectors x and y
% Vectors x and y can be a mixture of column and/or row vector. In other
% words, x and y can be n x 1 or 1 x n arrays. However, one size must
% equal 1 and the other size equal n.
% Extract the row and column sizes of x and y
[ m_x, n_x ] = size( x );
[ m_y, n_y ] = size( y );
% Make sure x and y are (row or column) vectors of equal length
if ( m_x ~= 1 & n_x ~= 1 ) | ( m_y ~= 1 & n_y ~= 1 )
alpha = 'FAILED';
return
end
if ( m_x * n_x ~= m_y * n_y )
alpha = 'FAILED';
return
end
if ( n_x == 1 ) % x is a column vector
if ( n_y == 1 ) % y is a column vector
% Copy the elements of x into the elements of y
for i=1:m_x
alpha = alpha + x( i,1 ) * y( i,1 );
end
else % y is a row vector
% Copy the elements of x into the elements of y
for i=1:m_x
alpha = alpha + x( i,1 ) * y( 1,i );
end
end
else % x is a row vector
if ( n_y == 1 ) % y is a column vector
% Copy the elements of x into the elements of y
for i=1:n_x
alpha = alpha + x( 1,i ) * y( i,1 );
end
else % y is a row vector
% Copy the elements of x into the elements of y
for i=1:n_x
alpha = alpha + x( 1,i ) * y( 1,i );
end
end
end
alpha_out = alpha;
return
end

View File

@@ -0,0 +1,52 @@
function [ x_out ] = laff_invscal( alpha, x )
% x_out = laff_invscal( alpha, x ) scales vector x by 1/alpha
% Vector x a column or row vector. In other words, x can be
% a n x 1 or 1 x n array. However, one size must equal 1 and the
% other size equal n.
% For convenience, we treat x as a m x n matrix where either m or n
% equals 1, making it a row or column vector.
% Extract the row and column sizes of alpha and x
[ m_alpha, n_alpha ] = size( alpha );
[ m_x, n_x ] = size( x );
if ( m_x ~= 1 & n_x ~= 1 )
x_out = 'FAILED';
return
end
% check if alpha is a scalar (is 1 x 1 )
if ( m_alpha ~= 1 | n_alpha ~= 1 ) % alpha is not 1 x 1
x_out = 'FAILED';
return
end
inv_alpha = 1/alpha;
if ( n_x == 1 ) % x is a column vector
% Scale the elements of x
for i=1:m_x
x( i,1 ) = inv_alpha * x( i,1 );
end
elseif ( m_x == 1 ) % x is a row vector
% Scale the elements of x_out
for i=1:n_x
x( 1,i ) = inv_alpha * x( 1,i );
end
else % x is neither a row nor a column vector
x_out = 'FAILED';
return
end
x_out = x;
return
end

View File

@@ -0,0 +1,50 @@
function [ x_out ] = laff_scal( alpha, x )
% x_out = laff_scal( alpha, x ) scales vector x by alpha
% Vector x a column or row vector. In other words, x can be
% a n x 1 or 1 x n array. However, one size must equal 1 and the
% other size equal n.
% For convenience, we treat x as a m x n matrix where either m or n
% equals 1, making it a row or column vector.
% Extract the row and column sizes of alpha and x
[ m_alpha, n_alpha ] = size( alpha );
[ m_x, n_x ] = size( x );
if ( m_x ~= 1 & n_x ~= 1 )
x_out = 'FAILED';
return
end
% check if alpha is a scalar (is 1 x 1 )
if ( m_alpha ~= 1 | n_alpha ~= 1 ) % alpha is not 1 x 1
x_out = 'FAILED';
return
end
if ( n_x == 1 ) % x is a column vector
% Scale the elements of x
for i=1:m_x
x( i,1 ) = alpha * x( i,1 );
end
elseif ( m_x == 1 ) % x is a row vector
% Scale the elements of x_out
for i=1:n_x
x( 1,i ) = alpha * x( 1,i );
end
else % x is neither a row nor a column vector
x_out = 'FAILED';
return
end
x_out = x;
return
end

View File

@@ -0,0 +1,36 @@
function [ alpha ] = laff_norm2( x )
% alpha = laff_norm2( x ) computes the (Euclidean) length of vector x
% Vector x a column or row vector. In other words, x can be
% a n x 1 or 1 x n array. However, one size must equal 1 and the
% other size equal n.
% For convenience, we treat x as a m x n matrix where either m or n
% equals 1, making it a row or column vector.
% Extract the row and column sizes x
[ m_x, n_x ] = size( x );
% Make sure x is a (row or column) vector
if ( m_x ~= 1 & n_x ~= 1 )
alpha = 'FAILED';
return
end
xmax = 0.0
for i=1:m_x
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 = alpha * xmax;
return
end

View File

@@ -0,0 +1,20 @@
function [ x_out ] = laff_onev( x )
% x_out = laff_onev( x ) sets the elements of vector x to one
% Vector x a column or row vector. In other words, x can be
% a n x 1 or 1 x n array. However, one size must equal 1 and the
% other size equal n.
if ~isvector( x )
x_out = 'FAILED';
return
end
% Now we cheat a little, and use the MATLAB intrinsic function zeros().
x_out = ones( size( x ) );
return
end

View File

@@ -0,0 +1,50 @@
function [ x_out ] = laff_scal( alpha, x )
% x_out = laff_scal( alpha, x ) scales vector x by alpha
% Vector x a column or row vector. In other words, x can be
% a n x 1 or 1 x n array. However, one size must equal 1 and the
% other size equal n.
% For convenience, we treat x as a m x n matrix where either m or n
% equals 1, making it a row or column vector.
% Extract the row and column sizes of alpha and x
[ m_alpha, n_alpha ] = size( alpha );
[ m_x, n_x ] = size( x );
if ( m_x ~= 1 & n_x ~= 1 )
x_out = 'FAILED';
return
end
% check if alpha is a scalar (is 1 x 1 )
if ( m_alpha ~= 1 | n_alpha ~= 1 ) % alpha is not 1 x 1
x_out = 'FAILED';
return
end
if ( n_x == 1 ) % x is a column vector
% Scale the elements of x
for i=1:m_x
x( i,1 ) = alpha * x( i,1 );
end
elseif ( m_x == 1 ) % x is a row vector
% Scale the elements of x_out
for i=1:n_x
x( 1,i ) = alpha * x( 1,i );
end
else % x is neither a row nor a column vector
x_out = 'FAILED';
return
end
x_out = x;
return
end

View File

@@ -0,0 +1,20 @@
function [ x_out ] = laff_zerov( x )
% x_out = laff_zerov( x ) sets the elements of vector x to zero
% Vector x a column or row vector. In other words, x can be
% a n x 1 or 1 x n array. However, one size must equal 1 and the
% other size equal n.
if ~isvector( x )
x_out = 'FAILED';
return
end
% Now we cheat a little, and use the MATLAB intrinsic function zeros().
x_out = zeros( size( x ) );
return
end

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

View File

@@ -0,0 +1,95 @@
% Create some vectors
x = [
1
2
3
]
y = [
0
-1
-2
]
z = [
4
3
2
1
]
% test dot product of column vector with column vector
disp( 'dot product of column vector with column vector' )
if ( isequal( laff_dot( x, y ), x' * y ) )
disp( 'TEST PASSED' )
else
disp( 'TEST FAILED' )
end
disp( ' ' )
% test dot product of column vector with row vector
disp( 'dot product of column vector with row vector' )
if ( isequal( laff_dot( x, y' ), x' * y ) )
disp( 'TEST PASSED' )
else
disp( 'TEST FAILED' )
end
disp( ' ' )
% test dot product of row vector with column vector
disp( 'dot product of row vector with column vector' )
if ( isequal( laff_dot( x', y ), x' * y ) )
disp( 'TEST PASSED' )
else
disp( 'TEST FAILED' )
end
disp( ' ' )
% test dot product of row vector with row vector
disp( 'dot product of row vector with row vector' )
if ( isequal( laff_dot( x', y' ), x' * y ) )
disp( 'TEST PASSED' )
else
disp( 'TEST FAILED' )
end
disp( ' ' )
% test dot product of column vector with column vector (wrong size)
disp( 'dot product of column vector with column vector (wrong size)' )
if ( isequal( laff_dot( x, z ), 'FAILED' ) )
disp( 'TEST PASSED' )
else
disp( 'TEST FAILED' )
end
disp( ' ' )
% test dot product of column vector with row vector (wrong size)
disp( 'dot product of column vector with row vector (wrong size)' )
if ( isequal( laff_dot( x, z' ), 'FAILED' ) )
disp( 'TEST PASSED' )
else
disp( 'TEST FAILED' )
end
disp( ' ' )
% test dot product of row vector with column vector (wrong size)
disp( 'dot product of row vector with column vector (wrong size)' )
if ( isequal( laff_dot( x', z ), 'FAILED' ) )
disp( 'TEST PASSED' )
else
disp( 'TEST FAILED' )
end
disp( ' ' )
% test dot product of row vector with row vector (wrong size)
disp( 'dot product of row vector with row vector (wrong size)' )
if ( isequal( laff_dot( x', z' ), 'FAILED' ) )
disp( 'TEST PASSED' )
else
disp( 'TEST FAILED' )
end

View 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

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