MathGroup Archive 2004

[Date Index] [Thread Index] [Author Index]

Search the Archive

Re: Further information about size limits for Normal[SparseArray[<>]]?

  • To: mathgroup at smc.vnet.net
  • Subject: [mg49616] Re: Further information about size limits for Normal[SparseArray[<>]]?
  • From: ab_def at prontomail.com (Maxim)
  • Date: Sat, 24 Jul 2004 03:48:38 -0400 (EDT)
  • References: <cdqr0s$kop$1@smc.vnet.net>
  • Sender: owner-wri-mathgroup at wolfram.com

Scott Morrison <scott at math.berkeley.edu> wrote in message news:<cdqr0s$kop$1 at smc.vnet.net>...
> 
> More generally, does anyone have a definitive (or even partial) list of 
> functions which know how to operate directly on SparseArrays, without 
> having to resort to their Normal forms?

Most of the basic operations have been extended to handle SparseArray
objects, so, for example, there is no need to add any fixes to
LinearAlgebra`MatrixManipulation` to make it compatible with
SparseArray, because those fixes have already been made to the basic
Mathematica functions like Part. However, there are a few exceptions:
in some cases a function has to do the parsing of the input expression
and forgets about SparseArray; for example:

In[1]:=
NDSolve[
  {y'[x] == SparseArray[{{1, 1} -> 1, {2, 2} -> 1}],
    y[0] == IdentityMatrix[2]},
  y, {x, 0, 1}]

NDSolve::ndnum: Encountered non-numerical value for a derivative at x
== 0.`.

Out[1]=
NDSolve[{y'[x] == SparseArray[<2>, {2, 2}], y[0] == {{1, 0}, {0, 1}}},
y, {x, 0, 1}]

In[2]:=
NDSolve[
  {y'[x] == IdentityMatrix[2],
    y[0] == SparseArray[{{1, 1} -> 1, {2, 2} -> 1}]},
  y, {x, 0, 1}]

NDSolve::ndinnt: Initial condition SparseArray[<2>, {2, 2}] is not a
number or a rectangular array of numbers.

Out[2]=
NDSolve[{y'[x] == {{1, 0}, {0, 1}}, y[0] == SparseArray[<2>, {2, 2}]},
y, {x, 0, 1}]

Both examples work as expected after converting SparseArray to a
matrix. This can lead to more unpleasant consequences; each of the
following two examples crashes the Mathematica kernel (at least, in
version 5.0):

FindRoot[Norm[x - SparseArray[{{1, 1} -> 1, {2, 2} -> 1}]], {x,
IdentityMatrix[2]}]
FindMinimum[Norm[x - SparseArray[{{1, 1} -> 1, {2, 2} -> 1}]], {x,
IdentityMatrix[2]}]

Next, sometimes it's hard to make this construct work seamlessly in
all situations; I suppose there was a good reason to make SparseArray
an atomic object, but this leads to some unexpected things; suppose we
do

a = SparseArray[{i_, j_} -> 2i + j - 3, {2, 2}];
(*Normal@a is {{0,1},{2,3}}*)

Map[Print, a, {2}]
MapIndexed[Print[#]&, a, {2}]
Print /@ Level[a, {2}]

What will be the output in those three cases? All three will be
different: the first one will output 1,2,3,0 because Map goes over the
rules in SparseArray and 0, as the default value, is processed last;
on the other hand, MapIndexed has to convert the SparseArray to List
(i.e., do Normal) and thus Print will give 0,1,2,3. Finally, even
though Map goes into SparseArray, Level gives {} because SparseArray
is an atom! So the third Print line won't print anything at all. I
don't think this is very logical.

Maxim Rytin
m.r at inbox.ru


  • Prev by Date: Re: NonlinearFit works not so good
  • Next by Date: RE: Quantum Mechanics, Boundary Value Problem
  • Previous by thread: Further information about size limits for Normal[SparseArray[<>]]?
  • Next by thread: Re: Re: Further information about size limits for Normal[SparseArray[<>]]?