adagsl_335d13f0/examples/deriv/src/deriv.adb

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
with System;
with Ada.Text_IO;            use Ada.Text_IO;
with Ada.Text_IO.C_Streams;
with Ada.Long_Float_Text_IO; use Ada.Long_Float_Text_IO;
with Ada.Integer_Text_IO;    use Ada.Integer_Text_IO;
with Interfaces.C;           use Interfaces.C;
with Interfaces.C.Strings;   use Interfaces.C.Strings;
with Ada.Strings.Unbounded;  use Ada.Strings.Unbounded;
with Ada.Command_Line;       use Ada.Command_Line;
with Ada.Numerics.Elementary_Functions ; use Ada.Numerics.Elementary_Functions ;

with GNAT.Source_Info; use GNAT.Source_Info;
with gsl;
with gsl.math ;
with gsl.deriv ;

with fns ;

procedure Deriv is
   use gsl.double_text_Io;
   use gsl.double_elementary_functions ;
   Verbose : boolean := True ;
   Status : Int ;
   procedure Test1 is
      myname : String := gnat.Source_Info.enclosing_entity ;
      logfile : File_Type ;
      f : aliased gsl.math.gsl_function ;
      result, abserror : aliased double ;

   begin
      if Verbose
      then
         Put_Line(myname);
      end if ;
      f.c_function := fns.Ex1'access ;
      f.params := System.Null_Address ;
      Put_Line("X = 2.0");
      Status := gsl.deriv.central(f'Access,2.0,1.0e-8,result'access,abserror'access);
      Put("Result = "); Put( result ) ; Put(" Abserror "); Put(abserror ) ; New_Line ;
      Put("Expected = "); Put(1.5 * sqrt(double(2.0)) ); New_Line;
      Put_Line("X = 0.0");
      Status := gsl.deriv.forward(f'Access,0.0,1.0e-8,result'access,abserror'access);
      Put("Result = "); Put( result ) ; Put(" Abserror "); Put(abserror ) ; New_Line ;
   end Test1 ;

   procedure Test2 is
      myname : String := gnat.Source_Info.enclosing_entity ;
      logfile : File_Type ;
      x,y : aliased double ;
      result, abserror : aliased double ;
      f : aliased gsl.math.gsl_function ;

   begin
      if Verbose
      then
         Put_Line(myname);
      end if ;
      Create(logfile,Out_File,myname & ".csv");
      Set_Output(logfile);
      f.c_function := fns.Tangent'Access ;
      f.params := System.Null_Address ;
      x := 0.0 ;
      loop
         Put(x) ; Put(" ; ");
         y := fns.Tangent(x,System.Null_Address) ; 
         Put(y) ; Put( " ; ") ;
         Status := gsl.deriv.central(f'Access,x,1.0e-8,result'access,abserror'access);
         Put( result ) ; Put( " ; ");
         Put( abserror ) ; Put( " ; ");
         New_Line;
         if x > Ada.Numerics.Pi - 0.01
         then
            exit ;
         end if ;
         x := x + 0.01 ;
      end loop ;
      Set_Output(Standard_Output);
      Close(logfile);
      Status := gsl.deriv.forward(f'Access,Ada.Numerics.Pi/2.0,1.0e-8,result'access,abserror'access);
      Put("Forward limit "); Put(result) ;
      Status := gsl.deriv.backward(f'Access,Ada.Numerics.Pi/2.0,1.0e-8,result'access,abserror'access);
      Put(" Backward "); Put(result);
      New_Line;
   end Test2 ;

begin
   if Verbose
   then
      Put_line(Gnat.Source_Info.enclosing_entity);
   end if;
   gsl.double_text_Io.Default_exp := 0;
   Test1 ;
   Test2 ;

end Deriv;