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[] {-33, 7, 222, -11};
- VandermondeMatrix m1 = new VandermondeMatrix(4, coeff);
- System.out.println(m1.countDeterminant());
- System.out.println(m1.matrixToString());
- }
- }
- public class VandermondeMatrix {
- int[] coefficients;
- int length, result = 1, i, j, k, currentElem, longest, maxLength, currentOffset;
- 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 {
- System.out.println("Empty Matrix");
- return -1;
- }
- }
- public int LongestElem() {
- if (length > 0) {
- longest = coefficients[0];
- for (i = 1; i < length; i++) {
- if (coefficients[i] > longest || (coefficients[i] < 0 && -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 + " ";
- if (j != 0) for (k = 0; k < maxLength - ("" + currentElem).length(); k++) string += " ";
- }
- else {
- if (i != length - 1) string += currentElem + "\n";
- else string += "" + currentElem;
- }
- 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