Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
6 views

Lesson 2.1 Array Mathematics in Python

Uploaded by

21s01abt020
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views

Lesson 2.1 Array Mathematics in Python

Uploaded by

21s01abt020
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

Array Mathematics in Python

Introduction to arrays and matrices

 So far, we have created arrays and matrices for the purpose of holding a
range of values as input data or the intermediate or final values of our
calculations.
 The outputs were calculated by applying the same formula to each element
of the array and stored as values in the output arrays.
 Beyond these simple calculations, python has the capability of executing
matrix mathematics functions, which have several advantages:
o some calculations can be applied to the entire matrix at one time. In
doing so, the code is simpler to write and the execution is much faster
than the incremental calculation of individual elements contained in a
loop. = savings in computational resources
 For a wide range of other calculations in science and engineering, matrix
algebra is the most direct way of solving a variety of problems. Toward this
end, we present a brief overview matrix mathematics. This is followed by the
presentation of the python programming syntax

Brief overview of matrix mathematics

Matrix notation represents a matrix in row and column order. Thus a 2 × 3 matrix as
shown in Equation 4.1 has two rows and three columns.

………………..4.1

Multiplication by a scalar (a single constant)

 multiply each element by the scalar. Thus, [A]∗2 will give an output matrix
with the values 2, 4, 6, 8, 10, 12.

Addition and Subtraction

 also done element by element.


 the matrices must have the same dimensions

PYTHON CODE
A + B

B - A
Transpose

 The transpose of a matrix makes the rows into columns and columns into rows.
 This transformation can be used to transform row vectors into column vectors or to provide a
different view of a data table.

Multiplication

 A matrix of size m × n can only be multiplied by a matrix that is of size n × p. That is the
number of rows in the first matrix must equal the number of columns in the
second matrix. The product is then a matrix of size m × p.
 Matrix multiplication is not commutative: [A] × [B] will not produce the same
result as [B] × [A].
 The computation of the multiplication is the sum of the scalar products of ith
row of the first matrix with the elements of the jth column of the second
matrix. More specifically, the calculation:
[C]=[A] × [B] (4.5)
where:
o [A] is of the size m × n
o [B] is of the size n × p
o [C] is of the size m × p
Then

where i, j, and k are the row and column indices for the input and
output

matrices.
Special matrices

identity matrix.

 a matrix that has ones in the diagonal and zeros everywhere else.
 is important in understanding the concept of the inverse of a matrix. When
the inverse of a matrix is multiplied by the original matrix, that product is the
identity matrix.
 In matrix notation, the inverse of a matrix [A] is often represented as [A′].
Thus:
[A] [A’] = [I]
 where I is the identity matrix. Only square matrices have an inverse.

Matrix operations in python

 we first need to import the NumPy library using import numpy as np,
 we can create row vectors with np.array([1,2,3]) (which will create thevector
[1 2 3]).
 To create a column vector use the syntax np.array([[1], [2], [3]]).

 To create a matrix, such as the one in Equation 4.1, you can combine the
syntax and use np.array([[1,2,3],[4,5,6]]) to create

 To add the matrices A and B together, you can simply type


A + B
 Matrix subtraction is similar. Subtracting matrix A from matrix B. The syntax
is
B – A
 Matrix multiplication is also straightforward. If A and B are the matrices
shown below you can multiply them to get the matrix [C] using the following
syntax:
C = np.dot(A,B)
 To transpose a matrix—it must be square—you can use the transpose
function.
np.transpose(C)

You might also like