MathGroup Archive 1999

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

Search the Archive

Re: Function definition

Rangachari Kidambi <rkidambi at> 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

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

f[Pi/2, Pi/2]
f[3Pi/2, Pi/2]
f[3Pi/2, 3Pi/2]
f[3Pi, 3Pi/2]

  • 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