2

Sorry for the title, but I can not think of a better one.

Starting informally, monotonicity says that if the number of votes for i is greater that the number of votes for j, the number of seats for i must be greater or equal to the number of seats for j.

Suppose the vector of votes is v = {70, 20, 10} and that we have three vectors of seats in a list, i.e., z = {{1, 0, 1}, {1, 1, 0}, {2, 0, 0}}.

I observe that:

70 > 20 and 1 > 0     70 > 20 and 1 = 1     70 > 20 and 2 > 0
70 > 10 and 1 = 1     70 > 10 and 1 = 1     70 > 10 and 2 > 0
20 > 10 and 0 < 1     20 > 10 and 1 > 0     10 > 10 and 1 = 0

which shows that only the first vector must be discarded because it does not respect monotonicity. I have tried the following command but obviously it does not fit.

Table[
  (v[[i]] < v[[j]] && z[[k, i]] <= z[[k, j]]) || 
  (v[[i]] > v[[j]] && z[[k, i]] >= z[[k, j]]), 
  {i, Length[z] - 1}, {j, i + 1, Length[z]}, {k, Length[z]}]

which gives

{{{True, True, True}, {True, True, True}}, {{False, True, True}}}

I need some help, perhaps an explanation on how to fix the bracketing in my poor attempt.

m_goldberg
  • 107,779
  • 16
  • 103
  • 257
cyrille.piatecki
  • 4,582
  • 13
  • 26

2 Answers2

2
Pick[z, And @@@ MapThread[UnsameQ, {Sign[Differences[z, {0, 1}]],
               ConstantArray[-Sign[Differences[v]], Length[z]]}, 2]]

{{1, 1, 0}, {2, 0, 0}}

The irregular brackets occurs because you let j run from i which is itself run over. I don't understand why you put {k, Length[z]} last rather than first

Coolwater
  • 20,257
  • 3
  • 35
  • 64
1
ClearAll[selectF, orderF]
orderF = Order @@@ Partition[#, 2, 1] &;
selectF[v_] := Function[{x}, Max@Abs[orderF@v - orderF@x] <= 1];

Pick[z, selectF[v] /@ z]

{{1, 1, 0}, {2, 0, 0}}

Pick[z, selectF[{70, 80, 10}] /@ z]

{{1, 1, 0}}

Pick[z, selectF[{10, 20, 30}] /@ z]

{}

kglr
  • 394,356
  • 18
  • 477
  • 896