Advertisement
Guest User

Untitled

a guest
Nov 17th, 2019
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.78 KB | None | 0 0
  1. package com.wat;
  2.  
  3. import Jama.Matrix;
  4.  
  5. import java.util.Arrays;
  6.  
  7. import static java.lang.Math.*;
  8.  
  9. import java.util.Comparator;
  10.  
  11. public class Main {
  12. private static int matrixSize = 3;
  13. private static boolean OPTIMIZE_MULTIPLICATION_SUM = true;
  14.  
  15. public static void main(String[] args) {
  16. Matrix lMatrix = getL(Matrix.random(matrixSize, matrixSize));
  17.  
  18. Matrix identityMatrix = Matrix.identity(matrixSize, matrixSize);
  19.  
  20. Matrix invertedMatrix = getInverted(lMatrix, identityMatrix);
  21.  
  22. Matrix selfIdentityMatrix = multiply(lMatrix, invertedMatrix);
  23.  
  24. lMatrix.print(5, 5);
  25. invertedMatrix.print(5, 5);
  26. selfIdentityMatrix.print(5, 5);
  27.  
  28. double error = 0;
  29. for (int i = 0; i < matrixSize; i++)
  30. for (int j = 0; j < matrixSize; j++)
  31. error += Math.sqrt(Math.pow(selfIdentityMatrix.get(i, j), 2) + Math.pow(identityMatrix.get(i, j), 2));
  32.  
  33. System.out.println("Error: " + error);
  34. }
  35.  
  36. private static Matrix getL(Matrix base) {
  37. Matrix l = new Matrix(matrixSize, matrixSize);
  38.  
  39. for (int i = 0; i < matrixSize; i++)
  40. for (int j = 0; j < matrixSize; j++) {
  41. if (i >= j)
  42. l.set(i, j, base.get(i, j) * 1);
  43. }
  44.  
  45. return l;
  46. }
  47.  
  48. private static Matrix getInverted(Matrix base, Matrix identityMatrix) {
  49. Matrix calculated = calculate(base, identityMatrix);
  50. return inverse(calculated);
  51. }
  52.  
  53. private static Matrix calculate(Matrix base, Matrix identityMatrix) {
  54. double[][] result = new double[matrixSize][matrixSize];
  55.  
  56. for (int i = 0; i < matrixSize; i++) {
  57. double[] identityColumn = identityMatrix.getMatrix(0, matrixSize - 1, i, i).transpose().getArray()[0];
  58.  
  59. result[i][0] = (identityColumn[0]) / base.get(0, 0);
  60.  
  61. for (int j = 1; j < matrixSize; j++) {
  62. double[] baseRow = base.getMatrix(j, j, 0, matrixSize - 1).getArray()[0];
  63. result[i][j] = (identityColumn[j] - getMultiplicationSum(baseRow, result[i])) / base.get(j, j);
  64. }
  65.  
  66. }
  67.  
  68. return new Matrix(result);
  69. }
  70.  
  71. private static double getMultiplicationSum(double[] row, double[] column) {
  72. if (OPTIMIZE_MULTIPLICATION_SUM) {
  73. Double[] sums = new Double[matrixSize];
  74. for (int i = 0; i < matrixSize; i++)
  75. sums[i] = row[i] * column[i];
  76.  
  77. Arrays.sort(sums, new Comparator<Double>() {
  78. @Override
  79. public int compare(Double o1, Double o2) {
  80. return Double.compare(Math.abs(o1), Math.abs(o2));
  81. }
  82. });
  83. double sum = 0;
  84.  
  85. for (Double item : sums) {
  86. sum += item;
  87. }
  88.  
  89. return sum;
  90. } else {
  91. double sum = 0;
  92. for (int i = 0; i < matrixSize; i++)
  93. sum += row[i] * column[i];
  94. return sum;
  95. }
  96. }
  97.  
  98. private static Matrix inverse(Matrix base) {
  99. double[][] inverted = new double[matrixSize][matrixSize];
  100. for (int i = 0; i < matrixSize; i++)
  101. for (int j = 0; j < matrixSize; j++)
  102. inverted[i][j] = base.get(j, i);
  103.  
  104. return new Matrix(inverted);
  105. }
  106.  
  107. private static Matrix multiply(Matrix first, Matrix second) {
  108. Matrix result = new Matrix(matrixSize, matrixSize);
  109. for (int i = 0; i < matrixSize; i++)
  110. for (int j = 0; j < matrixSize; j++) {
  111. double s = 0;
  112. for (int k = 0; k < matrixSize; k++)
  113. s += first.get(i, k) * second.get(k, j);
  114. result.set(i, j, s);
  115. }
  116. return result;
  117. }
  118. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement