MCPcopy Create free account
hub / github.com/numpy/numpy / is_scalar_with_conversion

Function is_scalar_with_conversion

numpy/core/src/multiarray/number.c:425–499  ·  view source on GitHub ↗

* Determine if object is a scalar and if so, convert the object * to a double and place it in the out_exponent argument * and return the "scalar kind" as a result. If the object is * not a scalar (or if there are other error conditions) * return NPY_NOSCALAR, and out_exponent is undefined. */

Source from the content-addressed store, hash-verified

423 * return NPY_NOSCALAR, and out_exponent is undefined.
424 */
425static NPY_SCALARKIND
426is_scalar_with_conversion(PyObject *o2, double* out_exponent)
427{
428 PyObject *temp;
429 const int optimize_fpexps = 1;
430
431 if (PyLong_Check(o2)) {
432 long tmp = PyLong_AsLong(o2);
433 if (error_converting(tmp)) {
434 PyErr_Clear();
435 return NPY_NOSCALAR;
436 }
437 *out_exponent = (double)tmp;
438 return NPY_INTPOS_SCALAR;
439 }
440
441 if (optimize_fpexps && PyFloat_Check(o2)) {
442 *out_exponent = PyFloat_AsDouble(o2);
443 return NPY_FLOAT_SCALAR;
444 }
445
446 if (PyArray_Check(o2)) {
447 if ((PyArray_NDIM((PyArrayObject *)o2) == 0) &&
448 ((PyArray_ISINTEGER((PyArrayObject *)o2) ||
449 (optimize_fpexps && PyArray_ISFLOAT((PyArrayObject *)o2))))) {
450 temp = Py_TYPE(o2)->tp_as_number->nb_float(o2);
451 if (temp == NULL) {
452 return NPY_NOSCALAR;
453 }
454 *out_exponent = PyFloat_AsDouble(o2);
455 Py_DECREF(temp);
456 if (PyArray_ISINTEGER((PyArrayObject *)o2)) {
457 return NPY_INTPOS_SCALAR;
458 }
459 else { /* ISFLOAT */
460 return NPY_FLOAT_SCALAR;
461 }
462 }
463 }
464 else if (PyArray_IsScalar(o2, Integer) ||
465 (optimize_fpexps && PyArray_IsScalar(o2, Floating))) {
466 temp = Py_TYPE(o2)->tp_as_number->nb_float(o2);
467 if (temp == NULL) {
468 return NPY_NOSCALAR;
469 }
470 *out_exponent = PyFloat_AsDouble(o2);
471 Py_DECREF(temp);
472
473 if (PyArray_IsScalar(o2, Integer)) {
474 return NPY_INTPOS_SCALAR;
475 }
476 else { /* IsScalar(o2, Floating) */
477 return NPY_FLOAT_SCALAR;
478 }
479 }
480 else if (PyIndex_Check(o2)) {
481 PyObject* value = PyNumber_Index(o2);
482 Py_ssize_t val;

Callers 1

fast_scalar_powerFunction · 0.85

Calls 1

PyArray_NDIMFunction · 0.85

Tested by

no test coverage detected