diff --git a/CMakeLists.txt b/CMakeLists.txt index 8007a672e..26f49a428 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -71,6 +71,7 @@ add_subdirectory(src) if(BUILD_TESTING) enable_testing() add_subdirectory(test) + add_subdirectory(example) endif() install(EXPORT ${PROJECT_NAME}-targets diff --git a/README.md b/README.md index 117159df2..a70d9f18f 100644 --- a/README.md +++ b/README.md @@ -149,12 +149,17 @@ To build the standard library run cmake --build build ``` -To test your build, run the test suite after the build has finished with +To test your build, run the test suite and all example programs after the build has finished with ```sh cmake --build build --target test ``` +To test only the test suite, run +```sh +ctest --test-dir build/test +``` + Please report failing tests on our [issue tracker](https://github.com/fortran-lang/stdlib/issues/new/choose) including details of the compiler used, the operating system and platform architecture. To install the project to the declared prefix run @@ -179,6 +184,15 @@ git checkout stdlib-fpm fpm build --profile release ``` +You can run the examples with `fpm` as: + +```sh +fpm run --example prog +``` + +with `prog` being the name of the example program (e.g., `example_sort`). + + To use `stdlib` within your `fpm` project, add the following lines to your `fpm.toml` file: ```toml [dependencies] diff --git a/ci/fpm-deployment.sh b/ci/fpm-deployment.sh index 7f80338c2..bdd3c2b6e 100644 --- a/ci/fpm-deployment.sh +++ b/ci/fpm-deployment.sh @@ -39,7 +39,7 @@ minor=$(cut -d. -f2 VERSION) patch=$(cut -d. -f3 VERSION) fyflags="${fyflags} -DPROJECT_VERSION_MAJOR=${major} -DPROJECT_VERSION_MINOR=${minor} -DPROJECT_VERSION_PATCH=${patch}" -mkdir -p "$destdir/src" "$destdir/test" +mkdir -p "$destdir/src" "$destdir/test" "$destdir/example" # Preprocess stdlib sources find src -maxdepth 1 -iname "*.fypp" \ @@ -49,6 +49,7 @@ find src -maxdepth 1 -iname "*.fypp" \ find src -maxdepth 1 -iname "*.f90" -exec cp {} "$destdir/src/" \; find test -name "test_*.f90" -exec cp {} "$destdir/test/" \; find test -name "*.dat" -exec cp {} "$destdir/" \; +find example -name "example_*.f90" -exec cp {} "$destdir/example/" \; # Include additional files cp "${include[@]}" "$destdir/" diff --git a/doc/specs/stdlib_array.md b/doc/specs/stdlib_array.md index 8acaa20b3..f9353aa22 100644 --- a/doc/specs/stdlib_array.md +++ b/doc/specs/stdlib_array.md @@ -46,14 +46,7 @@ Returns an array of default integer size, with a maximum length of `size(array)` #### Examples ```fortran -program demo_trueloc - use stdlib_array, only : trueloc - implicit none - real, allocatable :: array(:) - allocate(array(500)) - call random_number(array) - array(trueloc(array > 0.5)) = 0.0 -end program demo_trueloc +{!example/array/example_trueloc.f90!} ``` @@ -90,12 +83,5 @@ Returns an array of default integer size, with a maximum length of `size(array)` #### Examples ```fortran -program demo_falseloc - use stdlib_array, only : falseloc - implicit none - real, allocatable :: array(:) - allocate(array(-200:200)) - call random_number(array) - array(falseloc(array < 0.5), lbound(array)) = 0.0 -end program demo_falseloc +{!example/array/example_falseloc.f90!} ``` diff --git a/doc/specs/stdlib_ascii.md b/doc/specs/stdlib_ascii.md index b861f6067..260ff8d50 100644 --- a/doc/specs/stdlib_ascii.md +++ b/doc/specs/stdlib_ascii.md @@ -51,11 +51,7 @@ The result is an intrinsic character type of the same length as `string`. #### Example ```fortran -program demo_to_lower - use stdlib_ascii, only : to_lower - implicit none - print'(a)', to_lower("HELLo!") ! returns "hello!" - end program demo_to_lower +{!example/ascii/example_ascii_to_lower.f90!} ``` ### `to_upper` @@ -87,11 +83,7 @@ The result is an intrinsic character type of the same length as `string`. #### Example ```fortran -program demo_to_upper - use stdlib_ascii, only : to_upper - implicit none - print'(a)', to_upper("hello!") ! returns "HELLO!" - end program demo_to_upper +{!example/ascii/example_ascii_to_upper.f90!} ``` ### `to_title` @@ -128,13 +120,7 @@ The result is an intrinsic character type of the same length as `string`. #### Example ```fortran -program demo_to_title - use stdlib_ascii, only : to_title - implicit none - print*, to_title("hello there!") ! returns "Hello There!" - print*, to_title("'enquoted'") ! returns "'Enquoted'" - print*, to_title("1st") ! returns "1st" - end program demo_to_title +{!example/ascii/example_ascii_to_title.f90!} ``` ### `to_sentence` @@ -169,13 +155,7 @@ The result is an intrinsic character type of the same length as `string`. #### Example ```fortran -program demo_to_sentence - use stdlib_ascii, only : to_sentence - implicit none - print*, to_sentence("hello!") ! returns "Hello!" - print*, to_sentence("'enquoted'") ! returns "'Enquoted'" - print*, to_sentence("1st") ! returns "1st" - end program demo_to_sentence +{!example/ascii/example_ascii_to_sentence.f90!} ``` ### `reverse` @@ -207,9 +187,5 @@ The result is an intrinsic character type of the same length as `string`. #### Example ```fortran -program demo_reverse - use stdlib_ascii, only : reverse - implicit none - print'(a)', reverse("Hello, World!") ! returns "!dlroW ,olleH" -end program demo_reverse +{!example/ascii/example_ascii_reverse.f90!} ``` diff --git a/doc/specs/stdlib_bitsets.md b/doc/specs/stdlib_bitsets.md index 0ac1977b2..77da22b9b 100644 --- a/doc/specs/stdlib_bitsets.md +++ b/doc/specs/stdlib_bitsets.md @@ -205,30 +205,7 @@ is mapped to a set bit, and `.false.` is mapped to an unset bit. #### Example ```fortran - program demo_assignment - use stdlib_bitsets - logical(int8) :: logical1(64) = .true. - logical(int32), allocatable :: logical2(:) - type(bitset_64) :: set0, set1 - set0 = logical1 - if ( set0 % bits() /= 64 ) then - error stop procedure // & - ' initialization with logical(int8) failed to set' // & - ' the right size.' - else if ( .not. set0 % all() ) then - error stop procedure // ' initialization with' // & - ' logical(int8) failed to set the right values.' - else - write(*,*) 'Initialization with logical(int8) succeeded.' - end if - set1 = set0 - if ( set1 == set0 ) & - write(*,*) 'Initialization by assignment succeeded' - logical2 = set1 - if ( all( logical2 ) ) then - write(*,*) 'Initialization of logical(int32) succeeded.' - end if - end program demo_assignment +{!example/bitsets/example_bitsets_assignment.f90!} ``` ### Table of the non-member comparison operations @@ -282,20 +259,7 @@ otherwise it is `.false.`. #### Example ```fortran - program demo_all - use stdlib_bitsets - character(*), parameter :: & - bits_all = '111111111111111111111111111111111' - type(bitset_64) :: set0 - call set0 % from_string( bits_all ) - if ( .not. set0 % all() ) then - error stop "FROM_STRING failed to interpret" // & - "BITS_ALL's value properly." - else - write(*,*) "FROM_STRING transferred BITS_ALL properly" // & - " into set0." - end if - end program demo_all +{!example/bitsets/example_bitsets_all.f90!} ``` ### `and` - bitwise `and` of the bits of two bitsets @@ -332,23 +296,7 @@ number of bits as `set1`. #### Example ```fortran - program demo_and - use stdlib_bitsets - type(bitset_large) :: set0, set1 - call set0 % init(166) - call set1 % init(166) - call and( set0, set1 ) ! none none - if ( none(set0) ) write(*,*) 'First test of AND worked.' - call set0 % not() - call and( set0, set1 ) ! all none - if ( none(set0) ) write(*,*) 'Second test of AND worked.' - call set1 % not() - call and( set0, set1 ) ! none all - if ( none(set0) ) write(*,*) 'Third test of AND worked.' - call set0 % not() - call and( set0, set1 ) ! all all - if ( all(set0) ) write(*,*) 'Fourth test of AND worked.' - end program demo_and +{!example/bitsets/example_bitsets_and.f90!} ``` ### `and_not` - Bitwise `and` of one bitset with the negation of another @@ -386,24 +334,7 @@ number of bits as `set1`, otherwise the result is undefined. #### Example ```fortran - program demo_and_not - use stdlib_bitsets - type(bitset_large) :: set0, set1 - call set0 % init(166) - call set1 % init(166) - call and_not( set0, set1 ) ! none none - if ( none(set0) ) write(*,*) 'First test of AND_NOT worked.' - call set0 % not() - call and_not( set0, set1 ) ! all none - if ( all(set0) ) write(*,*) 'Second test of AND_NOT worked.' - call set0 % not() - call set1 % not() - call and_not( set0, set1 ) ! none all - if ( none(set0) ) write(*,*) 'Third test of AND_NOT worked.' - call set0 % not() - call and_not( set0, set1 ) ! all all - if ( none(set0) ) write(*,*) 'Fourth test of AND_NOT worked.' - end program demo_and_not +{!example/bitsets/example_bitsets_and_not.f90!} ``` ### `any` - determine whether any bits are set @@ -437,21 +368,7 @@ is `.false.`. #### Example ```fortran - program demo_any - use stdlib_bitsets - character(*), parameter :: & - bits_0 = '0000000000000000000' - type(bitset_64) :: set0 - call set0 % from_string( bits_0 ) - if ( .not. set0 % any() ) then - write(*,*) "FROM_STRING interpreted " // & - "BITS_0's value properly." - end if - call set0 % set(5) - if ( set0 % any() ) then - write(*,*) "ANY interpreted SET0's value properly." - end if - end program demo_any +{!example/bitsets/example_bitsets_any.f90!} ``` ### `bit_count` - return the number of bits that are set @@ -485,21 +402,7 @@ equal to the number of bits that are set in `self`. #### Example ```fortran - program demo_bit_count - use stdlib_bitsets - character(*), parameter :: & - bits_0 = '0000000000000000000' - type(bitset_64) :: set0 - call set0 % from_string( bits_0 ) - if ( set0 % bit_count() == 0 ) then - write(*,*) "FROM_STRING interpreted " // & - "BITS_0's value properly." - end if - call set0 % set(5) - if ( set0 % bit_count() == 1 ) then - write(*,*) "BIT_COUNT interpreted SET0's value properly." - end if - end program demo_bit_count +{!example/bitsets/example_bitsets_bit_count.f90!} ``` #### `bits` - returns the number of bits @@ -533,17 +436,7 @@ the number of defined bits in `self`. #### Example ```fortran - program demo_bits - use stdlib_bitsets - character(*), parameter :: & - bits_0 = '0000000000000000000' - type(bitset_64) :: set0 - call set0 % from_string( bits_0 ) - if ( set0 % bits() == 19 ) then - write(*,*) "FROM_STRING interpreted " // & - "BITS_0's size properly." - end if - end program demo_bits +{!example/bitsets/example_bitsets_bits.f90!} ``` ### `clear` - clears a sequence of one or more bits @@ -594,17 +487,7 @@ an `intent(in)` argument. #### Example ```fortran - program demo_clear - use stdlib_bitsets - type(bitset_large) :: set0 - call set0 % init(166) - call set0 % not() - if ( set0 % all() ) write(*,*) 'SET0 is properly initialized.' - call set0 % clear(165) - if ( .not. set0 % test(165) ) write(*,*) 'Bit 165 is cleared.' - call set0 % clear(0,164) - if ( set0 % none() ) write(*,*) 'All bits are cleared.' - end program demo_clear +{!example/bitsets/example_bitsets_clear.f90!} ``` ### `extract` - create a new bitset from a range in an old bitset @@ -655,16 +538,7 @@ an `intent(out)` argument. If present it shall have one of the values: #### Example ```fortran - program demo_extract - use stdlib_bitsets - type(bitset_large) :: set0, set1 - call set0 % init(166) - call set0 % set(100,150) - call extract( set1, set0, 100, 150) - if ( set1 % bits() == 51 ) & - write(*,*) 'SET1 has the proper size.' - if ( set1 % all() ) write(*,*) 'SET1 has the proper values.' - end program demo_extract +{!example/bitsets/example_bitsets_extract.f90!} ``` ### `flip` - flip the values of a sequence of one or more bits @@ -716,16 +590,7 @@ an `intent(in)` argument. #### Example ```fortran - program demo_flip - use stdlib_bitsets - type(bitset_large) :: set0 - call set0 % init(166) - if ( set0 % none() ) write(*,*) 'SET0 is properly initialized.' - call set0 % flip(165) - if ( set0 % test(165) ) write(*,*) 'Bit 165 is flipped.' - call set0 % flip(0,164) - if ( set0 % all() ) write(*,*) 'All bits are flipped.' - end program demo_flip +{!example/bitsets/example_bitsets_flip.f90!} ``` ### `from_string` - initializes a bitset from a binary literal @@ -775,23 +640,7 @@ codes: #### Example ```fortran - program demo_from_string - use stdlib_bitsets - character(*), parameter :: & - bits_all = '111111111111111111111111111111111' - type(bitset_64) :: set0 - call set0 % from_string( bits_all ) - if ( bits(set0) /= 33 ) then - error stop "FROM_STRING failed to interpret " // & - 'BITS_ALL's size properly." - else if ( .not. set0 % all() ) then - error stop "FROM_STRING failed to interpret" // & - "BITS_ALL's value properly." - else - write(*,*) "FROM_STRING transferred BITS_ALL properly" // & - " into set0." - end if - end program demo_from_string +{!example/bitsets/example_bitsets_from_string.f90!} ``` ### `init` - `bitset_type` initialization routines @@ -840,14 +689,7 @@ stop code. It can have any of the following error codes: #### Example ```fortran - program demo_init - use stdlib_bitsets - type(bitset_large) :: set0 - call set0 % init(166) - if ( set0 % bits() == 166 ) & - write(*,*) 'SET0 has the proper size.' - if ( set0 % none() ) write(*,*) 'SET0 is properly initialized.' - end program demo_init +{!example/bitsets/example_bitsets_init.f90!} ``` ### `input` - reads a bitset from an unformatted file @@ -900,36 +742,7 @@ values for this `status` are: #### Example ```fortran - program demo_input - character(*), parameter :: & - bits_0 = '000000000000000000000000000000000', & - bits_1 = '000000000000000000000000000000001', & - bits_33 = '100000000000000000000000000000000' - integer :: unit - type(bitset_64) :: set0, set1, set2, set3, set4, set5 - call set0 % from_string( bits_0 ) - call set1 % from_string( bits_1 ) - call set2 % from_string( bits_33 ) - open( newunit=unit, file='test.bin', status='replace', & - form='unformatted', action='write' ) - call set2 % output(unit) - call set1 % output(unit) - call set0 % output(unit) - close( unit ) - open( newunit=unit, file='test.bin', status='old', & - form='unformatted', action='read' ) - call set5 % input(unit) - call set4 % input(unit) - call set3 % input(unit) - close( unit ) - if ( set3 /= set0 .or. set4 /= set1 .or. set5 /= set2 ) then - error stop 'Transfer to and from units using ' // & - ' output and input failed.' - else - write(*,*) 'Transfer to and from units using ' // & - 'output and input succeeded.' - end if - end program demo_input +{!example/bitsets/example_bitsets_input.f90!} ``` ### `none` - determines whether no bits are set @@ -964,21 +777,7 @@ The result is `.true.` if no bits in `self` are set, otherwise it is #### Example ```fortran - program demo_none - use stdlib_bitsets - character(*), parameter :: & - bits_0 = '0000000000000000000' - type(bitset_large) :: set0 - call set0 % from_string( bits_0 ) - if ( set0 % none() ) then - write(*,*) "FROM_STRING interpreted " // & - "BITS_0's value properly." - end if - call set0 % set(5) - if ( .not. set0 % none() ) then - write(*,*) "NONE interpreted SET0's value properly." - end if - end program demo_none +{!example/bitsets/example_bitsets_none.f90!} ``` ### `not` - Performs the logical complement on a bitset @@ -1008,19 +807,7 @@ complement of their values on input. #### Example ```fortran - program demo_not - use stdlib_bitsets - type(bitset_large) :: set0 - call set0 % init( 155 ) - if ( set0 % none() ) then - write(*,*) "FROM_STRING interpreted " // & - "BITS_0's value properly." - end if - call set0 % not() - if ( set0 % all() ) then - write(*,*) "ALL interpreted SET0's value properly." - end if - end program demo_not +{!example/bitsets/example_bitsets_not.f90!} ``` ### `or` - Bitwise OR of the bits of two bitsets @@ -1057,24 +844,7 @@ otherwise the results are undefined. #### Example ```fortran - program demo_or - use stdlib_bitsets - type(bitset_large) :: set0, set1 - call set0 % init(166) - call set1 % init(166) - call or( set0, set1 ) ! none none - if ( none(set0) ) write(*,*) 'First test of OR worked.' - call set0 % not() - call or( set0, set1 ) ! all none - if ( all(set0) ) write(*,*) 'Second test of OR worked.' - call set0 % not() - call set1 % not() - call or( set0, set1 ) ! none all - if ( all(set0) ) write(*,*) 'Third test of OR worked.' - call set0 % not() - call or( set0, set1 ) ! all all - if ( all(set0) ) write(*,*) 'Fourth test of OR worked.' - end program demo_or +{!example/bitsets/example_bitsets_or.f90!} ``` ### `output` - Writes a binary representation of a bitset to a file @@ -1117,36 +887,7 @@ code. The two code values have the meaning: #### Example ```fortran - program demo_output - character(*), parameter :: & - bits_0 = '000000000000000000000000000000000', & - bits_1 = '000000000000000000000000000000001', & - bits_33 = '100000000000000000000000000000000' - integer :: unit - type(bitset_64) :: set0, set1, set2, set3, set4, set5 - call set0 % from_string( bits_0 ) - call set1 % from_string( bits_1 ) - call set2 % from_string( bits_33 ) - open( newunit=unit, file='test.bin', status='replace', & - form='unformatted', action='write' ) - call set2 % output(unit) - call set1 % output(unit) - call set0 % output(unit) - close( unit ) - open( newunit=unit, file='test.bin', status='old', & - form='unformatted', action='read' ) - call set5 % input(unit) - call set4 % input(unit) - call set3 % input(unit) - close( unit ) - if ( set3 /= set0 .or. set4 /= set1 .or. set5 /= set2 ) then - error stop 'Transfer to and from units using ' // & - ' output and input failed.' - else - write(*,*) 'Transfer to and from units using ' // & - 'output and input succeeded.' - end if - end program demo_output +{!example/bitsets/example_bitsets_output.f90!} ``` ### `read_bitset` - initializes `self` with the value of a *bitset_literal* @@ -1227,39 +968,7 @@ as its error code. The possible error codes are: #### Example ```fortran - program demo_read_bitset - character(*), parameter :: & - bits_0 = 'S33B000000000000000000000000000000000', & - bits_1 = 'S33B000000000000000000000000000000001', & - bits_33 = 'S33B100000000000000000000000000000000' - character(:), allocatable :: test_0, test_1, test_2 - integer :: unit - type(bitset_64) :: set0, set1, set2, set3, set4, set5 - call set0 % read_bitset( bits_0, status ) - call set1 % read_bitset( bits_1, status ) - call set2 % read_bitset( bits_2, status ) - call set0 % write_bitset( test_0, status ) - call set1 % write_bitset( test_1, status ) - call set2 % write_bitset( test_2, status ) - if ( bits_0 == test_0 .and. bits_1 == test_1 .and. & - bits_2 == test_2 ) then - write(*,*) 'READ_BITSET to WRITE_BITSET strings worked.' - end if - open( newunit=unit, file='test.txt', status='replace', & - form='formatted', action='write' ) - call set2 % write_bitset(unit, advance='no') - call set1 % write_bitset(unit, advance='no') - call set0 % write_bitset(unit) - close( unit ) - open( newunit=unit, file='test.txt', status='old', & - form='formatted', action='read' ) - call set3 % read_bitset(unit, advance='no') - call set4 % read_bitset(unit, advance='no') - call set5 % read_bitset(unit) - if ( set3 == set0 .and. set4 == set1 .and. set5 == set2 ) then - write(*,*) WRITE_BITSET to READ_BITSET through unit worked.' - end if - end program demo_read_bitset +{!example/bitsets/example_bitsets_read_bitset.f90!} ``` ### `set` - sets a sequence of one or more bits to 1 @@ -1313,16 +1022,7 @@ Elemental subroutine #### Example ```fortran - program demo_set - use stdlib_bitsets - type(bitset_large) :: set0 - call set0 % init(166) - if ( set0 % none() ) write(*,*) 'SET0 is properly initialized.' - call set0 % set(165) - if ( set0 % test(165) ) write(*,*) 'Bit 165 is set.' - call set0 % set(0,164) - if ( set0 % all() ) write(*,*) 'All bits are set.' - end program demo_set +{!example/bitsets/example_bitsets_set.f90!} ``` ### `test` - determine whether a bit is set @@ -1362,17 +1062,7 @@ otherwise it is `.false.`. If `pos` is outside the range #### Example ```fortran - program demo_test - use stdlib_bitsets - type(bitset_large) :: set0 - call set0 % init(166) - call set0 % not() - if ( set0 % all() ) write(*,*) 'SET0 is properly initialized.' - call set0 % clear(165) - if ( .not. set0 % test(165) ) write(*,*) 'Bit 165 is cleared.' - call set0 % set(165) - if ( set0 % test(165) ) write(*,*) 'Bit 165 is set.' - end program demo_test +{!example/bitsets/example_bitsets_test.f90!} ``` ### `to_string` - represent a bitset as a binary literal @@ -1416,20 +1106,7 @@ the stop code. The values have the following meanings: #### Example ```fortran - program demo_to_string - use stdlib_bitsets - character(*), parameter :: & - bits_all = '111111111111111111111111111111111' - type(bitset_64) :: set0 - character(:), allocatable :: new_string - call set0 % init(33) - call set0 % not() - call set0 % to_string( new_string ) - if ( new_string == bits_all ) then - write(*,*) "TO_STRING transferred BITS0 properly" // & - " into NEW_STRING." - end if - end program demo_to_string +{!example/bitsets/example_bitsets_to_string.f90!} ``` ### `value` - determine the value of a bit @@ -1468,17 +1145,7 @@ is zero. #### Example ```fortran - program demo_value - use stdlib_bitsets - type(bitset_large) :: set0 - call set0 % init(166) - call set0 % not() - if ( set0 % all() ) write(*,*) 'SET0 is properly initialized.' - call set0 % clear(165) - if ( set0 % value(165) == 0 ) write(*,*) 'Bit 165 is cleared.' - call set0 % set(165) - if ( set0 % value(165) == 1 ) write(*,*) 'Bit 165 is set.' - end program demo_value +{!example/bitsets/example_bitsets_value.f90!} ``` ### `write_bitset` - writes a *bitset-literal* @@ -1545,39 +1212,7 @@ the following error code values: #### Example ```fortran - program demo_write_bitset - character(*), parameter :: & - bits_0 = 'S33B000000000000000000000000000000000', & - bits_1 = 'S33B000000000000000000000000000000001', & - bits_33 = 'S33B100000000000000000000000000000000' - character(:), allocatable :: test_0, test_1, test_2 - integer :: unit - type(bitset_64) :: set0, set1, set2, set3, set4, set5 - call set0 % read_bitset( bits_0, status ) - call set1 % read_bitset( bits_1, status ) - call set2 % read_bitset( bits_2, status ) - call set0 % write_bitset( test_0, status ) - call set1 % write_bitset( test_1, status ) - call set2 % write_bitset( test_2, status ) - if ( bits_0 == test_0 .and. bits_1 == test_1 .and. & - bits_2 == test_2 ) then - write(*,*) 'READ_BITSET to WRITE_BITSET strings worked.' - end if - open( newunit=unit, file='test.txt', status='replace', & - form='formatted', action='write' ) - call set2 % write_bitset(unit, advance='no') - call set1 % write_bitset(unit, advance='no') - call set0 % write_bitset(unit) - close( unit ) - open( newunit=unit, file='test.txt', status='old', & - form='formatted', action='read' ) - call set3 % read_bitset(unit, advance='no') - call set4 % read_bitset(unit, advance='no') - call set5 % read_bitset(unit) - if ( set3 == set0 .and. set4 == set1 .and. set5 == set2 ) then - write(*,*) WRITE_BITSET to READ_BITSET through unit worked.' - end if - end program demo_write_bitset +{!example/bitsets/example_bitsets_write_bitset.f90!} ``` ### `xor` - bitwise exclusive `or` @@ -1614,24 +1249,7 @@ samee number of bits, otherwise the result is undefined. #### Example ```fortran - program demo_xor - use stdlib_bitsets - type(bitset_large) :: set0, set1 - call set0 % init(166) - call set1 % init(166) - call xor( set0, set1 ) ! none none - if ( none(set0) ) write(*,*) 'First test of XOR worked.' - call set0 % not() - call xor( set0, set1 ) ! all none - if ( all(set0) ) write(*,*) 'Second test of XOR worked.' - call set0 % not() - call set1 % not() - call xor( set0, set1 ) ! none all - if ( all(set0) ) write(*,*) 'Third test of XOR worked.' - call set0 % not() - call xor( set0, set1 ) ! all all - if ( none(set0) ) write(*,*) 'Fourth test of XOR worked.' - end program demo_xor +{!example/bitsets/example_bitsets_xor.f90!} ``` ## Specification of the `stdlib_bitsets` operators @@ -1677,22 +1295,7 @@ to the same value, otherwise the result is `.false.`. #### Example ```fortran - program demo_equality - use stdlib_bitsets - type(bitset_64) :: set0, set1, set2 - call set0 % init( 33 ) - call set1 % init( 33 ) - call set2 % init( 33 ) - call set1 % set( 0 ) - call set2 % set( 32 ) - if ( set0 == set0 .and. set1 == set1 .and. set2 == set2 .and. & - .not. set0 == set1 .and. .not. set0 == set2 .and. .not. & - set1 == set2 ) then - write(*,*) 'Passed 64 bit equality tests.' - else - error stop 'Failed 64 bit equality tests.' - end if - end program demo_equality +{!example/bitsets/example_bitsets_equality.f90!} ``` ### `/=` - compare two bitsets to determine whether any bits differ in value @@ -1736,22 +1339,7 @@ the result is `.false.`. #### Example ```fortran - program demo_inequality - use stdlib_bitsets - type(bitset_64) :: set0, set1, set2 - call set0 % init( 33 ) - call set1 % init( 33 ) - call set2 % init( 33 ) - call set1 % set( 0 ) - call set2 % set( 32 ) - if ( set0 /= set1 .and. set0 /= set2 .and. set1 /= set2 .and. & - .not. set0 /= set0 .and. .not. set1 /= set1 .and. .not. & - set2 /= set2 ) then - write(*,*) 'Passed 64 bit inequality tests.' - else - error stop 'Failed 64 bit inequality tests.' - end if - end program demo_inequality +{!example/bitsets/example_bitsets_inequality.f90!} ``` ### `>=` - compare two bitsets to determine whether the first is greater than or equal to the second @@ -1798,23 +1386,7 @@ or the highest order different bit is set to 1 in `set1` and to 0 in #### Example ```fortran - program demo_ge - use stdlib_bitsets - type(bitset_64) :: set0, set1, set2 - call set0 % init( 33 ) - call set1 % init( 33 ) - call set2 % init( 33 ) - call set1 % set( 0 ) - call set2 % set( 32 ) - if ( set1 >= set0 .and. set2 >= set1 .and. set2 >= set0 .and. & - set0 >= set0 .and. set1 >= set1 .and. set2 >= set2 .and. & - .not. set0 >= set1 .and. .not. set0 >= set2 .and. .not. & - set1 >= set2 ) then - write(*,*) 'Passed 64 bit greater than or equals tests.' - else - error stop 'Failed 64 bit greater than or equals tests.' - end if - end program demo_ge +{!example/bitsets/example_bitsets_ge.f90!} ``` ### `>` - compare two bitsets to determine whether the first is greater than the other @@ -1861,22 +1433,7 @@ highest order different bit is set to 1 in `set1` and to 0 in `set2`, #### Example ```fortran - program demo_gt - use stdlib_bitsets - type(bitset_64) :: set0, set1, set2 - call set0 % init( 33 ) - call set1 % init( 33 ) - call set2 % init( 33 ) - call set1 % set( 0 ) - call set2 % set( 32 ) - if ( set1 > set0 .and. set2 > set1 .and. set2 > set0 .and. & - .not. set0 > set0 .and. .not. set0 > set1 .and. .not. & - set1 > set2 ) then - write(*,*) 'Passed 64 bit greater than tests.' - else - error stop 'Failed 64 bit greater than tests.' - end if - end program demo_gt +{!example/bitsets/example_bitsets_gt.f90!} ``` ### `<=` - compare two bitsets to determine whether the first is less than or equal to the other @@ -1923,23 +1480,7 @@ or the highest order different bit is set to 0 in `set1` and to 1 in #### Example ```fortran - program demo_le - use stdlib_bitsets - type(bitset_64) :: set0, set1, set2 - call set0 % init( 33 ) - call set1 % init( 33 ) - call set2 % init( 33 ) - call set1 % set( 0 ) - call set2 % set( 32 ) - if ( set0 <= set1 .and. set1 <= set2 .and. set0 <= set2 .and. & - set0 <= set0 .and. set1 <= set1 .and. set2 <= set2 .and. & - .not. set1 <= set0 .and. .not. set2 <= set0 .and. .not. & - set2 <= set1 ) then - write(*,*) 'Passed 64 bit less than or equal tests.' - else - error stop 'Failed 64 bit less than or equal tests.' - end if - end program demo_le +{!example/bitsets/example_bitsets_le.f90!} ``` ### `<` - compare two bitsets to determine whether the first is less than the other @@ -1986,20 +1527,5 @@ highest order different bit is set to 0 in `set1` and to 1 in `set2`, #### Example ```fortran - program demo_lt - use stdlib_bitsets - type(bitset_64) :: set0, set1, set2 - call set0 % init( 33 ) - call set1 % init( 33 ) - call set2 % init( 33 ) - call set1 % set( 0 ) - call set2 % set( 32 ) - if ( set0 < set1 .and. set1 < set2 .and. set0 < set2 .and. & - .not. set0 < set0 .and. .not. set2 < set0 .and. .not. & - set2 < set1 ) then - write(*,*) 'Passed 64 bit less than tests.' - else - error stop 'Failed 64 bit less than tests.' - end if - end program demo_lt +{!example/bitsets/example_bitsets_lt.f90!} ``` diff --git a/doc/specs/stdlib_error.md b/doc/specs/stdlib_error.md index 697e3f618..aca862b11 100644 --- a/doc/specs/stdlib_error.md +++ b/doc/specs/stdlib_error.md @@ -53,40 +53,16 @@ If `condition` is `.false.`, and: #### Examples ```fortran -program demo_check1 - use stdlib_error, only: check - implicit none - integer :: a = 1 - ! If a /= 5, stops the program with exit code 1 and prints 'Check failed.' - call check(a == 5) -end program demo_check1 +{!example/error/example_check1.f90!} ``` ```fortran -program demo_check2 - use stdlib_error, only: check - implicit none - integer :: a = 1 - ! If a /= 5, stops the program with exit code 1 and prints 'a == 5 failed.' - call check(a == 5, msg='a == 5 failed.') -end program demo_check2 +{!example/error/example_check2.f90!} ``` ```fortran -program demo_check3 - use stdlib_error, only: check - implicit none - integer :: a = 1 - ! If a /= 5, prints 'a == 5 failed.', but doesn't stop the program. - call check(a == 5, msg='a == 5 failed.', warn=.true.) -end program demo_check2 +{!example/error/example_check3.f90!} ``` ```fortran -program demo_check3 - use stdlib_error, only: check - implicit none - integer :: a = 1 - ! If a /= 5, stops the program with exit code 77 and prints 'a == 5 failed.' - call check(a == 5, msg='a == 5 failed.', code=77) -end program demo_check3 +{!example/error/example_check4.f90!} ``` ### `error_stop` - aborts the program @@ -118,19 +94,11 @@ Aborts the program with printing the message `msg` to `stderr` and a nonzero exi Without error code: ```fortran -program demo_error_stop1 - use stdlib_error, only: error_stop - implicit none - call error_stop("Invalid argument") -end program demo_error_stop1 +{!example/error/example_error_stop1.f90!} ``` With error code: ```fortran -program demo_error_stop2 - use stdlib_error, only: error_stop - implicit none - call error_stop("Invalid argument", code = 123) -end program demo_error_stop2 +{!example/error/example_error_stop2.f90!} ``` diff --git a/doc/specs/stdlib_hash_procedures.md b/doc/specs/stdlib_hash_procedures.md old mode 100755 new mode 100644 index 5d2e601a7..c790b1ef3 --- a/doc/specs/stdlib_hash_procedures.md +++ b/doc/specs/stdlib_hash_procedures.md @@ -543,19 +543,7 @@ E. Knuth. It multiplies the `key` by the odd valued approximation to ##### Example ```fortran -program demo_fibonacci_hash - use stdlib_hash_32bit, only: fibonacci_hash - use iso_fortran_env, only: int32 - implicit none - integer, allocatable :: array1(:) - integer(int32) :: hash, source - allocate( array1(0:2**6-1) ) - array1(:) = 0 - source = 42_int32 - hash = fibonacci_hash(source, 6) - array1(hash) = source - print *, hash -end program demo_fibonacci_hash +{!example/hash_procedures/example_fibonacci_hash.f90!} ``` #### `fnv_1_hash`- calculates a hash code from a key @@ -609,14 +597,7 @@ function for character strings. ##### Example ```fortran -program demo_fnv_1_hash - use stdlib_hash_32bit, only: fnv_1_hash - use iso_fortran_env, only: int32 - implicit none - integer(int32) :: hash - hash = fnv_1_hash([ 5, 4, 3, 1, 10, 4, 9]) - print *, hash -end program demo_fnv_1_hash +{!example/hash_procedures/example_fnv_1_hash.f90!} ``` @@ -670,14 +651,7 @@ function for character strings. ##### Example ```fortran -program demo_fnv_1a_hash - use stdlib_hash_32bit, only: fnv_1a_hash - use iso_fortran_env, only: int32 - implicit none - integer(int32) :: hash - hash = fnv_1a_hash( [ 5, 4, 3, 1, 10, 4, 9] ) - print *, hash -end program demo_fnv_1a_hash +{!example/hash_procedures/example_fnv_1a_hash.f90!} ``` @@ -844,17 +818,7 @@ function for character strings. ##### Example ```fortran -program demo_nmhash32 - use stdlib_hash_32bit, only: nmhash32, & - new_nmhash32_seed - use iso_fortran_env, only: int32 - implicit none - integer(int32) :: hash - integer(int32) :: seed = 42_int32 - call new_nmhash32_seed(seed) - hash = nmhash32([ 5, 4, 3, 1, 10, 4, 9], seed) - print *, seed, hash -end program demo_nmhash32 +{!example/hash_procedures/example_nmhash32.f90!} ``` @@ -906,17 +870,7 @@ function for character strings. ##### Example ```fortran -program demo_nmhash32x - use stdlib_hash_32bit, only: nmhash32x, & - new_nmhash32x_seed - use iso_fortran_env, only: int32 - implicit none - integer(int32) :: hash - integer(int32) :: seed = 42_int32 - call new_nmhash32x_seed(seed) - hash = nmhash32x([ 5, 4, 3, 1, 10, 4, 9], seed) - print *, seed, hash -end program demo_nmhash32x +{!example/hash_procedures/example_nmhash32x.f90!} ``` #### `odd_random_integer` - returns an odd integer @@ -999,24 +953,7 @@ It multiplies the `key` by `seed`, and returns the ##### Example ```fortran -program demo_universal_mult_hash - use stdlib_hash_32bit, only: odd_random_integer, & - universal_mult_hash - use iso_fortran_env, only: int32 - implicit none - integer, allocatable :: array1(:) - integer(int32) :: hash, i, seed, source - seed = 0 - allocate( array1(0:2**6-1) ) - do i = 0, 2**6-1 - array1(i) = i - end do - call odd_random_integer( seed ) - source = 42_int32 - hash = universal_mult_hash(source, seed, 6) - array1(hash) = source - print *, seed, hash, array1 -end program demo_odd_random_integer +{!example/hash_procedures/example_universal_mult_hash.f90!} ``` #### `water_hash`- calculates a hash code from a key and a seed @@ -1074,17 +1011,7 @@ function for character strings. ##### Example ```fortran -program demo_water_hash - use stdlib_hash_32bit, only: water_hash, & - new_water_hash_seed - use iso_fortran_env, only: int32, int64 - implicit none - integer(int32) :: hash - integer(int64) :: seed = 42_int64 - call new_water_hash_seed( seed ) - hash = water_hash([ 5, 4, 3, 1, 10, 4, 9], seed) - print *, hash, seed -end program demo_water_hash +{!example/hash_procedures/example_water_hash.f90!} ``` ## The `stdlib_hash_64bit` module @@ -1175,19 +1102,7 @@ E. Knuth. It multiplies the `key` by the odd valued approximation to ##### Example ```fortran -program demo_fibonacci_hash - use stdlib_hash_64bit, only: fibonacci_hash - use iso_fortran_env, only: int64 - implicit none - integer, allocatable :: array1(:) - integer(int64) :: hash, source - allocate( array1(0:2**6-1) ) - array1(:) = 0 - source = int(Z'1FFFFFFFF', int64) - hash = fibonacci_hash(source, 6) - array1(hash) = source - print *, hash -end program demo_fibonacci_hash +{!example/hash_procedures/example_fibonacci_hash_64.f90!} ``` #### `FNV_1`- calculates a hash code from a key @@ -1241,16 +1156,7 @@ function for character strings. ##### Example ```fortran -program demo_fnv_1_hash - use stdlib_hash_64bit, only: fnv_1_hash - use iso_fortran_env, only: int64 - implicit none - integer, allocatable :: array1(:) - integer(int64) :: hash - array1 = [ 5, 4, 3, 1, 10, 4, 9] - hash = fnv_1_hash(array1) - print *, hash -end program demo_fnv_1_hash +{!example/hash_procedures/example_fnv_1_hash_64.f90!} ``` @@ -1304,16 +1210,7 @@ function for character strings. ##### Example ```fortran -program demo_fnv_1a_hash - use stdlib_hash_64bit, only: fnv_1a_hash - use iso_fortran_env, only: int64 - implicit none - integer, allocatable :: array1(:) - integer(int64) :: hash - array1 = [ 5, 4, 3, 1, 10, 4, 9] - hash = fnv_1a_hash(array1) - print *, hash -end program demo_fnv_1a_hash +{!example/hash_procedures/example_fnv_1a_hash_64.f90!} ``` @@ -1473,19 +1370,7 @@ function for character strings. ##### Example ```fortran -program demo_pengy_hash - use stdlib_hash_64bit, only: new_pengy_hash_seed, pengy_hash - use iso_fortran_env, only: int32, int64 - implicit none - integer, allocatable :: key(:) - integer(int64) :: hash - integer(int32) :: seed - key = [ 0, 1, 2, 3 ] - seed = 0_int32 - call new_pengy_hash_seed( seed ) - hash = pengy_hash( key, seed ) - print *, seed, hash -end program demo_pengy_hash +{!example/hash_procedures/example_pengy_hash.f90!} ``` @@ -1535,19 +1420,7 @@ and has no known bad seeds. ##### Example ```fortran -program demo_spooky_hash - use stdlib_hash_64bit, only: new_spooky_hash_seed, & - spooky_hash - use iso_fortran_env, only: int64 - implicit none - integer, allocatable :: key(:) - integer(int64) :: hash(2), seed(2) - key = [ 0, 1, 2, 3 ] - seed = [ 119_int64, 2_int64**41-1 ] - call new_spooky_hash_seed( seed ) - hash = spooky_hash( key, seed ) - print *, seed, hash -end program demo_spooky_hash +{!example/hash_procedures/example_spooky_hash.f90!} ``` #### `universal_mult_hash` - maps an integer to a smaller number of bits @@ -1596,22 +1469,7 @@ It multiplies the `key` by `seed`, and returns the ```fortran -program demo_universal_mult_hash - use stdlib_hash_32bit, only: odd_random_integer, & - universal_mult_hash - use iso_fortran_env, only: int64 - implicit none - integer, allocatable :: array1(:) - integer(int64) :: hash, i, seed, source - seed = 0 - allocate( array1(0:2**6-1) ) - array1 = 0 - call odd_random_integer( seed ) - source = 42_int64 - hash = universal_mult_hash(source, seed, 6) - array1(hash) = source - print *, seed, hash, array1 -end program demo_universal_mult_hash +{!example/hash_procedures/example_universal_mult_hash_64.f90!} ``` diff --git a/doc/specs/stdlib_hashmaps.md b/doc/specs/stdlib_hashmaps.md index b0b56ed15..65dfb1351 100644 --- a/doc/specs/stdlib_hashmaps.md +++ b/doc/specs/stdlib_hashmaps.md @@ -227,18 +227,7 @@ is an `intent(out)` argument. ##### Example ```fortran - program demo_copy_key - use stdlib_hashmap_wrappers, only: & - copy_key, operator(==), key_type, set - use iso_fortran_env, only: int8 - implicit none - integer(int8) :: i, value(15) - type(key_type) :: old_key, new_key - value = [(i, i = 1, 15)] - call set( old_key, value ) - call copy_key( old_key, new_key ) - print *, "old_key == new_key = ", old_key == new_key - end program demo_copy_key +{!example/hashmaps/example_hashmaps_copy_key.f90!} ``` #### `copy_other` - Returns a copy of the other data @@ -270,27 +259,7 @@ is an `intent(out)` argument. ##### Example ```fortran - program demo_copy_other - use stdlib_hashmap_wrappers, only: copy_other, other_type - use iso_fortran_env, only: int8 - implicit none - type(other_type) :: other_in, other_out - integer(int8) :: i - type dummy_type - integer(int8) :: value(15) - end type - type(dummy_type) :: dummy_val - do i = 1, 15 - dummy_val % value1(i) = i - end do - allocate(other_in % value, source=dummy_val) - call copy_other( other_in, other_out ) - select type(out => other_out % value) - type is (dummy_type) - print *, "other_in == other_out = ", & - all( dummy_val % value == out % value ) - end select - end program demo_copy_other +{!example/hashmaps/example_hashmaps_copy_other.f90!} ``` @@ -356,19 +325,7 @@ expected to be minor compared to its faster hashing rate. ##### Example ```fortran - program demo_fnv_1_hasher - use stdlib_hashmap_wrappers, only: & - fnv_1_hasher, key_type, set - use iso_fortran_env, only: int32 - implicit none - integer(int8), allocatable :: array1(:) - integer(int32) :: hash - type(key_type) :: key - array1 = [ 5_int8, 4_int8, 3_int8, 1_int8, 10_int8, 4_int8 ] - call set( key, array1 ) - hash = fnv_1_hasher(key) - print *, hash - end program demo_fnv_1_hasher +{!example/hashmaps/example_hashmaps_fnv_1_hasher.f90!} ``` @@ -420,19 +377,7 @@ expected to be minor compared to its faster hashing rate. ##### Example ```fortran - program demo_fnv_1a_hasher - use stdlib_hashmap_wrappers, only: & - fnv_1a_hasher, key_type, set - use iso_fortran_env, only: int32 - implicit none - integer(int8), allocatable :: array1(:) - integer(int32) :: hash - type(key_type) :: key - array1 = [ 5_int8, 4_int8, 3_int8, 1_int8, 10_int8, 4_int8 ] - call set( key, array1 ) - hash = fnv_1a_hasher(key) - print *, hash - end program demo_fnv_1a_hasher +{!example/hashmaps/example_hashmaps_fnv_1a_hasher.f90!} ``` #### `free_key` - frees the memory associated with a key @@ -462,18 +407,7 @@ is an `intent(out)` argument. ##### Example ```fortran - program demo_free_key - use stdlib_hashmap_wrappers, only: & - copy_key, free_key, key_type, set - use iso_fortran_env, only: int8 - implicit none - integer(int8) :: i, value(15) - type(key_type) :: old_key, new_key - value = [(i, i=1, 15)] - call set( old_key, value ) - call copy_key( old_key, new_key ) - call free_key( old_key ) - end program demo_free_key +{!example/hashmaps/example_hashmaps_free_key.f90!} ``` #### `free_other` - frees the memory associated with other data @@ -503,24 +437,7 @@ is an `intent(out)` argument. ##### Example ```fortran - program demo_free_other - use stdlib_hashmap_wrappers, only: & - copy_other, free_other, other_type - use iso_fortran_env, only: int8 - implicit none - type dummy_type - integer(int8) :: value(15) - end type dummy_type - type(dummy_type) :: dummy_val - type(other_type), allocatable :: other_in, other_out - integer(int8) :: i - do i=1, 15 - dummy_val % value(i) = i - end do - allocate(other_in % value, source=dummy_val) - call copy_other( other_in, other_out ) - call free_other( other_out ) - end program demo_free_other +{!example/hashmaps/example_hashmaps_free_other.f90!} ``` @@ -564,22 +481,7 @@ an allocatable of `class(*)`. It is an `intent(out)` argument. ##### Example ```fortran - program demo_get - use stdlib_hashmap_wrappers, only: & - get, key_type, set - use iso_fortran_env, only: int8 - implicit none - integer(int8), allocatable :: value(:), result(:) - type(key_type) :: key - integer(int8) :: i - allocate( value(1:15) ) - do i=1, 15 - value(i) = i - end do - call set( key, value ) - call get( key, result ) - print *, 'RESULT == VALUE = ', all( value == result ) - end program demo_get +{!example/hashmaps/example_hashmaps_get.f90!} ``` @@ -623,21 +525,7 @@ pointers intended for use as a hash function for the hash maps. ##### Example ```fortran - program demo_hasher_fun - use stdlib_hashmap_wrappers, only: & - fnv_1a_hasher, hasher_fun, set - use iso_fortran_env, only: int8, int32 - implicit none - type(hasher_fun), pointer :: hasher_pointer - integer(int8), allocatable :: array1(:) - integer(int32) :: hash - type(key_type) :: key - hasher_pointer => fnv_1a_hasher - array1 = [ 5_int8, 4_int8, 3_int8, 1_int8, 10_int8, 4_int8 ] - call set( key, array1 ) - hash = hasher_pointer(key) - print *, hash - end program demo_hasher_fun +{!example/hashmaps/example_hashmaps_hasher_fun.f90!} ``` #### `operator(==)` - Compares two keys for equality @@ -677,20 +565,7 @@ The result is `.true.` if the keys are equal, otherwise `.falss.`. ##### Example ```fortran - program demo_equal_keys - use stdlib_hashmap_wrappers, only: & - copy_key, operator(==), key_type, set - use iso_fortran_env, only: int8 - implicit none - integer(int8) :: i, value(15) - type(key_type) :: old_key, new_key - do i=1, 15 - value(i) = i - end do - call set( old_key, value ) - call copy_key( old_key, new_key ) - print *, "old_key == new_key = ", old_key == new_key - end program demo_equal_keys +{!example/hashmaps/example_hashmaps_equal_keys.f90!} ``` #### `seeded_nmhash32_hasher`- calculates a hash code from a key @@ -740,19 +615,7 @@ This code passes the SMHasher tests. ##### Example ```fortran - program demo_seeded_nmhash32_hasher - use stdlib_hashmap_wrappers, only: & - seeded_nmhash32_hasher, key_type, set - use iso_fortran_env, only: int32 - implicit none - integer(int8), allocatable :: array1(:) - integer(int32) :: hash - type(key_type) :: key - array1 = [ 5_int8, 4_int8, 3_int8, 1_int8, 10_int8, 4_int8 ] - call set( key, array1 ) - hash = seeded_nmhash32_hasher (key) - print *, hash - end program demo_seeded_nmhash32_hasher +{!example/hashmaps/example_hashmaps_seeded_nmhash32_hasher.f90!} ``` #### `seeded_nmhash32x_hasher`- calculates a hash code from a key @@ -801,19 +664,7 @@ This code passes the SMHasher tests. ##### Example ```fortran - program demo_seeded_nmhash32x_hasher - use stdlib_hashmap_wrappers, only: & - seeded_nmhash32x_hasher, key_type, set - use iso_fortran_env, only: int32 - implicit none - integer(int8), allocatable :: array1(:) - integer(int32) :: hash - type(key_type) :: key - array1 = [ 5_int8, 4_int8, 3_int8, 1_int8, 10_int8, 4_int8 ] - call set( key, array1 ) - hash = seeded_nmhash32x_hasher (key) - print *, hash - end program demo_seeded_nmhash32x_hasher +{!example/hashmaps/example_hashmaps_seeded_nmhash32x_hasher.f90!} ``` #### `seeded_water_hasher`- calculates a hash code from a key @@ -863,19 +714,7 @@ This code passes the SMHasher tests. ##### Example ```fortran - program demo_seeded_water_hasher - use stdlib_hashmap_wrappers, only: & - seeded_water_hasher, key_type, set - use iso_fortran_env, only: int32 - implicit none - integer(int8), allocatable :: array1(:) - integer(int32) :: hash - type(key_type) :: key - array1 = [ 5_int8, 4_int8, 3_int8, 1_int8, 10_int8, 4_int8 ] - call set( key, array1 ) - hash = seeded_water_hasher (key) - print *, hash - end program demo_seeded_water_hasher +{!example/hashmaps/example_hashmaps_seeded_water_hasher.f90!} ``` @@ -924,22 +763,7 @@ value to an `int8` vector. ##### Example ```fortran - program demo_set - use stdlib_hashmap_wrappers, only: & - get, key_type, set - use iso_fortran_env, only: int8 - implicit none - integer(int8), allocatable :: value(:), result(:) - type(key_type) :: key - integer(int8) :: i - allocate( value(1:15) ) - do i=1, 15 - value(i) = i - end do - call set( key, value ) - call get( key, result ) - print *, 'RESULT == VALUE = ', all( value == result ) - end program demo_set +{!example/hashmaps/example_hashmaps_set.f90!} ``` @@ -1385,16 +1209,7 @@ The result will be the number of procedure calls on the hash map. ##### Example ```fortran - program demo_calls - use stdlib_hashmaps, only: chaining_hashmap_type, int_calls - use stdlib_hashmap_wrappers, only: fnv_1_hasher - implicit none - type(chaining_hashmap_type) :: map - integer(int_calls) :: initial_calls - call map % init( fnv_1_hasher ) - initial_calls = map % calls() - print *, "INITIAL_CALLS = ", initial_calls - end program demo_calls +{!example/hashmaps/example_hashmaps_calls.f90!} ``` @@ -1432,16 +1247,7 @@ The result will be the number of entries in the hash map. ##### Example ```fortran - program demo_entries - use stdlib_hashmaps, only: open_hashmap_type, int_index - use stdlib_hashmap_wrappers, only: fnv_1_hasher - implicit none - type(open_hashmap_type) :: map - type(int_index) :: initial_entries - call map % init( fnv_1_hasher ) - initial_entries = map % entries () - print *, "INITIAL_ENTRIES = ", initial_entries - end program demo_entries +{!example/hashmaps/example_hashmaps_entries.f90!} ``` @@ -1489,39 +1295,7 @@ undefined. ```fortran - program demo_get_other_data - use, intrinsic:: iso_fortran_env, only: & - int8 - use stdlib_hashmaps, only: chaining_hashmap_type, int_index - use stdlib_hashmap_wrappers, only: fnv_1_hasher, key_type, other_type - logical :: conflict, exists - type(key_type) :: key - type(other_type) :: other - type(chaining_hashmap_type) :: map - type dummy_type - integer(int8) :: value(4) - end type dummy_type - type(dummy_type) :: dummy - class(*), allocatable :: data - dummy % value = [ 4_int8, 3_int8, 2_int8, 1_int8 ] - allocate( data, source=dummy ) - call map % init( fnv_1_hasher ) - call set( key, [ 0_int8, 1_int8, 2_int8, 3_int8, 4_int8 ] ) - call set( other, data ) - call map % map_entry( key, other, conflict ) - if ( .not. conflict ) then - call map % get_other_data( key, other ) - else - stop 'Key is already present in the map.' - end if - call get( other, data ) - select type( data ) - type is (dummy_type) - print *, 'Other data % value = ', data % value - class default - print *, 'Invalid data type in other' - end select - end program demo_get_other_data +{!example/hashmaps/example_hashmaps_get_other_data.f90!} ``` @@ -1584,13 +1358,7 @@ has the value `alloc_fault`. ##### Example ```fortran - program demo_init - use stdlib_hashmaps, only: chaining_hashmap_type - use stdlib_hashmap_wrappers, only: fnv_1_hasher - implicit none - type(chaining_hashmap_type) :: map - call map % init( fnv_1_hasher, slots_bits=10 ) - end program demo_init +{!example/hashmaps/example_hashmaps_init.f90!} ``` @@ -1632,19 +1400,7 @@ is being examined. ##### Example ```fortran - program demo_key_test - use stdlib_kinds, only: int8 - use stdlib_hashmaps, only: chaining_hashmap_type - use stdlib_hashmap_wrappers, only: fnv_1_hasher, key_type - implicit none - type(chaining_hashmap_type) :: map - type(key_type) :: key - logocal :: present - call map % init( fnv_1_hasher ) - call set(key, [0_int8, 1_int8] ) - call map % key_test ( key, present ) - print *, "Initial key of 10 present for empty map = ", present - end program demo_key_test +{!example/hashmaps/example_hashmaps_key_test.f90!} ``` @@ -1684,16 +1440,7 @@ number of slots in the hash map. ##### Example ```fortran - program demo_loading - use stdlib_hashmaps, only: open_hashmap_type - use stdlib_hashmap_wrappers, only: fnv_1_hasher - implicit none - type(open_hashmap_type) :: map - real :: ratio - call map % init( fnv_1_hasher ) - ratio = map % loading () - print *, "Initial loading = ", ratio - end program demo_loading +{!example/hashmaps/example_hashmaps_loading.f90!} ``` #### `map_entry` - inserts an entry into the hash map @@ -1743,22 +1490,7 @@ is ignored. ##### Example ```fortran - program demo_map_entry - use, intrinsic:: iso_fortran_env, only: int8 - use stdlib_hashmaps, only: chaining_hashmap_type - use stdlib_hashmap_wrappers, only: fnv_1_hasher, key_type, other_type, set - type(chaining_hashmap_type) :: map - type(key_type) :: key - logical :: conflict - type(other_type) :: other - class(*), allocatable :: dummy - allocate( dummy, source=4 ) - call map % init( fnv_1_hasher, slots_bits=10 ) - call set( key, [ 5_int8, 7_int8, 4_int8, 13_int8 ] ) - call set( other, dummy ) - call map % map_entry( key, other, conflict ) - print *, 'CONFLICT = ', conflict - end program demo_map_entry +{!example/hashmaps/example_hashmaps_map_entry.f90!} ``` #### `map_probes` - returns the number of hash map probes @@ -1797,16 +1529,7 @@ rehashing. ##### Example ```fortran - program demo_probes - use stdlib_hashmaps, only: chaining_hashmap_type, int_index - use stdlib_hashmap_wrappers: fnv_1_hasher - implicit none - type(chaining_hashmap_type) :: map - real :: nprobes - call map % init( fnv_1_hasher ) - nprobes = map % map_probes() - print *, "Initial probes = ", nprobes - end program demo_probes +{!example/hashmaps/example_hashmaps_probes.f90!} ``` #### `num_slots` - returns the number of hash map slots. @@ -1844,16 +1567,7 @@ The result is the number of slots in `map`. ##### Example ```fortran - program demo_num_slots - use stdlib_hashmaps, only: chaining_hashmap_type, int_index - use stdlib_hashmap_wrappers, only: fnv_1_hasher - implicit none - type(chaining_hashmap_type) :: map - integer(int_index) :: initial_slots - call map % init( fnv_1_hasher ) - initial_slots = map % num_slots () - print *, "Initial slots = ", initial_slots - end program demo_num_slots +{!example/hashmaps/example_hashmaps_num_slots.f90!} ``` @@ -1888,23 +1602,7 @@ It is the hash method to be used by `map`. ##### Example ```fortran - program demo_rehash - use stdlib_kinds, only: int8 - use stdlib_hashmaps, only: open_hashmap_type - use stdlib_hashmap_wrappers, only: fnv_1_hasher, fnv_1a_hasher,& - key_type, other_type, set - implicit none - type(open_hashmap_type) :: map - type(key_type) :: key - type(other_type) :: other - class(*), allocatable :: dummy - allocate( dummy, source='a dummy value' ) - call map % init( fnv_1_hasher, slots_bits=10 ) - call set( key, [ 5_int8, 7_int8, 4_int8, 13_int8 ] ) - call set( other, dummy ) - call map % map_entry( key, other ) - call map % rehash( fnv_1a_hasher ) - end program demo_rehash +{!example/hashmaps/example_hashmaps_rehash.f90!} ``` #### `remove` - removes an entry from the hash map @@ -1945,23 +1643,7 @@ absent, the procedure returns with no entry with the given key. ##### Example ```fortran - program demo_remove - use stdlib_hashmaps, only: open_hashmap_type, int_index - use stdlib_hashmap_wrappers, only: fnv_1_hasher, & - fnv_1a_hasher, key_type, other_type - type(open_hashmap_type) :: map - type(key_type) :: key - type(other_type) :: other - logical :: existed - class(*), allocatable :: dummy - allocate( dummy, source=4.0 ) - call map % init( fnv_1_hasher, slots_bits=10 ) - call set( key, [ 5_int8, 7_int8, 4_int8, 13_int8 ] ) - call set( other, dummy ) - call map % map_entry( key, other ) - call map % remove( key, existed ) - print *, "Removed key existed = ", existed - end program demo_remove +{!example/hashmaps/example_hashmaps_remove.f90!} ``` #### `set_other_data` - replaces the other data for an entry @@ -2008,28 +1690,7 @@ not exist and nothing was done. ##### Example ```fortran - program demo_set_other_data - use stdlib_kinds, only: int8 - use stdlib_hashmaps, only: open_hashmap_type - use stdlib_hashmap_wrappers, only: fnv_1_hasher, & - fnv_1a_hasher, key_type, other_type, set - implicit none - logical :: exists - type(open_hashmap_type) :: map - type(key_type) :: key - type(other_type) :: other - class(*), allocatable :: dummy - call map % init( fnv_1_hasher, slots_bits=10 ) - allocate( dummy, source='A value' ) - call set( key, [ 5_int8, 7_int8, 4_int8, 13_int8 ] ) - call set( other, dummy ) - call map % map_entry( key, other ) - deallocate( dummy ) - allocate( dummy, source='Another value' ) - call set( other, dummy ) - call map % set_other_data( key, other, exists ) - print *, 'The entry to have its other data replaced exists = ', exists - end program demo_set_other_data +{!example/hashmaps/example_hashmaps_set_other_data.f90!} ``` #### `slots_bits` - returns the number of bits used to address the hash map slots @@ -2067,16 +1728,7 @@ The result is the number of bits used in addressing the slots in `map`. ##### Example ```fortran - program demo_slots_bits - use stdlib_hashmaps, only: chaining_hashmap_type - use stdlib_hashmap_wrappers, only: fnv_1_hasher - implicit none - type(chaining_hashmap_type) :: map - integer :: bits - call map % init( fnv_1_hasher ) - bits = map % slots_bits () - print *, "Initial slot bits = ", bits - end program demo_slots_bits +{!example/hashmaps/example_hashmaps_slots_bits.f90!} ``` @@ -2117,14 +1769,5 @@ from their slot index the map. ##### Example ```fortran - program demo_total_depth - use stdlib_hashmaps, only: chaining_hashmap_type, int_depth - use stdlib_hashmap_wrappers, only: fnv_1_hasher - implicit none - type(chaining_hashmap_type) :: map - integer(int_depth) :: initial_depth - call map % init( fnv_1_hasher ) - initial_depth = map % total_depth () - print *, "Initial total depth = ", initial_depth - end program demo_total_depth +{!example/hashmaps/example_hashmaps_total_depth.f90!} ``` diff --git a/doc/specs/stdlib_io.md b/doc/specs/stdlib_io.md index 6fb30a97d..3ed75d638 100644 --- a/doc/specs/stdlib_io.md +++ b/doc/specs/stdlib_io.md @@ -36,12 +36,7 @@ Returns an allocated rank-2 `array` with the content of `filename`. ### Example ```fortran -program demo_loadtxt - use stdlib_io, only: loadtxt - implicit none - real, allocatable :: x(:,:) - call loadtxt('example.dat', x) -end program demo_loadtxt +{!example/io/example_loadtxt.f90!} ``` @@ -91,14 +86,7 @@ The result is a scalar of type `integer`. ### Example ```fortran -program demo_open - use stdlib_io, only: open - implicit none - integer :: u - u = open('example.dat', 'wt') - write(u,'(a)')'This is an example for open' - close(u) -end program demo_open +{!example/io/example_open.f90!} ``` @@ -128,12 +116,7 @@ Provides a text file called `filename` that contains the rank-2 `array`. ### Example ```fortran -program demo_savetxt - use stdlib_io, only: savetxt - implicit none - real :: x(3,2) = 1 - call savetxt('example.dat', x) -end program demo_savetxt +{!example/io/example_savetxt.f90!} ``` @@ -174,12 +157,7 @@ Returns an allocated `array` with the content of `filename` in case of success. ### Example ```fortran -program demo_loadnpy - use stdlib_io_npy, only: load_npy - implicit none - real, allocatable :: x(:,:) - call load_npy('example.npy', x) -end program demo_loadnpy +{!example/io/example_loadnpy.f90!} ``` @@ -220,12 +198,7 @@ Provides a npy file called `filename` that contains the rank-2 `array`. ### Example ```fortran -program demo_savenpy - use stdlib_io_npy, only: save_npy - implicit none - real :: x(3,2) = 1 - call save_npy('example.npy', x) -end program demo_savenpy +{!example/io/example_savenpy.f90!} ``` ## `getline` @@ -263,19 +236,7 @@ Read a whole line from a formatted unit into a string variable ### Example ```fortran -program demo_getline - use, intrinsic :: iso_fortran_env, only : input_unit, output_unit - use stdlib_io, only: getline - implicit none - character(len=:), allocatable :: line - integer :: stat - - call getline(input_unit, line, stat) - do while(stat == 0) - write(output_unit, '(a)') line - call getline(input_unit, line, stat) - end do -end program demo_getline +{!example/io/example_getline.f90!} ``` ## Formatting constants @@ -292,30 +253,5 @@ Provides formats for all kinds as defined in the `stdlib_kinds` module. ### Example ```fortran -program demo_fmt_constants - use stdlib_kinds, only : int32, int64, sp, dp - use stdlib_io, only : FMT_INT, FMT_REAL_SP, FMT_REAL_DP, FMT_COMPLEX_SP, FMT_COMPLEX_DP - implicit none - - integer(kind=int32) :: i32 - integer(kind=int64) :: i64 - real(kind=sp) :: r32 - real(kind=dp) :: r64 - complex(kind=sp) :: c32 - complex(kind=dp) :: c64 - - i32 = 100_int32 - i64 = 100_int64 - r32 = 100.0_sp - r64 = 100.0_dp - c32 = cmplx(100.0_sp, kind=sp) - c64 = cmplx(100.0_dp, kind=dp) - - print "(2("//FMT_INT//",1x))", i32, i64 ! outputs: 100 100 - print FMT_REAL_SP, r32 ! outputs: 1.00000000E+02 - print FMT_REAL_DP, r64 ! outputs: 1.0000000000000000E+002 - print FMT_COMPLEX_SP, c32 ! outputs: 1.00000000E+02 0.00000000E+00 - print FMT_COMPLEX_DP, c64 ! outputs: 1.0000000000000000E+002 0.0000000000000000E+000 - -end program demo_fmt_constants +{!example/io/example_fmt_constants.f90!} ``` diff --git a/doc/specs/stdlib_linalg.md b/doc/specs/stdlib_linalg.md index 749aeed10..f7569e0c6 100644 --- a/doc/specs/stdlib_linalg.md +++ b/doc/specs/stdlib_linalg.md @@ -33,66 +33,23 @@ Returns a diagonal array or a vector with the extracted diagonal elements. ### Example ```fortran -program demo_diag1 - use stdlib_linalg, only: diag - implicit none - real, allocatable :: A(:,:) - integer :: i - A = diag([(1,i=1,10)]) ! creates a 10 by 10 identity matrix -end program demo_diag1 +{!example/linalg/example_diag1.f90!} ``` ```fortran -program demo_diag2 - use stdlib_linalg, only: diag - implicit none - real :: v(:) - real, allocatable :: A(:,:) - integer :: i - v = [1,2,3,4,5] - A = diag(v) ! creates a 5 by 5 matrix with elements of v on the diagonal -end program demo_diag2 +{!example/linalg/example_diag2.f90!} ``` ```fortran -program demo_diag3 - use stdlib_linalg, only: diag - implicit none - integer, parameter :: n = 10 - real :: c(n), ul(n-1) - real :: A(n,n) - integer :: i - c = 2 - ul = -1 - A = diag(ul,-1) + diag(c) + diag(ul,1) ! Gil Strang's favorite matrix -end program demo_diag3 +{!example/linalg/example_diag3.f90!} ``` ```fortran -program demo_diag4 - use stdlib_linalg, only: diag - implicit none - integer, parameter :: n = 12 - real :: A(n,n) - real :: v(n) - integer :: i - call random_number(A) - v = diag(A) ! v contains diagonal elements of A -end program demo_diag4 +{!example/linalg/example_diag4.f90!} ``` ```fortran -program demo_diag5 - use stdlib_linalg, only: diag - implicit none - integer, parameter :: n = 3 - real :: A(n,n) - real, allocatable :: v(:) - integer :: i - A = reshape([1,2,3,4,5,6,7,8,9],[n,n]) - v = diag(A,-1) ! v is [2,6] - v = diag(A,1) ! v is [4,8] -end program demo_diag5 +{!example/linalg/example_diag5.f90!} ``` ## `eye` - Construct the identity matrix @@ -130,7 +87,6 @@ The use of `int8` was suggested to save storage. Since the result of `eye` is of `integer(int8)` type, one should be careful about using it in arithmetic expressions. For example: ```fortran -real :: A(:,:) !> Be careful A = eye(2,2)/2 !! A == 0.0 !> Recommend @@ -140,28 +96,11 @@ A = eye(2,2)/2.0 !! A == diag([0.5, 0.5]) ### Example ```fortran -program demo_eye1 - use stdlib_linalg, only: eye - implicit none - integer :: i(2,2) - real :: a(3,3) - real :: b(2,3) !! Matrix is non-square. - complex :: c(2,2) - I = eye(2) !! [1,0; 0,1] - A = eye(3) !! [1.0,0.0,0.0; 0.0,1.0,0.0; 0.0,0.0,1.0] - A = eye(3,3) !! [1.0,0.0,0.0; 0.0,1.0,0.0; 0.0,0.0,1.0] - B = eye(2,3) !! [1.0,0.0,0.0; 0.0,1.0,0.0] - C = eye(2,2) !! [(1.0,0.0),(0.0,0.0); (0.0,0.0),(1.0,0.0)] - C = (1.0,1.0)*eye(2,2) !! [(1.0,1.0),(0.0,0.0); (0.0,0.0),(1.0,1.0)] -end program demo_eye1 +{!example/linalg/example_eye1.f90!} ``` ```fortran -program demo_eye2 - use stdlib_linalg, only: eye, diag - implicit none - print *, all(eye(4) == diag([1,1,1,1])) ! prints .true. -end program demo_eye2 +{!example/linalg/example_eye2.f90!} ``` ## `trace` - Trace of a matrix @@ -188,13 +127,7 @@ Returns the trace of the matrix, i.e. the sum of diagonal elements. ### Example ```fortran -program demo_trace - use stdlib_linalg, only: trace - implicit none - real :: A(3,3) - A = reshape([1,2,3,4,5,6,7,8,9],[3,3]) - print *, trace(A) ! 1 + 5 + 9 -end program demo_trace +{!example/linalg/example_trace.f90!} ``` ## `outer_product` - Computes the outer product of two vectors @@ -224,15 +157,7 @@ Returns a rank-2 array equal to `u v^T` (where `u, v` are considered column vect ### Example ```fortran -program demo_outer_product - use stdlib_linalg, only: outer_product - implicit none - real, allocatable :: A(:,:), u(:), v(:) - u = [1., 2., 3. ] - v = [3., 4.] - A = outer_product(u,v) - !A = reshape([3., 6., 9., 4., 8., 12.], [3,2]) -end program demo_outer_product +{!example/linalg/example_outer_product.f90!} ``` ## `is_square` - Checks if a matrix is square @@ -260,16 +185,7 @@ Returns a `logical` scalar that is `.true.` if the input matrix is square, and ` ### Example ```fortran -program demo_is_square - use stdlib_linalg, only: is_square - implicit none - real :: A(2,2), B(3,2) - logical :: res - A = reshape([1., 2., 3., 4.], shape(A)) - B = reshape([1., 2., 3., 4., 5., 6.], shape(B)) - res = is_square(A) ! returns .true. - res = is_square(B) ! returns .false. -end program demo_is_square +{!example/linalg/example_is_square.f90!} ``` ## `is_diagonal` - Checks if a matrix is diagonal @@ -298,16 +214,7 @@ Note that nonsquare matrices may be diagonal, so long as `a_ij = 0` when `i /= j ### Example ```fortran -program demo_is_diagonal - use stdlib_linalg, only: is_diagonal - implicit none - real :: A(2,2), B(2,2) - logical :: res - A = reshape([1., 0., 0., 4.], shape(A)) - B = reshape([1., 0., 3., 4.], shape(B)) - res = is_diagonal(A) ! returns .true. - res = is_diagonal(B) ! returns .false. -end program demo_is_diagonal +{!example/linalg/example_is_diagonal.f90!} ``` ## `is_symmetric` - Checks if a matrix is symmetric @@ -335,16 +242,7 @@ Returns a `logical` scalar that is `.true.` if the input matrix is symmetric, an ### Example ```fortran -program demo_is_symmetric - use stdlib_linalg, only: is_symmetric - implicit none - real :: A(2,2), B(2,2) - logical :: res - A = reshape([1., 3., 3., 4.], shape(A)) - B = reshape([1., 0., 3., 4.], shape(B)) - res = is_symmetric(A) ! returns .true. - res = is_symmetric(B) ! returns .false. -end program demo_is_symmetric +{!example/linalg/example_is_symmetric.f90!} ``` ## `is_skew_symmetric` - Checks if a matrix is skew-symmetric @@ -372,16 +270,7 @@ Returns a `logical` scalar that is `.true.` if the input matrix is skew-symmetri ### Example ```fortran -program demo_is_skew_symmetric - use stdlib_linalg, only: is_skew_symmetric - implicit none - real :: A(2,2), B(2,2) - logical :: res - A = reshape([0., -3., 3., 0.], shape(A)) - B = reshape([0., 3., 3., 0.], shape(B)) - res = is_skew_symmetric(A) ! returns .true. - res = is_skew_symmetric(B) ! returns .false. -end program demo_is_skew_symmetric +{!example/linalg/example_is_skew_symmetric.f90!} ``` ## `is_hermitian` - Checks if a matrix is Hermitian @@ -409,16 +298,7 @@ Returns a `logical` scalar that is `.true.` if the input matrix is Hermitian, an ### Example ```fortran -program demo_is_hermitian - use stdlib_linalg, only: is_hermitian - implicit none - complex :: A(2,2), B(2,2) - logical :: res - A = reshape([cmplx(1.,0.), cmplx(3.,-1.), cmplx(3.,1.), cmplx(4.,0.)], shape(A)) - B = reshape([cmplx(1.,0.), cmplx(3.,1.), cmplx(3.,1.), cmplx(4.,0.)], shape(B)) - res = is_hermitian(A) ! returns .true. - res = is_hermitian(B) ! returns .false. -end program demo_is_hermitian +{!example/linalg/example_is_hermitian.f90!} ``` ## `is_triangular` - Checks if a matrix is triangular @@ -450,16 +330,7 @@ Specifically, upper triangular matrices satisfy `a_ij = 0` when `j < i`, and low ### Example ```fortran -program demo_is_triangular - use stdlib_linalg, only: is_triangular - implicit none - real :: A(3,3), B(3,3) - logical :: res - A = reshape([1., 0., 0., 4., 5., 0., 7., 8., 9.], shape(A)) - B = reshape([1., 0., 3., 4., 5., 0., 7., 8., 9.], shape(B)) - res = is_triangular(A,'u') ! returns .true. - res = is_triangular(B,'u') ! returns .false. -end program demo_is_triangular +{!example/linalg/example_is_triangular.f90!} ``` ## `is_hessenberg` - Checks if a matrix is hessenberg @@ -491,14 +362,5 @@ Specifically, upper Hessenberg matrices satisfy `a_ij = 0` when `j < i-1`, and l ### Example ```fortran -program demo_is_hessenberg - use stdlib_linalg, only: is_hessenberg - implicit none - real :: A(3,3), B(3,3) - logical :: res - A = reshape([1., 2., 0., 4., 5., 6., 7., 8., 9.], shape(A)) - B = reshape([1., 2., 3., 4., 5., 6., 7., 8., 9.], shape(B)) - res = is_hessenberg(A,'u') ! returns .true. - res = is_hessenberg(B,'u') ! returns .false. -end program demo_is_hessenberg +{!example/linalg/example_is_hessenberg.f90!} ``` diff --git a/doc/specs/stdlib_logger.md b/doc/specs/stdlib_logger.md index 60a823406..7e6cf6c8e 100644 --- a/doc/specs/stdlib_logger.md +++ b/doc/specs/stdlib_logger.md @@ -195,18 +195,7 @@ an `intent(in)` argument. It shall be the name of the file to be opened. #### Example ```fortran -program demo_global_logger - use stdlib_logger, global => global_logger - - integer :: unit, stat - - call global % add_log_file( 'error_log.txt', unit, & - position='asis', stat=stat ) - if ( stat /= success ) then - error stop 'Unable to open "error_log.txt".' - end if - -end program demo_global_logger +{!example/logger/example_global_logger.f90!} ``` ### `add_log_unit` - add a unit to the array `self % log_units` @@ -262,28 +251,7 @@ to `unit`. #### Example ```fortran -program demo_add_log_unit - use stdlib_logger, only: global_logger, read_only_error - - character(256) :: iomsg - integer :: iostat, unit, stat - - open( newunit=unit, 'error_log.txt', & - form='formatted', status='replace', & - position='rewind', err=999, & - action='read', iostat=iostat, iomsg=iomsg ) - - call global_logger % add_log_unit( unit, stat ) - select case ( stat ) - - case ( read_only_error ) - error stop 'Unable to write to "error_log.txt".' - - end select - - 999 error stop 'Unable to open "error_log.txt". - -end program demo_add_log_unit +{!example/logger/example_add_log_unit.f90!} ``` ### `configuration` - report a logger's configuration @@ -410,12 +378,7 @@ Pure subroutine #### Example ```fortran -program demo_configure - use stdlib_logger, only: global => global_logger - - call global % configure( indent=.false., max_width=72 ) - -end program demo_configure +{!example/logger/example_configure.f90!} ``` ### `log_debug` - Writes the string `message` to `self % log_units` @@ -705,26 +668,7 @@ Subroutine #### Example ```fortran -program demo_log_io_error - use stdlib_logger, global=>global_logger - - character(*), parameter :: filename = 'dummy.txt' - integer :: iostat, lun - character(128) :: iomsg - character(*), parameter :: message = & - 'Failure in opening "dummy.txt".' - - open( newunit=lun, file = filename, form='formatted', & - status='old', iostat=iostat, iomsg=iomsg ) - if ( iostat /= 0 ) then - call global % log_io_error( message, & - procedure = 'EXAMPLE', & - iostat=iostat, & - iomsg = iomsg ) - error stop 'Error on opening a file' - end if - -end program demo_log_io_error +{!example/logger/example_log_io_error.f90!} ``` ### `log_message` - write the string `message` to `self % log_units` @@ -873,30 +817,7 @@ Subroutine #### Example ```fortran -program demo_log_text_error - use stdlib_logger - - character(*), parameter :: filename = 'dummy.txt' - integer :: col_no, line_no, lun - character(128) :: line - character(*), parameter :: message = 'Bad text found.' - - open( newunit=lun, file = filename, statu='old', & - form='formatted' ) - line_no = 0 - do - read( lun, fmt='(a)', end=900 ) line - line_no = line_no + 1 - call check_line( line, status, col_no ) - if ( status /= 0 ) - call global_logger % log_text_error( line, & - col_no, message, filename, line_no ) - error stop 'Error in reading ' // filename - end if - end do - 900 continue - -end program demo_log_text_error +{!example/logger/example_log_text_error.f90!} ``` ### `log_units_assigned` - returns the number of active I/O units diff --git a/doc/specs/stdlib_math.md b/doc/specs/stdlib_math.md index 9091b836d..eb7b7cc2f 100644 --- a/doc/specs/stdlib_math.md +++ b/doc/specs/stdlib_math.md @@ -51,44 +51,14 @@ The output is a scalar of `type` and `kind` same as to that of the arguments. Here inputs are of type `integer` and kind `int32` ```fortran -program demo_clip_integer - use stdlib_math, only: clip - use stdlib_kinds, only: int32 - implicit none - integer(int32) :: x - integer(int32) :: xmin - integer(int32) :: xmax - integer(int32) :: clipped_value - - xmin = -5_int32 - xmax = 5_int32 - x = 12_int32 - - clipped_value = clip(x, xmin, xmax) - ! clipped_value <- 5 -end program demo_clip_integer +{!example/math/example_clip_integer.f90!} ``` ##### Example 2: Here inputs are of type `real` and kind `sp` ```fortran -program demo_clip_real - use stdlib_math, only: clip - use stdlib_kinds, only: sp - implicit none - real(sp) :: x - real(sp) :: xmin - real(sp) :: xmax - real(sp) :: clipped_value - - xmin = -5.769_sp - xmax = 3.025_sp - x = 3.025_sp - - clipped_value = clip(x, xmin, xmax) - ! clipped_value <- 3.02500010 -end program demo_clip_real +{!example/math/example_clip_real.f90!} ``` ### `gcd` function @@ -125,15 +95,7 @@ Returns an integer of the same `kind` as that of the arguments. ##### Example 1: ```fortran -program demo_gcd - use stdlib_math, only: gcd - implicit none - integer :: a, b, c - - a = 48 - b = 18 - c = gcd(a, b) ! returns 6 -end program demo_gcd +{!example/math/example_gcd.f90!} ``` ### `linspace` - Create a linearly spaced rank one array @@ -176,36 +138,14 @@ If `start`/`end` are `integer` types, the `result` will default to a `real(dp)` Here inputs are of type `complex` and kind `dp` ```fortran -program demo_linspace_complex - use stdlib_math, only: linspace - use stdlib_kinds, only: dp - implicit none - - complex(dp) :: start = complex(10.0_dp, 5.0_dp) - complex(dp) :: end = complex(-10.0_dp, 15.0_dp) - - complex(dp) :: z(11) - - z = linspace(start, end, 11) -end program demo_linspace_complex +{!example/math/example_linspace_complex.f90!} ``` ##### Example 2: Here inputs are of type `integer` and kind `int16`, with the result defaulting to `real(dp)`. ```fortran -program demo_linspace_int16 - use stdlib_math, only: linspace - use stdlib_kinds, only: int16, dp - implicit none - - integer(int16) :: start = 10_int16 - integer(int16) :: end = 23_int16 - - real(dp) :: r(15) - - r = linspace(start, end, 15) -end program demo_linspace_int16 +{!example/math/example_linspace_int16.f90!} ``` ### `logspace` - Create a logarithmically spaced rank one array @@ -267,58 +207,21 @@ For function calls where the `base` is specified, the `type` and `kind` of the r Here inputs are of type `complex` and kind `dp`. `n` and `base` is not specified and thus default to 50 and 10, respectively. ```fortran -program demo_logspace_complex - use stdlib_math, only: logspace - use stdlib_kinds, only: dp - implicit none - - complex(dp) :: start = (10.0_dp, 5.0_dp) - complex(dp) :: end = (-10.0_dp, 15.0_dp) - - complex(dp) :: z(11) ! Complex values raised to complex powers results in complex values - - z = logspace(start, end, 11) -end program demo_logspace_complex +{!example/math/example_logspace_complex.f90!} ``` ##### Example 2: Here inputs are of type `integer` and default kind. `base` is not specified and thus defaults to 10. ```fortran -program demo_logspace_int - use stdlib_math, only: logspace - use stdlib_kinds, only: dp - implicit none - - integer :: start = 10 - integer :: end = 23 - integer :: n = 15 - - real(dp) :: r(n) ! Integer values raised to real powers results in real values - - r = logspace(start, end, n) -end program demo_logspace_int +{!example/math/example_logspace_int.f90!} ``` ##### Example 3: Here `start`/`end` are of type `real` and double precision. `base` is type `complex` and also double precision. ```fortran -program demo_logspace_rstart_cbase - use stdlib_math, only: logspace - use stdlib_kinds, only: dp - implicit none - - real(dp) :: start = 0.0_dp - real(dp) :: end = 3.0_dp - integer :: n = 4 - complex(dp) :: base = (0.0_dp, 1.0_dp) - - complex(dp) :: z(n) ! complex values raised to real powers result in complex values - - z = logspace(start, end, n, base) - -end program demo_logspace_rstart_cbase +{!example/math/example_logspace_rstart_cbase.f90!} ``` ### `arange` function @@ -368,25 +271,7 @@ For `real` type arguments, the length of the result vector is `floor((end - star #### Example ```fortran -program demo_math_arange - use stdlib_math, only: arange - - print *, arange(3) ! [1,2,3] - print *, arange(-1) ! [1,0,-1] - print *, arange(0,2) ! [0,1,2] - print *, arange(1,-1) ! [1,0,-1] - print *, arange(0, 2, 2) ! [0,2] - - print *, arange(3.0) ! [1.0,2.0,3.0] - print *, arange(0.0,5.0) ! [0.0,1.0,2.0,3.0,4.0,5.0] - print *, arange(0.0,6.0,2.5) ! [0.0,2.5,5.0] - - print *, (1.0,1.0)*arange(3) ! [(1.0,1.0),(2.0,2.0),[3.0,3.0]] - - print *, arange(0.0,2.0,-2.0) ! [0.0,2.0]. Not recommended: `step` argument is negative! - print *, arange(0.0,2.0,0.0) ! [0.0,1.0,2.0]. Not recommended: `step` argument is zero! - -end program demo_math_arange +{!example/math/example_math_arange.f90!} ``` ### `arg` function @@ -422,13 +307,7 @@ Notes: Although the angle of the complex number `0` is undefined, `arg((0,0))` r #### Example ```fortran -program demo_math_arg - use stdlib_math, only: arg - print *, arg((0.0, 0.0)) ! 0.0 - print *, arg((3.0, 4.0)) ! 0.927 - print *, arg(2.0*exp((0.0, 0.5))) ! 0.5 - print *, arg([(0.0, 1.0), (1.0, 0.0), (0.0, -1.0), (-1.0, 0.0)]) ! [π/2, 0.0, -π/2, π] -end program demo_math_arg +{!example/math/example_math_arg.f90!} ``` ### `argd` function @@ -464,13 +343,7 @@ Notes: Although the angle of the complex number `0` is undefined, `argd((0,0))` #### Example ```fortran -program demo_math_argd - use stdlib_math, only: argd - print *, argd((0.0, 0.0)) ! 0.0° - print *, argd((3.0, 4.0)) ! 53.1° - print *, argd(2.0*exp((0.0, 0.5))) ! 28.64° - print *, argd([(0.0, 1.0), (1.0, 0.0), (0.0, -1.0), (-1.0, 0.0)]) ! [90°, 0°, -90°, 180°] -end program demo_math_argd +{!example/math/example_math_argd.f90!} ``` ### `argpi` function @@ -506,13 +379,7 @@ Notes: Although the angle of the complex number `0` is undefined, `argpi((0,0))` #### Example ```fortran -program demo_math_argpi - use stdlib_math, only: argpi - print *, argpi((0.0, 0.0)) ! 0.0 - print *, argpi((3.0, 4.0)) ! 0.295 - print *, argpi(2.0*exp((0.0, 0.5))) ! 0.159 - print *, argpi([(0.0, 1.0), (1.0, 0.0), (0.0, -1.0), (-1.0, 0.0)]) ! [0.5, 0.0, -0.5, 1.0] -end program demo_math_argpi +{!example/math/example_math_argpi.f90!} ``` ### `is_close` function @@ -572,20 +439,7 @@ Returns a `logical` scalar/array. #### Example ```fortran -program demo_math_is_close - - use stdlib_math, only: is_close - real :: x(2) = [1, 2], y, NAN - - y = -3 - NAN = sqrt(y) - - print *, is_close(x,[real :: 1, 2.1]) ! [T, F] - print *, is_close(2.0, 2.1, abs_tol=0.1) ! T - print *, NAN, is_close(2.0, NAN), is_close(2.0, NAN, equal_nan=.true.) ! NAN, F, F - print *, is_close(NAN, NAN), is_close(NAN, NAN, equal_nan=.true.) ! F, T - -end program demo_math_is_close +{!example/math/example_math_is_close.f90!} ``` ### `all_close` function @@ -636,21 +490,7 @@ Returns a `logical` scalar. #### Example ```fortran -program demo_math_all_close - - use stdlib_math, only: all_close - real :: x(2) = [1, 2], y, NAN - complex :: z(4, 4) - - y = -3 - NAN = sqrt(y) - z = (1.0, 1.0) - - print *, all_close(z+cmplx(1.0e-11, 1.0e-11), z) ! T - print *, NAN, all_close([NAN], [NAN]), all_close([NAN], [NAN], equal_nan=.true.) - ! NAN, F, T - -end program demo_math_all_close +{!example/math/example_math_all_close.f90!} ``` ### `diff` function @@ -712,26 +552,5 @@ When both `prepend` and `append` are not present, the result `y` has one fewer e #### Example ```fortran -program demo_diff - - use stdlib_math, only: diff - implicit none - - integer :: i(7) = [1, 1, 2, 3, 5, 8, 13] - real :: x(6) = [0, 5, 15, 30, 50, 75] - integer :: A(3, 3) = reshape([1, 7, 17, 3, 11, 19, 5, 13, 23], [3, 3]) - integer :: Y(3, 2) - - print *, diff(i) ! [0, 1, 1, 2, 3, 5] - print *, diff(x, 2) ! [5.0, 5.0, 5.0, 5.0] - - Y = diff(A, n=1, dim=2) - print *, Y(1, :) ! [2, 2] - print *, Y(2, :) ! [4, 2] - print *, Y(3, :) ! [2, 4] - - print *, diff(i, prepend=[0]) ! [1, 0, 1, 1, 2, 3, 5] - print *, diff(i, append=[21]) ! [0, 1, 1, 2, 3, 5, 8] - -end program demo_diff -``` \ No newline at end of file +{!example/math/example_diff.f90!} +``` diff --git a/doc/specs/stdlib_optval.md b/doc/specs/stdlib_optval.md index 25a54b3e5..95e3972a0 100644 --- a/doc/specs/stdlib_optval.md +++ b/doc/specs/stdlib_optval.md @@ -35,18 +35,5 @@ If `x` is present, the result is `x`, otherwise the result is `default`. ### Example ```fortran -program demo_optval - use stdlib_optval, only: optval - implicit none - print *, root(64.0) -! 8.0 - print *, root(64.0, 3) -! 4.0 -contains - real function root(x, n) - real, intent(in) :: x - integer, intent(in), optional :: n - root = x**(1.0/optval(n, 2)) - end function root -end program demo_optval +{!example/optval/example_optval.f90!} ``` diff --git a/doc/specs/stdlib_quadrature.md b/doc/specs/stdlib_quadrature.md index bd67f8af0..a582bd42a 100644 --- a/doc/specs/stdlib_quadrature.md +++ b/doc/specs/stdlib_quadrature.md @@ -39,16 +39,7 @@ If the size of `y` is zero or one, the result is zero. ### Example ```fortran -program demo_trapz - use stdlib_quadrature, only: trapz - implicit none - real :: x(5) = [0., 1., 2., 3., 4.] - real :: y(5) = x**2 - print *, trapz(y, x) -! 22.0 - print *, trapz(y, 0.5) -! 11.0 -end program demo_trapz +{!example/quadrature/example_trapz.f90!} ``` ## `trapz_weights` - trapezoidal rule weights for given abscissas @@ -78,17 +69,7 @@ If the size of `x` is one, then the sole element of the result is zero. ### Example ```fortran -program demo_trapz_weights - use stdlib_quadrature, only: trapz_weights - implicit none - real :: x(5) = [0., 1., 2., 3., 4.] - real :: y(5) = x**2 - real :: w(5) - w = trapz_weights(x) - print *, sum(w*y) -! 22.0 -end program demo_trapz_weights - +{!example/quadrature/example_trapz_weights.f90!} ``` ## `simps` - integrate sampled values using Simpson's rule @@ -130,16 +111,7 @@ If the size of `y` is two, the result is the same as if `trapz` had been called ### Example ```fortran -program demo_simps - use stdlib_quadrature, only: simps - implicit none - real :: x(5) = [0., 1., 2., 3., 4.] - real :: y(5) = 3.*x**2 - print *, simps(y, x) -! 64.0 - print *, simps(y, 0.5) -! 32.0 -end program demo_simps +{!example/quadrature/example_simps.f90!} ``` ## `simps_weights` - Simpson's rule weights for given abscissas @@ -175,16 +147,7 @@ If the size of `x` is two, then the result is the same as if `trapz_weights` had ### Example ```fortran -program demo_simps_weights - use stdlib_quadrature, only: simps_weights - implicit none - real :: x(5) = [0., 1., 2., 3., 4.] - real :: y(5) = 3.*x**2 - real :: w(5) - w = simps_weights(x) - print *, sum(w*y) -! 64.0 -end program demo_simps_weights +{!example/quadrature/example_simps_weights.f90!} ``` ## `gauss_legendre` - Gauss-Legendre quadrature (a.k.a. Gaussian quadrature) nodes and weights @@ -222,15 +185,7 @@ If not specified, the default integral is -1 to 1. ### Example ```fortran -program integrate - use iso_fortran_env, dp => real64 - implicit none - - integer, parameter :: N = 6 - real(dp), dimension(N) :: x,w - call gauss_legendre(x,w) - print *, "integral of x**2 from -1 to 1 is", sum(x**2 * w) -end program +{!example/quadrature/example_gauss_legendre.f90!} ``` ## `gauss_legendre_lobatto` - Gauss-Legendre-Lobatto quadrature nodes and weights @@ -268,13 +223,5 @@ If not specified, the default integral is -1 to 1. ### Example ```fortran -program integrate - use iso_fortran_env, dp => real64 - implicit none - - integer, parameter :: N = 6 - real(dp), dimension(N) :: x,w - call gauss_legendre_lobatto(x,w) - print *, "integral of x**2 from -1 to 1 is", sum(x**2 * w) -end program +{!example/quadrature/example_gauss_legendre_lobatto.f90!} ``` diff --git a/doc/specs/stdlib_random.md b/doc/specs/stdlib_random.md index f4586a4ae..0bc72415a 100644 --- a/doc/specs/stdlib_random.md +++ b/doc/specs/stdlib_random.md @@ -33,14 +33,7 @@ Return a scalar of type `integer`. ### Example ```fortran -program demo_random_seed - use stdlib_random, only : random_seed - implicit none - integer :: seed_put, seed_get - - seed_put = 1234567 - call random_seed(seed_put, seed_get) ! set and get current value of seed -end program demo_random_seed +{!example/random/example_random_seed.f90!} ``` ## `dist_rand` - Get a random integer with specified kind @@ -68,20 +61,5 @@ Return a scalar of type `integer`. ### Example ```fortran -program demo_dist_rand - use stdlib_random, only : dist_rand, random_seed - implicit none - integer :: put, get - - put = 135792468 - call random_seed(put, get) ! set and get current value of seed - print *, dist_rand(1_int8) ! random integer in [-2^7, 2^7 - 1] -! -90 - print *, dist_rand(1_int16) ! random integer in [-2^15, 2^15 - 1] -! -32725 - print *, dist_rand(1_int32) ! random integer in [-2^31, 2^31 - 1] -! -1601563881 - print *, dist_rand(1_int64) ! random integer in [-2^63, 2^63 - 1] -! 180977695517992208 -end program demo_dist_rand +{!example/random/example_dist_rand.f90!} ``` diff --git a/doc/specs/stdlib_selection.md b/doc/specs/stdlib_selection.md index d2d90a8f6..9f2772f2b 100644 --- a/doc/specs/stdlib_selection.md +++ b/doc/specs/stdlib_selection.md @@ -107,33 +107,7 @@ code here to be released under stdlib's MIT license. ### Example ```fortran -program demo_select - use stdlib_selection, only: select - implicit none - - real, allocatable :: array(:) - real :: kth_smallest - integer :: k, left, right - - array = [3., 2., 7., 4., 5., 1., 4., -1.] - - k = 2 - call select(array, k, kth_smallest) - print*, kth_smallest ! print 1.0 - - k = 7 - ! Due to the previous call to select, we know for sure this is in an - ! index >= 2 - call select(array, k, kth_smallest, left=2) - print*, kth_smallest ! print 5.0 - - k = 6 - ! Due to the previous two calls to select, we know for sure this is in - ! an index >= 2 and <= 7 - call select(array, k, kth_smallest, left=2, right=7) - print*, kth_smallest ! print 4.0 - -end program demo_select +{!example/selection/example_select.f90!} ``` ## `arg_select` - find the index of the k-th smallest value in an input array @@ -216,35 +190,7 @@ code here to be released under stdlib's MIT license. ```fortran -program demo_arg_select - use stdlib_selection, only: arg_select - implicit none - - real, allocatable :: array(:) - integer, allocatable :: indx(:) - integer :: kth_smallest - integer :: k, left, right - - array = [3., 2., 7., 4., 5., 1., 4., -1.] - indx = [( k, k = 1, size(array) )] - - k = 2 - call arg_select(array, indx, k, kth_smallest) - print*, array(kth_smallest) ! print 1.0 - - k = 7 - ! Due to the previous call to arg_select, we know for sure this is in an - ! index >= 2 - call arg_select(array, indx, k, kth_smallest, left=2) - print*, array(kth_smallest) ! print 5.0 - - k = 6 - ! Due to the previous two calls to arg_select, we know for sure this is in - ! an index >= 2 and <= 7 - call arg_select(array, indx, k, kth_smallest, left=2, right=7) - print*, array(kth_smallest) ! print 4.0 - -end program demo_arg_select +{!example/selection/example_arg_select.f90!} ``` ## Comparison with using `sort` @@ -255,83 +201,7 @@ should see a speed improvement with the selection routines which grows like LOG(size(`array`)). ```fortran -program selection_vs_sort - use stdlib_kinds, only: dp, sp, int64 - use stdlib_selection, only: select, arg_select - use stdlib_sorting, only: sort - implicit none - - call compare_select_sort_for_median(1) - call compare_select_sort_for_median(11) - call compare_select_sort_for_median(101) - call compare_select_sort_for_median(1001) - call compare_select_sort_for_median(10001) - call compare_select_sort_for_median(100001) - - contains - subroutine compare_select_sort_for_median(N) - integer, intent(in) :: N - - integer :: i, k, result_arg_select, indx(N), indx_local(N) - real :: random_vals(N), local_random_vals(N) - integer, parameter :: test_reps = 100 - integer(int64) :: t0, t1 - real :: result_sort, result_select - integer(int64) :: time_sort, time_select, time_arg_select - logical :: select_test_passed, arg_select_test_passed - - ! Ensure N is odd - if(mod(N, 2) /= 1) stop - - time_sort = 0 - time_select = 0 - time_arg_select = 0 - - select_test_passed = .true. - arg_select_test_passed = .true. - - indx = (/( i, i = 1, N) /) - - k = (N+1)/2 ! Deliberate integer division - - do i = 1, test_reps - call random_number(random_vals) - - ! Compute the median with sorting - local_random_vals = random_vals - call system_clock(t0) - call sort(local_random_vals) - result_sort = local_random_vals(k) - call system_clock(t1) - time_sort = time_sort + (t1 - t0) - - ! Compute the median with selection, assuming N is odd - local_random_vals = random_vals - call system_clock(t0) - call select(local_random_vals, k, result_select) - call system_clock(t1) - time_select = time_select + (t1 - t0) - - ! Compute the median with arg_select, assuming N is odd - local_random_vals = random_vals - indx_local = indx - call system_clock(t0) - call arg_select(local_random_vals, indx_local, k, result_arg_select) - call system_clock(t1) - time_arg_select = time_arg_select + (t1 - t0) - - if(result_select /= result_sort) select_test_passed = .FALSE. - if(local_random_vals(result_arg_select) /= result_sort) arg_select_test_passed = .FALSE. - end do - - print*, "select ; N=", N, '; ', merge('PASS', 'FAIL', select_test_passed), & - '; Relative-speedup-vs-sort:', (1.0*time_sort)/(1.0*time_select) - print*, "arg_select; N=", N, '; ', merge('PASS', 'FAIL', arg_select_test_passed), & - '; Relative-speedup-vs-sort:', (1.0*time_sort)/(1.0*time_arg_select) - - end subroutine - -end program +{!example/selection/selection_vs_sort.f90!} ``` The results seem consistent with expectations when the `array` is large; the program prints: diff --git a/doc/specs/stdlib_sorting.md b/doc/specs/stdlib_sorting.md index 7368c691e..59c484f1d 100644 --- a/doc/specs/stdlib_sorting.md +++ b/doc/specs/stdlib_sorting.md @@ -261,16 +261,7 @@ function `LGT`. ##### Example ```fortran - program demo_ord_sort - use stdlib_sorting, only: ord_sort - implicit none - integer, allocatable :: array1(:), work(:) - - array1 = [ 5, 4, 3, 1, 10, 4, 9] - allocate(work, mold = array1) - call ord_sort(array1, work) - print*, array1 !print [1, 3, 4, 4, 5, 9, 10] - end program demo_ord_sort +{!example/sorting/example_ord_sort.f90!} ``` #### `sort` - sorts an input array @@ -324,15 +315,7 @@ element of `array` is a `NaN`. Sorting of `CHARACTER(*)` and ```fortran - program demo_sort - use stdlib_sorting, only: sort - implicit none - integer, allocatable :: array(:) - - array = [ 5, 4, 3, 1, 10, 4, 9] - call sort(array) - print*, array !print [1, 3, 4, 4, 5, 9, 10] - end program demo_sort +{!example/sorting/example_sort.f90!} ``` #### `sort_index` - creates an array of sorting indices for an input array, while also sorting the array. @@ -453,21 +436,21 @@ Sorting a rank 2 array based on the data in a column Sorting an array of a derived type based on the data in one component ```fortran - subroutine sort_a_data( a_data, a, work, index, iwork ) - ! Sort `a_data` in terms or its component `a` - type(a_type), intent(inout) :: a_data(:) - integer(int32), intent(inout) :: a(:) - integer(int32), intent(out) :: work(:) - integer(int_size), intent(out) :: index(:) - integer(int_size), intent(out) :: iwork(:) - ! Extract a component of `a_data` - a(1:size(a_data)) = a_data(:) % a - ! Find the indices to sort the component - call sort_index(a(1:size(a_data)), index(1:size(a_data)),& - work(1:size(a_data)/2), iwork(1:size(a_data)/2)) - ! Sort a_data based on the sorting of that component - a_data(:) = a_data( index(1:size(a_data)) ) - end subroutine sort_a_data + subroutine sort_a_data( a_data, a, work, index, iwork ) + ! Sort `a_data` in terms or its component `a` + type(a_type), intent(inout) :: a_data(:) + integer(int32), intent(inout) :: a(:) + integer(int32), intent(out) :: work(:) + integer(int_size), intent(out) :: index(:) + integer(int_size), intent(out) :: iwork(:) + ! Extract a component of `a_data` + a(1:size(a_data)) = a_data(:) % a + ! Find the indices to sort the component + call sort_index(a(1:size(a_data)), index(1:size(a_data)),& + work(1:size(a_data)/2), iwork(1:size(a_data)/2)) + ! Sort a_data based on the sorting of that component + a_data(:) = a_data( index(1:size(a_data)) ) + end subroutine sort_a_data ``` diff --git a/doc/specs/stdlib_specialfunctions_gamma.md b/doc/specs/stdlib_specialfunctions_gamma.md index bd277d4d8..7e83da715 100644 --- a/doc/specs/stdlib_specialfunctions_gamma.md +++ b/doc/specs/stdlib_specialfunctions_gamma.md @@ -38,43 +38,7 @@ The function returns a value with the same type and kind as input argument. ### Example ```fortran -program demo_gamma - use stdlib_kinds, only : dp, int64 - use stdlib_specialfunctions_gamma, only : gamma - implicit none - - integer :: i - integer(int64) :: n - real :: x - real(dp) :: y - complex :: z - complex(dp) :: z1 - - i = 10 - n = 15_int64 - x = 2.5 - y = 4.3_dp - z = (2.3, 0.6) - z1 = (-4.2_dp, 3.1_dp) - - print *, gamma(i) !integer gives exact result -! 362880 - - print *, gamma(n) -! 87178291200 - - print *, gamma(x) ! intrinsic function call -! 1.32934034 - - print *, gamma(y) ! intrinsic function call -! 8.8553433604540341 - - print *, gamma(z) -! (0.988054395, 0.383354813) - - print *, gamma(z1) -! (-2.78916032990983999E-005, 9.83164600163221218E-006) -end program demo_gamma +{!example/specialfunctions_gamma/example_gamma.f90!} ``` ## `log_gamma` - Calculate the natural logarithm of the gamma function @@ -108,41 +72,7 @@ The function returns real single precision values for integer input arguments, w ### Example ```fortran -program demo_log_gamma - use stdlib_kinds, only : dp - use stdlib_specialfunctions_gamma, only : log_gamma - implicit none - - integer :: i - real :: x - real(dp) :: y - complex :: z - complex(dp) :: z1 - - i = 10 - x = 8.76 - y = x - z = (5.345, -3.467) - z1 = z - print *, log_gamma(i) !default single precision output -!12.8018274 - - print *, log_gamma(x) !intrinsic function call - -!10.0942659 - - print *, log_gamma(y) !intrinsic function call - -!10.094265528673880 - - print *, log_gamma(z) !same kind as input - -!(2.56165648, -5.73382425) - - print *, log_gamma(z1) - -!(2.5616575105114614, -5.7338247782852498) -end program demo_log_gamma +{!example/specialfunctions_gamma/example_log_gamma.f90!} ``` ## `log_factorial` - calculate the logarithm of a factorial @@ -173,21 +103,7 @@ The function returns real type values with single precision. ### Example ```fortran -program demo_log_factorial - use stdlib_kinds, only : int64 - use stdlib_specialfunctions_gamma, only : lf => log_factorial - implicit none - integer :: n - - n = 10 - print *, lf(n) - -! 15.1044130 - - print *, lf(35_int64) - -! 92.1361771 -end program demo_log_factorial +{!example/specialfunctions_gamma/example_log_factorial.f90!} ``` ## `lower_incomplete_gamma` - calculate lower incomplete gamma integral @@ -224,22 +140,7 @@ The function returns a real type value with the same kind as argument x. ### Example ```fortran -program demo_ligamma - use stdlib_specialfunctions_gamma, only : lig => lower_incomplete_gamma - implicit none - integer :: p - real :: p1, x - - p = 3 - p1 = 2.3 - print *, lig(p, -5.0) - -! -2521.02417 - - print *, lig(p1, 5.0) - -! 1.09715652 -end demo_ligamma +{!example/specialfunctions_gamma/example_ligamma.f90!} ``` ## `upper_incomplete_gamma` - calculate the upper incomplete gamma integral @@ -276,18 +177,7 @@ The function returns a real type value with the same kind as argument x. ### Example ```fortran -program demo_uigamma - use stdlib_specialfunctions_gamma, only : uig => upper_incomplete_gamma - implicit none - - print *, uig(3, -5.0) - -!2523.02295 - - print *, uig(2.3, 5.0) - -!6.95552528E-02 -end program demo_uigamma +{!example/specialfunctions_gamma/example_uigamma.f90!} ``` ## `log_lower_incomplete_gamma` - calculate the natural logarithm of the lower incomplete gamma integral @@ -382,14 +272,7 @@ The function returns a real type value with the same kind as argument x. ### Example ```fortran -program demo_gamma_p - use stdlib_specialfunctions_gamma, only : rgp => regularized_gamma_p - implicit none - - print *, rgp(3.0, 5.0) - -! 0.875347972 -end program demo_gamma_p +{!example/specialfunctions_gamma/example_gamma_p.f90!} ``` ## `regularized_gamma_q` - calculate the gamma quotient Q @@ -426,12 +309,5 @@ The function returns a real type value with the same kind as argument x. ### Example ```fortran -program demo_gamma_q - use stdlib_specialfunctions_gamma, only : rgq => regularized_gamma_q - implicit none - - print *, rgq(3.0, 5.0) - -! 0.124652028 -end program demo_gamma_q +{!example/specialfunctions_gamma/example_gamma_q.f90!} ``` diff --git a/doc/specs/stdlib_stats.md b/doc/specs/stdlib_stats.md index 0ce19b0eb..c79f218fd 100644 --- a/doc/specs/stdlib_stats.md +++ b/doc/specs/stdlib_stats.md @@ -52,14 +52,7 @@ If `mask` is specified, the result is the Pearson correlation of all elements of ### Example ```fortran -program demo_corr - use stdlib_stats, only: corr - implicit none - real :: x(1:6) = [ 1., 2., 3., 4., 5., 6. ] - real :: y(1:2, 1:3) = reshape([ -1., 40., -3., 4., 10., 6. ], [ 2, 3]) - print *, corr(x, 1) !returns 1. - print *, corr(y, 2) !returns reshape([ 1., -.32480, -.32480, 1. ], [ 2, 3]) -end program demo_corr +{!example/stats/example_corr.f90!} ``` ## `cov` - covariance of array elements @@ -115,15 +108,7 @@ If `mask` is specified, the result is the covariance of all elements of `array` ### Example ```fortran -program demo_cov - use stdlib_stats, only: cov - implicit none - real :: x(1:6) = [ 1., 2., 3., 4., 5., 6. ] - real :: y(1:2, 1:3) = reshape([ 1., 2., 3., 4., 5., 6. ], [ 2, 3]) - print *, cov(x, 1) !returns 3.5 - print *, cov(x, 1, corrected = .false.) !returns 2.9167 - print *, cov(y, 1) !returns a square matrix of size 3 with all elements equal to 0.5 -end program demo_cov +{!example/stats/example_cov.f90!} ``` ## `mean` - mean of array elements @@ -166,16 +151,7 @@ If `mask` is specified, the result is the mean of all elements of `array` corres ### Example ```fortran -program demo_mean - use stdlib_stats, only: mean - implicit none - real :: x(1:6) = [ 1., 2., 3., 4., 5., 6. ] - real :: y(1:2, 1:3) = reshape([ 1., 2., 3., 4., 5., 6. ], [ 2, 3]) - print *, mean(x) !returns 3.5 - print *, mean(y) !returns 3.5 - print *, mean(y, 1) !returns [ 1.5, 3.5, 5.5 ] - print *, mean(y, 1,y > 3.) !returns [ NaN, 4.0, 5.5 ] -end program demo_mean +{!example/stats/example_mean.f90!} ``` ## `median` - median of array elements @@ -240,16 +216,7 @@ If `mask` is specified, the result is the median of all elements of `array` corr ### Example ```fortran -program demo_median - use stdlib_stats, only: median - implicit none - real :: x(1:6) = [ 1., 2., 3., 4., 5., 6. ] - real :: y(1:2, 1:3) = reshape([ 1., 2., 3., 4., 5., 6. ], [ 2, 3]) - print *, median(x) !returns 3.5 - print *, median(y) !returns 3.5 - print *, median(y, 1) !returns [ 1.5, 3.5, 5.5 ] - print *, median(y, 1,y > 3.) !returns [ NaN, 4.0, 5.5 ] -end program demo_median +{!example/stats/example_median.f90!} ``` ## `moment` - central moments of array elements @@ -313,18 +280,7 @@ If `mask` is specified, the result is the _k_-th (central) moment of all elemen ### Example ```fortran -program demo_moment - use stdlib_stats, only: moment - implicit none - real :: x(1:6) = [ 1., 2., 3., 4., 5., 6. ] - real :: y(1:2, 1:3) = reshape([ 1., 2., 3., 4., 5., 6. ], [ 2, 3]) - print *, moment(x, 2) !returns 2.9167 - print *, moment(y, 2) !returns 2.9167 - print *, moment(y, 2, 1) !returns [0.25, 0.25, 0.25] - print *, moment(y, 2, 1, mask = (y > 3.)) !returns [NaN, 0., 0.25] - print *, moment(x, 2, center = 0.) !returns 15.1667 - print *, moment(y, 1, 1, center = 0.) !returns [1.5, 3.5, 5.5] -end program demo_moment +{!example/stats/example_moment.f90!} ``` ## `var` - variance of array elements @@ -382,16 +338,5 @@ If the variance is computed with only one single element, then the result is IEE ### Example ```fortran -program demo_var - use stdlib_stats, only: var - implicit none - real :: x(1:6) = [ 1., 2., 3., 4., 5., 6. ] - real :: y(1:2, 1:3) = reshape([ 1., 2., 3., 4., 5., 6. ], [ 2, 3]) - print *, var(x) !returns 3.5 - print *, var(x, corrected = .false.) !returns 2.9167 - print *, var(y) !returns 3.5 - print *, var(y, 1) !returns [0.5, 0.5, 0.5] - print *, var(y, 1, y > 3.) !returns [NaN, NaN, 0.5] - print *, var(y, 1, y > 3., corrected=.false.) !returns [NaN, 0., 0.25] -end program demo_var +{!example/stats/example_var.f90!} ``` diff --git a/doc/specs/stdlib_stats_distribution_exponential.md b/doc/specs/stdlib_stats_distribution_exponential.md index 9a53bee95..4be87df65 100644 --- a/doc/specs/stdlib_stats_distribution_exponential.md +++ b/doc/specs/stdlib_stats_distribution_exponential.md @@ -46,39 +46,7 @@ The result is a scalar or rank one array with a size of `array_size`, and has th ### Example ```fortran -program demo_exponential_rvs - use stdlib_random, only : random_seed - use stdlib_stats_distribution_exponential, only: rexp => rvs_exp - - implicit none - real :: a(2,3,4) - complex :: scale - integer :: seed_put, seed_get - - seed_put = 1234567 - call random_seed(seed_put, seed_get) - - print *, rexp( ) !single standard exponential random variate - -! 0.358690143 - - print *, rexp(2.0) !exponential random variate with lambda=2.0 - -! 0.816459715 - - print *, rexp(0.3, 10) !an array of 10 variates with lambda=0.3 - -! 1.84008647E-02 3.59742008E-02 0.136567295 0.262772143 3.62352766E-02 -! 0.547133625 0.213591918 4.10784185E-02 0.583882213 0.671128035 - - scale = (2.0, 0.7) - print *, rexp(scale) - !single complex exponential random variate with real part of lambda=2.0; - !imagainary part of lambda=0.7 - -! (1.41435969,4.081114382E-02) - -end program demo_exponential_rvs +{!example/stats_distribution_exponential/example_exponential_rvs.f90!} ``` ## `pdf_exp` - exponential distribution probability density function @@ -122,45 +90,7 @@ The result is a scalar or an array, with a shape conformable to arguments, and h ### Example ```fortran -program demo_exponential_pdf - use stdlib_random, only : random_seed - use stdlib_stats_distribution_exponential, only: exp_pdf => pdf_exp, & - rexp => rvs_exp - - implicit none - real :: x(2,3,4),a(2,3,4) - complex :: scale - integer :: seed_put, seed_get - - seed_put = 1234567 - call random_seed(seed_put, seed_get) - - print *, exp_pdf(1.0,1.0) !a probability density at 1.0 in standard expon - -! 0.367879450 - - print *, exp_pdf(2.0,2.0) !a probability density at 2.0 with lambda=2.0 - -! 3.66312787E-02 - - x = reshape(rexp(0.5, 24),[2,3,4]) ! standard expon random variates array - a(:,:,:) = 0.5 - print *, exp_pdf(x, a) ! a rank 3 standard expon probability density - -! 0.457115263 0.451488823 0.492391467 0.485233188 0.446215510 -! 0.401670188 0.485127628 0.316924453 0.418474048 0.483173639 -! 0.307366133 0.285812140 0.448017836 0.426440030 0.403896868 -! 0.334653258 0.410376132 0.485370994 0.333617479 0.263791025 -! 0.249779820 0.457159877 0.495636940 0.482243657 - - scale = (1.0, 2.) - print *, exp_pdf((1.5,1.0), scale) - ! a complex expon probability density function at (1.5,1.0) with real part - !of lambda=1.0 and imaginary part of lambda=2.0 - -! 6.03947677E-02 - -end program demo_exponential_pdf +{!example/stats_distribution_exponential/example_exponential_pdf.f90!} ``` ## `cdf_exp` - exponential cumulative distribution function @@ -204,45 +134,5 @@ The result is a scalar or an array, with a shape conformable to arguments, and h ### Example ```fortran -program demo_exponential_cdf - use stdlib_random, only : random_seed - use stdlib_stats_distribution_exponential, only : exp_cdf => cdf_exp, & - rexp => rvs_exp - - implicit none - real :: x(2,3,4),a(2,3,4) - complex :: scale - integer :: seed_put, seed_get - - seed_put = 1234567 - call random_seed(seed_put, seed_get) - - print *, exp_cdf(1.0, 1.0) ! a standard exponential cumulative at 1.0 - -! 0.632120550 - - print *, exp_cdf(2.0, 2.0) ! a cumulative at 2.0 with lambda=2 - -! 0.981684387 - - x = reshape(rexp(0.5, 24),[2,3,4]) - ! standard exponential random variates array - a(:,:,:) = 0.5 - print *, exp_cdf(x, a) ! a rank 3 array of standard exponential cumulative - -! 8.57694745E-02 9.70223546E-02 1.52170658E-02 2.95336246E-02 -! 0.107568979 0.196659625 2.97447443E-02 0.366151094 0.163051903 -! 3.36527228E-02 0.385267735 0.428375721 0.103964329 0.147119939 -! 0.192206264 0.330693483 0.179247737 2.92580128E-02 0.332765043 -! 0.472417951 0.500440359 8.56802464E-02 8.72612000E-03 3.55126858E-02 - - scale = (0.5,1.0) - print *, exp_cdf((0.5,0.5),scale) - !complex exponential cumulative distribution at (0.5,0.5) with real part of - !lambda=0.5 and imaginary part of lambda=1.0 - -! 8.70351046E-02 - -end program demo_exponential_cdf - +{!example/stats_distribution_exponential/example_exponential_cdf.f90!} ``` diff --git a/doc/specs/stdlib_stats_distribution_normal.md b/doc/specs/stdlib_stats_distribution_normal.md index 88e692ccc..20d8b5956 100644 --- a/doc/specs/stdlib_stats_distribution_normal.md +++ b/doc/specs/stdlib_stats_distribution_normal.md @@ -49,51 +49,7 @@ The result is a scalar or rank one array, with a size of `array_size`, and as th ### Example ```fortran -program demo_normal_rvs - use stdlib_random, only: random_seed - use stdlib_stats_distribution_normal, only: norm => rvs_normal - - implicit none - real :: a(2,3,4), b(2,3,4) - complx :: loc, scale - integer :: seed_put, seed_get - - seed_put = 1234567 - call random_seed(seed_put, seed_get) - - print *, norm( ) !single standard normal random variate - -! 0.563655198 - - print *, norm(1.0, 2.0) - !normal random variate mu=1.0, sigma=2.0 - -! -0.633261681 - - print *, norm(0.0, 1.0, 10) !an array of 10 standard norml random variates - -! -3.38123664E-02 -0.190365672 0.220678389 -0.424612164 -0.249541596 -! 0.865260184 1.11086845 -0.328349441 1.10873628 1.27049923 - - a(:,:,:) = 1.0 - b(:,:,:) = 1.0 - print *, norm(a,b) ! a rank 3 random variates array - -!0.152776539 -7.51764774E-02 1.47208166 0.180561781 1.32407105 -! 1.20383692 0.123445868 -0.455737948 -0.469808221 1.60750175 -! 1.05748117 0.720934749 0.407810807 1.48165631 2.31749439 -! 0.414566994 3.06084275 1.86505437 1.36338580 7.26878643E-02 -! 0.178585172 1.39557445 0.828021586 0.872084975 - - loc = (-1.0, 2.0) - scale = (2.0, 1.0) - print *, norm(loc, scale) - !single complex normal random variate with real part of mu=-1, sigma=2; - !imagainary part of mu=2.0 and sigma=1.0 - -! (1.22566295,2.12518454) - -end program demo_normal_rvs +{!example/stats_distribution_normal/example_normal_rvs.f90!} ``` ## `pdf_normal` - normal distribution probability density function @@ -137,50 +93,7 @@ The result is a scalar or an array, with a shape conformable to arguments, and a ### Example ```fortran -program demo_normal_pdf - use stdlib_random, only : random_seed - use stdlib_stats_distribution_normal, only : norm_pdf=>pdf_normal, & - norm => rvs_normal - - implicit none - real :: x(3,4,5),a(3,4,5),b(3,4,5) - complx :: loc, scale - integer :: seed_put, seed_get - - seed_put = 1234567 - call random_seed(seed_put, seed_get) - - print *, norm_pdf(1.0,0.,1.) !a probability density at 1.0 in standard normal - -! 0.241970733 - - print *, norm_pdf(2.0,-1.0, 2.0) - !a probability density at 2.0 with mu=-1.0 sigma=2.0 - -!6.47588000E-02 - - x = reshape(norm(0.0, 1.0, 60),[3,4,5]) - ! standard normal random variates array - - a(:,:,:) = 0.0 - b(:,:,:) = 1.0 - print *, norm_pdf(x, a, b) ! standard normal probability density array - -! 0.340346158 0.285823315 0.398714304 0.391778737 0.389345556 -! 0.364551932 0.386712372 0.274370432 0.215250477 0.378006011 -! 0.215760440 0.177990928 0.278640658 0.223813817 0.356875211 -! 0.285167664 0.378533930 0.390739858 0.271684974 0.138273031 -! 0.135456234 0.331718773 0.398283750 0.383706540 - - loc = (1.0, -0.5) - scale = (1.0, 2.) - print *, norm_pdf((1.5,1.0), loc, scale) - ! a complex normal probability density function at (1.5,1.0) with real part - ! of mu=1.0, sigma=1.0 and imaginary part of mu=-0.5, sigma=2.0 - -! 5.30100204E-02 - -end program demo_normal_pdf +{!example/stats_distribution_normal/example_normal_pdf.f90!} ``` ## `cdf_normal` - normal distribution cumulative distribution function @@ -224,49 +137,5 @@ The result is a scalar or an array, with a shape conformable to arguments, as th ### Example ```fortran -program demo_norm_cdf - use stdlib_random, only : random_seed - use stdlib_stats_distribution_normal, only : norm_cdf => cdf_normal, & - norm => rvs_normal - - implicit none - real :: x(2,3,4),a(2,3,4),b(2,3,4) - complx :: loc, scale - integer :: seed_put, seed_get - - seed_put = 1234567 - call random_seed(seed_put, seed_get) - - print *, norm_cdf(1.0, 0.0, 1.0) ! a standard normal cumulative at 1.0 - -! 0.841344714 - - print *, norm_cdf(2.0, -1.0, 2.0) - ! a cumulative at 2.0 with mu=-1 sigma=2 - -! 0.933192849 - - x = reshape(norm(0.0, 1.0, 24),[2,3,4]) - ! standard normal random variates array - - a(:,:,:) = 0.0 - b(:,:,:) = 1.0 - print *, norm_cdf(x, a, b) ! standard normal cumulative array - -! 0.713505626 0.207069695 0.486513376 0.424511284 0.587328553 -! 0.335559726 0.401470929 0.806552052 0.866687536 0.371323735 -! 0.866228044 0.898046613 0.198435277 0.141147852 0.681565762 -! 0.206268221 0.627057910 0.580759525 0.190364420 7.27325380E-02 -! 7.08068311E-02 0.728241026 0.522919059 0.390097380 - - loc = (1.0,0.0) - scale = (0.5,1.0) - print *, norm_cdf((0.5,-0.5),loc,scale) - !complex normal cumulative distribution at (0.5,-0.5) with real part of - !mu=1.0, sigma=0.5 and imaginary part of mu=0.0, sigma=1.0 - -!4.89511043E-02 - -end program demo_norm_cdf - +{!example/stats_distribution_normal/example_norm_cdf.f90!} ``` diff --git a/doc/specs/stdlib_stats_distribution_uniform.md b/doc/specs/stdlib_stats_distribution_uniform.md index cd22d15fe..3d8882cbd 100644 --- a/doc/specs/stdlib_stats_distribution_uniform.md +++ b/doc/specs/stdlib_stats_distribution_uniform.md @@ -35,36 +35,7 @@ Return a randomized rank one array of the input type. ### Example ```fortran -program demo_shuffle - use stdlib_random, only : random_seed - use stdlib_stats_distribution_uniform, only : shuffle - implicit none - integer :: seed_put, seed_get, i - real :: x(10) - integer :: n(10) - complex :: z(10) - - do i=1, 10 - n(i) = i - x(i) = real(i) - z(i) = cmplx(real(i), real(i)) - end do - seed_put = 32165498 - call random_seed(seed_put, seed_get) ! set and get current value of seed - print *, shuffle(n) ! get randomized n - -!10 6 9 2 8 1 3 5 7 4 - - print *, shuffle(x) ! get randomized x - -!5.0 10.0 9.0 4.0 3.0 8.0 2.0 1.0 7.0 6.0 - - print *, shuffle(z) ! get randomized z - -!(8.0, 8.0) (7.0, 7.0) (4.0, 4.0) (1.0, 1.0) (5.0, 5.0) -!(9.0, 9.0) (6.0, 6.0) (3.0, 3.0) (2.0, 2.0) (10.0, 10.0) - -end program demo_shuffle +{!example/stats_distribution_uniform/example_shuffle.f90!} ``` ## `rvs_uniform` - uniform distribution random variates @@ -114,85 +85,7 @@ The result is a scalar or a rank one array with size of `array_size`, of type `i ### Example ```fortran -program demo_uniform_rvs - use stdlib_random, only:random_seed - use stdlib_stats_distribution_uniform, only:uni=> rvs_uniform - - implicit none - complex :: loc, scale - real :: a(3,4,5), b(3,4,5) - integer :: seed_put, seed_get - - seed_put = 1234567 - call random_seed(seed_put, seed_get) - - print *, uni( ) !real standard uniform random variate in [0., 1.] -! 0.161520019 - - print *, uni(3.0) !an uniform random variate in [0., 3.] -! 1.65974522 - - print *, uni(-0.5, 1.0) !an uniform random variate in [-0.5, 0.5] -! 0.486900032 - - print *, uni(-1.0,2.0,10) - !an array of 10 uniform random variates in [-1., 1.] - -!0.884182811 -0.771520197 0.560377002 0.709313750 -7.12267756E-02 -!-0.431066573 0.497536063 -0.396331906 -0.325983286 0.137686729 - - print *, uni(20) !a random integer variate in [0, 20] -! 17 - - print *, uni(5,13) !a random integer variate in [5, 18] -! 15 - - print *, uni(3,19,10) !an array of 10 integer variates in [3,22] - -!7 16 16 12 9 21 19 4 3 19 - - loc = (-0.5, -0.5) - scale = (1.0, 1.0) - - print *, uni(scale) !a complex uniform random variate in unit square - -!(0.139202669, 0.361759573) - - print *, uni(loc,scale) - !a complex uniform random variate in [(-0.5, -0.5), (0.5, 0.5)] - -!(0.296536088,-0.143987954) - - print *, uni(loc, scale, 10) - !an array of 10 complex uniform random variate in [(-0.5, -0.5), (0.5, 0.5)] - -!(-0.302334785,-0.401923567) (0.281620383,9.534919262E-02) -! (-0.374348879,0.457528770) (0.442990601,-0.240510434) -! (-0.421572685,0.279313922) (-0.182090610,5.901372433E-02) -! (-7.864198089E-02,0.378484428) (-0.423258364,-0.201292425) -! (0.193327367,-0.353985727) (-0.397661150,0.355926156) - - a(:,:,:) = -0.5 - b(:,:,:) = 1.0 - - print *, uni(a,b) - !a rank 3 array of random variates in [-0.5,0.5] - -! -0.249188632 -0.199248433 -0.389813602 2.88307667E-03 0.238479793, -! 0.264856219 -0.205177426 -0.480921626 0.131218433 0.252170086, -! -0.303151041 -8.89462233E-02 -0.377370685 0.341802299 0.323204756, -! 0.358679056 -0.138909757 0.384329498 -0.109372199 0.132353067, -! 0.494320452 0.419343710 -0.103044361 0.461389005 0.403132677 -! 0.121850729 0.403839290 -0.349389791 0.490482628 0.156600773 -! 8.46788883E-02 -0.483680278 0.388107836 0.119698405 0.154214382 -! 0.153113484 0.236523747 0.155937552 -0.135760903 0.219589531 -! 0.394639254 6.30156994E-02 -0.342692465 -0.444846451 -0.215700030 -! 0.204189956 -0.208748132 0.355063021 8.98272395E-02 -0.237928331 -! 2.98077464E-02 -0.485149682 -8.06870461E-02 -0.372713923 -! -0.178335011 0.283877611 -2.13934183E-02 -9.21690464E-03 -! 4.56320047E-02 0.220112979 - -end program demo_uniform_rvs +{!example/stats_distribution_uniform/example_uniform_rvs.f90!} ``` ## `pdf_uniform` - Uniform distribution probability density function @@ -240,56 +133,7 @@ The result is a scalar or an array, with a shape conformable to arguments, of ty ### Example ```fortran -program demo_uniform_pdf - use stdlib_random, only : random_seed - use stdlib_stats_distribution_uniform, only : uni_pdf => pdf_uniform, & - uni => rvs_uniform - - implicit none - complex :: loc, scale - real :: a(3,4,5), b(3,4,5), x(3,4,5) - integer :: seed_put, seed_get - - seed_put = 1234567 - call random_seed(seed_put, seed_get) - - print *, uni_pdf(3, 2, 10) !probability density at 3 in range [2, 10] - -! 9.09090936E-02 - - print *, uni_pdf(0.5,0.0,1.0) !a probability density at 0.5 in [0., 1.] - -! 1.00000000 - - - print *, uni_pdf(0.7,-1.0,2.0) !a probability density at 0.7 in [-1., 1.] - -! 0.500000000 - - a(:,:,:) = 0.0 - b(:,:,:) = 2.0 - x = reshape(uni(0.,2.,60),[3,4,5])! uniform random variates array in [0., 2.] - print *, uni_pdf(x, a, b) ! probability density array in [0., 2.] - -! 0.500000000 0.500000000 0.500000000 0.500000000 0.500000000 0.500000000 -! 0.500000000 0.500000000 0.500000000 0.500000000 0.500000000 0.500000000 -! 0.500000000 0.500000000 0.500000000 0.500000000 0.500000000 0.500000000 -! 0.500000000 0.500000000 0.500000000 0.500000000 0.500000000 0.500000000 -! 0.500000000 0.500000000 0.500000000 0.500000000 0.500000000 0.500000000 -! 0.500000000 0.500000000 0.500000000 0.500000000 0.500000000 0.500000000 -! 0.500000000 0.500000000 0.500000000 0.500000000 0.500000000 0.500000000 -! 0.500000000 0.500000000 0.500000000 0.500000000 0.500000000 0.500000000 -! 0.500000000 0.500000000 0.500000000 0.500000000 0.500000000 0.500000000 -! 0.500000000 0.500000000 0.500000000 0.500000000 0.500000000 0.500000000 - - loc = (-0.5,-0.5) - scale = (1.0,1.0) - print *, uni_pdf((-0.1,0.2), loc, scale) - ! joint probability density at (-0.1,0.2) in [(-0.5, -0.5), (0.5, 0.5)] - -! 1.00000000 -end program demo_uniform_pdf - +{!example/stats_distribution_uniform/example_uniform_pdf.f90!} ``` ## `cdf_uniform` - Uniform distribution cumulative distribution function @@ -339,53 +183,5 @@ The result is a scalar or an array, with a shape conformable to arguments, of ty ### Example ```fortran -program demo_uniform_cdf - use stdlib_random, only : random_seed - use stdlib_stats_distribution_uniform, only : uni_cdf => cdf_uniform, & - uni => rvs_uniform - - implicit none - real :: x(3,4,5), a(3,4,5), b(3,4,5) - complex :: loc, scale - integer :: seed_put, seed_get - - seed_put = 1234567 - call random_seed(seed_put, seed_get) - - print *, uni_cdf(0.5,0.,1.) ! a cumulative at 0.5 in [0., 1.] - -!0.500000000 - - print *, uni_cdf(0.7,-1.0,2.0) ! a cumulative at 0.7 in [-1.0, 1.0] - -! 0.850000024 - - print *, uni_cdf(6, 2, 10) ! a cumulative at 6 in [2, 10] - -! 0.454545468 - - a(:,:,:) = -1.0 - b(:,:,:) = 2.0 - x = reshape(uni(-1.0,2.0,60),[3,4,5]) ! uniform random variates array - print *, uni_cdf(x,a,b) ! cumulative array in [-1.0, 1.0] - -!0.161520004 0.553248405 0.986900032 0.942091405 0.114239901 0.780188501 -! 0.854656875 0.464386612 0.284466714 0.748768032 0.301834047 0.337008357 -!0.568843365 0.596165061 0.180993259 0.614166319 0.214835495 7.98164606E-02 -!0.641274095 0.607101977 0.701139212 0.230517209 1.97925568E-02 0.857982159 -!0.712761045 0.139202654 0.361759573 0.796536088 0.356012046 0.197665215 -!9.80764329E-02 0.781620383 0.595349193 0.125651121 0.957528770 0.942990601 -!0.259489566 7.84273148E-02 0.779313922 0.317909390 0.559013724 0.421358019 -!0.878484428 7.67416358E-02 0.298707575 0.693327367 0.146014273 0.102338850 -!0.855926156 0.250811368 0.300751567 0.110186398 0.502883077 0.738479793 -!0.764856219 0.294822574 1.90783739E-02 0.631218433 0.752170086 0.196848959 - - loc = (0., 0.) - scale=(2., 1.) - print *, uni_cdf((1.2,0.5), loc, scale) - ! joint cumulative distribution at (1.2,0.5) in [(0.,0.), (2.,1.)] - -! 0.300000012 -end program demo_uniform_cdf - +{!example/stats_distribution_uniform/example_uniform_cdf.f90!} ``` diff --git a/doc/specs/stdlib_string_type.md b/doc/specs/stdlib_string_type.md index 3349280d5..a0444b442 100644 --- a/doc/specs/stdlib_string_type.md +++ b/doc/specs/stdlib_string_type.md @@ -67,13 +67,7 @@ The result is an instance of `string_type` with zero length. #### Example ```fortran -program demo - use stdlib_string_type - implicit none - type(string_type) :: string - string = string_type() - ! len(string) == 0 -end program demo +{!example/string_type/example_constructor_empty.f90!} ``` @@ -111,15 +105,7 @@ The result is an instance of `string_type`. #### Example ```fortran -program demo - use stdlib_string_type - implicit none - type(string_type) :: string - string = string_type("Sequence") - ! len(string) == 8 - string = string_type(" S p a c e d ") - ! len(string) == 13 -end program demo +{!example/string_type/example_constructor_scalar.f90!} ``` @@ -153,15 +139,7 @@ The result is an instance of `string_type`. #### Example ```fortran -program demo - use stdlib_string_type - implicit none - type(string_type) :: string - string = string_type(42) - ! len(string) == 2 - string = string_type(-289) - ! len(string) == 4 -end program demo +{!example/string_type/example_constructor_integer.f90!} ``` @@ -195,15 +173,7 @@ The result is an instance of `string_type`. #### Example ```fortran -program demo - use stdlib_string_type - implicit none - type(string_type) :: string - string = string_type(.true.) - ! len(string) == 1 - string = string_type(.false.) - ! len(string) == 1 -end program demo +{!example/string_type/example_constructor_logical.f90!} ``` @@ -232,14 +202,7 @@ Elemental subroutine, `assignment(=)`. #### Example ```fortran -program demo - use stdlib_string_type - implicit none - type(string_type) :: string - ! len(string) == 0 - string = "Sequence" - ! len(string) == 8 -end program demo +{!example/string_type/example_constructor_character.f90!} ``` @@ -273,20 +236,7 @@ The result is a default integer scalar value. #### Example ```fortran -program demo - use stdlib_string_type - implicit none - type(string_type) :: string - integer :: length - - string = "Some longer sentence for this example." - length = len(string) - ! length == 38 - - string = "Whitespace " - length = len(string) - ! length == 38 -end program demo +{!example/string_type/example_len.f90!} ``` @@ -321,20 +271,7 @@ The result is a default integer scalar value. #### Example ```fortran -program demo - use stdlib_string_type - implicit none - type(string_type) :: string - integer :: length - - string = "Some longer sentence for this example." - length = len_trim(string) - ! length == 38 - - string = "Whitespace " - length = len_trim(string) - ! length == 10 -end program demo +{!example/string_type/example_len_trim.f90!} ``` @@ -369,15 +306,7 @@ The result is a scalar `string_type` value. #### Example ```fortran -program demo - use stdlib_string_type - implicit none - type(string_type) :: string - - string = "Whitespace " - string = trim(string) - ! len(string) == 10 -end program demo +{!example/string_type/example_trim.f90!} ``` @@ -412,15 +341,7 @@ The result is a scalar `string_type` value. #### Example ```fortran -program demo - use stdlib_string_type - implicit none - type(string_type) :: string - - string = " Whitespace" - string = adjustl(string) - ! char(string) == "Whitespace " -end program demo +{!example/string_type/example_adjustl.f90!} ``` @@ -455,15 +376,7 @@ The result is a scalar `string_type` value. #### Example ```fortran -program demo - use stdlib_string_type - implicit none - type(string_type) :: string - - string = "Whitespace " - string = adjustr(string) - ! char(string) == " Whitespace" -end program demo +{!example/string_type/example_adjustr.f90!} ``` @@ -499,15 +412,7 @@ The result is a scalar `string_type` value. #### Example ```fortran -program demo - use stdlib_string_type - implicit none - type(string_type) :: string - - string = "What? " - string = repeat(string, 3) - ! string == "What? What? What? " -end program demo +{!example/string_type/example_repeat.f90!} ``` @@ -541,16 +446,7 @@ The result is a scalar character value. #### Example ```fortran -program demo - use stdlib_string_type - implicit none - type(string_type) :: string - character(len=:), allocatable :: dlc - - string = "Character sequence" - dlc = char(string) - ! dlc == "Character sequence" -end program demo +{!example/string_type/example_char.f90!} ``` @@ -585,19 +481,7 @@ The result is a scalar character value. #### Example ```fortran -program demo - use stdlib_string_type - implicit none - type(string_type) :: string - character(len=:), allocatable :: dlc - character(len=1), allocatable :: chars(:) - - string = "Character sequence" - dlc = char(string, 3) - ! dlc == "a" - chars = char(string, [3, 5, 8, 12, 14, 15, 18]) - ! chars == ["a", "a", "e", "e", "u", "e", "e"] -end program demo +{!example/string_type/example_char_position.f90!} ``` @@ -633,16 +517,7 @@ The result is a scalar character value. #### Example ```fortran -program demo - use stdlib_string_type - implicit none - type(string_type) :: string - character(len=:), allocatable :: dlc - - string = "Fortran" - dlc = char(string, 1, 4) - ! dlc == "Fort" -end program demo +{!example/string_type/example_char_range.f90!} ``` @@ -679,15 +554,7 @@ The result is a default integer scalar value. #### Example ```fortran -program demo - use stdlib_string_type - implicit none - type(string_type) :: string - integer :: code - - string = "Fortran" - code = ichar(string) -end program demo +{!example/string_type/example_ichar.f90!} ``` @@ -724,15 +591,7 @@ The result is a default integer scalar value. #### Example ```fortran -program demo - use stdlib_string_type - implicit none - type(string_type) :: string - integer :: code - - string = "Fortran" - code = iachar(string) -end program demo +{!example/string_type/example_iachar.f90!} ``` @@ -772,22 +631,7 @@ The result is a default integer scalar value. #### Example ```fortran -program demo - use stdlib_string_type - implicit none - type(string_type) :: string - integer :: pos - - string = "Search this string for this expression" - pos = index(string, "this") - ! pos == 8 - - pos = index(string, "this", back=.true.) - ! pos == 24 - - pos = index(string, "This") - ! pos == 0 -end program demo +{!example/string_type/example_index.f90!} ``` @@ -827,22 +671,7 @@ The result is a default integer scalar value. #### Example ```fortran -program demo - use stdlib_string_type - implicit none - type(string_type) :: string - integer :: pos - - string = "fortran" - pos = scan(string, "ao") - ! pos == 2 - - pos = scan(string, "ao", .true.) - ! pos == 6 - - pos = scan(string, "c++") - ! pos == 0 -end program demo +{!example/string_type/example_scan.f90!} ``` @@ -882,28 +711,7 @@ The result is a default integer scalar value. #### Example ```fortran -program demo - use stdlib_string_type - implicit none - type(string_type) :: string - integer :: pos - - string = "fortran" - pos = verify(string, "ao") - ! pos == 1 - - pos = verify(string, "fo") - ! pos == 3 - - pos = verify(string, "c++") - ! pos == 1 - - pos = verify(string, "c++", back=.true.) - ! pos == 7 - - pos = verify(string, string) - ! pos == 0 -end program demo +{!example/string_type/example_verify.f90!} ``` @@ -942,22 +750,7 @@ The result is a default logical scalar value. #### Example ```fortran -program demo - use stdlib_string_type - implicit none - type(string_type) :: string - logical :: res - - string = "bcd" - res = lgt(string, "abc") - ! res .eqv. .true. - - res = lgt(string, "bcd") - ! res .eqv. .false. - - res = lgt(string, "cde") - ! res .eqv. .false. -end program demo +{!example/string_type/example_lgt.f90!} ``` @@ -996,22 +789,7 @@ The result is a default logical scalar value. #### Example ```fortran -program demo - use stdlib_string_type - implicit none - type(string_type) :: string - logical :: res - - string = "bcd" - res = llt(string, "abc") - ! res .eqv. .false. - - res = llt(string, "bcd") - ! res .eqv. .false. - - res = llt(string, "cde") - ! res .eqv. .true. -end program demo +{!example/string_type/example_llt.f90!} ``` @@ -1051,22 +829,7 @@ The result is a default logical scalar value. #### Example ```fortran -program demo - use stdlib_string_type - implicit none - type(string_type) :: string - logical :: res - - string = "bcd" - res = lge(string, "abc") - ! res .eqv. .true. - - res = lge(string, "bcd") - ! res .eqv. .true. - - res = lge(string, "cde") - ! res .eqv. .false. -end program demo +{!example/string_type/example_lge.f90!} ``` @@ -1106,22 +869,7 @@ The result is a default logical scalar value. #### Example ```fortran -program demo - use stdlib_string_type - implicit none - type(string_type) :: string - logical :: res - - string = "bcd" - res = lle(string, "abc") - ! res .eqv. .false. - - res = lle(string, "bcd") - ! res .eqv. .true. - - res = lle(string, "cde") - ! res .eqv. .true. -end program demo +{!example/string_type/example_lle.f90!} ``` @@ -1156,18 +904,7 @@ The result is a scalar `string_type` value. #### Example ```fortran -program demo - use stdlib_string_type - implicit none - type(string_type) :: string, lowercase_string - - string = "Lowercase This String" - ! string <-- "Lowercase This String" - - lowercase_string = to_lower(string) - ! string <-- "Lowercase This String" - ! lowercase_string <-- "lowercase this string" -end program demo +{!example/string_type/example_to_lower.f90!} ``` @@ -1202,18 +939,7 @@ The result is a scalar `string_type` value. #### Example ```fortran -program demo - use stdlib_string_type - implicit none - type(string_type) :: string, uppercase_string - - string = "Uppercase This String" - ! string <-- "Uppercase This String" - - uppercase_string = to_upper(string) - ! string <-- "Uppercase This String" - ! uppercase_string <-- "UPPERCASE THIS STRING" -end program demo +{!example/string_type/example_to_upper.f90!} ``` @@ -1253,18 +979,7 @@ The result is a scalar `string_type` value. #### Example ```fortran -program demo_to_title - use stdlib_string_type - implicit none - type(string_type) :: string, titlecase_string - - string = "titlecase this string." - ! string <-- "titlecase this string." - - titlecase_string = to_title(string) - ! string <-- "titlecase this string." - ! titlecase_string <-- "Titlecase This String." -end program demo_to_title +{!example/string_type/example_to_title.f90!} ``` @@ -1301,18 +1016,7 @@ The result is a scalar `string_type` value. #### Example ```fortran -program demo_to_sentence - use stdlib_string_type - implicit none - type(string_type) :: string, sentencecase_string - - string = "sentencecase this string." - ! string <-- "sentencecase this string." - - sentencecase_string = to_sentence(string) - ! string <-- "sentencecase this string." - ! sentencecase_string <-- "Sentencecase this string." -end program demo_to_sentence +{!example/string_type/example_to_sentence.f90!} ``` @@ -1346,18 +1050,7 @@ The result is a scalar `string_type` value. #### Example ```fortran -program demo - use stdlib_string_type - implicit none - type(string_type) :: string, reverse_string - - string = "Reverse This String" - ! string <-- "Reverse This String" - - reverse_string = reverse(string) - ! string <-- "Reverse This String" - ! reverse_string <-- "gnirtS sihT esreveR" -end program demo +{!example/string_type/example_reverse.f90!} ``` @@ -1399,22 +1092,7 @@ The result is a default logical scalar value. #### Example ```fortran -program demo - use stdlib_string_type - implicit none - type(string_type) :: string - logical :: res - - string = "bcd" - res = string > "abc" - ! res .eqv. .true. - - res = string > "bcd" - ! res .eqv. .false. - - res = string > "cde" - ! res .eqv. .false. -end program demo +{!example/string_type/example_gt.f90!} ``` @@ -1456,22 +1134,7 @@ The result is a default logical scalar value. #### Example ```fortran -program demo - use stdlib_string_type - implicit none - type(string_type) :: string - logical :: res - - string = "bcd" - res = string < "abc" - ! res .eqv. .false. - - res = string < "bcd" - ! res .eqv. .false. - - res = string < "cde" - ! res .eqv. .true. -end program demo +{!example/string_type/example_lt.f90!} ``` @@ -1513,22 +1176,7 @@ The result is a default logical scalar value. #### Example ```fortran -program demo - use stdlib_string_type - implicit none - type(string_type) :: string - logical :: res - - string = "bcd" - res = string >= "abc" - ! res .eqv. .true. - - res = string >= "bcd" - ! res .eqv. .true. - - res = string >= "cde" - ! res .eqv. .false. -end program demo +{!example/string_type/example_ge.f90!} ``` @@ -1570,22 +1218,7 @@ The result is a default logical scalar value. #### Example ```fortran -program demo - use stdlib_string_type - implicit none - type(string_type) :: string - logical :: res - - string = "bcd" - res = string <= "abc" - ! res .eqv. .false. - - res = string <= "bcd" - ! res .eqv. .true. - - res = string <= "cde" - ! res .eqv. .true. -end program demo +{!example/string_type/example_le.f90!} ``` @@ -1627,22 +1260,7 @@ The result is a default logical scalar value. #### Example ```fortran -program demo - use stdlib_string_type - implicit none - type(string_type) :: string - logical :: res - - string = "bcd" - res = string == "abc" - ! res .eqv. .false. - - res = string == "bcd" - ! res .eqv. .true. - - res = string == "cde" - ! res .eqv. .false. -end program demo +{!example/string_type/example_eq.f90!} ``` @@ -1684,22 +1302,7 @@ The result is a default logical scalar value. #### Example ```fortran -program demo - use stdlib_string_type - implicit none - type(string_type) :: string - logical :: res - - string = "bcd" - res = string /= "abc" - ! res .eqv. .true. - - res = string /= "bcd" - ! res .eqv. .false. - - res = string /= "cde" - ! res .eqv. .true. -end program demo +{!example/string_type/example_ne.f90!} ``` @@ -1738,15 +1341,7 @@ The result is an instance of `string_type`. #### Example ```fortran -program demo - use stdlib_string_type - implicit none - type(string_type) :: string - - string = "Hello, " - string = string // "World!" - ! len(string) == 13 -end program demo +{!example/string_type/example_cont.f90!} ``` @@ -1783,21 +1378,7 @@ Unformatted user defined derived type output. #### Example ```fortran -program demo - use stdlib_string_type - implicit none - type(string_type) :: string - integer :: io - string = "Important saved value" - - open(newunit=io, form="unformatted", status="scratch") - write(io) string - - rewind(io) - - read(io) string - close(io) -end program demo +{!example/string_type/example_uwrite.f90!} ``` @@ -1840,22 +1421,7 @@ Formatted user defined derived type output. #### Example ```fortran -program demo - use stdlib_string_type - implicit none - type(string_type) :: string - integer :: io - string = "Important saved value" - - open(newunit=io, form="formatted", status="scratch") - write(io, *) string - write(io, *) - - rewind(io) - - read(io, *) string - close(io) -end program demo +{!example/string_type/example_fwrite.f90!} ``` @@ -1894,21 +1460,7 @@ Unformatted derived type input. #### Example ```fortran -program demo - use stdlib_string_type - implicit none - type(string_type) :: string - integer :: io - string = "Important saved value" - - open(newunit=io, form="unformatted", status="scratch") - write(io) string - - rewind(io) - - read(io) string - close(io) -end program demo +{!example/string_type/example_uread.f90!} ``` @@ -1955,22 +1507,7 @@ Formatted derived type input. #### Example ```fortran -program demo - use stdlib_string_type - implicit none - type(string_type) :: string - integer :: io - string = "Important saved value" - - open(newunit=io, form="formatted", status="scratch") - write(io, *) string - write(io, *) - - rewind(io) - - read(io, *) string - close(io) -end program demo +{!example/string_type/example_fread.f90!} ``` @@ -2005,25 +1542,5 @@ Pure subroutine (Elemental subroutine, only when both `from` and `to` are `type( #### Example ```fortran -program demo_move - use stdlib_string_type, only : string_type, assignment(=), move - implicit none - type(string_type) :: from_string - character(len=:), allocatable :: from_char, to_char - - from_string = "move this string" - from_char = "move this char" - ! from_string <-- "move this string" - ! from_char <-- "move this char" - ! to_char <-- (unallocated) - - call move(from_string, to_char) - ! from_string <-- "" - ! to_char <-- "move this string" - - call move(from_char, to_char) - ! from_char <-- (unallocated) - ! to_string <-- "move this char" - -end program demo_move +{!example/string_type/example_move.f90!} ``` diff --git a/doc/specs/stdlib_stringlist_type.md b/doc/specs/stdlib_stringlist_type.md index 7a4141d93..27f4f19c3 100644 --- a/doc/specs/stdlib_stringlist_type.md +++ b/doc/specs/stdlib_stringlist_type.md @@ -72,19 +72,7 @@ The result is of type `stringlist_index_type`. #### Example ```fortran -program demo_fidx_bidx - use stdlib_stringlist_type, only: stringlist_index_type, fidx, bidx - implicit none - - type(stringlist_index_type) :: index - - index = fidx(1) - ! forward index 1 - - index = bidx(3) - ! backward index 3 - -end program demo_fidx_bidx +{!example/stringlist_type/example_stringlist_type_fidx_bidx.f90!} ``` @@ -120,23 +108,7 @@ The result is an instance of type `stringlist_type`. #### Example ```fortran -program demo_constructor - use stdlib_stringlist_type, only: stringlist_type - use stdlib_string_type, only: string_type - implicit none - - type(stringlist_type) :: stringlist - - stringlist = stringlist_type() - ! stringlist <-- { } (empty stringlist) - - stringlist = stringlist_type(["#1", "#2", "#3"]) - ! stringlist <-- {"#1", "#2", "#3"} - - stringlist = stringlist_type([string_type("#1"), string_type("#2")]) - ! stringlist <-- {"#1", "#2"} - -end program demo_constructor +{!example/stringlist_type/example_stringlist_type_constructor.f90!} ``` @@ -170,29 +142,7 @@ Pure subroutine. #### Example ```fortran -program demo_insert_at - use stdlib_stringlist_type, only: stringlist_type, stringlist_index_type, fidx, bidx - use stdlib_string_type, only: string_type - implicit none - - type(stringlist_type) :: stringlist - type(stringlist_index_type) :: index - - index = fidx(1) - call stringlist%insert_at( index, "Element No. one" ) - ! stringlist <-- {"Element No. one"} - - index = bidx(1) - call stringlist%insert_at( index, string_type( "Element No. two" ) ) - ! stringlist <-- {"Element No. one", "Element No. two"} - - call stringlist%insert_at( fidx(2), string_type( "Element No. three" ) ) - ! stringlist <-- {"Element No. one", "Element No. three", "Element No. two"} - - call stringlist%insert_at( bidx(1), "Element No. four" ) - ! stringlist <-- {"Element No. one", "Element No. three", "Element No. two", "Element No. four"} - -end program demo_insert_at +{!example/stringlist_type/example_stringlist_type_insert_at.f90!} ``` @@ -227,34 +177,7 @@ The result is a string of type `string_type`. #### Example ```fortran -program demo_get - use stdlib_stringlist_type, only: stringlist_type, fidx, bidx - use stdlib_string_type, only: string_type - implicit none - - type(stringlist_type) :: stringlist - type(string_type) :: output - - !> inserting 4 elements to the stringlist - call stringlist%insert_at( fidx(1), "Element No. one" ) - call stringlist%insert_at( fidx(1), "Element No. two" ) - call stringlist%insert_at( fidx(1), "Element No. three" ) - call stringlist%insert_at( fidx(1), "Element No. four" ) - ! stringlist <-- {"Element No. four", "Element No. three", "Element No. two", "Element No. one"} - - output = stringlist%get( fidx(1) ) - ! output <-- "Element No. four" - - output = stringlist%get( bidx(1) ) - ! output <-- "Element No. one" - - !> accessing out of bounds index - output = stringlist%get( bidx(5) ) - ! output <-- "" - output = stringlist%get( fidx(0) ) - ! output <-- "" - -end program demo_get +{!example/stringlist_type/example_stringlist_type_get.f90!} ``` @@ -288,25 +211,7 @@ The result is of type `integer`. #### Example ```fortran -program demo_len - use stdlib_stringlist_type, only: stringlist_type, bidx - implicit none - - type(stringlist_type) :: stringlist - integer :: output - - output = stringlist%len() - ! output <-- 0 - - !> inserting 2 elements to the stringlist - call stringlist%insert_at( bidx(1), "Element No. one" ) - call stringlist%insert_at( bidx(1), "Element No. two" ) - ! stringlist <-- {"Element No. one", "Element No. two"} - - print'(a)', stringlist%len() - ! 2 - -end program demo_len +{!example/stringlist_type/example_stringlist_type_len.f90!} ``` @@ -336,25 +241,7 @@ No arguments. #### Example ```fortran -program demo_clear - use stdlib_stringlist_type, only: stringlist_type, fidx - implicit none - - type(stringlist_type) :: stringlist - - !> inserting 2 elements to the stringlist - call stringlist%insert_at( fidx(1), "Element No. one" ) - call stringlist%insert_at( fidx(1), "Element No. two" ) - ! stringlist <-- {"Element No. two", "Element No. one"} - - call stringlist%clear() - ! stringlist <-- { } (empty stringlist) - - !> inserting 1 element to the stringlist - call stringlist%insert_at( fidx(1), "Element No. one" ) - ! stringlist <-- {"Element No. one"} - -end program demo_clear +{!example/stringlist_type/example_stringlist_type_clear.f90!} ``` @@ -396,35 +283,7 @@ The result is a default `logical` scalar value. #### Example ```fortran -program demo_equality_operator - use stdlib_stringlist_type, only: stringlist_type, fidx, list_head, operator(==) - use stdlib_string_type, only: string_type - implicit none - - type(stringlist_type) :: stringlist - type(string_type), allocatable :: stringarray(:) - logical :: res - - !> inserting 4 elements to the stringlist - call stringlist%insert_at( fidx(1), "#1" ) - call stringlist%insert_at( list_head, "#2" ) - call stringlist%insert_at( fidx(1), "#3" ) - call stringlist%insert_at( list_head, "#4" ) - ! stringlist <-- {"#4", "#3", "#2", "#1"} - - !> creating an array of 4 string_type elements - stringarray = [string_type("#4"), string_type("#3"), string_type("#2"), string_type("#1")] - - res = ( stringarray == stringlist ) - ! res <-- .true. - - res = ( stringlist == ["#4", "#3", "#2", "#1"] ) - ! res <-- .true. - - print'(a)', stringlist == ["#4", "#3", "#1"] - ! .false. - -end program demo_equality_operator +{!example/stringlist_type/example_stringlist_type_equality_operator.f90!} ``` @@ -466,35 +325,7 @@ The result is a default `logical` scalar value. #### Example ```fortran -program demo_inequality_operator - use stdlib_stringlist_type, only: stringlist_type, bidx, list_tail, operator(/=) - use stdlib_string_type, only: string_type - implicit none - - type(stringlist_type) :: stringlist - type(string_type), allocatable :: stringarray(:) - logical :: res - - !> inserting 4 elements to the stringlist - call stringlist%insert_at( bidx(1), "#1" ) - call stringlist%insert_at( list_tail, "#2" ) - call stringlist%insert_at( bidx(1), "#3" ) - call stringlist%insert_at( list_tail, "#4" ) - ! stringlist <-- {"#1", "#2", "#3", "#4"} - - !> creating an array of 4 string_type elements - stringarray = [string_type("#1"), string_type("#2"), string_type("#3"), string_type("#4")] - - res = ( stringarray /= stringlist ) - ! res <-- .false. - - res = ( stringlist /= ["#111", "#222", "#333", "#444"] ) - ! res <-- .true. - - print'(a)', stringlist /= ["#4", "#3", "#1"] - ! .true. - -end program demo_inequality_operator +{!example/stringlist_type/example_stringlist_type_inequality_operator.f90!} ``` @@ -534,31 +365,5 @@ The result is an instance of `[[stdlib_stringlist_type(module):stringlist_type(t #### Example ```fortran -program demo_concatenate_operator - use stdlib_stringlist_type, only: stringlist_type, operator(//) - use stdlib_string_type, only: string_type - implicit none - - type(stringlist_type) :: first_stringlist, second_stringlist - type(string_type), allocatable :: stringarray(:) - - first_stringlist = first_stringlist // "Element No. one" - ! first_stringlist <-- {"Element No. one"} - - second_stringlist = string_type("Element No. two") // first_stringlist - ! second_stringlist <-- {Element No. two, "Element No. one"} - - !> Creating an array of 2 string_type elements - stringarray = [string_type("Element No. three"), string_type("Element No. four")] - - second_stringlist = first_stringlist // stringarray - ! second_stringlist <-- {"Element No. one", "Element No. three", "Element No. four"} - - second_stringlist = ["#1", "#2"] // second_stringlist - ! second_stringlist <-- {"#1", "#2", "Element No. one", "Element No. three", "Element No. four"} - - first_stringlist = first_stringlist // second_stringlist - ! first_stringlist <-- {"Element No. one", "#1", "#2", "Element No. one", "Element No. three", "Element No. four"} - -end program demo_concatenate_operator +{!example/stringlist_type/example_stringlist_type_concatenate_operator.f90!} ``` diff --git a/doc/specs/stdlib_strings.md b/doc/specs/stdlib_strings.md index 714869118..cb2c18c59 100644 --- a/doc/specs/stdlib_strings.md +++ b/doc/specs/stdlib_strings.md @@ -45,16 +45,7 @@ The result is of the same type as `string`. #### Example ```fortran -program demo - use stdlib_ascii, only : TAB, VT, NUL, LF, CR, FF - use stdlib_strings, only : strip - implicit none - print'(a)', strip(" hello ") ! "hello" - print'(a)', strip(TAB//"goodbye"//CR//LF) ! "goodbye" - print'(a)', strip(" "//TAB//LF//VT//FF//CR) ! "" - print'(a)', strip(" ! ")//"!" ! "!!" - print'(a)', strip("Hello") ! "Hello" -end program demo +{!example/strings/example_strip.f90!} ``` @@ -93,20 +84,7 @@ The result is of the same type as `string`. #### Example ```fortran -program demo - use stdlib_ascii, only : TAB, VT, NUL, LF, CR, FF - use stdlib_strings, only : chomp - implicit none - print'(a)', chomp(" hello ") ! " hello" - print'(a)', chomp(TAB//"goodbye"//CR//LF) ! "\tgoodbye" - print'(a)', chomp(" "//TAB//LF//VT//FF//CR) ! "" - print'(a)', chomp(" ! ")//"!" ! " !!" - print'(a)', chomp("Hello") ! "Hello" - print'(a)', chomp("hello", ["l", "o"]) ! "he" - print'(a)', chomp("hello", set=["l", "o"]) ! "he" - print'(a)', chomp("hello", "lo") ! "hel" - print'(a)', chomp("hello", substring="lo") ! "hel" -end program demo +{!example/strings/example_chomp.f90!} ``` @@ -143,12 +121,7 @@ The result is of scalar logical type. #### Example ```fortran -program demo - use stdlib_strings, only : starts_with - implicit none - print'(a)', starts_with("pattern", "pat") ! T - print'(a)', starts_with("pattern", "ern") ! F -end program demo +{!example/strings/example_starts_with.f90!} ``` @@ -185,12 +158,7 @@ The result is of scalar logical type. #### Example ```fortran -program demo - use stdlib_strings, only : ends_with - implicit none - print'(a)', ends_with("pattern", "ern") ! T - print'(a)', ends_with("pattern", "pat") ! F -end program demo +{!example/strings/example_ends_with.f90!} ``` @@ -245,26 +213,7 @@ The result is of the same type as `string`. #### Example ```fortran -program demo_slice - use stdlib_string_type - use stdlib_strings, only : slice - implicit none - type(string_type) :: string - character(len=10) :: char - - string = "abcdefghij" - ! string <-- "abcdefghij" - - char = "abcdefghij" - ! char <-- "abcdefghij" - - print'(a)', slice("abcdefghij", 2, 6, 2) ! "bdf" - print'(a)', slice(char, 2, 6, 2) ! "bdf" - - string = slice(string, 2, 6, 2) - ! string <-- "bdf" - -end program demo_slice +{!example/strings/example_slice.f90!} ``` @@ -309,19 +258,7 @@ The result is a scalar of integer type or an integer array of rank equal to the #### Example ```fortran -program demo_find - use stdlib_string_type, only: string_type, assignment(=) - use stdlib_strings, only : find - implicit none - type(string_type) :: string - - string = "needle in the character-stack" - - print *, find(string, "needle") ! 1 - print *, find(string, ["a", "c"], [3, 2]) ! [27, 20] - print *, find("qwqwqwq", "qwq", 3, [.false., .true.]) ! [0, 5] - -end program demo_find +{!example/strings/example_find.f90!} ``` @@ -361,22 +298,7 @@ The result is of the same type as `string`. #### Example ```fortran -program demo_replace_all - use stdlib_string_type, only: string_type, assignment(=) - use stdlib_strings, only : replace_all - implicit none - type(string_type) :: string - - string = "hurdles here, hurdles there, hurdles everywhere" - ! string <-- "hurdles here, hurdles there, hurdles everywhere" - - print'(a)', replace_all(string, "hurdles", "learn from") - ! "learn from here, learn from there, learn from everywhere" - - string = replace_all(string, "hurdles", "technology") - ! string <-- "technology here, technology there, technology everywhere" - -end program demo_replace_all +{!example/strings/example_replace_all.f90!} ``` @@ -416,21 +338,7 @@ The result is of the same type as `string`. #### Example ```fortran -program demo_padl - use stdlib_string_type, only: string_type, assignment(=) - use stdlib_strings, only : padl - implicit none - string_type :: string - - string = "left pad this string" - ! string <-- "left pad this string" - - print *, padl(string, 25, "$") ! "$$$$$left pad this string" - - string = padl(string, 25) - ! string <-- " left pad this string" - -end program demo_padl +{!example/strings/example_padl.f90!} ``` @@ -470,21 +378,7 @@ The result is of the same type as `string`. #### Example ```fortran -program demo_padr - use stdlib_string_type, only: string_type, assignment(=) - use stdlib_strings, only : padr - implicit none - string_type :: string - - string = "right pad this string" - ! string <-- "right pad this string" - - print *, padr(string, 25, "$") ! "right pad this string$$$$" - - string = padr(string, 25) - ! string <-- "right pad this string " - -end program demo_padr +{!example/strings/example_padr.f90!} ``` @@ -524,19 +418,7 @@ The result is a scalar of integer type or an integer array of rank equal to the #### Example ```fortran -program demo_count - use stdlib_string_type, only: string_type, assignment(=) - use stdlib_strings, only : count - implicit none - type(string_type) :: string - - string = "How much wood would a woodchuck chuck if a woodchuck could chuck wood?" - - print *, count(string, "wood") ! 4 - print *, count(string, ["would", "chuck", "could"]) ! [1, 4, 1] - print *, count("a long queueueueue", "ueu", [.false., .true.]) ! [2, 4] - -end program demo_count +{!example/strings/example_count.f90!} ``` @@ -575,35 +457,5 @@ The result is an `allocatable` length `character` scalar with up to `128` cached #### Example ```fortran -program demo_to_string - use stdlib_strings, only: to_string - - !> Example for `complex` type - print *, to_string((1, 1)) !! "(1.00000000,1.00000000)" - print *, to_string((1, 1), '(F6.2)') !! "( 1.00, 1.00)" - print *, to_string((1000, 1), '(ES0.2)'), to_string((1000, 1), '(SP,F6.3)') - !! "(1.00E+3,1.00)""(******,+1.000)" - !! Too narrow formatter for real number - !! Normal demonstration(`******` from Fortran Standard) - - !> Example for `integer` type - print *, to_string(-3) !! "-3" - print *, to_string(42, '(I4)') !! " 42" - print *, to_string(1, '(I0.4)'), to_string(2, '(B4)') !! "0001"" 10" - - !> Example for `real` type - print *, to_string(1.) !! "1.00000000" - print *, to_string(1., '(F6.2)') !! " 1.00" - print *, to_string(1., 'F6.2') !! " 1.00" - print *, to_string(1., '(SP,ES9.2)'), to_string(1, '(F7.3)') !! "+1.00E+00""[*]" - !! 1 wrong demonstration (`[*]` from `to_string`) - - !> Example for `logical` type - print *, to_string(.true.) !! "T" - print *, to_string(.true., '(L2)') !! " T" - print *, to_string(.true., 'L2') !! " T" - print *, to_string(.false., '(I5)') !! "[*]" - !! 1 wrong demonstrations(`[*]` from `to_string`) - -end program demo_to_string +{!example/strings/example_to_string.f90!} ``` diff --git a/doc/specs/stdlib_version.md b/doc/specs/stdlib_version.md index 2739fe2cf..26ddc4927 100644 --- a/doc/specs/stdlib_version.md +++ b/doc/specs/stdlib_version.md @@ -54,11 +54,5 @@ Pure subroutine. #### Example ```fortran -program demo_version - use stdlib_version, only : get_stdlib_version - implicit none - character(len=:), allocatable :: version - call get_stdlib_version(string=version) - print '(a)', version -end program demo_version +{!example/version/example_version.f90!} ``` diff --git a/example/CMakeLists.txt b/example/CMakeLists.txt new file mode 100644 index 000000000..3dd43694f --- /dev/null +++ b/example/CMakeLists.txt @@ -0,0 +1,32 @@ +macro(ADD_EXAMPLE name) + add_executable(example_${name} example_${name}.f90) + target_link_libraries(example_${name} "${PROJECT_NAME}") + add_test(NAME ${name} + COMMAND $ ${CMAKE_CURRENT_BINARY_DIR} + WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}) +endmacro(ADD_EXAMPLE) + +add_subdirectory(array) +add_subdirectory(ascii) +add_subdirectory(bitsets) +add_subdirectory(error) +add_subdirectory(hashmaps) +add_subdirectory(hash_procedures) +add_subdirectory(io) +add_subdirectory(linalg) +add_subdirectory(logger) +add_subdirectory(math) +add_subdirectory(optval) +add_subdirectory(quadrature) +add_subdirectory(random) +add_subdirectory(selection) +add_subdirectory(sorting) +add_subdirectory(specialfunctions_gamma) +add_subdirectory(stats) +add_subdirectory(stats_distribution_exponential) +add_subdirectory(stats_distribution_normal) +add_subdirectory(stats_distribution_uniform) +add_subdirectory(stringlist_type) +add_subdirectory(strings) +add_subdirectory(string_type) +add_subdirectory(version) diff --git a/example/array/CMakeLists.txt b/example/array/CMakeLists.txt new file mode 100644 index 000000000..9af8de306 --- /dev/null +++ b/example/array/CMakeLists.txt @@ -0,0 +1,2 @@ +ADD_EXAMPLE(falseloc) +ADD_EXAMPLE(trueloc) diff --git a/example/array/example_falseloc.f90 b/example/array/example_falseloc.f90 new file mode 100644 index 000000000..dd06721a6 --- /dev/null +++ b/example/array/example_falseloc.f90 @@ -0,0 +1,8 @@ +program example_falseloc + use stdlib_array, only: falseloc + implicit none + real, allocatable :: array(:) + allocate (array(-200:200)) + call random_number(array) + array(falseloc(array < 0.5, lbound(array, 1))) = 0.0 +end program example_falseloc diff --git a/example/array/example_trueloc.f90 b/example/array/example_trueloc.f90 new file mode 100644 index 000000000..a6e6fffb4 --- /dev/null +++ b/example/array/example_trueloc.f90 @@ -0,0 +1,8 @@ +program example_trueloc + use stdlib_array, only: trueloc + implicit none + real, allocatable :: array(:) + allocate (array(500)) + call random_number(array) + array(trueloc(array > 0.5)) = 0.0 +end program example_trueloc diff --git a/example/ascii/CMakeLists.txt b/example/ascii/CMakeLists.txt new file mode 100644 index 000000000..e346ac61d --- /dev/null +++ b/example/ascii/CMakeLists.txt @@ -0,0 +1,5 @@ +ADD_EXAMPLE(ascii_reverse) +ADD_EXAMPLE(ascii_to_lower) +ADD_EXAMPLE(ascii_to_sentence) +ADD_EXAMPLE(ascii_to_title) +ADD_EXAMPLE(ascii_to_upper) diff --git a/example/ascii/example_ascii_reverse.f90 b/example/ascii/example_ascii_reverse.f90 new file mode 100644 index 000000000..ef5901851 --- /dev/null +++ b/example/ascii/example_ascii_reverse.f90 @@ -0,0 +1,5 @@ +program example_reverse + use stdlib_ascii, only: reverse + implicit none + print'(a)', reverse("Hello, World!") ! returns "!dlroW ,olleH" +end program example_reverse diff --git a/example/ascii/example_ascii_to_lower.f90 b/example/ascii/example_ascii_to_lower.f90 new file mode 100644 index 000000000..dca955010 --- /dev/null +++ b/example/ascii/example_ascii_to_lower.f90 @@ -0,0 +1,5 @@ +program example_to_lower + use stdlib_ascii, only: to_lower + implicit none + print'(a)', to_lower("HELLo!") ! returns "hello!" +end program example_to_lower diff --git a/example/ascii/example_ascii_to_sentence.f90 b/example/ascii/example_ascii_to_sentence.f90 new file mode 100644 index 000000000..34f7ea872 --- /dev/null +++ b/example/ascii/example_ascii_to_sentence.f90 @@ -0,0 +1,7 @@ +program example_to_sentence + use stdlib_ascii, only: to_sentence + implicit none + print *, to_sentence("hello!") ! returns "Hello!" + print *, to_sentence("'enquoted'") ! returns "'Enquoted'" + print *, to_sentence("1st") ! returns "1st" +end program example_to_sentence diff --git a/example/ascii/example_ascii_to_title.f90 b/example/ascii/example_ascii_to_title.f90 new file mode 100644 index 000000000..50931ca07 --- /dev/null +++ b/example/ascii/example_ascii_to_title.f90 @@ -0,0 +1,7 @@ +program example_to_title + use stdlib_ascii, only: to_title + implicit none + print *, to_title("hello there!") ! returns "Hello There!" + print *, to_title("'enquoted'") ! returns "'Enquoted'" + print *, to_title("1st") ! returns "1st" +end program example_to_title diff --git a/example/ascii/example_ascii_to_upper.f90 b/example/ascii/example_ascii_to_upper.f90 new file mode 100644 index 000000000..93c12a18f --- /dev/null +++ b/example/ascii/example_ascii_to_upper.f90 @@ -0,0 +1,5 @@ +program example_to_upper + use stdlib_ascii, only: to_upper + implicit none + print'(a)', to_upper("hello!") ! returns "HELLO!" +end program example_to_upper diff --git a/example/bitsets/CMakeLists.txt b/example/bitsets/CMakeLists.txt new file mode 100644 index 000000000..6e380ebf2 --- /dev/null +++ b/example/bitsets/CMakeLists.txt @@ -0,0 +1,30 @@ +ADD_EXAMPLE(bitsets_all) +ADD_EXAMPLE(bitsets_and) +ADD_EXAMPLE(bitsets_and_not) +ADD_EXAMPLE(bitsets_any) +ADD_EXAMPLE(bitsets_assignment) +ADD_EXAMPLE(bitsets_bit_count) +ADD_EXAMPLE(bitsets_bits) +ADD_EXAMPLE(bitsets_clear) +ADD_EXAMPLE(bitsets_equality) +ADD_EXAMPLE(bitsets_extract) +ADD_EXAMPLE(bitsets_flip) +ADD_EXAMPLE(bitsets_from_string) +ADD_EXAMPLE(bitsets_ge) +ADD_EXAMPLE(bitsets_gt) +ADD_EXAMPLE(bitsets_inequality) +ADD_EXAMPLE(bitsets_init) +ADD_EXAMPLE(bitsets_input) +ADD_EXAMPLE(bitsets_le) +ADD_EXAMPLE(bitsets_lt) +ADD_EXAMPLE(bitsets_none) +ADD_EXAMPLE(bitsets_not) +ADD_EXAMPLE(bitsets_or) +ADD_EXAMPLE(bitsets_output) +ADD_EXAMPLE(bitsets_read_bitset) +ADD_EXAMPLE(bitsets_set) +ADD_EXAMPLE(bitsets_test) +ADD_EXAMPLE(bitsets_to_string) +ADD_EXAMPLE(bitsets_value) +ADD_EXAMPLE(bitsets_write_bitset) +ADD_EXAMPLE(bitsets_xor) diff --git a/example/bitsets/example_bitsets_all.f90 b/example/bitsets/example_bitsets_all.f90 new file mode 100644 index 000000000..39fc18632 --- /dev/null +++ b/example/bitsets/example_bitsets_all.f90 @@ -0,0 +1,14 @@ +program example_all + use stdlib_bitsets + character(*), parameter :: & + bits_all = '111111111111111111111111111111111' + type(bitset_64) :: set0 + call set0%from_string(bits_all) + if (.not. set0%all()) then + error stop "FROM_STRING failed to interpret"// & + "BITS_ALL's value properly." + else + write (*, *) "FROM_STRING transferred BITS_ALL properly"// & + " into set0." + end if +end program example_all diff --git a/example/bitsets/example_bitsets_and.f90 b/example/bitsets/example_bitsets_and.f90 new file mode 100644 index 000000000..d70e54359 --- /dev/null +++ b/example/bitsets/example_bitsets_and.f90 @@ -0,0 +1,17 @@ +program example_and + use stdlib_bitsets + type(bitset_large) :: set0, set1 + call set0%init(166) + call set1%init(166) + call and(set0, set1) ! none none + if (set0%none()) write (*, *) 'First test of AND worked.' + call set0%not() + call and(set0, set1) ! all none + if (set0%none()) write (*, *) 'Second test of AND worked.' + call set1%not() + call and(set0, set1) ! none all + if (set0%none()) write (*, *) 'Third test of AND worked.' + call set0%not() + call and(set0, set1) ! all all + if (set0%all()) write (*, *) 'Fourth test of AND worked.' +end program example_and diff --git a/example/bitsets/example_bitsets_and_not.f90 b/example/bitsets/example_bitsets_and_not.f90 new file mode 100644 index 000000000..8e3677c8a --- /dev/null +++ b/example/bitsets/example_bitsets_and_not.f90 @@ -0,0 +1,18 @@ +program example_and_not + use stdlib_bitsets + type(bitset_large) :: set0, set1 + call set0%init(166) + call set1%init(166) + call and_not(set0, set1) ! none none + if (set0%none()) write (*, *) 'First test of AND_NOT worked.' + call set0%not() + call and_not(set0, set1) ! all none + if (set0%all()) write (*, *) 'Second test of AND_NOT worked.' + call set0%not() + call set1%not() + call and_not(set0, set1) ! none all + if (set0%none()) write (*, *) 'Third test of AND_NOT worked.' + call set0%not() + call and_not(set0, set1) ! all all + if (set0%none()) write (*, *) 'Fourth test of AND_NOT worked.' +end program example_and_not diff --git a/example/bitsets/example_bitsets_any.f90 b/example/bitsets/example_bitsets_any.f90 new file mode 100644 index 000000000..3224fc074 --- /dev/null +++ b/example/bitsets/example_bitsets_any.f90 @@ -0,0 +1,15 @@ +program example_any + use stdlib_bitsets + character(*), parameter :: & + bits_0 = '0000000000000000000' + type(bitset_64) :: set0 + call set0%from_string(bits_0) + if (.not. set0%any()) then + write (*, *) "FROM_STRING interpreted "// & + "BITS_0's value properly." + end if + call set0%set(5) + if (set0%any()) then + write (*, *) "ANY interpreted SET0's value properly." + end if +end program example_any diff --git a/example/bitsets/example_bitsets_assignment.f90 b/example/bitsets/example_bitsets_assignment.f90 new file mode 100644 index 000000000..3493c1b6a --- /dev/null +++ b/example/bitsets/example_bitsets_assignment.f90 @@ -0,0 +1,26 @@ +program example_assignment + use stdlib_bitsets + use stdlib_kinds, only: int8, int32 + implicit none + logical(int8) :: logical1(64) = .true. + logical(int32), allocatable :: logical2(:) + type(bitset_64) :: set0, set1 + set0 = logical1 + if (set0%bits() /= 64) then + error stop & + ' initialization with logical(int8) failed to set'// & + ' the right size.' + else if (.not. set0%all()) then + error stop ' initialization with'// & + ' logical(int8) failed to set the right values.' + else + write (*, *) 'Initialization with logical(int8) succeeded.' + end if + set1 = set0 + if (set1 == set0) & + write (*, *) 'Initialization by assignment succeeded' + logical2 = set1 + if (all(logical2)) then + write (*, *) 'Initialization of logical(int32) succeeded.' + end if +end program example_assignment diff --git a/example/bitsets/example_bitsets_bit_count.f90 b/example/bitsets/example_bitsets_bit_count.f90 new file mode 100644 index 000000000..1cbd15f1e --- /dev/null +++ b/example/bitsets/example_bitsets_bit_count.f90 @@ -0,0 +1,15 @@ +program example_bit_count + use stdlib_bitsets + character(*), parameter :: & + bits_0 = '0000000000000000000' + type(bitset_64) :: set0 + call set0%from_string(bits_0) + if (set0%bit_count() == 0) then + write (*, *) "FROM_STRING interpreted "// & + "BITS_0's value properly." + end if + call set0%set(5) + if (set0%bit_count() == 1) then + write (*, *) "BIT_COUNT interpreted SET0's value properly." + end if +end program example_bit_count diff --git a/example/bitsets/example_bitsets_bits.f90 b/example/bitsets/example_bitsets_bits.f90 new file mode 100644 index 000000000..b177da1e9 --- /dev/null +++ b/example/bitsets/example_bitsets_bits.f90 @@ -0,0 +1,11 @@ +program example_bits + use stdlib_bitsets + character(*), parameter :: & + bits_0 = '0000000000000000000' + type(bitset_64) :: set0 + call set0%from_string(bits_0) + if (set0%bits() == 19) then + write (*, *) "FROM_STRING interpreted "// & + "BITS_0's size properly." + end if +end program example_bits diff --git a/example/bitsets/example_bitsets_clear.f90 b/example/bitsets/example_bitsets_clear.f90 new file mode 100644 index 000000000..d184d147d --- /dev/null +++ b/example/bitsets/example_bitsets_clear.f90 @@ -0,0 +1,11 @@ +program example_clear + use stdlib_bitsets + type(bitset_large) :: set0 + call set0%init(166) + call set0%not() + if (set0%all()) write (*, *) 'SET0 is properly initialized.' + call set0%clear(165) + if (.not. set0%test(165)) write (*, *) 'Bit 165 is cleared.' + call set0%clear(0, 164) + if (set0%none()) write (*, *) 'All bits are cleared.' +end program example_clear diff --git a/example/bitsets/example_bitsets_equality.f90 b/example/bitsets/example_bitsets_equality.f90 new file mode 100644 index 000000000..59bc8d3f6 --- /dev/null +++ b/example/bitsets/example_bitsets_equality.f90 @@ -0,0 +1,16 @@ +program example_equality + use stdlib_bitsets + type(bitset_64) :: set0, set1, set2 + call set0%init(33) + call set1%init(33) + call set2%init(33) + call set1%set(0) + call set2%set(32) + if (set0 == set0 .and. set1 == set1 .and. set2 == set2 .and. & + .not. set0 == set1 .and. .not. set0 == set2 .and. .not. & + set1 == set2) then + write (*, *) 'Passed 64 bit equality tests.' + else + error stop 'Failed 64 bit equality tests.' + end if +end program example_equality diff --git a/example/bitsets/example_bitsets_extract.f90 b/example/bitsets/example_bitsets_extract.f90 new file mode 100644 index 000000000..c4795422c --- /dev/null +++ b/example/bitsets/example_bitsets_extract.f90 @@ -0,0 +1,10 @@ +program example_extract + use stdlib_bitsets + type(bitset_large) :: set0, set1 + call set0%init(166) + call set0%set(100, 150) + call extract(set1, set0, 100, 150) + if (set1%bits() == 51) & + write (*, *) 'SET1 has the proper size.' + if (set1%all()) write (*, *) 'SET1 has the proper values.' +end program example_extract diff --git a/example/bitsets/example_bitsets_flip.f90 b/example/bitsets/example_bitsets_flip.f90 new file mode 100644 index 000000000..449f758c4 --- /dev/null +++ b/example/bitsets/example_bitsets_flip.f90 @@ -0,0 +1,10 @@ +program example_flip + use stdlib_bitsets + type(bitset_large) :: set0 + call set0%init(166) + if (set0%none()) write (*, *) 'SET0 is properly initialized.' + call set0%flip(165) + if (set0%test(165)) write (*, *) 'Bit 165 is flipped.' + call set0%flip(0, 164) + if (set0%all()) write (*, *) 'All bits are flipped.' +end program example_flip diff --git a/example/bitsets/example_bitsets_from_string.f90 b/example/bitsets/example_bitsets_from_string.f90 new file mode 100644 index 000000000..ab85d853a --- /dev/null +++ b/example/bitsets/example_bitsets_from_string.f90 @@ -0,0 +1,17 @@ +program example_from_string + use stdlib_bitsets + character(*), parameter :: & + bits_all = '111111111111111111111111111111111' + type(bitset_64) :: set0 + call set0%from_string(bits_all) + if (bits(set0) /= 33) then + error stop "FROM_STRING failed to interpret "// & + "BITS_ALL's size properly." + else if (.not. set0%all()) then + error stop "FROM_STRING failed to interpret"// & + "BITS_ALL's value properly." + else + write (*, *) "FROM_STRING transferred BITS_ALL properly"// & + " into set0." + end if +end program example_from_string diff --git a/example/bitsets/example_bitsets_ge.f90 b/example/bitsets/example_bitsets_ge.f90 new file mode 100644 index 000000000..0a905714d --- /dev/null +++ b/example/bitsets/example_bitsets_ge.f90 @@ -0,0 +1,17 @@ +program example_ge + use stdlib_bitsets + type(bitset_64) :: set0, set1, set2 + call set0%init(33) + call set1%init(33) + call set2%init(33) + call set1%set(0) + call set2%set(32) + if (set1 >= set0 .and. set2 >= set1 .and. set2 >= set0 .and. & + set0 >= set0 .and. set1 >= set1 .and. set2 >= set2 .and. & + .not. set0 >= set1 .and. .not. set0 >= set2 .and. .not. & + set1 >= set2) then + write (*, *) 'Passed 64 bit greater than or equals tests.' + else + error stop 'Failed 64 bit greater than or equals tests.' + end if +end program example_ge diff --git a/example/bitsets/example_bitsets_gt.f90 b/example/bitsets/example_bitsets_gt.f90 new file mode 100644 index 000000000..bc45228f7 --- /dev/null +++ b/example/bitsets/example_bitsets_gt.f90 @@ -0,0 +1,16 @@ +program example_gt + use stdlib_bitsets + type(bitset_64) :: set0, set1, set2 + call set0%init(33) + call set1%init(33) + call set2%init(33) + call set1%set(0) + call set2%set(32) + if (set1 > set0 .and. set2 > set1 .and. set2 > set0 .and. & + .not. set0 > set0 .and. .not. set0 > set1 .and. .not. & + set1 > set2) then + write (*, *) 'Passed 64 bit greater than tests.' + else + error stop 'Failed 64 bit greater than tests.' + end if +end program example_gt diff --git a/example/bitsets/example_bitsets_inequality.f90 b/example/bitsets/example_bitsets_inequality.f90 new file mode 100644 index 000000000..cd8815071 --- /dev/null +++ b/example/bitsets/example_bitsets_inequality.f90 @@ -0,0 +1,16 @@ +program example_inequality + use stdlib_bitsets + type(bitset_64) :: set0, set1, set2 + call set0%init(33) + call set1%init(33) + call set2%init(33) + call set1%set(0) + call set2%set(32) + if (set0 /= set1 .and. set0 /= set2 .and. set1 /= set2 .and. & + .not. set0 /= set0 .and. .not. set1 /= set1 .and. .not. & + set2 /= set2) then + write (*, *) 'Passed 64 bit inequality tests.' + else + error stop 'Failed 64 bit inequality tests.' + end if +end program example_inequality diff --git a/example/bitsets/example_bitsets_init.f90 b/example/bitsets/example_bitsets_init.f90 new file mode 100644 index 000000000..b900b9bec --- /dev/null +++ b/example/bitsets/example_bitsets_init.f90 @@ -0,0 +1,8 @@ +program example_init + use stdlib_bitsets + type(bitset_large) :: set0 + call set0%init(166) + if (set0%bits() == 166) & + write (*, *) 'SET0 has the proper size.' + if (set0%none()) write (*, *) 'SET0 is properly initialized.' +end program example_init diff --git a/example/bitsets/example_bitsets_input.f90 b/example/bitsets/example_bitsets_input.f90 new file mode 100644 index 000000000..12600956d --- /dev/null +++ b/example/bitsets/example_bitsets_input.f90 @@ -0,0 +1,32 @@ +program example_input + use stdlib_bitsets + implicit none + character(*), parameter :: & + bits_0 = '000000000000000000000000000000000', & + bits_1 = '000000000000000000000000000000001', & + bits_33 = '100000000000000000000000000000000' + integer :: unit + type(bitset_64) :: set0, set1, set2, set3, set4, set5 + call set0%from_string(bits_0) + call set1%from_string(bits_1) + call set2%from_string(bits_33) + open (newunit=unit, file='test.bin', status='replace', & + form='unformatted', action='write') + call set2%output(unit) + call set1%output(unit) + call set0%output(unit) + close (unit) + open (newunit=unit, file='test.bin', status='old', & + form='unformatted', action='read') + call set5%input(unit) + call set4%input(unit) + call set3%input(unit) + close (unit) + if (set3 /= set0 .or. set4 /= set1 .or. set5 /= set2) then + error stop 'Transfer to and from units using '// & + ' output and input failed.' + else + write (*, *) 'Transfer to and from units using '// & + 'output and input succeeded.' + end if +end program example_input diff --git a/example/bitsets/example_bitsets_le.f90 b/example/bitsets/example_bitsets_le.f90 new file mode 100644 index 000000000..e17f0d95e --- /dev/null +++ b/example/bitsets/example_bitsets_le.f90 @@ -0,0 +1,17 @@ +program example_le + use stdlib_bitsets + type(bitset_64) :: set0, set1, set2 + call set0%init(33) + call set1%init(33) + call set2%init(33) + call set1%set(0) + call set2%set(32) + if (set0 <= set1 .and. set1 <= set2 .and. set0 <= set2 .and. & + set0 <= set0 .and. set1 <= set1 .and. set2 <= set2 .and. & + .not. set1 <= set0 .and. .not. set2 <= set0 .and. .not. & + set2 <= set1) then + write (*, *) 'Passed 64 bit less than or equal tests.' + else + error stop 'Failed 64 bit less than or equal tests.' + end if +end program example_le diff --git a/example/bitsets/example_bitsets_lt.f90 b/example/bitsets/example_bitsets_lt.f90 new file mode 100644 index 000000000..5c2b44740 --- /dev/null +++ b/example/bitsets/example_bitsets_lt.f90 @@ -0,0 +1,16 @@ +program example_lt + use stdlib_bitsets + type(bitset_64) :: set0, set1, set2 + call set0%init(33) + call set1%init(33) + call set2%init(33) + call set1%set(0) + call set2%set(32) + if (set0 < set1 .and. set1 < set2 .and. set0 < set2 .and. & + .not. set0 < set0 .and. .not. set2 < set0 .and. .not. & + set2 < set1) then + write (*, *) 'Passed 64 bit less than tests.' + else + error stop 'Failed 64 bit less than tests.' + end if +end program example_lt diff --git a/example/bitsets/example_bitsets_none.f90 b/example/bitsets/example_bitsets_none.f90 new file mode 100644 index 000000000..f00ea8a49 --- /dev/null +++ b/example/bitsets/example_bitsets_none.f90 @@ -0,0 +1,15 @@ +program example_none + use stdlib_bitsets + character(*), parameter :: & + bits_0 = '0000000000000000000' + type(bitset_large) :: set0 + call set0%from_string(bits_0) + if (set0%none()) then + write (*, *) "FROM_STRING interpreted "// & + "BITS_0's value properly." + end if + call set0%set(5) + if (.not. set0%none()) then + write (*, *) "NONE interpreted SET0's value properly." + end if +end program example_none diff --git a/example/bitsets/example_bitsets_not.f90 b/example/bitsets/example_bitsets_not.f90 new file mode 100644 index 000000000..78ebc33b6 --- /dev/null +++ b/example/bitsets/example_bitsets_not.f90 @@ -0,0 +1,13 @@ +program example_not + use stdlib_bitsets + type(bitset_large) :: set0 + call set0%init(155) + if (set0%none()) then + write (*, *) "FROM_STRING interpreted "// & + "BITS_0's value properly." + end if + call set0%not() + if (set0%all()) then + write (*, *) "ALL interpreted SET0's value properly." + end if +end program example_not diff --git a/example/bitsets/example_bitsets_or.f90 b/example/bitsets/example_bitsets_or.f90 new file mode 100644 index 000000000..ddaface21 --- /dev/null +++ b/example/bitsets/example_bitsets_or.f90 @@ -0,0 +1,18 @@ +program example_or + use stdlib_bitsets + type(bitset_large) :: set0, set1 + call set0%init(166) + call set1%init(166) + call or(set0, set1) ! none none + if (set0%none()) write (*, *) 'First test of OR worked.' + call set0%not() + call or(set0, set1) ! all none + if (set0%all()) write (*, *) 'Second test of OR worked.' + call set0%not() + call set1%not() + call or(set0, set1) ! none all + if (set0%all()) write (*, *) 'Third test of OR worked.' + call set0%not() + call or(set0, set1) ! all all + if (set0%all()) write (*, *) 'Fourth test of OR worked.' +end program example_or diff --git a/example/bitsets/example_bitsets_output.f90 b/example/bitsets/example_bitsets_output.f90 new file mode 100644 index 000000000..b10dba27b --- /dev/null +++ b/example/bitsets/example_bitsets_output.f90 @@ -0,0 +1,32 @@ +program example_output + use stdlib_bitsets + implicit none + character(*), parameter :: & + bits_0 = '000000000000000000000000000000000', & + bits_1 = '000000000000000000000000000000001', & + bits_33 = '100000000000000000000000000000000' + integer :: unit + type(bitset_64) :: set0, set1, set2, set3, set4, set5 + call set0%from_string(bits_0) + call set1%from_string(bits_1) + call set2%from_string(bits_33) + open (newunit=unit, file='test.bin', status='replace', & + form='unformatted', action='write') + call set2%output(unit) + call set1%output(unit) + call set0%output(unit) + close (unit) + open (newunit=unit, file='test.bin', status='old', & + form='unformatted', action='read') + call set5%input(unit) + call set4%input(unit) + call set3%input(unit) + close (unit) + if (set3 /= set0 .or. set4 /= set1 .or. set5 /= set2) then + error stop 'Transfer to and from units using '// & + ' output and input failed.' + else + write (*, *) 'Transfer to and from units using '// & + 'output and input succeeded.' + end if +end program example_output diff --git a/example/bitsets/example_bitsets_read_bitset.f90 b/example/bitsets/example_bitsets_read_bitset.f90 new file mode 100644 index 000000000..54a3e09d3 --- /dev/null +++ b/example/bitsets/example_bitsets_read_bitset.f90 @@ -0,0 +1,35 @@ +program example_read_bitset + use stdlib_bitsets + implicit none + character(*), parameter :: & + bits_0 = 'S33B000000000000000000000000000000000', & + bits_1 = 'S33B000000000000000000000000000000001', & + bits_2 = 'S33B100000000000000000000000000000000' + character(:), allocatable :: test_0, test_1, test_2 + integer :: unit, status + type(bitset_64) :: set0, set1, set2, set3, set4, set5 + call set0%read_bitset(bits_0, status) + call set1%read_bitset(bits_1, status) + call set2%read_bitset(bits_2, status) + call set0%write_bitset(test_0, status) + call set1%write_bitset(test_1, status) + call set2%write_bitset(test_2, status) + if (bits_0 == test_0 .and. bits_1 == test_1 .and. & + bits_2 == test_2) then + write (*, *) 'READ_BITSET to WRITE_BITSET strings worked.' + end if + open (newunit=unit, file='test.txt', status='replace', & + form='formatted', action='write') + call set2%write_bitset(unit, advance='no') + call set1%write_bitset(unit, advance='no') + call set0%write_bitset(unit) + close (unit) + open (newunit=unit, file='test.txt', status='old', & + form='formatted', action='read') + call set3%read_bitset(unit, advance='no') + call set4%read_bitset(unit, advance='no') + call set5%read_bitset(unit) + if (set3 == set0 .and. set4 == set1 .and. set5 == set2) then + write (*, *) 'WRITE_BITSET to READ_BITSET through unit worked.' + end if +end program example_read_bitset diff --git a/example/bitsets/example_bitsets_set.f90 b/example/bitsets/example_bitsets_set.f90 new file mode 100644 index 000000000..5409e5c17 --- /dev/null +++ b/example/bitsets/example_bitsets_set.f90 @@ -0,0 +1,10 @@ +program example_set + use stdlib_bitsets + type(bitset_large) :: set0 + call set0%init(166) + if (set0%none()) write (*, *) 'SET0 is properly initialized.' + call set0%set(165) + if (set0%test(165)) write (*, *) 'Bit 165 is set.' + call set0%set(0, 164) + if (set0%all()) write (*, *) 'All bits are set.' +end program example_set diff --git a/example/bitsets/example_bitsets_test.f90 b/example/bitsets/example_bitsets_test.f90 new file mode 100644 index 000000000..2f5faa41c --- /dev/null +++ b/example/bitsets/example_bitsets_test.f90 @@ -0,0 +1,11 @@ +program example_test + use stdlib_bitsets + type(bitset_large) :: set0 + call set0%init(166) + call set0%not() + if (set0%all()) write (*, *) 'SET0 is properly initialized.' + call set0%clear(165) + if (.not. set0%test(165)) write (*, *) 'Bit 165 is cleared.' + call set0%set(165) + if (set0%test(165)) write (*, *) 'Bit 165 is set.' +end program example_test diff --git a/example/bitsets/example_bitsets_to_string.f90 b/example/bitsets/example_bitsets_to_string.f90 new file mode 100644 index 000000000..2bf151f83 --- /dev/null +++ b/example/bitsets/example_bitsets_to_string.f90 @@ -0,0 +1,14 @@ +program example_to_string + use stdlib_bitsets + character(*), parameter :: & + bits_all = '111111111111111111111111111111111' + type(bitset_64) :: set0 + character(:), allocatable :: new_string + call set0%init(33) + call set0%not() + call set0%to_string(new_string) + if (new_string == bits_all) then + write (*, *) "TO_STRING transferred BITS0 properly"// & + " into NEW_STRING." + end if +end program example_to_string diff --git a/example/bitsets/example_bitsets_value.f90 b/example/bitsets/example_bitsets_value.f90 new file mode 100644 index 000000000..9c8f84f6e --- /dev/null +++ b/example/bitsets/example_bitsets_value.f90 @@ -0,0 +1,11 @@ +program example_value + use stdlib_bitsets + type(bitset_large) :: set0 + call set0%init(166) + call set0%not() + if (set0%all()) write (*, *) 'SET0 is properly initialized.' + call set0%clear(165) + if (set0%value(165) == 0) write (*, *) 'Bit 165 is cleared.' + call set0%set(165) + if (set0%value(165) == 1) write (*, *) 'Bit 165 is set.' +end program example_value diff --git a/example/bitsets/example_bitsets_write_bitset.f90 b/example/bitsets/example_bitsets_write_bitset.f90 new file mode 100644 index 000000000..e56b4c58a --- /dev/null +++ b/example/bitsets/example_bitsets_write_bitset.f90 @@ -0,0 +1,35 @@ +program example_write_bitset + use stdlib_bitsets + implicit none + character(*), parameter :: & + bits_0 = 'S33B000000000000000000000000000000000', & + bits_1 = 'S33B000000000000000000000000000000001', & + bits_2 = 'S33B100000000000000000000000000000000' + character(:), allocatable :: test_0, test_1, test_2 + integer :: unit, status + type(bitset_64) :: set0, set1, set2, set3, set4, set5 + call set0%read_bitset(bits_0, status) + call set1%read_bitset(bits_1, status) + call set2%read_bitset(bits_2, status) + call set0%write_bitset(test_0, status) + call set1%write_bitset(test_1, status) + call set2%write_bitset(test_2, status) + if (bits_0 == test_0 .and. bits_1 == test_1 .and. & + bits_2 == test_2) then + write (*, *) 'READ_BITSET to WRITE_BITSET strings worked.' + end if + open (newunit=unit, file='test.txt', status='replace', & + form='formatted', action='write') + call set2%write_bitset(unit, advance='no') + call set1%write_bitset(unit, advance='no') + call set0%write_bitset(unit) + close (unit) + open (newunit=unit, file='test.txt', status='old', & + form='formatted', action='read') + call set3%read_bitset(unit, advance='no') + call set4%read_bitset(unit, advance='no') + call set5%read_bitset(unit) + if (set3 == set0 .and. set4 == set1 .and. set5 == set2) then + write (*, *) 'WRITE_BITSET to READ_BITSET through unit worked.' + end if +end program example_write_bitset diff --git a/example/bitsets/example_bitsets_xor.f90 b/example/bitsets/example_bitsets_xor.f90 new file mode 100644 index 000000000..886b16e48 --- /dev/null +++ b/example/bitsets/example_bitsets_xor.f90 @@ -0,0 +1,18 @@ +program example_xor + use stdlib_bitsets + type(bitset_large) :: set0, set1 + call set0%init(166) + call set1%init(166) + call xor(set0, set1) ! none none + if (set0%none()) write (*, *) 'First test of XOR worked.' + call set0%not() + call xor(set0, set1) ! all none + if (set0%all()) write (*, *) 'Second test of XOR worked.' + call set0%not() + call set1%not() + call xor(set0, set1) ! none all + if (set0%all()) write (*, *) 'Third test of XOR worked.' + call set0%not() + call xor(set0, set1) ! all all + if (set0%none()) write (*, *) 'Fourth test of XOR worked.' +end program example_xor diff --git a/example/error/CMakeLists.txt b/example/error/CMakeLists.txt new file mode 100644 index 000000000..c4830efb9 --- /dev/null +++ b/example/error/CMakeLists.txt @@ -0,0 +1,11 @@ +ADD_EXAMPLE(check1) +set_tests_properties(check1 PROPERTIES WILL_FAIL true) +ADD_EXAMPLE(check2) +set_tests_properties(check2 PROPERTIES WILL_FAIL true) +ADD_EXAMPLE(check3) +ADD_EXAMPLE(check4) +set_tests_properties(check4 PROPERTIES SKIP_RETURN_CODE 77) +ADD_EXAMPLE(error_stop1) +set_tests_properties(error_stop1 PROPERTIES WILL_FAIL true) +ADD_EXAMPLE(error_stop2) +set_tests_properties(error_stop2 PROPERTIES WILL_FAIL true) diff --git a/example/error/example_check1.f90 b/example/error/example_check1.f90 new file mode 100644 index 000000000..4c5c3d0a0 --- /dev/null +++ b/example/error/example_check1.f90 @@ -0,0 +1,7 @@ +program example_check1 + use stdlib_error, only: check + implicit none + integer :: a = 1 +! If a /= 5, stops the program with exit code 1 and prints 'Check failed.' + call check(a == 5) +end program example_check1 diff --git a/example/error/example_check2.f90 b/example/error/example_check2.f90 new file mode 100644 index 000000000..c505e4450 --- /dev/null +++ b/example/error/example_check2.f90 @@ -0,0 +1,7 @@ +program example_check2 + use stdlib_error, only: check + implicit none + integer :: a = 1 +! If a /= 5, stops the program with exit code 1 and prints 'a == 5 failed.' + call check(a == 5, msg='a == 5 failed.') +end program example_check2 diff --git a/example/error/example_check3.f90 b/example/error/example_check3.f90 new file mode 100644 index 000000000..97e9b5b91 --- /dev/null +++ b/example/error/example_check3.f90 @@ -0,0 +1,7 @@ +program example_check3 + use stdlib_error, only: check + implicit none + integer :: a = 1 +! If a /= 5, prints 'a == 5 failed.', but doesn't stop the program. + call check(a == 5, msg='a == 5 failed.', warn=.true.) +end program example_check3 diff --git a/example/error/example_check4.f90 b/example/error/example_check4.f90 new file mode 100644 index 000000000..da1c15531 --- /dev/null +++ b/example/error/example_check4.f90 @@ -0,0 +1,7 @@ +program example_check4 + use stdlib_error, only: check + implicit none + integer :: a = 1 +! If a /= 5, stops the program with exit code 77 and prints 'a == 5 failed.' + call check(a == 5, msg='a == 5 failed.', code=77) +end program example_check4 diff --git a/example/error/example_error_stop1.f90 b/example/error/example_error_stop1.f90 new file mode 100644 index 000000000..29ee87a52 --- /dev/null +++ b/example/error/example_error_stop1.f90 @@ -0,0 +1,5 @@ +program example_error_stop1 + use stdlib_error, only: error_stop + implicit none + call error_stop("Invalid argument") +end program example_error_stop1 diff --git a/example/error/example_error_stop2.f90 b/example/error/example_error_stop2.f90 new file mode 100644 index 000000000..09107c512 --- /dev/null +++ b/example/error/example_error_stop2.f90 @@ -0,0 +1,5 @@ +program example_error_stop2 + use stdlib_error, only: error_stop + implicit none + call error_stop("Invalid argument", code=123) +end program example_error_stop2 diff --git a/example/hash_procedures/CMakeLists.txt b/example/hash_procedures/CMakeLists.txt new file mode 100644 index 000000000..7258747dd --- /dev/null +++ b/example/hash_procedures/CMakeLists.txt @@ -0,0 +1,13 @@ +ADD_EXAMPLE(fibonacci_hash_64) +ADD_EXAMPLE(fibonacci_hash) +ADD_EXAMPLE(fnv_1a_hash_64) +ADD_EXAMPLE(fnv_1a_hash) +ADD_EXAMPLE(fnv_1_hash_64) +ADD_EXAMPLE(fnv_1_hash) +ADD_EXAMPLE(nmhash32) +ADD_EXAMPLE(nmhash32x) +ADD_EXAMPLE(pengy_hash) +ADD_EXAMPLE(spooky_hash) +ADD_EXAMPLE(universal_mult_hash_64) +ADD_EXAMPLE(universal_mult_hash) +ADD_EXAMPLE(water_hash) diff --git a/example/hash_procedures/example_fibonacci_hash.f90 b/example/hash_procedures/example_fibonacci_hash.f90 new file mode 100644 index 000000000..35fe47caf --- /dev/null +++ b/example/hash_procedures/example_fibonacci_hash.f90 @@ -0,0 +1,13 @@ +program example_fibonacci_hash + use stdlib_hash_32bit, only: fibonacci_hash + use iso_fortran_env, only: int32 + implicit none + integer, allocatable :: array1(:) + integer(int32) :: hash, source + allocate (array1(0:2**6 - 1)) + array1(:) = 0 + source = 42_int32 + hash = fibonacci_hash(source, 6) + array1(hash) = source + print *, hash +end program example_fibonacci_hash diff --git a/example/hash_procedures/example_fibonacci_hash_64.f90 b/example/hash_procedures/example_fibonacci_hash_64.f90 new file mode 100644 index 000000000..45b6e1031 --- /dev/null +++ b/example/hash_procedures/example_fibonacci_hash_64.f90 @@ -0,0 +1,13 @@ +program example_fibonacci_hash_64 + use stdlib_hash_64bit, only: fibonacci_hash + use iso_fortran_env, only: int64 + implicit none + integer, allocatable :: array1(:) + integer(int64) :: hash, source + allocate (array1(0:2**6 - 1)) + array1(:) = 0 + source = int(Z'1FFFFFFFF', int64) + hash = fibonacci_hash(source, 6) + array1(hash) = source + print *, hash +end program example_fibonacci_hash_64 diff --git a/example/hash_procedures/example_fnv_1_hash.f90 b/example/hash_procedures/example_fnv_1_hash.f90 new file mode 100644 index 000000000..adf97c011 --- /dev/null +++ b/example/hash_procedures/example_fnv_1_hash.f90 @@ -0,0 +1,8 @@ +program example_fnv_1_hash + use stdlib_hash_32bit, only: fnv_1_hash + use iso_fortran_env, only: int32 + implicit none + integer(int32) :: hash + hash = fnv_1_hash([5, 4, 3, 1, 10, 4, 9]) + print *, hash +end program example_fnv_1_hash diff --git a/example/hash_procedures/example_fnv_1_hash_64.f90 b/example/hash_procedures/example_fnv_1_hash_64.f90 new file mode 100644 index 000000000..e3e30eea3 --- /dev/null +++ b/example/hash_procedures/example_fnv_1_hash_64.f90 @@ -0,0 +1,10 @@ +program example_fnv_1_hash_64 + use stdlib_hash_64bit, only: fnv_1_hash + use iso_fortran_env, only: int64 + implicit none + integer, allocatable :: array1(:) + integer(int64) :: hash + array1 = [5, 4, 3, 1, 10, 4, 9] + hash = fnv_1_hash(array1) + print *, hash +end program example_fnv_1_hash_64 diff --git a/example/hash_procedures/example_fnv_1a_hash.f90 b/example/hash_procedures/example_fnv_1a_hash.f90 new file mode 100644 index 000000000..2dbff6ea0 --- /dev/null +++ b/example/hash_procedures/example_fnv_1a_hash.f90 @@ -0,0 +1,8 @@ +program example_fnv_1a_hash + use stdlib_hash_32bit, only: fnv_1a_hash + use iso_fortran_env, only: int32 + implicit none + integer(int32) :: hash + hash = fnv_1a_hash([5, 4, 3, 1, 10, 4, 9]) + print *, hash +end program example_fnv_1a_hash diff --git a/example/hash_procedures/example_fnv_1a_hash_64.f90 b/example/hash_procedures/example_fnv_1a_hash_64.f90 new file mode 100644 index 000000000..d14ed807c --- /dev/null +++ b/example/hash_procedures/example_fnv_1a_hash_64.f90 @@ -0,0 +1,10 @@ +program example_fnv_1a_hash_64 + use stdlib_hash_64bit, only: fnv_1a_hash + use iso_fortran_env, only: int64 + implicit none + integer, allocatable :: array1(:) + integer(int64) :: hash + array1 = [5, 4, 3, 1, 10, 4, 9] + hash = fnv_1a_hash(array1) + print *, hash +end program example_fnv_1a_hash_64 diff --git a/example/hash_procedures/example_nmhash32.f90 b/example/hash_procedures/example_nmhash32.f90 new file mode 100644 index 000000000..a0654d2ac --- /dev/null +++ b/example/hash_procedures/example_nmhash32.f90 @@ -0,0 +1,11 @@ +program example_nmhash32 + use stdlib_hash_32bit, only: nmhash32, & + new_nmhash32_seed + use iso_fortran_env, only: int32 + implicit none + integer(int32) :: hash + integer(int32) :: seed = 42_int32 + call new_nmhash32_seed(seed) + hash = nmhash32([5, 4, 3, 1, 10, 4, 9], seed) + print *, seed, hash +end program example_nmhash32 diff --git a/example/hash_procedures/example_nmhash32x.f90 b/example/hash_procedures/example_nmhash32x.f90 new file mode 100644 index 000000000..34339ef16 --- /dev/null +++ b/example/hash_procedures/example_nmhash32x.f90 @@ -0,0 +1,11 @@ +program example_nmhash32x + use stdlib_hash_32bit, only: nmhash32x, & + new_nmhash32x_seed + use iso_fortran_env, only: int32 + implicit none + integer(int32) :: hash + integer(int32) :: seed = 42_int32 + call new_nmhash32x_seed(seed) + hash = nmhash32x([5, 4, 3, 1, 10, 4, 9], seed) + print *, seed, hash +end program example_nmhash32x diff --git a/example/hash_procedures/example_pengy_hash.f90 b/example/hash_procedures/example_pengy_hash.f90 new file mode 100644 index 000000000..4b8bcf145 --- /dev/null +++ b/example/hash_procedures/example_pengy_hash.f90 @@ -0,0 +1,13 @@ +program example_pengy_hash + use stdlib_hash_64bit, only: new_pengy_hash_seed, pengy_hash + use iso_fortran_env, only: int32, int64 + implicit none + integer, allocatable :: key(:) + integer(int64) :: hash + integer(int32) :: seed + key = [0, 1, 2, 3] + seed = 0_int32 + call new_pengy_hash_seed(seed) + hash = pengy_hash(key, seed) + print *, seed, hash +end program example_pengy_hash diff --git a/example/hash_procedures/example_spooky_hash.f90 b/example/hash_procedures/example_spooky_hash.f90 new file mode 100644 index 000000000..0c80d618f --- /dev/null +++ b/example/hash_procedures/example_spooky_hash.f90 @@ -0,0 +1,13 @@ +program example_spooky_hash + use stdlib_hash_64bit, only: new_spooky_hash_seed, & + spooky_hash + use iso_fortran_env, only: int64 + implicit none + integer, allocatable :: key(:) + integer(int64) :: hash(2), seed(2) + key = [0, 1, 2, 3] + seed = [119_int64, 2_int64**41 - 1] + call new_spooky_hash_seed(seed) + hash = spooky_hash(key, seed) + print *, seed, hash +end program example_spooky_hash diff --git a/example/hash_procedures/example_universal_mult_hash.f90 b/example/hash_procedures/example_universal_mult_hash.f90 new file mode 100644 index 000000000..70071937c --- /dev/null +++ b/example/hash_procedures/example_universal_mult_hash.f90 @@ -0,0 +1,18 @@ +program example_universal_mult_hash + use stdlib_hash_32bit, only: odd_random_integer, & + universal_mult_hash + use iso_fortran_env, only: int32 + implicit none + integer, allocatable :: array1(:) + integer(int32) :: hash, i, seed, source + seed = 0 + allocate (array1(0:2**6 - 1)) + do i = 0, 2**6 - 1 + array1(i) = i + end do + call odd_random_integer(seed) + source = 42_int32 + hash = universal_mult_hash(source, seed, 6) + array1(hash) = source + print *, seed, hash, array1 +end program example_universal_mult_hash diff --git a/example/hash_procedures/example_universal_mult_hash_64.f90 b/example/hash_procedures/example_universal_mult_hash_64.f90 new file mode 100644 index 000000000..ab4644170 --- /dev/null +++ b/example/hash_procedures/example_universal_mult_hash_64.f90 @@ -0,0 +1,16 @@ +program example_universal_mult_hash_64 + use stdlib_hash_64bit, only: odd_random_integer, & + universal_mult_hash + use iso_fortran_env, only: int64 + implicit none + integer, allocatable :: array1(:) + integer(int64) :: hash, seed, source + seed = 0 + allocate (array1(0:2**6 - 1)) + array1 = 0 + call odd_random_integer(seed) + source = 42_int64 + hash = universal_mult_hash(source, seed, 6) + array1(hash) = source + print *, seed, hash, array1 +end program example_universal_mult_hash_64 diff --git a/example/hash_procedures/example_water_hash.f90 b/example/hash_procedures/example_water_hash.f90 new file mode 100644 index 000000000..6b4464c9c --- /dev/null +++ b/example/hash_procedures/example_water_hash.f90 @@ -0,0 +1,11 @@ +program example_water_hash + use stdlib_hash_32bit, only: water_hash, & + new_water_hash_seed + use iso_fortran_env, only: int32, int64 + implicit none + integer(int32) :: hash + integer(int64) :: seed = 42_int64 + call new_water_hash_seed(seed) + hash = water_hash([5, 4, 3, 1, 10, 4, 9], seed) + print *, hash, seed +end program example_water_hash diff --git a/example/hashmaps/CMakeLists.txt b/example/hashmaps/CMakeLists.txt new file mode 100644 index 000000000..c3962fcfb --- /dev/null +++ b/example/hashmaps/CMakeLists.txt @@ -0,0 +1,27 @@ +ADD_EXAMPLE(hashmaps_calls) +ADD_EXAMPLE(hashmaps_copy_key) +ADD_EXAMPLE(hashmaps_copy_other) +ADD_EXAMPLE(hashmaps_entries) +ADD_EXAMPLE(hashmaps_equal_keys) +ADD_EXAMPLE(hashmaps_fnv_1a_hasher) +ADD_EXAMPLE(hashmaps_fnv_1_hasher) +ADD_EXAMPLE(hashmaps_free_key) +ADD_EXAMPLE(hashmaps_free_other) +ADD_EXAMPLE(hashmaps_get) +ADD_EXAMPLE(hashmaps_get_other_data) +ADD_EXAMPLE(hashmaps_hasher_fun) +ADD_EXAMPLE(hashmaps_init) +ADD_EXAMPLE(hashmaps_key_test) +ADD_EXAMPLE(hashmaps_loading) +ADD_EXAMPLE(hashmaps_map_entry) +ADD_EXAMPLE(hashmaps_num_slots) +ADD_EXAMPLE(hashmaps_probes) +ADD_EXAMPLE(hashmaps_rehash) +ADD_EXAMPLE(hashmaps_remove) +ADD_EXAMPLE(hashmaps_seeded_nmhash32_hasher) +ADD_EXAMPLE(hashmaps_seeded_nmhash32x_hasher) +ADD_EXAMPLE(hashmaps_seeded_water_hasher) +ADD_EXAMPLE(hashmaps_set) +ADD_EXAMPLE(hashmaps_set_other_data) +ADD_EXAMPLE(hashmaps_slots_bits) +ADD_EXAMPLE(hashmaps_total_depth) diff --git a/example/hashmaps/example_hashmaps_calls.f90 b/example/hashmaps/example_hashmaps_calls.f90 new file mode 100644 index 000000000..2e8306675 --- /dev/null +++ b/example/hashmaps/example_hashmaps_calls.f90 @@ -0,0 +1,10 @@ +program example_calls + use stdlib_hashmaps, only: chaining_hashmap_type, int_calls + use stdlib_hashmap_wrappers, only: fnv_1_hasher + implicit none + type(chaining_hashmap_type) :: map + integer(int_calls) :: initial_calls + call map%init(fnv_1_hasher) + initial_calls = map%calls() + print *, "INITIAL_CALLS = ", initial_calls +end program example_calls diff --git a/example/hashmaps/example_hashmaps_copy_key.f90 b/example/hashmaps/example_hashmaps_copy_key.f90 new file mode 100644 index 000000000..615796ec1 --- /dev/null +++ b/example/hashmaps/example_hashmaps_copy_key.f90 @@ -0,0 +1,12 @@ +program example_copy_key + use stdlib_hashmap_wrappers, only: & + copy_key, operator(==), key_type, set + use iso_fortran_env, only: int8 + implicit none + integer(int8) :: i, value(15) + type(key_type) :: old_key, new_key + value = [(i, i=1, 15)] + call set(old_key, value) + call copy_key(old_key, new_key) + print *, "old_key == new_key = ", old_key == new_key +end program example_copy_key diff --git a/example/hashmaps/example_hashmaps_copy_other.f90 b/example/hashmaps/example_hashmaps_copy_other.f90 new file mode 100644 index 000000000..a9d9c03e1 --- /dev/null +++ b/example/hashmaps/example_hashmaps_copy_other.f90 @@ -0,0 +1,22 @@ +program example_copy_other + use stdlib_hashmap_wrappers, only: & + copy_other, other_type + use iso_fortran_env, only: int8 + implicit none + type(other_type) :: other_in, other_out + integer(int8) :: i + type dummy_type + integer(int8) :: value(15) + end type + type(dummy_type) :: dummy_val + do i = 1, 15 + dummy_val%value(i) = i + end do + allocate (other_in%value, source=dummy_val) + call copy_other(other_in, other_out) + select type (out => other_out%value) + type is (dummy_type) + print *, "other_in == other_out = ", & + all(dummy_val%value == out%value) + end select +end program example_copy_other diff --git a/example/hashmaps/example_hashmaps_entries.f90 b/example/hashmaps/example_hashmaps_entries.f90 new file mode 100644 index 000000000..2e5e5ea4e --- /dev/null +++ b/example/hashmaps/example_hashmaps_entries.f90 @@ -0,0 +1,10 @@ +program example_entries + use stdlib_hashmaps, only: open_hashmap_type, int_index + use stdlib_hashmap_wrappers, only: fnv_1_hasher + implicit none + type(open_hashmap_type) :: map + integer(int_index) :: initial_entries + call map%init(fnv_1_hasher) + initial_entries = map%entries() + print *, "INITIAL_ENTRIES = ", initial_entries +end program example_entries diff --git a/example/hashmaps/example_hashmaps_equal_keys.f90 b/example/hashmaps/example_hashmaps_equal_keys.f90 new file mode 100644 index 000000000..dbf66334d --- /dev/null +++ b/example/hashmaps/example_hashmaps_equal_keys.f90 @@ -0,0 +1,14 @@ +program example_equal_keys + use stdlib_hashmap_wrappers, only: & + copy_key, operator(==), key_type, set + use iso_fortran_env, only: int8 + implicit none + integer(int8) :: i, value(15) + type(key_type) :: old_key, new_key + do i = 1, 15 + value(i) = i + end do + call set(old_key, value) + call copy_key(old_key, new_key) + print *, "old_key == new_key = ", old_key == new_key +end program example_equal_keys diff --git a/example/hashmaps/example_hashmaps_fnv_1_hasher.f90 b/example/hashmaps/example_hashmaps_fnv_1_hasher.f90 new file mode 100644 index 000000000..f8b052125 --- /dev/null +++ b/example/hashmaps/example_hashmaps_fnv_1_hasher.f90 @@ -0,0 +1,12 @@ +program example_fnv_1_hasher + use stdlib_hashmap_wrappers, only: fnv_1_hasher, key_type, set + use iso_fortran_env, only: int8, int32 + implicit none + integer(int8), allocatable :: array1(:) + integer(int32) :: hash + type(key_type) :: key + array1 = [5_int8, 4_int8, 3_int8, 1_int8, 10_int8, 4_int8] + call set(key, array1) + hash = fnv_1_hasher(key) + print *, hash +end program example_fnv_1_hasher diff --git a/example/hashmaps/example_hashmaps_fnv_1a_hasher.f90 b/example/hashmaps/example_hashmaps_fnv_1a_hasher.f90 new file mode 100644 index 000000000..b843f89fe --- /dev/null +++ b/example/hashmaps/example_hashmaps_fnv_1a_hasher.f90 @@ -0,0 +1,13 @@ +program example_fnv_1a_hasher + use stdlib_hashmap_wrappers, only: & + fnv_1a_hasher, key_type, set + use iso_fortran_env, only: int8, int32 + implicit none + integer(int8), allocatable :: array1(:) + integer(int32) :: hash + type(key_type) :: key + array1 = [5_int8, 4_int8, 3_int8, 1_int8, 10_int8, 4_int8] + call set(key, array1) + hash = fnv_1a_hasher(key) + print *, hash +end program example_fnv_1a_hasher diff --git a/example/hashmaps/example_hashmaps_free_key.f90 b/example/hashmaps/example_hashmaps_free_key.f90 new file mode 100644 index 000000000..70039a425 --- /dev/null +++ b/example/hashmaps/example_hashmaps_free_key.f90 @@ -0,0 +1,12 @@ +program example_free_key + use stdlib_hashmap_wrappers, only: & + copy_key, free_key, key_type, set + use iso_fortran_env, only: int8 + implicit none + integer(int8) :: i, value(15) + type(key_type) :: old_key, new_key + value = [(i, i=1, 15)] + call set(old_key, value) + call copy_key(old_key, new_key) + call free_key(old_key) +end program example_free_key diff --git a/example/hashmaps/example_hashmaps_free_other.f90 b/example/hashmaps/example_hashmaps_free_other.f90 new file mode 100644 index 000000000..6f4a0f292 --- /dev/null +++ b/example/hashmaps/example_hashmaps_free_other.f90 @@ -0,0 +1,18 @@ +program example_free_other + use stdlib_hashmap_wrappers, only: & + copy_other, free_other, other_type + use iso_fortran_env, only: int8 + implicit none + type dummy_type + integer(int8) :: value(15) + end type dummy_type + type(dummy_type) :: dummy_val + type(other_type) :: other_in, other_out + integer(int8) :: i + do i = 1, 15 + dummy_val%value(i) = i + end do + allocate (other_in%value, source=dummy_val) + call copy_other(other_in, other_out) + call free_other(other_out) +end program example_free_other diff --git a/example/hashmaps/example_hashmaps_get.f90 b/example/hashmaps/example_hashmaps_get.f90 new file mode 100644 index 000000000..ed78e1a03 --- /dev/null +++ b/example/hashmaps/example_hashmaps_get.f90 @@ -0,0 +1,16 @@ +program example_get + use stdlib_hashmap_wrappers, only: & + get, key_type, set + use iso_fortran_env, only: int8 + implicit none + integer(int8), allocatable :: value(:), result(:) + type(key_type) :: key + integer(int8) :: i + allocate (value(1:15)) + do i = 1, 15 + value(i) = i + end do + call set(key, value) + call get(key, result) + print *, 'RESULT == VALUE = ', all(value == result) +end program example_get diff --git a/example/hashmaps/example_hashmaps_get_other_data.f90 b/example/hashmaps/example_hashmaps_get_other_data.f90 new file mode 100644 index 000000000..cedf5e1fe --- /dev/null +++ b/example/hashmaps/example_hashmaps_get_other_data.f90 @@ -0,0 +1,32 @@ +program example_get_other_data + use stdlib_kinds, only: int8 + use stdlib_hashmaps, only: chaining_hashmap_type, int_index + use stdlib_hashmap_wrappers, only: fnv_1_hasher, key_type, other_type, set, get + logical :: conflict + type(key_type) :: key + type(other_type) :: other + type(chaining_hashmap_type) :: map + type dummy_type + integer(int8) :: value(4) + end type dummy_type + type(dummy_type) :: dummy + class(*), allocatable :: data + dummy%value = [4_int8, 3_int8, 2_int8, 1_int8] + allocate (data, source=dummy) + call map%init(fnv_1_hasher) + call set(key, [0_int8, 1_int8, 2_int8, 3_int8, 4_int8]) + call set(other, data) + call map%map_entry(key, other, conflict) + if (.not. conflict) then + call map%get_other_data(key, other) + else + error stop 'Key is already present in the map.' + end if + call get(other, data) + select type (data) + type is (dummy_type) + print *, 'Other data % value = ', data%value + class default + print *, 'Invalid data type in other' + end select +end program example_get_other_data diff --git a/example/hashmaps/example_hashmaps_hasher_fun.f90 b/example/hashmaps/example_hashmaps_hasher_fun.f90 new file mode 100644 index 000000000..0e4c0145e --- /dev/null +++ b/example/hashmaps/example_hashmaps_hasher_fun.f90 @@ -0,0 +1,14 @@ +program example_hasher_fun + use stdlib_hashmap_wrappers, only: fnv_1a_hasher, hasher_fun, set, key_type + use stdlib_kinds, only: int8, int32 + implicit none + procedure(hasher_fun), pointer :: hasher_pointer + integer(int8), allocatable :: array1(:) + integer(int32) :: hash + type(key_type) :: key + hasher_pointer => fnv_1a_hasher + array1 = [5_int8, 4_int8, 3_int8, 1_int8, 10_int8, 4_int8] + call set(key, array1) + hash = hasher_pointer(key) + print *, hash +end program example_hasher_fun diff --git a/example/hashmaps/example_hashmaps_init.f90 b/example/hashmaps/example_hashmaps_init.f90 new file mode 100644 index 000000000..ef2d85b54 --- /dev/null +++ b/example/hashmaps/example_hashmaps_init.f90 @@ -0,0 +1,7 @@ +program example_init + use stdlib_hashmaps, only: chaining_hashmap_type + use stdlib_hashmap_wrappers, only: fnv_1_hasher + implicit none + type(chaining_hashmap_type) :: map + call map%init(fnv_1_hasher, slots_bits=10) +end program example_init diff --git a/example/hashmaps/example_hashmaps_key_test.f90 b/example/hashmaps/example_hashmaps_key_test.f90 new file mode 100644 index 000000000..c3248b5c8 --- /dev/null +++ b/example/hashmaps/example_hashmaps_key_test.f90 @@ -0,0 +1,13 @@ +program example_key_test + use stdlib_kinds, only: int8 + use stdlib_hashmaps, only: chaining_hashmap_type + use stdlib_hashmap_wrappers, only: fnv_1_hasher, key_type, set + implicit none + type(chaining_hashmap_type) :: map + type(key_type) :: key + logical :: present + call map%init(fnv_1_hasher) + call set(key, [0_int8, 1_int8]) + call map%key_test(key, present) + print *, "Initial key of 10 present for empty map = ", present +end program example_key_test diff --git a/example/hashmaps/example_hashmaps_loading.f90 b/example/hashmaps/example_hashmaps_loading.f90 new file mode 100644 index 000000000..33e1e43ec --- /dev/null +++ b/example/hashmaps/example_hashmaps_loading.f90 @@ -0,0 +1,10 @@ +program example_loading + use stdlib_hashmaps, only: open_hashmap_type + use stdlib_hashmap_wrappers, only: fnv_1_hasher + implicit none + type(open_hashmap_type) :: map + real :: ratio + call map%init(fnv_1_hasher) + ratio = map%loading() + print *, "Initial loading = ", ratio +end program example_loading diff --git a/example/hashmaps/example_hashmaps_map_entry.f90 b/example/hashmaps/example_hashmaps_map_entry.f90 new file mode 100644 index 000000000..c28a4c28b --- /dev/null +++ b/example/hashmaps/example_hashmaps_map_entry.f90 @@ -0,0 +1,16 @@ +program example_map_entry + use, intrinsic:: iso_fortran_env, only: int8 + use stdlib_hashmaps, only: chaining_hashmap_type + use stdlib_hashmap_wrappers, only: fnv_1_hasher, key_type, other_type, set + type(chaining_hashmap_type) :: map + type(key_type) :: key + logical :: conflict + type(other_type) :: other + class(*), allocatable :: dummy + allocate (dummy, source=4) + call map%init(fnv_1_hasher, slots_bits=10) + call set(key, [5_int8, 7_int8, 4_int8, 13_int8]) + call set(other, dummy) + call map%map_entry(key, other, conflict) + print *, 'CONFLICT = ', conflict +end program example_map_entry diff --git a/example/hashmaps/example_hashmaps_num_slots.f90 b/example/hashmaps/example_hashmaps_num_slots.f90 new file mode 100644 index 000000000..64fc900b4 --- /dev/null +++ b/example/hashmaps/example_hashmaps_num_slots.f90 @@ -0,0 +1,10 @@ +program example_num_slots + use stdlib_hashmaps, only: chaining_hashmap_type, int_index + use stdlib_hashmap_wrappers, only: fnv_1_hasher + implicit none + type(chaining_hashmap_type) :: map + integer(int_index) :: initial_slots + call map%init(fnv_1_hasher) + initial_slots = map%num_slots() + print *, "Initial slots = ", initial_slots +end program example_num_slots diff --git a/example/hashmaps/example_hashmaps_probes.f90 b/example/hashmaps/example_hashmaps_probes.f90 new file mode 100644 index 000000000..b279a7983 --- /dev/null +++ b/example/hashmaps/example_hashmaps_probes.f90 @@ -0,0 +1,10 @@ +program example_probes + use stdlib_hashmaps, only: chaining_hashmap_type + use stdlib_hashmap_wrappers, only: fnv_1_hasher + implicit none + type(chaining_hashmap_type) :: map + integer :: nprobes + call map%init(fnv_1_hasher) + nprobes = map%map_probes() + print *, "Initial probes = ", nprobes +end program example_probes diff --git a/example/hashmaps/example_hashmaps_rehash.f90 b/example/hashmaps/example_hashmaps_rehash.f90 new file mode 100644 index 000000000..2205ca370 --- /dev/null +++ b/example/hashmaps/example_hashmaps_rehash.f90 @@ -0,0 +1,17 @@ +program example_rehash + use stdlib_kinds, only: int8 + use stdlib_hashmaps, only: open_hashmap_type + use stdlib_hashmap_wrappers, only: fnv_1_hasher, fnv_1a_hasher, & + key_type, other_type, set + implicit none + type(open_hashmap_type) :: map + type(key_type) :: key + type(other_type) :: other + class(*), allocatable :: dummy + allocate (dummy, source='a dummy value') + call map%init(fnv_1_hasher, slots_bits=10) + call set(key, [5_int8, 7_int8, 4_int8, 13_int8]) + call set(other, dummy) + call map%map_entry(key, other) + call map%rehash(fnv_1a_hasher) +end program example_rehash diff --git a/example/hashmaps/example_hashmaps_remove.f90 b/example/hashmaps/example_hashmaps_remove.f90 new file mode 100644 index 000000000..53e1e9b1d --- /dev/null +++ b/example/hashmaps/example_hashmaps_remove.f90 @@ -0,0 +1,18 @@ +program example_remove + use stdlib_kinds, only: int8 + use stdlib_hashmaps, only: open_hashmap_type, int_index + use stdlib_hashmap_wrappers, only: fnv_1_hasher, & + fnv_1a_hasher, key_type, other_type, set + type(open_hashmap_type) :: map + type(key_type) :: key + type(other_type) :: other + logical :: existed + class(*), allocatable :: dummy + allocate (dummy, source=4.0) + call map%init(fnv_1_hasher, slots_bits=10) + call set(key, [5_int8, 7_int8, 4_int8, 13_int8]) + call set(other, dummy) + call map%map_entry(key, other) + call map%remove(key, existed) + print *, "Removed key existed = ", existed +end program example_remove diff --git a/example/hashmaps/example_hashmaps_seeded_nmhash32_hasher.f90 b/example/hashmaps/example_hashmaps_seeded_nmhash32_hasher.f90 new file mode 100644 index 000000000..1ffe8b151 --- /dev/null +++ b/example/hashmaps/example_hashmaps_seeded_nmhash32_hasher.f90 @@ -0,0 +1,13 @@ +program example_seeded_nmhash32_hasher + use stdlib_hashmap_wrappers, only: & + seeded_nmhash32_hasher, key_type, set + use iso_fortran_env, only: int8, int32 + implicit none + integer(int8), allocatable :: array1(:) + integer(int32) :: hash + type(key_type) :: key + array1 = [5_int8, 4_int8, 3_int8, 1_int8, 10_int8, 4_int8] + call set(key, array1) + hash = seeded_nmhash32_hasher(key) + print *, hash +end program example_seeded_nmhash32_hasher diff --git a/example/hashmaps/example_hashmaps_seeded_nmhash32x_hasher.f90 b/example/hashmaps/example_hashmaps_seeded_nmhash32x_hasher.f90 new file mode 100644 index 000000000..4394f0939 --- /dev/null +++ b/example/hashmaps/example_hashmaps_seeded_nmhash32x_hasher.f90 @@ -0,0 +1,13 @@ +program example_seeded_nmhash32x_hasher + use stdlib_kinds, only: int8, int32 + use stdlib_hashmap_wrappers, only: & + seeded_nmhash32x_hasher, key_type, set + implicit none + integer(int8), allocatable :: array1(:) + integer(int32) :: hash + type(key_type) :: key + array1 = [5_int8, 4_int8, 3_int8, 1_int8, 10_int8, 4_int8] + call set(key, array1) + hash = seeded_nmhash32x_hasher(key) + print *, hash +end program example_seeded_nmhash32x_hasher diff --git a/example/hashmaps/example_hashmaps_seeded_water_hasher.f90 b/example/hashmaps/example_hashmaps_seeded_water_hasher.f90 new file mode 100644 index 000000000..cbcf9324e --- /dev/null +++ b/example/hashmaps/example_hashmaps_seeded_water_hasher.f90 @@ -0,0 +1,13 @@ +program example_seeded_water_hasher + use stdlib_hashmap_wrappers, only: & + seeded_water_hasher, key_type, set + use iso_fortran_env, only: int8, int32 + implicit none + integer(int8), allocatable :: array1(:) + integer(int32) :: hash + type(key_type) :: key + array1 = [5_int8, 4_int8, 3_int8, 1_int8, 10_int8, 4_int8] + call set(key, array1) + hash = seeded_water_hasher(key) + print *, hash +end program example_seeded_water_hasher diff --git a/example/hashmaps/example_hashmaps_set.f90 b/example/hashmaps/example_hashmaps_set.f90 new file mode 100644 index 000000000..3bfed35ee --- /dev/null +++ b/example/hashmaps/example_hashmaps_set.f90 @@ -0,0 +1,16 @@ +program example_set + use stdlib_hashmap_wrappers, only: & + get, key_type, set + use iso_fortran_env, only: int8 + implicit none + integer(int8), allocatable :: value(:), result(:) + type(key_type) :: key + integer(int8) :: i + allocate (value(1:15)) + do i = 1, 15 + value(i) = i + end do + call set(key, value) + call get(key, result) + print *, 'RESULT == VALUE = ', all(value == result) +end program example_set diff --git a/example/hashmaps/example_hashmaps_set_other_data.f90 b/example/hashmaps/example_hashmaps_set_other_data.f90 new file mode 100644 index 000000000..fbc3863f7 --- /dev/null +++ b/example/hashmaps/example_hashmaps_set_other_data.f90 @@ -0,0 +1,22 @@ +program example_set_other_data + use stdlib_kinds, only: int8 + use stdlib_hashmaps, only: open_hashmap_type + use stdlib_hashmap_wrappers, only: fnv_1_hasher, & + fnv_1a_hasher, key_type, other_type, set + implicit none + logical :: exists + type(open_hashmap_type) :: map + type(key_type) :: key + type(other_type) :: other + class(*), allocatable :: dummy + call map%init(fnv_1_hasher, slots_bits=10) + allocate (dummy, source='A value') + call set(key, [5_int8, 7_int8, 4_int8, 13_int8]) + call set(other, dummy) + call map%map_entry(key, other) + deallocate (dummy) + allocate (dummy, source='Another value') + call set(other, dummy) + call map%set_other_data(key, other, exists) + print *, 'The entry to have its other data replaced exists = ', exists +end program example_set_other_data diff --git a/example/hashmaps/example_hashmaps_slots_bits.f90 b/example/hashmaps/example_hashmaps_slots_bits.f90 new file mode 100644 index 000000000..141eb62fe --- /dev/null +++ b/example/hashmaps/example_hashmaps_slots_bits.f90 @@ -0,0 +1,10 @@ +program example_slots_bits + use stdlib_hashmaps, only: chaining_hashmap_type + use stdlib_hashmap_wrappers, only: fnv_1_hasher + implicit none + type(chaining_hashmap_type) :: map + integer :: bits + call map%init(fnv_1_hasher) + bits = map%slots_bits() + print *, "Initial slot bits = ", bits +end program example_slots_bits diff --git a/example/hashmaps/example_hashmaps_total_depth.f90 b/example/hashmaps/example_hashmaps_total_depth.f90 new file mode 100644 index 000000000..51a15929e --- /dev/null +++ b/example/hashmaps/example_hashmaps_total_depth.f90 @@ -0,0 +1,10 @@ +program example_total_depth + use stdlib_hashmaps, only: chaining_hashmap_type, int_depth + use stdlib_hashmap_wrappers, only: fnv_1_hasher + implicit none + type(chaining_hashmap_type) :: map + integer(int_depth) :: initial_depth + call map%init(fnv_1_hasher) + initial_depth = map%total_depth() + print *, "Initial total depth = ", initial_depth +end program example_total_depth diff --git a/example/io/CMakeLists.txt b/example/io/CMakeLists.txt new file mode 100644 index 000000000..2e606d2d1 --- /dev/null +++ b/example/io/CMakeLists.txt @@ -0,0 +1,7 @@ +ADD_EXAMPLE(fmt_constants) +#ADD_EXAMPLE(getline) +ADD_EXAMPLE(loadnpy) +ADD_EXAMPLE(loadtxt) +ADD_EXAMPLE(open) +ADD_EXAMPLE(savenpy) +ADD_EXAMPLE(savetxt) diff --git a/example/io/example.dat b/example/io/example.dat new file mode 100644 index 000000000..43d2cdfe5 --- /dev/null +++ b/example/io/example.dat @@ -0,0 +1,3 @@ + 1.00000000E+00 1.00000000E+00 + 1.00000000E+00 1.00000000E+00 + 1.00000000E+00 1.00000000E+00 diff --git a/example/io/example.npy b/example/io/example.npy new file mode 100644 index 000000000..2f182a5ab Binary files /dev/null and b/example/io/example.npy differ diff --git a/example/io/example_fmt_constants.f90 b/example/io/example_fmt_constants.f90 new file mode 100644 index 000000000..03a4f20bb --- /dev/null +++ b/example/io/example_fmt_constants.f90 @@ -0,0 +1,26 @@ +program example_fmt_constants + use stdlib_kinds, only: int32, int64, sp, dp + use stdlib_io, only: FMT_INT, FMT_REAL_SP, FMT_REAL_DP, FMT_COMPLEX_SP, FMT_COMPLEX_DP + implicit none + + integer(kind=int32) :: i32 + integer(kind=int64) :: i64 + real(kind=sp) :: r32 + real(kind=dp) :: r64 + complex(kind=sp) :: c32 + complex(kind=dp) :: c64 + + i32 = 100_int32 + i64 = 100_int64 + r32 = 100.0_sp + r64 = 100.0_dp + c32 = cmplx(100.0_sp, kind=sp) + c64 = cmplx(100.0_dp, kind=dp) + + print "(2("//FMT_INT//",1x))", i32, i64 ! outputs: 100 100 + print FMT_REAL_SP, r32 ! outputs: 1.00000000E+02 + print FMT_REAL_DP, r64 ! outputs: 1.0000000000000000E+002 + print FMT_COMPLEX_SP, c32 ! outputs: 1.00000000E+02 0.00000000E+00 + print FMT_COMPLEX_DP, c64 ! outputs: 1.0000000000000000E+002 0.0000000000000000E+000 + +end program example_fmt_constants diff --git a/example/io/example_getline.f90 b/example/io/example_getline.f90 new file mode 100644 index 000000000..f61265099 --- /dev/null +++ b/example/io/example_getline.f90 @@ -0,0 +1,13 @@ +program example_getline + use, intrinsic :: iso_fortran_env, only: input_unit, output_unit + use stdlib_io, only: getline + implicit none + character(len=:), allocatable :: line + integer :: stat + + call getline(input_unit, line, stat) + do while (stat == 0) + write (output_unit, '(a)') line + call getline(input_unit, line, stat) + end do +end program example_getline diff --git a/example/io/example_loadnpy.f90 b/example/io/example_loadnpy.f90 new file mode 100644 index 000000000..b037312ec --- /dev/null +++ b/example/io/example_loadnpy.f90 @@ -0,0 +1,6 @@ +program example_loadnpy + use stdlib_io_npy, only: load_npy + implicit none + real, allocatable :: x(:, :) + call load_npy('example.npy', x) +end program example_loadnpy diff --git a/example/io/example_loadtxt.f90 b/example/io/example_loadtxt.f90 new file mode 100644 index 000000000..5db4f02e2 --- /dev/null +++ b/example/io/example_loadtxt.f90 @@ -0,0 +1,6 @@ +program example_loadtxt + use stdlib_io, only: loadtxt + implicit none + real, allocatable :: x(:, :) + call loadtxt('example.dat', x) +end program example_loadtxt diff --git a/example/io/example_open.f90 b/example/io/example_open.f90 new file mode 100644 index 000000000..5eef97791 --- /dev/null +++ b/example/io/example_open.f90 @@ -0,0 +1,8 @@ +program example_open + use stdlib_io, only: open + implicit none + integer :: u + u = open ('example.dat', 'wt') + write (u, '(a)') 'This is an example for open' + close (u) +end program example_open diff --git a/example/io/example_savenpy.f90 b/example/io/example_savenpy.f90 new file mode 100644 index 000000000..b6929f40f --- /dev/null +++ b/example/io/example_savenpy.f90 @@ -0,0 +1,6 @@ +program example_savenpy + use stdlib_io_npy, only: save_npy + implicit none + real :: x(3, 2) = 1 + call save_npy('example.npy', x) +end program example_savenpy diff --git a/example/io/example_savetxt.f90 b/example/io/example_savetxt.f90 new file mode 100644 index 000000000..b1e6b94d9 --- /dev/null +++ b/example/io/example_savetxt.f90 @@ -0,0 +1,6 @@ +program example_savetxt + use stdlib_io, only: savetxt + implicit none + real :: x(3, 2) = 1 + call savetxt('example.dat', x) +end program example_savetxt diff --git a/example/linalg/CMakeLists.txt b/example/linalg/CMakeLists.txt new file mode 100644 index 000000000..3f31a5574 --- /dev/null +++ b/example/linalg/CMakeLists.txt @@ -0,0 +1,16 @@ +ADD_EXAMPLE(diag1) +ADD_EXAMPLE(diag2) +ADD_EXAMPLE(diag3) +ADD_EXAMPLE(diag4) +ADD_EXAMPLE(diag5) +ADD_EXAMPLE(eye1) +ADD_EXAMPLE(eye2) +ADD_EXAMPLE(is_diagonal) +ADD_EXAMPLE(is_hermitian) +ADD_EXAMPLE(is_hessenberg) +ADD_EXAMPLE(is_skew_symmetric) +ADD_EXAMPLE(is_square) +ADD_EXAMPLE(is_symmetric) +ADD_EXAMPLE(is_triangular) +ADD_EXAMPLE(outer_product) +ADD_EXAMPLE(trace) diff --git a/example/linalg/example_diag1.f90 b/example/linalg/example_diag1.f90 new file mode 100644 index 000000000..de03ac159 --- /dev/null +++ b/example/linalg/example_diag1.f90 @@ -0,0 +1,7 @@ +program example_diag1 + use stdlib_linalg, only: diag + implicit none + real, allocatable :: A(:, :) + integer :: i + A = diag([(1, i=1, 10)]) ! creates a 10 by 10 identity matrix +end program example_diag1 diff --git a/example/linalg/example_diag2.f90 b/example/linalg/example_diag2.f90 new file mode 100644 index 000000000..5dd260bcc --- /dev/null +++ b/example/linalg/example_diag2.f90 @@ -0,0 +1,8 @@ +program example_diag2 + use stdlib_linalg, only: diag + implicit none + real, allocatable :: v(:) + real, allocatable :: A(:, :) + v = [1, 2, 3, 4, 5] + A = diag(v) ! creates a 5 by 5 matrix with elements of v on the diagonal +end program example_diag2 diff --git a/example/linalg/example_diag3.f90 b/example/linalg/example_diag3.f90 new file mode 100644 index 000000000..b76533a78 --- /dev/null +++ b/example/linalg/example_diag3.f90 @@ -0,0 +1,10 @@ +program example_diag3 + use stdlib_linalg, only: diag + implicit none + integer, parameter :: n = 10 + real :: c(n), ul(n - 1) + real :: A(n, n) + c = 2 + ul = -1 + A = diag(ul, -1) + diag(c) + diag(ul, 1) ! Gil Strang's favorite matrix +end program example_diag3 diff --git a/example/linalg/example_diag4.f90 b/example/linalg/example_diag4.f90 new file mode 100644 index 000000000..dd1a848d8 --- /dev/null +++ b/example/linalg/example_diag4.f90 @@ -0,0 +1,9 @@ +program example_diag4 + use stdlib_linalg, only: diag + implicit none + integer, parameter :: n = 12 + real :: A(n, n) + real :: v(n) + call random_number(A) + v = diag(A) ! v contains diagonal elements of A +end program example_diag4 diff --git a/example/linalg/example_diag5.f90 b/example/linalg/example_diag5.f90 new file mode 100644 index 000000000..438a4b9b3 --- /dev/null +++ b/example/linalg/example_diag5.f90 @@ -0,0 +1,10 @@ +program example_diag5 + use stdlib_linalg, only: diag + implicit none + integer, parameter :: n = 3 + real :: A(n, n) + real, allocatable :: v(:) + A = reshape([1, 2, 3, 4, 5, 6, 7, 8, 9], [n, n]) + v = diag(A, -1) ! v is [2,6] + v = diag(A, 1) ! v is [4,8] +end program example_diag5 diff --git a/example/linalg/example_eye1.f90 b/example/linalg/example_eye1.f90 new file mode 100644 index 000000000..7edda482b --- /dev/null +++ b/example/linalg/example_eye1.f90 @@ -0,0 +1,14 @@ +program example_eye1 + use stdlib_linalg, only: eye + implicit none + integer :: i(2, 2) + real :: a(3, 3) + real :: b(2, 3) !! Matrix is non-square. + complex :: c(2, 2) + I = eye(2) !! [1,0; 0,1] + A = eye(3) !! [1.0,0.0,0.0; 0.0,1.0,0.0; 0.0,0.0,1.0] + A = eye(3, 3) !! [1.0,0.0,0.0; 0.0,1.0,0.0; 0.0,0.0,1.0] + B = eye(2, 3) !! [1.0,0.0,0.0; 0.0,1.0,0.0] + C = eye(2, 2) !! [(1.0,0.0),(0.0,0.0); (0.0,0.0),(1.0,0.0)] + C = (1.0, 1.0)*eye(2, 2) !! [(1.0,1.0),(0.0,0.0); (0.0,0.0),(1.0,1.0)] +end program example_eye1 diff --git a/example/linalg/example_eye2.f90 b/example/linalg/example_eye2.f90 new file mode 100644 index 000000000..93eddb76c --- /dev/null +++ b/example/linalg/example_eye2.f90 @@ -0,0 +1,5 @@ +program example_eye2 + use stdlib_linalg, only: eye, diag + implicit none + print *, all(eye(4) == diag([1, 1, 1, 1])) ! prints .true. +end program example_eye2 diff --git a/example/linalg/example_is_diagonal.f90 b/example/linalg/example_is_diagonal.f90 new file mode 100644 index 000000000..bd93a6d19 --- /dev/null +++ b/example/linalg/example_is_diagonal.f90 @@ -0,0 +1,10 @@ +program example_is_diagonal + use stdlib_linalg, only: is_diagonal + implicit none + real :: A(2, 2), B(2, 2) + logical :: res + A = reshape([1., 0., 0., 4.], shape(A)) + B = reshape([1., 0., 3., 4.], shape(B)) + res = is_diagonal(A) ! returns .true. + res = is_diagonal(B) ! returns .false. +end program example_is_diagonal diff --git a/example/linalg/example_is_hermitian.f90 b/example/linalg/example_is_hermitian.f90 new file mode 100644 index 000000000..3492dcf7d --- /dev/null +++ b/example/linalg/example_is_hermitian.f90 @@ -0,0 +1,10 @@ +program example_is_hermitian + use stdlib_linalg, only: is_hermitian + implicit none + complex :: A(2, 2), B(2, 2) + logical :: res + A = reshape([cmplx(1., 0.), cmplx(3., -1.), cmplx(3., 1.), cmplx(4., 0.)], shape(A)) + B = reshape([cmplx(1., 0.), cmplx(3., 1.), cmplx(3., 1.), cmplx(4., 0.)], shape(B)) + res = is_hermitian(A) ! returns .true. + res = is_hermitian(B) ! returns .false. +end program example_is_hermitian diff --git a/example/linalg/example_is_hessenberg.f90 b/example/linalg/example_is_hessenberg.f90 new file mode 100644 index 000000000..bb1e74b5d --- /dev/null +++ b/example/linalg/example_is_hessenberg.f90 @@ -0,0 +1,10 @@ +program example_is_hessenberg + use stdlib_linalg, only: is_hessenberg + implicit none + real :: A(3, 3), B(3, 3) + logical :: res + A = reshape([1., 2., 0., 4., 5., 6., 7., 8., 9.], shape(A)) + B = reshape([1., 2., 3., 4., 5., 6., 7., 8., 9.], shape(B)) + res = is_hessenberg(A, 'u') ! returns .true. + res = is_hessenberg(B, 'u') ! returns .false. +end program example_is_hessenberg diff --git a/example/linalg/example_is_skew_symmetric.f90 b/example/linalg/example_is_skew_symmetric.f90 new file mode 100644 index 000000000..96d6cb333 --- /dev/null +++ b/example/linalg/example_is_skew_symmetric.f90 @@ -0,0 +1,10 @@ +program example_is_skew_symmetric + use stdlib_linalg, only: is_skew_symmetric + implicit none + real :: A(2, 2), B(2, 2) + logical :: res + A = reshape([0., -3., 3., 0.], shape(A)) + B = reshape([0., 3., 3., 0.], shape(B)) + res = is_skew_symmetric(A) ! returns .true. + res = is_skew_symmetric(B) ! returns .false. +end program example_is_skew_symmetric diff --git a/example/linalg/example_is_square.f90 b/example/linalg/example_is_square.f90 new file mode 100644 index 000000000..237ea6d6a --- /dev/null +++ b/example/linalg/example_is_square.f90 @@ -0,0 +1,10 @@ +program example_is_square + use stdlib_linalg, only: is_square + implicit none + real :: A(2, 2), B(3, 2) + logical :: res + A = reshape([1., 2., 3., 4.], shape(A)) + B = reshape([1., 2., 3., 4., 5., 6.], shape(B)) + res = is_square(A) ! returns .true. + res = is_square(B) ! returns .false. +end program example_is_square diff --git a/example/linalg/example_is_symmetric.f90 b/example/linalg/example_is_symmetric.f90 new file mode 100644 index 000000000..ead88da47 --- /dev/null +++ b/example/linalg/example_is_symmetric.f90 @@ -0,0 +1,10 @@ +program example_is_symmetric + use stdlib_linalg, only: is_symmetric + implicit none + real :: A(2, 2), B(2, 2) + logical :: res + A = reshape([1., 3., 3., 4.], shape(A)) + B = reshape([1., 0., 3., 4.], shape(B)) + res = is_symmetric(A) ! returns .true. + res = is_symmetric(B) ! returns .false. +end program example_is_symmetric diff --git a/example/linalg/example_is_triangular.f90 b/example/linalg/example_is_triangular.f90 new file mode 100644 index 000000000..4cc68b647 --- /dev/null +++ b/example/linalg/example_is_triangular.f90 @@ -0,0 +1,10 @@ +program example_is_triangular + use stdlib_linalg, only: is_triangular + implicit none + real :: A(3, 3), B(3, 3) + logical :: res + A = reshape([1., 0., 0., 4., 5., 0., 7., 8., 9.], shape(A)) + B = reshape([1., 0., 3., 4., 5., 0., 7., 8., 9.], shape(B)) + res = is_triangular(A, 'u') ! returns .true. + res = is_triangular(B, 'u') ! returns .false. +end program example_is_triangular diff --git a/example/linalg/example_outer_product.f90 b/example/linalg/example_outer_product.f90 new file mode 100644 index 000000000..7c20d5c54 --- /dev/null +++ b/example/linalg/example_outer_product.f90 @@ -0,0 +1,9 @@ +program example_outer_product + use stdlib_linalg, only: outer_product + implicit none + real, allocatable :: A(:, :), u(:), v(:) + u = [1., 2., 3.] + v = [3., 4.] + A = outer_product(u, v) +!A = reshape([3., 6., 9., 4., 8., 12.], [3,2]) +end program example_outer_product diff --git a/example/linalg/example_trace.f90 b/example/linalg/example_trace.f90 new file mode 100644 index 000000000..fd9795a0d --- /dev/null +++ b/example/linalg/example_trace.f90 @@ -0,0 +1,7 @@ +program example_trace + use stdlib_linalg, only: trace + implicit none + real :: A(3, 3) + A = reshape([1, 2, 3, 4, 5, 6, 7, 8, 9], [3, 3]) + print *, trace(A) ! 1 + 5 + 9 +end program example_trace diff --git a/example/logger/CMakeLists.txt b/example/logger/CMakeLists.txt new file mode 100644 index 000000000..ecccf6eab --- /dev/null +++ b/example/logger/CMakeLists.txt @@ -0,0 +1,7 @@ +ADD_EXAMPLE(add_log_unit) +ADD_EXAMPLE(configure) +ADD_EXAMPLE(global_logger) +ADD_EXAMPLE(log_io_error) +set_tests_properties(log_io_error PROPERTIES WILL_FAIL true) +ADD_EXAMPLE(log_text_error) +set_tests_properties(log_text_error PROPERTIES WILL_FAIL true) diff --git a/example/logger/dummy.txt b/example/logger/dummy.txt new file mode 100644 index 000000000..bebb7bc86 --- /dev/null +++ b/example/logger/dummy.txt @@ -0,0 +1,2 @@ +a word +it should fail with the presence of this .$/ diff --git a/example/logger/example_add_log_unit.f90 b/example/logger/example_add_log_unit.f90 new file mode 100644 index 000000000..b02292b57 --- /dev/null +++ b/example/logger/example_add_log_unit.f90 @@ -0,0 +1,19 @@ +program example_add_log_unit + use stdlib_logger, only: global_logger, read_only_error + + character(256) :: iomsg + integer :: iostat, unit, stat + + open (newunit=unit, file='error_log.txt', & + form='formatted', status='replace', & + position='rewind', & + action='write', iostat=iostat, iomsg=iomsg) + + call global_logger%add_log_unit(unit, stat) + + select case (stat) + case (read_only_error) + error stop 'Unable to write to "error_log.txt".' + end select + +end program example_add_log_unit diff --git a/example/logger/example_configure.f90 b/example/logger/example_configure.f90 new file mode 100644 index 000000000..582a9f89d --- /dev/null +++ b/example/logger/example_configure.f90 @@ -0,0 +1,6 @@ +program example_configure + use stdlib_logger, only: global => global_logger + + call global%configure(indent=.false., max_width=72) + +end program example_configure diff --git a/example/logger/example_global_logger.f90 b/example/logger/example_global_logger.f90 new file mode 100644 index 000000000..67d290678 --- /dev/null +++ b/example/logger/example_global_logger.f90 @@ -0,0 +1,12 @@ +program example_global_logger + use stdlib_logger, global => global_logger + + integer :: unit, stat + + call global%add_log_file('error_log.txt', unit, & + position='asis', stat=stat) + if (stat /= success) then + error stop 'Unable to open "error_log.txt".' + end if + +end program example_global_logger diff --git a/example/logger/example_log_io_error.f90 b/example/logger/example_log_io_error.f90 new file mode 100644 index 000000000..47389aa12 --- /dev/null +++ b/example/logger/example_log_io_error.f90 @@ -0,0 +1,20 @@ +program example_log_io_error + use stdlib_logger, global => global_logger + + character(*), parameter :: filename = 'nodummy.txt' + integer :: iostat, lun + character(128) :: iomsg + character(*), parameter :: message = & + 'Failure in opening "nodummy.txt".' + + open (newunit=lun, file=filename, form='formatted', & + status='old', iostat=iostat, iomsg=iomsg) + if (iostat /= 0) then + call global%log_io_error(message, & + procedure='EXAMPLE', & + iostat=iostat, & + iomsg=iomsg) + error stop 'Error on opening a file' + end if + +end program example_log_io_error diff --git a/example/logger/example_log_text_error.f90 b/example/logger/example_log_text_error.f90 new file mode 100644 index 000000000..b94f231a9 --- /dev/null +++ b/example/logger/example_log_text_error.f90 @@ -0,0 +1,36 @@ +program example_log_text_error + use stdlib_logger + + character(*), parameter :: filename = 'dummy.txt' + integer :: col_no, line_no, lun, status + character(128) :: line + character(*), parameter :: message = 'Bad text found.' + + open (newunit=lun, file=filename, status='old', & + form='formatted') + line_no = 0 + do + read (lun, fmt='(a)', end=900) line + line_no = line_no + 1 + call check_line(line, status, col_no) + if (status /= 0) then + call global_logger%log_text_error(line, & + col_no, message, filename, line_no) + error stop 'Error in reading '//filename + end if + end do +900 continue + +contains + + subroutine check_line(line, status, col_no) + character(*), intent(in) :: line + integer, intent(inout) :: status + integer, intent(inout) :: col_no + ! scan the line for forbidden characters + col_no = scan(line, ".$/") + ! col_no > 0 means there is a forbidden character + status = col_no + end subroutine + +end program example_log_text_error diff --git a/example/math/CMakeLists.txt b/example/math/CMakeLists.txt new file mode 100644 index 000000000..a75f193f4 --- /dev/null +++ b/example/math/CMakeLists.txt @@ -0,0 +1,15 @@ +ADD_EXAMPLE(clip_integer) +ADD_EXAMPLE(clip_real) +ADD_EXAMPLE(diff) +ADD_EXAMPLE(gcd) +ADD_EXAMPLE(linspace_complex) +ADD_EXAMPLE(linspace_int16) +ADD_EXAMPLE(logspace_complex) +ADD_EXAMPLE(logspace_int) +ADD_EXAMPLE(logspace_rstart_cbase) +ADD_EXAMPLE(math_all_close) +ADD_EXAMPLE(math_arange) +ADD_EXAMPLE(math_argd) +ADD_EXAMPLE(math_arg) +ADD_EXAMPLE(math_argpi) +ADD_EXAMPLE(math_is_close) diff --git a/example/math/example_clip_integer.f90 b/example/math/example_clip_integer.f90 new file mode 100644 index 000000000..fedb45cff --- /dev/null +++ b/example/math/example_clip_integer.f90 @@ -0,0 +1,16 @@ +program example_clip_integer + use stdlib_math, only: clip + use stdlib_kinds, only: int32 + implicit none + integer(int32) :: x + integer(int32) :: xmin + integer(int32) :: xmax + integer(int32) :: clipped_value + + xmin = -5_int32 + xmax = 5_int32 + x = 12_int32 + + clipped_value = clip(x, xmin, xmax) +! clipped_value <- 5 +end program example_clip_integer diff --git a/example/math/example_clip_real.f90 b/example/math/example_clip_real.f90 new file mode 100644 index 000000000..d67332919 --- /dev/null +++ b/example/math/example_clip_real.f90 @@ -0,0 +1,16 @@ +program example_clip_real + use stdlib_math, only: clip + use stdlib_kinds, only: sp + implicit none + real(sp) :: x + real(sp) :: xmin + real(sp) :: xmax + real(sp) :: clipped_value + + xmin = -5.769_sp + xmax = 3.025_sp + x = 3.025_sp + + clipped_value = clip(x, xmin, xmax) +! clipped_value <- 3.02500010 +end program example_clip_real diff --git a/example/math/example_diff.f90 b/example/math/example_diff.f90 new file mode 100644 index 000000000..415896b8f --- /dev/null +++ b/example/math/example_diff.f90 @@ -0,0 +1,22 @@ +program example_diff + + use stdlib_math, only: diff + implicit none + + integer :: i(7) = [1, 1, 2, 3, 5, 8, 13] + real :: x(6) = [0, 5, 15, 30, 50, 75] + integer :: A(3, 3) = reshape([1, 7, 17, 3, 11, 19, 5, 13, 23], [3, 3]) + integer :: Y(3, 2) + + print *, diff(i) ! [0, 1, 1, 2, 3, 5] + print *, diff(x, 2) ! [5.0, 5.0, 5.0, 5.0] + + Y = diff(A, n=1, dim=2) + print *, Y(1, :) ! [2, 2] + print *, Y(2, :) ! [4, 2] + print *, Y(3, :) ! [2, 4] + + print *, diff(i, prepend=[0]) ! [1, 0, 1, 1, 2, 3, 5] + print *, diff(i, append=[21]) ! [0, 1, 1, 2, 3, 5, 8] + +end program example_diff diff --git a/example/math/example_gcd.f90 b/example/math/example_gcd.f90 new file mode 100644 index 000000000..3603f3ec3 --- /dev/null +++ b/example/math/example_gcd.f90 @@ -0,0 +1,9 @@ +program example_gcd + use stdlib_math, only: gcd + implicit none + integer :: a, b, c + + a = 48 + b = 18 + c = gcd(a, b) ! returns 6 +end program example_gcd diff --git a/example/math/example_linspace_complex.f90 b/example/math/example_linspace_complex.f90 new file mode 100644 index 000000000..a9ff31edd --- /dev/null +++ b/example/math/example_linspace_complex.f90 @@ -0,0 +1,12 @@ +program example_linspace_complex + use stdlib_math, only: linspace + use stdlib_kinds, only: dp + implicit none + + complex(dp) :: start = cmplx(10.0_dp, 5.0_dp, kind=dp) + complex(dp) :: end = cmplx(-10.0_dp, 15.0_dp, kind=dp) + + complex(dp) :: z(11) + + z = linspace(start, end, 11) +end program example_linspace_complex diff --git a/example/math/example_linspace_int16.f90 b/example/math/example_linspace_int16.f90 new file mode 100644 index 000000000..375a05bb1 --- /dev/null +++ b/example/math/example_linspace_int16.f90 @@ -0,0 +1,12 @@ +program example_linspace_int16 + use stdlib_math, only: linspace + use stdlib_kinds, only: int16, dp + implicit none + + integer(int16) :: start = 10_int16 + integer(int16) :: end = 23_int16 + + real(dp) :: r(15) + + r = linspace(start, end, 15) +end program example_linspace_int16 diff --git a/example/math/example_logspace_complex.f90 b/example/math/example_logspace_complex.f90 new file mode 100644 index 000000000..45bb480f7 --- /dev/null +++ b/example/math/example_logspace_complex.f90 @@ -0,0 +1,12 @@ +program example_logspace_complex + use stdlib_math, only: logspace + use stdlib_kinds, only: dp + implicit none + + complex(dp) :: start = (10.0_dp, 5.0_dp) + complex(dp) :: end = (-10.0_dp, 15.0_dp) + + complex(dp) :: z(11) ! Complex values raised to complex powers results in complex values + + z = logspace(start, end, 11) +end program example_logspace_complex diff --git a/example/math/example_logspace_int.f90 b/example/math/example_logspace_int.f90 new file mode 100644 index 000000000..1d9387253 --- /dev/null +++ b/example/math/example_logspace_int.f90 @@ -0,0 +1,13 @@ +program example_logspace_int + use stdlib_math, only: logspace + use stdlib_kinds, only: dp + implicit none + + integer, parameter :: start = 10 + integer, parameter :: end = 23 + integer, parameter :: n = 15 + + real(dp) :: r(n) ! Integer values raised to real powers results in real values + + r = logspace(start, end, n) +end program example_logspace_int diff --git a/example/math/example_logspace_rstart_cbase.f90 b/example/math/example_logspace_rstart_cbase.f90 new file mode 100644 index 000000000..069907a8b --- /dev/null +++ b/example/math/example_logspace_rstart_cbase.f90 @@ -0,0 +1,15 @@ +program example_logspace_rstart_cbase + use stdlib_math, only: logspace + use stdlib_kinds, only: dp + implicit none + + real(dp) :: start = 0.0_dp + real(dp) :: end = 3.0_dp + integer, parameter :: n = 4 + complex(dp) :: base = (0.0_dp, 1.0_dp) + + complex(dp) :: z(n) ! complex values raised to real powers result in complex values + + z = logspace(start, end, n, base) + +end program example_logspace_rstart_cbase diff --git a/example/math/example_math_all_close.f90 b/example/math/example_math_all_close.f90 new file mode 100644 index 000000000..99f96dbf9 --- /dev/null +++ b/example/math/example_math_all_close.f90 @@ -0,0 +1,15 @@ +program example_math_all_close + + use stdlib_math, only: all_close + real :: y, NAN + complex :: z(4, 4) + + y = -3 + NAN = sqrt(y) + z = (1.0, 1.0) + + print *, all_close(z + cmplx(1.0e-11, 1.0e-11), z) ! T + print *, NAN, all_close([NAN], [NAN]), all_close([NAN], [NAN], equal_nan=.true.) +! NAN, F, T + +end program example_math_all_close diff --git a/example/math/example_math_arange.f90 b/example/math/example_math_arange.f90 new file mode 100644 index 000000000..2c1637d15 --- /dev/null +++ b/example/math/example_math_arange.f90 @@ -0,0 +1,19 @@ +program example_math_arange + use stdlib_math, only: arange + + print *, arange(3) ! [1,2,3] + print *, arange(-1) ! [1,0,-1] + print *, arange(0, 2) ! [0,1,2] + print *, arange(1, -1) ! [1,0,-1] + print *, arange(0, 2, 2) ! [0,2] + + print *, arange(3.0) ! [1.0,2.0,3.0] + print *, arange(0.0, 5.0) ! [0.0,1.0,2.0,3.0,4.0,5.0] + print *, arange(0.0, 6.0, 2.5) ! [0.0,2.5,5.0] + + print *, (1.0, 1.0)*arange(3) ! [(1.0,1.0),(2.0,2.0),[3.0,3.0]] + + print *, arange(0.0, 2.0, -2.0) ! [0.0,2.0]. Not recommended: `step` argument is negative! + print *, arange(0.0, 2.0, 0.0) ! [0.0,1.0,2.0]. Not recommended: `step` argument is zero! + +end program example_math_arange diff --git a/example/math/example_math_arg.f90 b/example/math/example_math_arg.f90 new file mode 100644 index 000000000..d1056212a --- /dev/null +++ b/example/math/example_math_arg.f90 @@ -0,0 +1,7 @@ +program example_math_arg + use stdlib_math, only: arg + print *, arg((0.0, 0.0)) ! 0.0 + print *, arg((3.0, 4.0)) ! 0.927 + print *, arg(2.0*exp((0.0, 0.5))) ! 0.5 + print *, arg([(0.0, 1.0), (1.0, 0.0), (0.0, -1.0), (-1.0, 0.0)]) ! [π/2, 0.0, -π/2, π] +end program example_math_arg diff --git a/example/math/example_math_argd.f90 b/example/math/example_math_argd.f90 new file mode 100644 index 000000000..485ce4f0b --- /dev/null +++ b/example/math/example_math_argd.f90 @@ -0,0 +1,7 @@ +program example_math_argd + use stdlib_math, only: argd + print *, argd((0.0, 0.0)) ! 0.0° + print *, argd((3.0, 4.0)) ! 53.1° + print *, argd(2.0*exp((0.0, 0.5))) ! 28.64° + print *, argd([(0.0, 1.0), (1.0, 0.0), (0.0, -1.0), (-1.0, 0.0)]) ! [90°, 0°, -90°, 180°] +end program example_math_argd diff --git a/example/math/example_math_argpi.f90 b/example/math/example_math_argpi.f90 new file mode 100644 index 000000000..6914f3ef0 --- /dev/null +++ b/example/math/example_math_argpi.f90 @@ -0,0 +1,7 @@ +program example_math_argpi + use stdlib_math, only: argpi + print *, argpi((0.0, 0.0)) ! 0.0 + print *, argpi((3.0, 4.0)) ! 0.295 + print *, argpi(2.0*exp((0.0, 0.5))) ! 0.159 + print *, argpi([(0.0, 1.0), (1.0, 0.0), (0.0, -1.0), (-1.0, 0.0)]) ! [0.5, 0.0, -0.5, 1.0] +end program example_math_argpi diff --git a/example/math/example_math_is_close.f90 b/example/math/example_math_is_close.f90 new file mode 100644 index 000000000..1775365b0 --- /dev/null +++ b/example/math/example_math_is_close.f90 @@ -0,0 +1,14 @@ +program example_math_is_close + + use stdlib_math, only: is_close + real :: x(2) = [1, 2], y, NAN + + y = -3 + NAN = sqrt(y) + + print *, is_close(x, [real :: 1, 2.1]) ! [T, F] + print *, is_close(2.0, 2.1, abs_tol=0.1) ! T + print *, NAN, is_close(2.0, NAN), is_close(2.0, NAN, equal_nan=.true.) ! NAN, F, F + print *, is_close(NAN, NAN), is_close(NAN, NAN, equal_nan=.true.) ! F, T + +end program example_math_is_close diff --git a/example/optval/CMakeLists.txt b/example/optval/CMakeLists.txt new file mode 100644 index 000000000..14dbdc736 --- /dev/null +++ b/example/optval/CMakeLists.txt @@ -0,0 +1 @@ +ADD_EXAMPLE(optval) diff --git a/example/optval/example_optval.f90 b/example/optval/example_optval.f90 new file mode 100644 index 000000000..9c29457bc --- /dev/null +++ b/example/optval/example_optval.f90 @@ -0,0 +1,14 @@ +program example_optval + use stdlib_optval, only: optval + implicit none + print *, root(64.0) +! 8.0 + print *, root(64.0, 3) +! 4.0 +contains + real function root(x, n) + real, intent(in) :: x + integer, intent(in), optional :: n + root = x**(1.0/optval(n, 2)) + end function root +end program example_optval diff --git a/example/quadrature/CMakeLists.txt b/example/quadrature/CMakeLists.txt new file mode 100644 index 000000000..cd199835d --- /dev/null +++ b/example/quadrature/CMakeLists.txt @@ -0,0 +1,6 @@ +ADD_EXAMPLE(gauss_legendre) +ADD_EXAMPLE(gauss_legendre_lobatto) +ADD_EXAMPLE(simps) +ADD_EXAMPLE(simps_weights) +ADD_EXAMPLE(trapz) +ADD_EXAMPLE(trapz_weights) diff --git a/example/quadrature/example_gauss_legendre.f90 b/example/quadrature/example_gauss_legendre.f90 new file mode 100644 index 000000000..7610963ad --- /dev/null +++ b/example/quadrature/example_gauss_legendre.f90 @@ -0,0 +1,10 @@ +program example_gauss_legendre + use iso_fortran_env, dp => real64 + use stdlib_quadrature, only: gauss_legendre + implicit none + + integer, parameter :: N = 6 + real(dp), dimension(N) :: x, w + call gauss_legendre(x, w) + print *, "integral of x**2 from -1 to 1 is", sum(x**2*w) +end program example_gauss_legendre diff --git a/example/quadrature/example_gauss_legendre_lobatto.f90 b/example/quadrature/example_gauss_legendre_lobatto.f90 new file mode 100644 index 000000000..569d6fd26 --- /dev/null +++ b/example/quadrature/example_gauss_legendre_lobatto.f90 @@ -0,0 +1,10 @@ +program example_gauss_legendre_lobatto + use iso_fortran_env, dp => real64 + use stdlib_quadrature, only: gauss_legendre_lobatto + implicit none + + integer, parameter :: N = 6 + real(dp), dimension(N) :: x, w + call gauss_legendre_lobatto(x, w) + print *, "integral of x**2 from -1 to 1 is", sum(x**2*w) +end program example_gauss_legendre_lobatto diff --git a/example/quadrature/example_simps.f90 b/example/quadrature/example_simps.f90 new file mode 100644 index 000000000..d215c7c8d --- /dev/null +++ b/example/quadrature/example_simps.f90 @@ -0,0 +1,10 @@ +program example_simps + use stdlib_quadrature, only: simps + implicit none + real, parameter :: x(5) = [0., 1., 2., 3., 4.] + real :: y(5) = 3.*x**2 + print *, simps(y, x) +! 64.0 + print *, simps(y, 0.5) +! 32.0 +end program example_simps diff --git a/example/quadrature/example_simps_weights.f90 b/example/quadrature/example_simps_weights.f90 new file mode 100644 index 000000000..86a172b80 --- /dev/null +++ b/example/quadrature/example_simps_weights.f90 @@ -0,0 +1,10 @@ +program example_simps_weights + use stdlib_quadrature, only: simps_weights + implicit none + real, parameter :: x(5) = [0., 1., 2., 3., 4.] + real :: y(5) = 3.*x**2 + real :: w(5) + w = simps_weights(x) + print *, sum(w*y) +! 64.0 +end program example_simps_weights diff --git a/example/quadrature/example_trapz.f90 b/example/quadrature/example_trapz.f90 new file mode 100644 index 000000000..612053fc5 --- /dev/null +++ b/example/quadrature/example_trapz.f90 @@ -0,0 +1,10 @@ +program example_trapz + use stdlib_quadrature, only: trapz + implicit none + real, parameter :: x(5) = [0., 1., 2., 3., 4.] + real :: y(5) = x**2 + print *, trapz(y, x) +! 22.0 + print *, trapz(y, 0.5) +! 11.0 +end program example_trapz diff --git a/example/quadrature/example_trapz_weights.f90 b/example/quadrature/example_trapz_weights.f90 new file mode 100644 index 000000000..dfa47ab1a --- /dev/null +++ b/example/quadrature/example_trapz_weights.f90 @@ -0,0 +1,11 @@ +program example_trapz_weights + use stdlib_quadrature, only: trapz_weights + implicit none + real, parameter :: x(5) = [0., 1., 2., 3., 4.] + real :: y(5) = x**2 + real :: w(5) + w = trapz_weights(x) + print *, sum(w*y) +! 22.0 +end program example_trapz_weights + diff --git a/example/random/CMakeLists.txt b/example/random/CMakeLists.txt new file mode 100644 index 000000000..11409b17a --- /dev/null +++ b/example/random/CMakeLists.txt @@ -0,0 +1,2 @@ +ADD_EXAMPLE(dist_rand) +ADD_EXAMPLE(random_seed) diff --git a/example/random/example_dist_rand.f90 b/example/random/example_dist_rand.f90 new file mode 100644 index 000000000..9cb1bf35b --- /dev/null +++ b/example/random/example_dist_rand.f90 @@ -0,0 +1,17 @@ +program example_dist_rand + use stdlib_kinds, only: int8, int16, int32, int64 + use stdlib_random, only: dist_rand, random_seed + implicit none + integer :: put, get + + put = 135792468 + call random_seed(put, get) ! set and get current value of seed + print *, dist_rand(1_int8) ! random integer in [-2^7, 2^7 - 1] +! -90 + print *, dist_rand(1_int16) ! random integer in [-2^15, 2^15 - 1] +! -32725 + print *, dist_rand(1_int32) ! random integer in [-2^31, 2^31 - 1] +! -1601563881 + print *, dist_rand(1_int64) ! random integer in [-2^63, 2^63 - 1] +! 180977695517992208 +end program example_dist_rand diff --git a/example/random/example_random_seed.f90 b/example/random/example_random_seed.f90 new file mode 100644 index 000000000..cad09ef13 --- /dev/null +++ b/example/random/example_random_seed.f90 @@ -0,0 +1,8 @@ +program example_random_seed + use stdlib_random, only: random_seed + implicit none + integer :: seed_put, seed_get + + seed_put = 1234567 + call random_seed(seed_put, seed_get) ! set and get current value of seed +end program example_random_seed diff --git a/example/selection/CMakeLists.txt b/example/selection/CMakeLists.txt new file mode 100644 index 000000000..d304c184b --- /dev/null +++ b/example/selection/CMakeLists.txt @@ -0,0 +1,2 @@ +ADD_EXAMPLE(arg_select) +ADD_EXAMPLE(select) diff --git a/example/selection/example_arg_select.f90 b/example/selection/example_arg_select.f90 new file mode 100644 index 000000000..e5cb53696 --- /dev/null +++ b/example/selection/example_arg_select.f90 @@ -0,0 +1,29 @@ +program example_arg_select + use stdlib_selection, only: arg_select + implicit none + + real, allocatable :: array(:) + integer, allocatable :: indx(:) + integer :: kth_smallest + integer :: k + + array = [3., 2., 7., 4., 5., 1., 4., -1.] + indx = [(k, k=1, size(array))] + + k = 2 + call arg_select(array, indx, k, kth_smallest) + print *, array(kth_smallest) ! print 1.0 + + k = 7 +! Due to the previous call to arg_select, we know for sure this is in an +! index >= 2 + call arg_select(array, indx, k, kth_smallest, left=2) + print *, array(kth_smallest) ! print 5.0 + + k = 6 +! Due to the previous two calls to arg_select, we know for sure this is in +! an index >= 2 and <= 7 + call arg_select(array, indx, k, kth_smallest, left=2, right=7) + print *, array(kth_smallest) ! print 4.0 + +end program example_arg_select diff --git a/example/selection/example_select.f90 b/example/selection/example_select.f90 new file mode 100644 index 000000000..48e4a8e77 --- /dev/null +++ b/example/selection/example_select.f90 @@ -0,0 +1,27 @@ +program example_select + use stdlib_selection, only: select + implicit none + + real, allocatable :: array(:) + real :: kth_smallest + integer :: k + + array = [3., 2., 7., 4., 5., 1., 4., -1.] + + k = 2 + call select(array, k, kth_smallest) + print *, kth_smallest ! print 1.0 + + k = 7 +! Due to the previous call to select, we know for sure this is in an +! index >= 2 + call select(array, k, kth_smallest, left=2) + print *, kth_smallest ! print 5.0 + + k = 6 +! Due to the previous two calls to select, we know for sure this is in +! an index >= 2 and <= 7 + call select(array, k, kth_smallest, left=2, right=7) + print *, kth_smallest ! print 4.0 + +end program example_select diff --git a/example/selection/selection_vs_sort.f90 b/example/selection/selection_vs_sort.f90 new file mode 100644 index 000000000..a2e43dc11 --- /dev/null +++ b/example/selection/selection_vs_sort.f90 @@ -0,0 +1,77 @@ +program selection_vs_sort + use stdlib_kinds, only: dp, sp, int64 + use stdlib_selection, only: select, arg_select + use stdlib_sorting, only: sort + implicit none + + call compare_select_sort_for_median(1) + call compare_select_sort_for_median(11) + call compare_select_sort_for_median(101) + call compare_select_sort_for_median(1001) + call compare_select_sort_for_median(10001) + call compare_select_sort_for_median(100001) + +contains + subroutine compare_select_sort_for_median(N) + integer, intent(in) :: N + + integer :: i, k, result_arg_select, indx(N), indx_local(N) + real :: random_vals(N), local_random_vals(N) + integer, parameter :: test_reps = 100 + integer(int64) :: t0, t1 + real :: result_sort, result_select + integer(int64) :: time_sort, time_select, time_arg_select + logical :: select_test_passed, arg_select_test_passed + +! Ensure N is odd + if (mod(N, 2) /= 1) stop + + time_sort = 0 + time_select = 0 + time_arg_select = 0 + + select_test_passed = .true. + arg_select_test_passed = .true. + + indx = (/(i, i=1, N)/) + + k = (N + 1)/2 ! Deliberate integer division + + do i = 1, test_reps + call random_number(random_vals) + +! Compute the median with sorting + local_random_vals = random_vals + call system_clock(t0) + call sort(local_random_vals) + result_sort = local_random_vals(k) + call system_clock(t1) + time_sort = time_sort + (t1 - t0) + +! Compute the median with selection, assuming N is odd + local_random_vals = random_vals + call system_clock(t0) + call select(local_random_vals, k, result_select) + call system_clock(t1) + time_select = time_select + (t1 - t0) + +! Compute the median with arg_select, assuming N is odd + local_random_vals = random_vals + indx_local = indx + call system_clock(t0) + call arg_select(local_random_vals, indx_local, k, result_arg_select) + call system_clock(t1) + time_arg_select = time_arg_select + (t1 - t0) + + if (result_select /= result_sort) select_test_passed = .FALSE. + if (local_random_vals(result_arg_select) /= result_sort) arg_select_test_passed = .FALSE. + end do + + print *, "select ; N=", N, '; ', merge('PASS', 'FAIL', select_test_passed), & + '; Relative-speedup-vs-sort:', (1.0*time_sort)/(1.0*time_select) + print *, "arg_select; N=", N, '; ', merge('PASS', 'FAIL', arg_select_test_passed), & + '; Relative-speedup-vs-sort:', (1.0*time_sort)/(1.0*time_arg_select) + + end subroutine + +end program diff --git a/example/sorting/CMakeLists.txt b/example/sorting/CMakeLists.txt new file mode 100644 index 000000000..8a08998d5 --- /dev/null +++ b/example/sorting/CMakeLists.txt @@ -0,0 +1,2 @@ +ADD_EXAMPLE(ord_sort) +ADD_EXAMPLE(sort) diff --git a/example/sorting/example_ord_sort.f90 b/example/sorting/example_ord_sort.f90 new file mode 100644 index 000000000..ecbc2544e --- /dev/null +++ b/example/sorting/example_ord_sort.f90 @@ -0,0 +1,10 @@ +program example_ord_sort + use stdlib_sorting, only: ord_sort + implicit none + integer, allocatable :: array1(:), work(:) + + array1 = [5, 4, 3, 1, 10, 4, 9] + allocate (work, mold=array1) + call ord_sort(array1, work) + print *, array1 !print [1, 3, 4, 4, 5, 9, 10] +end program example_ord_sort diff --git a/example/sorting/example_sort.f90 b/example/sorting/example_sort.f90 new file mode 100644 index 000000000..f6926042e --- /dev/null +++ b/example/sorting/example_sort.f90 @@ -0,0 +1,9 @@ +program example_sort + use stdlib_sorting, only: sort + implicit none + integer, allocatable :: array(:) + + array = [5, 4, 3, 1, 10, 4, 9] + call sort(array) + print *, array !print [1, 3, 4, 4, 5, 9, 10] +end program example_sort diff --git a/example/specialfunctions_gamma/CMakeLists.txt b/example/specialfunctions_gamma/CMakeLists.txt new file mode 100644 index 000000000..212ba81c0 --- /dev/null +++ b/example/specialfunctions_gamma/CMakeLists.txt @@ -0,0 +1,7 @@ +ADD_EXAMPLE(gamma) +ADD_EXAMPLE(gamma_p) +ADD_EXAMPLE(gamma_q) +ADD_EXAMPLE(ligamma) +ADD_EXAMPLE(log_factorial) +ADD_EXAMPLE(log_gamma) +ADD_EXAMPLE(uigamma) diff --git a/example/specialfunctions_gamma/example_gamma.f90 b/example/specialfunctions_gamma/example_gamma.f90 new file mode 100644 index 000000000..6df5f42e4 --- /dev/null +++ b/example/specialfunctions_gamma/example_gamma.f90 @@ -0,0 +1,37 @@ +program example_gamma + use stdlib_kinds, only: dp, int64 + use stdlib_specialfunctions_gamma, only: gamma + implicit none + + integer :: i + integer(int64) :: n + real :: x + real(dp) :: y + complex :: z + complex(dp) :: z1 + + i = 10 + n = 15_int64 + x = 2.5 + y = 4.3_dp + z = (2.3, 0.6) + z1 = (-4.2_dp, 3.1_dp) + + print *, gamma(i) !integer gives exact result +! 362880 + + print *, gamma(n) +! 87178291200 + + print *, gamma(x) ! intrinsic function call +! 1.32934034 + + print *, gamma(y) ! intrinsic function call +! 8.8553433604540341 + + print *, gamma(z) +! (0.988054395, 0.383354813) + + print *, gamma(z1) +! (-2.78916032990983999E-005, 9.83164600163221218E-006) +end program example_gamma diff --git a/example/specialfunctions_gamma/example_gamma_p.f90 b/example/specialfunctions_gamma/example_gamma_p.f90 new file mode 100644 index 000000000..589483812 --- /dev/null +++ b/example/specialfunctions_gamma/example_gamma_p.f90 @@ -0,0 +1,8 @@ +program example_gamma_p + use stdlib_specialfunctions_gamma, only: rgp => regularized_gamma_p + implicit none + + print *, rgp(3.0, 5.0) + +! 0.875347972 +end program example_gamma_p diff --git a/example/specialfunctions_gamma/example_gamma_q.f90 b/example/specialfunctions_gamma/example_gamma_q.f90 new file mode 100644 index 000000000..72edcb3c8 --- /dev/null +++ b/example/specialfunctions_gamma/example_gamma_q.f90 @@ -0,0 +1,8 @@ +program example_gamma_q + use stdlib_specialfunctions_gamma, only: rgq => regularized_gamma_q + implicit none + + print *, rgq(3.0, 5.0) + +! 0.124652028 +end program example_gamma_q diff --git a/example/specialfunctions_gamma/example_ligamma.f90 b/example/specialfunctions_gamma/example_ligamma.f90 new file mode 100644 index 000000000..19a4cd448 --- /dev/null +++ b/example/specialfunctions_gamma/example_ligamma.f90 @@ -0,0 +1,16 @@ +program example_ligamma + use stdlib_specialfunctions_gamma, only: lig => lower_incomplete_gamma + implicit none + integer :: p + real :: p1 + + p = 3 + p1 = 2.3 + print *, lig(p, -5.0) + +! -2521.02417 + + print *, lig(p1, 5.0) + +! 1.09715652 +end program example_ligamma diff --git a/example/specialfunctions_gamma/example_log_factorial.f90 b/example/specialfunctions_gamma/example_log_factorial.f90 new file mode 100644 index 000000000..d8419961c --- /dev/null +++ b/example/specialfunctions_gamma/example_log_factorial.f90 @@ -0,0 +1,15 @@ +program example_log_factorial + use stdlib_kinds, only: int64 + use stdlib_specialfunctions_gamma, only: lf => log_factorial + implicit none + integer :: n + + n = 10 + print *, lf(n) + +! 15.1044130 + + print *, lf(35_int64) + +! 92.1361771 +end program example_log_factorial diff --git a/example/specialfunctions_gamma/example_log_gamma.f90 b/example/specialfunctions_gamma/example_log_gamma.f90 new file mode 100644 index 000000000..70b093670 --- /dev/null +++ b/example/specialfunctions_gamma/example_log_gamma.f90 @@ -0,0 +1,35 @@ +program example_log_gamma + use stdlib_kinds, only: dp + use stdlib_specialfunctions_gamma, only: log_gamma + implicit none + + integer :: i + real :: x + real(dp) :: y + complex :: z + complex(dp) :: z1 + + i = 10 + x = 8.76 + y = x + z = (5.345, -3.467) + z1 = z + print *, log_gamma(i) !default single precision output +!12.8018274 + + print *, log_gamma(x) !intrinsic function call + +!10.0942659 + + print *, log_gamma(y) !intrinsic function call + +!10.094265528673880 + + print *, log_gamma(z) !same kind as input + +!(2.56165648, -5.73382425) + + print *, log_gamma(z1) + +!(2.5616575105114614, -5.7338247782852498) +end program example_log_gamma diff --git a/example/specialfunctions_gamma/example_uigamma.f90 b/example/specialfunctions_gamma/example_uigamma.f90 new file mode 100644 index 000000000..95c07411e --- /dev/null +++ b/example/specialfunctions_gamma/example_uigamma.f90 @@ -0,0 +1,12 @@ +program example_uigamma + use stdlib_specialfunctions_gamma, only: uig => upper_incomplete_gamma + implicit none + + print *, uig(3, -5.0) + +!2523.02295 + + print *, uig(2.3, 5.0) + +!6.95552528E-02 +end program example_uigamma diff --git a/example/stats/CMakeLists.txt b/example/stats/CMakeLists.txt new file mode 100644 index 000000000..94e5f98e9 --- /dev/null +++ b/example/stats/CMakeLists.txt @@ -0,0 +1,6 @@ +ADD_EXAMPLE(corr) +ADD_EXAMPLE(cov) +ADD_EXAMPLE(mean) +ADD_EXAMPLE(median) +ADD_EXAMPLE(moment) +ADD_EXAMPLE(var) diff --git a/example/stats/example_corr.f90 b/example/stats/example_corr.f90 new file mode 100644 index 000000000..ecbd2652e --- /dev/null +++ b/example/stats/example_corr.f90 @@ -0,0 +1,8 @@ +program example_corr + use stdlib_stats, only: corr + implicit none + real :: x(1:6) = [1., 2., 3., 4., 5., 6.] + real :: y(1:2, 1:3) = reshape([-1., 40., -3., 4., 10., 6.], [2, 3]) + print *, corr(x, 1) !returns 1. + print *, corr(y, 2) !returns reshape([ 1., -.32480, -.32480, 1. ], [ 2, 3]) +end program example_corr diff --git a/example/stats/example_cov.f90 b/example/stats/example_cov.f90 new file mode 100644 index 000000000..4750afc8e --- /dev/null +++ b/example/stats/example_cov.f90 @@ -0,0 +1,9 @@ +program example_cov + use stdlib_stats, only: cov + implicit none + real :: x(1:6) = [1., 2., 3., 4., 5., 6.] + real :: y(1:2, 1:3) = reshape([1., 2., 3., 4., 5., 6.], [2, 3]) + print *, cov(x, 1) !returns 3.5 + print *, cov(x, 1, corrected=.false.) !returns 2.9167 + print *, cov(y, 1) !returns a square matrix of size 3 with all elements equal to 0.5 +end program example_cov diff --git a/example/stats/example_mean.f90 b/example/stats/example_mean.f90 new file mode 100644 index 000000000..76cccffba --- /dev/null +++ b/example/stats/example_mean.f90 @@ -0,0 +1,10 @@ +program example_mean + use stdlib_stats, only: mean + implicit none + real :: x(1:6) = [1., 2., 3., 4., 5., 6.] + real :: y(1:2, 1:3) = reshape([1., 2., 3., 4., 5., 6.], [2, 3]) + print *, mean(x) !returns 3.5 + print *, mean(y) !returns 3.5 + print *, mean(y, 1) !returns [ 1.5, 3.5, 5.5 ] + print *, mean(y, 1, y > 3.) !returns [ NaN, 4.0, 5.5 ] +end program example_mean diff --git a/example/stats/example_median.f90 b/example/stats/example_median.f90 new file mode 100644 index 000000000..004e5e228 --- /dev/null +++ b/example/stats/example_median.f90 @@ -0,0 +1,10 @@ +program example_median + use stdlib_stats, only: median + implicit none + real :: x(1:6) = [1., 2., 3., 4., 5., 6.] + real :: y(1:2, 1:3) = reshape([1., 2., 3., 4., 5., 6.], [2, 3]) + print *, median(x) !returns 3.5 + print *, median(y) !returns 3.5 + print *, median(y, 1) !returns [ 1.5, 3.5, 5.5 ] + print *, median(y, 1, y > 3.) !returns [ NaN, 4.0, 5.5 ] +end program example_median diff --git a/example/stats/example_moment.f90 b/example/stats/example_moment.f90 new file mode 100644 index 000000000..a1a6b38a8 --- /dev/null +++ b/example/stats/example_moment.f90 @@ -0,0 +1,12 @@ +program example_moment + use stdlib_stats, only: moment + implicit none + real :: x(1:6) = [1., 2., 3., 4., 5., 6.] + real :: y(1:2, 1:3) = reshape([1., 2., 3., 4., 5., 6.], [2, 3]) + print *, moment(x, 2) !returns 2.9167 + print *, moment(y, 2) !returns 2.9167 + print *, moment(y, 2, 1) !returns [0.25, 0.25, 0.25] + print *, moment(y, 2, 1, mask=(y > 3.)) !returns [NaN, 0., 0.25] + print *, moment(x, 2, center=0.) !returns 15.1667 + print *, moment(y, 1, 1, center=0.) !returns [1.5, 3.5, 5.5] +end program example_moment diff --git a/example/stats/example_var.f90 b/example/stats/example_var.f90 new file mode 100644 index 000000000..8b477fff9 --- /dev/null +++ b/example/stats/example_var.f90 @@ -0,0 +1,12 @@ +program example_var + use stdlib_stats, only: var + implicit none + real :: x(1:6) = [1., 2., 3., 4., 5., 6.] + real :: y(1:2, 1:3) = reshape([1., 2., 3., 4., 5., 6.], [2, 3]) + print *, var(x) !returns 3.5 + print *, var(x, corrected=.false.) !returns 2.9167 + print *, var(y) !returns 3.5 + print *, var(y, 1) !returns [0.5, 0.5, 0.5] + print *, var(y, 1, y > 3.) !returns [NaN, NaN, 0.5] + print *, var(y, 1, y > 3., corrected=.false.) !returns [NaN, 0., 0.25] +end program example_var diff --git a/example/stats_distribution_exponential/CMakeLists.txt b/example/stats_distribution_exponential/CMakeLists.txt new file mode 100644 index 000000000..c3f7fe286 --- /dev/null +++ b/example/stats_distribution_exponential/CMakeLists.txt @@ -0,0 +1,3 @@ +ADD_EXAMPLE(exponential_cdf) +ADD_EXAMPLE(exponential_pdf) +ADD_EXAMPLE(exponential_rvs) diff --git a/example/stats_distribution_exponential/example_exponential_cdf.f90 b/example/stats_distribution_exponential/example_exponential_cdf.f90 new file mode 100644 index 000000000..cc6f2c74f --- /dev/null +++ b/example/stats_distribution_exponential/example_exponential_cdf.f90 @@ -0,0 +1,41 @@ +program example_exponential_cdf + use stdlib_random, only: random_seed + use stdlib_stats_distribution_exponential, only: exp_cdf => cdf_exp, & + rexp => rvs_exp + + implicit none + real :: x(2, 3, 4), a(2, 3, 4) + complex :: scale + integer :: seed_put, seed_get + + seed_put = 1234567 + call random_seed(seed_put, seed_get) + + print *, exp_cdf(1.0, 1.0) ! a standard exponential cumulative at 1.0 + +! 0.632120550 + + print *, exp_cdf(2.0, 2.0) ! a cumulative at 2.0 with lambda=2 + +! 0.981684387 + + x = reshape(rexp(0.5, 24), [2, 3, 4]) +! standard exponential random variates array + a(:, :, :) = 0.5 + print *, exp_cdf(x, a) ! a rank 3 array of standard exponential cumulative + +! 8.57694745E-02 9.70223546E-02 1.52170658E-02 2.95336246E-02 +! 0.107568979 0.196659625 2.97447443E-02 0.366151094 0.163051903 +! 3.36527228E-02 0.385267735 0.428375721 0.103964329 0.147119939 +! 0.192206264 0.330693483 0.179247737 2.92580128E-02 0.332765043 +! 0.472417951 0.500440359 8.56802464E-02 8.72612000E-03 3.55126858E-02 + + scale = (0.5, 1.0) + print *, exp_cdf((0.5, 0.5), scale) +!complex exponential cumulative distribution at (0.5,0.5) with real part of +!lambda=0.5 and imaginary part of lambda=1.0 + +! 8.70351046E-02 + +end program example_exponential_cdf + diff --git a/example/stats_distribution_exponential/example_exponential_pdf.f90 b/example/stats_distribution_exponential/example_exponential_pdf.f90 new file mode 100644 index 000000000..f8eb7a636 --- /dev/null +++ b/example/stats_distribution_exponential/example_exponential_pdf.f90 @@ -0,0 +1,39 @@ +program example_exponential_pdf + use stdlib_random, only: random_seed + use stdlib_stats_distribution_exponential, only: exp_pdf => pdf_exp, & + rexp => rvs_exp + + implicit none + real :: x(2, 3, 4), a(2, 3, 4) + complex :: scale + integer :: seed_put, seed_get + + seed_put = 1234567 + call random_seed(seed_put, seed_get) + + print *, exp_pdf(1.0, 1.0) !a probability density at 1.0 in standard expon + +! 0.367879450 + + print *, exp_pdf(2.0, 2.0) !a probability density at 2.0 with lambda=2.0 + +! 3.66312787E-02 + + x = reshape(rexp(0.5, 24), [2, 3, 4]) ! standard expon random variates array + a(:, :, :) = 0.5 + print *, exp_pdf(x, a) ! a rank 3 standard expon probability density + +! 0.457115263 0.451488823 0.492391467 0.485233188 0.446215510 +! 0.401670188 0.485127628 0.316924453 0.418474048 0.483173639 +! 0.307366133 0.285812140 0.448017836 0.426440030 0.403896868 +! 0.334653258 0.410376132 0.485370994 0.333617479 0.263791025 +! 0.249779820 0.457159877 0.495636940 0.482243657 + + scale = (1.0, 2.) + print *, exp_pdf((1.5, 1.0), scale) +! a complex expon probability density function at (1.5,1.0) with real part +!of lambda=1.0 and imaginary part of lambda=2.0 + +! 6.03947677E-02 + +end program example_exponential_pdf diff --git a/example/stats_distribution_exponential/example_exponential_rvs.f90 b/example/stats_distribution_exponential/example_exponential_rvs.f90 new file mode 100644 index 000000000..551d2fdce --- /dev/null +++ b/example/stats_distribution_exponential/example_exponential_rvs.f90 @@ -0,0 +1,32 @@ +program example_exponential_rvs + use stdlib_random, only: random_seed + use stdlib_stats_distribution_exponential, only: rexp => rvs_exp + + implicit none + complex :: scale + integer :: seed_put, seed_get + + seed_put = 1234567 + call random_seed(seed_put, seed_get) + + print *, rexp() !single standard exponential random variate + +! 0.358690143 + + print *, rexp(2.0) !exponential random variate with lambda=2.0 + +! 0.816459715 + + print *, rexp(0.3, 10) !an array of 10 variates with lambda=0.3 + +! 1.84008647E-02 3.59742008E-02 0.136567295 0.262772143 3.62352766E-02 +! 0.547133625 0.213591918 4.10784185E-02 0.583882213 0.671128035 + + scale = (2.0, 0.7) + print *, rexp(scale) +!single complex exponential random variate with real part of lambda=2.0; +!imagainary part of lambda=0.7 + +! (1.41435969,4.081114382E-02) + +end program example_exponential_rvs diff --git a/example/stats_distribution_normal/CMakeLists.txt b/example/stats_distribution_normal/CMakeLists.txt new file mode 100644 index 000000000..955f00ee1 --- /dev/null +++ b/example/stats_distribution_normal/CMakeLists.txt @@ -0,0 +1,3 @@ +ADD_EXAMPLE(normal_pdf) +ADD_EXAMPLE(normal_rvs) +ADD_EXAMPLE(norm_cdf) diff --git a/example/stats_distribution_normal/example_norm_cdf.f90 b/example/stats_distribution_normal/example_norm_cdf.f90 new file mode 100644 index 000000000..a67450277 --- /dev/null +++ b/example/stats_distribution_normal/example_norm_cdf.f90 @@ -0,0 +1,45 @@ +program example_norm_cdf + use stdlib_random, only: random_seed + use stdlib_stats_distribution_normal, only: norm_cdf => cdf_normal, & + norm => rvs_normal + + implicit none + real :: x(2, 3, 4), a(2, 3, 4), b(2, 3, 4) + complex :: loc, scale + integer :: seed_put, seed_get + + seed_put = 1234567 + call random_seed(seed_put, seed_get) + + print *, norm_cdf(1.0, 0.0, 1.0) ! a standard normal cumulative at 1.0 + +! 0.841344714 + + print *, norm_cdf(2.0, -1.0, 2.0) +! a cumulative at 2.0 with mu=-1 sigma=2 + +! 0.933192849 + + x = reshape(norm(0.0, 1.0, 24), [2, 3, 4]) +! standard normal random variates array + + a(:, :, :) = 0.0 + b(:, :, :) = 1.0 + print *, norm_cdf(x, a, b) ! standard normal cumulative array + +! 0.713505626 0.207069695 0.486513376 0.424511284 0.587328553 +! 0.335559726 0.401470929 0.806552052 0.866687536 0.371323735 +! 0.866228044 0.898046613 0.198435277 0.141147852 0.681565762 +! 0.206268221 0.627057910 0.580759525 0.190364420 7.27325380E-02 +! 7.08068311E-02 0.728241026 0.522919059 0.390097380 + + loc = (1.0, 0.0) + scale = (0.5, 1.0) + print *, norm_cdf((0.5, -0.5), loc, scale) +!complex normal cumulative distribution at (0.5,-0.5) with real part of + !mu=1.0, sigma=0.5 and imaginary part of mu=0.0, sigma=1.0 + +!4.89511043E-02 + +end program example_norm_cdf + diff --git a/example/stats_distribution_normal/example_normal_pdf.f90 b/example/stats_distribution_normal/example_normal_pdf.f90 new file mode 100644 index 000000000..2dc62a370 --- /dev/null +++ b/example/stats_distribution_normal/example_normal_pdf.f90 @@ -0,0 +1,44 @@ +program example_normal_pdf + use stdlib_random, only: random_seed + use stdlib_stats_distribution_normal, only: norm_pdf => pdf_normal, & + norm => rvs_normal + + implicit none + real :: x(3, 4, 5), a(3, 4, 5), b(3, 4, 5) + complex :: loc, scale + integer :: seed_put, seed_get + + seed_put = 1234567 + call random_seed(seed_put, seed_get) + + print *, norm_pdf(1.0, 0., 1.) !a probability density at 1.0 in standard normal + +! 0.241970733 + + print *, norm_pdf(2.0, -1.0, 2.0) +!a probability density at 2.0 with mu=-1.0 sigma=2.0 + +!6.47588000E-02 + + x = reshape(norm(0.0, 1.0, 60), [3, 4, 5]) +! standard normal random variates array + + a(:, :, :) = 0.0 + b(:, :, :) = 1.0 + print *, norm_pdf(x, a, b) ! standard normal probability density array + +! 0.340346158 0.285823315 0.398714304 0.391778737 0.389345556 +! 0.364551932 0.386712372 0.274370432 0.215250477 0.378006011 +! 0.215760440 0.177990928 0.278640658 0.223813817 0.356875211 +! 0.285167664 0.378533930 0.390739858 0.271684974 0.138273031 +! 0.135456234 0.331718773 0.398283750 0.383706540 + + loc = (1.0, -0.5) + scale = (1.0, 2.) + print *, norm_pdf((1.5, 1.0), loc, scale) +! a complex normal probability density function at (1.5,1.0) with real part + ! of mu=1.0, sigma=1.0 and imaginary part of mu=-0.5, sigma=2.0 + +! 5.30100204E-02 + +end program example_normal_pdf diff --git a/example/stats_distribution_normal/example_normal_rvs.f90 b/example/stats_distribution_normal/example_normal_rvs.f90 new file mode 100644 index 000000000..935f8aa6b --- /dev/null +++ b/example/stats_distribution_normal/example_normal_rvs.f90 @@ -0,0 +1,45 @@ +program example_normal_rvs + use stdlib_random, only: random_seed + use stdlib_stats_distribution_normal, only: norm => rvs_normal + + implicit none + real :: a(2, 3, 4), b(2, 3, 4) + complex :: loc, scale + integer :: seed_put, seed_get + + seed_put = 1234567 + call random_seed(seed_put, seed_get) + + print *, norm() !single standard normal random variate + +! 0.563655198 + + print *, norm(1.0, 2.0) +!normal random variate mu=1.0, sigma=2.0 + +! -0.633261681 + + print *, norm(0.0, 1.0, 10) !an array of 10 standard norml random variates + +! -3.38123664E-02 -0.190365672 0.220678389 -0.424612164 -0.249541596 +! 0.865260184 1.11086845 -0.328349441 1.10873628 1.27049923 + + a(:, :, :) = 1.0 + b(:, :, :) = 1.0 + print *, norm(a, b) ! a rank 3 random variates array + +!0.152776539 -7.51764774E-02 1.47208166 0.180561781 1.32407105 +! 1.20383692 0.123445868 -0.455737948 -0.469808221 1.60750175 +! 1.05748117 0.720934749 0.407810807 1.48165631 2.31749439 +! 0.414566994 3.06084275 1.86505437 1.36338580 7.26878643E-02 +! 0.178585172 1.39557445 0.828021586 0.872084975 + + loc = (-1.0, 2.0) + scale = (2.0, 1.0) + print *, norm(loc, scale) +!single complex normal random variate with real part of mu=-1, sigma=2; + !imagainary part of mu=2.0 and sigma=1.0 + +! (1.22566295,2.12518454) + +end program example_normal_rvs diff --git a/example/stats_distribution_uniform/CMakeLists.txt b/example/stats_distribution_uniform/CMakeLists.txt new file mode 100644 index 000000000..7060859bc --- /dev/null +++ b/example/stats_distribution_uniform/CMakeLists.txt @@ -0,0 +1,4 @@ +ADD_EXAMPLE(shuffle) +ADD_EXAMPLE(uniform_cdf) +ADD_EXAMPLE(uniform_pdf) +ADD_EXAMPLE(uniform_rvs) diff --git a/example/stats_distribution_uniform/example_shuffle.f90 b/example/stats_distribution_uniform/example_shuffle.f90 new file mode 100644 index 000000000..b686280e2 --- /dev/null +++ b/example/stats_distribution_uniform/example_shuffle.f90 @@ -0,0 +1,30 @@ +program example_shuffle + use stdlib_random, only: random_seed + use stdlib_stats_distribution_uniform, only: shuffle + implicit none + integer :: seed_put, seed_get, i + real :: x(10) + integer :: n(10) + complex :: z(10) + + do i = 1, 10 + n(i) = i + x(i) = real(i) + z(i) = cmplx(real(i), real(i)) + end do + seed_put = 32165498 + call random_seed(seed_put, seed_get) ! set and get current value of seed + print *, shuffle(n) ! get randomized n + +!10 6 9 2 8 1 3 5 7 4 + + print *, shuffle(x) ! get randomized x + +!5.0 10.0 9.0 4.0 3.0 8.0 2.0 1.0 7.0 6.0 + + print *, shuffle(z) ! get randomized z + +!(8.0, 8.0) (7.0, 7.0) (4.0, 4.0) (1.0, 1.0) (5.0, 5.0) +!(9.0, 9.0) (6.0, 6.0) (3.0, 3.0) (2.0, 2.0) (10.0, 10.0) + +end program example_shuffle diff --git a/example/stats_distribution_uniform/example_uniform_cdf.f90 b/example/stats_distribution_uniform/example_uniform_cdf.f90 new file mode 100644 index 000000000..d683a2e6f --- /dev/null +++ b/example/stats_distribution_uniform/example_uniform_cdf.f90 @@ -0,0 +1,49 @@ +program example_uniform_cdf + use stdlib_random, only: random_seed + use stdlib_stats_distribution_uniform, only: uni_cdf => cdf_uniform, & + uni => rvs_uniform + + implicit none + real :: x(3, 4, 5), a(3, 4, 5), b(3, 4, 5) + complex :: loc, scale + integer :: seed_put, seed_get + + seed_put = 1234567 + call random_seed(seed_put, seed_get) + + print *, uni_cdf(0.5, 0., 1.) ! a cumulative at 0.5 in [0., 1.] + +!0.500000000 + + print *, uni_cdf(0.7, -1.0, 2.0) ! a cumulative at 0.7 in [-1.0, 1.0] + +! 0.850000024 + + print *, uni_cdf(6, 2, 10) ! a cumulative at 6 in [2, 10] + +! 0.454545468 + + a(:, :, :) = -1.0 + b(:, :, :) = 2.0 + x = reshape(uni(-1.0, 2.0, 60), [3, 4, 5]) ! uniform random variates array + print *, uni_cdf(x, a, b) ! cumulative array in [-1.0, 1.0] + +!0.161520004 0.553248405 0.986900032 0.942091405 0.114239901 0.780188501 +! 0.854656875 0.464386612 0.284466714 0.748768032 0.301834047 0.337008357 +!0.568843365 0.596165061 0.180993259 0.614166319 0.214835495 7.98164606E-02 +!0.641274095 0.607101977 0.701139212 0.230517209 1.97925568E-02 0.857982159 +!0.712761045 0.139202654 0.361759573 0.796536088 0.356012046 0.197665215 +!9.80764329E-02 0.781620383 0.595349193 0.125651121 0.957528770 0.942990601 +!0.259489566 7.84273148E-02 0.779313922 0.317909390 0.559013724 0.421358019 +!0.878484428 7.67416358E-02 0.298707575 0.693327367 0.146014273 0.102338850 +!0.855926156 0.250811368 0.300751567 0.110186398 0.502883077 0.738479793 +!0.764856219 0.294822574 1.90783739E-02 0.631218433 0.752170086 0.196848959 + + loc = (0., 0.) + scale = (2., 1.) + print *, uni_cdf((1.2, 0.5), loc, scale) +! joint cumulative distribution at (1.2,0.5) in [(0.,0.), (2.,1.)] + +! 0.300000012 +end program example_uniform_cdf + diff --git a/example/stats_distribution_uniform/example_uniform_pdf.f90 b/example/stats_distribution_uniform/example_uniform_pdf.f90 new file mode 100644 index 000000000..ac2df37b7 --- /dev/null +++ b/example/stats_distribution_uniform/example_uniform_pdf.f90 @@ -0,0 +1,49 @@ +program example_uniform_pdf + use stdlib_random, only: random_seed + use stdlib_stats_distribution_uniform, only: uni_pdf => pdf_uniform, & + uni => rvs_uniform + + implicit none + complex :: loc, scale + real :: a(3, 4, 5), b(3, 4, 5), x(3, 4, 5) + integer :: seed_put, seed_get + + seed_put = 1234567 + call random_seed(seed_put, seed_get) + + print *, uni_pdf(3, 2, 10) !probability density at 3 in range [2, 10] + +! 9.09090936E-02 + + print *, uni_pdf(0.5, 0.0, 1.0) !a probability density at 0.5 in [0., 1.] + +! 1.00000000 + + print *, uni_pdf(0.7, -1.0, 2.0) !a probability density at 0.7 in [-1., 1.] + +! 0.500000000 + + a(:, :, :) = 0.0 + b(:, :, :) = 2.0 + x = reshape(uni(0., 2., 60), [3, 4, 5])! uniform random variates array in [0., 2.] + print *, uni_pdf(x, a, b) ! probability density array in [0., 2.] + +! 0.500000000 0.500000000 0.500000000 0.500000000 0.500000000 0.500000000 +! 0.500000000 0.500000000 0.500000000 0.500000000 0.500000000 0.500000000 +! 0.500000000 0.500000000 0.500000000 0.500000000 0.500000000 0.500000000 +! 0.500000000 0.500000000 0.500000000 0.500000000 0.500000000 0.500000000 +! 0.500000000 0.500000000 0.500000000 0.500000000 0.500000000 0.500000000 +! 0.500000000 0.500000000 0.500000000 0.500000000 0.500000000 0.500000000 +! 0.500000000 0.500000000 0.500000000 0.500000000 0.500000000 0.500000000 +! 0.500000000 0.500000000 0.500000000 0.500000000 0.500000000 0.500000000 +! 0.500000000 0.500000000 0.500000000 0.500000000 0.500000000 0.500000000 +! 0.500000000 0.500000000 0.500000000 0.500000000 0.500000000 0.500000000 + + loc = (-0.5, -0.5) + scale = (1.0, 1.0) + print *, uni_pdf((-0.1, 0.2), loc, scale) +! joint probability density at (-0.1,0.2) in [(-0.5, -0.5), (0.5, 0.5)] + +! 1.00000000 +end program example_uniform_pdf + diff --git a/example/stats_distribution_uniform/example_uniform_rvs.f90 b/example/stats_distribution_uniform/example_uniform_rvs.f90 new file mode 100644 index 000000000..2ac18b1b1 --- /dev/null +++ b/example/stats_distribution_uniform/example_uniform_rvs.f90 @@ -0,0 +1,79 @@ +program example_uniform_rvs + use stdlib_random, only: random_seed + use stdlib_stats_distribution_uniform, only: uni => rvs_uniform + + implicit none + complex :: loc, scale + real :: a(3, 4, 5), b(3, 4, 5) + integer :: seed_put, seed_get + + seed_put = 1234567 + call random_seed(seed_put, seed_get) + + print *, uni() !real standard uniform random variate in [0., 1.] +! 0.161520019 + + print *, uni(3.0) !an uniform random variate in [0., 3.] +! 1.65974522 + + print *, uni(-0.5, 1.0) !an uniform random variate in [-0.5, 0.5] +! 0.486900032 + + print *, uni(-1.0, 2.0, 10) +!an array of 10 uniform random variates in [-1., 1.] + +!0.884182811 -0.771520197 0.560377002 0.709313750 -7.12267756E-02 +!-0.431066573 0.497536063 -0.396331906 -0.325983286 0.137686729 + + print *, uni(20) !a random integer variate in [0, 20] +! 17 + + print *, uni(5, 13) !a random integer variate in [5, 18] +! 15 + + print *, uni(3, 19, 10) !an array of 10 integer variates in [3,22] + +!7 16 16 12 9 21 19 4 3 19 + + loc = (-0.5, -0.5) + scale = (1.0, 1.0) + + print *, uni(scale) !a complex uniform random variate in unit square + +!(0.139202669, 0.361759573) + + print *, uni(loc, scale) +!a complex uniform random variate in [(-0.5, -0.5), (0.5, 0.5)] + +!(0.296536088,-0.143987954) + + print *, uni(loc, scale, 10) +!an array of 10 complex uniform random variate in [(-0.5, -0.5), (0.5, 0.5)] + +!(-0.302334785,-0.401923567) (0.281620383,9.534919262E-02) +! (-0.374348879,0.457528770) (0.442990601,-0.240510434) +! (-0.421572685,0.279313922) (-0.182090610,5.901372433E-02) +! (-7.864198089E-02,0.378484428) (-0.423258364,-0.201292425) +! (0.193327367,-0.353985727) (-0.397661150,0.355926156) + + a(:, :, :) = -0.5 + b(:, :, :) = 1.0 + + print *, uni(a, b) +!a rank 3 array of random variates in [-0.5,0.5] + +! -0.249188632 -0.199248433 -0.389813602 2.88307667E-03 0.238479793, +! 0.264856219 -0.205177426 -0.480921626 0.131218433 0.252170086, +! -0.303151041 -8.89462233E-02 -0.377370685 0.341802299 0.323204756, +! 0.358679056 -0.138909757 0.384329498 -0.109372199 0.132353067, +! 0.494320452 0.419343710 -0.103044361 0.461389005 0.403132677 +! 0.121850729 0.403839290 -0.349389791 0.490482628 0.156600773 +! 8.46788883E-02 -0.483680278 0.388107836 0.119698405 0.154214382 +! 0.153113484 0.236523747 0.155937552 -0.135760903 0.219589531 +! 0.394639254 6.30156994E-02 -0.342692465 -0.444846451 -0.215700030 +! 0.204189956 -0.208748132 0.355063021 8.98272395E-02 -0.237928331 +! 2.98077464E-02 -0.485149682 -8.06870461E-02 -0.372713923 +! -0.178335011 0.283877611 -2.13934183E-02 -9.21690464E-03 +! 4.56320047E-02 0.220112979 + +end program example_uniform_rvs diff --git a/example/string_type/CMakeLists.txt b/example/string_type/CMakeLists.txt new file mode 100644 index 000000000..c3a754cd3 --- /dev/null +++ b/example/string_type/CMakeLists.txt @@ -0,0 +1,40 @@ +ADD_EXAMPLE(adjustl) +ADD_EXAMPLE(adjustr) +ADD_EXAMPLE(char) +ADD_EXAMPLE(char_position) +ADD_EXAMPLE(char_range) +ADD_EXAMPLE(constructor_character) +ADD_EXAMPLE(constructor_empty) +ADD_EXAMPLE(constructor_integer) +ADD_EXAMPLE(constructor_logical) +ADD_EXAMPLE(constructor_scalar) +ADD_EXAMPLE(cont) +ADD_EXAMPLE(eq) +ADD_EXAMPLE(fread) +ADD_EXAMPLE(fwrite) +ADD_EXAMPLE(ge) +ADD_EXAMPLE(gt) +ADD_EXAMPLE(iachar) +ADD_EXAMPLE(ichar) +ADD_EXAMPLE(index) +ADD_EXAMPLE(le) +ADD_EXAMPLE(len) +ADD_EXAMPLE(len_trim) +ADD_EXAMPLE(lge) +ADD_EXAMPLE(lgt) +ADD_EXAMPLE(lle) +ADD_EXAMPLE(llt) +ADD_EXAMPLE(lt) +ADD_EXAMPLE(move) +ADD_EXAMPLE(ne) +ADD_EXAMPLE(repeat) +ADD_EXAMPLE(reverse) +ADD_EXAMPLE(scan) +ADD_EXAMPLE(to_lower) +ADD_EXAMPLE(to_sentence) +ADD_EXAMPLE(to_title) +ADD_EXAMPLE(to_upper) +ADD_EXAMPLE(trim) +ADD_EXAMPLE(uread) +ADD_EXAMPLE(uwrite) +ADD_EXAMPLE(verify) diff --git a/example/string_type/example_adjustl.f90 b/example/string_type/example_adjustl.f90 new file mode 100644 index 000000000..3b747d9a6 --- /dev/null +++ b/example/string_type/example_adjustl.f90 @@ -0,0 +1,9 @@ +program example_adjustl + use stdlib_string_type + implicit none + type(string_type) :: string + + string = " Whitespace" + string = adjustl(string) +! char(string) == "Whitespace " +end program example_adjustl diff --git a/example/string_type/example_adjustr.f90 b/example/string_type/example_adjustr.f90 new file mode 100644 index 000000000..e2f1451b8 --- /dev/null +++ b/example/string_type/example_adjustr.f90 @@ -0,0 +1,9 @@ +program example_adjustr + use stdlib_string_type + implicit none + type(string_type) :: string + + string = "Whitespace " + string = adjustr(string) +! char(string) == " Whitespace" +end program example_adjustr diff --git a/example/string_type/example_char.f90 b/example/string_type/example_char.f90 new file mode 100644 index 000000000..ba5804cc1 --- /dev/null +++ b/example/string_type/example_char.f90 @@ -0,0 +1,10 @@ +program example_char + use stdlib_string_type + implicit none + type(string_type) :: string + character(len=:), allocatable :: dlc + + string = "Character sequence" + dlc = char(string) +! dlc == "Character sequence" +end program example_char diff --git a/example/string_type/example_char_position.f90 b/example/string_type/example_char_position.f90 new file mode 100644 index 000000000..3297950d6 --- /dev/null +++ b/example/string_type/example_char_position.f90 @@ -0,0 +1,13 @@ +program example_char_position + use stdlib_string_type + implicit none + type(string_type) :: string + character(len=:), allocatable :: dlc + character(len=1), allocatable :: chars(:) + + string = "Character sequence" + dlc = char(string, 3) +! dlc == "a" + chars = char(string, [3, 5, 8, 12, 14, 15, 18]) +! chars == ["a", "a", "e", "e", "u", "e", "e"] +end program example_char_position diff --git a/example/string_type/example_char_range.f90 b/example/string_type/example_char_range.f90 new file mode 100644 index 000000000..8741baa0c --- /dev/null +++ b/example/string_type/example_char_range.f90 @@ -0,0 +1,10 @@ +program example_char_range + use stdlib_string_type + implicit none + type(string_type) :: string + character(len=:), allocatable :: dlc + + string = "Fortran" + dlc = char(string, 1, 4) +! dlc == "Fort" +end program example_char_range diff --git a/example/string_type/example_constructor_character.f90 b/example/string_type/example_constructor_character.f90 new file mode 100644 index 000000000..eeeeb5f61 --- /dev/null +++ b/example/string_type/example_constructor_character.f90 @@ -0,0 +1,8 @@ +program example_constructor_character + use stdlib_string_type + implicit none + type(string_type) :: string +! len(string) == 0 + string = "Sequence" +! len(string) == 8 +end program example_constructor_character diff --git a/example/string_type/example_constructor_empty.f90 b/example/string_type/example_constructor_empty.f90 new file mode 100644 index 000000000..38be70b0b --- /dev/null +++ b/example/string_type/example_constructor_empty.f90 @@ -0,0 +1,7 @@ +program example_constructor_empty + use stdlib_string_type + implicit none + type(string_type) :: string + string = string_type() +! len(string) == 0 +end program example_constructor_empty diff --git a/example/string_type/example_constructor_integer.f90 b/example/string_type/example_constructor_integer.f90 new file mode 100644 index 000000000..dbad31f00 --- /dev/null +++ b/example/string_type/example_constructor_integer.f90 @@ -0,0 +1,9 @@ +program example_constructor_integer + use stdlib_string_type + implicit none + type(string_type) :: string + string = string_type(42) +! len(string) == 2 + string = string_type(-289) +! len(string) == 4 +end program example_constructor_integer diff --git a/example/string_type/example_constructor_logical.f90 b/example/string_type/example_constructor_logical.f90 new file mode 100644 index 000000000..341f4382b --- /dev/null +++ b/example/string_type/example_constructor_logical.f90 @@ -0,0 +1,9 @@ +program example_constructor_logical + use stdlib_string_type + implicit none + type(string_type) :: string + string = string_type(.true.) +! len(string) == 1 + string = string_type(.false.) +! len(string) == 1 +end program example_constructor_logical diff --git a/example/string_type/example_constructor_scalar.f90 b/example/string_type/example_constructor_scalar.f90 new file mode 100644 index 000000000..d49ba231e --- /dev/null +++ b/example/string_type/example_constructor_scalar.f90 @@ -0,0 +1,9 @@ +program example_constructor_scalar + use stdlib_string_type + implicit none + type(string_type) :: string + string = string_type("Sequence") +! len(string) == 8 + string = string_type(" S p a c e d ") +! len(string) == 13 +end program example_constructor_scalar diff --git a/example/string_type/example_cont.f90 b/example/string_type/example_cont.f90 new file mode 100644 index 000000000..b19e4029f --- /dev/null +++ b/example/string_type/example_cont.f90 @@ -0,0 +1,9 @@ +program example_cont + use stdlib_string_type + implicit none + type(string_type) :: string + + string = "Hello, " + string = string//"World!" +! len(string) == 13 +end program example_cont diff --git a/example/string_type/example_eq.f90 b/example/string_type/example_eq.f90 new file mode 100644 index 000000000..7ab27557d --- /dev/null +++ b/example/string_type/example_eq.f90 @@ -0,0 +1,16 @@ +program example_eq + use stdlib_string_type + implicit none + type(string_type) :: string + logical :: res + + string = "bcd" + res = string == "abc" +! res .eqv. .false. + + res = string == "bcd" +! res .eqv. .true. + + res = string == "cde" +! res .eqv. .false. +end program example_eq diff --git a/example/string_type/example_fread.f90 b/example/string_type/example_fread.f90 new file mode 100644 index 000000000..db1ef7ca8 --- /dev/null +++ b/example/string_type/example_fread.f90 @@ -0,0 +1,16 @@ +program example_fread + use stdlib_string_type + implicit none + type(string_type) :: string + integer :: io + string = "Important saved value" + + open (newunit=io, form="formatted", status="scratch") + write (io, *) string + write (io, *) + + rewind (io) + + read (io, *) string + close (io) +end program example_fread diff --git a/example/string_type/example_fwrite.f90 b/example/string_type/example_fwrite.f90 new file mode 100644 index 000000000..30f543258 --- /dev/null +++ b/example/string_type/example_fwrite.f90 @@ -0,0 +1,16 @@ +program example_fwrite + use stdlib_string_type + implicit none + type(string_type) :: string + integer :: io + string = "Important saved value" + + open (newunit=io, form="formatted", status="scratch") + write (io, *) string + write (io, *) + + rewind (io) + + read (io, *) string + close (io) +end program example_fwrite diff --git a/example/string_type/example_ge.f90 b/example/string_type/example_ge.f90 new file mode 100644 index 000000000..98c19e701 --- /dev/null +++ b/example/string_type/example_ge.f90 @@ -0,0 +1,16 @@ +program example_ge + use stdlib_string_type + implicit none + type(string_type) :: string + logical :: res + + string = "bcd" + res = string >= "abc" +! res .eqv. .true. + + res = string >= "bcd" +! res .eqv. .true. + + res = string >= "cde" +! res .eqv. .false. +end program example_ge diff --git a/example/string_type/example_gt.f90 b/example/string_type/example_gt.f90 new file mode 100644 index 000000000..f69f7d851 --- /dev/null +++ b/example/string_type/example_gt.f90 @@ -0,0 +1,16 @@ +program example_gt + use stdlib_string_type + implicit none + type(string_type) :: string + logical :: res + + string = "bcd" + res = string > "abc" +! res .eqv. .true. + + res = string > "bcd" +! res .eqv. .false. + + res = string > "cde" +! res .eqv. .false. +end program example_gt diff --git a/example/string_type/example_iachar.f90 b/example/string_type/example_iachar.f90 new file mode 100644 index 000000000..76f558a8a --- /dev/null +++ b/example/string_type/example_iachar.f90 @@ -0,0 +1,9 @@ +program example_iachar + use stdlib_string_type + implicit none + type(string_type) :: string + integer :: code + + string = "Fortran" + code = iachar(string) +end program example_iachar diff --git a/example/string_type/example_ichar.f90 b/example/string_type/example_ichar.f90 new file mode 100644 index 000000000..adf51f000 --- /dev/null +++ b/example/string_type/example_ichar.f90 @@ -0,0 +1,9 @@ +program example_ichar + use stdlib_string_type + implicit none + type(string_type) :: string + integer :: code + + string = "Fortran" + code = ichar(string) +end program example_ichar diff --git a/example/string_type/example_index.f90 b/example/string_type/example_index.f90 new file mode 100644 index 000000000..09ea81f41 --- /dev/null +++ b/example/string_type/example_index.f90 @@ -0,0 +1,16 @@ +program example_index + use stdlib_string_type + implicit none + type(string_type) :: string + integer :: pos + + string = "Search this string for this expression" + pos = index(string, "this") +! pos == 8 + + pos = index(string, "this", back=.true.) +! pos == 24 + + pos = index(string, "This") +! pos == 0 +end program example_index diff --git a/example/string_type/example_le.f90 b/example/string_type/example_le.f90 new file mode 100644 index 000000000..4cf0fb229 --- /dev/null +++ b/example/string_type/example_le.f90 @@ -0,0 +1,16 @@ +program example_le + use stdlib_string_type + implicit none + type(string_type) :: string + logical :: res + + string = "bcd" + res = string <= "abc" +! res .eqv. .false. + + res = string <= "bcd" +! res .eqv. .true. + + res = string <= "cde" +! res .eqv. .true. +end program example_le diff --git a/example/string_type/example_len.f90 b/example/string_type/example_len.f90 new file mode 100644 index 000000000..6c7685f3f --- /dev/null +++ b/example/string_type/example_len.f90 @@ -0,0 +1,14 @@ +program example_len + use stdlib_string_type + implicit none + type(string_type) :: string + integer :: length + + string = "Some longer sentence for this example." + length = len(string) +! length == 38 + + string = "Whitespace " + length = len(string) +! length == 38 +end program example_len diff --git a/example/string_type/example_len_trim.f90 b/example/string_type/example_len_trim.f90 new file mode 100644 index 000000000..6e119a274 --- /dev/null +++ b/example/string_type/example_len_trim.f90 @@ -0,0 +1,14 @@ +program example_len_trim + use stdlib_string_type + implicit none + type(string_type) :: string + integer :: length + + string = "Some longer sentence for this example." + length = len_trim(string) +! length == 38 + + string = "Whitespace " + length = len_trim(string) +! length == 10 +end program example_len_trim diff --git a/example/string_type/example_lge.f90 b/example/string_type/example_lge.f90 new file mode 100644 index 000000000..4821934ab --- /dev/null +++ b/example/string_type/example_lge.f90 @@ -0,0 +1,16 @@ +program example_lge + use stdlib_string_type + implicit none + type(string_type) :: string + logical :: res + + string = "bcd" + res = lge(string, "abc") +! res .eqv. .true. + + res = lge(string, "bcd") +! res .eqv. .true. + + res = lge(string, "cde") +! res .eqv. .false. +end program example_lge diff --git a/example/string_type/example_lgt.f90 b/example/string_type/example_lgt.f90 new file mode 100644 index 000000000..abcbdcf2c --- /dev/null +++ b/example/string_type/example_lgt.f90 @@ -0,0 +1,16 @@ +program example_lgt + use stdlib_string_type + implicit none + type(string_type) :: string + logical :: res + + string = "bcd" + res = lgt(string, "abc") +! res .eqv. .true. + + res = lgt(string, "bcd") +! res .eqv. .false. + + res = lgt(string, "cde") +! res .eqv. .false. +end program example_lgt diff --git a/example/string_type/example_lle.f90 b/example/string_type/example_lle.f90 new file mode 100644 index 000000000..c452fd39d --- /dev/null +++ b/example/string_type/example_lle.f90 @@ -0,0 +1,16 @@ +program example_lle + use stdlib_string_type + implicit none + type(string_type) :: string + logical :: res + + string = "bcd" + res = lle(string, "abc") +! res .eqv. .false. + + res = lle(string, "bcd") +! res .eqv. .true. + + res = lle(string, "cde") +! res .eqv. .true. +end program example_lle diff --git a/example/string_type/example_llt.f90 b/example/string_type/example_llt.f90 new file mode 100644 index 000000000..1c25100cf --- /dev/null +++ b/example/string_type/example_llt.f90 @@ -0,0 +1,16 @@ +program example_llt + use stdlib_string_type + implicit none + type(string_type) :: string + logical :: res + + string = "bcd" + res = llt(string, "abc") +! res .eqv. .false. + + res = llt(string, "bcd") +! res .eqv. .false. + + res = llt(string, "cde") +! res .eqv. .true. +end program example_llt diff --git a/example/string_type/example_lt.f90 b/example/string_type/example_lt.f90 new file mode 100644 index 000000000..be8fedfb6 --- /dev/null +++ b/example/string_type/example_lt.f90 @@ -0,0 +1,16 @@ +program example_lt + use stdlib_string_type + implicit none + type(string_type) :: string + logical :: res + + string = "bcd" + res = string < "abc" +! res .eqv. .false. + + res = string < "bcd" +! res .eqv. .false. + + res = string < "cde" +! res .eqv. .true. +end program example_lt diff --git a/example/string_type/example_move.f90 b/example/string_type/example_move.f90 new file mode 100644 index 000000000..5ee6e9882 --- /dev/null +++ b/example/string_type/example_move.f90 @@ -0,0 +1,21 @@ +program example_move + use stdlib_string_type, only: string_type, assignment(=), move + implicit none + type(string_type) :: from_string + character(len=:), allocatable :: from_char, to_char + + from_string = "move this string" + from_char = "move this char" +! from_string <-- "move this string" +! from_char <-- "move this char" +! to_char <-- (unallocated) + + call move(from_string, to_char) +! from_string <-- "" +! to_char <-- "move this string" + + call move(from_char, to_char) +! from_char <-- (unallocated) +! to_string <-- "move this char" + +end program example_move diff --git a/example/string_type/example_ne.f90 b/example/string_type/example_ne.f90 new file mode 100644 index 000000000..6dfc49b5d --- /dev/null +++ b/example/string_type/example_ne.f90 @@ -0,0 +1,16 @@ +program example_ne + use stdlib_string_type + implicit none + type(string_type) :: string + logical :: res + + string = "bcd" + res = string /= "abc" +! res .eqv. .true. + + res = string /= "bcd" +! res .eqv. .false. + + res = string /= "cde" +! res .eqv. .true. +end program example_ne diff --git a/example/string_type/example_repeat.f90 b/example/string_type/example_repeat.f90 new file mode 100644 index 000000000..c15d0d2c3 --- /dev/null +++ b/example/string_type/example_repeat.f90 @@ -0,0 +1,9 @@ +program example_repeat + use stdlib_string_type + implicit none + type(string_type) :: string + + string = "What? " + string = repeat(string, 3) +! string == "What? What? What? " +end program example_repeat diff --git a/example/string_type/example_reverse.f90 b/example/string_type/example_reverse.f90 new file mode 100644 index 000000000..3b63eb778 --- /dev/null +++ b/example/string_type/example_reverse.f90 @@ -0,0 +1,12 @@ +program example_reverse + use stdlib_string_type + implicit none + type(string_type) :: string, reverse_string + + string = "Reverse This String" +! string <-- "Reverse This String" + + reverse_string = reverse(string) +! string <-- "Reverse This String" +! reverse_string <-- "gnirtS sihT esreveR" +end program example_reverse diff --git a/example/string_type/example_scan.f90 b/example/string_type/example_scan.f90 new file mode 100644 index 000000000..c1476ae79 --- /dev/null +++ b/example/string_type/example_scan.f90 @@ -0,0 +1,16 @@ +program example_scan + use stdlib_string_type + implicit none + type(string_type) :: string + integer :: pos + + string = "fortran" + pos = scan(string, "ao") +! pos == 2 + + pos = scan(string, "ao", .true.) +! pos == 6 + + pos = scan(string, "c++") +! pos == 0 +end program example_scan diff --git a/example/string_type/example_to_lower.f90 b/example/string_type/example_to_lower.f90 new file mode 100644 index 000000000..e6bbab4d7 --- /dev/null +++ b/example/string_type/example_to_lower.f90 @@ -0,0 +1,12 @@ +program example_to_lower + use stdlib_string_type + implicit none + type(string_type) :: string, lowercase_string + + string = "Lowercase This String" +! string <-- "Lowercase This String" + + lowercase_string = to_lower(string) +! string <-- "Lowercase This String" +! lowercase_string <-- "lowercase this string" +end program example_to_lower diff --git a/example/string_type/example_to_sentence.f90 b/example/string_type/example_to_sentence.f90 new file mode 100644 index 000000000..0b42acda8 --- /dev/null +++ b/example/string_type/example_to_sentence.f90 @@ -0,0 +1,12 @@ +program example_to_sentence + use stdlib_string_type + implicit none + type(string_type) :: string, sentencecase_string + + string = "sentencecase this string." +! string <-- "sentencecase this string." + + sentencecase_string = to_sentence(string) +! string <-- "sentencecase this string." +! sentencecase_string <-- "Sentencecase this string." +end program example_to_sentence diff --git a/example/string_type/example_to_title.f90 b/example/string_type/example_to_title.f90 new file mode 100644 index 000000000..2e6264ccc --- /dev/null +++ b/example/string_type/example_to_title.f90 @@ -0,0 +1,12 @@ +program example_to_title + use stdlib_string_type + implicit none + type(string_type) :: string, titlecase_string + + string = "titlecase this string." +! string <-- "titlecase this string." + + titlecase_string = to_title(string) +! string <-- "titlecase this string." +! titlecase_string <-- "Titlecase This String." +end program example_to_title diff --git a/example/string_type/example_to_upper.f90 b/example/string_type/example_to_upper.f90 new file mode 100644 index 000000000..ef013a29c --- /dev/null +++ b/example/string_type/example_to_upper.f90 @@ -0,0 +1,12 @@ +program example_to_upper + use stdlib_string_type + implicit none + type(string_type) :: string, uppercase_string + + string = "Uppercase This String" +! string <-- "Uppercase This String" + + uppercase_string = to_upper(string) +! string <-- "Uppercase This String" +! uppercase_string <-- "UPPERCASE THIS STRING" +end program example_to_upper diff --git a/example/string_type/example_trim.f90 b/example/string_type/example_trim.f90 new file mode 100644 index 000000000..8c0b6eea0 --- /dev/null +++ b/example/string_type/example_trim.f90 @@ -0,0 +1,9 @@ +program example_trim + use stdlib_string_type + implicit none + type(string_type) :: string + + string = "Whitespace " + string = trim(string) +! len(string) == 10 +end program example_trim diff --git a/example/string_type/example_uread.f90 b/example/string_type/example_uread.f90 new file mode 100644 index 000000000..ed9fe1acd --- /dev/null +++ b/example/string_type/example_uread.f90 @@ -0,0 +1,15 @@ +program example_uread + use stdlib_string_type + implicit none + type(string_type) :: string + integer :: io + string = "Important saved value" + + open (newunit=io, form="unformatted", status="scratch") + write (io) string + + rewind (io) + + read (io) string + close (io) +end program example_uread diff --git a/example/string_type/example_uwrite.f90 b/example/string_type/example_uwrite.f90 new file mode 100644 index 000000000..43b3aeaa2 --- /dev/null +++ b/example/string_type/example_uwrite.f90 @@ -0,0 +1,15 @@ +program example_uwrite + use stdlib_string_type + implicit none + type(string_type) :: string + integer :: io + string = "Important saved value" + + open (newunit=io, form="unformatted", status="scratch") + write (io) string + + rewind (io) + + read (io) string + close (io) +end program example_uwrite diff --git a/example/string_type/example_verify.f90 b/example/string_type/example_verify.f90 new file mode 100644 index 000000000..a94bbf887 --- /dev/null +++ b/example/string_type/example_verify.f90 @@ -0,0 +1,22 @@ +program example_verify + use stdlib_string_type + implicit none + type(string_type) :: string + integer :: pos + + string = "fortran" + pos = verify(string, "ao") +! pos == 1 + + pos = verify(string, "fo") +! pos == 3 + + pos = verify(string, "c++") +! pos == 1 + + pos = verify(string, "c++", back=.true.) +! pos == 7 + + pos = verify(string, string) +! pos == 0 +end program example_verify diff --git a/example/stringlist_type/CMakeLists.txt b/example/stringlist_type/CMakeLists.txt new file mode 100644 index 000000000..53da72da5 --- /dev/null +++ b/example/stringlist_type/CMakeLists.txt @@ -0,0 +1,9 @@ +ADD_EXAMPLE(stringlist_type_clear) +ADD_EXAMPLE(stringlist_type_concatenate_operator) +ADD_EXAMPLE(stringlist_type_constructor) +ADD_EXAMPLE(stringlist_type_equality_operator) +ADD_EXAMPLE(stringlist_type_fidx_bidx) +ADD_EXAMPLE(stringlist_type_get) +ADD_EXAMPLE(stringlist_type_inequality_operator) +ADD_EXAMPLE(stringlist_type_insert_at) +ADD_EXAMPLE(stringlist_type_len) diff --git a/example/stringlist_type/example_stringlist_type_clear.f90 b/example/stringlist_type/example_stringlist_type_clear.f90 new file mode 100644 index 000000000..1834a6320 --- /dev/null +++ b/example/stringlist_type/example_stringlist_type_clear.f90 @@ -0,0 +1,19 @@ +program example_clear + use stdlib_stringlist_type, only: stringlist_type, fidx + implicit none + + type(stringlist_type) :: stringlist + +!> inserting 2 elements to the stringlist + call stringlist%insert_at(fidx(1), "Element No. one") + call stringlist%insert_at(fidx(1), "Element No. two") +! stringlist <-- {"Element No. two", "Element No. one"} + + call stringlist%clear() +! stringlist <-- { } (empty stringlist) + +!> inserting 1 element to the stringlist + call stringlist%insert_at(fidx(1), "Element No. one") +! stringlist <-- {"Element No. one"} + +end program example_clear diff --git a/example/stringlist_type/example_stringlist_type_concatenate_operator.f90 b/example/stringlist_type/example_stringlist_type_concatenate_operator.f90 new file mode 100644 index 000000000..104e7a9b8 --- /dev/null +++ b/example/stringlist_type/example_stringlist_type_concatenate_operator.f90 @@ -0,0 +1,27 @@ +program example_concatenate_operator + use stdlib_stringlist_type, only: stringlist_type, operator(//) + use stdlib_string_type, only: string_type + implicit none + + type(stringlist_type) :: first_stringlist, second_stringlist + type(string_type), allocatable :: stringarray(:) + + first_stringlist = first_stringlist//"Element No. one" +! first_stringlist <-- {"Element No. one"} + + second_stringlist = string_type("Element No. two")//first_stringlist +! second_stringlist <-- {Element No. two, "Element No. one"} + +!> Creating an array of 2 string_type elements + stringarray = [string_type("Element No. three"), string_type("Element No. four")] + + second_stringlist = first_stringlist//stringarray +! second_stringlist <-- {"Element No. one", "Element No. three", "Element No. four"} + + second_stringlist = ["#1", "#2"]//second_stringlist +! second_stringlist <-- {"#1", "#2", "Element No. one", "Element No. three", "Element No. four"} + + first_stringlist = first_stringlist//second_stringlist +! first_stringlist <-- {"Element No. one", "#1", "#2", "Element No. one", "Element No. three", "Element No. four"} + +end program example_concatenate_operator diff --git a/example/stringlist_type/example_stringlist_type_constructor.f90 b/example/stringlist_type/example_stringlist_type_constructor.f90 new file mode 100644 index 000000000..f4369eb85 --- /dev/null +++ b/example/stringlist_type/example_stringlist_type_constructor.f90 @@ -0,0 +1,17 @@ +program example_constructor + use stdlib_stringlist_type, only: stringlist_type + use stdlib_string_type, only: string_type + implicit none + + type(stringlist_type) :: stringlist + + stringlist = stringlist_type() +! stringlist <-- { } (empty stringlist) + + stringlist = stringlist_type(["#1", "#2", "#3"]) +! stringlist <-- {"#1", "#2", "#3"} + + stringlist = stringlist_type([string_type("#1"), string_type("#2")]) +! stringlist <-- {"#1", "#2"} + +end program example_constructor diff --git a/example/stringlist_type/example_stringlist_type_equality_operator.f90 b/example/stringlist_type/example_stringlist_type_equality_operator.f90 new file mode 100644 index 000000000..dcc46804f --- /dev/null +++ b/example/stringlist_type/example_stringlist_type_equality_operator.f90 @@ -0,0 +1,29 @@ +program example_equality_operator + use stdlib_stringlist_type, only: stringlist_type, fidx, list_head, operator(==) + use stdlib_string_type, only: string_type + implicit none + + type(stringlist_type) :: stringlist + type(string_type), allocatable :: stringarray(:) + logical :: res + +!> inserting 4 elements to the stringlist + call stringlist%insert_at(fidx(1), "#1") + call stringlist%insert_at(list_head, "#2") + call stringlist%insert_at(fidx(1), "#3") + call stringlist%insert_at(list_head, "#4") +! stringlist <-- {"#4", "#3", "#2", "#1"} + +!> creating an array of 4 string_type elements + stringarray = [string_type("#4"), string_type("#3"), string_type("#2"), string_type("#1")] + + res = (stringarray == stringlist) +! res <-- .true. + + res = (stringlist == ["#4", "#3", "#2", "#1"]) +! res <-- .true. + + print'(a)', stringlist == ["#4", "#3", "#1"] +! .false. + +end program example_equality_operator diff --git a/example/stringlist_type/example_stringlist_type_fidx_bidx.f90 b/example/stringlist_type/example_stringlist_type_fidx_bidx.f90 new file mode 100644 index 000000000..9a6ec5e85 --- /dev/null +++ b/example/stringlist_type/example_stringlist_type_fidx_bidx.f90 @@ -0,0 +1,13 @@ +program example_fidx_bidx + use stdlib_stringlist_type, only: stringlist_index_type, fidx, bidx + implicit none + + type(stringlist_index_type) :: index + + index = fidx(1) +! forward index 1 + + index = bidx(3) +! backward index 3 + +end program example_fidx_bidx diff --git a/example/stringlist_type/example_stringlist_type_get.f90 b/example/stringlist_type/example_stringlist_type_get.f90 new file mode 100644 index 000000000..612292290 --- /dev/null +++ b/example/stringlist_type/example_stringlist_type_get.f90 @@ -0,0 +1,28 @@ +program example_get + use stdlib_stringlist_type, only: stringlist_type, fidx, bidx + use stdlib_string_type, only: string_type + implicit none + + type(stringlist_type) :: stringlist + type(string_type) :: output + +!> inserting 4 elements to the stringlist + call stringlist%insert_at(fidx(1), "Element No. one") + call stringlist%insert_at(fidx(1), "Element No. two") + call stringlist%insert_at(fidx(1), "Element No. three") + call stringlist%insert_at(fidx(1), "Element No. four") +! stringlist <-- {"Element No. four", "Element No. three", "Element No. two", "Element No. one"} + + output = stringlist%get(fidx(1)) +! output <-- "Element No. four" + + output = stringlist%get(bidx(1)) +! output <-- "Element No. one" + +!> accessing out of bounds index + output = stringlist%get(bidx(5)) +! output <-- "" + output = stringlist%get(fidx(0)) +! output <-- "" + +end program example_get diff --git a/example/stringlist_type/example_stringlist_type_inequality_operator.f90 b/example/stringlist_type/example_stringlist_type_inequality_operator.f90 new file mode 100644 index 000000000..49f64369d --- /dev/null +++ b/example/stringlist_type/example_stringlist_type_inequality_operator.f90 @@ -0,0 +1,29 @@ +program example_inequality_operator + use stdlib_stringlist_type, only: stringlist_type, bidx, list_tail, operator(/=) + use stdlib_string_type, only: string_type + implicit none + + type(stringlist_type) :: stringlist + type(string_type), allocatable :: stringarray(:) + logical :: res + +!> inserting 4 elements to the stringlist + call stringlist%insert_at(bidx(1), "#1") + call stringlist%insert_at(list_tail, "#2") + call stringlist%insert_at(bidx(1), "#3") + call stringlist%insert_at(list_tail, "#4") +! stringlist <-- {"#1", "#2", "#3", "#4"} + +!> creating an array of 4 string_type elements + stringarray = [string_type("#1"), string_type("#2"), string_type("#3"), string_type("#4")] + + res = (stringarray /= stringlist) +! res <-- .false. + + res = (stringlist /= ["#111", "#222", "#333", "#444"]) +! res <-- .true. + + print'(a)', stringlist /= ["#4", "#3", "#1"] +! .true. + +end program example_inequality_operator diff --git a/example/stringlist_type/example_stringlist_type_insert_at.f90 b/example/stringlist_type/example_stringlist_type_insert_at.f90 new file mode 100644 index 000000000..2b0e99a60 --- /dev/null +++ b/example/stringlist_type/example_stringlist_type_insert_at.f90 @@ -0,0 +1,23 @@ +program example_insert_at + use stdlib_stringlist_type, only: stringlist_type, stringlist_index_type, fidx, bidx + use stdlib_string_type, only: string_type + implicit none + + type(stringlist_type) :: stringlist + type(stringlist_index_type) :: index + + index = fidx(1) + call stringlist%insert_at(index, "Element No. one") +! stringlist <-- {"Element No. one"} + + index = bidx(1) + call stringlist%insert_at(index, string_type("Element No. two")) +! stringlist <-- {"Element No. one", "Element No. two"} + + call stringlist%insert_at(fidx(2), string_type("Element No. three")) +! stringlist <-- {"Element No. one", "Element No. three", "Element No. two"} + + call stringlist%insert_at(bidx(1), "Element No. four") +! stringlist <-- {"Element No. one", "Element No. three", "Element No. two", "Element No. four"} + +end program example_insert_at diff --git a/example/stringlist_type/example_stringlist_type_len.f90 b/example/stringlist_type/example_stringlist_type_len.f90 new file mode 100644 index 000000000..e0afb008b --- /dev/null +++ b/example/stringlist_type/example_stringlist_type_len.f90 @@ -0,0 +1,19 @@ +program example_len + use stdlib_stringlist_type, only: stringlist_type, bidx + implicit none + + type(stringlist_type) :: stringlist + integer :: output + + output = stringlist%len() +! output <-- 0 + +!> inserting 2 elements to the stringlist + call stringlist%insert_at(bidx(1), "Element No. one") + call stringlist%insert_at(bidx(1), "Element No. two") +! stringlist <-- {"Element No. one", "Element No. two"} + + print'(a)', stringlist%len() +! 2 + +end program example_len diff --git a/example/strings/CMakeLists.txt b/example/strings/CMakeLists.txt new file mode 100644 index 000000000..6cc750765 --- /dev/null +++ b/example/strings/CMakeLists.txt @@ -0,0 +1,11 @@ +ADD_EXAMPLE(chomp) +ADD_EXAMPLE(count) +ADD_EXAMPLE(ends_with) +ADD_EXAMPLE(find) +ADD_EXAMPLE(padl) +ADD_EXAMPLE(padr) +ADD_EXAMPLE(replace_all) +ADD_EXAMPLE(slice) +ADD_EXAMPLE(starts_with) +ADD_EXAMPLE(strip) +ADD_EXAMPLE(to_string) diff --git a/example/strings/example_chomp.f90 b/example/strings/example_chomp.f90 new file mode 100644 index 000000000..dfd0e3fdc --- /dev/null +++ b/example/strings/example_chomp.f90 @@ -0,0 +1,14 @@ +program example_chomp + use stdlib_ascii, only: TAB, VT, LF, CR, FF + use stdlib_strings, only: chomp + implicit none + print'(a)', chomp(" hello ") ! " hello" + print'(a)', chomp(TAB//"goodbye"//CR//LF) ! "\tgoodbye" + print'(a)', chomp(" "//TAB//LF//VT//FF//CR) ! "" + print'(a)', chomp(" ! ")//"!" ! " !!" + print'(a)', chomp("Hello") ! "Hello" + print'(a)', chomp("hello", ["l", "o"]) ! "he" + print'(a)', chomp("hello", set=["l", "o"]) ! "he" + print'(a)', chomp("hello", "lo") ! "hel" + print'(a)', chomp("hello", substring="lo") ! "hel" +end program example_chomp diff --git a/example/strings/example_count.f90 b/example/strings/example_count.f90 new file mode 100644 index 000000000..5d02e4859 --- /dev/null +++ b/example/strings/example_count.f90 @@ -0,0 +1,13 @@ +program example_count + use stdlib_string_type, only: string_type, assignment(=) + use stdlib_strings, only: count + implicit none + type(string_type) :: string + + string = "How much wood would a woodchuck chuck if a woodchuck could chuck wood?" + + print *, count(string, "wood") ! 4 + print *, count(string, ["would", "chuck", "could"]) ! [1, 4, 1] + print *, count("a long queueueueue", "ueu", [.false., .true.]) ! [2, 4] + +end program example_count diff --git a/example/strings/example_ends_with.f90 b/example/strings/example_ends_with.f90 new file mode 100644 index 000000000..e7c2c31da --- /dev/null +++ b/example/strings/example_ends_with.f90 @@ -0,0 +1,6 @@ +program example_ends_with + use stdlib_strings, only: ends_with + implicit none + print'(a)', ends_with("pattern", "ern") ! T + print'(a)', ends_with("pattern", "pat") ! F +end program example_ends_with diff --git a/example/strings/example_find.f90 b/example/strings/example_find.f90 new file mode 100644 index 000000000..c389458a9 --- /dev/null +++ b/example/strings/example_find.f90 @@ -0,0 +1,13 @@ +program example_find + use stdlib_string_type, only: string_type, assignment(=) + use stdlib_strings, only: find + implicit none + type(string_type) :: string + + string = "needle in the character-stack" + + print *, find(string, "needle") ! 1 + print *, find(string, ["a", "c"], [3, 2]) ! [27, 20] + print *, find("qwqwqwq", "qwq", 3, [.false., .true.]) ! [0, 5] + +end program example_find diff --git a/example/strings/example_padl.f90 b/example/strings/example_padl.f90 new file mode 100644 index 000000000..0e0f72ee0 --- /dev/null +++ b/example/strings/example_padl.f90 @@ -0,0 +1,15 @@ +program example_padl + use stdlib_string_type, only: string_type, assignment(=), write (formatted) + use stdlib_strings, only: padl + implicit none + type(string_type) :: string + + string = "left pad this string" +! string <-- "left pad this string" + + print '(dt)', padl(string, 25, "$") ! "$$$$$left pad this string" + + string = padl(string, 25) +! string <-- " left pad this string" + +end program example_padl diff --git a/example/strings/example_padr.f90 b/example/strings/example_padr.f90 new file mode 100644 index 000000000..0c567335e --- /dev/null +++ b/example/strings/example_padr.f90 @@ -0,0 +1,15 @@ +program example_padr + use stdlib_string_type, only: string_type, assignment(=), write (formatted) + use stdlib_strings, only: padr + implicit none + type(string_type) :: string + + string = "right pad this string" +! string <-- "right pad this string" + + print '(dt)', padr(string, 25, "$") ! "right pad this string$$$$" + + string = padr(string, 25) +! string <-- "right pad this string " + +end program example_padr diff --git a/example/strings/example_replace_all.f90 b/example/strings/example_replace_all.f90 new file mode 100644 index 000000000..dc293a771 --- /dev/null +++ b/example/strings/example_replace_all.f90 @@ -0,0 +1,16 @@ +program example_replace_all + use stdlib_string_type, only: string_type, assignment(=), write (formatted) + use stdlib_strings, only: replace_all + implicit none + type(string_type) :: string + + string = "hurdles here, hurdles there, hurdles everywhere" +! string <-- "hurdles here, hurdles there, hurdles everywhere" + + print'(dt)', replace_all(string, "hurdles", "learn from") +! "learn from here, learn from there, learn from everywhere" + + string = replace_all(string, "hurdles", "technology") +! string <-- "technology here, technology there, technology everywhere" + +end program example_replace_all diff --git a/example/strings/example_slice.f90 b/example/strings/example_slice.f90 new file mode 100644 index 000000000..d4516b08c --- /dev/null +++ b/example/strings/example_slice.f90 @@ -0,0 +1,20 @@ +program example_slice + use stdlib_string_type + use stdlib_strings, only: slice + implicit none + type(string_type) :: string + character(len=10) :: chars + + string = "abcdefghij" +! string <-- "abcdefghij" + + chars = "abcdefghij" +! chars <-- "abcdefghij" + + print'(a)', slice("abcdefghij", 2, 6, 2) ! "bdf" + print'(a)', slice(chars, 2, 6, 2) ! "bdf" + + string = slice(string, 2, 6, 2) +! string <-- "bdf" + +end program example_slice diff --git a/example/strings/example_starts_with.f90 b/example/strings/example_starts_with.f90 new file mode 100644 index 000000000..ab388dbee --- /dev/null +++ b/example/strings/example_starts_with.f90 @@ -0,0 +1,6 @@ +program example_starts_with + use stdlib_strings, only: starts_with + implicit none + print'(a)', starts_with("pattern", "pat") ! T + print'(a)', starts_with("pattern", "ern") ! F +end program example_starts_with diff --git a/example/strings/example_strip.f90 b/example/strings/example_strip.f90 new file mode 100644 index 000000000..4f0d3eae8 --- /dev/null +++ b/example/strings/example_strip.f90 @@ -0,0 +1,10 @@ +program example_strip + use stdlib_ascii, only: TAB, VT, LF, CR, FF + use stdlib_strings, only: strip + implicit none + print'(a)', strip(" hello ") ! "hello" + print'(a)', strip(TAB//"goodbye"//CR//LF) ! "goodbye" + print'(a)', strip(" "//TAB//LF//VT//FF//CR) ! "" + print'(a)', strip(" ! ")//"!" ! "!!" + print'(a)', strip("Hello") ! "Hello" +end program example_strip diff --git a/example/strings/example_to_string.f90 b/example/strings/example_to_string.f90 new file mode 100644 index 000000000..0cb3f5872 --- /dev/null +++ b/example/strings/example_to_string.f90 @@ -0,0 +1,31 @@ +program example_to_string + use stdlib_strings, only: to_string + +!> Example for `complex` type + print *, to_string((1, 1)) !! "(1.00000000,1.00000000)" + print *, to_string((1, 1), '(F6.2)') !! "( 1.00, 1.00)" + print *, to_string((1000, 1), '(ES0.2)'), to_string((1000, 1), '(SP,F6.3)') +!! "(1.00E+3,1.00)""(******,+1.000)" +!! Too narrow formatter for real number +!! Normal demonstration(`******` from Fortran Standard) + +!> Example for `integer` type + print *, to_string(-3) !! "-3" + print *, to_string(42, '(I4)') !! " 42" + print *, to_string(1, '(I0.4)'), to_string(2, '(B4)') !! "0001"" 10" + +!> Example for `real` type + print *, to_string(1.) !! "1.00000000" + print *, to_string(1., '(F6.2)') !! " 1.00" + print *, to_string(1., 'F6.2') !! " 1.00" + print *, to_string(1., '(SP,ES9.2)'), to_string(1, '(F7.3)') !! "+1.00E+00""[*]" +!! 1 wrong demonstration (`[*]` from `to_string`) + +!> Example for `logical` type + print *, to_string(.true.) !! "T" + print *, to_string(.true., '(L2)') !! " T" + print *, to_string(.true., 'L2') !! " T" + print *, to_string(.false., '(I5)') !! "[*]" +!! 1 wrong demonstrations(`[*]` from `to_string`) + +end program example_to_string diff --git a/example/version/CMakeLists.txt b/example/version/CMakeLists.txt new file mode 100644 index 000000000..303a38b53 --- /dev/null +++ b/example/version/CMakeLists.txt @@ -0,0 +1 @@ +ADD_EXAMPLE(version) diff --git a/example/version/example_version.f90 b/example/version/example_version.f90 new file mode 100644 index 000000000..e7382ce6d --- /dev/null +++ b/example/version/example_version.f90 @@ -0,0 +1,7 @@ +program example_version + use stdlib_version, only: get_stdlib_version + implicit none + character(len=:), allocatable :: version + call get_stdlib_version(string=version) + print '(a)', version +end program example_version diff --git a/src/stdlib_bitsets.fypp b/src/stdlib_bitsets.fypp index f4cd7013e..0605f2792 100644 --- a/src/stdlib_bitsets.fypp +++ b/src/stdlib_bitsets.fypp @@ -151,7 +151,7 @@ module stdlib_bitsets !!#### Example !! !!```fortran -!! program demo_all +!! program example_all !! use stdlib_bitsets !! character(*), parameter :: & !! bits_all = '111111111111111111111111111111111' @@ -167,7 +167,7 @@ module stdlib_bitsets !! write(*,*) "FROM_STRING transferred BITS_ALL properly" // & !! " into set0." !! end if -!! end program demo_all +!! end program example_all !!``` import :: bitset_type logical :: all @@ -182,7 +182,7 @@ module stdlib_bitsets !!#### Example !! !!```fortran -!! program demo_any +!! program example_any !! use stdlib_bitsets !! character(*), parameter :: & !! bits_0 = '0000000000000000000' @@ -196,7 +196,7 @@ module stdlib_bitsets !! if ( set0 % any() ) then !! write(*,*) "ANY interpreted SET0's value properly." !! end if -!! end program demo_any +!! end program example_any !!``` import :: bitset_type logical :: any @@ -211,7 +211,7 @@ module stdlib_bitsets !!#### Example !! !!```fortran -!! program demo_bit_count +!! program example_bit_count !! use stdlib_bitsets !! character(*), parameter :: & !! bits_0 = '0000000000000000000' @@ -225,7 +225,7 @@ module stdlib_bitsets !! if ( set0 % bit_count() == 1 ) then !! write(*,*) "BIT_COUNT interpreted SET0's value properly." !! end if -!! end program demo_bit_count +!! end program example_bit_count !!``` import :: bitset_type, bits_kind integer(bits_kind) :: bit_count @@ -241,7 +241,7 @@ module stdlib_bitsets !!#### Example !! !!```fortran -!! program demo_clear +!! program example_clear !! use stdlib_bitsets !! type(bitset_large) :: set0 !! call set0 % init(166) @@ -251,7 +251,7 @@ module stdlib_bitsets !! if ( .not. set0 % test(165) ) write(*,*) 'Bit 165 is cleared.' !! call set0 % clear(0,164) !! if ( set0 % none() ) write(*,*) 'All bits are cleared.' -!! end program demo_clear +!! end program example_clear !!``` import :: bitset_type, bits_kind class(bitset_type), intent(inout) :: self @@ -279,7 +279,7 @@ module stdlib_bitsets !!#### Example !! !!```fortran -!! program demo_flip +!! program example_flip !! use stdlib_bitsets !! type(bitset_large) :: set0 !! call set0 % init(166) @@ -288,7 +288,7 @@ module stdlib_bitsets !! if ( set0 % test(165) ) write(*,*) 'Bit 165 is flipped.' !! call set0 % flip(0,164) !! if ( set0 % all() ) write(*,*) 'All bits are flipped.' -!! end program demo_flip +!! end program example_flip !!``` import :: bitset_type, bits_kind class(bitset_type), intent(inout) :: self @@ -319,7 +319,7 @@ module stdlib_bitsets !!#### Example !! !!```fortran -!! program demo_from_string +!! program example_from_string !! use stdlib_bitsets !! character(*), parameter :: & !! bits_all = '111111111111111111111111111111111' @@ -335,7 +335,7 @@ module stdlib_bitsets !! write(*,*) "FROM_STRING transferred BITS_ALL properly" // & !! " into set0." !! end if -!! end program demo_from_string +!! end program example_from_string !!``` import :: bitset_type class(bitset_type), intent(out) :: self @@ -356,14 +356,14 @@ module stdlib_bitsets !!#### Example !! !!```fortran -!! program demo_init +!! program example_init !! use stdlib_bitsets !! type(bitset_large) :: set0 !! call set0 % init(166) !! if ( set0 % bits() == 166 ) & !! write(*,*) `SET0 has the proper size.' !! if ( set0 % none() ) write(*,*) 'SET0 is properly initialized.' -!! end program demo_init +!! end program example_init !!``` import :: bitset_type, bits_kind class(bitset_type), intent(out) :: self @@ -387,7 +387,7 @@ module stdlib_bitsets !!#### Example !! !!```fortran -!! program demo_input +!! program example_input !! character(*), parameter :: & !! bits_0 = '000000000000000000000000000000000', & !! bits_1 = '000000000000000000000000000000001', & @@ -416,7 +416,7 @@ module stdlib_bitsets !! write(*,*) 'Transfer to and from units using ' // & !! 'output and input succeeded.' !! end if -!! end program demo_input +!! end program example_input !!``` import :: bitset_type class(bitset_type), intent(out) :: self @@ -432,7 +432,7 @@ module stdlib_bitsets !!#### Example !! !!```fortran -!! program demo_none +!! program example_none !! use stdlib_bitsets !! character(*), parameter :: & !! bits_0 = '0000000000000000000' @@ -446,7 +446,7 @@ module stdlib_bitsets !! if ( .not. set0 % none() ) then !! write(*,*) "NONE interpreted SET0's value properly." !! end if -!! end program demo_none +!! end program example_none !!``` import :: bitset_type logical :: none @@ -461,7 +461,7 @@ module stdlib_bitsets !!#### Example !! !!```fortran -!! program demo_not +!! program example_not !! use stdlib_bitsets !! type(bitset_large) :: set0 !! call set0 % init( 155 ) @@ -473,7 +473,7 @@ module stdlib_bitsets !! if ( set0 % all() ) then !! write(*,*) "ALL interpreted SET0's value properly." !! end if -!! end program demo_not +!! end program example_not !!``` import :: bitset_type class(bitset_type), intent(inout) :: self @@ -491,7 +491,7 @@ module stdlib_bitsets !!#### Example !! !!```fortran -!! program demo_output +!! program example_output !! character(*), parameter :: & !! bits_0 = '000000000000000000000000000000000', & !! bits_1 = '000000000000000000000000000000001', & @@ -520,7 +520,7 @@ module stdlib_bitsets !! write(*,*) 'Transfer to and from units using ' // & !! 'output and input succeeded.' !! end if -!! end program demo_output +!! end program example_output !!``` import :: bitset_type class(bitset_type), intent(in) :: self @@ -550,7 +550,7 @@ module stdlib_bitsets !!#### Example !! !!```fortran -!! program demo_read_bitset +!! program example_read_bitset !! character(*), parameter :: & !! bits_0 = 'S33B000000000000000000000000000000000', & !! bits_1 = 'S33B000000000000000000000000000000001', & @@ -582,7 +582,7 @@ module stdlib_bitsets !! if ( set3 == set0 .and. set4 == set1 .and. set5 == set2 ) then !! write(*,*) WRITE_BITSET to READ_BITSET through unit worked.' !! end if -!! end program demo_read_bitset +!! end program example_read_bitset !!``` import :: bitset_type class(bitset_type), intent(out) :: self @@ -630,7 +630,7 @@ module stdlib_bitsets !!#### Example !! !!```fortran -!! program demo_set +!! program example_set !! use stdlib_bitsets !! type(bitset_large) :: set0 !! call set0 % init(166) @@ -639,7 +639,7 @@ module stdlib_bitsets !! if ( set0 % test(165) ) write(*,*) 'Bit 165 is set.' !! call set0 % set(0,164) !! if ( set0 % all() ) write(*,*) 'All bits are set.' -!! end program demo_set +!! end program example_set !!``` import :: bitset_type, bits_kind class(bitset_type), intent(inout) :: self @@ -666,7 +666,7 @@ module stdlib_bitsets !!#### Example !! !!```fortran -!! program demo_test +!! program example_test !! use stdlib_bitsets !! type(bitset_large) :: set0 !! call set0 % init(166) @@ -676,7 +676,7 @@ module stdlib_bitsets !! if ( .not. set0 % test(165) ) write(*,*) 'Bit 165 is cleared.' !! call set0 % set(165) !! if ( set0 % test(165) ) write(*,*) 'Bit 165 is set.' -!! end program demo_test +!! end program example_test !!``` import :: bitset_type, bits_kind logical :: test @@ -693,7 +693,7 @@ module stdlib_bitsets !!#### Example !! !!```fortran -!! program demo_to_string +!! program example_to_string !! use stdlib_bitsets !! character(*), parameter :: & !! bits_all = '111111111111111111111111111111111' @@ -706,7 +706,7 @@ module stdlib_bitsets !! write(*,*) "TO_STRING transferred BITS0 properly" // & !! " into NEW_STRING." !! end if -!! end program demo_to_string +!! end program example_to_string !!``` import :: bitset_type class(bitset_type), intent(in) :: self @@ -723,7 +723,7 @@ module stdlib_bitsets !!#### Example !! !!```fortran -!! program demo_value +!! program example_value !! use stdlib_bitsets !! type(bitset_large) :: set0 !! call set0 % init(166) @@ -733,7 +733,7 @@ module stdlib_bitsets !! if ( set0 % value(165) == 0 ) write(*,*) 'Bit 165 is cleared.' !! call set0 % set(165) !! if ( set0 % value(165) == 1 ) write(*,*) 'Bit 165 is set.' -!! end program demo_value +!! end program example_value !!``` import :: bitset_type, bits_kind integer :: value @@ -754,7 +754,7 @@ module stdlib_bitsets !!#### Example !! !!```fortran -!! program demo_write_bitset +!! program example_write_bitset !! character(*), parameter :: & !! bits_0 = 'S33B000000000000000000000000000000000', & !! bits_1 = 'S33B000000000000000000000000000000001', & @@ -786,7 +786,7 @@ module stdlib_bitsets !! if ( set3 == set0 .and. set4 == set1 .and. set5 == set2 ) then !! write(*,*) WRITE_BITSET to READ_BITSET through unit worked.' !! end if -!! end program demo_write_bitset +!! end program example_write_bitset !!``` import :: bitset_type class(bitset_type), intent(in) :: self @@ -1140,7 +1140,7 @@ module stdlib_bitsets !!#### Example !! !!```fortran -!! program demo_assignment +!! program example_assignment !! use stdlib_bitsets !! logical(int8) :: logical1(64) = .true. !! logical(int32), allocatable :: logical2(:) @@ -1163,7 +1163,7 @@ module stdlib_bitsets !! if ( all( logical2 ) ) then !! write(*,*) 'Initialization of logical(int32) succeeded.' !! end if -!! end program demo_assignment +!! end program example_assignment !!``` pure module subroutine assign_large( set1, set2 ) @@ -1552,7 +1552,7 @@ module stdlib_bitsets !!#### Example !! !!```fortran -!! program demo_and +!! program example_and !! use stdlib_bitsets !! type(bitset_large) :: set0, set1 !! call set0 % init(166) @@ -1568,7 +1568,7 @@ module stdlib_bitsets !! call set0 % not() !! call and( set0, set1 ) ! all all !! if ( all(set0) ) write(*,*) 'Fourth test of AND worked.' -!! end program demo_and +!! end program example_and !!``` elemental module subroutine and_large(set1, set2) type(bitset_large), intent(inout) :: set1 @@ -1595,7 +1595,7 @@ module stdlib_bitsets !!#### Example !! !!```fortran -!! program demo_and_not +!! program example_and_not !! use stdlib_bitsets !! type(bitset_large) :: set0, set1 !! call set0 % init(166) @@ -1612,7 +1612,7 @@ module stdlib_bitsets !! call set0 % not() !! call and_not( set0, set1 ) ! all all !! if ( none(set0) ) write(*,*) 'Fourth test of AND_NOT worked.' -!! end program demo_and_not +!! end program example_and_not !!``` elemental module subroutine and_not_large(set1, set2) @@ -1641,7 +1641,7 @@ module stdlib_bitsets !!#### Example !! !!```fortran -!! program demo_extract +!! program example_extract !! use stdlib_bitsets !! type(bitset_large) :: set0, set1 !! call set0 % init(166) @@ -1650,7 +1650,7 @@ module stdlib_bitsets !! if ( set1 % bits() == 51 ) & !! write(*,*) 'SET1 has the proper size.' !! if ( set1 % all() ) write(*,*) 'SET1 has the proper values.' -!! end program demo_extract +!! end program example_extract !!``` module subroutine extract_large(new, old, start_pos, stop_pos, status) @@ -1681,7 +1681,7 @@ module stdlib_bitsets !!#### Example !! !!```fortran -!! program demo_or +!! program example_or !! use stdlib_bitsets !! type(bitset_large) :: set0, set1 !! call set0 % init(166) @@ -1698,7 +1698,7 @@ module stdlib_bitsets !! call set0 % not() !! call or( set0, set1 ) ! all all !! if ( all(set0) ) write(*,*) 'Fourth test of OR worked.' -!! end program demo_or +!! end program example_or !!``` elemental module subroutine or_large(set1, set2) type(bitset_large), intent(inout) :: set1 @@ -1724,7 +1724,7 @@ module stdlib_bitsets !!#### Example !! !!```fortran -!! program demo_xor +!! program example_xor !! use stdlib_bitsets !! type(bitset_large) :: set0, set1 !! call set0 % init(166) @@ -1741,7 +1741,7 @@ module stdlib_bitsets !! call set0 % not() !! call xor( set0, set1 ) ! all all !! if ( none(set0) ) write(*,*) 'Fourth test of XOR worked.' -!! end program demo_xor +!! end program example_xor !!``` elemental module subroutine xor_large(set1, set2) type(bitset_large), intent(inout) :: set1 @@ -1767,7 +1767,7 @@ module stdlib_bitsets !!#### Example !! !!```fortran -!! program demo_equality +!! program example_equality !! use stdlib_bitsets !! type(bitset_64) :: set0, set1, set2 !! call set0 % init( 33 ) @@ -1782,7 +1782,7 @@ module stdlib_bitsets !! else !! error stop 'Failed 64 bit equality tests.' !! end if -!! end program demo_equality +!! end program example_equality !!``` elemental module function eqv_large(set1, set2) result(eqv) logical :: eqv @@ -1808,7 +1808,7 @@ module stdlib_bitsets !!#### Example !! !!```fortran -!! program demo_inequality +!! program example_inequality !! use stdlib_bitsets !! type(bitset_64) :: set0, set1, set2 !! call set0 % init( 33 ) @@ -1823,7 +1823,7 @@ module stdlib_bitsets !! else !! error stop 'Failed 64 bit inequality tests.' !! end if -!! end program demo_inequality +!! end program example_inequality !!``` elemental module function neqv_large(set1, set2) result(neqv) logical :: neqv @@ -1850,7 +1850,7 @@ module stdlib_bitsets !!#### Example !! !!```fortran -!! program demo_gt +!! program example_gt !! use stdlib_bitsets !! type(bitset_64) :: set0, set1, set2 !! call set0 % init( 33 ) @@ -1865,7 +1865,7 @@ module stdlib_bitsets !! else !! error stop 'Failed 64 bit greater than tests.' !! end if -!! end program demo_gt +!! end program example_gt !!``` elemental module function gt_large(set1, set2) result(gt) logical :: gt @@ -1892,7 +1892,7 @@ module stdlib_bitsets !!#### Example !! !!```fortran -!! program demo_ge +!! program example_ge !! use stdlib_bitsets !! type(bitset_64) :: set0, set1, set2 !! call set0 % init( 33 ) @@ -1908,7 +1908,7 @@ module stdlib_bitsets !! else !! error stop 'Failed 64 bit greater than or equals tests.' !! end if -!! end program demo_ge +!! end program example_ge !!``` elemental module function ge_large(set1, set2) result(ge) logical :: ge @@ -1935,7 +1935,7 @@ module stdlib_bitsets !!#### Example !! !!```fortran -!! program demo_lt +!! program example_lt !! use stdlib_bitsets !! type(bitset_64) :: set0, set1, set2 !! call set0 % init( 33 ) @@ -1950,7 +1950,7 @@ module stdlib_bitsets !! else !! error stop 'Failed 64 bit less than tests.' !! end if -!! end program demo_lt +!! end program example_lt !!``` elemental module function lt_large(set1, set2) result(lt) logical :: lt @@ -1977,7 +1977,7 @@ module stdlib_bitsets !!#### Example !! !!```fortran -!! program demo_le +!! program example_le !! use stdlib_bitsets !! type(bitset_64) :: set0, set1, set2 !! call set0 % init( 33 ) @@ -1993,7 +1993,7 @@ module stdlib_bitsets !! else !! error stop 'Failed 64 bit less than or equal tests.' !! end if -!! end program demo_le +!! end program example_le !!``` elemental module function le_large(set1, set2) result(le) logical :: le