summaryrefslogtreecommitdiff
path: root/base/src/main/java/bjc/utils/math
diff options
context:
space:
mode:
Diffstat (limited to 'base/src/main/java/bjc/utils/math')
-rw-r--r--base/src/main/java/bjc/utils/math/CardinalState.java25
-rw-r--r--base/src/main/java/bjc/utils/math/Dual.java25
-rw-r--r--base/src/main/java/bjc/utils/math/DualExpr.java24
-rw-r--r--base/src/main/java/bjc/utils/math/DualExprParser.java19
-rw-r--r--base/src/main/java/bjc/utils/math/NumberUtils.java209
5 files changed, 164 insertions, 138 deletions
diff --git a/base/src/main/java/bjc/utils/math/CardinalState.java b/base/src/main/java/bjc/utils/math/CardinalState.java
index 3c97b68..de2224c 100644
--- a/base/src/main/java/bjc/utils/math/CardinalState.java
+++ b/base/src/main/java/bjc/utils/math/CardinalState.java
@@ -7,21 +7,21 @@ import java.util.function.LongPredicate;
/*
* @TODO 2/12/18 Ben Culkin :AdditionalCardinals
- *
+ *
* Add some built-in implementations for various things.
*
* By this, I mean for various unit scales, like custom and metric weights
*/
/**
* Customizations for number cardinalization.
- *
+ *
* @author EVE
*
*/
public class CardinalState {
/**
* Alias type for converting numbers to cardinals.
- *
+ *
* @author EVE
*
*/
@@ -44,31 +44,32 @@ public class CardinalState {
/**
* Create a new set of cardinalization customizations.
- *
+ *
* @param customNumbers
- * The custom numbers to use.
+ * The custom numbers to use.
* @param customScales
- * The custom scales to use.
+ * The custom scales to use.
*/
- public CardinalState(Map<Long, String> customNumbers, Map<LongPredicate, Cardinalizer> customScales) {
+ public CardinalState(Map<Long, String> customNumbers,
+ Map<LongPredicate, Cardinalizer> customScales) {
this.customNumbers = customNumbers;
this.customScales = customScales;
}
/**
* Handle a custom cardinal number
- *
+ *
* @param number
- * The number to handle
+ * The number to handle
* @return The number as a cardinal, or null if we don't handle it.
*/
public String handleCustom(long number) {
- if(customNumbers.containsKey(number)) {
+ if (customNumbers.containsKey(number)) {
return customNumbers.get(number);
}
- for(Entry<LongPredicate, Cardinalizer> ent : customScales.entrySet()) {
- if(ent.getKey().test(number)) {
+ for (Entry<LongPredicate, Cardinalizer> ent : customScales.entrySet()) {
+ if (ent.getKey().test(number)) {
return ent.getValue().apply(number, this);
}
}
diff --git a/base/src/main/java/bjc/utils/math/Dual.java b/base/src/main/java/bjc/utils/math/Dual.java
index 7eea6a9..4a81e8a 100644
--- a/base/src/main/java/bjc/utils/math/Dual.java
+++ b/base/src/main/java/bjc/utils/math/Dual.java
@@ -3,8 +3,8 @@ package bjc.utils.math;
/**
* Represents a 'dual' number.
*
- * Think imaginary numbers, where instead of i, we add a value d such that
- * d^2 = 0.
+ * Think imaginary numbers, where instead of i, we add a value d such that d^2 =
+ * 0.
*/
public class Dual {
/**
@@ -28,7 +28,7 @@ public class Dual {
* Create a new dual number with a zero dual part.
*
* @param real
- * The real part of the number.
+ * The real part of the number.
*/
public Dual(double real) {
this.real = real;
@@ -39,9 +39,9 @@ public class Dual {
* Create a new dual number with a specified dual part.
*
* @param real
- * The real part of the number.
+ * The real part of the number.
* @param dual
- * The dual part of the number.
+ * The dual part of the number.
*/
public Dual(double real, double dual) {
this.real = real;
@@ -67,12 +67,17 @@ public class Dual {
@Override
public boolean equals(Object obj) {
- if(this == obj) return true;
- if(obj == null) return false;
- if(getClass() != obj.getClass()) return false;
+ if (this == obj)
+ return true;
+ if (obj == null)
+ return false;
+ if (getClass() != obj.getClass())
+ return false;
Dual other = (Dual) obj;
- if(Double.doubleToLongBits(dual) != Double.doubleToLongBits(other.dual)) return false;
- if(Double.doubleToLongBits(real) != Double.doubleToLongBits(other.real)) return false;
+ if (Double.doubleToLongBits(dual) != Double.doubleToLongBits(other.dual))
+ return false;
+ if (Double.doubleToLongBits(real) != Double.doubleToLongBits(other.real))
+ return false;
return true;
}
}
diff --git a/base/src/main/java/bjc/utils/math/DualExpr.java b/base/src/main/java/bjc/utils/math/DualExpr.java
index 81ee31c..a682814 100644
--- a/base/src/main/java/bjc/utils/math/DualExpr.java
+++ b/base/src/main/java/bjc/utils/math/DualExpr.java
@@ -83,7 +83,8 @@ public class DualExpr {
/**
* Create a new constant dual number.
*
- * @param num The value of the dual number.
+ * @param num
+ * The value of the dual number.
*/
public DualExpr(Dual num) {
this.type = ExprType.CONSTANT;
@@ -94,8 +95,10 @@ public class DualExpr {
/**
* Create a new unary dual number.
*
- * @param type The type of operation to perform.
- * @param val The parameter to the value.
+ * @param type
+ * The type of operation to perform.
+ * @param val
+ * The parameter to the value.
*/
public DualExpr(DualExpr.ExprType type, DualExpr val) {
this.type = type;
@@ -106,9 +109,12 @@ public class DualExpr {
/**
* Create a new binary dual number.
*
- * @param type The type of operation to perform.
- * @param left The left hand side of the expression.
- * @param right The right hand side of the expression.
+ * @param type
+ * The type of operation to perform.
+ * @param left
+ * The left hand side of the expression.
+ * @param right
+ * The right hand side of the expression.
*/
public DualExpr(DualExpr.ExprType type, DualExpr left, DualExpr right) {
this.type = type;
@@ -189,7 +195,8 @@ public class DualExpr {
lval = left.evaluate();
if (lval.real <= 0) {
- throw new IllegalArgumentException("ERROR: Attempted to take non-positive log.");
+ throw new IllegalArgumentException(
+ "ERROR: Attempted to take non-positive log.");
}
return new Dual(Math.log(lval.real), lval.dual / lval.real);
@@ -197,7 +204,8 @@ public class DualExpr {
// @TODO Ben Culkin 3/27/2020 :RealPower
// Make this no longer a special case; since it doesn't have to be one.
//
- // 3/28/2020 - This is less of a special case, but I've not implemented the bit for variable exponents.
+ // 3/28/2020 - This is less of a special case, but I've not implemented the
+ // bit for variable exponents.
lval = left.evaluate();
rval = right.evaluate();
diff --git a/base/src/main/java/bjc/utils/math/DualExprParser.java b/base/src/main/java/bjc/utils/math/DualExprParser.java
index d90024f..f130a9a 100644
--- a/base/src/main/java/bjc/utils/math/DualExprParser.java
+++ b/base/src/main/java/bjc/utils/math/DualExprParser.java
@@ -1,5 +1,5 @@
/**
- *
+ *
*/
package bjc.utils.math;
@@ -16,19 +16,20 @@ import bjc.utils.math.DualExpr.ExprType;
/**
* Create DualExprs from strings.
- *
+ *
* @author Ben Culkin
*
*/
public class DualExprParser {
/**
* Result class from parsing exprs.
+ *
* @author Ben Culkin
*
*/
public static class Result {
/**
- * The resulting expression.
+ * The resulting expression.
*/
public DualExpr expr;
/**
@@ -46,10 +47,10 @@ public class DualExprParser {
/**
* Parses a dual expression from a postfix expression string.
- *
+ *
* @param expr
* The string to parse the dual expression from.
- *
+ *
* @return Both the parsed expression, and a map of all the variables used
*/
public static Result parseExpression(String expr) {
@@ -58,17 +59,17 @@ public class DualExprParser {
/**
* Parses a dual expression from a postfix expression string.
- *
+ *
* @param expr
* The string to parse the dual expression from.
*
* @param preVars
* Any pre-existing variables to use.
- *
+ *
* @return Both the parsed expression, and a map of all the variables used
- *
+ *
* @throws StackUnderflow
- * If the expression is not properly formatted.
+ * If the expression is not properly formatted.
*/
public static Result parseExpression(String expr, Map<String, DualExpr> preVars) {
Result res = new Result();
diff --git a/base/src/main/java/bjc/utils/math/NumberUtils.java b/base/src/main/java/bjc/utils/math/NumberUtils.java
index 7dc2ff3..7d138fb 100644
--- a/base/src/main/java/bjc/utils/math/NumberUtils.java
+++ b/base/src/main/java/bjc/utils/math/NumberUtils.java
@@ -2,26 +2,26 @@ package bjc.utils.math;
/**
* A variety of functions for doing useful stuff with numbers.
- *
+ *
* @author EVE
*
*/
public class NumberUtils {
/*
* @TODO 2/12/18 Ben Culkin :RomanExpansion
- *
- * Use U+305 for large roman numerals, as well as excels 'concise'
- * numerals (as implemented by roman()).
+ *
+ * Use U+305 for large roman numerals, as well as excels 'concise' numerals (as
+ * implemented by roman()).
*/
/**
* Convert a number into a roman numeral.
- *
+ *
* @param number
- * The number to convert.
+ * The number to convert.
* @param classic
- * Whether to use classic roman numerals (use IIII instead of IV,
- * and such).
+ * Whether to use classic roman numerals (use IIII instead of IV,
+ * and such).
* @return The number as a roman numeral.
*/
public static String toRoman(long number, boolean classic) {
@@ -29,102 +29,102 @@ public class NumberUtils {
long currNumber = number;
- if(currNumber == 0) {
+ if (currNumber == 0) {
return "N";
}
- if(currNumber < 0) {
+ if (currNumber < 0) {
currNumber *= -1;
work.append("-");
}
- if(currNumber >= 1000) {
+ if (currNumber >= 1000) {
int numM = (int) (currNumber / 1000);
currNumber = currNumber % 1000;
- for(int i = 0; i < numM; i++) {
+ for (int i = 0; i < numM; i++) {
work.append("M");
}
}
- if(currNumber >= 900 && !classic) {
+ if (currNumber >= 900 && !classic) {
currNumber = currNumber % 900;
work.append("CM");
}
- if(currNumber >= 500) {
+ if (currNumber >= 500) {
currNumber = currNumber % 500;
work.append("D");
}
- if(currNumber >= 400 && !classic) {
+ if (currNumber >= 400 && !classic) {
currNumber = currNumber % 400;
work.append("CD");
}
- if(currNumber >= 100) {
+ if (currNumber >= 100) {
int numC = (int) (currNumber / 100);
currNumber = currNumber % 100;
- for(int i = 0; i < numC; i++) {
+ for (int i = 0; i < numC; i++) {
work.append("C");
}
}
- if(currNumber >= 90 && !classic) {
+ if (currNumber >= 90 && !classic) {
currNumber = currNumber % 90;
work.append("XC");
}
- if(currNumber >= 50) {
+ if (currNumber >= 50) {
currNumber = currNumber % 50;
work.append("L");
}
- if(currNumber >= 40 && !classic) {
+ if (currNumber >= 40 && !classic) {
currNumber = currNumber % 40;
work.append("XL");
}
- if(currNumber >= 10) {
+ if (currNumber >= 10) {
int numX = (int) (currNumber / 10);
currNumber = currNumber % 10;
- for(int i = 0; i < numX; i++) {
+ for (int i = 0; i < numX; i++) {
work.append("X");
}
}
- if(currNumber >= 9 && !classic) {
+ if (currNumber >= 9 && !classic) {
currNumber = currNumber % 9;
work.append("IX");
}
- if(currNumber >= 5) {
+ if (currNumber >= 5) {
currNumber = currNumber % 5;
work.append("V");
}
- if(currNumber >= 4 && !classic) {
+ if (currNumber >= 4 && !classic) {
currNumber = currNumber % 4;
work.append("IV");
}
- if(currNumber >= 1) {
+ if (currNumber >= 1) {
int numI = (int) (currNumber / 1);
currNumber = currNumber % 1;
- for(int i = 0; i < numI; i++) {
+ for (int i = 0; i < numI; i++) {
work.append("I");
}
}
@@ -134,9 +134,9 @@ public class NumberUtils {
/**
* Convert a number into a cardinal number.
- *
+ *
* @param number
- * The number to convert
+ * The number to convert
* @return The number as a cardinal.
*/
public static String toCardinal(long number) {
@@ -144,34 +144,37 @@ public class NumberUtils {
}
private static String[] cardinals = new String[] {
- "zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine", "ten", "eleven",
- "twelve", "thirteen", "fourteen", "fifteen", "sixteen", "seventeen", "eighteen", "nineteen",
- "twenty",
+ "zero", "one", "two", "three", "four", "five", "six", "seven", "eight",
+ "nine", "ten", "eleven", "twelve", "thirteen", "fourteen", "fifteen",
+ "sixteen", "seventeen", "eighteen", "nineteen", "twenty",
};
/**
* Convert a number into a cardinal number.
- *
+ *
* @param number
- * The number to convert to a cardinal.
+ * The number to convert to a cardinal.
* @param custom
- * The customizations to use.
+ * The customizations to use.
* @return The number as a cardinal.
*/
public static String toCardinal(long number, CardinalState custom) {
- if(custom != null) {
+ if (custom != null) {
String res = custom.handleCustom(number);
- if(res != null) return res;
+ if (res != null)
+ return res;
}
- if(number < 0) return "negative " + toCardinal(number * -1, custom);
+ if (number < 0)
+ return "negative " + toCardinal(number * -1, custom);
- if(number <= 20) return cardinals[(int) number];
+ if (number <= 20)
+ return cardinals[(int) number];
- if(number < 100) {
- if(number % 10 == 0) {
- switch((int) number) {
+ if (number < 100) {
+ if (number % 10 == 0) {
+ switch ((int) number) {
case 30:
return "thirty";
case 40:
@@ -200,35 +203,39 @@ public class NumberUtils {
return toCardinal(numTens, custom) + "-" + toCardinal(numOnes, custom);
}
- if(number < 1000) {
+ if (number < 1000) {
long numHundreds = number / 100;
long rest = number % 100;
- return toCardinal(numHundreds, custom) + " hundred and " + toCardinal(rest, custom);
+ return toCardinal(numHundreds, custom) + " hundred and "
+ + toCardinal(rest, custom);
}
long MILLION = (long) (Math.pow(10, 6));
- if(number < MILLION) {
+ if (number < MILLION) {
long numThousands = number / 1000;
long rest = number % 1000;
- return toCardinal(numThousands, custom) + " thousand, " + toCardinal(rest, custom);
+ return toCardinal(numThousands, custom) + " thousand, "
+ + toCardinal(rest, custom);
}
long BILLION = (long) (Math.pow(10, 9));
- if(number < BILLION) {
+ if (number < BILLION) {
long numMillions = number / MILLION;
long rest = number % MILLION;
- return toCardinal(numMillions, custom) + " million, " + toCardinal(rest, custom);
+ return toCardinal(numMillions, custom) + " million, "
+ + toCardinal(rest, custom);
}
long TRILLION = (long) (Math.pow(10, 12));
- if(number < TRILLION) {
+ if (number < TRILLION) {
long numBillions = number / BILLION;
long rest = number % BILLION;
- return toCardinal(numBillions, custom) + " billion, " + toCardinal(rest, custom);
+ return toCardinal(numBillions, custom) + " billion, "
+ + toCardinal(rest, custom);
}
throw new IllegalArgumentException(
@@ -237,18 +244,18 @@ public class NumberUtils {
/**
* Convert a number into an ordinal.
- *
+ *
* @param number
- * The number to convert to an ordinal.
+ * The number to convert to an ordinal.
* @return The number as an ordinal.
*/
public static String toOrdinal(long number) {
- if(number < 0) {
+ if (number < 0) {
return "minus " + toOrdinal(number);
}
- if(number < 20) {
- switch((int) number) {
+ if (number < 20) {
+ switch ((int) number) {
case 0:
return "zeroth";
case 1:
@@ -297,9 +304,9 @@ public class NumberUtils {
}
}
- if(number < 100) {
- if(number % 10 == 0) {
- switch((int) number) {
+ if (number < 100) {
+ if (number % 10 == 0) {
+ switch ((int) number) {
case 20:
return "twentieth";
case 30:
@@ -317,7 +324,8 @@ public class NumberUtils {
case 90:
return "ninetieth";
default:
- throw new IllegalArgumentException(String.format("Illegal number %d", number));
+ throw new IllegalArgumentException(
+ String.format("Illegal number %d", number));
}
}
@@ -329,11 +337,11 @@ public class NumberUtils {
long tens = procNum / 10;
long ones = procNum % 10;
- if(tens == 1) {
+ if (tens == 1) {
return Long.toString(number) + "th";
}
- switch((int) ones) {
+ switch ((int) ones) {
case 1:
return Long.toString(number) + "st";
case 2:
@@ -349,19 +357,19 @@ public class NumberUtils {
static {
int idx = 0;
- for(char i = 0; i < 10; i++) {
+ for (char i = 0; i < 10; i++) {
radixChars[idx] = (char) ('0' + i);
idx += 1;
}
- for(char i = 0; i < 26; i++) {
+ for (char i = 0; i < 26; i++) {
radixChars[idx] = (char) ('A' + i);
idx += 1;
}
- for(char i = 0; i < 26; i++) {
+ for (char i = 0; i < 26; i++) {
radixChars[idx] = (char) ('a' + i);
idx += 1;
@@ -370,58 +378,61 @@ public class NumberUtils {
/**
* Convert a number into a commafied string.
- *
+ *
* @param val
- * The number to convert.
+ * The number to convert.
* @param mincols
- * The minimum number of columns to use.
+ * The minimum number of columns to use.
* @param padchar
- * The padding char to use.
+ * The padding char to use.
* @param commaInterval
- * The interval to place commas at.
+ * The interval to place commas at.
* @param commaChar
- * The character to use as a comma
+ * The character to use as a comma
* @param signed
- * Whether or not to always display a sign
+ * Whether or not to always display a sign
* @param radix
- * The radix to use
+ * The radix to use
* @return The number as a commafied string.
*/
- public static String toCommaString(long val, int mincols, char padchar, int commaInterval, char commaChar,
- boolean signed, int radix) {
- if(radix > radixChars.length) {
+ public static String toCommaString(long val, int mincols, char padchar,
+ int commaInterval, char commaChar, boolean signed, int radix) {
+ if (radix > radixChars.length) {
throw new IllegalArgumentException(
- String.format("Radix %d is larger than largest supported radix %d", radix,
- radixChars.length));
+ String.format("Radix %d is larger than largest supported radix %d",
+ radix, radixChars.length));
}
StringBuilder work = new StringBuilder();
boolean isNeg = false;
long currVal = val;
- if(currVal < 0) {
- isNeg = true;
+ if (currVal < 0) {
+ isNeg = true;
currVal *= -1;
}
- if(currVal == 0) {
+ if (currVal == 0) {
work.append(radixChars[0]);
} else {
int valCounter = 0;
- while(currVal != 0) {
+ while (currVal != 0) {
valCounter += 1;
int radDigit = (int) (currVal % radix);
work.append(radixChars[radDigit]);
currVal = currVal / radix;
- if(commaInterval != 0 && valCounter % commaInterval == 0 && currVal != 0) work.append(commaChar);
+ if (commaInterval != 0 && valCounter % commaInterval == 0 && currVal != 0)
+ work.append(commaChar);
}
}
- if(isNeg) work.append("-");
- else if(signed) work.append("+");
+ if (isNeg)
+ work.append("-");
+ else if (signed)
+ work.append("+");
work.reverse();
@@ -430,25 +441,24 @@ public class NumberUtils {
*
* Should we have some way to specify how to pad?
*
- * By this, I mean specify padding direction (left, right,
- * balanced...)
+ * By this, I mean specify padding direction (left, right, balanced...)
*/
StringBuilder pad = new StringBuilder();
- if(work.length() < mincols) {
+ if (work.length() < mincols) {
@SuppressWarnings("unused")
int padCount = 0;
- for(int i = work.length(); i < mincols; i++) {
+ for (int i = work.length(); i < mincols; i++) {
// @NOTE 9/6/18 :CommaPad
//
// I have no idea if this is the intended
// behavior, or if something is wrong with the
// example case in the menu
// if (commaInterval != 0 && padCount != 0) {
- // if (Character.isDigit(padchar) && padCount % commaInterval == 0)
- // pad.append(commaChar);
- // else
- pad.append(padchar);
+ // if (Character.isDigit(padchar) && padCount % commaInterval == 0)
+ // pad.append(commaChar);
+ // else
+ pad.append(padchar);
// }
padCount++;
@@ -460,20 +470,21 @@ public class NumberUtils {
/**
* Convert a number to a normal commafied string.
- *
+ *
* @param val
- * The value to convert.
+ * The value to convert.
* @param mincols
- * The minimum number of columns.
+ * The minimum number of columns.
* @param padchar
- * The padding char to use.
+ * The padding char to use.
* @param signed
- * Whether or not to display the sign.
+ * Whether or not to display the sign.
* @param radix
- * The radix to use.
+ * The radix to use.
* @return The number as a normal commafied string.
*/
- public static String toNormalString(long val, int mincols, char padchar, boolean signed, int radix) {
+ public static String toNormalString(long val, int mincols, char padchar,
+ boolean signed, int radix) {
return toCommaString(val, mincols, padchar, 0, ',', signed, radix);
}
}