AtomQ
Details
- You can use AtomQ in a recursive procedure to tell when you have reached the bottom of the tree corresponding to an expression.
- AtomQ gives True for symbols, numbers, strings, and other raw objects, such as sparse arrays.
- AtomQ gives True for any object whose subparts cannot be accessed using functions like Map.
Examples
open all close allBasic Examples (1)
Scope (6)
AtomQ["This is an atom"]AtomQ[ThisIsAnAtom]AtomQ[1.23]Rational numbers appear to have a compound structure:
FullForm[22 / 7]As numbers, they are not subdividable:
AtomQ[22 / 7]The parts can be accessed through Numerator and Denominator:
{Numerator[22 / 7], Denominator[22 / 7]}Complex numbers appear to have a compound structure:
FullForm[1 + 2 I]As numbers, they are not subdividable:
AtomQ[1 + 2 I]The parts can be accessed through Re and Im:
{Re[1 + 2 I], Im[1 + 2 I]}SparseArray objects are atomic raw objects:
s = SparseArray[{i_, i_} -> i, 3]AtomQ[s]Commands that work with SparseArray objects typically do so on the represented array:
a = Normal[s]s[[2, 2]]a[[2, 2]]The FullForm of a SparseArray object is designed to be sufficient to reconstruct the raw object:
FullForm[s]s1 = SparseArray[Automatic, List[3, 3], 0, List[1, List[List[0, 1, 2, 3], List[List[1], List[2], List[3]]], List[1, 2, 3]]]Applications (2)
Find the number of unsubdividable leaves in an expression:
leaves[expr_, OptionsPattern[]] := Block[{c = 0, cl, e},
cl[e_ ? AtomQ] := c++;
cl[e_] := Scan[cl, e, Heads -> OptionValue[Heads]];
cl[expr];
c];
Options[leaves] = {Heads -> True};e = Fold[f, x, Range[5]]leaves[e]This is equivalent to LeafCount:
LeafCount[e]With the option Heads->False, only atoms with no branches are counted:
leaves[e, Heads -> False]This corresponds to the dangling leaves you see with TreeForm:
TreeForm[e]Find the minimum and maximum "depth" of an expression:
depth[expr_] := Block[{mind = Infinity, maxd = 0, burrow},
burrow[e_ ? AtomQ, d_] := (mind = Min[d, mind];maxd = Max[d, maxd]);
burrow[e_, d_] := Scan[burrow[#, d + 1]&, e];
burrow[expr, 0];
{mind, maxd}]depth[1 + x + 2x ^ 2]depth[{{1, 2}, {3, 4}}]Depth gives the maximum depth plus 1:
Depth[1 + x + 2x ^ 2]Properties & Relations (1)
Map[f,expr,{-1}] generally maps f on atoms in expr:
Map[f, 1 + x + 2x ^ 2, {-1}]This is equivalent to the following recursive function:
mapf[e_ ? AtomQ] := f[e];
mapf[head_[args___]] := Apply[head, Table[mapf[arg], {arg, {args}}]]mapf[1 + x + 2x ^ 2]Related Guides
History
Introduced in 1988 (1.0) | Updated in 2003 (5.0)
Text
Wolfram Research (1988), AtomQ, Wolfram Language function, https://reference.wolfram.com/language/ref/AtomQ.html (updated 2003).
CMS
Wolfram Language. 1988. "AtomQ." Wolfram Language & System Documentation Center. Wolfram Research. Last Modified 2003. https://reference.wolfram.com/language/ref/AtomQ.html.
APA
Wolfram Language. (1988). AtomQ. Wolfram Language & System Documentation Center. Retrieved from https://reference.wolfram.com/language/ref/AtomQ.html
BibTeX
@misc{reference.wolfram_2026_atomq, author="Wolfram Research", title="{AtomQ}", year="2003", howpublished="\url{https://reference.wolfram.com/language/ref/AtomQ.html}", note=[Accessed: 13-June-2026]}
BibLaTeX
@online{reference.wolfram_2026_atomq, organization={Wolfram Research}, title={AtomQ}, year={2003}, url={https://reference.wolfram.com/language/ref/AtomQ.html}, note=[Accessed: 13-June-2026]}