Message Boards Message Boards


Replace terms in an expression by a single variable?

Posted 3 months ago
3 Replies
4 Total Likes


I have a symbolic expression, say:

exp1 == (r P^(-1))/Log[S/N0]

Since I know that Log[S/N0]/r is equal to something special (namely T), I want to replace that terms by T:

T == Log[S/N0]/r

and get something like exp2 from exp1 using that special term. I.e.:

exp2 = 1/(P * T) 

I tried several things like:

Simplify[(r P^(-1 + a))/Log[S/N0], T == Log[S/N0]/r]

But I am not getting any simplification of exp1 to get exp2 knowing that 1/T is r/Log[S/N0].

Does anybody know how to do this kind of thing in Mathematica? How to simplify exp1 to get exp2?

3 Replies


Normally for simple expressions you can use Replace[] (also typed /.):

For example:

In[13]:= eqn = exp1 == (r P^(-1))/Log[S/N0]

Out[13]= exp1 == r/(P Log[S/N0])

In[14]:= eqn /. S/N0 -> xx

Out[14]= exp1 == r/(P Log[xx])

HOWEVER, this relies on the pattern matching features of Mathematica and can fail on expressions that simplify in a variety of ways (such as your case with Log[S/N0]/r -> T)

This fails because

In[15]:= FullForm[eqn]


EDITED: Note that your expression has a different form than the original so you can't do a pattern match without getting a bit more clever and account for the simplified terms in the multiplication. Another approach is to use Reduce[]. Reduce is a bit more complicated to use but it does respect the mathematical relationships.

In your case:

In[18]:= Reduce[{exp1 == (r P^(-1))/Log[S/N0], T == Log[S/N0]/r}, {T}]

Out[18]= P Log[S/N0] != 0 && exp1 == r/(P Log[S/N0]) && exp1 != 0 && 
 T == 1/(exp1 P)

Which gives you the answer with additional information that the denominator can't go to zero. The downside of this is that you then need to parse the result to pick out what you want.



Thank you! This is exactly what I was looking for.

As to getting pattern matching to work, you can use the fullform to give you insight as to a pattern to match:

In[22]:= eqn /. Times[r, Power[Log[Times[Power[N0, -1], S]], -1]] -> T

Out[22]= exp1 == T/P


Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
or Discard

Group Abstract Group Abstract