This notebook walks you through how to implement simple functions that make a square matrix symmetric by copying the lower triangular part in its upper triangular part (after transposing).
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.
Write your
Symmetrize_from_lower_triangle_unb_var1( A )
routine, using the Spark webpage .
import flame
import laff as laff
def Symmetrize_from_lower_triangle_unb_var1(A):
ATL, ATR, \
ABL, ABR = flame.part_2x2(A, \
0, 0, 'TL')
while ATL.shape[0] < A.shape[0]:
A00, a01, A02, \
a10t, alpha11, a12t, \
A20, a21, A22 = flame.repart_2x2_to_3x3(ATL, ATR, \
ABL, ABR, \
1, 1, 'BR')
#------------------------------------------------------------#
laff.copy( a10t, a01 )
#------------------------------------------------------------#
ATL, ATR, \
ABL, ABR = flame.cont_with_3x3_to_2x2(A00, a01, A02, \
a10t, alpha11, a12t, \
A20, a21, A22, \
'TL')
flame.merge_2x2(ATL, ATR, \
ABL, ABR, A)
Let's quickly test the routine by creating a 5 x 5 matrix and then executing the routine.
from numpy import random
from numpy import matrix
A = matrix( random.rand( 5,5 ) )
print( 'A before =' )
print( A )
Symmetrize_from_lower_triangle_unb_var1( A )
print( 'A after =' )
print( A )
Bingo, it seems to work!
Now, an alternative routine that sets the upper triangular part by rows.
Use the Spark webpage to generate the routine
Symmetrize_from_lower_triangle_unb_var2( A )
.
import flame
import laff as laff
def Symmetrize_from_lower_triangle_unb_var2(A):
ATL, ATR, \
ABL, ABR = flame.part_2x2(A, \
0, 0, 'TL')
while ATL.shape[0] < A.shape[0]:
A00, a01, A02, \
a10t, alpha11, a12t, \
A20, a21, A22 = flame.repart_2x2_to_3x3(ATL, ATR, \
ABL, ABR, \
1, 1, 'BR')
#------------------------------------------------------------#
laff.copy( a21, a12t )
#------------------------------------------------------------#
ATL, ATR, \
ABL, ABR = flame.cont_with_3x3_to_2x2(A00, a01, A02, \
a10t, alpha11, a12t, \
A20, a21, A22, \
'TL')
flame.merge_2x2(ATL, ATR, \
ABL, ABR, A)
Test your routine with the following
from numpy import random
from numpy import matrix
A = matrix( random.rand( 5,5 ) )
print( 'A before =' )
print( A )
Symmetrize_from_lower_triangle_unb_var2( A )
print( 'A after =' )
print( A )
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.