Jul 03, 2024

Wiki

Python

Aide

edit SideBar

Search

Recherche De Motifs


On peut utiliser la méthode .match() pour de la recherche de motifs au sein d'une expression :

Expressions

  >>> from sympy import *
  >>> x = Symbol('x')
  >>> p = Wild('p')
  >>> q = Wild('q')
  >>> (5*x**2 + 3*x).match(p*x**2 + q*x)
  {p_: 5, q_: 3}

  >>> (x**2).match(p*x**q)
  {p_: 1, q_: 2}

S'il n'y a aucun motif correspondant, le retour est None :

  >>> print (x+1).match(p**x)
  None

Fonctions

Il est possible d'étendre cette recherche de motifs aux fonctions :

  >>> f = WildFunction('f', nofargs=1)
  >>> (5*cos(x)).match(p*f)
  {p_: 5, f_: cos(x)}

  >>> (cos(3*x)).match(f(p*x))
  {p_: 3, f_: cos}

  >>> g = WildFunction('g', nofargs=2)
  >>> (5*cos(x)).match(p*g)
  None

Exclusion de résultats

Enfin, on peut exclure certains résultats dans la recherche de motifs :

  >>> x = Symbol('x')
  >>> p = Wild('p', exclude=[1,x])
  >>> print (x+1).match(x+p)
  None

  >>> print (x+1).match(p+1)
  None

  >>> print (x+1).match(x+2+p)
  {p_: -1}

Page Actions

Recent Changes

Group & Page

Back Links