0
$\begingroup$

Can anyone explain two examples from the Mathematica Documentation Center reference page on Extract? One is:

e = f[g[1, 2], {h[3]}];
Extract[e, {{1, 2}, {2, 1, 1}}]
{2, 3}

Also, what is h[3]?

The other example is:

Extract[<|1 -> 1 + x^2|>, {Key[1], 2}]
x^2
$\endgroup$
3
$\begingroup$

To understand extract you need to understand an expression tree and for such a tree, the notion of a position. Extract extracts a part of an expression based on its position. The positions in the first example you give are {1, 2} and {2, 1, 1}.

{1, 2}, the position of the integer 2 in the expression f[g[1, 2], {h[3]}].

{2, 1, 1}, the position of the integer 3 in the same expression.

Together these positions determine the two integers, 2 and 3, which are combined in a list: {2, 3}.

As suggested: first read up on how expressions are represented as trees. Positions indicate how to find a part of the expression by specifying how to go down the root of the tree to the desired part of the expression.

The expression tree includes for instance the integers listed in the example but also "heads" (look up Mathematica Expressions).

Only after this can you understand Extract, which retrieves a part based on its position.

h[3] has no significance other than that it is part of the expression that is given in the example. So the fact that you are asking about this seems to indicate you need to look up expressions first.

| improve this answer | |
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.