Open GoogleCodeExporter opened 9 years ago
GiNaC supports patterns in has/subs but their algorithm contains
some random factors.
See
http://www.ginac.de/tutorial/Pattern-matching-and-advanced-substitutions.html
Original comment by pearu.peterson
on 17 Jan 2008 at 3:27
We have match/subs/has methods that are related as follows:
expr.match(pattern) -> {patternsubexpr: replacement}
expr.subs(subexpr, replacement) -> newexpr
expr.has(pattern) -> True|False
Since .has method has a one line implementation:
def has(self, pattern):
return self.match(pattern) is not None
it can be eliminated from the discussion.
match and subs are closely related:
pattern.subs(expr.match(pattern)) -> expr
The question is whether a .subs method with the
given property should have more features. For example,
the above property does not require that .subs
should support patterns.
On the orher hand, we might need a method that
handles a more general matching task:
(x**2 + sin(x**2)).match_subexpr(p**q) -> {p:x,q:2}
Original comment by pearu.peterson
on 17 Jan 2008 at 4:56
Correction:
def has(self, pattern):
return self.match_subexpr(pattern) is not None
Original comment by pearu.peterson
on 17 Jan 2008 at 4:57
Question: how hard match should try to match? Eg
(2*x).match(v*x+1,v)
could either return no match or v=x**(-1)*(2*x - 1).
The later is correct mathematically only if x!=0 but match
should not introduce new silent assumptions.
Also, this behaviour of match seems not useful - it does not
reflect what is the pattern of the original expression.
Pattern matching task is not to solve equations.
Hence, I'd return no match for this particular example.
Original comment by pearu.peterson
on 3 Mar 2008 at 3:59
http://documents.wolfram.com/mathematica/book/section-2.3
answers many questions regarding pattern matching.
Original comment by pearu.peterson
on 3 Mar 2008 at 8:28
Original issue reported on code.google.com by
pearu.peterson
on 17 Jan 2008 at 2:58