Re: = versus := and trying to speed up calculations
- To: mathgroup at smc.vnet.net
- Subject: [mg35439] Re: = versus := and trying to speed up calculations
- From: Ignacio Rodriguez <ignacio at sgirmn.pluri.ucm.es>
- Date: Fri, 12 Jul 2002 04:29:04 -0400 (EDT)
- References: <agjjlu$25v$1@smc.vnet.net>
- Sender: owner-wri-mathgroup at wolfram.com
The main difference between Set (=) and SetDelayed (:=) has to do with evaluation of its rhs. A good way to see the difference is with the Random[] function. Type a=Random[] and evaluate a several times (this is, type a and Ctrl+Return several times). Now type a:=Random[] and do the same. See the difference? In your case, I do not think that using Set or SetDelayed is important, as the rhs of your expressions do not seem to me to need evaluation. However, you can try the following: Bracket[a_.*e[i_],b_.*e[j_]]:=(Bracket[a*e[i],b*e[j]]=Which[...]); By doing so, you will construct a table of values for the Bracket function for definite values of its arguments. This can be seen using DownValues[Bracket]. Mathematica will give precedence to the more specific definitions. So you will not need to evaluate the same bracket several times. However, you will need more memory to store those results. As a last comment, I think your main definition for Bracket needs parentheses in the i-j case, so that it is bilinear. Geoff Tims wrote: >I have the following function and it is called many many times in a program >I have written. This is a bracket operator for a certain type of Lie >Algebra I am looking at and it must be called over and over to test whether >or not the Jacobi is actually 0 as it should be. With very low numbers for >n, my program runs in a second or less. If n is around 20+ or if the >coefficients are large, the program takes nearer a minute or two. That's >not a long time, but I have a feeling that it's having to calculate the same >Bracket many times in the program and I'm hoping to get rid of that somehow. >However, I don't understand the differences between := and = enough. I've >read the help files, but I don't understand the subtle differences such as I >can call > >Bracket[stuff, something]:= somethingelse > >more than once, but it doesn't seem as if I can use = more than once. > >Bracket[a_.*e[i_], b_.*e[j_]] := > Which[ > i + j > n, 0, > i == j, 0, > i == 1, a*b*e[j + 1], > i == 2 && j == 3, 14a*b*e[7], > i == 3 && j == 4, 0, > i < j, -a*b*Bracket[e[i + 1], e[j - 1]] + > Bracket[e[1], Bracket[e[i], e[j - 1]]], > i > j, -a*b*Bracket[e[j], e[i]] > ]; >(* bilinear function *) >Bracket[mul_, expr_Plus] := Map[Bracket[mul, #] &, expr]; >Bracket[expr_Plus, mul_] := Map[Bracket[#, mul] &, expr]; >Bracket[0, x_] := 0 >Bracket[x_, 0] := 0 > > >Any help would be much appreciated. >Geoff Tims > > >