Converting a function into an operator

6 views (last 30 days)
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?

Answers (3)

Matt J
Matt J on 17 May 2021
Edited: Matt J on 17 May 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
  2 Comments
Max van den Heuvel
Max van den Heuvel on 20 May 2021
Thank you for your response! This seems like something that could help me.
However I want the * operator to stay the same, running my program gives (for example):
fitness: 0.972863, mse: 3.364136
-0.744263 * (x3) + 0.002868 * ((x1)*(x3)) + 0.004657 * ((x2)*(x3)) + 0.627342
And I want it to also be able to show sin, log etc. if it suits the model. How would this fit when the iputs for operator is the line:
>> symbols{1} = {'+','*','/','^'};
I tried adding the code you wrote and then writing symbols{1} = {'+','*','/','^','SIN*'}; but it gives me these errors:
Error using gpols_olsq (line 23)
Invalid expression. Check for missing multiplication operator, missing or unbalanced delimiters, or other syntax error. To construct matrices, use brackets instead of parentheses.
Error in gpols_evaluate (line 102)
[vfsdel,err] = gpols_olsq(fs,X,Y,olslimit);
Error in OWN_CODE (line 56)
popu = gpols_evaluate(popu,[1:popusize],X,Y,[],opt1(6:9));
Matt J
Matt J on 20 May 2021
Edited: Matt J on 20 May 2021
I don't really follow that, but it might be worth pointing out that if you have other operations that need to be done with the operator, they need to be defined as well, e.g.,
A.Ops.mrdivide=@(A,b) asin(b);

Sign in to comment.


Rik
Rik on 17 May 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)
x = 0.7071
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

Steven Lord
Steven Lord on 20 May 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.
  1 Comment
Max van den Heuvel
Max van den Heuvel on 20 May 2021
I am afraid this is the case indeed. Thank you for commenting and mentioning this, I have now send them an email regarding this!

Sign in to comment.

Products


Release

R2021a

Community Treasure Hunt

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

Start Hunting!