Skip to main content
added 11 characters in body
Source Link
Mr.Wizard
  • 275.2k
  • 34
  • 606
  • 1.5k

Combining the infix forms of Mathematica's functions with the prefix or postfix forms of other functions can get tricky. One must pay careful attention of the rules of precedence for each operator form. Here are two tips on what to do when a complex combination of operators does work as expected.

  • Retreat. Enter the expression or part of the expression in normal form. In your case, I would would retreat to

    Expand[(x^2 + y + Log[z])^2 /. y -> x/2] /. z -> E^x

     Expand[(x^2 + y + Log[z])^2 /. y -> x/2] /. z -> E^x 

Now there is little doubt about what the order of evaluation will be.

  • Wrap the expression with Hold and InputForm and evaluate that, which will show you how Mathematica brackets the expression. Applying this to your expression:

    InputForm[Hold[(x^2 + y + Log[z])^2 /. y -> x/2 // Expand /. z -> E^x]]

     InputForm[Hold[(x^2 + y + Log[z])^2 /. y -> x/2 // Expand /. z -> E^x]] 

Hold[(Expand /. z -> E^x)[(x^2 + y + Log[z])^2 /. y -> x/2]]

Hold[(Expand /. z -> E^x)[(x^2 + y + Log[z])^2 /. y -> x/2]] 

Now you can clearly see what is going wrong with your expression.

Combining the infix forms of Mathematica's functions with the prefix or postfix forms of other functions can get tricky. One must pay careful attention of the rules of precedence for each operator form. Here are two tips on what to do when a complex combination of operators does work as expected.

  • Retreat. Enter the expression or part of the expression in normal form. In your case, I would would retreat to

    Expand[(x^2 + y + Log[z])^2 /. y -> x/2] /. z -> E^x

Now there is little doubt about what the order of evaluation will be.

  • Wrap the expression with Hold and InputForm and evaluate that, which will show you how Mathematica brackets the expression. Applying this to your expression:

    InputForm[Hold[(x^2 + y + Log[z])^2 /. y -> x/2 // Expand /. z -> E^x]]

Hold[(Expand /. z -> E^x)[(x^2 + y + Log[z])^2 /. y -> x/2]]

Now you can clearly see what is going wrong with your expression.

Combining the infix forms of Mathematica's functions with the prefix or postfix forms of other functions can get tricky. One must pay careful attention of the rules of precedence for each operator form. Here are two tips on what to do when a complex combination of operators does work as expected.

  • Retreat. Enter the expression or part of the expression in normal form. In your case, I would would retreat to

     Expand[(x^2 + y + Log[z])^2 /. y -> x/2] /. z -> E^x 

Now there is little doubt about what the order of evaluation will be.

  • Wrap the expression with Hold and InputForm and evaluate that, which will show you how Mathematica brackets the expression. Applying this to your expression:

     InputForm[Hold[(x^2 + y + Log[z])^2 /. y -> x/2 // Expand /. z -> E^x]] 
Hold[(Expand /. z -> E^x)[(x^2 + y + Log[z])^2 /. y -> x/2]] 

Now you can clearly see what is going wrong with your expression.

Source Link
m_goldberg
  • 108.6k
  • 16
  • 107
  • 263

Combining the infix forms of Mathematica's functions with the prefix or postfix forms of other functions can get tricky. One must pay careful attention of the rules of precedence for each operator form. Here are two tips on what to do when a complex combination of operators does work as expected.

  • Retreat. Enter the expression or part of the expression in normal form. In your case, I would would retreat to

    Expand[(x^2 + y + Log[z])^2 /. y -> x/2] /. z -> E^x

Now there is little doubt about what the order of evaluation will be.

  • Wrap the expression with Hold and InputForm and evaluate that, which will show you how Mathematica brackets the expression. Applying this to your expression:

    InputForm[Hold[(x^2 + y + Log[z])^2 /. y -> x/2 // Expand /. z -> E^x]]

Hold[(Expand /. z -> E^x)[(x^2 + y + Log[z])^2 /. y -> x/2]]

Now you can clearly see what is going wrong with your expression.