comp.lang.ada
 help / color / mirror / Atom feed
* math: passing function to function
@ 2001-04-30 11:12 Staffan Dittmer
  2001-04-30 12:10 ` Jacob Sparre Andersen
                   ` (2 more replies)
  0 siblings, 3 replies; 5+ messages in thread
From: Staffan Dittmer @ 2001-04-30 11:12 UTC (permalink / raw)


I'm trying to implement a root finder using the Brent method
but find myself getting stuck. 
I cannot figure  out how to pass a function 
- the function to solve - as a parameter 
to  the solver.

I've tried using access to subprogram types to make something like
function solver(fun: access to function to solve; ...) return long_float;
which usually compiles ok, but exits raising 
constraint error as soon as the code gets more involved than the 
rudimentary examples given in the RM.

I would very much appreciate it if someone could point me to 
some mathematical Ada source code where things like the above 
are bound to be used. 
 
The latest line of thought is that it might be possible to
make a generic construction like

generic
function fun(X:float) return float renames function_to_solve;
function solver(...) return float is
begin 
......

but I have no greater hopes for this being even remotely possible.

Any other ideas?

/ Staffan Dittmer



^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: math: passing function to function
  2001-04-30 11:12 math: passing function to function Staffan Dittmer
@ 2001-04-30 12:10 ` Jacob Sparre Andersen
  2001-04-30 12:57 ` Marc A. Criley
  2001-04-30 14:38 ` Ted Dennison
  2 siblings, 0 replies; 5+ messages in thread
From: Jacob Sparre Andersen @ 2001-04-30 12:10 UTC (permalink / raw)


Staffan:

> I've tried using access to subprogram types to make something like
> function solver(fun: access to function to solve; ...) return long_float;
> which usually compiles ok, but exits raising
> constraint error as soon as the code gets more involved than the
> rudimentary examples given in the RM.

Strange. Have you found out where the constraint error is raised?
(in the "fun" or in "solver")

> I would very much appreciate it if someone could point me to
> some mathematical Ada source code where things like the above
> are bound to be used.

I think there should be an example in "Generic_Least_Squares" in
the collection you can find at:

   http://hugin.ldraw.org/Ada/matematik/

> The latest line of thought is that it might be possible to
> make a generic construction like
> 
> generic
> function fun(X:float) return float renames function_to_solve;
> function solver(...) return float is
> begin
> ......

Have a look at 12.1(22) and 12.1(24) in the LRM.

Jacob
-- 
"hackers have almost lost interest in Windows NT exploits
 because they're becoming so common"



^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: math: passing function to function
  2001-04-30 11:12 math: passing function to function Staffan Dittmer
  2001-04-30 12:10 ` Jacob Sparre Andersen
@ 2001-04-30 12:57 ` Marc A. Criley
  2001-04-30 13:51   ` Staffan Dittmer
  2001-04-30 14:38 ` Ted Dennison
  2 siblings, 1 reply; 5+ messages in thread
From: Marc A. Criley @ 2001-04-30 12:57 UTC (permalink / raw)


Staffan Dittmer wrote:
> 
> I'm trying to implement a root finder using the Brent method
> but find myself getting stuck.
> I cannot figure  out how to pass a function
> - the function to solve - as a parameter
> to  the solver.
> 
> I've tried using access to subprogram types to make something like
> function solver(fun: access to function to solve; ...) return long_float;
> which usually compiles ok, but exits raising
> constraint error as soon as the code gets more involved than the
> rudimentary examples given in the RM.

The only problems I've ever experienced with passing around access
subprograms have been self-inflicted :-)  I _strongly_ suspect that the
problem lies in the implementation of either the "solver" or the
"function to solve".

Are any global variables being referenced that may not be getting set? 
Or reset between iterations?  Are you sure that the function is being
invoked only with valid values--i.e., that that is not causing a
Constraint_Error?

If you're using GNAT, compile the code with the -g option, then link it
was -largs -gnatE so that you can get an exception traceback.  Then use
addr2line to identify exactly which line is raising the exception.  Or
run it in a debugger and simply "break exception" and look at the
location and values that triggered the situation.

Good luck.

Marc A. Criley
Senior Staff Engineer
Quadrus Corporation
www.quadruscorp.com



^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: math: passing function to function
  2001-04-30 12:57 ` Marc A. Criley
@ 2001-04-30 13:51   ` Staffan Dittmer
  0 siblings, 0 replies; 5+ messages in thread
From: Staffan Dittmer @ 2001-04-30 13:51 UTC (permalink / raw)


Thanks for replies.
Giving up on the access to subprogram approach for now
since I've had better success with the generic approach.

This seems to work the way I want it, also probably less 
prone to self induced constraint errors ...
 
package Root_Pak is
   generic
   with function Fun(X:Float) return Float;
   function Root(X1,X2:Float) return Float;
end Root_Pak;

package Fun_Pak is
   function To_Solve(X:Float) return Float;
end Fun_Pak;

with Text_Io,Fun_Pak,Root_pak;
use Text_Io;
procedure calc is
   function Solve is new Root_Pak.root(Fun_Pak.To_Solve);
   X1,X2:Float:=4.0;
begin
   Put_Line(Float'Image(Solve(X1,X2)));
end calc;

/ Staffan



^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: math: passing function to function
  2001-04-30 11:12 math: passing function to function Staffan Dittmer
  2001-04-30 12:10 ` Jacob Sparre Andersen
  2001-04-30 12:57 ` Marc A. Criley
@ 2001-04-30 14:38 ` Ted Dennison
  2 siblings, 0 replies; 5+ messages in thread
From: Ted Dennison @ 2001-04-30 14:38 UTC (permalink / raw)


In article <9cjham$lu4$1@eol.dd.chalmers.se>, Staffan Dittmer says...
>
>I've tried using access to subprogram types to make something like
>function solver(fun: access to function to solve; ...) return long_float;
>which usually compiles ok, but exits raising 
>constraint error as soon as the code gets more involved than the 
>rudimentary examples given in the RM.

It was probably a real live Constraint_Error. That happens. Find the source, and
fix it.

If things are getting confusing for you, you might want to try throuroughly
testing the "fun" function before attempting to pass it to "solver". Make sure
to test around the boundries of all the types you use.

---
T.E.D.    homepage   - http://www.telepath.com/dennison/Ted/TED.html
          home email - mailto:dennison@telepath.com



^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2001-04-30 14:38 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2001-04-30 11:12 math: passing function to function Staffan Dittmer
2001-04-30 12:10 ` Jacob Sparre Andersen
2001-04-30 12:57 ` Marc A. Criley
2001-04-30 13:51   ` Staffan Dittmer
2001-04-30 14:38 ` Ted Dennison

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox