[Date Index]
[Thread Index]
[Author Index]
why are some functions not listable over packed arrays?
*To*: mathgroup at smc.vnet.net
*Subject*: [mg53845] why are some functions not listable over packed arrays?
*From*: barrowes at alum.mit.edu
*Date*: Sat, 29 Jan 2005 06:02:56 -0500 (EST)
*Sender*: owner-wri-mathgroup at wolfram.com
I am trying to make the common numerical logical operators (like Equal,
Or, And, etc.) listable, but when array sizes become large, Equal and
Unequal don't thread properly over matrices.
For example, after
SetAttributes[Equal,Listable]
the following works fine
{2,3,4,5,4}==4
to yield
{False,False,True,False,True}
However, when a matrix has more than 249 elements in it, this no longer
works:
aa = Table[Random[Real, 100], {250}]
aa==50
yields
{list of numbers}=50
Not what I want. However, it works for a Table size of 249 and below.
I looked around a bit and found this has to do with aa defaulting to a
packed array for arrays with > 249 elements. Indeed, when
Developer`PackedArrayQ[aa]
was True, Equal was not Threaded properly over the array. Unequal also
has this problem, though Greater, LessEqual, etc., seem to be fine.
Note that this won't even work when a section of large array aa smaller
than 250 is used:
aa[[Range[10]]]==50
also fails when aa has >249 elements.
I have found two workarounds. The first is to set the default size at
which any array automatically becomes a packed array to infinity by:
Developer`SetSystemOptions["CompileOptions" -> "TableCompileLength" ->
8]
But this has the undesirable side effect of eliminating many speedups
associated with packed array numerical implementations internal to
mathematica.
The other workaround is to make sure that Equal is overloaded
explicitly.
Unprotect[Equal];
Equal[xx,yy]:=Thread[System`Equal[xx,yy]];
Protect[Equal];
For some reason, this works. In fact, explicitly calling
Thread[Equal[aa,50]]
works for large aa even when:
aa==50
does not.
BTW, probably an easy question, but:
Equal[xx_,yy_]:=Thread[System`Equal[xx,yy]];
(with the underscore pattern matcher) will not work. Why?
After redefining Equal as above, however,
aa==50
works for any size array.
Is this a bug?
Prev by Date:
**Re: Integrate a Piecewise funition, stange behaviour**
Next by Date:
**Re: Algebra of Einstein velocity addition**
Previous by thread:
**Re: Solving matrix equation**
Next by thread:
**small puzzle programming**
| |