Question

I just thought of writing some function similar to Mathematica's partition function with passing option in maxima as,

listpartitionpad(l,n,k,d):= block([temp:[],gap,newl,ntemp:[]],
                    newl:apply(create_listpad,flatten([n,k,d,l])),
                    map(lambda([x],if(length(newl)>=x+n-1 and is(unique[x]#[d]))then temp:cons(part(newl,makelist(i,i,x,x+n-1)),temp) 
                    else temp:cons(part(newl,makelist(s,s,x,length(newl))),temp)),makelist(i,i,1,length(newl),k)),
                    ntemp:sublist(temp,lambda([x],is(length(x)=n))),reverse(ntemp));

Usage:listpartitionpad([a,b,c,d,e,f,g],3,3,x); => [[a,b,c],[d,e,f],[g,x,x]]

Now allof the list manipulation functions are coded in lisp as I checked.

My question is that is it fine that I could code any such function in maxima language rather than in lisp or it will give me some performance issue or something else I should know that I don't yet. I ran a simple test

:lisp(time(loop repeat 1000000))

real time       :      0.850 secs
run-gbc time    :      0.540 secs
child run time  :      0.000 secs
gbc time        :      0.310 secs

In case of another maxima based approach,

block([],for i:1 thru 1000000 do []);

Evaluation took 5.7700 seconds (5.7700 elapsed)

And this difference grows exponentially as i grows.

Is this the reason all list manipulations are coded in lisp ?

Was it helpful?

Solution

A few different points.

  • Maxima is mostly implemented in Lisp because (1) the implementation needs access to the internal structure of expressions, and (2) typically Lisp code is faster than Maxima code.
  • While it's true that Lisp code usually runs much faster than Maxima code, my advice is to use Maxima to implement algorithms unless you are very familiar with Lisp. The speed difference is probably not going to make much practical difference. The time you spend, as the developer, is more important.
  • About block([],for i:1 thru 1000000 do []), if its run time really is nonlinear in the loop count, that sounds like a bug. If you can verify that, feel free to open a bug report about it. http://sourceforge.net/p/maxima/bugs (you need to create an SF login to submit a bug report.)

OTHER TIPS

I don't think the language of implementation can possible explain the exponential growth (which is probably quadratic rather then exponential really).

I suspect that your algorithm is suboptimal.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top