Matrix multiplication gives different result than manual dot products with each column

1 回表示 (過去 30 日間)
N = 1000;
B = 10;
C = 2;
x = randn(N,B);
b = randn(B,C);
matMult = x * b;
dotProd = [x*b(:,1), x*b(:,2)];
isequal(matMult, dotProd) %==0
mean(matMult(:) - dotProd(:)) % approaches 0
Does anyone know why these two expressions don't give the same answer?
  2 件のコメント

サインインしてコメントする。

採用された回答

Bruno Luong
Bruno Luong 2023 年 3 月 2 日
"Does anyone know why these two expressions don't give the same answer? "
Why should they? The algorithm can perform diffrent sum orders, different accumulation scheme, different thread numbers. One should not expect they to be exactly equal.
  2 件のコメント
Andrew
Andrew 2023 年 3 月 2 日
From a person with thorough knowledge of linear algebra but minimal computer science, one would expect them to be identical because the two expressions (matMult & dotProd) request the same mathematical operation. Your list of algorithmic differences is what I was looking for to understand the computer science behind the differences.
Bruno Luong
Bruno Luong 2023 年 3 月 2 日
編集済み: Bruno Luong 2023 年 3 月 2 日
When one works with numerical calculation on computer, one should be awared about the imperfect of finite precision aritmetics (round off error, non associativity, non commutivity, ...).
The "*" MATLAB operation is performed by Blas and Lapack library and can be differently implemented by platform and version. They are not documented by TMW because they reserved the freedom to change it (and they did serveral times in the pass).
The proof is that your code returns identical result on Linux machine R2022b (see @KSSV answer), but not on Windows (for example my laptop or your computer). It can also change with HW such as the number of cores of the CPU.
Bottomline is that use should not numerical calculation of the same mathematical expression returns the exact same answer if they call different function calls to achieve the same expression.

サインインしてコメントする。

その他の回答 (1 件)

KSSV
KSSV 2023 年 3 月 2 日
As you are comparing floatting point number, you should not use isequal. You should proceed like shown below:
N = 1000;
B = 10;
C = 2;
x = randn(N,B);
b = randn(B,C);
matMult = x * b;
dotProd = [x*b(:,1), x*b(:,2)];
tol = 10^-5 ; % can be changed
all(abs(matMult-dotProd)<tol,'all')
ans = logical
1

カテゴリ

Help Center および File ExchangeWord games についてさらに検索

製品


リリース

R2022b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by