Java Matrices: Write a program that reads in two matrices from two different tex
ID: 3746749 • Letter: J
Question
Java Matrices:
Write a program that reads in two matrices from two different text files. Should use a singly or double linked list to store the two matrices and perform the following operations on them:
- Add, Subtract, Mulitply, Transpose, and Determinant
Requirements:
Use singly or doubly linked list date structures only:
- No other library methods or existing collection framework.
- No use of Array, ArrayList, List, or Vectors for storing.
- No two-dimensional arrays.
- Input files can only be read Exactly Once for all operations.
For the determinant operation, you may augment your linked list node to retain row/column id and employ recursion to directly implement the standard method for computing determinant of a matrix. You are encouraged to design your own node representation (e.g., each node element has two pointers: one to its next right and another to its next bottom element that facilitate both horizontal and vertical traversals like one gets in a 2d array)
Inputs:
The program takes 3 or 4 arguments as input. Assuming input matrix are in space deliminated files a.txt and b.txt, an few examples of format operation query will be:
- Addition: add a.txt b.txt output.txt
- Transpose: tra a.txt output.txt
- Determinant: det a.txt output.txt
The input file can contains only integer (e.g. -3). However your output should be in floating point format with 1 decimal place (e.g. -3.0).
Example Input:
Input A
11 17 20 13 1
8 6 7 20 1
10 18 15 10 6
12 11 3 10 6
7 20 8 8 11
Input B
12 3 5 19 13
11 4 15 6 17
14 8 17 11 18
4 4 16 12 19
5 12 12 14 1
*Please include all operations when answering, especially the determinant. Also, make sure your code works with the provided matrices.
Explanation / Answer
Determinant operation, you may augment your linked list node to retain row/column id and employ recursion to directly implement the standard method for computing determinant of a matrix.
import java.io.BufferedOutputStream;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Scanner;
class UserMatrix {
private int row;
public int getRow() {
return row;
}
private int column;
public int getColumn() {
return column;
}
public LinkedList root[];
public UserMatrix(int row, int column) {
this.row = row;
this.column = column;
root = new LinkedList[row];
}
}
class LinkedList {
int data;
LinkedList next;
public LinkedList() {
// TODO Auto-generated constructor stub
}
public LinkedList(int data) {
// TODO Auto-generated constructor stub
this.data = data;
this.next = null;
}
}
class Operation {
public UserMatrix add(UserMatrix A, UserMatrix B) {
UserMatrix c = new UserMatrix(A.getRow(), A.getColumn());
int rowC = A.getRow();
for (int i = 0; i < rowC; i++) {
LinkedList C = null, obj1 = null, obj2 = A.root[i], obj3 = B.root[i];
while (obj2 != null && obj3 != null) {
if (C == null) {
obj1 = new LinkedList(obj2.data + obj3.data);
C = obj1;
obj2 = obj2.next;
obj3 = obj3.next;
} else {
obj1.next = new LinkedList(obj2.data + obj3.data);
obj1 = obj1.next;
obj2 = obj2.next;
obj3 = obj3.next;
}
}
c.root[i] = C;
}
return c;
}
public UserMatrix subtract(UserMatrix A, UserMatrix B) {
UserMatrix c = new UserMatrix(A.getRow(), A.getColumn());
int rowC = A.getRow();
for (int i = 0; i < rowC; i++) {
LinkedList C = null, obj1 = null, obj2 = A.root[i], obj3 = B.root[i];
while (obj2 != null && obj3 != null) {
if (C == null) {
obj1 = new LinkedList(obj2.data - obj3.data);
C = obj1;
obj2 = obj2.next;
obj3 = obj3.next;
} else {
obj1.next = new LinkedList(obj2.data - obj3.data);
obj1 = obj1.next;
obj2 = obj2.next;
obj3 = obj3.next;
}
}
c.root[i] = C;
}
return c;
}
public int getItem(UserMatrix A, int i, int j) {
LinkedList obj = A.root[i];
int k = 0;
while (obj != null) {
if (k == j) {
return obj.data;
}
k++;
obj = obj.next;
}
return -1;
}
public UserMatrix transpose(UserMatrix A) {
UserMatrix c = new UserMatrix(A.getColumn(), A.getRow());
int rowC = c.getRow();
for (int i = 0; i < rowC; i++) {
LinkedList C = null, obj1 = null;
for (int j = 0; j < c.getColumn(); j++) {
if (j == 0) {
int temp = getItem(A, j, i);
obj1 = new LinkedList(temp);
C = obj1;
} else {
int temp = getItem(A, j, i);
obj1.next = new LinkedList(temp);
obj1 = obj1.next;
}
}
c.root[i] = C;
}
return c;
}
public UserMatrix multiply(UserMatrix A, UserMatrix B) {
if (A.getColumn() != B.getRow()) {
System.out.println("dimension mismatch ");
return null;
}
UserMatrix c = new UserMatrix(A.getRow(), B.getColumn());
int rowC = c.getRow();
B = transpose(B);
for (int i = 0; i < rowC; i++) {
LinkedList C = null, obj1 = null;
for (int j = 0; j < c.getColumn(); j++) {
if (j == 0) {
int temp = getcal(A.root[i], B.root[j]);
obj1 = new LinkedList(temp);
C = obj1;
} else {
int temp = getcal(A.root[i], B.root[j]);
obj1.next = new LinkedList(temp);
obj1 = obj1.next;
}
}
c.root[i] = C;
}
return c;
}
private int getcal(LinkedList A, LinkedList B) {
// TODO Auto-generated method stub
int k = 0;
while (A != null) {
k += (A.data * B.data);
A = A.next;
B = B.next;
}
return k;
}
}
public class Solution {
public static void main(String[] args) throws IOException {
String str1 ,str2,str3;
str2="";
int operation = -1;
if (args.length <= 0)
{
System.out.println("No cmd arguments");
return;
}
else {
if (args[0].equalsIgnoreCase("add"))
operation = 0;
else if (args[0].equalsIgnoreCase("sub"))
operation = 1;
else if (args[0].equalsIgnoreCase("mul"))
operation = 2;
else if (args[0].equalsIgnoreCase("Tra"))
operation = 3;
}
if(operation==3)
{
str1=args[1];
str3=args[2];
}
else
{
str1=args[1];
str2=args[2];
str3=args[3];
}
File file = new File(str1);
Scanner sc1 = new Scanner(file);
int rowA = sc1.nextInt();
int columnA = sc1.nextInt();
UserMatrix obj = new UserMatrix(rowA, columnA);
for (int i = 0; i < rowA; i++) {
LinkedList A = null, obj1 = null;
for (int j = 0; j < columnA; j++) {
if (j == 0) {
obj1 = new LinkedList(sc1.nextInt());
A = obj1;
} else {
obj1.next = new LinkedList(sc1.nextInt());
obj1 = obj1.next;
}
}
obj.root[i] = A;
}
UserMatrix obj1 = null;
if(operation!=3)
{
File file1 = new File(str2);
Scanner sc2 = new Scanner(file1);
int rowB = sc2.nextInt();
int columnB = sc2.nextInt();
obj1 = new UserMatrix(rowB, columnB);
for (int i = 0; i < rowB; i++) {
LinkedList B = null, objA1 = null;
for (int j = 0; j < columnB; j++) {
if (j == 0) {
objA1 = new LinkedList(sc2.nextInt());
B = objA1;
} else {
objA1.next = new LinkedList(sc2.nextInt());
objA1 = objA1.next;
}
}
obj1.root[i] = B;
}
}
Operation op = new Operation();
UserMatrix C = null ;
System.out.println(operation);
switch(operation)
{
case 0: C= op.add(obj1, obj);
break;
case 1: C=op.subtract(obj1, obj);
break;
case 3:C=op.transpose(obj);
break;
case 2:C=op.multiply(obj1, obj);
break;
}
PrintWriter writer = new PrintWriter(str3, "UTF-8");
writer.print( C.getRow());
writer.println(" "+ C.getColumn());
writer.println();
for (int i = 0; i < C.getRow(); i++)
{
LinkedList objout = C.root[i];
for (int j = 0; j < C.getColumn() && objout != null; j++)
{
writer.print(objout.data);
writer.print(" ");
objout = objout.next;
}
writer.println("");
}
writer.close();
}
}
/*
A.txt
2 3
1 2 3
4 5 6
first row contains number of rows and column respectively
acc. to example rows=2 column=3
and others are element of matrix.
" file path are not cmd line input you should copy and paste acc. to main function"
*/
/* we run program Add a.txt b.txt out.txt
sub Add a.txt b.txt out.txt
mul a.txt b.txt out.txt
tra a.txt out.txt
*/
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.