adagsl_335d13f0/examples/chebyshev/src/chebyshev.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
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
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.version ;
with gsl.math ;
with gsl.chebyshev ;

with signals ;

procedure Chebyshev is
   use gsl.double_text_Io ;
   Verbose : boolean := True ;
   Status : Int ;

   procedure Test1 is
   -- 32.6 Examples - Step Function
      myname : String := gnat.Source_Info.enclosing_entity ;
      logfile : File_Type ;
      N : constant := 1000 ;
      order : constant := 40 ;
      ch : access gsl.chebyshev.gsl_cheb_series 
            := gsl.chebyshev.alloc(order);
      fn : aliased gsl.math.gsl_function ;
      r10, r40 : double ;
      x : double ;
   begin
      if Verbose
      then
         Put_Line(myname);
      end if ;
      fn.c_function := signals.step'access ;
      fn.params := System.Null_Address ;
      Status := gsl.chebyshev.init(ch,fn'access,0.0,1.0);
      Create(logfile,Out_File,myname & ".csv");
      Set_Output(logfile);
      for i in 1..N
      loop
         x := double(i-1)/double(N);
         r10 := gsl.chebyshev.eval_n(ch,10,x);
         r40 := gsl.chebyshev.eval(ch,x);
         Put( x ); Put(" ; ");
         Put( signals.step(x,System.Null_Address) ); Put(" ; ");
         Put( r10 ); Put(" ; ");
         Put( r40 ); Put(" ; ");
         New_Line ;
      end loop ;
      Set_Output(Standard_Output);
      Close(logfile);

      gsl.chebyshev.free(ch);
   end Test1 ;

   procedure Test2 is
   -- Extend. Square Wave + other functions.
      myname : String := gnat.Source_Info.enclosing_entity ;
      logfile : File_Type ;
      N : constant := 1000 ;
      order : constant := 100 ;
      ch : access gsl.chebyshev.gsl_cheb_series 
            := gsl.chebyshev.alloc(order);
      fn : aliased gsl.math.gsl_function ;
      r10, r100 : aliased double ;
      r10_err, r100_err : aliased double ;
      x : double ;
   begin
      if Verbose
      then
         Put_Line(myname);
      end if ;
      fn.c_function := signals.square'access ;
      fn.params := System.Null_Address ;
      Status := gsl.chebyshev.init(ch,fn'access,0.0,1.0);
      Create(logfile,Out_File,myname & ".csv");
      Set_Output(logfile);
      for i in 1..N
      loop
         x := double(i-1)/double(N);
         Status := gsl.chebyshev.eval_n_err(ch,10,x,r10'Access, r10_err'access);
         Status := gsl.chebyshev.eval_err(ch,x,r100'Access,r100_err'Access);
         Put( x ); Put(" ; ");
         Put( signals.square(x,System.Null_Address) ); Put(" ; ");
         Put( r10 ); Put(" ; ");
         Put( r100 ); Put(" ; ");
         Put( r10_err ); Put(" ; ");
         Put( r100_err ); Put(" ; ");
         New_Line ;
      end loop ;
      Set_Output(Standard_Output);
      Close(logfile);

      gsl.chebyshev.free(ch);
   end Test2 ;

   procedure Test3 is
   -- Extend. Square Wave 
   --    Derivative and Integrals
      myname : String := gnat.Source_Info.enclosing_entity ;
      logfile : File_Type ;
      N : constant := 1000 ;
      order : constant := 100 ;
      ch : access gsl.chebyshev.gsl_cheb_series 
            := gsl.chebyshev.alloc(order);
      chderiv : access gsl.chebyshev.gsl_cheb_series 
         := gsl.chebyshev.alloc(order);
      chint : access gsl.chebyshev.gsl_cheb_series 
         := gsl.chebyshev.alloc(order);
      fn : aliased gsl.math.gsl_function ;
      r10, r100 : aliased double ;
      x : double ;
   begin
      if Verbose
      then
         Put_Line(myname);
      end if ;
      fn.c_function := signals.square'access ;
      fn.params := System.Null_Address ;
      Status := gsl.chebyshev.init(ch,fn'access,0.0,1.0);
      Status := gsl.chebyshev.calc_deriv(chderiv,ch);
      Status := gsl.chebyshev.calc_integ(chint,ch);

      Create(logfile,Out_File,myname & ".csv");
      Set_Output(logfile);
      for i in 1..N
      loop
         x := double(i-1)/double(N);
         r100 := gsl.chebyshev.eval(ch,x);
         Put(x) ; Put(" ; ");
         Put(signals.square(x,System.Null_Address)); Put(" ; ");
         Put(r100); Put(" ; ");
         r100 := gsl.chebyshev.eval(chderiv,x);
         Put(r100); Put(" ; ");
         r100 := gsl.chebyshev.eval(chint,x);
         Put(r100); Put(" ; ");
         New_Line;
      end loop ;
      Set_Output(Standard_Output);
      Close(logfile);

      gsl.chebyshev.free(ch);
   end Test3 ;

begin
   if Verbose
   then
      Put_Line(gnat.Source_Info.File);
      gsl.version.Report ;
   end if;
   Test1 ;
   Test2 ;
   Test3 ;
end Chebyshev;