Note that there are some explanatory texts on larger screens.

plurals
  1. POIterating over pairs of numbers in an increasing order
    primarykey
    data
    text
    <p>I'll explain where the question comes from at the bottom, but here's the statement. Suppose I have two lists of non-negative integers, which I'll write <code>(A[0] ... A[n])</code> and <code>(B[0] ... B[m])</code>. They are strictly increasing, so <code>A[i+1] &gt; A[i]</code> for all <code>i</code> and similarly for <code>B</code>. I want to collect all <code>n * m</code> pairs of elements in increasing order of their sum.</p> <p>So, for example if <code>A = (0 1 2)</code> and <code>B = (1 4)</code>, then I want to end up collecting <code>((0 1) (1 1) (2 1) (0 4) (1 4) (2 4))</code>. If there is a tie, I don't care which order I collect the two elements in. For example, if <code>A = (0 1)</code> and <code>B = (0 1)</code>, then I don't mind which of the mixed terms, <code>(0 1)</code> or <code>(1 0)</code>, I pick up first.</p> <p>Obviously, I'd like this to be reasonably efficient. I hope that it's possible in time asymptotic to <code>m * n</code>. Specifically, I'm hoping that the ordered input makes this problem strictly easier than the equivalent problem if I didn't know anything about the inputs. What I was thinking about when I first asked the question was the amount of state we have to store. I was hoping it was possible with a constant amount, but maybe this is unrealistic. (The things I've tried since have all failed!)</p> <p>The code will actually be written in Lisp, but I think that the problem statement is pretty much independent of that. The inputs would most naturally come as singly-linked lists, but I will have to reverse them in advance anyway, so if random-access is relevant, I can make them arrays. In case it's relevant, I expect this to be mostly called on quite small lists, so a massive constant term / constant factor in runtime probably precludes a solution. (Although I'd be fascinated to hear about the algorithm idea!)</p> <p>The background: I've been looking at the source code for Maxima, a computer algebra system and in particular at its code for the multiplication of two polynomials. Polynomials are represented in a "sparse format", so <code>x^5 + x^2 + 2</code> might appear as <code>(5 1 2 1 0 2)</code>, with descending exponents followed by their respective coefficients. To compute the product efficiently, what I really want to do is to collect together the degree zero terms, then the degree 1 terms etc. The current code avoids solving this problem by making a half-hearted stab at it for efficiency, then doing a sort of generic polynomial addition to deal with coefficients in an order it doesn't expect. I feel like we should be able to do better!</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload