Module operator fix

This commit is contained in:
serso 2016-03-05 11:43:54 +01:00
parent 58e1088524
commit b1a0e588c1
29 changed files with 281 additions and 116 deletions

View File

@ -35,6 +35,7 @@ import butterknife.ButterKnife;
import com.squareup.otto.Bus;
import jscl.NumeralBase;
import jscl.math.Generic;
import jscl.math.NotDoubleException;
import org.solovyev.android.calculator.converter.ConverterFragment;
import org.solovyev.android.calculator.jscl.JsclOperation;
@ -134,8 +135,10 @@ public class DisplayFragment extends BaseFragment implements View.OnClickListene
addMenu(menu, item.title, this);
}
}
if (result.toDouble() != null) {
try {
result.doubleValue();
addMenu(menu, R.string.c_convert, this);
} catch (NotDoubleException ignored) {
}
}
if (launcher.canPlot(result)) {
@ -207,10 +210,10 @@ public class DisplayFragment extends BaseFragment implements View.OnClickListene
if (result == null) {
return 1d;
}
final Double value = result.toDouble();
if (value == null) {
try {
return result.doubleValue();
} catch (NotDoubleException ignored) {
return 1d;
}
return value;
}
}

View File

@ -22,6 +22,7 @@
package org.solovyev.android.calculator;
import android.content.SharedPreferences;
import android.os.Build;
import org.junit.Assert;
import org.junit.Test;
@ -37,6 +38,8 @@ import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicBoolean;
import static org.mockito.Mockito.mock;
@Config(constants = BuildConfig.class, sdk = Build.VERSION_CODES.LOLLIPOP)
@RunWith(value = RobolectricGradleTestRunner.class)
public class AndroidEditorViewTest {
@ -52,6 +55,7 @@ public class AndroidEditorViewTest {
final int count = 10;
final int maxTextLength = 100;
final Editor editor = new Editor(RuntimeEnvironment.application, mock(SharedPreferences.class), Tests.makeEngine());
final Random random = new Random(new Date().getTime());
final CountDownLatch startLatchLatch = new CountDownLatch(threadNum);
final CountDownLatch finishLatch = new CountDownLatch(threadNum * count);
@ -75,7 +79,7 @@ public class AndroidEditorViewTest {
for (int j = 0; j < count; j++) {
try {
int textLength = random.nextInt(maxTextLength);
App.getEditor().insert(Strings.generateRandomString(textLength), textLength);
editor.insert(Strings.generateRandomString(textLength), textLength);
} catch (Throwable e) {
System.out.println(e);
error.set(true);

View File

@ -30,6 +30,7 @@ import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.robolectric.RobolectricGradleTestRunner;
import org.robolectric.RuntimeEnvironment;
import org.robolectric.annotation.Config;
import static org.mockito.Mockito.mock;
@ -42,7 +43,7 @@ public class EditorTest {
@Before
public void setUp() throws Exception {
editor = new Editor(mock(SharedPreferences.class), Tests.makeEngine());
editor = new Editor(RuntimeEnvironment.application, mock(SharedPreferences.class), Tests.makeEngine());
editor.bus = mock(Bus.class);
}

View File

@ -62,7 +62,7 @@ public class JsclMathEngine implements MathEngine {
if (Math.floor(value) == value) {
return (int) value;
} else {
throw new NotIntegerException();
throw NotIntegerException.get();
}
}

View File

@ -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();
}
}

View File

@ -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();

View File

@ -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();
}
}

View File

@ -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

View File

@ -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

View File

@ -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;

View 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;
}
}

View File

@ -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;
}
}

View File

@ -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));
}
}

View File

@ -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();
}
}

View File

@ -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() {

View File

@ -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);
}

View File

@ -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("{");

View File

@ -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();
}

View File

@ -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;
}
}

View File

@ -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();

View File

@ -128,7 +128,6 @@ public abstract class AbstractFunction extends Variable {
public Generic numeric() {
final AbstractFunction result = newNumericFunction();
return result.selfNumeric();
}

View File

@ -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();
}
}

View File

@ -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();
}
}

View File

@ -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);

View File

@ -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

View File

