Are scalars precomputed when multiplied with or added to vectors?
David Bateman
David.Bateman at motorola.com
Wed Mar 12 10:25:25 CDT 2008
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.
D.
--
David Bateman David.Bateman at motorola.com
Motorola Labs - Paris +33 1 69 35 48 04 (Ph)
Parc Les Algorithmes, Commune de St Aubin +33 6 72 01 06 33 (Mob)
91193 Gif-Sur-Yvette FRANCE +33 1 69 35 77 01 (Fax)
The information contained in this communication has been classified as:
[x] General Business Information
[ ] Motorola Internal Use Only
[ ] Motorola Confidential Proprietary
More information about the Help-octave
mailing list