octave-maintainers
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

polymorphism in functions


From: Pascal A. Dupuis
Subject: polymorphism in functions
Date: Thu, 8 Jan 2004 03:47:33 -0600
User-agent: Mutt/1.5.4i

Hello,

I'm writing a function to test if data can be described by some
pdf. Similar to hist(), data are collected in bins, and expected and
observed occurences are compared for the bins.

But I rapidly found myself in need of some flexibility: I can wish to
pass mu and sigma for a normal distribution, or a vector with
threshold values for the bins, or a set of threshold and expected
number of occurences to avoid recalculating the latter each time, or ...

Implementing polymorphism requires to test the number and types of
input arguments, but this becomes rapidly complicated to get things
working right.

One possible solution is to use a formalism of named arguments, like
in R: 

%# default number of bin, distribution = gaussian (default)
result=myfunc(x, mu = someval, sigma = someotherval)
%# specify explicitelly the threshold 
result=myfunc(x, sigma = someotherval, threshold=[-3:.5:3])
%# specify explicitelly the number of bins
result=myfunc(x, mu = someval, nbin=16)
%# gives precomputed threshold and expectations for a uniform pdf over 0:1
 result=myfunc(x, threshold=[.1:.1:.9], expected=(ones(1,10)*length(x)/10))

So you get an idea of the flexibility. At the program level, things
are rather simple:
- if one of (threshold, expectation) is given, compute the other one
- if none is given, compute both based on mu, sigma, and distribution
type

The prototype for such a function would f.i. be:

function p = myfunc(x, [ mu = 0, sigma=[], threshold=[], expected=[],
                    nbin = 8, type='gaussian']);

The named arguments are placed inside a [], and are of the form 
name=default_value;

To build the function invocation at compile time, the steps are:
1) create and initialize the variable arguments with default values,
2) parse the argument list, and, with pairs of type LHS = RHS,
evaluate the RHS and store the result in the variable whose name is
given is LHS. This need to be performed only once if the LHS are constant.

Advantages:
-more flexibility in the way the function works;
-ability to add supplemental variables arguments without breaking
compatibility with previous scripts
-safer to program; doesn't require a lot of (dynamic) tests to decide how to
take the arguments into account.

Problems:
-code a bit bigger
-each function invocation will be translated into an initial code
performing the variable setup, followed by a call to the function
core.

What do you think about it ?

Best regards

Pascal Dupuis
 
-- 
Dr. ir. Pascal Dupuis
K. U. Leuven, ESAT/ELECTA (formerly ELEN):  http://www.esat.kuleuven.ac.be/
Kasteelpark Arenberg, 10; B-3001 Leuven-Heverlee, Belgium
Tel. +32-16-32 10 21 -- Fax +32-16-32 19 85



reply via email to

[Prev in Thread] Current Thread [Next in Thread]