From 1d6957c4d94aebdb08942dd09168a5d8b62a2d79 Mon Sep 17 00:00:00 2001 From: Arthur Drobot Date: Tue, 8 Aug 2017 14:53:34 -0700 Subject: [PATCH 1/6] Write a RationalNumber class. --- build.gradle | 1 + .../nwapw/abacus/number/PreciseNumber.java | 10 ++ .../nwapw/abacus/number/RationalNumber.java | 115 ++++++++++++++++++ 3 files changed, 126 insertions(+) mode change 100644 => 100755 build.gradle create mode 100755 src/main/java/org/nwapw/abacus/number/RationalNumber.java diff --git a/build.gradle b/build.gradle old mode 100644 new mode 100755 index 1a408bc..cd5a65d --- a/build.gradle +++ b/build.gradle @@ -11,6 +11,7 @@ repositories { } dependencies { + compile group: 'org.apache.commons', name: 'commons-math3', version: '3.6.1' compile 'com.moandjiezana.toml:toml4j:0.7.1' compile "org.jetbrains.kotlin:kotlin-stdlib-jre8" testCompile 'junit:junit:4.12' diff --git a/src/main/java/org/nwapw/abacus/number/PreciseNumber.java b/src/main/java/org/nwapw/abacus/number/PreciseNumber.java index 094e5a9..2d515fc 100755 --- a/src/main/java/org/nwapw/abacus/number/PreciseNumber.java +++ b/src/main/java/org/nwapw/abacus/number/PreciseNumber.java @@ -1,6 +1,7 @@ package org.nwapw.abacus.number; import java.math.BigDecimal; +import java.math.BigInteger; import java.math.MathContext; /** @@ -61,6 +62,15 @@ public class PreciseNumber extends NumberInterface { this.value = value; } + /** + * Constructs a precise number from the given BigInteger. + * + * @param value a BigInteger object representing the value of the number. + */ + public PreciseNumber(BigInteger value) { + this.value = new BigDecimal(value); + } + @Override public int getMaxPrecision() { return internalContext.getPrecision(); diff --git a/src/main/java/org/nwapw/abacus/number/RationalNumber.java b/src/main/java/org/nwapw/abacus/number/RationalNumber.java new file mode 100755 index 0000000..62653e6 --- /dev/null +++ b/src/main/java/org/nwapw/abacus/number/RationalNumber.java @@ -0,0 +1,115 @@ +package org.nwapw.abacus.number; + +import org.apache.commons.math3.fraction.BigFraction; +import org.apache.commons.math3.fraction.Fraction; + +import java.math.BigInteger; + +public class RationalNumber extends NumberInterface{ + + static final RationalNumber ONE = new RationalNumber(BigFraction.ONE); + + /** + * The value of the number. + */ + private BigFraction value; + + /** + * Constructs a new instance with the given value. + * @param value + */ + public RationalNumber(BigFraction value){ + this.value = value; + } + + @Override + public int getMaxPrecision() { + return 0; + } + + @Override + protected NumberInterface multiplyInternal(NumberInterface multiplier) { + return new RationalNumber(value.multiply(((RationalNumber)multiplier).value)); + } + + @Override + protected NumberInterface divideInternal(NumberInterface divisor) { + return new RationalNumber(value.divide(((RationalNumber)divisor).value)); + } + + @Override + protected NumberInterface addInternal(NumberInterface summand) { + return new RationalNumber(value.add(((RationalNumber)summand).value)); + } + + @Override + protected NumberInterface subtractInternal(NumberInterface subtrahend) { + return new RationalNumber(value.subtract(((RationalNumber)subtrahend).value)); + } + + @Override + protected NumberInterface negateInternal() { + return new RationalNumber(value.negate()); + } + + @Override + protected NumberInterface intPowInternal(int exponent) { + return new RationalNumber(value.pow(exponent)); + } + + @Override + public int compareTo(NumberInterface number) { + return value.compareTo(((RationalNumber)number).value); + } + + @Override + public int signum() { + return value.getNumerator().signum(); + } + + @Override + protected NumberInterface ceilingInternal() { + if(value.getNumeratorAsInt() != 1){ + return floorInternal().add(ONE); + } + return this; + } + + @Override + protected NumberInterface floorInternal() { + BigInteger floor = value.bigDecimalValue().toBigInteger(); + if(value.compareTo(BigFraction.ZERO) < 0 && value.getDenominatorAsInt() != 1){ + floor = floor.subtract(BigInteger.ONE); + } + return new RationalNumber(new BigFraction(floor)); + } + + @Override + protected NumberInterface fractionalPartInternal() { + return this.subtractInternal(floorInternal()); + } + + @Override + public int intValue() { + return 0; + } + + @Override + protected NumberInterface promoteToInternal(Class toClass) { + return null; + } + + @Override + public NumberInterface getMaxError() { + return toPreciseNumber().getMaxError(); + } + + @Override + public String toString(){ + return toPreciseNumber().toString(); + } + + PreciseNumber toPreciseNumber(){ + return (PreciseNumber) new PreciseNumber(value.getNumerator()).divideInternal(new PreciseNumber(value.getDenominator())); + } +} From ca6d8d2ba2689db3a45cacdec2dc7732ae8c5df3 Mon Sep 17 00:00:00 2001 From: Arthur Drobot Date: Wed, 9 Aug 2017 12:47:43 -0700 Subject: [PATCH 2/6] Add arcsin. --- .../nwapw/abacus/plugin/StandardPlugin.java | 35 +++++++++++++++++++ 1 file changed, 35 insertions(+) diff --git a/src/main/java/org/nwapw/abacus/plugin/StandardPlugin.java b/src/main/java/org/nwapw/abacus/plugin/StandardPlugin.java index d30c265..cad9445 100755 --- a/src/main/java/org/nwapw/abacus/plugin/StandardPlugin.java +++ b/src/main/java/org/nwapw/abacus/plugin/StandardPlugin.java @@ -451,6 +451,40 @@ public class StandardPlugin extends Plugin { } }; + /** + * The arcsin function (return type in radians). + * @param manager + */ + public final Function functionArcsin = new Function() { + @Override + protected boolean matchesParams(NumberInterface[] params) { + return params.length == 1 + && FUNCTION_ABS.apply(params[0]).compareTo(fromInt(params[0].getClass(), 1)) <= 0; + } + + @Override + protected NumberInterface applyInternal(NumberInterface[] params) { + if(FUNCTION_ABS.apply(params[0]).compareTo(new NaiveNumber(0.8).promoteTo(params[0].getClass())) >= 0){ + NumberInterface[] newParams = {FUNCTION_SQRT.apply(fromInt(params[0].getClass(), 1).subtract(params[0].multiply(params[0])))}; + return piFor(params[0].getClass()).divide(fromInt(params[0].getClass(), 2)) + .subtract(applyInternal(newParams)).multiply(fromInt(params[0].getClass(), params[0].signum())); + } + NumberInterface currentTerm = params[0], sum = currentTerm, + multiplier = currentTerm.multiply(currentTerm), summandBound = sum.getMaxError().multiply(fromInt(sum.getClass(), 1).subtract(multiplier)), + power = currentTerm, coefficient = fromInt(params[0].getClass(), 1); + int exponent = 1; + while(FUNCTION_ABS.apply(currentTerm).compareTo(summandBound) > 0){ + exponent += 2; + power = power.multiply(multiplier); + coefficient = coefficient.multiply(fromInt(params[0].getClass(), exponent-2)) + .divide(fromInt(params[0].getClass(), exponent - 1)); + currentTerm = power.multiply(coefficient).divide(fromInt(power.getClass(), exponent)); + sum = sum.add(currentTerm); + } + return sum; + } + }; + public StandardPlugin(PluginManager manager) { super(manager); } @@ -568,6 +602,7 @@ public class StandardPlugin extends Plugin { registerFunction("sec", functionSec); registerFunction("csc", functionCsc); registerFunction("cot", functionCot); + registerFunction("arcsin", functionArcsin); } @Override From efe76a6fdc4c4a9245aa4ef632deec22eed6529a Mon Sep 17 00:00:00 2001 From: Arthur Drobot Date: Wed, 9 Aug 2017 15:08:54 -0700 Subject: [PATCH 3/6] Add arccos. --- .../nwapw/abacus/plugin/StandardPlugin.java | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/src/main/java/org/nwapw/abacus/plugin/StandardPlugin.java b/src/main/java/org/nwapw/abacus/plugin/StandardPlugin.java index cad9445..78d457b 100755 --- a/src/main/java/org/nwapw/abacus/plugin/StandardPlugin.java +++ b/src/main/java/org/nwapw/abacus/plugin/StandardPlugin.java @@ -453,7 +453,6 @@ public class StandardPlugin extends Plugin { /** * The arcsin function (return type in radians). - * @param manager */ public final Function functionArcsin = new Function() { @Override @@ -485,6 +484,22 @@ public class StandardPlugin extends Plugin { } }; + /** + * The arccos function. + */ + public final Function functionArccos = new Function() { + @Override + protected boolean matchesParams(NumberInterface[] params) { + return params.length == 1 && FUNCTION_ABS.apply(params[0]).compareTo(fromInt(params[0].getClass(), 1)) <= 0; + } + + @Override + protected NumberInterface applyInternal(NumberInterface[] params) { + return piFor(params[0].getClass()).divide(fromInt(params[0].getClass(), 2)) + .subtract(functionArcsin.apply(params)); + } + }; + public StandardPlugin(PluginManager manager) { super(manager); } @@ -603,6 +618,7 @@ public class StandardPlugin extends Plugin { registerFunction("csc", functionCsc); registerFunction("cot", functionCot); registerFunction("arcsin", functionArcsin); + registerFunction("arccos", functionArccos); } @Override From 1f0e6a7ce452497168d58de2b85cfde785130caf Mon Sep 17 00:00:00 2001 From: Arthur Drobot Date: Wed, 9 Aug 2017 15:57:02 -0700 Subject: [PATCH 4/6] Add arccsc. --- .../org/nwapw/abacus/plugin/StandardPlugin.java | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/src/main/java/org/nwapw/abacus/plugin/StandardPlugin.java b/src/main/java/org/nwapw/abacus/plugin/StandardPlugin.java index 78d457b..3e3c2d7 100755 --- a/src/main/java/org/nwapw/abacus/plugin/StandardPlugin.java +++ b/src/main/java/org/nwapw/abacus/plugin/StandardPlugin.java @@ -500,6 +500,22 @@ public class StandardPlugin extends Plugin { } }; + /** + * The arccsc function. + */ + public final Function functionArccsc = new Function() { + @Override + protected boolean matchesParams(NumberInterface[] params) { + return params.length == 1 && FUNCTION_ABS.apply(params[0]).compareTo(fromInt(params[0].getClass(), 1)) >= 0; + } + + @Override + protected NumberInterface applyInternal(NumberInterface[] params) { + NumberInterface[] reciprocalParamArr = {fromInt(params[0].getClass(), 1).divide(params[0])}; + return functionArcsin.apply(reciprocalParamArr); + } + }; + public StandardPlugin(PluginManager manager) { super(manager); } @@ -619,6 +635,7 @@ public class StandardPlugin extends Plugin { registerFunction("cot", functionCot); registerFunction("arcsin", functionArcsin); registerFunction("arccos", functionArccos); + registerFunction("arccsc", functionArccsc); } @Override From 4226df72f5a733532b68ccc5626855895e7b2142 Mon Sep 17 00:00:00 2001 From: Arthur Drobot Date: Wed, 9 Aug 2017 16:04:26 -0700 Subject: [PATCH 5/6] Add arcsec. Adjust some comments. --- .../nwapw/abacus/plugin/StandardPlugin.java | 23 ++++++++++++++++--- 1 file changed, 20 insertions(+), 3 deletions(-) diff --git a/src/main/java/org/nwapw/abacus/plugin/StandardPlugin.java b/src/main/java/org/nwapw/abacus/plugin/StandardPlugin.java index 3e3c2d7..1fee257 100755 --- a/src/main/java/org/nwapw/abacus/plugin/StandardPlugin.java +++ b/src/main/java/org/nwapw/abacus/plugin/StandardPlugin.java @@ -452,7 +452,7 @@ public class StandardPlugin extends Plugin { }; /** - * The arcsin function (return type in radians). + * The arcsine function (return type in radians). */ public final Function functionArcsin = new Function() { @Override @@ -485,7 +485,7 @@ public class StandardPlugin extends Plugin { }; /** - * The arccos function. + * The arccosine function. */ public final Function functionArccos = new Function() { @Override @@ -501,7 +501,7 @@ public class StandardPlugin extends Plugin { }; /** - * The arccsc function. + * The arccosecant function. */ public final Function functionArccsc = new Function() { @Override @@ -516,6 +516,22 @@ public class StandardPlugin extends Plugin { } }; + /** + * The arcsecant function. + */ + public final Function functionArcsec = new Function() { + @Override + protected boolean matchesParams(NumberInterface[] params) { + return params.length == 1 && FUNCTION_ABS.apply(params[0]).compareTo(fromInt(params[0].getClass(), 1)) >= 0; + } + + @Override + protected NumberInterface applyInternal(NumberInterface[] params) { + NumberInterface[] reciprocalParamArr = {fromInt(params[0].getClass(), 1).divide(params[0])}; + return functionArccos.apply(reciprocalParamArr); + } + }; + public StandardPlugin(PluginManager manager) { super(manager); } @@ -636,6 +652,7 @@ public class StandardPlugin extends Plugin { registerFunction("arcsin", functionArcsin); registerFunction("arccos", functionArccos); registerFunction("arccsc", functionArccsc); + registerFunction("arcsec", functionArcsec); } @Override From fee9f091fdb65cd0863519ca5ea158236d37746f Mon Sep 17 00:00:00 2001 From: Arthur Drobot Date: Wed, 9 Aug 2017 18:43:52 -0700 Subject: [PATCH 6/6] Revert "Write a RationalNumber class." This reverts commit f3942b6760f9a6a6e35536e027a9f36177783de9. --- build.gradle | 1 - .../nwapw/abacus/number/PreciseNumber.java | 10 -- .../nwapw/abacus/number/RationalNumber.java | 115 ------------------ 3 files changed, 126 deletions(-) mode change 100755 => 100644 build.gradle delete mode 100755 src/main/java/org/nwapw/abacus/number/RationalNumber.java diff --git a/build.gradle b/build.gradle old mode 100755 new mode 100644 index cd5a65d..1a408bc --- a/build.gradle +++ b/build.gradle @@ -11,7 +11,6 @@ repositories { } dependencies { - compile group: 'org.apache.commons', name: 'commons-math3', version: '3.6.1' compile 'com.moandjiezana.toml:toml4j:0.7.1' compile "org.jetbrains.kotlin:kotlin-stdlib-jre8" testCompile 'junit:junit:4.12' diff --git a/src/main/java/org/nwapw/abacus/number/PreciseNumber.java b/src/main/java/org/nwapw/abacus/number/PreciseNumber.java index 2d515fc..094e5a9 100755 --- a/src/main/java/org/nwapw/abacus/number/PreciseNumber.java +++ b/src/main/java/org/nwapw/abacus/number/PreciseNumber.java @@ -1,7 +1,6 @@ package org.nwapw.abacus.number; import java.math.BigDecimal; -import java.math.BigInteger; import java.math.MathContext; /** @@ -62,15 +61,6 @@ public class PreciseNumber extends NumberInterface { this.value = value; } - /** - * Constructs a precise number from the given BigInteger. - * - * @param value a BigInteger object representing the value of the number. - */ - public PreciseNumber(BigInteger value) { - this.value = new BigDecimal(value); - } - @Override public int getMaxPrecision() { return internalContext.getPrecision(); diff --git a/src/main/java/org/nwapw/abacus/number/RationalNumber.java b/src/main/java/org/nwapw/abacus/number/RationalNumber.java deleted file mode 100755 index 62653e6..0000000 --- a/src/main/java/org/nwapw/abacus/number/RationalNumber.java +++ /dev/null @@ -1,115 +0,0 @@ -package org.nwapw.abacus.number; - -import org.apache.commons.math3.fraction.BigFraction; -import org.apache.commons.math3.fraction.Fraction; - -import java.math.BigInteger; - -public class RationalNumber extends NumberInterface{ - - static final RationalNumber ONE = new RationalNumber(BigFraction.ONE); - - /** - * The value of the number. - */ - private BigFraction value; - - /** - * Constructs a new instance with the given value. - * @param value - */ - public RationalNumber(BigFraction value){ - this.value = value; - } - - @Override - public int getMaxPrecision() { - return 0; - } - - @Override - protected NumberInterface multiplyInternal(NumberInterface multiplier) { - return new RationalNumber(value.multiply(((RationalNumber)multiplier).value)); - } - - @Override - protected NumberInterface divideInternal(NumberInterface divisor) { - return new RationalNumber(value.divide(((RationalNumber)divisor).value)); - } - - @Override - protected NumberInterface addInternal(NumberInterface summand) { - return new RationalNumber(value.add(((RationalNumber)summand).value)); - } - - @Override - protected NumberInterface subtractInternal(NumberInterface subtrahend) { - return new RationalNumber(value.subtract(((RationalNumber)subtrahend).value)); - } - - @Override - protected NumberInterface negateInternal() { - return new RationalNumber(value.negate()); - } - - @Override - protected NumberInterface intPowInternal(int exponent) { - return new RationalNumber(value.pow(exponent)); - } - - @Override - public int compareTo(NumberInterface number) { - return value.compareTo(((RationalNumber)number).value); - } - - @Override - public int signum() { - return value.getNumerator().signum(); - } - - @Override - protected NumberInterface ceilingInternal() { - if(value.getNumeratorAsInt() != 1){ - return floorInternal().add(ONE); - } - return this; - } - - @Override - protected NumberInterface floorInternal() { - BigInteger floor = value.bigDecimalValue().toBigInteger(); - if(value.compareTo(BigFraction.ZERO) < 0 && value.getDenominatorAsInt() != 1){ - floor = floor.subtract(BigInteger.ONE); - } - return new RationalNumber(new BigFraction(floor)); - } - - @Override - protected NumberInterface fractionalPartInternal() { - return this.subtractInternal(floorInternal()); - } - - @Override - public int intValue() { - return 0; - } - - @Override - protected NumberInterface promoteToInternal(Class toClass) { - return null; - } - - @Override - public NumberInterface getMaxError() { - return toPreciseNumber().getMaxError(); - } - - @Override - public String toString(){ - return toPreciseNumber().toString(); - } - - PreciseNumber toPreciseNumber(){ - return (PreciseNumber) new PreciseNumber(value.getNumerator()).divideInternal(new PreciseNumber(value.getDenominator())); - } -}