Module operator fix
This commit is contained in:
@@ -62,7 +62,7 @@ public class JsclMathEngine implements MathEngine {
|
||||
if (Math.floor(value) == value) {
|
||||
return (int) value;
|
||||
} else {
|
||||
throw new NotIntegerException();
|
||||
throw NotIntegerException.get();
|
||||
}
|
||||
}
|
||||
|
||||
|
@@ -626,17 +626,34 @@ public class Expression extends Generic {
|
||||
if (size == 0) {
|
||||
return JsclInteger.valueOf(0);
|
||||
} else if (size == 1) {
|
||||
|
||||
final Literal l = literals[0];
|
||||
final JsclInteger c = coefficients[0];
|
||||
|
||||
if (l.degree() == 0) {
|
||||
return c;
|
||||
} else {
|
||||
throw new NotIntegerException();
|
||||
throw NotIntegerException.get();
|
||||
}
|
||||
} else {
|
||||
throw new NotIntegerException();
|
||||
throw NotIntegerException.get();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public double doubleValue() throws NotDoubleException {
|
||||
if (size == 0) {
|
||||
return 0f;
|
||||
} else if (size == 1) {
|
||||
final Literal l = literals[0];
|
||||
final JsclInteger c = coefficients[0];
|
||||
|
||||
if (l.degree() == 0) {
|
||||
return c.doubleValue();
|
||||
} else {
|
||||
throw NotDoubleException.get();
|
||||
}
|
||||
} else {
|
||||
throw NotDoubleException.get();
|
||||
}
|
||||
}
|
||||
|
||||
|
@@ -39,10 +39,6 @@ public abstract class Generic implements Arithmetic<Generic>, Comparable {
|
||||
return null;
|
||||
}
|
||||
|
||||
public Double toDouble() {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Nonnull
|
||||
public Generic subtract(@Nonnull Generic that) {
|
||||
return add(that.negate());
|
||||
@@ -171,6 +167,8 @@ public abstract class Generic implements Arithmetic<Generic>, Comparable {
|
||||
|
||||
public abstract JsclInteger integerValue() throws NotIntegerException;
|
||||
|
||||
public abstract double doubleValue() throws NotDoubleException;
|
||||
|
||||
public abstract Variable variableValue() throws NotVariableException;
|
||||
|
||||
public abstract Variable[] variables();
|
||||
|
@@ -4,13 +4,12 @@ import jscl.JsclMathEngine;
|
||||
import jscl.math.function.Constant;
|
||||
import jscl.mathml.MathML;
|
||||
|
||||
import javax.annotation.Nonnull;
|
||||
import javax.annotation.Nullable;
|
||||
import java.math.BigInteger;
|
||||
import java.util.Collections;
|
||||
import java.util.Set;
|
||||
|
||||
import javax.annotation.Nonnull;
|
||||
import javax.annotation.Nullable;
|
||||
|
||||
public final class JsclInteger extends Generic {
|
||||
|
||||
public static final JsclInteger factory = new JsclInteger(BigInteger.valueOf(0));
|
||||
@@ -162,8 +161,8 @@ public final class JsclInteger extends Generic {
|
||||
return 0;
|
||||
}
|
||||
|
||||
public JsclInteger mod(JsclInteger integer) {
|
||||
return new JsclInteger(content.mod(integer.content));
|
||||
public JsclInteger mod(JsclInteger that) {
|
||||
return new JsclInteger(content.mod(that.content));
|
||||
}
|
||||
|
||||
public JsclInteger modPow(JsclInteger exponent, JsclInteger integer) {
|
||||
@@ -372,7 +371,7 @@ public final class JsclInteger extends Generic {
|
||||
}
|
||||
|
||||
@Override
|
||||
public Double toDouble() {
|
||||
public double doubleValue() throws NotDoubleException {
|
||||
return content.doubleValue();
|
||||
}
|
||||
}
|
||||
|
@@ -247,7 +247,12 @@ public class JsclVector extends Generic {
|
||||
}
|
||||
|
||||
public JsclInteger integerValue() throws NotIntegerException {
|
||||
throw new NotIntegerException();
|
||||
throw NotIntegerException.get();
|
||||
}
|
||||
|
||||
@Override
|
||||
public double doubleValue() throws NotDoubleException {
|
||||
throw NotDoubleException.get();
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@@ -329,7 +329,12 @@ public class Matrix extends Generic {
|
||||
}
|
||||
|
||||
public JsclInteger integerValue() throws NotIntegerException {
|
||||
throw new NotIntegerException();
|
||||
throw NotIntegerException.get();
|
||||
}
|
||||
|
||||
@Override
|
||||
public double doubleValue() throws NotDoubleException {
|
||||
throw NotDoubleException.get();
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@@ -151,6 +151,11 @@ public class ModularInteger extends Generic implements Field {
|
||||
return JsclInteger.valueOf(content);
|
||||
}
|
||||
|
||||
@Override
|
||||
public double doubleValue() throws NotDoubleException {
|
||||
return content;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isInteger() {
|
||||
return true;
|
||||
|
18
jscl/src/main/java/jscl/math/NotDoubleException.java
Normal file
18
jscl/src/main/java/jscl/math/NotDoubleException.java
Normal file
@@ -0,0 +1,18 @@
|
||||
package jscl.math;
|
||||
|
||||
import javax.annotation.Nonnull;
|
||||
|
||||
public class NotDoubleException extends ArithmeticException {
|
||||
|
||||
@SuppressWarnings("ThrowableInstanceNeverThrown")
|
||||
private static final NotDoubleException INSTANCE = new NotDoubleException();
|
||||
|
||||
private NotDoubleException() {
|
||||
super("Not double!");
|
||||
}
|
||||
|
||||
@Nonnull
|
||||
public static NotDoubleException get() {
|
||||
return INSTANCE;
|
||||
}
|
||||
}
|
@@ -1,11 +1,18 @@
|
||||
package jscl.math;
|
||||
|
||||
import javax.annotation.Nonnull;
|
||||
|
||||
public class NotIntegerException extends ArithmeticException {
|
||||
public NotIntegerException() {
|
||||
this("Not integer!");
|
||||
|
||||
@SuppressWarnings("ThrowableInstanceNeverThrown")
|
||||
private static final NotIntegerException INSTANCE = new NotIntegerException();
|
||||
|
||||
private NotIntegerException() {
|
||||
super("Not integer!");
|
||||
}
|
||||
|
||||
public NotIntegerException(String s) {
|
||||
super(s);
|
||||
@Nonnull
|
||||
public static NotIntegerException get() {
|
||||
return INSTANCE;
|
||||
}
|
||||
}
|
||||
|
@@ -1,22 +1,17 @@
|
||||
package jscl.math;
|
||||
|
||||
import java.math.BigInteger;
|
||||
import java.util.Collections;
|
||||
import java.util.Set;
|
||||
|
||||
import javax.annotation.Nonnull;
|
||||
|
||||
import jscl.JsclMathEngine;
|
||||
import jscl.math.function.Constant;
|
||||
import jscl.math.function.Constants;
|
||||
import jscl.math.function.IConstant;
|
||||
import jscl.math.numeric.Complex;
|
||||
import jscl.math.numeric.INumeric;
|
||||
import jscl.math.numeric.Numeric;
|
||||
import jscl.math.numeric.Real;
|
||||
import jscl.math.numeric.Vector;
|
||||
import jscl.math.numeric.*;
|
||||
import jscl.mathml.MathML;
|
||||
|
||||
import javax.annotation.Nonnull;
|
||||
import java.math.BigInteger;
|
||||
import java.util.Collections;
|
||||
import java.util.Set;
|
||||
|
||||
public final class NumericWrapper extends Generic implements INumeric<NumericWrapper> {
|
||||
|
||||
@Nonnull
|
||||
@@ -189,19 +184,19 @@ public final class NumericWrapper extends Generic implements INumeric<NumericWra
|
||||
}
|
||||
|
||||
public Generic expand() {
|
||||
return null;
|
||||
return this;
|
||||
}
|
||||
|
||||
public Generic factorize() {
|
||||
return null;
|
||||
return this;
|
||||
}
|
||||
|
||||
public Generic elementary() {
|
||||
return null;
|
||||
return this;
|
||||
}
|
||||
|
||||
public Generic simplify() {
|
||||
return null;
|
||||
return this;
|
||||
}
|
||||
|
||||
public Generic numeric() {
|
||||
@@ -240,17 +235,22 @@ public final class NumericWrapper extends Generic implements INumeric<NumericWra
|
||||
|
||||
public JsclInteger integerValue() throws NotIntegerException {
|
||||
if (content instanceof Real) {
|
||||
double doubleValue = ((Real) content).doubleValue();
|
||||
double doubleValue = content.doubleValue();
|
||||
if (Math.floor(doubleValue) == doubleValue) {
|
||||
return JsclInteger.valueOf((int) doubleValue);
|
||||
} else {
|
||||
throw new NotIntegerException();
|
||||
throw NotIntegerException.get();
|
||||
}
|
||||
} else {
|
||||
throw new NotIntegerException();
|
||||
throw NotIntegerException.get();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public double doubleValue() throws NotDoubleException {
|
||||
return content.doubleValue();
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isInteger() {
|
||||
if (content instanceof Real) {
|
||||
@@ -454,8 +454,13 @@ public final class NumericWrapper extends Generic implements INumeric<NumericWra
|
||||
return content.toBigInteger();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Double toDouble() {
|
||||
return content.toDouble();
|
||||
@Nonnull
|
||||
public static Generic valueOf(long value) {
|
||||
return new NumericWrapper(new JsclInteger(BigInteger.valueOf(value)));
|
||||
}
|
||||
|
||||
@Nonnull
|
||||
public static Generic valueOf(double value) {
|
||||
return new NumericWrapper(Real.valueOf(value));
|
||||
}
|
||||
}
|
||||
|
@@ -205,10 +205,15 @@ public final class Rational extends Generic implements Field {
|
||||
if (denominator.compareTo(BigInteger.ONE) == 0) {
|
||||
return new JsclInteger(numerator);
|
||||
} else {
|
||||
throw new NotIntegerException();
|
||||
throw NotIntegerException.get();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public double doubleValue() throws NotDoubleException {
|
||||
return numerator.doubleValue() / denominator.doubleValue();
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isInteger() {
|
||||
try {
|
||||
@@ -318,9 +323,4 @@ public final class Rational extends Generic implements Field {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public Double toDouble() {
|
||||
return numerator.doubleValue() / denominator.doubleValue();
|
||||
}
|
||||
}
|
||||
|
@@ -6,15 +6,16 @@ import jscl.mathml.MathML;
|
||||
import jscl.util.ArrayComparator;
|
||||
|
||||
import javax.annotation.Nonnull;
|
||||
import java.util.Arrays;
|
||||
import java.util.HashSet;
|
||||
import java.util.Objects;
|
||||
import java.util.Set;
|
||||
|
||||
public class Constant extends Variable {
|
||||
|
||||
public static final int PRIME_CHARS = 3;
|
||||
private int prime;
|
||||
private Generic subscripts[];
|
||||
private final int prime;
|
||||
private final Generic subscripts[];
|
||||
private Object[] hashArray;
|
||||
|
||||
public Constant(String name) {
|
||||
this(name, 0, new Generic[0]);
|
||||
@@ -148,7 +149,20 @@ public class Constant extends Variable {
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
return Objects.hash(Constant.class, name, subscripts, prime);
|
||||
final Object[] hashArray = getHashArray();
|
||||
hashArray[0] = Constant.class;
|
||||
hashArray[1] = name;
|
||||
hashArray[2] = subscripts;
|
||||
hashArray[3] = prime;
|
||||
return Arrays.deepHashCode(this.hashArray);
|
||||
}
|
||||
|
||||
@Nonnull
|
||||
private Object[] getHashArray() {
|
||||
if(hashArray == null) {
|
||||
hashArray = new Object[4];
|
||||
}
|
||||
return hashArray;
|
||||
}
|
||||
|
||||
public String toString() {
|
||||
|
@@ -3,6 +3,7 @@ package jscl.math.numeric;
|
||||
import jscl.AngleUnit;
|
||||
import jscl.JsclMathEngine;
|
||||
import jscl.math.NotDivisibleException;
|
||||
import jscl.math.NotDoubleException;
|
||||
import jscl.text.msg.JsclMessage;
|
||||
import jscl.text.msg.Messages;
|
||||
import org.solovyev.common.msg.MessageType;
|
||||
@@ -207,6 +208,11 @@ public final class Complex extends Numeric {
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public double doubleValue() {
|
||||
throw NotDoubleException.get();
|
||||
}
|
||||
|
||||
public Complex copyOf(@Nonnull Complex complex) {
|
||||
return valueOf(complex.real, complex.imaginary);
|
||||
}
|
||||
|
@@ -1,6 +1,7 @@
|
||||
package jscl.math.numeric;
|
||||
|
||||
import jscl.math.NotDivisibleException;
|
||||
import jscl.math.NotDoubleException;
|
||||
import jscl.util.ArrayComparator;
|
||||
|
||||
import javax.annotation.Nonnull;
|
||||
@@ -288,6 +289,11 @@ public class Matrix extends Numeric {
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public double doubleValue() {
|
||||
throw NotDoubleException.get();
|
||||
}
|
||||
|
||||
public String toString() {
|
||||
final StringBuilder result = new StringBuilder();
|
||||
result.append("{");
|
||||
|
@@ -1,16 +1,15 @@
|
||||
package jscl.math.numeric;
|
||||
|
||||
import static jscl.math.numeric.Complex.I;
|
||||
import static jscl.math.numeric.Real.ONE;
|
||||
import static jscl.math.numeric.Real.TWO;
|
||||
|
||||
import jscl.AngleUnit;
|
||||
import jscl.JsclMathEngine;
|
||||
import jscl.math.Arithmetic;
|
||||
|
||||
import javax.annotation.Nonnull;
|
||||
import java.math.BigInteger;
|
||||
|
||||
import javax.annotation.Nonnull;
|
||||
import static jscl.math.numeric.Complex.I;
|
||||
import static jscl.math.numeric.Real.ONE;
|
||||
import static jscl.math.numeric.Real.TWO;
|
||||
|
||||
public abstract class Numeric implements Arithmetic<Numeric>, INumeric<Numeric>, Comparable {
|
||||
|
||||
@@ -332,7 +331,5 @@ public abstract class Numeric implements Arithmetic<Numeric>, INumeric<Numeric>,
|
||||
return null;
|
||||
}
|
||||
|
||||
public Double toDouble() {
|
||||
return null;
|
||||
}
|
||||
public abstract double doubleValue();
|
||||
}
|
||||
|
@@ -2,9 +2,8 @@ package jscl.math.numeric;
|
||||
|
||||
import jscl.math.NotDivisibleException;
|
||||
|
||||
import java.math.BigInteger;
|
||||
|
||||
import javax.annotation.Nonnull;
|
||||
import java.math.BigInteger;
|
||||
|
||||
public final class Real extends Numeric {
|
||||
|
||||
@@ -255,10 +254,6 @@ public final class Real extends Numeric {
|
||||
} else throw new ArithmeticException();
|
||||
}
|
||||
|
||||
public double doubleValue() {
|
||||
return content;
|
||||
}
|
||||
|
||||
public int compareTo(@Nonnull Real that) {
|
||||
return Double.compare(this.content, that.content);
|
||||
}
|
||||
@@ -289,7 +284,7 @@ public final class Real extends Numeric {
|
||||
}
|
||||
|
||||
@Override
|
||||
public Double toDouble() {
|
||||
public double doubleValue() {
|
||||
return content;
|
||||
}
|
||||
}
|
||||
|
@@ -1,6 +1,7 @@
|
||||
package jscl.math.numeric;
|
||||
|
||||
import jscl.math.NotDivisibleException;
|
||||
import jscl.math.NotDoubleException;
|
||||
import jscl.util.ArrayComparator;
|
||||
|
||||
import javax.annotation.Nonnull;
|
||||
@@ -163,6 +164,11 @@ public class Vector extends Numeric {
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public double doubleValue() {
|
||||
throw NotDoubleException.get();
|
||||
}
|
||||
|
||||
public String toString() {
|
||||
final StringBuilder result = new StringBuilder();
|
||||
|
||||
|
@@ -128,7 +128,6 @@ public abstract class AbstractFunction extends Variable {
|
||||
|
||||
public Generic numeric() {
|
||||
final AbstractFunction result = newNumericFunction();
|
||||
|
||||
return result.selfNumeric();
|
||||
}
|
||||
|
||||
|
@@ -66,11 +66,11 @@ public class DoubleFactorial extends PostfixFunction {
|
||||
if (result instanceof JsclInteger) {
|
||||
return new NumericWrapper(((JsclInteger) result));
|
||||
} else {
|
||||
throw new NotIntegerException();
|
||||
throw NotIntegerException.get();
|
||||
}
|
||||
|
||||
} else {
|
||||
throw new NotIntegerException();
|
||||
throw NotIntegerException.get();
|
||||
}
|
||||
}
|
||||
|
||||
|
@@ -47,11 +47,11 @@ public class Factorial extends PostfixFunction {
|
||||
if (result instanceof JsclInteger) {
|
||||
return new NumericWrapper(((JsclInteger) result));
|
||||
} else {
|
||||
throw new NotIntegerException();
|
||||
throw NotIntegerException.get();
|
||||
}
|
||||
|
||||
} else {
|
||||
throw new NotIntegerException();
|
||||
throw NotIntegerException.get();
|
||||
}
|
||||
}
|
||||
|
||||
|
@@ -1,9 +1,7 @@
|
||||
package jscl.math.operator;
|
||||
|
||||
import jscl.math.Generic;
|
||||
import jscl.math.JsclInteger;
|
||||
import jscl.math.NotIntegerException;
|
||||
import jscl.math.Variable;
|
||||
import jscl.math.*;
|
||||
import jscl.math.numeric.Real;
|
||||
|
||||
import javax.annotation.Nonnull;
|
||||
|
||||
@@ -26,14 +24,23 @@ public class Modulo extends Operator {
|
||||
|
||||
public Generic selfExpand() {
|
||||
try {
|
||||
final JsclInteger first = parameters[0].integerValue();
|
||||
final JsclInteger second = parameters[1].integerValue();
|
||||
|
||||
return first.mod(second);
|
||||
|
||||
return tryIntegerMod();
|
||||
} catch (NotIntegerException e) {
|
||||
}
|
||||
return parameters[0].remainder(parameters[1]);
|
||||
return tryRealMod();
|
||||
}
|
||||
|
||||
private Generic tryRealMod() {
|
||||
final double numerator = parameters[0].doubleValue();
|
||||
final double denominator = parameters[1].doubleValue();
|
||||
return new NumericWrapper(Real.valueOf(numerator % denominator));
|
||||
}
|
||||
|
||||
@Nonnull
|
||||
private Generic tryIntegerMod() throws NotIntegerException{
|
||||
final JsclInteger numerator = parameters[0].integerValue();
|
||||
final JsclInteger denominator = parameters[1].integerValue();
|
||||
return numerator.mod(denominator);
|
||||
}
|
||||
|
||||
@Nonnull
|
||||
@@ -42,6 +49,16 @@ public class Modulo extends Operator {
|
||||
return new Modulo(parameters);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Generic numeric() {
|
||||
return newNumericFunction().selfNumeric();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Generic selfNumeric() {
|
||||
return selfExpand();
|
||||
}
|
||||
|
||||
@Nonnull
|
||||
public Variable newInstance() {
|
||||
return new Modulo(null, null);
|
||||
|
@@ -178,7 +178,12 @@ final class PolynomialWrapper extends Generic {
|
||||
}
|
||||
|
||||
public JsclInteger integerValue() throws NotIntegerException {
|
||||
throw new NotIntegerException();
|
||||
throw NotIntegerException.get();
|
||||
}
|
||||
|
||||
@Override
|
||||
public double doubleValue() throws NotDoubleException {
|
||||
throw NotDoubleException.get();
|
||||
}
|
||||
|
||||
@Override
|
||||
|
Reference in New Issue
Block a user