Web-Books
in the Austria-Forum
Austria-Forum
Web-Books
Informatik
Programming for Computations – Python - A Gentle Introduction to Numerical Simulations with Python 3.6, Volume Second Edition
Page - 53 -
  • User
  • Version
    • full version
    • text only version
  • Language
    • Deutsch - German
    • English

Page - 53 - in Programming for Computations – Python - A Gentle Introduction to Numerical Simulations with Python 3.6, Volume Second Edition

Image of the Page - 53 -

Image of the Page - 53 - in Programming for Computations – Python - A Gentle Introduction to Numerical Simulations with Python 3.6, Volume Second Edition

Text of the Page - 53 -

2.3 NumericalPythonArrays 53 Note how zerosmust be called with double parentheses now. The accessing of individualmatrix elements should be according to intuition. With some experience from matrix-vector algebra, it is clear thaty is correctly computed here. Note that mostprogrammerswoulduse theNumPyfunctioneyehere, togeneratethe identity matrix directly. One would then callI = eye(3) and getI as a two dimensional arraywithoneson thediagonal. Ifyouareexperiencedwithmatricesandvectors in Matlab, there is anotherway to handle matrices and vectors with NumPy, which will appear more like you are usedto.Forexample,amatrix-vectorproductis thencodedasA*xandnotbyuseof thedot function.Toachieve this,we mustuseobjectsofanother type, i.e.,matrix objects (note that amatrixobject will have different properties than anndarray object!). If we do the same matrix-vector calculation as above, we can show how ndarrayobjects may be converted into matrixobjects and how the calculations thencanbe fulfilled: In [1]: import numpy as np In [2]: I = np.eye(3) # create identity matrix In [3]: I Out[3]: array([[ 1., 0., 0.], [ 0., 1., 0.], [ 0., 0., 1.]]) In [4]: type(I) # confirm that type is ndarray Out[4]: numpy.ndarray In [5]: I = np.matrix(I) # convert to matrix object In [6]: type(I) # confirm that type is matrix Out[6]: numpy.matrixlib.defmatrix.matrix In [7]: x = np.array([1.0, 2.0, 3.0]) # create ndarray vector In [8]: x = np.matrix(x) # convert to matrix object (row vector) In [9]: x = x.transpose() # convert to column vector In [10]: y = I*x # computes matrix-vector product In [11]: y Out[11]: matrix([[ 1.], [ 2.], [ 3.]]) Note thatnp.matrix(x) turnsx, with typendarray, into a row vector by default (type matrix), so x must be transposed with x.transpose() before it can be multipliedwith thematrixI.
back to the  book Programming for Computations – Python - A Gentle Introduction to Numerical Simulations with Python 3.6, Volume Second Edition"
Programming for Computations – Python A Gentle Introduction to Numerical Simulations with Python 3.6, Volume Second Edition
Title
Programming for Computations – Python
Subtitle
A Gentle Introduction to Numerical Simulations with Python 3.6
Volume
Second Edition
Authors
Svein Linge
Hans Petter Langtangen
Publisher
Springer Open
Date
2020
Language
English
License
CC BY 4.0
ISBN
978-3-319-32428-9
Size
17.8 x 25.4 cm
Pages
356
Keywords
Programmiersprache, Informatik, programming language, functional, imperative, object-oriented, reflective
Category
Informatik
Web-Books
Library
Privacy
Imprint
Austria-Forum
Austria-Forum
Web-Books
Programming for Computations – Python