Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class Test {
- public static void main(String[] args) {
- int[] coeff = new int[] {1, 2, 3, 4, 5};
- VandermondeMatrix m1 = new VandermondeMatrix(5, coeff);
- System.out.println(m1.countDeterminant());
- System.out.println(m1.matrixToString());
- }
- }
- public class VandermondeMatrix {
- private int[] coefficients;
- private int length, result = 1, i, j, k, currentElem, longest, maxLength, currentOffset;
- private String string = "";
- public VandermondeMatrix(int length, int[] coefficients) {
- this.length = length;
- this.coefficients = coefficients;
- }
- public int countDeterminant() {
- if (length > 0) {
- for (i = 1; i < length; i++) {
- for (j = 0; j < i; j++) result *= coefficients[i] - coefficients[j];
- }
- return result;
- }
- else return -1;
- }
- public int LongestElem() {
- if (length > 0) {
- longest = coefficients[0];
- for (i = 1; i < length; i++) {
- if (coefficients[i] > longest) longest = coefficients[i];
- }
- return longest;
- }
- else return 0;
- }
- public String matrixToString() {
- if (length > 0) {
- longest = LongestElem();
- for (i = 0; i < length; i++) {
- currentElem = 1;
- maxLength = ("" + longest).length();
- currentOffset = longest;
- for (j = 0; j < length; j++) {
- if (j != length - 1) {
- string += currentElem + " ";
- for (k = 0; k < maxLength - ("" + currentElem).length(); k++) string += " ";
- }
- else string += currentElem + "\n";
- currentElem *= coefficients[i];
- if (j >= 1) {
- currentOffset *= longest;
- maxLength = ("" + currentOffset).length();
- }
- }
- }
- return string;
- }
- else return "";
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement