Re: Threading over matrices
- To: mathgroup at smc.vnet.net
- Subject: [mg90772] Re: [mg90741] Threading over matrices
- From: Murray Eisenberg <murray at math.umass.edu>
- Date: Wed, 23 Jul 2008 06:00:06 -0400 (EDT)
- Organization: Mathematics & Statistics, Univ. of Mass./Amherst
- References: <200807220757.DAA13797@smc.vnet.net>
- Reply-to: murray at math.umass.edu
What should x > y mean when x and y are matrices (or just lists of
numbers, or higher-dimensional tables)?
I presume that, for your If[x > y,...] example, you would NOT, in fact,
want x > y for matrices simply to act element by element, since you'd
just get another matrix of True / False answers, and that would hardly
be a suitable first argument for an If.
Such an elementwise comparison, with result again a matrix, would be the
natural analog of what happens with +, in other words, giving > the
Attribute of being Listable. So I doubt that you really want what you
seem to suggest!
So perhaps you'd expect the result to be a single True or False -- True
in case all the individual, elementwise comparisons give True. That's
easy to make happen:
And @@ Flatten[Thread /@ Thread[x > y]]
But maybe you, or somebody else using such a computation, wants
something else, namely whether all the elements of SOME row of x are
greater than all the elements of the corresponding row of y. Or ditto
for columns instead of rows.
So it would seem that one reason for Mathematica NOT automatically
threading > over lists and matrices is that there is no clear-cut
semantics for the result.
A deeper answer is that the overall language design of Mathematica does
not facilitate such things. The trouble begins with the fact that
matrices and higher-dimensional tables are just lists of lists, or lists
of lists of lists, etc., rather than primitive types of objects. For
easier, nearly automatic treatment of the sort of thing you are asking,
you would really want a language where arbitrary dimension "arrays" are
primitive kinds of objects; where functions have "rank"; and where you
can change at will the rank upon which the functions operate. There are
such languages (J is one). But I know of no such language that also has
the symbolic capabilities of Mathematica.
"Robert <"@frank-exchange-of-views.oucs.ox.ac.uk wrote:
> How can I get evaluations to thread over matrices with
> conditional functions?
> Here's examples that show the behaviour that's really
> frustrating me.
> Create a couple of matrices:
>
> x = Table[Random[],{3},{4}];
> y = Table[Random[],{3},{4}];
>
> [Some functions act element-by-element] But some don't, e.g.
>
> x > y
> x > a
>
> I would have liked those to produce a matrix of corresponding
> True and False results, and then something like:
>
> If[x > y, 1/x, x - y]
> Piecewise[{{1,x==a},{x^2,x>a}},x y^2]
>
> to produce a matrix of results corresponding to each element.
>
> They don't - I haven't managed to find out why they don't or
> more usefully how to do what I would like them to do.
--
Murray Eisenberg murray at math.umass.edu
Mathematics & Statistics Dept.
Lederle Graduate Research Tower phone 413 549-1020 (H)
University of Massachusetts 413 545-2859 (W)
710 North Pleasant Street fax 413 545-1801
Amherst, MA 01003-9305
- References:
- Threading over matrices
- From: "Robert <"@frank-exchange-of-views.oucs.ox.ac.uk
- Threading over matrices