Replace Pattern Matching
Issue #103
resolved
I can't really tell does the program support Replace, ReplaceAll and ReplaceRepeated?
Basically does it support pattern matching?
Comments (6)
-
repo owner -
reporter - changed status to resolved
-
reporter @axelclk I closed the issue as resolved, but how would do you examples in Java?
I believe this is a purely Java project yes?
-
repo owner There are methods in the IExpr interface: replaceAll(), replacePart, replaceRepeated()
public static void main(String[] args) { try { F.initSymbols(null, null, false); // expr = a+b+c IAST expr = F.Plus(F.a, F.b, F.c); // rule1 = b -> 10*d IAST rule1 = F.Rule(F.b, F.Times(F.integer(10), F.d)); IExpr result = expr.replaceAll(rule1); if (result != null) { // print: a+10*d+c System.out.println(result.toString()); } IAST listOfRules = F.List(); listOfRules.add(rule1); // rule2 = b -> 10*e IAST rule2 = F.Rule(F.c, F.Times(F.integer(10), F.e)); listOfRules.add(rule2); result = expr.replaceAll(listOfRules); if (result != null) { // print: a+10*d+10*e System.out.println(result.toString()); } } catch (SyntaxError e) { // catch Symja parser errors here System.out.println(e.getMessage()); } catch (MathException me) { // catch Symja math errors here System.out.println(me.getMessage()); } catch (Exception e) { e.printStackTrace(); } }
-
repo owner Added handling of "level specification" as 3rd argument in Replace() function:
https://bitbucket.org/axelclk/symja_android_library/commits/6cac457db85caa1b11235ca621d592709be0a152
-
repo owner a- b /. a -> x
is converted to
ReplaceAll( a- b , a -> x)
see:
FullForm(Hold(a- b /. a -> x))
- Log in to comment
The program should support ReplaceAll, ReplacePart and ReplaceRepeated.
For the missing Replace() function I started this commit: https://bitbucket.org/axelclk/symja_android_library/commits/566ac7b8caebe1d718490c787b0129534a1c7da4
at the moment the "level specification" feature isn't implmented for the Replace() function.
These are some examples which should work: