MathGroup Archive 2006

[Date Index] [Thread Index] [Author Index]

Search the Archive

Re: Is mathematica able to transform formula

Hi Pujo

Mathematica will need some help from you.  One of the issues is that
Mathematica's expressions are one-way rewrite rules, they state that
the thing to the left of the = can be replaced wherever it occurs by
the thing to the right.  So you could write:

In[1]:= c=a b
Out[1]:= a b

(* by default M uses spaces to indicate multiplication, on input or
output *)

In[2]:= f = c+5
Out[2]:= 5+a b

(* M outputs expressions in canonical order, canonical according to M
that is *)

To do the sort of thing which I think you might be trying to achieve,
you should investigate Patterns and ReplaceAll and associated
functions. There are lot of good examples in the on-line Help.  For
example, one way to achieve what you want might be:

In[6]:= f = a b + 5 /. (a b) -> c
Out[6]:= 5+2c

but this isn't a lot of reward for all your typing.

To do what you apparently want to do, and make a replacement which runs
from right to left you could do something VERY DANGEROUS INDEED.  Try:

In[20]:= Unprotect[Times]

In[21]:= Times[a,b] ^= c

In[22]:= Protect[Times]

and now when you type

In[23]:= f = a b + 5

you get

Out[23]:= 5 + c

Have a look in the documentation for Protect / Unprotect, UpValues and
DownValues, and read all the stuff which tells you why it is a VERY BAD
IDEA to such basic functionality of Mathematica.  Most of us only do
it, oh, just about every day :-).  A generally safer approach is to
define Unprotected functions of your own which do what you want and to
use them instead of altered core functions.

Good luck.

Mark Westwood

ajikoe at wrote:
> Hello,
> I have a question:
> if I have a symbolic expression:
> f  = a * b + 5
> c = a * b
> Can Mathematica builds f = c + 5 for me ?
> Sincerely Yours,
> pujo

  • Prev by Date: Re: NIntegrate that upper limit is infinite
  • Next by Date: A serious error?
  • Previous by thread: RE: Is mathematica able to transform formula
  • Next by thread: Ask Mathematica to sub for a long expression