Advertisement
Guest User

Untitled

a guest
Sep 26th, 2016
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.94 KB | None | 0 0
  1. import java.util.*;
  2. public class question1partA {
  3.     public static void main (String[] args){
  4.         double[][] output = {
  5.                   { 4, 12, -16},
  6.                   { 12, 37, -43},
  7.                   { -16, -43, 98}
  8.         };
  9.         double[] b = {100,200,150};
  10.        
  11.         //print matrix
  12.         for (int i = 0; i < output.length; i++){
  13.             for (int j = 0; j < output.length; j++){
  14.                 System.out.print(output[i][j] + " ");
  15.             }
  16.             System.out.println();
  17.         }
  18.         System.out.println();
  19.        
  20.         double[][] out = findL(output);
  21.         //print L
  22.         for (int i = 0; i < out.length; i++){
  23.             for (int j = 0; j < out.length; j++){
  24.                 System.out.print(out[i][j] + " ");
  25.             }
  26.             System.out.println();
  27.         }
  28.         System.out.println();
  29.        
  30.         double[] yout = findY(out, b);
  31.         //print y
  32.         for (int i = 0; i < yout.length; i++){
  33.             System.out.print(yout[i] + " ");
  34.         }
  35.     }
  36.     //Choleski decomposition
  37.     public static double[][] findL(double[][] array){
  38.         double[][] L = new double[array.length][array.length];
  39.        
  40.         //populating L array with 0's first
  41.         for (int i = 0; i < L.length ; i++){
  42.             for (int j = 0; j < L.length; j++){
  43.                 L[i][j] = 0;
  44.             }
  45.         }
  46.         for (int j = 0; j < array.length; j++){
  47.             double f1 = 0;
  48.             //computes for the sub of Ljj to Ljj-1
  49.             for (int a = 0 ; a < j  ; a++){
  50.                 f1 = f1 + Math.pow((double) L[j][a], 2);
  51.             }
  52.             L[j][j] = Math.sqrt(array[j][j] - f1);
  53.            
  54.             for (int i = j+1; i < array.length; i++){
  55.                 double f2 = 0;
  56.                 for (int b = 0; b < j ; b++){
  57.                     f2 = f2 + L[i][b] * L[j][b];               
  58.                 }
  59.                 L[i][j] = (array[i][j] - f2) / L[j][j];
  60.             }
  61.         }
  62.         return L;
  63.        
  64.  
  65.     }
  66.     //find Y
  67.     public static double[] findY(double[][] larray, double[] barray){
  68.         double[] y = new double[larray.length];
  69.        
  70.         for (int i = 0; i < y.length ; i++){
  71.             y[i] = 0;
  72.         }
  73.        
  74.         double f3 = 0;
  75.         for (int i = 0; i < larray.length; i++) {
  76.             for (int j = 0; j <= i - 1 ; j++){
  77.                 f3 = f3 + larray[i][j]*y[j];
  78.             }
  79.             y[i] = (barray[i] - f3)/larray[i][i];
  80.         }
  81.         return y;
  82.     }
  83.    
  84.    
  85. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement