summaryrefslogtreecommitdiff
path: root/base/src/main/java/bjc/utils/math
diff options
context:
space:
mode:
authorbjculkin <bjculkin@mix.wvu.edu>2018-02-12 22:19:02 -0500
committerbjculkin <bjculkin@mix.wvu.edu>2018-02-12 22:19:02 -0500
commit32f5da54c628408c96db09d279f3a7ef44b3bd19 (patch)
treeef56d04518b8791409ed65db58c304b74b23a01b /base/src/main/java/bjc/utils/math
parent49cd96c4c5bbb883c0c5c10d7916ad2e93ff2df0 (diff)
Update
Diffstat (limited to 'base/src/main/java/bjc/utils/math')
-rw-r--r--base/src/main/java/bjc/utils/math/DualExpr.java84
-rw-r--r--base/src/main/java/bjc/utils/math/NumberUtils.java106
2 files changed, 97 insertions, 93 deletions
diff --git a/base/src/main/java/bjc/utils/math/DualExpr.java b/base/src/main/java/bjc/utils/math/DualExpr.java
index d0e9acf..8c73f34 100644
--- a/base/src/main/java/bjc/utils/math/DualExpr.java
+++ b/base/src/main/java/bjc/utils/math/DualExpr.java
@@ -1,4 +1,5 @@
package bjc.utils.math;
+
/**
* Represents an expression using dual numbers.
*
@@ -68,11 +69,11 @@ public class DualExpr {
/**
* The left (or first) part of the expression.
*/
- public DualExpr left;
+ public DualExpr left;
/**
* The right (or second) part of the expression.
*/
- public DualExpr right;
+ public DualExpr right;
/**
* The power to use, for power operations.
@@ -83,7 +84,7 @@ public class DualExpr {
* Create a new constant dual number.
*
* @param num
- * The value of the dual number.
+ * The value of the dual number.
*/
public DualExpr(Dual num) {
this.type = ExprType.CONSTANT;
@@ -95,9 +96,9 @@ public class DualExpr {
* Create a new unary dual number.
*
* @param type
- * The type of operation to perform.
+ * The type of operation to perform.
* @param val
- * The parameter to the value.
+ * The parameter to the value.
*/
public DualExpr(DualExpr.ExprType type, DualExpr val) {
this.type = type;
@@ -109,9 +110,11 @@ public class DualExpr {
* Create a new binary dual number.
*
* @param type
- * The type of operation to perform.
- * @param val
- * The parameter to the value.
+ * 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;
@@ -124,9 +127,9 @@ public class DualExpr {
* Create a new power expression.
*
* @param left
- * The expression to raise.
+ * The expression to raise.
* @param power
- * The power to raise it by.
+ * The power to raise it by.
*/
public DualExpr(DualExpr left, int power) {
this.type = ExprType.POWER;
@@ -148,7 +151,7 @@ public class DualExpr {
Dual lval, rval;
/* Perform the right operation for each type. */
- switch (type) {
+ switch(type) {
case CONSTANT:
return number;
case ADDITION:
@@ -176,7 +179,7 @@ public class DualExpr {
rval = right.evaluate();
{
- if (rval.real == 0) {
+ if(rval.real == 0) {
throw new IllegalArgumentException("ERROR: Attempted to divide by zero.");
}
@@ -206,16 +209,15 @@ public class DualExpr {
case LOGARITHM:
lval = left.evaluate();
- if (lval.real <= 0) {
- throw new IllegalArgumentException(
- "ERROR: Attempted to take non-positive log.");
+ if(lval.real <= 0) {
+ throw new IllegalArgumentException("ERROR: Attempted to take non-positive log.");
}
return new Dual(Math.log(lval.real), lval.dual / lval.real);
case POWER:
lval = left.evaluate();
- if (lval.real == 0) {
+ if(lval.real == 0) {
throw new IllegalArgumentException("ERROR: Raising zero to a power.");
}
@@ -239,7 +241,7 @@ public class DualExpr {
@Override
public String toString() {
- switch (type) {
+ switch(type) {
case ABSOLUTE:
return String.format("abs(%s)", left.toString());
case ADDITION:
@@ -271,58 +273,54 @@ public class DualExpr {
public int hashCode() {
final int prime = 31;
int result = 1;
-
+
result = prime * result + ((left == null) ? 0 : left.hashCode());
result = prime * result + ((number == null) ? 0 : number.hashCode());
result = prime * result + power;
result = prime * result + ((right == null) ? 0 : right.hashCode());
result = prime * result + ((type == null) ? 0 : type.hashCode());
-
+
return result;
}
@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;
+
DualExpr other = (DualExpr) obj;
- if (type != other.type) {
+ if(type != other.type) {
return false;
}
-
- if (left == null) {
- if (other.left != null) {
+
+ if(left == null) {
+ if(other.left != null) {
return false;
}
- } else if (!left.equals(other.left)) {
+ } else if(!left.equals(other.left)) {
return false;
}
-
- if (number == null) {
- if (other.number != null)
- return false;
- } else if (!number.equals(other.number)) {
+
+ if(number == null) {
+ if(other.number != null) return false;
+ } else if(!number.equals(other.number)) {
return false;
}
-
- if (power != other.power) {
+
+ if(power != other.power) {
return false;
}
-
- if (right == null) {
- if (other.right != null) {
+
+ if(right == null) {
+ if(other.right != null) {
return false;
}
- } else if (!right.equals(other.right)) {
+ } else if(!right.equals(other.right)) {
return false;
}
-
+
return true;
}
} \ No newline at end of file
diff --git a/base/src/main/java/bjc/utils/math/NumberUtils.java b/base/src/main/java/bjc/utils/math/NumberUtils.java
index 9391040..ed58bb8 100644
--- a/base/src/main/java/bjc/utils/math/NumberUtils.java
+++ b/base/src/main/java/bjc/utils/math/NumberUtils.java
@@ -27,7 +27,7 @@ public class NumberUtils {
}
if(currNumber >= 1000) {
- int numM = (int)(currNumber / 1000);
+ int numM = (int) (currNumber / 1000);
currNumber = currNumber % 1000;
for(int i = 0; i < numM; i++) {
@@ -54,7 +54,7 @@ public class NumberUtils {
}
if(currNumber >= 100) {
- int numC = (int)(currNumber / 100);
+ int numC = (int) (currNumber / 100);
currNumber = currNumber % 100;
for(int i = 0; i < numC; i++) {
@@ -81,7 +81,7 @@ public class NumberUtils {
}
if(currNumber >= 10) {
- int numX = (int)(currNumber / 10);
+ int numX = (int) (currNumber / 10);
currNumber = currNumber % 10;
for(int i = 0; i < numX; i++) {
@@ -108,7 +108,7 @@ public class NumberUtils {
}
if(currNumber >= 1) {
- int numI = (int)(currNumber / 1);
+ int numI = (int) (currNumber / 1);
currNumber = currNumber % 1;
for(int i = 0; i < numI; i++) {
@@ -118,25 +118,23 @@ public class NumberUtils {
return work.toString();
}
-
+
public static String toCardinal(long number) {
return toCardinal(number, null);
}
- 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",
- };
+ 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", };
public static class CardinalState {
- public final Map<Long, String> customNumbers;
- public final Map<LongPredicate, BiFunction<Long, CardinalState, String>> customScales;
+ public final Map<Long, String> customNumbers;
+ public final Map<LongPredicate, BiFunction<Long, CardinalState, String>> customScales;
- public CardinalState(Map<Long, String> customNumbers, Map<LongPredicate, BiFunction<Long, CardinalState, String>> customScales) {
+ public CardinalState(Map<Long, String> customNumbers,
+ Map<LongPredicate, BiFunction<Long, CardinalState, String>> customScales) {
this.customNumbers = customNumbers;
- this.customScales = customScales;
+ this.customScales = customScales;
}
public String handleCustom(long number) {
@@ -144,7 +142,8 @@ public class NumberUtils {
return customNumbers.get(number);
}
- for(Entry<LongPredicate, BiFunction<Long, CardinalState, String>> ent : customScales.entrySet()) {
+ for(Entry<LongPredicate, BiFunction<Long, CardinalState, String>> ent : customScales
+ .entrySet()) {
if(ent.getKey().test(number)) {
return ent.getValue().apply(number, this);
}
@@ -163,11 +162,11 @@ public class NumberUtils {
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) {
+ switch((int) number) {
case 30:
return "thirty";
case 40:
@@ -183,51 +182,52 @@ public class NumberUtils {
case 90:
return "ninety";
default:
- /*
+ /*
* Shouldn't happen.
*/
- assert(false);
+ assert (false);
}
}
- long numTens = (long)(number / 10);
+ long numTens = (long) (number / 10);
long numOnes = number % 10;
return toCardinal(numTens, custom) + "-" + toCardinal(numOnes, custom);
}
if(number < 1000) {
- long numHundreds = (long)(number / 100);
- long rest = number % 100;
+ long numHundreds = (long) (number / 100);
+ long rest = number % 100;
return toCardinal(numHundreds, custom) + " hundred and " + toCardinal(rest, custom);
}
- long MILLION = (long)(Math.pow(10, 6));
+ long MILLION = (long) (Math.pow(10, 6));
if(number < MILLION) {
- long numThousands = (long)(number / 1000);
- long rest = number % 1000;
+ long numThousands = (long) (number / 1000);
+ long rest = number % 1000;
return toCardinal(numThousands, custom) + " thousand, " + toCardinal(rest, custom);
}
- long BILLION = (long)(Math.pow(10, 9));
+ long BILLION = (long) (Math.pow(10, 9));
if(number < BILLION) {
- long numMillions = (long)(number / MILLION);
- long rest = number % MILLION;
+ long numMillions = (long) (number / MILLION);
+ long rest = number % MILLION;
return toCardinal(numMillions, custom) + " million, " + toCardinal(rest, custom);
}
- long TRILLION = (long)(Math.pow(10, 12));
+ long TRILLION = (long) (Math.pow(10, 12));
if(number < TRILLION) {
- long numBillions = (long)(number / BILLION);
- long rest = number % BILLION;
+ long numBillions = (long) (number / BILLION);
+ long rest = number % BILLION;
return toCardinal(numBillions, custom) + " billion, " + toCardinal(rest, custom);
}
- throw new IllegalArgumentException("Numbers greater than or equal to 1 trillion are not supported yet.");
+ throw new IllegalArgumentException(
+ "Numbers greater than or equal to 1 trillion are not supported yet.");
}
public static String toOrdinal(long number) {
@@ -236,7 +236,7 @@ public class NumberUtils {
}
if(number < 20) {
- switch((int)number) {
+ switch((int) number) {
case 0:
return "zeroth";
case 1:
@@ -281,13 +281,13 @@ public class NumberUtils {
/*
* Shouldn't happen.
*/
- assert(false);
+ assert (false);
}
}
if(number < 100) {
if(number % 10 == 0) {
- switch((int)number) {
+ switch((int) number) {
case 20:
return "twentieth";
case 30:
@@ -304,6 +304,8 @@ public class NumberUtils {
return "eightieth";
case 90:
return "ninetieth";
+ default:
+ throw new IllegalArgumentException(String.format("Illegal number %d", number));
}
}
@@ -311,15 +313,15 @@ public class NumberUtils {
return toCardinal(number - numPostfix) + "-" + toOrdinal(numPostfix);
}
- long procNum = number % 100;
- long tens = (long)(procNum / 10);
- long ones = procNum % 10;
+ long procNum = number % 100;
+ long tens = (long) (procNum / 10);
+ long ones = procNum % 10;
if(tens == 1) {
return Long.toString(number) + "th";
- }
+ }
- switch((int)ones) {
+ switch((int) ones) {
case 1:
return Long.toString(number) + "st";
case 2:
@@ -330,33 +332,36 @@ public class NumberUtils {
return Long.toString(number) + "th";
}
}
-
+
private static char[] radixChars = new char[62];
static {
int idx = 0;
for(char i = 0; i < 10; i++) {
- radixChars[idx] = (char)('0' + i);
+ radixChars[idx] = (char) ('0' + i);
idx += 1;
}
for(char i = 0; i < 26; i++) {
- radixChars[idx] = (char)('A' + i);
+ radixChars[idx] = (char) ('A' + i);
idx += 1;
}
for(char i = 0; i < 26; i++) {
- radixChars[idx] = (char)('a' + i);
+ radixChars[idx] = (char) ('a' + i);
idx += 1;
}
}
- public static String toCommaString(long val, int mincols, char padchar, int commaInterval, char commaChar, boolean signed, int radix) {
+ 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));
+ throw new IllegalArgumentException(
+ String.format("Radix %d is larger than largest supported radix %d", radix,
+ radixChars.length));
}
StringBuilder work = new StringBuilder();
@@ -376,15 +381,16 @@ public class NumberUtils {
while(currVal != 0) {
valCounter += 1;
- int radDigit = (int)(currVal % radix);
+ int radDigit = (int) (currVal % radix);
work.append(radixChars[radDigit]);
- currVal = (long)(currVal / radix);
+ currVal = (long) (currVal / radix);
if(commaInterval != 0 && valCounter % commaInterval == 0) work.append(commaChar);
}
}
- if(isNeg) work.append("-");
+ if(isNeg)
+ work.append("-");
else if(signed) work.append("+");
work.reverse();