Skip to content

Commit e11454d

Browse files
author
Jim-215-Fisher
committed
Merge remote-tracking branch 'upstream/master' into Distribution-Uniform
2 parents 7a650f1 + 0d1cc1c commit e11454d

File tree

7 files changed

+207
-9
lines changed

7 files changed

+207
-9
lines changed

CHANGELOG.md

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
# Unreleased
2+
3+
- new module `stdlib_ascii`
4+
[#32](https://github.com/fortran-lang/stdlib/pull/32)
5+
- new module `stdlib_bitsets`
6+
[#239](https://github.com/fortran-lang/stdlib/pull/239)
7+
- new derived types `bitset_64` and `bitset_large`
8+
- new abstract base class `bitset_type`
9+
- new module `stdlib_error`
10+
[#53](https://github.com/fortran-lang/stdlib/pull/53)
11+
- new module `stdlib_io`
12+
- new procedures `loadtxt` and `savetxt`
13+
[#23](https://github.com/fortran-lang/stdlib/pull/23)
14+
[#37](https://github.com/fortran-lang/stdlib/pull/37)
15+
- new procedure `open`
16+
[#71](https://github.com/fortran-lang/stdlib/pull/71)
17+
[#77](https://github.com/fortran-lang/stdlib/pull/77)
18+
- new module `stdlib_kinds`
19+
[#63](https://github.com/fortran-lang/stdlib/pull/63)
20+
- new module `stdlib_linalg`
21+
- new procedures `diag`, `eye` and `trace`
22+
[#170](https://github.com/fortran-lang/stdlib/pull/170)
23+
- new procedures `linspace` and `logspace`
24+
[#420](https://github.com/fortran-lang/stdlib/pull/420)
25+
- new procedure `outer_product`
26+
[#432](https://github.com/fortran-lang/stdlib/pull/432)
27+
- new procedure `arange`
28+
[#480](https://github.com/fortran-lang/stdlib/pull/480)
29+
- new module `stdlib_logger`
30+
- new derived type: `logger_type`
31+
[#228](https://github.com/fortran-lang/stdlib/pull/228)
32+
[#261](https://github.com/fortran-lang/stdlib/pull/261)
33+
- new module `stdlib_math`
34+
- new procedure `clip`
35+
[#355](https://github.com/fortran-lang/stdlib/pull/355)
36+
- new module `stdlib_optval`
37+
[#73](https://github.com/fortran-lang/stdlib/pull/73)
38+
[#96](https://github.com/fortran-lang/stdlib/pull/96)
39+
[#139](https://github.com/fortran-lang/stdlib/pull/139)
40+
- new module `stdlib_quadrature`
41+
- new procedures `trapz`, `trapz_weights`, `simps` and `simps_weights`
42+
[#146](https://github.com/fortran-lang/stdlib/pull/146)
43+
- new procedures `gauss_legendre`, `gauss_legendre_lobatto`
44+
[#313](https://github.com/fortran-lang/stdlib/pull/313)
45+
- new module `stdlib_sorting`
46+
- new procedures `sort`, `ord_sort` and `sort_index`
47+
[#408](https://github.com/fortran-lang/stdlib/pull/408)
48+
- new module `stdlib_specialfunctions`
49+
- new procedures `legendre` and `dlegendre`
50+
[#313](https://github.com/fortran-lang/stdlib/pull/313)
51+
- new module `stdlib_stats`
52+
- new procedure `mean`
53+
[#124](https://github.com/fortran-lang/stdlib/pull/124)
54+
[#130](https://github.com/fortran-lang/stdlib/pull/130)
55+
[#132](https://github.com/fortran-lang/stdlib/pull/132)
56+
- new procedure `var`
57+
[#144](https://github.com/fortran-lang/stdlib/pull/144)
58+
- new procedure `moment`
59+
[#153](https://github.com/fortran-lang/stdlib/pull/153)
60+
- new procedure `corr`
61+
[#191](https://github.com/fortran-lang/stdlib/pull/191)
62+
- new procedure `median`
63+
[#426](https://github.com/fortran-lang/stdlib/pull/426)
64+
- new module `stdlib_stats_distribution_PRNG`
65+
[#271](https://github.com/fortran-lang/stdlib/pull/271)
66+
- new module `stdlib_string_type`
67+
- new derived types `string_type`
68+
[#320](https://github.com/fortran-lang/stdlib/pull/320)
69+
- new procedure `move`
70+
[#467](https://github.com/fortran-lang/stdlib/pull/467)
71+
- new module `stdlib_stringlist_type`
72+
- new derived types `stringlist_type` and `stringlist_index_type`
73+
[#470](https://github.com/fortran-lang/stdlib/pull/470)
74+
- new module `stdlib_strings`
75+
- new procedure `to_string`
76+
[#444](https://github.com/fortran-lang/stdlib/pull/444)
77+
- new procedures `strip` and `chomp`
78+
[#343](https://github.com/fortran-lang/stdlib/pull/343)
79+
- new procedures `starts_with` and `ends_with`
80+
[#384](https://github.com/fortran-lang/stdlib/pull/384)
81+
- new procedure `slice`
82+
[#414](https://github.com/fortran-lang/stdlib/pull/414)
83+
- new procedure `find`
84+
[#433](https://github.com/fortran-lang/stdlib/pull/433)
85+
- new procedure `replace_all`
86+
[#436](https://github.com/fortran-lang/stdlib/pull/436)
87+
- new procedures `padl` and `padr`
88+
[#441](https://github.com/fortran-lang/stdlib/pull/441)
89+
- new procedure `count`
90+
[#453](https://github.com/fortran-lang/stdlib/pull/453)
91+
- new module `stdlib_system`
92+
- new procedure `sleep`
93+
[#54](https://github.com/fortran-lang/stdlib/pull/54)

doc/specs/stdlib_math.md

Lines changed: 46 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,51 @@ program demo_clip_real
9191
end program demo_clip_real
9292
```
9393

94+
### `gcd` function
95+
96+
#### Description
97+
98+
Returns the greatest common divisor of two integers.
99+
100+
#### Syntax
101+
102+
`res = [[stdlib_math(module):gcd(interface)]] (a, b)`
103+
104+
#### Status
105+
106+
Experimental
107+
108+
#### Class
109+
110+
Elemental function.
111+
112+
#### Argument(s)
113+
114+
`a`: One integer with `intent(in)` to get the divisor for.
115+
`b`: Another integer with `intent(in)` to get the divisor for.
116+
117+
Note: All arguments must be integers of the same `kind`.
118+
119+
#### Output value or Result value
120+
121+
Returns an integer of the same `kind` as that of the arguments.
122+
123+
#### Examples
124+
125+
##### Example 1:
126+
127+
```fortran
128+
program demo_gcd
129+
use stdlib_math, only: gcd
130+
implicit none
131+
integer :: a, b, c
132+
133+
a = 48
134+
b = 18
135+
c = gcd(a, b) ! returns 6
136+
end program demo_gcd
137+
```
138+
94139
### `linspace` - Create a linearly spaced rank one array
95140

96141
#### Description
@@ -342,4 +387,4 @@ program demo_math_arange
342387
print *, arange(0.0,2.0,0.0) !! [0.0,1.0,2.0]. Not recommended: `step` argument is zero!
343388
344389
end program demo_math_arange
345-
```
390+
```

src/CMakeLists.txt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,12 @@ set(SRC
6767
add_library(${PROJECT_NAME} ${SRC})
6868

6969
set(LIB_MOD_DIR ${CMAKE_CURRENT_BINARY_DIR}/mod_files/)
70+
# We need the module directory before we finish the configure stage since the
71+
# build interface might resolve before the module directory is generated by CMake
72+
if(NOT EXISTS "${LIB_MOD_DIR}")
73+
make_directory("${LIB_MOD_DIR}")
74+
endif()
75+
7076
set_target_properties(${PROJECT_NAME} PROPERTIES
7177
Fortran_MODULE_DIRECTORY ${LIB_MOD_DIR})
7278
target_include_directories(${PROJECT_NAME} PUBLIC

src/stdlib_math.fypp

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ module stdlib_math
88

99
implicit none
1010
private
11-
public :: clip, linspace, logspace
11+
public :: clip, gcd, linspace, logspace
1212
public :: EULERS_NUMBER_SP, EULERS_NUMBER_DP, EULERS_NUMBER_QP
1313
public :: DEFAULT_LINSPACE_LENGTH, DEFAULT_LOGSPACE_BASE, DEFAULT_LOGSPACE_LENGTH
1414
public :: arange
@@ -28,6 +28,16 @@ module stdlib_math
2828
#:endfor
2929
end interface clip
3030

31+
!> Returns the greatest common divisor of two integers
32+
!> ([Specification](../page/specs/stdlib_math.html#gcd))
33+
!>
34+
!> Version: experimental
35+
interface gcd
36+
#:for k1, t1 in INT_KINDS_TYPES
37+
module procedure gcd_${k1}$
38+
#:endfor
39+
end interface gcd
40+
3141
interface linspace
3242
!! Version: Experimental
3343
!!
@@ -292,4 +302,25 @@ contains
292302
end function clip_${k1}$
293303

294304
#:endfor
305+
306+
#:for k1, t1 in INT_KINDS_TYPES
307+
!> Returns the greatest common divisor of two integers of kind ${k1}$
308+
!> using the Euclidean algorithm.
309+
elemental function gcd_${k1}$(a, b) result(res)
310+
${t1}$, intent(in) :: a
311+
${t1}$, intent(in) :: b
312+
${t1}$ :: res
313+
314+
${t1}$ :: rem, tmp
315+
316+
rem = min(abs(a), abs(b))
317+
res = max(abs(a), abs(b))
318+
do while (rem /= 0_${k1}$)
319+
tmp = rem
320+
rem = mod(res, rem)
321+
res = tmp
322+
end do
323+
end function gcd_${k1}$
324+
325+
#:endfor
295326
end module stdlib_math

src/stdlib_string_type.fypp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1082,7 +1082,7 @@ contains
10821082
type(string_type), intent(in) :: rhs
10831083
type(string_type) :: string
10841084

1085-
string%raw = maybe(rhs) // maybe(lhs)
1085+
string%raw = maybe(lhs) // maybe(rhs)
10861086

10871087
end function concat_string_string
10881088

src/tests/math/test_stdlib_math.f90

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
! SPDX-Identifier: MIT
22

33
program test_stdlib_math
4-
use stdlib_math, only: clip
4+
use stdlib_math, only: clip, gcd
55
use stdlib_error, only: check
66
use stdlib_kinds, only: int8, int16, int32, int64, sp, dp, qp
77
implicit none
@@ -95,4 +95,23 @@ program test_stdlib_math
9595
call check(clip(-55891546.2_qp, -689712245.23_qp, -8958133457.23_qp) == -689712245.23_qp, &
9696
'clip_qp failed for invalid case', warn=.true.)
9797

98+
99+
! gcd function
100+
! testing format: check(gcd(a, b) == correct answer)
101+
call check(gcd(0, 0) == 0, 'gcd(0, 0) failed.', warn=.true.)
102+
call check(gcd(2, 0) == 2, 'gcd(2, 0) failed.', warn=.true.)
103+
call check(gcd(0, -2) == 2, 'gcd(0, -2) failed.', warn=.true.)
104+
call check(gcd(3, 3) == 3, 'gcd(3, 3) failed.', warn=.true.)
105+
call check(gcd(9, 6) == 3, 'gcd(9, 6) failed.', warn=.true.)
106+
call check(gcd(6, 9) == 3, 'gcd(6, 9) failed.', warn=.true.)
107+
call check(gcd(-9, 6) == 3, 'gcd(-9, 6) failed.', warn=.true.)
108+
call check(gcd(9, -6) == 3, 'gcd(9, -6) failed.', warn=.true.)
109+
call check(gcd(-9, -6) == 3, 'gcd(-9, -6) failed.', warn=.true.)
110+
call check(gcd(97, 91) == 1, 'gcd(97, 91) failed.', warn=.true.)
111+
112+
call check(gcd(48_int8, 18_int8) == 6_int8, 'gcd(48, 18) failed for int8.', warn=.true.)
113+
call check(gcd(48_int16, 18_int16) == 6_int16, 'gcd(48, 18) failed for int16', warn=.true.)
114+
call check(gcd(48_int32, 18_int32) == 6_int32, 'gcd(48, 18) failed for int32', warn=.true.)
115+
call check(gcd(48_int64, 18_int64) == 6_int64, 'gcd(48, 18) failed for int64', warn=.true.)
116+
98117
end program test_stdlib_math

src/tests/string/test_string_operator.f90

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -99,11 +99,15 @@ subroutine test_ne
9999
end subroutine test_ne
100100

101101
subroutine test_concat
102-
type(string_type) :: string
103-
104-
string = "Hello, "
105-
string = string // "World!"
106-
call check(len(string) == 13)
102+
type(string_type) :: a, b
103+
104+
a = "a"
105+
b = "b"
106+
call check( "a" // b == "ab" )
107+
call check( a // "b" == "ab" )
108+
call check( a // b == "ab" )
109+
call check( a // "" == "a" )
110+
call check( "" // b == "b" )
107111
end subroutine test_concat
108112

109113
end module test_string_operator

0 commit comments

Comments
 (0)