フィルターのクリア

mtimes and transpose are being treated as a single function when used together?

1 回表示 (過去 30 日間)
Koby Goldberg
Koby Goldberg 2018 年 10 月 4 日
コメント済み: Koby Goldberg 2018 年 10 月 7 日
I'm trying to overload the mtimes function so each * operator will perform my own matrix multiplication (using mex) instead of matlab's built-in function.
I managed to overload the mtimes function when writing it in the same m-file with the calling function, and when I perform A*B I see that my function is being called (using debug message running from my mtimes function). So far so good. But when I perform A*B.' my mtimes is NOT called!. Neither when I perform A.'*B or A.'*B.'.
See the example code below, and the debug messages I see in Matlab console when I run it.
function testme
A = randn(3,3);
B = randn(3,3);
disp('start');
C1 = A*B;
C2 = A*B.';
C3 = A.'*B;
C4 = A.'*B.';
disp('end');
end
function out = mtimes(a,b)
out = a + b; % here should come my mex function. using '+' to let others copy-paste and check it for themselves without the mex being missing.
disp('been in my-mtimes');
end
output on console:
>> testme
start
been in my-mtimes
end
It looks like A*B.' is calling different functions than 'mtimes' following 'transpose'... Any idea which function is called on A*B.' or A.'*B? BTW wrapping B.' in brackets (B.') doesn't change the behavior. Neither using A*transpose(B)... None of them are entering my mtimes function... I guess this is some kind of optimization that matlab does to save the transpose MIPS, but there must be a function that does that, which I can't find... Any help would be appreciated!
Thanks, Koby
  9 件のコメント
James Tursa
James Tursa 2018 年 10 月 6 日
編集済み: James Tursa 2018 年 10 月 6 日
It all depends on what syntaxes the parser recognizes in order to call the combined BLAS function. I have never seen any documentation on this, nor have I done any testing to see how this might have changed from release to release.
Koby Goldberg
Koby Goldberg 2018 年 10 月 7 日
Thanks Matt, nice trick with adding 0 ! I verified, it indeed works :)
Thanks!

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

採用された回答

Matt J
Matt J 2018 年 10 月 4 日
編集済み: Matt J 2018 年 10 月 4 日
To circumvent this, my recommendation would be to overload mtimes in a subclass of type double.
classdef myclass < double
methods
function obj=myclass(data)
obj=obj@double(data);
end
function out = mtimes(a,b)
out = myclass( a + b );
disp('been in my-mtimes');
end
end
end
  5 件のコメント
Koby Goldberg
Koby Goldberg 2018 年 10 月 4 日
Thanks guys for all the info and good suggestions. So, bottom line, I guess it is impossible to override the *' pair, since this optimization is performed by the interpreter under the hood. Anyway, you've given me good workaround ideas!
Thanks, Koby
Matt J
Matt J 2018 年 10 月 4 日
編集済み: Matt J 2018 年 10 月 4 日
You're welcome, but please Accept-click the answer if you consider the matter addressed.

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeNumber Theory についてさらに検索

製品


リリース

R2016a

Community Treasure Hunt

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

Start Hunting!

Translated by