Fixing pointers in solvers.
[JKLU.git] / src / test / java / edu / ufl / cise / klu / tdouble / demo / Dklu_demo.java
blob8a42a034dd4b6d684d3dbf6595dbe5313f8bb734
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 edu.ufl.cise.klu.common.KLU_common;
8 import edu.ufl.cise.klu.common.KLU_numeric;
9 import edu.ufl.cise.klu.common.KLU_symbolic;
10 import edu.ufl.cise.klu.common.KLU_version;
11 import static edu.ufl.cise.klu.tdouble.Dklu_analyze.klu_analyze;
12 import static edu.ufl.cise.klu.tdouble.Dklu_defaults.klu_defaults;
13 import static edu.ufl.cise.klu.tdouble.Dklu_diagnostics.klu_rgrowth;
14 import static edu.ufl.cise.klu.tdouble.Dklu_diagnostics.klu_condest;
15 import static edu.ufl.cise.klu.tdouble.Dklu_diagnostics.klu_rcond;
16 import static edu.ufl.cise.klu.tdouble.Dklu_diagnostics.klu_flops;
17 import static edu.ufl.cise.klu.tdouble.Dklu_factor.klu_factor;
18 import static edu.ufl.cise.klu.tdouble.Dklu_solve.klu_solve;
19 import edu.ufl.cise.klu.tdouble.io.MatrixInfo;
20 import edu.ufl.cise.klu.tdouble.io.MatrixSize;
21 import edu.ufl.cise.klu.tdouble.io.MatrixVectorReader;
23 /**
24 * Read in a Matrix Market matrix(using CHOLMOD) and solve a linear system.
26 public class Dklu_demo {
28 private static void REAL (double[] X, int i, double v)
30 X[2*i] = v;
33 private static void IMAG (double[] X, int i, double v)
35 X[2*i + 1] = v;
38 // private static double REAL (double[] X, int i)
39 // {
40 // return X[2*i] ;
41 // }
43 // private static double IMAG (double[] X, int i)
44 // {
45 // return X[2*i + 1] ;
46 // }
48 // private static double CABS (double[] X, int i)
49 // {
50 // return Math.sqrt(REAL(X, i) * REAL(X, i) + IMAG(X, i) * IMAG(X, i)) ;
51 // }
53 private static double MAX (double a, double b)
55 return (a) > (b) ? (a) : (b) ;
58 /**
60 * @param n A is n-by-n
61 * @param Ap size n+1, column pointers
62 * @param Ai size nz = Ap [n], row indices
63 * @param Ax size nz, numerical values
64 * @param isreal nonzero if A is real, 0 otherwise
65 * @param B size n, right-hand-side
66 * @param X size n, solution to Ax=b
67 * @param R size n, residual r = b-A*x
68 * @param lunz size 1, nnz(L+U+F)
69 * @param rnorm size 1, norm(b-A*x,1) / norm(A,1)
70 * @param Common default parameters and statistics
71 * @return 1 if successful, 0 otherwise
73 public static int klu_backslash(int n, int[] Ap, int[] Ai, double[] Ax,
74 int isreal, double[] B, double[] X, double[] R, int[] lunz,
75 double[] rnorm, KLU_common Common)
77 double anorm = 0, asum;
78 KLU_symbolic Symbolic;
79 KLU_numeric Numeric;
80 int i, j, p;
82 if (Ap == null || Ai == null || Ax == null || B == null || X == null || B == null)
83 return(0);
85 /* ---------------------------------------------------------------------- */
86 /* symbolic ordering and analysis */
87 /* ---------------------------------------------------------------------- */
89 Symbolic = klu_analyze(n, Ap, Ai, Common);
90 if (Symbolic == null) return(0);
92 if (isreal != 0)
95 /* ------------------------------------------------------------------ */
96 /* factorization */
97 /* ------------------------------------------------------------------ */
99 Numeric = klu_factor(Ap, Ai, Ax, Symbolic, Common);
100 if (Numeric == null)
102 //Dklu_free_symbolic.klu_free_symbolic(Symbolic, Common);
103 return(0);
106 /* ------------------------------------------------------------------ */
107 /* statistics(not required to solve Ax=b) */
108 /* ------------------------------------------------------------------ */
110 klu_rgrowth(Ap, Ai, Ax, Symbolic, Numeric, Common);
111 klu_condest(Ap, Ax, Symbolic, Numeric, Common);
112 klu_rcond(Symbolic, Numeric, Common);
113 klu_flops(Symbolic, Numeric, Common);
114 lunz[0] = Numeric.lnz + Numeric.unz - n +
115 (Numeric.Offp != null ? Numeric.Offp [n] : 0);
117 /* ------------------------------------------------------------------ */
118 /* solve Ax=b */
119 /* ------------------------------------------------------------------ */
121 for(i = 0; i < n; i++)
123 X [i] = B [i];
125 klu_solve(Symbolic, Numeric, n, 1, X, 0, Common);
127 /* ------------------------------------------------------------------ */
128 /* compute residual, rnorm = norm(b-Ax,1) / norm(A,1) */
129 /* ------------------------------------------------------------------ */
131 for(i = 0; i < n; i++)
133 R [i] = B [i];
135 for(j = 0; j < n; j++)
137 asum = 0;
138 for(p = Ap [j]; p < Ap [j+1]; p++)
140 /* R(i) -= A(i,j) * X(j) */
141 R [Ai [p]] -= Ax [p] * X [j];
142 asum += Math.abs(Ax [p]);
144 anorm = MAX (anorm, asum);
146 rnorm[0] = 0;
147 for(i = 0; i < n; i++)
149 rnorm[0] = MAX (rnorm[0], Math.abs(R [i]));
152 /* ------------------------------------------------------------------ */
153 /* free numeric factorization */
154 /* ------------------------------------------------------------------ */
156 //klu_free_numeric(Numeric, Common);
157 Numeric = null;
160 else
162 throw new UnsupportedOperationException();
164 /* ------------------------------------------------------------------ */
165 /* statistics(not required to solve Ax=b) */
166 /* ------------------------------------------------------------------ */
168 // Numeric = klu_z_factor(Ap, Ai, Ax, Symbolic, Common);
169 // if (Numeric == null)
170 // {
171 // klu_free_symbolic(Symbolic, Common);
172 // return(0);
173 // }
175 // /* ------------------------------------------------------------------ */
176 // /* statistics */
177 // /* ------------------------------------------------------------------ */
179 // klu_z_rgrowth(Ap, Ai, Ax, Symbolic, Numeric, Common);
180 // klu_z_condest(Ap, Ax, Symbolic, Numeric, Common);
181 // klu_z_rcond(Symbolic, Numeric, Common);
182 // klu_z_flops(Symbolic, Numeric, Common);
183 // lunz = Numeric.lnz + Numeric.unz - n +
184 // (Numeric.Offp != null ? Numeric.Offp [n] : 0);
186 // /* ------------------------------------------------------------------ */
187 // /* solve Ax=b */
188 // /* ------------------------------------------------------------------ */
190 // for(i = 0; i < 2*n; i++)
191 // {
192 // X [i] = B [i];
193 // }
194 // klu_z_solve(Symbolic, Numeric, n, 1, X, Common);
196 // /* ------------------------------------------------------------------ */
197 // /* compute residual, rnorm = norm(b-Ax,1) / norm(A,1) */
198 // /* ------------------------------------------------------------------ */
200 // for(i = 0; i < 2*n; i++)
201 // {
202 // R [i] = B [i];
203 // }
204 // for(j = 0; j < n; j++)
205 // {
206 // asum = 0;
207 // for(p = Ap [j]; p < Ap [j+1]; p++)
208 // {
209 // /* R(i) -= A(i,j) * X(j) */
210 // i = Ai [p];
211 // REAL(R,i) -= REAL(Ax,p) * REAL(X,j) - IMAG(Ax,p) * IMAG(X,j);
212 // IMAG(R,i) -= IMAG(Ax,p) * REAL(X,j) + REAL(Ax,p) * IMAG(X,j);
213 // asum += CABS(Ax, p);
214 // }
215 // anorm = MAX(anorm, asum);
216 // }
217 // rnorm = 0;
218 // for(i = 0; i < n; i++)
219 // {
220 // rnorm = MAX (rnorm, CABS(R, i));
221 // }
223 // /* ------------------------------------------------------------------ */
224 // /* free numeric factorization */
225 // /* ------------------------------------------------------------------ */
227 // klu_z_free_numeric(&Numeric, Common);
230 /* ---------------------------------------------------------------------- */
231 /* free symbolic analysis, and residual */
232 /* ---------------------------------------------------------------------- */
234 //klu_free_symbolic(Symbolic, Common);
235 Symbolic = null;
237 return (1);
241 * Given a sparse matrix A, set up a right-hand-side and solve X = A\b.
243 public static void klu_demo(int n, int[] Ap, int[] Ai, double[] Ax,
244 int isreal)
246 KLU_common Common = new KLU_common();
247 double[] B, X, R;
248 int i;
249 int[] lunz = new int[1];
250 double[] rnorm = new double[1];
252 System.out.printf("KLU: %s, version: %d.%d.%d\n",
253 KLU_version.KLU_DATE, KLU_version.KLU_MAIN_VERSION,
254 KLU_version.KLU_SUB_VERSION, KLU_version.KLU_SUBSUB_VERSION);
256 /* ---------------------------------------------------------------------- */
257 /* set defaults */
258 /* ---------------------------------------------------------------------- */
260 klu_defaults(Common);
262 /* ---------------------------------------------------------------------- */
263 /* create a right-hand-side */
264 /* ---------------------------------------------------------------------- */
266 if (isreal != 0)
268 /* B = 1 +(1:n)/n */
269 B = new double[n];
270 X = new double[n];
271 R = new double[n];
272 if (B != null)
274 for(i = 0; i < n; i++)
276 B [i] = 1 + ((double) i+1) / ((double) n);
280 else
282 /* real(B) = 1 +(1:n)/n, imag(B) = (n:-1:1)/n */
283 B = new double[2 * n];
284 X = new double[2 * n];
285 R = new double[2 * n];
286 if (B != null)
288 for(i = 0; i < n; i++)
290 REAL(B, i, 1 + ((double) i+1) / ((double) n));
291 IMAG(B, i, ((double) n-i) / ((double) n));
296 /* ---------------------------------------------------------------------- */
297 /* X = A\b using KLU and print statistics */
298 /* ---------------------------------------------------------------------- */
300 if (klu_backslash(n, Ap, Ai, Ax, isreal, B, X, R, lunz, rnorm, Common) == 0)
302 System.out.printf("KLU failed\n");
304 else
306 System.out.printf("n %d nnz(A) %d nnz(L+U+F) %d resid %g\n" +
307 "recip growth %g condest %g rcond %g flops %g\n",
308 n, Ap [n], lunz[0], rnorm, Common.rgrowth, Common.condest,
309 Common.rcond, Common.flops);
312 /* ---------------------------------------------------------------------- */
313 /* free the problem */
314 /* ---------------------------------------------------------------------- */
316 if (isreal != 0)
318 B = null;
319 X = null;
320 R = null;
322 else
324 B = null;
325 X = null;
326 R = null;
328 // System.out.printf("peak memory usage: %g bytes\n\n",(double)(Common.mempeak));
332 * Read in a sparse matrix in Matrix Market format using CHOLMOD, and then
333 * solve Ax=b with KLU. Note that CHOLMOD is only used to read the matrix.
335 public static void main(String[] args) {
337 FileReader fileReader;
338 MatrixVectorReader reader;
339 MatrixInfo info;
340 MatrixSize size;
342 try {
343 fileReader = new FileReader(args[0]);
344 reader = new MatrixVectorReader(fileReader);
346 info = reader.readMatrixInfo();
347 size = reader.readMatrixSize(info);
349 if (size.numRows() != size.numColumns() || !info.isGeneral()
350 ||(!(info.isReal() || info.isComplex()))
351 || !info.isCoordinate())
353 System.out.printf("invalid matrix\n");
355 else
357 int nnz = size.numEntries();
358 int[] i = new int[nnz];
359 int[] p = new int[nnz];
360 double[] x;
362 if (info.isComplex()) {
363 double[] xR = new double[nnz];
364 double[] xI = new double[nnz];
366 reader.readCoordinate(i, p, xR, xI);
368 x = new double[2 * nnz];
369 for (int j = 0; j < nnz; j++) {
370 REAL(x, j, xR[j]);
371 IMAG(x, j, xI[j]);
374 else
376 x = new double[nnz];
377 reader.readCoordinate(i, p, x);
381 klu_demo(size.numRows(), p, i, x, info.isReal() ? 1 : 0);
384 } catch (FileNotFoundException e) {
385 e.printStackTrace();
386 } catch (IOException e) {
387 e.printStackTrace();