MathGroup Archive 1998

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

Search the Archive

Re: Efficient use of coefficient--Efficient simplification


  • To: mathgroup@smc.vnet.net
  • Subject: [mg11192] Re: [mg11183] Efficient use of coefficient--Efficient simplification
  • From: Daniel Lichtblau <danl@wolfram.com>
  • Date: Mon, 2 Mar 1998 23:10:48 -0500
  • References: <199802250831.DAA03299@smc.vnet.net.>

Joel Cannon wrote:
> 
> I wish to simplify expressions such as the following which possess terms
> involving ket[n+i]. They involve a variable range of indices i.
> 
> Out[164]=
> -(Sqrt[n] (Sqrt[-1 + n] ket[-2 + n] - Sqrt[n] ket[n])) +
> 
>   Sqrt[1 + n] (Sqrt[1 + n] ket[n] - Sqrt[2 + n] ket[2 + n])
> 
> I can collect the various terms in ket[n+i] with the following
> operation:
> 
> In[176]:=
> Out[164]//Expand// Table[ket[n+i] Coefficient[#,ket[n+i]  ],{i,-2,2}]&
> //Plus @@ #&
> 
> Out[176]=
> -(Sqrt[-1 + n] Sqrt[n] ket[-2 + n]) + (1 + 2 n) ket[n] -
> 
>   Sqrt[1 + n] Sqrt[2 + n] ket[2 + n]
> 
> My problem is this, Since I do no know what the range of ket[n+i] will
> be, I would like to write a general expression that will find what
> ket[n+i] are present and collect the coefficients of each of these. The
> inelegant wat is to run over a range of i's that will surely bracket
> any ket[n+i] that I will possibly encounter, but that is distasteful.
> I am using version 2.2  but will probably switch to 3.0 soon.
> 
> Any other suggestions to that way I have done things are welcomed.  If
> possible, please copy me on email since I might otherwise miss posts to
> the newsgroup.
> 
> cannon@alpha.centenary.edu
> 
> Thanks very much,
> 
> ------------------------------------------------------------------------------
> Joel W. Cannon                 |   (318)869-5160          Dept. of
> Physics               |   (318)869-5026  FAX    Centenary College of
> Louisiana |       P. O. Box 41188                      |
> Shreveport, LA 71134-1188      |


I version 3 you can use Collect for the job. For example, to do just the
collection,

In[5]:= ee =  -(Sqrt[n] (Sqrt[-1 + n] ket[-2 + n] -
	Sqrt[n] ket[n])) +  Sqrt[1 + n] (Sqrt[1 + n] ket[n] -
	Sqrt[2 + n] ket[2 + n]);

In[6]:= Collect[ee, ket[_]]
Out[6]= -(Sqrt[-1 + n] Sqrt[n] ket[-2 + n]) + (1 + 2 n) ket[n] -
>    Sqrt[1 + n] Sqrt[2 + n] ket[2 + n]

If you really need to know all the ket[...] factors that appear, one
approach is to use the three argument form of Collect. The third arg
specifies a function to apply to each collected coefficient. The
function we use is that which returns 1. Thus we get either a single
ket[] term, of sum of such, or a sum of such plus 1 if there is a
nontrivial constant term in the expression.

In[7]:=  Collect[ee, ket[_], 1&]
Out[7]= ket[-2 + n] + ket[n] + ket[2 + n]

In[8]:= Collect[3*ket[4+n] + x*ket[4+n], ket[_], 1&] Out[8]= ket[4 + n]

In[9]:=  Collect[3*ket[4+n] + x*ket[4+n] + x*t, ket[_], 1&] Out[9]= 1 +
ket[4 + n]

I demonstrate this mostly as a cheap excuse to show off the three
argument Collect. A cleaner method, one that works in version 2 as
well, would be

In[12]:= Union[Cases[ee, _.*b:ket[_]->b, Infinity]] Out[12]= {ket[-2 +
n], ket[n], ket[2 + n]}


Daniel Lichtblau
Wolfram Research



  • Prev by Date: Previous variable affects "/."
  • Next by Date: replacement rules in packages
  • Prev by thread: RE: Efficient use of coefficient--Efficient simplification
  • Next by thread: Re: Efficient use of coefficient--Efficient simplification