QuantLib-SWING: is it possible to use polymorphism?

classic Classic list List threaded Threaded
9 messages Options
Reply | Threaded
Open this post in threaded view
|

QuantLib-SWING: is it possible to use polymorphism?

andrea-110
Hi,

I've tried to use polymorphism in QuantLib-SWING Java version

Something like

class MyPayoff extends Payoff {}

then

Payoff p = new MyPayoff()
VanillaOption vo = new VanillaOption(p,...)

but I get "wrong payoff type" since it must inherit from StrikeTypedPayoff which is not exposed in Java.

Am I doing something stupid?


------------------------------------------------------------------------------
Come build with us! The BlackBerry(R) Developer Conference in SF, CA
is the only developer event you need to attend this year. Jumpstart your
developing skills, take BlackBerry mobile applications to market and stay
ahead of the curve. Join us from November 9 - 12, 2009. Register now!
http://p.sf.net/sfu/devconference
_______________________________________________
QuantLib-dev mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/quantlib-dev
Reply | Threaded
Open this post in threaded view
|

Re: QuantLib-SWING: is it possible to use polymorphism?

andrea-110
On 19/10/09 10:53, Andrea wrote:
> Hi,
>
> I've tried to use polymorphism in QuantLib-SWING Java version
>
> Something like
>
> class MyPayoff extends Payoff {}

I did try as well

class MyPayoff extends PlainVanillaPayoff
{
        MyPayoff()
        {
                super(Option.Type.Put, 10.0);
        }

        public double getValue(double arg0)
        {
                throw new RuntimeException();
        }
}

but the exception is never thrown.

I've used this class in the example EquityOptions.java.


------------------------------------------------------------------------------
Come build with us! The BlackBerry(R) Developer Conference in SF, CA
is the only developer event you need to attend this year. Jumpstart your
developing skills, take BlackBerry mobile applications to market and stay
ahead of the curve. Join us from November 9 - 12, 2009. Register now!
http://p.sf.net/sfu/devconference
_______________________________________________
QuantLib-dev mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/quantlib-dev
Reply | Threaded
Open this post in threaded view
|

Re: QuantLib-SWING: is it possible to use polymorphism?

Luigi Ballabio
In reply to this post by andrea-110
On Mon, 2009-10-19 at 10:53 +0100, Andrea wrote:
> I've tried to use polymorphism in QuantLib-SWING Java version
>
> Something like
>
> class MyPayoff extends Payoff {}

Andrea,
        unfortunately, polymorphism doesn't currently work across languages.
Sorry.

Luigi



--

Ninety percent of everything is crap.
--- Theodore Sturgeon



------------------------------------------------------------------------------
Come build with us! The BlackBerry(R) Developer Conference in SF, CA
is the only developer event you need to attend this year. Jumpstart your
developing skills, take BlackBerry mobile applications to market and stay
ahead of the curve. Join us from November 9 - 12, 2009. Register now!
http://p.sf.net/sfu/devconference
_______________________________________________
QuantLib-dev mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/quantlib-dev
Reply | Threaded
Open this post in threaded view
|

Re: QuantLib-SWING: is it possible to use polymorphism?

andrea-110
On 21/10/09 15:55, Luigi Ballabio wrote:

> On Mon, 2009-10-19 at 10:53 +0100, Andrea wrote:
>> I've tried to use polymorphism in QuantLib-SWING Java version
>>
>> Something like
>>
>> class MyPayoff extends Payoff {}
>
> Andrea,
> unfortunately, polymorphism doesn't currently work across languages.
> Sorry.
>
> Luigi

Indeed.
I've tried to use directors and it seems to work well if there are no shared_ptr involved.

My problem now is

given

class A; (with some virtual functions)

class B
{
        virtual boost::shared_ptr<A> a() = 0;
};

I don't seem to be able to create a shared_ptr in the target language.
Everything would be very easy if instead of the shared_ptr the code had a raw pointer.
I guess I need some SWIG expert here.



------------------------------------------------------------------------------
Come build with us! The BlackBerry(R) Developer Conference in SF, CA
is the only developer event you need to attend this year. Jumpstart your
developing skills, take BlackBerry mobile applications to market and stay
ahead of the curve. Join us from November 9 - 12, 2009. Register now!
http://p.sf.net/sfu/devconference
_______________________________________________
QuantLib-dev mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/quantlib-dev
Reply | Threaded
Open this post in threaded view
|

Re: QuantLib-SWING: is it possible to use polymorphism?

