Ideas on generalizing the syntax used for constructing complex variables
3 visualizzazioni (ultimi 30 giorni)
Mostra commenti meno recenti
Russell Carpenter
il 21 Gen 2025
Commentato: Russell Carpenter
il 22 Gen 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 Commenti
Steven Lord
il 22 Gen 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.]
Risposta accettata
Walter Roberson
il 21 Gen 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 Commenti
Walter Roberson
il 22 Gen 2025
details = regexp('3dogs + 2cats', '(?<sign>[+-]*)\s*(?<count>\d+)(?<entity>\w+)\s*', 'names')
details(1)
details(2)
Più risposte (1)
埃博拉酱
il 22 Gen 2025
Modificato: 埃博拉酱
il 22 Gen 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)数组(不具有属性)。
Vedere anche
Categorie
Scopri di più su Systems of Nonlinear Equations in Help Center e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!