Skip to main content
Mathematics LibreTexts

25.2: Code Review

  • Page ID
    69407
  • \( \newcommand{\vecs}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} } \) \( \newcommand{\vecd}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash {#1}}} \)\(\newcommand{\id}{\mathrm{id}}\) \( \newcommand{\Span}{\mathrm{span}}\) \( \newcommand{\kernel}{\mathrm{null}\,}\) \( \newcommand{\range}{\mathrm{range}\,}\) \( \newcommand{\RealPart}{\mathrm{Re}}\) \( \newcommand{\ImaginaryPart}{\mathrm{Im}}\) \( \newcommand{\Argument}{\mathrm{Arg}}\) \( \newcommand{\norm}[1]{\| #1 \|}\) \( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\) \( \newcommand{\Span}{\mathrm{span}}\) \(\newcommand{\id}{\mathrm{id}}\) \( \newcommand{\Span}{\mathrm{span}}\) \( \newcommand{\kernel}{\mathrm{null}\,}\) \( \newcommand{\range}{\mathrm{range}\,}\) \( \newcommand{\RealPart}{\mathrm{Re}}\) \( \newcommand{\ImaginaryPart}{\mathrm{Im}}\) \( \newcommand{\Argument}{\mathrm{Arg}}\) \( \newcommand{\norm}[1]{\| #1 \|}\) \( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\) \( \newcommand{\Span}{\mathrm{span}}\)\(\newcommand{\AA}{\unicode[.8,0]{x212B}}\)

    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:

    #Standard Python Libraries only
    import math
    import copy
    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.

    # Put your test code here
    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.

    # 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?

    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.

    #Put your test code here
    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.

    # 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; a detailed edit history is available upon request.

    • Was this article helpful?