IsSubsequence - Maple Help
For the best experience, we recommend viewing online help using Google Chrome or Microsoft Edge.

Online Help

All Products    Maple    MapleSim


ArrayTools

  

IsSubsequence

  

determine if one 1-D container is a subsequence of another 1-D container

 

Calling Sequence

Parameters

Options

Description

Examples

Compatibility

Calling Sequence

IsSubsequence( A, B, options )

Parameters

A, B

-

1-D rtables or lists

options

-

(optional) equation(s) of the form keyword = value, where keyword is one of digits, direction, match, output, relativeerror, or ulp

Options

• 

digits: Positive integer, specifies the working precision used for floating-point calculations and comparisons. The default is Digits.

• 

direction: Either forward or reverse, specifies if scanning is to be performed in the forward direction or in reverse. The default is forward.

• 

match: Specifies how matches are to be determined. There are a few options:

– 

exact: Matches have to be exact to be counted. This is the default.

– 

float: Matches are determined using floating-point comparisons.

– 

regexp: Matches strings using regular expressions.

– 

wildcard: Matches strings using wildcards.

– 

An expression of type callable (e.g. procedure). If match=f, then f(a,b), where a is a value in A and b is a value in B, must return either true (for a match) or false.

– 

List of the form [callable,...]. If match=[f,rest], where rest is the expression sequence of remaining elements in the list, then f(a,b,rest), where a is a value in A and b is a value in B, must return either true (for a match) or false.

• 

relativeerror: Either true or false, specifies if floating-point errors are to be measured in absolute or relative terms. The default is true.

• 

ulp: Positive integer, specifies the number of units in the last place to be used for floating-point comparisons. The default is 1.

• 

output: The type of output. The supported options are:

– 

check: Either true or false, the result of checking if A is a subsequence of B. This is the default.

– 

indices: List of integers for the indices of matches found while checking if A is a subsequence of B.

– 

list of any of the above options: Returns an expression sequence with the corresponding outputs, in the same order.

Description

• 

The IsSubsequence command determines if A is a subsequence of B, where A and B are 1-D rtables or lists. Here, A is a subsequence of B when there is a list K of strictly increasing indices such that B[K]=A.

• 

When direction=forward, elements of B are scanned for matches from left-to-right. Similarly, when direction=reverse, elements of B are scanned from right-to-left.

• 

When output includes indices, the indices of matches encountered in B are returned, even if it is determined that A is not a subsequence of B.

• 

When match=float, all data must be coercible to software or hardware floats, and comparisons are made with the digits, relativeerror, and ulp options providing flexibility and tolerance. When Digits<=evalhf(Digits) and UseHardwareFloats<>false, rtables with hardware floats and external code will be employed. Note that in this case, it is most efficient to pass numeric data in containers that already have the same hardware datatype (either float[8] or complex[8]).

• 

Comparisons when match=regexp and match=wildcard are made, respectively, with the StringTools:-RegMatch and StringTools:-WildcardMatch commands.

  

When B is a 1-D Array with initial index different than 1 and output includes indices, the indices list corresponds to the indices of B as opposed to an alias of B with initial index 1.

  

When A is empty, A is considered to be a subsequence of B.

• 

The command works fastest when:

– 

match is exact and either the data consists solely of integers or the size of A is much smaller than the size of B; or

– 

match is float.

Examples

withArrayTools&colon;

Example 1

A1Array1&comma;3&comma;5

A1135

(1)

A2Array2&comma;4&comma;6

A2246

(2)

BArray1&comma;2&comma;3&comma;4&comma;5

B12345

(3)

IsSubsequenceA1&comma;B

true

(4)

IsSubsequenceA2&comma;B

false

(5)

Example 2

IsSubsequencea&comma;b&comma;a&comma;b&comma;c

true

(6)

IsSubsequenceb&comma;a&comma;a&comma;b&comma;c

false

(7)

Example 3

• 

Scanning can be performed from the left and from the right, and the indices returned can depend on the choice:

AVectorcolumn2&comma;4

A24

(8)

BVectorrowseq1..10&comma;seq1..10

B1234567891012345678910

(9)

IsSubsequenceA&comma;B&comma;direction=forward

true

(10)

IsSubsequenceA&comma;B&comma;direction=forward&comma;output=indices

2&comma;4

(11)

IsSubsequenceA&comma;B&comma;direction=reverse

true

(12)

IsSubsequenceA&comma;B&comma;direction=reverse&comma;output=indices

12&comma;14

(13)

Example 4

• 

Indices can be returned even when the first container is not a subsequence of the second:

BArray27&comma;3&comma;18&comma;21&comma;15&comma;12&comma;30&comma;9&comma;24&comma;6

B27318211512309246

(14)

A1Array3&comma;15&comma;9

A13159

(15)

IsSubsequenceA1&comma;B&comma;output=check&comma;indices

true,2&comma;5&comma;8

(16)

A2Array18&comma;30&comma;21

A2183021

(17)

IsSubsequenceA2&comma;B&comma;direction=forward&comma;output=check&comma;indices

false,3&comma;7

(18)

IsSubsequenceA2&comma;B&comma;direction=reverse&comma;output=check&comma;indices

false,4

(19)

Example 5

• 

Custom comparisons can be passed:

A1.1&comma;2.8&comma;5.3

A1.1&comma;2.8&comma;5.3

(20)

B1&comma;2&comma;3&comma;4&comma;5

B1&comma;2&comma;3&comma;4&comma;5

(21)

IsSubsequenceA&comma;B

false

(22)

f := proc( u, v ) evalb( abs( u - v ) < 0.3 ); end proc;

fprocu&comma;vevalbabsuv<0.3end proc

(23)

g := proc( u, v ) evalb( abs( u - v ) <= 0.3 ); end proc;

gprocu&comma;vevalbabsuv<=0.3end proc

(24)

h := proc( u, v, w ) evalb( abs( u - v ) <= w ); end proc;

hprocu&comma;v&comma;wevalbabsuv<=wend proc

(25)

IsSubsequenceA&comma;B&comma;match=f

false

(26)

IsSubsequenceA&comma;B&comma;match=g

true

(27)

IsSubsequenceA&comma;B&comma;match=h&comma;0.29

false

(28)

IsSubsequenceA&comma;B&comma;match=h&comma;0.30

true

(29)

Example 6

• 

Comparisons for strings can be made with wildcards:

A1a*&comma;c*&comma;e*

A1a*&comma;c*&comma;e*

(30)

A2a*&comma;C*&comma;e*

A2a*&comma;C*&comma;e*

(31)

Baaa&comma;bbb&comma;ccc&comma;ddd&comma;eee

Baaa&comma;bbb&comma;ccc&comma;ddd&comma;eee

(32)

IsSubsequenceA1&comma;B&comma;match=wildcard

true

(33)

IsSubsequenceA2&comma;B&comma;match=wildcard

false

(34)

Example 7

• 

Comparisons for strings can be made with regular expressions:

Aa[0-1]&comma;a[2-3]&comma;a[4-5]&comma;a[6-7]&comma;a[8-9]

Aa[0-1]&comma;a[2-3]&comma;a[4-5]&comma;a[6-7]&comma;a[8-9]

(35)

Bseqcata&comma;i&comma;i=0..9

Ba0&comma;a1&comma;a2&comma;a3&comma;a4&comma;a5&comma;a6&comma;a7&comma;a8&comma;a9

(36)

IsSubsequenceA&comma;B&comma;match=regexp&comma;direction=forward&comma;output=check&comma;indices

true,1&comma;3&comma;5&comma;7&comma;9

(37)

IsSubsequenceA&comma;B&comma;match=regexp&comma;direction=reverse&comma;output=check&comma;indices

true,2&comma;4&comma;6&comma;8&comma;10

(38)

Example 8

• 

Consider the following containers:

BArrayexp1&comma;π&comma;γ

B&ExponentialE;πγ

(39)

Aevalf5B2&comma;3

A3.14160.57722

(40)
• 

Due to the truncation, A is not a subsequence of B when match=exact:

IsSubsequenceA&comma;B

false

(41)
• 

However, if we use match=float with an appropriate choice for digits, we can confirm that A is a subsequence of B:

IsSubsequenceA&comma;B&comma;match=float

false

(42)

IsSubsequenceA&comma;B&comma;match=float&comma;digits=5&comma;ulp=1

true

(43)

Compatibility

• 

The ArrayTools:-IsSubsequence command was introduced in Maple 2023.

• 

For more information on Maple 2023 changes, see Updates in Maple 2023.

See Also

ArrayTools

ArrayTools:-Lookup