Basic Operations
Last updated
Last updated
Let and with:
Then , if and only if:
That is, the corresponding elements of and are equal.
Vector addition (sum of vectors) is defined by:
In other words, the vectors are added element-wise, yielding a new vector of the same size.
It is often referred to as the AXPY
operation, which stands for alpha times x plus y. We emphasize that it is typically used in situations where the output vector overwrites the input vector y.
Consider the following scaled vector addition:
Now, imagine all the data for this scaled vector addition starts by being stored in the main memory of a computer. Further, in order to compute, data must be brought into registers and there are only a few registers available.
Now, we know that the scaling factor 2 in the above example is going to be used many times. So, we store it in a register and keep it there during the entire computation. This costs us 1 memory operation (memops).
A vector (valued) function is a mathematical function of one or more scalars and/or vectors whose output is a vector.
For example,
Another example can be,
The AXPY
and DOT
operations are also examples of vector functions.
We will now focus on vector functions as functions that map a vector to another vector:
For example,
Another example can be,
Do note the following:
For given matrices A and B, their product is given by:
Hadamard product is element-wise multiplication. Following equations provide details of how the this product operation is calculated for given matrices A and B:
The dot (inner) product between two vectors a and b is given by:
Alternatively,
The transpose of a matrix A is given by:
The following equation provides the deifnition of inverse of a matrix:
Not all square matrices have an inverse.
For such cases, and non-square matrices, "pseudoinverse" is defined.
In this interpretation, let and . These 2 vectors actually lie in a plane and if we view them in that plane, we can observe the result of doing . If lay the vectors "heel to toe" , e.g. if we lay vector 's heel (where starts) to toe of (where ends), can be given by going from heel of to toe of . Vice-versa is also true.
Another geometric interpretation is, if we lay 's heel to toe of or we lay 's heel to toe of , this would create a parallelogram. The result of is given by the diagonal of the parallelogram. This is known as the parallelogram method.
Scaling a vector is same as stretching the vector. Let and . Then, vector scaling is given by:
Let and . Then, vector subtraction can be given by:
Taking the negative of means it points in the opposite direction. Now, we have two vectors and that are heel to toe. can be given by going from heel of to toe of .
Same as for Vector Addition, the parallelogram method can be applied here as well. is the other diagonal of the parallelogram.
One of the most commonly encountered operations when implementing more complex linear algebra operations is the scaled vector addition, which (given and )computes to:
Further, for each pair of one element from and one element from , we need to read these elements and write the result back to . That means 2 memops for reading, 1 more for writing.
Finally, for each pair of elements from and , we perform a multiplication and then an addition. That would be 2 floating point operations (flops).
Now, if the size of the vectors is n, then we would have to do memops and flops.
, then happens sometimes.
, then happens sometimes.
The cost of a dot product with vectors of size is memops and flops.