It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 10 years ago.
Hi I have the Jama librarry, but this libraray work only with Double numbers..and its very slow. for Android app ..and finally i dont need so high precision of eig decomp..so is there some JAva libaray with float num.....similar in syntax with jama? becouse...I dont want to re-- write again my 440 rows code thanks. eig. Transpose, inverse and so basic Linear algebra operations..
or exist same java library eigenvalues. with threads?
I know of one library called la4j, you might be interested in looking into that. I should mention that, generally, I don't think Java is a good choice if you're planning on doing many matrix manipulations/calculations (I myself have tried and hit a dead-end), you might be better off looking into Python (NumPy) or C++ (Armadillo) for such projects.
Or are you looking for something like this?
import java.util.Arrays;
public class Matrix {
protected int rows;
protected int cols;
double[][] values;
public Matrix(int rows, int cols) {
this.rows = rows;
this.cols = cols;
values = new double[rows][cols];
for (int i = 0; i < rows; i++)
for (int j = 0; j < cols; j++)
values[i][j] = 0;
}
public Matrix(int[][] M) {
this.rows = M.length;
this.cols = M[0].length;
values = new double[rows][cols];
for (int i = 0; i < rows; i++)
for (int j = 0; j < cols; j++)
values[i][j] = M[i][j];
}
public Matrix(double[][] M) {
this.rows = M.length;
this.cols = M[0].length;
values = new double[rows][cols];
for (int i = 0; i < rows; i++)
for (int j = 0; j < cols; j++)
values[i][j] = M[i][j];
}
public void setToEye() {
for (int i = 0; i < rows; i++)
for (int j = 0; j < cols; j++)
values[i][j] = (i == j) ? 1 : 0;
}
public static int[] matrixSize(Matrix M) {
int[] size = new int[2];
size[0] = M.rows;
size[1] = M.cols;
return size;
}
public static double vectMul(double[] A, double[] B) {
double suma = 0;
for (int i = 0; i < A.length; i++)
suma += A[i] * B[i];
return suma;
}
public static Matrix matrixTranspose(Matrix M) {
int[] size = matrixSize(M);
double[][] Mt = new double[size[0]][size[1]];
for (int i = 0; i < size[0]; i++)
for (int j = 0; j < size[1]; j++)
Mt[i][j] = M.getValue(j, i);
return new Matrix(Mt);
}
public static Matrix matrixMul(Matrix A, Matrix B) {
int m1 = matrixSize(A)[0];
int n1 = matrixSize(A)[1];
int m2 = matrixSize(B)[0];
int n2 = matrixSize(B)[1];
double[][] rez;
if (n1 != m2) {
System.err.println("Inner matrix dimensions must agree!");
return null;
}
rez = new double[m1][n2];
for (int i = 0; i < m1; i++)
for (int j = 0; j < n2; j++)
rez[i][j] = vectMul(A.getRow(i), B.getColumn(j));
Matrix r = new Matrix(rez);
return r;
}
public static Matrix matrixMulWithMod(Matrix A, Matrix B, double mod) {
int m1 = matrixSize(A)[0];
int n1 = matrixSize(A)[1];
int m2 = matrixSize(B)[0];
int n2 = matrixSize(B)[1];
double[][] rez;
if (n1 != m2) {
System.err.println("Inner matrix dimensions must agree!");
return null;
}
rez = new double[m1][n2];
for (int i = 0; i < m1; i++)
for (int j = 0; j < n2; j++)
rez[i][j] = vectMul(A.getRow(i), B.getColumn(j)) % mod;
Matrix r = new Matrix(rez);
return r;
}
public String toString() {
StringBuilder sb = new StringBuilder();
for (int i = 0; i < this.rows; i++) {
sb.append(Arrays.toString(values[i]));
sb.append('\n');
}
String str = sb.toString();
return str;
}
public double[][] getValues() {
return values;
}
public void setValues(double[][] values) {
this.values = values;
}
public void setValues(int[][] values) {
for (int i = 0; i < rows; i++)
for (int j = 0; j < cols; j++)
this.values[i][j] = (double) values[i][j];
}
public double getValue(int row, int col) {
return values[row][col];
}
public void setValue(int row, int col, double value) {
values[row][col] = value;
}
public double[] getRow(int row) {
double[] temp = new double[cols];
for (int i = 0; i < temp.length; i++)
temp[i] = values[row][i];
return temp;
}
public double[] getColumn(int col) {
double[] temp = new double[rows];
for (int i = 0; i < temp.length; i++)
temp[i] = values[i][col];
return temp;
}
public double[] toDoubleArray() {
double[] temp = new double[rows * cols];
for (int i = 0; i < rows; i++)
for (int j = 0; j < cols; j++)
temp[i * (rows + 1) + j] = values[i][j];
return temp;
}
public int[] toIntArray() {
int[] temp = new int[rows * cols];
for (int i = 0; i < rows; i++)
for (int j = 0; j < cols; j++)
temp[i * (rows + 1) + j] = (int) values[i][j];
return temp;
}
public int getRowCount() {
return this.rows;
}
public int getColumnsCount() {
return this.cols;
}
public static double getMatrixDet(Matrix M) {
int m = M.getRowCount();
int n = M.getColumnsCount();
double D = 0;
if (m != n) {
System.err.println("Matrix must be square!");
System.exit(0);
}
if (n > 1) {
Matrix I = new Matrix(m - 1, n - 1);
for (int i = 1; i < m; i++)
for (int j = 1; j < n; j++)
I.setValue(i - 1, j - 1, M.getValue(i, j));
D = M.getValue(0, 0) * getMatrixDet(I);
} else
D = M.getValue(0, 0);
// za niz , kopira iz niza a elemente 0:i-1 i+1:n sredi za matrcu
Matrix I = new Matrix(m - 1, n - 1);
for (int i = 1; i < n; i++) {
I = M.withoutIthRowAndJthCol(i, 0);
D = D + Math.pow((-1), i) * M.getValue(i, 0) * getMatrixDet(I);
}
return D;
}
public Matrix transpose() {
Matrix temp = new Matrix(this.values);
for (int i = 0; i < this.rows; i++)
for (int j = 0; j < this.cols; j++)
this.values[i][j] = temp.getValue(j, i);
return this;
}
private Matrix withoutIthRowAndJthCol(int row, int col) {
Matrix temp = new Matrix(this.rows - 1, this.cols - 1);
int k = 0, l = 0;
for (int i = 0; i < this.getRowCount(); i++) {
if (i == row)
continue;
for (int j = 0; j < this.getColumnsCount(); j++) {
if (j == col)
continue;
temp.setValue(k, l, this.values[i][j]);
l++;
}
l %= 2;
k++;
}
return temp;
}
public static Matrix getMatrixAdj(Matrix M) {
int m = M.getRowCount();
int n = M.getColumnsCount();
Matrix A = new Matrix(m, n);
if (m != n) {
System.err.println("Matrix must be square!");
System.exit(0);
}
for (int i = 0; i < m; i++)
for (int j = 0; j < n; j++) {
A.setValue(i, j, Math.pow((-1), i + j)
* getMatrixDet(M.withoutIthRowAndJthCol(i, j)));
}
A.transpose();
return A;
}
public static Matrix matrixDiv(Matrix M, double n) {
Matrix temp = M;
for (int i = 0; i < M.getRowCount(); i++)
for (int j = 0; j < M.getColumnsCount(); j++)
temp.setValue(i, j, (M.getValue(i, j) / n));
return temp;
}
public static Matrix getMatrixInv(Matrix M) {
Matrix I = new Matrix(M.getRowCount(), M.getColumnsCount());
if (M.getRowCount() != M.getColumnsCount()) {
System.err.println("Matrix must be square!");
System.exit(0);
}
if (getMatrixDet(M) == 0) {
System.err.println("Matrix is singular!");
System.exit(0);
}
I = matrixDiv(getMatrixAdj(M), getMatrixDet(M));
return I;
}
}
if this is what you wanted, you're welcome.
Related
I'm currently working on implementing the Floyd Warshall algorithm from pseudocode to java. I felt like I had it correct, but anytime I ran it on a graph, I was getting an output of large negative number and I feel that it was probably the way I implemented my algorithm. After looking I realized I may have missed something. I haven't implemented the k-1 part of the pseudocode and I'm not sure how to implement this. Im hoping this will correct the issue.
Here it the code i have written.
public static int[][] floyd(int n, int[][] W, int[][] P, int[][] D) {
D = W;
for (int i = 0; i < n; i++)
for (int j = 0; j < n; j++)
P[i][j] = 0;
for (int k = 0; k < n; k++)
for (int i = 0; i < n; i++)
for (int j = 0; j < n; j++) {
if ((D[i][j] == Integer.MAX_VALUE) && (D[i][k] + D[k][j] == Integer.MAX_VALUE)) { // add
continue;
} else if (D[i][j] <= D[i][k] + D[k][j]) {
D[i][j] = D[i][j];
} else {
D[i][j] = D[i][k] + D[k][j];
}
}
return D;
}
And here is the pseudocode im writing based off of.
Floyd-Warshall(W)
n = W.rows
D(0) = W
for (k = 1 to n)
for ( i = 1 to n )
for ( j = 1 to n )
if (dij(k-1) == INF) && (dik(k-1) + dkj(k-1)) == INF)
continue
else if (dij(k-1) ≤ dik(k-1) + dkj(k-1))
dij(k) = dij(k-1) // add
else
dij(k) = dik(k-1) + dkj(k-1) // add
return (D(n))
EDIT
Heres all of my code thats relevant with some corrections
int V = g.nodeList.size();
int W[][] = new int[V][V];
int P[][] = new int[V][V];
// Adding wights to graph
for (int i = 0; i < V; i++) {
for (int j = 0; j < V; j++) {
W[j][i] = Integer.parseInt(g.edgeList.get((j * V) + i).label);
if (W[j][i] == 0) {
W[i][j] = Integer.MAX_VALUE;
}
}
}
System.out.println("Matrix to find the shortest path of.");
printMatrix(W, V, g);
System.out.println("Shortest Path Matrix.");
printMatrix(floyd(V, W, P), V, g);
System.out.println("Path Matrix");
printPredMatrix(P, V, g);
}
public static int[][] floyd(int n, int[][] W, int[][] P) {
for (int i = 0; i < n; i++)
for (int j = 0; j < n; j++)
P[i][j] = 0;
for (int k = 0; k < n; k++)
for (int i = 0; i < n; i++)
for (int j = 0; j < n; j++) {
if ((W[i][j] == Integer.MAX_VALUE) && (W[i][k] + W[k][j] == Integer.MAX_VALUE)) { // add
continue;
} else if (W[i][j] <= W[i][k] + W[k][j]) {
W[i][j] = W[i][j];
} else {
W[i][j] = W[i][k] + W[k][j];
}
}
return W;
}
public static int min(int i, int j) {
if (i > j) {
return j;
}
return i;
}
public static void printMatrix(int[][] Matrix, int V, Graph g) {
System.out.print("\n\t");
for (int j = 0; j < V; j++) {
System.out.print(g.nodeList.get(j).name + "\t");
}
System.out.println();
for (int j = 0; j < 35; j++) {
System.out.print("-");
}
System.out.println();
for (int i = 0; i < V; i++) {
System.out.print(g.nodeList.get(i).name + " |\t");
for (int j = 0; j < V; j++) {
if ((Matrix[i][j] == Integer.MAX_VALUE)||(Matrix[i][j] == Integer.MIN_VALUE)) {
System.out.print("~");
} else {
System.out.print(Matrix[i][j]);
}
System.out.print("\t");
}
System.out.println("\n");
}
System.out.println("\n");
}
public static void printPredMatrix(int[][] Matrix, int V, Graph g) {
System.out.print("\n\t");
for (int j = 0; j < V; j++) {
System.out.print(g.nodeList.get(j).name + "\t");
}
System.out.println();
for (int j = 0; j < V * 3; j++) {
System.out.print("-");
}
System.out.println();
for (int i = 0; i < V; i++) {
System.out.print(g.nodeList.get(i).name + " |\t");
for (int j = 0; j < V; j++) {
System.out.print(Matrix[i][j]);
System.out.print("\t");
}
System.out.println("\n");
}
System.out.println("\n");
}
}```
When I'm implementing the SOR approach with java, I find this problem:
when I do this:
Matrix mm = ((D.sub(L)).inv());
B = mm.dot(b);
The value of B is right. However, when I use:
B = ((D.sub(L)).inv()).dot(b);
I get a quite different value of B which is wrong.
I fell this may be a reference problem, but I can't get the exact answer.
This is the SOR method:
public static Matrix[] SOR(Matrix m, Matrix b, double omiga) {
Matrix D = getD(m);
Matrix L = getL(m);
Matrix U = getU(m);
Matrix M = null;
Matrix B = null;
try {
Matrix mm = ((D.sub(L.pro(omiga))).inv());
M = mm.dot((D.pro(1 - omiga)).add(U.pro(omiga)));
// This is right:
B = (mm.dot(b)).pro(omiga);
// This is wrong:
// B = (((D.sub(L.pro(omiga))).inv()).dot(b)).pro(omiga);
} catch (MatrixProcessException e) {
System.out.println("3 Matrix process problem!");
}
Matrix[] matrices = {M, B};
return matrices;
}
And this is the Matrix class created by me (methods in it have all been checked and have no problem):
public class Matrix {
private int row;
private int col;
private int rowNum;
private int colNum;
private double[][] m;
public Matrix(int rowNum, int colNum) {
this.rowNum = rowNum;
this.colNum = colNum;
m = new double[rowNum][colNum];
for (int i = 0; i < rowNum; i++) {
for (int j = 0; j < colNum; j++) {
m[i][j] = 0;
}
}
}
public Matrix(double[][] m) {
rowNum = m.length;
colNum = m[0].length;
this.m = m;
}
public Matrix(double[] v) {
rowNum = v.length;
colNum = 1;
double[][] m = new double[rowNum][colNum];
for (int i = 0; i < rowNum; i++) {
m[i][0] = v[i];
}
this.m = m;
}
public Matrix pro(double lambda) {
for (int i = 0; i < rowNum; i++) {
for (int j = 0; j < colNum; j++) {
m[i][j] *= lambda;
}
}
return this;
}
public Matrix add(Matrix mm) throws MatrixProcessException {
if (mm.getRowNum() != rowNum || mm.getColNum() != colNum) {
throw new MatrixProcessException();
}
double[][] m1 = m;
double[][] m2 = mm.getM();
double[][] addM = new double[m.length][m[0].length];
for (int i = 0; i < m.length; i++) {
for (int j = 0; j < m[0].length; j++) {
addM[i][j] = m1[i][j] + m2[i][j];
}
}
return new Matrix(addM);
}
public Matrix sub(Matrix mm) throws MatrixProcessException {
if (mm.getRowNum() != rowNum || mm.getColNum() != colNum) {
throw new MatrixProcessException();
}
return this.add(mm.pro(-1));
}
}
public double[][] getM() {
return m;
}
public void setM(double[][] m) {
this.m = m;
}
public int getRowNum() {
return rowNum;
}
public void setRowNum(int rowNum) {
this.rowNum = rowNum;
}
public int getColNum() {
return colNum;
}
public void setColNum(int colNum) {
this.colNum = colNum;
}
public Matrix dot(Matrix M) throws MatrixProcessException {
double[][] m1 = m;
double[][] m2 = M.getM();
if (this.colNum != M.rowNum) {
System.out.println("sss");
throw new MatrixProcessException();
}
double[][] ansMatrix = new double[this.rowNum][M.colNum];
for (int i = 0; i < this.rowNum; i++) {
for (int j = 0; j < M.colNum; j++) {
double num = 0;
for (int k = 0; k < this.colNum; k++) {
num += m1[i][k] * m2[k][j];
}
ansMatrix[i][j] = num;
}
}
return new Matrix(ansMatrix);
}
public Matrix inv() throws MatrixProcessException {
if (rowNum != colNum) {
throw new MatrixProcessException();
}
Matrix mm = new Matrix(rowNum, 2 * colNum);
for (int i = 0; i < rowNum; i++) {
for (int j = 0; j < 2 * colNum; j++) {
if (j < rowNum) {
mm.set(i, j, m[i][j]);
}
else {
if (j - rowNum == i) {
mm.set(i, j, 1);
}
}
}
}
Collections.calculateUpperTriangle(mm);
Collections.calculateSimplest(mm);
for (int i = 0; i < rowNum; i++) {
mm.proR(i, 1.0 / mm.get(i, i));
}
Matrix M = new Matrix(rowNum, colNum);
for (int i = 0; i < rowNum; i++) {
for (int j = 0; j < rowNum; j++) {
M.set(i, j, mm.get(i, j + rowNum));
}
}
return M;
}
public void showMatrix() {
System.out.println("\nMatrix:");
for (int i = 0; i < rowNum; i++) {
for (int j = 0; j < colNum; j++) {
System.out.print(m[i][j] + " ");
}
System.out.println();
}
}
public double[] toVector() {
double[] v = new double[rowNum];
if (colNum == 1) {
for (int i = 0; i < rowNum; i++) {
v[i] = m[i][0];
}
}
else {
System.out.println("This matrix is not a vector");
}
return v;
}
}
May anyone help me to get the reason of the problem?
Thx!
I feel like I'm really close with this implementation of a memoized matrix chain algorithm in Java, but I'm getting an array out of bounds error on line 45 and 53. These, for some reason, really seem to mess me up. Maybe there's something I'm continually messing up with, but I dunno, obviously. Can anyone help me out?
public class Lab2 {
//fields
static int p[];
static int m[][];
final static int INFINITY = 999999999;
public Lab2() {
//
}
public static void main(String[] args) {
Lab2 lab2 = new Lab2();
Lab2.m = new int[7][7];
Lab2.p = new int[7];
Lab2.p[0] = 20;
Lab2.p[1] = 8;
Lab2.p[2] = 4;
Lab2.p[3] = 25;
Lab2.p[4] = 30;
Lab2.p[5] = 5;
Lab2.p[6] = 10;
int n = Lab2.p.length-1;
//initialize m array to infinity
for (int i = 1; i <= n; i++){
for (int j = i; j <= n; j++){
Lab2.m[i][j]= INFINITY;
}
}
lab2.lookUpChain(m, p, 1, n);
for (int i = 0; i < 8; i++){
for (int j = 0; j < 8; j++){
System.out.println(m[i][j]);
}
}
}
//
public int lookUpChain(int m[][], int p[], int i, int j ){
if (m[i][j]<INFINITY){
return m[i][j];
}
if (i == j){
m[i][j] = 0;
}
else{
for (int k = i; k <= j; i++){
int q = (lookUpChain(m,p,i,k)) + (lookUpChain(m,p,k+1,j)) + (p[i]*p[k]*p[j]);
if (q < m[i][j]){
m[i][j] = q;
}
}
}
return m[i][j];
}
}
else{
for (int k = i; k <= j; i++)
Change to:
else{
for (int k = i; k <= j; k++) // change i to k
My 2D FFT algorithm is outputting the correct values, but they are in the wrong order. For example, for input:
1050.0 1147.0 1061.0 1143.0
1046.0 1148.0 1118.0 1073.0
1072.0 1111.0 1154.0 1101.0
1078.0 1101.0 1106.0 1062.0
Taking the FFT, and then inverse FFT results in:
1050.0 1143.0 1061.0 1147.0
1078.0 1062.0 1106.0 1101.0
1072.0 1101.0 1154.0 1111.0
1046.0 1073.0 1118.0 1148.0
You can see that if you flip the last 3 columns horizontally, then the last 3 rows vertically, the data will be correct. As far as I can tell this is true for all input sizes so it's an easy (albeit hacky) fix. I am however worried about about computational time of the fix because I may have to perform this on 1024x1024 or even 2048x2048 images in the future.
I am fairly confident that my 1D FFT algorithm doFFT() is correct, and I am getting the expected values for the forward 2D FFT. It is just the inverse 2D FFT that is causing me trouble.
Does anyone see where my error is?
Code
private static double[] cose;
private static double[] sin;
public static void main(String[] args) {
float[][] img = new float[][]{
{ 1050.0f, 1147.0f, 1061.0f, 1143.0f},
{ 1046.0f, 1148.0f, 1118.0f, 1073.0f},
{ 1072.0f, 1111.0f, 1154.0f, 1101.0f},
{ 1078.0f, 1101.0f, 1106.0f, 1062.0f}
};
int size = img.length;
System.out.println("Image");
for (int i = 0; i < size; i++)
{
for (int j = 0; j < size; j++)
{
System.out.print(img[i][j] + "\t");
}
System.out.println();
}
Complex[][] fft = fft2D(toComplex(img), false);
Complex[][] inverse = fft2D(fft, true);
System.out.println("\nInverse");
for (int i = 0; i < size; i++)
{
for (int j = 0; j < size; j++)
{
System.out.print(inverse[i][j].getReal() + "\t");
}
System.out.println();
}
}
public static Complex[][] fft2D(Complex[][] pixels, boolean inverse){
int size = pixels.length;
computeCosSin(size);
Complex[][] data = transpose(pixels.clone());
Complex[] temp;
// FFT of rows
for (int i = 0; i < size; i++)
{
temp = doFFT(data[i], size);
data[i] = temp;
}
// FFT of columns
for (int i = 0; i < size; i++)
{
temp = new Complex[size];
for (int j = 0; j < size; j++)
{
temp[j] = data[j][i];
}
Complex[] temp2 = doFFT(temp, size);
for (int j = 0; j < size; j++)
{
data[j][i] = temp2[j];
}
}
if (!inverse)
{
for (int i = 0; i < size; i++)
{
for (int j = 0; j < size; j++)
{
data[i][j] = data[i][j].divide(size*size);
}
}
}
return data;
}
public static Complex[] doFFT(Complex[] data, int size){
Complex[] temp = new Complex[size];
int j = 0;
for (int i = 0; i < size; i++) {
temp[i] = data[j];
int k = size / 2;
while ((j >= k) && (k > 0)) {
j -= k;
k /= 2;
}
j += k;
}
Complex n,m,h,f;
for(int i=0; i<size;i+=4){
n = temp[i].add(temp[i+1]);
m = temp[i+2].add(temp[i+3]);
h = temp[i].subtract(temp[i+1]);
f = temp[i+2].subtract(temp[i+3]);
Complex mult = h.add(f.multiply(Complex.I));
Complex sub = h.subtract(f.multiply(Complex.I));
temp[i] = n.add(m);
temp[i+2] = n.subtract(m);
temp[i+1] = sub;
temp[i+3] = mult;
}
int u;
for(int i=4; i< size;i<<=1){
int v = size/(i <<1);
for(int c=0; c< size;c +=i<<1){
for(int x=0; x < i; x++){
u = v*x;
double calc = temp[i+c+x].getReal()*cose[u] - temp[i+c+x].getImaginary()*sin[u];
double calc2 = temp[i+c+x].getReal()*sin[u] + temp[i+c+x].getImaginary()*cose[u];
Complex fftArray = new Complex(calc,calc2);
temp[(i+c+x)] =temp[(c+x)].subtract(fftArray);
temp[(c+x)] = temp[(c+x)].add(fftArray);
}
}
}
return temp;
}
public static Complex[][] toComplex(float[][] arr)
{
Complex[][] newArr = new Complex[arr.length][arr.length];
for (int i = 0; i < arr.length; i++)
{
for (int j = 0; j < arr.length; j++)
{
newArr[i][j] = new Complex(arr[i][j], 0.0);
}
}
return newArr;
}
public static Complex[][] transpose(Complex[][] array)
{
for (int i = 0; i < array.length; i++)
{
for (int j = i+1; j < array[i].length; j++)
{
Complex temp = array[i][j];
array[i][j] = array[j][i];
array[j][i] = temp;
}
}
return array;
}
public static void computeCosSin(int size){
double num = (2.0*Math.PI)/size;
double cos = Math.cos(num);
double sine = Math.sin(num);
cose = new double[size];
sin = new double[size];
cose[0] =1.0;
for(int i=1; i<size;i++){
cose[i] = cos*cose[i-1] + sine*sin[i-1];
sin[i] = cos*sin[i-1] - sine*cose[i-1];
}
}
}
This doesn't solve the root problem, but it does change the data I'm getting to the data I expect so it will serve my purpose for now. I do worry it will be incredibly slow on large arrays.
This function swaps row i with row N-i and then swaps every column i with column N-i, for 0 < i < N, (Assuming a square, power of 2 input array)
public Complex[][] inverseFix(Complex[][] array)
{
int size = array.length;
// Swap rows
Complex[] temp;
for (int i = 1; i < size/2; i++)
{
temp = array[i];
array[i] = array[size-i];
array[size-i] = temp;
}
// Swap columns
Complex temp2;
for (int i = 0; i < size; i++)
{
for (int j = 1; j < size/2; j++)
{
temp2 = array[i][j];
array[i][j] = array[i][size-j];
array[i][size-j] = temp2;
}
}
return array;
}
My goal is to print out the Matrix when the two arrays are multiplied together. What am I doing wrong with this code? How do I get it so that it prints out the matrix? (Sorry I do not know what other details i should provide and I cannot submit this post unless I add more detail).
public class Matrices {
static int mRows = 0;
static int mCol = 0;
static int nRows = 0;
static int nCol = 0;
public static int[][] multiplyMatrices(int[][] m, int[][] n){
mRows = m.length;
mCol = m[0].length;
nRows = n.length;
nCol = n[0].length;
if(canBeMultiplied(m,n) == false){
throw new IllegalArgumentException("Cannot multiply arrays");
}
int[][] answer = new int[mRows][nCol];
for(int i = 0; i < mRows; i++){
for(int j = 0; j < nCol; j++){
for(int k = 0; k < mCol; k++){
answer[i][j] += m[i][k] * n[k][j];
}
}
}
return answer;
}
public static boolean canBeMultiplied(int[][] m, int[][]n){
mRows = m.length;
mCol = m[0].length;
nRows = n.length;
nCol = n[0].length;
if(nRows == mCol){
return true;
}
return false;
}
public static void main(String[] args) {
int[][] temp1 = {{1,2,3},{4,5,6}};
int[][] temp2 ={{1},{2},{3}};
for(int i = 0; i < mRows; i++){
for(int j = 0; j < nCol; j++){
System.out.print(multiplyMatrices(temp1,temp2)[i][j]);
}
System.out.print("\n");
}
}
}
Thanks for your help.
This could will loop through the the 2D array and print each element.
static final int ROWS = 2;
static final int COLS = 4;
int[][] a2 = new int[ROWS][COLS];
//... Print array in rectangular form
for (int i = 0; i < ROWS; i++) {
for (int j = 0; j < COLS; j++) {
System.out.print(" " + a2[i][j]);
}
System.out.println("");
}