Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public E get(int i, int j){
- /*
- * I'm assuming that this XMatrix has the following:
- * size: how big the matrix is
- * top: array containing the elements at the top of the matrix
- * diagonals: array containing the diagonal elements
- * bottom: array containing the elements at the bottom of the matrix
- *
- * Also assuming row-major mapping for the diagonal elements.
- */
- /* the return value */
- E ret = null;
- /* make sure we're not given a 0 or something too big (no 0s because matrices start at 1) */
- if(1 <= i && i <= size){
- if(1 <= j && j <= size){
- if(i == 1){
- /* on the top row */
- ret = top[j - 1];
- } else if(i == size){
- /* on the bottom row */
- ret = bottom[j - 1];
- } else{
- /* check to see if it's on the X */
- if((i == j) || (i * 2 == j)){
- /*
- * The diagonals matrix has matrix.size rows
- * and matrix.size - 2 columns. Each row has 2 elements,
- * except for the middle row which only has 1.
- * If an element is in the middle of the matrix,
- * then it is in the middle of the diagonals array.
- * Else it's on a diagonal. Since there are 2 elements in each row,
- * minus one for the middle row
- */
- int middle = (size + 1) / 2;
- if(i == middle && j == middle)
- ret = diagonals[middle];
- /*
- * I know these next four lines aren't correct,
- * but I spent way to long thinking about them
- */
- else if(j < middle)
- ret = diagonals[middle - ((i * 2) + (j - 1))];
- else if(j > middle)
- ret = diagonals[((i * 2) + j - 1) - middle];
- }
- }
- }
- }
- return ret;
- }
Add Comment
Please, Sign In to add comment