Skip to content

Commit 11bbb79

Browse files
committed
Merge branch 'master' of https://github.com/fortran-lang/stdlib into string-list-new
2 parents 3a9484b + 9fb85ff commit 11bbb79

15 files changed

+1198
-25
lines changed

doc/specs/stdlib_linalg.md

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,3 +168,41 @@ program demo_trace
168168
print *, trace(A) ! 1 + 5 + 9
169169
end program demo_trace
170170
```
171+
172+
## `outer_product` - Computes the outer product of two vectors
173+
174+
### Status
175+
176+
Experimental
177+
178+
### Description
179+
180+
Computes the outer product of two vectors
181+
182+
### Syntax
183+
184+
`d = [[stdlib_linalg(module):outer_product(interface)]](u, v)`
185+
186+
### Arguments
187+
188+
`u`: Shall be a rank-1 array
189+
190+
`v`: Shall be a rank-1 array
191+
192+
### Return value
193+
194+
Returns a rank-2 array equal to `u v^T` (where `u, v` are considered column vectors). The shape of the returned array is `[size(u), size(v)]`.
195+
196+
### Example
197+
198+
```fortran
199+
program demo_outer_product
200+
use stdlib_linalg, only: outer_product
201+
implicit none
202+
real, allocatable :: A(:,:), u(:), v(:)
203+
u = [1., 2., 3. ]
204+
v = [3., 4.]
205+
A = outer_product(u,v)
206+
!A = reshape([3., 6., 9., 4., 8., 12.], [3,2])
207+
end program demo_outer_product
208+
```

doc/specs/stdlib_quadrature.md

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -186,3 +186,95 @@ program demo_simps_weights
186186
! 64.0
187187
end program demo_simps_weights
188188
```
189+
190+
## `gauss_legendre` - Gauss-Legendre quadrature (a.k.a. Gaussian quadrature) nodes and weights
191+
192+
### Status
193+
194+
Experimental
195+
196+
### Description
197+
198+
Computes Gauss-Legendre quadrature (also known as simply Gaussian quadrature) nodes and weights,
199+
for any `N` (number of nodes).
200+
Using the nodes `x` and weights `w`, you can compute the integral of some function `f` as follows:
201+
`integral = sum(f(x) * w)`.
202+
203+
Only double precision is supported - if lower precision is required, you must do the appropriate conversion yourself.
204+
Accuracy has been validated up to N=64 by comparing computed results to tablulated values known to be accurate to machine precision
205+
(maximum difference from those values is 2 epsilon).
206+
207+
### Syntax
208+
209+
`subroutine [[stdlib_quadrature(module):gauss_legendre(interface)]] (x, w[, interval])`
210+
211+
### Arguments
212+
213+
`x`: Shall be a rank-one array of type `real(real64)`. It is an *output* argument, representing the quadrature nodes.
214+
215+
`w`: Shall be a rank-one array of type `real(real64)`, with the same dimension as `x`.
216+
It is an *output* argument, representing the quadrature weights.
217+
218+
`interval`: (Optional) Shall be a two-element array of type `real(real64)`.
219+
If present, the nodes and weigts are calculated for integration from `interval(1)` to `interval(2)`.
220+
If not specified, the default integral is -1 to 1.
221+
222+
### Example
223+
224+
```fortran
225+
program integrate
226+
use iso_fortran_env, dp => real64
227+
implicit none
228+
229+
integer, parameter :: N = 6
230+
real(dp), dimension(N) :: x,w
231+
call gauss_legendre(x,w)
232+
print *, "integral of x**2 from -1 to 1 is", sum(x**2 * w)
233+
end program
234+
```
235+
236+
## `gauss_legendre_lobatto` - Gauss-Legendre-Lobatto quadrature nodes and weights
237+
238+
### Status
239+
240+
Experimental
241+
242+
### Description
243+
244+
Computes Gauss-Legendre-Lobatto quadrature nodes and weights,
245+
for any `N` (number of nodes).
246+
Using the nodes `x` and weights `w`, you can compute the integral of some function `f` as follows:
247+
`integral = sum(f(x) * w)`.
248+
249+
Only double precision is supported - if lower precision is required, you must do the appropriate conversion yourself.
250+
Accuracy has been validated up to N=64 by comparing computed results to tablulated values known to be accurate to machine precision
251+
(maximum difference from those values is 2 epsilon).
252+
253+
### Syntax
254+
255+
`subroutine [[stdlib_quadrature(module):gauss_legendre_lobatto(interface)]] (x, w[, interval])`
256+
257+
### Arguments
258+
259+
`x`: Shall be a rank-one array of type `real(real64)`. It is an *output* argument, representing the quadrature nodes.
260+
261+
`w`: Shall be a rank-one array of type `real(real64)`, with the same dimension as `x`.
262+
It is an *output* argument, representing the quadrature weights.
263+
264+
`interval`: (Optional) Shall be a two-element array of type `real(real64)`.
265+
If present, the nodes and weigts are calculated for integration from `interval(1)` to `interval(2)`.
266+
If not specified, the default integral is -1 to 1.
267+
268+
### Example
269+
270+
```fortran
271+
program integrate
272+
use iso_fortran_env, dp => real64
273+
implicit none
274+
275+
integer, parameter :: N = 6
276+
real(dp), dimension(N) :: x,w
277+
call gauss_legendre_lobatto(x,w)
278+
print *, "integral of x**2 from -1 to 1 is", sum(x**2 * w)
279+
end program
280+
```

