Skip to content

Commit 0c83b57

Browse files
committed
Convert relational functions: ITE,Eq,Ne,Lt,Le,Gt,Ge,...
1 parent c6e404c commit 0c83b57

File tree

3 files changed

+67
-17
lines changed

3 files changed

+67
-17
lines changed

SympyToSymja/src/main/java/org/matheclipse/core/sympy2symja/SymjaExpressionVisitor.java

+40-10
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,10 @@ public class SymjaExpressionVisitor
7979

8080
public static final Map<String, String> S_CONSTANT_SYMBOLS_MAP =
8181
ParserConfig.TRIE_STRING2STRING_BUILDER.withMatch(TrieMatch.EXACT).build();
82+
83+
public static final Map<String, String> FUNCTIONS_SYMBOLS_MAP =
84+
ParserConfig.TRIE_STRING2STRING_BUILDER.withMatch(TrieMatch.EXACT).build();
85+
8286
static {
8387
CONSTANT_SYMBOLS_MAP.put("oo", "CInfinity");
8488
CONSTANT_SYMBOLS_MAP.put("pi", "Pi");
@@ -91,7 +95,24 @@ public class SymjaExpressionVisitor
9195
S_CONSTANT_SYMBOLS_MAP.put("NegativeInfinity", "CNInfinity");
9296
S_CONSTANT_SYMBOLS_MAP.put("NaN", "Indeterminate");
9397
S_CONSTANT_SYMBOLS_MAP.put("One", "C1");
98+
S_CONSTANT_SYMBOLS_MAP.put("Pi", "Pi");
9499
S_CONSTANT_SYMBOLS_MAP.put("Zero", "C0");
100+
101+
FUNCTIONS_SYMBOLS_MAP.put("ITE", "If");
102+
FUNCTIONS_SYMBOLS_MAP.put("Eq", "Equal");
103+
FUNCTIONS_SYMBOLS_MAP.put("Equality", "Equal");
104+
FUNCTIONS_SYMBOLS_MAP.put("Ne", "Unequal");
105+
FUNCTIONS_SYMBOLS_MAP.put("Unequality", "Unequal");
106+
107+
FUNCTIONS_SYMBOLS_MAP.put("Lt", "Less");
108+
FUNCTIONS_SYMBOLS_MAP.put("StrictLessThan", "Less");
109+
FUNCTIONS_SYMBOLS_MAP.put("LessThan", "LessEqual");
110+
FUNCTIONS_SYMBOLS_MAP.put("Le", "LessEqual");
111+
FUNCTIONS_SYMBOLS_MAP.put("Gt", "Greater");
112+
FUNCTIONS_SYMBOLS_MAP.put("StrictGreaterThan", "Greater");
113+
FUNCTIONS_SYMBOLS_MAP.put("GreaterThan", "GreaterEqual");
114+
FUNCTIONS_SYMBOLS_MAP.put("Ge", "GreaterEqual");
115+
95116
}
96117

