Re: Re: TransformationFunctions

```On 30 Jul 2009, at 18:30, O wrote:

> On Jul 24, 3:14 am, Andrzej Kozlowski <a... at mimuw.edu.pl> wrote:
>> On 24 Jul 2009, at 15:46, Oles Zaburannyi wrote:
>>
>>
>>
>>
>>
>>> 2009/7/23 Andrzej Kozlowski <a... at mimuw.edu.pl>
>>
>>> On 23 Jul 2009, at 16:54, Peter Breitfeld wrote:
>>
>>> ".... at ntaxa.com" wrote:
>>
>>> Can anyone advice why following code does not work:
>>
>>> In[538]:=tf[z_NonCommutativeMultiply] := -z[[2]]**z[[1]]
>>> In[539]:=Simplify[x ** y + y ** x,TransformationFunctions -> {tf,
>>> Automatic}]
>>> Out[539]:=x ** y + y ** x
>>
>>> I expect rather 0
>>
>>> By the way:
>>> In[540]:=x ** y + tf[y ** x]
>>> Out[540]:=0
>>
>>> I think, the problem here is, that Simplify will apply tf to both
>>> products. So I would suggest you do something like this:
>>
>>> tfrule = (x_ ** y_ + y_ ** x_) :> 0;
>>> tf[expr_] := expr /. tfrule;
>>> Simplify[x ** y + y ** x, TransformationFunctions -> {tf}]
>>
>>> Out=0
>>
>>> -- _________________________________________________________________
>>> Peter Breitfeld, Bad Saulgau, Germany --http://www.pBreitfeld.de
>>
>>> There are problems with this approach.
>>> Consider:
>>
>>> tfrule = (x_ ** y_ + y_ ** x_) :> 0;
>>> tf[expr_] := expr /. tfrule;
>>
>>> Simplify[x ** y + y ** x, TransformationFunctions -> {tf}]
>>> 0
>>
>>> but
>>
>>> Simplify[2 x ** y + y ** x, TransformationFunctions -> {tf}]
>>> 2 x ** y + y ** x
>>
>>> by contrast:
>>
>>> tf1[expr_] :=
>>> expr /. z_NonCommutativeMultiply :> If[Not[OrderedQ[z]], -Sort[z],
>>> z]
>>
>>> In[42]:= Simplify[x ** y + y ** x, TransformationFunctions ->
>>> {Automatic, tf1}]
>>> Out[42]= 0
>>
>>> but also
>>
>>> Simplify[2 x ** y + y ** x, TransformationFunctions -> {Automatic,
>>> tf1}]
>>> x ** y
>>
>>> Andrzej Kozlowski
>>
>>> thanks, but your solution give incorrect result for
>>
>>> In[3]:=Simplify[(z ** x ** y + z ** y ** x),
>>> TransformationFunctions -> {Automatic, tf1}]
>>> Out[3]:=-2 x ** y ** z
>>> (should be 0)
>>
>>> As i understand my main problem is that Simplify apply
>>> transformation function and select simplest form AT EACH STEP.
>>> So, if globally simplest form is achieved only by MORE COMPLEX
>>> intermediate forms that simplification will be omited.
>>
>>> Am i right?
>>
>>> in another words simplification like
>>
>>> initial form -> a bit complex form -> maybe even more complex form -
>>>> simplest form
>>
>>> is omited by Simplify?
>>
>>> --
>>> No Signature
>>
>> You are right. I did not give this enough thought and did not
>> consider =
>
>> the possibility of multiplying more than two
>> elements. However, this is easy to fix:
>>
>> tf1[expr_] :=
>>  expr /. z_NonCommutativeMultiply :>
>>    If[Not[OrderedQ[z]], Signature[List @@ z]*Sort[z], z]
>>
>> I think will not work correctly:
>>
>> Simplify[x ** y + y ** x, TransformationFunctions -> {Automatic,
>> tf1}]
>> 0
>>
>> Simplify[(z ** x ** y + z ** y ** x),
>>  TransformationFunctions -> {Automatic, tf1}]
>>  0
>>
>> As for your question; yes. Simplification generally only proceeds to
>> forms which are simpler (with respect to the given complexity
>> function, which can be user controlled) at every step. However, there
>> are some exceptions to this, because at eeach step simplify remembers
>> not only the form of the expression that has the least complexity
>> found so far but also (for most expression) a standard form of the
>> expression. It will therefore sometimes notice cancellations when
>> between two expressions which have the same standard form, even if
>> there is no complexity decreasing route "connecting" them. But this
>> is =
>
>> not really something that a user can control and shows itself most
>> often when Simplify is used to show that some expression is 0 (rather
>> then in finding the simplest form of the expression).
>>
>> Andrzej Kozlowski
>
>
> can you tell me neither my plan is realistic or not?
>
> for example i have some expression:
>
> a1**a2**a3**a4**a5 and have some transformation function ai**aj ->
> aj**ai + fij
>
> what i want from simplify:
> 1. i expect that simplify will try to to apply my function to EACH
> possible product (namely a1 and a2, a2 and a3, a3 and a4, a4 and a5).

> 2. i expect that even after applying transformation function
> expression will be more complicated simplify will try to transform it
> further.

I think (and I am only guessing), Simplify will apply your function
and then the kind of simplifications that are automatically applied on
evaluation (such as canceling x and - x and so on). It may also
perform some additional cancelations based on using standard forms,
but I am not sure about that. If the complexity as measured by the
given complexity function goes up at this point, this expression will
be abandoned and not transformed any more. I think FullSimplify might
do a little better, but I am not sure. Of course you could try
applying Simplify with different complexity functions, and even define
complexity functions that actually increase "standard complexity". But
it is tricky to get the results one wants in this way.

>
> I think my plan is rather common and a lot of people waste time to do
> something like that, can you advise me only to dig in that direction
> or not?

I think you have to first make clear what really you want Simplify to
do for you. It is not really meant to transform expressions into a
desired form. One purpose is to find expressions equivalent to yours
but simpler with respect to some specified criteria of simplicity
( you can iterate Simplify using a different Complexity function at
each iteration). The other purpose is to try to prove that two
expressions that look different are actually equal. Simplify and
especially FullSimplify is much better at doing that than at reducing
an expression to a desired form.

Andrzej Kozlowski

```

• Prev by Date: Re: The audience for Mathematica
• Next by Date: Re: The audience for Mathematica
• Previous by thread: Re: TransformationFunctions
• Next by thread: Show doesn't work inside Do loop ?