Skip to content

Commit efa82b2

Browse files
committed
Add more comprehensive documentation
1 parent 9c4dcef commit efa82b2

File tree

2 files changed

+75
-25
lines changed

2 files changed

+75
-25
lines changed

doc/specs/stdlib_math.md

Lines changed: 68 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -111,16 +111,19 @@ Function.
111111

112112
#### Argument(s)
113113

114-
`start`: Shall be scalar of any numeric type. This argument is `intent(in)`.
114+
`start`: Shall be scalar of any numeric type or kind. This argument is `intent(in)`.
115115
`end`: Shall be the same `type` and `kind` as `start`. This argument is `intent(in)`.
116116
`n`: Shall be an integer specifying the length of the output. This argument is `optional` and `intent(in)`.
117117

118118
#### Output value or Result value
119119

120-
The output is a rank 1 array of `type` and `kind`, whose length is either 100 (default value) or `n`.
120+
The output is a rank 1 array whose length is either 100 (default value) or `n`.
121121

122122
If `n` == 1, return a rank 1 array whose only element is `end`.
123-
If `n` <= 0, return a rank 1 array with length 0
123+
If `n` <= 0, return a rank 1 array with length 0.
124+
125+
If `start`/`end` are real or complex types, the `result` will be the same `type` and `kind` as `start`/`end`.
126+
If `start`/`end` are integer types, the `result` will default to a double precision real array.
124127

125128
#### Examples
126129

@@ -144,17 +147,17 @@ end program demo_linspace_complex
144147

145148
##### Example 2:
146149

147-
Here inputs are of type `integer` and kind `int16`
150+
Here inputs are of type `integer` and kind `int16`, with the result defaulting to double precision real.
148151
```fortran
149152
program demo_linspace_int16
150153
use stdlib_math, only: linspace
151-
use stdlib_kinds, only: int16
154+
use stdlib_kinds, only: int16, dp
152155
implicit none
153156
154157
integer(int16) :: start = 10_int16
155158
integer(int16) :: end = 23_int16
156159
157-
integer(int16) :: r(15)
160+
real(dp) :: r(15)
158161
159162
r = linspace(start, end, 15)
160163
end program demo_linspace_int16
@@ -164,7 +167,7 @@ end program demo_linspace_int16
164167

165168
#### Description
166169

167-
Returns a logarithmically spaced rank 1 array from [`base`^`start`, `base`^`end`]. The default size of the array is 100. Optionally, you can specify the length of the returned array by passing `n`.
170+
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).
168171

169172
#### Syntax
170173

@@ -180,52 +183,95 @@ Function.
180183

181184
#### Argument(s)
182185

183-
`start`: Shall be a scalar of any numeric type. This argument is `intent(in)`.
186+
`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)`.
184187
`end`: Shall be the same `type` and `kind` as `start`. This argument is `intent(in)`.
185188
`n`: Shall be an integer specifying the length of the output. This argument is `optional` and `intent(in)`.
186-
`base` : Shall be a scalar of any numeric type. This argument is `optional` and `intent(in)`
189+
`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)`.
187190

188191
#### Output value or Result value
189192

190-
The output is a rank 1 array of `type` and `kind`, whose length is either 50 (default value) or `n`.
193+
The output is a rank 1 array whose length is either 50 (default value) or `n`.
191194

192195
If `n` == 1, return a rank 1 array whose only element is `base`^`end`.
193196
If `n` <= 0, return a rank 1 array with length 0
194197

198+
The `type` and `kind` of the output is dependent on the `type` and `kind` of the passed parameters.
199+
200+
For function calls where the `base` is not specified: `logspace(start, end)`/`logspace(start, end, n)`, the `type` and `kind` of
201+
the output follows the same scheme as above for `linspace`.
202+
>If `start`/`end` are real or complex types, the `result` will be the same `type` and `kind` as `start`/`end`.
203+
>If `start`/`end` are integer types, the `result` will default to a double precision real array.
204+
205+
For function calls where the `base` is specified, the `type` and `kind` of the result is in accordance with the following table:
206+
207+
| `start`/`end` | `n` | `base` | `output` |
208+
| ------------- | --- | ------ | -------- |
209+
| `real(KIND)` | `Integer` | `real(KIND)` | `real(KIND)` |
210+
| " " | " " | `complex(KIND)` | `complex(KIND)` |
211+
| " " | " " | `Integer` | `real(KIND)` |
212+
| `complex(KIND)` | " " | `real(KIND)` | `complex(KIND)` |
213+
| " " | " " | `complex(KIND)` | `complex(KIND)` |
214+
| " " | " " | `Integer` | `complex(KIND)` |
215+
| `Integer` | " " | `real(KIND)` | `real(KIND)` |
216+
| " " | " " | `complex(KIND)` | `complex(KIND)` |
217+
| " " | " " | `Integer` | `Integer` |
218+
195219
#### Examples
196220

197221
##### Example 1:
198222

199-
Here inputs are of type `complex` and kind `dp`
223+
Here inputs are of type `complex` and kind `dp`. `n` and `base` is not specified and thus default to 50 and 10, respectively.
200224
```fortran
201225
program demo_logspace_complex
202226
use stdlib_math, only: logspace
203227
use stdlib_kinds, only: dp
204228
implicit none
205229
206-
complex(dp) :: start = complex(10.0_dp, 5.0_dp)
207-
complex(dp) :: end = complex(-10.0_dp, 15.0_dp)
230+
complex(dp) :: start = (10.0_dp, 5.0_dp)
231+
complex(dp) :: end = (-10.0_dp, 15.0_dp)
208232
209-
complex(dp) :: z(11)
233+
complex(dp) :: z(11) ! Complex values raised to complex powers results in complex values
210234
211235
z = logspace(start, end, 11)
212236
end program demo_logspace_complex
213237
```
214238

215239
##### Example 2:
216240

217-
Here inputs are of type `integer` and kind `int16`
241+
Here inputs are of type `integer` and default kind. `base` is not specified and thus defaults to 10.
218242
```fortran
219-
program demo_logspace_int16
243+
program demo_logspace_int
220244
use stdlib_math, only: logspace
221-
use stdlib_kinds, only: int16
245+
use stdlib_kinds, only: dp
222246
implicit none
223247
224-
integer(int16) :: start = 10_int16
225-
integer(int16) :: end = 23_int16
248+
integer :: start = 10
249+
integer :: end = 23
250+
integer :: n = 15
251+
252+
real(dp) :: r(n) ! Integer values raised to real powers results in real values
253+
254+
r = logspace(start, end, n)
255+
end program demo_logspace_int
256+
```
257+
258+
##### Example 3:
259+
260+
Here `start`/`end` are of type `real` and double precision. `base` is type `complex` and also double precision.
261+
```fortran
262+
program demo_logspace_rstart_cbase
263+
use stdlib_math, only: logspace
264+
use stdlib_kinds, only: dp
265+
implicit none
266+
267+
real(dp) :: start = 0.0_dp
268+
real(dp) :: end = 3.0_dp
269+
integer :: n = 4
270+
complex(dp) :: base = (0.0_dp, 1.0_dp)
271+
272+
complex(dp) :: z(n) ! complex values raised to real powers result in complex values
226273
227-
integer(int16) :: r(15)
274+
z = logspace(start, end, n, base)
228275
229-
r = logspace(start, end, 15)
230-
end program demo_logspace_int16
276+
end program demo_logspace_rstart_cbase
231277
```

src/stdlib_math.fypp

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,8 @@ module stdlib_math
156156
#:for k1, t1 in REAL_KINDS_TYPES
157157
! Generate logarithmically spaced sequence from ${k1}$ base to the powers
158158
! of ${k1}$ start and end. [base^start, ... , base^end]
159-
! RName = ${RName}$
159+
! Different combinations of parameter types will lead to different result types.
160+
! Those combinations are indicated in the body of each function.
160161
#:set RName = rname("logspace", 1, t1, k1, "n_rbase")
161162
module function ${RName}$(start, end, n, base) result(res)
162163
${t1}$, intent(in) :: start
@@ -192,7 +193,8 @@ module stdlib_math
192193
#:for k1, t1 in CMPLX_KINDS_TYPES
193194
! Generate logarithmically spaced sequence from ${k1}$ base to the powers
194195
! of ${k1}$ start and end. [base^start, ... , base^end]
195-
! RName = ${RName}$
196+
! Different combinations of parameter types will lead to different result types.
197+
! Those combinations are indicated in the body of each function.
196198
#:set RName = rname("logspace", 1, t1, k1, "n_rbase")
197199
module function ${RName}$(start, end, n, base) result(res)
198200
${t1}$, intent(in) :: start
@@ -225,9 +227,11 @@ module stdlib_math
225227
#:endfor
226228
#! ========================================================
227229
#! ========================================================
230+
#! Provide support for Integer start/endpoints
228231
! Generate logarithmically spaced sequence from ${k1}$ base to the powers
229232
! of ${k1}$ start and end. [base^start, ... , base^end]
230-
! RName = ${RName}$
233+
! Different combinations of parameter types will lead to different result types.
234+
! Those combinations are indicated in the body of each function.
231235
#:for k1 in REAL_KINDS
232236
#:set RName = rname("logspace", 1, "integer(int32)", "int32", "n_r" + str(k1) + "base")
233237
module function ${RName}$(start, end, n, base) result(res)

0 commit comments

Comments
 (0)