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

Simple sparse matrix data structure. More...

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

Public Member Functions

 SparseMatrix ()
 create empty matrix More...
 
 SparseMatrix (boolean symmetric)
 create empty matrix (see isSymmetricMatrix) More...
 
 SparseMatrix (SparseMatrix< T > other)
 copy constructor More...
 
 SparseMatrix (SparseMatrix< T > other, boolean transpose)
 copy constructor More...
 
boolean isSymmetricMatrix ()
 Was matrix created explicitly as symmetric matrix? More...
 
int getNumRows ()
 computed from maximum column index [O(1)] More...
 
int getMinRowIndex ()
 get minimum column index [O(1)] More...
 
set (int i, int j, T data)
 Set entry (i,j) to data [O(log(nnz))]. More...
 
Vector< T > getRowEntries (int i)
 get entries in row i as array More...
 
int[] getRowColumnIndices (int i)
 get column indices in row i as array More...
 
int rowDegree (int i)
 get number of nonzero entries in row i More...
 
SparseMatrixCS< T > getTransposed ()
 get transposed matrix More...
 
- Public Member Functions inherited from aud.graph.matrix.SparseMatrixCS< T >
 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...
 

Static Public Member Functions

static void main (String args[])
 Example and test: show aud.util.ColormapCount color map. More...
 

Protected Attributes

SparseMatrixCS< T > rmat_ = null
 Store transposed. More...
 
- Protected Attributes inherited from aud.graph.matrix.SparseMatrixCS< T >
TreeMap< Coordinate, T > mat_
 

Detailed Description

Simple sparse matrix data structure.

Sparse matrix with efficient access to column and row vectors based on SparseMatrixCS: the implementation essentially stores the transposed SparseMatrixCS for row access.

No extra matrix is stored if the matrix is declared symmetric at construction. However, to keep the code simple, the upper and lower triangular matrices are stored in this case.

See also
SparseMatrixCS

Definition at line 19 of file SparseMatrix.java.

Constructor & Destructor Documentation

◆ SparseMatrix() [1/4]

create empty matrix

Definition at line 32 of file SparseMatrix.java.

32 {
33 this(false);
34 }

◆ SparseMatrix() [2/4]

aud.graph.matrix.SparseMatrix< T >.SparseMatrix ( boolean  symmetric)

create empty matrix (see isSymmetricMatrix)

Definition at line 37 of file SparseMatrix.java.

37 {
38 super();
39 rmat_=symmetric ? this : new SparseMatrixCS<T>();
40 }
SparseMatrixCS< T > rmat_
Store transposed.

References aud.graph.matrix.SparseMatrix< T >.rmat_.

◆ SparseMatrix() [3/4]

copy constructor

Definition at line 42 of file SparseMatrix.java.

42 {
43 super(other);
44 rmat_= other.isSymmetricMatrix() ?
45 new SparseMatrixCS<T>(other.rmat_) : this;
46 }

References aud.graph.matrix.SparseMatrix< T >.isSymmetricMatrix(), and aud.graph.matrix.SparseMatrix< T >.rmat_.

+ Here is the call graph for this function:

◆ SparseMatrix() [4/4]

aud.graph.matrix.SparseMatrix< T >.SparseMatrix ( SparseMatrix< T >  other,
boolean  transpose 
)

copy constructor

Definition at line 48 of file SparseMatrix.java.

48 {
49 super((transpose && !other.isSymmetricMatrix()) ? other.rmat_ : other);
50 if (other.isSymmetricMatrix())
51 rmat_=this;
52 else
53 rmat_=transpose ?
54 new SparseMatrixCS<T>(other) : new SparseMatrixCS<T>(other.rmat_);
55 }
SparseMatrixCS()
create empty matrix with "arbitrarily growing" dimensions

References aud.graph.matrix.SparseMatrix< T >.isSymmetricMatrix(), and aud.graph.matrix.SparseMatrix< T >.rmat_.

+ Here is the call graph for this function:

Member Function Documentation

◆ getMinRowIndex()

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

get minimum column index [O(1)]

Reimplemented from aud.graph.matrix.SparseMatrixCS< T >.

Definition at line 65 of file SparseMatrix.java.

65 {
66 return rmat_.getMinColumnIndex();
67 }

References aud.graph.matrix.SparseMatrix< T >.rmat_.

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

+ Here is the caller graph for this function:

◆ getNumRows()

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

computed from maximum column index [O(1)]

Reimplemented from aud.graph.matrix.SparseMatrixCS< T >.

Definition at line 61 of file SparseMatrix.java.

61 {
62 return rmat_.getNumColumns();
63 }

References aud.graph.matrix.SparseMatrix< T >.rmat_.

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

+ Here is the caller graph for this function:

◆ getRowColumnIndices()

int[] aud.graph.matrix.SparseMatrix< T >.getRowColumnIndices ( int  i)

get column indices in row i as array

Exceptions
IndexOutOfBoundsExceptionon error (non-positive index)

Definition at line 87 of file SparseMatrix.java.

