This notebook walks you through how to implement $ y := A x + y $ where $ A $ is symmetric with only the upper triangular part stored.
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.
Symv_u_unb_var1( A, x, y )
This routine, given symmetric matrix $ A \in \mathbb{R}^{n \times n} $, $ x \in \mathbb{R}^n $, and $ y \in \mathbb{R}^n $, computes $ y := A x + y $. The "u" in the name of the routine indicates that $ A $ is stored in the upper triangular part of the matrix.
The specific laff functions we will use are
laff.dots( x, y, alpha )
which computes $ \alpha := x^T y + \alpha $. Use the Spark webpage to generate a code skeleton. (Make sure you adjust the name of the routine.)
#Insert code here...
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
A = matrix( random.rand( 4,4 ) )
x = matrix( random.rand( 4,1 ) )
y = matrix( random.rand( 4,1 ) )
yold = matrix( random.rand( 4,1 ) )
print( 'A before =' )
print( A )
print( 'x before =' )
print( x )
print( 'y before =' )
print( y )
import numpy as np
# Notice that A is not symmetric. We now "symmetrize it"
Asymm = np.triu( A ) + np.transpose( np.triu( A, 1 ) )
laff.copy( y, yold ) # save the original vector y
Symv_u_unb_var1( A, x, y )
print( 'y after =' )
print( y )
print( 'y - ( Asymm * x + yold ) = ' )
print( y - ( Asymm * x + yold ) )
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.
Symv_u_unb_var2( A, x, y )
This routine, given symmetric matrix $ A \in \mathbb{R}^{n \times n} $, $ x \in \mathbb{R}^n $, and $ y \in \mathbb{R}^n $, computes $ y := A x + y $. The "u" in the name of the routine indicates that $ A $ is stored in the upper triangular part of the matrix.
The specific laff functions we will use are
laff.axpy( alpha, x, y )
which computes $ y := \alpha x + y $. Use the Spark webpage to generate a code skeleton. (Make sure you adjust the name of the routine.)
#Insert code here...
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
A = matrix( random.rand( 4,4 ) )
x = matrix( random.rand( 4,1 ) )
y = matrix( random.rand( 4,1 ) )
yold = matrix( random.rand( 4,1 ) )
print( 'A before =' )
print( A )
print( 'x before =' )
print( x )
print( 'y before =' )
print( y )
import numpy as np
# Notice that A is not symmetric. We now "symmetrize it"
Asymm = np.triu( A ) + np.transpose( np.triu( A, 1 ) )
laff.copy( y, yold ) # save the original vector y
Symv_u_unb_var2( A, x, y )
print( 'y after =' )
print( y )
print( 'y - ( Asymm * x + yold ) = ' )
print( y - ( Asymm * x + yold ) )
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.