org.psjava.ds.numbersystrem.FractionNumberSystem Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of psjava Show documentation
Show all versions of psjava Show documentation
Problem Solving Library for Java
The newest version!
package org.psjava.ds.numbersystrem;
import java.util.Comparator;
import org.psjava.algo.math.numbertheory.GCD;
public class FractionNumberSystem implements DivisableNumberSystem>, Comparator> {
public static FractionNumberSystem newInstance(IntegerDivisableNumberSystem subSystem) {
return new FractionNumberSystem(subSystem);
}
private final IntegerDivisableNumberSystem ns;
private FractionNumberSystem(IntegerDivisableNumberSystem subSystem) {
this.ns = subSystem;
}
@Override
public Fraction getOne() {
return Fraction.valueOf(ns.getOne(), ns.getOne());
}
@Override
public Fraction getZero() {
return Fraction.valueOf(ns.getZero(), ns.getOne());
}
@Override
public Fraction getByInt(int v) {
return Fraction.valueOf(ns.getByInt(v), ns.getOne());
}
@Override
public Fraction add(Fraction v1, Fraction v2) {
T num = ns.add(ns.multiply(v1.numerator, v2.denominator), ns.multiply(v2.numerator, v1.denominator));
T denom = ns.multiply(v1.denominator, v2.denominator);
return reduce(num, denom);
}
@Override
public Fraction subtract(Fraction minuend, Fraction subtrahend) {
T num = ns.subtract(ns.multiply(minuend.numerator, subtrahend.denominator), ns.multiply(subtrahend.numerator, minuend.denominator));
T denom = ns.multiply(minuend.denominator, subtrahend.denominator);
return reduce(num, denom);
}
@Override
public Fraction multiply(Fraction v1, Fraction v2) {
T num = ns.multiply(v1.numerator, v2.numerator);
T denom = ns.multiply(v1.denominator, v2.denominator);
return reduce(num, denom);
}
@Override
public boolean areEqual(Fraction o1, Fraction o2) {
T t1 = ns.multiply(o1.numerator, o2.denominator);
T t2 = ns.multiply(o2.numerator, o1.denominator);
return ns.areEqual(t1, t2);
}
@Override
public Fraction divide(Fraction dividend, Fraction divisor) {
T num = ns.multiply(dividend.numerator, divisor.denominator);
T denom = ns.multiply(dividend.denominator, divisor.numerator);
return reduce(num, denom);
}
@Override
public int getSign(Fraction v) {
return ns.getSign(v.numerator) * ns.getSign(v.denominator);
}
@Override
public boolean isNegative(Fraction v) {
return getSign(v) < 0;
}
@Override
public boolean isOne(Fraction v) {
return ns.areEqual(v.numerator, v.denominator);
}
@Override
public boolean isPositive(Fraction v) {
return getSign(v) > 0;
}
@Override
public boolean isZero(Fraction v) {
return ns.isZero(v.numerator);
}
@Override
public int compare(Fraction o1, Fraction o2) {
o1 = reduce(o1);
o2 = reduce(o2);
T v1 = ns.multiply(o1.numerator, o2.denominator);
T v2 = ns.multiply(o2.numerator, o1.denominator);
return ns.compare(v1, v2);
}
public Fraction reduce(Fraction v) {
return reduce(v.numerator, v.denominator);
}
private Fraction reduce(T num, T denom) {
if (ns.isNegative(denom)) {
num = AddInvert.calc(ns, num);
denom = AddInvert.calc(ns, denom);
}
T gcd = GCD.gcd(ns, num, denom);
num = ns.integerDivide(num, gcd);
denom = ns.integerDivide(denom, gcd);
return Fraction.valueOf(num, denom);
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy