MathGroup Archive 1997

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

Search the Archive

Re: Re: Which, If and "neither True nor False"

  • To: mathgroup at smc.vnet.net
  • Subject: [mg9877] Re: [mg9728] Re: Which, If and "neither True nor False"
  • From: Allan Hayes <hay at haystack.demon.co.uk>
  • Date: Sun, 30 Nov 1997 00:39:27 -0500
  • References: <01bcfb4f$a6e45020$a90a5092@pehowland.dra.hmg.gb>
  • Sender: owner-wri-mathgroup at wolfram.com

Re: [mg9728] Re: Which, If and "neither True nor False"

Paul Howland has sent me a solution by Robert Villegas to his original
question ("The If statement has an optional fourth argument which is
executed if the test returns neither True nor False.  Is it possible to
do the same in a Which statement?"). I give two variations to this,
both faster. 

Attributes[MyWhich]=HoldAll;  (*Robert Villegas*) MyWhich[args___,
Indeterminate, indetValue_]:=
	With[{evalWhich=Which[args]},
		Switch[evalWhich,
			_Which, indetValue,
			otherwise_, evalWhich
	]
]
MyWhich[args___] := Which[args]

Timing on test given below, 2.25 Second.

SetAttributes[MyWhich2, HoldAll]
MyWhich2[args___, Indeterminate, indetValue_] := 
	Switch[#, 
		_Which, indetValue, 
		otherwise_, #
	]&[Which[args]]; 
MyWhich2[args___] := Which[args]

Timing on test, 1.78333 Second

SetAttributes[MyWhich3, HoldAll]
MyWhich3[args___, Indeterminate, indetValue_] := 
	Replace[Which[args],_Which->indetValue]; MyWhich3[args___] :=
Which[args]

Timing on test, 1.23333 Second

So, at least for this function, passing values with Function is quicker
than with With, and avoiding this passing by using Replace is best of
all.

The test used was
Do[{MyWhich[True,6],
	MyWhich[False,6,Indeterminate,7],
	MyWhich[xxx,6,Indeterminate,7]
},{1000}]//Timing//First
 
-- 
Allan Hayes
Mathematica Training and Consulting
Leicester, UK
hay at haystack.demon.co.uk
http://www.haystack.demon.co.uk
voice: +44 (0)116 271 4198
fax: +44 (0)116 271 4198


  • Prev by Date: Re: New User Question
  • Next by Date: Re: Trivial integral freezes 3.0
  • Previous by thread: Re: Re: Which, If and "neither True nor False"
  • Next by thread: how to generate many plots without so many input lines?