-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMatrix.java
More file actions
118 lines (108 loc) · 3.46 KB
/
Matrix.java
File metadata and controls
118 lines (108 loc) · 3.46 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
/*
* PROJECT III: Matrix.java
*
* This file contains a template for the class Matrix. Not all methods are
* implemented. 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.
*/
public abstract class Matrix {
/**
* Two variables to describe the dimensions of the Matrix.
*/
protected int m, n;
/**
* Constructor function. This is protected since abstract classes cannot
* be instantiated anyway. Subclasses should call this function from their
* constructors to set m and n.
*
* @param M The first dimension of the matrix.
* @param N The second dimension of the matrix.
*/
protected Matrix(int M, int N) {
// This method is complete.
m = M; n = N;
}
/**
* Returns a String representation of the Matrix using the getIJ getter
* function. You should use MaF.dF to format the double numbers to a
* sensible number of decimal places and place the numbers in columns.
*
* @return A String representation of the Matrix.
*/
public String toString() {
String l = "";
for (int i = 0; i < this.m; i++) {
for(int j = 0; j < this.n; j++) {
if (j == 0) {
l = l + "[";
}
l = l + MaF.dF(getIJ(i,j),4,3);
if (j == this.n - 1) {
} else {
l = l + ",";
}
}
l = l + "]" + "\n";
}
return l;
}
/**
* 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 matrix A.
*
* @param A The Matrix to add to this matrix.
* @return The sum of this matrix with the matrix A.
*/
public abstract Matrix add(Matrix A);
/**
* 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 a The scalar to multiply the matrix by.
* @return The product of this matrix with the scalar a.
*/
public abstract Matrix multiply(double a);
/**
* Fills the matrix with random numbers which are uniformly distributed
* between 0 and 1.
*/
public abstract void random();
}