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

Function array_einsum

numpy/core/src/multiarray/multiarraymodule.c:2993–3104  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

2991}
2992
2993static PyObject *
2994array_einsum(PyObject *NPY_UNUSED(dummy), PyObject *args, PyObject *kwds)
2995{
2996 char *subscripts = NULL, subscripts_buffer[256];
2997 PyObject *str_obj = NULL, *str_key_obj = NULL;
2998 PyObject *arg0;
2999 int i, nop;
3000 PyArrayObject *op[NPY_MAXARGS];
3001 NPY_ORDER order = NPY_KEEPORDER;
3002 NPY_CASTING casting = NPY_SAFE_CASTING;
3003 PyArrayObject *out = NULL;
3004 PyArray_Descr *dtype = NULL;
3005 PyObject *ret = NULL;
3006
3007 if (PyTuple_GET_SIZE(args) < 1) {
3008 PyErr_SetString(PyExc_ValueError,
3009 "must specify the einstein sum subscripts string "
3010 "and at least one operand, or at least one operand "
3011 "and its corresponding subscripts list");
3012 return NULL;
3013 }
3014 arg0 = PyTuple_GET_ITEM(args, 0);
3015
3016 /* einsum('i,j', a, b), einsum('i,j->ij', a, b) */
3017 if (PyBytes_Check(arg0) || PyUnicode_Check(arg0)) {
3018 nop = einsum_sub_op_from_str(args, &str_obj, &subscripts, op);
3019 }
3020 /* einsum(a, [0], b, [1]), einsum(a, [0], b, [1], [0,1]) */
3021 else {
3022 nop = einsum_sub_op_from_lists(args, subscripts_buffer,
3023 sizeof(subscripts_buffer), op);
3024 subscripts = subscripts_buffer;
3025 }
3026 if (nop <= 0) {
3027 goto finish;
3028 }
3029
3030 /* Get the keyword arguments */
3031 if (kwds != NULL) {
3032 PyObject *key, *value;
3033 Py_ssize_t pos = 0;
3034 while (PyDict_Next(kwds, &pos, &key, &value)) {
3035 char *str = NULL;
3036
3037 Py_XDECREF(str_key_obj);
3038 str_key_obj = PyUnicode_AsASCIIString(key);
3039 if (str_key_obj != NULL) {
3040 key = str_key_obj;
3041 }
3042
3043 str = PyBytes_AsString(key);
3044
3045 if (str == NULL) {
3046 PyErr_Clear();
3047 PyErr_SetString(PyExc_TypeError, "invalid keyword");
3048 goto finish;
3049 }
3050

Callers

nothing calls this directly

Calls 6

einsum_sub_op_from_strFunction · 0.85
einsum_sub_op_from_listsFunction · 0.85
PyArray_OrderConverterFunction · 0.85
PyArray_CastingConverterFunction · 0.85
PyArray_DescrConverter2Function · 0.85
PyArray_ReturnFunction · 0.85

Tested by

no test coverage detected