Services & Resources / Wolfram Forums / MathGroup Archive
-----

MathGroup Archive 2011

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

Search the Archive

1/Diagonal[A] gives different result compared to 1/Diagonal[Normal@A] when A is sparse

  • To: mathgroup at smc.vnet.net
  • Subject: [mg123270] 1/Diagonal[A] gives different result compared to 1/Diagonal[Normal@A] when A is sparse
  • From: "Nasser M. Abbasi" <nma at 12000.org>
  • Date: Wed, 30 Nov 2011 03:18:50 -0500 (EST)
  • Delivered-to: l-mathgroup@mail-archive0.wolfram.com

I found this strange behavior, and I do not think it is correct.

This is version 8.04.

1/Diagonal[A]  gives a divide by zero error, but 1/Diagonal[Normal@A] does
not. This is when A is sparse.

------------------------------
Clear["Global`*"]

makeMatrix[n_]:=Module[{numberOfUnknowns=n^2,r,A},

A=SparseArray[
{
  Band[{1,1}]->4.0,
  Band[{2,1}]->-1,
  Band[{1,2}]->-1,
  Band[{1,n+1}]->-1,
  Band[{n+1,1}]->-1
},{numberOfUnknowns,numberOfUnknowns},0.
];

r=Range[n,n^2-n,n];
(A[[#,#+1]]=0.)&/@r;
(A[[#+1,#]]=0.)&/@r;

A
];

(A = makeMatrix[3])//MatrixForm

(Diagonal[A])//Normal

1/Diagonal[Normal@A]  (* ===> OK *)
1/Diagonal[A]         (* error *)

----------------------------------------

So, 1/Diagonal[Normal@A]  gives
{0.25,0.25,0.25,0.25,0.25,0.25,0.25,0.25,0.25}

but 1/Diagonal[A]  gives 1/0

In another system I use, both operations give
{0.25,0.25,0.25,0.25,0.25,0.25,0.25,0.25,0.25}

i.e. if the matrix is sparse or not, 1/Diagonal[A] should work
regardless.  I think sparse matrices need to be more integrated into
all Mathemaitca matrix operations.

Or Am I missing something here?

Thanks,
--Nasser



  • Prev by Date: Ploting a transformation of a set
  • Next by Date: Re: how to get string in sci. notation to a number?
  • Previous by thread: Ploting a transformation of a set
  • Next by thread: How to sort elements from a two-dimensional list