Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6207841
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T05:42:13+00:00 2026-05-24T05:42:13+00:00

Maybe someone can explain to me why Replace gives a different answer than ReplaceAll

  • 0

Maybe someone can explain to me why Replace gives a different answer than ReplaceAll, even though I am using one rule, and, I think, I have one expression.

According to the documentation:

ReplaceAll looks at each part of expr, tries all the rules on it, and then goes on to 
the next part of expr. The first rule that applies to a particular part is used; no 
further rules are tried on that part, or on any of its subparts

and for Replace

A list of rules can be given. The rules are tried in order. The result of the first 
one that applies is returned. If none of the rules apply, the original expr is 
returned.

I have this expression:

z/(0.5 -1.4 z+z^2)

and this one rule

z -> Exp[I*w]

The following input

Clear[z]
hz = z/(0.5 - 1.4 z + z^2);
Replace[hz, z -> Exp[I*w]]
ReplaceAll[hz, z -> Exp[I*w]]

yields this result:

z/(0.5 -1.4 z+z^2)                    (*from Replace*)
E^(I w)/(0.5 -1.4 E^(I w)+E^(2 I w))  (*from ReplaceAll*)

So, ReplaceAll did the job I expected, but not Replace. The only thing I think I am missing here, is what makes Parts of an expression. So maybe hz is not ‘one’ expression? Ok, fine, but it should then have been able to apply the rule at least to one part of it. But it did not do any replacement anywhere

FullForm[hz]
Times[z,Power[Plus[0.5`,Times[-1.4`,z],Power[z,2]],-1]]

Isn’t hz one expression? If not, how many parts does it have? Is it the number of leaves at the end of the graph generated by TreeForm[hz]? If so, then I see 3 ‘z’ in there, and Replace[] should then have replaced one of them

enter image description here

I always used /. without thinking about it, which is the same as ReplaceAll, but now I used Replace[] thinking is was the same as /., I found this and noticed the different functions.
(so many functions to learn, so little time 🙂

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T05:42:14+00:00Added an answer on May 24, 2026 at 5:42 am

    In fact, Replace and ReplaceAll are not the same. You can think of Replace as a more precise version, since with Replace you can specify the levels in expression on which you want the replacements to happen. The difference here is somewhat similar to that between Map and MapAll, except the subtlety that ReplaceAll does not work depth-first (see below). Another difference, also rather subtle, is that Replace takes the Heads option, while ReplaceAll does not, which makes Replace yet more precise than ReplaceAll.

    By default, Replace works only at level 0, which is, entire expression. Your z is deeper however:

    In[220]:= Clear[z]
    hz = z/(0.5 - 1.4 z + z^2);
    
    
    Position[hz, z]
    
    Out[222]= {{1}, {2, 1, 2, 2}, {2, 1, 3, 1}}
    

    If you use the level specification for Replace, you can achieve an effect similar to, but not always the same as that of ReplaceAll:

    In[223]:= Replace[hz,z->Exp[I*w],{0,Infinity}]
    
    Out[223]= E^(I w)/(0.5\[VeryThinSpace]-1.4 E^(I w)+E^(2 I w))
    

    The difference between Replace with lev.spec {0,Infinity} and ReplaceAll is that the former acts depth-first, sub-expressions before expressions, while the latter works from larger expressions to their parts. It is discussed in more detail e.g. here. One example where this difference was used to one’s advantage can be found in this post.

    Coming back to default behavior of Replace, which operates on entire expression: it is very useful when you want to transform only the entire expression, but none of its parts (which may accidentally match the pattern in your rule). One example of such application of Replace is here.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This is very strange, maybe someone can explain what's happening, or this is a
I'm not understanding this behavior. Maybe someone can explain to me why my current
This makes no sense to me. Maybe someone here can explain why this happens.
Can someone explain the following behaviour (I'm using Visual Studio 2010). header: #pragma once
Maybe someone can explain this behavior. Sometimes I will do an SVN update, and
I have the following problem, maybe someone can help me (or explain, where my
Maybe this is just obvious to everyone but can someone explain where XOR (or
Maybe I'm a little behind the eight ball. Hopefully someone can explain this. I
Can someone explain the difference between using define('SOMETHING', true); and $SOMETHING = true; And
I have stumbled upon a riddle which I can't explain, maybe someone here will

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.