5  Numerical Libraries

Ada features a strong predefined standard library covering a variety of disciplines. In particular the numerical libraries are a solid foundation for scientific and engineering computations. Combined with a comprehensive set of data structures lead to reliable applications.

In this chapter numerical libraries are surveyed.

5.1 Learning Objectives

  • Creating text files in comma (or :) separated format

  • Elementary numerical function library

  • Interfacing with a plotting tool through files

5.2 Sinusoid

Basic sinusoids are supported directly by the library.

Code Fragment

~/bin/codemd ../Prj/dtypes/src/curves.adb -x DegRad -l
0039 |    function Radians (d : Integer) return Float is
0040 |    begin
0041 |       return 2.0 * Ada.Numerics.Pi * Float (d) / 360.0;
0042 |    end Radians;
0043 |    function Degrees (r : Float) return Integer is
0044 |    begin
0045 |       return Integer (r * 360.0 / (2.0 * Ada.Numerics.Pi));
0046 |    end Degrees;

Functions as defined above leverage the basic constants such as pi and e defined in the libraries.

~/bin/codemd ../Prj/dtypes/src/curves.adb -x Trig -l
0060 |       CreateOutputFile (myname, outfile);
0061 |       for deg in 0 .. 360 loop
0062 |          Put (Float'(Radians (deg))'Image);
0063 |          Put (" ; ");
0064 |          Put (Float'(Elementary_Functions.Sin (Radians (deg)))'Image);
0065 |          Put (" ; ");
0066 |          Put (Float'(Elementary_Functions.Cos (Radians (deg)))'Image);
0067 |          Put (" ; ");
0068 |          Put (Float'(Elementary_Functions.Tan (Radians (deg)))'Image);
0069 |          New_Line;
0070 |       end loop;
0071 |       Close (outfile);

which produces a CSV file in several columns :

~/bin/curves trig
sed -n "10,20p" curves.Trigonometric.csv
curves curves.adb Compiled Jan 21 2025 07:20:27
curves.Trigonometric
 1.57080E-01 ;  1.56434E-01 ;  9.87688E-01 ;  1.58384E-01
 1.74533E-01 ;  1.73648E-01 ;  9.84808E-01 ;  1.76327E-01
 1.91986E-01 ;  1.90809E-01 ;  9.81627E-01 ;  1.94380E-01
 2.09440E-01 ;  2.07912E-01 ;  9.78148E-01 ;  2.12557E-01
 2.26893E-01 ;  2.24951E-01 ;  9.74370E-01 ;  2.30868E-01
 2.44346E-01 ;  2.41922E-01 ;  9.70296E-01 ;  2.49328E-01
 2.61799E-01 ;  2.58819E-01 ;  9.65926E-01 ;  2.67949E-01
 2.79253E-01 ;  2.75637E-01 ;  9.61262E-01 ;  2.86745E-01
 2.96706E-01 ;  2.92372E-01 ;  9.56305E-01 ;  3.05731E-01
 3.14159E-01 ;  3.09017E-01 ;  9.51057E-01 ;  3.24920E-01
 3.31613E-01 ;  3.25568E-01 ;  9.45519E-01 ;  3.44328E-01

which can be plotted using Julia

using CSV
using DataFrames
using Plots

df=DataFrames.DataFrame(CSV.File("curves.Trigonometric.csv",delim=";",header=false));
plot(df.Column1 , [df.Column2,df.Column3] ,
                  label=["Sine" "Cosine"] ,
                  title="Trignometric Functions" , show=true)

5.3 Hyperbolic

The numerical library supports hyperbolic functions as well:

~/bin/codemd ../Prj/dtypes/src/curves.adb -x Hyper -l
0087 |       CreateOutputFile (myname, outfile);
0088 |       for d in 0 .. 40 loop
0089 |          x := Float (d) * 0.1 - 2.0;
0090 |          Put (x'Image);
0091 |          Put (" ; ");
0092 |          Put (Elementary_Functions.Sinh (x), Fore => 3, Aft => 3, Exp => 0);
0093 |          Put (" ; ");
0094 |          Put (Elementary_Functions.Cosh (x), Fore => 3, Aft => 3, Exp => 0);
0095 |          Put (" ; ");
0096 |          Put (Elementary_Functions.Tanh (x), Fore => 3, Aft => 3, Exp => 0);
0097 |          Put (" ; ");
0098 |          New_Line;
0099 |       end loop;
0100 |       Close (outfile);
0101 |       Set_Output (Standard_Output);

In this fragment, it can be seen that the floating point variables are directly printed instead of using the built in Image attribute. Direct print then affords the control over the format of the print out. The output generated by the above fragment illustrates this:

~/bin/curves hyper
sed -n "10,20p" curves.Hyperbolic.csv
curves curves.adb Compiled Jan 21 2025 07:20:27
curves.Hyperbolic
-1.10000E+00 ;  -1.336 ;   1.669 ;  -0.800 ; 
-1.00000E+00 ;  -1.175 ;   1.543 ;  -0.762 ; 
-9.00000E-01 ;  -1.027 ;   1.433 ;  -0.716 ; 
-8.00000E-01 ;  -0.888 ;   1.337 ;  -0.664 ; 
-7.00000E-01 ;  -0.759 ;   1.255 ;  -0.604 ; 
-6.00000E-01 ;  -0.637 ;   1.185 ;  -0.537 ; 
-5.00000E-01 ;  -0.521 ;   1.128 ;  -0.462 ; 
-4.00000E-01 ;  -0.411 ;   1.081 ;  -0.380 ; 
-3.00000E-01 ;  -0.305 ;   1.045 ;  -0.291 ; 
-2.00000E-01 ;  -0.201 ;   1.020 ;  -0.197 ; 
-1.00000E-01 ;  -0.100 ;   1.005 ;  -0.100 ; 
using CSV
using DataFrames
using Plots

df=DataFrames.DataFrame(CSV.File("curves.Hyperbolic.csv",delim=";",header=false));
plot(df.Column1 , [df.Column2,df.Column3,df.Column4] ,
                  label=["Sinh" "Cosh" "Tanh"] ,
                  title="Hyperbolic Functions" , show=false)

5.4 Hypotrochoid

The curve hypotrochoid and its parameteric equations are found in Wikipedia

Code Fragment

~/bin/codemd ../Prj/dtypes/src/curves.adb -x Hypo -l
0123 |       for theta in 0 .. 3 * 360 loop
0124 |          x :=
0125 |            (fixedR - rollingR) * Cos (Radians (theta)) +
0126 |            d * Cos (Radians (theta) * (fixedR - rollingR) / rollingR);
0127 |          y :=
0128 |            (fixedR - rollingR) * Sin (Radians (theta)) -
0129 |            d * Sin (Radians (theta) * (fixedR - rollingR) / rollingR);
0130 |          Put (Float (Radians (theta))'Image);
0131 |          Put (" ; ");
0132 |          Put (x'Image);
0133 |          Put (" ; ");
0134 |          Put (y'Image);
0135 |          New_Line;
0136 |       end loop;
~/bin/curves troch
curves curves.adb Compiled Jan 21 2025 07:20:27
curves.Hypotrochoid
using CSV
using DataFrames
using Plots

df=DataFrames.DataFrame(CSV.File("curves.Hypotrochoid.csv",delim=";",header=false));
plot(df.Column2 , [df.Column3] ,
                  label=["hypotrochoid"] ,
                  title="Hypotrochoid" , show=false)

5.5 Stretch

  1. Step response in the time domain gives an overview of the step response in the time domain of a capacitor and a resistor. Given a circuit with a resistor and a capacitor in series, generate the voltages across each component when a DC voltage is applied in a CSV file to enable plotting.

5.6 Sample Implementation

Repository: TechAdaBook
Directory: Prj/dtypes