Luigi Ballabio
On Wed, 2009-10-21 at 17:58 +0100, Andrea wrote:

> On 21/10/09 15:55, Luigi Ballabio wrote:
> > unfortunately, polymorphism doesn't currently work across
> languages.
> >
> Indeed.
> I've tried to use directors and it seems to work well if there are no
> shared_ptr involved.
>
> My problem now is [...] I don't seem to be able to create a shared_ptr
> in the target language.

There would be quite a bit of work to do.  You would have to:
- add a constructor to boost::shared_ptr in common.i so that it takes a
pointer;
- modify the exported payoff classes so that they no longer hide the
shared_ptr (e.g., export the actual PlainVanillaPayoff, not
PlainVanillaPayoffPtr) and enable directors;
- export the shared_ptr<Payoff> as a separate class;
- fix the other interfaces so that they work with the new classes.

Let me know if you get stuck in any of the steps.

Luigi


--

Green's Law of Debate:
Anything is possible if you don't know what you're talking about.



------------------------------------------------------------------------------
Come build with us! The BlackBerry(R) Developer Conference in SF, CA
is the only developer event you need to attend this year. Jumpstart your
developing skills, take BlackBerry mobile applications to market and stay
ahead of the curve. Join us from November 9 - 12, 2009. Register now!
http://p.sf.net/sfu/devconference
_______________________________________________
QuantLib-dev mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/quantlib-dev
Reply | Threaded
Open this post in threaded view
|

Re: QuantLib-SWING: is it possible to use polymorphism?

andrea-110
On 29/10/09 16:03, Luigi Ballabio wrote:

> On Wed, 2009-10-21 at 17:58 +0100, Andrea wrote:
>> On 21/10/09 15:55, Luigi Ballabio wrote:
>>> unfortunately, polymorphism doesn't currently work across
>> languages.
>>>
>> Indeed.
>> I've tried to use directors and it seems to work well if there are no
>> shared_ptr involved.
>>
>> My problem now is [...] I don't seem to be able to create a shared_ptr
>> in the target language.
>
> There would be quite a bit of work to do.  You would have to:
> - add a constructor to boost::shared_ptr in common.i so that it takes a
> pointer;

I've got a big doubt here.
Is it safe to stick the raw pointer into a shared_ptr? Is it already owned by some other swig mechanism?

> - modify the exported payoff classes so that they no longer hide the
> shared_ptr (e.g., export the actual PlainVanillaPayoff, not
> PlainVanillaPayoffPtr) and enable directors;
> - export the shared_ptr<Payoff> as a separate class;
> - fix the other interfaces so that they work with the new classes.
>
> Let me know if you get stuck in any of the steps.
>
> Luigi
>
>



------------------------------------------------------------------------------
Come build with us! The BlackBerry(R) Developer Conference in SF, CA
is the only developer event you need to attend this year. Jumpstart your
developing skills, take BlackBerry mobile applications to market and stay
ahead of the curve. Join us from November 9 - 12, 2009. Register now!
http://p.sf.net/sfu/devconference
_______________________________________________
QuantLib-dev mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/quantlib-dev
Reply | Threaded
Open this post in threaded view
|

Re: QuantLib-SWING: is it possible to use polymorphism?

Tito Ingargiola


Hi Andrea,

Another approach you may consider is illustrated in the DiscreteHedging.java example.  Here I wanted to use Quantlib's MC capabilities but wanted the pricer to be implemented in java.  By pulling the MonteCarloModel impl into java I was able to use a pathpricer implemented in java.  

Not sure if this is applicable to your problem, but it's an approach I've used in a number of cases to integrate quantlib and java.  You may also implement your Payoff in C++ and then expose it yourself via swig to your java....

Best,

     Tito.


----- Original Message ----
From: Andrea <[hidden email]>
To: [hidden email]
Sent: Thu, October 29, 2009 1:59:42 PM
Subject: Re: [Quantlib-dev] QuantLib-SWING: is it possible to use polymorphism?

On 29/10/09 16:03, Luigi Ballabio wrote:

> On Wed, 2009-10-21 at 17:58 +0100, Andrea wrote:
>> On 21/10/09 15:55, Luigi Ballabio wrote:
>>>     unfortunately, polymorphism doesn't currently work across
>> languages.
>>>
>> Indeed.
>> I've tried to use directors and it seems to work well if there are no
>> shared_ptr involved.
>>
>> My problem now is [...] I don't seem to be able to create a shared_ptr
>> in the target language.
>
> There would be quite a bit of work to do.  You would have to:
> - add a constructor to boost::shared_ptr in common.i so that it takes a
> pointer;

