1. Introduction

Matrices are a fundamental concept in mathematics and computer science. They are used in a variety of applications, including machine learning, image processing, and computer graphics.

In brief, a matrix is a rectangular array of numbers, which we can think of as a grid of values. We can represent a matrix using a multi-dimensional array in Kotlin.

In this tutorial, we’ll learn how to add and multiply two matrices using multi-dimensional arrays in Kotlin.

2. How to Add Two Matrices

To add two matrices in Kotlin, we first need to ensure that both matrices have the same dimensions, meaning they have the same number of rows and columns.

Once we confirm this, we can create a new matrix to hold the sum of the two input matrices.

Then, we iterate through both matrices and add their corresponding elements together, storing the results in the new matrix.

Finally, we return the new matrix with the added values:

fun addMatrices(matrix1: Array<Array<Int>>, matrix2: Array<Array<Int>>): Array<Array<Int>> {
    val row = matrix1.size
    val col = matrix1[0].size
    val sum = Array(row) { Array(col) { 0 } }

    for (i in 0 until row) {
        for (j in 0 until col) {
            sum[i][j] = matrix1[i][j] + matrix2[i][j]
        }
    }

    return sum
}

Let’s then test this method for correctness:

@Test
fun `test cases to ensure correct addition of matrices`() {
    val matrix1 = arrayOf(
        arrayOf(1, 2),
        arrayOf(3, 4)
    )

    val matrix2 = arrayOf(
        arrayOf(5, 6),
        arrayOf(7, 8)
    )

    val expected = arrayOf(
        arrayOf(6, 8),
        arrayOf(10, 12)
    )

    assertArrayEquals(expected, addMatrices(matrix1, matrix2))
}

3. How to Multiply Two Matrices

Matrix multiplication is a bit more involved than adding matrices.

To multiply two matrices, it’s crucial that the number of columns in the first matrix matches the number of rows in the second matrix. This ensures compatibility.

The dimensions of the resulting matrix will have the number of rows of the first matrix and the number of columns of the second matrix.

Finally, to find each element in the result, we multiply corresponding elements from the first matrix’s row and the second matrix’s column, then sum them. We repeat this process for each element in the result, giving us the final matrix:

fun multiplyMatrices(matrix1: Array<Array<Int>>, matrix2: Array<Array<Int>>): Array<Array<Int>> {
    val row1 = matrix1.size
    val col1 = matrix1[0].size
    val col2 = matrix2[0].size
    val product = Array(row1) { Array(col2) { 0 } }

    for (i in 0 until row1) {
        for (j in 0 until col2) {
            for (k in 0 until col1) {
                product[i][j] += matrix1[i][k] * matrix2[k][j]
            }
        }
    }

    return product
}

As usual, let’s make sure our method works as expected:

@Test
fun `test cases to ensure correct multiplication of Matrices`() {
    val matrix1 = arrayOf(
        arrayOf(1, 2),
        arrayOf(3, 4)
    )

    val matrix2 = arrayOf(
        arrayOf(5, 6),
        arrayOf(7, 8)
    )

    val expected = arrayOf(
        arrayOf(19, 22),
        arrayOf(43, 50)
    )

    assertArrayEquals(expected, multiplyMatrices(matrix1, matrix2))
}

4. Conclusion

In this article, we saw how to perform addition and multiplication of matrices using Kotlin. We also included unit tests to ensure our methods function correctly. Furthermore, matrices are powerful tools we can use in a variety of applications, and it’s essential to understand how to work with them.

As always, the source code for the examples is available over on GitHub.