Example 7: Solve a Small Linear System With Logging
A solution to a small linear system is found. The coefficient matrix is stored as a full matrix and no preconditioning is used. Typically, preconditioning is required to achieve convergence in a reasonable number of iterations. Logging is enabled so that intermediate output and a summary report are generated.
import com.imsl.math.*;
import com.imsl.Messages;
import com.imsl.IMSLException;
import java.util.logging.Logger;
import java.util.logging.LogRecord;
import java.util.logging.Level;
import java.util.logging.Handler;
public class GenMinResEx7 implements GenMinRes.Function {
static private double a[][] = {
{33.0, 16.0, 72.0},
{-24.0, -10.0, -57.0},
{18.0, -11.0, 7.0}
};
static private double b[] = {129.0, -96.0, 8.5};
// If A were to be read in from some outside source the //
// code to read the matrix could reside in a constructor. //
public void amultp(double p[], double z[]) {
double [] result;
result = Matrix.multiply(a,p);
System.arraycopy(result,0,z,0,z.length);
}
public static void main(String args[]) throws Exception {
int n = 3;
GenMinResEx7 atp = new GenMinResEx7();
// Construct a GenMinRes object
GenMinRes gnmnrs = new GenMinRes(n, atp);
Logger logger = gnmnrs.getLogger();
Handler h = new java.util.logging.FileHandler("GenMinReslog.txt");
logger.addHandler(h);
logger.setLevel(Level.FINER);
h.setFormatter(new GenMinRes.Formatter());
// Solve Ax = b
new PrintMatrix("x").print(gnmnrs.solve(b));
}
}
Output
x
0
0 1
1 1.5
2 1
Link to Java source.