Re: How can I teach Mathematica new functions

• To: mathgroup at smc.vnet.net
• Subject: [mg21717] Re: [mg21708] How can I teach Mathematica new functions
• From: BobHanlon at aol.com
• Date: Sun, 23 Jan 2000 17:58:01 -0500 (EST)
• Sender: owner-wri-mathgroup at wolfram.com

```Clear[sbj]

If you provide a general definition for sbj then Mathematica will always
apply that definition so that sbj could never appear in an output. On the
other hand, you must provide a means to evaluate sbj for numeric inputs.
Consequently, define sbj only for numeric inputs.

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

{sbj[n, z], sbj[3, z], sbj[n, 4.2], sbj[3, 4.2], sbj[2, Pi], sbj[2, Pi] // N}

{sbj[n, z], sbj[3, z], sbj[n, 4.2], 0.23697022838678544,
BesselJ[5/2, Pi]/Sqrt[2], 0.30396355092701327}

Plot[Evaluate[Table[sbj[n, z], {n, 0, 2}]], {z, 0, 3Pi},
PlotStyle -> {RGBColor[1, 0, 0], RGBColor[0, 1, 0], RGBColor[0, 0, 1]}];

In working on other definitions related to sbj, it is useful to have the
substitution rules

SBJtoBJ = sbj[n_, z_] :> Sqrt[Pi/(2*z)]*BesselJ[n + 1/2, z];
BJtoSBJ = BesselJ[n_, z_] :> Sqrt[2*z/Pi]*sbj[n - 1/2, z];

For its derivative

Simplify[D[sbj[n, z] /. SBJtoBJ, z] /. BJtoSBJ]

(Sqrt[1/z]*(z*sbj[n - 1, z] - sbj[n, z] - z*sbj[n + 1, z]))/
(2*Sqrt[z])

Derivative[0, 1][sbj][n_, z_] :=
Sqrt[1/z]*(z*sbj[n - 1, z] - sbj[n, z] - z*sbj[n + 1, z])/(2 Sqrt[z])

D[sbj[n, z], z] == %%

True

Simplify[D[sbj[n, z], z], Element[z, Reals]]

-((-z*sbj[n - 1, z] + sbj[n, z] + z*sbj[n + 1, z])/
(2*Abs[z]))

Its integral

Integrate[sbj[n, z] /. SBJtoBJ, z] // Simplify

2^(-n - 2)*Sqrt[Pi]*Sqrt[1/z]*z^(n + 3/2)*Gamma[(n + 1)/2]*
HypergeometricPFQRegularized[{(n + 1)/2},
{n + 3/2, (n + 3)/2}, -(z^2/4)]

sbj /: Integrate[sbj[n_, z_], z_] :=
2^(-n - 2) * Sqrt[Pi] * Sqrt[1/z]*z^(n + 3/2) * Gamma[(n + 1)/2] *
HypergeometricPFQRegularized[{(n + 1)/2},
{n + 3/2, (n + 3)/2}, -z^2/4]

Integrate[sbj[n, z], z] == %%

True

A definite integral

Integrate[sbj[n, z] /. SBJtoBJ, {z, 0, Infinity}] /. BJtoSBJ

Sqrt[Pi/2]*If[Re[n] > -1, Gamma[(n + 1)/2]/
(Sqrt[2]*Gamma[n/2 + 1]), Integrate[
Sqrt[1/z]*Sqrt[(2*z)/Pi]*sbj[n + 1/2 - 1/2, z],
{z, 0, Infinity}]]

sbj /: Integrate[sbj[n_, z_], {z_, 0, Infinity}, opts___] :=
If[Simplify[Re[n] > -1, Assumptions /. Flatten[{opts}]],
Sqrt[Pi] * Gamma[(n + 1)/2]/
(2*Gamma[n/2 + 1]),
Integrate[sbj[n, z], {z, 0, Infinity}]]

Integrate[sbj[n, z], {z, 0, Infinity}]

If[Re[n] > -1, (Sqrt[Pi]*Gamma[(n + 1)/2])/
(2*Gamma[n/2 + 1]), Integrate[sbj[n, z],
{z, 0, Infinity}]]

Integrate[sbj[n, z], {z, 0, Infinity}, Assumptions -> n > -1] == %[[2]]

True

And any other definitions that you need in your application.

Bob Hanlon

In a message dated 1/23/2000 3:14:08 AM, gar at gone.home writes:

>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?
>

```

• Prev by Date: Re: DSolve problems with system of ODEs. Out in pure notation?
• Next by Date: Thermodynamics - Steam Tables
• Previous by thread: Re: DSolve problems with system of ODEs. Out in pure notation?
• Next by thread: Re: How can I teach Mathematica new functions