0
$\begingroup$

I have tried x /. x->a using the Simplify with assumption x==a, using the ComplexityFunction in the Doc., but my code does not show the expected output. Which part of my code should be corrected? (Although x==a did not work, but interestingly x==1 did work)

Simplify[expr,ComplexityFunction->f] applies f to each intermediate expression generated by Simplify, treating the one which yields the smallest numerical value as simplest.

f[e_] := Module[{c},c=100Count[e,x,{0, Infinity}];Print[{ e,LeafCount[e],c}]]
Simplify[x,x==a,ComplexityFunction->f] (* not a ? *)

※Although x==a did not work, but interestingly x==1 did work

f[e_] := Module[{c},c=100Count[e,x,{0, Infinity}];Print[{ e,LeafCount[e],c}]]
Simplify[x,x==1,ComplexityFunction->f](* 1 *)
$\endgroup$
3
  • 1
    $\begingroup$ Just a side note, this is one of the examples of letter-makes-difference, namely compare the results of Simplify[x, x == y] and Simplify[x, x == a]. $\endgroup$ Commented Aug 4 at 11:25
  • $\begingroup$ Your question ignores the error messages: Bad mistake. Test your complexity function and narrow down the source of the error, and you will have made a big step toward working code and understanding Mma. Perhaps trial this: FullForm[f[x]]. (FullForm[] shows exactly what the return value is. It's not typeset all pretty and can be long; but it's not as confusing as the default output style.) $\endgroup$ Commented Aug 4 at 13:51
  • $\begingroup$ @Domain Thank you for your comment and references;(It seems that WL want to replace a letter with the next letter in alphabetical order. So, the letter makes difference; mathematica.stackexchange.com/questions/17926/…) And I changed a to y, then my code works;f[e_] := Module[{c},c=100Count[e,x,{0, Infinity}];Print[{ e,LeafCount[e],c}]] Simplify[x,x==y,ComplexityFunction->f] (* y *) $\endgroup$ Commented Aug 5 at 23:41

1 Answer 1

1
$\begingroup$

The last statement in your complexity function is a print statement. Therefore, this function does not return a reasonable answer.

According to the help, Simplify takes 2 arguments, not 3. I wondering that this does not produce an error message.

With a corrected complexity function, the following works as expected. E.g. without the complexity function:

Simplify[{x + y + x y}]

{x + y + x y}

and with the complexity function:

f[e_] := Module[{c}, c = 100 Count[e, x, {0, Infinity}]];
Simplify[{x + y + x y}, ComplexityFunction -> f] 

{y + x (1 + y)}
$\endgroup$
1
  • $\begingroup$ A couple of points: (1) Simplify[] takes a number of options according to the help, so more than two arguments is accepted. See also SyntaxInformation@Simplify. (2) My guess is that a return value of LeafCount[e] + c is probably what the OP was looking for. $\endgroup$ Commented Aug 4 at 13:58

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.