List Archives > 
Maple User Group List Archive > 
Archive by date > 
This Month By Date > 
This Month By Topic
[MUG] Re: Matrix and filling it up
| [MUG] Re: Matrix and filling it up |
|
Author: Carl Devore
Posted: Sat, 26 Oct 2002 20:40:22 -0400
|
>> From: Carl Devore "devore"
On Wed, 23 Oct 2002, Caroline Ibrahim wrote:
> I want to create this huge matrix (say 1048576 by 5) and fill it up with
> 0, 1, 2 and 3 in an ordered manner. For example the first column of the
> matrix will have 262144 zeros, then 262144 ones,then 262144 twos and
> 262144 threes. In the second column, the first 65536 rows will have
> zeros, the next 65536 rows ones, then 65536 rows of twos and 65536 rows of
> threes. The rest of the rows of the second column are filled the same way
> the first 262144 are filled up. I hope you got the pattern here of
> filling up the rest of the columns.
>
> What's the best way to create this matrix with less running time? Can
> this be done using sequences? (I need to use it in some other
> calculations).
More important than filling it up efficiently is using it efficiently.
Yes, the fastest way that I can find to fill it is with sequences:
A:= Matrix(
[seq]([seq](seq(i $ 4^(10-j), i= 0..3), k= 1..4^(j-1)), j= 1..5)
,scan= columns
,datatype= integer[1]
);
This takes 6.6 secs on my computer. The datatype= integer[1] will cut the
memory usage by a factor 4, but does not have a significant effect on the
fill time. The restricted datatype may inhibit some subsequent
calculations.
It might be better to use an indexing function, escpecially if you do not
plan to change the Matrix. The indexing function giving the value as a
function of the row & column (i & j) would be:
(i,j)-> irem(iquo(i-1, 4^(10-j)), 4);
This will use almost 0 memory, but makes the lookups take a bit longer.
Example:
`index/Caroline`:= IJ-> irem(iquo(IJ[1]-1, 4^(10-IJ[2])), 4);
proc() :-A:= Matrix(4^10,5, storage= empty, shape= Caroline); [][] end();
A[746908,3];
1
The matrix creation needs to be done in a procedure to avoid processing
every (i,j) at creation time.
|
[View Complete Thread]
Previous by date: [MUG] filter transformations -- basic algebraic manipulations, Ben Nevile
Next by date: [MUG] Re: How to plot with a constraint on the domain, Carl Devore
Previous thread: [MUG] solving inequality, M A Suzen
Next thread: [MUG] How to plot with a constraint on the domain, Anders Poulsen
|