Advertisement
Void-voiD

Untitled

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