# Standard library: Numerics¶

The standard library provides support for common numeric operations on floating-point types as well as on complex types and matrices. In the sections below, we present a brief introduction to these numeric operations.

## Elementary Functions¶

The `Ada.Numerics.Elementary_Functions`

package provides common
operations for floating-point types, such as square root, logarithm,
and the trigonometric functions (e.g., sin, cos). For example:

Here we use the standard `e`

and `Pi`

constants from the
`Ada.Numerics`

package.

The `Ada.Numerics.Elementary_Functions`

package provides operations
for the `Float`

type. Similar packages are available for
`Long_Float`

and `Long_Long_Float`

types. For example, the
`Ada.Numerics.Long_Elementary_Functions`

package offers the same set
of operations for the `Long_Float`

type. In addition, the
`Ada.Numerics.Generic_Elementary_Functions`

package is a generic
version of the package that you can instantiate for custom floating-point
types. In fact, the `Elementary_Functions`

package can be defined as
follows:

```
package Elementary_Functions is new
Ada.Numerics.Generic_Elementary_Functions (Float);
```

## Random Number Generation¶

The `Ada.Numerics.Float_Random`

package provides a simple random
number generator for the range between 0.0 and 1.0. To use it, declare a
generator `G`

, which you pass to `Random`

. For example:

The standard library also includes a random number generator for discrete
numbers, which is part of the `Ada.Numerics.Discrete_Random`

package.
Since it's a generic package, you have to instantiate it for the desired
discrete type. This allows you to specify a range for the generator. In the
following example, we create an application that displays random integers
between 1 and 10:

Here, package `R`

is instantiated with the `Random_Range`

type,
which has a constrained range between 1 and 10. This allows us to
control the range used for the random numbers. We could easily modify
the application to display random integers between 0 and 20 by
changing the specification of the `Random_Range`

type. We can also
use floating-point or fixed-point types.

## Complex Types¶

The `Ada.Numerics.Complex_Types`

package provides support for complex
number types and the `Ada.Numerics.Complex_Elementary_Functions`

package provides support for common operations on complex number types,
similar to the `Ada.Numerics.Elementary_Functions`

package. Finally,
you can use the `Ada.Text_IO.Complex_IO`

package to perform I/O
operations on complex numbers. In the following example, we declare
variables of the `Complex`

type and initialize them using an aggregate:

As we can see from this example, all the common operators, such as `*`

and `+`

, are available for complex types. You also have typical
operations on complex numbers, such as `Argument`

and `Exp`

. In
addition to initializing complex numbers in the cartesian form using
aggregates, you can do so from the polar form by calling the
`Compose_From_Polar`

function.

The `Ada.Numerics.Complex_Types`

and
`Ada.Numerics.Complex_Elementary_Functions`

packages provide
operations for the `Float`

type. Similar packages are available for
`Long_Float`

and `Long_Long_Float`

types. In addition, the
`Ada.Numerics.Generic_Complex_Types`

and
`Ada.Numerics.Generic_Complex_Elementary_Functions`

packages are
generic versions that you can instantiate for custom or pre-defined
floating-point types. For example:

```
with Ada.Numerics.Generic_Complex_Types;
with Ada.Numerics.Generic_Complex_Elementary_Functions;
with Ada.Text_IO.Complex_IO;
procedure Show_Elem_Math is
package Complex_Types is new
Ada.Numerics.Generic_Complex_Types (Float);
use Complex_Types;
package Elementary_Functions is new
Ada.Numerics.Generic_Complex_Elementary_Functions (Complex_Types);
use Elementary_Functions;
package C_IO is new Ada.Text_IO.Complex_IO (Complex_Types);
use C_IO;
X, Y : Complex;
R, Th : Float;
```

## Vector and Matrix Manipulation¶

The `Ada.Numerics.Real_Arrays`

package provides support for
vectors and matrices. It includes common matrix operations such as
inverse, determinant, eigenvalues in addition to simpler operators
such as matrix addition and multiplication. You can declare vectors
and matrices using the `Real_Vector`

and `Real_Matrix`

types,
respectively.

The following example uses some of the operations from the
`Ada.Numerics.Real_Arrays`

package:

Matrix dimensions are automatically determined from the aggregate used for initialization when you don't specify them. You can, however, also use explicit ranges. For example:

```
M1 : Real_Matrix (1 .. 2, 1 .. 3) :=
((1.0, 5.0, 1.0),
(2.0, 2.0, 1.0));
```

The `Ada.Numerics.Real_Arrays`

package implements operations for the
`Float`

type. Similar packages are available for `Long_Float`

and
`Long_Long_Float`

types. In addition, the
`Ada.Numerics.Generic_Real_Arrays`

package is a generic version that
you can instantiate with custom floating-point types. For example, the
`Real_Arrays`

package can be defined as follows:

```
package Real_Arrays is new
Ada.Numerics.Generic_Real_Arrays (Float);
```