Skip to main content
Library homepage
 

Text Color

Text Size

 

Margin Size

 

Font Type

Enable Dyslexic Font
Mathematics LibreTexts

25.2: Code Review

( \newcommand{\kernel}{\mathrm{null}\,}\)

In the next in-class assignment, we are going to avoid some of the more advanced libraries (i.e. no numpy or scipy or sympy) to try to get a better understanding about what is going on in the math. The following code implements some common linear algebra functions:

Login with LibreOne to run this code cell interactively.

If you have already signed in, please refresh the page.

#Standard Python Libraries only
import math
import copy
#Standard Python Libraries only
import math
import copy

Login with LibreOne to run this code cell interactively.

If you have already signed in, please refresh the page.

def dot(u,v):
    '''Calculate the dot product between vectors u and v'''
    temp = 0;
    for i in range(len(u)):
        temp += u[i]*v[i]
    return temp
def dot(u,v):
    '''Calculate the dot product between vectors u and v'''
    temp = 0;
    for i in range(len(u)):
        temp += u[i]*v[i]
    return temp
Do This

Write a quick test to compare the output of the above dot function with the numpy dot function.

Login with LibreOne to run this code cell interactively.

If you have already signed in, please refresh the page.

# Put your test code here
# Put your test code here

Login with LibreOne to run this code cell interactively.

If you have already signed in, please refresh the page.

def multiply(m1,m2):
    '''Calculate the matrix multiplication between m1 and m2 represented as list-of-list.'''
    n = len(m1)
    d = len(m2)
    m = len(m2[0])
    
    if len(m1[0]) != d:
        print("ERROR - inner dimentions not equal")
    
    #make zero matrix
    result = [[0 for j in range(m)] for i in range(n)]
#    print(result)
    for i in range(0,n):
        for j in range(0,m):
            for k in range(0,d):
                #print(i,j,k)
                #print('result', result[i][j])
                #print('m1', m1[i][k])
                #print('m2', m2[k][j])
                result[i][j] = result[i][j] + m1[i][k] * m2[k][j]
    return result
def multiply(m1,m2):
    '''Calculate the matrix multiplication between m1 and m2 represented as list-of-list.'''
    n = len(m1)
    d = len(m2)
    m = len(m2[0])
    
    if len(m1[0]) != d:
        print("ERROR - inner dimentions not equal")
    
    #make zero matrix
    result = [[0 for j in range(m)] for i in range(n)]
#    print(result)
    for i in range(0,n):
        for j in range(0,m):
            for k in range(0,d):
                #print(i,j,k)
                #print('result', result[i][j])
                #print('m1', m1[i][k])
                #print('m2', m2[k][j])
                result[i][j] = result[i][j] + m1[i][k] * m2[k][j]
    return result
Do This

Write a quick test to compare the output of the above multiply function with the numpy multiply function.

Login with LibreOne to run this code cell interactively.

If you have already signed in, please refresh the page.

# Put your test code here
# Put your test code here
Question

What is the big-O complexity of the above multiply function?

Question

Line 11 in the provided multiply code initializes a matrix of the size of the output matrix as a list of lists with zeros. What is the big-O complexity of line 11?

Login with LibreOne to run this code cell interactively.

If you have already signed in, please refresh the page.

def norm(u):
    '''Calculate the norm of vector u'''
    nm = 0
    for i in range(len(u)):
        nm += u[i]*u[i]
    return math.sqrt(nm)
def norm(u):
    '''Calculate the norm of vector u'''
    nm = 0
    for i in range(len(u)):
        nm += u[i]*u[i]
    return math.sqrt(nm)
Do This

Write a quick test to compare the outputs of the above norm function with the numpy norm function.

Login with LibreOne to run this code cell interactively.

If you have already signed in, please refresh the page.

#Put your test code here
#Put your test code here

Login with LibreOne to run this code cell interactively.

If you have already signed in, please refresh the page.

def transpose(A):
    '''Calculate the transpose of matrix A represented as list of lists'''
    n = len(A)
    m = len(A[0])
    AT = list()
    for j in range(0,m):    
        temp = list()
        for i in range(0,n):
            temp.append(A[i][j])
        AT.append(temp)
    return AT
def transpose(A):
    '''Calculate the transpose of matrix A represented as list of lists'''
    n = len(A)
    m = len(A[0])
    AT = list()
    for j in range(0,m):    
        temp = list()
        for i in range(0,n):
            temp.append(A[i][j])
        AT.append(temp)
    return AT
Do This

Write a quick test to compare the output of the above transpose function with the numpy transpose function.

Login with LibreOne to run this code cell interactively.

If you have already signed in, please refresh the page.

# Put your test code here
# Put your test code here
Question

What is the big-O complexity of the above transpose function?

Question

Explain any differences in results between the provided functions and their numpy counterparts.


This page titled 25.2: Code Review is shared under a CC BY-NC 4.0 license and was authored, remixed, and/or curated by Dirk Colbry via source content that was edited to the style and standards of the LibreTexts platform.

Support Center

How can we help?