Services & Resources / Wolfram Forums
-----
 /
MathGroup Archive
1995
*January
*February
*March
*April
*May
*June
*July
*August
*September
*October
*November
*December
*Archive Index
*Ask about this page
*Print this page
*Give us feedback
*Sign up for the Wolfram Insider

MathGroup Archive 1995

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

Search the Archive

Compile[] of Which[]

  • To: mathgroup at christensen.cybernetics.net
  • Subject: [mg1274] Compile[] of Which[]
  • From: Jonathan Rich <rich at news.acns.nwu.edu>
  • Date: Wed, 31 May 1995 03:25:41 -0400
  • Organization: Northwestern University, Evanston, IL, US

I am trying to compile a procedure.  In the course of this,
I came across what is either an error in the documentation, or
a bug, or something I don't understand properly.  The documentation
for "Compile[]" (see MathSource #0203-971 by Matt Cook or
0204-028 by Jerry Keiper) clearly states that the testing function
"Which[]" is compilable.  However in my experience, looking at the
guts of the CompiledFunction, it requests that "Which[]" be
evaluated externally, which defeats the whole purpose of Compile
and greatly slows down the calculation.  This has forced me to 
replace a single "Which" by numerous "If"s.  Using this workaround
Compile will work as it should.  However I would like clarification
on whether the documentation is in error or just what is going on.
Below is a very simple example to demonstrate the problem:

Mathematica 2.2 for HP9000 Series 700
Copyright 1988-93 Wolfram Research, Inc.
 -- Motif graphics initialized -- 

In[1]:= fWhich=Compile[{a,b},
            Which[ a<b,b,
                    a>b,a,
                    a==b,a
            ]
        ];

In[2]:= fIf=Compile[{a,b},
            If[ a<b,b,
                If[ a>b,a,a
                ]
            ]
        ];

In[3]:= fIf//InputForm

Out[3]//InputForm= 
  CompiledFunction[{_Real, _Real}, {2, 0, 4, 0}, 
   {{1, 17}, {4, 1, 0}, {4, 2, 1}, {75, 0, 1, 0}, {69, 0, 3}, {17, 1, 3}, 
    {70, 7}, {75, 1, 0, 1}, {69, 1, 3}, {17, 0, 2}, {70, 2}, {17, 0, 2}, 
    {17, 2, 3}, {8, 3}}, Function[{a, b}, If[a < b, b, If[a > b, a, a]]]]

In[4]:= fWhich//InputForm

Out[4]//InputForm= 
  CompiledFunction[{_Real, _Real}, {0, 0, 3, 0}, 
   {{1, 17}, {4, 1, 0}, {4, 2, 1}, 
    {24, Function[{a, b}, Which[a < b, b, a > b, a, a == b, a]], 2}, {8, 2}}, 
   Function[{a, b}, Which[a < b, b, a > b, a, a == b, a]]]

The above shows that "fif" is fully compiled but that in "fwhich" the
opcode 24 indicates that "Which[]" is considered non-compilable and so
must be evaluated externally despite the documentation which says otherwise.

Any clarification would be much appreciated.
Thanks.

-- 
Jonathan Rich
Geoscience Dept., NWU



  • Prev by Date: Problem with Machine Precision
  • Next by Date: Re: Points disappeared in ListPlot
  • Previous by thread: Problem with Machine Precision
  • Next by thread: Re: Points disappeared in ListPlot