From: Tom Kramer
Subject: XLISP-STAT
Date: 
Message-ID: <21814@cosmos.cme.nist.gov>
; ······@gosset.kodak.com (Jeffrey D. Morris) writes:

;> I have a diagonal matrix, L, of rank p and am trying to get sums of
;> the diagonal elements.  What I need to do is sum elements 1 to p, then
;> 2 to p,..., p-1 to p. each time outputting the sum.  I have written two
;> do loops to do the summing, but am unable to figure out how to output
;> the intermediate values.  I would also like to output another function
;> value at the same time as the intermediate sum that is a function of
;> that sum.

; This looks like a setup, but I will bite.

; If it is a diagonal matrix, it can be represented by a list of the
; elements on the diagonal. In this form the problem becomes simple
; using maplist.

(defun partial_sums (diagonal)
  (maplist #'(lambda (liz) (apply #'+ liz))
           diagonal))

; Then, for example,
  (partial_sums '(3 1 -7 2 3))
; returns (2 -1 -2 5 3)

; If you don't want the last element of the list, you can either ignore
; it, or add a call to butlast in the partial_sums function.

; Then, if you want another function of the partial sums, just use mapcar
; with the function you want.

; If your function is cos, for example
(mapcar #'cos '(2 -1 -2 5 3))
; returns (-0.41614684 0.5403023 -0.41614684 0.2836622 -0.9899925)

; If you have some other representation for the matrix, such as an
; array, there is undoubtedly another way to write the function, but you
; could just start by converting to a list representation and then using
; the above.