MathGroup Archive 1998

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

Search the Archive

Q:Proper use of Evaluate[] inside Which[]

  • To: mathgroup at smc.vnet.net
  • Subject: [mg14400] Q:Proper use of Evaluate[] inside Which[]
  • From: sergio_r at my-dejanews.com
  • Date: Sun, 18 Oct 1998 15:10:05 -0400
  • Organization: Deja News - The Leader in Internet Discussion
  • Sender: owner-wri-mathgroup at wolfram.com

(*---------------------------------------------------------------------------
Hello:
   How can I force the evaluation of an expression
   inside Which? For instance, the following piece
   of code:
----------------------------------------------------------------------------*)
Clear[Xa, Xb, iIndex, jIndex, X, g, ff, x, f, k];

                Xa = 0.0; Xb = 3.0; jIndex = 3; iIndex = 1;
              X[1] = Xa; X[i_] := X[i-1] + (Xb - Xa)/(jIndex-1)
        g[j_][x_] := (x-X[j])*(x-j)

  ff[i_][j_][x_] := Which[
                    (i == 1 && j != 1),
                         Which[ x >= Evaluate[X[j-1]] && x <
Evaluate[X[j]],
                                       Evaluate[g[j][x]],
                                x >= Evaluate[X[j]] && x <=
Evaluate[X[j+1]],
                                       Evaluate[g[i][x]],
                                True, 0],
                    True, 0];
k = 0;
Do[For[i=1, i<=(iIndex + 2), i=i+1,
                  k = k + 1;
          f[k][x_] := Evaluate[ff[i][j][x]]],
     {j,1,(jIndex-1)}]

?f

(*---------------------------------------------------------------------------
   produces the following output:

In[1]:= $Version[]
Out[1]= Solaris 3.0 (November 22, 1996)[]

In[2]:= << test.m
Global`f

f[1][x_] := 0
f[2][x_] := 0
f[3][x_] := 0
f[4][x_] := Which[x >= 0. && x < 1.5, (-2 + x)*(-1.5 + x),
   x >= Evaluate[X[2]] && x <= Evaluate[X[2 + 1]], (-1 + x)*(0. + x),
True, 0] f[5][x_] := 0
f[6][x_] := 0

  How come Evaluate[] does not evaluate the second condition?
  How can I overcome this? I would like to get something like:

f[4][x_] := Which[x >= 0.  && x <  1.5, (-2 + x)*(-1.5 + x),
                  x >= 1.5 && x <= 3.0, (-1 + x)*(0. + x),
                  True, 0]
Sergio Rojas
E-mail: sergio at scisun.sci.ccny.cuny.edu

PS. Mathematica 2.2 output for this code is:

In[10]:= $Version
Out[10]= SPARC 2.2 (February 5, 1994)

In[11]:=  << test.m
Global`f

f[1][x_] := 0
f[2][x_] := 0
f[3][x_] := 0
f[4][x_] := Which[x >= Evaluate[X[2 - 1]] && x < Evaluate[X[2]],
   (-2 + x)*(-1.5 + x), x >= Evaluate[X[2]] && x <= Evaluate[X[2 + 1]],
   (-1 + x)*(0. + x), True, 0]
f[5][x_] := 0
f[6][x_] := 0
----------------------------------------------------------------------------*)

-----------== Posted via Deja News, The Discussion Network ==----------
http://www.dejanews.com/       Search, Read, Discuss, or Start Your Own



  • Prev by Date: I need some help ...
  • Next by Date: Re: Two programming challenges
  • Previous by thread: Re: I need some help ...
  • Next by thread: Corrupted NB