0% found this document useful (0 votes)
14 views

Lecture_TWP_Python_A04_1a_Linear_Algebra

The document provides an overview of using Python's numpy library for linear algebra, including array creation, matrix operations, and eigenvalue computations. It includes code examples demonstrating various functionalities of numpy for scientific computing. Additionally, references for further reading on Python programming and scientific applications are provided.

Uploaded by

Nirjhar Dhang
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views

Lecture_TWP_Python_A04_1a_Linear_Algebra

The document provides an overview of using Python's numpy library for linear algebra, including array creation, matrix operations, and eigenvalue computations. It includes code examples demonstrating various functionalities of numpy for scientific computing. Additionally, references for further reading on Python programming and scientific applications are provided.

Uploaded by

Nirjhar Dhang
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 30

Together With Python

Together With Python


Computation, Visualization and Reporting
Part A-4 : Linear Algebra

Nirjhar Dhang


Version : 1.732 : 3
Created on : August 11, 2016
Last revision : January 11, 2025
Together With Python
Computation, Visualization and Reporting
Part A-4 : Linear Algebra

Nirjhar Dhang


Version : 1.732 : 3
Created on : August 11, 2016
Last revision : January 11, 2025
Overview

1 Python numpy

2 Arrays in numpy

3 Summary

4 References
Python numpy
Python numpy

Numpy is the fundamental package for scientific computing with


Python.
It is a general-purpose array-processing package.
It provides a high-performance multidimensional array object,
and tools for working with these arrays.
Arrays in numpy
Arrays in numpy

import numpy as np

A = np.array([10, 20, 30])


print(’A:’)
print(A)

B = np.array([[10, 20, 30],


[40, 50, 60]])
print(’B:’)
print(B)

C = np.array((10, 20, 30))


print(’C:’)
print(C)
Arrays in numpy

Output
A:
[10 20 30]
B:
[[10 20 30]
[40 50 60]]
C:
[10 20 30]
Arrays in numpy

import numpy as np
A=np.array([[10.0,20.0,30.0],
[40.0,50.0,60.0],
[70.0,80.0,90.0]])
print(’A:’)
print(A)
print(’Transpose of A:’)
print(A.T)
Arrays in numpy

Output
A:
[[10. 20. 30.]
[40. 50. 60.]
[70. 80. 90.]]
Transpose of A:
[[10. 40. 70.]
[20. 50. 80.]
[30. 60. 90.]]
Arrays in numpy

import numpy as np
A=np.array([[6.0,2.0,1.0],
[2.0,7.0,1.0],
[1.0,1.0,8.0]])
B=np.array([9.0,10.0,10.0])
C=np.dot(A,B)
print(’A:’)
print(A)
print(’B:’)
print(B)
print(’C:’)
print(C)
Arrays in numpy

Output
A:
[[6. 2. 1.]
[2. 7. 1.]
[1. 1. 8.]]
B:
[ 9. 10. 10.]
C:
[84. 98. 99.]
Arrays in numpy

import numpy as np
A=np.array([[6.0,2.0,1.0],
[2.0,7.0,1.0],
[1.0,1.0,8.0]])
B=np.array([9.0,10.0,10.0])
C=np.linalg.solve(A,B)
print(’A:’)
print(A)
print(’B:’)
print(B)
print(’C:’)
print(C)
Arrays in numpy

Output
A:
[[6. 2. 1.]
[2. 7. 1.]
[1. 1. 8.]]
B:
[ 9. 10. 10.]
C:
[1. 1. 1.]
Arrays in numpy

import numpy as np
import numpy.linalg as la

A=np.array([[2.0,1.0],
[1.0,2.0]])
Eva,Eve=la.eig(A)
print(’A:’)
print(A)
print(’Eigen values:’)
print(Eva)
print(’Eigen vectors:’)
print(Eve)
Arrays in numpy

Output
A:
[[2. 1.]
[1. 2.]]
Eigen values:
[3. 1.]
Eigen vectors:
[[ 0.70710678 -0.70710678]
[ 0.70710678 0.70710678]]
Arrays in numpy

import numpy as np
import numpy.linalg as la

A=np.array([[6.0,2.0,1.0],
[2.0,7.0,1.0],
[1.0,1.0,8.0]])
Eva,Eve=la.eig(A)
print(’A:’)
print(A)
print(’Eigen values:’)
print(Eva)
print(’Eigen vectors:’)
print(Eve)
Arrays in numpy

Output
A:
[[6. 2. 1.]
[2. 7. 1.]
[1. 1. 8.]]
Eigen values:
[9.71447874 4.42879858 6.85672268]
Eigen vectors:
[[-0.49183142 -0.79848935 -0.34715503]
[-0.59615502 0.5994227 -0.53412697]
[-0.6345873 0.05574221 0.77083835]]
Arrays in numpy

import numpy as np

A=np.zeros((3,3),float)

A[0][0]=6.0
A[0][1]=2.0
A[0][2]=1.0
A[1][0]=2.0
A[1][1]=7.0
A[1][2]=1.0
A[2][0]=1.0
A[2][1]=1.0
A[2][2]=9.0
Arrays in numpy

B=np.zeros((3),float)

B[0]=9.0
B[1]=10.0
B[2]=11.0

C=np.linalg.solve(A,B)

print(’A:’)
print(A)
print(’B:’)
print(B)
print(’C:’)
print(C)
Arrays in numpy

Output
A:
[[6. 2. 1.]
[2. 7. 1.]
[1. 1. 9.]]
B:
[ 9. 10. 11.]
C:
[1. 1. 1.]
Arrays in numpy

import numpy as np
import numpy.linalg as la

A=np.zeros((3,3),float)

A[0][0]=6.0
A[0][1]=2.0
A[0][2]=1.0
A[1][0]=2.0
A[1][1]=7.0
A[1][2]=1.0
A[2][0]=1.0
A[2][1]=1.0
A[2][2]=9.0
Arrays in numpy

Eva,Eve=la.eig(A)

print(’A:’)
print(A)
print(’Eigen values:’)
print(Eva)
print(’Eigen vectors:’)
print(Eve)
Arrays in numpy

Output
A:
[[6. 2. 1.]
[2. 7. 1.]
[1. 1. 9.]]
Eigen values:
[10.20440155 4.43115039 7.36444806]
Eigen vectors:
[[-0.41760969 -0.79624527 -0.43771637]
[-0.49785133 0.60350126 -0.6228405 ]
[-0.76009618 0.04218655 0.64843974]]
Summary
Summary

Linear Algebra using numpy is explained


References
References

Python for Scientists, John M. Stewart, Cambridge University


Press, 2014
Programming Python, Mark Lutz, O’Reilly,2012

You might also like