87 {
88 return rmat_.getColumnRowIndices(i);
89 }

References aud.graph.matrix.SparseMatrix< T >.rmat_.

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

+ Here is the caller graph for this function:

◆ getRowEntries()

Vector< T > aud.graph.matrix.SparseMatrix< T >.getRowEntries ( int  i)

get entries in row i as array

Exceptions
IndexOutOfBoundsExceptionon error (non-positive index)

Definition at line 80 of file SparseMatrix.java.

80 {
81 return rmat_.getColumnEntries(i);
82 }

References aud.graph.matrix.SparseMatrix< T >.rmat_.

Referenced by aud.graph.matrix.SparseMatrix< T >.rowDegree(), aud.test.SparseMatrixTest.testMatrix(), and aud.test.SparseMatrixTest.testSymmatrixMatrix().

+ Here is the caller graph for this function:

◆ getTransposed()

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

get transposed matrix

Reimplemented from aud.graph.matrix.SparseMatrixCS< T >.

Definition at line 97 of file SparseMatrix.java.

97 {
98 return new SparseMatrixCS<T>(rmat_);
99 }

References aud.graph.matrix.SparseMatrix< T >.rmat_.

◆ isSymmetricMatrix()

◆ main()

static void aud.graph.matrix.SparseMatrix< T >.main ( String  args[])
static

Example and test: show aud.util.ColormapCount color map.

Definition at line 106 of file SparseMatrix.java.

106 {
107 SparseMatrix<Integer> m=new SparseMatrix<Integer>();
108
109 m.set(1,1,-1);
110 m.set(2,1,0);
111 m.set(3,1,1);
112 m.set(4,1,2);
113 m.set(5,1,3);
114 m.set(6,1,4);
115 m.set(7,1,5);
116 m.set(8,1,6);
117 m.set(9,1,7);
118 m.set(10,1,8);
119 m.set(11,1,9);
120 m.set(12,1,10);
121 m.set(13,1,11);
122 m.set(1,1,11);
123
124 m.set(1,5,11);
125
126 Colormap<Integer> colormap= new ColormapCount();
127
128 //m.renderSpySVG(new java.io.File("colormap.svg"),colormap);
129
130 m.spy("colormap",colormap);
131
132 System.out.println(m.spyTikZ(true,colormap));
133 }
color map for (small) positive integer counts
simple interface for color map
Definition: Colormap.java:4

References aud.graph.matrix.SparseMatrix< T >.set(), aud.graph.matrix.SparseMatrixCS< T >.spy(), and aud.graph.matrix.SparseMatrixCS< T >.spyTikZ().

+ Here is the call graph for this function:

◆ rowDegree()

int aud.graph.matrix.SparseMatrix< T >.rowDegree ( int  i)

get number of nonzero entries in row i

Exceptions
IndexOutOfBoundsExceptionon error (non-positive index)
Returns
lenght of getRowEntries

Definition at line 95 of file SparseMatrix.java.

95{ return getRowEntries(i).size(); }
Vector< T > getRowEntries(int i)
get entries in row i as array

References aud.graph.matrix.SparseMatrix< T >.getRowEntries().

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

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

◆ set()

T aud.graph.matrix.SparseMatrix< 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 from aud.graph.matrix.SparseMatrixCS< T >.

Definition at line 69 of file SparseMatrix.java.

69 {
70 T v,vr;
71 v=super.set(i,j,data);
72 vr=rmat_._set(j,i,data);
73 assert(v==vr || i==j);
74 return v;
75 }

References aud.graph.matrix.SparseMatrix< T >.rmat_.

Referenced by aud.graph.matrix.SparseMatrix< T >.main(), aud.test.AdjacencyMatrixTest.testMatrix(), aud.test.SparseMatrixTest.testMatrix(), aud.test.AdjacencyMatrixTest.testSymmatrixMatrix(), and aud.test.SparseMatrixTest.testSymmatrixMatrix().

+ Here is the caller graph for this function:

Member Data Documentation

◆ rmat_

SparseMatrixCS<T> aud.graph.matrix.SparseMatrix< T >.rmat_ = null
protected

Store transposed.

rmat_ stores the transposed matrix to enable efficient access to rows. For symmetric matrices (isSymmetricMatrix, rmat_ is a reference to
this
! (This convention keeps the implementation simple but doubles the number of entries to be stored.)

Definition at line 29 of file SparseMatrix.java.

Referenced by aud.graph.matrix.SparseMatrix< T >.getMinRowIndex(), aud.graph.matrix.SparseMatrix< T >.getNumRows(), aud.graph.matrix.SparseMatrix< T >.getRowColumnIndices(), aud.graph.matrix.SparseMatrix< T >.getRowEntries(), aud.graph.matrix.SparseMatrix< T >.getTransposed(), aud.graph.matrix.SparseMatrix< T >.isSymmetricMatrix(), aud.graph.matrix.SparseMatrix< T >.set(), and aud.graph.matrix.SparseMatrix< T >.SparseMatrix().


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