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

Online Help

All Products    Maple    MapleSim


Home : Support : Online Help : ArrayTools/IsSubsequence

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 will be scanned for matches from left-to-right. Similarly, when direction=reverse, elements of B will be scanned from right-to-left.

• 

When output includes indices, the indices of matches encountered in B will be 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 will correspond 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:

AVector&apos;column&apos;2&comma;4

A24

(8)

BVector&apos;row&apos;seq1..10&comma;seq1..10

B1234567891012345678910

(9)

IsSubsequenceA&comma;B&comma;&apos;direction&apos;&equals;&apos;forward&apos;

true

(10)

IsSubsequenceA&comma;B&comma;&apos;direction&apos;&equals;&apos;forward&apos;&comma;&apos;output&apos;&equals;&apos;indices&apos;

2&comma;4

(11)

IsSubsequenceA&comma;B&comma;&apos;direction&apos;&equals;&apos;reverse&apos;

true

(12)

IsSubsequenceA&comma;B&comma;&apos;direction&apos;&equals;&apos;reverse&apos;&comma;&apos;output&apos;&equals;&apos;indices&apos;

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;&apos;output&apos;&equals;&apos;check&apos;&comma;&apos;indices&apos;

true,2&comma;5&comma;8

(16)

A2Array18&comma;30&comma;21

A2183021

(17)

IsSubsequenceA2&comma;B&comma;&apos;direction&apos;&equals;&apos;forward&apos;&comma;&apos;output&apos;&equals;&apos;check&apos;&comma;&apos;indices&apos;

false,3&comma;7

(18)

IsSubsequenceA2&comma;B&comma;&apos;direction&apos;&equals;&apos;reverse&apos;&comma;&apos;output&apos;&equals;&apos;check&apos;&comma;&apos;indices&apos;

false,4

(19)

Example 5

• 

Consider the following containers:

BArray&ExponentialE;&comma;Pi&comma;gamma

B&ExponentialE;πγ

(20)

Aevalf5B2&comma;3

A3.14160.57722

(21)
• 

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

IsSubsequenceA&comma;B

false

(22)
• 

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;&apos;match&apos;&equals;&apos;float&apos;

false

(23)

IsSubsequenceA&comma;B&comma;&apos;match&apos;&equals;&apos;float&apos;&comma;&apos;digits&apos;&equals;5&comma;&apos;ulp&apos;&equals;1

true

(24)

Example 6

• 

Custom comparisons can be passed:

A1.1&comma;2.8&comma;5.3

A1.1&comma;2.8&comma;5.3

(25)

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

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

(26)

IsSubsequenceA&comma;B

false

(27)

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

fprocu&comma;vevalbabsuv<0.3end proc

(28)

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

gprocu&comma;vevalbabsuv<=0.3end proc

(29)

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

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

(30)

IsSubsequenceA&comma;B&comma;&apos;match&apos;&equals;f

false

(31)

IsSubsequenceA&comma;B&comma;&apos;match&apos;&equals;g

true

(32)

IsSubsequenceA&comma;B&comma;&apos;match&apos;&equals;h&comma;0.29

false

(33)

IsSubsequenceA&comma;B&comma;&apos;match&apos;&equals;h&comma;0.30

true

(34)

Example 7

• 

Comparisons for strings can be made with wildcards:

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

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

(35)

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

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

(36)

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

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

(37)

IsSubsequenceA1&comma;B&comma;&apos;match&apos;&equals;&apos;wildcard&apos;

true

(38)

IsSubsequenceA2&comma;B&comma;&apos;match&apos;&equals;&apos;wildcard&apos;

false

(39)

Example 8

• 

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]

(40)

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

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

(41)

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

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

(42)

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

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

(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