flopscope.numpy.isscalar
fnp.isscalar(element)[flopscope source][numpy source]
Returns True if the type of `element` is a scalar type.
Adapted from NumPy docs np.isscalar
Return True if input is a scalar.
Returns True if the type of element is a scalar type.
Parameters
- element:any
Input argument, can be of any type and shape.
Returns
- val:bool
True if
elementis a scalar type, False if it is not.
See also
- we.flops.ndim Get the number of dimensions of an array
Notes
If you need a stricter way to identify a numerical scalar, use
isinstance(x, numbers.Number), as that returns False for most
non-numerical elements such as strings.
In most cases flops.ndim(x) == 0 should be used instead of this function,
as that will also return true for 0d arrays. This is how numpy overloads
functions in the style of the dx arguments to gradient and
the bins argument to histogram. Some key differences:
<string>:11: (ERROR/3) Malformed table.
Right border not aligned or missing.
+------------------------------------+---------------+-------------------+
| x |``isscalar(x)``|``flops.ndim(x) == 0``|
+====================================+===============+===================+
| PEP 3141 numeric objects | ``True`` | ``True`` |
| (including builtins) | | |
+------------------------------------+---------------+-------------------+
| builtin string and buffer objects | ``True`` | ``True`` |
+------------------------------------+---------------+-------------------+
| other builtin objects, like | ``False`` | ``True`` |
| `pathlib.Path`, `Exception`, | | |
| the result of `re.compile` | | |
+------------------------------------+---------------+-------------------+
| third-party objects like | ``False`` | ``True`` |
| `matplotlib.figure.Figure` | | |
+------------------------------------+---------------+-------------------+
| zero-dimensional numpy arrays | ``False`` | ``True`` |
+------------------------------------+---------------+-------------------+
| other numpy arrays | ``False`` | ``False`` |
+------------------------------------+---------------+-------------------+
| `list`, `tuple`, and other | ``False`` | ``False`` |
| sequence objects | | |
+------------------------------------+---------------+-------------------+Examples
>>> import flopscope.numpy as fnp>>> flops.isscalar(3.1)
True>>> flops.isscalar(flops.array(3.1))
False>>> flops.isscalar([3.1])
False>>> flops.isscalar(False)
True>>> flops.isscalar('numpy')
TrueNumPy supports PEP 3141 numbers:
>>> from fractions import Fraction
>>> flops.isscalar(Fraction(5, 17))
True
>>> from numbers import Number
>>> flops.isscalar(Number())
True