Skip to main content
Mathematics LibreTexts

42.3: Review of Python Numpy Package

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

    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 sp
    
    x = sp.linalg.solve(A, b)
    print("X="+str(x))
    X=[[12.]
     [18.]]
    

    The numpy.linalg library is just a subset of the scipy.linalg library. Oddly you can’t load the SciPy library the same way. Instead you can call it as follows:

    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 using a python linear algebra solver \(18x + 21y = 22672x - 3y = 644\)

    ##Put your answer to the above question here.

    This page titled 42.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?