You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

logic_ops.py 25 kB

5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750
  1. # Copyright 2021 Huawei Technologies Co., Ltd
  2. #
  3. # Licensed under the Apache License, Version 2.0 (the "License");
  4. # you may not use this file except in compliance with the License.
  5. # You may obtain a copy of the License at
  6. #
  7. # http://www.apache.org/licenses/LICENSE-2.0
  8. #
  9. # Unless required by applicable law or agreed to in writing, software
  10. # distributed under the License is distributed on an "AS IS" BASIS,
  11. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. # See the License for the specific language governing permissions and
  13. # limitations under the License.
  14. # ============================================================================
  15. """logical operations, the function docs are adapted from Numpy API."""
  16. from ..ops import functional as F
  17. from ..ops.primitive import constexpr
  18. from ..common import dtype as mstype
  19. from ..common import Tensor
  20. from .._c_expression import typing
  21. from .math_ops import _apply_tensor_op, absolute
  22. from .array_creations import zeros, ones, empty
  23. from .utils import _check_input_tensor, _to_tensor, _isnan
  24. from .utils_const import _raise_type_error, _is_shape_empty, _infer_out_shape
  25. def not_equal(x1, x2, dtype=None):
  26. """
  27. Returns (x1 != x2) element-wise.
  28. Note:
  29. Numpy arguments `out`, `where`, `casting`, `order`, `subok`, `signature`,
  30. and `extobj` are not supported.
  31. Args:
  32. x1 (Tensor): First input tensor to be compared.
  33. x2 (Tensor): Second input tensor to be compared.
  34. dtype (:class:`mindspore.dtype`, optional): defaults to None. Overrides the dtype of the
  35. output Tensor.
  36. Returns:
  37. Tensor or scalar, element-wise comparison of `x1` and `x2`. Typically of type
  38. bool, unless `dtype` is passed. This is a scalar if both `x1` and `x2` are
  39. scalars.
  40. Raises:
  41. TypeError: If the input is not a tensor.
  42. Supported Platforms:
  43. ``Ascend`` ``GPU`` ``CPU``
  44. Examples:
  45. >>> import mindspore.numpy as np
  46. >>> a = np.asarray([1, 2])
  47. >>> b = np.asarray([[1, 3],[1, 4]])
  48. >>> print(np.not_equal(a, b))
  49. >>> [[False True]
  50. [False True]]
  51. """
  52. _check_input_tensor(x1, x2)
  53. return _apply_tensor_op(F.not_equal, x1, x2, dtype=dtype)
  54. def less_equal(x1, x2, dtype=None):
  55. """
  56. Returns the truth value of ``(x1 <= x2)`` element-wise.
  57. Note:
  58. Numpy arguments `out`, `where`, `casting`, `order`, `subok`, `signature`,
  59. and `extobj` are not supported.
  60. Args:
  61. x1 (Tensor): Input array.
  62. x2 (Tensor): Input array. If ``x1.shape != x2.shape``, they must be
  63. broadcastable to a common shape (which becomes the shape of the output).
  64. dtype (:class:`mindspore.dtype`, optional): defaults to None. Overrides the dtype of the
  65. output Tensor.
  66. Returns:
  67. Tensor or scalar, element-wise comparison of `x1` and `x2`. Typically of type
  68. bool, unless `dtype` is passed. This is a scalar if both `x1` and `x2` are
  69. scalars.
  70. Supported Platforms:
  71. ``Ascend`` ``GPU`` ``CPU``
  72. Examples:
  73. >>> import mindspore.numpy as np
  74. >>> output = np.less_equal(np.array([4, 2, 1]), np.array([2, 2, 2]))
  75. >>> print(output)
  76. [False True True]
  77. """
  78. _check_input_tensor(x1, x2)
  79. return _apply_tensor_op(F.tensor_le, x1, x2, dtype=dtype)
  80. def less(x1, x2, dtype=None):
  81. """
  82. Returns the truth value of ``(x1 < x2)`` element-wise.
  83. Note:
  84. Numpy arguments `out`, `where`, `casting`, `order`, `subok`, `signature`,
  85. and `extobj` are not supported.
  86. Args:
  87. x1 (Tensor): input array.
  88. x2 (Tensor): Input array. If ``x1.shape != x2.shape``, they must be
  89. broadcastable to a common shape (which becomes the shape of the output).
  90. dtype (:class:`mindspore.dtype`, optional): defaults to None. Overrides the dtype of the
  91. output Tensor.
  92. Returns:
  93. Tensor or scalar, element-wise comparison of `x1` and `x2`. Typically of type
  94. bool, unless `dtype` is passed. This is a scalar if both `x1` and `x2` are
  95. scalars.
  96. Supported Platforms:
  97. ``Ascend`` ``GPU`` ``CPU``
  98. Examples:
  99. >>> import mindspore.numpy as np
  100. >>> output = np.less(np.array([1, 2]), np.array([2, 2]))
  101. >>> print(output)
  102. [ True False]
  103. """
  104. return _apply_tensor_op(F.tensor_lt, x1, x2, dtype=dtype)
  105. def greater_equal(x1, x2, dtype=None):
  106. """
  107. Returns the truth value of ``(x1 >= x2)`` element-wise.
  108. Note:
  109. Numpy arguments `out`, `where`, `casting`, `order`, `subok`, `signature`,
  110. and `extobj` are not supported.
  111. Args:
  112. x1 (Tensor): Input array.
  113. x2 (Tensor): Input array. If ``x1.shape != x2.shape``, they must be
  114. broadcastable to a common shape (which becomes the shape of the output).
  115. dtype (:class:`mindspore.dtype`, optional): defaults to None. Overrides the dtype of the
  116. output Tensor.
  117. Returns:
  118. Tensor or scalar, element-wise comparison of `x1` and `x2`. Typically of type
  119. bool, unless `dtype` is passed. This is a scalar if both `x1` and `x2` are
  120. scalars.
  121. Supported Platforms:
  122. ``Ascend`` ``GPU`` ``CPU``
  123. Examples:
  124. >>> import mindspore.numpy as np
  125. >>> output = np.greater_equal(np.array([4, 2, 1]), np.array([2, 2, 2]))
  126. >>> print(output)
  127. [ True True False]
  128. """
  129. return _apply_tensor_op(F.tensor_ge, x1, x2, dtype=dtype)
  130. def greater(x1, x2, dtype=None):
  131. """
  132. Returns the truth value of ``(x1 > x2)`` element-wise.
  133. Note:
  134. Numpy arguments `out`, `where`, `casting`, `order`, `subok`, `signature`,
  135. and `extobj` are not supported.
  136. Args:
  137. x1 (Tensor): Input array.
  138. x2 (Tensor): Input array. If ``x1.shape != x2.shape``, they must be
  139. broadcastable to a common shape (which becomes the shape of the output).
  140. dtype (:class:`mindspore.dtype`, optional): defaults to None. Overrides the dtype of the
  141. output Tensor.
  142. Returns:
  143. Tensor or scalar, element-wise comparison of `x1` and `x2`. Typically of type
  144. bool, unless `dtype` is passed. This is a scalar if both `x1` and `x2` are
  145. scalars.
  146. Supported Platforms:
  147. ``Ascend`` ``GPU`` ``CPU``
  148. Examples:
  149. >>> import mindspore.numpy as np
  150. >>> output = np.greater(np.array([4, 2]), np.array([2, 2]))
  151. >>> print(output)
  152. [ True False]
  153. """
  154. return _apply_tensor_op(F.tensor_gt, x1, x2, dtype=dtype)
  155. def equal(x1, x2, dtype=None):
  156. """
  157. Returns the truth value of ``(x1 == x2)`` element-wise.
  158. Note:
  159. Numpy arguments `out`, `where`, `casting`, `order`, `subok`, `signature`,
  160. and `extobj` are not supported.
  161. Args:
  162. x1 (Tensor): Input array.
  163. x2 (Tensor): Input array. If ``x1.shape != x2.shape``, they must be
  164. broadcastable to a common shape (which becomes the shape of the output).
  165. dtype (:class:`mindspore.dtype`, optional): defaults to None. Overrides the dtype of the
  166. output Tensor.
  167. Returns:
  168. Tensor or scalar, element-wise comparison of `x1` and `x2`. Typically of type
  169. bool, unless `dtype` is passed. This is a scalar if both `x1` and `x2` are
  170. scalars.
  171. Supported Platforms:
  172. ``Ascend`` ``GPU`` ``CPU``
  173. Examples:
  174. >>> import mindspore.numpy as np
  175. >>> output = np.equal(np.array([0, 1, 3]), np.arange(3))
  176. >>> print(output)
  177. [ True True False]
  178. """
  179. return _apply_tensor_op(F.equal, x1, x2, dtype=dtype)
  180. def isfinite(x, dtype=None):
  181. """
  182. Tests element-wise for finiteness (not infinity or not Not a Number).
  183. The result is returned as a boolean array.
  184. Note:
  185. Numpy arguments `out`, `where`, `casting`, `order`, `subok`, `signature`,
  186. and `extobj` are not supported.
  187. On GPU, the supported dtypes are np.float16, and np.float32.
  188. Args:
  189. x (Tensor): Input values.
  190. dtype (:class:`mindspore.dtype`, optional): defaults to None. Overrides the dtype of the
  191. output Tensor.
  192. Returns:
  193. Tensor or scalar, true where `x` is not positive infinity, negative infinity,
  194. or NaN; false otherwise. This is a scalar if `x` is a scalar.
  195. Supported Platforms:
  196. ``Ascend`` ``GPU`` ``CPU``
  197. Examples:
  198. >>> import mindspore.numpy as np
  199. >>> output = np.isfinite(np.array([np.inf, 1., np.nan]).astype('float32'))
  200. >>> print(output)
  201. [False True False]
  202. >>> output = np.isfinite(np.log(np.array(-1.).astype('float32')))
  203. >>> print(output)
  204. False
  205. """
  206. return _apply_tensor_op(F.isfinite, x, dtype=dtype)
  207. def isnan(x, dtype=None):
  208. """
  209. Tests element-wise for NaN and return result as a boolean array.
  210. Note:
  211. Numpy arguments `out`, `where`, `casting`, `order`, `subok`, `signature`,
  212. and `extobj` are not supported.
  213. Only np.float32 is currently supported.
  214. Args:
  215. x (Tensor): Input values.
  216. dtype (:class:`mindspore.dtype`, optional): defaults to None. Overrides the dtype of the
  217. output Tensor.
  218. Returns:
  219. Tensor or scalar, true where `x` is NaN, false otherwise. This is a scalar if
  220. `x` is a scalar.
  221. Supported Platforms:
  222. ``GPU`` ``CPU``
  223. Examples:
  224. >>> import mindspore.numpy as np
  225. >>> output = np.isnan(np.array(np.nan, np.float32))
  226. >>> print(output)
  227. True
  228. >>> output = np.isnan(np.array(np.inf, np.float32))
  229. >>> print(output)
  230. False
  231. """
  232. return _apply_tensor_op(_isnan, x, dtype=dtype)
  233. def _isinf(x):
  234. """Computes isinf without applying keyword arguments."""
  235. shape = F.shape(x)
  236. zeros_tensor = zeros(shape, mstype.float32)
  237. ones_tensor = ones(shape, mstype.float32)
  238. not_inf = F.isfinite(x)
  239. is_nan = _isnan(x)
  240. res = F.select(not_inf, zeros_tensor, ones_tensor)
  241. res = F.select(is_nan, zeros_tensor, res)
  242. return F.cast(res, mstype.bool_)
  243. def isinf(x, dtype=None):
  244. """
  245. Tests element-wise for positive or negative infinity.
  246. Returns a boolean array of the same shape as `x`, True where ``x == +/-inf``, otherwise False.
  247. Note:
  248. Numpy arguments `out`, `where`, `casting`, `order`, `subok`, `signature`,
  249. and `extobj` are not supported.
  250. Only np.float32 is currently supported.
  251. Args:
  252. x (Tensor): Input values.
  253. dtype (:class:`mindspore.dtype`, optional): defaults to None. Overrides the dtype of the
  254. output Tensor.
  255. Returns:
  256. Tensor or scalar, true where `x` is positive or negative infinity, false
  257. otherwise. This is a scalar if `x` is a scalar.
  258. Supported Platforms:
  259. ``GPU`` ``CPU``
  260. Examples:
  261. >>> import mindspore.numpy as np
  262. >>> output = np.isinf(np.array(np.inf, np.float32))
  263. >>> print(output)
  264. True
  265. >>> output = np.isinf(np.array([np.inf, -np.inf, 1.0, np.nan], np.float32))
  266. >>> print(output)
  267. [ True True False False]
  268. """
  269. return _apply_tensor_op(_isinf, x, dtype=dtype)
  270. def _is_sign_inf(x, fn):
  271. """Tests element-wise for inifinity with sign."""
  272. shape = F.shape(x)
  273. zeros_tensor = zeros(shape, mstype.float32)
  274. ones_tensor = ones(shape, mstype.float32)
  275. not_inf = F.isfinite(x)
  276. is_sign = fn(x, zeros_tensor)
  277. res = F.select(not_inf, zeros_tensor, ones_tensor)
  278. res = F.select(is_sign, res, zeros_tensor)
  279. return F.cast(res, mstype.bool_)
  280. def isposinf(x):
  281. """
  282. Tests element-wise for positive infinity, returns result as bool array.
  283. Note:
  284. Numpy argument `out` is not supported.
  285. Only np.float32 is currently supported.
  286. Args:
  287. x (Tensor): Input values.
  288. Returns:
  289. Tensor or scalar, true where `x` is positive infinity, false otherwise.
  290. This is a scalar if `x` is a scalar.
  291. Raises:
  292. TypeError: if the input is not a tensor.
  293. Supported Platforms:
  294. ``GPU`` ``CPU``
  295. Examples:
  296. >>> import mindspore.numpy as np
  297. >>> output = np.isposinf(np.array([-np.inf, 0., np.inf], np.float32))
  298. >>> print(output)
  299. [False False True]
  300. """
  301. _check_input_tensor(x)
  302. return _is_sign_inf(x, F.tensor_gt)
  303. def isneginf(x):
  304. """
  305. Tests element-wise for negative infinity, returns result as bool array.
  306. Note:
  307. Numpy argument `out` is not supported.
  308. Only np.float32 is currently supported.
  309. Args:
  310. x (Tensor): Input values.
  311. Returns:
  312. Tensor or scalar, true where `x` is negative infinity, false otherwise.
  313. This is a scalar if `x` is a scalar.
  314. Raises:
  315. TypeError: if the input is not a tensor.
  316. Supported Platforms:
  317. ``GPU`` ``CPU``
  318. Examples:
  319. >>> import mindspore.numpy as np
  320. >>> output = np.isneginf(np.array([-np.inf, 0., np.inf], np.float32))
  321. >>> print(output)
  322. [ True False False]
  323. """
  324. return _is_sign_inf(x, F.tensor_lt)
  325. @constexpr
  326. def _isscalar(x):
  327. """Returns True if x is a scalar type"""
  328. return isinstance(x, (typing.Number, typing.Int, typing.UInt, typing.Float,
  329. typing.Bool, typing.String))
  330. def isscalar(element):
  331. """
  332. Returns True if the type of element is a scalar type.
  333. Note:
  334. Only object types recognized by the mindspore parser are supported,
  335. which includes objects, types, methods and functions defined within
  336. the scope of mindspore. Other built-in types are not supported.
  337. Args:
  338. element (any): Input argument, can be of any type and shape.
  339. Returns:
  340. Boolean, True if `element` is a scalar type, False if it is not.
  341. Raises:
  342. TypeError: if the type of `element` is not supported by mindspore parser.
  343. Supported Platforms:
  344. ``Ascend`` ``GPU`` ``CPU``
  345. Examples:
  346. >>> import mindspore.numpy as np
  347. >>> output = np.isscalar(3.1)
  348. >>> print(output)
  349. True
  350. >>> output = np.isscalar(np.array(3.1))
  351. >>> print(output)
  352. False
  353. >>> output = np.isscalar(False)
  354. >>> print(output)
  355. True
  356. >>> output = np.isscalar('numpy')
  357. >>> print(output)
  358. True
  359. """
  360. obj_type = F.typeof(element)
  361. return not isinstance(obj_type, Tensor) and _isscalar(obj_type)
  362. def isclose(a, b, rtol=1e-05, atol=1e-08, equal_nan=False):
  363. """
  364. Returns a boolean tensor where two tensors are element-wise equal within a tolerance.
  365. The tolerance values are positive, typically very small numbers. The relative
  366. difference (:math:`rtol * abs(b)`) and the absolute difference `atol` are added together
  367. to compare against the absolute difference between `a` and `b`.
  368. Note:
  369. For finite values, isclose uses the following equation to test whether two
  370. floating point values are equivalent.
  371. :math:`absolute(a - b) <= (atol + rtol * absolute(b))`
  372. Args:
  373. a (Union[Tensor, list, tuple]): Input first tensor to compare.
  374. b (Union[Tensor, list, tuple]): Input second tensor to compare.
  375. rtol (Number): The relative tolerance parameter (see Note).
  376. atol (Number): The absolute tolerance parameter (see Note).
  377. equal_nan (bool): Whether to compare ``NaN`` as equal. If True, ``NaN`` in
  378. `a` will be considered equal to ``NaN`` in `b` in the output tensor.
  379. Returns:
  380. A ``bool`` tensor of where `a` and `b` are equal within the given tolerance.
  381. Raises:
  382. TypeError: If inputs have types not specified above.
  383. Supported Platforms:
  384. ``GPU`` ``CPU``
  385. Examples:
  386. >>> a = np.array([0,1,2,float('inf'),float('inf'),float('nan')])
  387. >>> b = np.array([0,1,-2,float('-inf'),float('inf'),float('nan')])
  388. >>> print(np.isclose(a, b))
  389. [ True True False False True False]
  390. >>> print(np.isclose(a, b, equal_nan=True))
  391. [ True True False False True True]
  392. """
  393. a, b = _to_tensor(a, b)
  394. if not isinstance(rtol, (int, float, bool)) or not isinstance(atol, (int, float, bool)):
  395. _raise_type_error("rtol and atol are expected to be numbers.")
  396. if not isinstance(equal_nan, bool):
  397. _raise_type_error("equal_nan is expected to be bool.")
  398. if _is_shape_empty(a.shape) or _is_shape_empty(b.shape):
  399. return empty(_infer_out_shape(a.shape, b.shape), dtype=mstype.bool_)
  400. rtol = _to_tensor(rtol).astype("float32")
  401. atol = _to_tensor(atol).astype("float32")
  402. res = absolute(a - b) <= (atol + rtol * absolute(b))
  403. # infs are treated as equal
  404. a_posinf = isposinf(a)
  405. b_posinf = isposinf(b)
  406. a_neginf = isneginf(a)
  407. b_neginf = isneginf(b)
  408. same_inf = F.logical_or(F.logical_and(a_posinf, b_posinf), F.logical_and(a_neginf, b_neginf))
  409. diff_inf = F.logical_or(F.logical_and(a_posinf, b_neginf), F.logical_and(a_neginf, b_posinf))
  410. res = F.logical_and(F.logical_or(res, same_inf), F.logical_not(diff_inf))
  411. both_nan = F.logical_and(_isnan(a), _isnan(b))
  412. if equal_nan:
  413. res = F.logical_or(both_nan, res)
  414. else:
  415. res = F.logical_and(F.logical_not(both_nan), res)
  416. return res
  417. def in1d(ar1, ar2, invert=False):
  418. """
  419. Tests whether each element of a 1-D array is also present in a second array.
  420. Returns a boolean array the same length as `ar1` that is True where an element
  421. of `ar1` is in `ar2` and False otherwise.
  422. Note:
  423. Numpy argument `assume_unique` is not supported since the implementation does
  424. not rely on the uniqueness of the input arrays.
  425. Args:
  426. ar1 (array_like): Input array with shape `(M,)`.
  427. ar2 (array_like): The values against which to test each value of `ar1`.
  428. invert (boolean, optional): If True, the values in the returned array are
  429. inverted (that is, False where an element of `ar1` is in `ar2` and True
  430. otherwise). Default is False.
  431. Returns:
  432. Tensor, with shape `(M,)`. The values ``ar1[in1d]`` are in `ar2`.
  433. Supported Platforms:
  434. ``Ascend`` ``GPU`` ``CPU``
  435. Examples:
  436. >>> test = np.array([0, 1, 2, 5, 0])
  437. >>> states = [0, 2]
  438. >>> mask = np.in1d(test, states)
  439. >>> print(mask)
  440. [ True False True False True]
  441. >>> mask = np.in1d(test, states, invert=True)
  442. >>> print(mask)
  443. [False True False True False]
  444. """
  445. ar1, ar2 = _to_tensor(ar1, ar2)
  446. ar1 = F.expand_dims(ar1.ravel(), -1)
  447. ar2 = ar2.ravel()
  448. included = F.equal(ar1, ar2)
  449. # F.reduce_sum only supports float
  450. res = F.reduce_sum(included.astype(mstype.float32), -1).astype(mstype.bool_)
  451. if invert:
  452. res = F.logical_not(res)
  453. return res
  454. def isin(element, test_elements, invert=False):
  455. """
  456. Calculates element in `test_elements`, broadcasting over `element` only. Returns a
  457. boolean array of the same shape as `element` that is True where an element of
  458. `element` is in `test_elements` and False otherwise.
  459. Note:
  460. Numpy argument `assume_unique` is not supported since the implementation does
  461. not rely on the uniqueness of the input arrays.
  462. Args:
  463. element (array_like): Input array.
  464. test_elements (array_like): The values against which to test each value of
  465. `element`.
  466. invert (boolean, optional): If True, the values in the returned array are
  467. inverted, as if calculating `element` not in `test_elements`. Default is False.
  468. Returns:
  469. Tensor, has the same shape as `element`. The values ``element[isin]`` are in
  470. `test_elements`.
  471. Supported Platforms:
  472. ``Ascend`` ``GPU`` ``CPU``
  473. Examples:
  474. >>> element = 2*np.arange(4).reshape((2, 2))
  475. >>> test_elements = [1, 2, 4, 8]
  476. >>> mask = np.isin(element, test_elements)
  477. >>> print(mask)
  478. [[False True]
  479. [ True False]]
  480. >>> mask = np.isin(element, test_elements, invert=True)
  481. >>> print(mask)
  482. [[ True False]
  483. [False True]]
  484. """
  485. res = in1d(element, test_elements, invert=invert)
  486. return F.reshape(res, F.shape(element))
  487. def logical_not(a, dtype=None):
  488. """
  489. Computes the truth value of NOT `a` element-wise.
  490. Note:
  491. Numpy arguments `out`, `where`, `casting`, `order`, `subok`, `signature`, and `extobj` are
  492. not supported.
  493. Args:
  494. a (Tensor): The input tensor whose dtype is bool.
  495. dtype (:class:`mindspore.dtype`, optional): Default: :class:`None`. Overrides the dtype of the
  496. output Tensor.
  497. Returns:
  498. Tensor or scalar.
  499. Boolean result with the same shape as `a` of the NOT operation on elements of `a`.
  500. This is a scalar if `a` is a scalar.
  501. Raises:
  502. TypeError: if the input is not a tensor or its dtype is not bool.
  503. Supported Platforms:
  504. ``Ascend`` ``GPU`` ``CPU``
  505. Examples:
  506. >>> import mindspore.numpy as np
  507. >>> a = np.array([True, False])
  508. >>> output = np.logical_not(a)
  509. >>> print(output)
  510. [False True]
  511. """
  512. return _apply_tensor_op(F.logical_not, a, dtype=dtype)
  513. def logical_or(x1, x2, dtype=None):
  514. """
  515. Computes the truth value of `x1` OR `x2` element-wise.
  516. Note:
  517. Numpy arguments `out`, `where`, `casting`, `order`, `subok`, `signature`,
  518. and `extobj` are not supported.
  519. Args:
  520. x1 (Tensor): Input tensor.
  521. x2 (Tensor): Input tensor. If ``x1.shape != x2.shape``, they must be
  522. broadcastable to a common shape (which becomes the shape of the output).
  523. dtype (:class:`mindspore.dtype`, optional): defaults to None. Overrides the dtype of the
  524. output Tensor.
  525. Returns:
  526. Tensor or scalar, element-wise comparison of `x1` and `x2`. Typically of type
  527. bool, unless ``dtype=object`` is passed. This is a scalar if both `x1` and `x2` are
  528. scalars.
  529. Supported Platforms:
  530. ``Ascend`` ``GPU`` ``CPU``
  531. Examples:
  532. >>> import mindspore.numpy as np
  533. >>> x1 = np.array([True, False])
  534. >>> x2 = np.array([False, True])
  535. >>> output = np.logical_or(x1, x2)
  536. >>> print(output)
  537. [ True True]
  538. """
  539. return _apply_tensor_op(F.logical_or, x1, x2, dtype=dtype)
  540. def logical_and(x1, x2, dtype=None):
  541. """
  542. Computes the truth value of `x1` AND `x2` element-wise.
  543. Note:
  544. Numpy arguments `out`, `where`, `casting`, `order`, `subok`, `signature`,
  545. and `extobj` are not supported.
  546. Args:
  547. x1 (Tensor): Input tensor.
  548. x2 (Tensor): Input tensor. If ``x1.shape != x2.shape``, they must be
  549. broadcastable to a common shape (which becomes the shape of the output).
  550. dtype (:class:`mindspore.dtype`, optional): defaults to None. Overrides the dtype of the
  551. output Tensor.
  552. Returns:
  553. Tensor or scalar.
  554. Boolean result of the logical AND operation applied to the elements of `x1` and `x2`;
  555. the shape is determined by broadcasting. This is a scalar if both `x1` and `x2` are scalars.
  556. Supported Platforms:
  557. ``Ascend`` ``GPU`` ``CPU``
  558. Examples:
  559. >>> import mindspore.numpy as np
  560. >>> x1 = np.array([True, False])
  561. >>> x2 = np.array([False, False])
  562. >>> output = np.logical_and(x1, x2)
  563. >>> print(output)
  564. [False False]
  565. """
  566. return _apply_tensor_op(F.logical_and, x1, x2, dtype=dtype)
  567. def logical_xor(x1, x2, dtype=None):
  568. """
  569. Computes the truth value of `x1` XOR `x2`, element-wise.
  570. Note:
  571. Numpy arguments `out`, `where`, `casting`, `order`, `subok`, `signature`,
  572. and `extobj` are not supported.
  573. Args:
  574. x1 (Tensor): Input tensor.
  575. x2 (Tensor): Input tensor. If ``x1.shape != x2.shape``, they must be
  576. broadcastable to a common shape (which becomes the shape of the output).
  577. dtype (:class:`mindspore.dtype`, optional): defaults to None. Overrides the dtype of the
  578. output Tensor.
  579. Returns:
  580. Tensor or scalar.
  581. Boolean result of the logical AND operation applied to the elements of `x1` and `x2`;
  582. the shape is determined by broadcasting. This is a scalar if both `x1` and `x2` are scalars.
  583. Supported Platforms:
  584. ``Ascend`` ``GPU`` ``CPU``
  585. Examples:
  586. >>> import mindspore.numpy as np
  587. >>> x1 = np.array([True, False])
  588. >>> x2 = np.array([False, False])
  589. >>> output = np.logical_xor(x1, x2)
  590. >>> print(output)
  591. [True False]
  592. """
  593. _check_input_tensor(x1)
  594. _check_input_tensor(x2)
  595. y1 = F.logical_or(x1, x2)
  596. y2 = F.logical_or(F.logical_not(x1), F.logical_not(x2))
  597. return _apply_tensor_op(F.logical_and, y1, y2, dtype=dtype)