Mathematica 9 is now available
Services & Resources / Wolfram Forums
-----
 /
MathGroup Archive
2000
*January
*February
*March
*April
*May
*June
*July
*August
*September
*October
*November
*December
*Archive Index
*Ask about this page
*Print this page
*Give us feedback
*Sign up for the Wolfram Insider

MathGroup Archive 2000

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

Search the Archive

Re: Re: How can I teach Mathematica new functions

  • To: mathgroup at smc.vnet.net
  • Subject: [mg21749] Re: [mg21724] Re: How can I teach Mathematica new functions
  • From: hwolf at debis.com
  • Date: Thu, 27 Jan 2000 22:56:31 -0500 (EST)
  • Organization: debis Systemhaus
  • References: <86e8lb$fbo@smc.vnet.net> <200001260845.DAA02267@smc.vnet.net>
  • Sender: owner-wri-mathgroup at wolfram.com

Jens-Peer Kuska schrieb:
> 
> 
> you are wrong because you where too lazy. First of all you should *not*
> define an explicit sbj[_,_] when you don't like to use the built-in
> functions. You must define rules for
> 
> Derivative[0,1][sbj][n_,z_]:= ..
> 
> and rules for simplifications of spherical bessel functions, then you
> have Mathematica taught a new function and Mathematica will work with it
> like a built-in one.
> 
> You have only created a short reference to the built-in function and it's no
> wonder that you only get built-in functions out.
> 
> Regards
>   Jens
> 
> gargamel wrote:
> >
> > Hello all,
> >
> > This is a real example, but my qestion is general:
> >
> > Suppose I define a new function, namely a spherical bessel function:
> >
> > sbj[n_,z_]=Sqrt[Pi/(2*z)]*BesselJ[n+1/2,z]
> >
> > Now I can easily do all kind of manipulations on my function.
> > For example, by typing:
> >
> > D[sbj[n,x],x]
> >
> > I get a fancy expression in terms of the BesselJ function (which I won't
> > copy here).
> > But I would want a simpler expression in terms of my function sbj
> > (which would be:
> >
> > -sbj(n,x)/(2x)+1/2*(sbj(n+1,x)-sbj(n-1,x))
> >
> > if I'm not mistaking).
> >
> > So basically what I want to do is to teach Mathematic the new function and
> > ask it
> > to use it to "simplify" results. It would also be nice if Mathematica's
> > functions Simplify
> > and FullSimplify would be able to incorporate new functions that I defines.
> >
> > I didn't find any way to do this. It seems to me that Mathematica knows only
> > what
> > It was taught at the "factory". Sure, you can define new function and even
> > due some
> > manipulations on them but they never become a real part of Mathematica's
> > knowledge
> > (and Mathematica will never give you your function as an answer to a
> > problem).
> >
> > Am I wrong?


Well, Jens,

this of course is the right way to define a functional calculus for the
spherical Bessel functions, e.g. when you want to write a Package which
knows everything about them. But as you stated so well, this is some
work, although Bob Hanlon has shown us a fine way how to get at all
those definitions needed. 

However, sometimes, mostly say, I *want* to be lazy and do no more work
than is neccessary. So I'd like to introduce the explicit rules, do the
calculation and then of course reconvert the result. This indeed works,
if you apply the definition and the reconversion in *different*
contexts. See:


In[1]:= j; r; 

This is just to define global symbols such that no local ones were
created -- it's not essential, just for a more pleasing result.

In[2]:= 
Begin["`sbj`"]; 
sbj[n_, z_] := Sqrt[Pi/(2*z)]*BesselJ[n + 1/2, z]; 
result = D[sbj[j, r], r]; 
End[]; 
Expand[PowerExpand[`sbj`result /. 
    BesselJ[n_, z_] :> Sqrt[2*z/Pi]*sbj[n, z]]]

Out[6]=
1/2*sbj[-(1/2) + j, r] - sbj[1/2 + j, r]/(2*r) - 
  1/2*sbj[3/2 + j, r]

plain, simple, the desired result

let's look, what happend:

In[7]:= Information["sbj", LongForm -> False]
"Global`sbj"

In[8]:= Information["`sbj`sbj", LongForm -> False]
"Global`sbj`sbj"

Global`sbj`sbj[Global`sbj`n_, Global`sbj`z_] := 
    Sqrt[Pi/(2*Global`sbj`z)]*BesselJ[Global`sbj`n + 
       1/2, Global`sbj`z]

Now what to do, if you want another (different) calculation? Just copy
the text above and insert the new expression you want to evaluate, e.g.

In[9]:=
Begin["`sbj`"]; 
sbj[n_, z_] := Sqrt[Pi/(2*z)]*BesselJ[n + 1/2, z]; 
result = Integrate[sbj[j, r], r]; 
End[]; 
Expand[PowerExpand[`sbj`result /. 
    BesselJ[n_, z_] :> Sqrt[2*z/Pi]*sbj[n, z]]]

Out[13]=
2^(-2 - j)*Sqrt[Pi]*r^(1 + j)*Gamma[1/2 + j/2]*
  HypergeometricPFQRegularized[{1/2 + j/2}, 
   {1 + 1/2*(1 + 2*j), 3/2 + j/2}, -(r^2/4)]


It would be great if we could hide the "copied lines" in a function and
execute the expression as an argument.

I tried, but didn't succeed. The reason was, the localization to the
context "`sbj`" did not work when executed within a CompoundExpression,
Module, Block or With construct. It even doesn't do within the following
"textual macro"

In[1]:=
ToExpression[StringJoin["j;r;
    Begin[\"`sbj`\"];
    sbj[n_,z_]:=Sqrt[Pi/(2*z)]*BesselJ[n+1/2,z];
    result=", StringTake[ToString[Hold[D[sbj[j, r], r]]], {6, -2}], ";
    End[];
    Expand at PowerExpand[`sbj`result/. BesselJ[n_,z_]:>Sqrt[2
z/Pi]*sbj[n,z] \
]"]]

Can someone explain me why this is so?

Kind regards, Hartmut


  • Prev by Date: Re: cellular automata & OOP stupid?
  • Next by Date: Re: Making a function dynamically define another conditional function...
  • Previous by thread: Re: How can I teach Mathematica new functions
  • Next by thread: Thermodynamics - Steam Tables