Re: Problem with substitutions in SparseArray?

*To*: mathgroup at smc.vnet.net*Subject*: [mg56908] Re: [mg56824] Problem with substitutions in SparseArray?*From*: Andrzej Kozlowski <akoz at mimuw.edu.pl>*Date*: Tue, 10 May 2005 03:42:10 -0400 (EDT)*References*: <200505070816.EAA20188@smc.vnet.net> <6d5b1cedba1e6f5bbfe7eb5fd2f511bc@mimuw.edu.pl>*Sender*: owner-wri-mathgroup at wolfram.com

You are right, I was careless so here is what I should have done: m = SparseArray[{{a, 0}, {0, 0}}]; SparseArray @@ {ArrayRules[m], Dimensions[m]} /. a -> 1; Normal[%] {{a, 0}, {0, 0}} Andrzej Kozlowski On 7 May 2005, at 20:16, Andrzej Kozlowski wrote: > 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/ >

**Follow-Ups**:**Re: Re: Problem with substitutions in SparseArray?***From:*Andrzej Kozlowski <akoz@mimuw.edu.pl>

**References**:**Problem with substitutions in SparseArray?***From:*Enrico Arrigoni <arrigoni@itp.tu-graz.ac.at>