Ideas on generalizing the syntax used for constructing complex variables

42 ビュー (過去 30 日間)
Russell Carpenter
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
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
y = 3.1416
z = pii % not pi*1i
Unrecognized function or variable 'pii'.
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.]
Russell Carpenter
Russell Carpenter 2025 年 1 月 22 日 15:36
It is a reasonable question. With imaginary number symbols i and j, this syntax only works if one types a number in front of the symbol. That is what I had in mind here. (And this only works, at least currently in Matlab, for i and j, not things like pi, i.e. 4pi returns an error with a suggestion "Did you mean 4*pi?").

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

採用された回答

Walter Roberson
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
Walter Roberson 2025 年 1 月 22 日 18:02
details = regexp('3dogs + 2cats', '(?<sign>[+-]*)\s*(?<count>\d+)(?<entity>\w+)\s*', 'names')
details = 1x2 struct array with fields:
sign count entity
details(1)
ans = struct with fields:
sign: '' count: '3' entity: 'dogs'
details(2)
ans = struct with fields:
sign: '+' count: '2' entity: 'cats'
Russell Carpenter
Russell Carpenter 2025 年 1 月 22 日 19:50
Nice! I figured there was a regex way to do something like I have been doing with sscanf. This is great!

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

その他の回答 (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)数组(不具有属性)
  1 件のコメント
Russell Carpenter
Russell Carpenter 2025 年 1 月 22 日 12:15
Thanks for providing an MWE that at least produces something (a hetereogeneous array of [Dog Dog Cat Cat Cat]). But it doesn't really generalize complex numbers. I am after something where for example Dog would be analagous to the real line, and Cat would be to the imaginary line, and the Pet plane would be to the complex plane. The goal would be to allow other pets too so that this generalizes to hyperplanes. There are lots of examples of things like this, such as various contributions on Matlab Central that provide hypercomplex and/or quaternion classes. I just think it would be nice if we could create such objects in a way that is consistent with how Matlab allows us to create complex numbers. Doing the plus and mtimes methods a bit differently from your MWE would get close to this, so thanks again for the contribution.

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

カテゴリ

Help Center および File ExchangeMatrices and Arrays についてさらに検索

製品


リリース

R2024b

Community Treasure Hunt

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

Start Hunting!

Translated by