NAME

flatten - returns a 1D vector containing every element of an array

SYNOPSIS

arr.flatten()

DESCRIPTION

The flatten() method recursively traverses all elements of the source arr and returns a single 1D vector containing those elements. If arr contains nested arrays or lists, their elements are also expanded into the result. The order of elements in the result follows a depth-first traversal of the source array.

If all elements encountered are numeric, the result will be a packed array of a type sufficient to hold all elements (e.g., if any element is a Float, the result will be a PackFloat array). If any non-numeric element is encountered, the result will be a List.

RETURN VALUE

A 1D vector (either a packed array or a List) containing all elements found in arr.

ERRORS

TypeCheck if the method is called on a non-array type.
ShapeCheck if the nesting depth exceeds internal limits.

EXAMPLE

    a = {{1, 2}, 3.5, {4, 5}}
    b = a.flatten()
    // b is now [1.0, 2.0, 3.5, 4.0, 5.0] (a PackFloat array)

    c = {{1, 2}, "hello", {3, 4}}
    d = c.flatten()
    // d is now {1, 2, "hello", 3, 4} (a List)

SEE ALSO

reshape
ravel