Skip to main content
Mathematics LibreTexts

44.3: Review of Python Numpy Package

  • Page ID
    70568
  • \( \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}}\)

    from urllib.request import urlretrieve
    urlretrieve('https://raw.githubusercontent.com/colbrydi/jupytercheck/master/answercheck.py', 
                'answercheck.py');
    Do This

    Watch the following video about the python Numpy package.

    Direct Link to the Youtube video.

    from IPython.display import YouTubeVideo
    YouTubeVideo("_hbWtNgstlI",width=640,height=320, cc_load_policy=True)

    The Python Numpy library has a matrix object which can be initialized as follows:

    import numpy as np
    A = np.matrix([[1,1], [20,25]])
    b = np.matrix([[30],[690]])
    print("A="+str(A))
    print("b="+str(b))
    A=[[ 1  1]
     [20 25]]
    b=[[ 30]
     [690]]
    

    Python can solve equations in the \(Ax=b\) format with the numpy.linalg library. For example:

    import numpy as np
    
    x = np.linalg.solve(A, b)
    print("x="+str(x))
    x=[[12.]
     [18.]]
    

    The numpy.linalg library is just a subset of the scipy.linalg library.

    import scipy.linalg as la
    
    x = la.solve(A, b)
    print("X="+str(x))
    X=[[12.]
     [18.]]
    
    Do This

    Convert the following system of linear equations to numpy matrices and solve it sing a Python linear algebra solver (Store the solutions in a vector named x).

    \(18x + 21y = 22672x - 3y = 644\)

    ##Put your answer to the above question here.
    from answercheck import checkanswer
    
    checkanswer.vector(x,'756ca9fa3951fad0e623b2a8315d5fd7');

    This page titled 44.3: Review of Python Numpy Package 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?