doc/specs/stdlib_specialfunctions.md

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
---
2+
title: specialfunctions
3+
---
4+
5+
# Special functions
6+
7+
[TOC]
8+
9+
## `legendre` - Calculate Legendre polynomials
10+
11+
### Status
12+
13+
Experimental
14+
15+
### Description
16+
17+
Computes the value of the n-th Legendre polynomial at a specified point.
18+
Currently only 64 bit floating point is supported.
19+
20+
This is an `elemental` function.
21+
22+
### Syntax
23+
24+
`result = [[stdlib_specialfunctions(module):legendre(interface)]] (n, x)`
25+
26+
### Arguments
27+
28+
`n`: Shall be a scalar of type `real(real64)`.
29+
30+
`x`: Shall be a scalar or array (this function is elemental) of type `real(real64)`.
31+
32+
### Return value
33+
34+
The function result will be the value of the `n`-th Legendre polynomial, evaluated at `x`.
35+
36+
37+
38+
## `dlegendre` - Calculate first derivatives of Legendre polynomials
39+
40+
### Status
41+
42+
Experimental
43+
44+
### Description
45+
46+
Computes the value of the first derivative of the n-th Legendre polynomial at a specified point.
47+
Currently only 64 bit floating point is supported.
48+
49+
This is an `elemental` function.
50+
51+
### Syntax
52+
53+
`result = [[stdlib_specialfunctions(module):dlegendre(interface)]] (n, x)`
54+
55+
### Arguments
56+
57+
`n`: Shall be a scalar of type `real(real64)`.
58+
59+
`x`: Shall be a scalar or array (this function is elemental) of type `real(real64)`.
60+
61+
### Return value
62+
63+
The function result will be the value of the first derivative of the `n`-th Legendre polynomial, evaluated at `x`.

