MathGroup Archive 2002

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

Search the Archive

RE: sparse matrix

  • To: mathgroup at smc.vnet.net
  • Subject: [mg35195] RE: [mg35190] sparse matrix
  • From: "DrBob" <majort at cox-internet.com>
  • Date: Mon, 1 Jul 2002 03:34:51 -0400 (EDT)
  • Sender: owner-wri-mathgroup at wolfram.com

If the matrix is sparse, don't STORE it as a matrix -- you waste a lot
of space if you do.  Here's one way to do it, with a short example file
I created:

r = ReadList["sparse.txt", {Number, Number, Number}]

{{2, 3, 12}, {1, 7, 41}}

ClearAll[sparse]
sparse[line_, col_] := 0
(sparse[#[[1]], #[[2]]] = #[[3]]) & /@ r;
?sparse

Global`sparse

sparse[1, 7] = 41
 
sparse[2, 3] = 12
 
sparse[line_, col_] := 0

The function "sparse" is zero for all inputs not found in the file.

If you had a situation where other default values applied, that can be
easily handled too.  For instance, if the default diagonal values are
one, while all others default to zero, define it this way:

ClearAll[sparse]
sparse[line_, line_] := 1
sparse[line_, col_] := 0
(sparse[#[[1]], #[[2]]] = #[[3]]) & /@ r;
?sparse

Inputs from the file will override defaults.

When you've defined "sparse" use Clear[r] to save space, or you can read
entries one line at a time, defining "sparse" as you go along, so the
"r" matrix isn't needed at all.

Bobby Treat

-----Original Message-----
From: Louis Patrick O'Carroll [mailto:louis at dcc.ufmg.br] 
To: mathgroup at smc.vnet.net
Subject: [mg35195] [mg35190] sparse matrix

Hi,

is it possible in mathematica to import from a text file a sparse
matrix?
The file is written in the form
l c v

where l is line, c is colum and v is a non zero value.

Thanks for the help,

Louis

PS: please send the responce to my email address.







  • Prev by Date: Calculus`Integration` in Math 4.1 with Win98
  • Next by Date: Apply and Heads option
  • Previous by thread: Calculus`Integration` in Math 4.1 with Win98
  • Next by thread: Apply and Heads option