ASKSAGE: Sage Q&A Forum - RSS feedhttps://ask.sagemath.org/questions/Q&A Forum for SageenCopyright Sage, 2010. Some rights reserved under creative commons license.Fri, 01 Feb 2019 14:31:37 +0100Checking that all values in an array satisfy an inequalityhttps://ask.sagemath.org/question/45259/checking-that-all-values-in-an-array-satisfy-an-inequality/I would like to know how to check element-wise that every entry in an array is less than or equal to a particular number. It would seem that something like this would require a loop, but I am not sure. Could someone help me? Thank you in advanceFri, 01 Feb 2019 09:19:09 +0100https://ask.sagemath.org/question/45259/checking-that-all-values-in-an-array-satisfy-an-inequality/Answer by tmonteil for <p>I would like to know how to check element-wise that every entry in an array is less than or equal to a particular number. It would seem that something like this would require a loop, but I am not sure. Could someone help me? Thank you in advance</p>
https://ask.sagemath.org/question/45259/checking-that-all-values-in-an-array-satisfy-an-inequality/?answer=45266#post-id-45266You can use `any` of `all` as follows:
sage: L = [1,2,3,4,5]
sage: all(x < 10 for x in L)
True
sage: all(x < 3 for x in L)
False
sage: any(x < 3 for x in L)
TrueFri, 01 Feb 2019 14:31:37 +0100https://ask.sagemath.org/question/45259/checking-that-all-values-in-an-array-satisfy-an-inequality/?answer=45266#post-id-45266Answer by rburing for <p>I would like to know how to check element-wise that every entry in an array is less than or equal to a particular number. It would seem that something like this would require a loop, but I am not sure. Could someone help me? Thank you in advance</p>
https://ask.sagemath.org/question/45259/checking-that-all-values-in-an-array-satisfy-an-inequality/?answer=45265#post-id-45265No explicit loop is necessary; you can use a generator expression in combination with `all`. If `L` is the list and `c` is the particular number, you can do:
all(el <= c for el in L)Fri, 01 Feb 2019 14:28:21 +0100https://ask.sagemath.org/question/45259/checking-that-all-values-in-an-array-satisfy-an-inequality/?answer=45265#post-id-45265