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

Re: [IMP-dev] Harmonic cleanup



Daniel Russel wrote:
[*] Unfortunately this is missed by the unit tests right now because
there is no way to overload return type in Python. (There is no way in
C++ either - we overload the arguments instead - but SWIG converts both
overloaded C++ functions to the same Python method.)
Swig handles return arguments via type maps, right? And handles overloaded functions. So is the problem that they can't coexist?

No - the problem is that in C++ we have two overloaded functions:
Float Harmonic::operator()(Float feat);
Float Harmonic::operator()(Float feat, Float &deriv);
where &deriv is used for a second return value, since C++ only allows a single return value.

Usually, typemaps are set up in SWIG to map 'second return values' to real return values in Python, since Python allows multiple returns (but does not have the concept of references or pointers to primitive types). So these would be mapped to something like
val = Harmonic(feat)
(val, deriv) = Harmonic(feat)
which wouldn't work of course, since you can't overload the return type.

(Actually, right now this is not done, and the second form still takes two arguments - but the second is an opaque SWIG "pointer-to-float" object which can't be used in Python.)

I propose mapping the second to a new Python method, i.e.
(val, deriv) = Harmonic.evaluate_deriv(feat)

I can do this very easily without any changes to the C++ code. But maybe for consistency between C++ and Python it makes more sense to replace operator() with evaluate(feat) and evaluate_deriv(feat, &deriv) ?

It would be really nice to be able to run tests on unary functions.

Well, we already can, of course - just not on the derivatives.

	Ben
--
                      http://salilab.org/~ben/
"It is a capital mistake to theorize before one has data."
	- Sir Arthur Conan Doyle