Message Boards Message Boards

0
|
2376 Views
|
2 Replies
|
4 Total Likes
View groups...
Share
Share this post:

Why does my pure function fail in this case?

I create a matrix.

basis = {{1, 0}, {0, 1}}

Which I want to become a graphic. This code works fine

Graphics[{Blue, Rectangle[basis[[1]], basis[[2]]]}, Axes -> True, 
 PlotRange -> {{0, 10}, {0, 10}}]

But this does not, although it seems to me to be the same

Graphics[{Blue, Rectangle[#1, #2] & /@ basis}, Axes -> True, 
 PlotRange -> {{0, 10}, {0, 10}}]
POSTED BY: Roger J Brown
2 Replies
Posted 1 year ago

First off, what you're probably wanting is something like this:

Graphics[{Blue, Rectangle @@ basis}, Axes -> True, PlotRange -> {{0, 10}, {0, 10}}]

Let's examine what's going on. Consider this:

f[#1, #2] & /@ {a, b}

We have a function we're mapping over a list. Each element of the list gets fed to the function in turn. So, we get something like this right off the bat:

f[#1, #2] &[a]

creates an error message and results in this:

f[a, #2]

So, it had something it could put in the first slot, but not the second. Now, it doesn't matter if the elements are themselves lists, they still count as "one thing" in terms of arguments:

f[#1, #2] & /@ {{1, 2}, {3, 4}}

results in an error message and

{f[{1, 2}, #2], f[{3, 4}, #2]}

Again, you can see that it's able to fill the first slot but not the second. You can deduce from this that a form like

f[#1, #2] & /@ list

will always "fail" for any list with no level 1 elements.

Alternatives to consider are Apply and MapApply. Compare

f[#1, #2] & @@ basis

with

f[#1, #2] & @@@ basis
POSTED BY: Eric Rimbey

I'm sure others can explain this better, but as I understand it, the function Rectangle requires two inputs: two pairs of x-y coordinates. However, when you map over basis, you're inputing one pair of coordinates at a time to the function. Try this:

Graphics[{Blue, Rectangle[#[[1]], #[[2]]] &[basis]}, Axes -> True, 
 PlotRange -> {{0, 10}, {0, 10}}]
POSTED BY: John Shonder
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