question about sublist



Hi Stavros,

On Wed, 18 Apr 2007, Stavros Macrakis wrote:

> Sheldon,
>
> I took a look at your routines, and don't see why they'd get a runtime error
> after being compiled.  Sorry about that; that appears to be a bug.
>
> That said, I am a bit confused at why you are programming this simple
> problem in such a way when you initially stated that you didn't want to
> waste space -- and when several of us have proposed more efficient
> solutions.

Actually, here the main effort was not to waste time. I thought by
loading various new lists, I would also increase the time of execution.

I saw that a combination of 'sublist' and 'lambda' works very fast for
certain related simplified problems.  So, I  wanted to learn how to add more
options to the 'lambda' constructions.  The comments from you and RJF
regarding 'map' have actually been very useful.

For instance, here is some comparative output of the routines I have
so far with the attached modified 'sublist.mac' file.

(%i1) load("sublist");
Evaluation took 00.10 seconds (00.10 elapsed)
(%o1)                            ./sublist.mac
(%i2) compile(signrun);

Compiling gazonk0.lsp.
End of Pass 1.
End of Pass 2.
OPTIMIZE levels: Safety=2, Space=3, Speed=2
Finished compiling gazonk0.lsp.
Evaluation took 0.00 seconds (0.46 elapsed)
(%o2)                              [signrun]
(%i3) xx: xrl(10000)$
Evaluation took 0.90 seconds (0.90 elapsed)
(%i4) xxx: xrl(100000)$
Evaluation took 8.69 seconds (8.74 elapsed)
(%i5) same_1(xxx)$
Evaluation took 10.37 seconds (10.41 elapsed)
(%i6) signrun(xxx)$
Evaluation took 501.92 seconds (502.58 elapsed)

/*************************************************************/

Since you expressed curiosity about why I am doing this, I am including a
brief description of the actual problem.

The reasons for it I can describe by private email if anyone is
interested.

The actual problem is to find the (close approximation) to
intersection points of a parametrized curve g in the plane with the
y-axis. The curve g is generated by taking n iterates of the
horizontal line segment $y = .63, 0 \leq x \leq 0.1$
by the polynomial map $H(x,y) -> (1 + y - a*x^2, 0.3*x)$
where the number of iterates n is 14, 15 or so, and 'a' is a real
parameter.   One interesting value is 'a=1.4'.  I want to understand
how the number of intersections changes with varying 'a' near 1.4

So, first, consider the case of  a = 1.4

I have an algebraic curve of degree 2^n, with n = 14, 15, and I want
to find the intersections with the y-axis in the plane.  I know there
is a wide literature on the finding the intersections of algebraic
curves, but I have not yet found anything which I can use with curves
of this high a degree.

Any ideas on this problem will be welcome.


So, I decided to try the following.

1. Generate a list li_1 of 10000 uniformly distributed points on the
    initial line segment.  Take the nth image by H, call it li_2.
    take the sublist where the x-coordinates change sign.  These should
    be close to the intersections.  Doing some experiments, this seems
    to actually work.  Of course, I might want to make an 'adaptive'
    method which adds more points when the successive points on the
    list li_2 are not close to each other.

Thanks again for all the responses,

-sen


> If your goal is to develop a particular style of programming without taking
> efficiency into consideration, that is perfectly legitimate, of course, but
> it is not supported particularly efficiently in Maxima (unlike, say, in
> APL).
>
> If on the other hand this is a class of problems that you want to be able to
> solve easily and efficiently, I'd think the best way to approach it would be
> to design and program some more appropriate map or sublist primitives, e.g.
> property_run(list,fun), which groups all runs of list which have the same
> value of fun.

>
>               -s
>
-------------- next part --------------
showtime: all;

xrl(i):= makelist(random(10.0)-5,j,1,i);


j_sublist(x):= block([l: [], prev: x[1]], for i:2 thru length(x) do
        (if prev*x[i] > 0 then l: cons(prev,l), prev:x[i]), reverse(l));


/**********************  old routines

li_2(list):= makelist([list[i],list[i+1]],i,1, length(list) -1)$

pos_2(list):= 
     sublist(list, lambda([x],x[1]*x[2] > 0))$   

*/


/* project the list to its first coordinate */

proj_1(list):= list[1]$


fex(x,y):=[x,y]$

/* to get the sublist of points whose immediate successor has the
opposite sign, do opp_1(list) */

opp_1(list):= block([zz,yy,uu,vv],
      zz: rest(list),
      yy: reverse(rest(reverse(list))),
      uu: map(fex,yy,zz),
  vv: sublist(uu, lambda([x], x[1]*x[2] < 0)),
     map(proj_1,vv) 
       )$
      
/* to get the sublist of points whose immediate successor has the
same sign, do same_1(list) */

same_1(list):= block([zz,yy,uu,vv],
      zz: rest(list),
      yy: reverse(rest(reverse(list))),
      uu: map(fex,yy,zz),
  vv: sublist(uu, lambda([x], x[1]*x[2] > 0)),
     map(proj_1,vv) 
       )$

signrun(l):=
  if l=[] then [] else
   block([acc:[],nextl:rest(l)],
              while nextl#[] do
                  ( if sign(first(l))=sign(first(nextl)) then
acc:cons(first(l),acc),
                    l: nextl, nextl:rest(l) ),
              reverse(acc) )$