MathGroup Archive 2006

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

Search the Archive

Re: Zero times a variable in numerical calculations

  • To: mathgroup at smc.vnet.net
  • Subject: [mg68410] Re: Zero times a variable in numerical calculations
  • From: Peter Pein <petsie at dordos.net>
  • Date: Fri, 4 Aug 2006 03:59:23 -0400 (EDT)
  • References: <a02020013-1047-ppc-D86740659B534E4384129C036A76A845@QuarkNew.local> <easjm1$fu2$1@smc.vnet.net>
  • Sender: owner-wri-mathgroup at wolfram.com

Maurits Haverkort schrieb:
> Dear Bill and others who answered my question.
> 
> Chop[0.0 a] indeed works and returns nicely 0 however I still have some 
> problems when using this on Sparse Arrays.
> 
> I define:
> M = SparseArray[{{1, 1} -> a, {2, 2} -> 1.0, {3, 3} -> 1.0, {2, 3} -> b, {3, 
> 2} -> b, {_, _} -> 0}, {3, 3}];
> and
> T = SparseArray[{{1, 1} -> 1.0, {2, 2} -> -Sqrt[0.5], {3, 3} -> Sqrt[0.5], 
> {2, 3} -> Sqrt[0.5], {3, 2} -> Sqrt[0.5], {_, _} -> 0}, {3, 3}];
> I calculate T.M.T This should result in a sparse array with 3 nonzero 
> elements. (a,1-b and 1+b on the diagonal).
> However
> T.M.T=SparseArray[<5>, {3, 3}]
> (T.M.T)[[3,2]]=0. (0.707107 + 0.707107 b)
> and
> (T.M.T)[[2,3]]=0.707107 (0.707107- 0.707107 b) + 0.707107 (-0.707107 + 
> 0.707107 b)
> 
> Chop works almost as
> Chop[T.M.T]=SparseArray[<5>, {3, 3}]
> But
> Chop[T.M.T][[3,2]]=0
> However
> Chop[T.M.T][[2,3]]=0.707107 (0.707107 - 0.707107 b) + 0.707107 (-0.707107 + 
> 0.707107 b)
> 
> Simplify does nothing. i.e.
> Simplify[T.M.T]=T.M.T
> 
> Chop[Simplify[Normal[T.M.T]]]
> does what I want, but does not work with sparse arrays and in between all 
> elements become large expressions so I run out of memory.
> 
> Thanks in advance,
> Maurits Haverkort
> 
> (P.S. Mathematica 5.2 under Linux (Redhat) 1 Gb and Windows XP, 512Mb, the 
> matrix size I aim for is 646*646, however I curently can't evaluate a 
> 134*134 matrix)
> 
> ----- Original Message ----- 
> From: "Bill Rowe" <readnewsciv at earthlink.net>
To: mathgroup at smc.vnet.net
> Subject: [mg68410] Re: Zero times a variable in numerical calculations
> 
> 
> On 8/2/06 at 10:49 AM, Haverkort at ph2.uni-koeln.de (Maurits Haverkort)
> wrote:
> 
>> 2) What is the fastest way to multiply a numerical matrix times a
>> half numerical half analytical sparse matrix, whereby the result is
>> band diagonal. (The current result has 0. a + 0. b + 0. c + 0. d +
>> 0. e + 0. f on all places where it should be zero.)
> 
> How are you defining the sparse matrix?
> 
> If I do
> 
> In[17]:=
> a=SparseArray[{{1, 2} -> Random[], {3, 4} -> Random[]},
>   {5, 5}];
> 
> followed by
> 
> In[18]:=
> Normal@a
> 
> Out[18]=
> {{0, 0.49847100218212825, 0, 0, 0}, {0, 0, 0, 0, 0},
>   {0, 0, 0, 0.009744974537243703, 0}, {0, 0, 0, 0, 0},
>   {0, 0, 0, 0, 0}}
> 
> 
> I see the zero elements are exact 0's not inexact 0's. Which means the 
> matrix multiplication will be evaluating 0 a etc which will evaluate to 0.
> --
> To reply via email subtract one hundred and four 
> 

Hi Maurits,

you might want to manipulate the array rules:

m = SparseArray[{{1, 1} -> a, {2, 2} -> 1., {3, 3} -> 1., {2, 3} -> b, {3, 2} -> b, {_, _} -> 0}, {3, 3}];
t = SparseArray[{{1, 1} -> 1., {2, 2} -> -Sqrt[0.5], {3, 3} -> Sqrt[0.5], {2, 3} -> Sqrt[0.5], {3, 2} -> Sqrt[0.5], {_, _} -> 0}, {3, 3}];

(t . m . t)[[2,3]]
gives the unsimplified element of the matrix.

result = SparseArray[ArrayRules[t . m . t] /.
  HoldPattern[ix_ -> val_] :> ix -> Chop[Simplify[val]]];

result[[2,3]]

gives 0

Normal[result]
-->
{{1.*a, 0, 0}, {0, 1.0000000000000002 - 1.0000000000000002*b, 0},
   {0, 0, 1.0000000000000002 + 1.0000000000000002*b}}


hth,
   Peter


  • Prev by Date: Re: Re: "No more memory available" -- a recurring problem
  • Next by Date: How to do Chebyshev expansion in Mathematica?
  • Previous by thread: Re: Zero times a variable in numerical calculations
  • Next by thread: mathematica 4.0 + NET/Link doesn't work?