mirror of
https://github.com/DanilaFe/abacus
synced 2026-01-10 17:25:19 +00:00
Merge branch 'master' of https://github.com/DanilaFe/abacus
This commit is contained in:
@@ -1,20 +0,0 @@
|
||||
package org.nwapw.abacus.number;
|
||||
|
||||
import java.util.HashMap;
|
||||
|
||||
public abstract class Function {
|
||||
|
||||
private static final HashMap<Class<? extends NumberInterface>, Integer> priorityMap =
|
||||
new HashMap<Class<? extends NumberInterface>, Integer>() {{
|
||||
put(NaiveNumber.class, 0);
|
||||
}};
|
||||
|
||||
protected abstract boolean matchesParams(NumberInterface[] params);
|
||||
protected abstract NumberInterface applyInternal(NumberInterface[] params);
|
||||
|
||||
public NumberInterface apply(NumberInterface...params) {
|
||||
if(!matchesParams(params)) return null;
|
||||
return applyInternal(params);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,94 +0,0 @@
|
||||
package org.nwapw.abacus.number;
|
||||
|
||||
import java.util.HashMap;
|
||||
|
||||
public class FunctionDatabase {
|
||||
|
||||
private HashMap<String, Function> functions;
|
||||
|
||||
private void registerDefault(){
|
||||
functions.put("+", new Function() {
|
||||
@Override
|
||||
protected boolean matchesParams(NumberInterface[] params) {
|
||||
return params.length >= 1;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected NumberInterface applyInternal(NumberInterface[] params) {
|
||||
NumberInterface sum = params[0];
|
||||
for(int i = 1; i < params.length; i++){
|
||||
sum = sum.add(params[i]);
|
||||
}
|
||||
return sum;
|
||||
}
|
||||
});
|
||||
|
||||
functions.put("-", new Function() {
|
||||
@Override
|
||||
protected boolean matchesParams(NumberInterface[] params) {
|
||||
return params.length == 2;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected NumberInterface applyInternal(NumberInterface[] params) {
|
||||
return params[0].subtract(params[1]);
|
||||
}
|
||||
});
|
||||
|
||||
functions.put("*", new Function() {
|
||||
@Override
|
||||
protected boolean matchesParams(NumberInterface[] params) {
|
||||
return params.length >= 1;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected NumberInterface applyInternal(NumberInterface[] params) {
|
||||
NumberInterface product = params[0];
|
||||
for(int i = 1; i < params.length; i++){
|
||||
product = product.multiply(params[i]);
|
||||
}
|
||||
return product;
|
||||
}
|
||||
});
|
||||
|
||||
functions.put("/", new Function() {
|
||||
@Override
|
||||
protected boolean matchesParams(NumberInterface[] params) {
|
||||
return params.length == 2;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected NumberInterface applyInternal(NumberInterface[] params) {
|
||||
return params[0].divide(params[1]);
|
||||
}
|
||||
});
|
||||
|
||||
functions.put("!", new Function() {
|
||||
@Override
|
||||
protected boolean matchesParams(NumberInterface[] params) {
|
||||
return params.length == 1;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected NumberInterface applyInternal(NumberInterface[] params) {
|
||||
NumberInterface factorial = params[0];
|
||||
NumberInterface multiplier = params[0];
|
||||
//It is necessary to later prevent calls of factorial on anything but non-negative integers.
|
||||
while((multiplier = multiplier.subtract(NaiveNumber.ONE.promoteTo(multiplier.getClass()))).signum() == 1){
|
||||
factorial = factorial.multiply(multiplier);
|
||||
}
|
||||
return factorial;
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
public FunctionDatabase(){
|
||||
functions = new HashMap<>();
|
||||
registerDefault();
|
||||
}
|
||||
|
||||
public Function getFunction(String name){
|
||||
return functions.get(name);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,14 +1,30 @@
|
||||
package org.nwapw.abacus.number;
|
||||
|
||||
/**
|
||||
* An implementation of NumberInterface using a double.
|
||||
*/
|
||||
public class NaiveNumber implements NumberInterface {
|
||||
|
||||
/**
|
||||
* The value of this number.
|
||||
*/
|
||||
private double value;
|
||||
|
||||
/**
|
||||
* Creates a new NaiveNumber with the given value.
|
||||
* @param value the value to use.
|
||||
*/
|
||||
public NaiveNumber(double value) {
|
||||
this.value = value;
|
||||
}
|
||||
|
||||
/**
|
||||
* The number zero.
|
||||
*/
|
||||
public static final NaiveNumber ZERO = new NaiveNumber(0);
|
||||
/**
|
||||
* The number one.
|
||||
*/
|
||||
public static final NaiveNumber ONE = new NaiveNumber(1);
|
||||
|
||||
@Override
|
||||
|
||||
@@ -1,17 +1,77 @@
|
||||
package org.nwapw.abacus.number;
|
||||
|
||||
/**
|
||||
* An interface used to represent a number.
|
||||
*/
|
||||
public interface NumberInterface {
|
||||
|
||||
/**
|
||||
* The precision to which this number operates.
|
||||
* @return the precision.
|
||||
*/
|
||||
int precision();
|
||||
|
||||
/**
|
||||
* Multiplies this number by another, returning
|
||||
* a new number instance.
|
||||
* @param multiplier the multiplier
|
||||
* @return the result of the multiplication.
|
||||
*/
|
||||
NumberInterface multiply(NumberInterface multiplier);
|
||||
/**
|
||||
* Divides this number by another, returning
|
||||
* a new number instance.
|
||||
* @param divisor the divisor
|
||||
* @return the result of the division.
|
||||
*/
|
||||
NumberInterface divide(NumberInterface divisor);
|
||||
/**
|
||||
* Adds this number to another, returning
|
||||
* a new number instance.
|
||||
* @param summand the summand
|
||||
* @return the result of the summation.
|
||||
*/
|
||||
NumberInterface add(NumberInterface summand);
|
||||
/**
|
||||
* Subtracts another number from this number,
|
||||
* a new number instance.
|
||||
* @param subtrahend the subtrahend.
|
||||
* @return the result of the subtraction.
|
||||
*/
|
||||
NumberInterface subtract(NumberInterface subtrahend);
|
||||
|
||||
/**
|
||||
* Returns a new instance of this number with
|
||||
* the sign flipped.
|
||||
* @return the new instance.
|
||||
*/
|
||||
NumberInterface negate();
|
||||
|
||||
/**
|
||||
* Raises this number to an integer power.
|
||||
* @param exponent
|
||||
* @return
|
||||
*/
|
||||
NumberInterface intPow(int exponent);
|
||||
|
||||
/**
|
||||
* Compares this number to another.
|
||||
* @param number the number to compare to.
|
||||
* @return same as Integer.compare();
|
||||
*/
|
||||
int compareTo(NumberInterface number);
|
||||
|
||||
/**
|
||||
* Same as Math.signum().
|
||||
* @return 1 if this number is positive, -1 if this number is negative, 0 if this number is 0.
|
||||
*/
|
||||
int signum();
|
||||
|
||||
/**
|
||||
* Promotes this class to another number class.
|
||||
* @param toClass the class to promote to.
|
||||
* @return the resulting new instance.
|
||||
*/
|
||||
NumberInterface promoteTo(Class<? extends NumberInterface> toClass);
|
||||
|
||||
}
|
||||
|
||||
@@ -1,28 +0,0 @@
|
||||
package org.nwapw.abacus.number;
|
||||
|
||||
import java.util.HashMap;
|
||||
|
||||
public abstract class externalFunction {
|
||||
|
||||
private HashMap<String, Function> functions;
|
||||
|
||||
public externalFunction(){
|
||||
functions=new HashMap<>();
|
||||
}
|
||||
|
||||
public boolean hasFunction(Function x){
|
||||
return functions.containsKey(x);
|
||||
}
|
||||
public Function getFunction(String x){
|
||||
return functions.get(x);
|
||||
}
|
||||
public boolean registerFunction(String x, Function y){
|
||||
if(!functions.containsKey(x))
|
||||
return functions.put(x,y)==null;
|
||||
return false;
|
||||
}
|
||||
public Function functionFor(String x){
|
||||
return null;
|
||||
}
|
||||
public abstract void load();
|
||||
}
|
||||
Reference in New Issue
Block a user