From APL Wiki
Jump to navigation Jump to search

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.


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
      (/x)  ⍴,x           ⍝ Or the product of the original shape

Ravelling a scalar yields a singleton vector.

      3  ,3                  ⍝ Not the same
      ⍴,3                     ⍝ It's now a vector

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
      ≢⍴ ,'a'                 ⍝ String

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 20
5 12 2
       ,[2.5] 5 4 3 20
5 4 1 3 2
Works in: Dyalog APL


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.


Ravel was present in the first version of APL\360[1] and has been included in every APL since.

External links




  1. 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 Primitive functionPrimitive operatorQuad name
Array model ShapeRankDepthBoundIndexAxisRavelRavel orderElementScalarVectorMatrixSimple scalarSimple arrayNested arrayBoxCellMajor cellSubarrayEmpty arrayPrototype
Concepts and paradigms Leading axis theoryScalar extensionConformabilityScalar functionPervasionGlyphIdentity elementTotal array ordering
APL built-ins [edit]
Primitive functions
Monadic ConjugateNegateSignumNotRollType
Dyadic AddSubtractEqual to (Xnor) ∙ Not Equal to (Xor) ∙ MinimumMaximumComparison functions
Structural ShapeReshapeTallyDepthRavelReverseRazeMixCut (K)PairReplicatePartitioned Enclose
Selection TakeDropUniqueIdentitySelect
Selector Index generatorInterval IndexIndices
Computational MatchNot MatchNub SieveFormatExecute
Primitive operators Monadic EachReplicate
Dyadic Reverse Compose
Quad names
Arrays Index originMigration level
Other ZildeHigh minusFunction axis