Showing posts with label scipy python program. Show all posts
Showing posts with label scipy python program. Show all posts

Scipy - Square matrix - Determinant, Eigen values and Eigen vectors

 # Scipy - Determinant of a square array

# Scipy - Eigen values and Eigen vectors


#importing the scipy and numpy packages


from scipy import linalg

import numpy as np

import random


# Creating a random 2d array object of size 3 x 3

A = np.random.randint(10, size=(2,2))


# Calculating the determinant of a square matrix

DA = linalg.det(A)


#printing the result array

print("Given square array")

print(A)


print("Determinant of the square array")

print(int(DA))


# Calling the eigen function

eig_val, eig_vect = linalg.eig(A)


# Printing the eigen values

print("Eigen values : ")

print(eig_val)


# Printing the eigen vectors

print("Eigen vectors : ")

print(eig_vect)



"""


Sample output


>python 8_6_scipy2.py

Given square array

[[0 5]

 [2 5]]

 

Determinant of the square array

-10


Eigen values :

[-1.53112887+0.j  6.53112887+0.j]


Eigen vectors :

[[-0.9561723  -0.60788018]

 [ 0.2928046  -0.79402877]]

 

"""


Scipy - Solving linear equations

 # Scipy - solving linear equations 



#importing the scipy and numpy packages


from scipy import linalg

import numpy as np


#Declaring the numpy arrays

a = np.array([[3, 2, 0], [1, -1, 0], [0, 5, 1]])

b = np.array([2, 4, -1])


#Passing the values to the solve function

x = linalg.solve(a, b)


#printing the result array

print(x)


"""


Given input

3x + 2y = 2

x - y = 4

5y + z = -1


Sample output

>python scipy1.py

[ 2. -2.  9.]


"""