

Matrix Multiplication Calculated with T-SQL
source link: https://www.tuicool.com/articles/hit/RbuYFzu
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

By:Eli Leiba | Last Updated: 2018-11-23 | | Related Tips:More > T-SQL
Problem
I need to perform matrix multiplication in T-SQL for one of my applications. Is this possible with T-SQL code in SQL Server?
Solution
The term "matrix multiplication" or sometimes called "matrix product" is a binary operation (an operation between two matrices) that produces a matrix from two matrices in the following manner:
Here is the definition (from the Wikipedia): https://en.wikipedia.org/wiki/Matrix_multiplication .
Matrix Multiplication with T-SQL
The stored procedure in this tip is a suggested T-SQL solution to this operation and does the mathematical operations by using the T-SQL equivalent methods. My solution involves creating a rather simple T-SQL stored procedure in a SQL Server application database, called dbo.multMatrixes that will get the following two parameters of type matrixType: @matA and @matB. These are two tables valued parameters (TVP) that represent the two matrices in T-SQL.
Create New Type
We need to create a new type in SQL Server and the matrix type is defined as follows:
CREATE TYPE matrixType AS TABLE (i INT,j INT, val REAL) GO
This type represents a matrix where V [i,j] equals the value of the cell in row 'i' and column 'j'.
i = 1,2,3,...n and j = 1,2,3,…m for the first matrix and 1,2,3,…m and 1,2,3,...p for the second matrix respectively.
The procedure does a join on these 2 tables matching j (matrix A) to i (matrix B) grouped by i (in matrix A) and j (in matrix B) and sums the product of the values giving the multiplied matrix as the result set.
SQL Server Stored Procedure to Calculate Matrix Multiplication
-- ================================================================================================= -- Author: Eli Leiba -- Create date: 11-2018 -- Procedure Name: dbo.multMatrixes -- Description: -- The procedure gets 2 parameters of type matrixType: @matA and @matB -- The procedure multiplies the two matrixes by doing the mathematical operation. -- It joins the tables matching j (matrix A) to i (matrix B), grouped by i (matrix A) and j (matrix B) and -- sums the product of the values. -- ================================================================================================= CREATE PROCEDURE dbo.multMatrixes @matA matrixType READONLY, @matB matrixType READONLY AS SELECT A.I, B.J, SUM (A.val * B.val) AS val FROM @matA A,@matB B WHERE A.J = B.I GROUP BY A.I, B.J; GO
Example Execution
Let's say we have two matrixes as follows:
Here is how the final matrix is calculated.
For each row in Matrix A we multiply this against each column in Matrix B. The values shaded in gray are the final values for the matrix which we will see below.
This is what Matrix C would look like using the numbers from above.
Below is the T-SQL script for populating the matrices and completing the multiplication. For the values, the first position is the row, the second is the column and the third is the value.
- For Matrix A, for row 1, column 1 the value is -1, this would be represented as (1, 1, -1)
- For Matrix A, for row 1, column 2 the value is -0, this would be represented as (1, 2, 0)
- etc...
DECLARE @matrixA matrixType; DECLARE @matrixB matrixType; -- filling first matrix INSERT INTO @matrixA VALUES (1, 1, -1), (1, 2, 0), (1, 3, 6), (2, 1, 2), (2, 2, 5), (2, 3, 1); -- filling second matrix INSERT INTO @matrixB VALUES (1, 1, 2), (1, 2, -4) ,(1, 3, 9), (2, 1, 0), (2, 2, 5) ,(2, 3, 1), (3, 1, 3), (3, 2, 6) ,(3, 3, -1); -- now doing the matrixes multiplication EXEC dbo.multMatrixes @matrixA, @matrixB
This returns 6 rows as follows. Column I is the row and column J is the column and val is the computed value.
If we pivot the data back to the matrix, we would have the following:
Next Steps
- You can create and compile this simple procedure (and type) in your application database and use it as a simple T-SQL tool for multiplying matrices.
- The procedure should work for SQL Server version 2008 and above (2012, 2014, 2016 and 2017) since table valued parameters for stored procedure had been introduced in SQL Server 2008.
Last Updated: 2018-11-23
About the author
Eli Leiba is a senior application DBA, a teacher and a senior database consultant with 19 years of RDBMS experience.
Related Resources
Recommend
-
58
Then we write 3 loops to multiply the matrices element wise. The shape of the final matrix will be (number of rows matrix_1) by (number of columns of matrix_2). Now let’s create a basic neural net where we wil...
-
8
Bra-Ket Notation Trivializes Matrix Multiplication 27 Nov 2016 One of the first things you notice, when learning quantum things, is people surrounding all their symbols with a strange angular notation. I...
-
14
Dynamic programming deep-diveDynamic programming deep-dive: Chain Matrix Multiplication Apr 25, 2019 • Avik Das
-
8
Matrix Multiplication Inches Closer to Mythic GoalRead LaterShareCopied!...
-
9
Limits On Matrix Multiplication August 30, 2018 Can 2.3728639 be best? Josh Alman is a graduate student at a technical school in the Boston area. He is working on...
-
3
MIT Researchers Open-Source Approximate Matrix Multiplication Algorithm MADDNESS Oct 05, 2021...
-
15
Java Program to Perform Matrix Multiplication In the current post, I have written a java program to perform a simple matrix multiplication. For matrix multiplication the column of the first matrix should be equal to the...
-
9
CUDA 8.0, GTX 1080, why is vector addition slower than 5x matrix multiplication? advertisements I am using latest CUDA 8.0...
-
4
In this article, we will learn matrix-vector multiplication using NumPy. Table Of Contents What is a matrix in numpy and how to create it? The numpy stand...
-
17
What Is Fast Matrix Multiplication? The definition of matrix multiplication says that for matrices...
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK