1package aud.graph.matrix;
4import java.util.TreeMap;
5import java.util.SortedMap;
38 protected TreeMap<Coordinate,T>
mat_;
43 mat_=
new TreeMap<Coordinate,T>();
47 mat_=
new TreeMap<Coordinate,T>(other.
mat_);
64 int imin=Integer.MAX_VALUE;
69 return (imin==Integer.MAX_VALUE) ? 0 : imin;
80 TreeMap<Coordinate,T> check(
int i,
int j) {
82 throw new IndexOutOfBoundsException();
90 public T
get(
int i,
int j) {
102 public T
set(
int i,
int j,T data) {
return _set(i,j,data); }
105 T _set(
int i,
int j,T data) {
122 @SuppressWarnings(
"unchecked")
124 SortedMap<Coordinate,T> sub=
127 Object[] a=sub.values().toArray();
130 for (
int i=0;i<a.length;++i)
139 SortedMap<Coordinate,T> sub=
142 int[] idx=
new int[sub.size()];
147 assert(i==idx.length);
176 m.
mat_.put(entry.getKey(),1);
189 int[] ri=
new int[
nnz()];
192 ri[i++]=entry.getKey().i;
199 int[] ci=
new int[
nnz()];
202 ci[i++]=entry.getKey().j;
207 @SuppressWarnings(
"unchecked")
209 T[] values=(T[])
new Object[
nnz()];
212 values[i++]=entry.getValue();
221 return rv.trim()+
"]";
230 String rv=
"\\matrix ("+name+
") [matrix of math nodes]\n{\n";
232 Class<?> intclass=Integer.valueOf(1).getClass();
238 rv+=(v.getClass()==intclass) ? v.toString() :
"1";
254 FileOutputStream f=
new FileOutputStream(svgfile);
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());
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());
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());
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());
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());
288 f.write(
"\n\n".getBytes());
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()));
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());
300 f.write(
"</svg>\n".getBytes());
302 }
catch (IOException e) {
303 System.err.println(
"ERROR: "+e.getMessage());
313 String s=
"\\begin{tikzpicture}[dot/.style={draw=none,fill=c},scale=1]\n";
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"+
323 " \\draw (\\x,-2.5pt) -- (\\x,2.5pt);\n"+
324 " \\node[above] at (\\x-0.5,0) {{\\scriptsize \\x}};\n"+
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" +
342 s+=
"\\end{tikzpicture}\n";
351 tmp=File.createTempFile(
"aud-",
"svg");
353 catch (IOException e) {
354 System.err.println(
"ERROR: "+e.getMessage());
Implementation of an array-based vector.
void push_back(T obj)
insert new entry obj at the end [O(1) for sufficient capacity]
void reserve(int n)
Ensure capacity for n entries.
Row/column coordinates (i,j).
Simple sparse matrix data structure.
int[] getRowIndices()
Get array of row indices.
int getMinColumnIndex()
get minimum row index [O(1)]
int getNumRows()
computes from maximium row index [O(nnz)]
int[] getColumnRowIndices(int j)
get row indices in column j as array
int getMinRowIndex()
get minimum row index [O(nnz)]
int columnDegree(int j)
get number of nonzero entries in column j
int getNumColumns()
computed from maximum column index [O(1)]
SVGViewer spy(String caption, Colormap< T > colormap)
render spy plot in new window
SparseMatrixCS< Integer > spones()
Get nonzero pattern.
SparseMatrixCS(SparseMatrixCS< T > other)
copy constructor
String spyTikZ(boolean rulers, Colormap< T > colormap)
SparseMatrixCS()
create empty matrix with "arbitrarily growing" dimensions
Vector< T > getColumnEntries(int j)
get entries in column j as array
File renderSpySVG(File svgfile, Colormap< T > colormap)
TreeMap< Coordinate, T > mat_
String toLaTeX(String name)
get LaTeX code for displaying (TikZ) matrix
SparseMatrixCS< T > getTransposed()
get transposed matrix
int nnz()
get number of nonzero entries
int[] getColumnIndices()
Get array of column indices in same order as for getRowIndices.
T[] getValues()
Get array of values in same order as for getRowIndices.
simple interface for color map
int getRGB(T data)
Map data to rgb color.
Simple SVG viewer based on Batik's SVGCanvas.
static SVGViewer displayWindow(File file, String caption)
create new SVGViewer (toplevel window) and display file
utilities (not related to AuD lecture)
AuD lecture: Data structures, algorithms, examples.