Sei sulla pagina 1di 6

package chanh;

import java.io.BufferedReader;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.charset.Charset;
import java.nio.file.Files;

public class Chanh {


static int count;

public static double[][] loadData(String filepath) {


double arr[][] = null;
Path path = Paths.get(filepath);
Charset charset = Charset.forName("US-ASCII");
try (BufferedReader reader = Files.newBufferedReader(path, charset)){
String line = null;

while ((line = reader.readLine()) != null) {


String k[] = line.split(" ");
if(k.length == 1) {
arr = new double[Integer.parseInt(k[0])]
[Integer.parseInt(k[0])];

}else {
for (int i =0; i < k.length; i++) {
arr[count][i]= Integer.parseInt(k[i]);
}
count++;
}

}
}catch (Exception e) {
System.out.println(e.getMessage());
//return false;
}
for(int i =0; i < arr.length; i++) {
for (int j =0; j < arr.length; j++) {
System.out.print(arr[i][j]+" ");
}
System.out.println();
}
//return true;
return arr;
}

public static double determinant(double A[][],int N)


{
double det=0;
if(N == 1)
{
det = A[0][0];
}
else if (N == 2)
{
det = A[0][0]*A[1][1] - A[1][0]*A[0][1];
}
else
{
det=0;
for(int j1=0;j1<N;j1++)
{
double[][] m = new double[N-1][];
for(int k=0;k<(N-1);k++)
{
m[k] = new double[N-1];
}
for(int i=1;i<N;i++)
{
int j2=0;
for(int j=0;j<N;j++)
{
if(j == j1)
continue;
m[i-1][j2] = A[i][j];
j2++;
}
}
det += Math.pow(-1.0,1.0+j1+1.0)* A[0][j1] * determinant(m,N-1);
}
}
return det;
}

public static double[][] invert(double a[][])


{
int n = a.length;
double x[][] = new double[n][n];
double b[][] = new double[n][n];
int index[] = new int[n];
for (int i=0; i<n; ++i)
b[i][i] = 1;

// Transform the matrix into an upper triangle


gaussian(a, index);

// Update the matrix b[i][j] with the ratios stored


for (int i=0; i<n-1; ++i)
for (int j=i+1; j<n; ++j)
for (int k=0; k<n; ++k)
b[index[j]][k]
-= a[index[j]][i]*b[index[i]][k];

// Perform backward substitutions


for (int i=0; i<n; ++i)
{
x[n-1][i] = b[index[n-1]][i]/a[index[n-1]][n-1];
for (int j=n-2; j>=0; --j)
{
x[j][i] = b[index[j]][i];
for (int k=j+1; k<n; ++k)
{
x[j][i] -= a[index[j]][k]*x[k][i];
}
x[j][i] /= a[index[j]][j];
}
}
return x;
}

// Method to carry out the partial-pivoting Gaussian


// elimination. Here index[] stores pivoting order.

public static void gaussian(double a[][], int index[])


{
int n = index.length;
double c[] = new double[n];

// Initialize the index


for (int i=0; i<n; ++i)
index[i] = i;

// Find the rescaling factors, one from each row


for (int i=0; i<n; ++i)
{
double c1 = 0;
for (int j=0; j<n; ++j)
{
double c0 = Math.abs(a[i][j]);
if (c0 > c1) c1 = c0;
}
c[i] = c1;
}

// Search the pivoting element from each column


int k = 0;
for (int j=0; j<n-1; ++j)
{
double pi1 = 0;
for (int i=j; i<n; ++i)
{
double pi0 = Math.abs(a[index[i]][j]);
pi0 /= c[index[i]];
if (pi0 > pi1)
{
pi1 = pi0;
k = i;
}
}

// Interchange rows according to the pivoting order


int itmp = index[j];
index[j] = index[k];
index[k] = itmp;
for (int i=j+1; i<n; ++i)
{
double pj = a[index[i]][j]/a[index[j]][j];

// Record pivoting ratios below the diagonal


a[index[i]][j] = pj;

// Modify other elements accordingly


for (int l=j+1; l<n; ++l)
a[index[i]][l] -= pj*a[index[j]][l];
}
}
}

public static void main(String[] args) {

double A[][] =
loadData("D:\\Nam4\\Nam4\\Java\\workspace\\chanh\\nhap.txt");

// double B[][] =
Chanh.loadData("D:\\Nam4\\Nam4\\Java\\workspace\\chanh\\B.txt");
// Chanh I = new Chanh();
// System.out.println(count);
System.out.println("Dinh thuc ma tran A : "+determinant(A,count));

count = 0;

double B[][] =
loadData("D:\\Nam4\\Nam4\\Java\\workspace\\chanh\\B.txt");

// Nh�n 2 hai ma tr?n A v� B : C = A * B

double C[][] = new double[count][count];


for (int i = 0; i < count; i++) {
for (int j = 0; j < count; j++) {

C[i][j] = 0;
for (int k = 0; k < count; k++) {
C[i][j] = (int) (C[i][j] + A[i][k] * B[k][j]);
}
}
}
System.out.println("Ma tr?n t�ch C: ");
for (int i = 0; i < count; i++) {
for (int j = 0; j < count; j++) {
System.out.print(C[i][j] + "\t");
}
System.out.println("\n");
}

// hi?n th? ma tr?n kh? ngh?ch E

double H[][] = invert(B);


System.out.println("Ma Tr?n kh? ngh?ch B l� : ");
for (int i = 0; i < count; i++) {
for (int j = 0; j < count; j++) {
System.out.print(H[i][j] + "\t");
}
System.out.println("\n");
}

// T�nh ra ma tr?n thuong F= A/B = A*B^-1 = A*E.


double G[][] = new double[count][count];
for (int i = 0; i < count; i++) {
for (int j = 0; j < count; j++) {
G[i][j] = 0;
for (int k = 0; k < count; k++) {
G[i][j] = G[i][j] + A[i][k] * H[k][j];
}
}
}

// hi?n th? ma tr?n t�ch F


double F[][] = new double[count][count];
System.out.println("Ma tr?n thuong F = A/B = A*B^-1 2: ");
for (int i = 0; i < count; i++) {
for (int j = 0; j < count; j++) {
System.out.print(F[i][j] + "\t");
}
System.out.println("\n");
}

// t�m ma tr?n D l� ma tr?n chuy?n v? c?a ma tr?n C


// ma tr?n D l� ma tr?n chuy?n v? c?a ma tr?n C
// th� c�c d�ng c?a ma tr?n C s? tr? th�nh
// c�c c?t c?a ma tr?n D v� ngu?c l?i
// v� d?: ma tr?n C c� s? d�ng m1 = 3 v� s? c?t n2 = 4 th�
// ma tr?n D s? c� s? d�ng n2 = 4 v� s? c?t m1 = 3
double D[][] = new double[count][count];
for (int i = 0; i < count; i++) {
for (int j = 0; j < count; j++) {
D[j][i] = C[i][j];
}
}

// hi?n th? ma tr?n D


System.out.println("Ma tr?n chuy?n v? c?a ma tr?n C l�: ");
for (int i = 0; i < count; i++) {
for (int j = 0; j < count; j++) {
System.out.print(D[i][j] + "\t");
}
System.out.println("\n");
}

double[][] sum1 = new double[count][count];


double[][] diff1 = new double[count][count];
for (int i = 0; i < count; i++) {
for (int j = 0; j < count; j++) {
sum1[i][j] = A[i][j] + B[i][j];
diff1[i][j] = A[i][j] - B[i][j];
}
}

double[][] sum2 = new double[count][count];


double[][] diff2 = new double[count][count];
for (int i = 0; i < count; i++) {
for (int j = 0; j < count; j++) {
sum2[i][j] = sum1[i][j] + F[i][j];
diff2[i][j] = diff1[i][j] - F[i][j];
}
}

double[][] sum = new double[count][count];


double[][] diff = new double[count][count];

for (int i = 0; i < count; i++) {


for (int j = 0; j < count; j++) {
sum[i][j] = sum2[i][j] + F[i][j];
diff[i][j] = diff2[i][j] - F[i][j];
}
}

System.out.println("T?ng ma tr?n l�:");


for (int i = 0; i < count; i++) {
for (int j = 0; j < count; j++) {
System.out.print(sum[i][j] + " ");
}
System.out.println();
}
System.out.println("Hi?u ma tr?n l�:");
for (int i = 0; i < count; i++) {
for (int j = 0; j < count; j++) {
System.out.print(diff[i][j] + " ");
}
System.out.println();
}

Potrebbero piacerti anche