Dominant arguments and gpuArray

1 次查看(过去 30 天)
"The dominant argument in a method's argument list determines which version of the method or function that the MATLAB runtime calls. Dominance is determined by the relative precedences of the classes of the arguments. In general, user-defined classes take precedence over built-in MATLAB classes"
However, I'm finding that gpuArrays take precedence over user-defined classes in the example below
classdef myclass
methods
function mldivide(x,y)
disp ' '
disp 'myclass mldivide method called'
disp ' '
end
end
end
>> which mldivide( gpuArray(1) , myclass)
mldivide is a built-in method % gpuArray method
Why does gpuArray, which is a built-in class, take precedence over my user-supplied class, contrary to the documentation?

采纳的回答

Jennifer Black
Jennifer Black 2018-9-10
MATLAB uses dominant argument dispatching to determine which version of a method to call. During method dispatching, MATLAB determines the dominant class from among the arguments in the call. In general, all MATLAB classes defined using the classdef syntax have equal precedence for purposes of method dispatching. It does not matter whether the class is authored by MathWorks or is a third-party class. The InferiorClasses class attribute does allow for some customization of this behavior, but in most cases, when a method is invoked and two or more objects are part of the argument list, the method defined for the class of the left-most object is invoked. That is why the which function is telling you that the mldivide method of gpuArray is invoked when called as mldivide(gpuArray(1), myclass).
If you use dot-based method invocation, then only the argument to the left of the dot is used for dispatching.
The section of the documentation cited above is using the phrase "built-in MATLAB classes" to refer to MATLAB's fundamental types. This includes double, single, signed and unsigned int8, int16, int32, and int64, logical, char, string, cell, struct, and function_handle. MATLAB classes defined using a classdef do take precedence over those fundamental types. We are reviewing that section of the documentation to make it clearer.

更多回答(1 个)

Alexander Jensen
Alexander Jensen 2018-9-7
编辑:Alexander Jensen 2018-9-7
I believe that what ever computation run within the class will choose the function you've defined in your class, however, calling the function outside of your class requires you to write:
myclass.mldivide(x,y)
Also:
which myclass.mldivide
However I might be wrong :)

类别

Help CenterFile Exchange 中查找有关 Construct and Work with Object Arrays 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by