-
Notifications
You must be signed in to change notification settings - Fork 191
Add meshgrid
subroutine in stdlib_math
#764
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
4bbf07b
WIP: first proposal
igirault 44c49ef
typos in specs; correction of tests
igirault cb3fb2d
Update doc/specs/stdlib_math.md
igirault 3a21a76
Update doc/specs/stdlib_math.md
igirault b7248d9
Update doc/specs/stdlib_math.md
igirault 8530358
modify optional argument; move to the top use statements in stdlib_ma…
igirault 76aaad5
Merge branch 'master' into meshgrid
jvdp1 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
program example_meshgrid | ||
|
||
use stdlib_math, only: meshgrid, linspace, stdlib_meshgrid_ij | ||
use stdlib_kinds, only: sp | ||
|
||
implicit none | ||
|
||
integer, parameter :: nx = 3, ny = 2 | ||
real(sp) :: x(nx), y(ny), & | ||
xm_cart(ny, nx), ym_cart(ny, nx), & | ||
xm_mat(nx, ny), ym_mat(nx, ny) | ||
|
||
x = linspace(0_sp, 1_sp, nx) | ||
y = linspace(0_sp, 1_sp, ny) | ||
|
||
call meshgrid(x, y, xm_cart, ym_cart) | ||
print *, "xm_cart = " | ||
call print_2d_array(xm_cart) | ||
print *, "ym_cart = " | ||
call print_2d_array(ym_cart) | ||
|
||
call meshgrid(x, y, xm_mat, ym_mat, indexing=stdlib_meshgrid_ij) | ||
print *, "xm_mat = " | ||
call print_2d_array(xm_mat) | ||
print *, "ym_mat = " | ||
call print_2d_array(ym_mat) | ||
|
||
contains | ||
subroutine print_2d_array(array) | ||
real(sp), intent(in) :: array(:, :) | ||
integer :: i | ||
|
||
do i = 1, size(array, dim=1) | ||
print *, array(i, :) | ||
end do | ||
end subroutine | ||
end program example_meshgrid |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
#:include "common.fypp" | ||
#:set IR_KINDS_TYPES = INT_KINDS_TYPES + REAL_KINDS_TYPES | ||
#:set RANKS = range(1, MAXRANK + 1) | ||
|
||
#:def meshgrid_loop(indices) | ||
#:for j in reversed(indices) | ||
do i${j}$ = 1, size(x${j}$) | ||
#:endfor | ||
#:for j in indices | ||
xm${j}$(${"".join(f"i{j}," for j in indices).removesuffix(",")}$) = & | ||
x${j}$(i${j}$) | ||
#:endfor | ||
#:for j in indices | ||
end do | ||
#:endfor | ||
#:enddef | ||
|
||
submodule(stdlib_math) stdlib_math_meshgrid | ||
|
||
use stdlib_error, only: error_stop | ||
|
||
contains | ||
|
||
#:for k1, t1 in IR_KINDS_TYPES | ||
#:for rank in RANKS | ||
#:if rank == 1 | ||
#:set XY_INDICES = [1] | ||
#:set IJ_INDICES = [1] | ||
#:else | ||
#:set XY_INDICES = [2, 1] + [j for j in range(3, rank + 1)] | ||
#:set IJ_INDICES = [1, 2] + [j for j in range(3, rank + 1)] | ||
#:endif | ||
#: set RName = rname("meshgrid", rank, t1, k1) | ||
module procedure ${RName}$ | ||
|
||
integer :: ${"".join(f"i{j}," for j in range(1, rank + 1)).removesuffix(",")}$ | ||
|
||
select case (optval(indexing, stdlib_meshgrid_xy)) | ||
case (stdlib_meshgrid_xy) | ||
$:meshgrid_loop(XY_INDICES) | ||
case (stdlib_meshgrid_ij) | ||
$:meshgrid_loop(IJ_INDICES) | ||
case default | ||
call error_stop("ERROR (meshgrid): unexpected indexing.") | ||
end select | ||
end procedure | ||
#:endfor | ||
#:endfor | ||
|
||
end submodule |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,11 @@ | ||
set( | ||
fppFiles | ||
"test_stdlib_math.fypp" | ||
"test_meshgrid.fypp" | ||
) | ||
fypp_f90("${fyppFlags}" "${fppFiles}" outFiles) | ||
|
||
ADDTEST(stdlib_math) | ||
ADDTEST(linspace) | ||
ADDTEST(logspace) | ||
ADDTEST(meshgrid) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,121 @@ | ||
! SPDX-Identifier: MIT | ||
|
||
#:include "common.fypp" | ||
#:set IR_KINDS_TYPES = INT_KINDS_TYPES + REAL_KINDS_TYPES | ||
#:set RANKS = range(1, MAXRANK + 1) | ||
#:set INDEXINGS = ["default", "xy", "ij"] | ||
|
||
#:def OPTIONAL_PART_IN_SIGNATURE(indexing) | ||
#:if indexing in ("xy", "ij") | ||
${f', stdlib_meshgrid_{indexing}'}$ | ||
#:endif | ||
#:enddef | ||
|
||
module test_meshgrid | ||
use testdrive, only : new_unittest, unittest_type, error_type, check | ||
use stdlib_math, only: meshgrid, stdlib_meshgrid_ij, stdlib_meshgrid_xy | ||
use stdlib_kinds, only: int8, int16, int32, int64, sp, dp, xdp, qp | ||
implicit none | ||
|
||
public :: collect_meshgrid | ||
|
||
contains | ||
|
||
!> Collect all exported unit tests | ||
subroutine collect_meshgrid(testsuite) | ||
!> Collection of tests | ||
type(unittest_type), allocatable, intent(out) :: testsuite(:) | ||
|
||
testsuite = [ & | ||
#:for k1, t1 in IR_KINDS_TYPES | ||
#:for rank in RANKS | ||
#:for INDEXING in INDEXINGS | ||
#: set RName = rname(f"meshgrid_{INDEXING}", rank, t1, k1) | ||
new_unittest("${RName}$", test_${RName}$), & | ||
#:endfor | ||
#:endfor | ||
#:endfor | ||
new_unittest("dummy", test_dummy) & | ||
] | ||
|
||
end subroutine collect_meshgrid | ||
|
||
#:for k1, t1 in IR_KINDS_TYPES | ||
#:for rank in RANKS | ||
#:for INDEXING in INDEXINGS | ||
#:if rank == 1 | ||
#:set INDICES = [1] | ||
#:else | ||
#:if INDEXING in ("default", "xy") | ||
#:set INDICES = [2, 1] + [j for j in range(3, rank + 1)] | ||
#:elif INDEXING == "ij" | ||
#:set INDICES = [1, 2] + [j for j in range(3, rank + 1)] | ||
#:endif | ||
#:endif | ||
#:set RName = rname(f"meshgrid_{INDEXING}", rank, t1, k1) | ||
#:set GRIDSHAPE = "".join("length," for j in range(rank)).removesuffix(",") | ||
subroutine test_${RName}$(error) | ||
!> Error handling | ||
type(error_type), allocatable, intent(out) :: error | ||
integer, parameter :: length = 3 | ||
${t1}$ :: ${"".join(f"x{j}(length)," for j in range(1, rank + 1)).removesuffix(",")}$ | ||
${t1}$ :: ${"".join(f"xm{j}({GRIDSHAPE})," for j in range(1, rank + 1)).removesuffix(",")}$ | ||
${t1}$ :: ${"".join(f"xm{j}_exact({GRIDSHAPE})," for j in range(1, rank + 1)).removesuffix(",")}$ | ||
integer :: i | ||
integer :: ${"".join(f"i{j}," for j in range(1, rank + 1)).removesuffix(",")}$ | ||
${t1}$, parameter :: ZERO = 0 | ||
jvdp1 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
! valid test case | ||
#:for index in range(1, rank + 1) | ||
x${index}$ = [(i, i = length * ${index - 1}$ + 1, length * ${index}$)] | ||
#:endfor | ||
#:for j in range(1, rank + 1) | ||
xm${j}$_exact = reshape( & | ||
[${"".join("(" for dummy in range(rank)) + f"x{j}(i{j})" + "".join(f", i{index} = 1, size(x{index}))" for index in INDICES)}$], & | ||
shape=[${GRIDSHAPE}$] & | ||
) | ||
#:endfor | ||
call meshgrid( & | ||
${"".join(f"x{j}," for j in range(1, rank + 1))}$ & | ||
${"".join(f"xm{j}," for j in range(1, rank + 1)).removesuffix(",")}$ & | ||
${OPTIONAL_PART_IN_SIGNATURE(INDEXING)}$ ) | ||
#:for j in range(1, rank + 1) | ||
call check(error, maxval(abs(xm${j}$ - xm${j}$_exact)), ZERO) | ||
if (allocated(error)) return | ||
#:endfor | ||
end subroutine test_${RName}$ | ||
#:endfor | ||
#:endfor | ||
#:endfor | ||
|
||
subroutine test_dummy(error) | ||
!> Error handling | ||
type(error_type), allocatable, intent(out) :: error | ||
end subroutine | ||
|
||
end module test_meshgrid | ||
|
||
program tester | ||
use, intrinsic :: iso_fortran_env, only : error_unit | ||
use testdrive, only : run_testsuite, new_testsuite, testsuite_type | ||
use test_meshgrid, only : collect_meshgrid | ||
implicit none | ||
integer :: stat, is | ||
type(testsuite_type), allocatable :: testsuites(:) | ||
character(len=*), parameter :: fmt = '("#", *(1x, a))' | ||
|
||
stat = 0 | ||
|
||
testsuites = [ & | ||
new_testsuite("meshgrid", collect_meshgrid) & | ||
] | ||
|
||
do is = 1, size(testsuites) | ||
write(error_unit, fmt) "Testing:", testsuites(is)%name | ||
call run_testsuite(testsuites(is)%collect, error_unit, stat) | ||
end do | ||
|
||
if (stat > 0) then | ||
write(error_unit, '(i0, 1x, a)') stat, "test(s) failed!" | ||
error stop | ||
end if | ||
end program tester |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.