gpt4 book ai didi

MATLAB:是否可以在 native 构造(单元、结构等)上重载运算符?

转载 作者:太空宇宙 更新时间:2023-11-03 19:08:38 25 4
gpt4 key购买 nike

我正在使用单元格来管理我正在处理的一些内容中的数据。我希望能够执行以下操作:

A = cellfun( @(X)( randn( 5,5 ) ), cell( 5,1 ), 'UniformOutput', 0 );
B = cellfun( @(X)( randn( 5,5 ) ), cell( 5,1 ), 'UniformOutput', 0 );
%#
%# Each of the following would fail if cell member dimensions
%# don't match up
%#
%# matrix sums for each cell entry
%# C = A + B;
C = cellfun( @(X,Y)( X + Y ), A, B, 'UniformOutput', 0 );
%#
%# direct/hadamard product
%# D = A .* B;
D = cellfun( @(X,Y)( X .* Y ), A, B, 'UniformOutput', 0 );
%#
%# matrix-matrix products (not across all entries)
%# E = A * B;
E = cellfun( @(X,Y)( X * Y ), A, B, 'UniformOutput', 0 );

但是,我不想使用极其冗长的语法来完成它。当我只想为单元格上的数学运算符提供定义时,为此创建一个新类似乎有点过分。

问题:上课是解决问题的唯一方法吗?

如果我编写一个类来执行此操作,那么编写代码肯定会更容易。我看到的最大的负面影响与优化有关,尽管还有一些其他的事情让我感到烦恼......

任何在幕后进行的优化(例如,当 Jacket 编译某些东西以在 GPU 上运行时)可能会更难确定要进行哪些优化。例如,假设我有两个单元格 (A,B),其中包含许多适当维数的矩阵。如果我编写代码来生成一个新单元格:

Z = c1*A + c2*B

...使用标量 {c1,c2},我可以用 Jacket(或其他任何东西)很容易确定它应该按照以下方式进行计算的方式来编写它:

Z{kk} = c1*A{kk} + c2*B{kk}

或者可能是比这更好的优化。否则。它最终可能会变得更慢和/或内存效率更低,例如:

temp1 = cellfun( @(X)( c1*X ), A );
temp2 = cellfun( @(X)( c2*X ), B );
Z = cellfun( @plus, temp1, temp2 );

假设 MATLAB 或 Jacket 无法对其进行优化,这最终会使用过多的内存。

最佳答案

事实上,可以为 MATLAB 中的内置数据类型创建新的运算符或重载现有的运算符。我在 my answer 中描述了一个例子另一个关于 modifying the default overflow behavior of integer types 的问题.

首先,您可能想要查看元胞数组目前存在哪些方法。您可以使用函数 METHODS 执行此操作,这是我在 MATLAB R2010b 中得到的:

>> methods cell

Methods for class cell:

aa2nt issorted regexptranslate strfind
accumarray newdepfun reshape strjust
cell2struct nt2aa rna2dna strmatch
ctranspose nwalign seq2regexp strtok
display permute setdiff transpose
dna2rna regexp setxor union
intersect regexpi sort unique
ismember regexprep strcat

算术运算符方法将在上面的列表中显示为它们的 function equivalents ,例如 + 运算符的 plus.* 运算符的 times。仅为元胞数组定义了 transpose 方法(.' 运算符)。您必须自己创建其余部分,定义给定运算符对元胞数组参数的行为方式。

您可以先创建一个名为@cell 的新文件夹,然后将其放在您的MATLAB path 上的现有文件夹中。 .然后将新方法放在 @cell 文件夹中。例如,元胞数组的 plus 方法的非常简单实现(没有任何输入检查、错误检查等)将是这样的:

function C = plus(A,B)
C = cellfun(@plus,A,B,'UniformOutput',false); %# Apply plus cell-wise
end

在上面的代码中,您可能首先要检查操作数 AB 是否是大小相同的元胞数组。但是,您可以创建任何您想要的独特功能,例如允许 B 成为一个标量值,该值将添加到 A 的每个单元格中。完全由您来定义 + 运算符对元胞数组的行为方式。

这将允许您以更紧凑的方式编写代码,如本例所示:

>> A = {[1 2 3] [4 5] 6};  %# One 3-element cell array
>> B = {5 [4 5] 2}; %# Another 3-element cell array
>> C = A+B; %# Use the new plus operator
>> C{:} %# Display the cell contents

ans =

6 7 8

ans =

8 10

ans =

8

我真的无法谈论幕后优化以及这可能会如何影响它们。我知道文档 "Techniques for Improving Performance"特别提到关于 overloading built-in functions :

Overloading MATLAB built-in functions on any of the standard MATLAB data classes can negatively affect performance. For example, if you overload the plus function to handle any of the integer classes differently, you may hinder certain optimizations in the MATLAB built-in function code for plus, and thus may slow down any programs that make use of this overload.

但是,在您的情况下,您没有为类重载现有 函数。您只是在创建该类不存在的新类,因此很难说这最终会对性能产生什么影响。

关于MATLAB:是否可以在 native 构造(单元、结构等)上重载运算符?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5365464/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com