From: Barry Margolin on
In article <xbaid3vrpj2d.fsf(a)cam.ac.uk>, Leo <sdl.web(a)gmail.com> wrote:

> On 2010-06-16 05:40 +0100, Barry Margolin wrote:
> > Define a class fsolve-1-er, and make this a superclass of both
> > multi-dimensional-root-solver-f and multi-dimensional-root-solver-fdf.
> > Then define the method:
> >
> > (defmethod fsolve ((solver fsolve-1-er)
> > &key ...)
> > (fsolve-1 solver absolute-error max-iteration print-steps))
>
> The two classes are from a common lisp package named GSLL. I am trying
> to do things in my own package instead of modifying GSLL. Is that
> possible? Thanks.
>
> Leo

You could define your own subclasses of each of the provided classes,
that also inherit from fsolve-1-er:

(defclass my-multi-dimensional-root-solver-f
(fsolve-1-er multi-dimensional-root-solver-f))

(defclass my-multi-dimensional-root-solver-fdf
(fsolve-1-er multi-dimensional-root-solver-fdf))

--
Barry Margolin, barmar(a)alum.mit.edu
Arlington, MA
*** PLEASE post questions in newsgroups, not directly to me ***
*** PLEASE don't copy me on replies, I'll read them in the group ***
From: Peder O. Klingenberg on
Tim Bradshaw <tfb(a)tfeb.org> writes:

> If t1 and t2 are types then (OR t1 t2) is a type which is either t1 or
> t2.

Isn't that what I said in the paragraph you deleted, only more
verbosely?

....Peder...
--
This must be Thursday. I never could get the hang of Thursdays.
From: Tim Bradshaw on
On 2010-06-17 08:05:09 +0100, Peder O. Klingenberg said:

> Isn't that what I said in the paragraph you deleted, only more
> verbosely?

Yes, I can't read, sorry.

From: Pascal Costanza on
On 17/06/2010 01:48, Barry Margolin wrote:
> In article<xbaid3vrpj2d.fsf(a)cam.ac.uk>, Leo<sdl.web(a)gmail.com> wrote:
>
>> On 2010-06-16 05:40 +0100, Barry Margolin wrote:
>>> Define a class fsolve-1-er, and make this a superclass of both
>>> multi-dimensional-root-solver-f and multi-dimensional-root-solver-fdf.
>>> Then define the method:
>>>
>>> (defmethod fsolve ((solver fsolve-1-er)
>>> &key ...)
>>> (fsolve-1 solver absolute-error max-iteration print-steps))
>>
>> The two classes are from a common lisp package named GSLL. I am trying
>> to do things in my own package instead of modifying GSLL. Is that
>> possible? Thanks.
>>
>> Leo
>
> You could define your own subclasses of each of the provided classes,
> that also inherit from fsolve-1-er:
>
> (defclass my-multi-dimensional-root-solver-f
> (fsolve-1-er multi-dimensional-root-solver-f))
>
> (defclass my-multi-dimensional-root-solver-fdf
> (fsolve-1-er multi-dimensional-root-solver-fdf))

....and you could define methods on make-instance to reroute the existing
classes to your own:

(defmethod make-instance
((class-name (eql 'multi-dimensional-root-solver-f))
&rest initargs)
(apply #'make-instance
'my-multi-dimensional-root-solver-f initargs))

(defmethod make-instance
((class-name (eql 'multi-dimensional-root-solver-fdf))
&rest initargs)
(apply #'make-instance
'my-multi-dimensional-root-solver-fdf initargs))

This is not fully standard-compliant, and may not fully work in all
corner cases, but should be good enough for many practical purposes.

(If the CLOS MOP is available, there are other ways to achieve similar
effects.)


Pascal

--
My website: http://p-cos.net
Common Lisp Document Repository: http://cdr.eurolisp.org
Closer to MOP & ContextL: http://common-lisp.net/project/closer/
From: Scott L. Burson on
On Jun 15, 5:49 pm, Leo <sdl....(a)gmail.com> wrote:
> On 2010-06-15 20:32 +0100, Captain Obvious wrote:
>
>
>
> > L> But using change-class is a bad idea, because methods for the
> > L> superclasses stopping working on the new SOLVER class.
>
> > I think the bad idea was inheriting from both.
>
> > (defclass solver (multi-dimensional-root-solver-f
> > multi-dimensional-root-solver-fdf)()
>
> > Do you really mean that solver IS BOTH solver-f AND solver-fdf?
>
> > As far as I understand from your code solver might be EITHER solver-f
> > OR solver-fdf.
> > It is not expressed via inheritance relationship.
>
> > I don't really understand why you need your own solver class at all.
> > Just use one of stock ones.
>
> You are right. I was looking for a way to solve this dilemma:
>
> The routine (defined as fsolve-1 in the example code below) to find the
> solution is essentially the same for both classes and I want to avoid
> doing:
>
> (defmethod fsolve
>     ((solver multi-dimensional-root-solver-f)
>      &key (absolute-error 1d-7) (max-iteration 500) print-steps)
>   (fsolve-1 solver absolute-error max-iteration print-steps))
>
> (defmethod fsolve
>     ((solver multi-dimensional-root-solver-fdf)
>      &key (absolute-error 1d-7) (max-iteration 500) print-steps)
>   (fsolve-1 solver absolute-error max-iteration print-steps))
>
> -fdf and -f's have a common superclass that is also the superclass of
>  many other classes.

I think you should just do this. You're not duplicating actual code;
you're just forwarding both these methods to a common function. If
you had control over the class hierarchy you would obviously give -fdf
and -f their own common superclass, but since you don't have that
option, I think this is the best solution. (Disclaimer: I didn't read
the entire thread closely.)

-- Scott