Skip to main content

How to Implement Matrix Multiplication in Batch Script

In mathematics and data science, Matrix Multiplication is a fundamental operation used for transforming coordinates, processing signals, and solving systems of linear equations. While Batch is not a mathematical computing language like Python or MATLAB, its ability to handle 2D arrays and nested loops allows for the implementation of this algorithm for small-scale datasets (e.g., 2x2 or 3x3 matrices).

In this guide, we will demonstrate how to multiply two 2D arrays in Batch.

The Strategy: Sum of Products

To multiply Matrix A by Matrix B:

  1. Verify that the number of columns in A equals the number of rows in B.
  2. The resulting Matrix C will have the rows of A and the columns of B.
  3. Each cell C[R][C] is the sum of A[R][k] * B[k][C] for all k.
note

For a matrix multiplication to be valid, the number of columns in Matrix A must equal the number of rows in Matrix B. The resulting Matrix C will have dimensions equal to the rows of A by the columns of B.

Implementation Script: 2x2 Matrix Multiplication

@echo off
setlocal enabledelayedexpansion

:: 1. Define dimensions
set "rowsA=2" & set "colsA=2"
set "rowsB=2" & set "colsB=2"

:: 2. Define Matrix A (2x2)
:: | 1 2 |
:: | 3 4 |
set "A_1_1=1" & set "A_1_2=2"
set "A_2_1=3" & set "A_2_2=4"

:: 3. Define Matrix B (2x2)
:: | 5 6 |
:: | 7 8 |
set "B_1_1=5" & set "B_1_2=6"
set "B_2_1=7" & set "B_2_2=8"

:: 4. Validate dimensions
if !colsA! NEQ !rowsB! (
echo [ERROR] Cannot multiply: Columns of A (!colsA!^) must equal Rows of B (!rowsB!^).
pause
exit /b 1
)

echo Matrix A:
call :printMatrix A %rowsA% %colsA%
echo.
echo Matrix B:
call :printMatrix B %rowsB% %colsB%
echo.
echo Multiplying A x B...

:: 5. Matrix Multiplication - Triple nested loop
for /L %%i in (1,1,%rowsA%) do (
for /L %%j in (1,1,%colsB%) do (
set "sum=0"
:: Inner loop for the "Dot Product" logic
for /L %%k in (1,1,%colsA%) do (
set /a "sum+=A_%%i_%%k * B_%%k_%%j"
)
set "C_%%i_%%j=!sum!"
)
)

:: 6. Display Resulting Matrix C
echo.
echo Result Matrix C:
call :printMatrix C %rowsA% %colsB%

endlocal
pause
exit /b 0

:: ============================================
:: PRINT MATRIX SUBROUTINE
:: ============================================
:printMatrix
:: Usage: call :printMatrix [prefix] [rows] [cols]
set "pfx=%~1"
set "r=%~2"
set "cc=%~3"
for /L %%i in (1,1,%r%) do (
set "rowStr="
for /L %%j in (1,1,%cc%) do (
call set "cellVal=%%%pfx%_%%i_%%j%%"
set "rowStr=!rowStr! !cellVal!"
)
echo !rowStr!
)
exit /b

Why Perform Matrix Multiplication in Batch?

  1. Coordinate Transformations: If you are building a text-based game or any graphics-related script, matrix multiplication is used to rotate or scale points.
  2. Logic Weights: In a decision-making script, you can use a matrix to represent weights for different criteria (Cost, Speed, Value), allowing the script to calculate a final "Score" for several options simultaneously.
  3. Algorithmic Demonstration: It is an excellent way to prove the power of Batch's FOR /L loops and variable namespacing.

Important Limitations

warning

Batch arithmetic uses signed 32-bit integers (maximum ±2,147,483,647). During multiplication, intermediate products can easily exceed this limit even when the final sum would be small. Always ensure your input values are small enough that no single A * B product overflows.

  1. 32-Bit Math: Batch results cannot exceed ~2.1 billion. Mid-calculation multiplications can easily hit this ceiling if you are using large numbers.
  2. No Decimals: All inputs and results must be whole numbers (integers).
  3. Performance: Because Batch processes three nested loops, multiplying even a 10×10 matrix can take several seconds. For high-performance math, use a PowerShell bridge.

Best Practices

  1. Clear Naming: Use clear naming like A_Row_Col so you don't confuse the row and column pointers during the dot product loop.
  2. Initialization: Always initialize your sum variable to 0 at the start of the %%j loop to prevent values from previous calculations leaking into your current cell.
tip

To scale this script to larger matrices (e.g., 3×3 or 4×4), simply change the dimension variables at the top. All loops and the print subroutine reference these variables, so no other code changes are needed.

Conclusion

Implementing matrix multiplication is a testament to the flexibility of the Batch environment. While it is not optimized for scientific computing, the ability to perform sum-of-products calculations over a grid of variables allows you to handle structured data in highly sophisticated ways. This knowledge provides the mathematical logic base needed for complex simulations, weighted decision systems, and spatial data transformations within any Windows command-line tool.