pearu / sympycore

Automatically exported from code.google.com/p/sympycore
Other
11 stars 1 forks source link

match/subs/has methods #44

Open GoogleCodeExporter opened 9 years ago

GoogleCodeExporter commented 9 years ago
This issue is created to find an answer to:

 Should subs method support patterns?

and that was motivated by the following example

  (x**2 + sin(y**3)).subs(p**q, z) -> z + sin(z)

where p,q are wild symbols. One may wonder if the
above behavior could have an application. If not
then the behavior should be disabled as it may
turn finding bugs difficult.

For information: subs and algsubs in maple do not support patterns.

However, supporting patterns might be useful. Consider
an example:

  (x**2 + sin(x**2)).subs(p**q, z) -> z+sin(z)

(to not let subs to loose information, it could also
return z=x**2 in some way).

match and has are included in the subject as patterns in
all three methods should behave in the same way.

Original issue reported on code.google.com by pearu.peterson on 17 Jan 2008 at 2:58

GoogleCodeExporter commented 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

GoogleCodeExporter commented 9 years ago
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

GoogleCodeExporter commented 9 years ago
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

GoogleCodeExporter commented 9 years ago
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

GoogleCodeExporter commented 9 years ago
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