AllowedHeads

AllowedHeads

is an option that specifies the heads of subexpressions into which a function may descend.

Details

  • Different functions use different default settings for AllowedHeads.
  • Settings for the AllowedHeads option include:
  • Automaticdefault settings
    Inheritedallow only Head[expr]
    Allallow any normal expression head
    hallow only head h
    {h1,}allow any of h1,
    h1|h2|treat the hi as interchangeable
    Fulltreat all normal expression heads as interchangeable
  • For AllowedHeads, a normal expression head is the head of an expression that is not AtomQ, Association or the head of a special array object such as SparseArray, NumericArray and QuantityArray.
  • For functions such as Dimensions, the setting AllowedHeads->{h1,h2,} requires that all elements at a given level have the same head. AllowedHeads->{,g1|g2|,} allows any of the gi as heads at a given level.

Examples

open allclose all

Basic Examples  (4)

Only consider nested expressions with head List:

By default, nested expressions with any head are considered arrays:

Only consider nested expressions with head List:

By default, rectangular nested expressions with any head are considered arrays:

Sum inside the outermost head:

Sum inside both f and g:

Scope  (3)

By default, Total only sums inside list representations and associations:

Sum inside several specific heads:

Sum inside any head:

Allow any head at each level of the array:

An array has a consistent head at each level:

Completely ignore heads, looking only at the tree structure of the expression:

Treat the heads g and h as the same head:

Without this stronger equivalence, the expression is considered one-dimensional:

Applications  (1)

Create a version of MatrixQ that only accepts list matrices:

Create several expressions to test:

The function matrixQ gives True only for listMat, which both is a matrix and has head List:

MatrixQ gives True for matrices of any known array type:

Properties & Relations  (2)

Once a head is excluded, no nested expressions are tested against the value of AllowedHeads:

If both f and List are allowed, then the inner list is tested and summed:

The meaning of AllowedHeads->Automatic may vary from function to function:

Introduced in 2019
 (12.0)
 |
Updated in 2020
 (12.1)