Hi,
can somebody explain, why
Simplify[x^2/y^2,ComplexityFunction->LeafCount]
does not simplify to (x/y)^2, although the LeafCount is:
LeafCount[Hold[x^2/y^2]] gives 10
and
LeafCount[Hold[(x/y)^2]] gives 8
Daniel
Hold[x^2/y^2] // FullForm ==> Hold[Times[Power[x,2],Power[Power[y,
2],-1]]]
x^2/y^2 // FullForm ==> Times[Power[x,2],Power[y,-2]]
x^2/y^2 // LeafCount ==> 7
let
expr = 2^(z + 1)*3^z
You can easily see that this can be re-written as 2*6^z and that:
LeafCount[Hold[2*6^z]]
6
while
LeafCount[expr]
9
so 2*6^z has a much smaller LeafCount than expr, but you can never
Simplify expr to 2*6^z because the Evaluator will always rewrite it
again as expr. This has a rather unpleasant consequence:
FullSimplify[2*6^z - 6^z]
2^(z + 1)*3^z - 6^z
Simplify cannot see that the simplest answer if 6^z (no matter what
ComplexityFunction you use) because immediately on evaluation 2*6^z is
converted to expr above and then it is too late; Simplify can't see
that the first term is simply twice the second term. Yet Simplify has
no problem noticing that:
Simplify[2*6^z - 6^z == 6^z]
True
The reason for the problem is the following "canonical form":
expressions like (a^k1*b^k2*c^k3...)^z *(a*l1*b^l2*...) where a, b, c
are positive are re-written as a^(k1+l1)*b^(k2+l2)*....
For example
(2^u*5^v)*(2*3^w*5^z)
2^(u + 1) 3^w 5^(v + z)
(actually the rule used is note general, but I just want to give an
example of a "canonical form" that causes trouble).
Since these reductions are made by the Evaluator, Simplify has no
effect on them. But because of them we get the following inconsistent
behaviour:
2*5^z - 5^z
5^z
but
2*6^z - 6^z
2^(z + 1)*3^z - 6^z
This, of course, cannot be changed by applying Simplify with any
ComplexityFunction because canonical forms can't be changed by
Simplify (unless you apply Hold).
Andrzej
On 15 Apr 2009, at 20:52, dh wrote:
> Hi Andrzej,
> thanks for your helpfull explanations.
> Let me just mention that there is an additional reason, internally
> the expression x^2/y^2 is rewritten and has the the same leaf count
> as (x/y)^2. Consider:
> FullForm[Hold[x^2/y^2]]
> FullForm[x^2/y^2]
> Daniel
>
>
> Andrzej Kozlowski wrote:
>> Actually, this point has been explained many times (by me ;-))
>> (I like to think of Mathematica's evaluation process in terms of
>> something called "The Evaluator", which I think I first found in
>> David Wagner's book "Power Programming with Mathematica". I think
>> it is only an abstraction, along with "the Parser", "the
>> Typesetter" etc, but a convenient one when one is thinking about
>> the evaluation process. )
>> The "Evaluator" always evaluates
>> (x/y)^2
>> to
>> x^2/y^2
>> This happens before Simplify takes any effect. Even if Simplify
>> converted x^2/y^2 to (x/y)^2 the Evaluator would kick in and again
>> convert it back to x^2/y^2. Since the Evaluator always overrides
>> Simplify there is no way to get (x/y)^2 as the output without using
>> Hold.
>> Perhaps you are asking why Mathematica (or the "Evaluator")
>> automatically converts (x/y)^2 to x^2/y^2. It's because of
>> something called "canonical forms" or "standard forms". Basically,
>> in order to optimize performance in computer algebra systems one
>> want to reduce to 0 as quickly as possible as many expressions that
>> are actually equal. The earlier you do this the better the
>> performance. If you allow expressions to contain a large number of
>> subexpressions that are actually 0 until you apply Simplify, it may
>> very seriously impair performance. "Canonical forms" (or "normal
>> forms", there is a slight difference between them but I shall
>> ignore it) are certain unique forms to which various expressions
>> are reduced automatically by the Evaluator (before applying
>> Simplify). This has the effect causing cancellations to occur
>> early. Also, these "canonical forms" have to be independent of any
>> particular ComplexityFunction used, hence the reduction has to be
>> performed outside Simplify. The advantage of using canonical forms
>> independent of ComplexityFunction is that they often enable
>> Mathematica to identify two expressions as equal even if Simplify
>> can't fine a sequence of Complexity reducing transformations that
>> will convert one expression into the other.
>> Not surprisingly, using "canonical forms" can sometimes produce
>> undesirable side-effects and this is one of them (a rather minor
>> one, worse ones do occur).
>> Andrzej Kozlowski
>> Andrzej Kozlowski
>> On 15 Apr 2009, at 18:02, dh wrote:
>>>
>>>
>>> Hi,
>>>
>>> can somebody explain, why
>>>
>>> Simplify[x^2/y^2,ComplexityFunction->LeafCount]
>>>
>>> does not simplify to (x/y)^2, although the LeafCount is:
>>>
>>> LeafCount[Hold[x^2/y^2]] gives 10
>>>
>>> and
>>>
>>> LeafCount[Hold[(x/y)^2]] gives 8
>>>
>>>
>>>
>>> Daniel
>>>
>>>
>>>
>
>
> --
>
> Daniel Huber
> Metrohm Ltd.
> Oberdorfstr. 68
> CH-9100 Herisau
> Tel. +41 71 353 8585, Fax +41 71 353 8907
> E-Mail:<mailto:d...@metrohm.com>
> Internet:<http://www.metrohm.com>
>
> The reason for the problem is the following "canonical form":
> expressions like (a^k1*b^k2*c^k3...)^z *(a*l1*b^l2*...) where a, b,
> c are positive are re-written as a^(k1+l1)*b^(k2+l2)*....
>
> For example
>
> (2^u*5^v)*(2*3^w*5^z)
> 2^(u + 1) 3^w 5^(v + z)
I did not state this "rule" correctly. The rule is that in products of
powers of integers are factored, with the factors being powers of
primes.
Andrzej
Hello Daniel,
There are two reasons. First, try this:
In[1]:= FullForm[Hold[x^2/y^2]]
Out[1]//FullForm= Hold[Times[Power[x,2],Power[Power[y,2],-1]]]
In[2]:= FullForm[x^2/y^2]
Out[2]//FullForm= Times[Power[x,2],Power[y,-2]]
Second, (x/y)^2 autoevaluates to x^2/y^2, so no function can return (x/
y)^2 without some kind of Hold-wrapper.
The LeafCounts of the different forms are
In[9]:= LeafCount[Unevaluated[x^2/y^2]]
Out[9]= 9
In[10]:= LeafCount[x^2/y^2]
Out[10]= 7
In[11]:= LeafCount[Unevaluated[(x/y)^2]]
Out[11]= 7
> FullSimplify[2*6^z - 6^z]
> 2^(z + 1)*3^z - 6^z
in my version:
Simplify[2*6^z - 6^z]
6^z
Daniel
>>> On 15 Apr 2009, at 18:02, dh wrote:
>>>>
>>>>
>>>> Hi,
>>>>
>>>> can somebody explain, why
>>>>
>>>> Simplify[x^2/y^2,ComplexityFunction->LeafCount]
>>>>
>>>> does not simplify to (x/y)^2, although the LeafCount is:
>>>>
>>>> LeafCount[Hold[x^2/y^2]] gives 10
>>>>
>>>> and
>>>>
>>>> LeafCount[Hold[(x/y)^2]] gives 8
>>>>
>>>>
>>>>
Cheers,
Sjoerd
I have version 7.01 and it does behave this way. The example I used I
took from my past posts to the MathGroup: I wrote about it at least
two times or more, before Mathematica 6, I think. But Simplify has
been improved and is much cleverer now.(I should have expected that
and checked). What has happened is that it now "knows" about this
problem and treats it as a special case. Thus, although the difference
between
2*6^z - 6^z
2^(z + 1) 3^z - 6^z
and
2*5^z - 5^z
5^z
still remains, when you apply Simplify you get the same answer.
However, although the "unpleasant consequence" that I wrote about has
been fixed, my main point remains unchanged. You can never Simplify
2^(z + 1)*3^z
to
2*6^z
even though
LeafCount[Hold[2*6^z]]
6
while
LeafCount[2^(z + 1)*3^z]
9
Andrzej Kozlowski
>>>> On 15 Apr 2009, at 18:02, dh wrote:
>>>>>
>>>>>
>>>>> Hi,
>>>>>
>>>>> can somebody explain, why
>>>>>
>>>>> Simplify[x^2/y^2,ComplexityFunction->LeafCount]
>>>>>
>>>>> does not simplify to (x/y)^2, although the LeafCount is:
>>>>>
>>>>> LeafCount[Hold[x^2/y^2]] gives 10
>>>>>
>>>>> and
>>>>>
>>>>> LeafCount[Hold[(x/y)^2]] gives 8
>>>>>
>>>>>
>>>>>
>>>>> Daniel
>>>>>
>>>>>
>>>>>
>>>
>>>
One reason may be the following:
In[1] = Hold[x^2/y^2]//FullForm
Out[1] = Hold[Times[Power[x, 2], Power[Power[y, 2], -1]]]
In[2] = x^2/y^2 // FullForm
Out[2] = Times[Power[x, 2], Power[y, -2]]
The LeafCount of the latter is then 7, not 10.
However, there seems to be more to it, since it looks like
there is a built-in rule, so that even if you type (x/y)^2, it will
be immediately rewritten as x^2/y^2 (or, (a*b)^c->a^c*b^c, etc):
In[3] = (x/y)^2
Out[3] = x^2/y^2
In[4] = Block[{Power}, Hold[Evaluate[((x/y)^2)]]]
Out[4] = Hold[(x/y)^2]
This probably means that even if LeafCount would indeed be larger, there
would be no chance to get the final result unless you either somehow
block this specific rule, or get inside Simplify and intercept some
intermediate result.
Regards,
Leonid
The "Evaluator" always evaluates
(x/y)^2
to
x^2/y^2
Andrzej Kozlowski
Hi Sjoerd,
why do you think x^2/y^2 is not equivalent to (x/y)^2 ????
Daniel
Cheers -- Sjoerd
On Apr 16, 10:16 am, "Sjoerd C. de Vries" <sjoerd.c.devr...@gmail.com>
wrote: