Numerics: Elementary Functions, Complex, Matrices, Random & Big Numbers

Use Ada.Numerics constants and Elementary_Functions (Sqrt, trig with Cycle, Log with Base, **), complex types and functions, Real_Arrays (Solve, Determinant, Inverse, Eigenvalues), Float_Random and Discrete_Random with seeds, and Ada 2022 arbitrary-precision Big_Integers and Big_Reals.

Ada was designed for engineering and scientific software, and its numerics library reflects that: accurate elementary functions with defined error behavior, complex arithmetic, linear algebra on vectors and matrices, reproducible random numbers, and (since Ada 2022) arbitrary-precision integers and rationals.

Elementary and Complex Functions

Ada.Numerics defines Pi and e. Ada.Numerics.Elementary_Functions (for Float; generic Generic_Elementary_Functions for your own types) provides Sqrt, Log (X) and Log (X, Base), Exp, "**" with real exponents, Sin/Cos/Tan/Cot and their inverses, hyperbolic functions, and Arctan (Y, X). Trig functions accept a Cycle parameter, so Sin (30.0, Cycle => 360.0) works in degrees exactly. Invalid arguments (Sqrt (-1.0), Log (0.0)) raise Ada.Numerics.Argument_Error. Ada.Numerics.Complex_Types defines Complex with Re/Im, arithmetic, abs, Argument, Compose_From_Polar; Complex_Elementary_Functions provides complex Sqrt, Exp, Log, etc.

• Pi, e; Sqrt, Log (X, Base), Exp, **, trig with Cycle. • Argument_Error for invalid inputs. • Complex_Types + Complex_Elementary_Functions. • Generic_* versions for your own float types.

Real_Arrays and Random Numbers

Ada.Numerics.Real_Arrays defines Real_Vector and Real_Matrix with +, -, * (matrix product, inner product, scalar), Transpose, Solve (A, B) for linear systems, Inverse, Determinant, and Eigenvalues/Eigensystem for symmetric matrices; Complex_Arrays does the same for complex numbers. Ada.Numerics.Float_Random generates uniform floats in 0.0 .. 1.0; Discrete_Random (T) generates values of a discrete subtype. Reset (G) seeds from the clock; Reset (G, 42) gives a reproducible sequence — essential for tests and simulations. Each generator is an independent object, so tasks can each own one.

• Real_Arrays: Solve, Inverse, Determinant, Eigenvalues. • Float_Random / Discrete_Random (T); Reset (G, Seed) for reproducibility. • Generators are objects — one per task.

Big Numbers (Ada 2022)

Ada.Numerics.Big_Numbers.Big_Integers provides Big_Integer with the usual operators, To_Big_Integer, To_Integer, To_String and From_String; Big_Reals provides exact rationals. Ada 2022 also allows literals to be used for such types via the Integer_Literal aspect (Fact : Big_Integer := 1). They are ideal for cryptography, exact combinatorics, and reference implementations used to test fixed-size arithmetic.

• Big_Integer: unlimited size integers. • Big_Real: exact rationals. • User-defined literals make them convenient.

Code Examples

Solve finds x = 1, y = 3 for 2x + y = 5, x + 3y = 10. 30! needs 33 digits — far beyond 64-bit integers. The dice sequence is reproducible with seed 42 for this GNAT runtime (other compilers may produce a different but equally reproducible sequence). Output: Sqrt (2.0) = 1.41421 Pi = 3.14159 Sin (Pi / 6.0) = 0.50000 Sin (30, 360) = 0.50000 Log (e) = 1.00000 Log (1000, 10) = 3.00000 2.0 0.5 = 1.41421 Arctan (1, 1) = 0.78540 abs (3+4i) = 5.00000 (3+4i)2 = (-7.00000E+00, 2.40000E+01) Sqrt (-4+0i) = ( 0.00000E+00, 2.00000E+00) Solve: x = 1.00000 Solve: y = 3.00000 Determinant (M) = 5.00000 30! = 265252859812191058636308480000000 Dice (seed 42): 1 6 5 5 1 6 5 3 5 6 Sqrt (-1.0) raised Ada.Numerics.Argument_Error Sin (30.0) -- expecting degrees

Sin (30.0, Cycle => 360.0) -- or Sin (Pi / 6.0) Without Cycle, angles are in radians.

Dice.Reset (G); -- in a unit test Dice.Reset (G, 42);

Clock-based seeds make tests irreproducible.

Fact : Long_Long_Integer; -- for 30!

Fact : Big_Integer;

30! overflows 64-bit integers; use Big_Integers.

What does Log (1000.0, Base => 10.0) return? A. 6.9 B. 3.0 C. 1000.0 D. Argument_Error B. 3.0 (verified).

Which exception does Sqrt (-1.0) raise? A. Constraint_Error B. Ada.Numerics.Argument_Error C. Program_Error D. It returns NaN

How do you make a random sequence reproducible? A. Reset (G, Seed) B. Random (G, Seed) C. pragma Deterministic D. It's always reproducible

Which package solves a linear system A x = b? A. Ada.Numerics.Real_Arrays (Solve) B. Ada.Numerics.Linear C. Ada.Containers.Vectors D. Interfaces.LAPACK only Trace the numerics example.

Real output: Sqrt (2.0) = 1.41421 Pi = 3.14159 Sin (Pi / 6.0) = 0.50000 Sin (30, 360) = 0.50000 Log (e) = 1.00000 Log (1000, 10) = 3.00000 2.0 0.5 = 1.41421 Arctan (1, 1) = 0.78540 abs (3+4i) = 5.00000 (3+4i)2 = (-7.00000E+00, 2.40000E+01 ) Sqrt (-4+0i) = ( 0.00000E+00, 2.00000E+00 ) Solve: x = 1.00000 Solve: y = 3.00000 Determinant (M) = 5.00000 30! = 265252859812191058636308480000000 Dice (seed 42): 1 6 5 5 1 6 5 3 5 6 Sqrt (-1.0) raised Ada.Numerics.Argument_Error

with Ada.Text_IO; use Ada.Text_IO;
with Ada.Numerics;                                   use Ada.Numerics;
with Ada.Numerics.Elementary_Functions;              use Ada.Numerics.Elementary_Functions;
with Ada.Numerics.Complex_Types;                     use Ada.Numerics.Complex_Types;
with Ada.Numerics.Complex_Elementary_Functions;      use Ada.Numerics.Complex_Elementary_Functions;
with Ada.Numerics.Real_Arrays;                       use Ada.Numerics.Real_Arrays;
with Ada.Numerics.Discrete_Random;
with Ada.Numerics.Big_Numbers.Big_Integers;          use Ada.Numerics.Big_Numbers.Big_Integers;

procedure L49_Numerics is
   subtype Die is Integer range 1 .. 6;
   package Dice is new Ada.Numerics.Discrete_Random (Die);
   G : Dice.Generator;

   package F_IO is new Float_IO (Float);
   procedure Show (Label : String; X : Float) is
   begin
      Put (Label);
      F_IO.Put (X, Fore => 3, Aft => 5, Exp => 0);
      New_Line;
   end Show;

   Z : constant Complex := (Re => 3.0, Im => 4.0);
   M : constant Real_Matrix := [[2.0, 1.0],
                                [1.0, 3.0]];
   Rhs : constant Real_Vector := [5.0, 10.0];
   X : Real_Vector (1 .. 2);
   Fact : Big_Integer := 1;
begin
   Sh
…