Converting a function into an operator
9 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
I'm currently using a genetic programming program for finding a model.
The program accepts operators as equation symbols: symbols{1} = {'+','*','/'}. But I am looking to put more complex things in there such as sin, cos, log etc. but this doesnt work, I believe because those are functions and not operators (I'm very new when it comes to programming). Is there a way to convert these functions into operators?
I'm looking into building a class since that is something I read that can work however I'm very new to programming and classes seem a little abra kadabra to me. Could someone help me?
0 commentaires
Réponses (3)
Matt J
le 17 Mai 2021
Modifié(e) : Matt J
le 17 Mai 2021
A shortcut might be to use my MatrixObj class
which has done most of the work for you. To define your own * operation, for example, you would just have to do
>> A=MatrixObj; %An operator
>> A.Ops.mtimes=@(A,b) sin(b); %make multiplication equivalent to sin()
>> A*(pi/4)
ans =
0.7071
Rik
le 17 Mai 2021
You can also do the work yourself by implementing mtimes as a method (I mimiced the interface suggested by Matt which ignores the first input):
x=[]*ExampleClass(pi/4)
classdef ExampleClass
properties
data=[];
end
methods
function obj=ExampleClass(in)
obj.data=in;
end
function res=mtimes(~,obj)
res=sin(obj.data);
end
end
end
0 commentaires
Steven Lord
le 20 Mai 2021
I wonder if the author of the package (whatever includes gpols_evaluate) that you're using only recognizes a subset of operations in the format of the models on which it operates. You should probably ask them if their tool can operate on models that involve functions like the trig or exponential functions or if they only accept models that just use the basic arithmetic operators. If it can they ought to be able to tell you how to enter such models in a way that their code can recognize.
A little quick searching suggests that it might be able to accept models involving functions, but I'm not 100% certain. See this page.
Voir également
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!