MathGroup Archive 2005

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

Search the Archive

Re: Problem with substitutions in SparseArray?


Thank you very much for your answer

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

However, this is  not very general. If you have, for example

  mm=SparseArray[{i_,i_}->a,{2,2}]
(nn = mm /. HoldPattern[SparseArray[x__, {y__, {a}}]] :>    SparseArray[x, 
{y, {1}}])
Normal[nn]

RESULT:
{{a, 0}, {0, a}}


>
> n=SparseArray[ArrayRules[m]/.a->1]//InputForm
>
>
> SparseArray[Automatic, {1, 1}, 0, {1, {{0, 1}, {{1}}},
>  {1}}]
>

With this one must be even more careful: in this case the zeroes of the 
matrix have been removed and you are left with a 1x1 instead of a 2x2 
matrix



I tried it this way, i. e. by defining a special replacement

ReplaceSparse[ff_,rr_Rule] := (ff/.rr)/. HoldPattern[SparseArray[xx__]]:> 
SparseArray @@ ({xx}/.rr)


but I don't like it very much

EXAMPLES:
ReplaceSparse[mm,a->1] //Normal
ReplaceSparse[m,a->1] //Normal



one could even think to redefine ReplaceAll in this way

Enrico


  • Prev by Date: Re: meaning of a "*" in search string?
  • Next by Date: Re: ArcTan[1/0] no result, but ArcTan[Infinity] ok. How to resolve?
  • Previous by thread: Re: Problem with substitutions in SparseArray?
  • Next by thread: Re: Problem with substitutions in SparseArray?