Path: news.mathworks.com!not-for-mail
From: "Yair Altman" <altmanyDEL@gmailDEL.comDEL>
Newsgroups: comp.soft-sys.matlab
Subject: Re: subfunctions versus overloaded functions
Date: Thu, 14 May 2009 18:58:02 +0000 (UTC)
Organization: TACT Computer Systems Ltd
Lines: 25
Message-ID: <guhpjq$8gs$1@fred.mathworks.com>
References: <guh57c$9ko$1@fred.mathworks.com> <guh9jn$1bk$1@fred.mathworks.com> <guhae5$q3e$1@fred.mathworks.com> <guhibb$ghs$1@fred.mathworks.com> <guhkma$hk7$1@fred.mathworks.com>
Reply-To: "Yair Altman" <altmanyDEL@gmailDEL.comDEL>
NNTP-Posting-Host: webapp-03-blr.mathworks.com
Content-Type: text/plain; charset="ISO-8859-1"
Content-Transfer-Encoding: 8bit
X-Trace: fred.mathworks.com 1242327482 8732 172.30.248.38 (14 May 2009 18:58:02 GMT)
X-Complaints-To: news@mathworks.com
NNTP-Posting-Date: Thu, 14 May 2009 18:58:02 +0000 (UTC)
X-Newsreader: MATLAB Central Newsreader 642467
Xref: news.mathworks.com comp.soft-sys.matlab:540018

"John D'Errico" wrote ... 
> I changed the name of my subfunction to isbigprime,
> which does the isprime computation for numbers as
> large as 2^46 or so. It still a subfunction, but now has
> a non-conflicting name.
> 
> Then I set a flag up front that indicated whether I was
> looking at doubles or at a vpi number, the alternative
> class. 
> 
> The code then has a switch inside that decides which
> function to call. Simple enough in theory, but a kludge
> since MATLAB is supposed to be smart enough to know
> the difference here.
> 
> John

I think that you can keep you original subfunction called isprime, and make no code switch outside it (after all, isprime should hide the implementation details). Then, within your isprime subfunction, use try-catch to process the regular (numeric) way in the try block, incurring negligible performance loss for most (numeric) cases; the VPI cases will be handled in the catch block. Alternately, use a method that only exists for VPIs at the beginning of your try block, thus throwing the normal (numeric) cases to the catch block and continuing with VPIs in the try block. Both of these options look cleaner and possibly better in my eyes.

Another idea is to test if isnumeric(N) - this could be faster than isa(N,'vpi'), although probably slower than try-catch.

To access the built-in isprime you can simply use builtin('isprime',N).

HTH,
Yair