Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package fowler;
- import java.math.BigDecimal;
- import java.math.RoundingMode;
- /**
- *
- * @author Dale Diaz
- */
- public class Fowler {
- public static void main(String[] args) {
- // Value for function of L
- final int L = 2;
- //////////////////////////////////////////////////
- ////////// Compute {(148!) / [(13!) * (81!) * (2!) * (52!)]} * e^13L
- ////////////////////////////////////////////////////////////////////////
- // Numerator of the first inner fraction
- BigDecimal numerator;
- // Calculate the numerator of the numerator variable (148!)
- BigDecimal num1 = new BigDecimal("1");
- for (int i = 1; i <= 148; i++) {
- num1 = num1.multiply(new BigDecimal(i + ""));
- }
- // Calculate the denominator of the numerator variable (13!81!2!52!)
- BigDecimal num2 = new BigDecimal("1");
- // 13!
- for (int i = 1; i <= 13; i++) {
- num2 = num2.multiply(new BigDecimal(i + ""));
- }
- // 81!
- for (int i = 1; i <= 81; i++) {
- num2 = num2.multiply(new BigDecimal(i + ""));
- }
- // 2!
- for (int i = 1; i <= 2; i++) {
- num2 = num2.multiply(new BigDecimal(i + ""));
- }
- // 52!
- for (int i = 1; i <= 52; i++) {
- num2 = num2.multiply(new BigDecimal(i + ""));
- }
- // Compute the numerator
- numerator = num1.divide(num2);
- // Numerator * e^(13L)
- BigDecimal e = new BigDecimal(Math.E);
- BigDecimal coefficient = new BigDecimal(Math.E);
- // If L < 0, raise to the absolute value of L and divide the result into one
- if (L < 0) {
- coefficient = e.pow(13 * Math.abs(L));
- coefficient = BigDecimal.valueOf(1).divide(coefficient, 200, RoundingMode.HALF_UP);
- } else {
- coefficient = e.pow(13 * L);
- }
- System.out.println("Coefficient: " + coefficient);
- numerator = numerator.multiply(new BigDecimal(coefficient + ""));
- System.out.println("Numerator*e^(13L): " + numerator + "\n");
- //////////////////////////////////////////////////
- ////////// Compute the sum from 0 to 15 of
- ////////// {(148!) / [(i)! * (94-i)! * (15-i)! * (39+i)!]} * e^(iL)
- ////////////////////////////////////////////////////////////////////////
- BigDecimal sum = new BigDecimal("0");
- // d1-d4 are the four denominator values in the combinatoric value
- BigDecimal d1;
- BigDecimal d2;
- BigDecimal d3;
- BigDecimal d4;
- BigDecimal dProduct = new BigDecimal("1");
- BigDecimal sumCoefficient = new BigDecimal("0"); // Represents E^(iL)
- // Compute the combinatoric value for each value of i and sum each result
- for (int i = 0; i <= 15; i++) {
- System.out.println("i = " + i);
- // Reset d1-d4
- d1 = new BigDecimal("1");
- d2 = new BigDecimal("1");
- d3 = new BigDecimal("1");
- d4 = new BigDecimal("1");
- // Compute i!
- if (i == 0) {
- d1 = new BigDecimal("1");
- } else {
- for (int j = 0; j < i; j++) {
- d1 = d1.multiply(new BigDecimal((j + 1) + ""));
- }
- }
- System.out.println("d1: " + d1);
- // Compute (94-i)!
- if ((94 - i) == 0) {
- d2 = new BigDecimal("1");
- } else {
- for (int j = 0; j < (94 - i); j++) {
- d2 = d2.multiply(new BigDecimal((j + 1) + ""));
- }
- }
- System.out.println("d2: " + d2);
- // Compute (15-i)!
- if ((15 - i) == 0) {
- d3 = new BigDecimal("1");
- } else {
- for (int j = 0; j < (15 - i); j++) {
- d3 = d3.multiply(new BigDecimal((j + 1) + ""));
- }
- }
- System.out.println("d3: " + d3);
- // Compute (39+i)!
- if ((39 + i) == 0) {
- d4 = new BigDecimal("1");
- } else {
- for (int j = 0; j < (39 + i); j++) {
- d4 = d4.multiply(new BigDecimal((j + 1) + ""));
- }
- }
- System.out.println("d4: " + d4);
- // Compute dProduct
- dProduct = new BigDecimal("1");
- dProduct = dProduct.multiply(d1);
- dProduct = dProduct.multiply(d2);
- dProduct = dProduct.multiply(d3);
- dProduct = dProduct.multiply(d4);
- System.out.println("Combinatoric Denominator: " + dProduct);
- // Compute e^(iL)
- sumCoefficient = new BigDecimal(Math.E);
- // If L is negative, raise sumCoefficient to the absolute value of the exponent and divide it into one
- if (L < 0) {
- sumCoefficient = sumCoefficient.pow(i * Math.abs(L));
- sumCoefficient = BigDecimal.valueOf(1).divide(sumCoefficient, 200, RoundingMode.HALF_UP);
- }
- System.out.println("E^il: " + sumCoefficient);
- // Change dProduct to represent the combinatoric value times the coefficient e^iL
- dProduct = dProduct.multiply(sumCoefficient);
- System.out.println("Denominator * E^iL: " + dProduct);
- // Add the current denominator value to the overall sum
- sum = sum.add(dProduct);
- System.out.println("Current sum: " + sum + "\n");
- }
- // Compute the outer numerator and store it in outerNumerator
- System.out.println("Numerator: " + numerator + " Divided by dProduct: " + dProduct);
- BigDecimal outerNumerator = numerator.divide(dProduct, 200, RoundingMode.HALF_EVEN);
- System.out.println("Outer Numerator: " + outerNumerator);
- // Compute 1/root(2pi) * 1/[e^((L^2)/2)]
- double exp = (double)L*(double)L/2;
- System.out.println("exp: " + exp);
- double coef = Math.pow(Math.E, exp);
- System.out.println("coef: " + coef);
- double ecoef = Math.sqrt(2 * (Math.PI));
- System.out.println("ecoef: " + ecoef);
- double product = coef * ecoef;
- System.out.println("product: " + product);
- // Divide whole product by 1 to invert
- product = 1 / product;
- System.out.println("product inverted: " + product);
- BigDecimal prod = new BigDecimal(product + "");
- System.out.println("Overall Numerator: " + prod.multiply(outerNumerator));
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement