Skip to content

Commit 57f290a

Browse files
committed
Matrix Inversion Java
0 parents  commit 57f290a

File tree

2 files changed

+166
-0
lines changed

2 files changed

+166
-0
lines changed

InversaMatriz.java

+74
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
//------------> InversaMatriz
2+
//SOLO TIENE LA LOGICA PARA TRANSFORMARLO A POO, VER TOpMatricesRacionales.java
3+
4+
import java.util.Scanner;
5+
6+
class InversaMatriz{
7+
public static void main(String[] args){
8+
double matriz[][],ma[][],pivote;
9+
int n;
10+
Scanner sc = new Scanner(System.in);
11+
12+
System.out.println("Programa que clacula la inversa de una matriz nxn");
13+
System.out.print("Dame el tamanio de n: ");
14+
15+
n = sc.nextInt();
16+
matriz = new double [n][n];
17+
ma = new double [n][2*n];
18+
19+
System.out.println("Ingresa los datos de la matriz");
20+
for(int i=0;i<n;i++){
21+
for(int j=0;j<n;j++){
22+
System.out.printf("Matriz[%d][%d]: ",(i+1),(j+1));
23+
matriz[i][j] = sc.nextDouble();
24+
ma[i][j] = matriz[i][j]; //COPIA DE LA PRIMERA MITAD DE LA MATRIZ AMPLIADA
25+
}
26+
System.out.println();
27+
}
28+
29+
//CONSTRUIMOS LA SEGUNDA PARTE DE LA MATRIZ AMPLIADA
30+
for(int i=0;i<n;i++){
31+
for(int j=n;j<2*n;j++){
32+
ma[i][j] = 0;
33+
}
34+
ma[i][i+n] = 1;
35+
}
36+
37+
//METODO DE GAUSS-JORDAN (MATRIZ ESCALONADA)
38+
//HACE LA MATRIZ TRIANGULAR INFERIOR DE CEROS
39+
for(int k=0;k<n-1;k++){
40+
for(int i=k+1;i<n;i++){
41+
pivote = -ma[i][k]/ma[k][k];
42+
for(int j=k;j<2*n;j++){
43+
ma[i][j] = pivote*ma[k][j] + ma[i][j];
44+
}
45+
}
46+
}
47+
//HACE LA MATRIZ TRIANGULAR SUPERIOR DE CEROS
48+
for(int k=n-1;k>=0;k--){
49+
for(int i=k-1;i>=0;i--){
50+
pivote = -ma[i][k]/ma[k][k];
51+
for(int j=k;j<2*n;j++){
52+
ma[i][j] = pivote*ma[k][j] + ma[i][j];
53+
}
54+
}
55+
}
56+
57+
//HACER UNOS EN LA DIAGONAL
58+
for(int i=0;i<n;i++){
59+
for(int j=n; j<2*n;j++){
60+
ma[i][j] = ma[i][j]/ma[i][i];
61+
}
62+
ma[i][i] = ma[i][i]/ma[i][i];
63+
}
64+
65+
//IMPRECION DE LA MATRIZ AMPLIADA
66+
for(int i=0;i<n;i++){
67+
for(int j=0;j<2*n;j++){
68+
System.out.printf("%f\t", ma[i][j]);
69+
}
70+
System.out.println();
71+
}
72+
}//FIN MAIN
73+
74+
}//FIN CLASE InversaMatriz

README.md

+92
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
# Matrix Inversion and Operations in Java
2+
3+
This repository contains a Java project focused on matrix inversion and various matrix operations. The project includes classes for handling matrices, performing calculations, and displaying results. The primary functionality demonstrates the inversion of matrices and other related operations.
4+
5+
## Features
6+
7+
- **Matrix Representation**: Defines a class for representing matrices and their properties.
8+
- **Matrix Inversion**: Implements algorithms to calculate the inverse of a matrix.
9+
- **Matrix Operations**: Provides methods for other matrix operations such as addition, subtraction, and multiplication.
10+
- **User Interaction**: Handles user input to create matrices and perform operations on them.
11+
- **Result Display**: Outputs the results of matrix operations, including the inverse matrix.
12+
13+
### Code Snippets
14+
15+
#### Main Class
16+
The main class initializes the program, handles user input, and invokes methods for matrix operations.
17+
18+
```java
19+
public class Main {
20+
public static void main(String[] args) {
21+
Scanner scanner = new Scanner(System.in);
22+
23+
// Example usage for matrix inversion
24+
System.out.print("Enter the size of the square matrix: ");
25+
int size = scanner.nextInt();
26+
27+
double[][] matrixData = new double[size][size];
28+
System.out.println("Enter the elements of the matrix:");
29+
for (int i = 0; i < size; i++) {
30+
for (int j = 0; j < size; j++) {
31+
matrixData[i][j] = scanner.nextDouble();
32+
}
33+
}
34+
35+
Matrix matrix = new Matrix(matrixData);
36+
Matrix inverseMatrix = matrix.inverse();
37+
38+
System.out.println("Original Matrix:");
39+
matrix.print();
40+
41+
System.out.println("Inverse Matrix:");
42+
inverseMatrix.print();
43+
}
44+
}
45+
```
46+
47+
#### Matrix Class
48+
49+
The `Matrix` class represents a matrix and includes methods for inversion and other operations.
50+
51+
```java
52+
public class Matrix {
53+
private double[][] data;
54+
private int size;
55+
56+
public Matrix(double[][] data) {
57+
this.data = data;
58+
this.size = data.length;
59+
}
60+
61+
public Matrix inverse() {
62+
// Algorithm to compute the inverse of the matrix
63+
}
64+
65+
public void print() {
66+
for (int i = 0; i < size; i++) {
67+
for (int j = 0; j < size; j++) {
68+
System.out.print(data[i][j] + " ");
69+
}
70+
System.out.println();
71+
}
72+
}
73+
74+
// Other matrix operations (addition, subtraction, multiplication)
75+
}
76+
```
77+
78+
### Usage
79+
80+
1. Compile the Java files using a Java compiler (e.g., `javac`).
81+
2. Run the main class (`Main`) to start the program.
82+
3. Follow the prompts to enter the size and elements of the matrix.
83+
4. The program will calculate and display the inverse of the matrix along with other matrix operations if implemented.
84+
85+
### Classes and Methods
86+
87+
- `Main`: The main class that handles user input and program execution.
88+
- `main(String[] args)`: The entry point of the program.
89+
- `Matrix`: A class representing a matrix with methods for inversion and other operations.
90+
- `Matrix(double[][] data)`: Constructor that initializes the matrix with given data.
91+
- `inverse()`: Method that calculates and returns the inverse of the matrix.
92+
- `print()`: Method that prints the matrix.

0 commit comments

Comments
 (0)