If we have a graphics object,
plt = Plot[Sin[x], {x, 0, Pi}]
and we need to retrieve the discrete point data from it. This can be easily done by using Extract and Position,
First@Extract[plt, Most@First@Position[plt, Line]]
Or using Cases,
First@Cases[plt, Line[data_] -> data, Infinity]
However, my question is, might this be done via generic pattern matching? For example, some code like
plt /. (patterns-for-any-nested-or-parallel-heads) __ [ ___, Line[data_], ___] :> data
It might not be difficult to handle the parallel heads, but I do not see a obvious way to deal with the nested heads in a generic manner. Of course we can manually type all the nested heads, but this is rather specific. I am exploring the capacity of pattern matching in Mathematica. Is it possible to figure a general way to make Mathematica does it by itself? Actually I think this might be part of the algorithm under the hood of Cases.
ReplaceAllor do you want to create the pattern that will fulfill theMatchQwith theGraphicsbut where onlyLineis explicitely used. – Kuba May 27 '14 at 19:30Casesis doing without usingCasesbut instead using/.? – mfvonh May 27 '14 at 19:34plt //. _[___, x_Line, ___] :> x– Kuba May 27 '14 at 19:35Cases. I am looking for a way to do this using simple pattern matching only withReplaceAll. – saturasl May 27 '14 at 19:40/.(I'll mull it over.) But concerning your original question, I don't think that is howCasesis implemented. My guess is the algorithm looks more likeReap[Scan[If[MatchQ[#, _Line], Sow[#]] &, plt, \[Infinity]]]. – mfvonh May 27 '14 at 19:58CasesandReplace(All)here. The former walks the expression tree from inside out (leaves first, root last), whileReplaceAllgoes the other way: outside first, then the leaves of the tree last. Yes, one of them can emulate the other, but this difference will always persist. *Also: Your question implies thatReplaceAllis somehow more fundamental ("generic pattern matching") thanCases. I wouldn't say this is the case and I'm pretty sure internally one isn't implemented in terms of the other. – Szabolcs May 27 '14 at 20:44Map,Scanetc. be directed to walk depth-first? – mfvonh May 27 '14 at 22:16Do[Scan[Print, expr, {i}], {i, 0, Depth[expr]}]. – Szabolcs May 27 '14 at 22:22Caseswhich is what I use. AGraphicsobject describes a state machine, and if you want something that captures the structure, use a parser. But, usually that is overkill, so stick withCases. – rcollyer May 28 '14 at 00:41CasesandReplaceAll. Although they all follow the standard evaluation procedure of Mathematica - each expression is evaluated until no further definitions apply - they may be controlled differently by kernel. It is a pity that there is little talk about the internal algorithm. I believe that my question can be solved by using a similiar code like the built-in code forTracewhich has very amazing capabilities with various options, likeTraceAboveandTraceOriginal. – saturasl May 28 '14 at 03:11ExtractandPosition. I like it, because it is a beautiful attack to reveal the internal of Mathematica, to some extent. This is very useful to me, thank you. – saturasl May 28 '14 at 03:22