Message Boards Message Boards

How to list the supported functions on a variable?

Posted 2 years ago

Hi, How to find what methods are supported on a variable. In the following code "trainingData" is a variable that holds 60K image and its numeric value in the image.

trainingData = ResourceData["MNIST", "TrainingData"]

If I want to browse the images how to do it. I guessed this might behave as list(https://www.wolfram.com/language/fast-introduction-for-programmers/en/lists/) and tried

trainingData[[5]]

which worked. But how to do this more systematically without guessing. Since in Mathematica everything is a symbol, is there a way to list all functions supported on a variable?

PS: I am new to Mathematica. I have intermediate experience in Matlab and Python. What I am looking for is similar to the "methods" function in Matlab or "dir" function in Python. Thanks

POSTED BY: Bob Rick
4 Replies
Posted 2 years ago

Does the "Rule" function actually supports list syntax. ie. using "[ ]"

Don't think about it as "list syntax". The double square brackets, [[]], is just syntactic sugar. It's just a convenient alternative to Part. Part is just a normal "function" that can be applied to any expression.

Rohit showed you how to inline the selection function. The Last function that it uses is very common. Other useful "destructuring" functions are Head, First, Most, Rest, and Part. Part has some interesting features, including things similar to Python's slice function.

When filtering/searching lists, Select, Cases, and Position are frequently used. Reorganizing lists can be done with GroupBy and GatherBy.

That's just the tip of the iceberg.

POSTED BY: Eric Rimbey
Posted 2 years ago

Thanks, Eric for this wonderful overview.

You can use Head to determine the head of the expression

I was looking for something like this. The idea is to learn about the variable by querying it.

I want to retrieve all the "0"s in the trainingData and store them in another variable. Basically, filter by the label. Looks like this function https://reference.wolfram.com/language/ref/Select.html seems appropriate.

The "Head" method on a single element of "trainingData[[1]]" output it is "Rule". Looks like "lhs" is image and "rhs" is integer value. I could not find the documentation for retrieving the "rhs" of "Rule". By experimentation, I found the list syntax works. ie. "trainingData[[1]][[2]]" outputs "0".

With this knowledge, I could retrieve all the samples containing 0's to a variable.

fzero[s_] := s[[2]] == 0
zerosTrainingData = Select[trainingData, fzero]

Two follow-up questions. 1. Does the "Rule" function actually supports list syntax. ie. using "[ ]". https://reference.wolfram.com/language/ref/Rule.html

  1. Can the same be written without using "fzero". i.e could the same be achieved in a single line.
POSTED BY: Bob Rick
Posted 2 years ago

Hi Bob,

Can the same be written without using "fzero". i.e could the same be achieved in a single line.

trainingData // Select[Last@# == 0 &]

Does the "Rule" function actually supports list syntax. ie. using "[ ]"

r = 1 -> 2
FullForm@r
(* Rule[1, 2] *)

Part[r, 0]
(* Rule *)

Part[r, 1]
(* 1 *)

Part [[ ]] can be used on any non-atomic symbol.

r[[1]]
(* 1 *)
POSTED BY: Rohit Namjoshi
Posted 2 years ago

"If I want to browse the images how to do it". Well, with 60K images, I'm guessing that you don't really want to browse them. Maybe you can clarify what you mean by "browse". Do you want to search, filter, iterate, etc?

"I guessed this might behave as list". There was no reason to guess this. The ResourceData output that you assigned to trainingData was clearly a list. You can see this because it was surrounded by brackets, "{" and "}".

"is there a way to list all functions supported on a variable?" This question makes me think there is a misunderstanding of how Mathematica works. Mathematica is basically an expression evaluation system. The following explanation is very rough, but hopefully helpful. When Mathematica sees an expression, it parses that expression and looks for rewrite rules that match the expression. It does this by using OwnValues, DownValues, and UpValues, and it does this in a very specific order and recursively. At some point, it will find no further evaluation rules and it stops. This might terminate in something that we might interpret as an "answer", like an integer value. But it can also terminate in something that to us looks only partially evaluated. It can also generate messages and errors in the process. So, a short answer to your question is that all functions are supported on all expressions.

Another way to think about this is that Mathematica doesn't have anything analogous to static type checking. So variables don't have types, and so they don't have a specific set of "supported functions". So another answer to your question is simply "no", there is no way to programmatically determine the intended semantics of an expression and thereby know what "functions it supports". However, by defining your own rewrite rules (typically via SetDelayed) and then using pattern matching and/or conditional constraints, you can precisely specify which expressions "work" in the sense of evaluating to a satisfying terminal expression. So there is a sense in which you can realize strong typing for your own custome "types".

You can inspect an expression in various ways. You can use Head to determine the head of the expression, the outermost wrapper, the thing that might sort of seem like a type. FullForm and TreeForm can help you understand the structure of an expression. There are other "functions" that help you extract parts, apply functions to subexpressions, search, etc. So there are many ways to interactively build an understanding of an expression that you're looking at. But if you're wanting something programmatic that will take an arbitrary expression and give you back a list of "things you can do to this expression that will result in a satisfying terminal expression", then this is effectively impossible (not literally impossible, because you have access to all of the DownValues and so forth, but I seriously doubt that this is the kind of thing you were asking about).

If this didn't answer your question, maybe you can provide a fuller example that shows the kind of thing you would like to be able to do.

POSTED BY: Eric Rimbey
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard

Group Abstract Group Abstract