[comp.lang.c] Calling IMSL routines

koft@elbereth.rutgers.edu (Dan Koft) (05/29/91)

Please help me if you can.  I have a C program that I want to call an
IMSL routine called QDAGI, the code in FORTRAN looks like:

	CALL QDAGI(F, BOUND, INTERV, ERRABS, ERRREL, RESULT, ERREST)

Where (from the IMSL manual):
F      - User supplied function to be integrated.  The form is F(X),
	 where: X - Independant variable (Input), F - The function
	 value (Output).  F must be declared EXTERNAL in the calling
	 program.
BOUND  - Finite bound of the integration range (Input). Ignored if
	 INTERV=2.
INTERV - Flag indicating integration range (Input). If INTERV is -1
	 the interval is -infinity to BOUND; if 1, BOUND to +infinity;
	 if 2 -infinity to +infinity.
ERRABS - Absolute accuracy desired (Input).
ERRREL - Relative accuracy desired (Input).
RESULT - Estimate of the integral from A to B of F (Output).
ERREST - Estimate of the absolute value of the error (Output).

I have tried all the ways I could think of to call this function
without proper results.  Below is the test program from the IMSL
manual as I have translated it to C.

#include <stdio.h>
#include <math.h>
#define Abs(x) ( (x)>0? (x) : (-x))
double F(double x);
 
extern void qdagi(double *F(), double *bound, int *interv, double *errabs,
                  double *errel, double *result, double *errest);
main()
{ 
double abs,bound,errabs,errest,error,errrel,exact,ppi,result;

int interv = 1;
bound = 0.0;
errabs = 0.0;
errrel = 0.001;

qdagi((&)F(),&bound,&interv,&errabs,&errrel,&result,&errest);
/* The above line is where I believe the problem lies*/

ppi = 3.1415926;
exact = -ppi * log(10.)/20.;
error = Abs(result - exact);
printf(" Computed = %8.3f           exact = %8.3f\nError ", result, exact);
printf("estimate = %f error = %f\n", errest, error);
}
 
double F(double x)
{
  double temp;
  temp = log(x)/(1. + pow((10. * x),2.0));
  return temp;
}
 
-- 

[=============================================================================]
Dan Koft				Email: koft@elbereth.rutgers.edu
RU-CS, Rutgers University		      {backbone}!rutgers!elbereth!koft
SERC, Busch Campus			Phone: (908) 932-3216
Piscataway, NJ  08855
[=============================================================================]

randolph@tuna.ssd.kodak.com (Gary L. Randolph) (05/30/91)

In article <May.29.12.00.35.1991.16903@elbereth.rutgers.edu> koft@elbereth.rutgers.edu (Dan Koft) writes:

>Please help me if you can.  I have a C program that I want to call an
>IMSL routine called QDAGI, the code in FORTRAN looks like:

>	CALL QDAGI(F, BOUND, INTERV, ERRABS, ERRREL, RESULT, ERREST)

...stuff deleted...

>I have tried all the ways I could think of to call this function
>without proper results.  Below is the test program from the IMSL
>manual as I have translated it to C.

>#include <stdio.h>
>#include <math.h>
>#define Abs(x) ( (x)>0? (x) : (-x))
>double F(double x);
 
>extern void qdagi(double *F(), double *bound, int *interv, double *errabs,
>                  double *errel, double *result, double *errest);
**********************^^^^^^^^^^^^^^^^^^^^****************

First, your declaration of the formal argument, F, is incorrect.

What you want is to declare F as pointer to function taking/returning double.

extern void qdagi(double (*F)(double), ...rest as above)

The parentheses are NOT optional!

>main()
>{ 
>double abs,bound,errabs,errest,error,errrel,exact,ppi,result;

>int interv = 1;
>bound = 0.0;
>errabs = 0.0;
>errrel = 0.001;

>qdagi((&)F(),&bound,&interv,&errabs,&errrel,&result,&errest);
>/* The above line is where I believe the problem lies*/

Yes, this is a problem. To pass the address of the function, you can pass
either F or &F (the difference has recently been discussed here or .c++)

So, the call would be:

qdagi(F, ...same as before);


To provide a simpler, yet silly, example:

double f(double x){ printf("%s %lf", "\nThe double: ", x); return x;}

void fun(double (*F)(double), double a){printf("%s %lf","\nThe DOUBLE: ",(*F)(a));}

main(){ fun(f,55);}

which results in:

The double: 55.000000
The DOUBLE: 55.000000

Gary Randolph
Eastman Kodak Company
.
.
.
.
.
.
.
.

khb@chiba.Eng.Sun.COM (Keith Bierman fpgroup) (05/30/91)

>calling IMSL/fortran library from C ....

The folks at IMSL have a C product as well nowadays. In addition, I
beleive that in at least one of their copious documents they deal with
the interlanguage calling issues a bit (it is platform specific). You
may find some material of interest in the Sun FORTRAN User's guide on
this topic as well.
--
----------------------------------------------------------------
Keith H. Bierman    keith.bierman@Sun.COM| khb@chiba.Eng.Sun.COM
SMI 2550 Garcia 12-33			 | (415 336 2648)   
    Mountain View, CA 94043