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 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711
|
---
title: math
---
# The `stdlib_math` module
[TOC]
## Introduction
`stdlib_math` module provides general purpose mathematical functions.
## Procedures and Methods provided
<!-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -->
### `clip` function
#### Description
Returns a value which lies in the given interval [`xmin`, `xmax`] (interval is `xmin` and `xmax` inclusive) and is closest to the input value `x`.
#### Syntax
`res = ` [[stdlib_math(module):clip(interface)]] ` (x, xmin, xmax)`
#### Status
Experimental
#### Class
Elemental function.
#### Argument(s)
`x`: scalar of either `integer` or `real` type. This argument is `intent(in)`.
`xmin`: scalar of either `integer` or `real` type. This argument is `intent(in)`.
`xmax`: scalar of either `integer` or `real` type, which must be greater than or equal to `xmin`. This argument is `intent(in)`.
Note: All arguments must have same `type` and same `kind`.
#### Output value or Result value
The output is a scalar of `type` and `kind` same as to that of the arguments.
#### Examples
##### Example 1:
Here inputs are of type `integer` and kind `int32`
```fortran
{!example/math/example_clip_integer.f90!}
```
##### Example 2:
Here inputs are of type `real` and kind `sp`
```fortran
{!example/math/example_clip_real.f90!}
```
<!-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -->
### `swap` subroutine
#### Description
Swaps the values in `lhs` and `rhs`.
#### Syntax
`call` [[stdlib_math(module):swap(interface)]] ` (lhs, rhs)`
#### Status
Experimental
#### Class
Elemental subroutine.
#### Argument(s)
`lhs`: scalar or array of any of the intrinsic types `integer`, `real`, `complex`, `logical`, `character`, `string_type`, `bitset` type. This argument is `intent(inout)`.
`rhs`: scalar or array of any of the intrinsic types `integer`, `real`, `complex`, `logical`, `character`, `string_type`, `bitset` type. This argument is `intent(inout)`.
##### Note
All arguments must have same `type` and same `kind`.
**WARNING**: For fix size characters with different length, the `swap` subroutine will truncate the longest amongst `lhs` and `rhs`. To avoid truncation it is possible to pass a subsection of the string.
#### Examples
```fortran
{!example/math/example_math_swap.F90!}
```
### `gcd` function
#### Description
Returns the greatest common divisor of two integers.
#### Syntax
`res = ` [[stdlib_math(module):gcd(interface)]] ` (a, b)`
#### Status
Experimental
#### Class
Elemental function.
#### Argument(s)
`a`: One integer with `intent(in)` to get the divisor for.
`b`: Another integer with `intent(in)` to get the divisor for.
Note: All arguments must be integers of the same `kind`.
#### Output value or Result value
Returns an integer of the same `kind` as that of the arguments.
#### Examples
##### Example 1:
```fortran
{!example/math/example_gcd.f90!}
```
### `linspace` - Create a linearly spaced rank one array
#### Description
Returns a linearly spaced rank 1 array from [`start`, `end`]. Optionally, you can specify the length of the returned array by passing `n`.
#### Syntax
`res = ` [[stdlib_math(module):linspace(interface)]] ` (start, end [, n])`
#### Status
Experimental
#### Class
Pure function.
#### Argument(s)
`start`: Shall be scalar of any numeric type or kind. This argument is `intent(in)`.
`end`: Shall be the same `type` and `kind` as `start`. This argument is `intent(in)`.
`n`: Shall be an integer specifying the length of the output. This argument is `optional` and `intent(in)`.
#### Output value or Result value
The output is a rank 1 array whose length is either 100 (default value) or `n`.
If `n` == 1, return a rank 1 array whose only element is `end`.
If `n` <= 0, return a rank 1 array with length 0.
If `start`/`end` are `real` or `complex` types, the `result` will be of the same type and kind as `start`/`end`.
If `start`/`end` are `integer` types, the `result` will default to a `real(dp)` array.
#### Examples
##### Example 1:
Here inputs are of type `complex` and kind `dp`
```fortran
{!example/math/example_linspace_complex.f90!}
```
##### Example 2:
Here inputs are of type `integer` and kind `int16`, with the result defaulting to `real(dp)`.
```fortran
{!example/math/example_linspace_int16.f90!}
```
### `logspace` - Create a logarithmically spaced rank one array
#### Description
Returns a logarithmically spaced rank 1 array from [`base`^`start`, `base`^`end`]. The default size of the array is 50. Optionally, you can specify the length of the returned array by passing `n`. You can also specify the `base` used to compute the range (default 10).
#### Syntax
`res = ` [[stdlib_math(module):logspace(interface)]] ` (start, end [, n [, base]])`
#### Status
Experimental
#### Class
Pure function.
#### Argument(s)
`start`: Shall be a scalar of any numeric type. All kinds are supported for real and complex arguments. For integers, only the default kind is currently implemented. This argument is `intent(in)`.
`end`: Shall be the same `type` and `kind` as `start`. This argument is `intent(in)`.
`n`: Shall be an integer specifying the length of the output. This argument is `optional` and `intent(in)`.
`base` : Shall be a scalar of any numeric type. All kinds are supported for real and complex arguments. For integers, only the default kind is currently implemented. This argument is `optional` and `intent(in)`.
#### Output value or Result value
The output is a rank 1 array whose length is either 50 (default value) or `n`.
If `n` == 1, return a rank 1 array whose only element is `base`^`end`.
If `n` <= 0, return a rank 1 array with length 0
The `type` and `kind` of the output is dependent on the `type` and `kind` of the passed parameters.
For function calls where the `base` is not specified: `logspace(start, end)`/`logspace(start, end, n)`, the `type` and `kind` of
the output follows the same scheme as above for `linspace`.
>If `start`/`end` are `real` or `complex` types, the `result` will be the same type and kind as `start`/`end`.
>If `start`/`end` are integer types, the `result` will default to a `real(dp)` array.
For function calls where the `base` is specified, the `type` and `kind` of the result is in accordance with the following table:
| `start`/`end` | `n` | `base` | `output` |
| ------------- | --- | ------ | -------- |
| `real(KIND)` | `Integer` | `real(KIND)` | `real(KIND)` |
| " " | " " | `complex(KIND)` | `complex(KIND)` |
| " " | " " | `Integer` | `real(KIND)` |
| `complex(KIND)` | " " | `real(KIND)` | `complex(KIND)` |
| " " | " " | `complex(KIND)` | `complex(KIND)` |
| " " | " " | `Integer` | `complex(KIND)` |
| `Integer` | " " | `real(KIND)` | `real(KIND)` |
| " " | " " | `complex(KIND)` | `complex(KIND)` |
| " " | " " | `Integer` | `Integer` |
#### Examples
##### Example 1:
Here inputs are of type `complex` and kind `dp`. `n` and `base` is not specified and thus default to 50 and 10, respectively.
```fortran
{!example/math/example_logspace_complex.f90!}
```
##### Example 2:
Here inputs are of type `integer` and default kind. `base` is not specified and thus defaults to 10.
```fortran
{!example/math/example_logspace_int.f90!}
```
##### Example 3:
Here `start`/`end` are of type `real` and double precision. `base` is type `complex` and also double precision.
```fortran
{!example/math/example_logspace_rstart_cbase.f90!}
```
### `arange` function
#### Status
Experimental
#### Class
Pure function.
#### Description
Creates a rank-1 `array` of the `integer/real` type with fixed-spaced values of given spacing, within a given interval.
#### Syntax
`result = ` [[stdlib_math(module):arange(interface)]] `(start [, end, step])`
#### Arguments
All arguments should be the same type and kind.
`start`: Shall be an `integer/real` scalar.
This is an `intent(in)` argument.
The default `start` value is `1`.
`end`: Shall be an `integer/real` scalar.
This is an `intent(in)` and `optional` argument.
The default `end` value is the inputted `start` value.
`step`: Shall be an `integer/real` scalar and large than `0`.
This is an `intent(in)` and `optional` argument.
The default `step` value is `1`.
##### Warning
If `step = 0`, the `step` argument will be corrected to `1/1.0` by the internal process of the `arange` function.
If `step < 0`, the `step` argument will be corrected to `abs(step)` by the internal process of the `arange` function.
#### Return value
Returns a rank-1 `array` of fixed-spaced values.
For `integer` type arguments, the length of the result vector is `(end - start)/step + 1`.
For `real` type arguments, the length of the result vector is `floor((end - start)/step) + 1`.
#### Example
```fortran
{!example/math/example_math_arange.f90!}
```
### `arg` function
#### Status
Experimental
#### Class
Elemental function.
#### Description
`arg` computes the phase angle (radian version) of `complex` scalar in the interval (-π,π].
The angles in `θ` are such that `z = abs(z)*exp((0.0, θ))`.
#### Syntax
`result = ` [[stdlib_math(module):arg(interface)]] `(z)`
#### Arguments
`z`: Shall be a `complex` scalar/array.
This is an `intent(in)` argument.
#### Return value
Returns the `real` type phase angle (radian version) of the `complex` argument `z`.
Notes: Although the angle of the complex number `0` is undefined, `arg((0,0))` returns the value `0`.
#### Example
```fortran
{!example/math/example_math_arg.f90!}
```
### `argd` function
#### Status
Experimental
#### Class
Elemental function.
#### Description
`argd` computes the phase angle (degree version) of `complex` scalar in the interval (-180.0,180.0].
The angles in `θ` are such that `z = abs(z)*exp((0.0, θ*π/180.0))`.
#### Syntax
`result = ` [[stdlib_math(module):argd(interface)]] `(z)`
#### Arguments
`z`: Shall be a `complex` scalar/array.
This is an `intent(in)` argument.
#### Return value
Returns the `real` type phase angle (degree version) of the `complex` argument `z`.
Notes: Although the angle of the complex number `0` is undefined, `argd((0,0))` returns the value `0`.
#### Example
```fortran
{!example/math/example_math_argd.f90!}
```
### `argpi` function
#### Status
Experimental
#### Class
Elemental function.
#### Description
`argpi` computes the phase angle (IEEE circular version) of `complex` scalar in the interval (-1.0,1.0].
The angles in `θ` are such that `z = abs(z)*exp((0.0, θ*π))`.
#### Syntax
`result = ` [[stdlib_math(module):argpi(interface)]] `(z)`
#### Arguments
`z`: Shall be a `complex` scalar/array.
This is an `intent(in)` argument.
#### Return value
Returns the `real` type phase angle (circular version) of the `complex` argument `z`.
Notes: Although the angle of the complex number `0` is undefined, `argpi((0,0))` returns the value `0`.
#### Example
```fortran
{!example/math/example_math_argpi.f90!}
```
### `deg2rad`
#### Status
Experimental
#### Class
Elemenal function.
#### Description
`deg2rad` converts phase angles from degrees to radians.
#### Syntax
`result = ` [[stdlib_math(module):deg2rad(interface)]] `(theta)`
#### Arguments
`theta`: Shall be a `real` scalar/array.
#### Return value
Returns the `real` phase angle in radians.
#### Example
```fortran
{!example/math/example_math_deg2rad.f90!}
```
### `rad2deg`
#### Status
Experimental
#### Class
Elemenal function.
#### Description
`rad2deg` converts phase angles from radians to degrees.
#### Syntax
`result = ` [[stdlib_math(module):rad2deg(interface)]] `(theta)`
#### Arguments
`theta`: Shall be a `real` scalar/array.
#### Return value
Returns the `real` phase angle in degrees.
#### Example
```fortran
{!example/math/example_math_rad2deg.f90!}
```
### `is_close` function
#### Description
Returns a boolean scalar/array where two scalars/arrays are element-wise equal within a tolerance.
```fortran
!> For `real` type
is_close(a, b, rel_tol, abs_tol) = abs(a - b) <= max(rel_tol*(abs(a), abs(b)), abs_tol)
!> and for `complex` type
is_close(a, b, rel_tol, abs_tol) = is_close(a%re, b%re, rel_tol, abs_tol) .and. &
is_close(a%im, b%im, rel_tol, abs_tol)
```
#### Syntax
`bool = ` [[stdlib_math(module):is_close(interface)]] ` (a, b [, rel_tol, abs_tol, equal_nan])`
#### Status
Experimental.
#### Class
Elemental function.
#### Arguments
Note: All `real/complex` arguments must have same `kind`.
If the value of `rel_tol/abs_tol` is negative (not recommended),
it will be corrected to `abs(rel_tol/abs_tol)` by the internal process of `is_close`.
`a`: Shall be a `real/complex` scalar/array.
This argument is `intent(in)`.
`b`: Shall be a `real/complex` scalar/array.
This argument is `intent(in)`.
`rel_tol`: Shall be a `real` scalar/array.
This argument is `intent(in)` and `optional`, which is `sqrt(epsilon(..))` by default.
`abs_tol`: Shall be a `real` scalar/array.
This argument is `intent(in)` and `optional`, which is `0.0` by default.
`equal_nan`: Shall be a `logical` scalar/array.
This argument is `intent(in)` and `optional`, which is `.false.` by default.
Whether to compare `NaN` values as equal. If `.true.`,
`NaN` values in `a` will be considered equal to `NaN` values in `b`.
#### Result value
Returns a `logical` scalar/array.
#### Example
```fortran
{!example/math/example_math_is_close.f90!}
```
### `all_close` function
#### Description
Returns a boolean scalar where two arrays are element-wise equal within a tolerance.
#### Syntax
`bool = ` [[stdlib_math(module):all_close(interface)]] ` (a, b [, rel_tol, abs_tol, equal_nan])`
#### Status
Experimental.
#### Class
Pure function.
#### Arguments
Note: All `real/complex` arguments must have same `kind`.
If the value of `rel_tol/abs_tol` is negative (not recommended),
it will be corrected to `abs(rel_tol/abs_tol)` by the internal process of `all_close`.
`a`: Shall be a `real/complex` array.
This argument is `intent(in)`.
`b`: Shall be a `real/complex` array.
This argument is `intent(in)`.
`rel_tol`: Shall be a `real` scalar.
This argument is `intent(in)` and `optional`, which is `sqrt(epsilon(..))` by default.
`abs_tol`: Shall be a `real` scalar.
This argument is `intent(in)` and `optional`, which is `0.0` by default.
`equal_nan`: Shall be a `logical` scalar.
This argument is `intent(in)` and `optional`, which is `.false.` by default.
Whether to compare `NaN` values as equal. If `.true.`,
`NaN` values in `a` will be considered equal to `NaN` values in `b`.
#### Result value
Returns a `logical` scalar.
#### Example
```fortran
{!example/math/example_math_all_close.f90!}
```
### `diff` function
#### Description
Computes differences between adjacent elements of an array.
#### Syntax
For a rank-1 array:
`y = ` [[stdlib_math(module):diff(interface)]] `(x [, n, prepend, append])`
and for a rank-2 array:
`y = ` [[stdlib_math(module):diff(interface)]] `(x [, n, dim, prepend, append])`
#### Status
Experimental.
#### Class
Pure function.
#### Arguments
`x`: The array to take a difference of.
Shall be a `real/integer` and `rank-1/rank-2` array.
This argument is `intent(in)`.
`n`: How many times to iteratively calculate the difference.
Shall be an `integer` scalar.
This argument is `intent(in)` and `optional`, and has value of `1` by default.
`dim`: The dimension of the input array along which to calculate the difference.
Its value must be between `1` and `rank(x)`.
Shall be an `integer` scalar.
This argument is `intent(in)` and `optional` and has a value of `1` by default.
`prepend`, `append`: Arrays to prepend or append to a along axis prior to performing the difference.
The dimension and shape must match a except along axis.
Shall be a `real/integer` and `rank-1/rank-2` array.
This argument is `intent(in)` and `optional`, which is no value by default.
Note:
- The `x`, `prepend` and `append` arguments must have the same `type`, `kind` and `rank`.
- If the value of `n` is less than or equal to `0` (which is not recommended), the return value of `diff` is `x`.
- If the value of `dim` is not equal to `1` or `2` (which is not recommended),
`1` will be used by the internal process of `diff`.
#### Result value
Returns the finite difference of the input array.
Shall be a `real/integer` and `rank-1/rank-2` array.
When both `prepend` and `append` are not present, the result `y` has one fewer element than `x` alongside the dimension `dim`.
#### Example
```fortran
{!example/math/example_diff.f90!}
```
### `meshgrid` subroutine
#### Description
Computes a list of coordinate matrices from coordinate vectors.
For $n \geq 1$ coordinate vectors $(x_1, x_2, ..., x_n)$ of sizes $(s_1, s_2, ..., s_n)$, `meshgrid` computes $n$ coordinate matrices $(X_1, X_2, ..., X_n)$ with identical shape corresponding to the selected indexing:
- Cartesian indexing (default behavior): the shape of the coordinate matrices is $(s_2, s_1, s_3, s_4, ... s_n)$.
- matrix indexing: the shape of the coordinate matrices is $(s_1, s_2, s_3, s_4, ... s_n)$.
#### Syntax
For a 2D problem in Cartesian indexing:
`call ` [[stdlib_math(module):meshgrid(interface)]] `(x, y, xm, ym)`
For a 3D problem in Cartesian indexing:
`call ` [[stdlib_math(module):meshgrid(interface)]] `(x, y, z, xm, ym, zm)`
For a 3D problem in matrix indexing:
`call ` [[stdlib_math(module):meshgrid(interface)]] `(x, y, z, xm, ym, zm, indexing="ij")`
The subroutine can be called in `n`-dimensional situations, as long as `n` is inferior to the maximum allowed array rank.
#### Status
Experimental.
#### Class
Subroutine.
#### Arguments
For a `n`-dimensional problem, with `n >= 1`:
`x1, x2, ..., xn`: The coordinate vectors.
Shall be `real/integer` and `rank-1` arrays.
These arguments are `intent(in)`.
`xm1, xm2, ..., xmn`: The coordinate matrices.
Shall be arrays of type `real` or `integer` of adequate shape:
- for Cartesian indexing, the shape of the coordinate matrices must be `[size(x2), size(x1), size(x3), ..., size(xn)]`.
- for matrix indexing, the shape of the coordinate matrices must be `[size(x1), size(x2), size(x3), ..., size(xn)]`.
These argument are `intent(out)`.
`indexing`: the selected indexing.
Shall be an `integer` equal to `stdlib_meshgrid_xy` for Cartesian indexing (default), or `stdlib_meshgrid_ij` for matrix indexing. `stdlib_meshgrid_xy` and `stdlib_meshgrid_ij` are public constants defined in the module.
This argument is `intent(in)` and `optional`, and is equal to `stdlib_meshgrid_xy` by default.
#### Example
```fortran
{!example/math/example_meshgrid.f90!}
```
|