How to write one universal function to evaluate many equations?
Mostrar comentarios más antiguos
I am working on verifying whether if equations are valid over a large data set. The challenge is that the number of equations to be verified is huge even though they are simple arithmetic equations and identities. In order to avoid writing a different function for each equation, I try to represent an equation as a 'model' and write only one universal function to eval each model. Here is a toy example to explain what I intend to do. My questions are 1.) whether if my approach is generally in right direction 2.) is there any way to avoid eval( ) in function foo( )? Thanks for your insights!
x = 7;
y = 3;
modelA = [x, y; "+", "-"];
foo(modelA)
z = -21;
modelB = [x, y, z; "-", "*", "=="];
foo(modelB)
function out = foo(model)
data = string(model(1,:));
operators = model(2,:);
a = strcat(operators, data);
out = eval(strcat(a{:}));
end
15 comentarios
Walter Roberson
el 2 de Jul. de 2023
are the variables strictly integers?
Simon
el 2 de Jul. de 2023
Stephen23
el 2 de Jul. de 2023
"In order to avoid writing a different function for each equation"
Why?
"I could certainly write function for each accounting equation; as a matter of fact, I have done a couple of that. Then I found this task is too tedious."
I don‘t see that it makes any difference: if you can define those string arrays then you can just as easily define some anonymous functions e.g. using STR2FUNC.
"Othere disadvantages include 1.) function naming and tracking would be out of control"
The approach you show in your question also requires lots of individually-named functions. You could easily avoid that with a cell array of anonymous functions or function handles. Or just one function with suitable SWITCH or other logical operations to perform the calculations. Or some suitable nesting of local functions, called based on some logical criteria.
"2.) accounting equations and their purposes are hidden inside functions, which I feel uncomfortable about."
Those are benefits: Mfile functions allow you to neatly and efficiently encapsulate some functionality and provide a documented interface, as well as limitless help, comments, etc. Calling functions is also very efficient. Your string arrays provide none of those benefits.
John D'Errico
el 2 de Jul. de 2023
Editada: John D'Errico
el 2 de Jul. de 2023
You are going to have many more problems. That is, now you are talking about ratios, but then you are testing for exact equality. That is a bad idea, as you should never tyest for exact equality of floating point numbers. Those tests will unpredictably fail, and then you will be left scratching your head, eventually coming back here to understand the concept I just mentioned. NEVER TEST FOR EXACT EQUALITY BETWEEN FLOATING POINT NUMBERS.
I'd suggest you start rethinking what you need to do, and find a better way than using eval anyway. Eval is slow as hell. It will surely cause you problems down the line too.
For example, you might decide to reduce this to a set of generic expresssions. Now, write one function for each of those generic classes. Pass in your variables, get a result. In the case where a ratio is involved, then you will probably need to apply a tolerance on the result.
Stephen23
el 2 de Jul. de 2023
How many variables do these formulas refer to? If there are many, it might be easier to pass them in a scalar structure and let each function decide for itself which field’s it wants to use.
Simon
el 2 de Jul. de 2023
What's the advantage of entering everything as a string and then using str2func, as opposed to storing the functions directly in a cell array?
C = ["@(x, y, z) x-y==z"; "@(x, y) x/y"]
foo = str2func(C(1));
foo(3,2,1)
goo = str2func(C(2));
goo(4, 5)
clear
C = {@(x,y,z) x - y == z; @(x,y) x/y;}
C{1}(3,2,1)
C{2}(4,5)
Or, use a struct and then name the functions for clarity?
clear
C.foo = @(x,y,z) x - y == z;
C.goo = @(x,y) x/y;
C.foo(3,2,1)
C.goo(4,5)
Stephen23
el 2 de Jul. de 2023
"What's the advantage of entering everything as a string and then using str2func, as opposed to storing the functions directly in a cell array?"
Advantage: the "equations" can be dynamically defined/created, but still offer the efficiency of calling function handles.
If the "equations" do not need to be dynamically created then simple (anonymous) function handles would be better.
Comments can be stored using either the cell array or struct, if a cell array or struct with named fields happen to be the way to go.
C{1,1} = @(x,y,z) x - y == z;
C{1,2} = "this is the foo function";
C
% or
S.foo.eqn = @(x,y,z) x - y == z;
S.foo.comment = "this is the foo function";
S.foo
Respuestas (0)
Categorías
Más información sobre Variables en Centro de ayuda y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!