Saying something good, for a change
- To: mathgroup at yoda.ncsa.uiuc.edu
- Subject: Saying something good, for a change
- From: uunet!sarastro.tamu.edu!fulling (Stephen A. Fulling)
- Date: Tue, 12 Jun 90 16:52:37 CDT
Since recent messages of mine were critical of Mathematica and its documentation, let me share my delight over an instance where Mathematica's special features enabled a hard calculation to be done easily. I need to integrate from 0 to Infinity various linear combinations of functions of the form r^a Exp[-b r^2]. (*) All these integrals can be done in closed form, and for a variety of reasons I don't want to use Mathematica's built-in integrator; I must use the formula. I programmed it: gaussParam[alpha_, gamma_] := Block[{beta,answer}, beta = (alpha+1)/2; answer = (1/2) gamma^(-beta) Gamma[beta]; Return[answer] ] The catch is that the integrand emerges as the output of another Mathematica function, and I was wondering if I'd need to write a C program to parse that expression to extract all the a's and b's, or ... That same weekend I was reading through Roman Maeder's book, Programming in Mathematica. I hit page 70: "Often we can avoid programming a loop altogether by applying functions to lists or other expressions. Section[s ...] about mapping of functions over expressions go the heart of Mathematica's programming language. Understanding this material will allow you to write concise programs in the style that the authors of Mathematica think is best to use." Blinding illumination! All I need to do is to write a function defined only on expressions that match the pattern (*), then Map[] it over sums! gauss[ c_. Exp[-gamma_. r^2] r^alpha_. ] := (c/2) gamma^(-(alpha+1)/2) Gamma[(alpha+1)/2] /; FreeQ[c, r] gauss[ c_. Exp[-gamma_. r^2] ] := (c/2) gamma^(-1/2) Gamma[1/2] /; FreeQ[c, r] gauss[expr_] := Map[gauss, expr] That doesn't mean that all is sweetness and light, however. I did run into two bugs-or-features in this little project, which will be reported in later messages. Steve Fulling Texas A&M Math