{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 逻辑函数\n",
    "\n",
    "## 真值测试\n",
    "### numpy.all\n",
    "### numpy.any\n",
    "\n",
    "- `numpy.all(a, axis=None, out=None, keepdims=np._NoValue)` Test whether all array elements along a given axis evaluate to True.\n",
    "- `numpy.any(a, axis=None, out=None, keepdims=np._NoValue)` Test whether any array element along a given axis evaluates to True.\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "【例】\n",
    "```python\n",
    "import numpy as np\n",
    "\n",
    "a = np.array([0, 4, 5])\n",
    "b = np.copy(a)\n",
    "print(np.all(a == b))  # True\n",
    "print(np.any(a == b))  # True\n",
    "\n",
    "b[0] = 1\n",
    "print(np.all(a == b))  # False\n",
    "print(np.any(a == b))  # True\n",
    "\n",
    "print(np.all([1.0, np.nan]))  # True\n",
    "print(np.any([1.0, np.nan]))  # True\n",
    "\n",
    "a = np.eye(3)\n",
    "print(np.all(a, axis=0))  # [False False False]\n",
    "print(np.any(a, axis=0))  # [ True  True  True]\n",
    "```\n",
    "\n",
    "\n",
    "---\n",
    "## 数组内容\n",
    "### numpy.isnan\n",
    "\n",
    "- `numpy.isnan(x, *args, **kwargs)` Test element-wise for NaN and return result as a boolean array.\n",
    "\n",
    "\n",
    "【例】\n",
    "```python\n",
    "a=np.array([1,2,np.nan])\n",
    "print(np.isnan(a))\n",
    "#[False False  True]\n",
    "\n",
    "```\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "---\n",
    "## 逻辑运算\n",
    "### numpy.logical_not\n",
    "### numpy.logical_and\n",
    "### numpy.logical_or\n",
    "### numpy.logical_xor\n",
    "\n",
    "\n",
    "- `numpy.logical_not(x, *args, **kwargs)`Compute the truth value of NOT x element-wise.\n",
    "- `numpy.logical_and(x1, x2, *args, **kwargs)` Compute the truth value of x1 AND x2 element-wise.\n",
    "- `numpy.logical_or(x1, x2, *args, **kwargs)`Compute the truth value of x1 OR x2 element-wise.\n",
    "- `numpy.logical_xor(x1, x2, *args, **kwargs)`Compute the truth value of x1 XOR x2, element-wise.\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "```python\n",
    "【例】计算非x元素的真值。\n",
    "\n",
    "import numpy as np\n",
    "\n",
    "print(np.logical_not(3))  \n",
    "# False\n",
    "print(np.logical_not([True, False, 0, 1]))\n",
    "# [False  True  True False]\n",
    "\n",
    "x = np.arange(5)\n",
    "print(np.logical_not(x < 3))\n",
    "# [False False False  True  True]\n",
    "\n",
    "【例】计算x1 AND x2元素的真值。\n",
    "\n",
    "print(np.logical_and(True, False))  \n",
    "# False\n",
    "print(np.logical_and([True, False], [True, False]))\n",
    "# [ True False]\n",
    "print(np.logical_and(x > 1, x < 4))\n",
    "# [False False  True  True False]\n",
    "\n",
    "【例】逐元素计算x1 OR x2的真值。\n",
    "\n",
    "\n",
    "print(np.logical_or(True, False))\n",
    "# True\n",
    "print(np.logical_or([True, False], [False, False]))\n",
    "# [ True False]\n",
    "print(np.logical_or(x < 1, x > 3))\n",
    "# [ True False False False  True]\n",
    "\n",
    "【例】计算x1 XOR x2的真值，按元素计算。\n",
    "\n",
    "print(np.logical_xor(True, False))\n",
    "# True\n",
    "print(np.logical_xor([True, True, False, False], [True, False, True, False]))\n",
    "# [False  True  True False]\n",
    "print(np.logical_xor(x < 1, x > 3))\n",
    "# [ True False False False  True]\n",
    "print(np.logical_xor(0, np.eye(2)))\n",
    "# [[ True False]\n",
    "#  [False  True]]\n",
    "```\n",
    "\n",
    "\n",
    "## 对照\n",
    "### numpy.greater\n",
    "### numpy.greater_equal\n",
    "### numpy.equal\n",
    "### numpy.not_equal\n",
    "### numpy.less\n",
    "### numpy.less_equal\n",
    "\n",
    "\n",
    "- `numpy.greater(x1, x2, *args, **kwargs)` Return the truth value of (x1 > x2) element-wise.\n",
    "- `numpy.greater_equal(x1, x2, *args, **kwargs)` Return the truth value of (x1 >= x2) element-wise.\n",
    "- `numpy.equal(x1, x2, *args, **kwargs)` Return (x1 == x2) element-wise.\n",
    "- `numpy.not_equal(x1, x2, *args, **kwargs)` Return (x1 != x2) element-wise.\n",
    "- `numpy.less(x1, x2, *args, **kwargs)` Return the truth value of (x1 < x2) element-wise.\n",
    "- `numpy.less_equal(x1, x2, *args, **kwargs)` Return the truth value of (x1 =< x2) element-wise.\n",
    "\n",
    "\n",
    "\n",
    "【例】numpy对以上对照函数进行了运算符的重载。\n",
    "```python\n",
    "import numpy as np\n",
    "\n",
    "x = np.array([1, 2, 3, 4, 5, 6, 7, 8])\n",
    "\n",
    "y = x > 2\n",
    "print(y)\n",
    "print(np.greater(x, 2))\n",
    "# [False False  True  True  True  True  True  True]\n",
    "\n",
    "y = x >= 2\n",
    "print(y)\n",
    "print(np.greater_equal(x, 2))\n",
    "# [False  True  True  True  True  True  True  True]\n",
    "\n",
    "y = x == 2\n",
    "print(y)\n",
    "print(np.equal(x, 2))\n",
    "# [False  True False False False False False False]\n",
    "\n",
    "y = x != 2\n",
    "print(y)\n",
    "print(np.not_equal(x, 2))\n",
    "# [ True False  True  True  True  True  True  True]\n",
    "\n",
    "y = x < 2\n",
    "print(y)\n",
    "print(np.less(x, 2))\n",
    "# [ True False False False False False False False]\n",
    "\n",
    "y = x <= 2\n",
    "print(y)\n",
    "print(np.less_equal(x, 2))\n",
    "# [ True  True False False False False False False]\n",
    "```\n",
    "\n",
    "【例】\n",
    "```python\n",
    "import numpy as np\n",
    "\n",
    "x = np.array([[11, 12, 13, 14, 15],\n",
    "              [16, 17, 18, 19, 20],\n",
    "              [21, 22, 23, 24, 25],\n",
    "              [26, 27, 28, 29, 30],\n",
    "              [31, 32, 33, 34, 35]])\n",
    "y = x > 20\n",
    "print(y)\n",
    "print(np.greater(x, 20))\n",
    "# [[False False False False False]\n",
    "#  [False False False False False]\n",
    "#  [ True  True  True  True  True]\n",
    "#  [ True  True  True  True  True]\n",
    "#  [ True  True  True  True  True]]\n",
    "\n",
    "y = x >= 20\n",
    "print(y)\n",
    "print(np.greater_equal(x, 20))\n",
    "# [[False False False False False]\n",
    "#  [False False False False  True]\n",
    "#  [ True  True  True  True  True]\n",
    "#  [ True  True  True  True  True]\n",
    "#  [ True  True  True  True  True]]\n",
    "\n",
    "y = x == 20\n",
    "print(y)\n",
    "print(np.equal(x, 20))\n",
    "# [[False False False False False]\n",
    "#  [False False False False  True]\n",
    "#  [False False False False False]\n",
    "#  [False False False False False]\n",
    "#  [False False False False False]]\n",
    "\n",
    "y = x != 20\n",
    "print(y)\n",
    "print(np.not_equal(x, 20))\n",
    "# [[ True  True  True  True  True]\n",
    "#  [ True  True  True  True False]\n",
    "#  [ True  True  True  True  True]\n",
    "#  [ True  True  True  True  True]\n",
    "#  [ True  True  True  True  True]]\n",
    "\n",
    "\n",
    "y = x < 20\n",
    "print(y)\n",
    "print(np.less(x, 20))\n",
    "# [[ True  True  True  True  True]\n",
    "#  [ True  True  True  True False]\n",
    "#  [False False False False False]\n",
    "#  [False False False False False]\n",
    "#  [False False False False False]]\n",
    "\n",
    "y = x <= 20\n",
    "print(y)\n",
    "print(np.less_equal(x, 20))\n",
    "# [[ True  True  True  True  True]\n",
    "#  [ True  True  True  True  True]\n",
    "#  [False False False False False]\n",
    "#  [False False False False False]\n",
    "#  [False False False False False]]\n",
    "```\n",
    "\n",
    "【例】\n",
    "```python\n",
    "import numpy as np\n",
    "\n",
    "np.random.seed(20200611)\n",
    "x = np.array([[11, 12, 13, 14, 15],\n",
    "              [16, 17, 18, 19, 20],\n",
    "              [21, 22, 23, 24, 25],\n",
    "              [26, 27, 28, 29, 30],\n",
    "              [31, 32, 33, 34, 35]])\n",
    "\n",
    "y = np.random.randint(10, 40, [5, 5])\n",
    "print(y)\n",
    "# [[32 28 31 33 37]\n",
    "#  [23 37 37 30 29]\n",
    "#  [32 24 10 33 15]\n",
    "#  [27 17 10 36 16]\n",
    "#  [25 32 23 39 34]]\n",
    "\n",
    "z = x > y\n",
    "print(z)\n",
    "print(np.greater(x, y))\n",
    "# [[False False False False False]\n",
    "#  [False False False False False]\n",
    "#  [False False  True False  True]\n",
    "#  [False  True  True False  True]\n",
    "#  [ True False  True False  True]]\n",
    "\n",
    "z = x >= y\n",
    "print(z)\n",
    "print(np.greater_equal(x, y))\n",
    "# [[False False False False False]\n",
    "#  [False False False False False]\n",
    "#  [False False  True False  True]\n",
    "#  [False  True  True False  True]\n",
    "#  [ True  True  True False  True]]\n",
    "\n",
    "z = x == y\n",
    "print(z)\n",
    "print(np.equal(x, y))\n",
    "# [[False False False False False]\n",
    "#  [False False False False False]\n",
    "#  [False False False False False]\n",
    "#  [False False False False False]\n",
    "#  [False  True False False False]]\n",
    "\n",
    "z = x != y\n",
    "print(z)\n",
    "print(np.not_equal(x, y))\n",
    "# [[ True  True  True  True  True]\n",
    "#  [ True  True  True  True  True]\n",
    "#  [ True  True  True  True  True]\n",
    "#  [ True  True  True  True  True]\n",
    "#  [ True False  True  True  True]]\n",
    "\n",
    "z = x < y\n",
    "print(z)\n",
    "print(np.less(x, y))\n",
    "# [[ True  True  True  True  True]\n",
    "#  [ True  True  True  True  True]\n",
    "#  [ True  True False  True False]\n",
    "#  [ True False False  True False]\n",
    "#  [False False False  True False]]\n",
    "\n",
    "z = x <= y\n",
    "print(z)\n",
    "print(np.less_equal(x, y))\n",
    "# [[ True  True  True  True  True]\n",
    "#  [ True  True  True  True  True]\n",
    "#  [ True  True False  True False]\n",
    "#  [ True False False  True False]\n",
    "#  [False  True False  True False]]\n",
    "```\n",
    "\n",
    "【例】注意 numpy 的广播规则。\n",
    "```python\n",
    "import numpy as np\n",
    "\n",
    "x = np.array([[11, 12, 13, 14, 15],\n",
    "              [16, 17, 18, 19, 20],\n",
    "              [21, 22, 23, 24, 25],\n",
    "              [26, 27, 28, 29, 30],\n",
    "              [31, 32, 33, 34, 35]])\n",
    "\n",
    "np.random.seed(20200611)\n",
    "y = np.random.randint(10, 50, 5)\n",
    "\n",
    "print(y)\n",
    "# [32 37 30 24 10]\n",
    "\n",
    "z = x > y\n",
    "print(z)\n",
    "print(np.greater(x, y))\n",
    "# [[False False False False  True]\n",
    "#  [False False False False  True]\n",
    "#  [False False False False  True]\n",
    "#  [False False False  True  True]\n",
    "#  [False False  True  True  True]]\n",
    "\n",
    "z = x >= y\n",
    "print(z)\n",
    "print(np.greater_equal(x, y))\n",
    "# [[False False False False  True]\n",
    "#  [False False False False  True]\n",
    "#  [False False False  True  True]\n",
    "#  [False False False  True  True]\n",
    "#  [False False  True  True  True]]\n",
    "\n",
    "z = x == y\n",
    "print(z)\n",
    "print(np.equal(x, y))\n",
    "# [[False False False False False]\n",
    "#  [False False False False False]\n",
    "#  [False False False  True False]\n",
    "#  [False False False False False]\n",
    "#  [False False False False False]]\n",
    "\n",
    "z = x != y\n",
    "print(z)\n",
    "print(np.not_equal(x, y))\n",
    "# [[ True  True  True  True  True]\n",
    "#  [ True  True  True  True  True]\n",
    "#  [ True  True  True False  True]\n",
    "#  [ True  True  True  True  True]\n",
    "#  [ True  True  True  True  True]]\n",
    "\n",
    "z = x < y\n",
    "print(z)\n",
    "print(np.less(x, y))\n",
    "# [[ True  True  True  True False]\n",
    "#  [ True  True  True  True False]\n",
    "#  [ True  True  True False False]\n",
    "#  [ True  True  True False False]\n",
    "#  [ True  True False False False]]\n",
    "\n",
    "z = x <= y\n",
    "print(z)\n",
    "print(np.less_equal(x, y))\n",
    "# [[ True  True  True  True False]\n",
    "#  [ True  True  True  True False]\n",
    "#  [ True  True  True  True False]\n",
    "#  [ True  True  True False False]\n",
    "#  [ True  True False False False]]\n",
    "```\n",
    "\n",
    "### numpy.isclose\n",
    "### numpy.allclose\n",
    "- `numpy.isclose(a, b, rtol=1.e-5, atol=1.e-8, equal_nan=False)` Returns a boolean array where two arrays are element-wise equal within a tolerance.\n",
    "- `numpy.allclose(a, b, rtol=1.e-5, atol=1.e-8, equal_nan=False)` Returns True if two arrays are element-wise equal within a tolerance. \n",
    "\n",
    "`numpy.allclose()` 等价于 `numpy.all(isclose(a, b, rtol=rtol, atol=atol, equal_nan=equal_nan))`。\n",
    "\n",
    "The tolerance values are positive, typically very small numbers.  The relative difference (`rtol * abs(b)`) and the absolute difference `atol` are added together to compare against the absolute difference between `a` and `b`.\n",
    "\n",
    "判断是否为True的计算依据：\n",
    "\n",
    "```python\n",
    "np.absolute(a - b) <= (atol + rtol * absolute(b))\n",
    "\n",
    "- atol：float，绝对公差。\n",
    "- rtol：float，相对公差。\n",
    "```\n",
    "\n",
    "NaNs are treated as equal if they are in the same place and if `equal_nan=True`.  Infs are treated as equal if they are in the same place and of the same sign in both arrays.\n",
    "\n",
    "【例】比较两个数组是否可以认为相等。\n",
    "```python\n",
    "import numpy as np\n",
    "\n",
    "x = np.isclose([1e10, 1e-7], [1.00001e10, 1e-8])\n",
    "print(x)  # [ True False]\n",
    "\n",
    "x = np.allclose([1e10, 1e-7], [1.00001e10, 1e-8])\n",
    "print(x)  # False\n",
    "\n",
    "x = np.isclose([1e10, 1e-8], [1.00001e10, 1e-9])\n",
    "print(x)  # [ True  True]\n",
    "\n",
    "x = np.allclose([1e10, 1e-8], [1.00001e10, 1e-9])\n",
    "print(x)  # True\n",
    "\n",
    "x = np.isclose([1e10, 1e-8], [1.0001e10, 1e-9])\n",
    "print(x)  # [False  True]\n",
    "\n",
    "x = np.allclose([1e10, 1e-8], [1.0001e10, 1e-9])\n",
    "print(x)  # False\n",
    "\n",
    "x = np.isclose([1.0, np.nan], [1.0, np.nan])\n",
    "print(x)  # [ True False]\n",
    "\n",
    "x = np.allclose([1.0, np.nan], [1.0, np.nan])\n",
    "print(x)  # False\n",
    "\n",
    "x = np.isclose([1.0, np.nan], [1.0, np.nan], equal_nan=True)\n",
    "print(x)  # [ True  True]\n",
    "\n",
    "x = np.allclose([1.0, np.nan], [1.0, np.nan], equal_nan=True)\n",
    "print(x)  # True\n",
    "```\n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python35",
   "language": "python",
   "name": "python35"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.10"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {},
   "toc_section_display": true,
   "toc_window_display": true
  },
  "varInspector": {
   "cols": {
    "lenName": 16,
    "lenType": 16,
    "lenVar": 40
   },
   "kernels_config": {
    "python": {
     "delete_cmd_postfix": "",
     "delete_cmd_prefix": "del ",
     "library": "var_list.py",
     "varRefreshCmd": "print(var_dic_list())"
    },
    "r": {
     "delete_cmd_postfix": ") ",
     "delete_cmd_prefix": "rm(",
     "library": "var_list.r",
     "varRefreshCmd": "cat(var_dic_list()) "
    }
   },
   "types_to_exclude": [
    "module",
    "function",
    "builtin_function_or_method",
    "instance",
    "_Feature"
   ],
   "window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
