Prefix: Difference between revisions

From APL Wiki
Jump to navigation Jump to search
Miraheze>Marshall
(Created page with "A '''prefix''' of a vector is another vector which is no longer than that vector, and shares all of its leading elements. In leading axis theory, an array may be c...")
 
(Prefix and Suffix vectors now have a page)
 
(8 intermediate revisions by 5 users not shown)
Line 1: Line 1:
A '''prefix''' of a [[vector]] is another vector which is no longer than that vector, and shares all of its leading [[element]]s. In [[leading axis theory]], an array may be considered a prefix of another array of the same rank if the same relation holds with [[major cell]]s in place of elements. The function [[Take]] produces prefixes of its right argument as long as the left argument is no greater than that argument's length ([[Tally]]). A vector is a prefix of another if and only if its [[Reverse]] is a [[suffix]] of that vector's reverse.
A '''prefix''' of a [[vector]] is another vector which is no longer than that vector, and shares all of its leading [[element]]s. In [[leading axis theory]], an array may be considered a prefix of another array of the same rank if the same relation holds with [[major cell]]s in place of elements. Consequently, a prefix is a kind of [[subarray]]. The function [[Take]] produces prefixes of its right argument as long as the left argument is non-negative and no greater than that argument's length ([[Tally]]). A vector is a prefix of another if and only if its [[Reverse]] is a [[suffix]] of that vector's reverse.


A vector is a prefix of another vector if its length is [[Less than or Equal to]] that vector's length, and every [[element]] in the prefix vector [[match]]es the corresponding element of the other vector (the one at the same [[index]]). This may be tested using [[Take]] and [[Match]]:
A vector is a prefix of another vector if its length is [[Less than or Equal to]] that vector's length, and every [[element]] in the prefix vector [[match]]es the corresponding element of the other vector (the one at the same [[index]]). This may be tested using [[Take]] and [[Match]]:
<source lang=apl>
<syntaxhighlight lang=apl>
       isPrefix ← {(≢⍺)≤(≢⍵) ∧ ⍺≡(≢⍺)↑⍵}
       isPrefix ← {((≢⍺)≤(≢⍵)) ∧ ⍺≡(≢⍺)↑⍵}
       'pre' isPrefix 'prefix'
       'pre' isPrefix 'prefix'
1
1
Line 10: Line 10:
       'pre  ' isPrefix 'pre'
       'pre  ' isPrefix 'pre'
0
0
</source>
</syntaxhighlight>


In [[leading axis theory]], the [[frame]] for k-cells of an array is a prefix of that array's [[shape]], while the [[cell shape]] is a [[suffix]].
In [[leading axis theory]], the [[frame]] for k-cells of an array is a prefix of that array's [[shape]], while the [[cell shape]] is a [[suffix]].


[[Iverson notation]] included the notion of a prefix vector <math>\alpha^j(n)</math> consisting of <math>j</math> ones followed by <math>n-j</math> zeros; such a vector could be used to produce a length-<math>j</math> prefix of a length-<math>n</math> vector using [[Compress]].
[[Iverson notation]] and very early APLs included the notion of a [[prefix vector]] <math>\alpha^j(n)</math> consisting of <math>j</math> ones followed by <math>n-j</math> zeros; such a vector could be used to produce a length-<math>j</math> prefix of a length-<math>n</math> vector using [[Compress]].


In [[Dyalog APL's]] [[Total array ordering]], a prefix of another array always orders earlier than that array; this is a consequence of the principly "nothing is less than something" used as the foundation for Dyalog's TAO.
In [[Dyalog APL]]'s [[Total array ordering]], a prefix of another array always orders earlier than that array; this is a consequence of the principally "nothing is less than something" used as the foundation for Dyalog's TAO.
 
[[Category:Array relationships]]

Latest revision as of 01:18, 2 March 2024

A prefix of a vector is another vector which is no longer than that vector, and shares all of its leading elements. In leading axis theory, an array may be considered a prefix of another array of the same rank if the same relation holds with major cells in place of elements. Consequently, a prefix is a kind of subarray. The function Take produces prefixes of its right argument as long as the left argument is non-negative and no greater than that argument's length (Tally). A vector is a prefix of another if and only if its Reverse is a suffix of that vector's reverse.

A vector is a prefix of another vector if its length is Less than or Equal to that vector's length, and every element in the prefix vector matches the corresponding element of the other vector (the one at the same index). This may be tested using Take and Match:

      isPrefix ← {((≢⍺)≤(≢⍵)) ∧ ⍺≡(≢⍺)↑⍵}
      'pre' isPrefix 'prefix'
1
      'pre' isPrefix 'suffix'
0
      'pre  ' isPrefix 'pre'
0

In leading axis theory, the frame for k-cells of an array is a prefix of that array's shape, while the cell shape is a suffix.

Iverson notation and very early APLs included the notion of a prefix vector consisting of ones followed by zeros; such a vector could be used to produce a length- prefix of a length- vector using Compress.

In Dyalog APL's Total array ordering, a prefix of another array always orders earlier than that array; this is a consequence of the principally "nothing is less than something" used as the foundation for Dyalog's TAO.