MathGroup Archive 1999

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

Search the Archive

Re: Function definition

  • To: mathgroup at smc.vnet.net
  • Subject: [mg15781] Re: Function definition
  • From: Emmanuel.Peccoud at e-notebooks.com (Emmanuel Peccoud)
  • Date: Sun, 7 Feb 1999 02:03:57 -0500 (EST)
  • Organization: e-NoteBooks Ltd.
  • References: <79ea4l$9hm@smc.vnet.net>
  • Sender: owner-wri-mathgroup at wolfram.com

Rangachari Kidambi <rkidambi at spock.usc.edu> wrote:

> Hi,
> 
> I need to define a function f(theta,phi) as
> 
> f(theta,phi) = f1 if 0 <= theta <= theta0, 0 <= phi <= phi0 f(theta,phi)
> = f2 otherwise
> 
> where theta, phi are spherical polar coordinates, <= is less than or
> equal to and theta0 and phi0 are constants.
> 
> I have tried using Do loops and If statements but haven't been
> successful.
> 
> Thanks for any help
> Ranga Kidambi

First define a function inRange to check that the arguments make sense.

inRange[x_]:=IntervalMemberQ[Interval[{0, 2Pi}], x]

Then a second function is defined to check if your two arguments are
below the thresholds. The third and fourht arguments are optionnal with
default value set to Pi. You can can that or use option if you prefer.

test[theta_, phi_, theta0_:Pi, phi0_:Pi ]:=
  TrueQ[0<=theta<=theta0 &&0<=phi<=phi0 ]

Then we use two definitions, one valid when test returns true, the other
one valid when test returns false. Note that we use inRange to make
sure that f1 or f2 is returned only for sensible arguments of the
function. The last definition is a garbage collecting definition
returning $Failed for anything not matching the more specific
conditions.

f[theta_?inRange, phi_?inRange]:= f1/;test[theta, phi] f[theta_?inRange,
phi_?inRange]:= f2 f[___]:=$Failed

In[70]:=
f[Pi/2, Pi/2]
f[3Pi/2, Pi/2]
f[3Pi/2, 3Pi/2]
f[3Pi, 3Pi/2]
f["foo"]
Out[70]=
f1
Out[71]=
f2
Out[72]=
f2
Out[73]=
$Failed
Out[74]=
$Failed


  • Prev by Date: Re: BracketingBar for output
  • Next by Date: Re: Q: Why can't I stop TraditionalForm dismissing Times Attributes ?
  • Previous by thread: Re: Function definition
  • Next by thread: Re: Function definition