Re: small question
- To: mathgroup at smc.vnet.net
- Subject: [mg74880] Re: small question
- From: Jean-Marc Gulliet <jeanmarc.gulliet at gmail.com>
- Date: Tue, 10 Apr 2007 05:11:11 -0400 (EDT)
- Organization: The Open University, Milton Keynes, UK
- References: <evd3or$5fo$1@smc.vnet.net>
KFUPM wrote:
> Dear Group
>
> I have two variables multiplied by each other
>
> N1 x (N1 times x) which is a part of an equation.
>
> What i want to do is to replace this multiplication by Nx1 (one
> variable) and i want to do this automatically using Mathematica,
> because i have a huge number of such terms, for example, N2 y, N3 z,
> N4 x, N5 y.... etc.
>
> Thereofore, i need to get this transformation:
>
> N1 x ------> Nx1
> N2 y ------> Ny2
> N3 z ------> Nz3
> and so on.
>
> Please notice that the letter N doesn't change only numbers(1,2,...)
> and (x,y,z....) do.
>
>
> Any help in this regard is highly appreciated.
You could use a transformation rule. In the following expression, you
just have to provide the list of the variables not in the format N1, N2,
etc.
expr = N1*x + N2*x + N3*x + N1*y + N2*y + N3*y +
N1*z + N2*z + N3*z;
vars = {x, y, z};
expr /. (n_)*(x_ /; MemberQ[vars, x]) :>
ToExpression[StringInsert[ToString[n], ToString[x], 2]]
Nx1 + Nx2 + Nx3 + Ny1 + Ny2 + Ny3 + Nz1 + Nz2 + Nz3
Regards,
Jean-Marc