It is easy to write a custom match; in the following example there is a 
match for a quartic
form; it is easy to write additional conditions, like restriction to 3 
variables, integer coefficients,
etc.

>>> def match4(t):
...     "match a quartic form"
...     a = []
...     for xx in t.args:
...         f = Factors(xx).factors
...         c = 1
...         d = {}
...         tot_deg = 0
...         for k, v in f.items():
...             if k.is_number:
...                 c = c*k**v
...             else:
...                 d[k] = v
...                 if not v.is_integer or v < 0:
...                     return None
...                 tot_deg += v
...         if tot_deg != 4:
...             return None
...         a.append((c, d))
...     return a
... 
>>> t = x**2*y**2 - 2*x**2*y*z + x**2*z**2 - 2*x*y**2*z - 2*x*y*z**2 + 
y**2*z**2
>>> match4(t)
[(1, {z: 2, y: 2}), (-2, {x: 1, z: 2, y: 1}), (1, {x: 2, z: 2}), (-2, {x: 
1, z: 1, y: 2}), (-2, {x: 2, z: 1, y: 1}), (1, {x: 2, y: 2})]


On Sunday, September 15, 2013 8:13:29 PM UTC+2, Thilina Rathnayake wrote:
>
> Hi All,
>
> Addressing issue 4004<https://code.google.com/p/sympy/issues/detail?id=4004>, 
> the equation after expanding can be written as below,
>
> In [1]: t = (x*y + y*z + x*z)**2 - 4*x*y*z*(x + y + z)
>> In [2]: expand(t)
>> Out[2]: 
>>  2  2      2        2  2        2            2    2  2
>> x ⋅y  - 2⋅x ⋅y⋅z + x ⋅z  - 2⋅x⋅y ⋅z - 2⋅x⋅y⋅z  + y ⋅z 
>>
>
> This is of the form `X**2 + Y**2 + Z**2 - 2*X*Y - 2*Y*Z - 2*X*Z` with X = 
> x*y, Y = y*z and
> Z = z*x. Latter can be solved by the Diophantine module for X, Y, Z and we 
> can recover
> solutions for x, y and z.
>
>  I tried to automate this process with Wild's and `match()` but couldn't 
> do it. Given
> an expression, I tried to determine if it is in the form `ap**2 + bq**2 + 
> cr**2 + d*p*q + e*q*r + f*r*p`
> where a, b, c, d, e, f are Wilds trying to match Integers(used exclude=[x, 
> y, z] while 
> creating these) and p, q, r trying to match the variables.
>
>  In[3]: _.match(a*p**2 + b*q**2 + c*r**2 + d*p*q + e*q*r + f*r*q)
>
> Above kept running forever. Is there a way to efficiently pattern match an 
> expression
> so that we can decide whether it belongs to a particular form or not?
>
> Regards,
> Thilina.
>
>

-- 
You received this message because you are subscribed to the Google Groups 
"sympy" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/sympy.
For more options, visit https://groups.google.com/groups/opt_out.

Reply via email to