Advertisement
Void-voiD

Untitled

Feb 21st, 2019
197
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.12 KB | None | 0 0
  1. public class Test {
  2.     public static void main(String[] args) {
  3.         int[] coeff = new int[] {1, 2, 3, 4, 5};
  4.         VandermondeMatrix m1 = new VandermondeMatrix(5, coeff);
  5.         System.out.println(m1.countDeterminant());
  6.         System.out.println(m1.matrixToString());
  7.     }
  8. }
  9.  
  10.  
  11. public class VandermondeMatrix {
  12.     private int[] coefficients;
  13.     private int length, result = 1, i, j, k, currentElem, longest, maxLength, currentOffset;
  14.     private String string = "";
  15.  
  16.     public VandermondeMatrix(int length, int[] coefficients) {
  17.         this.length = length;
  18.         this.coefficients = coefficients;
  19.     }
  20.  
  21.     public int countDeterminant() {
  22.         if (length > 0) {
  23.             for (i = 1; i < length; i++) {
  24.                 for (j = 0; j < i; j++) result *= coefficients[i] - coefficients[j];
  25.             }
  26.             return result;
  27.         }
  28.         else return -1;
  29.     }
  30.  
  31.     public int LongestElem() {
  32.         if (length > 0) {
  33.             longest = coefficients[0];
  34.             for (i = 1; i < length; i++) {
  35.                 if (coefficients[i] > longest) longest = coefficients[i];
  36.             }
  37.             return longest;
  38.         }
  39.         else return 0;
  40.     }
  41.  
  42.     public String matrixToString() {
  43.         if (length > 0) {
  44.             longest = LongestElem();
  45.             for (i = 0; i < length; i++) {
  46.                 currentElem = 1;
  47.                 maxLength = ("" + longest).length();
  48.                 currentOffset = longest;
  49.                 for (j = 0; j < length; j++) {
  50.                     if (j != length - 1) {
  51.                         string += currentElem + " ";
  52.                         for (k = 0; k < maxLength - ("" + currentElem).length(); k++) string += " ";
  53.                     }
  54.                     else string += currentElem + "\n";
  55.                     currentElem *= coefficients[i];
  56.                     if (j >= 1) {
  57.                         currentOffset *= longest;
  58.                         maxLength = ("" + currentOffset).length();
  59.                     }
  60.                 }
  61.             }
  62.             return string;
  63.         }
  64.         else return "";
  65.     }
  66. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement