Skip to content

Commit 233d871

Browse files
authored
Merge pull request #420 from ejovo13/lin_log_space
First implementation of real-valued linspace.
2 parents d0f13b0 + f51b67f commit 233d871

10 files changed

+1266
-7
lines changed

doc/specs/stdlib_math.md

+188-3
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ title: math
1919

2020
#### Description
2121

22-
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`.
22+
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`.
2323

2424
#### Syntax
2525

@@ -35,8 +35,8 @@ Elemental function.
3535

3636
#### Argument(s)
3737

38-
`x`: scalar of either `integer` or `real` type. This argument is `intent(in)`.
39-
`xmin`: scalar of either `integer` or `real` type. This argument is `intent(in)`.
38+
`x`: scalar of either `integer` or `real` type. This argument is `intent(in)`.
39+
`xmin`: scalar of either `integer` or `real` type. This argument is `intent(in)`.
4040
`xmax`: scalar of either `integer` or `real` type, which must be greater than or equal to `xmin`. This argument is `intent(in)`.
4141

4242
Note: All arguments must have same `type` and same `kind`.
@@ -90,3 +90,188 @@ program demo_clip_real
9090
! clipped_value <- 3.02500010
9191
end program demo_clip_real
9292
```
93+
94+
### `linspace` - Create a linearly spaced rank one array
95+
96+
#### Description
97+
98+
Returns a linearly spaced rank 1 array from [`start`, `end`]. Optionally, you can specify the length of the returned array by passing `n`.
99+
100+
#### Syntax
101+
102+
`res = [[stdlib_math(module):linspace(interface)]] (start, end [, n])`
103+
104+
#### Status
105+
106+
Experimental
107+
108+
#### Class
109+
110+
Function.
111+
112+
#### Argument(s)
113+
114+
`start`: Shall be scalar of any numeric type or kind. This argument is `intent(in)`.
115+
`end`: Shall be the same `type` and `kind` as `start`. This argument is `intent(in)`.
116+
`n`: Shall be an integer specifying the length of the output. This argument is `optional` and `intent(in)`.
117+
118+
#### Output value or Result value
119+
120+
The output is a rank 1 array whose length is either 100 (default value) or `n`.
121+
122+
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.
124+
125+
If `start`/`end` are `real` or `complex` types, the `result` will be of the same type and kind as `start`/`end`.
126+
If `start`/`end` are `integer` types, the `result` will default to a `real(dp)` array.
127+
128+
#### Examples
129+
130+
##### Example 1:
131+
132+
Here inputs are of type `complex` and kind `dp`
133+
```fortran
134+
program demo_linspace_complex
135+
use stdlib_math, only: linspace
136+
use stdlib_kinds, only: dp
137+
implicit none
138+
139+
complex(dp) :: start = complex(10.0_dp, 5.0_dp)
140+
complex(dp) :: end = complex(-10.0_dp, 15.0_dp)
141+
142+
complex(dp) :: z(11)
143+
144+
z = linspace(start, end, 11)
145+
end program demo_linspace_complex
146+
```
147+
148+
##### Example 2:
149+
150+
Here inputs are of type `integer` and kind `int16`, with the result defaulting to `real(dp)`.
151+
```fortran
152+
program demo_linspace_int16
153+
use stdlib_math, only: linspace
154+
use stdlib_kinds, only: int16, dp
155+
implicit none
156+
157+
integer(int16) :: start = 10_int16
158+
integer(int16) :: end = 23_int16
159+
160+
real(dp) :: r(15)
161+
162+
r = linspace(start, end, 15)
163+
end program demo_linspace_int16
164+
```
165+
166+
### `logspace` - Create a logarithmically spaced rank one array
167+
168+
#### Description
169+
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).
171+
172+
#### Syntax
173+
174+
`res = [[stdlib_math(module):logspace(interface)]] (start, end [, n [, base]])`
175+
176+
#### Status
177+
178+
Experimental
179+
180+
#### Class
181+
182+
Function.
183+
184+
#### Argument(s)
185+
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)`.
187+
`end`: Shall be the same `type` and `kind` as `start`. This argument is `intent(in)`.
188+
`n`: Shall be an integer specifying the length of the output. 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)`.
190+
191+
#### Output value or Result value
192+
193+
The output is a rank 1 array whose length is either 50 (default value) or `n`.
194+
195+
If `n` == 1, return a rank 1 array whose only element is `base`^`end`.
196+
If `n` <= 0, return a rank 1 array with length 0
197+
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 `real(dp)` 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+
219+
#### Examples
220+
221+
##### Example 1:
222+
223+
Here inputs are of type `complex` and kind `dp`. `n` and `base` is not specified and thus default to 50 and 10, respectively.
224+
```fortran
225+
program demo_logspace_complex
226+
use stdlib_math, only: logspace
227+
use stdlib_kinds, only: dp
228+
implicit none
229+
230+
complex(dp) :: start = (10.0_dp, 5.0_dp)
231+
complex(dp) :: end = (-10.0_dp, 15.0_dp)
232+
233+
complex(dp) :: z(11) ! Complex values raised to complex powers results in complex values
234+
235+
z = logspace(start, end, 11)
236+
end program demo_logspace_complex
237+
```
238+
239+
##### Example 2:
240+
241+
Here inputs are of type `integer` and default kind. `base` is not specified and thus defaults to 10.
242+
```fortran
243+
program demo_logspace_int
244+
use stdlib_math, only: logspace
245+
use stdlib_kinds, only: dp
246+
implicit none
247+
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
273+
274+
z = logspace(start, end, n, base)
275+
276+
end program demo_logspace_rstart_cbase
277+
```

src/CMakeLists.txt

+2
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@ set(fppFiles
2929
stdlib_quadrature_simps.fypp
3030
stdlib_stats_distribution_PRNG.fypp
3131
stdlib_math.fypp
32+
stdlib_math_linspace.fypp
33+
stdlib_math_logspace.fypp
3234
stdlib_string_type.fypp
3335
)
3436

src/Makefile.manual

+7
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@ SRCFYPP =\
2525
stdlib_stats_moment_scalar.fypp \
2626
stdlib_stats_var.fypp \
2727
stdlib_math.fypp \
28+
stdlib_math_linspace.fypp \
29+
stdlib_math_logspace.fypp \
2830
stdlib_stats_distribution_PRNG.fypp \
2931
stdlib_string_type.fypp
3032

@@ -73,6 +75,7 @@ stdlib_error.o: stdlib_optval.o
7375
stdlib_specialfunctions.o: stdlib_kinds.o
7476
stdlib_specialfunctions_legendre.o: stdlib_kinds.o stdlib_specialfunctions.o
7577
stdlib_io.o: \
78+
stdlib_ascii.o \
7679
stdlib_error.o \
7780
stdlib_optval.o \
7881
stdlib_kinds.o
@@ -141,4 +144,8 @@ stdlib_strings.o: stdlib_ascii.o \
141144
stdlib_string_type.o \
142145
stdlib_optval.o
143146
stdlib_math.o: stdlib_kinds.o
147+
stdlib_math_linspace.o: \
148+
stdlib_math.o
149+
stdlib_math_logspace.o: \
150+
stdlib_math_linspace.o
144151
stdlib_linalg_outer_product.o: stdlib_linalg.o

0 commit comments

Comments
 (0)