# Simple implementation of nxn Dense Matrix in Java

So far this is what I have completed. Still working on these two methods

• boolean isIdentityMatrix(DenseMatrix matrix);
• boolean isInvertible(DenseMatrix matrix);

Suggestions are appreciated. Thanks.

//Dense Matrix

package matrix.denseMatrix;

import java.util.Arrays;

public class DenseMatrix
{
private int[][] elements;
private int n;

// in default 3x3 matrix will be created
public DenseMatrix()
{
this(3);
}

public DenseMatrix(int size)
{
n = size;
elements = new int[n][n];

for(int[] rows : elements)
{
Arrays.fill(rows, 0);
}
}

public int size()
{
return n;
}

public int getValue(int i, int j)
{
if((i | j) >= n || (i | j) < 0)
{
throw new IndexOutOfBoundsException();
}
else
{
return elements[i][j];
}
}

public boolean set(int i, int j, int value)
{
if((i | j) >= n || (i | j) < 0)
{
throw new IndexOutOfBoundsException();
}
else
{
elements[i][j] = value;
return true;
}
}

public boolean increaseBy(int i, int j, int valueBy) throws IndexOutOfBoundsException
{
return set(i, j, getValue(i, j) + valueBy);
}

public boolean decreaseBy(int i, int j, int valueBy) throws IndexOutOfBoundsException
{
return set(i, j, getValue(i, j) - valueBy);
}

public void clear()
{
elements = null;
n = 0;
}

{
if(this.size() != other.size())
{
return null;
}
else
{
DenseMatrix result = new DenseMatrix(this.size());

for(int i = 0; i < this.size(); i++)
{
for(int j = 0; j < this.size(); j++)
{
result.set(i, j, this.getValue(i, j) + other.getValue(i, j));
}
}
return result;
}
}

public DenseMatrix negative()
{
DenseMatrix result = new DenseMatrix(this.size());

for(int i = 0; i < this.size(); i++)
{
for(int j = 0; j < this.size(); j++)
{
result.set(i, j, this.getValue(i, j) * -1);
}
}
return result;
}

public DenseMatrix multiply(DenseMatrix other)
{
if(this.size() != other.size())
{
return null;
}
else
{
DenseMatrix result = new DenseMatrix(this.size());

for(int i = 0; i < this.size(); i++)
{
for(int j = 0; j < this.size(); j++)
{
for(int k = 0; k < this.size(); k++)
{
result.increaseBy(i, j, (this.getValue(i, k) * other.getValue(k, j)));
}
}
}
return result;
}
}

public DenseMatrix transpose()
{
DenseMatrix result = new DenseMatrix(this.size());

for(int i = 0; i < this.size(); i++)
{
for(int j = 0; j < this.size(); j++)
{
result.set(i, j, this.getValue(j, i));
}
}
return result;
}

@Override
public boolean equals(Object object)
{
if (this == object)
{
return true;
}
if (!(object instanceof DenseMatrix))
{
return false;
}

DenseMatrix that = (DenseMatrix) object;

if(this.size() != that.size())
{
return false;
}
else
{
boolean isEqual = true;
CHECKING: for(int i = 0; i < this.size(); i++)
{
for(int j = 0; j < this.size(); j++)
{
if(this.getValue(i, j) != that.getValue(i, j))
{
isEqual = false;
break CHECKING;
}
}
}
return isEqual;
}
}

@Override
public DenseMatrix clone()
{
DenseMatrix clonedMatrix = new DenseMatrix(this.size());

for(int i = 0; i < this.size(); i++)
{
for(int j = 0; j < this.size(); j++)
{
clonedMatrix.set(i, j, this.getValue(i, j));
}
}
return clonedMatrix;
}
}


if((i | j) >= n || (i | j) < 0)


This line does not work as you probably expect: i | j is the binary or function and will create a new int, like in this example:

i = 0101_b;
j = 0011_b;
k = i | j; // == 0111_b


You will get false IndexOutOfBoundsException with this, for example, if your matrix is 3x3, and you call getValue(2, 1).

• A 2x2 matrix can have the max index of (1, 1). Any index being above 1 will throw IndexOutOfBoundsException. – Hamidur Rahman Aug 23 '18 at 18:03
• I typed the wrong number, sorry. I meant 3x3. You will get IndexOutOfBounds because 2 | 1 = 3, 3 >= 3 is true and therefore you will get an IndexOutOfBounds when you have a 3x3 matrix and enter getValue(2,1) – TreffnonX Feb 19 '20 at 14:50

one dimensional array
Defining the backing array with only one dimension allows immediatly to use the class Arrays - instead of row- or column-wise. It provides functionality which then you don't need to write.

private int[] elements;

Initialization:
elements = new int[n*n];

That's all. There is no need to fill the array row-wise with 0. According to JLS all elements are initialized with the default value of the component type. This is 0 in case of int,

Determine the position of an element to get or set it's value: elements[i * n + j];

comparing two arrays for equality
When using a one dimensional array you can call Arrays.equals(int[] a, int[] b) to check if the backing arrays are equal.

cloning the matrix
When using a one dimensional array you can call Arrays.copyOf(elements, elements.length) to create a copy of the backing array using a native method which performs very fast.

dedicated zero-size matrix
If your application will use frequently zero-size matrices, you could follow the example of Collections.emptyMap() and implement a dedicated subtype of DenseMatrix which will have simplified implementations of the mathods provided by DenseMatrix. Storing such a zero-sized matrix as a singleton will save memory and creation time.