Skip to content

Commit

Permalink
Implied Multiplication bug when blank character fix #292
Browse files Browse the repository at this point in the history
  • Loading branch information
mariuszgromada committed Jan 8, 2023
1 parent 4ad3111 commit 4b90d33
Show file tree
Hide file tree
Showing 8 changed files with 224 additions and 27 deletions.
43 changes: 26 additions & 17 deletions CURRENT/c-sharp/src/org/mariuszgromada/math/mxparser/Expression.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* @(#)Expression.cs 5.2.0 2023-01-07
* @(#)Expression.cs 5.2.0 2023-01-08
*
* MathParser.org-mXparser DUAL LICENSE AGREEMENT as of date 2022-05-22
* The most up-to-date license is available at the below link:
Expand Down Expand Up @@ -6050,8 +6050,7 @@ private void initialTokensAdd(Token token) {
initialTokens.Add(Token.makeMultiplyToken());
initialTokens.Add(token);
return;
}
else impliedMultiplicationError = true;
} else impliedMultiplicationError = true;
}
}
else if (precedingToken.isSpecialTokenName()) {
Expand All @@ -6063,8 +6062,7 @@ private void initialTokensAdd(Token token) {
initialTokens.Add(Token.makeMultiplyToken());
initialTokens.Add(token);
return;
}
else impliedMultiplicationError = true;
} else impliedMultiplicationError = true;
}
}
else if (token.isLeftParenthesis()) {
Expand All @@ -6074,26 +6072,23 @@ private void initialTokensAdd(Token token) {
initialTokens.Add(Token.makeMultiplyToken());
initialTokens.Add(token);
return;
}
else impliedMultiplicationError = true;
} else impliedMultiplicationError = true;
}
// '2(' case
if (precedingToken.isNumber()) {
if (impliedMultiplicationMode) {
initialTokens.Add(Token.makeMultiplyToken());
initialTokens.Add(token);
return;
}
else impliedMultiplicationError = true;
} else impliedMultiplicationError = true;
}
// 'e(', 'pi(' cases
if (precedingToken.isIdentifier()) {
if (impliedMultiplicationMode) {
initialTokens.Add(Token.makeMultiplyToken());
initialTokens.Add(token);
return;
}
else impliedMultiplicationError = true;
} else impliedMultiplicationError = true;
}
} else if (precedingToken.isRightParenthesis()) {
// ')2', ')h.1212', ')1_2_3' cases
Expand All @@ -6102,8 +6097,7 @@ private void initialTokensAdd(Token token) {
initialTokens.Add(Token.makeMultiplyToken());
initialTokens.Add(token);
return;
}
else impliedMultiplicationError = true;
} else impliedMultiplicationError = true;
}
// ')x', ')sin(x)', ')[sdf]' cases
if (!token.isParameterSeparator() &&
Expand All @@ -6114,8 +6108,7 @@ private void initialTokensAdd(Token token) {
initialTokens.Add(Token.makeMultiplyToken());
initialTokens.Add(token);
return;
}
else impliedMultiplicationError = true;
} else impliedMultiplicationError = true;
}
} else if (token.isUnicodeRootOperator()) {
/* Unicode root operator */
Expand All @@ -6127,8 +6120,24 @@ private void initialTokensAdd(Token token) {
initialTokens.Add(Token.makeMultiplyToken());
initialTokens.Add(token);
return;
}
else impliedMultiplicationError = true;
} else impliedMultiplicationError = true;
}
} else if (!token.isLeftParenthesis()
&& !token.isRightParenthesis()
&& !token.isBinaryOperator()
&& !token.isParameterSeparator()
&& !token.isUnaryRightOperator()) {
/* Blank support: '2 x', 'n x', 'n sin(x)' */
if (!precedingToken.isLeftParenthesis() &&
!precedingToken.isRightParenthesis() &&
!precedingToken.isBinaryOperator() &&
!precedingToken.isParameterSeparator() &&
!precedingToken.isUnaryLeftOperator()) {
if (impliedMultiplicationMode) {
initialTokens.Add(Token.makeMultiplyToken());
initialTokens.Add(token);
return;
} else impliedMultiplicationError = true;
}
}
/* End: Implied Multiplication related part*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -457,7 +457,7 @@ public static double forwardDifference(Expression f, double h, Argument x, doubl
* @param h the difference
* @param x the argument name
*
* @return Forward difference(h) value calculated at at the current value of the argument x.
* @return Forward difference(h) value calculated at the current value of the argument x.
*
* @see Expression
* @see Argument
Expand Down Expand Up @@ -500,7 +500,7 @@ public static double backwardDifference(Expression f, double h, Argument x, doub
* @param h the difference
* @param x the argument name
*
* @return Backward difference(h) value calculated at at the current value of the argument x.
* @return Backward difference(h) value calculated at the current value of the argument x.
*
* @see Expression
* @see Argument
Expand Down
53 changes: 51 additions & 2 deletions CURRENT/c-sharp/tests-and-release/4-Unit-Tests/ApiTest.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* @(#)ApiTest.cs 5.2.0 2023-01-04
* @(#)ApiTest.cs 5.2.0 2023-01-08
*
* MathParser.org-mXparser DUAL LICENSE AGREEMENT as of date 2022-05-22
* The most up-to-date license is available at the below link:
Expand Down Expand Up @@ -6764,7 +6764,56 @@ public void testApi0244() {
TestCommonTools.consolePrintTestApiEnd(testResult);
Assert.IsTrue(testResult);
}

[TestMethod]
public void testApi0245() {
TestCommonTools.testApiSettingsInit();
bool testResult = false;
String testDescr = "Implied Multiplication & canonical expression string test";
TestCommonTools.consolePrintTestApiStart(245, testDescr);
testResult = testCanonicalString("2 x", "2*x");
TestCommonTools.consolePrintTestApiEnd(testResult);
Assert.IsTrue(testResult);
}
[TestMethod]
public void testApi0246() {
TestCommonTools.testApiSettingsInit();
bool testResult = false;
String testDescr = "Implied Multiplication & canonical expression string test";
TestCommonTools.consolePrintTestApiStart(246, testDescr);
testResult = testCanonicalString("x 2 ", "x*2");
TestCommonTools.consolePrintTestApiEnd(testResult);
Assert.IsTrue(testResult);
}
[TestMethod]
public void testApi0247() {
TestCommonTools.testApiSettingsInit();
bool testResult = false;
String testDescr = "Implied Multiplication & canonical expression string test";
TestCommonTools.consolePrintTestApiStart(247, testDescr);
testResult = testCanonicalString("2 x sin(x) ", "2*x*sin(x)");
TestCommonTools.consolePrintTestApiEnd(testResult);
Assert.IsTrue(testResult);
}
[TestMethod]
public void testApi0248() {
TestCommonTools.testApiSettingsInit();
bool testResult = false;
String testDescr = "Implied Multiplication & canonical expression string test";
TestCommonTools.consolePrintTestApiStart(248, testDescr);
testResult = testCanonicalString(" ( 2+3) n x sin( x ) ", "(2+3)*n*x*sin(x)");
TestCommonTools.consolePrintTestApiEnd(testResult);
Assert.IsTrue(testResult);
}
[TestMethod]
public void testApi0249() {
TestCommonTools.testApiSettingsInit();
bool testResult = false;
String testDescr = "Implied Multiplication & canonical expression string test";
TestCommonTools.consolePrintTestApiStart(249, testDescr);
testResult = testCanonicalString("- (-2+3) -n -x sin( - x ) ", "-(-2+3)-n-x*sin(-x)");
TestCommonTools.consolePrintTestApiEnd(testResult);
Assert.IsTrue(testResult);
}
public static bool testCanonicalString(String expStr, String expResStr, params String[] elements) {
mXparser.consolePrintln();
mXparser.consolePrintln("------ expStr = " + expStr);
Expand Down
38 changes: 37 additions & 1 deletion CURRENT/c-sharp/tests-and-release/4-Unit-Tests/SyntaxTest.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* @(#)SyntaxTest.cs 5.2.0 2022-12-31
* @(#)SyntaxTest.cs 5.2.0 2023-01-08
*
* MathParser.org-mXparser DUAL LICENSE AGREEMENT as of date 2022-05-22
* The most up-to-date license is available at the below link:
Expand Down Expand Up @@ -6076,5 +6076,41 @@ public void testSyn0385() {
TestCommonTools.consolePrintTestSynEnd(syn, reg, testResult, e);
Assert.IsTrue(testResult);
}
[TestMethod]
public void testSyn0386() {
TestCommonTools.testSynSettingsInit();
bool testResult = false;
String testDescr = "MULTIPLICATION_OPERATOR_MISSING_TRY_IMPLIED_MULTIPLICATION_MODE";
String expStr = "2 pi*sin(pi)";
TestCommonTools.consolePrintTestSynStart(386, testDescr + " " + expStr);
Expression e = new Expression(expStr);
e.disableImpliedMultiplicationMode();
bool syn = e.checkSyntax();
bool reg = false;
StringResources stringResources = StringModel.getStringResources();
String errorMessage = e.getErrorMessage();
if (syn == reg && errorMessage.Contains(stringResources.MULTIPLICATION_OPERATOR_MISSING_TRY_IMPLIED_MULTIPLICATION_MODE))
testResult = true;
TestCommonTools.consolePrintTestSynEnd(syn, reg, testResult, e);
Assert.IsTrue(testResult);
}
[TestMethod]
public void testSyn0387() {
TestCommonTools.testSynSettingsInit();
bool testResult = false;
String testDescr = "Implied Multiplication with blank spaces";
String expStr = "2 pi*sin(pi)";
TestCommonTools.consolePrintTestSynStart(387, testDescr + " " + expStr);
Expression e = new Expression(expStr);
e.enableImpliedMultiplicationMode();
bool syn = e.checkSyntax();
bool reg = true;
StringResources stringResources = StringModel.getStringResources();
String errorMessage = e.getErrorMessage();
if (syn == reg && errorMessage.Contains(stringResources.NO_ERRORS_DETECTED))
testResult = true;
TestCommonTools.consolePrintTestSynEnd(syn, reg, testResult, e);
Assert.IsTrue(testResult);
}
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* @(#)Expression.java 5.2.0 2023-01-07
* @(#)Expression.java 5.2.0 2023-01-08
*
* MathParser.org-mXparser DUAL LICENSE AGREEMENT as of date 2022-05-22
* The most up-to-date license is available at the below link:
Expand Down Expand Up @@ -6140,6 +6140,23 @@ private void initialTokensAdd(Token token) {
return;
} else impliedMultiplicationError = true;
}
} else if (!token.isLeftParenthesis()
&& !token.isRightParenthesis()
&& !token.isBinaryOperator()
&& !token.isParameterSeparator()
&& !token.isUnaryRightOperator()) {
/* Blank support: '2 x', 'n x', 'n sin(x)' */
if (!precedingToken.isLeftParenthesis() &&
!precedingToken.isRightParenthesis() &&
!precedingToken.isBinaryOperator() &&
!precedingToken.isParameterSeparator() &&
!precedingToken.isUnaryLeftOperator()) {
if (impliedMultiplicationMode) {
initialTokens.add(Token.makeMultiplyToken());
initialTokens.add(token);
return;
} else impliedMultiplicationError = true;
}
}
/* End: Implied Multiplication related part*/
initialTokens.add(token);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -456,7 +456,7 @@ public static double forwardDifference(Expression f, double h, Argument x, doubl
* @param h the difference
* @param x the argument name
*
* @return Forward difference(h) value calculated at at the current value of the argument x.
* @return Forward difference(h) value calculated at the current value of the argument x.
*
* @see Expression
* @see Argument
Expand Down Expand Up @@ -499,7 +499,7 @@ public static double backwardDifference(Expression f, double h, Argument x, doub
* @param h the difference
* @param x the argument name
*
* @return Backward difference(h) value calculated at at the current value of the argument x.
* @return Backward difference(h) value calculated at the current value of the argument x.
*
* @see Expression
* @see Argument
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* @(#)SyntaxTest.java 5.2.0 2023-01-04
* @(#)SyntaxTest.java 5.2.0 2023-01-08
*
* MathParser.org-mXparser DUAL LICENSE AGREEMENT as of date 2022-05-22
* The most up-to-date license is available at the below link:
Expand Down Expand Up @@ -6767,6 +6767,56 @@ public void testApi0244() {
TestCommonTools.consolePrintTestApiEnd(testResult);
Assertions.assertTrue(testResult);
}
@Test
public void testApi0245() {
TestCommonTools.testApiSettingsInit();
boolean testResult = false;
String testDescr = "Implied Multiplication & canonical expression string test";
TestCommonTools.consolePrintTestApiStart(245, testDescr);
testResult = testCanonicalString("2 x", "2*x");
TestCommonTools.consolePrintTestApiEnd(testResult);
Assertions.assertTrue(testResult);
}
@Test
public void testApi0246() {
TestCommonTools.testApiSettingsInit();
boolean testResult = false;
String testDescr = "Implied Multiplication & canonical expression string test";
TestCommonTools.consolePrintTestApiStart(246, testDescr);
testResult = testCanonicalString("x 2 ", "x*2");
TestCommonTools.consolePrintTestApiEnd(testResult);
Assertions.assertTrue(testResult);
}
@Test
public void testApi0247() {
TestCommonTools.testApiSettingsInit();
boolean testResult = false;
String testDescr = "Implied Multiplication & canonical expression string test";
TestCommonTools.consolePrintTestApiStart(247, testDescr);
testResult = testCanonicalString("2 x sin(x) ", "2*x*sin(x)");
TestCommonTools.consolePrintTestApiEnd(testResult);
Assertions.assertTrue(testResult);
}
@Test
public void testApi0248() {
TestCommonTools.testApiSettingsInit();
boolean testResult = false;
String testDescr = "Implied Multiplication & canonical expression string test";
TestCommonTools.consolePrintTestApiStart(248, testDescr);
testResult = testCanonicalString(" ( 2+3) n x sin( x ) ", "(2+3)*n*x*sin(x)");
TestCommonTools.consolePrintTestApiEnd(testResult);
Assertions.assertTrue(testResult);
}
@Test
public void testApi0249() {
TestCommonTools.testApiSettingsInit();
boolean testResult = false;
String testDescr = "Implied Multiplication & canonical expression string test";
TestCommonTools.consolePrintTestApiStart(249, testDescr);
testResult = testCanonicalString("- (-2+3) -n -x sin( - x ) ", "-(-2+3)-n-x*sin(-x)");
TestCommonTools.consolePrintTestApiEnd(testResult);
Assertions.assertTrue(testResult);
}
public static boolean testCanonicalString(String expStr, String expResStr, String... elements) {
mXparser.consolePrintln();
mXparser.consolePrintln("------ expStr = " + expStr);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* @(#)SyntaxTest.java 5.2.0 2022-12-31
* @(#)SyntaxTest.java 5.2.0 2023-01-08
*
* MathParser.org-mXparser DUAL LICENSE AGREEMENT as of date 2022-05-22
* The most up-to-date license is available at the below link:
Expand Down Expand Up @@ -6073,4 +6073,40 @@ public void testSyn0385() {
TestCommonTools.consolePrintTestSynEnd(syn, reg, testResult, e);
Assertions.assertTrue(testResult);
}
@Test
public void testSyn0386() {
TestCommonTools.testSynSettingsInit();
boolean testResult = false;
String testDescr = "MULTIPLICATION_OPERATOR_MISSING_TRY_IMPLIED_MULTIPLICATION_MODE";
String expStr = "2 pi*sin(pi)";
TestCommonTools.consolePrintTestSynStart(386, testDescr + " " + expStr);
Expression e = new Expression(expStr);
e.disableImpliedMultiplicationMode();
boolean syn = e.checkSyntax();
boolean reg = false;
StringResources stringResources = StringModel.getStringResources();
String errorMessage = e.getErrorMessage();
if (syn == reg && errorMessage.contains(stringResources.MULTIPLICATION_OPERATOR_MISSING_TRY_IMPLIED_MULTIPLICATION_MODE))
testResult = true;
TestCommonTools.consolePrintTestSynEnd(syn, reg, testResult, e);
Assertions.assertTrue(testResult);
}
@Test
public void testSyn0387() {
TestCommonTools.testSynSettingsInit();
boolean testResult = false;
String testDescr = "Implied Multiplication with blank spaces";
String expStr = "2 pi*sin(pi)";
TestCommonTools.consolePrintTestSynStart(387, testDescr + " " + expStr);
Expression e = new Expression(expStr);
e.enableImpliedMultiplicationMode();
boolean syn = e.checkSyntax();
boolean reg = true;
StringResources stringResources = StringModel.getStringResources();
String errorMessage = e.getErrorMessage();
if (syn == reg && errorMessage.contains(stringResources.NO_ERRORS_DETECTED))
testResult = true;
TestCommonTools.consolePrintTestSynEnd(syn, reg, testResult, e);
Assertions.assertTrue(testResult);
}
}

0 comments on commit 4b90d33

Please sign in to comment.