Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.*;
- public class question1partA {
- public static void main (String[] args){
- double[][] output = {
- { 4, 12, -16},
- { 12, 37, -43},
- { -16, -43, 98}
- };
- double[] b = {100,200,150};
- //print matrix
- for (int i = 0; i < output.length; i++){
- for (int j = 0; j < output.length; j++){
- System.out.print(output[i][j] + " ");
- }
- System.out.println();
- }
- System.out.println();
- double[][] out = findL(output);
- //print L
- for (int i = 0; i < out.length; i++){
- for (int j = 0; j < out.length; j++){
- System.out.print(out[i][j] + " ");
- }
- System.out.println();
- }
- System.out.println();
- double[] yout = findY(out, b);
- //print y
- for (int i = 0; i < yout.length; i++){
- System.out.print(yout[i] + " ");
- }
- }
- //Choleski decomposition
- public static double[][] findL(double[][] array){
- double[][] L = new double[array.length][array.length];
- //populating L array with 0's first
- for (int i = 0; i < L.length ; i++){
- for (int j = 0; j < L.length; j++){
- L[i][j] = 0;
- }
- }
- for (int j = 0; j < array.length; j++){
- double f1 = 0;
- //computes for the sub of Ljj to Ljj-1
- for (int a = 0 ; a < j ; a++){
- f1 = f1 + Math.pow((double) L[j][a], 2);
- }
- L[j][j] = Math.sqrt(array[j][j] - f1);
- for (int i = j+1; i < array.length; i++){
- double f2 = 0;
- for (int b = 0; b < j ; b++){
- f2 = f2 + L[i][b] * L[j][b];
- }
- L[i][j] = (array[i][j] - f2) / L[j][j];
- }
- }
- return L;
- }
- //find Y
- public static double[] findY(double[][] larray, double[] barray){
- double[] y = new double[larray.length];
- for (int i = 0; i < y.length ; i++){
- y[i] = 0;
- }
- double f3 = 0;
- for (int i = 0; i < larray.length; i++) {
- for (int j = 0; j <= i - 1 ; j++){
- f3 = f3 + larray[i][j]*y[j];
- }
- y[i] = (barray[i] - f3)/larray[i][i];
- }
- return y;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement