[Top][All Lists]

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Order of Evaluation

From: fi
Subject: Order of Evaluation
Date: Mon, 19 Aug 2019 20:56:00 +0200
User-agent: Mutt/1.5.24 (2015-08-30)

Dear List,

consider the following functions (assumed free of side effects). The
functions themself do not matter. Here are some simple examples for

f = @(x) x .^ pi + log(x);   % ... some expression ... 
g = @(x) sin(x) * exp(-x/5); % ... another expression ... 
h = @(x) sqrt(x .^ cos(x));  % ... normally a costly (in time) expression ...

% and last:

y = @(x) f(h(x)) + g(h(x)); 

% here h(x) will be evaluated twice. This is inefficient, especially
% if y() is evaluated frequently (e.g. in finding roots or
% integrating). 
% Unfortunately Octave (like Matlab) does not have a sequence operator like C.
% So constructs like the following lead to syntax errors:

y = @(x) H = h(x), f(H) + g(H); % also wrong if in brackets

% The only idea I found was:

y = @(x) f(H = h(x)) + g(H); 

This works, but is this safe? Does Octave guarantee to evaluate
expressions like sums or products from left to right?  Will this hold
for the future? What if some day Octave will be parallelized and the
summands are caculated indepentently from each other? Is there any
way to calculate H definitively in advance inside y()?

Best Regards

Torsten Finke 

Torsten Finke

reply via email to

[Prev in Thread] Current Thread [Next in Thread]