In MATLAB, you can find the indices of array entries that match a user-supplied condition with the function find()

Here’s a similar function in Maxima. This makes liberal use of the functions **sublist_indices()**, **lambda()**, and several functions from the stringproc package: **sequal()**, **eval_string()**, and **simplode(). **Oh, and also the shameless hack of using **ascii(32)** as the space character 🙂

Here are some examples. The code is included at the bottom.

find(exp):=block( oo:op(exp), if sequal(oo,"=") or sequal(oo,">") or sequal(oo,">=") or sequal(oo,"<") or sequal(oo,"<=") or sequal(oo,"#") then sublist_indices(first(exp),lambda([x1],eval_string(simplode([x1,op(exp),last(exp)])))) else if sequal(oo,"and") or sequal(oo,"or") then( e1:first(exp), e2:second(exp), sublist_indices(first(e1),lambda([x1], eval_string(simplode([x1,op(e1),last(e1),ascii(32),oo,ascii(32),x1,op(e2),last(e2)])))) ) )$

Advertisements