Rescaling a general size vector (original) (raw)
Next: General size matrices Up: General size vectors Previous: General size vector subtraction Contents
Rescaling a general size vector
Multiplying or dividing a vector by a scalar value follows the scheme of the above copy, addition and subtraction operations. To multiply a vector
by a scalar
,
, use for example
Gan_Vector vx, vy; /* declare vectors x & y */
/* ... create & fill vx, create (& optionally fill) vy ... */
gan_vec_scale_q ( &vx, 5.0, &vy ); /* y = 5*x */to multiply all the elements in vector
by five, writing the result into vector
. Alternatively you can create the rescaled vector from scratch as in
Gan_Vector *pvy; /* declare vector y */
/* ... create & fill vx ... */
pvy = gan_vec_scale_s ( &vx, 5.0 ); /* y = 5*x */or overwrite
with the result
gan_vec_scale_i ( &vx, 5.0 ); /* replace x = 5*x */There are similar routines for dividing a general size vector by a scalar value:
gan_vec_divide_q ( &vx, 5.0, &vy ); /* y = x/5 */
pvy = gan_vec_divide_s ( &vx, 5.0 ); /* y = x/5 */
gan_vec_divide_i ( &vx, 5.0 ); /* replace x = x/5 */Passing zero as the scalar value in this case invokes the error handler, with a division by zero error (error code GAN_ERROR_DIVISION_BY_ZERO), and NULL is returned.
There are specific routines to negate a vector, i.e. multiply it by -1, as follows:
gan_vec_negate_q ( &vx, &vy ); /* y = -x */
pvy = gan_vec_negate_s ( &vx ); /* y = -x */
gan_vec_negate_i ( &vx ); /* replace x = -x */Error detection: The Gandalf error handler is invoked and NULLis returned if an error occurs. The most likely failure modes are (i) failing to create the result vector; (ii) division by zero.
Next: General size matrices Up: General size vectors Previous: General size vector subtraction Contents
2006-03-17