Skip to content

Commit

Permalink
extend gcd and pairs
Browse files Browse the repository at this point in the history
  • Loading branch information
iTitus committed Dec 13, 2020
1 parent 9a82ad3 commit 14a0ac0
Show file tree
Hide file tree
Showing 10 changed files with 472 additions and 89 deletions.
48 changes: 48 additions & 0 deletions src/main/java/io/github/ititus/data/pair/IntIntPair.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package io.github.ititus.data.pair;

import io.github.ititus.data.ArrayUtil;

public final class IntIntPair {

private final int a;
private final int b;

private IntIntPair(int a, int b) {
this.a = a;
this.b = b;
}

public int a() {
return a;
}

public int b() {
return b;
}

@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (!(o instanceof IntIntPair)) {
return false;
}
IntIntPair that = (IntIntPair) o;
return a == that.a && b == that.b;
}

@Override
public int hashCode() {
return ArrayUtil.hash(a, b);
}

@Override
public String toString() {
return "<" + a + "," + b + ">";
}

public static IntIntPair of(int a, int b) {
return new IntIntPair(a, b);
}
}
48 changes: 48 additions & 0 deletions src/main/java/io/github/ititus/data/pair/LongLongPair.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package io.github.ititus.data.pair;

import io.github.ititus.data.ArrayUtil;

public final class LongLongPair {

private final long a;
private final long b;

private LongLongPair(long a, long b) {
this.a = a;
this.b = b;
}

public long a() {
return a;
}

public long b() {
return b;
}

@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (!(o instanceof LongLongPair)) {
return false;
}
LongLongPair that = (LongLongPair) o;
return a == that.a && b == that.b;
}

@Override
public int hashCode() {
return ArrayUtil.hash(a, b);
}

@Override
public String toString() {
return "<" + a + "," + b + ">";
}

public static LongLongPair of(long a, long b) {
return new LongLongPair(a, b);
}
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
package io.github.ititus.data;
package io.github.ititus.data.pair;

import java.util.Map;
import java.util.Objects;

