In the case of = (self-classify), we have the advantage that the indices are easily constructed. There is one 1 per column, located at (i.~ ~.) y. indices =. (,. i.@#) (i.~ ~.) 20 ?@$ 5 This finds these row indices, and appends the column indices, which are given simply by i. . sparse =. 1 (<"_1 indices)} 1$.5 20 Now we make a shape 5 20 sparse matrix, and fill in ones at the correct entries.
Other tasks can similarly be performed by directly manipulating indices and values, which doesn't face the implementation limitations of sparse arrays. Marshall On Fri, Jul 13, 2012 at 12:54 AM, Roger Hui <rogerhui.can...@gmail.com>wrote: > The sparse linear algebra example in the dictionary page for $. shows you > how it's done. > http://www.jsoftware.com/help/dictionary/d211.htm#sparse%20linear%20algebra > > Another example: > > x=: 1 $. 1e6 1e6 NB. create an empty 1e6 by 1e6 sparse matrix > d=: 1e6 ?@$ 0 NB. 1e6-element dense vector > x=: d (<"1 ,.~i.1e6)}x NB. amend the diagonal of x by d > $x NB. shape of x > 1000000 1000000 > x -: |: x NB. x is symmetric > 1 > d -: +/x > 1 > d -: +/"1 x > 1 > > > > On Thu, Jul 12, 2012 at 8:10 PM, Ed Gottsman <edward.j.gotts...@gmail.com > >wrote: > > > Greetings. I'm just picking J up again after a brief 10-year hiatus and > I > > have a question about sparse arrays. I understand I can convert a dense > > array to a sparse array so: > > > > sparse =. $. ? 1000000 # 3 NB. Gives me an array whose zeros are not > > taking up any room. Perfect. > > > > But what do I do if the dense array I'm trying to "sparse" is too big to > > exist as a dense array? For example: > > > > sparse =. $. = ? 100000000 # 1000 NB. Gives me an out of memory error. > > > > Now, the self-classify of this array is mostly zeros and, as a sparse > > array, it wouldn't take up much space. But I can't work out how to make > it > > into a sparse array...as it's being created? Before it's created? Is > > there a way to bind the sparse verb to the self-classify verb such that = > > "pipes" directly into the sparse version of its result? > > > > Any insights into this would be greatly appreciated. > > > > Many thanks. > > > > Ed > > ---------------------------------------------------------------------- > > For information about J forums see http://www.jsoftware.com/forums.htm > > > ---------------------------------------------------------------------- > For information about J forums see http://www.jsoftware.com/forums.htm > ---------------------------------------------------------------------- For information about J forums see http://www.jsoftware.com/forums.htm