问题
What would be the most efficient way to expand a dense matrix with new columns in FORTRAN?
Say T is a dense matrix m by n
and I would like to make it m by n+1. One strategy I could think of : Reallocate at each step and assign the last column or would there be some better ways, such as allocating some space before and checking if that is sufficient and if not do the reallocation kind of stuff? Any ideas?
回答1:
Assuming m
and n
are in some sense not exceedingly large, so that your matrices fit into memory and what you're after is performance in time, what I'd do I'd allocate a large matrix and store the actual size separately. This is what, for example, BLAS libraries use as a 'leading dimension'. Then, when you need to add a column, you check if your actual size is still smaller than the maximum size, and reallocate memory if necessary.
回答2:
If you have a Fortran 2003 Compiler you might make use of move_alloc: http://software.intel.com/sites/products/documentation/hpc/compilerpro/en-us/fortran/lin/compiler_f/lref_for/source_files/rfmvallo.htm
来源:https://stackoverflow.com/questions/7468268/expand-a-dense-matrix