-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMatrix.java
More file actions
150 lines (136 loc) · 4.94 KB
/
Copy pathMatrix.java
File metadata and controls
150 lines (136 loc) · 4.94 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
/*
* PROJECT III: Matrix.java
*
* This file contains a template for the class Matrix. Not all methods are
* implemented and they do not have placeholder return statements. Make sure
* you have carefully read the project formulation before starting to work
* on this file.
*
* Some of the methods here are abstract. That means that they must be
* implemented by their subclasses. If you're not sure about abstract classes,
* you should consult the lecture notes for more information.
*
* Remember not to change the names, parameters or return types of any
* variables in this file!
*
* The function of the methods and instance variables are outlined in the
* comments directly above them.
*
* Tasks:
*
* 1) Complete this class with the indicated methods and instance variables.
*
* 2) Fill in the following fields:
*
* NAME: Archie Bevan
* UNIVERSITY ID: 5538165
* DEPARTMENT: Maths and stats
*/
public abstract class Matrix {
/**
* Two variables to describe the dimensions of the Matrix.
*/
protected int iDim;
protected int jDim;
/**
* Constructor function. This is protected since abstract classes cannot
* be instantiated anyway. Subclasses should call this function from their
* constructors to set iDim and jDim.
*
* @param firstDim The first dimension of the matrix.
* @param secondDim The second dimension of the matrix.
*/
protected Matrix(int firstDim, int secondDim) {
// This method is complete.
iDim = firstDim;
jDim = secondDim;
}
// public int getIDim(){
// return iDim;
// }
// pubilc int getJDim(){
// return jDim;
// }
/**
* Returns a String representation of the Matrix using the getIJ getter
* function. You should use String.format() to format the double numbers to a
* sensible number of decimal places and place the numbers in columns.
* Remember: %n at the end of each row in the string will insert a
* newline character.
*
* e.g.
* -2.00 0.00 0.00
* 1.00 -1.00 0.12
* 0.00 0.00 1.00
*
* @return A String representation of the Matrix.
*/
public String toString() {
// You need to fill in this method.
/*Here i am initialising a variable called output by putting it equal to "" so i can add values to the
string using the get function. I then use two for loops to represent the row and column dimensions starting from 0 and ending at
the dimension-1 . I then use the get function to get the value for that specfic co-ordinate. Then i made a string where i added the ij
value and used %.2f to split up each row. I had to use String.format in order to make this work as i kept encountering the "%.2f"
being printed rather than it separating a new line. I then used += to update the output string every iteration
*/
String output = "";
for(int i=0;i <= iDim-1;i++){
for(int j=0;j <= jDim-1;j++){
double ij = getIJ(i,j);
String newdpij = String.format("%.2f",ij);
output += newdpij + " ";
}
output += "%n";
}
return output.toString() ;
}
/**
* Getter function: return the (i,j)'th entry of the matrix.
*
* @param i The location in the first co-ordinate.
* @param j The location in the second co-ordinate.
* @return The (i,j)'th entry of the matrix.
*/
public abstract double getIJ(int i, int j);
/**
* Setter function: set the (i,j)'th entry of the data array.
*
* @param i The location in the first co-ordinate.
* @param j The location in the second co-ordinate.
* @param val The value to set the (i,j)'th entry to.
*/
public abstract void setIJ(int i, int j, double val);
/**
* Return the determinant of this matrix.
*
* @return The determinant of the matrix.
*/
public abstract double determinant();
/**
* Add the matrix to another second matrix.
*
* @param second The Matrix to add to this matrix.
* @return The sum of this matrix with the second matrix.
*/
public abstract Matrix add(Matrix second);
/**
* Multiply the matrix by another matrix A. This is a _left_ product,
* i.e. if this matrix is called B then it calculates the product BA.
*
* @param A The Matrix to multiply by.
* @return The product of this matrix with the matrix A.
*/
public abstract Matrix multiply(Matrix A);
/**
* Multiply the matrix by a scalar.
*
* @param scalar The scalar to multiply the matrix by.
* @return The product of this matrix with the scalar.
*/
public abstract Matrix multiply(double scalar);
/**
* Fills the matrix with random numbers which are uniformly distributed
* between 0 and 1.
*/
public abstract void random();
}