My newbie question of the minute:
In Bill's function below, he as a lisp list which he works on.
I have a maxima list of length 188 (i.e., like
[4, 6, 7, -3, 8, 2, 2, 5, 2, . . . ]
How do I transform this to the lisp list, apply your function, and
find the indices where 2 appears?
More generally, this procedure is actually a special case of finding
the inverse of a function.
That is, (using latex notation) we have a map
$f$ from the set $N = \{ 0, 1,2, \ldots , 187 \}$ into a set $B$ (in this
case the set of integers), and we want to find the pre-image of an
element (in this case the element $2$).
Since maxima operates on sets and lists, it may be useful to have
something which operates on both objects, or, since there are
conversion tools, maybe just on sets themselves.
TIA,
-sen
On Thu, 4 Jan 2007, Bill Wood wrote:
> On Thu, 2007-01-04 at 11:23 -0500, sen1 at math.msu.edu wrote:
>> How about a name more mnemonic about indices?
>>
>> Like
>> find_index, list_index, or locate_index
>>
>> Of course, the actual function could actually find many indices for various
>> predicates.
>
>> From the examples it seems clear that the desired functionality is to
> return the list of indices of all occurrences of items satisfying the
> predicate. The Common Lisp built-in "position-if" returns the index of
> only the left-most (right-most if :from-end is non-nil) occurrence, so
> some code has to be written. How about "all-positions-if" or
> "all-positions"? "positions-if" or "positions" might also be used, but
> they could too easily be confused with (or fat-fingered into)
> "position-if" or "position", and you might want those functions as well.
>
> Here's a bare-bones CL function definition:
>
> (defun all-positions-if (items pred)
> (do ((i 0 (1+ i))
> (xs items (cdr xs))
> (acc '() (if (funcall pred (car xs)) (cons i acc) acc)))
> ((endp xs) (nreverse acc))))
>
> Test runs:
>
> * (all-positions-if '(2 1 3 3 5) #'(lambda (x) (= x 3)))
> (2 3)
> * (all-positions-if '(2 1 3 3 5) #'(lambda (x) (= x 33)))
> NIL
>
> (Don't forget, CL uses 0-based indexing; I guess maxima uses 1-based?)
>
> If you like a more functional style, here's an alternative definition:
>
> (defun all-positions-if (items pred)
> (labels ((h (pr x)
> (cons (1+ (car pr))
> (if (funcall pred x)
> (cons (car pr) (cdr pr))
> (cdr pr)))))
> (nreverse (cdr (reduce #'h items :initial-value (cons 0 '()))))))
>
> -- Bill Wood
>
>
>
>
>
> _______________________________________________
> Maxima mailing list
> Maxima at math.utexas.edu
> http://www.math.utexas.edu/mailman/listinfo/maxima
>
--
---------------------------------------------------------------------------
| Sheldon E. Newhouse | e-mail: sen1 at math.msu.edu |
| Mathematics Department | |
| Michigan State University | telephone: 517-355-9684 |
| E. Lansing, MI 48824-1027 USA | FAX: 517-432-1562 |
---------------------------------------------------------------------------