@ -8,7 +8,7 @@ import jscl.math.function.Constant;
import jscl.math.function.ExtendedConstant;
import jscl.math.function.IConstant;
import jscl.text.ParseException;
import junit.framework.Assert;
import org.junit.Assert;
import org.junit.Test;
import javax.annotation.Nonnull;
@ -21,14 +21,10 @@ import java.net.URL;
import java.net.URLConnection;
import java.util.Set;
import static junit.framework.Assert.fail;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
/**
* User: serso
* Date: 10/27/11
* Time: 3:54 PM
*/
public class ExpressionTest {
private static final String expressions = "-24.37581129610191-((2699.798527427213-4032.781981216783)*√(4657.120529143301)/6202.47137988087-ln(4435.662292261872)*sin(5134.044125137488)-sin(5150.617980207194)+sin(1416.6029070906816))\n" +
@ -111,11 +107,11 @@ public class ExpressionTest {
@Test
public void testConstants() throws Exception {
Assert.assertTrue(Expression.valueOf("3+4").getConstants().isEmpty());
assertTrue(Expression.valueOf("3+4").getConstants().isEmpty());
Set<? extends Constant> constants = Expression.valueOf("3+4*t").getConstants();
Assert.assertTrue(constants.size() == 1);
Assert.assertTrue(constants.contains(new Constant("t")));
assertTrue(constants.size() == 1);
assertTrue(constants.contains(new Constant("t")));
IConstant constant = null;
@ -125,9 +121,9 @@ public class ExpressionTest {
constant = me.getConstantsRegistry().addOrUpdate(t_0.create());
constants = Expression.valueOf("3+4*t_0+t_0+t_1").getConstants();
Assert.assertTrue(constants.size() == 2);
Assert.assertTrue(constants.contains(new Constant("t_0")));
Assert.assertTrue(constants.contains(new Constant("t_1")));
assertTrue(constants.size() == 2);
assertTrue(constants.contains(new Constant("t_0")));
assertTrue(constants.contains(new Constant("t_1")));
final Expression expression = Expression.valueOf("2*t_0+5*t_1");

View File

@ -7,6 +7,7 @@ import jscl.text.ParseException;
import jscl.util.ExpressionGeneratorWithInput;
import org.junit.Assert;
import org.junit.Test;
import org.solovyev.common.Functor;
import javax.annotation.Nonnull;
import java.io.InputStreamReader;
@ -20,14 +21,14 @@ import java.util.List;
*/
public class NumeralBaseConversionTest {
public static void testExpression(@Nonnull String[] line, @Nonnull Converter<String, String> converter) throws ParseException {
public static void testExpression(@Nonnull String[] line, @Nonnull Functor<String, String> converter) throws ParseException {
final String dec = line[0].toUpperCase();
final String hex = "0x:" + line[1].toUpperCase();
final String bin = "0b:" + line[2].toUpperCase();
final String decResult = Expression.valueOf(converter.convert(dec)).numeric().toString();
final String hexResult = Expression.valueOf(converter.convert(hex)).numeric().toString();
final String binResult = Expression.valueOf(converter.convert(bin)).numeric().toString();
final String decResult = Expression.valueOf(converter.apply(dec)).numeric().toString();
final String hexResult = Expression.valueOf(converter.apply(hex)).numeric().toString();
final String binResult = Expression.valueOf(converter.apply(bin)).numeric().toString();
Assert.assertEquals(decResult, hexResult);
Assert.assertEquals(decResult, binResult);
@ -95,38 +96,38 @@ public class NumeralBaseConversionTest {
}
}
private static class DummyExpression implements Converter<String, String> {
private static class DummyExpression implements Functor<String, String> {
@Nonnull
@Override
public String convert(@Nonnull String s) {
public String apply(@Nonnull String s) {
return s;
}
}
private static class Expression1 implements Converter<String, String> {
private static class Expression1 implements Functor<String, String> {
@Nonnull
@Override
public String convert(@Nonnull String s) {
public String apply(@Nonnull String s) {
return s + "*" + s;
}
}
private static class Expression2 implements Converter<String, String> {
private static class Expression2 implements Functor<String, String> {
@Nonnull
@Override
public String convert(@Nonnull String s) {
public String apply(@Nonnull String s) {
return s + "*" + s + " * sin(" + s + ") - 0b:1101";
}
}
private static class Expression3 implements Converter<String, String> {
private static class Expression3 implements Functor<String, String> {
@Nonnull
@Override
public String convert(@Nonnull String s) {
public String apply(@Nonnull String s) {
return s + "*" + s + " * sin(" + s + ") - 0b:1101 + √(" + s + ") + exp ( " + s + ")";
}
}

View File

@ -1,9 +1,8 @@
package jscl.math.function;
import jscl.JsclMathEngine;
import junit.framework.Assert;
import org.junit.Assert;
import org.junit.Test;
import org.solovyev.common.math.Maths;
/**
* User: serso
@ -28,6 +27,6 @@ public class DegTest {
}
private void assertEquals(double expected, Double actual) {
Assert.assertTrue("Expected=" + expected + ", actual=" + actual, Maths.equals(expected, actual, 8));
Assert.assertEquals(expected, actual, Math.pow(10, -8));
}
}

View File

@ -0,0 +1,57 @@
package jscl.math.operator;
import jscl.math.Expression;
import jscl.math.Generic;
import jscl.math.NumericWrapper;
import jscl.text.ParseException;
import org.junit.Assert;
import org.junit.Test;
import javax.annotation.Nonnull;
import static org.junit.Assert.assertEquals;
public class ModuloTest {
@Test
public void testNumeric() throws Exception {
assertMod(2, 2);
assertMod(1, 2);
assertMod(3.5, 2);
assertMod(3, 2);
assertMod(1.5, 2);
assertMod(1.5, "1.5", "2");
assertMod(1.5, "3.5", "2");
}
private void assertMod(double expected, @Nonnull String numerator, @Nonnull String denominator) throws ParseException {
final Modulo mod = makeModulo(numerator, denominator);
final Generic numeric = mod.numeric();
assertEquals(expected, numeric.doubleValue(), Math.pow(10, -8));
}
private void assertMod(int numerator, int denominator) {
final Modulo mod = makeModulo(numerator, denominator);
final Generic numeric = mod.numeric();
Assert.assertTrue(numeric.isInteger());
assertEquals(numerator % denominator, numeric.integerValue().intValue());
}
private void assertMod(double numerator, double denominator) {
final Modulo mod = makeModulo(numerator, denominator);
final Generic numeric = mod.numeric();
assertEquals(numerator % denominator, numeric.doubleValue(), Math.pow(10, -8));
}
private Modulo makeModulo(int n, int d) {
return new Modulo(NumericWrapper.valueOf(n), NumericWrapper.valueOf(d));
}
private Modulo makeModulo(String n, String d) throws ParseException {
return new Modulo(Expression.valueOf(n), Expression.valueOf(d));
}
private Modulo makeModulo(double n, double d) {
return new Modulo(NumericWrapper.valueOf(n), NumericWrapper.valueOf(d));
}
}