ArrayTools
IsMonotonic
determine if a 1-D list or rtable is decreasing, increasing, non-decreasing, or non-increasing
Calling Sequence
Parameters
Options
Description
Examples
Compatibility
IsMonotonic( data )
IsMonotonic( data, section )
data
-
1-D list or rtable of data of type realcons
section
(optional) range of the form a..b, a.., ..b, or .., where a and b are integers. The default is .., which corresponds to the entire container.
direction: Either increasing or decreasing. The default is increasing.
strict: Either true or false. The default is true.
compiled: Either true or false. The default is false.
The IsMonotonic command determines if a 1-D list or rtable, or a section of it, is monotonic (increasing, non-decreasing, decreasing, or non-increasing).
If direction=increasing and strict=true, then the result is true if data[i+1]>data[i] for all valid indices i, and false otherwise.
If direction=increasing and strict=false, then the result is true if data[i+1]>=data[i] for all valid indices i, and false otherwise.
If direction=decreasing and strict=true, then the result is true if data[i+1]<data[i] for all valid indices i, and false otherwise.
If direction=decreasing and strict=false, then the result is true if data[i+1]<=data[i] for all valid indices i, and false otherwise.
If data has fewer than two elements, or the section contains fewer than two elements, the result is vacuously true.
If data is not an Array with lower index different than 1, you can use valid negative indices in section for counting backward. For example, if data is a Vector with 15 elements, then 5..-2 is a valid range for section (equivalent to 5..14), with -2 indicating the second element from the end. However, if data is an Array with lower index different than 1, you can only use valid literal indices.
Suppose the working precision of the session is larger than that of hardware floats (i.e. Digits>evalhf(Digits)), or use of hardware floats has been disabled (i.e. UseHardwareFloats=false). Here, IsMonotonic will always use the uncompiled version of the subcommand used to check the specific variety of monotonicity.
Suppose Digits<=evalhf(Digits) and UseHardwareFloats<>false. Here, IsMonotonic will use the compiled version of the subcommand used to check the specific variety of monotonicity when either the subcommand has already been compiled or compiled=true (the command will be compiled at first execution). On the other hand, if the subcommand has not yet been compiled and compiled=false, then the subcommand will be called using evalhf mode.
Use of the compiled=true option is recommended when either a very long container or numerous smaller containers are to be scanned. In these scenarios, the cost of the overhead is likely worth it.
with⁡ArrayTools:
Example 1
Strictly increasing list:
A≔5,10,15,20
dataplot⁡A,view=0..5,0..20,color=blue
IsMonotonic⁡A,direction=increasing,strict=true
true
IsMonotonic⁡A,direction=decreasing,strict=true
false
Example 2
Increasing but not strictly increasing:
B≔5,10,10,20
dataplot⁡B,view=0..5,0..20,color=blue
IsMonotonic⁡B,direction=increasing,strict=true
IsMonotonic⁡B,direction=increasing,strict=false
Example 3
Not monotonic on the whole, but monotonic on subsections:
C≔Array⁡2,3,4,5,4,3,2,3,4,5
C≔2345432345
dataplot⁡C,view=0..10,0..5,color=blue
IsMonotonic⁡C,direction=increasing,strict=false
IsMonotonic⁡C,..4,direction=increasing,strict=true
IsMonotonic⁡C,4..7,direction=decreasing,strict=true
IsMonotonic⁡C,7..−1,direction=increasing,strict=true
Example 4
Data can include infinity:
L≔−∞,0,∞
IsMonotonic⁡L,direction=increasing
IsMonotonic⁡L,direction=decreasing
Example 5
Consider the following long Array:
data≔Array⁡1..100000,i↦i,datatype=float8:
The Array is strictly increasing, and confirmation using the IsMonotonic command requires checking the elements all the way to the end.
First, check monotonicity with compile=false:
CodeTools:-Usage⁡IsMonotonic⁡data,compiled=false
memory used=3.63KiB, alloc change=0 bytes, cpu time=18.00ms, real time=18.00ms, gc time=0ns
Second, check with the compiled=true option, which triggers the procedure to compile for the strictly increasing case:
CodeTools:-Usage⁡IsMonotonic⁡data,compiled=true
memory used=6.60MiB, alloc change=32.00MiB, cpu time=238.00ms, real time=258.00ms, gc time=0ns
Finally, since compilation has already occurred, the check is much quicker when using either compiled=true or compiled=false:
memory used=3.52KiB, alloc change=0 bytes, cpu time=1000.00us, real time=1000.00us, gc time=0ns
memory used=3.83KiB, alloc change=0 bytes, cpu time=1000.00us, real time=1000.00us, gc time=0ns
The ArrayTools[IsMonotonic] command was introduced in Maple 2021.
For more information on Maple 2021 changes, see Updates in Maple 2021.
See Also
CodeTools
Compiler
dataplot
Download Help Document
What kind of issue would you like to report? (Optional)