0% found this document useful (0 votes)
39 views2 pages

Week

The document demonstrates using NumPy to perform linear algebra operations. It shows: 1) Using matrix inverse and multiplication to solve a system of linear equations for variables W, X, Y, Z. 2) Calculating the inverse, determinant, and eigenvalues of a 2x2 matrix. 3) Concatenating arrays to combine them horizontally and vertically. 4) Creating a matrix by concatenating arrays and using it to approximate pi.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
39 views2 pages

Week

The document demonstrates using NumPy to perform linear algebra operations. It shows: 1) Using matrix inverse and multiplication to solve a system of linear equations for variables W, X, Y, Z. 2) Calculating the inverse, determinant, and eigenvalues of a 2x2 matrix. 3) Concatenating arrays to combine them horizontally and vertically. 4) Creating a matrix by concatenating arrays and using it to approximate pi.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

Untitled3

August 20, 2018

In [1]: import numpy as np


#question no2
#we can convert the equation as AB=C, where all A,B,C are matrix.
#The value of A can be determined by A=B^-1C,#inverse of B can be done by folllowing way
B=np.array([[1,3,-5,2],[0,4,-2,1],[2,-1,3,-1],[1,1,1,1]])
from numpy.linalg import inv
B_inv=inv(B)
C=np.array([[0],[6],[5],[10]])
#A can be identified by matrix multiplication of B_inv and C.
A=np.dot(inv(B),C)
print(A)
print("value of W is:",A[0])
print("value of X is:",A[1])
print("value of Y is:",A[2])
print("value of Z is:",A[3])

[[1.]
[2.]
[3.]
[4.]]
value of W is: [1.]
value of X is: [2.]
value of Y is: [3.]
value of Z is: [4.]

In [2]: import numpy as np


from numpy.linalg import inv
M=np.arange(1,5,1).reshape(2,2)
M_inv=inv(M)
print("inverse of M is:",M_inv)
from numpy.linalg import det
M_det=det(M)
print("determinant of M is:",M_det)
from numpy.linalg import eigvals
M_eigv=eigvals(M)
print("eigen value of M is:",M_eigv)

1
inverse of M is: [[-2. 1. ]
[ 1.5 -0.5]]
determinant of M is: -2.0000000000000004
eigen value of M is: [-0.37228132 5.37228132]

In [3]: A=np.array([1,2,3])
B=np.array([4,5,6])
np.concatenate((A,B),axis=0)

Out[3]: array([1, 2, 3, 4, 5, 6])

In [21]: a=np.zeros(100).reshape(10,10)
b=np.ones(10).reshape(1,10)
column=np.ones(12)[:, np.newaxis]
upper=np.concatenate((a,b),axis=0)
lower=np.concatenate((b,upper),axis=0)
left=np.concatenate((column,lower),axis=1)
ans=np.concatenate((left,column),axis=1)
ans

Out[21]: array([[1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1.],
[1., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 1.],
[1., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 1.],
[1., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 1.],
[1., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 1.],
[1., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 1.],
[1., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 1.],
[1., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 1.],
[1., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 1.],
[1., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 1.],
[1., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 1.],
[1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1.]])

In [13]: import numpy as np


array=np.arange(0,30,1)
pie_array=((-1/3)**array)/(2*(array)+1)
pie_array.sum()*12**0.5

Out[13]: 3.1415926535897936

You might also like