-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patheigen_valuein_.cpp maths
128 lines (101 loc) · 2.72 KB
/
eigen_valuein_.cpp maths
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
#include<bits/stdc++.h>
using namespace std;
int calculateDeterminant(int** arr, int rows, int cols) {
// Base case: If the matrix is 1x1, return its only element
if (rows == 1 && cols == 1) {
return arr[0][0];
}
// Initialize the determinant value
int determinant = 0;
// Create a temporary submatrix
int** subMatrix = new int*[rows - 1];
for (int i = 0; i < rows - 1; ++i) {
subMatrix[i] = new int[cols - 1];
}
// Iterate through the first row to calculate the determinant
for (int j = 0; j < cols; ++j) {
// Create the submatrix by excluding the current row and column
int subRow = 0;
for (int i = 1; i < rows; ++i) {
int subCol = 0;
for (int k = 0; k < cols; ++k) {
if (k != j) {
subMatrix[subRow][subCol] = arr[i][k];
subCol++;
}
}
subRow++;
}
// Calculate the determinant recursively
int sign = (j % 2 == 0) ? 1 : -1; // Alternating signs
determinant += sign * arr[0][j] * calculateDeterminant(subMatrix, rows - 1, cols - 1);
}
// Clean up memory for the submatrix
for (int i = 0; i < rows - 1; ++i) {
delete[] subMatrix[i];
}
delete[] subMatrix;
return determinant;
}
int main(){
int rows,col;
cout<<"Give the value of X and Y : ";
cin>>rows>>col;
int** arr = new int*[rows];
for (int i = 0; i < rows; i++)
{
arr[i] = new int[col];
for (int j = 0; j < col; j++)
{
cin>>arr[i][j];
}
}
//initializing the identity matrix ;
int** id =new int*[rows];
for (int i = 0; i < rows; i++)
{
id[i] =new int[col];
for (int j = 0; j < col; j++)
{
if(i==j){
id[i][j] = 1;
}
else
{
id[i][j] =0;
}
}
}
// A-λI
cout<<calculateDeterminant(arr,rows,col)<<endl;
for (int i = 0; i < rows; i++)
{
for (int j = 0; j < col; j++)
{
arr[i][j] = arr[i][j] - id[i][j];
}
}
cout<<endl;
//Printing the A-λI function
for (int i = 0; i < rows; i++)
{
for (int j = 0; j < col; j++)
{
if (i==j)
{
cout<<arr[i][j]<<"-λ\t";
}
else
{
cout<<arr[i][j]<<"\t";
}
}
cout<<endl;
}
for (int i = 0; i < rows; i++) {
delete[] arr[i];
delete[] id[i];
}
delete[] arr;
delete[] id;
}