List Archives > 
Maple User Group List Archive > 
Archive by date > 
This Month By Date > 
This Month By Topic
[MUG] Sorting a List of Lists for Plotting
| [MUG] Sorting a List of Lists for Plotting |
|
Author: JReinmann524
Posted: 02/03/2000 17:20:19 GMT
|
>> From:
Hello,
I have been using "fsolve" to to solve a transcendental equation with two
parameters (a and b). I choose one parameter and solve for the other. I
then put the two values in a list [a,b]. Next, I add this list to an already
existing sequence of lists by the command.
> L:=L,[a,b];
I then plot the list [L] with the command
>plot([L]).
I always get a plot, but since the list is not ordered with respect to the
parameter 'a', the plot results in straight line segments zigzagging all over
the plot.
QUESTION 1:
Can Maple order the list with respect to the parameter 'a' ?
I have written a bubble sort procedure that does the sorting nicely, but I
wonder if Maple can do this for me. It seems like such a common requirement.
QUESTION 2:
Is there any way to add an element, say [a1,b1], to a selected position
somewhere inside of the sequence L or the list [L] ?
Thanks,
John Reinmann email:
|
| [MUG] Re: Sorting a List of Lists for Plotting |
|
Author: Maple Group
Posted: 06/03/2000 19:54:05 GMT
|
>> From: Maple Group
| >> From:
| I have been using "fsolve" to to solve a transcendental equation with two
| parameters (a and b). I choose one parameter and solve for the other. I
| then put the two values in a list [a,b]. Next, I add this list to an already
| existing sequence of lists by the command.
| > L:=L,[a,b];
| I then plot the list [L] with the command
| >plot([L]).
| QUESTION 1: Can Maple order the list with respect to the parameter 'a' ?
| QUESTION 2: Is there any way to add an element, say [a1,b1], to a
| selected position somewhere inside of the sequence L or the list [L] ?
-=*=-=*=-=*=-=*=-=*=-=*=-=*=-=*=-=*=-=*=-=*=-=*=-=*=-=*=-=*=-=*=-=*=-=*=-=*=-
From: Norbert Roth
To:
Date: Sat, 04 Mar 2000 05:22:18 +0100
Subject: Sorting a List of Lists for Plotting
Hello John,
on 02-Mar-00 you wrote :
; QUESTION 1:
Hhmm, AFAIR there exist's a sort command,
but it works only for one dimensional lists, e.g. L:=[1,7,15,...]
(But I maybe wrong at this point, try ?sort to get further info)
; QUESTION 2:
Yes, this command exists:
'subsop(i=x,L)' replaces the i-th element of the list L by x
Adding an element to the list can be done via 'op(L,x)'.
Here's a quick hack which solves your sorting problem:
> BSort:=proc(L:list)
local i,j,temp,TL;
TL:=L;
for i from 1 to nops(TL)-1
do
for j from 1 to nops(TL)-i
do
if TL[j][1] > TL[j+1][1] then
temp:=TL[j]:TL:=subsop(j=TL[j+1],TL):
TL:=subsop((j+1)=temp,TL) fi;
od;
od;
RETURN(TL)
end;
When using NL:=BSort(L),
NL will contain the sorted version of L.
Regards
NR
PS
I just had another idea how to implement a sorting
algorithm in MapleV, so here's another procedure,
it's about seven times as fast a the last one.
> StrangeSort:=proc(L:list)
local i,j,TL,LL,ML,xplace;
TL:=L:
for i from 1 to nops(L)
do
LL:=seq(TL[j][1],j=1..nops(TL)):
#for changing the sorting sequence from descending
#to ascending just change 'max(LL)' to min(LL) in
#the next line of the procedure
member(max(LL),convert([LL],list),'xplace'):
if i=1 then NL:=TL[xplace] else NL:=NL,TL[xplace] fi;
TL:=subsop(xplace=NULL,TL):
od:
NL:=convert([ML],list);
RETURN(ML):
end;
When using NL:=StrangeSort(L),
NL will contain the sorted version of L.
Regards
NR
--
Remark of the day :
Description of human morality :
We ought to.
But we dont !
-=*=-=*=-=*=-=*=-=*=-=*=-=*=-=*=-=*=-=*=-=*=-=*=-=*=-=*=-=*=-=*=-=*=-=*=-=*=-
Date: Sat, 4 Mar 2000 23:32:39 -0800 (PST)
From: Robert Israel
To:
Subject: Sorting a List of Lists for Plotting
On Thu, 2 Mar 2000 wrote:
|> QUESTION 1:
See the help for "sort". All you need to do is write a Boolean-valued
function that defines the ordering you wish to use (returning true if its
first argument is to come before the second). Thus:
> lex2:= proc(a,b)
if a[1] = b[1] then evalb(a[2] <= b[2])
else evalb(a[1] <= b[1])
fi
end;
> sort([L],lex2);
|> QUESTION 2:
(for a list: this version must be called with the name of a variable to
which the list has been assigned)
> insert:= proc(L::uneval, x, pos::posint)
assign(L,[op(L[1..pos-1]),x,op(L[pos..-1])]);
eval(L);
end;
Robert Israel
Department of Mathematics http://www.math.ubc.ca/~israel
University of British Columbia
Vancouver, BC, Canada V6T 1Z2
-=*=-=*=-=*=-=*=-=*=-=*=-=*=-=*=-=*=-=*=-=*=-=*=-=*=-=*=-=*=-=*=-=*=-=*=-=*=-
Date: Mon, 06 Mar 2000 11:10:50 +0100
From: Adri van der Meer
To:
Subject: Sorting a List of Lists for Plotting
| QUESTION 1:
It is:
sort( [L], (x,y)->evalb(x[1]<x[2]) );
| QUESTION 2:
There is no "easy way"; you have to create a new list or
sequence. For example (if L a list)
insert := proc(element,place,L)
local K,i;
K := NULL;
for i to place-1 do K := K,L[i] od;
K := K,element;
for i from place to nops(L) do K := K,L[i] od;
[K]
end;
--
A. van der Meer
Dept. Applied Mathematics
University of Twente Phone +31 (53) 4893427
P.O. Box 217 Fax +31 (53) 4894824
7500 AE Enschede
-=*=-=*=-=*=-=*=-=*=-=*=-=*=-=*=-=*=-=*=-=*=-=*=-=*=-=*=-=*=-=*=-=*=-=*=-=*=-
Date: Mon, 06 Mar 2000 12:20:31 +0000
To:
From: Stanley J Houghton
Subject: Sorting a List of Lists for Plotting
>QUESTION 1:Can Maple order the list with respect to the parameter 'a' ?
Certainly, using the sort procedure as follows:
> sort(L,(a,b)->evalb(a[1]<b[1]));
See the help pages on sort for more details.
>QUESTION 2:
You can use basic list processing to add an element as follows, say at
position 'n':
>L:=[op(1..n-1,L),[a1,b1],op(n..-1,L)];
but it is again not very efficient. You may be better, generating the list
and then sorting it.
Hope it helps
Stan
-=*=-=*=-=*=-=*=-=*=-=*=-=*=-=*=-=*=-=*=-=*=-=*=-=*=-=*=-=*=-=*=-=*=-=*=-=*=-
Date: Mon, 6 Mar 2000 09:28:01 -0600 (CST)
From: Andrzej Pindor
To:
Subject: Sorting a List of Lists for Plotting
How about something along the folowing lines:
add_list:=proc(l_old,n,new_element) local i;
[seq(l_old[i],i=1..n-1),new_element,seq(l_old[i],i=n..nops(l_old))]
end;
For a sequence (instead of a list) just drop [] around the expression above.
Hope it helps
Andrzej
=========
Dr. Andrzej Pindor The foolish reject what they see and not what
University of Toronto think; the wise reject what they think and not
Information Commons what they see. Huang Po
Phone: (416) 978-5045 Fax: (416) 978-7705
-=*=-=*=-=*=-=*=-=*=-=*=-=*=-=*=-=*=-=*=-=*=-=*=-=*=-=*=-=*=-=*=-=*=-=*=-=*=-
From: "Symancyk, Daniel"
To:
Subject: Sorting a List of Lists for Plotting
Date: Mon, 6 Mar 2000 11:53:18 -0500
QUESTION 1
If you use sort with a boolean procedure (such as the one below
from robert Israel) you can sort your [L] the way you want.
>sortf:= proc(a,b)
if a[1] = b[1] then
evalb(convert(a[2],string) <= convert(b[2],string))
else evalb(a[1] < b[1])
fi
end;
Then use
>m:=sort([L],sortf);
>plot(m);
The graph will not zigzag.
Note: zigzag is not a problem and reordering is not needed if you are
willing to graph just points.
>plot([L],style=point);
QUESTION 2
Here is a procedure that will insert a newEntry into list L
at a specified location. The procedure uses the fact that
subsop allows you to replace an entry in a list with a new entry.
>insertL:=proc(insertLocation::posint,newEntry,L::list)
local L1,L2,j;
if nops(L)=0 then [newEntry];
else
if insertLocation<=0 or insertLocation>nops(L) then ERROR(`insertion
location must be between 1 and the number of items in the list`) fi;
L1:=L[1..insertLocation];
L2:=L[insertLocation..nops(L)];
L1:=subsop(insertLocation=newEntry,L1);
for j from 1 to nops(L2) do
L1:=[op(L1),op(j,L2)];
od;
L1;
fi;
end:
For example
>M:=[ [2,3], [3.5,7], [4,9] ];
This inserts [6,7] as the third item in M
>M:=insertL(3,[6,7],M);
Daniel F. Symancyk
Math Department
Anne Arundel Community College
Arnold, MD 21012
410-541-2587
|
Previous by date: [MUG] Re: Diff remember table, Maple Group
Next by date: [MUG] Re: A question about solving Laplace's PDE with Maple, HOLMGREN
Previous thread: [MUG] Plotting error: non-numeric vertex, Tim Howard
Next thread: [MUG] A question about solving Laplace's PDE with Maple, Vassil Vassilev
|