Ideas on generalizing the syntax used for constructing complex variables
1 view (last 30 days)
Show older comments
Russell Carpenter
on 21 Jan 2025
Commented: Russell Carpenter
on 22 Jan 2025
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 Comments
Steven Lord
on 22 Jan 2025
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.]
Accepted Answer
Walter Roberson
on 21 Jan 2025
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 Comments
Walter Roberson
on 22 Jan 2025
details = regexp('3dogs + 2cats', '(?<sign>[+-]*)\s*(?<count>\d+)(?<entity>\w+)\s*', 'names')
details(1)
details(2)
More Answers (1)
埃博拉酱
on 22 Jan 2025
Edited: 埃博拉酱
on 22 Jan 2025
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)数组(不具有属性)。
See Also
Categories
Find more on Matrices and Arrays in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!