Ideas on generalizing the syntax used for constructing complex variables
42 ビュー (過去 30 日間)
古いコメントを表示
Russell Carpenter
2025 年 1 月 21 日 21:07
コメント済み: Russell Carpenter
2025 年 1 月 22 日 19:50
I am looking for ideas on how to construct a class that generalizes the interface Matlab uses for creating and manipulating complex variables. The general idea is that one could construct a "pets" object by simplying typing:
mypets = 2dogs + 3cats
which would create an object of the pets class.
I can think of a few clunky ways to do this, such as having the constructor parse a string like this:
mypets = pets('2dogs + 3cats').
Another slightly nicer way would be to leave out the parenthesis like this:
pets 2dogs + 3cats
but this still requires an explicit constructor reference. Any ideas anyone?
2 件のコメント
Steven Lord
2025 年 1 月 22 日 14:19
Would you expect the following code to give you an object representing 2 dogs and 3 cats as well? [Leaving this code commented out so I can run code later in the comment, as it would error.]
%{
x = 2;
y = 3;
mypets = xdogs + ycats
%}
If so, how would you expect MATLAB to behave if you had four functions or classes with names dogs, cats, xdogs, and ycats? Should it use your approach and be equivalent to 2dogs+3cats, or should it call the functions whose names are an exact match and ignore this proposed new syntax entirely?
For the current complex number creation syntax in this scenario, MATLAB will call the function whose name is an exact match if there is one and error if there isn't.
p = 4;
y = pi % not 4i
z = pii % not pi*1i
I suspect overloading multiplication and addition is about the best you're going to be able to do. [Don't forget, too, that both 2*dogs and dogs*2 would call the overloaded multiplication method. You should probably account for both cases.]
採用された回答
Walter Roberson
2025 年 1 月 21 日 23:24
There is no hope of extending MATLAB such that 2dogs + 3cats is recognized.
You are going to need an explicit constuctor -- whether that be
pets('2dogs + 3cats')
or
pets 2dogs + 3cats
or
pets(2, 'dogs') + pets(3, 'cats')
or
dogs(2) + cats(3)
4 件のコメント
Walter Roberson
2025 年 1 月 22 日 18:02
details = regexp('3dogs + 2cats', '(?<sign>[+-]*)\s*(?<count>\d+)(?<entity>\w+)\s*', 'names')
details(1)
details(2)
その他の回答 (1 件)
埃博拉酱
2025 年 1 月 22 日 1:51
編集済み: 埃博拉酱
2025 年 1 月 22 日 1:59
The imaginary units i and j are keywords hard-coded into the MATLAB interpreter. You can't add new custom keywords. Conceptually, the special way of writing complex denominations is simply to omit the multiplication sign. For your custom type, at least you can't omit the multiplication sign, so you can overload multiplication operators by defining times and mtimes function in your dog/cat/pet class.
classdef Pet<matlab.mixin.Heterogeneous
methods
function obj=plus(A,B)
obj=[A,B];
end
end
end
classdef Cat<Pet
methods
function obj=mtimes(Repeat,obj)
obj=repmat(obj,1,Repeat);
end
end
end
classdef Dog<Pet
methods
function obj=mtimes(Repeat,obj)
obj=repmat(obj,1,Repeat);
end
end
end
>> 2*Dog+3*Cat
ans =
1×5 异构 Pet (Dog, Cat)数组(不具有属性)。
参考
カテゴリ
Help Center および File Exchange で Matrices and Arrays についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!