Use Real all the time

This commit is contained in:
serso 2016-04-09 21:12:15 +02:00
parent 1090ba856f
commit 29d4d77831
17 changed files with 994 additions and 1073 deletions

View File

@ -21,6 +21,7 @@ import android.widget.*;
import butterknife.Bind;
import butterknife.ButterKnife;
import jscl.JsclMathEngine;
import jscl.NumeralBase;
import midpcalc.Real;
import org.solovyev.android.calculator.*;
@ -310,12 +311,13 @@ public class ConverterFragment extends BaseDialogFragment
if (!TextUtils.isEmpty(groupingSeparator)) {
value = value.replace(groupingSeparator, "");
}
return Double.longBitsToDouble(new Real(value).toDoubleBits());
final long bits = new Real(value).toDoubleBits();
return Double.longBitsToDouble(bits);
}
@Nonnull
private String formatDouble(double toValue) {
return JsclMathEngine.getInstance().formatDec(toValue);
return JsclMathEngine.getInstance().format(toValue, NumeralBase.dec);
}
@Override

View File

@ -9,7 +9,7 @@ import jscl.math.operator.Percent;
import jscl.math.operator.Rand;
import jscl.math.operator.matrix.OperatorsRegistry;
import jscl.text.ParseException;
import midpcalc.Real;
import org.solovyev.common.NumberFormatter;
import org.solovyev.common.math.MathRegistry;
import org.solovyev.common.msg.MessageRegistry;
import org.solovyev.common.msg.Messages;
@ -17,7 +17,6 @@ import org.solovyev.common.msg.Messages;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.text.DecimalFormatSymbols;
import java.util.List;
import java.util.Locale;
@ -33,17 +32,10 @@ public class JsclMathEngine implements MathEngine {
@Nonnull
private final ConstantsRegistry constantsRegistry = new ConstantsRegistry();
@Nonnull
private final ThreadLocal<DecimalFormat> defaultNumbersFormat = new ThreadLocal<DecimalFormat>() {
private final ThreadLocal<NumberFormatter> numberFormatter = new ThreadLocal<NumberFormatter>() {
@Override
protected DecimalFormat initialValue() {
return new DecimalFormat();
}
};
@Nonnull
private final ThreadLocal<DecimalFormat> smallNumbersFormat = new ThreadLocal<DecimalFormat>() {
@Override
protected DecimalFormat initialValue() {
return new DecimalFormat("##0.#####E0");
protected NumberFormatter initialValue() {
return new NumberFormatter();
}
};
@Nonnull
@ -165,18 +157,12 @@ public class JsclMathEngine implements MathEngine {
public String format(@Nonnull Double value, @Nonnull NumeralBase nb) throws NumeralBaseException {
if (value.isInfinite()) {
return formatInfinity(value);
} else if (value.isNaN()) {
}
if (value.isNaN()) {
// return "NaN"
return String.valueOf(value);
} else if (nb == NumeralBase.dec) {
return formatDec(value);
} else {
return convert(value, nb);
}
}
@Nonnull
public String formatDec(@Nonnull Double value) {
if (nb == NumeralBase.dec) {
if (value == 0d) {
return "0";
}
@ -185,87 +171,16 @@ public class JsclMathEngine implements MathEngine {
if (constant != null) {
return constant.getName();
}
}
final NumberFormatter nf = numberFormatter.get();
nf.setGroupingSeparator(useGroupingSeparator ? decimalGroupSymbols.getGroupingSeparator() : NumberFormatter.NO_GROUPING);
nf.setPrecision(roundResult ? precision : NumberFormatter.DEFAULT_PRECISION);
if (scienceNotation) {
return formatDecEngineering(value);
}
return formatDecDefault(value);
}
@Nonnull
private String formatDecEngineering(@Nonnull Double value) {
final double absValue = Math.abs(value);
final boolean smallNumber = absValue < 1 && absValue >= 0.001;
final Real.NumberFormat nf = new Real.NumberFormat();
nf.fse = smallNumber ? Real.NumberFormat.FSE_FIX : Real.NumberFormat.FSE_ENG;
if (useGroupingSeparator) {
nf.thousand = decimalGroupSymbols.getGroupingSeparator();
}
if (roundResult) {
nf.precision = precision;
}
final Real real = new Real(Double.toString(value));
return stripTrailingZeros(real.toString(nf));
}
@Nonnull
private String stripTrailingZeros(@Nonnull String s) {
final int dot = s.indexOf('.');
if (dot < 0) {
// no dot - no trailing zeros
return s;
}
final int e = s.lastIndexOf('E');
final int start;
String exponent = "";
if (e > 0) {
exponent = s.substring(e);
if (exponent.length() == 2 && exponent.charAt(1) == '0') {
exponent = "";
}
start = e - 1;
nf.useEngineeringFormat(NumberFormatter.DEFAULT_MAGNITUDE);
} else {
start = s.length() - 1;
nf.useSimpleFormat();
}
final int i = findLastNonZero(s, start);
return s.substring(0, i == dot ? i : i + 1) + exponent;
}
private int findLastNonZero(String s, int start) {
int i = start;
for (; i >= 0; i--) {
if (s.charAt(i) != '0') {
break;
}
}
return i;
}
@Nonnull
private String formatDecDefault(@Nonnull Double value) {
final BigDecimal bd;
if (roundResult) {
bd = BigDecimal.valueOf(value).setScale(precision, BigDecimal.ROUND_HALF_UP);
} else {
bd = BigDecimal.valueOf(value);
}
value = bd.doubleValue();
if (value == 0) {
return "0";
}
final DecimalFormat df = Math.abs(value) < Math.pow(10, -5) ? smallNumbersFormat.get() : defaultNumbersFormat.get();
df.setDecimalFormatSymbols(decimalGroupSymbols);
df.setGroupingUsed(useGroupingSeparator);
df.setGroupingSize(NumeralBase.dec.getGroupingSize());
if (roundResult) {
df.setMaximumFractionDigits(precision);
} else {
// set maximum fraction digits high enough to show all fraction digits in case of no rounding
df.setMaximumFractionDigits(MAX_FRACTION_DIGITS);
}
return df.format(value);
return nf.format(value, nb.radix).toString();
}
@Nullable

File diff suppressed because it is too large Load Diff

View File

@ -0,0 +1,136 @@
package org.solovyev.common;
import midpcalc.Real;
import javax.annotation.Nonnull;
import java.math.BigDecimal;
import static midpcalc.Real.NumberFormat.*;
public class NumberFormatter {
public static final int NO_GROUPING = 0;
public static final int DEFAULT_PRECISION = 16;
public static final int DEFAULT_MAGNITUDE = 5;
private final Real.NumberFormat numberFormat = new Real.NumberFormat();
private final Real real = new Real();
private int format = FSE_NONE;
private int simpleFormatMagnitude = DEFAULT_MAGNITUDE;
private int precision = DEFAULT_PRECISION;
private char groupingSeparator;
public void useEngineeringFormat(int simpleFormatMagnitude) {
this.format = FSE_ENG;
this.simpleFormatMagnitude = simpleFormatMagnitude;
}
public void useSimpleFormat() {
this.format = FSE_NONE;
this.simpleFormatMagnitude = DEFAULT_MAGNITUDE;
}
public void setPrecision(int precision) {
this.precision = precision;
}
public void setGroupingSeparator(char groupingSeparator) {
this.groupingSeparator = groupingSeparator;
}
@Nonnull
public CharSequence format(double value) {
return format(value, 10);
}
@Nonnull
public CharSequence format(double value, int radix) {
if (radix != 2 && radix != 8 && radix != 10 && radix != 16) {
throw new IllegalArgumentException("Unsupported radix: " + radix);
}
double absValue = Math.abs(value);
final boolean dec = radix == 10;
final boolean fixedFormat = !dec || format == FSE_NONE || Math.pow(10, -simpleFormatMagnitude) <= absValue && absValue < Math.pow(10, simpleFormatMagnitude);
if (fixedFormat) {
final int newScale = (int) (precision * Math.max(1, radix / 10f));
value = BigDecimal.valueOf(value).setScale(newScale, BigDecimal.ROUND_HALF_UP).doubleValue();
absValue = Math.abs(value);
}
numberFormat.fse = fixedFormat ? FSE_FIX : FSE_ENG;
numberFormat.thousand = groupingSeparator;
numberFormat.precision = precision;
numberFormat.base = radix;
numberFormat.maxwidth = fixedFormat ? 100 : 30;
if (radix == 2 && value < 0) {
return "-" + prepare(absValue);
}
return prepare(value);
}
@Nonnull
private CharSequence prepare(double value) {
return stripZeros(realFormat(value)).replace('e', 'E');
}
@Nonnull
private String realFormat(double value) {
real.assign(Double.toString(value));
return real.toString(numberFormat);
}
@Nonnull
private String stripZeros(@Nonnull String s) {
int dot = -1;
int firstNonZero = -1;
for (int i = 0; i < s.length(); i++) {
final char c = s.charAt(i);
if (c != '0' && c != groupingSeparator && firstNonZero == -1) {
firstNonZero = i;
}
if (c == '.') {
dot = i;
break;
}
}
if (firstNonZero == -1) {
// all zeros
return "";
}
if (dot < 0) {
// no dot - no trailing zeros
return s.substring(firstNonZero);
}
if (firstNonZero == dot) {
// one zero before dot must be kept
firstNonZero--;
}
final int e = s.lastIndexOf('e');
final int i = findLastNonZero(s, e);
final int end = i == dot ? i : i + 1;
return s.substring(firstNonZero, end) + getExponent(s, e);
}
@Nonnull
private String getExponent(@Nonnull String s, int e) {
String exponent = "";
if (e > 0) {
exponent = s.substring(e);
if (exponent.length() == 2 && exponent.charAt(1) == '0') {
exponent = "";
}
}
return exponent;
}
private int findLastNonZero(@Nonnull String s, int e) {
int i = e > 0 ? e - 1 : s.length() - 1;
for (; i >= 0; i--) {
if (s.charAt(i) != '0') {
break;
}
}
return i;
}
}

View File

@ -34,24 +34,23 @@ public class JsclMathEngineTest {
me.setPrecision(10);
assertEquals("111 1111 0011 0110", me.format(32566d, NumeralBase.bin));
assertEquals("100.0100 1100 11", me.format(4.3d, NumeralBase.bin));
assertEquals("1 0001 0101 0011.0101 0101 10", me.format(4435.33423d, NumeralBase.bin));
assertEquals("1100.0101 0101 01", me.format(12.3333d, NumeralBase.bin));
assertEquals("1 0011 1101 1110 0100 0011 0101 0101.0001 1111 00", me.format(333333333.1212213321d, NumeralBase.bin));
assertEquals("100.0100110011", me.format(4.3d, NumeralBase.bin));
assertEquals("1 0001 0101 0011.010101011", me.format(4435.33423d, NumeralBase.bin));
assertEquals("1100.0101010101", me.format(12.3333d, NumeralBase.bin));
assertEquals("1 0011 1101 1110 0100 0011 0101 0101.00011111", me.format(333333333.1212213321d, NumeralBase.bin));
assertEquals("0.EE EE EE EE EE", me.format(14d / 15d, NumeralBase.hex));
assertEquals("0.EEEEEEEEEF", me.format(14d / 15d, NumeralBase.hex));
assertEquals("7F 36", me.format(32566d, NumeralBase.hex));
assertEquals("24", me.format(36d, NumeralBase.hex));
assertEquals("8", me.format(8d, NumeralBase.hex));
assertEquals("1 3D", me.format(317d, NumeralBase.hex));
assertEquals("13 DE 43 55.1F 08 5B EF 14", me.format(333333333.1212213321d, NumeralBase.hex));
assertEquals("D 25 0F 77 0A.6F 73 18 FC 50", me.format(56456345354.43534534523459999d, NumeralBase.hex));
assertEquals("3 E7.4C CC CC CC CC", me.format(999.3d, NumeralBase.hex));
assertEquals("13 DE 43 55.1F085BEF", me.format(333333333.1212213321d, NumeralBase.hex));
assertEquals("D 25 0F 77 0A.6F7319", me.format(56456345354.43534534523459999d, NumeralBase.hex));
assertEquals("3 E7.4CCCCCCCCD", me.format(999.3d, NumeralBase.hex));
me.setRoundResult(false);
assertEquals("0.00 00 00 00 00 00 00 00 00 6C", me.format(0.00000000000000000000009d, NumeralBase.hex));
assertEquals("0.00 00 00 00 00 00 00 00 00 0A", me.format(0.000000000000000000000009d, NumeralBase.hex));
assertEquals("6.CCDA6A054226DB6E-19", me.format(0.00000000000000000000009d, NumeralBase.hex));
assertEquals("A.E15D766ED03E2BEE-20", me.format(0.000000000000000000000009d, NumeralBase.hex));
} finally {
me.setUseGroupingSeparator(false);
}
@ -80,7 +79,7 @@ public class JsclMathEngineTest {
@Test
public void testPiComputation() throws Exception {
final JsclMathEngine me = JsclMathEngine.getInstance();
assertEquals("-1+122.46467991473532E-18*i", me.evaluate("exp(√(-1)*Π)"));
assertEquals("-1+0.0000000000000001*i", me.evaluate("exp(√(-1)*Π)"));
}
@Test
@ -101,8 +100,8 @@ public class JsclMathEngineTest {
assertEquals("111E3", me.format(111000d));
assertEquals("110E3", me.format(110000d));
assertEquals("100E3", me.format(100000d));
assertEquals("10E3", me.format(10000d));
assertEquals("1E3", me.format(1000d));
assertEquals("10000", me.format(10000d));
assertEquals("1000", me.format(1000d));
assertEquals("100", me.format(100d));
assertEquals("100.1", me.format(100.1d));
assertEquals("100.12", me.format(100.12d));
@ -122,8 +121,8 @@ public class JsclMathEngineTest {
assertEquals("0.001", me.format(0.001));
assertEquals("0.001", me.format(0.00100000001));
assertEquals("0.0011", me.format(0.0011));
assertEquals("999.999E-6", me.format(0.000999999));
assertEquals("100E-6", me.format(0.0001));
assertEquals("0.001", me.format(0.000999999));
assertEquals("0.0001", me.format(0.0001));
assertEquals("1E-6", me.format(0.000001));
assertEquals("10E-9", me.format(0.00000001));
@ -156,8 +155,8 @@ public class JsclMathEngineTest {
assertEquals("111E3", me.format(111000d));
assertEquals("110E3", me.format(110000d));
assertEquals("100E3", me.format(100000d));
assertEquals("10E3", me.format(10000d));
assertEquals("1E3", me.format(1000d));
assertEquals("10000", me.format(10000d));
assertEquals("1000", me.format(1000d));
assertEquals("100", me.format(100d));
assertEquals("100.1", me.format(100.1d));
assertEquals("100.12", me.format(100.12d));
@ -176,8 +175,8 @@ public class JsclMathEngineTest {
assertEquals("0.01", me.format(0.01));
assertEquals("0.001", me.format(0.001));
assertEquals("0.0011", me.format(0.0011));
assertEquals("999.999E-6", me.format(0.000999999));
assertEquals("100E-6", me.format(0.0001));
assertEquals("0.000999999", me.format(0.000999999));
assertEquals("0.0001", me.format(0.0001));
assertEquals("100.001E3", me.format(100001d));
assertEquals("111.111E3", me.format(111111d));

View File

@ -4,25 +4,22 @@ 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;
/**
* User: serso
* Date: 11/29/11
* Time: 12:20 PM
*/
import static org.junit.Assert.assertEquals;
public class NumeralBaseTest {
@Test
public void testEvaluation() throws Exception {
MathEngine me = JsclMathEngine.getInstance();
Assert.assertEquals("3", me.evaluate("0b:1+0b:10"));
Assert.assertEquals("5", me.evaluate("0b:1+0b:100"));
Assert.assertEquals("8", me.evaluate("0b:1+0b:100+(0b:1+0b:10)"));
Assert.assertEquals("18", me.evaluate("0b:1+0b:100+(0b:1+0b:10)+10"));
Assert.assertEquals("18.5", me.evaluate("0b:1+0b:100+(0b:1+0b:10)+10.5"));
assertEquals("3", me.evaluate("0b:1+0b:10"));
assertEquals("5", me.evaluate("0b:1+0b:100"));
assertEquals("8", me.evaluate("0b:1+0b:100+(0b:1+0b:10)"));
assertEquals("18", me.evaluate("0b:1+0b:100+(0b:1+0b:10)+10"));
assertEquals("18.5", me.evaluate("0b:1+0b:100+(0b:1+0b:10)+10.5"));
try {
me.evaluate("0b:1+0b:100.+(0b:1+0b:10)+10.5");
Assert.fail();
@ -35,7 +32,7 @@ public class NumeralBaseTest {
} catch (ParseException e) {
}
Assert.assertEquals("2748", me.evaluate("0x:ABC"));
assertEquals("2748", me.evaluate("0x:ABC"));
try {
me.evaluate("0x:");
@ -43,15 +40,15 @@ public class NumeralBaseTest {
} catch (ParseException e) {
}
Assert.assertEquals("0", me.evaluate("0x:0"));
assertEquals("0", me.evaluate("0x:0"));
IConstant constant = null;
try {
final ExtendedConstant.Builder a = new ExtendedConstant.Builder(new Constant("a"), 2d);
constant = me.getConstantsRegistry().addOrUpdate(a.create());
Assert.assertEquals("2748", me.evaluate("0x:ABC"));
Assert.assertEquals("5496", me.evaluate("0x:ABC*a"));
Assert.assertEquals("27480", me.evaluate("0x:ABC*0x:A"));
assertEquals("2748", me.evaluate("0x:ABC"));
assertEquals("5496", me.evaluate("0x:ABC*a"));
assertEquals("27480", me.evaluate("0x:ABC*0x:A"));
} finally {
if (constant != null) {
final ExtendedConstant.Builder a = new ExtendedConstant.Builder(new Constant("a"), (String) null);
@ -67,24 +64,24 @@ public class NumeralBaseTest {
final NumeralBase defaultNumeralBase = me.getNumeralBase();
try {
me.setNumeralBase(NumeralBase.bin);
Assert.assertEquals("", me.evaluate(""));
Assert.assertEquals("-1011010+110101111.10000110101100011010*i", me.evaluate("asin(-1110100101)"));
Assert.assertEquals("11", me.evaluate("0b:1+0b:10"));
Assert.assertEquals("10", me.evaluate("0d:2"));
Assert.assertEquals("11", me.evaluate("0d:3"));
Assert.assertEquals("100", me.evaluate("0d:4"));
Assert.assertEquals("11111111", me.evaluate("0d:255"));
Assert.assertEquals("11", me.evaluate("1+10"));
Assert.assertEquals("-1", me.evaluate("1-10"));
Assert.assertEquals("11-i", me.evaluate("1+i+10-10*i"));
Assert.assertEquals("11111110", me.evaluate("111001+11000101"));
Assert.assertEquals("1101100100101111", me.evaluate("11011001001011110/10"));
Assert.assertEquals("1001000011001010", me.evaluate("11011001001011110/11"));
Assert.assertEquals("0.10101010101010101010", me.evaluate("10/11"));
assertEquals("", me.evaluate(""));
assertEquals("-1011010+110101111.100001101011001*i", me.evaluate("asin(-1110100101)"));
assertEquals("11", me.evaluate("0b:1+0b:10"));
assertEquals("10", me.evaluate("0d:2"));
assertEquals("11", me.evaluate("0d:3"));
assertEquals("100", me.evaluate("0d:4"));
assertEquals("11111111", me.evaluate("0d:255"));
assertEquals("11", me.evaluate("1+10"));
assertEquals("-1", me.evaluate("1-10"));
assertEquals("11-i", me.evaluate("1+i+10-10*i"));
assertEquals("11111110", me.evaluate("111001+11000101"));
assertEquals("1101100100101111", me.evaluate("11011001001011110/10"));
assertEquals("1001000011001010", me.evaluate("11011001001011110/11"));
assertEquals("0.1010101010101011", me.evaluate("10/11"));
me.setNumeralBase(NumeralBase.hex);
org.junit.Assert.assertEquals("637B", me.evaluate("56CE+CAD"));
org.junit.Assert.assertEquals("637B", me.simplify("56CE+CAD"));
assertEquals("637B", me.evaluate("56CE+CAD"));
assertEquals("637B", me.simplify("56CE+CAD"));
} finally {
me.setNumeralBase(defaultNumeralBase);

View File

@ -8,7 +8,6 @@ import jscl.math.function.Constant;
import jscl.math.function.ExtendedConstant;
import jscl.math.function.IConstant;
import jscl.text.ParseException;
import org.junit.Assert;
import org.junit.Test;
import javax.annotation.Nonnull;
@ -21,9 +20,7 @@ import java.net.URL;
import java.net.URLConnection;
import java.util.Set;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
import static org.junit.Assert.*;
public class ExpressionTest {
@ -102,7 +99,7 @@ public class ExpressionTest {
@Test
public void testImag() throws Exception {
Assert.assertEquals("-i", Expression.valueOf("i^3").numeric().toString());
assertEquals("-i", Expression.valueOf("i^3").numeric().toString());
}
@Test
@ -127,9 +124,9 @@ public class ExpressionTest {
final Expression expression = Expression.valueOf("2*t_0+5*t_1");
Assert.assertEquals("7", expression.substitute(new Constant("t_1"), Expression.valueOf(1.0)).numeric().toString());
Assert.assertEquals("12", expression.substitute(new Constant("t_1"), Expression.valueOf(2.0)).numeric().toString());
Assert.assertEquals("27", expression.substitute(new Constant("t_1"), Expression.valueOf(5.0)).numeric().toString());
assertEquals("7", expression.substitute(new Constant("t_1"), Expression.valueOf(1.0)).numeric().toString());
assertEquals("12", expression.substitute(new Constant("t_1"), Expression.valueOf(2.0)).numeric().toString());
assertEquals("27", expression.substitute(new Constant("t_1"), Expression.valueOf(5.0)).numeric().toString());
} finally {
if (constant != null) {
@ -141,138 +138,138 @@ public class ExpressionTest {
@Test
public void testExpressions() throws Exception {
Assert.assertEquals("3", Expression.valueOf("3").numeric().toString());
Assert.assertEquals("0.6931471805599453", Expression.valueOf("ln(2)").numeric().toString());
Assert.assertEquals("1", Expression.valueOf("lg(10)").numeric().toString());
Assert.assertEquals("0", Expression.valueOf("eq(0, 1)").numeric().toString());
Assert.assertEquals("1", Expression.valueOf("eq(1, 1)").numeric().toString());
assertEquals("3", Expression.valueOf("3").numeric().toString());
assertEquals("0.6931471805599453", Expression.valueOf("ln(2)").numeric().toString());
assertEquals("1", Expression.valueOf("lg(10)").numeric().toString());
assertEquals("0", Expression.valueOf("eq(0, 1)").numeric().toString());
assertEquals("1", Expression.valueOf("eq(1, 1)").numeric().toString());
Assert.assertEquals("24", Expression.valueOf("4!").numeric().toString());
assertEquals("24", Expression.valueOf("4!").numeric().toString());
try {
Expression.valueOf("(-3+2)!").numeric().toString();
fail();
} catch (ArithmeticException e) {
}
Assert.assertEquals("24", Expression.valueOf("(2+2)!").numeric().toString());
Assert.assertEquals("120", Expression.valueOf("(2+2+1)!").numeric().toString());
Assert.assertEquals("24", Expression.valueOf("(2.0+2.0)!").numeric().toString());
Assert.assertEquals("24", Expression.valueOf("4.0!").numeric().toString());
Assert.assertEquals("48", Expression.valueOf("2*4.0!").numeric().toString());
Assert.assertEquals("40320", Expression.valueOf("(2*4.0)!").numeric().toString());
assertEquals("24", Expression.valueOf("(2+2)!").numeric().toString());
assertEquals("120", Expression.valueOf("(2+2+1)!").numeric().toString());
assertEquals("24", Expression.valueOf("(2.0+2.0)!").numeric().toString());
assertEquals("24", Expression.valueOf("4.0!").numeric().toString());
assertEquals("48", Expression.valueOf("2*4.0!").numeric().toString());
assertEquals("40320", Expression.valueOf("(2*4.0)!").numeric().toString());
final JsclMathEngine me = JsclMathEngine.getInstance();
final AngleUnit angleUnits = me.getAngleUnits();
try {
me.setAngleUnits(AngleUnit.rad);
Assert.assertEquals("-0.9055783620066238", Expression.valueOf("sin(4!)").numeric().toString());
assertEquals("-0.9055783620066238", Expression.valueOf("sin(4!)").numeric().toString());
} finally {
me.setAngleUnits(angleUnits);
}
Assert.assertEquals("1", Expression.valueOf("(3.14/3.14)!").numeric().toString());
Assert.assertEquals("1", Expression.valueOf("2/2!").numeric().toString());
assertEquals("1", Expression.valueOf("(3.14/3.14)!").numeric().toString());
assertEquals("1", Expression.valueOf("2/2!").numeric().toString());
try {
Assert.assertEquals("3.141592653589793!", Expression.valueOf("3.141592653589793!").numeric().toString());
assertEquals("3.141592653589793!", Expression.valueOf("3.141592653589793!").numeric().toString());
fail();
} catch (NotIntegerException e) {
}
Assert.assertEquals("0.5235987755982988", Expression.valueOf("3.141592653589793/3!").numeric().toString());
assertEquals("0.5235987755982988", Expression.valueOf("3.141592653589793/3!").numeric().toString());
try {
Assert.assertEquals("3.141592653589793/3.141592653589793!", Expression.valueOf("3.141592653589793/3.141592653589793!").numeric().toString());
assertEquals("3.141592653589793/3.141592653589793!", Expression.valueOf("3.141592653589793/3.141592653589793!").numeric().toString());
fail();
} catch (ArithmeticException e) {
}
try {
Assert.assertEquals("7.2!", Expression.valueOf("7.2!").numeric().toString());
assertEquals("7.2!", Expression.valueOf("7.2!").numeric().toString());
fail();
} catch (NotIntegerException e) {
}
try {
Assert.assertEquals("ln(7.2!)", Expression.valueOf("ln(7.2!)").numeric().toString());
assertEquals("ln(7.2!)", Expression.valueOf("ln(7.2!)").numeric().toString());
fail();
} catch (NotIntegerException e) {
}
Assert.assertEquals("ln(7.2!)", Expression.valueOf("ln(7.2!)").simplify().toString());
assertEquals("ln(7.2!)", Expression.valueOf("ln(7.2!)").simplify().toString());
Assert.assertEquals("36", Expression.valueOf("3!^2").numeric().toString());
Assert.assertEquals("1", Expression.valueOf("(π/π)!").numeric().toString());
Assert.assertEquals("720", Expression.valueOf("(3!)!").numeric().toString());
Assert.assertEquals("36", Expression.valueOf("3!*3!").numeric().toString());
assertEquals("36", Expression.valueOf("3!^2").numeric().toString());
assertEquals("1", Expression.valueOf("(π/π)!").numeric().toString());
assertEquals("720", Expression.valueOf("(3!)!").numeric().toString());
assertEquals("36", Expression.valueOf("3!*3!").numeric().toString());
Assert.assertEquals("100", Expression.valueOf("0.1E3").numeric().toString());
assertEquals("100", Expression.valueOf("0.1E3").numeric().toString());
final AngleUnit defaultAngleUnits = me.getAngleUnits();
try {
me.setAngleUnits(AngleUnit.rad);
Assert.assertEquals("0.017453292519943295", Expression.valueOf("").numeric().toString());
Assert.assertEquals("0.03490658503988659", Expression.valueOf("").numeric().toString());
Assert.assertEquals("0.05235987755982989", Expression.valueOf("").numeric().toString());
Assert.assertEquals("0.26179938779914946", Expression.valueOf("3°*5").numeric().toString());
Assert.assertEquals("0.0027415567780803775", Expression.valueOf("3°^2").numeric().toString());
Assert.assertEquals("0.01096622711232151", Expression.valueOf("3!°^2").numeric().toString());
Assert.assertEquals("0.0009138522593601259", Expression.valueOf("3°°").numeric().toString());
Assert.assertEquals("0.08726646259971647", Expression.valueOf("").numeric().toString());
Assert.assertEquals("2.0523598775598297", Expression.valueOf("2+3°").numeric().toString());
assertEquals("0.0174532925199433", Expression.valueOf("").numeric().toString());
assertEquals("0.0349065850398866", Expression.valueOf("").numeric().toString());
assertEquals("0.0523598775598299", Expression.valueOf("").numeric().toString());
assertEquals("0.2617993877991495", Expression.valueOf("3°*5").numeric().toString());
assertEquals("0.0027415567780804", Expression.valueOf("3°^2").numeric().toString());
assertEquals("0.0109662271123215", Expression.valueOf("3!°^2").numeric().toString());
assertEquals("0.0009138522593601", Expression.valueOf("3°°").numeric().toString());
assertEquals("0.0872664625997165", Expression.valueOf("").numeric().toString());
assertEquals("2.05235987755983", Expression.valueOf("2+3°").numeric().toString());
} finally {
me.setAngleUnits(defaultAngleUnits);
}
try {
me.setAngleUnits(AngleUnit.deg);
Assert.assertEquals("1", Expression.valueOf("").numeric().toString());
Assert.assertEquals("2", Expression.valueOf("").numeric().toString());
Assert.assertEquals("3", Expression.valueOf("").numeric().toString());
Assert.assertEquals("15", Expression.valueOf("3°*5").numeric().toString());
Assert.assertEquals("9", Expression.valueOf("3°^2").numeric().toString());
Assert.assertEquals("36", Expression.valueOf("3!°^2").numeric().toString());
Assert.assertEquals("3", Expression.valueOf("3°°").numeric().toString());
Assert.assertEquals("5", Expression.valueOf("").numeric().toString());
Assert.assertEquals("5", Expression.valueOf("2+3°").numeric().toString());
assertEquals("1", Expression.valueOf("").numeric().toString());
assertEquals("2", Expression.valueOf("").numeric().toString());
assertEquals("3", Expression.valueOf("").numeric().toString());
assertEquals("15", Expression.valueOf("3°*5").numeric().toString());
assertEquals("9", Expression.valueOf("3°^2").numeric().toString());
assertEquals("36", Expression.valueOf("3!°^2").numeric().toString());
assertEquals("3", Expression.valueOf("3°°").numeric().toString());
assertEquals("5", Expression.valueOf("").numeric().toString());
assertEquals("5", Expression.valueOf("2+3°").numeric().toString());
} finally {
me.setAngleUnits(defaultAngleUnits);
}
Assert.assertEquals("6", Expression.valueOf("2*∂(3*x,x)").expand().toString());
Assert.assertEquals("3", Expression.valueOf("∂(3*x,x)").expand().toString());
Assert.assertEquals("12", Expression.valueOf("∂(x^3,x,2)").expand().toString());
Assert.assertEquals("3*a", Expression.valueOf("∂(3*x*a,x)").expand().toString());
Assert.assertEquals("0", Expression.valueOf("∂(3*x*a,x,0.011,2)").expand().toString());
Assert.assertEquals("0", Expression.valueOf("2*∂(3*x*a,x,0.011,2)").expand().toString());
Assert.assertEquals("ln(8)+lg(8)*ln(8)", Expression.valueOf("ln(8)*lg(8)+ln(8)").expand().toString());
Assert.assertEquals("3.9573643765059856", Expression.valueOf("ln(8)*lg(8)+ln(8)").numeric().toString());
assertEquals("6", Expression.valueOf("2*∂(3*x,x)").expand().toString());
assertEquals("3", Expression.valueOf("∂(3*x,x)").expand().toString());
assertEquals("12", Expression.valueOf("∂(x^3,x,2)").expand().toString());
assertEquals("3*a", Expression.valueOf("∂(3*x*a,x)").expand().toString());
assertEquals("0", Expression.valueOf("∂(3*x*a,x,0.011,2)").expand().toString());
assertEquals("0", Expression.valueOf("2*∂(3*x*a,x,0.011,2)").expand().toString());
assertEquals("ln(8)+lg(8)*ln(8)", Expression.valueOf("ln(8)*lg(8)+ln(8)").expand().toString());
assertEquals("3.957364376505986", Expression.valueOf("ln(8)*lg(8)+ln(8)").numeric().toString());
Assert.assertEquals("4!", Expression.valueOf("4.0!").simplify().toString());
Assert.assertEquals("", Expression.valueOf("4.0°").simplify().toString());
Assert.assertEquals("30°", Expression.valueOf("30°").simplify().toString());
assertEquals("4!", Expression.valueOf("4.0!").simplify().toString());
assertEquals("", Expression.valueOf("4.0°").simplify().toString());
assertEquals("30°", Expression.valueOf("30°").simplify().toString());
Assert.assertEquals("1", Expression.valueOf("abs(1)").numeric().toString());
Assert.assertEquals("0", Expression.valueOf("abs(0)").numeric().toString());
Assert.assertEquals("0", Expression.valueOf("abs(-0)").numeric().toString());
Assert.assertEquals("1", Expression.valueOf("abs(-1)").numeric().toString());
Assert.assertEquals("", Expression.valueOf("abs(-∞)").numeric().toString());
assertEquals("1", Expression.valueOf("abs(1)").numeric().toString());
assertEquals("0", Expression.valueOf("abs(0)").numeric().toString());
assertEquals("0", Expression.valueOf("abs(-0)").numeric().toString());
assertEquals("1", Expression.valueOf("abs(-1)").numeric().toString());
assertEquals("", Expression.valueOf("abs(-∞)").numeric().toString());
Assert.assertEquals("1", Expression.valueOf("abs(i)").numeric().toString());
Assert.assertEquals("0", Expression.valueOf("abs(0+0*i)").numeric().toString());
Assert.assertEquals("1", Expression.valueOf("abs(-i)").numeric().toString());
Assert.assertEquals("2.23606797749979", Expression.valueOf("abs(2-i)").numeric().toString());
Assert.assertEquals("2.23606797749979", Expression.valueOf("abs(2+i)").numeric().toString());
Assert.assertEquals("2.8284271247461903", Expression.valueOf("abs(2+2*i)").numeric().toString());
Assert.assertEquals("2.8284271247461903", Expression.valueOf("abs(2-2*i)").numeric().toString());
assertEquals("1", Expression.valueOf("abs(i)").numeric().toString());
assertEquals("0", Expression.valueOf("abs(0+0*i)").numeric().toString());
assertEquals("1", Expression.valueOf("abs(-i)").numeric().toString());
assertEquals("2.23606797749979", Expression.valueOf("abs(2-i)").numeric().toString());
assertEquals("2.23606797749979", Expression.valueOf("abs(2+i)").numeric().toString());
assertEquals("2.82842712474619", Expression.valueOf("abs(2+2*i)").numeric().toString());
assertEquals("2.82842712474619", Expression.valueOf("abs(2-2*i)").numeric().toString());
try {
final ExtendedConstant.Builder k = new ExtendedConstant.Builder(new Constant("k"), 2.8284271247461903);
me.getConstantsRegistry().addOrUpdate(k.create());
Assert.assertEquals("k", Expression.valueOf("k").numeric().toString());
Assert.assertEquals("k", Expression.valueOf("k").simplify().toString());
Assert.assertEquals("k", Expression.valueOf("k").simplify().toString());
Assert.assertEquals("k^3", Expression.valueOf("k*k*k").simplify().toString());
Assert.assertEquals("22.627416997969526", Expression.valueOf("k*k*k").numeric().toString());
assertEquals("k", Expression.valueOf("k").numeric().toString());
assertEquals("k", Expression.valueOf("k").simplify().toString());
assertEquals("k", Expression.valueOf("k").simplify().toString());
assertEquals("k^3", Expression.valueOf("k*k*k").simplify().toString());
assertEquals("22.62741699796953", Expression.valueOf("k*k*k").numeric().toString());
} finally {
final ExtendedConstant.Builder k = new ExtendedConstant.Builder(new Constant("k"), (String) null);
me.getConstantsRegistry().addOrUpdate(k.create());
@ -281,9 +278,9 @@ public class ExpressionTest {
try {
final ExtendedConstant.Builder k_1 = new ExtendedConstant.Builder(new Constant("k_1"), 3d);
me.getConstantsRegistry().addOrUpdate(k_1.create());
Assert.assertEquals("k_1", Expression.valueOf("k_1").numeric().toString());
Assert.assertEquals("k_1", Expression.valueOf("k_1[0]").numeric().toString());
Assert.assertEquals("k_1", Expression.valueOf("k_1[2]").numeric().toString());
assertEquals("k_1", Expression.valueOf("k_1").numeric().toString());
assertEquals("k_1", Expression.valueOf("k_1[0]").numeric().toString());
assertEquals("k_1", Expression.valueOf("k_1[2]").numeric().toString());
} finally {
final ExtendedConstant.Builder k_1 = new ExtendedConstant.Builder(new Constant("k_1"), (String) null);
me.getConstantsRegistry().addOrUpdate(k_1.create());
@ -291,67 +288,67 @@ public class ExpressionTest {
Generic expression = me.simplifyGeneric("cos(t)+∂(cos(t),t)");
Generic substituted = expression.substitute(new Constant("t"), Expression.valueOf(100d));
Assert.assertEquals("-1.1584559306791382", substituted.numeric().toString());
assertEquals("-1.158455930679138", substituted.numeric().toString());
expression = me.simplifyGeneric("abs(t)^2+2!");
substituted = expression.substitute(new Constant("t"), Expression.valueOf(10d));
Assert.assertEquals("102", substituted.numeric().toString());
assertEquals("102", substituted.numeric().toString());
expression = me.simplifyGeneric("abs(t)^2+10%");
substituted = expression.substitute(new Constant("t"), Expression.valueOf(10d));
Assert.assertEquals("110", substituted.numeric().toString());
assertEquals("110", substituted.numeric().toString());
expression = me.simplifyGeneric("abs(t)^2-10%");
substituted = expression.substitute(new Constant("t"), Expression.valueOf(10d));
Assert.assertEquals("90", substituted.numeric().toString());
assertEquals("90", substituted.numeric().toString());
expression = me.simplifyGeneric("(abs(t)^2)*10%");
substituted = expression.substitute(new Constant("t"), Expression.valueOf(10d));
Assert.assertEquals("10", substituted.numeric().toString());
assertEquals("10", substituted.numeric().toString());
expression = me.simplifyGeneric("(abs(t)^2)/10%");
substituted = expression.substitute(new Constant("t"), Expression.valueOf(10d));
Assert.assertEquals("1000", substituted.numeric().toString());
assertEquals("1000", substituted.numeric().toString());
expression = me.simplifyGeneric("abs(t)^2+t%");
substituted = expression.substitute(new Constant("t"), Expression.valueOf(10d));
Assert.assertEquals("110", substituted.numeric().toString());
assertEquals("110", substituted.numeric().toString());
expression = me.simplifyGeneric("abs(t)^2-t%");
substituted = expression.substitute(new Constant("t"), Expression.valueOf(10d));
Assert.assertEquals("90", substituted.numeric().toString());
assertEquals("90", substituted.numeric().toString());
expression = me.simplifyGeneric("(abs(t)^2)*t%");
substituted = expression.substitute(new Constant("t"), Expression.valueOf(10d));
Assert.assertEquals("10", substituted.numeric().toString());
assertEquals("10", substituted.numeric().toString());
expression = me.simplifyGeneric("(abs(t)^2)/t%");
substituted = expression.substitute(new Constant("t"), Expression.valueOf(10d));
Assert.assertEquals("1000", substituted.numeric().toString());
assertEquals("1000", substituted.numeric().toString());
expression = me.simplifyGeneric("Σ(t, t, 0, 10)");
Assert.assertEquals("55", expression.numeric().toString());
assertEquals("55", expression.numeric().toString());
expression = me.simplifyGeneric("Σ(t, t, 0, 10)");
substituted = expression.substitute(new Constant("t"), Expression.valueOf(10d));
Assert.assertEquals("55", substituted.numeric().toString());
assertEquals("55", substituted.numeric().toString());
expression = me.simplifyGeneric("10*Σ(t, t, 0, 10)");
substituted = expression.substitute(new Constant("t"), Expression.valueOf(10d));
Assert.assertEquals("550", substituted.numeric().toString());
assertEquals("550", substituted.numeric().toString());
expression = me.simplifyGeneric("t*Σ(t, t, 0, 10)");
substituted = expression.substitute(new Constant("t"), Expression.valueOf(10d));
Assert.assertEquals("550", substituted.numeric().toString());
assertEquals("550", substituted.numeric().toString());
expression = me.simplifyGeneric("t*Σ(t+100%, t, 0, 10)");
substituted = expression.substitute(new Constant("t"), Expression.valueOf(10d));
Assert.assertEquals("1100", substituted.numeric().toString());
assertEquals("1100", substituted.numeric().toString());
Assert.assertEquals("i*t", Expression.valueOf("i*t").expand().simplify().toString());
Assert.assertEquals("t", Expression.valueOf("t").simplify().toString());
Assert.assertEquals("t^3", Expression.valueOf("t*t*t").simplify().toString());
assertEquals("i*t", Expression.valueOf("i*t").expand().simplify().toString());
assertEquals("t", Expression.valueOf("t").simplify().toString());
assertEquals("t^3", Expression.valueOf("t*t*t").simplify().toString());
try {
Expression.valueOf("t").numeric();
@ -367,60 +364,60 @@ public class ExpressionTest {
} catch (ArithmeticException e) {
}
Assert.assertEquals("√(1+t)/(1+t)", Expression.valueOf("1/√(1+t)").simplify().toString());
assertEquals("√(1+t)/(1+t)", Expression.valueOf("1/√(1+t)").simplify().toString());
Assert.assertEquals("t", Expression.valueOf("t").simplify().toString());
Assert.assertEquals("t^3", Expression.valueOf("t*t*t").simplify().toString());
assertEquals("t", Expression.valueOf("t").simplify().toString());
assertEquals("t^3", Expression.valueOf("t*t*t").simplify().toString());
try {
me.setAngleUnits(AngleUnit.rad);
Assert.assertEquals("0.6931471805599453+Π*i", Expression.valueOf("ln(-2)").numeric().toString());
assertEquals("0.6931471805599453+Π*i", Expression.valueOf("ln(-2)").numeric().toString());
} finally {
me.setAngleUnits(AngleUnit.deg);
}
Assert.assertEquals("-2/57", Expression.valueOf("1/(-57/2)").simplify().toString());
Assert.assertEquals("sin(30)", Expression.valueOf("sin(30)").expand().toString());
Assert.assertEquals("sin(n)", Expression.valueOf("sin(n)").expand().toString());
Assert.assertEquals("sin(n!)", Expression.valueOf("sin(n!)").expand().toString());
Assert.assertEquals("sin(n°)", Expression.valueOf("sin(n°)").expand().toString());
Assert.assertEquals("sin(30°)", Expression.valueOf("sin(30°)").expand().toString());
Assert.assertEquals("0.49999999999999994", Expression.valueOf("sin(30°)").expand().numeric().toString());
Assert.assertEquals("sin(2!)", Expression.valueOf("sin(2!)").expand().toString());
assertEquals("-2/57", Expression.valueOf("1/(-57/2)").simplify().toString());
assertEquals("sin(30)", Expression.valueOf("sin(30)").expand().toString());
assertEquals("sin(n)", Expression.valueOf("sin(n)").expand().toString());
assertEquals("sin(n!)", Expression.valueOf("sin(n!)").expand().toString());
assertEquals("sin(n°)", Expression.valueOf("sin(n°)").expand().toString());
assertEquals("sin(30°)", Expression.valueOf("sin(30°)").expand().toString());
assertEquals("0.4999999999999999", Expression.valueOf("sin(30°)").expand().numeric().toString());
assertEquals("sin(2!)", Expression.valueOf("sin(2!)").expand().toString());
Assert.assertEquals("12", Expression.valueOf("3*(3+1)").expand().toString());
Assert.assertEquals("114.59155902616465", Expression.valueOf("deg(2)").numeric().toString());
assertEquals("12", Expression.valueOf("3*(3+1)").expand().toString());
assertEquals("114.5915590261647", Expression.valueOf("deg(2)").numeric().toString());
try {
Assert.assertEquals("-0.1425465430742778", Expression.valueOf("∏(tan(3))").numeric().toString());
assertEquals("-0.1425465430742778", Expression.valueOf("∏(tan(3))").numeric().toString());
fail();
} catch (ParseException e) {
}
try {
Assert.assertEquals("-0.14255", Expression.valueOf("sin(2,2)").expand().numeric().toString());
assertEquals("-0.14255", Expression.valueOf("sin(2,2)").expand().numeric().toString());
fail();
} catch (ParseException e) {
}
try {
Assert.assertEquals("114.59155902616465", Expression.valueOf("deg(2,2)").numeric().toString());
assertEquals("114.59155902616465", Expression.valueOf("deg(2,2)").numeric().toString());
fail();
} catch (ParseException e) {
}
Assert.assertEquals("0.49999999999999994", Expression.valueOf("sin(30°)").numeric().toString());
Assert.assertEquals("π", Expression.valueOf("√(π)^2").simplify().toString());
Assert.assertEquals("π", Expression.valueOf("√(π^2)").simplify().toString());
Assert.assertEquals("π^2", Expression.valueOf("√(π^2*π^2)").simplify().toString());
Assert.assertEquals("π^3", Expression.valueOf("√(π^4*π^2)").simplify().toString());
Assert.assertEquals("e*π^2", Expression.valueOf("√(π^4*e^2)").simplify().toString());
assertEquals("0.4999999999999999", Expression.valueOf("sin(30°)").numeric().toString());
assertEquals("π", Expression.valueOf("√(π)^2").simplify().toString());
assertEquals("π", Expression.valueOf("√(π^2)").simplify().toString());
assertEquals("π^2", Expression.valueOf("√(π^2*π^2)").simplify().toString());
assertEquals("π^3", Expression.valueOf("√(π^4*π^2)").simplify().toString());
assertEquals("e*π^2", Expression.valueOf("√(π^4*e^2)").simplify().toString());
Assert.assertEquals("1", Expression.valueOf("(π/π)!").numeric().toString());
assertEquals("1", Expression.valueOf("(π/π)!").numeric().toString());
// in deg mode π=180 and factorial of 180 is calculating
Assert.assertEquals("0", Expression.valueOf("Π/Π!").numeric().toString());
assertEquals("0", Expression.valueOf("Π/Π!").numeric().toString());
Assert.assertEquals("122.46467991473532E-18*i", Expression.valueOf("exp((Π*i))+1").numeric().toString());
Assert.assertEquals("20*x^3", Expression.valueOf("∂(5*x^4, x)").expand().simplify().toString());
Assert.assertEquals("25*x", Expression.valueOf("5*x*5").expand().simplify().toString());
Assert.assertEquals("20*x", Expression.valueOf("5*x*4").expand().simplify().toString());
assertEquals("0.0000000000000001*i", Expression.valueOf("exp((Π*i))+1").numeric().toString());
assertEquals("20*x^3", Expression.valueOf("∂(5*x^4, x)").expand().simplify().toString());
assertEquals("25*x", Expression.valueOf("5*x*5").expand().simplify().toString());
assertEquals("20*x", Expression.valueOf("5*x*4").expand().simplify().toString());
try {
me.evaluate("0b:π");
@ -439,11 +436,11 @@ public class ExpressionTest {
try {
me.setNumeralBase(NumeralBase.hex);
Assert.assertEquals("0.EEEEEEEEEEEEEC880AB7", me.evaluate("0x:E/0x:F"));
Assert.assertEquals("E/F", me.simplify("0x:E/0x:F"));
assertEquals("0.EEEEEEEEEEEEEC88", me.evaluate("0x:E/0x:F"));
assertEquals("E/F", me.simplify("0x:E/0x:F"));
Assert.assertEquals("0.EEEEEEEEEEEEEC880AB7", me.evaluate("E/F"));
Assert.assertEquals("E/F", me.simplify("E/F"));
assertEquals("0.EEEEEEEEEEEEEC88", me.evaluate("E/F"));
assertEquals("E/F", me.simplify("E/F"));
} finally {
me.setNumeralBase(NumeralBase.dec);
@ -451,58 +448,58 @@ public class ExpressionTest {
try {
me.setAngleUnits(AngleUnit.rad);
Assert.assertEquals("-1.5707963267948966+2.993222846126381*i", me.evaluate("asin(-10)"));
Assert.assertEquals("-1.5707963267948966+1.3169578969248166*i", me.evaluate("asin(-2)"));
Assert.assertEquals("-1.5707963267948966", me.evaluate("asin(-1)"));
Assert.assertEquals("0", me.evaluate("asin(0)"));
Assert.assertEquals("1.5707963267948966", me.evaluate("asin(1)"));
Assert.assertEquals("1.5707963267948966-1.3169578969248166*i", me.evaluate("asin(2)"));
Assert.assertEquals("1.5707963267948966-2.993222846126381*i", me.evaluate("asin(10)"));
assertEquals("-1.570796326794897+2.993222846126381*i", me.evaluate("asin(-10)"));
assertEquals("-1.570796326794897+1.316957896924817*i", me.evaluate("asin(-2)"));
assertEquals("-1.570796326794897", me.evaluate("asin(-1)"));
assertEquals("0", me.evaluate("asin(0)"));
assertEquals("1.570796326794897", me.evaluate("asin(1)"));
assertEquals("1.570796326794897-1.316957896924817*i", me.evaluate("asin(2)"));
assertEquals("1.570796326794897-2.993222846126381*i", me.evaluate("asin(10)"));
Assert.assertEquals("Π-2.9932228461263786*i", me.evaluate("acos(-10)"));
Assert.assertEquals("Π-1.3169578969248164*i", me.evaluate("acos(-2)"));
Assert.assertEquals("Π", me.evaluate("acos(-1)"));
Assert.assertEquals("1.5707963267948966", me.evaluate("acos(0)"));
Assert.assertEquals("0", me.evaluate("acos(1)"));
Assert.assertEquals("1.3169578969248164*i", me.evaluate("acos(2)"));
Assert.assertEquals("2.9932228461263786*i", me.evaluate("acos(10)"));
assertEquals("Π-2.993222846126379*i", me.evaluate("acos(-10)"));
assertEquals("Π-1.316957896924816*i", me.evaluate("acos(-2)"));
assertEquals("Π", me.evaluate("acos(-1)"));
assertEquals("1.570796326794897", me.evaluate("acos(0)"));
assertEquals("0", me.evaluate("acos(1)"));
assertEquals("1.316957896924816*i", me.evaluate("acos(2)"));
assertEquals("2.993222846126379*i", me.evaluate("acos(10)"));
Assert.assertEquals("-1.4711276743037347", me.evaluate("atan(-10)"));
Assert.assertEquals("-1.1071487177940904", me.evaluate("atan(-2)"));
Assert.assertEquals("-0.7853981633974483", me.evaluate("atan(-1)"));
Assert.assertEquals("0", me.evaluate("atan(0)"));
Assert.assertEquals("0.7853981633974483", me.evaluate("atan(1)"));
Assert.assertEquals("1.1071487177940904", me.evaluate("atan(2)"));
Assert.assertEquals("1.4711276743037347", me.evaluate("atan(10)"));
assertEquals("-1.471127674303735", me.evaluate("atan(-10)"));
assertEquals("-1.10714871779409", me.evaluate("atan(-2)"));
assertEquals("-0.7853981633974483", me.evaluate("atan(-1)"));
assertEquals("0", me.evaluate("atan(0)"));
assertEquals("0.7853981633974483", me.evaluate("atan(1)"));
assertEquals("1.10714871779409", me.evaluate("atan(2)"));
assertEquals("1.471127674303735", me.evaluate("atan(10)"));
for (int i = -10; i < 10; i++) {
Assert.assertEquals(me.evaluate("3.14159265358979323846/2 - atan(" + i + ")"), me.evaluate("acot(" + i + ")"));
assertEquals(me.evaluate("3.14159265358979323846/2 - atan(" + i + ")"), me.evaluate("acot(" + i + ")"));
}
Assert.assertEquals("3.0419240010986313", me.evaluate("3.14159265358979323846/2 - atan(-10)"));
Assert.assertEquals("3.0419240010986313", me.evaluate("acot(-10)"));
Assert.assertEquals("1.5707963267948966", me.evaluate("acot(0)"));
Assert.assertEquals("2.677945044588987", me.evaluate("acot(-2)"));
Assert.assertEquals("2.356194490192345", me.evaluate("acot(-1)"));
Assert.assertEquals("0.7853981633974483", me.evaluate("acot(1)"));
Assert.assertEquals("0.46364760900080615", me.evaluate("acot(2)"));
Assert.assertEquals("0.09966865249116186", me.evaluate("acot(10)"));
assertEquals("3.041924001098631", me.evaluate("3.14159265358979323846/2 - atan(-10)"));
assertEquals("3.041924001098631", me.evaluate("acot(-10)"));
assertEquals("1.570796326794897", me.evaluate("acot(0)"));
assertEquals("2.677945044588987", me.evaluate("acot(-2)"));
assertEquals("2.356194490192345", me.evaluate("acot(-1)"));
assertEquals("0.7853981633974483", me.evaluate("acot(1)"));
assertEquals("0.4636476090008062", me.evaluate("acot(2)"));
assertEquals("0.0996686524911619", me.evaluate("acot(10)"));
Assert.assertEquals("Π", me.evaluate("π"));
Assert.assertEquals("Π", me.evaluate("3.14159265358979323846"));
assertEquals("Π", me.evaluate("π"));
assertEquals("Π", me.evaluate("3.14159265358979323846"));
} finally {
me.setAngleUnits(AngleUnit.deg);
}
Assert.assertEquals("180", me.evaluate("Π"));
Assert.assertEquals("180", me.evaluate("200-10%"));
assertEquals("180", me.evaluate("Π"));
assertEquals("180", me.evaluate("200-10%"));
Assert.assertEquals("", me.evaluate("1/0"));
Assert.assertEquals("-∞", me.evaluate("-1/0"));
Assert.assertEquals("-∞", me.evaluate("-1/0"));
Assert.assertEquals("", me.evaluate("(1 + 2) / (5 - 3 - 2)"));
Assert.assertEquals("", me.evaluate("(1 + 2) / (5.1 - 3.1 - 2.0 )"));
Assert.assertEquals("", me.evaluate("1/0"));
assertEquals("", me.evaluate("1/0"));
assertEquals("-∞", me.evaluate("-1/0"));
assertEquals("-∞", me.evaluate("-1/0"));
assertEquals("", me.evaluate("(1 + 2) / (5 - 3 - 2)"));
assertEquals("", me.evaluate("(1 + 2) / (5.1 - 3.1 - 2.0 )"));
assertEquals("", me.evaluate("1/0"));
}
@Test
@ -523,111 +520,111 @@ public class ExpressionTest {
try {
mathEngine.setAngleUnits(AngleUnit.rad);
Assert.assertEquals("Π", mathEngine.evaluate("π"));
Assert.assertEquals("π/2", mathEngine.simplify("π/2"));
Assert.assertEquals(mathEngine.evaluate("0.9092974268256816953960198659117448427022549714478902683789"), mathEngine.evaluate("sin(2)"));
Assert.assertEquals(mathEngine.evaluate("0.1411200080598672221007448028081102798469332642522655841518"), mathEngine.evaluate("sin(3)"));
Assert.assertEquals(mathEngine.evaluate("0"), mathEngine.evaluate("sin(0)"));
assertEquals("Π", mathEngine.evaluate("π"));
assertEquals("π/2", mathEngine.simplify("π/2"));
assertEquals(mathEngine.evaluate("0.9092974268256816953960198659117448427022549714478902683789"), mathEngine.evaluate("sin(2)"));
assertEquals(mathEngine.evaluate("0.1411200080598672221007448028081102798469332642522655841518"), mathEngine.evaluate("sin(3)"));
assertEquals(mathEngine.evaluate("0"), mathEngine.evaluate("sin(0)"));
Assert.assertEquals(mathEngine.evaluate("1"), mathEngine.evaluate("cos(0)"));
Assert.assertEquals(mathEngine.evaluate("0.8623188722876839341019385139508425355100840085355108292801"), mathEngine.evaluate("cos(100)"));
Assert.assertEquals(mathEngine.evaluate("-0.416146836547142386997568229500762189766000771075544890755"), mathEngine.evaluate("cos(2)"));
assertEquals(mathEngine.evaluate("1"), mathEngine.evaluate("cos(0)"));
assertEquals(mathEngine.evaluate("0.8623188722876839341019385139508425355100840085355108292801"), mathEngine.evaluate("cos(100)"));
assertEquals(mathEngine.evaluate("-0.416146836547142386997568229500762189766000771075544890755"), mathEngine.evaluate("cos(2)"));
Assert.assertEquals(mathEngine.evaluate("-2.185039863261518991643306102313682543432017746227663164562"), mathEngine.evaluate("tan(2)"));
Assert.assertEquals(mathEngine.evaluate("-0.142546543074277805295635410533913493226092284901804647633"), mathEngine.evaluate("tan(3)"));
Assert.assertEquals(mathEngine.evaluate("0.6483608274590872"), mathEngine.evaluate("tan(10)"));
assertEquals(mathEngine.evaluate("-2.185039863261518991643306102313682543432017746227663164562"), mathEngine.evaluate("tan(2)"));
assertEquals(mathEngine.evaluate("-0.142546543074277805295635410533913493226092284901804647633"), mathEngine.evaluate("tan(3)"));
assertEquals(mathEngine.evaluate("0.6483608274590872"), mathEngine.evaluate("tan(10)"));
Assert.assertEquals(mathEngine.evaluate("0.6420926159343306"), mathEngine.evaluate("cot(1)"));
Assert.assertEquals(mathEngine.evaluate("-0.457657554360285763750277410432047276428486329231674329641"), mathEngine.evaluate("cot(2)"));
Assert.assertEquals(mathEngine.evaluate("-7.015252551434533469428551379526476578293103352096353838156"), mathEngine.evaluate("cot(3)"));
assertEquals(mathEngine.evaluate("0.6420926159343306"), mathEngine.evaluate("cot(1)"));
assertEquals(mathEngine.evaluate("-0.457657554360285763750277410432047276428486329231674329641"), mathEngine.evaluate("cot(2)"));
assertEquals(mathEngine.evaluate("-7.015252551434533469428551379526476578293103352096353838156"), mathEngine.evaluate("cot(3)"));
} finally {
mathEngine.setAngleUnits(defaultAngleUnits);
}
try {
mathEngine.setAngleUnits(AngleUnit.deg);
Assert.assertEquals(mathEngine.evaluate("0.9092974268256816953960198659117448427022549714478902683789"), mathEngine.evaluate("sin(deg(2))"));
Assert.assertEquals(mathEngine.evaluate("0.1411200080598672221007448028081102798469332642522655841518"), mathEngine.evaluate("sin(deg(3))"));
Assert.assertEquals(mathEngine.evaluate("0"), mathEngine.evaluate("sin(deg(0))"));
assertEquals(mathEngine.evaluate("0.9092974268256816953960198659117448427022549714478902683789"), mathEngine.evaluate("sin(deg(2))"));
assertEquals(mathEngine.evaluate("0.1411200080598672221007448028081102798469332642522655841518"), mathEngine.evaluate("sin(deg(3))"));
assertEquals(mathEngine.evaluate("0"), mathEngine.evaluate("sin(deg(0))"));
Assert.assertEquals(mathEngine.evaluate("1"), mathEngine.evaluate("cos(deg(0))"));
Assert.assertEquals(mathEngine.evaluate("0.8623188722876839341019385139508425355100840085355108292801"), mathEngine.evaluate("cos(deg(100))"));
Assert.assertEquals(mathEngine.evaluate("-0.416146836547142386997568229500762189766000771075544890755"), mathEngine.evaluate("cos(deg(2))"));
assertEquals(mathEngine.evaluate("1"), mathEngine.evaluate("cos(deg(0))"));
assertEquals(mathEngine.evaluate("0.8623188722876839341019385139508425355100840085355108292801"), mathEngine.evaluate("cos(deg(100))"));
assertEquals(mathEngine.evaluate("-0.416146836547142386997568229500762189766000771075544890755"), mathEngine.evaluate("cos(deg(2))"));
Assert.assertEquals(mathEngine.evaluate("-2.185039863261518991643306102313682543432017746227663164562"), mathEngine.evaluate("tan(deg(2))"));
Assert.assertEquals(mathEngine.evaluate("-0.142546543074277805295635410533913493226092284901804647633"), mathEngine.evaluate("tan(deg(3))"));
Assert.assertEquals(mathEngine.evaluate("0.6483608274590872"), mathEngine.evaluate("tan(deg(10))"));
assertEquals(mathEngine.evaluate("-2.185039863261518991643306102313682543432017746227663164562"), mathEngine.evaluate("tan(deg(2))"));
assertEquals(mathEngine.evaluate("-0.142546543074277805295635410533913493226092284901804647633"), mathEngine.evaluate("tan(deg(3))"));
assertEquals(mathEngine.evaluate("0.6483608274590872"), mathEngine.evaluate("tan(deg(10))"));
Assert.assertEquals(mathEngine.evaluate("0.6420926159343306"), mathEngine.evaluate("cot(deg(1))"));
Assert.assertEquals(mathEngine.evaluate("-0.457657554360285763750277410432047276428486329231674329641"), mathEngine.evaluate("cot(deg(2))"));
Assert.assertEquals(mathEngine.evaluate("-7.015252551434533469428551379526476578293103352096353838156"), mathEngine.evaluate("cot(deg(3))"));
assertEquals(mathEngine.evaluate("0.6420926159343306"), mathEngine.evaluate("cot(deg(1))"));
assertEquals(mathEngine.evaluate("-0.457657554360285763750277410432047276428486329231674329641"), mathEngine.evaluate("cot(deg(2))"));
assertEquals(mathEngine.evaluate("-7.015252551434533469428551379526476578293103352096353838156"), mathEngine.evaluate("cot(deg(3))"));
} finally {
mathEngine.setAngleUnits(defaultAngleUnits);
}
try {
mathEngine.setAngleUnits(AngleUnit.rad);
Assert.assertEquals(mathEngine.evaluate("-0.5235987755982989"), mathEngine.evaluate("asin(-0.5)"));
Assert.assertEquals(mathEngine.evaluate("-0.47349551215005636"), mathEngine.evaluate("asin(-0.456)"));
Assert.assertEquals(mathEngine.evaluate("0.32784124364198347"), mathEngine.evaluate("asin(0.322)"));
assertEquals(mathEngine.evaluate("-0.5235987755982989"), mathEngine.evaluate("asin(-0.5)"));
assertEquals(mathEngine.evaluate("-0.47349551215005636"), mathEngine.evaluate("asin(-0.456)"));
assertEquals(mathEngine.evaluate("0.32784124364198347"), mathEngine.evaluate("asin(0.322)"));
Assert.assertEquals(mathEngine.evaluate("1.2429550831529133"), mathEngine.evaluate("acos(0.322)"));
Assert.assertEquals(mathEngine.evaluate("1.5587960387762325"), mathEngine.evaluate("acos(0.012)"));
Assert.assertEquals(mathEngine.evaluate("1.6709637479564563"), mathEngine.evaluate("acos(-0.1)"));
assertEquals(mathEngine.evaluate("1.2429550831529133"), mathEngine.evaluate("acos(0.322)"));
assertEquals(mathEngine.evaluate("1.5587960387762325"), mathEngine.evaluate("acos(0.012)"));
assertEquals(mathEngine.evaluate("1.6709637479564563"), mathEngine.evaluate("acos(-0.1)"));
Assert.assertEquals(mathEngine.evaluate("0.3805063771123649"), mathEngine.evaluate("atan(0.4)"));
Assert.assertEquals(mathEngine.evaluate("0.09966865249116204"), mathEngine.evaluate("atan(0.1)"));
Assert.assertEquals(mathEngine.evaluate("-0.5404195002705842"), mathEngine.evaluate("atan(-0.6)"));
assertEquals(mathEngine.evaluate("0.3805063771123649"), mathEngine.evaluate("atan(0.4)"));
assertEquals(mathEngine.evaluate("0.09966865249116204"), mathEngine.evaluate("atan(0.1)"));
assertEquals(mathEngine.evaluate("-0.5404195002705842"), mathEngine.evaluate("atan(-0.6)"));
Assert.assertEquals(mathEngine.evaluate("1.0603080048781206"), mathEngine.evaluate("acot(0.56)"));
assertEquals(mathEngine.evaluate("1.0603080048781206"), mathEngine.evaluate("acot(0.56)"));
// todo serso: wolfram alpha returns -0.790423 instead of 2.3511694068615325 (-PI)
Assert.assertEquals(mathEngine.evaluate("2.3511694068615325"), mathEngine.evaluate("acot(-0.99)"));
assertEquals(mathEngine.evaluate("2.3511694068615325"), mathEngine.evaluate("acot(-0.99)"));
// todo serso: wolfram alpha returns -1.373401 instead of 1.7681918866447774 (-PI)
Assert.assertEquals(mathEngine.evaluate("1.7681918866447774"), mathEngine.evaluate("acot(-0.2)"));
assertEquals(mathEngine.evaluate("1.7681918866447774"), mathEngine.evaluate("acot(-0.2)"));
} finally {
mathEngine.setAngleUnits(defaultAngleUnits);
}
try {
mathEngine.setAngleUnits(AngleUnit.deg);
Assert.assertEquals(mathEngine.evaluate("deg(-0.5235987755982989)"), mathEngine.evaluate("asin(-0.5)"));
Assert.assertEquals(mathEngine.evaluate("-27.129294464583623"), mathEngine.evaluate("asin(-0.456)"));
Assert.assertEquals(mathEngine.evaluate("18.783919611005786"), mathEngine.evaluate("asin(0.322)"));
assertEquals(mathEngine.evaluate("deg(-0.5235987755982989)"), mathEngine.evaluate("asin(-0.5)"));
assertEquals(mathEngine.evaluate("-27.129294464583623"), mathEngine.evaluate("asin(-0.456)"));
assertEquals(mathEngine.evaluate("18.783919611005786"), mathEngine.evaluate("asin(0.322)"));
Assert.assertEquals(mathEngine.evaluate("71.21608038899423"), mathEngine.evaluate("acos(0.322)"));
Assert.assertEquals(mathEngine.evaluate("89.31243414358914"), mathEngine.evaluate("acos(0.012)"));
Assert.assertEquals(mathEngine.evaluate("95.73917047726678"), mathEngine.evaluate("acos(-0.1)"));
assertEquals(mathEngine.evaluate("71.21608038899423"), mathEngine.evaluate("acos(0.322)"));
assertEquals(mathEngine.evaluate("89.31243414358914"), mathEngine.evaluate("acos(0.012)"));
assertEquals(mathEngine.evaluate("95.73917047726678"), mathEngine.evaluate("acos(-0.1)"));
Assert.assertEquals(mathEngine.evaluate("deg(0.3805063771123649)"), mathEngine.evaluate("atan(0.4)"));
Assert.assertEquals(mathEngine.evaluate("deg(0.09966865249116204)"), mathEngine.evaluate("atan(0.1)"));
Assert.assertEquals(mathEngine.evaluate("deg(-0.5404195002705842)"), mathEngine.evaluate("atan(-0.6)"));
assertEquals(mathEngine.evaluate("deg(0.3805063771123649)"), mathEngine.evaluate("atan(0.4)"));
assertEquals(mathEngine.evaluate("deg(0.09966865249116204)"), mathEngine.evaluate("atan(0.1)"));
assertEquals(mathEngine.evaluate("deg(-0.5404195002705842)"), mathEngine.evaluate("atan(-0.6)"));
Assert.assertEquals(mathEngine.evaluate("deg(1.0603080048781206)"), mathEngine.evaluate("acot(0.56)"));
assertEquals(mathEngine.evaluate("deg(1.0603080048781206)"), mathEngine.evaluate("acot(0.56)"));
// todo serso: wolfram alpha returns -0.790423 instead of 2.3511694068615325 (-PI)
Assert.assertEquals(mathEngine.evaluate("134.7120839334429"), mathEngine.evaluate("acot(-0.99)"));
assertEquals(mathEngine.evaluate("134.7120839334429"), mathEngine.evaluate("acot(-0.99)"));
// todo serso: wolfram alpha returns -1.373401 instead of 1.7681918866447774 (-PI)
Assert.assertEquals(mathEngine.evaluate("deg(1.7681918866447774)"), mathEngine.evaluate("acot(-0.2)"));
assertEquals(mathEngine.evaluate("deg(1.7681918866447774)"), mathEngine.evaluate("acot(-0.2)"));
} finally {
mathEngine.setAngleUnits(defaultAngleUnits);
}
try {
mathEngine.setAngleUnits(AngleUnit.deg);
Assert.assertEquals(mathEngine.evaluate("0.0348994967025009716459951816253329373548245760432968714250"), mathEngine.evaluate("(sin(2))"));
Assert.assertEquals(mathEngine.evaluate("0.0523359562429438327221186296090784187310182539401649204835"), mathEngine.evaluate("(sin(3))"));
Assert.assertEquals(mathEngine.evaluate("0"), mathEngine.evaluate("sin(0)"));
assertEquals(mathEngine.evaluate("0.0348994967025009716459951816253329373548245760432968714250"), mathEngine.evaluate("(sin(2))"));
assertEquals(mathEngine.evaluate("0.0523359562429438327221186296090784187310182539401649204835"), mathEngine.evaluate("(sin(3))"));
assertEquals(mathEngine.evaluate("0"), mathEngine.evaluate("sin(0)"));
Assert.assertEquals(mathEngine.evaluate("1"), mathEngine.evaluate("cos(0)"));
Assert.assertEquals(mathEngine.evaluate("-0.1736481776669303"), mathEngine.evaluate("(cos(100))"));
Assert.assertEquals(mathEngine.evaluate("0.9993908270190958"), mathEngine.evaluate("(cos(2))"));
assertEquals(mathEngine.evaluate("1"), mathEngine.evaluate("cos(0)"));
assertEquals(mathEngine.evaluate("-0.1736481776669303"), mathEngine.evaluate("(cos(100))"));
assertEquals(mathEngine.evaluate("0.9993908270190958"), mathEngine.evaluate("(cos(2))"));
Assert.assertEquals(mathEngine.evaluate("0.03492076949174773"), mathEngine.evaluate("(tan(2))"));
Assert.assertEquals(mathEngine.evaluate("0.05240777928304121"), mathEngine.evaluate("(tan(3))"));
Assert.assertEquals(mathEngine.evaluate("0.17632698070846498"), mathEngine.evaluate("(tan(10))"));
assertEquals(mathEngine.evaluate("0.03492076949174773"), mathEngine.evaluate("(tan(2))"));
assertEquals(mathEngine.evaluate("0.05240777928304121"), mathEngine.evaluate("(tan(3))"));
assertEquals(mathEngine.evaluate("0.17632698070846498"), mathEngine.evaluate("(tan(10))"));
Assert.assertEquals(mathEngine.evaluate("57.28996163075943"), mathEngine.evaluate("(cot(1))"));
Assert.assertEquals(mathEngine.evaluate("28.636253282915604"), mathEngine.evaluate("(cot(2))"));
Assert.assertEquals(mathEngine.evaluate("19.081136687728208"), mathEngine.evaluate("(cot(3))"));
assertEquals(mathEngine.evaluate("57.28996163075943"), mathEngine.evaluate("(cot(1))"));
assertEquals(mathEngine.evaluate("28.636253282915604"), mathEngine.evaluate("(cot(2))"));
assertEquals(mathEngine.evaluate("19.081136687728208"), mathEngine.evaluate("(cot(3))"));
} finally {
mathEngine.setAngleUnits(defaultAngleUnits);
}
@ -640,7 +637,7 @@ public class ExpressionTest {
testSinEqualsToSinh(mathEngine, 6d);
testSinEqualsToSinh(mathEngine, -1d, "-0.8414709848078965");
testSinEqualsToSinh(mathEngine, -3.3d, "0.1577456941432482");
testSinEqualsToSinh(mathEngine, -232.2d, "0.27429486373689577");
testSinEqualsToSinh(mathEngine, -232.2d, "0.2742948637368958");
} finally {
mathEngine.setAngleUnits(defaultAngleUnits);
}
@ -648,23 +645,23 @@ public class ExpressionTest {
try {
mathEngine.setAngleUnits(AngleUnit.deg);
testSinEqualsToSinh(mathEngine, 0d);
testSinEqualsToSinh(mathEngine, 1d, "0.01745240643728351");
testSinEqualsToSinh(mathEngine, 3d, "0.052335956242943835");
testSinEqualsToSinh(mathEngine, 6d, "0.10452846326765347");
testSinEqualsToSinh(mathEngine, -1d, "-0.01745240643728351");
testSinEqualsToSinh(mathEngine, -3.3d, "-0.05756402695956728");
testSinEqualsToSinh(mathEngine, 1d, "0.0174524064372835");
testSinEqualsToSinh(mathEngine, 3d, "0.0523359562429438");
testSinEqualsToSinh(mathEngine, 6d, "0.1045284632676535");
testSinEqualsToSinh(mathEngine, -1d, "-0.0174524064372835");
testSinEqualsToSinh(mathEngine, -3.3d, "-0.0575640269595673");
testSinEqualsToSinh(mathEngine, -232.2d, "0.7901550123756904");
Assert.assertEquals("Π/2", mathEngine.simplify("Π/2"));
assertEquals("Π/2", mathEngine.simplify("Π/2"));
} finally {
mathEngine.setAngleUnits(defaultAngleUnits);
}
try {
mathEngine.setAngleUnits(AngleUnit.rad);
Assert.assertEquals(mathEngine.evaluate("1.5707963267948966-0.8813735870195429*i"), mathEngine.evaluate("acos(i)"));
Assert.assertEquals(mathEngine.evaluate("0.9045568943023814-1.0612750619050357*i"), mathEngine.evaluate("acos(1+i)"));
Assert.assertEquals(mathEngine.evaluate("0.9999999999999999-0.9999999999999998*i"), mathEngine.evaluate("cos(acos(1-i))"));
Assert.assertEquals(mathEngine.evaluate("-0.9045568943023814-1.0612750619050355*i"), mathEngine.evaluate("-acos(1-i)"));
assertEquals(mathEngine.evaluate("1.5707963267948966-0.8813735870195429*i"), mathEngine.evaluate("acos(i)"));
assertEquals(mathEngine.evaluate("0.9045568943023814-1.0612750619050357*i"), mathEngine.evaluate("acos(1+i)"));
assertEquals(mathEngine.evaluate("0.9999999999999999-0.9999999999999998*i"), mathEngine.evaluate("cos(acos(1-i))"));
assertEquals(mathEngine.evaluate("-0.9045568943023814-1.0612750619050355*i"), mathEngine.evaluate("-acos(1-i)"));
} finally {
mathEngine.setAngleUnits(defaultAngleUnits);
}
@ -677,12 +674,12 @@ public class ExpressionTest {
private void testSinEqualsToSinh(@Nonnull MathEngine mathEngine, @Nonnull Double x, @Nullable String expected) throws ParseException {
if (expected == null) {
Assert.assertEquals(mathEngine.evaluate("sinh(i*" + x + ")/i"), mathEngine.evaluate("sin(" + x + ")"));
assertEquals(mathEngine.evaluate("sinh(i*" + x + ")/i"), mathEngine.evaluate("sin(" + x + ")"));
// Assert.assertEquals(mathEngine.evaluate("exp("+x+")-sinh(" + x + ")"), mathEngine.evaluate("cosh(" + x + ")"));
} else {
Assert.assertEquals(expected, mathEngine.evaluate("sin(" + x + ")"));
Assert.assertEquals(expected, mathEngine.evaluate("(exp(i * " + x + ") - cos(" + x + "))/i"));
Assert.assertEquals(expected, mathEngine.evaluate("(exp(i * " + x + ") - cos(" + x + "))/i"));
assertEquals(expected, mathEngine.evaluate("sin(" + x + ")"));
assertEquals(expected, mathEngine.evaluate("(exp(i * " + x + ") - cos(" + x + "))/i"));
assertEquals(expected, mathEngine.evaluate("(exp(i * " + x + ") - cos(" + x + "))/i"));
}
}
@ -693,32 +690,32 @@ public class ExpressionTest {
@Test
public void testIntegrals() throws Exception {
Assert.assertEquals("50", Expression.valueOf("∫ab(x, x, 0, 10)").expand().numeric().toString());
Assert.assertEquals("1/2*a^2", Expression.valueOf("∫ab(x, x, 0, a)").expand().toString());
assertEquals("50", Expression.valueOf("∫ab(x, x, 0, 10)").expand().numeric().toString());
assertEquals("1/2*a^2", Expression.valueOf("∫ab(x, x, 0, a)").expand().toString());
try {
Assert.assertEquals("∫ab(x, x, 0)", Expression.valueOf("∫ab(x, x, 0)").expand().toString());
assertEquals("∫ab(x, x, 0)", Expression.valueOf("∫ab(x, x, 0)").expand().toString());
fail();
} catch (ParseException e) {
}
try {
Assert.assertEquals("∫ab(x, x)", Expression.valueOf("∫ab(x, x)").expand().simplify().toString());
assertEquals("∫ab(x, x)", Expression.valueOf("∫ab(x, x)").expand().simplify().toString());
fail();
} catch (ParseException e) {
}
Assert.assertEquals("x^2/2", Expression.valueOf("∫(x, x)").expand().simplify().toString());
assertEquals("x^2/2", Expression.valueOf("∫(x, x)").expand().simplify().toString());
try {
Assert.assertEquals("x^2/2", Expression.valueOf("∫(x, x)").expand().numeric().toString());
assertEquals("x^2/2", Expression.valueOf("∫(x, x)").expand().numeric().toString());
fail();
} catch (ArithmeticException e) {
}
Assert.assertEquals("x^2/2", Expression.valueOf("∫(x, x)").expand().simplify().toString());
Assert.assertEquals("ln(x)", Expression.valueOf("∫(1/x, x)").expand().simplify().toString());
assertEquals("x^2/2", Expression.valueOf("∫(x, x)").expand().simplify().toString());
assertEquals("ln(x)", Expression.valueOf("∫(1/x, x)").expand().simplify().toString());
try {
JsclMathEngine.getInstance().setAngleUnits(AngleUnit.rad);
Assert.assertEquals("2*ln(2)+ln(cosh(x))", Expression.valueOf("∫(tanh(x), x)").expand().simplify().toString());
Assert.assertEquals("2*ln(2)+ln(sin(x))", Expression.valueOf("∫(cot(x), x)").expand().simplify().toString());
Assert.assertEquals("-2*ln(2)-ln(cos(x))", Expression.valueOf("∫(tan(x), x)").expand().simplify().toString());
assertEquals("2*ln(2)+ln(cosh(x))", Expression.valueOf("∫(tanh(x), x)").expand().simplify().toString());
assertEquals("2*ln(2)+ln(sin(x))", Expression.valueOf("∫(cot(x), x)").expand().simplify().toString());
assertEquals("-2*ln(2)-ln(cos(x))", Expression.valueOf("∫(tan(x), x)").expand().simplify().toString());
} finally {
JsclMathEngine.getInstance().setAngleUnits(AngleUnit.deg);
}
@ -729,12 +726,12 @@ public class ExpressionTest {
final AngleUnit defaultAngleUnits = JsclMathEngine.getInstance().getAngleUnits();
try {
JsclMathEngine.getInstance().setAngleUnits(AngleUnit.rad);
Assert.assertEquals("-0.9092974268256817", Expression.valueOf("∂(cos(t),t,2)").numeric().toString());
Assert.assertEquals("∂(cos(t), t, 2, 1)", Expression.valueOf("∂(cos(t),t,2)").simplify().toString());
Assert.assertEquals("-2.234741690198506", Expression.valueOf("∂(t*cos(t),t,2)").numeric().toString());
Assert.assertEquals("-4.469483380397012", Expression.valueOf("2*∂(t*cos(t),t,2)").numeric().toString());
Assert.assertEquals("-sin(2)", Expression.valueOf("∂(cos(t),t,2)").expand().toString());
Assert.assertEquals("-sin(t)", Expression.valueOf("∂(cos(t),t)").expand().toString());
assertEquals("-0.9092974268256817", Expression.valueOf("∂(cos(t),t,2)").numeric().toString());
assertEquals("∂(cos(t), t, 2, 1)", Expression.valueOf("∂(cos(t),t,2)").simplify().toString());
assertEquals("-2.234741690198506", Expression.valueOf("∂(t*cos(t),t,2)").numeric().toString());
assertEquals("-4.469483380397012", Expression.valueOf("2*∂(t*cos(t),t,2)").numeric().toString());
assertEquals("-sin(2)", Expression.valueOf("∂(cos(t),t,2)").expand().toString());
assertEquals("-sin(t)", Expression.valueOf("∂(cos(t),t)").expand().toString());
assertEquals("-sin(t)", Expression.valueOf("∂(cos(t),t,t,1)").expand().simplify().toString());
assertEquals("∂(cos(t), t, t, 1°)", Expression.valueOf("∂(cos(t),t,t,1°)").expand().simplify().toString());
} finally {
@ -746,18 +743,18 @@ public class ExpressionTest {
@Test
public void testSum() throws Exception {
Assert.assertEquals("3", Expression.valueOf("Σ(n,n,1,2)").expand().toString());
Assert.assertEquals("200", Expression.valueOf("Σ(n/n,n,1,200)").expand().toString());
Assert.assertEquals("1/3", Expression.valueOf("Σ((n-1)/(n+1),n,1,2)").expand().toString());
Assert.assertEquals("sin(1)", Expression.valueOf("Σ(sin(n),n,1,1)").expand().toString());
Assert.assertEquals("1/1!", Expression.valueOf("Σ(n/n!,n,1,1)").expand().toString());
Assert.assertEquals("2", Expression.valueOf("Σ(n/n!,n,1,2)").expand().numeric().toString());
Assert.assertEquals("2.7182818284590455", Expression.valueOf("Σ(n/n!,n,1,200)").expand().numeric().toString());
Assert.assertEquals("2.718281828459046", Expression.valueOf("Σ(n/(2*n/2)!,n,1,200)").expand().numeric().toString());
Assert.assertEquals(Expression.valueOf("3").numeric().toString(), Expression.valueOf("Σ(n°,n,1,2)").expand().numeric().toString());
Assert.assertEquals("200", Expression.valueOf("Σ(n°/n°,n,1,200)").expand().numeric().toString());
Assert.assertEquals("-sin(1)-sin(2)", Expression.valueOf("Σ(∂(cos(t),t,n),n,1,2)").expand().toString());
Assert.assertEquals("-0.05235190313978448", Expression.valueOf("Σ(∂(cos(t),t,n),n,1,2)").expand().numeric().toString());
assertEquals("3", Expression.valueOf("Σ(n,n,1,2)").expand().toString());
assertEquals("200", Expression.valueOf("Σ(n/n,n,1,200)").expand().toString());
assertEquals("1/3", Expression.valueOf("Σ((n-1)/(n+1),n,1,2)").expand().toString());
assertEquals("sin(1)", Expression.valueOf("Σ(sin(n),n,1,1)").expand().toString());
assertEquals("1/1!", Expression.valueOf("Σ(n/n!,n,1,1)").expand().toString());
assertEquals("2", Expression.valueOf("Σ(n/n!,n,1,2)").expand().numeric().toString());
assertEquals("2.718281828459046", Expression.valueOf("Σ(n/n!,n,1,200)").expand().numeric().toString());
assertEquals("2.718281828459046", Expression.valueOf("Σ(n/(2*n/2)!,n,1,200)").expand().numeric().toString());
assertEquals(Expression.valueOf("3").numeric().toString(), Expression.valueOf("Σ(n°,n,1,2)").expand().numeric().toString());
assertEquals("200", Expression.valueOf("Σ(n°/n°,n,1,200)").expand().numeric().toString());
assertEquals("-sin(1)-sin(2)", Expression.valueOf("Σ(∂(cos(t),t,n),n,1,2)").expand().toString());
assertEquals("-0.0523519031397845", Expression.valueOf("Σ(∂(cos(t),t,n),n,1,2)").expand().numeric().toString());
}
@Test
@ -766,19 +763,19 @@ public class ExpressionTest {
//final NumeralBase defaultNumeralBase = me.getDefaultNumeralBase();
try {
//me.setDefaultNumeralBase(NumeralBase.bin);
Assert.assertEquals("10", me.evaluate("0b:01010"));
Assert.assertEquals("10", me.evaluate("0b:1010"));
Assert.assertEquals("520", me.evaluate("0o:1010"));
Assert.assertEquals("1010", me.evaluate("1010"));
Assert.assertEquals("1010.1", me.evaluate("1010.1"));
assertEquals("10", me.evaluate("0b:01010"));
assertEquals("10", me.evaluate("0b:1010"));
assertEquals("520", me.evaluate("0o:1010"));
assertEquals("1010", me.evaluate("1010"));
assertEquals("1010.1", me.evaluate("1010.1"));
} finally {
//me.setDefaultNumeralBase(defaultNumeralBase);
}
try {
me.setNumeralBase(NumeralBase.hex);
Assert.assertEquals("22F", me.evaluate("22F*exp(F)/exp(F)"));
Assert.assertEquals("E", me.evaluate("E"));
assertEquals("22F", me.evaluate("22F*exp(F)/exp(F)"));
assertEquals("E", me.evaluate("E"));
} finally {
me.setNumeralBase(NumeralBase.dec);
@ -790,48 +787,48 @@ public class ExpressionTest {
final JsclMathEngine me = JsclMathEngine.getInstance();
try {
me.setUseGroupingSeparator(true);
Assert.assertEquals("123 456.7891011", Expression.valueOf("123456.7891011").numeric().toString());
Assert.assertEquals("123 456.7891011", Expression.valueOf("123456.7891011").simplify().toString());
Assert.assertEquals("123 456.78910111231", Expression.valueOf("123456.7891011123123123123123").simplify().toString());
Assert.assertEquals("1.222E-6", Expression.valueOf("1222/(10^9)").numeric().toString());
Assert.assertEquals("12 345", JsclInteger.valueOf(12345L).toString());
assertEquals("123 456.7891011", Expression.valueOf("123456.7891011").numeric().toString());
assertEquals("123 456.7891011", Expression.valueOf("123456.7891011").simplify().toString());
assertEquals("123 456.7891011123", Expression.valueOf("123456.7891011123123123123123").simplify().toString());
assertEquals("0.000001222", Expression.valueOf("1222/(10^9)").numeric().toString());
assertEquals("12 345", JsclInteger.valueOf(12345L).toString());
me.setScienceNotation(true);
Assert.assertEquals("0", Expression.valueOf("0.0").simplify().toString());
Assert.assertEquals("1", Expression.valueOf("1.0").simplify().toString());
Assert.assertEquals("100", Expression.valueOf("100.0").simplify().toString());
assertEquals("0", Expression.valueOf("0.0").simplify().toString());
assertEquals("1", Expression.valueOf("1.0").simplify().toString());
assertEquals("100", Expression.valueOf("100.0").simplify().toString());
me.setScienceNotation(false);
me.setRoundResult(true);
me.setPrecision(5);
Assert.assertEquals("0", Expression.valueOf("1222/(10^9)").numeric().toString());
assertEquals("0", Expression.valueOf("1222/(10^9)").numeric().toString());
me.setScienceNotation(true);
me.setRoundResult(true);
me.setPrecision(5);
Assert.assertEquals("1.222E-6", Expression.valueOf("1222/(10^9)").numeric().toString());
assertEquals("1.222E-6", Expression.valueOf("1222/(10^9)").numeric().toString());
me.setRoundResult(true);
me.setPrecision(10);
Assert.assertEquals("1.222E-6", Expression.valueOf("1222/(10^9)").numeric().toString());
assertEquals("1.222E-6", Expression.valueOf("1222/(10^9)").numeric().toString());
me.setRoundResult(false);
Assert.assertEquals("1.222E-6", Expression.valueOf("1222/(10^9)").numeric().toString());
assertEquals("1.222E-6", Expression.valueOf("1222/(10^9)").numeric().toString());
me.setScienceNotation(false);
Assert.assertEquals("0.3333333333333333", Expression.valueOf("1/3").numeric().toString());
assertEquals("0.3333333333333333", Expression.valueOf("1/3").numeric().toString());
me.setScienceNotation(true);
Assert.assertEquals("0.3333333333333333", Expression.valueOf("1/3").numeric().toString());
assertEquals("0.3333333333333333", Expression.valueOf("1/3").numeric().toString());
me.setRoundResult(true);
me.setPrecision(10);
Assert.assertEquals("0.3333333333", Expression.valueOf("1/3").numeric().toString());
assertEquals("0.3333333333", Expression.valueOf("1/3").numeric().toString());
me.setScienceNotation(false);
me.setRoundResult(true);
me.setPrecision(10);
Assert.assertEquals("0.3333333333", Expression.valueOf("1/3").numeric().toString());
assertEquals("0.3333333333", Expression.valueOf("1/3").numeric().toString());
} finally {
me.setUseGroupingSeparator(false);

View File

@ -9,6 +9,8 @@ import org.junit.Test;
import java.util.Arrays;
import static org.junit.Assert.assertEquals;
/**
* User: serso
* Date: 11/15/11
@ -20,20 +22,20 @@ public class CustomFunctionTest {
public void testLog() throws Exception {
JsclMathEngine mathEngine = JsclMathEngine.getInstance();
Assert.assertEquals("", Expression.valueOf("1/0").numeric().toString());
Assert.assertEquals("", Expression.valueOf("ln(10)/ln(1)").numeric().toString());
assertEquals("", Expression.valueOf("1/0").numeric().toString());
assertEquals("", Expression.valueOf("ln(10)/ln(1)").numeric().toString());
// logarithm
final CustomFunction.Builder jBuilder = new CustomFunction.Builder(true, "log", Arrays.asList("a", "b"), "ln(b)/ln(a)");
Function function = mathEngine.getFunctionsRegistry().addOrUpdate(jBuilder.create());
Assert.assertEquals("log(a, b)", function.toString());
Assert.assertEquals("ln(b)/ln(a)", ((CustomFunction) mathEngine.getFunctionsRegistry().get("log")).getContent());
Assert.assertEquals("", Expression.valueOf("log(1, 10)").numeric().toString());
Assert.assertEquals("3.3219280948873626", Expression.valueOf("log(2, 10)").numeric().toString());
Assert.assertEquals("1.4306765580733933", Expression.valueOf("log(5, 10)").numeric().toString());
Assert.assertEquals("0.9602525677891275", Expression.valueOf("log(11, 10)").numeric().toString());
Assert.assertEquals("1/b*1/ln(a)", Expression.valueOf("∂(log(a, b), b)").expand().toString());
Assert.assertEquals("-1/a*(1/ln(a))^2*ln(b)", Expression.valueOf("∂(log(a, b), a)").expand().toString());
assertEquals("log(a, b)", function.toString());
assertEquals("ln(b)/ln(a)", ((CustomFunction) mathEngine.getFunctionsRegistry().get("log")).getContent());
assertEquals("", Expression.valueOf("log(1, 10)").numeric().toString());
assertEquals("3.321928094887363", Expression.valueOf("log(2, 10)").numeric().toString());
assertEquals("1.430676558073393", Expression.valueOf("log(5, 10)").numeric().toString());
assertEquals("0.9602525677891275", Expression.valueOf("log(11, 10)").numeric().toString());
assertEquals("1/b*1/ln(a)", Expression.valueOf("∂(log(a, b), b)").expand().toString());
assertEquals("-1/a*(1/ln(a))^2*ln(b)", Expression.valueOf("∂(log(a, b), a)").expand().toString());
}
@ -43,15 +45,15 @@ public class CustomFunctionTest {
final CustomFunction.Builder jBuilder = new CustomFunction.Builder("t1", Arrays.asList("a"), "sin(a)");
mathEngine.getFunctionsRegistry().addOrUpdate(jBuilder.create());
Assert.assertEquals("1", Expression.valueOf("t1(90)").numeric().toString());
Assert.assertEquals("cos(t)", Expression.valueOf("∂(t1(t), t)").expand().toString());
Assert.assertEquals("0", Expression.valueOf("∂(t1(t), t2)").expand().toString());
Assert.assertEquals("cos(a)", Expression.valueOf("∂(t1(a), a)").expand().toString());
Assert.assertEquals("1", Expression.valueOf("∂(t1(a), t1(a))").expand().toString());
assertEquals("1", Expression.valueOf("t1(90)").numeric().toString());
assertEquals("cos(t)", Expression.valueOf("∂(t1(t), t)").expand().toString());
assertEquals("0", Expression.valueOf("∂(t1(t), t2)").expand().toString());
assertEquals("cos(a)", Expression.valueOf("∂(t1(a), a)").expand().toString());
assertEquals("1", Expression.valueOf("∂(t1(a), t1(a))").expand().toString());
final CustomFunction.Builder jBuilder1 = new CustomFunction.Builder("t2", Arrays.asList("a", "b"), "b*sin(a)");
mathEngine.getFunctionsRegistry().addOrUpdate(jBuilder1.create());
Assert.assertEquals("y*cos(x)", Expression.valueOf("∂(t2(x, y), x)").expand().toString());
Assert.assertEquals("sin(x)", Expression.valueOf("∂(t2(x, y), y)").expand().toString());
assertEquals("y*cos(x)", Expression.valueOf("∂(t2(x, y), x)").expand().toString());
assertEquals("sin(x)", Expression.valueOf("∂(t2(x, y), y)").expand().toString());
}
@Test
@ -60,19 +62,19 @@ public class CustomFunctionTest {
final CustomFunction.Builder jBuilder = new CustomFunction.Builder("t1", Arrays.asList("a"), "sin(a)");
mathEngine.getFunctionsRegistry().addOrUpdate(jBuilder.create());
Assert.assertEquals("1", Expression.valueOf("t1(90)").numeric().toString());
assertEquals("1", Expression.valueOf("t1(90)").numeric().toString());
try {
mathEngine.setAngleUnits(AngleUnit.rad);
Assert.assertEquals("-cos(t)", Expression.valueOf("∫(t1(t), t)").expand().toString());
Assert.assertEquals("t2*sin(t)", Expression.valueOf("∫(t1(t), t2)").expand().toString());
Assert.assertEquals("-cos(a)", Expression.valueOf("∫(t1(a), a)").expand().toString());
Assert.assertEquals("1/2*sin(a)^2", Expression.valueOf("∫(t1(a), t1(a))").expand().toString());
assertEquals("-cos(t)", Expression.valueOf("∫(t1(t), t)").expand().toString());
assertEquals("t2*sin(t)", Expression.valueOf("∫(t1(t), t2)").expand().toString());
assertEquals("-cos(a)", Expression.valueOf("∫(t1(a), a)").expand().toString());
assertEquals("1/2*sin(a)^2", Expression.valueOf("∫(t1(a), t1(a))").expand().toString());
final CustomFunction.Builder jBuilder1 = new CustomFunction.Builder("t2", Arrays.asList("a", "b"), "b*sin(a)");
mathEngine.getFunctionsRegistry().addOrUpdate(jBuilder1.create());
Assert.assertEquals("-y*cos(x)", Expression.valueOf("∫(t2(x, y), x)").expand().toString());
Assert.assertEquals("1/2*y^2*sin(x)", Expression.valueOf("∫(t2(x, y), y)").expand().toString());
assertEquals("-y*cos(x)", Expression.valueOf("∫(t2(x, y), x)").expand().toString());
assertEquals("1/2*y^2*sin(x)", Expression.valueOf("∫(t2(x, y), y)").expand().toString());
} finally {
mathEngine.setAngleUnits(AngleUnit.deg);
}
@ -85,16 +87,16 @@ public class CustomFunctionTest {
final CustomFunction.Builder jBuilder = new CustomFunction.Builder("testFunction", Arrays.asList("a", "b", "c", "d"), "b*cos(a)/c+d");
mathEngine.getFunctionsRegistry().addOrUpdate(jBuilder.create());
Assert.assertEquals("6.749543120264322", Expression.valueOf("testFunction(2, 3, 4, 6)").numeric().toString());
Assert.assertEquals("7.749543120264322", Expression.valueOf("testFunction(2, 3, 4, 7)").numeric().toString());
Assert.assertEquals("6.749543120264322", Expression.valueOf("testFunction(2*1, 3, 4, 6)").numeric().toString());
Assert.assertEquals("6.749543120264322", Expression.valueOf("testFunction(2*1, 3, 4, 3!)").numeric().toString());
Assert.assertEquals("6.749543120264322", Expression.valueOf("testFunction(2*1, 3, 2^2-1+e^0, 3!)").numeric().toString());
Assert.assertEquals("testFunction(2, 3, 4, 3!)", Expression.valueOf("testFunction(2*1, 3, 2^2-1+e^0, 3!)").simplify().toString());
Assert.assertEquals("3*cos(2)/4+3!", Expression.valueOf("testFunction(2*1, 3, 2^2-1+e^0, 3!)").expand().toString());
Assert.assertEquals("3*(1/2*1/exp(2*i)+1/2*exp(2*i))/4+3!", Expression.valueOf("testFunction(2*1, 3, 2^2-1+e^0, 3!)").elementary().toString());
Assert.assertEquals("sin(t)^2*testFunction(2, 3, 4, 3!)", Expression.valueOf("sin(t)*testFunction(2*1, 3, 2^2-1+e^0, 3!)*sin(t)").simplify().toString());
Assert.assertEquals("testFunction(2, 3, 4, 3!)^2", Expression.valueOf("testFunction(2*1, 3, 2^2-1+e^0, 3!)*testFunction(2, 3, 4, 3!)").simplify().toString());
assertEquals("6.749543120264322", Expression.valueOf("testFunction(2, 3, 4, 6)").numeric().toString());
assertEquals("7.749543120264322", Expression.valueOf("testFunction(2, 3, 4, 7)").numeric().toString());
assertEquals("6.749543120264322", Expression.valueOf("testFunction(2*1, 3, 4, 6)").numeric().toString());
assertEquals("6.749543120264322", Expression.valueOf("testFunction(2*1, 3, 4, 3!)").numeric().toString());
assertEquals("6.749543120264322", Expression.valueOf("testFunction(2*1, 3, 2^2-1+e^0, 3!)").numeric().toString());
assertEquals("testFunction(2, 3, 4, 3!)", Expression.valueOf("testFunction(2*1, 3, 2^2-1+e^0, 3!)").simplify().toString());
assertEquals("3*cos(2)/4+3!", Expression.valueOf("testFunction(2*1, 3, 2^2-1+e^0, 3!)").expand().toString());
assertEquals("3*(1/2*1/exp(2*i)+1/2*exp(2*i))/4+3!", Expression.valueOf("testFunction(2*1, 3, 2^2-1+e^0, 3!)").elementary().toString());
assertEquals("sin(t)^2*testFunction(2, 3, 4, 3!)", Expression.valueOf("sin(t)*testFunction(2*1, 3, 2^2-1+e^0, 3!)*sin(t)").simplify().toString());
assertEquals("testFunction(2, 3, 4, 3!)^2", Expression.valueOf("testFunction(2*1, 3, 2^2-1+e^0, 3!)*testFunction(2, 3, 4, 3!)").simplify().toString());
try {
Expression.valueOf("testFunction(2*1, 3, 2^2-1+e^0, 3!)*testFunction(2, 3, 4)");
Assert.fail();
@ -106,31 +108,31 @@ public class CustomFunctionTest {
mathEngine.getConstantsRegistry().addOrUpdate(a.create());
final CustomFunction.Builder jBuilder1 = new CustomFunction.Builder("testFunction2", Arrays.asList("a", "b", "c", "d"), "b*cos(a)/c+d");
mathEngine.getFunctionsRegistry().addOrUpdate(jBuilder1.create());
Assert.assertEquals("6.749543120264322", Expression.valueOf("testFunction2(2, 3, 4, 6)").numeric().toString());
Assert.assertEquals("7.749543120264322", Expression.valueOf("testFunction2(2, 3, 4, 7)").numeric().toString());
Assert.assertEquals("6.749543120264322", Expression.valueOf("testFunction2(2*1, 3, 4, 6)").numeric().toString());
Assert.assertEquals("6.749543120264322", Expression.valueOf("testFunction2(2*1, 3, 2^2-1+e^0, 3!)").numeric().toString());
assertEquals("6.749543120264322", Expression.valueOf("testFunction2(2, 3, 4, 6)").numeric().toString());
assertEquals("7.749543120264322", Expression.valueOf("testFunction2(2, 3, 4, 7)").numeric().toString());
assertEquals("6.749543120264322", Expression.valueOf("testFunction2(2*1, 3, 4, 6)").numeric().toString());
assertEquals("6.749543120264322", Expression.valueOf("testFunction2(2*1, 3, 2^2-1+e^0, 3!)").numeric().toString());
final CustomFunction.Builder jBuilder2 = new CustomFunction.Builder("testFunction3", Arrays.asList("a", "b", "c", "d"), "testFunction2(a, b, c, d) - testFunction(a, b, c, d)");
mathEngine.getFunctionsRegistry().addOrUpdate(jBuilder2.create());
Assert.assertEquals("0", Expression.valueOf("testFunction3(2, 3, 4, 6)").numeric().toString());
Assert.assertEquals("0", Expression.valueOf("testFunction3(2, 3, 4, 7)").numeric().toString());
Assert.assertEquals("0", Expression.valueOf("testFunction3(2*1, 3, 4, 6)").numeric().toString());
Assert.assertEquals("0", Expression.valueOf("testFunction3(2*1, 3, 2^2-1+e^0, 3!)").numeric().toString());
assertEquals("0", Expression.valueOf("testFunction3(2, 3, 4, 6)").numeric().toString());
assertEquals("0", Expression.valueOf("testFunction3(2, 3, 4, 7)").numeric().toString());
assertEquals("0", Expression.valueOf("testFunction3(2*1, 3, 4, 6)").numeric().toString());
assertEquals("0", Expression.valueOf("testFunction3(2*1, 3, 2^2-1+e^0, 3!)").numeric().toString());
final CustomFunction.Builder jBuilder3 = new CustomFunction.Builder("testFunction4", Arrays.asList("a", "b", "c", "d"), "testFunction2(a, b/2, c/3, d/4) - testFunction(a, b!, c, d)");
mathEngine.getFunctionsRegistry().addOrUpdate(jBuilder3.create());
Assert.assertEquals("-4.874771560132161", Expression.valueOf("testFunction4(2, 3, 4, 6)").numeric().toString());
Assert.assertEquals("-5.624771560132161", Expression.valueOf("testFunction4(2, 3, 4, 7)").numeric().toString());
Assert.assertEquals("-4.874771560132161", Expression.valueOf("testFunction4(2*1, 3, 4, 6)").numeric().toString());
Assert.assertEquals("-4.874771560132161", Expression.valueOf("testFunction4(2*1, 3, 2^2-1+e^0, 3!)").numeric().toString());
assertEquals("-4.874771560132161", Expression.valueOf("testFunction4(2, 3, 4, 6)").numeric().toString());
assertEquals("-5.624771560132161", Expression.valueOf("testFunction4(2, 3, 4, 7)").numeric().toString());
assertEquals("-4.874771560132161", Expression.valueOf("testFunction4(2*1, 3, 4, 6)").numeric().toString());
assertEquals("-4.874771560132161", Expression.valueOf("testFunction4(2*1, 3, 2^2-1+e^0, 3!)").numeric().toString());
final CustomFunction.Builder jBuilder4 = new CustomFunction.Builder("testFunction5", Arrays.asList("a", "b"), "testFunction2(a, b/2, 2, 1) - testFunction(a, b!, 4!, 1)");
mathEngine.getFunctionsRegistry().addOrUpdate(jBuilder4.create());
Assert.assertEquals("0.4996954135095478", Expression.valueOf("testFunction5(2, 3)").numeric().toString());
Assert.assertEquals("0.4996954135095478", Expression.valueOf("testFunction5(2, 3)").numeric().toString());
Assert.assertEquals("0.4996954135095478", Expression.valueOf("testFunction5(2*1, 3)").numeric().toString());
Assert.assertEquals("-111.02230246251565E-18", Expression.valueOf("testFunction5(2*1, 2^2-1+e^0)").numeric().toString());
assertEquals("0.4996954135095478", Expression.valueOf("testFunction5(2, 3)").numeric().toString());
assertEquals("0.4996954135095478", Expression.valueOf("testFunction5(2, 3)").numeric().toString());
assertEquals("0.4996954135095478", Expression.valueOf("testFunction5(2*1, 3)").numeric().toString());
assertEquals("-0.0000000000000001", Expression.valueOf("testFunction5(2*1, 2^2-1+e^0)").numeric().toString());
try {
Expression.valueOf("testFunction5(2, 3.5)").numeric();
@ -141,19 +143,19 @@ public class CustomFunctionTest {
final CustomFunction.Builder jBuilder5 = new CustomFunction.Builder("testFunction6", Arrays.asList("a", "b"), "testFunction(a, b!, 4!, Π)");
mathEngine.getFunctionsRegistry().addOrUpdate(jBuilder5.create());
Assert.assertEquals("180.24984770675476", Expression.valueOf("testFunction6(2, 3)").numeric().toString());
assertEquals("180.2498477067548", Expression.valueOf("testFunction6(2, 3)").numeric().toString());
final ExtendedConstant.Builder e = new ExtendedConstant.Builder(new Constant("e"), 181d);
mathEngine.getConstantsRegistry().addOrUpdate(e.create());
final CustomFunction.Builder jBuilder6 = new CustomFunction.Builder("testFunction7", Arrays.asList("a", "b"), "testFunction(a, b!, 4!, e)");
mathEngine.getFunctionsRegistry().addOrUpdate(jBuilder6.create());
Assert.assertEquals("181.24984770675476", Expression.valueOf("testFunction7(2, 3)").numeric().toString());
assertEquals("181.2498477067548", Expression.valueOf("testFunction7(2, 3)").numeric().toString());
final ExtendedConstant.Builder e1 = new ExtendedConstant.Builder(new Constant("e"), 181d);
mathEngine.getConstantsRegistry().addOrUpdate(e1.create());
final CustomFunction.Builder jBuilder7 = new CustomFunction.Builder("testFunction8", Arrays.asList("a", "b"), "testFunction(sin(a), b!, 4!, e)");
mathEngine.getFunctionsRegistry().addOrUpdate(jBuilder7.create());
Assert.assertEquals("181.24999995362296", Expression.valueOf("testFunction8(2, 3)").numeric().toString());
assertEquals("181.249999953623", Expression.valueOf("testFunction8(2, 3)").numeric().toString());
}
@ -169,17 +171,17 @@ public class CustomFunctionTest {
mathEngine.getFunctionsRegistry().addOrUpdate(jBuilder2.create());
try {
Assert.assertEquals("1", Expression.valueOf("f(1, 1)").numeric().toString());
assertEquals("1", Expression.valueOf("f(1, 1)").numeric().toString());
Assert.fail();
} catch (ArithmeticException e) {
//ok
}
Assert.assertEquals("1.4142135623730951", Expression.valueOf("f2(1, 1)").numeric().toString());
Assert.assertEquals("5", Expression.valueOf("f2(4, 3)").numeric().toString());
assertEquals("1.414213562373095", Expression.valueOf("f2(1, 1)").numeric().toString());
assertEquals("5", Expression.valueOf("f2(4, 3)").numeric().toString());
Assert.assertEquals("2*z1", Expression.valueOf("∂(f3(z1, z2), z1)").expand().toString());
Assert.assertEquals("2*z2", Expression.valueOf("∂(f3(z1, z2), z2)").expand().toString());
assertEquals("2*z1", Expression.valueOf("∂(f3(z1, z2), z1)").expand().toString());
assertEquals("2*z2", Expression.valueOf("∂(f3(z1, z2), z2)").expand().toString());
// test symbols
final CustomFunction.Builder jBuilder3 = new CustomFunction.Builder("f4", Arrays.asList("x", "y"), "2 000*x^2+y^2");

View File

@ -1,34 +1,30 @@
package jscl.math.function;
import jscl.JsclMathEngine;
import junit.framework.Assert;
import org.junit.Test;
/**
* User: serso
* Date: 11/12/11
* Time: 4:00 PM
*/
import static org.junit.Assert.assertEquals;
public class RadTest {
@Test
public void testRad() throws Exception {
final JsclMathEngine mathEngine = JsclMathEngine.getInstance();
final JsclMathEngine mathEngine = new JsclMathEngine();
Assert.assertEquals("0.03490658503988659", mathEngine.evaluate("rad(2)"));
Assert.assertEquals("0.03490658503988659", mathEngine.evaluate("rad(1+1)"));
Assert.assertEquals("-0.03490658503988659", mathEngine.evaluate("rad(-2)"));
Assert.assertEquals("-0.03490658503988659", mathEngine.evaluate("rad(-1-1)"));
Assert.assertEquals("π", mathEngine.evaluate("rad(180)"));
Assert.assertEquals(String.valueOf(-Math.PI), mathEngine.evaluate("rad(-180)"));
assertEquals("0.0349065850398866", mathEngine.evaluate("rad(2)"));
assertEquals("0.0349065850398866", mathEngine.evaluate("rad(1+1)"));
assertEquals("-0.0349065850398866", mathEngine.evaluate("rad(-2)"));
assertEquals("-0.0349065850398866", mathEngine.evaluate("rad(-1-1)"));
assertEquals("π", mathEngine.evaluate("rad(180)"));
assertEquals(String.valueOf(-Math.PI), mathEngine.evaluate("rad(-180)"));
// todo serso: think about zeroes
Assert.assertEquals("rad(-180, 0, 0)", mathEngine.simplify("rad(-180)"));
Assert.assertEquals("rad(2, 0, 0)", mathEngine.simplify("rad(1+1)"));
assertEquals("rad(-180, 0, 0)", mathEngine.simplify("rad(-180)"));
assertEquals("rad(2, 0, 0)", mathEngine.simplify("rad(1+1)"));
Assert.assertEquals("rad(-180, 0, 0)", mathEngine.elementary("rad(-180)"));
Assert.assertEquals("rad(2, 0, 0)", mathEngine.elementary("rad(1+1)"));
assertEquals("rad(-180, 0, 0)", mathEngine.elementary("rad(-180)"));
assertEquals("rad(2, 0, 0)", mathEngine.elementary("rad(1+1)"));
Assert.assertEquals(mathEngine.evaluate("rad(43.1025)"), mathEngine.evaluate("rad(43,6,9)"));
Assert.assertEquals(mathEngine.evaluate("rad(102.765)"), mathEngine.evaluate("rad(102, 45, 54)"));
assertEquals(mathEngine.evaluate("rad(43.1025)"), mathEngine.evaluate("rad(43,6,9)"));
assertEquals(mathEngine.evaluate("rad(102.765)"), mathEngine.evaluate("rad(102, 45, 54)"));
}
}

View File

@ -2,14 +2,10 @@ package jscl.math.function;
import jscl.AngleUnit;
import jscl.JsclMathEngine;
import junit.framework.Assert;
import org.junit.Test;
/**
* User: serso
* Date: 5/14/12
* Time: 1:15 PM
*/
import static org.junit.Assert.assertEquals;
public class SqrtTest {
@Test
@ -17,13 +13,13 @@ public class SqrtTest {
final JsclMathEngine me = JsclMathEngine.getInstance();
final AngleUnit defaultAngleUnits = me.getAngleUnits();
Assert.assertEquals("0.9999060498015505+0.013707354604707477*i", me.evaluate("√(√(-1))"));
Assert.assertEquals("0.9984971498638638+0.05480366514878954*i", me.evaluate("√(√(-1))^4"));
assertEquals("0.9999060498015505+0.0137073546047075*i", me.evaluate("√(√(-1))"));
assertEquals("0.9984971498638638+0.0548036651487895*i", me.evaluate("√(√(-1))^4"));
try {
me.setAngleUnits(AngleUnit.rad);
Assert.assertEquals("0.7071067811865476+0.7071067811865475*i", me.evaluate("√(√(-1))"));
Assert.assertEquals("-1+277.55575615628914E-18*i", me.evaluate("√(√(-1))^4"));
assertEquals("0.7071067811865476+0.7071067811865475*i", me.evaluate("√(√(-1))"));
assertEquals("-1+0.0000000000000003*i", me.evaluate("√(√(-1))^4"));
} finally {
me.setAngleUnits(defaultAngleUnits);
}

View File

@ -20,9 +20,9 @@ public class CosTest {
me.getConstantsRegistry().addOrUpdate(t.create());
Assert.assertEquals("-sin(t)", me.simplify("∂(cos(t),t,t,1)"));
Assert.assertEquals("∂(cos(t), t, t, 1°)", me.simplify("∂(cos(t),t,t,1°)"));
Assert.assertEquals("-0.17364817766693033", me.evaluate("∂(cos(t),t,t,1)"));
Assert.assertEquals("-0.1736481776669303", me.evaluate("∂(cos(t),t,t,1)"));
Assert.assertEquals("∂(cos(t), t, t, 1°)", me.evaluate("∂(cos(t),t,t,1°)"));
Assert.assertEquals("-0.17364817766693033", me.evaluate("∂(cos(t),t,t,2-1)"));
Assert.assertEquals("-0.17364817766693033", me.evaluate("∂(cos(t),t,t,2^5-31)"));
Assert.assertEquals("-0.1736481776669303", me.evaluate("∂(cos(t),t,t,2-1)"));
Assert.assertEquals("-0.1736481776669303", me.evaluate("∂(cos(t),t,t,2^5-31)"));
}
}

View File

@ -28,7 +28,7 @@ public class SinTest {
me.setAngleUnits(AngleUnit.rad);
Assert.assertEquals("0.5403023058681398", me.evaluate("cos(1)"));
Assert.assertEquals("0.3623577544766736", me.evaluate("cos(1.2)"));
Assert.assertEquals("0.17794455139146614", me.evaluate("∫ab(sin(x), x, 1, 1.2)"));
Assert.assertEquals("0.1779445513914661", me.evaluate("∫ab(sin(x), x, 1, 1.2)"));
} finally {
me.setAngleUnits(AngleUnit.deg);
}

View File

@ -29,9 +29,9 @@ public class TanTest {
me.setAngleUnits(AngleUnit.rad);
assertEquals("-2*ln(2)-ln(cos(x))", me.simplify("∫(tan(x), x)"));
assertEquals("-(2*ln(2)+ln(cos(x*π)))/π", me.simplify("∫(tan(π*x), x)"));
assertEquals("-0.015308831465985804", me.evaluate("ln(cos(10*π/180))"));
assertEquals("-0.0153088314659858", me.evaluate("ln(cos(10*π/180))"));
assertEquals("-0.1438410362258904", me.evaluate("ln(cos(30*π/180))"));
assertEquals("0.12853220475990468", me.evaluate("∫ab(tan(x), x, 10*π/180, 30*π/180)"));
assertEquals("0.1285322047599047", me.evaluate("∫ab(tan(x), x, 10*π/180, 30*π/180)"));
} finally {
me.setAngleUnits(AngleUnit.deg);
}

View File

@ -7,17 +7,12 @@ import org.junit.Test;
import static junit.framework.Assert.assertEquals;
/**
* User: serso
* Date: 5/14/12
* Time: 2:24 PM
*/
public class ComplexTest {
@Test
public void testSmallImag() throws Exception {
assertEquals("1+100E-18*i", Complex.valueOf(1, 0.0000000000000001).toString());
assertEquals("1-100E-18*i", Complex.valueOf(1, -0.0000000000000001).toString());
assertEquals("1+0.0000000000000001*i", Complex.valueOf(1, 0.0000000000000001).toString());
assertEquals("1-0.0000000000000001*i", Complex.valueOf(1, -0.0000000000000001).toString());
}
@Test
@ -25,10 +20,10 @@ public class ComplexTest {
try {
JsclMathEngine.getInstance().setAngleUnits(AngleUnit.rad);
assertEquals("1.543080634815244", Expression.valueOf("cos(i)").numeric().toString());
assertEquals("1.1752011936438014*i", Expression.valueOf("sin(i)").numeric().toString());
assertEquals("11013.232874703395*i", Expression.valueOf("sin(10*i)").numeric().toString());
assertEquals("11013.232920103324", Expression.valueOf("cos(10*i)").numeric().toString());
assertEquals("0.46211715726000974*i", Expression.valueOf("tan(i/2)").numeric().toString());
assertEquals("1.175201193643801*i", Expression.valueOf("sin(i)").numeric().toString());
assertEquals("11013.2328747034*i", Expression.valueOf("sin(10*i)").numeric().toString());
assertEquals("11013.23292010332", Expression.valueOf("cos(10*i)").numeric().toString());
assertEquals("0.4621171572600097*i", Expression.valueOf("tan(i/2)").numeric().toString());
assertEquals("-2.163953413738653*i", Expression.valueOf("cot(i/2)").numeric().toString());
} finally {
JsclMathEngine.getInstance().setAngleUnits(JsclMathEngine.DEFAULT_ANGLE_UNITS);

View File

@ -48,7 +48,7 @@ public class PercentTest {
Assert.assertEquals("-49", mathEngine.evaluate("1-(100-50%)"));
Assert.assertEquals("50", mathEngine.evaluate("100-50%"));
Assert.assertEquals("2600", mathEngine.evaluate("100+50%^2"));
Assert.assertEquals("101.08138265680029", mathEngine.evaluate("100+50^2%"));
Assert.assertEquals("101.0813826568003", mathEngine.evaluate("100+50^2%"));
Assert.assertEquals("22500", mathEngine.evaluate("(100+50%)^2"));
Assert.assertEquals("225", mathEngine.evaluate("(100+50%)+50%"));
Assert.assertEquals("225", mathEngine.evaluate("(100+50%)+(abs(-50)+10-10)%"));

View File

@ -3,14 +3,10 @@ package jscl.math.operator;
import jscl.JsclMathEngine;
import jscl.math.function.Constant;
import jscl.math.function.ExtendedConstant;
import org.junit.Assert;
import org.junit.Test;
/**
* User: serso
* Date: 1/30/12
* Time: 4:17 PM
*/
import static org.junit.Assert.assertEquals;
public class SumTest {
@Test
@ -20,7 +16,7 @@ public class SumTest {
me.getConstantsRegistry().addOrUpdate(x.create());
final ExtendedConstant.Builder i = new ExtendedConstant.Builder(new Constant("i"), (String) null);
me.getConstantsRegistry().addOrUpdate(i.create());
Assert.assertEquals("51.735296462438285", me.evaluate("Σ((1+x/i)^i, i, 1, 10)"));
Assert.assertEquals("686.0048440525586", me.evaluate("Σ((1+x/i)^i, i, 1, 100)"));
assertEquals("51.73529646243829", me.evaluate("Σ((1+x/i)^i, i, 1, 10)"));
assertEquals("686.0048440525586", me.evaluate("Σ((1+x/i)^i, i, 1, 100)"));
}
}

View File

@ -0,0 +1,36 @@
package org.solovyev.common;
import org.junit.Before;
import org.junit.Test;
import static org.junit.Assert.assertEquals;
public class NumberFormatterTest {
private NumberFormatter numberFormatter;
@Before
public void setUp() throws Exception {
numberFormatter = new NumberFormatter();
}
@Test
public void testEngineeringFormat() throws Exception {
numberFormatter.useEngineeringFormat(5);
assertEquals("0.1", numberFormatter.format(0.1d));
assertEquals("0.01", numberFormatter.format(0.01d));
assertEquals("0.001", numberFormatter.format(0.001d));
assertEquals("5", numberFormatter.format(5d));
assertEquals("5000", numberFormatter.format(5000d));
}
@Test
public void testSimpleFormat() throws Exception {
numberFormatter.useSimpleFormat();
assertEquals("5000000000000000000", numberFormatter.format(5000000000000000000d));
assertEquals("5000000000000000000", numberFormatter.format(5000000000000000001d));
assertEquals("5999999999999994900", numberFormatter.format(5999999999999994999d));
assertEquals("5E19", numberFormatter.format(50000000000000000000d));
assertEquals("5E40", numberFormatter.format(50000000000000000000000000000000000000000d));
}
}