MathGroup Archive 2005

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

Search the Archive

Re: Problem with substitutions in SparseArray?

  • To: mathgroup at smc.vnet.net
  • Subject: [mg56852] Re: [mg56824] Problem with substitutions in SparseArray?
  • From: Andrzej Kozlowski <akoz at mimuw.edu.pl>
  • Date: Sat, 7 May 2005 15:35:12 -0400 (EDT)
  • References: <200505070816.EAA20188@smc.vnet.net>
  • Sender: owner-wri-mathgroup at wolfram.com

On 7 May 2005, at 17:16, Enrico Arrigoni wrote:

>
> Substitutions don't seem to work in SparseArrays
>
> try:
>
> m = SparseArray[{{a,0},{0,0}}];
> ms = m/.a->1.;
> Normal[ms]
>
> RESULT:
>
> {{a, 0}, {0, 0}}
>

The reason is that SparseArrays are atoms:


m=SparseArray[{{a,0},{0,0}}];


AtomQ[m]

True

You cannot change the structure of atoms by applying rules to their 
"components" because Atoms "do not have components". Compare thsi with, 
for example

Complex[2,3]/.(2->5)


2+3 I

(Please no more discussions whether complex numbers are "really" atoms 
or not).

To apply a rule to an atom you have to apply it to the "whole thing", 
e.g.


(n = m /. HoldPattern[SparseArray[x__, {y__, {a}}]] :>
     SparseArray[x, {y, {1}}])//InputForm


SparseArray[Automatic, {2, 2}, 0, {1, {{0, 1, 1}, {{1}}},
   {1}}]


This is of course not very convenient, but fortunately there is an easy 
way to do the same thing:

n=SparseArray[ArrayRules[m]/.a->1]//InputForm


SparseArray[Automatic, {1, 1}, 0, {1, {{0, 1}, {{1}}},
   {1}}]


Andrzej Kozlowski
Chiba, Japan
http://www.akikoz.net/andrzej/index.html
http://www.mimuw.edu.pl/~akoz/


  • Prev by Date: Re: How to quickly find number of non-zero elements in sparse matrix rows?
  • Next by Date: Re: Re: Mathematica Notebook Organiztion
  • Previous by thread: Problem with substitutions in SparseArray?
  • Next by thread: Re: Problem with substitutions in SparseArray?