97118
@Override
@@ -181,13 +202,13 @@ public String visitAtom(Atom atom, SymjaIndentation param) {
181202
if (args.size() == 1) {
182203
Expression arg1 = args.get(0);
183204
// String numberString = ((Num) arg1).getN();
184-
string = "F.IntegerPart(";
185-
string = string.concat(arg1.accept(new SymjaExpressionVisitor(),
186-
new SymjaIndentation(param.getIndentationLevel())));
187-
string = string + ")";
188-
return string;
205+
string = "F.IntegerPart(";
206+
string = string.concat(arg1.accept(new SymjaExpressionVisitor(),
207+
new SymjaIndentation(param.getIndentationLevel())));
208+
string = string + ")";
209+
return string;
189210

190-
}
211+
}
191212
}
192213
} else if (name.equals("Float") && trailers.size() == 1) {
193214
Expression expression = trailers.get(0);
@@ -225,11 +246,20 @@ public String visitAtom(Atom atom, SymjaIndentation param) {
225246
}
226247
}
227248
}
228-
String str = AST2Expr.PREDEFINED_SYMBOLS_MAP.get(name.toLowerCase());
229-
if (str != null) {
230-
string = "F." + str;
249+
if (name.equals("Tuple")) {
250+
string = "F.List";
231251
} else {
232-
string = name;
252+
String str = FUNCTIONS_SYMBOLS_MAP.get(name);
253+
if (str != null) {
254+
string = "F." + str;
255+
} else {
256+
str = AST2Expr.PREDEFINED_SYMBOLS_MAP.get(name.toLowerCase());
257+
if (str != null) {
258+
string = "F." + str;
259+
} else {
260+
string = name;
261+
}
262+
}
233263
}
234264
} else {
235265
string = string.concat(atomElement.accept(new SymjaExpressionVisitor(),

SympyToSymja/src/main/java/org/matheclipse/core/sympy2symja/SymjaOperatorVisitor.java

+4-4
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ public String visitMult(Mult mult, SymjaIndentation param) {
7979

8080
@Override
8181
public String visitPow(Pow pow, SymjaIndentation param) {
82-
return "Power";
82+
return "F.Power";
8383
}
8484

8585
@Override
@@ -94,17 +94,17 @@ public String visitSub(Sub sub, SymjaIndentation param) {
9494

9595
@Override
9696
public String visitEq(Eq eq, SymjaIndentation param) {
97-
return "Equal";
97+
return "F.Equal";
9898
}
9999

100100
@Override
101101
public String visitGt(Gt gt, SymjaIndentation param) {
102-
return "Greater";
102+
return "F.Greater";
103103
}
104104

105105
@Override
106106
public String visitGtE(GtE gte, SymjaIndentation param) {
107-
return "GreaterEqual";
107+
return "F.GreaterEqual";
108108
}
109109

110110
@Override

SympyToSymja/src/test/java/org/matheclipse/core/sympy2symja/SympyToSymjaTest.java

+23-3
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,14 @@ public void testBessel() {
2222
"F.Subtract(F.Times(F.Csc(F.Times(F.Pi, nu)), F.BesselJ(F.Negate(nu), z)), F.Times(F.Cot(F.Times(F.Pi, nu)), F.BesselY(nu, z)))");
2323
}
2424

25+
@Test
26+
public void testLineComment() {
27+
check("test # this is a line comment", //
28+
"test");
29+
check("integrate((1 - t**z) / (1 - t), (t, 0, 1)) # this is a line comment", //
30+
"F.Integrate(F.Divide(F.Subtract(F.C1, F.Power(t, z)), F.Subtract(F.C1, t)), F.List(t, F.C0, F.C1))");
31+
}
32+
2533
@Test
2634
public void testFloat() {
2735
INum apfloat = F.num(new Apfloat("0.3", 20));
@@ -49,21 +57,33 @@ public void testLegendreExpr() {
4957
check("(k, 0, floor((n - m)*S.Half))", //
5058
"F.List(k, F.C0, F.Floor(F.Times(F.Subtract(n, m), F.C1D2)))");
5159
check("(1 - x**2)**(m/2) * Sum(kern, (k, 0, floor((n - m)*S.Half)))", //
52-
"F.Times(Power(F.Subtract(F.C1, Power(x, F.C2)), F.Divide(m, F.C2)), F.Sum(kern, F.List(k, F.C0, F.Floor(F.Times(F.Subtract(n, m), F.C1D2)))))");
60+
"F.Times(F.Power(F.Subtract(F.C1, F.Power(x, F.C2)), F.Divide(m, F.C2)), F.Sum(kern, F.List(k, F.C0, F.Floor(F.Times(F.Subtract(n, m), F.C1D2)))))");
5361
}
5462

5563
@Test
5664
public void testIntegrate() {
5765
check("-oo", //
5866
"F.CNInfinity");
5967
check("integrate(exp(-x**2), (x, -oo, oo))", //
60-
"F.Integrate(F.Exp(F.Negate(Power(x, F.C2))), F.List(x, F.CNInfinity, F.CInfinity))");
68+
"F.Integrate(F.Exp(F.Negate(F.Power(x, F.C2))), F.List(x, F.CNInfinity, F.CInfinity))");
69+
}
70+
71+
@Test
72+
public void testRelations() {
73+
check("(Tuple(Le(a, b), Le(c, a)), ITE(b >= c, true, Or(Le(a, b), Ge(a, c))))", //
74+
"F.List(F.List(F.LessEqual(a, b), F.LessEqual(c, a)), F.If(F.GreaterEqual(b, c), true, F.Or(F.LessEqual(a, b), F.GreaterEqual(a, c))))");
75+
}
76+
77+
@Test
78+
public void testProduct() {
79+
check("product(ratio, (n, n0, n - 1))", //
80+
"F.Product(ratio, F.List(n, n0, F.Subtract(n, F.C1)))");
6181
}
6282

6383
@Test
6484
public void testPower() {
6585
check("x*3**Rational(1, 6) / gamma(Rational(1, 3))", //
66-
"F.Divide(F.Times(x, Power(F.C3, F.Rational(F.C1, F.C6))), F.Gamma(F.Rational(F.C1, F.C3)))");
86+
"F.Divide(F.Times(x, F.Power(F.C3, F.Rational(F.C1, F.C6))), F.Gamma(F.Rational(F.C1, F.C3)))");
6787
}
6888

6989
private void check(String str, String expected) {

0 commit comments

Comments
 (0)