Are scalars precomputed when multiplied with or added to vectors?

Olaf Till olaf.till at uni-jena.de
Thu Mar 13 01:53:57 CDT 2008


On Wed, Mar 12, 2008 at 04:25:25PM +0100, David Bateman wrote:
> Olaf Till wrote:
> > If x and y are vectors and all other variables are scalars, e.g. in
> >
> > octave:1> y = sqrt (x.^2 * a^2 + sqrt ((a + b)^2 + c * d)) + c + d;
> >
> > will all scalar subexpressions
> >
> > (i.e.: a^2, sqrt ((a + b)^2 + c * d), c + d)
> >
> > be computed by Octave only once for the operations on all components
> > of x, or is it worth explicitly precomputing them?
> >
> >   
> Given the precedence of the above the answer is yes for a^2 and
> sqrt((a+b)^2+c*d). However for c + d the answer is less clear.. I
> believe that the binop will treat this as two scalar/matrix additions
> rather than a scalar/scalar addition and then a matrix/scalar addition..
> Put parenthesis around c+d to ensure what you want.
> 

Thanks, this spares a lot effort.

Olaf


More information about the Help-octave mailing list