PriyoScriptPriyoScript
Advanced

Advanced Concepts

End-to-end matrix multiplication example combining loops, arrays, functions, and control flow.

1. Concept Overview

Matrix multiplication is one of the best exercises for mastering core programming logic.

It requires understanding how multiple programming constructs work together:

  • Arrays (2D data structures)
  • Nested loops
  • Index-based access
  • Accumulating values
  • Function decomposition
  • Input and output handling

Because of this, matrix multiplication appears in many real-world systems such as:

  • Graphics engines
  • Physics simulations
  • Machine learning models
  • Data processing pipelines
  • Scientific computing

Learning to implement matrix multiplication helps build algorithmic thinking, which is essential for solving more complex programming problems.


2. Mathematical Idea

Two matrices can be multiplied only if:

Columns of Matrix A = Rows of Matrix B

Example:

A (2 x 2)      B (2 x 2)

[1 2]          [5 6]
[3 4]          [7 8]

Result matrix:

C = A x B

[ (1x5 + 2x7)   (1x6 + 2x8) ]
[ (3x5 + 4x7)   (3x6 + 4x8) ]

Output:

[19 22]
[43 50]

This operation is called a dot product between rows and columns.


3. Program Examples

Example 1: Matrix Multiplication (2x2) with User Input

monalisa {
  lisaaTask readMatrix2x2(label) {
    priyoTell.Info("Enter values for " + label)
    priyoKeep m = [[0, 0], [0, 0]]

    prakritiCount (priyoChange i = 0; i < 2; i = i + 1) {
      prakritiCount (priyoChange j = 0; j < 2; j = j + 1) {
        m[i][j] = priyoListenNumber("Value at [" + i + "][" + j + "]: ")
      }
    }

    priyoGiveBack m
  }

  lisaaTask multiply2x2(a, b) {
    priyoKeep result = [[0, 0], [0, 0]]

    prakritiCount (priyoChange i = 0; i < 2; i = i + 1) {
      prakritiCount (priyoChange j = 0; j < 2; j = j + 1) {
        priyoKeep sum = 0
        prakritiCount (priyoChange k = 0; k < 2; k = k + 1) {
          sum = sum + a[i][k] * b[k][j]
        }
        result[i][j] = sum
      }
    }

    priyoGiveBack result
  }

  priyoKeep m1 = readMatrix2x2("Matrix A")
  priyoKeep m2 = readMatrix2x2("Matrix B")
  priyoKeep out = multiply2x2(m1, m2)

  priyoTell.Success("Result matrix:")
  priyoTell(out)
}

Output:

Enter values for Matrix A
Enter values for Matrix B
Result matrix:
[[19, 22], [43, 50]]

4. Printing Matrix Row by Row

Sometimes we want to print matrices in a cleaner way.

monalisa {
  priyoKeep matrix = [[19, 22], [43, 50]]

  prakritiCount (row priyoInside matrix) {
    priyoTell(row)
  }
}

Output:

[19, 22]
[43, 50]

This loop iterates over each row in the matrix.


5. Validating Matrix Dimensions

Before multiplying matrices, we must check if the operation is valid.

monalisa {
  lisaaTask canMultiply(colsA, rowsB) {
    priyoGiveBack colsA == rowsB
  }

  priyoTell(canMultiply(2, 2))
  priyoTell(canMultiply(3, 2))
}

Output:

priyoTrue
priyoFalse

This simple validation prevents invalid mathematical operations.


6. Summing All Elements in a Matrix

We can also process matrices after computation.

monalisa {
  priyoKeep matrix = [[19, 22], [43, 50]]
  priyoKeep total = 0

  prakritiCount (row priyoInside matrix) {
    prakritiCount (val priyoInside row) {
      total = total + val
    }
  }

  priyoTell(total)
}

Output:

134

This demonstrates nested iteration over 2D structures.


7. Final Practical Pattern (Compute + Print)

A common workflow:

  1. Compute a matrix
  2. Store the result
  3. Print each row clearly
monalisa {
  priyoKeep out = [[19, 22], [43, 50]]

  priyoTell("Final matrix:")
  prakritiCount (row priyoInside out) {
    priyoTell(row)
  }
}

Output:

Final matrix:
[19, 22]
[43, 50]

8. Execution Flow

When the program runs, this sequence happens:

  1. The user enters values for two matrices.
  2. The program calls the multiplication function.
  3. The outer loop selects the row index i.
  4. The middle loop selects the column index j.
  5. The inner loop multiplies and accumulates values using index k.
  6. The calculated sum becomes result[i][j].
  7. After all loops finish, the final matrix is printed.

9. Algorithm Representation

Matrix multiplication algorithm:

for each row i in A
  for each column j in B
    sum = 0
    for each k
      sum += A[i][k] * B[k][j]
    result[i][j] = sum

This is the classic triple-loop matrix algorithm.


10. Time Complexity

For an n x n matrix:

Time Complexity = O(n^3)

Reason:

  • First loop -> rows
  • Second loop -> columns
  • Third loop -> dot product computation

Total operations grow roughly with n^3.


11. Beginner Mental Model

Think of matrix multiplication like combining ingredients in a recipe table.

  • Matrix A = ingredient quantities
  • Matrix B = recipe combinations
  • Each output cell mixes matching row and column ingredients

Steps:

  1. Pick a row from matrix A
  2. Pick a column from matrix B
  3. Multiply matching values
  4. Add them together
  5. Store the result in the output matrix

Why this example matters

Matrix multiplication is one of the first programs where multiple programming ideas combine into a complete algorithm.

By understanding this example, you learn how to combine:

  • Functions
  • Nested loops
  • Arrays
  • Data validation
  • Structured output

This is the foundation for building real computational systems.


12. What You Learned

After completing this page, you now understand:

  • How to represent matrices using arrays
  • How nested loops operate in algorithms
  • How to perform dot product calculations
  • How to validate matrix dimensions
  • How to structure programs using reusable functions

These concepts are essential for data science, graphics programming, and algorithm design.

Last updated on

On this page