I've got a big doubt here.
Is it safe to stick the raw pointer into a shared_ptr? Is it already owned by some other swig mechanism?

> - modify the exported payoff classes so that they no longer hide the
> shared_ptr (e.g., export the actual PlainVanillaPayoff, not
> PlainVanillaPayoffPtr) and enable directors;
> - export the shared_ptr<Payoff> as a separate class;
> - fix the other interfaces so that they work with the new classes.
>
> Let me know if you get stuck in any of the steps.
>
> Luigi
>
>



------------------------------------------------------------------------------
Come build with us! The BlackBerry(R) Developer Conference in SF, CA
is the only developer event you need to attend this year. Jumpstart your
developing skills, take BlackBerry mobile applications to market and stay
ahead of the curve. Join us from November 9 - 12, 2009. Register now!
http://p.sf.net/sfu/devconference
_______________________________________________
QuantLib-dev mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/quantlib-dev


------------------------------------------------------------------------------
Come build with us! The BlackBerry(R) Developer Conference in SF, CA
is the only developer event you need to attend this year. Jumpstart your
developing skills, take BlackBerry mobile applications to market and stay
ahead of the curve. Join us from November 9 - 12, 2009. Register now!
http://p.sf.net/sfu/devconference
_______________________________________________
QuantLib-dev mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/quantlib-dev
Reply | Threaded
Open this post in threaded view
|

Re: QuantLib-SWING: is it possible to use polymorphism?

andrea-110
On 31/10/09 14:15, Tito Ingargiola wrote:
>
>
> Hi Andrea,
>
> Another approach you may consider is illustrated in the DiscreteHedging.java example.
> Here I wanted to use Quantlib's MC capabilities but wanted the pricer to be implemented in java.
> By pulling the MonteCarloModel impl into java I was able to use a pathpricer implemented in java.


When you say "pulling", you mean you have rewritten it in Java, mirroring the PathPricer->JPathPricer.

What I am trying to do is to have the JPathPricer automatically generated by swig.
This is actually easy since JPathPricer only returns a double.

My problem is the class "Option" that usually returns a shared_ptr to a "Payoff" and here I have
problems.


> Not sure if this is applicable to your problem, but it's an approach I've used in a number of cases to integrate quantlib and java.
> You may also implement your Payoff in C++ and then expose it yourself via swig to your java....

This is certanly possible, but I would like to avoid writing any C++ and only use Java.

>
> Best,
>
>      Tito.


------------------------------------------------------------------------------
Come build with us! The BlackBerry(R) Developer Conference in SF, CA
is the only developer event you need to attend this year. Jumpstart your
developing skills, take BlackBerry mobile applications to market and stay
ahead of the curve. Join us from November 9 - 12, 2009. Register now!
http://p.sf.net/sfu/devconference
_______________________________________________
QuantLib-dev mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/quantlib-dev
Reply | Threaded
Open this post in threaded view
|

Re: QuantLib-SWING: is it possible to use polymorphism?

Luigi Ballabio
In reply to this post by andrea-110
On Thu, 2009-10-29 at 17:59 +0000, Andrea wrote:
> > There would be quite a bit of work to do.  You would have to:
> > - add a constructor to boost::shared_ptr in common.i so that it takes a
> > pointer;
>
> I've got a big doubt here.
> Is it safe to stick the raw pointer into a shared_ptr? Is it already owned by some other swig mechanism?

You're right, the shared_ptr would take ownership against SWIG.
I don't see any easy solution to this one...

Another possibility would be to write a C++ payoff that adapts a Java
payoff. Its constructor would take a Java instance and store it, and its
operator() would use the foreign interface to call the corresponding
method in the Java instance. Kind of a home-made director. I don't know
if it would be possible to hide the mechanism so that it seems that the
Java class inherits from Payoff, though...

Luigi


--

There's no sense in being precise when you don't even know what you're
talking about.
-- John von Neumann



------------------------------------------------------------------------------
Come build with us! The BlackBerry(R) Developer Conference in SF, CA
is the only developer event you need to attend this year. Jumpstart your
developing skills, take BlackBerry mobile applications to market and stay
ahead of the curve. Join us from November 9 - 12, 2009. Register now!
http://p.sf.net/sfu/devconference
_______________________________________________
QuantLib-dev mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/quantlib-dev