如何从MAXIMA中的sort或member命令获取标记

时间:2016-02-12 12:56:56

标签: sorting maxima

MAXIMA中的排序或成员函数不返回已排序元素的标记或要搜索的元素的索引。是否有返回索引的sort函数?感谢。

斯文

1 个答案:

答案 0 :(得分:0)

There aren't built-in functions for those tasks, but I think you can write something fairly easily:

member_index (x, l) :=
 (sublist_indices (l, lambda ([y], x = y)),
  if %% = [] then false else first (%%));

This member_index function returns the index of the first instance of x. If you want indices for all instances, just remove the if expression and return %% instead.

ranks (l) :=
  block ([l1, l2],
         makelist ([l[i], i], i, length (l)), 
         l1 : sort (%%, lambda ([x, y], orderlessp (x, y))),
         l2 : makelist (0, length (l)),
         for i thru length (l) do l2[l1[i][2]] : i,
         l2);

This ranks function returns integer values when there are ties (i.e. same values). Sometimes it is preferable to return the average rank for ties; that take a little more code. There is a ranks function in share/amatrix/wilcoxon.mac which does average ties. You can load that function via load(wilcoxon);.