Kotlin Program to Multiply Two Matrices Using Multi-dimensional Arrays

Matrix multiplication is a fundamental operation in linear algebra and has wide applications in various fields such as computer graphics, scientific computing, and machine learning. In Kotlin, we can perform matrix multiplication efficiently using multi-dimensional arrays. In this article, we will explore three real-world Kotlin Program to Multiply Two Matrices Using Multi-dimensional Arrays. Each example will showcase a different approach to matrix multiplication and provide insights into Kotlin’s versatility in handling mathematical computations.

1. Predefined Matrices Example

Kotlin
fun main() {
    val matrix1 = arrayOf(
        intArrayOf(1, 2, 3),
        intArrayOf(4, 5, 6)
    )
    val matrix2 = arrayOf(
        intArrayOf(7, 8),
        intArrayOf(9, 10),
        intArrayOf(11, 12)
    )

    val result = multiplyMatrices(matrix1, matrix2)
    printMatrix(result)
}

fun multiplyMatrices(matrix1: Array<IntArray>, matrix2: Array<IntArray>): Array<IntArray> {
    val rows1 = matrix1.size
    val cols1 = matrix1[0].size
    val cols2 = matrix2[0].size

    val result = Array(rows1) { IntArray(cols2) }

    for (i in 0 until rows1) {
        for (j in 0 until cols2) {
            for (k in 0 until cols1) {
                result[i][j] += matrix1[i][k] * matrix2[k][j]
            }
        }
    }

    return result
}

fun printMatrix(matrix: Array<IntArray>) {
    for (row in matrix) {
        for (element in row) {
            print("$element\t")
        }
        println()
    }
}

1.1 Explanation

This example demonstrates matrix multiplication using predefined matrices matrix1 and matrix2. The multiplyMatrices function calculates the product of two matrices, and the printMatrix function displays the result.

1.2 Output

The output of Example 1 will be:

Kotlin
58  64  
139 154

2. Randomly Generated Matrices Example

Kotlin
import kotlin.random.Random

fun main() {
    val rows1 = 3
    val cols1 = 4
    val cols2 = 5

    val matrix1 = generateRandomMatrix(rows1, cols1)
    val matrix2 = generateRandomMatrix(cols1, cols2)

    val result = multiplyMatrices(matrix1, matrix2)
    printMatrix(result)
}

fun generateRandomMatrix(rows: Int, cols: Int): Array<IntArray> {
    val matrix = Array(rows) { IntArray(cols) }

    for (i in 0 until rows) {
        for (j in 0 until cols) {
            matrix[i][j] = Random.nextInt(1, 10)
        }
    }

    return matrix
}

// The multiplyMatrices and printMatrix functions remain the same as Example 1

2.1 Explanation

This example generates random matrices of specified dimensions and then performs matrix multiplication using the multiplyMatrices function. The generateRandomMatrix function creates random matrices, simulating real-world data scenarios.

2.2 Output

The output of Example 2 will vary each time due to random matrix generation but will follow the format of the multiplied matrix.

3. User-Input Matrices Example

Kotlin
fun main() {
    val matrix1 = inputMatrix("Matrix 1")
    val matrix2 = inputMatrix("Matrix 2")

    val result = multiplyMatrices(matrix1, matrix2)
    printMatrix(result)
}

fun inputMatrix(name: String): Array<IntArray> {
    println("Enter the number of rows for $name:")
    val rows = readLine()!!.toInt()

    println("Enter the number of columns for $name:")
    val cols = readLine()!!.toInt()

    println("Enter elements for $name:")
    val matrix = Array(rows) { IntArray(cols) }

    for (i in 0 until rows) {
        for (j in 0 until cols) {
            matrix[i][j] = readLine()!!.toInt()
        }
    }

    return matrix
}

// The multiplyMatrices and printMatrix functions remain the same as Example 1

3.1 Explanation

This example allows users to input their own matrices for multiplication. The inputMatrix function prompts users to enter matrix elements, and then the multiplication is performed using the multiplyMatrices function.

3.2 Output

The output of Example 3 will depend on the user’s input matrices and their dimensions.

These three examples demonstrate different approaches to matrix multiplication in Kotlin using multi-dimensional arrays. Example 1 uses predefined matrices, Example 2 generates random matrices, and Example 3 takes user input for matrices. Each example highlights Kotlin’s flexibility in handling matrix operations efficiently.