/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package matrix;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Random;
import java.util.Scanner;
public class MatrixUtils {
public static void fillByRandom(Matrix m) {
int rowCount = m.getRowCount();
int colCount = m.getColCount();
Random random = new Random();
for (int i = 1; i <= rowCount; i++) {
for (int j = 1; j <= colCount; j++) {
try {
m.put(i, j, random.nextInt(50));
} catch (MatrixIndexException e) {
System.err.println(e.getMessage());
}
}
}
}
public static void fillByNumber(Matrix m, int number) {
int rowCount = m.getRowCount();
int colCount = m.getColCount();
for (int i = 1; i <= colCount; i++) {
for (int j = 1; j <= rowCount; j++) {
try {
m.put(i, j, number);
} catch (MatrixIndexException e) {
System.err.println(e.getMessage());
}
}
}
}
public static Matrix fillByKeyboard() {
Scanner scan = new Scanner(System.in);
System.out.println("Enter The Number Of Matrix Rows:");
int rowCount = scan.nextInt();
System.out.println("Enter The Number Of Matrix Columns:");
int colCount = scan.nextInt();
try {
Matrix2D m = new Matrix2D(rowCount, colCount);
System.out.println("Enter Matrix Data:");
for (int i = 1; i <= rowCount; i++) {
for (int j = 1; j <= colCount; j++) {
int value = scan.nextInt();
m.put(i, j, value);
}
}
return m;
} catch (MatrixIndexException ex) {
System.err.println(ex.getMessage());
}
return null;
}
public static Matrix MatrixMultiple(Matrix a, Matrix b) throws MatrixIndexException {
int aRows = a.getRowCount();
int aColumns = a.getColCount();
int bRows = b.getRowCount();
int bColumns = b.getColCount();
if (aColumns != bRows) {
throw new IllegalArgumentException("A:Rows: " + aColumns + " did not match B:Columns " + bRows + ".");
}
Matrix2D c = new Matrix2D(aRows, bColumns);
for (int i = 0; i < aRows; i++) {
for (int j = 0; j < bColumns; j++) {
c.put(i+1,j+1,0);
}
}
for (int i = 0; i < aRows; i++) {
for (int j = 0; j < bColumns; j++) {
for (int k = 0; k < aColumns; k++) {
c.put(i+1,j+1,c.get(i+1,j+1) + a.get(i+1, k+1) * b.get(k+1, j+1));
}
}
}
return c;
}
public static Matrix writeMatrixToFile(m, filename) {
}
}