I need to implement these updates into the algorithm
So I write this code below:
format long
m = 5;
A = rand( m, m );
t = rand( m, 1 );
r = rand( m, 1 );
[ B, t, r ] = BiRed( A, t, r );
Bi = BiFromB( B );
disp('Singular values of B')
svd( Bi )
disp('Singular values of A')
svd( A )
function [ A_out, t_out, r_out ] = BiRed( A, t, r )
[ ATL, ATR, ...
ABL, ABR ] = FLA_Part_2x2( A, ...
0, 0, 'FLA_TL' );
[ tT, ...
tB ] = FLA_Part_2x1( t, ...
0, 'FLA_TOP' );
[ rT, ...
rB ] = FLA_Part_2x1( r, ...
0, 'FLA_TOP' );
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' );
[ t0, ...
tau1, ...
t2 ] = FLA_Repart_2x1_to_3x1( tT, ...
tB, ...
1, 'FLA_BOTTOM' );
[ r0, ...
rho1, ...
r2 ] = FLA_Repart_2x1_to_3x1( rT, ...
rB, ...
1, 'FLA_BOTTOM' );
%-----implement the updates here-----%
[u21, tau] = Housev1(a21);
n = size(A22, 1);
u = [1; u21];
H = eye(n+1) - tau * (u * u');
updated_block = H * [a12t; A22];
a12t = updated_block(1, :);
A22 = updated_block(2:end, :);
[u12, rho] = Housev1(a12t');
H_right = eye(size(A22, 2)) - rho * (u12 * u12');
A22 = A22 * H_right;
%------------------------------------%
[ ATL, ATR, ...
ABL, ABR ] = FLA_Cont_with_3x3_to_2x2( A00, a01, A02, ...
a10t, alpha11, a12t, ...
A20, a21, A22, ...
'FLA_TL' );
[ tT, ...
tB ] = FLA_Cont_with_3x1_to_2x1( t0, ...
tau1, ...
t2, ...
'FLA_TOP' );
[ rT, ...
rB ] = FLA_Cont_with_3x1_to_2x1( r0, ...
rho1, ...
r2, ...
'FLA_TOP' );
end
A_out = [ ATL, ATR
ABL, ABR ];
t_out = [ tT
tB ];
r_out = [ rT
rB ];
end
function [ B ] = BiFromB( B )
B = tril( triu( B ), 1 );
end
function [ rho, ...
u2, tau ] = Housev( chi1, ...
x2 )
normx = sqrt( chi1 * chi1 + x2' * x2 );
rho = - sign( chi1 ) * normx;
nu1 = chi1 - rho;
u2 = x2 / nu1;
tau = ( 1 + u2' * u2 ) / 2;
end
function [ u, tau ] = Housev1( x )
n = size( x, 1 );
if ( n == 0 )
error('Housev1:InputError', 'Error calling Housev1: input vector x is empty.');
end
[ rho, ...
u2, tau ] = Housev( x(1,1), ...
x(2:n, 1) );
u = [ rho
u2 ];
end
When I run it, it shows error input vector x is empty
.
I try to add some debug code:
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');
disp(['Sizes - A00: ', mat2str(size(A00)), ', A22: ', mat2str(size(A22))]);
if isempty(a21)
disp('a21 is empty, ending loop.');
break; % Break the loop if no more updates can be done.
end
[u21, tau] = Housev1(a21);
n = size([a12t; A22], 1);
H = eye(n) - tau * [1; u21] * [1; u21]';
updated_block = H * [a12t; A22];
a12t = updated_block(1, :);
A22 = updated_block(2:end, :);
if isempty(a12t)
disp('a12t is empty, ending loop.');
break; % Break the loop if no more updates can be done.
end
[u12, rho] = Housev1(a12t');
H_right = eye(size(A22, 2)) - rho * (u12 * u12');
A22 = A22 * H_right;
[ATL, ATR, ...
ABL, ABR] = FLA_Cont_with_3x3_to_2x2(A00, a01, A02, ...
a10t, alpha11, a12t, ...
A20, a21, A22, ...
'FLA_TL');
disp(['Size of ATL after update: ', mat2str(size(ATL))]);
if size(ATL, 1) == size(A, 1)
break; % Explicitly break if ATL has grown to full size.
end
end
,
this is the output:
Sizes - A00: [0 0], A22: [4 4]
Size of ATL after update: [1 1]
Sizes - A00: [1 1], A22: [3 3]
Size of ATL after update: [2 2]
Sizes - A00: [2 2], A22: [2 2]
Size of ATL after update: [3 3]
Sizes - A00: [3 3], A22: [1 1]
Size of ATL after update: [4 4]
Sizes - A00: [4 4], A22: [0 0]
a21 is empty, ending loop.
Singular values of B
ans =
1.106627568873231
0.857960978170602
0.413228870344143
0.301257867048619
0.013521687400442
Singular values of A
ans =
2.301262463484019
0.820665505888531
0.455426251555126
0.287366178798951
0.046120083989295
I don’t understand, why the input is empty? How to implement it correctly?
notes:
- If you want to see the detail description, it is on https://www.cs.utexas.edu/users/flame/laff/alaff/chapter11-reduction-to-bidirectional-form.html or you can google “ALAFF Reduction to bidiagonal form” and pick the top result.
- If you want to see the skeleton code, initial code, and flame library, it is on https://github.com/ULAFF/ALAFF/blob/master/Assignments/Week11/matlab or you can google “assignments/week11/matlab/ github” and pick the top result then choose the “week 11” folder.