9

Possible Duplicate:
What is the most convenient way to read definitions of in-memory symbols when we don’t have the source files?

I would like to look at the code that implements FillingTransform. Does Mathematica provide any way to do this?

williewonka
  • 91
  • 1
  • 2
  • 2
    I think in general the answer must be no. Mathematica is not open source software. Sometimes the help documentation will reveal more, but with FillingTransform this seems not to be the case. – Jacob Akkerboom Jan 03 '13 at 13:45
  • 1
    In general, any function that has attribute ReadProtected is mainly implemented in Mathematica code and you can access its source by removing the attribute (as in Oleks' answer). There are of course those readprotected functions that simply call another function that is fully concealed. There is little hope that someone is able to reverse-engineer these functions just by observing their behaviour. – István Zachar Jan 03 '13 at 17:07

1 Answers1

20

In general, @Jacob Akkerboom is correct: one cannot usually view the source code for arbitrary Mathematica functions. In this particular case, however, we find an exception.

By doing:

FillingTransform (* trigger auto-load *)
ClearAttributes[FillingTransform, ReadProtected]
Begin["Image`MorphologicalOperationsDump`"]

Information[FillingTransform]

we observe a call to iFillingTransform (i.e. the implementation of FillingTransform; this naming convention is commonly encountered among recently added functions). Thereafter,

Information[iFillingTransform]

reveals a number of checks on the arguments and setup for a call to oFillingTransform. If one does

Information[oFillingTransform]

then the complete implementation becomes visible. Although I won't reproduce that here for obvious reasons, the basic idea is a combination of GeodesicErosion and GeodesicDilation with some image arithmetic to mask off each hole in turn.

Oleksandr R.
  • 23,023
  • 4
  • 87
  • 125
  • 1
    Hiho hiho... been spelunking again? – Yves Klett Jan 03 '13 at 14:33
  • There are more functions which might be analysed this way. Could you find them all or most of them ? – Artes Jan 03 '13 at 18:39
  • 1
    @Artes Have you looked at Simon's answer in the linked duplicate? I find it so useful that it's in my inits – rm -rf Jan 03 '13 at 20:27
  • @R. M. Thanks for your advice. Indeed, Simon's answer is very handy. Nevertheless, what could I do if I'd like to collect all those functions for which Spelunk yielded interesting information ? Mapping it like here Spelunk /@ ToExpression@Names["System*"]` is not a good idea. – Artes Jan 03 '13 at 20:47
  • 1
    @Artes Well, that would require a definition of "interesting", right? :) Barring any contextual information that might be of interest to the programmer, how do you programmatically gauge what's worth looking into in the entire system? Do you go by the number of down-values defined or the character length of the definition or something else? My experience has been that I only find useful internal stuff when I'm in need of something and go spelunking. Most of the time, even simple code looks boring under the hood, because of all the checks for invalid user input, which adds to the bloat. – rm -rf Jan 03 '13 at 21:45
  • @R. M. Definitions might be helpful, however I fill I've found something here Spelunk[TensorExpand] while definitely not found anything here Spelunk[FrobeniusSolve]. The latter is a quite generic behaviour. – Artes Jan 03 '13 at 21:55
  • @R. M., thanks for the recommendation :-) You prompted me to update the answer - now you can Spelunk recursively... – Simon Woods Jan 03 '13 at 23:37
  • @Oleksandr, do you know the meaning of the 'o' at the front of oFillingTransform? Perhaps overload, operator, or object, in the context of a Function Object? Assuming the 'i' in iFillingTransform is short for implementation, as you mentioned, then I'm curious if this naming convention implies some design pattern considerations which WRI staff tend to utilize for writing packages. I would appreciate any additional thoughts anyone may have on this issue? Would it be worth opening a question on this topic? – rfrasier Mar 04 '16 at 03:40
  • @rfrasier-mlp I don't know, sorry. You can certainly ask a question, although the answer may only be available from WRI, and might not be particularly useful to us as users. I doubt if it has anything to do with object-orientation or related concepts, as not only is this not an accepted paradigm in Mathematica, but the functions using this naming convention do not seem to make any attempt to venture into the world of objects either. – Oleksandr R. Mar 04 '16 at 07:54