This notebook walks you through how to implement $ y := A^T x + y $ without explicitly transposing the matrix.
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" and "import flame" statements.
Above, you find two algorithms. The one on the left computes $ y := A x + y $ via dot products and the one on the right computes $ y := A^T x + y $ via dot products. You are to implement the one on the right.
Mvmult_t_unb_var1( A, x, y )
This routine, given $ A \in \mathbb{R}^{m \times m} $, $ x \in \mathbb{R}^m $, and $ y \in \mathbb{R}^m $, computes $ y := A^T x + y $. The "t" in the name of the routine indicates this is the "transpose" matrix-vector multiplication.
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 3 x 4 matrix and related vectors, performing the computation.
from numpy import random
from numpy import matrix
A = matrix( random.rand( 3,4 ) )
x = matrix( random.rand( 3,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 )
A before = [[ 0.45946038 0.36711289 0.62663084 0.69366725] [ 0.61839962 0.32329523 0.54882629 0.52064797] [ 0.92298778 0.09047515 0.40856054 0.23082752]] x before = [[ 0.47016 ] [ 0.77363531] [ 0.30106805]] y before = [[ 0.86834438] [ 0.30196564] [ 0.06375715] [ 0.70927731]]
from numpy import transpose
laff.copy( y, yold ) # save the original vector y
Mvmult_t_unb_var1( A, x, y )
print( 'y after =' )
print( y )
print( 'y - ( transpose( A ) * x + yold ) = ' )
print( y - ( transpose( A ) * x + yold ) )
y after = [[ 1.84066219] [ 0.75191922] [ 0.90596983] [ 1.50769835]] y - ( transpose( A ) * x + yold ) = [[ 0.00000000e+00] [ -1.11022302e-16] [ 0.00000000e+00] [ 0.00000000e+00]]
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.
axpy
s)¶Above, you find two algorithms. The one on the left computes $ y := A x + y $ via axpy
s and the one on the right computes $ y := A^T x + y $ via axpy
s. You are to implement the one on the right.
Mvmult_t_unb_var2( A, x, y )
This routine, given $ A \in \mathbb{R}^{m \times m} $, $ x \in \mathbb{R}^m $, and $ y \in \mathbb{R}^m $, computes $ y := A^T x + y $. The "t" in the name of the routine indicates this is the "transpose" matrix-vector multiplication.
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 3 x 4 matrix and related vectors, performing the computation.
from numpy import random
from numpy import matrix
A = matrix( random.rand( 3,4 ) )
x = matrix( random.rand( 3,1 ) )
y = matrix( random.rand( 4,1 ) )
yold = matrix( random.rand( 4,1 ) )
print( 'A before =' )
print( y )
print( 'x before =' )
print( x )
print( 'y before =' )
print( y )
A before = [[ 0.59304972] [ 0.18665298] [ 0.09586729] [ 0.71568854]] x before = [[ 0.2797929 ] [ 0.70916909] [ 0.73616771]] y before = [[ 0.59304972] [ 0.18665298] [ 0.09586729] [ 0.71568854]]
from numpy import transpose
laff.copy( y, yold ) # save the original vector y
Mvmult_t_unb_var2( A, x, y )
print( 'y after =' )
print( y )
print( 'y - ( transpose( A ) * x + yold ) = ' )
print( y - ( transpose( A ) * x + yold ) )
y after = [[ 1.03703548] [ 1.19297584] [ 0.95849002] [ 1.58112475]] y - ( transpose( A ) * x + yold ) = [[ 0.] [ 0.] [ 0.] [ 0.]]
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.