Re: Re: sparsearray and its nonempty cells

• To: mathgroup at smc.vnet.net
• Subject: [mg72676] Re: [mg72648] Re: sparsearray and its nonempty cells
• From: Carl Woll <carlw at wolfram.com>
• Date: Sun, 14 Jan 2007 05:31:00 -0500 (EST)
• References: <eo4oa3\$mhi\$1@smc.vnet.net><eo7njr\$20l\$1@smc.vnet.net> <200701131002.FAA25549@smc.vnet.net>

```Arkadiusz.Majka at gmail.com wrote:

>DearAll,
>
>Of course I can simply use Sort  but I would prefer to obtain already
>sorted list directly from
>
>Mysparse/. SparseArray[_, _, _, x_] :> x[[2, 2]]
>
>or from ArrayRules[...].
>
>BTW Most[First/@ArrayRules[Mysparse]] does not produce sorted list
>despite what dh wrote.
>
>In practice I have very long list Mysparse/. SparseArray[_, _, _, x_]
>:> x[[2, 2]]
>and I want to take the maximum of it. This searching takes some time.
>If I would have sorted list (given in advance) I would take the last
>element.
>
>
I would expect taking the maximum of

Mysparse/. SparseArray[_, _, _, x_] :> x[[2, 2]]

to be instantaneous. Can you give an example of a sparse matrix where

Mysparse /. SparseArray[_,_,_,x_] :> Max[x[[2,2]]]

is measurably slower than

Mysparse /. SparseArray[_,_,_,x_] :> First[x[[2,2]]]

I use First so that there is no chance that the FrontEnd tries to render x[[2,2]], which as you say can be a very long list.

Carl Woll
Wolfram Research

>Take again:
>
>Out[279]=
>SparseArray[i_ /; 2 â?¤ i â?¤ 4 :> A, 6]
>
>In[280]:=
>% /. SparseArray[_, _, _, x_] :> x[[2,2]]
>
>{{3}, {2}, {4}}
>
>and:
>
>
>SparseArray[i_ /; 2 â?¤ i â?¤ 4 :> A, 6]//Trace
>
>{{i_ /; 2 â?¤ i â?¤ 4 :> A, i_ /; 2 â?¤ i â?¤ 4 :> A},
>    SparseArray[i_ /; 2 â?¤ i â?¤ 4 :> A, 6], {2 â?¤ 1 â?¤
>  4, False}, {2 â?¤ 2 â?¤ 4,
>    True}, {2 â?¤ 3 â?¤ 4, True}, {2 â?¤ 4 â?¤ 4, True}, {2 â?¤ 5 â?¤
>4, False}, {2 â?¤ 6 â?¤
>    4, False}, SparseArray[<\[InvisibleSpace]3\[InvisibleSpace]>, {6}]}
>
>Note, that True is for 2, 3 and 4 - ordered!
>
>Wht do you think?
>
>Arek
>
>
>
>
>dh napisaÅ?(a):
>
>
>>Hi Arek,
>>if you say: Mysparse // FullForm you will see that the array is not
>>stored in ascending order as you imply. To get your output sorted,
>>simply wrap Sort around it.
>>Daniel
>>
>>
>>
>>>Hi,
>>>
>>>I bulit the following sparse array
>>>
>>>Mysparse=SparseArray[i_ /; 2 â?¤ i â?¤ 11 :> 0.8, 20]
>>>
>>>in order to find out all nonempty cells in this array I call (I want to
>>>avoid Normal)
>>>
>>>
>>>Mysparse/. SparseArray[_, _, _, x_] :> x[[2, 2]]
>>>
>>>and I get
>>>
>>>{{8}, {11}, {3}, {7}, {2}, {6}, {10}, {9}, {5}, {4}}
>>>
>>>This is of course correct, but why it is not sorted????
>>>
>>>How can I build Mysparse to get sorted list of nonempty cells?
>>>
>>>Arek
>>>
>>>
>>>

```

• Prev by Date: RE: Re: Re: Limit and Root Objects
• Next by Date: Re: Re: Re: Limit and Root Objects
• Previous by thread: Re: sparsearray and its nonempty cells
• Next by thread: Re: sparsearray and its nonempty cells