Search All of the Math Forum:
Views expressed in these public forums are not endorsed by
NCTM or The Math Forum.


Kaba
Posts:
289
Registered:
5/23/11


Re: matrices as vectors
Posted:
Feb 22, 2013 12:43 PM


20.2.2013 4:01, leonard14279@gmail.com wrote: > The question is, are there already existing formulas for matrix operations, > where the matrix is represented as a 1dvector?
This isn't actually a math question; it's more about software engineering. You wish to implement algorithms to compute with matrices. How you store the matrix in the memory is a separate concern from what the abstract algorithms do. What you need to do is to abstract this detail away, so that you can implement things in the same abstraction level as they are presented in mathematics, but also change the way the matrix is stored, if you wish.
As noted in the book Matrix Computations, there are several ways to organize computations, all of which are equivalent in mathematics, but some of which perform faster because they utilize the cache of modern computer more efficiently.
Most of the implementations of (dense) matrices use a contiguous block of memory, as you describe, storing rowbyrow (rowmajor), or columnbycolumn (columnmajor). This localizes the memory better than, say, allocating each row separately.
A scheme which strikes a good balance between performance and flexibility is to precompute for each ndimensional array an element H in Z^n (n integers) such that given a coordinate x in Z^n you can compute the linearized index I in Z as
I = sum_{i = 1}^n x_i H_i,
i.e. the dot product. For example, for a 2dimensional array of width 100 and height 200, you would compute the index as (rowmajor)
I = y * 100 + x.
so that H = [1, 100].
 http://kaba.hilvi.org



