在 SQL Server 2008 中对两个表进行乘法
如果您有两个 SQL 表并且将它们视为矩阵,则两个矩阵 A 和 B 的乘积 C 定义为
c_ik = a_ij*b_jk
其中 j
对所有可能的 i
值求和和 k 以及上面的符号使用爱因斯坦求和约定。不存在显式求和符号的重复索引的隐含求和称为爱因斯坦求和,通常用于矩阵和张量分析。因此,为了定义矩阵乘法,矩阵的维数必须满足
所以
其中
此处描述 示例
a1 a2 a3
----------
14 9 4
2 11 5
0 12 17
5 2 3
b1 b2
-------
12 25
9 10
8 5
等于
c1 c2
---------
273 455
243 235
244 205
102 160
我正在考虑执行 While 循环,但我想有更好的方法来做到这一点 也许使用枢轴或坐标为 i、j、k 的 CTE?
DECLARE @count int
DECLARE @max int
SET @count int = 1
SET @max = 4
WHILE @count < @max
BEGIN
INSERT INTO C_Table (c1, c2)
--Do not know how to make the other products or how to get them...
Select Sum( (T1.Value * T2.Value) )
From A As T1
Join B As T2
...
SET @count = @count + 1
END
我在这里有点迷失......
If you have two SQL tables and you see them like matrices, The product C of two matrices A and B is defined as
c_ik = a_ij*b_jk
where j
is summed over for all possible values of i
and k
and the notation above uses the Einstein summation convention. The implied summation over repeated indices without the presence of an explicit sum sign is called Einstein summation, and is commonly used in both matrix and tensor analysis. Therefore, in order for matrix multiplication to be defined, the dimensions of the matrices must satisfy
so
where
for example
a1 a2 a3
----------
14 9 4
2 11 5
0 12 17
5 2 3
b1 b2
-------
12 25
9 10
8 5
is equals to
c1 c2
---------
273 455
243 235
244 205
102 160
I was thinking in doing a While loop, but I guess there is a better way to do this
maybe using pivot or a CTE with coords of i,j,k?
DECLARE @count int
DECLARE @max int
SET @count int = 1
SET @max = 4
WHILE @count < @max
BEGIN
INSERT INTO C_Table (c1, c2)
--Do not know how to make the other products or how to get them...
Select Sum( (T1.Value * T2.Value) )
From A As T1
Join B As T2
...
SET @count = @count + 1
END
I am a little lost here...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论