Question

In: Computer Science

Given the matrix A (2x2 matrix taking the first two column vectors from the input file),...

Given the matrix A (2x2 matrix taking the first two column vectors from the input file), compute the followings. Λ: the diagonal matrix whose diagonal elements are the corresponding eignevalues Λii = λi for i=1,2 R: the 2x2 matrix whose ith column vector is the eigenvector corresponding to λi for i=1,2 RΛRT: the matrix compositions 1/0: the comparison between A and RΛRT (is A= RΛRT?) Your output should have seven lines where the first two lines correspond to the 2x2 matrix of Λ, the next two lines is R, and the last two lines is the output matrix of RΛRT, and the last line is one binary number corresponding to yes (1) or no (0). The first six lines have two numbers per line, while the last line has one binary number. The output should contain numbers with up to four significant digits. If there is one real eigenvalue (λ1=λ2), then the two column vectors for R are the same, and your output should be of the aforementioned format. If there are no real eigenvalues, your output should be a single line printing "No real eigenvalues".

Input format (2x3 matrix):

n11 n12 n13

n21 n22 n23

In python please

Solutions

Expert Solution

ANSWER:

  • I have provided the properly commented  and indented code so you can easily copy the code as well as check for correct indentation.
  • I have provided the output image of the code so you can easily cross-check for the correct output of the code.
  • Have a nice and healthy day!!

CODE

import numpy as np

# reading input file "matrix.txt"
inputFile = open("matrix.txt")

# defining empty matrix A and b
A = []
b = []

# looping through each line and converting input to matrix
for line in inputFile:
    # spliting line and converting each value to float
    row = [int(c) for c in line.split()]
    # first two values to A
    A.append(row[:2])
    # and last to b
    b.append(row[2])
    
# eigen values and matrix using numpy.linalg.eig function
[V, R] = np.linalg.eig(A)

# converting eigen values V to diagonal matrix, using np.diag function
V = np.diag(V)

# calculating RVRT,using np.dot to multiply matrix
RV = np.dot(R,V)
RVRT = np.dot(RV, R.T)

# displaying result
# displaying result, diagonal matrix of V for 2x2 matrix display
print("{:.4f} {:.4f}\n{:.4f} {:.4f}".format(*V.flatten()))
# displaying result, diagonal matrix of R for 2x2 matrix display
print("{:.4f} {:.4f}\n{:.4f} {:.4f}".format(*R.flatten()))
# displaying result, diagonal matrix of RVRT for 2x2 matrix display
print("{:.4f} {:.4f}\n{:.4f} {:.4f}".format(*RVRT.flatten()))

# comparing RVRT to A and finding if all values of both matrix match using np.all
# finaly converting bool to int using int function
print(int(np.all(RVRT == A)))

INPUT IMAGE (matrix.txt)

OUTPUT IMAGE


Related Solutions

Write a program which reads the matrix A (2x2 matrix taking the first two column vectors...
Write a program which reads the matrix A (2x2 matrix taking the first two column vectors from the input file) and the vector b (the third column vector) and solve for x in Ax=b for general A. If there is a unique solution, your output should be a 2x2 matrix with two lines and two numbers per line. The output should contain numbers with up to four significant digits. If the system is unsolvable or inconsistent, then your output should...
Given a 2x2 matrix, A = 1 4 2 -1 Find its eigen values, eigen vectors....
Given a 2x2 matrix, A = 1 4 2 -1 Find its eigen values, eigen vectors. Can matrix be diagnolized?
The columns of the input file correspond to the point coordinates, for example, the first column...
The columns of the input file correspond to the point coordinates, for example, the first column provides the coordinates of the first point and the column length indicates whether it is 2D vs. 3D. First, compute the area of the triangle (the first line of your output file). Second, using the first two points, construct a line if 2D or a plane if 3D (the bisector of the two points) and find the distance of the third point to that...
Write a program that reads two strings from an input file (The first line is X,...
Write a program that reads two strings from an input file (The first line is X, the second line is Y), compute the longest common subsequence length AND the resulting string. You will need to write 2 methods 1) return LCS length in iterative function // return the length of LCS. L is the 2D matrix, X, Y are the input strings, m=|X|, n=|Y| int lcs_it(int **C, string X, string Y, int m, int n ) 2) return LCS resulting...
Exercise 2 — Matrix file The file format for the matrix will have the first line...
Exercise 2 — Matrix file The file format for the matrix will have the first line contain 2 integers, which are the number of rows and columns. After this line, repeatedly count from 0 to 9, filling in the matrix size conditions as specified above. For example: 3 4 0 1 2 3 4 5 6 7 8 9 0 1 Randomly create different sized arrays with random numbers. There should be a space between each number. You will be...
for input matrix a , write a function to return the element in row 2 column...
for input matrix a , write a function to return the element in row 2 column 3 of that matrix
Java. Given an input file with each line representing a record of data and the first...
Java. Given an input file with each line representing a record of data and the first token (word) being the key that the file is sorted on, we want to load it and output the line number and record for any duplicate keys we encounter. Remember we are assuming the file is sorted by the key and we want to output to the screen the records (and line numbers) with duplicate keys. We are given a text file and have...
For the following matrices, first find a basis for the column space of the matrix. Then...
For the following matrices, first find a basis for the column space of the matrix. Then use the Gram-Schmidt process to find an orthogonal basis for the column space. Finally, scale the vectors of the orthogonal basis to find an orthonormal basis for the column space. (a) [1 1 1, 1 0 2, 3 1 0, 0 0 4 ] b) [?1 6 6, 3 ?8 3, 1 ?2 6, 1 ?4 ?3 ]
Java Write a program that will only accept input from a file provided as the first...
Java Write a program that will only accept input from a file provided as the first command line argument. If no file is given or the file cannot be opened, simply print “Error opening file!” and stop executing. A valid input file should contain two lines. The first line is any valid string, and the second line should contain a single integer. The program should then print the single character from the string provided as the first line of input...
In parts a-d evaluate the following determinants. show all steps. a. 2x2 matrix the first row...
In parts a-d evaluate the following determinants. show all steps. a. 2x2 matrix the first row being 1 and 2 the second row being -3 and 4. b. 3x3 matrix, the first row being 2,1, 5, the second row being 0, 3, 2, the third row being 0, 0, 4. c. 3x3 matrix, the first row being 3, -1, 4, the second row being 2, -2, 3, the third row being 1, -1, 2 d. 4x4 matrix, the first row...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT