MathGroup Archive 2003

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

Search the Archive

Re: Defining function in loop

  • To: mathgroup at smc.vnet.net
  • Subject: [mg40483] Re: Defining function in loop
  • From: Jens-Peer Kuska <kuska at informatik.uni-leipzig.de>
  • Date: Mon, 7 Apr 2003 04:56:13 -0400 (EDT)
  • Organization: Universitaet Leipzig
  • References: <b6j9oo$645$1@smc.vnet.net>
  • Sender: owner-wri-mathgroup at wolfram.com

Hi,

Condition[] evaluate it's arguments in nonstandard way
but

Clear[g]
g[x_] := 0 /; x < 0
SetDelayed[g[x_], #] & /@ Table[i /; Evaluate[ i - 1 <= x < i], {i, 3}];
g[x_] := 4 /; x > 3


will work and you many know the Floor[] function

Regards
  Jens


Vadim Nagornyi wrote:
> 
> Hello,
> I am definihg step-like function
> 
> Clear[h]
> h[x_] := 0/;x<0
> h[x_] := 1/;0<=x<=1
> h[x_] := 2/;1<=x<=2
> h[x_] := 3/;2<=x<=3
> h[x_] := 4/;x>3
> 
> It works fine:
> 
> Table[h[x],{x,-1,4,.5}]
> {0, 0, 1, 1, 1, 2, 2, 3, 3, 4, 4}
> 
> As I may need more steps, I am trying to do the same in loop:
> 
> Clear[g]
> g[x_] := 0/;x<0
> Do[g[x_] := i/;i-1<=x<=i, {i,3}]
> g[x_] := 4/;x>3
> 
> This time no luck:
> 
> Table[g[x],{x,-1,4,.5}]
> {0, 0, 1, 1, 1, 2, 2, 3, 3, 4, 4}
> {0, 0, g[0.], g[0.5], g[1.], g[1.5], g[2.], g[2.5], g[3.], 4, 4}
> 
> How to do this?
> 
> Thanks.
> Vadim.


  • Prev by Date: Re: ImageRotate
  • Next by Date: Re: Super-Increasing List
  • Previous by thread: RE: Defining function in loop
  • Next by thread: EXCEL XP and Mathematica 4.2 - Problem with M-code sheets