Web-Books
im Austria-Forum
Austria-Forum
Web-Books
Informatik
Programming for Computations – Python - A Gentle Introduction to Numerical Simulations with Python 3.6, Band Second Edition
Seite - 53 -
  • Benutzer
  • Version
    • Vollversion
    • Textversion
  • Sprache
    • Deutsch
    • English - Englisch

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

Bild der Seite - 53 -

Bild der Seite - 53 - in Programming for Computations – Python - A Gentle Introduction to Numerical Simulations with Python 3.6, Band Second Edition

Text der Seite - 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.
zurück zum  Buch Programming for Computations – Python - A Gentle Introduction to Numerical Simulations with Python 3.6, Band Second Edition"
Programming for Computations – Python A Gentle Introduction to Numerical Simulations with Python 3.6, Band Second Edition
Titel
Programming for Computations – Python
Untertitel
A Gentle Introduction to Numerical Simulations with Python 3.6
Band
Second Edition
Autoren
Svein Linge
Hans Petter Langtangen
Verlag
Springer Open
Datum
2020
Sprache
englisch
Lizenz
CC BY 4.0
ISBN
978-3-319-32428-9
Abmessungen
17.8 x 25.4 cm
Seiten
356
Schlagwörter
Programmiersprache, Informatik, programming language, functional, imperative, object-oriented, reflective
Kategorie
Informatik
Web-Books
Bibliothek
Datenschutz
Impressum
Austria-Forum
Austria-Forum
Web-Books
Programming for Computations – Python