基本情報技術者試験 平成15年度・秋期・午後 問12 ソースプログラム
class MatrixArithmeticException extends Exception { }
public class Matrix {
private double[][] values;
public Matrix(double[][] initVal) {
values = new double[initVal.length][initVal[0].length];
// 配列 initVal の内容を配列 values にコピーする
for (int i = 0; i < initVal.length; i++) {
System.arraycopy(initVal[i], 0, values[i], 0,
initVal[i].length);
}
}
public Matrix add(Matrix m) throws MatrixArithmeticException {
if (values.length != m.values.length ||
values[0].length != m.values[0].length)
throw new MatrixArithmeticException();
double[][] result =
new double[values.length][values[0].length];
for (int i = 0; i < values.length; i++) {
for (int j = 0; j < values[0].length; j++) {
result[i][j] = values[i][j] + m.values[i][j];
}
}
return new Matrix(result);
}
public Matrix multiply(Matrix m)
throws MatrixArithmeticException {
if (values[0].length != m.values.length)
throw new MatrixArithmeticException();
double[][] result =
new double[values.length][m.values[0].length];
for (int i = 0; i < values.length; i++) {
for (int j = 0; j < m.values[0].length; j++) {
for (int k = 0; k < m.values.length; k++) {
result[i][j] += values[i][k] * m.values[k][j];
}
}
}
return new Matrix(result);
}
public Matrix multiply(double d) {
double[][] result =
new double[values.length][values[0].length];
for (int i = 0; i < values.length; i++) {
for (int j = 0; j < values[0].length; j++) {
result[i][j] = values[i][j] * d;
}
}
return new Matrix(result);
}
public String toString() {
StringBuffer rep = new StringBuffer("[");
for (int i = 0; i < values.length; i++) {
rep.append("[");
for (int j = 0; j < values[0].length; j++) {
rep.append(((j > 0) ? " " : "") + values[i][j]);
}
rep.append("]");
}
rep.append("]");
return new String(rep);
}
}
class MatrixArithmeticException extends Exception { }
public class Matrix {
private double[][] values;
public Matrix(double[][] initVal) {
values = new double[initVal.length][initVal[0].length];
// 配列 initVal の内容を配列 values にコピーする
for (int i = 0; i < initVal.length; i++) {
System.arraycopy(initVal[i], 0, values[i], 0,
initVal[i].length);
}
}
public Matrix add(Matrix m) throws MatrixArithmeticException {
if (values.length != m.values.length ||
values[0].length != m.values[0].length)
throw new MatrixArithmeticException();
double[][] result =
new double[values.length][values[0].length];
for (int i = 0; i < values.length; i++) {
for (int j = 0; j < values[0].length; j++) {
result[i][j] = values[i][j] + m.values[i][j];
}
}
return new Matrix(result);
}
public Matrix multiply(Matrix m)
throws MatrixArithmeticException {
if (values[0].length != m.values.length)
throw new MatrixArithmeticException();
double[][] result =
new double[values.length][m.values[0].length];
for (int i = 0; i < values.length; i++) {
for (int j = 0; j < m.values[0].length; j++) {
for (int k = 0; k < m.values.length; k++) {
result[i][j] += values[i][k] * m.values[k][j];
}
}
}
return new Matrix(result);
}
public Matrix multiply(double d) {
double[][] result =
new double[values.length][values[0].length];
for (int i = 0; i < values.length; i++) {
for (int j = 0; j < values[0].length; j++) {
result[i][j] = values[i][j] * d;
}
}
return new Matrix(result);
}
public String toString() {
StringBuffer rep = new StringBuffer("[");
for (int i = 0; i < values.length; i++) {
rep.append("[");
for (int j = 0; j < values[0].length; j++) {
rep.append(((j > 0) ? " " : "") + values[i][j]);
}
rep.append("]");
}
rep.append("]");
return new String(rep);
}
}