public final class Pair<A, B> implements Printable {
public final class Pair<A, B> {

private final A a;
private final B b;
Expand All @@ -17,11 +16,19 @@ public static <A, B> Pair<A, B> of(A a, B b) {
return new Pair<>(a, b);
}

public A getA() {
public static IntIntPair of(int a, int b) {
return IntIntPair.of(a, b);
}

public static LongLongPair of(long a, long b) {
return LongLongPair.of(a, b);
}

public A a() {
return a;
}

public B getB() {
public B b() {
return b;
}

Expand All @@ -42,19 +49,8 @@ public int hashCode() {
return Objects.hash(a, b);
}

@Override
public String getPrefix() {
return "Pair";
}

@Override
public void getPrintableFields(Map<String, String> fields) {
fields.put("a", Objects.toString(a));
fields.put("b", Objects.toString(b));
}

@Override
public String toString() {
return Printable.toPrintableString(this);
return "<" + a + "," + b + ">";
}
}
91 changes: 54 additions & 37 deletions src/main/java/io/github/ititus/math/number/BigIntegerMath.java
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
package io.github.ititus.math.number;

import io.github.ititus.math.time.StopWatchStatistics;

import java.math.BigInteger;
import java.util.Map;
import java.util.NavigableMap;
import java.util.TreeMap;

import static java.math.BigInteger.ONE;
import static java.math.BigInteger.ZERO;

public final class BigIntegerMath {

public static final BigInteger THREE = of(3);
Expand All @@ -15,7 +16,6 @@ public final class BigIntegerMath {
public static final BigInteger MINUS_ONE = of(-1);

private static final NavigableMap<BigInteger, BigInteger> FACTORIAL_CACHE = new TreeMap<>();
private static final StopWatchStatistics gcdTime = StopWatchStatistics.arithmetic();

private BigIntegerMath() {
}
Expand All @@ -34,20 +34,20 @@ public static BigInteger of(String n) {

@SuppressWarnings("Duplicates")
public static BigInteger pow(BigInteger base, BigInteger exponent) {
if (base.equals(BigInteger.ONE) || exponent.signum() == 0) {
return BigInteger.ONE;
if (base.equals(ONE) || exponent.signum() == 0) {
return ONE;
} else if (base.signum() == 0) {
return BigInteger.ZERO;
return ZERO;
} else if (exponent.signum() < 0) {
throw new ArithmeticException();
}

BigInteger n = BigInteger.ONE;
BigInteger n = ONE;

while (exponent.signum() > 0) {
if (BigIntegerMath.isOdd(exponent)) {
n = n.multiply(base);
exponent = exponent.subtract(BigInteger.ONE);
exponent = exponent.subtract(ONE);
} else {
base = base.multiply(base);
exponent = exponent.shiftRight(1);
Expand All @@ -58,31 +58,48 @@ public static BigInteger pow(BigInteger base, BigInteger exponent) {
}

public static BigInteger gcd(BigInteger a, BigInteger b) {
//gcdTime.start();
BigInteger gcd;
if (a.signum() == 0) {
gcd = b.abs();
} else if (b.signum() == 0 || a.equals(b)) {
gcd = a.abs();
} else if (a.equals(BigInteger.ONE) || b.equals(BigInteger.ONE)) {
gcd = BigInteger.ONE;
} /*else if (!isProbablePrime(a, 10) && !isProbablePrime(a, 10)) {
gcd = a.gcd(b);
}*/ else { // a or b might be prime
gcd = a.gcd(b);
if (a.equals(ONE) || b.equals(ONE)) {
return ONE;
}
//System.out.println("gcd: " + gcdTime.stop());
return gcd;

return a.gcd(b);
}

public static BigInteger lcm(BigInteger a, BigInteger b) {
if (a.equals(BigInteger.ZERO) || b.equals(BigInteger.ZERO)) {
if (a.equals(ZERO) || b.equals(ZERO)) {
throw new ArithmeticException();
}

return a.divide(gcd(a, b)).multiply(b).abs();
}

public static ExtendedGdcBigIntegerResult extendedGcd(BigInteger a, BigInteger b) {
BigInteger old_r = a.abs();
BigInteger r = b.abs();
BigInteger old_s = ONE;
BigInteger s = ZERO;
BigInteger old_t = ZERO;
BigInteger t = ONE;

while (r.signum() != 0) {
BigInteger q = old_r.divide(r);

BigInteger tmp = r;
r = old_r.subtract(q.multiply(r));
old_r = tmp;

tmp = s;
s = old_s.subtract(q.multiply(s));
old_s = tmp;

tmp = t;
t = old_t.subtract(q.multiply(t));
old_t = tmp;
}

return new ExtendedGdcBigIntegerResult(old_r, old_s, old_t);
}

public static boolean isOdd(BigInteger n) {
return n.testBit(0);
}
Expand All @@ -102,7 +119,7 @@ public static boolean isProbablePrime(BigInteger n, int certainty) {
public static boolean isPrime(BigInteger n) {
if (n.signum() <= 0) {
throw new IllegalArgumentException();
} else if (n.equals(BigInteger.ONE)) {
} else if (n.equals(ONE)) {
return false;
} else if (isEven(n)) {
return false;
Expand All @@ -112,7 +129,7 @@ public static boolean isPrime(BigInteger n) {
}

public static boolean isPowerOf2(BigInteger n) {
return n.signum() != 0 && n.and(n.subtract(BigInteger.ONE)).signum() == 0;
return n.signum() != 0 && n.and(n.subtract(ONE)).signum() == 0;
}

public static boolean isInt(BigInteger n) {
Expand All @@ -125,25 +142,25 @@ public static boolean isLong(BigInteger n) {

public static BigInteger binomial(BigInteger n, BigInteger k) {
if (k.signum() < 0 || k.compareTo(n) > 0) {
return BigInteger.ZERO;
return ZERO;
} else if (k.signum() == 0 || n.equals(k)) {
return BigInteger.ONE;
return ONE;
}

k = k.min(n.subtract(k));

BigInteger result = BigInteger.ONE;
BigInteger product = BigInteger.ONE;
for (BigInteger i = BigInteger.ONE; i.compareTo(k) <= 0; i = i.add(BigInteger.ONE)) {
BigInteger result = ONE;
BigInteger product = ONE;
for (BigInteger i = ONE; i.compareTo(k) <= 0; i = i.add(ONE)) {
product = product.multiply(i);
result = result.multiply(n.subtract(k).add(i));
}
return result.divide(product);
}

public static BigInteger multinomial(BigInteger... n) {
BigInteger result = BigInteger.ONE;
BigInteger sum = BigInteger.ZERO;
BigInteger result = ONE;
BigInteger sum = ZERO;
for (BigInteger n_i : n) {
sum = sum.add(n_i);
result = result.multiply(binomial(sum, n_i));
Expand All @@ -154,13 +171,13 @@ public static BigInteger multinomial(BigInteger... n) {
public static BigInteger factorial(BigInteger n) {
if (n.signum() < 0) {
throw new ArithmeticException();
} else if (n.signum() == 0 || n.equals(BigInteger.ONE)) {
return BigInteger.ONE;
} else if (n.signum() == 0 || n.equals(ONE)) {
return ONE;
}

Map.Entry<BigInteger, BigInteger> maxEntry = FACTORIAL_CACHE.lastEntry();
BigInteger maxN = maxEntry != null ? maxEntry.getKey() : BigInteger.ONE;
BigInteger result = maxEntry != null ? maxEntry.getValue() : BigInteger.ONE;
BigInteger maxN = maxEntry != null ? maxEntry.getKey() : ONE;
BigInteger result = maxEntry != null ? maxEntry.getValue() : ONE;

if (maxN.equals(n)) {
return result;
Expand All @@ -169,7 +186,7 @@ public static BigInteger factorial(BigInteger n) {
}

while (maxN.compareTo(n) < 0) {
maxN = maxN.add(BigInteger.ONE);
maxN = maxN.add(ONE);
result = result.multiply(maxN);
FACTORIAL_CACHE.put(maxN, result);
}
Expand Down
35 changes: 1 addition & 34 deletions src/main/java/io/github/ititus/math/number/BigRationalMath.java
Original file line number Diff line number Diff line change
@@ -1,12 +1,9 @@
package io.github.ititus.math.number;

import io.github.ititus.math.function.PowerSeriesCalculator;
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;

import static io.github.ititus.math.number.BigRational.of;
import static io.github.ititus.math.number.BigRationalConstants.*;
Expand Down Expand Up @@ -100,38 +97,8 @@ public static BigRational ln(BigRational x) {
return ZERO;
}

StopWatch w = StopWatch.createRunning();

BigRational lnFast = PowerSeriesCalculator.lnFast(x);

Duration d = w.stop();
System.out.println("lnFast: " + DurationFormatter.formatMillis(d));
w.start();

BigRational exp = lnFast.exp();

d = w.stop();
System.out.println("exp: " + DurationFormatter.formatMillis(d));
w.start();

BigRational newX = x.divide(exp);

d = w.stop();
System.out.println("div: " + DurationFormatter.formatMillis(d));
w.start();

BigRational ln = PowerSeriesCalculator.ln(newX);

d = w.stop();
System.out.println("ln: " + DurationFormatter.formatMillis(d));
w.start();

BigRational realLn = lnFast.add(ln);

d = w.stop();
System.out.println("add: " + DurationFormatter.formatMillis(d));

return realLn;
return lnFast.add(PowerSeriesCalculator.ln(x.divide(lnFast.exp())));
}

public static BigRational sin(BigRational x) {
Expand Down
Loading

0 comments on commit 14a0ac0

Please sign in to comment.