AuD
Lecture 'Algorithmen und Datenstrukturen' (code examples)
aud.graph.matrix.SparseMatrixCS< T > Class Template Reference

Simple sparse matrix data structure. More...

+ Inheritance diagram for aud.graph.matrix.SparseMatrixCS< T >:
+ Collaboration diagram for aud.graph.matrix.SparseMatrixCS< T >:

Public Member Functions

 SparseMatrixCS ()
 create empty matrix with "arbitrarily growing" dimensions More...
 
 SparseMatrixCS (SparseMatrixCS< T > other)
 copy constructor More...
 
int nnz ()
 get number of nonzero entries More...
 
int getNumRows ()
 computes from maximium row index [O(nnz)] More...
 
int getMinRowIndex ()
 get minimum row index [O(nnz)] More...
 
int getNumColumns ()
 computed from maximum column index [O(1)] More...
 
int getMinColumnIndex ()
 get minimum row index [O(1)] More...
 
get (int i, int j)
 get entry (i,j) [O(log(nnz))] More...
 
set (int i, int j, T data)
 Set entry (i,j) to data [O(log(nnz))]. More...
 
Vector< T > getColumnEntries (int j)
 get entries in column j as array More...
 
int[] getColumnRowIndices (int j)
 get row indices in column j as array More...
 
int columnDegree (int j)
 get number of nonzero entries in column j More...
 
SparseMatrixCS< T > getTransposed ()
 get transposed matrix More...
 
SparseMatrixCS< Integer > spones ()
 Get nonzero pattern. More...
 
int[] getRowIndices ()
 Get array of row indices. More...
 
int[] getColumnIndices ()
 Get array of column indices in same order as for getRowIndices. More...
 
T[] getValues ()
 Get array of values in same order as for getRowIndices. More...
 
String toString ()
 
String toLaTeX (String name)
 get LaTeX code for displaying (TikZ) matrix More...
 
File renderSpySVG (File svgfile, Colormap< T > colormap)
 
String spyTikZ (boolean rulers, Colormap< T > colormap)
 
SVGViewer spy (String caption, Colormap< T > colormap)
 render spy plot in new window More...
 

Protected Attributes

TreeMap< Coordinate, T > mat_
 

Detailed Description

Simple sparse matrix data structure.

