class Matrix {
private int a[][];
public Matrix(int n, int m) {
a = new int[n][m];
}
public Matrix(int a[][]) {
this.a = a;
}
public int getVerticalSize() {
return a.length;
}
public int getHorizontalSize() {
return a[0].length;
}
public int getElement(int i, int j) {
return a[i][j];
}
public void setElement(int i, int j, int value) {
a[i][j] = value;
}
@Override
for (int[] vector : a) {
for (int value : vector)
s += value + " ";
s += "\n";
}
return s;
}
public static Matrix add(Matrix a, Matrix b) {
int v = a.getVerticalSize();
int h = a.getHorizontalSize();
Matrix result = new Matrix(v, h);
for (int i = 0; i < h; i++) {
for (int j = 0; j < v; j++) {
int value = 0;
value = a.getElement(i, j) + b.getElement(i, j);
result.setElement(i, j, value);
}
}
return result;
}
public static Matrix subtract(Matrix a, Matrix b) {
int v = a.getVerticalSize();
int h = a.getHorizontalSize();
Matrix result = new Matrix(v, h);
for (int i = 0; i < h; i++) {
for (int j = 0; j < v; j++) {
int value = 0;
value = a.getElement(i, j) - b.getElement(i, j);
result.setElement(i, j, value);
}
}
return result;
}
public static Matrix multiply(Matrix a, Matrix b) {
int v = a.getVerticalSize();
int h = b.getHorizontalSize();
int temp = a.getHorizontalSize();
Matrix result = new Matrix(v, h);
for (int i = 0; i < v; i++)
for (int j = 0; j < h; j++) {
int value = 0;
for (int k = 0; k < temp; k++) {
value += a.getElement(i, k) * b.getElement(k, j);
}
result.setElement(i, j, value);
}
return result;
}
public void transMatrix() {
int x = a.length;
int retA[][] = new int[x][x];
for (int i = 0; i < x; i++) {
for (int j = 0; j < x; j++) {
retA[j][i] = a[i][j];
}
}
for (int i = 0; i < a.length; i++) {
System.
arraycopy(retA
[i
],
0, a
[i
],
0, a.
length); }
}
public static void main
(String[] args
) {
int[][] a = {{-9, 1, 0}, {4, 1, 1}, {-2, 2, -1}};
int[][] b = {{0, 0, 0}, {0, 2, 0}, {0, 0, 1}};
Matrix A = new Matrix(a);
System.
out.
println("Element [0,0] : " + A.
getElement(0,
0));
Matrix B = new Matrix(b);
B.setElement(0, 0, 1);
System.
out.
println("Size of matrix B : " + B.
getHorizontalSize() + " " + B.
getVerticalSize() + B
);
Matrix C;
C = add(A, B);
System.
out.
println("add " + C
);
C = subtract(A, B);
System.
out.
println("subtract " + C
);
C = multiply(A, B);
System.
out.
println("multiply " + C
);
A.transMatrix();
System.
out.
println("Transposed A : " + A
);
}
}
CmNsYXNzIE1hdHJpeCB7CgogICAgcHJpdmF0ZSBpbnQgYVtdW107CgogICAgcHVibGljIE1hdHJpeChpbnQgbiwgaW50IG0pIHsKICAgICAgICBhID0gbmV3IGludFtuXVttXTsKICAgIH0KCiAgICBwdWJsaWMgTWF0cml4KGludCBhW11bXSkgewogICAgICAgIHRoaXMuYSA9IGE7CiAgICB9CgogICAgcHVibGljIGludCBnZXRWZXJ0aWNhbFNpemUoKSB7CiAgICAgICAgcmV0dXJuIGEubGVuZ3RoOwogICAgfQoKICAgIHB1YmxpYyBpbnQgZ2V0SG9yaXpvbnRhbFNpemUoKSB7CiAgICAgICAgcmV0dXJuIGFbMF0ubGVuZ3RoOwogICAgfQoKICAgIHB1YmxpYyBpbnQgZ2V0RWxlbWVudChpbnQgaSwgaW50IGopIHsKICAgICAgICByZXR1cm4gYVtpXVtqXTsKICAgIH0KCiAgICBwdWJsaWMgdm9pZCBzZXRFbGVtZW50KGludCBpLCBpbnQgaiwgaW50IHZhbHVlKSB7CiAgICAgICAgYVtpXVtqXSA9IHZhbHVlOwogICAgfQoKICAgIEBPdmVycmlkZQogICAgcHVibGljIFN0cmluZyB0b1N0cmluZygpIHsKICAgICAgICBTdHJpbmcgcyA9ICJcbiI7CiAgICAgICAgZm9yIChpbnRbXSB2ZWN0b3IgOiBhKSB7CiAgICAgICAgICAgIGZvciAoaW50IHZhbHVlIDogdmVjdG9yKQogICAgICAgICAgICAgICAgcyArPSB2YWx1ZSArICIgIjsKICAgICAgICAgICAgcyArPSAiXG4iOwogICAgICAgIH0KICAgICAgICByZXR1cm4gczsKICAgIH0KCiAgICBwdWJsaWMgc3RhdGljIE1hdHJpeCBhZGQoTWF0cml4IGEsIE1hdHJpeCBiKSB7CiAgICAgICAgaW50IHYgPSBhLmdldFZlcnRpY2FsU2l6ZSgpOwogICAgICAgIGludCBoID0gYS5nZXRIb3Jpem9udGFsU2l6ZSgpOwogICAgICAgIE1hdHJpeCByZXN1bHQgPSBuZXcgTWF0cml4KHYsIGgpOwoKICAgICAgICBmb3IgKGludCBpID0gMDsgaSA8IGg7IGkrKykgewogICAgICAgICAgICBmb3IgKGludCBqID0gMDsgaiA8IHY7IGorKykgewogICAgICAgICAgICAgICAgaW50IHZhbHVlID0gMDsKICAgICAgICAgICAgICAgIHZhbHVlID0gYS5nZXRFbGVtZW50KGksIGopICsgYi5nZXRFbGVtZW50KGksIGopOwogICAgICAgICAgICAgICAgcmVzdWx0LnNldEVsZW1lbnQoaSwgaiwgdmFsdWUpOwogICAgICAgICAgICB9CiAgICAgICAgfQogICAgICAgIHJldHVybiByZXN1bHQ7CiAgICB9CgogICAgcHVibGljIHN0YXRpYyBNYXRyaXggc3VidHJhY3QoTWF0cml4IGEsIE1hdHJpeCBiKSB7CiAgICAgICAgaW50IHYgPSBhLmdldFZlcnRpY2FsU2l6ZSgpOwogICAgICAgIGludCBoID0gYS5nZXRIb3Jpem9udGFsU2l6ZSgpOwogICAgICAgIE1hdHJpeCByZXN1bHQgPSBuZXcgTWF0cml4KHYsIGgpOwoKICAgICAgICBmb3IgKGludCBpID0gMDsgaSA8IGg7IGkrKykgewogICAgICAgICAgICBmb3IgKGludCBqID0gMDsgaiA8IHY7IGorKykgewogICAgICAgICAgICAgICAgaW50IHZhbHVlID0gMDsKICAgICAgICAgICAgICAgIHZhbHVlID0gYS5nZXRFbGVtZW50KGksIGopIC0gYi5nZXRFbGVtZW50KGksIGopOwogICAgICAgICAgICAgICAgcmVzdWx0LnNldEVsZW1lbnQoaSwgaiwgdmFsdWUpOwogICAgICAgICAgICB9CiAgICAgICAgfQogICAgICAgIHJldHVybiByZXN1bHQ7CiAgICB9CgogICAgcHVibGljIHN0YXRpYyBNYXRyaXggbXVsdGlwbHkoTWF0cml4IGEsIE1hdHJpeCBiKSB7CiAgICAgICAgaW50IHYgPSBhLmdldFZlcnRpY2FsU2l6ZSgpOwogICAgICAgIGludCBoID0gYi5nZXRIb3Jpem9udGFsU2l6ZSgpOwogICAgICAgIGludCB0ZW1wID0gYS5nZXRIb3Jpem9udGFsU2l6ZSgpOwoKICAgICAgICBNYXRyaXggcmVzdWx0ID0gbmV3IE1hdHJpeCh2LCBoKTsKCiAgICAgICAgZm9yIChpbnQgaSA9IDA7IGkgPCB2OyBpKyspCiAgICAgICAgICAgIGZvciAoaW50IGogPSAwOyBqIDwgaDsgaisrKSB7CiAgICAgICAgICAgICAgICBpbnQgdmFsdWUgPSAwOwogICAgICAgICAgICAgICAgZm9yIChpbnQgayA9IDA7IGsgPCB0ZW1wOyBrKyspIHsKICAgICAgICAgICAgICAgICAgICB2YWx1ZSArPSBhLmdldEVsZW1lbnQoaSwgaykgKiBiLmdldEVsZW1lbnQoaywgaik7CiAgICAgICAgICAgICAgICB9CiAgICAgICAgICAgICAgICByZXN1bHQuc2V0RWxlbWVudChpLCBqLCB2YWx1ZSk7CiAgICAgICAgICAgIH0KICAgICAgICByZXR1cm4gcmVzdWx0OwogICAgfQoKICAgIHB1YmxpYyB2b2lkIHRyYW5zTWF0cml4KCkgewogICAgICAgIGludCB4ID0gYS5sZW5ndGg7CiAgICAgICAgaW50IHJldEFbXVtdID0gbmV3IGludFt4XVt4XTsKICAgICAgICBmb3IgKGludCBpID0gMDsgaSA8IHg7IGkrKykgewogICAgICAgICAgICBmb3IgKGludCBqID0gMDsgaiA8IHg7IGorKykgewogICAgICAgICAgICAgICAgcmV0QVtqXVtpXSA9IGFbaV1bal07CiAgICAgICAgICAgIH0KICAgICAgICB9CiAgICAgICAgZm9yIChpbnQgaSA9IDA7IGkgPCBhLmxlbmd0aDsgaSsrKSB7CiAgICAgICAgICAgIFN5c3RlbS5hcnJheWNvcHkocmV0QVtpXSwgMCwgYVtpXSwgMCwgYS5sZW5ndGgpOwogICAgICAgIH0KICAgIH0KCiAgICBwdWJsaWMgc3RhdGljIHZvaWQgbWFpbihTdHJpbmdbXSBhcmdzKSB7CgogICAgICAgIGludFtdW10gYSA9IHt7LTksIDEsIDB9LCB7NCwgMSwgMX0sIHstMiwgMiwgLTF9fTsKICAgICAgICBpbnRbXVtdIGIgPSB7ezAsIDAsIDB9LCB7MCwgMiwgMH0sIHswLCAwLCAxfX07CgogICAgICAgIE1hdHJpeCBBID0gbmV3IE1hdHJpeChhKTsKICAgICAgICBTeXN0ZW0ub3V0LnByaW50bG4oIkEiICsgQSk7CiAgICAgICAgU3lzdGVtLm91dC5wcmludGxuKCJFbGVtZW50IFswLDBdIDogIiArIEEuZ2V0RWxlbWVudCgwLCAwKSk7CgogICAgICAgIE1hdHJpeCBCID0gbmV3IE1hdHJpeChiKTsKICAgICAgICBCLnNldEVsZW1lbnQoMCwgMCwgMSk7CiAgICAgICAgU3lzdGVtLm91dC5wcmludGxuKCJTaXplIG9mIG1hdHJpeCBCIDogIiArIEIuZ2V0SG9yaXpvbnRhbFNpemUoKSArICIgIiArIEIuZ2V0VmVydGljYWxTaXplKCkgKyBCKTsKCiAgICAgICAgTWF0cml4IEM7CiAgICAgICAgQyA9IGFkZChBLCBCKTsKICAgICAgICBTeXN0ZW0ub3V0LnByaW50bG4oImFkZCAiICsgQyk7CgogICAgICAgIEMgPSBzdWJ0cmFjdChBLCBCKTsKICAgICAgICBTeXN0ZW0ub3V0LnByaW50bG4oInN1YnRyYWN0ICIgKyBDKTsKCiAgICAgICAgQyA9IG11bHRpcGx5KEEsIEIpOwogICAgICAgIFN5c3RlbS5vdXQucHJpbnRsbigibXVsdGlwbHkgIiArIEMpOwoKICAgICAgICBBLnRyYW5zTWF0cml4KCk7CiAgICAgICAgU3lzdGVtLm91dC5wcmludGxuKCJUcmFuc3Bvc2VkIEEgOiAiICsgQSk7CgogICAgfQp9Cg==