3

Let x and y be arrays defined as Array[x, m], Array[y,n] respectively.

How to generate the following function of $x$ and $y$ for variable $k$:

x[1]<y[n-k+1] && x[2]<y[n-k+2] && ... && x[k]<y[n]

Assume that $k<n$ and $k<m$.

J. M.'s missing motivation
  • 124,525
  • 11
  • 401
  • 574
AIB
  • 239
  • 2
  • 10

3 Answers3

6

This can be done in many ways. A good approach uses e.g. MapThread, taking e.g. $n = 7, m = 6$ :

And @@ MapThread[ Less, {Array[y, 7][[3 ;;]], Reverse[Array[x, 6]][[2 ;;]]}] //
TraditionalForm
y(3) < x(5) && y(4) < x(4) && y(5) < x(3) && y(6) < x(2) && y(7) < x(1)

or Inner :

Inner[ Less, Array[y, 7][[3 ;;]], Reverse[Array[x, 6]][[2 ;;]], And]

Edit

As suggested in the comments let's add another general solution:

sol[k_, m_, n_] /; 1 <= k <= m <= n := 
    Inner[ Less, Take[ Array[y, n], {k, m}], 
                 Take[ Reverse @ Array[x, n], {k - 1, m - 1}], And]

so we have, e.g.

sol[4, 6, 7]
y[4] < x[5] && y[5] < x[4] && y[6] < x[3]
Artes
  • 57,212
  • 12
  • 157
  • 245
5

Another approach, using Table:

n = 15;

doIt[k_ /; k < n] := And @@ Table[x[i] < y[n - k + i], {i, 1, k}]

Then:

doIt[5]
x[1] < y[11] && x[2] < y[12] && x[3] < y[13] && x[4] < y[14] && x[5] < y[15]

Note: maybe you want to condition k on m as well

J. M.'s missing motivation
  • 124,525
  • 11
  • 401
  • 574
Pinguin Dirk
  • 6,519
  • 1
  • 26
  • 36
4

If

x = Symbol["x" <> ToString[#]] & /@ Range[10];
y = Symbol["y" <> ToString[#]] & /@ Range[6];
k=4;

And @@ Thread[x[[1 ;; k]] < y[[-k ;;]]]
(* x1 < y3 && x2 < y4 && x3 < y5 && x4 < y6 *)
b.gates.you.know.what
  • 20,103
  • 2
  • 43
  • 84