src/CMakeLists.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ set(fppFiles
99
stdlib_io.fypp
1010
stdlib_linalg.fypp
1111
stdlib_linalg_diag.fypp
12+
stdlib_linalg_outer_product.fypp
1213
stdlib_optval.fypp
1314
stdlib_sorting.fypp
1415
stdlib_sorting_ord_sort.fypp
@@ -49,6 +50,9 @@ set(SRC
4950
stdlib_logger.f90
5051
stdlib_strings.f90
5152
stdlib_system.F90
53+
stdlib_specialfunctions.f90
54+
stdlib_specialfunctions_legendre.f90
55+
stdlib_quadrature_gauss.f90
5256
${outFiles}
5357
)
5458

src/Makefile.manual

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ SRCFYPP =\
66
stdlib_io.fypp \
77
stdlib_linalg.fypp \
88
stdlib_linalg_diag.fypp \
9+
stdlib_linalg_outer_product.fypp \
910
stdlib_optval.fypp \
1011
stdlib_quadrature.fypp \
1112
stdlib_quadrature_trapz.fypp \
@@ -29,8 +30,12 @@ SRCFYPP =\
2930

3031
SRC = f18estop.f90 \
3132
stdlib_error.f90 \
33+
stdlib_specialfunctions.f90 \
34+
stdlib_specialfunctions_legendre.f90 \
35+
stdlib_io.f90 \
3236
stdlib_kinds.f90 \
3337
stdlib_logger.f90 \
38+
stdlib_quadrature_gauss.f90 \
3439
stdlib_strings.f90 \
3540
$(SRCGEN)
3641

@@ -65,6 +70,8 @@ stdlib_bitsets.o: stdlib_kinds.o
6570
stdlib_bitsets_64.o: stdlib_bitsets.o
6671
stdlib_bitsets_large.o: stdlib_bitsets.o
6772
stdlib_error.o: stdlib_optval.o
73+
stdlib_specialfunctions.o: stdlib_kinds.o
74+
stdlib_specialfunctions_legendre.o: stdlib_kinds.o stdlib_specialfunctions.o
6875
stdlib_io.o: \
6976
stdlib_error.o \
7077
stdlib_optval.o \
@@ -77,6 +84,9 @@ stdlib_linalg_diag.o: \
7784
stdlib_logger.o: stdlib_ascii.o stdlib_optval.o
7885
stdlib_optval.o: stdlib_kinds.o
7986
stdlib_quadrature.o: stdlib_kinds.o
87+
88+
stdlib_quadrature_gauss.o: stdlib_kinds.o stdlib_quadrature.o
89+
8090
stdlib_quadrature_simps.o: \
8191
stdlib_quadrature.o \
8292
stdlib_error.o \
@@ -131,3 +141,4 @@ stdlib_strings.o: stdlib_ascii.o \
131141
stdlib_string_type.o \
132142
stdlib_optval.o
133143
stdlib_math.o: stdlib_kinds.o
144+
stdlib_linalg_outer_product.o: stdlib_linalg.o

src/stdlib_linalg.fypp

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ module stdlib_linalg
1111
public :: diag
1212
public :: eye
1313
public :: trace
14+
public :: outer_product
1415

1516
interface diag
1617
!! version: experimental
@@ -52,6 +53,7 @@ module stdlib_linalg
5253
#:endfor
5354
end interface
5455

56+
5557
! Matrix trace
5658
interface trace
5759
!! version: experimental
@@ -63,6 +65,21 @@ module stdlib_linalg
6365
#:endfor
6466
end interface
6567

68+
69+
! Outer product (of two vectors)
70+
interface outer_product
71+
!! version: experimental
72+
!!
73+
!! Computes the outer product of two vectors, returning a rank-2 array
74+
!! ([Specification](../page/specs/stdlib_linalg.html#description_3))
75+
#:for k1, t1 in RCI_KINDS_TYPES
76+
pure module function outer_product_${t1[0]}$${k1}$(u, v) result(res)
77+
${t1}$, intent(in) :: u(:), v(:)
78+
${t1}$ :: res(size(u),size(v))
79+
end function outer_product_${t1[0]}$${k1}$
80+
#:endfor
81+
end interface outer_product
82+
6683
contains
6784

6885
function eye(n) result(res)

src/stdlib_linalg_outer_product.fypp

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
#:include "common.fypp"
2+
#:set RCI_KINDS_TYPES = REAL_KINDS_TYPES + CMPLX_KINDS_TYPES + INT_KINDS_TYPES
3+
submodule (stdlib_linalg) stdlib_linalg_outer_product
4+
5+
implicit none
6+
7+
contains
8+
9+
#:for k1, t1 in RCI_KINDS_TYPES
10+
pure module function outer_product_${t1[0]}$${k1}$(u, v) result(res)
11+
${t1}$, intent(in) :: u(:), v(:)
12+
${t1}$ :: res(size(u),size(v))
13+
integer :: col
14+
do col = 1, size(v)
15+
res(:,col) = v(col) * u
16+
end do
17+
end function outer_product_${t1[0]}$${k1}$
18+
#:endfor
19+
20+
end submodule

src/stdlib_quadrature.fypp

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ module stdlib_quadrature
1212
public :: trapz_weights
1313
public :: simps
1414
public :: simps_weights
15+
public :: gauss_legendre
16+
public :: gauss_legendre_lobatto
1517

1618

1719
interface trapz
@@ -90,6 +92,28 @@ module stdlib_quadrature
9092
end interface simps_weights
9193

9294

95+
interface gauss_legendre
96+
!! version: experimental
97+
!!
98+
!! Computes Gauss-Legendre quadrature nodes and weights.
99+
pure module subroutine gauss_legendre_fp64 (x, w, interval)
100+
real(dp), intent(out) :: x(:), w(:)
101+
real(dp), intent(in), optional :: interval(2)
102+
end subroutine
103+
end interface gauss_legendre
104+
105+
106+
interface gauss_legendre_lobatto
107+
!! version: experimental
108+
!!
109+
!! Computes Gauss-Legendre-Lobatto quadrature nodes and weights.
110+
pure module subroutine gauss_legendre_lobatto_fp64 (x, w, interval)
111+
real(dp), intent(out) :: x(:), w(:)
112+
real(dp), intent(in), optional :: interval(2)
113+
end subroutine
114+
end interface gauss_legendre_lobatto
115+
116+
93117
! Interface for a simple f(x)-style integrand function.
94118
! Could become fancier as we learn about the performance
95119
! ramifications of different ways to do callbacks.
@@ -103,4 +127,6 @@ module stdlib_quadrature
103127
#:endfor
104128
end interface
105129

130+
131+
106132
end module stdlib_quadrature

0 commit comments

Comments
 (0)