I have a problem with matrix substraction in Python and Java. I have followed the same steps in both programming languages but outputs are different.
import numpy as np
array1 = [[1,3], [5,6],[7,8]]
array1 = np.transpose(array1)
array2 = [[1,0,1]]
array3 = np.subtract(array2,array1)
print(array3)
which output is matrix like this:
[[ 0 -5 -6]
[-2 -6 -7]]
This works fine and in the way i need. But i need this output in Java. So i have tried following snippet of code:
double [][] array1 = new double[][]{
{1,2},
{3,4},
{5,6}
};
double [][] array2 = new double[][]{
{1,0,1}
};
array1 = np.T(array1);
double [][] vysl = np.subtract(array2, array1);
where
public static double[][] subtract(double[][] a, double[][] b) {
int m = a.length;
int n = a[0].length;
double[][] c = new double[m][n];
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
c[i][j] = a[i][j] - b[i][j];
}
}
return c;
}
public static double[][] T(double[][] a) {
int m = a.length;
int n = a[0].length;
double[][] b = new double[n][m];
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
b[j][i] = a[i][j];
}
}
return b;
}
But the result is different matrix :
for (int i = 0; i < vysl.length; i++)
{
for (int y = 0; y < vysl[0].length; y++)
System.out.print(vysl[i][y] + " ");
System.out.println("");
}
0.0 -3.0 -4.0
I have displayed the matrix with this 2D cycle. This matrix has only 1 row with 3 columns, but the preceding matrix from pythom had 2 rows a 3 columns. Can you please tell me what I am doing wrong a the way I can get the matrix with 2 rows and 3 columns in Java? How can I implement broadcasting rule in Java?
I could see issue in your code...for your use case following code gives expected output
public static void main(String[] args) {
double[][] array1 = new double[][] { { 1, 3 }, { 5, 6 }, { 7, 8 } };
double[][] array2 = new double[][] { { 1, 0, 1 } };
array1 = np(array1);
double[][] vysl = subtract(array2, array1);
System.out.println("complete");
}
public static double[][] np(double[][] a) {
int x = a.length;
int y = a[0].length;
double[][] c = new double[y][x];
for (int i = 0; i < y; i++) {
for (int j = 0; j < x; j++) {
c[i][j] = a[j][i];
}
}
return c;
}
public static double[][] subtract(double[][] a, double[][] b) {
int m = b.length;
int n = b[0].length;
double[][] c = new double[m][n];
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
c[i][j] = a[0][j] - b[i][j];
}
}
return c;
}
I have 2 1d arrays and i am trying to populate them into a single 2d array in JAVA.
For instance:
a[] = {2,7}
b[] = {9,1}
The results should then be:
result[][] = {{2,9}, {7,1}}
This is my code
import java.util.Arrays;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter Test Cases:\t");
int t = sc.nextInt();
int[] a;
int[] b;
int i, j, x;
for (int k = 0; k < t; k++) {
System.out.println("Enter 1st Array Limit:\t");
int len = sc.nextInt();
System.out.println("Enter 2nd Array Limit:\t");
int len1 = sc.nextInt();
a = new int[len];
b = new int[len1];
System.out.println("Enter Sum Value");
x = sc.nextInt();
System.out.println("Enter " + len + " elements:\t");
for (i = 0; i < len; i++) {
a[i] = sc.nextInt();
}
System.out.println("Enter " + len1 + " elements:\t");
for (j = 0; j < len1; j++) {
b[j] = sc.nextInt();
}
int [][] c = new int[len][2];
for (i = 0; i < len; i++) {
for (j = 0; j < len1; j++) {
if (a[i] + b[j] == x) {
for(int l = 0; i < a.length; i++){
c[l][0] = a[i];
c[l][1] = b[j];
}
}
}
}
System.out.println(Arrays.deepToString(c));
}
}
}
This still produces wrong output
i want to find Find all pairs with a given sum
int[] a = {2,7};
int[] b = {9,1};
int[][] c = new int[a.length][2];
for(int i = 0; i < a.length; i++){
c[i][0] = a[i];
c[i][1] = b[i];
}
should do the trick
I'm trying to make a simple matrix multiplication method using multidimensional arrays ([2][2]). I'm kinda new at this, and I just can't find what it is I'm doing wrong. I'd really appreciate any help in telling me what it is. I'd rather not use libraries or anything like that, I'm mostly doing this to learn how it works. Thank you so much in advance.
I'm declaring my arays in the main method as follows:
Double[][] A={{4.00,3.00},{2.00,1.00}};
Double[][] B={{-0.500,1.500},{1.000,-2.0000}};
A*B should return the identity matrix. It doesn't.
public static Double[][] multiplicar(Double[][] A, Double[][] B){
//the method runs and returns a matrix of the correct dimensions
//(I actually changed the .length function to a specific value to eliminate
//it as a possible issue), but not the correct values
Double[][] C= new Double[2][2];
int i,j;
////I fill the matrix with zeroes, if I don't do this it gives me an error
for(i=0;i<2;i++) {
for(j=0;j<2;j++){
C[i][j]=0.00000;
}
}
///this is where I'm supposed to perform the adding of every element in
//a row of A multiplied by the corresponding element in the
//corresponding column of B, for all columns in B and all rows in A
for(i=0;i<2;i++){
for(j=0;j<2;j++)
C[i][j]+=(A[i][j]*B[j][i]);
}
return C;
}
You can try this code:
public class MyMatrix {
Double[][] A = { { 4.00, 3.00 }, { 2.00, 1.00 } };
Double[][] B = { { -0.500, 1.500 }, { 1.000, -2.0000 } };
public static Double[][] multiplicar(Double[][] A, Double[][] B) {
int aRows = A.length;
int aColumns = A[0].length;
int bRows = B.length;
int bColumns = B[0].length;
if (aColumns != bRows) {
throw new IllegalArgumentException("A:Rows: " + aColumns + " did not match B:Columns " + bRows + ".");
}
Double[][] C = new Double[aRows][bColumns];
for (int i = 0; i < aRows; i++) {
for (int j = 0; j < bColumns; j++) {
C[i][j] = 0.00000;
}
}
for (int i = 0; i < aRows; i++) { // aRow
for (int j = 0; j < bColumns; j++) { // bColumn
for (int k = 0; k < aColumns; k++) { // aColumn
C[i][j] += A[i][k] * B[k][j];
}
}
}
return C;
}
public static void main(String[] args) {
MyMatrix matrix = new MyMatrix();
Double[][] result = multiplicar(matrix.A, matrix.B);
for (int i = 0; i < 2; i++) {
for (int j = 0; j < 2; j++)
System.out.print(result[i][j] + " ");
System.out.println();
}
}
}
Java. Matrix multiplication.
Tested with matrices of different size.
public class Matrix {
/**
* Matrix multiplication method.
* #param m1 Multiplicand
* #param m2 Multiplier
* #return Product
*/
public static double[][] multiplyByMatrix(double[][] m1, double[][] m2) {
int m1ColLength = m1[0].length; // m1 columns length
int m2RowLength = m2.length; // m2 rows length
if(m1ColLength != m2RowLength) return null; // matrix multiplication is not possible
int mRRowLength = m1.length; // m result rows length
int mRColLength = m2[0].length; // m result columns length
double[][] mResult = new double[mRRowLength][mRColLength];
for(int i = 0; i < mRRowLength; i++) { // rows from m1
for(int j = 0; j < mRColLength; j++) { // columns from m2
for(int k = 0; k < m1ColLength; k++) { // columns from m1
mResult[i][j] += m1[i][k] * m2[k][j];
}
}
}
return mResult;
}
public static String toString(double[][] m) {
String result = "";
for(int i = 0; i < m.length; i++) {
for(int j = 0; j < m[i].length; j++) {
result += String.format("%11.2f", m[i][j]);
}
result += "\n";
}
return result;
}
public static void main(String[] args) {
// #1
double[][] multiplicand = new double[][] {
{3, -1, 2},
{2, 0, 1},
{1, 2, 1}
};
double[][] multiplier = new double[][] {
{2, -1, 1},
{0, -2, 3},
{3, 0, 1}
};
System.out.println("#1\n" + toString(multiplyByMatrix(multiplicand, multiplier)));
// #2
multiplicand = new double[][] {
{1, 2, 0},
{-1, 3, 1},
{2, -2, 1}
};
multiplier = new double[][] {
{2},
{-1},
{1}
};
System.out.println("#2\n" + toString(multiplyByMatrix(multiplicand, multiplier)));
// #3
multiplicand = new double[][] {
{1, 2, -1},
{0, 1, 0}
};
multiplier = new double[][] {
{1, 1, 0, 0},
{0, 2, 1, 1},
{1, 1, 2, 2}
};
System.out.println("#3\n" + toString(multiplyByMatrix(multiplicand, multiplier)));
}
}
Output:
#1
12.00 -1.00 2.00
7.00 -2.00 3.00
5.00 -5.00 8.00
#2
0.00
-4.00
7.00
#3
0.00 4.00 0.00 0.00
0.00 2.00 1.00 1.00
static int b[][]={{21,21},{22,22}};
static int a[][] ={{1,1},{2,2}};
public static void mul(){
int c[][] = new int[2][2];
for(int i=0;i<b.length;i++){
for(int j=0;j<b.length;j++){
c[i][j] =0;
}
}
for(int i=0;i<a.length;i++){
for(int j=0;j<b.length;j++){
for(int k=0;k<b.length;k++){
c[i][j]= c[i][j] +(a[i][k] * b[k][j]);
}
}
}
for(int i=0;i<c.length;i++){
for(int j=0;j<c.length;j++){
System.out.print(c[i][j]);
}
System.out.println("\n");
}
}
Try this,
public static Double[][] multiplicar(Double A[][],Double B[][]){
Double[][] C= new Double[2][2];
int i,j,k;
for (i = 0; i < 2; i++) {
for (j = 0; j < 2; j++) {
C[i][j] = 0.00000;
}
}
for(i=0;i<2;i++){
for(j=0;j<2;j++){
for (k=0;k<2;k++){
C[i][j]+=(A[i][k]*B[k][j]);
}
}
}
return C;
}
try this,it may help you
import java.util.Scanner;
public class MulTwoArray {
public static void main(String[] args) {
int i, j, k;
int[][] a = new int[3][3];
int[][] b = new int[3][3];
int[][] c = new int[3][3];
Scanner sc = new Scanner(System.in);
System.out.println("Enter size of array a");
int rowa = sc.nextInt();
int cola = sc.nextInt();
System.out.println("Enter size of array b");
int rowb = sc.nextInt();
int colb = sc.nextInt();
//read and b
System.out.println("Enter elements of array a");
for (i = 0; i < rowa; ++i) {
for (j = 0; j < cola; ++j) {
a[i][j] = sc.nextInt();
}
System.out.println();
}
System.out.println("Enter elements of array b");
for (i = 0; i < rowb; ++i) {
for (j = 0; j < colb; ++j) {
b[i][j] = sc.nextInt();
}
System.out.println("\n");
}
//print a and b
System.out.println("the elements of array a");
for (i = 0; i < rowa; ++i) {
for (j = 0; j < cola; ++j) {
System.out.print(a[i][j]);
System.out.print("\t");
}
System.out.println("\n");
}
System.out.println("the elements of array b");
for (i = 0; i < rowb; ++i) {
for (j = 0; j < colb; ++j) {
System.out.print(b[i][j]);
System.out.print("\t");
}
System.out.println("\n");
}
//multiply a and b
for (i = 0; i < rowa; ++i) {
for (j = 0; j < colb; ++j) {
c[i][j] = 0;
for (k = 0; k < cola; ++k) {
c[i][j] += a[i][k] * b[k][j];
}
}
}
//print multi result
System.out.println("result of multiplication of array a and b is ");
for (i = 0; i < rowa; ++i) {
for (j = 0; j < colb; ++j) {
System.out.print(c[i][j]);
System.out.print("\t");
}
System.out.println("\n");
}
}
}
The method mults is a procedure(Pascal) or subroutine(Fortran)
The method multMatrix is a function(Pascal,Fortran)
import java.util.*;
public class MatmultE
{
private static Scanner sc = new Scanner(System.in);
public static void main(String [] args)
{
double[][] A={{4.00,3.00},{2.00,1.00}};
double[][] B={{-0.500,1.500},{1.000,-2.0000}};
double[][] C=multMatrix(A,B);
printMatrix(A);
printMatrix(B);
printMatrix(C);
double a[][] = {{1, 2, -2, 0}, {-3, 4, 7, 2}, {6, 0, 3, 1}};
double b[][] = {{-1, 3}, {0, 9}, {1, -11}, {4, -5}};
double[][] c=multMatrix(a,b);
printMatrix(a);
printMatrix(b);
printMatrix(c);
double[][] a1 = readMatrix();
double[][] b1 = readMatrix();
double[][] c1 = new double[a1.length][b1[0].length];
mults(a1,b1,c1,a1.length,a1[0].length,b1.length,b1[0].length);
printMatrix(c1);
printMatrixE(c1);
}
public static double[][] readMatrix() {
int rows = sc.nextInt();
int cols = sc.nextInt();
double[][] result = new double[rows][cols];
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
result[i][j] = sc.nextDouble();
}
}
return result;
}
public static void printMatrix(double[][] mat) {
System.out.println("Matrix["+mat.length+"]["+mat[0].length+"]");
int rows = mat.length;
int columns = mat[0].length;
for (int i = 0; i < rows; i++) {
for (int j = 0; j < columns; j++) {
System.out.printf("%8.3f " , mat[i][j]);
}
System.out.println();
}
System.out.println();
}
public static void printMatrixE(double[][] mat) {
System.out.println("Matrix["+mat.length+"]["+mat[0].length+"]");
int rows = mat.length;
int columns = mat[0].length;
for (int i = 0; i < rows; i++) {
for (int j = 0; j < columns; j++) {
System.out.printf("%9.2e " , mat[i][j]);
}
System.out.println();
}
System.out.println();
}
public static double[][] multMatrix(double a[][], double b[][]){//a[m][n], b[n][p]
if(a.length == 0) return new double[0][0];
if(a[0].length != b.length) return null; //invalid dims
int n = a[0].length;
int m = a.length;
int p = b[0].length;
double ans[][] = new double[m][p];
for(int i = 0;i < m;i++){
for(int j = 0;j < p;j++){
ans[i][j]=0;
for(int k = 0;k < n;k++){
ans[i][j] += a[i][k] * b[k][j];
}
}
}
return ans;
}
public static void mults(double a[][], double b[][], double c[][], int r1,
int c1, int r2, int c2){
for(int i = 0;i < r1;i++){
for(int j = 0;j < c2;j++){
c[i][j]=0;
for(int k = 0;k < c1;k++){
c[i][j] += a[i][k] * b[k][j];
}
}
}
}
}
where as input matrix you can enter
inE.txt
4 4
1 1 1 1
2 4 8 16
3 9 27 81
4 16 64 256
4 3
4.0 -3.0 4.0
-13.0 19.0 -7.0
3.0 -2.0 7.0
-1.0 1.0 -1.0
in unix like cmmd line execute the command:
$ java MatmultE < inE.txt > outE.txt
and you get the output
outC.txt
Matrix[2][2]
4.000 3.000
2.000 1.000
Matrix[2][2]
-0.500 1.500
1.000 -2.000
Matrix[2][2]
1.000 0.000
0.000 1.000
Matrix[3][4]
1.000 2.000 -2.000 0.000
-3.000 4.000 7.000 2.000
6.000 0.000 3.000 1.000
Matrix[4][2]
-1.000 3.000
0.000 9.000
1.000 -11.000
4.000 -5.000
Matrix[3][2]
-3.000 43.000
18.000 -60.000
1.000 -20.000
Matrix[4][3]
-7.000 15.000 3.000
-36.000 70.000 20.000
-105.000 189.000 57.000
-256.000 420.000 96.000
Matrix[4][3]
-7.00e+00 1.50e+01 3.00e+00
-3.60e+01 7.00e+01 2.00e+01
-1.05e+02 1.89e+02 5.70e+01
-2.56e+02 4.20e+02 9.60e+01
My code is super easy and works for any order of matrix
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println(" Enter No. of rows in matrix 1 : ");
int arows = sc.nextInt();
System.out.println(" Enter No. of columns in matrix 1 : ");
int acols = sc.nextInt();
System.out.println(" Enter No. of rows in matrix 2 : ");
int brows = sc.nextInt();
System.out.println(" Enter No. of columns in matrix 2 : ");
int bcols = sc.nextInt();
if (acols == brows) {
System.out.println(" Enter elements of matrix 1 ");
int a[][] = new int[arows][acols];
int b[][] = new int[brows][bcols];
for (int i = 0; i < arows; i++) {
for (int j = 0; j < acols; j++) {
a[i][j] = sc.nextInt();
}
}
System.out.println(" Enter elements of matrix 2 ");
for (int i = 0; i < brows; i++) {
for (int j = 0; j < bcols; j++) {
b[i][j] = sc.nextInt();
}
}
System.out.println(" The Multiplied matrix is : ");
int sum = 0;
int c[][] = new int[arows][bcols];
for (int i = 0; i < arows; i++) {
for (int j = 0; j < bcols; j++) {
for (int k = 0; k < brows; k++) {
sum = sum + a[i][k] * b[k][j];
c[i][j] = sum;
}
System.out.print(c[i][j] + " ");
sum = 0;
}
System.out.println();
}
} else {
System.out.println("Order of matrix in invalid");
}
}
multiply 4x4 Matrixes
float[] mul(float[] l, float[] r) {
float[] res = new float[16];
for (int i = 0; i < 16; i++) {
int y = i / 4;
int x = i % 4;
res[i] = l[x] * r[y] +
l[x + 4] * r[y + 4] +
l[x + 8] * r[y + 8] +
l[x + 12] * r[y + 12];
}
return res;
}
import java.util.*;
public class Mult {
public static int[][] C;
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
System.out.println("Enter Row of Matrix A");
int Rowa = s.nextInt();
System.out.println("Enter Column of Matrix A");
int Cola = s.nextInt();
System.out.println("Enter Row of Matrix B");
int Rowb = s.nextInt();
System.out.println("Enter Column of Matrix B");
int Colb = s.nextInt();
int[][] A = new int[Rowa][Cola];
int[][] B = new int[Rowb][Colb];
C = new int[Rowa][Colb];
//int[][] C = new int;
System.out.println("Enter Values of Matrix A");
for (int i = 0; i < A.length; i++) {
for (int j = 0; j < A.length; j++) {
A[i][j] = s.nextInt();
}
}
System.out.println("Enter Values of Matrix B");
for (int i = 0; i < B.length; i++) {
for (int j = 0; j < B.length; j++) {
B[i][j] = s.nextInt();
}
}
if (Cola == Rowb) {
for (int i = 0; i < A.length; i++) {
for (int j = 0; j < A.length; j++) {
C[i][j] = 0;
for (int k = 0; k < B.length; k++) {
C[i][j] += A[i][k] * B[k][j];
}
}
}
} else {
System.out.println("Cannot multiply");
}
// Printing matrix A
/*
for (int i = 0; i < A.length; i++) {
for (int j = 0; j < A.length; j++) {
System.out.print(A[i][j] + "\t");
}
System.out.println();
}
*/
for (int i = 0; i < A.length; i++) {
for (int j = 0; j < A.length; j++) {
System.out.print(C[i][j] + "\t");
}
System.out.println();
}
}
}
#include <stdio.h>
int main()
{
int row = 2;
int col = 3;
int a[row][col];
int count = 1;
printf("Array A \n");
for (int i = 0; i < row; i++)
{
for (int j = 0; j < col; j++)
{
a[i][j] = count;
count++;
printf(" %d ", a[i][j]);
}
printf("\n");
}
int b[col][row];
printf("\nArray B \n");
for (int i = 0; i < col; i++)
{
for (int j = 0; j < row; j++)
{
b[i][j] = count;
count++;
printf(" %d ", b[i][j]);
}
printf("\n");
}
printf("\n A * B \n");
int c[row][col];
int mul = 1, plus = 0;
for (int i = 0; i < row; i++)
{
for (int j = 0; j < row; j++)
{
plus = 0;
for (int k = 0; k < col; k++)
{
mul = a[i][k] * b[k][j];
plus += mul;
}
c[i][j] = plus;
printf(" %d ", c[i][j]);
}
printf("\n");
}
}
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("");
}
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.