## Minimum Scalar Product

### August 10, 2012

We begin with a function to compute the sum of the pair-wise products of two vectors, which we represent as lists:

`(define (scalar xs ys) (sum (map * xs ys)))`

Then the minimum scalar product is found when one vector is sorted in increasing order and the other vector is sorted in decreasing order; there is no need to generate and test all possible permutations:

`(define (msp xs ys) (scalar (sort < xs) (sort > ys)))`

Here are the two sample problems:

`> (msp '(1 3 -5) '(-2 4 1))`

-25

> (msp '(1 2 3 4 5) '(1 0 1 0 1))

6

We used `sum`

and `sort`

from the Standard Prelude. You can run the program at http://programmingpraxis.codepad.org/ucF1geyn.

Advertisements

Pages: 1 2

My Haskell solution (see http://bonsaicode.wordpress.com/2012/08/10/programming-praxis-minimum-scalar-product/ for a version with comments):

[…] Pages: 1 2 […]

Is there a simple proof of the assertion that this always works? I looked briefly but failed to find one on the web and I need to attend to other things. I did find some other pages that simply assert that this pair of permutations produces the minimum. I found a purported counter-example that failed to be a counter-example. And I wrote a test program, below, that seems to confirm the assertion by always returning an empty set of permutations (of the second vector) that produce an even smaller result. So I believe it but if there is a memorable proof, I’d like to know.

It’s not a proof, but a simple observation is that the

maximumscalar product is produced by multiplying the largest item from each vector, then the second largest, and so on; then theminimumscalar product is the opposite. I’ll ask at a couple of web sites I know.perl6 version

Here's an attempt at a proof:

Consider two vectors:

A = <a_1, a_2, ..., a_n> sorted highest to lowest, so a_i >= a_j if i < j; and

B = <b_1, b_2, ..., b_n> sorted lowest to highest, so b_i <= b_j if i < j.

The scalar product (A dot B) of these two vectors is:

a_1*b_1 + a_2*b_2 + ... + a_i*b_i + ...

Assume there exist a B' in which the positions of b_i and b_j have been

exchanged so that:

A dot B' < A dot B

... + a_i*b_j + ... + a_j*b_i + ... < ... + a_i*b_i + ... + a_j*b_j + ...

all the terms in the '...' are the same on both sides, so this reduces to:

a_i*b_j + a_j*b_i < a_i*b_i + a_j*b_j

which can be rearranged:

a_i*(b_j - b_i) < a_j*(b_j - b_i)

By definition of B, b_i <= b_j for i < j.

If b_i == b_j, then B' == B, and (A dot B') == (A dot B).

If b_i < b_j, then (b_j - b_i) > 0, so:

a_i < a_j

This contradicts that A is sorted so that a_i >= a_j for i < j.

Therefore, the assumption must be false.

[…] today’s Programming Praxis exercise, our goal is to calculate the minimum scalar product of two vectors. […]

My Python solution:

def min_scalar_product(v1, v2):

return sum([x * y for x, y in zip(sorted(v1), sorted(v2, reverse=True))])

Min Scalar Product

My try in Common lisp

I asked at http://www.reddit.com/r/math/comments/xzsi8/minimum_scalar_product/ and got this answer from Luonos. I think that’s pretty much the same as Mike’s proof.

Requires -std=c++0x option when compiled with g++.

A Go solution:

[…] Another post from Programming Praxis, this time we’re to figure out what is the minimum scalar product of two vectors. Basically, you want to rearrange two given lists a1, a2, …, an and b1, b2, …, bn such that a1b1 + a2b2 + … + anbn is minimized. […]