This data structure is intended for sparse storage: it stores only non-null entries, i.e., the required amount of storage is O(nnz) (not O(m*n))).

  • SparseMatrixCS stores and retrieves columns efficiently. The retrieval of rows is not supported.

  • The matrix has potentially infinite dimensions. (Dimension is determined by the maximium row and column indices for all entries. We don't require this property for adjacency matrices.

  • The implementation is based on a simplified compressed column storage (CCS) scheme based on sorted tuples in a
    TreeMap
    .

Definition at line 36 of file SparseMatrixCS.java.

Constructor & Destructor Documentation

◆ SparseMatrixCS() [1/2]

create empty matrix with "arbitrarily growing" dimensions

Definition at line 42 of file SparseMatrixCS.java.

42 {
43 mat_=new TreeMap<Coordinate,T>();
44 }
TreeMap< Coordinate, T > mat_

References aud.graph.matrix.SparseMatrixCS< T >.mat_.

◆ SparseMatrixCS() [2/2]

copy constructor

Definition at line 46 of file SparseMatrixCS.java.

46 {
47 mat_=new TreeMap<Coordinate,T>(other.mat_);
48 }

References aud.graph.matrix.SparseMatrixCS< T >.mat_.

Member Function Documentation

◆ columnDegree()

int aud.graph.matrix.SparseMatrixCS< T >.columnDegree ( int  j)

get number of nonzero entries in column j

Exceptions
IndexOutOfBoundsExceptionon error (non-positive index)
Returns
length of getColumnEntries

Definition at line 155 of file SparseMatrixCS.java.

155{ return getColumnEntries(j).size(); }
Vector< T > getColumnEntries(int j)
get entries in column j as array

References aud.graph.matrix.SparseMatrixCS< T >.getColumnEntries().

Referenced by aud.test.SparseMatrixCSTest.testMatrix(), aud.test.SparseMatrixTest.testMatrix(), and aud.test.SparseMatrixTest.testSymmatrixMatrix().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ get()

T aud.graph.matrix.SparseMatrixCS< T >.get ( int  i,
int  j 
)

get entry (i,j) [O(log(nnz))]

Exceptions
IndexOutOfBoundsException(non-positive indices)
Returns
entry or null

Definition at line 90 of file SparseMatrixCS.java.

90 {
91 return check(i,j).get(new Coordinate(i,j));
92 }

Referenced by aud.test.AdjacencyMatrixTest.testMatrix(), aud.test.SparseMatrixCSTest.testMatrix(), aud.test.SparseMatrixTest.testMatrix(), aud.test.AdjacencyMatrixTest.testSymmatrixMatrix(), and aud.test.SparseMatrixTest.testSymmatrixMatrix().

+ Here is the caller graph for this function:

◆ getColumnEntries()

Vector< T > aud.graph.matrix.SparseMatrixCS< T >.getColumnEntries ( int  j)

get entries in column j as array

Exceptions
IndexOutOfBoundsExceptionon error (non-positive index)

Definition at line 123 of file SparseMatrixCS.java.

123 {
124 SortedMap<Coordinate,T> sub=
125 check(1,j).subMap(new Coordinate(-1,j),
126 new Coordinate(Integer.MAX_VALUE,j));
127 Object[] a=sub.values().toArray();
128 Vector<T> v=new Vector<T>();
129 v.reserve(a.length);
130 for (int i=0;i<a.length;++i)
131 v.push_back((T) a[i]);
132 return v;
133 }

Referenced by aud.graph.matrix.SparseMatrixCS< T >.columnDegree(), aud.test.SparseMatrixCSTest.testMatrix(), aud.test.SparseMatrixTest.testMatrix(), and aud.test.SparseMatrixTest.testSymmatrixMatrix().

+ Here is the caller graph for this function:

◆ getColumnIndices()

int[] aud.graph.matrix.SparseMatrixCS< T >.getColumnIndices ( )

Get array of column indices in same order as for getRowIndices.

Definition at line 198 of file SparseMatrixCS.java.

198 {
199 int[] ci=new int[nnz()];
200 int i=0;
201 for (Map.Entry<Coordinate,T> entry : mat_.entrySet())
202 ci[i++]=entry.getKey().j;
203 return ci;
204 }
int nnz()
get number of nonzero entries

References aud.graph.matrix.SparseMatrixCS< T >.mat_, and aud.graph.matrix.SparseMatrixCS< T >.nnz().

+ Here is the call graph for this function:

◆ getColumnRowIndices()

int[] aud.graph.matrix.SparseMatrixCS< T >.getColumnRowIndices ( int  j)

get row indices in column j as array

Exceptions
IndexOutOfBoundsExceptionon error (non-positive index)

Definition at line 138 of file SparseMatrixCS.java.

138 {
139 SortedMap<Coordinate,T> sub=
140 check(1,j).subMap(new Coordinate(-1,j),
141 new Coordinate(Integer.MAX_VALUE,j));
142 int[] idx=new int[sub.size()];
143 int i=0;
144 for (Coordinate c : sub.keySet()) {
145 idx[i++]=c.i;
146 }
147 assert(i==idx.length);
148 return idx;
149 }

Referenced by aud.graph.AdjacencyMatrix< Edge >.clearColumnAndRow(), aud.test.SparseMatrixCSTest.testMatrix(), aud.test.SparseMatrixTest.testMatrix(), and aud.test.SparseMatrixTest.testSymmatrixMatrix().

+ Here is the caller graph for this function:

◆ getMinColumnIndex()

int aud.graph.matrix.SparseMatrixCS< T >.getMinColumnIndex ( )

get minimum row index [O(1)]

Definition at line 74 of file SparseMatrixCS.java.

74{ return mat_.size()==0 ? 0 : mat_.firstKey().j; }

References aud.graph.matrix.SparseMatrixCS< T >.mat_.

Referenced by aud.test.SparseMatrixCSTest.testMatrix(), aud.test.SparseMatrixTest.testMatrix(), aud.test.SparseMatrixTest.testSymmatrixMatrix(), and aud.graph.matrix.SparseMatrixCS< T >.toLaTeX().

+ Here is the caller graph for this function:

◆ getMinRowIndex()

int aud.graph.matrix.SparseMatrixCS< T >.getMinRowIndex ( )

get minimum row index [O(nnz)]

Reimplemented in aud.graph.matrix.SparseMatrix< T >.

Definition at line 63 of file SparseMatrixCS.java.

63 {
64 int imin=Integer.MAX_VALUE;
65 for (Coordinate c : mat_.keySet()) {
66 if (c.i<imin)
67 imin=c.i;
68 }
69 return (imin==Integer.MAX_VALUE) ? 0 : imin;
70 }

References aud.graph.matrix.SparseMatrixCS< T >.mat_.

Referenced by aud.test.SparseMatrixCSTest.testMatrix(), and aud.graph.matrix.SparseMatrixCS< T >.toLaTeX().

+ Here is the caller graph for this function:

◆ getNumColumns()

◆ getNumRows()

int aud.graph.matrix.SparseMatrixCS< T >.getNumRows ( )

computes from maximium row index [O(nnz)]

Reimplemented in aud.graph.matrix.SparseMatrix< T >.

Definition at line 54 of file SparseMatrixCS.java.

54 {
55 int imax=0;
56 for (Coordinate c : mat_.keySet()) {
57 if (c.i>imax)
58 imax=c.i;
59 }
60 return imax;
61 }

References aud.graph.matrix.SparseMatrixCS< T >.mat_.

Referenced by aud.graph.matrix.SparseMatrixCS< T >.renderSpySVG(), aud.graph.matrix.SparseMatrixCS< T >.spyTikZ(), aud.test.SparseMatrixCSTest.testMatrix(), and aud.graph.matrix.SparseMatrixCS< T >.toLaTeX().

+ Here is the caller graph for this function:

◆ getRowIndices()

int[] aud.graph.matrix.SparseMatrixCS< T >.getRowIndices ( )

Get array of row indices.

See also
getColumnIndices
getValues

Definition at line 188 of file SparseMatrixCS.java.

188 {
189 int[] ri=new int[nnz()];
190 int i=0;
191 for (Map.Entry<Coordinate,T> entry : mat_.entrySet())
192 ri[i++]=entry.getKey().i;
193 return ri;
194 }

References aud.graph.matrix.SparseMatrixCS< T >.mat_, and aud.graph.matrix.SparseMatrixCS< T >.nnz().

+ Here is the call graph for this function:

◆ getTransposed()

SparseMatrixCS< T > aud.graph.matrix.SparseMatrixCS< T >.getTransposed ( )

get transposed matrix

Reimplemented in aud.graph.matrix.SparseMatrix< T >.

Definition at line 159 of file SparseMatrixCS.java.

159 {
160 SparseMatrixCS<T> m=new SparseMatrixCS<T>();
161 for (Map.Entry<Coordinate,T> entry : mat_.entrySet()) {
162 Coordinate c=entry.getKey();
163 m.mat_.put(new Coordinate(c.j,c.i),entry.getValue());
164 }
165 return m;
166 }

References aud.graph.matrix.Coordinate.i, aud.graph.matrix.Coordinate.j, and aud.graph.matrix.SparseMatrixCS< T >.mat_.

◆ getValues()

T[] aud.graph.matrix.SparseMatrixCS< T >.getValues ( )

Get array of values in same order as for getRowIndices.

Definition at line 208 of file SparseMatrixCS.java.

208 {
209 T[] values=(T[]) new Object[nnz()];
210 int i=0;
211 for (Map.Entry<Coordinate,T> entry : mat_.entrySet())
212 values[i++]=entry.getValue();
213 return values;
214 }

References aud.graph.matrix.SparseMatrixCS< T >.mat_, and aud.graph.matrix.SparseMatrixCS< T >.nnz().

+ Here is the call graph for this function:

◆ nnz()

◆ renderSpySVG()

File aud.graph.matrix.SparseMatrixCS< T >.renderSpySVG ( File  svgfile,
Colormap< T >  colormap 
)

Definition at line 249 of file SparseMatrixCS.java.

249 {
250 if (colormap==null)
251 colormap=new Colormap<T>();
252
253 try {
254 FileOutputStream f=new FileOutputStream(svgfile);
255
256 // write header
257 float sz=5.0f; // cell size
258 float r=2.0f; // radius
259 float w=(getNumColumns()+1)*sz, h=(getNumRows()+1)*sz;
260
261 String buf=
262 "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"+
263 "<svg xmlns=\"http://www.w3.org/2000/svg\"\n"+
264 " width=\""+w+"mm\"\n"+
265 " height=\""+h+"mm\"\n"+
266 " version=\"1.1\"\n>\n\n";
267 f.write(buf.getBytes());
268
269 // rulers
270 f.write(("<line x1=\""+0+"mm\" y1=\""+(sz-r)+
271 "mm\" x2=\""+w+"mm\" y2=\""+(sz-r)+"mm\" "+
272 "style=\"stroke:#000000;stroke-width:1pt\"/>\n").getBytes());
273 for (int i=1;i<=getNumColumns();++i) {
274 float x=sz*(0.5f+i);
275 f.write(("<line x1=\""+x+"mm\" y1=\""+(r)+
276 "mm\" x2=\""+x+"mm\" y2=\""+(r+r)+"mm\" "+
277 "style=\"stroke:#000000;stroke-width:1pt\"/>\n").getBytes());
278 }
279 f.write(("<line x1=\""+(sz-r)+"mm\" y1=\""+0+
280 "mm\" x2=\""+(sz-r)+"mm\" y2=\""+h+"mm\" "+
281 "style=\"stroke:#000000;stroke-width:1pt\"/>\n").getBytes());
282 for (int i=1;i<=getNumRows();++i) {
283 float y=sz*(0.5f+i);
284 f.write(("<line x1=\""+(r)+"mm\" y1=\""+y+
285 "mm\" x2=\""+(r+r)+"mm\" y2=\""+(y)+"mm\" "+
286 "style=\"stroke:#000000;stroke-width:1pt\"/>\n").getBytes());
287 }
288 f.write("\n\n".getBytes());
289
290 // data
291 for (Map.Entry<Coordinate,T> entry : mat_.entrySet()) {
292 float x=sz*(0.5f+entry.getKey().j), y=sz*(0.5f+entry.getKey().i);
293 String fill=String.format("%06x",colormap.getRGB(entry.getValue()));
294
295 buf="<circle style=\"fill:#"+fill+";stroke:#000000;stroke-width:0px\" "+
296 "r=\""+r+"mm\" cx=\""+x+"mm\" cy=\""+y+"mm\"/>\n";
297 f.write(buf.getBytes());
298 }
299
300 f.write("</svg>\n".getBytes());
301 f.close();
302 } catch (IOException e) {
303 System.err.println("ERROR: "+e.getMessage());
304 return null; // note: probably triggers NullPointerException
305 }
306 return svgfile;
307 }
int getNumRows()
computes from maximium row index [O(nnz)]
int getNumColumns()
computed from maximum column index [O(1)]

References aud.graph.matrix.SparseMatrixCS< T >.getNumColumns(), aud.graph.matrix.SparseMatrixCS< T >.getNumRows(), aud.util.Colormap< T >.getRGB(), and aud.graph.matrix.SparseMatrixCS< T >.mat_.

Referenced by aud.graph.matrix.SparseMatrixCS< T >.spy().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ set()

T aud.graph.matrix.SparseMatrixCS< T >.set ( int  i,
int  j,
data 
)

Set entry (i,j) to data [O(log(nnz))].

Parameters
irow index (>=0)
jcolumn index (>=0)
datanew value; if data==null, then the entry will be erased from the matrix.
Exceptions
IndexOutOfBoundsExceptionon error (non-positive indices)a
Returns
old value (or null

Reimplemented in aud.graph.matrix.SparseMatrix< T >.

Definition at line 102 of file SparseMatrixCS.java.

102{ return _set(i,j,data); }

Referenced by aud.test.SparseMatrixCSTest.testMatrix().

+ Here is the caller graph for this function:

◆ spones()

SparseMatrixCS< Integer > aud.graph.matrix.SparseMatrixCS< T >.spones ( )

Get nonzero pattern.

Returns a matrix with integer entries that equal 1 for every nonzero entry. (Same as Matlab's spones).

Definition at line 173 of file SparseMatrixCS.java.

173 {
174 SparseMatrixCS<Integer> m=new SparseMatrixCS<Integer>();
175 for (Map.Entry<Coordinate,T> entry : mat_.entrySet())
176 m.mat_.put(entry.getKey(),1);
177 return m;
178 }

References aud.graph.matrix.SparseMatrixCS< T >.mat_.

Referenced by aud.test.SparseMatrixCSTest.testMatrix().

+ Here is the caller graph for this function:

◆ spy()

SVGViewer aud.graph.matrix.SparseMatrixCS< T >.spy ( String  caption,
Colormap< T >  colormap 
)

render spy plot in new window

Definition at line 348 of file SparseMatrixCS.java.

348 {
349 File tmp=null;
350 try {
351 tmp=File.createTempFile("aud-","svg");
352 }
353 catch (IOException e) {
354 System.err.println("ERROR: "+e.getMessage());
355 System.exit(-1);
356 }
357 tmp.deleteOnExit();
358 renderSpySVG(tmp,colormap!=null ? colormap: new Colormap<T>());
359
360 SVGViewer v=SVGViewer.displayWindow(tmp,caption!=null ? caption : "spy");
361
362 return v;
363 }
File renderSpySVG(File svgfile, Colormap< T > colormap)

References aud.util.SVGViewer.displayWindow(), and aud.graph.matrix.SparseMatrixCS< T >.renderSpySVG().

Referenced by aud.graph.matrix.SparseMatrix< T >.main().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ spyTikZ()

String aud.graph.matrix.SparseMatrixCS< T >.spyTikZ ( boolean  rulers,
Colormap< T >  colormap 
)

Definition at line 309 of file SparseMatrixCS.java.

309 {
310 if (colormap==null)
311 colormap=new Colormap<T>();
312
313 String s="\\begin{tikzpicture}[dot/.style={draw=none,fill=c},scale=1]\n";
314
315 if (rulers) {
316 s+=" \\foreach \\y in {0,...,"+(getNumRows()-1)+"} {\n"+
317 " \\draw (-2.5pt,-\\y) -- (2.5pt,-\\y);\n"+
318 " \\node[left] at (-1ex,-\\y-0.5) {{\\scriptsize \\y}};\n"+
319 " }\n"+
320 " \\draw (0,0) -- (0,-"+getNumRows()+");\n\n"
321 ;
322 s+=" \\foreach \\x in {1,...,"+(getNumColumns())+"} {\n"+
323 " \\draw (\\x,-2.5pt) -- (\\x,2.5pt);\n"+
324 " \\node[above] at (\\x-0.5,0) {{\\scriptsize \\x}};\n"+
325 " }\n"+
326 " \\draw (0,0) -- ("+(getNumColumns())+",0);\n\n"
327 ;
328 }
329
330 float sz=1;
331 for (Map.Entry<Coordinate,T> entry : mat_.entrySet()) {
332 int rgb=colormap.getRGB(entry.getValue());
333 float r=(float) (rgb>>16)/255.0f;
334 float g=(float) ((rgb>> 8)&0xff)/255.0f;
335 float b=(float) (rgb&0xff)/255.0f;
336 s+=" \\definecolor{c}{rgb}{"+r+","+g+","+b+"}\n";
337 float x=sz*(-0.5f+entry.getKey().j), y=-sz*(-0.5f+entry.getKey().i);
338 s+=" \\draw[dot] ("+x+","+y+") circle (0.4" +
339 ");\n";
340 }
341
342 s+="\\end{tikzpicture}\n";
343 return s;
344 }

References aud.graph.matrix.SparseMatrixCS< T >.getNumColumns(), aud.graph.matrix.SparseMatrixCS< T >.getNumRows(), aud.util.Colormap< T >.getRGB(), and aud.graph.matrix.SparseMatrixCS< T >.mat_.

Referenced by aud.graph.matrix.SparseMatrix< T >.main().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ toLaTeX()

String aud.graph.matrix.SparseMatrixCS< T >.toLaTeX ( String  name)

get LaTeX code for displaying (TikZ) matrix

Definition at line 229 of file SparseMatrixCS.java.

229 {
230 String rv="\\matrix ("+name+") [matrix of math nodes]\n{\n";
231 int m=getNumRows(),n=getNumColumns();
232 Class<?> intclass=Integer.valueOf(1).getClass();
233 for (int i=getMinRowIndex();i<=m;++i) {
234 for (int j=getMinColumnIndex();j<=n;++j) {
235 T v=get(i,j);
236 if (v!=null) {
237 rv+=" ";
238 rv+=(v.getClass()==intclass) ? v.toString() : "1";
239 }
240 if (j!=n)
241 rv+=" &";
242 }
243 rv+=" \\\\\n";
244 }
245 rv+="};\n";
246 return rv;
247 }
int getMinColumnIndex()
get minimum row index [O(1)]
int getMinRowIndex()
get minimum row index [O(nnz)]

References aud.graph.matrix.SparseMatrixCS< T >.getMinColumnIndex(), aud.graph.matrix.SparseMatrixCS< T >.getMinRowIndex(), aud.graph.matrix.SparseMatrixCS< T >.getNumColumns(), and aud.graph.matrix.SparseMatrixCS< T >.getNumRows().

+ Here is the call graph for this function:

◆ toString()

String aud.graph.matrix.SparseMatrixCS< T >.toString ( )

Definition at line 216 of file SparseMatrixCS.java.

216 {
217 String rv="[";
218 for (Map.Entry<Coordinate,T> entry : mat_.entrySet()) {
219 rv+=entry+" ";
220 }
221 return rv.trim()+"]";
222 }

References aud.graph.matrix.SparseMatrixCS< T >.mat_.

Member Data Documentation

◆ mat_


The documentation for this class was generated from the following file: