2018-09-02 13:36:49 +02:00
|
|
|
package it.cavallium.warppi.math;
|
2017-02-14 20:02:03 +01:00
|
|
|
|
2018-09-02 13:36:49 +02:00
|
|
|
import it.cavallium.warppi.math.rules.Rule;
|
2018-09-12 22:16:33 +02:00
|
|
|
import it.cavallium.warppi.util.Error;
|
2017-05-16 22:02:44 +02:00
|
|
|
import it.unimi.dsi.fastutil.objects.ObjectArrayList;
|
|
|
|
|
2019-10-25 22:48:49 +02:00
|
|
|
import java.util.Objects;
|
|
|
|
|
2017-02-14 20:02:03 +01:00
|
|
|
public abstract class FunctionSingle implements Function {
|
2017-04-10 22:50:43 +02:00
|
|
|
|
2018-10-12 23:20:12 +02:00
|
|
|
private boolean simplified;
|
|
|
|
|
2017-02-14 20:02:03 +01:00
|
|
|
/**
|
2017-04-10 22:50:43 +02:00
|
|
|
* Create a new instance of FunctionSingle. The Math Context will be the
|
|
|
|
* same of <strong>value</strong>'s.
|
2018-09-22 11:17:30 +02:00
|
|
|
*
|
2017-04-10 22:50:43 +02:00
|
|
|
* @throws NullPointerException
|
|
|
|
* when value is null.
|
|
|
|
* @param value
|
|
|
|
* The parameter of this function.
|
2017-02-14 20:02:03 +01:00
|
|
|
*/
|
2018-09-22 11:17:30 +02:00
|
|
|
public FunctionSingle(final Function value) throws NullPointerException {
|
2017-02-14 20:02:03 +01:00
|
|
|
mathContext = value.getMathContext();
|
|
|
|
parameter = value;
|
|
|
|
}
|
2017-04-10 22:50:43 +02:00
|
|
|
|
2017-06-09 22:47:30 +02:00
|
|
|
/**
|
|
|
|
* Create a new instance of FunctionSingle.
|
2018-09-22 11:17:30 +02:00
|
|
|
*
|
2017-06-09 22:47:30 +02:00
|
|
|
* @param mathContext
|
|
|
|
* Math Context
|
|
|
|
*/
|
2018-09-22 11:17:30 +02:00
|
|
|
public FunctionSingle(final MathContext mathContext) {
|
2017-06-09 22:47:30 +02:00
|
|
|
this.mathContext = mathContext;
|
|
|
|
parameter = null;
|
|
|
|
}
|
|
|
|
|
2017-02-14 20:02:03 +01:00
|
|
|
/**
|
|
|
|
* Create a new instance of FunctionSingle.
|
2018-09-22 11:17:30 +02:00
|
|
|
*
|
2017-04-10 22:50:43 +02:00
|
|
|
* @param mathContext
|
|
|
|
* Math Context
|
|
|
|
* @param value
|
|
|
|
* The parameter of this function.
|
2017-02-14 20:02:03 +01:00
|
|
|
*/
|
2018-09-22 11:17:30 +02:00
|
|
|
public FunctionSingle(final MathContext mathContext, final Function value) {
|
2017-02-14 20:02:03 +01:00
|
|
|
this.mathContext = mathContext;
|
|
|
|
parameter = value;
|
|
|
|
}
|
2017-04-10 22:50:43 +02:00
|
|
|
|
2017-03-03 23:06:43 +01:00
|
|
|
protected final MathContext mathContext;
|
2017-04-10 22:50:43 +02:00
|
|
|
|
2017-02-14 20:02:03 +01:00
|
|
|
/**
|
|
|
|
* Function parameter.<br>
|
|
|
|
* <u>MUST NOT BE MODIFIED IF ALREADY SET UP.</u>
|
|
|
|
*/
|
|
|
|
protected Function parameter;
|
|
|
|
|
|
|
|
/**
|
2018-09-22 11:17:30 +02:00
|
|
|
*
|
2017-02-14 20:02:03 +01:00
|
|
|
* @return Parameter.
|
|
|
|
*/
|
|
|
|
public Function getParameter() {
|
|
|
|
return parameter;
|
|
|
|
}
|
2017-04-10 22:50:43 +02:00
|
|
|
|
2017-02-14 20:02:03 +01:00
|
|
|
/**
|
2018-09-22 11:17:30 +02:00
|
|
|
*
|
2019-11-16 01:32:47 +01:00
|
|
|
* @param value
|
2017-04-10 22:50:43 +02:00
|
|
|
* Parameter.
|
2017-02-14 20:02:03 +01:00
|
|
|
* @return A new instance of this function.
|
|
|
|
*/
|
2018-09-22 11:17:30 +02:00
|
|
|
public FunctionSingle setParameter(final Function value) {
|
2017-04-10 22:50:43 +02:00
|
|
|
final FunctionSingle s = clone();
|
2017-02-14 20:02:03 +01:00
|
|
|
s.parameter = value;
|
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2018-09-22 11:17:30 +02:00
|
|
|
public FunctionSingle setParameter(final int index, final Function var) throws IndexOutOfBoundsException {
|
2018-09-28 11:39:28 +02:00
|
|
|
if (index == 0) {
|
2017-02-14 20:02:03 +01:00
|
|
|
return this.setParameter(var);
|
2018-09-28 11:39:28 +02:00
|
|
|
} else {
|
2017-02-14 20:02:03 +01:00
|
|
|
throw new IndexOutOfBoundsException();
|
2018-09-28 11:39:28 +02:00
|
|
|
}
|
2017-02-14 20:02:03 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2018-09-22 11:17:30 +02:00
|
|
|
public Function getParameter(final int index) throws IndexOutOfBoundsException {
|
2018-09-28 11:39:28 +02:00
|
|
|
if (index == 0) {
|
2017-02-14 20:02:03 +01:00
|
|
|
return this.getParameter();
|
2018-09-28 11:39:28 +02:00
|
|
|
} else {
|
2017-02-14 20:02:03 +01:00
|
|
|
throw new IndexOutOfBoundsException();
|
2018-09-28 11:39:28 +02:00
|
|
|
}
|
2017-02-14 20:02:03 +01:00
|
|
|
}
|
2017-04-10 22:50:43 +02:00
|
|
|
|
2017-02-14 20:02:03 +01:00
|
|
|
@Override
|
|
|
|
public MathContext getMathContext() {
|
|
|
|
return mathContext;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2018-09-22 11:17:30 +02:00
|
|
|
public final ObjectArrayList<Function> simplify(final Rule rule) throws Error, InterruptedException {
|
2018-05-12 21:18:29 +02:00
|
|
|
final ObjectArrayList<Function> simplifiedParam = parameter.simplify(rule);
|
2018-09-28 11:39:28 +02:00
|
|
|
if (simplifiedParam == null) {
|
2018-05-12 21:18:29 +02:00
|
|
|
return rule.execute(this);
|
2018-09-28 11:39:28 +02:00
|
|
|
}
|
2018-05-12 21:18:29 +02:00
|
|
|
|
|
|
|
final ObjectArrayList<Function> result = new ObjectArrayList<>();
|
2018-09-28 11:39:28 +02:00
|
|
|
for (final Function f : simplifiedParam) {
|
2017-12-22 22:39:58 +01:00
|
|
|
result.add(this.setParameter(f));
|
2018-09-28 11:39:28 +02:00
|
|
|
}
|
2017-02-14 20:02:03 +01:00
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public abstract FunctionSingle clone();
|
2017-04-10 22:50:43 +02:00
|
|
|
|
2017-02-14 20:02:03 +01:00
|
|
|
@Override
|
|
|
|
public int hashCode() {
|
2019-10-25 22:48:49 +02:00
|
|
|
return Objects.hash(parameter);
|
2017-02-14 20:02:03 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public abstract boolean equals(Object o);
|
2019-11-16 01:32:47 +01:00
|
|
|
|
|
|
|
@Override
|
|
|
|
public String toString() {
|
|
|
|
return this.getClass().getSimpleName() + "(" + getParameter() + ")";
|
|
|
|
}
|
2017-02-14 20:02:03 +01:00
|
|
|
}
|