List Archives > 
Maple User Group List Archive > 
Archive by date > 
This Month By Date > 
This Month By Topic
[MUG] Re: more efficient way to generate a set?
| [MUG] Re: more efficient way to generate a set? |
|
Author: Maple User Group
Posted: Mon, 29 Jul 2002 16:39:33 -0400
|
>> From: Maple User Group "maple_gr"
-=*=-=*=-=*=-=*=-=*=-=*=-=*=-=*=-=*=-=*=-=*=-=*=-=*=-=*=-=*=-=*=-=*=-=*=-=*=-
Date: Wed, 24 Jul 2002 17:13:28 -0700 (PDT)
From: Robert Israel "israel"
To: "maple-list"
Subject: more efficient way to generate a set?
It's better to construct the set at once using seq, rather than
repeated use of union.
gpelts:= [seq(seq([i,j], i=0..1),j=0..7)];
list_nos:=
{seq(seq(seq(seq(seq([i1,i2,i3,i4,i5],i1=gpelts),i2=gpelts),i3=gpelts),
i4=gpelts),i5=gpelts)}:
On my computer (Pentium II/266 mhz under Linux), this took about 144
seconds.
Robert Israel "israel"
Department of Mathematics http://www.math.ubc.ca/~israel
University of British Columbia
Vancouver, BC, Canada V6T 1Z2
-=*=-=*=-=*=-=*=-=*=-=*=-=*=-=*=-=*=-=*=-=*=-=*=-=*=-=*=-=*=-=*=-=*=-=*=-=*=-
From: Douglas Wilhelm Harder "douglas"
Subject: more efficient way to generate a set?
To: "maple-list"
Date: Wed, 24 Jul 2002 20:53:52 -0400 (EDT)
Hello,
You can try nested seq calls:
N := 7:
gpelts := {}:
for i from 0 to 1 do
for j from 0 to N do
n := [i,j];
gpelts := gpelts union {n};
od;
od:
list_nos := {seq( seq( seq( seq( seq( [i, j, k, l, m], i = gpelts ), j = gpelts ), k = gpelts ), l = gpelts ), m = gpelts )}:
I did it for smaller values of N, and here are the comparisions:
do-loop
N = 1 bytes used=2552312, alloc=1703624, time=2.17
N = 2 bytes used=122598104, alloc=8059452, time=124.34
seq
N = 1 bytes used=311112, alloc=327620, time=0.10
N = 2 bytes used=1072408, alloc=982860, time=0.20
N = 3 bytes used=3870484, alloc=2752008, time=0.59
N = 4 bytes used=12667948, alloc=7535260, time=3.13
N = 5 bytes used=34889472, alloc=19001960, time=15.15
I'm not going to try 7, but I suspect it should be a lot faster.
Cheers,
Douglas
=== =- - -=- -- =- ==- = === -=- --= -=-- - = ---- - == -= -=-- -=--
Douglas Wilhelm Harder
Department of Electrical and Computer Engineering http://ece.uwaterloo.ca/
University of Waterloo http://cheetah.vlsi.uwaterloo.ca/~dwharder/
.___ http://links.uwaterloo.ca/
\ .----. .-----._____.-----._______.-----._____.-----._____.----
\____/ --10-- http://scg.uwaterloo.ca/ http://mapleapps.com/ Maple 8
-=*=-=*=-=*=-=*=-=*=-=*=-=*=-=*=-=*=-=*=-=*=-=*=-=*=-=*=-=*=-=*=-=*=-=*=-=*=-
Date: Wed, 24 Jul 2002 18:01:04 -0700 (MST)
From: Matthias Kawski "kawski"
To: Caroline Ibrahim "cibrah"
Subject: more efficient way to generate a set?
Dear Caroline:
I am sorry that I cannot help you directly with the question
you asked, but I would like to add two comments.
* Nested for-loops take time - that is why for some 20 years
plus we try to think "vector operations" (in MAPLE "map"),
but it is more succinct in MATLAB. I teach my students to
work hard to avoid "for" if at all possible....
* More importantly, why would you want to "write out" all
(2^4)^5 elements? Admittedly, I am grown up with writing
matrices element-by-element into memory; but some years
ago I was taught (the hard way) that that was most often
pretty stupid, as all one needed was a description of how
the matrix "acted" on a vector/matrix -- and for that, in
many cases, it is very inefficient to write a matrix ele-
ment-by-element into memory. Maybe this philosophy will
help you, too; after all, you probably will want to do
some operations on that set, but unlikely will want to
just look at a straightforward list of some 1000000 ele-
ments.
Matthias
**********************************************************
Matthias Kawski http://math.asu.edu/~kawski
Dept. of Mathematics and Statistics "kawski"
Arizona State University office: (480) 965 3376
Tempe, Arizona 85287-1804 home: (480) 893 0107
**********************************************************
-=*=-=*=-=*=-=*=-=*=-=*=-=*=-=*=-=*=-=*=-=*=-=*=-=*=-=*=-=*=-=*=-=*=-=*=-=*=-
Date: Wed, 24 Jul 2002 21:34:31 -0500
From: Bob McElrath "mcelrath"
To: "maple-list"
Subject: more efficient way to generate a set?
Why actually create the set? Put whatever code you want to use the set
inside your innermost loop, and examine one element.
If you absolutely need the set, try creating it as a list first:
list_nos :=3D [op(list_nos), oneno];
And then after your big loop:
convert(list_nos, set);
That way Maple won't check for uniqueness at each iteration (which is
probably what is making it so slow).
Cheers,
-- Bob
Bob McElrath ()=20
Univ. of Wisconsin at Madison, Department of Physics
"No nation could preserve its freedom in the midst of continual warfare."
--James Madison, April 20, 1795
-=*=-=*=-=*=-=*=-=*=-=*=-=*=-=*=-=*=-=*=-=*=-=*=-=*=-=*=-=*=-=*=-=*=-=*=-=*=-
From: "Dr Francis J. Wright" "F.J.Wright"
To: "cibrah"
Subject: more efficient way to generate a set?
Date: Thu, 25 Jul 2002 23:17:23 +0100
I would do it like this:
> gpelts := {seq(seq([i,j], j=0..7), i=0..1)}:
> list_nos := {seq(seq(seq(seq(seq([i,j,k,l,m], m=gpelts), l=gpelts),
k=gpelts), j=gpelts), i=gpelts)}:
It took just over 150 seconds to run using Maple 7 on a 1GHz Pentium III
under Windows XP. Generally, using seq is the fastest way to generate
anything based on a sequence, and it is often useful to construct a set by
first constructing a sequence of its elements.
Francis
-=*=-=*=-=*=-=*=-=*=-=*=-=*=-=*=-=*=-=*=-=*=-=*=-=*=-=*=-=*=-=*=-=*=-=*=-=*=-
Date: Sun, 28 Jul 2002 08:24:06 -0700 (PDT)
From: Anders Ballestad "anders"
To: "maple-list"
Subject: more efficient way to generate a set?
Is it possible to vectorize this code and implement it with
FOrtran 90 or Matlab? Vectorization is very fast...
Greetings Anders Ballestad
U. of British Columbia
Vancouver, Canada
|
Previous by date: [MUG] Re: question, Helmut Kahovec
Next by date: [MUG] Bug in print(), Helmut Kahovec
Previous thread: [MUG] How to force a common factor from a simbolic matrix, Nicolai Sirbu
Next thread: [MUG] Bug in print(), Helmut Kahovec
|