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("");
}
Related
In this exercise we have to draw a rectangle using two-dimensional arrays that goes from 10 to 15 rows and 20 to 30 column, putting in the border of the rectangle "#" while putting inside the rectangle "-". It has to look something like this: https://i.stack.imgur.com/IOqY6.png
The code I have so far is this, but I need some help fixing it since I'm a bit lost with the exercise:
public class Practica9{
public static void main(String[] args){
char [][] tablero = new char [10][20];
for (int i = 0; i < 10; i++){
for (int j = 0; j < 20; j++){
tablero [0][19] = #;
tablero [9][19] = #;
System.out.println (tablero[0][19]);
System.out.println (tablero[9][19]);
}
}
for (int i = 0; i < 10; i++){
for (int j = 0; j < 20; j++){
tablero [1][18] = -;
tablero [8][18] = -;
System.out.println (tablero [1][18]);
System.out.println (tablero [8][18]);
}
}
}
}
public static void main(String[] args){
int rows = 15;
int columns =30;
char [][] rectangle = new char[rows][columns];
// fill array
for(int i = 0; i < rows; i++){
for(int j = 0; j < columns; j++){
if(i==0 || j==0 || i==rows-1 || j==columns-1){
rectangle[i][j] = '#';
}
else{
rectangle[i][j] = '-';
}
}
}
// print array
for(int i = 0; i < rows; i++){
for(int j = 0; j < columns; j++){
System.out.print(rectangle[i][j]);
}
System.out.println();
}
}
It only prints out the average to the first column but doesn't do anything for the next.
it only prints out 717, for the first column average. It is a ragged array. Everything else compiles fine.
import java.util.Scanner;
import java.io.*;
public class Fitness
{
public static void main(String [] args)
{
int [][] week = {{800,1000,100},{450,100,845,20,1200,200},{1800,250,400},{0,1500,800,120},{600,500},{700,1400,1700,100},{675}};
System.out.println("Average over 7 days");
avgCalb(week);
static void avgCalb(int [][] x)
{
for(int j = 0; j < x[0].length; j++)
{
int colTotal = 0;
for(int i = 0; i < x.length; i++)
{
colTotal = colTotal + x[i][j];
}
System.out.println(colTotal/7);
break;
}
}
}
summing columns
static void avgCalb(int [][] x)
{
int[] colTotal = new int[x.length];
for(int j = 0; j < x.length; j++)
{
for(int i = 0; i < x[j].length; i++)
{
colTotal[i] += x[j][i]
}
}
for(int i = 0;i<colTotal.length;i++) {
System.out.println((double)colTotal[i]/colTotal.length);
}
}
summing rows
static void avgCalb(int [][] x)
{
for(int j = 0; j < x.length; j++)
{
int colTotal = 0;
for(int i = 0; i < x[j].length; i++)
{
colTotal += x[j][i];
}
System.out.println(colTotal/x[j].length);
}
}
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;
}
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.