Numeral bases

This commit is contained in:
serso
2016-02-11 16:26:18 +01:00
parent 9e10640e54
commit 7350a9ed0c
23 changed files with 442 additions and 803 deletions

View File

@@ -2,15 +2,6 @@ package jscl.math;
import org.solovyev.common.Converter;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import jscl.math.function.Constant;
import jscl.math.function.Fraction;
import jscl.math.function.Inverse;
@@ -25,6 +16,15 @@ import jscl.text.ParserUtils;
import jscl.text.msg.Messages;
import jscl.util.ArrayUtils;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
public class Expression extends Generic {
protected static final Converter<Variable, Generic> FACTORIZE_CONVERTER = new Converter<Variable, Generic>() {

View File

@@ -4,12 +4,22 @@ import jscl.math.function.Constant;
import jscl.mathml.MathML;
import jscl.text.ParserUtils;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import java.math.BigInteger;
import java.util.Set;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
public abstract class Generic implements Arithmetic<Generic>, Comparable {
public BigInteger toBigInteger() {
return null;
}
public Double toDouble() {
return null;
}
@Nonnull
public Generic subtract(@Nonnull Generic that) {
return add(that.negate());

View File

@@ -4,12 +4,13 @@ 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));
@@ -364,4 +365,14 @@ public final class JsclInteger extends Generic {
e1.appendChild(element.text(String.valueOf(content)));
element.appendChild(e1);
}
@Override
public BigInteger toBigInteger() {
return content;
}
@Override
public Double toDouble() {
return content.doubleValue();
}
}

View File

@@ -4,13 +4,19 @@ import jscl.JsclMathEngine;
import jscl.math.function.Constant;
import jscl.math.function.Constants;
import jscl.math.function.IConstant;
import jscl.math.numeric.*;
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.mathml.MathML;
import javax.annotation.Nonnull;
import java.math.BigInteger;
import java.util.Collections;
import java.util.Set;
import javax.annotation.Nonnull;
public final class NumericWrapper extends Generic implements INumeric<NumericWrapper> {
@Nonnull
@@ -415,7 +421,7 @@ public final class NumericWrapper extends Generic implements INumeric<NumericWra
}
public String toJava() {
return "JsclDouble.valueOf(" + new Double(((Real) content).doubleValue()) + ")";
return "JsclDouble.valueOf(" + ((Real) content).doubleValue() + ")";
}
public void toMathML(MathML element, Object data) {
@@ -442,4 +448,14 @@ public final class NumericWrapper extends Generic implements INumeric<NumericWra
e1.appendChild(element.text(String.valueOf(new Double(((Real) content).doubleValue()))));
element.appendChild(e1);
}
@Override
public BigInteger toBigInteger() {
return content.toBigInteger();
}
@Override
public Double toDouble() {
return content.toDouble();
}
}

View File

@@ -5,11 +5,12 @@ import jscl.math.function.Fraction;
import jscl.math.function.Inverse;
import jscl.mathml.MathML;
import javax.annotation.Nonnull;
import java.math.BigInteger;
import java.util.Collections;
import java.util.Set;
import javax.annotation.Nonnull;
public final class Rational extends Generic implements Field {
public static final Rational factory = new Rational(BigInteger.valueOf(0), BigInteger.valueOf(1));
@@ -308,4 +309,18 @@ public final class Rational extends Generic implements Field {
element.appendChild(e1);
}
}
@Override
public BigInteger toBigInteger() {
try {
return integerValue().toBigInteger();
} catch (NotIntegerException e) {
return null;
}
}
@Override
public Double toDouble() {
return numerator.doubleValue() / denominator.doubleValue();
}
}

View File

@@ -1,14 +1,16 @@
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 static jscl.math.numeric.Complex.I;
import static jscl.math.numeric.Real.ONE;
import static jscl.math.numeric.Real.TWO;
import javax.annotation.Nonnull;
public abstract class Numeric implements Arithmetic<Numeric>, INumeric<Numeric>, Comparable {
@@ -325,4 +327,12 @@ public abstract class Numeric implements Arithmetic<Numeric>, INumeric<Numeric>,
protected String toString(final double value) {
return JsclMathEngine.getInstance().format(value, JsclMathEngine.getInstance().getNumeralBase());
}
public BigInteger toBigInteger() {
return null;
}
public Double toDouble() {
return null;
}
}

View File

@@ -2,6 +2,8 @@ package jscl.math.numeric;
import jscl.math.NotDivisibleException;
import java.math.BigInteger;
import javax.annotation.Nonnull;
public final class Real extends Numeric {
@@ -277,4 +279,17 @@ public final class Real extends Numeric {
public Complex toComplex() {
return Complex.valueOf(this.content, 0.);
}
@Override
public BigInteger toBigInteger() {
if (content == Math.floor(content)) {
return BigInteger.valueOf((long) content);
}
return null;
}
@Override
public Double toDouble() {
return content;
}
}