From: Tom Kramer
Subject: diagonal matrix partial sums
Date: 
Message-ID: <21840@cosmos.cme.nist.gov>
A few days ago I posted a solution to the problem:

> 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.

The following is another solution. It is less obvious, but much more
efficient. The argument is a list of the numbers on the diagonal of
the matrix. As before, there is an extra partial sum at the end of the
returned list that was not asked for, namely the last element of the
argument. This function could easily be modified slightly to get rid
of it.

(defun partial_sums (diagonal)
  (cond ((null (rest diagonal)) diagonal)
        (t
         (let ((partials (partial_sums (rest diagonal))))
           (cons (+ (first diagonal) (first partials)) partials)))))

(partial_sums '(3 1 -7 2 3)) returns (2 -1 -2 5 3), as before.