Ravel: Difference between revisions
Miraheze>Adám Brudzewsky m (Text replacement - "{{APL programming language}}" to "{{APL features}}") |
m (→Examples) |
||
(8 intermediate revisions by 3 users not shown) | |||
Line 8: | Line 8: | ||
You can use ravel to squash a [[matrix]] down to one dimension. The elements are listed in reading order—left to right, top to bottom. | You can use ravel to squash a [[matrix]] down to one dimension. The elements are listed in reading order—left to right, top to bottom. | ||
< | <syntaxhighlight lang=apl> | ||
⎕←x ← 3 4⍴⍳12 ⍝ A matrix | |||
1 2 3 4 | 1 2 3 4 | ||
5 6 7 8 | 5 6 7 8 | ||
Line 19: | Line 19: | ||
(,×/⍴x) ≡ ⍴,x ⍝ Or the product of the original shape | (,×/⍴x) ≡ ⍴,x ⍝ Or the product of the original shape | ||
1 | 1 | ||
</ | </syntaxhighlight> | ||
Ravelling a [[scalar]] yields a [[singleton]] [[vector]]. | Ravelling a [[scalar]] yields a [[singleton]] [[vector]]. | ||
< | <syntaxhighlight lang=apl> | ||
,3 | ,3 | ||
3 | 3 | ||
Line 29: | Line 29: | ||
⍴,3 ⍝ It's now a vector | ⍴,3 ⍝ It's now a vector | ||
1 | 1 | ||
</ | </syntaxhighlight> | ||
[[String]] notation cannot produce a single-character string since it produces a [[scalar]] character instead. Using Ravel on a list of characters in quotes ensures it will be a [[vector]] of characters. | [[String]] notation cannot produce a single-character string since it produces a [[scalar]] character instead. Using Ravel on a list of characters in quotes ensures it will be a [[vector]] of characters. | ||
< | <syntaxhighlight lang=apl> | ||
≢⍴ 'a' ⍝ Scalar character | ≢⍴ 'a' ⍝ Scalar character | ||
0 | 0 | ||
≢⍴ ,'a' ⍝ String | ≢⍴ ,'a' ⍝ String | ||
1 | 1 | ||
</ | </syntaxhighlight> | ||
=== With axis === | |||
[[Function axis|Axis specification]] may accept either a [[vector]] of two or more adjacent [[axis]] [[Index|indices]], or a single non-integer value. If multiple axes are given, they are merged into one axis whose length is the product of their lengths. If only one value is given, a new axis of length 1 is inserted in the indicated "gap" between axes. | [[Function axis|Axis specification]] may accept either a [[vector]] of two or more adjacent [[axis]] [[Index|indices]], or a single non-integer value. If multiple axes are given, they are merged into one axis whose length is the product of their lengths. If only one value is given, a new axis of length 1 is inserted in the indicated "gap" between axes. | ||
< | <syntaxhighlight lang=apl> | ||
⍴ ,[2 3] 5 4 3 2⍴0 | ⍴ ,[2 3] 5 4 3 2⍴0 | ||
5 12 2 | 5 12 2 | ||
⍴ ,[2.5] 5 4 3 2⍴0 | ⍴ ,[2.5] 5 4 3 2⍴0 | ||
5 4 1 3 2 | 5 4 1 3 2 | ||
</ | </syntaxhighlight> | ||
{{Works in|[[Dyalog APL]]}} | {{Works in|[[Dyalog APL]]}} | ||
== Description == | == Description == | ||
The ravel of an array < | The ravel of an array <syntaxhighlight lang=apl inline>A</syntaxhighlight> has shape <syntaxhighlight lang=apl inline>,×/⍴A</syntaxhighlight> and shares elements with <syntaxhighlight lang=apl inline>A</syntaxhighlight>. Thus Ravel may be modelled as a [[Reshape|reshaping]] function <syntaxhighlight lang=apl inline>{(×/⍴⍵)⍴⍵}</syntaxhighlight>. The [[element]] with [[index]] [[vector]] <syntaxhighlight lang=apl inline>I</syntaxhighlight> is moved to index <syntaxhighlight lang=apl inline>(⍴A)⊥I</syntaxhighlight> in [[index origin]] 0, or <syntaxhighlight lang=apl inline>⎕IO+(⍴A)⊥I-⎕IO</syntaxhighlight> in arbitrary index origin. | ||
As with any reshaping, the result of Ravel has the same [[prototype]] as the argument. | As with any reshaping, the result of Ravel has the same [[prototype]] as the argument. | ||
Line 57: | Line 57: | ||
Ravel was present in the first version of [[APL\360]]<ref>Falkoff, A.D., and K.E. Iverson. [https://www.jsoftware.com/papers/APL360TerminalSystem.htm "The APL\360 Terminal System"]. Research Report RC-1922, IBM, 1967-10-16.</ref> and has been included in every APL since. | Ravel was present in the first version of [[APL\360]]<ref>Falkoff, A.D., and K.E. Iverson. [https://www.jsoftware.com/papers/APL360TerminalSystem.htm "The APL\360 Terminal System"]. Research Report RC-1922, IBM, 1967-10-16.</ref> and has been included in every APL since. | ||
== See also == | |||
* [[Enlist]] | |||
== External links == | == External links == | ||
Line 66: | Line 69: | ||
=== Documentation === | === Documentation === | ||
* [ | * [https://help.dyalog.com/latest/index.htm#Language/Primitive%20Functions/Ravel.htm Dyalog] ([https://help.dyalog.com/latest/index.htm#Language/Primitive%20Functions/Ravel%20with%20Axes.htm with axis]), | ||
* [http://wiki.nars2000.org/index.php/Rho NARS2000] | * [http://wiki.nars2000.org/index.php/Rho NARS2000] | ||
Line 73: | Line 76: | ||
* [https://www.jsoftware.com/help/dictionary/d320.htm J Dictionary], [https://code.jsoftware.com/wiki/Vocabulary/comma J NuVoc] | * [https://www.jsoftware.com/help/dictionary/d320.htm J Dictionary], [https://code.jsoftware.com/wiki/Vocabulary/comma J NuVoc] | ||
* [https://mlochbaum.github.io/BQN/doc/reshape.html BQN] (as Deshape) | |||
== References == | == References == | ||
Line 79: | Line 84: | ||
{{APL features}} | {{APL features}} | ||
{{APL built-ins}} | {{APL built-ins}}[[Category:Primitive functions]] |
Latest revision as of 08:56, 14 February 2024
,
|
Ravel (,
) is a primitive function introduced in APL\360 which returns the ravel of an array. In the APL array model, an array's ravel is the vector containing all its elements in ravel order. It is equivalent to reshaping an array using its bound for the new shape. Reshaping the ravel using the original array's shape restores that array.
In some APLs an axis may be specified for Ravel in order to combine only some axes of an array, or insert a length-1 axis.
The name "ravel" references the process of undoing woven or knitted fabric, thus removing its structure and rendering it linear.
Examples
You can use ravel to squash a matrix down to one dimension. The elements are listed in reading order—left to right, top to bottom.
⎕←x ← 3 4⍴⍳12 ⍝ A matrix 1 2 3 4 5 6 7 8 9 10 11 12 ,x ⍝ Its ravel 1 2 3 4 5 6 7 8 9 10 11 12 ⍴,x ⍝ The shape is the total number of elements 12 (,×/⍴x) ≡ ⍴,x ⍝ Or the product of the original shape 1
Ravelling a scalar yields a singleton vector.
,3 3 3 ≡ ,3 ⍝ Not the same 0 ⍴,3 ⍝ It's now a vector 1
String notation cannot produce a single-character string since it produces a scalar character instead. Using Ravel on a list of characters in quotes ensures it will be a vector of characters.
≢⍴ 'a' ⍝ Scalar character 0 ≢⍴ ,'a' ⍝ String 1
With axis
Axis specification may accept either a vector of two or more adjacent axis indices, or a single non-integer value. If multiple axes are given, they are merged into one axis whose length is the product of their lengths. If only one value is given, a new axis of length 1 is inserted in the indicated "gap" between axes.
⍴ ,[2 3] 5 4 3 2⍴0 5 12 2 ⍴ ,[2.5] 5 4 3 2⍴0 5 4 1 3 2
Description
The ravel of an array A
has shape ,×/⍴A
and shares elements with A
. Thus Ravel may be modelled as a reshaping function {(×/⍴⍵)⍴⍵}
. The element with index vector I
is moved to index (⍴A)⊥I
in index origin 0, or ⎕IO+(⍴A)⊥I-⎕IO
in arbitrary index origin.
As with any reshaping, the result of Ravel has the same prototype as the argument.
History
Ravel was present in the first version of APL\360[1] and has been included in every APL since.
See also
External links
Lessons
Documentation
- BQN (as Deshape)
References
- ↑ Falkoff, A.D., and K.E. Iverson. "The APL\360 Terminal System". Research Report RC-1922, IBM, 1967-10-16.
APL features [edit] | |
---|---|
Built-ins | Primitives (functions, operators) ∙ Quad name |
Array model | Shape ∙ Rank ∙ Depth ∙ Bound ∙ Index (Indexing) ∙ Axis ∙ Ravel ∙ Ravel order ∙ Element ∙ Scalar ∙ Vector ∙ Matrix ∙ Simple scalar ∙ Simple array ∙ Nested array ∙ Cell ∙ Major cell ∙ Subarray ∙ Empty array ∙ Prototype |
Data types | Number (Boolean, Complex number) ∙ Character (String) ∙ Box ∙ Namespace ∙ Function array |
Concepts and paradigms | Conformability (Scalar extension, Leading axis agreement) ∙ Scalar function (Pervasion) ∙ Identity element ∙ Complex floor ∙ Array ordering (Total) ∙ Tacit programming (Function composition, Close composition) ∙ Glyph ∙ Leading axis theory ∙ Major cell search ∙ First-class function |
Errors | LIMIT ERROR ∙ RANK ERROR ∙ SYNTAX ERROR ∙ DOMAIN ERROR ∙ LENGTH ERROR ∙ INDEX ERROR ∙ VALUE ERROR ∙ EVOLUTION ERROR |