This notebook walks you through how to implement a simple function that scales a matrix $ A $ by a scalar $ \beta $, overwriting $ A $.
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.
The specific laff function we will use is laff.scal which, when given a row or column vector (stored as a 1 x n or n x 1 matrix) and a scalar scales that vector.
Code the routine with the Spark webpage .
# insert code here
Let's quickly test the routine by creating a 5 x 4 matrix and then scaling by a scalar.
from numpy import random
from numpy import matrix
beta = matrix( '2.' )
A = matrix( random.rand( 5,4 ) )
print( 'beta = ' )
print( beta )
print( 'A before =' )
print( A )
Scale_a_matrix_unb_var1( beta, A )
print( 'A after =' )
print( A )
Bingo, it seems to work!
Now, write an alternative algorithm that scales $ A $ one row at a time.
Use the Spark webpage to generate the function Scale_a_matrix_unb_var2( beta, A ) that computes $ A := \beta A $ one row at a time.
# insert code here
Test your routine with the following
from numpy import random
from numpy import matrix
beta = matrix( '2' );
A = matrix( random.rand( 5,4 ) )
print( 'A before =' )
print( A )
Scale_a_matrix_unb_var2( beta, 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.