Message Boards Message Boards

1
|
4243 Views
|
2 Replies
|
4 Total Likes
View groups...
Share
Share this post:

InterpolatingFunction appears to be "Listable"?

Dear all,

consider an InterpolatingFunction depending on more than on variable:

ClearAll["Global`*"]
data = RandomReal[{-1, 1}, {10, 10}];
func = ListInterpolation[data, {{0, 1}, {0, 1}}];

A regular function call would then be:

func[x, y]
(* Out:  InterpolatingFunction[{{0.`,1.`},{0.`,1.`}},"<>"][x,y]  *)

But if I try a List as argument (I definitely expected no further evaluation!), I get:

func[{x, y}]
(* Out:  {InterpolatingFunction[{{0.`,1.`},{0.`,1.`}},"<>"][x], InterpolatingFunction[{{0.`,1.`},{0.`,1.`}},"<>"][y]}  *)

so, InterpolatingFunction seems to have the attribute Listable, which one does not find when calling

Attributes[InterpolatingFunction]
(*  Out:  {Protected,ReadProtected}  *)

Can anybody give me a hint on where I am missing the point? (On a somewhat larger piece of code it took me quite a while tracking that effect down ...)

Best regards -- Henrik

POSTED BY: Henrik Schachner
2 Replies

Dear Sander,

thank you for your interesting answer! So the bottom line seems to be: Never use List as an argument unless you know for sure how it is going to be evaluated! - OK!

BTW: Your example above

Unprotect[Sin];
ClearAttributes[Sin,Listable]
Sin[{1.,2.,3.}]

does not evaluate in MM v9, so this behavior is quite "new".

Best regards -- Henrik

POSTED BY: Henrik Schachner

If a function does not have an attribute listable, it does not mean that it does not accept lists of input (yes those are three negations in a single sentence). But if a function does have it, like e.g. Sin, you can see that the input is transformed into separate calls:

Trace[Sin[{1., 2., 3.}]]
{Sin[{1.,2.,3.}],{Sin[1.],Sin[2.],Sin[3.]},{Sin[1.],0.841471},{Sin[2.],0.909297},{Sin[3.],0.14112},{0.841471,0.909297,0.14112}}

that 'splitting' is courtesy of the listable attribute… If you, however, have some special numbers, you see that Sin will not apply the listable attribute:

a = {1.0, 2.0, 3.0};
a = Developer`ToPackedArray[a];
Trace[Sin[a]]
{{a,{1.,2.,3.}},Sin[{1.,2.,3.}],{0.841471,0.909297,0.14112}}

it will do them 'all at once'… My guess would be that InterpolatingFunction does exactly the same…

EDIT: To further improve my point, if you remove the Listable attribute from Sin it will still accept lists (it is, though, not a guarantee that all listable functions have this behavior of course):

Unprotect[Sin]
SetAttributes[Sin,Listable]
Attributes[Sin]
Trace[Sin[{1.,2.,3.}]]
ClearAttributes[Sin,Listable]
Attributes[Sin]
Trace[Sin[{1.,2.,3.}]]
POSTED BY: Sander Huisman
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