Skip to content

Commit

Permalink
Merge pull request #5 from iTitus/value-interface
Browse files Browse the repository at this point in the history
Abstract QuantityValue and fully switch to BigRational
  • Loading branch information
iTitus authored Nov 10, 2020
2 parents 85f69b1 + df40500 commit e511bdd
Show file tree
Hide file tree
Showing 32 changed files with 791 additions and 135 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ protected BigRational getCoefficient(BigInteger n) {
return BigRationalConstants.ZERO;
}

return BigRational.of(BigInteger.ONE, n);
return BigRational.ofInv(n);
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package io.github.ititus.math.number;

import java.math.BigDecimal;

public final class BigDecimalConstants {

public static final BigDecimal TWO = new BigDecimal(2);
public static final BigDecimal ONE_OVER_TWO = new BigDecimal("0.5");

private BigDecimalConstants() {
}
}
162 changes: 144 additions & 18 deletions src/main/java/io/github/ititus/math/number/BigRational.java
Original file line number Diff line number Diff line change
Expand Up @@ -86,12 +86,140 @@ public static BigRational of(BigDecimal d) {
int scale = d.scale();
BigInteger scalar = BigInteger.TEN.pow(Math.abs(scale));

if (scale <= 0) {
of(unscaled.multiply(scalar));
if (scale < 0) {
return of(unscaled.multiply(scalar));
}
return of(unscaled, scalar);
}

public static BigRational ofInv(int n) {
return of(n).inverse();
}

public static BigRational ofInv(long n) {
return of(n).inverse();
}

public static BigRational ofInv(BigInteger n) {
return of(n).inverse();
}

public static BigRational ofInv(float f) {
return of(f).inverse();
}

public static BigRational ofInv(double d) {
return of(d).inverse();
}

public static BigRational ofInv(BigDecimal d) {
return of(d).inverse();
}

public static BigRational ofExp(int scale, int exponent) {
return of(scale).multiply(BigRationalConstants.TEN.pow(exponent));
}

public static BigRational ofExp(int scale, long exponent) {
return of(scale).multiply(BigRationalConstants.TEN.pow(exponent));
}

public static BigRational ofExp(int scale, BigInteger exponent) {
return of(scale).multiply(BigRationalConstants.TEN.pow(exponent));
}

public static BigRational ofExp(long scale, int exponent) {
return of(scale).multiply(BigRationalConstants.TEN.pow(exponent));
}

public static BigRational ofExp(long scale, long exponent) {
return of(scale).multiply(BigRationalConstants.TEN.pow(exponent));
}

public static BigRational ofExp(long scale, BigInteger exponent) {
return of(scale).multiply(BigRationalConstants.TEN.pow(exponent));
}

public static BigRational ofExp(BigInteger scale, int exponent) {
return of(scale).multiply(BigRationalConstants.TEN.pow(exponent));
}

public static BigRational ofExp(BigInteger scale, long exponent) {
return of(scale).multiply(BigRationalConstants.TEN.pow(exponent));
}

public static BigRational ofExp(BigInteger scale, BigInteger exponent) {
return of(scale).multiply(BigRationalConstants.TEN.pow(exponent));
}

public static BigRational ofExp(float scale, int exponent) {
return of(scale).multiply(BigRationalConstants.TEN.pow(exponent));
}

public static BigRational ofExp(float scale, long exponent) {
return of(scale).multiply(BigRationalConstants.TEN.pow(exponent));
}

public static BigRational ofExp(float scale, BigInteger exponent) {
return of(scale).multiply(BigRationalConstants.TEN.pow(exponent));
}

public static BigRational ofExp(double scale, int exponent) {
return of(scale).multiply(BigRationalConstants.TEN.pow(exponent));
}

public static BigRational ofExp(double scale, long exponent) {
return of(scale).multiply(BigRationalConstants.TEN.pow(exponent));
}

public static BigRational ofExp(double scale, BigInteger exponent) {
return of(scale).multiply(BigRationalConstants.TEN.pow(exponent));
}

public static BigRational ofExp(BigDecimal scale, int exponent) {
return of(scale).multiply(BigRationalConstants.TEN.pow(exponent));
}

public static BigRational ofExp(BigDecimal scale, long exponent) {
return of(scale).multiply(BigRationalConstants.TEN.pow(exponent));
}

public static BigRational ofExp(BigDecimal scale, BigInteger exponent) {
return of(scale).multiply(BigRationalConstants.TEN.pow(exponent));
}

public static BigRational of(int numerator, int denominator) {
return of(BigIntegerMath.of(numerator), BigIntegerMath.of(denominator));
}

public static BigRational of(int numerator, long denominator) {
return of(BigIntegerMath.of(numerator), BigIntegerMath.of(denominator));
}

public static BigRational of(int numerator, BigInteger denominator) {
return of(BigIntegerMath.of(numerator), denominator);
}

public static BigRational of(long numerator, int denominator) {
return of(BigIntegerMath.of(numerator), BigIntegerMath.of(denominator));
}

public static BigRational of(long numerator, long denominator) {
return of(BigIntegerMath.of(numerator), BigIntegerMath.of(denominator));
}

public static BigRational of(long numerator, BigInteger denominator) {
return of(BigIntegerMath.of(numerator), denominator);
}

public static BigRational of(BigInteger numerator, int denominator) {
return of(numerator, BigIntegerMath.of(denominator));
}

public static BigRational of(BigInteger numerator, long denominator) {
return of(numerator, BigIntegerMath.of(denominator));
}

@SuppressWarnings("Duplicates")
public static BigRational of(BigInteger numerator, BigInteger denominator) {
if (denominator.equals(BigInteger.ZERO)) {
Expand All @@ -117,22 +245,6 @@ public static BigRational of(BigInteger numerator, BigInteger denominator) {

if (r.equals(MINUS_ONE)) {
return MINUS_ONE;
} else if (r.equals(TWO)) {
return TWO;
} else if (r.equals(THREE)) {
return THREE;
} else if (r.equals(FOUR)) {
return FOUR;
} else if (r.equals(TEN)) {
return TEN;
} else if (r.equals(ONE_OVER_TWO)) {
return ONE_OVER_TWO;
} else if (r.equals(ONE_OVER_THREE)) {
return ONE_OVER_THREE;
} else if (r.equals(THREE_OVER_FOUR)) {
return THREE_OVER_FOUR;
} else if (r.equals(FIVE_OVER_FOUR)) {
return FIVE_OVER_FOUR;
}

return r;
Expand Down Expand Up @@ -295,6 +407,20 @@ public BigRational pow(BigInteger exponent) {
return BigRationalMath.pow(this, exponent);
}

public BigRational pow(BigDecimal exponent) {
if (exponent.signum() == 0) {
return ONE;
} else if (isZero()) {
return ZERO;
} else if (exponent.equals(BigDecimal.ONE)) {
return this;
} else if (exponent.equals(BigDecimalConstants.TWO)) {
return squared();
}

return BigRationalMath.pow(this, exponent);
}

public BigRational pow(BigRational exponent) {
if (exponent.isZero()) {
return ONE;
Expand Down
19 changes: 19 additions & 0 deletions src/main/java/io/github/ititus/math/number/BigRationalMath.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import io.github.ititus.math.time.DurationFormatter;
import io.github.ititus.math.time.StopWatch;

import java.math.BigDecimal;
import java.math.BigInteger;
import java.time.Duration;

Expand Down Expand Up @@ -40,6 +41,24 @@ public static BigRational pow(BigRational base, BigInteger exponent) {
return r;
}

public static BigRational pow(BigRational base, BigDecimal exponent) {
if (base.isOne() || exponent.signum() == 0) {
return ONE;
} else if (base.isZero()) {
return ZERO;
} else if (exponent.equals(BigDecimal.ONE)) {
return base;
} else if (exponent.signum() < 0) {
return pow(base, exponent.negate()).inverse();
}

if (exponent.equals(BigDecimalConstants.ONE_OVER_TWO)) {
return base.sqrt();
}

return exp(BigRational.of(exponent).multiply(ln(base)));
}

public static BigRational pow(BigRational base, BigRational exponent) {
if (base.isOne() || exponent.isZero()) {
return ONE;
Expand Down
14 changes: 0 additions & 14 deletions src/main/java/io/github/ititus/si/quantity/AbstractQuantity.java

This file was deleted.

21 changes: 11 additions & 10 deletions src/main/java/io/github/ititus/si/quantity/Constants.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,29 +2,30 @@

import io.github.ititus.si.quantity.type.*;

import static io.github.ititus.math.number.BigRational.ofExp;
import static io.github.ititus.si.unit.Units.*;

public final class Constants {

/* Defined (exact) constants */

// Length
public static final Quantity<Speed> SPEED_OF_LIGHT = METRES_PER_SECOND.get(299_792_458.0);
public static final Quantity<Speed> SPEED_OF_LIGHT = METRES_PER_SECOND.get(299_792_458);

// Mass
public static final Quantity<?> PLANCK_CONSTANT = JOULE.multiply(SECOND).get(6.626_070_15e-34);
public static final Quantity<?> PLANCK_CONSTANT = JOULE.multiply(SECOND).get(ofExp(662_607_015, -42));

// Time
public static final Quantity<Frequency> HYPERFINE_TRANSITION_FREQUENCY_OF_CAESIUM = HERTZ.get(9_192_631_770.0);
public static final Quantity<Frequency> HYPERFINE_TRANSITION_FREQUENCY_OF_CAESIUM = HERTZ.get(9_192_631_770L);

// Electric Current
public static final Quantity<ElectricCharge> ELEMENTARY_CHARGE = COULOMB.get(1.602_176_634e-19);
public static final Quantity<ElectricCharge> ELEMENTARY_CHARGE = COULOMB.get(ofExp(1_602_176_634, -28));

// Thermodynamic Temperature
public static final Quantity<?> BOLTZMANN_CONSTANT = JOULE.divide(KELVIN).get(1.380_649_76e-23);
public static final Quantity<?> BOLTZMANN_CONSTANT = JOULE.divide(KELVIN).get(ofExp(1_380_649_76, -31));

// Amount of Substance
public static final Quantity<?> AVOGADRO_CONSTANT = MOLE.inverse().get(6.022_140_76e23);
public static final Quantity<?> AVOGADRO_CONSTANT = MOLE.inverse().get(ofExp(6_022_140_76, 15));

// Luminous Intensity
public static final Quantity<LuminousEfficacy> LUMINOUS_EFFICACY_OF_540_THZ_RADIATION = LUMEN_PER_WATT.get(683);
Expand All @@ -33,15 +34,15 @@ public final class Constants {
/* Measured constants */

public static final Quantity<?> GRAVITATIONAL_CONSTANT =
METRE.pow(3).divide(KILOGRAM.multiply(SECOND.pow(2))).get(6.674_30e-11);
METRE.pow(3).divide(KILOGRAM.multiply(SECOND.pow(2))).get(ofExp(667_430, -16));

public static final Quantity<ElectricPermittivity> VACUUM_ELECTRIC_PERMITTIVITY =
FARAD_PER_METRE.get(8.854_187_812_8e-12);
FARAD_PER_METRE.get(ofExp(88_541_878_128L, -22));

public static final Quantity<MagneticPermeability> VACUUM_MAGNETIC_PERMEABILITY =
HENRY_PER_METRE.get(1.256_637_062_12e-6);
HENRY_PER_METRE.get(ofExp(125_663_706_212L, -17));

public static final Quantity<Mass> ELECTRON_MASS = KILOGRAM.get(9.109_383_701_5e-31);
public static final Quantity<Mass> ELECTRON_MASS = KILOGRAM.get(ofExp(91_093_837_015L, -41));

private Constants() {
}
Expand Down
Loading

0 comments on commit e511bdd

Please sign in to comment.