This notebook walks you through how to implement $ x := L^T x $ where $ L $ is lower triangular. Vector $ y $ is not to be touched (and, indeed, not even passed into the routines). This is a little trickier than you might think, especially when you work with upper triangular matrix $ U $, later. You may want to do a few small problems by hand if you don't get the right answer. Also, PictureFLAME may help you see what is going on.
We will use some functions that are part of our laff library (of which this function will become a part) as well as some routines from the FLAME API (Application Programming Interface) that allows us to write code that closely resembles how we typeset algorithms using the FLAME notation. These functions are imported with the "import laff as laff" and "import flame" statements.
Trmv_lt_unb_var1( L, x )
This routine, given lower triangular $ L \in \mathbb{R}^{n \times n} $ and $ x \in \mathbb{R}^n $ computes $ x := L^T x $. The "lt" in the name of the routine indicates this is the "lower, transpose" matrix-vector multiplication.
The specific laff functions we will use are
laff.dots( x, y, alpha )
which computes $ \alpha := x^T y + \alpha $. laff.scal( alpha, x )
which computes $ x := \alpha x $. Use the Spark webpage to generate a code skeleton. (Make sure you adjust the name of the routine.)
import flame
import laff as laff
def Trmv_lt_unb_var1(L, x):
LTL, LTR, \
LBL, LBR = flame.part_2x2(L, \
0, 0, 'TL')
xT, \
xB = flame.part_2x1(x, \
0, 'TOP')
while LTL.shape[0] < L.shape[0]:
L00, l01, L02, \
l10t, lambda11, l12t, \
L20, l21, L22 = flame.repart_2x2_to_3x3(LTL, LTR, \
LBL, LBR, \
1, 1, 'BR')
x0, \
chi1, \
x2 = flame.repart_2x1_to_3x1(xT, \
xB, \
1, 'BOTTOM')
#------------------------------------------------------------#
laff.scal( lambda11, chi1 )
laff.dots( l21, x2, chi1 )
#------------------------------------------------------------#
LTL, LTR, \
LBL, LBR = flame.cont_with_3x3_to_2x2(L00, l01, L02, \
l10t, lambda11, l12t, \
L20, l21, L22, \
'TL')
xT, \
xB = flame.cont_with_3x1_to_2x1(x0, \
chi1, \
x2, \
'TOP')
flame.merge_2x1(xT, \
xB, x)
Let's quickly test the routine by creating a 4 x 4 matrix and related vectors, performing the computation.
from numpy import random
from numpy import matrix
L = matrix( random.rand( 4,4 ) )
x = matrix( random.rand( 4,1 ) )
xold = matrix( random.rand( 4,1 ) )
# Notice that L is not lower triangular. We will only use the lower triangular part.
print( 'L before =' )
print( L )
print( 'x before =' )
print( x )
import numpy as np
laff.copy( x, xold ) # save the original vector x
Trmv_lt_unb_var1( L, x )
print( 'x after =' )
print( x )
#np.tril makes the matrix lower triangular
print( 'x - ( np.transpose( np.tril( L ) ) * xold ) = ' )
print( x - ( np.transpose( np.tril( L ) ) * xold ) )
Bingo, it seems to work! (Notice that we are doing floating point computations, which means that due to rounding you may not get an exact "0".)
Copy and paste the code into PictureFLAME , a webpage where you can watch your routine in action. Just cut and paste into the box.
Disclaimer: we implemented a VERY simple interpreter. If you do something wrong, we cannot guarantee the results. But if you do it right, you are in for a treat.
If you want to reset the problem, just click in the box into which you pasted the code and hit "next" again.
Trmv_lt_unb_var2( L, x )
This routine, given lower triangular $ L \in \mathbb{R}^{n \times n} $ and $ x \in \mathbb{R}^n $ computes $ x := L^T x $. The "lt" in the name of the routine indicates this is the "lower triangular, transpose" matrix-vector multiplication.
The specific laff functions we will use are
laff.axpy( alpha, x, y )
which computes $ y := \alpha x + y $. laff.scal( alpha, x )
which computes $ x := \alpha x $. Use the Spark webpage to generate a code skeleton. (Make sure you adjust the name of the routine.)
import flame
import laff as laff
def Trmv_lt_unb_var2(L, x):
LTL, LTR, \
LBL, LBR = flame.part_2x2(L, \
0, 0, 'TL')
xT, \
xB = flame.part_2x1(x, \
0, 'TOP')
while LTL.shape[0] < L.shape[0]:
L00, l01, L02, \
l10t, lambda11, l12t, \
L20, l21, L22 = flame.repart_2x2_to_3x3(LTL, LTR, \
LBL, LBR, \
1, 1, 'BR')
x0, \
chi1, \
x2 = flame.repart_2x1_to_3x1(xT, \
xB, \
1, 'BOTTOM')
#------------------------------------------------------------#
laff.axpy( chi1, l10t, x0 )
laff.scal( lambda11, chi1 )
#------------------------------------------------------------#
LTL, LTR, \
LBL, LBR = flame.cont_with_3x3_to_2x2(L00, l01, L02, \
l10t, lambda11, l12t, \
L20, l21, L22, \
'TL')
xT, \
xB = flame.cont_with_3x1_to_2x1(x0, \
chi1, \
x2, \
'TOP')
flame.merge_2x1(xT, \
xB, x)
Let's quickly test the routine by creating a 4 x 4 matrix and related vectors, performing the computation.
from numpy import random
from numpy import matrix
L = matrix( random.rand( 4,4 ) )
x = matrix( random.rand( 4,1 ) )
xold = matrix( random.rand( 4,1 ) )
# L is not lower triangular. We will only use the lower triangular part.
print( 'L before =' )
print( L )
print( 'x before =' )
print( x )
laff.copy( x, xold ) # save the original vector y
Trmv_lt_unb_var2( L, x )
print( 'x after =' )
print( x )
#np.tril makes the matrix lower triangular
print( 'x - ( np.transpose( np.tril( L ) ) * xold ) = ' )
print( x - ( np.transpose( np.tril( L ) ) * xold ) )
Bingo, it seems to work! (Notice that we are doing floating point computations, which means that due to rounding you may not get an exact "0".)
Copy and paste the code into PictureFLAME , a webpage where you can watch your routine in action. Just cut and paste into the box.
Disclaimer: we implemented a VERY simple interpreter. If you do something wrong, we cannot guarantee the results. But if you do it right, you are in for a treat.
If you want to reset the problem, just click in the box into which you pasted the code and hit "next" again.