Matrices III: The ultimate matrix
Description:
This is the third member of a series of katas about matrices.
In the previous katas, we have made Matrix
classes capable of setting and retrieving the elements in the matrix of performing operations on them. Now we will combine the mathematical functionality with the generic element type.
Task
Implement the following methods and constructors in the generic Matrix
class:
Matrix()
: the constructor takes a two dimensional array and converts the values to store internally. The argument mustn't be null, mustn't contain null elements and the subarrays' length must be the same. Matrices cannot be of size 0 in any dimension. The second the constructor should take 2 int parameters, the number of rows and columns respectively, and intialize a matrix of the given size.get()
: returns the element in the matrix at position(i, j)
. Indices range from 1 to the number of of rows/columns.set()
: changes the element at position(i, j)
to the value passed to the method.transpose()
: returns the transpose of the matrix (rows and columns flipped)add()
: adds the argument to this matrix; only matrices with the same dimensions can be added.multiply()
: multiplies the argument with this matrix, so A.multiply(B) is equivalent to AB (see the rules of matrix multiplcation in previous kata or wikipedia).multiplyLeft()
andmultiplyRight()
: multiplies this matrix with the given non-null argument, multiplying each element with the argument. The first method does c*e, the second e*c for every element e and the argument c.pow()
: raises this matrix to a positive integer power. Only defined for square matrices (number of rows and columns are equal).subMatrix()
: discards the i-th row and j-th column of the matrix.toArray()
: converts this matrix to a two-dimensional array with element type of the matrix' element type.
All methods that would modify the original matrix should return a new matrix instead, so the Matrix
class should be immutable. If an argument doesn't hold to the requirements specified in the description of the method, you should throw an IndexOutOfBoundsException
if it's an index issue, or an IllegalArgumentException
otherwise. Only exception to that rule, is when you want to raise a non-square matrix to any power: then you should throw an UnsupportedOperationException
.
Generics
Keep in mind that you need to write all the methods and constructors using generics, so that the matrix can contain any type of element. The type parameter of the matrix is the element type of the matrix. Now the problem is, how do you add or multiply together two objects which are not numbers?
For that purpose, you need to define the MatrixElement
interface (outside the Matrix
˙class), which has 2 abstract methods: add
and muliply
. In order to ensure type safety, the interface itself should be generic as well, and the two methods should take 1 argument and return an object both of the type specified by the type parameter.
I let you figure out how to implement exactly those parameters, but the error messages given by the JVM will probably help you out. You also need to make it so that a matrix can contain other matrices in order to be able to nest matrices, which is just pretty cool IMO.
Similar Kata:
Stats:
Created | Oct 10, 2016 |
Published | Oct 30, 2016 |
Warriors Trained | 87 |
Total Skips | 17 |
Total Code Submissions | 189 |
Total Times Completed | 5 |
Java Completions | 5 |
Total Stars | 5 |
% of votes with a positive feedback rating | 0% of 1 |
Total "Very Satisfied" Votes | 0 |
Total "Somewhat Satisfied" Votes | 0 |
Total "Not Satisfied" Votes | 1 |
Total Rank Assessments | 1 |
Average Assessed Rank | 5 kyu |
Highest Assessed Rank | 5 kyu |
Lowest Assessed Rank | 5 kyu |