1

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 ?

Pankaj Sejwal
  • 1,605
  • 1
  • 18
  • 27

2 Answers2

3

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.)
Robert Dodier
  • 16,905
  • 2
  • 31
  • 48
  • I plotted list of points and I find them linear. So I think I am expecting too much from system, it seems to work fine. Actually Mathematica does it for 100 million in 3 seconds. That's why I enquired. – Pankaj Sejwal Mar 13 '14 at 09:54
  • @Rorschach OK, thanks for investigating. By the way, you can compile functions in Maxima which can speed them up (a little or a lot, it varies widely). E.g. `f() := block(...)` (stuff above) and then `compile(f)`. – Robert Dodier Mar 13 '14 at 15:49
  • Ya I checkd it and it brought down time by 17 times almost. That's really cool. I read it theoretically but didn't try it yet. Now for 100 million its almost 15 seconds, though earlier it was seemingly infinity. – Pankaj Sejwal Mar 14 '14 at 11:03
0

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.

sds
  • 58,617
  • 29
  • 161
  • 278
  • well I can agree in case of larger code but with the shortest snippet it must be kind of proof of concept. – Pankaj Sejwal Mar 12 '14 at 15:56
  • proof of concept or not, if your algo is quadratic, you will lose no matter what language. – sds Mar 12 '14 at 16:53
  • actually its just a for loop running and there is considerable time gap. Robert has correctly explained it. Thanks for your opinion. :) – Pankaj Sejwal Mar 12 '14 at 19:48