1 package edu
.ufl
.cise
.klu
.tdouble
.demo
;
3 import java
.io
.FileNotFoundException
;
4 import java
.io
.FileReader
;
5 import java
.io
.IOException
;
7 import org
.apache
.commons
.lang
.mutable
.MutableDouble
;
8 import org
.apache
.commons
.lang
.mutable
.MutableInt
;
10 import edu
.ufl
.cise
.klu
.common
.KLU_common
;
11 import edu
.ufl
.cise
.klu
.common
.KLU_numeric
;
12 import edu
.ufl
.cise
.klu
.common
.KLU_symbolic
;
13 import edu
.ufl
.cise
.klu
.common
.KLU_version
;
14 import edu
.ufl
.cise
.klu
.tdouble
.Dklu_analyze
;
15 import edu
.ufl
.cise
.klu
.tdouble
.Dklu_defaults
;
16 import edu
.ufl
.cise
.klu
.tdouble
.Dklu_diagnostics
;
17 import edu
.ufl
.cise
.klu
.tdouble
.Dklu_factor
;
18 import edu
.ufl
.cise
.klu
.tdouble
.Dklu_free_numeric
;
19 import edu
.ufl
.cise
.klu
.tdouble
.Dklu_free_symbolic
;
20 import edu
.ufl
.cise
.klu
.tdouble
.Dklu_solve
;
21 import edu
.ufl
.cise
.klu
.tdouble
.io
.MatrixInfo
;
22 import edu
.ufl
.cise
.klu
.tdouble
.io
.MatrixSize
;
23 import edu
.ufl
.cise
.klu
.tdouble
.io
.MatrixVectorReader
;
26 * Read in a Matrix Market matrix(using CHOLMOD) and solve a linear system.
28 public class Dklu_demo
{
30 private static void REAL (double[] X
, int i
, double v
)
35 private static void IMAG (double[] X
, int i
, double v
)
40 private static double REAL (double[] X
, int i
)
45 private static double IMAG (double[] X
, int i
)
50 private static double CABS (double[] X
, int i
)
52 return Math
.sqrt(REAL(X
, i
) * REAL(X
, i
) + IMAG(X
, i
) * IMAG(X
, i
)) ;
55 private static double MAX (double a
, double b
)
57 return (a
) > (b
) ?
(a
) : (b
) ;
62 * @param n A is n-by-n
63 * @param Ap size n+1, column pointers
64 * @param Ai size nz = Ap [n], row indices
65 * @param Ax size nz, numerical values
66 * @param isreal nonzero if A is real, 0 otherwise
67 * @param B size n, right-hand-side
68 * @param X size n, solution to Ax=b
69 * @param R size n, residual r = b-A*x
70 * @param lunz nnz(L+U+F)
71 * @param rnorm norm(b-A*x,1) / norm(A,1)
72 * @param Common default parameters and statistics
73 * @return 1 if successful, 0 otherwise
75 public static int klu_backslash(int n
, int[] Ap
, int[] Ai
, double[] Ax
,
76 int isreal
, double[] B
, double[] X
, double[] R
, MutableInt lunz
,
77 MutableDouble rnorm
, KLU_common Common
)
79 double anorm
= 0, asum
;
80 KLU_symbolic Symbolic
;
84 if (Ap
== null || Ai
== null || Ax
== null || B
== null || X
== null ||
87 /* ---------------------------------------------------------------------- */
88 /* symbolic ordering and analysis */
89 /* ---------------------------------------------------------------------- */
91 Symbolic
= Dklu_analyze
.klu_analyze(n
, Ap
, Ai
, Common
);
92 if (Symbolic
== null) return(0);
97 /* ------------------------------------------------------------------ */
99 /* ------------------------------------------------------------------ */
101 Numeric
= Dklu_factor
.klu_factor(Ap
, Ai
, Ax
, Symbolic
, Common
);
104 Dklu_free_symbolic
.klu_free_symbolic(Symbolic
, Common
);
108 /* ------------------------------------------------------------------ */
109 /* statistics(not required to solve Ax=b) */
110 /* ------------------------------------------------------------------ */
112 Dklu_diagnostics
.klu_rgrowth(Ap
, Ai
, Ax
, Symbolic
, Numeric
, Common
);
113 Dklu_diagnostics
.klu_condest(Ap
, Ax
, Symbolic
, Numeric
, Common
);
114 Dklu_diagnostics
.klu_rcond(Symbolic
, Numeric
, Common
);
115 Dklu_diagnostics
.klu_flops(Symbolic
, Numeric
, Common
);
116 lunz
.setValue( Numeric
.lnz
+ Numeric
.unz
- n
+
117 ((Numeric
.Offp
!= null) ?
(Numeric
.Offp
[n
]) : 0) );
119 /* ------------------------------------------------------------------ */
121 /* ------------------------------------------------------------------ */
123 for(i
= 0; i
< n
; i
++)
127 Dklu_solve
.klu_solve(Symbolic
, Numeric
, n
, 1, X
, Common
);
129 /* ------------------------------------------------------------------ */
130 /* compute residual, rnorm = norm(b-Ax,1) / norm(A,1) */
131 /* ------------------------------------------------------------------ */
133 for(i
= 0; i
< n
; i
++)
137 for(j
= 0; j
< n
; j
++)
140 for(p
= Ap
[j
]; p
< Ap
[j
+1]; p
++)
142 /* R(i) -= A(i,j) * X(j) */
143 R
[Ai
[p
]] -= Ax
[p
] * X
[j
];
144 asum
+= Math
.abs(Ax
[p
]);
146 anorm
= MAX (anorm
, asum
);
149 for(i
= 0; i
< n
; i
++)
151 rnorm
.setValue( MAX (rnorm
.doubleValue(), Math
.abs(R
[i
])) );
154 /* ------------------------------------------------------------------ */
155 /* free numeric factorization */
156 /* ------------------------------------------------------------------ */
158 Dklu_free_numeric
.klu_free_numeric(Numeric
, Common
);
163 throw new UnsupportedOperationException();
165 /* ------------------------------------------------------------------ */
166 /* statistics(not required to solve Ax=b) */
167 /* ------------------------------------------------------------------ */
169 // Numeric = DZklu_factor.klu_z_factor(Ap, Ai, Ax, Symbolic, Common);
170 // if (Numeric == null)
172 // DZklu_free_symbolic.klu_free_symbolic(Symbolic, Common);
176 // /* ------------------------------------------------------------------ */
178 // /* ------------------------------------------------------------------ */
180 // DZklu_diagnostics.klu_z_rgrowth(Ap, Ai, Ax, Symbolic, Numeric, Common);
181 // DZklu_diagnostics.klu_z_condest(Ap, Ax, Symbolic, Numeric, Common);
182 // DZklu_diagnostics.klu_z_rcond(Symbolic, Numeric, Common);
183 // DZklu_diagnostics.klu_z_flops(Symbolic, Numeric, Common);
184 // lunz = Numeric.lnz + Numeric.unz - n +
185 // ((Numeric.Offp != null) ? (Numeric.Offp [n]) : 0);
187 // /* ------------------------------------------------------------------ */
189 // /* ------------------------------------------------------------------ */
191 // for(i = 0; i < 2*n; i++)
195 // DZklu_solve.klu_z_solve(Symbolic, Numeric, n, 1, X, Common);
197 // /* ------------------------------------------------------------------ */
198 // /* compute residual, rnorm = norm(b-Ax,1) / norm(A,1) */
199 // /* ------------------------------------------------------------------ */
201 // for(i = 0; i < 2*n; i++)
205 // for(j = 0; j < n; j++)
208 // for(p = Ap [j]; p < Ap [j+1]; p++)
210 // /* R(i) -= A(i,j) * X(j) */
212 // REAL(R,i) -= REAL(Ax,p) * REAL(X,j) - IMAG(Ax,p) * IMAG(X,j);
213 // IMAG(R,i) -= IMAG(Ax,p) * REAL(X,j) + REAL(Ax,p) * IMAG(X,j);
214 // asum += CABS(Ax, p);
216 // anorm = MAX(anorm, asum);
219 // for(i = 0; i < n; i++)
221 // rnorm = MAX (rnorm, CABS(R, i));
224 // /* ------------------------------------------------------------------ */
225 // /* free numeric factorization */
226 // /* ------------------------------------------------------------------ */
228 // DZklu_free_numeric.klu_z_free_numeric(&Numeric, Common);
231 /* ---------------------------------------------------------------------- */
232 /* free symbolic analysis, and residual */
233 /* ---------------------------------------------------------------------- */
235 Dklu_free_symbolic
.klu_free_symbolic(Symbolic
, Common
);
240 * Given a sparse matrix A, set up a right-hand-side and solve X = A\b.
242 public static void klu_demo(int n
, int[] Ap
, int[] Ai
, double[] Ax
,
245 MutableDouble rnorm
= new MutableDouble();
246 KLU_common Common
= new KLU_common();
249 MutableInt lunz
= new MutableInt();
251 System
.out
.printf("KLU: %s, version: %d.%d.%d\n",
252 KLU_version
.KLU_DATE
, KLU_version
.KLU_MAIN_VERSION
,
253 KLU_version
.KLU_SUB_VERSION
, KLU_version
.KLU_SUBSUB_VERSION
);
255 /* ---------------------------------------------------------------------- */
257 /* ---------------------------------------------------------------------- */
259 Dklu_defaults
.klu_defaults(Common
);
261 /* ---------------------------------------------------------------------- */
262 /* create a right-hand-side */
263 /* ---------------------------------------------------------------------- */
268 // B = klu_malloc(n, sizeof(Double), Common);
269 // X = klu_malloc(n, sizeof(Double), Common);
270 // R = klu_malloc(n, sizeof(Double), Common);
276 for(i
= 0; i
< n
; i
++)
278 B
[i
] = 1 + ((double) i
+1) / ((double) n
);
284 /* real(B) = 1 +(1:n)/n, imag(B) = (n:-1:1)/n */
285 // B = klu_malloc(n, 2 * sizeof(Double), Common);
286 // X = klu_malloc(n, 2 * sizeof(Double), Common);
287 // R = klu_malloc(n, 2 * sizeof(Double), Common);
288 B
= new double[2 * n
];
289 X
= new double[2 * n
];
290 R
= new double[2 * n
];
293 for(i
= 0; i
< n
; i
++)
295 REAL(B
, i
, 1 + ((double) i
+1) / ((double) n
));
296 IMAG(B
, i
, ((double) n
-i
) / ((double) n
));
301 /* ---------------------------------------------------------------------- */
302 /* X = A\b using KLU and print statistics */
303 /* ---------------------------------------------------------------------- */
305 if (klu_backslash(n
, Ap
, Ai
, Ax
, isreal
, B
, X
, R
, lunz
, rnorm
, Common
) == 0)
307 System
.out
.printf("KLU failed\n");
311 System
.out
.printf("n %d nnz(A) %d nnz(L+U+F) %d resid %g\n" +
312 "recip growth %g condest %g rcond %g flops %g\n",
313 n
, Ap
[n
], lunz
.intValue(), rnorm
, Common
.rgrowth
, Common
.condest
,
314 Common
.rcond
, Common
.flops
);
317 /* ---------------------------------------------------------------------- */
318 /* free the problem */
319 /* ---------------------------------------------------------------------- */
323 // klu_free(B, n, sizeof(Double), Common);
324 // klu_free(X, n, sizeof(Double), Common);
325 // klu_free(R, n, sizeof(Double), Common);
332 // klu_free(B, 2*n, sizeof(Double), Common);
333 // klu_free(X, 2*n, sizeof(Double), Common);
334 // klu_free(R, 2*n, sizeof(Double), Common);
339 //System.out.printf("peak memory usage: %g bytes\n\n",(double)(Common.mempeak));
343 * Read in a sparse matrix in Matrix Market format using CHOLMOD, and then
344 * solve Ax=b with KLU. Note that CHOLMOD is only used to read the matrix.
346 public static void main(String
[] args
) {
348 FileReader fileReader
;
349 MatrixVectorReader reader
;
354 fileReader
= new FileReader(args
[0]);
355 reader
= new MatrixVectorReader(fileReader
);
357 info
= reader
.readMatrixInfo();
358 size
= reader
.readMatrixSize(info
);
360 if (size
.numRows() != size
.numColumns() || !info
.isGeneral()
361 ||(!(info
.isReal() || info
.isComplex()))
362 || !info
.isCoordinate())
364 System
.out
.printf("invalid matrix\n");
368 int nnz
= size
.numEntries();
369 int[] i
= new int[nnz
];
370 int[] p
= new int[nnz
];
373 if (info
.isComplex()) {
374 double[] xR
= new double[nnz
];
375 double[] xI
= new double[nnz
];
377 reader
.readCoordinate(i
, p
, xR
, xI
);
379 x
= new double[2 * nnz
];
380 for (int j
= 0; j
< nnz
; j
++) {
386 reader
.readCoordinate(i
, p
, x
);
390 klu_demo(size
.numRows(), p
, i
, x
, info
.isReal() ?
1 : 0);
393 } catch (FileNotFoundException e
) {
395 } catch (IOException e
) {