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$>
References: <guh57c$9ko$> <guh9jn$1bk$> <guhae5$q3e$> <guhibb$ghs$> <guhkma$hk7$>
Reply-To: "Yair Altman" <altmanyDEL@gmailDEL.comDEL>
Content-Type: text/plain; charset="ISO-8859-1"
Content-Transfer-Encoding: 8bit
X-Trace: 1242327482 8732 (14 May 2009 18:58:02 GMT)
NNTP-Posting-Date: Thu, 14 May 2009 18:58:02 +0000 (UTC)
X-Newsreader: MATLAB Central Newsreader 642467
Xref: 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).