1

Let us suppose that we have an array ordered. We want to check if the sub-arrays t and t_inv are following the same order as the imposed order inorder array.

Reading from left to right: the first element is [0,0] and so on until [0,3]. t_inv is inversed because the first to elements are swapped, they do not follow the ordering as in ordered.

# imposed order 
ordered = jnp.array([[0, 0],[0,1],[0,2],[0,3]])

# array with permuted order
t = jnp.array([[[0, 0],[0, 1], [0,3]]])
t_inv = jnp.array([[[0, 1],[0, 0], [0,3]]])

I expect the following:

 result: ordered(t) = 1, because "ordered"  
and ordered(t_inv) = -1, because "swapped/not ordered"

How can you check that the sub arrays are indeed part of the ordered array and ouput whether the order is correct or not?

4
  • I am having trouble understanding the problem setup. How exactly does the ordered array relate to t and t_inv? When you say "the first elements are swapped", which elements of t_inv are you talking about, and how do they relate to what is specified in ordered? Is it important that t and t_inv are three-dimensional arrays, or is that a typo?
    – jakevdp
    Commented May 25, 2022 at 17:46
  • Hello, thanks for the comment. Let us assume ordered = [e_1, e_2, e_3, e_4], we define the order where element e_1 lies in the first position, element e_2 in second etc. Then we are given an array t = [e_1, e_2, e_4] which is respecting the order (e_1 is before e_2 and before e_4) , and t_inv = [e_2, e_1, e_4] has e_2 and e_1 in swapped positions (wrt to ordered=[...] array) t and t_inv don't need to have the same number of elements.. we focus on the order (if e_1 is present, then it has to be always in the first position, then comes e_2 and so on)
    – relaxon
    Commented May 25, 2022 at 20:14
  • Ah, thanks. I was confused because before you edited the question, t and t_inv included pairs that didn't appear in ordered. But t and t_inv are still three-dimensional – is that your intention?
    – jakevdp
    Commented May 26, 2022 at 1:25
  • Yes, sorry I corrected the typo: t and t_inv need to be a sub-array of ordered. they don't necessarily need to have the same number of elements as ordered, but definitely their elements need to be contained in ordered
    – relaxon
    Commented May 26, 2022 at 8:47

1 Answer 1

1

You could do something like this:

import jax.numpy as jnp

# imposed order 
ordered = jnp.array([[0, 0],[0,1],[0,2],[0,3]])

# array with permuted order
t = jnp.array([[0, 0],[0, 1], [0,3]])
t_inv = jnp.array([[0, 1],[0, 0], [0,3]])


def is_sorted(t, ordered):
  index = jnp.where((t[:, None] == ordered).all(-1))[1]
  return jnp.where((index == jnp.sort(index)).all(), 1, -1)

print(is_sorted(t, ordered))
# 1
print(is_sorted(t_inv, ordered))
# -1

Scaling-wise, it might be faster to use a solution based on searchsorted, but the current implementation of jnp.searchsorted in JAX is relatively slow because XLA doesn't have any native binary search algorithm, so in practice the full pairwise comparison can often be more performant.

4
  • thank you! I will also take a look at the jnp.searchsorted function
    – relaxon
    Commented May 26, 2022 at 8:48
  • Wha if I the permuted order is e.g. c = jnp.array([[[0, 0],[0, 1], [0,1]], [[0, 1],[0, 0], [0,3]]]) , I concatenated t and t_inv. I tried using jax vmap over the inputs and replacing t[:, None] == ordered with t == ordered, however I get a ConcretizationTypeError:
    – relaxon
    Commented May 26, 2022 at 13:00
  • You would only get a ConcretizationTypeError if you attempted to do control flow using t == ordered within a transform like jit or vmap, or tried to pass it to a NumPy (not jax.numpy) function. I suspect your description here leaves out some changes you may have made.
    – jakevdp
    Commented May 26, 2022 at 13:58
  • yes, I am also using jax.vmap(is_sorted, in_axes=(0,None))(c, ordered)
    – relaxon
    Commented May 26, 2022 at 14:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.