-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMatrix2D.java
More file actions
151 lines (148 loc) · 4.75 KB
/
Matrix2D.java
File metadata and controls
151 lines (148 loc) · 4.75 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
// Created by Brendan C. Reidy
// Created on April 26, 2019
// Two Dimensional Matrix
// Desc: Matrix2D is a matrix containing vectors of varying length
// Last Modified 4/30/19 @ 3:40 PM
import java.io.File;
import java.io.FileNotFoundException;
import java.io.PrintWriter;
import java.util.Scanner;
public class Matrix2D {
public static final String DELIM = ",";
public Vector[] matrix;
public int length;
public Matrix2D(){
this.matrix = null;
int length = 0;
}
public Matrix2D(int size){
this.matrix = new Vector[size];
this.length = 0;
}
public Matrix2D(float[] values){
this.matrix = new Vector[1];
this.matrix[0] = new Vector(values);
this.length = 1;
}
public Matrix2D(int[] values){
this.matrix = new Vector[1];
this.matrix[0] = new Vector(values);
this.length = 1;
}
public Matrix2D(double[] values){
this.matrix = new Vector[1];
this.matrix[0] = new Vector(values);
this.length = 1;
}
public void add(float[] values){
Vector[] temp = new Vector[this.length+1];
for(int i=0; i<this.length; i++)
temp[i] = matrix[i];
temp[this.length] = new Vector(values);
this.length = this.length+1;
this.matrix = temp;
}
public void add(int[] values){
Vector[] temp = new Vector[this.length+1];
for(int i=0; i<this.length; i++)
temp[i] = matrix[i];
temp[this.length] = new Vector(values);
this.length = this.length+1;
this.matrix = temp;
}
public void add(double[] values){
Vector[] temp = new Vector[this.length+1];
for(int i=0; i<this.length; i++)
temp[i] = matrix[i];
temp[this.length] = new Vector(values);
this.length = this.length+1;
this.matrix = temp;
}
public void setValue(float aValue, int x, int y){
Vector values = matrix[x];
values.set(aValue, y);
}
public float[] getArrayAt(int index){
return matrix[index].toArray();
}
public String toString(){
String str="";
for(int x=0; x<this.length; x++){
float[] current = getArrayAt(x);
for(int y=0; y<current.length; y++){
str += current[y] + ",";
}
if(x!=this.length-1)
str += "\n";
}
return str;
}
public void print(){
System.out.println(toString());
}
public void printInt(){
String str="";
for(int x=0; x<this.length; x++){
float[] current = getArrayAt(x);
for(int y=0; y<current.length; y++){
str += (int) (current[y] + 0.5) + ",";
}
str += "\n";
}
System.out.println(str);
}
public void saveToFile(String aFileName){
if(aFileName==null)
return;
try{
PrintWriter writer = new PrintWriter(aFileName, "UTF-8");
for(int index = 0; index<this.length; index++){
String str = "";
float[] values = getArrayAt(index);
for(int i=0; i<values.length; i++){
str+=values[i] + ",";
}
writer.println(str);
}
writer.close();
}catch(FileNotFoundException e){
System.out.println("File does not exist");
}catch(Exception e){
System.out.println(e.getMessage());
}
}
public static Matrix2D reverse(Matrix2D aMatrix){
Matrix2D newMatrix = new Matrix2D();
for(int i=aMatrix.length-1; i>=0; i--)
newMatrix.add(aMatrix.getArrayAt(i));
return newMatrix;
}
public static Matrix2D loadFromFile(String aFileName){
System.out.println("Loading: " + aFileName);
Matrix2D newMatrix = new Matrix2D();
if(aFileName==null)
return null;
try {
Scanner fileScanner = new Scanner(new File(aFileName));
String fileLine;
String[] splitLines;
int line = 0;
while (fileScanner.hasNext()) {
fileLine = fileScanner.nextLine();
splitLines = fileLine.split(DELIM);
float[] values = new float[splitLines.length];
for(int i=0; i<splitLines.length; i++){
float current = Float.parseFloat(splitLines[i]);
values[i]=current;
}
newMatrix.add(values);
}
fileScanner.close();
}catch(FileNotFoundException e){
System.out.println("File does not exist");
}catch(Exception e){
System.out.println(e.getMessage());
}
return newMatrix;
}
}