当前位置: 首页>>代码示例>>C++>>正文


C++ PyArray_Check函数代码示例

本文整理汇总了C++中PyArray_Check函数的典型用法代码示例。如果您正苦于以下问题:C++ PyArray_Check函数的具体用法?C++ PyArray_Check怎么用?C++ PyArray_Check使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。


在下文中一共展示了PyArray_Check函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。

示例1: sprintf

/** Makes sure a Numpy Array has the given type and dimension.  Raises
a Python exception if it does not.
@param type_num See http://docs.scipy.org/doc/numpy/reference/c-api.dtype.html eg: NPY_DOUBLE */
PyArrayObject *check_dimensions(
PyObject *ovec,
std::string const &vname,
int type_num,
int ndim,
int const *dims,
int N_ndim)
{
	// Check user and template #-dimensions match
	if (ndim != N_ndim) {
 		char buf[200 + vname.size()];
		sprintf(buf, "check_dimensions(%s): Number of template dimensions (%d) must match number of array dimensions (%d)", vname.c_str(), N_ndim, ndim);
		fprintf(stderr, "%s\n", buf);
		PyErr_SetString(PyExc_ValueError, buf);
		throw std::exception();
	}

	// Check that it's not null
	if (!ovec) {
		std::string serr = "check_dimensions: Array object " + vname + " is null";
		fprintf(stderr, "%s\n", serr.c_str());
		PyErr_SetString(PyExc_ValueError, serr.c_str());
		throw std::exception();
	}

	// Check that it's type PyArrayObject
	if (!PyArray_Check(ovec)) {
		std::string serr = "check_dimensions: Object " + vname + " is not a Numpy array";
		fprintf(stderr, "%s\n", serr.c_str());
		PyErr_SetString(PyExc_ValueError,
			serr.c_str());
		throw std::exception();
	}
	PyArrayObject *vec = (PyArrayObject *)ovec;

	// Check the data type and number of dimensions
	if (vec->descr->type_num != type_num || vec->nd != ndim)  {
		char buf[200 + vname.size()];
		sprintf(buf, "check_dimensions: %s must be of type_num %d and %d dimensions (its is of type_num=%d and %d dimensions).", vname.c_str(), type_num, ndim, vec->descr->type_num, vec->nd);
		fprintf(stderr, "%s\n", buf);
		PyErr_SetString(PyExc_ValueError, buf);
		throw std::exception();
	}

	// Check the dimensions themselves
	for (int i=0; i<ndim; ++i) {
		if (dims[i] < 0) continue;		// Don't check this dimension
		if (dims[i] != vec->dimensions[i]) {
			char buf[200 + vname.size()];
			sprintf(buf,
				"%s: Array dimension #%d is %d, should be %d",
				vname.c_str(), i, vec->dimensions[i], dims[i]);
			fprintf(stderr, "%s\n", buf);
			PyErr_SetString(PyExc_ValueError, buf);
			throw std::exception();
		}
	}
	return vec;
}
开发者ID:seifer08ms,项目名称:icebin,代码行数:62,代码来源:pyutil.cpp

示例2: PyLWPR_S_init_alpha

static int PyLWPR_S_init_alpha(PyLWPR *self,PyObject *value, void *closure) { 
   if (!PyArray_Check(value)) {
      PyErr_SetString(PyExc_TypeError, "Attribute 'init_alpha' must be a numpy array.");
      return -1;
   }
   return set_matrix_from_array(self->model.nIn, self->model.nInStore, self->model.nIn, 
                                 self->model.init_alpha, (PyArrayObject *)value);
}
开发者ID:mc01104,项目名称:CTR,代码行数:8,代码来源:lwprmodule.c

示例3: convert

    static void convert(PyObject *src, T &dst) {
      if (PyArray_Check(src)) {
	Converter<PyArrayObject*, T>::convert(src, dst);
      }
      else {
	throw std::string("Cannot convert object!");
      }
    }
开发者ID:fubincom,项目名称:convert-xy,代码行数:8,代码来源:converter.hpp

示例4: string

    static PyArrayObject *cast(PyObject *obj) {
      if (PyArray_Check(obj)) {
	return (PyArrayObject*)obj;
      }
      else {
	throw std::string("Object must be a Python array!");
      }
    }
开发者ID:fubincom,项目名称:convert-xy,代码行数:8,代码来源:converter.hpp

示例5: td_py_get_eltype

td_tag_t td_py_get_eltype(void *v)
{
    if (PyArray_Check( (PyObject *) v)){
        PyArrayObject *arr = (PyArrayObject *)v;
        return numpy_type_to_td(PyArray_TYPE(v));
    }
    return TD_UNKNOWN;
}
开发者ID:gvidaver,项目名称:xlang,代码行数:8,代码来源:td_python.c

示例6: _IsWriteable

NPY_NO_EXPORT npy_bool
_IsWriteable(PyArrayObject *ap)
{
    PyObject *base=PyArray_BASE(ap);
#if defined(NPY_PY3K)
    Py_buffer view;
#else
    void *dummy;
    Py_ssize_t n;
#endif

    /* If we own our own data, then no-problem */
    if ((base == NULL) || (PyArray_FLAGS(ap) & NPY_ARRAY_OWNDATA)) {
        return NPY_TRUE;
    }
    /*
     * Get to the final base object
     * If it is a writeable array, then return TRUE
     * If we can find an array object
     * or a writeable buffer object as the final base object
     * or a string object (for pickling support memory savings).
     * - this last could be removed if a proper pickleable
     * buffer was added to Python.
     *
     * MW: I think it would better to disallow switching from READONLY
     *     to WRITEABLE like this...
     */

    while(PyArray_Check(base)) {
        if (PyArray_CHKFLAGS((PyArrayObject *)base, NPY_ARRAY_OWNDATA)) {
            return (npy_bool) (PyArray_ISWRITEABLE((PyArrayObject *)base));
        }
        base = PyArray_BASE((PyArrayObject *)base);
    }

    /*
     * here so pickle support works seamlessly
     * and unpickled array can be set and reset writeable
     * -- could be abused --
     */
    if (PyString_Check(base)) {
        return NPY_TRUE;
    }
#if defined(NPY_PY3K)
    if (PyObject_GetBuffer(base, &view, PyBUF_WRITABLE|PyBUF_SIMPLE) < 0) {
        PyErr_Clear();
        return NPY_FALSE;
    }
    PyBuffer_Release(&view);
#else
    if (PyObject_AsWriteBuffer(base, &dummy, &n) < 0) {
        PyErr_Clear();
        return NPY_FALSE;
    }
#endif
    return NPY_TRUE;
}
开发者ID:mingwandroid,项目名称:numpy,代码行数:57,代码来源:common.c

示例7: fast_scalar_power

/* optimize float array or complex array to a scalar power */
static PyObject *
fast_scalar_power(PyArrayObject *a1, PyObject *o2, int inplace)
{
    double exp;

    if (PyArray_Check(a1) && array_power_is_scalar(o2, &exp)) {
        PyObject *fastop = NULL;
        if (PyArray_ISFLOAT(a1) || PyArray_ISCOMPLEX(a1)) {
            if (exp == 1.0) {
                /* we have to do this one special, as the
                   "copy" method of array objects isn't set
                   up early enough to be added
                   by PyArray_SetNumericOps.
                */
                if (inplace) {
                    Py_INCREF(a1);
                    return (PyObject *)a1;
                } else {
                    return PyArray_Copy(a1);
                }
            }
            else if (exp == -1.0) {
                fastop = n_ops.reciprocal;
            }
            else if (exp ==  0.0) {
                fastop = n_ops.ones_like;
            }
            else if (exp ==  0.5) {
                fastop = n_ops.sqrt;
            }
            else if (exp ==  2.0) {
                fastop = n_ops.square;
            }
            else {
                return NULL;
            }

            if (inplace) {
                return PyArray_GenericInplaceUnaryFunction(a1, fastop);
            } else {
                return PyArray_GenericUnaryFunction(a1, fastop);
            }
        }
        else if (exp==2.0) {
            fastop = n_ops.multiply;
            if (inplace) {
                return PyArray_GenericInplaceBinaryFunction
                    (a1, (PyObject *)a1, fastop);
            }
            else {
                return PyArray_GenericBinaryFunction
                    (a1, (PyObject *)a1, fastop);
            }
        }
    }
    return NULL;
}
开发者ID:chadnetzer,项目名称:numpy-gaurdro,代码行数:58,代码来源:number.c

示例8: evaluateTwoDimXY

 /**
 * Function to call to evaluate model
 * @param args: input numpy array  [x[],y[]]
 * @return: numpy array object 
 */
 static PyObject * evaluateTwoDimXY( SCCrystalModel* model, 
                              PyArrayObject *x, PyArrayObject *y)
 {
    PyArrayObject *result;
    int x_len, y_len, dims[1];
    //check validity of input vectors
    if (x->nd != 1 || x->descr->type_num != PyArray_DOUBLE
        || y->nd != 1 || y->descr->type_num != PyArray_DOUBLE
        || y->dimensions[0] != x->dimensions[0]){
        const char * message= "evaluateTwoDimXY  expect 2 numpy arrays";
        PyErr_SetString(PyExc_ValueError , message); 
        return NULL;
    }
   
	if (PyArray_Check(x) && PyArray_Check(y)) {
		
	    x_len = dims[0]= x->dimensions[0];
        y_len = dims[0]= y->dimensions[0];
	    
	    // Make a new double matrix of same dims
        result=(PyArrayObject *) PyArray_FromDims(1,dims,NPY_DOUBLE);
        if (result == NULL){
	    const char * message= "Could not create result ";
        PyErr_SetString(PyExc_RuntimeError , message);
	    return NULL;
	    }
       
        /* Do the calculation. */
#pragma omp parallel for
        for (int i=0; i< x_len; i++) {
            double x_value = *(double *)(x->data + i*x->strides[0]);
  		    double y_value = *(double *)(y->data + i*y->strides[0]);
  			double *result_value = (double *)(result->data +
  			      i*result->strides[0]);
  			*result_value = (*model)(x_value, y_value);
        }           
        return PyArray_Return(result); 
        
        }else{
		    PyErr_SetString(CSCCrystalModelError, 
                   "CSCCrystalModel.evaluateTwoDimXY couldn't run.");
	        return NULL;
		}      	
}
开发者ID:ricleal,项目名称:SasModeling,代码行数:49,代码来源:CSCCrystalModel.cpp

示例9: new_points

/*
 * new_points
 *
 * Stores the items of a Numerical Python array or a Python sequence in a
 * C vector of double values.  Returns the number of elements or -1.
 * The C vector must be freed with g_free().
 */
static gint
new_points(PyObject *sequence, gdouble **out_points, int *out_array_type)
{
    int n = 0;
    gdouble *points = NULL;

    *out_array_type = PyArray_NOTYPE;
#ifndef WITHOUT_NUMPY
    if (PyArray_Check(sequence)) {
        PyArrayObject *array;

        array = (PyArrayObject *)
                PyArray_ContiguousFromObject(sequence, PyArray_DOUBLE, 1, 1);
        if (!array)
            return -1;
        n = array->dimensions[0];
        points = g_new(gdouble, n);
        memcpy(points, array->data, n * sizeof(double));
        Py_DECREF(array);
        *out_array_type = ((PyArrayObject *) sequence)->descr->type_num;
    } else
#endif
        if (PySequence_Check(sequence)) {
            n = PySequence_Length(sequence);
            if (n > 0) {
                int i;

                points = g_new(gdouble, n);
                for (i = 0; i < n; ++i) {
                    PyObject *item, *value;

                    item = PySequence_GetItem(sequence, i);
                    if (PyFloat_Check(item)) {
                        points[i] = PyFloat_AS_DOUBLE(item);
                    } else if (PyNumber_Check(item)
                               && (value = PyNumber_Float(item))) {
                        points[i] = PyFloat_AS_DOUBLE(value);
                        Py_DECREF(value);
                    } else {
                        PyErr_SetString(PyExc_TypeError,
                                        "sequence items must be numbers");
                        Py_DECREF(item);
                        g_free(points);
                        points = NULL;
                        return -1;
                    }
                    Py_DECREF(item);
                }
            }
        } else if (sequence != Py_None) {
            PyErr_SetString(PyExc_TypeError, "argument must be sequence or None");
            return -1;
        }
    *out_points = points;
    return n;
}
开发者ID:Onjrew,项目名称:OpenEV,代码行数:63,代码来源:gtkplotdata-support.c

示例10: try_pyarr_from_string

static int try_pyarr_from_string(PyObject *obj,const string str) {
  PyArrayObject *arr = NULL;
  if (PyArray_Check(obj) && (!((arr = (PyArrayObject *)obj) == NULL)))
    { STRINGCOPYN(arr->data,str,PyArray_NBYTES(arr)); }
  return 1;
capi_fail:
  PRINTPYOBJERR(obj);
  PyErr_SetString(_lbfgsb_error,"try_pyarr_from_string failed");
  return 0;
}
开发者ID:wskinner,项目名称:LeapHMM,代码行数:10,代码来源:_lbfgsbmodule.c

示例11: array

/*NUMPY_API
  Compute the size of an array (in number of items)
*/
NPY_NO_EXPORT intp
PyArray_Size(PyObject *op)
{
    if (PyArray_Check(op)) {
        return PyArray_SIZE((PyArrayObject *)op);
    }
    else {
        return 0;
    }
}
开发者ID:Apkawa,项目名称:numpy-refactor,代码行数:13,代码来源:arrayobject.c

示例12: PyArray_CopyObject

/*NUMPY_API*/
NPY_NO_EXPORT int
PyArray_CopyObject(PyArrayObject *dest, PyObject *src_object)
{
    PyArrayObject *src;
    PyObject *r;
    int ret;

    /*
     * Special code to mimic Numeric behavior for
     * character arrays.
     */
    if (dest->descr->type == PyArray_CHARLTR && dest->nd > 0 \
        && PyString_Check(src_object)) {
        intp n_new, n_old;
        char *new_string;
        PyObject *tmp;

        n_new = dest->dimensions[dest->nd-1];
        n_old = PyString_Size(src_object);
        if (n_new > n_old) {
            new_string = (char *)malloc(n_new);
            memmove(new_string, PyString_AS_STRING(src_object), n_old);
            memset(new_string + n_old, ' ', n_new - n_old);
            tmp = PyString_FromStringAndSize(new_string, n_new);
            free(new_string);
            src_object = tmp;
        }
    }

    if (PyArray_Check(src_object)) {
        src = (PyArrayObject *)src_object;
        Py_INCREF(src);
    }
    else if (!PyArray_IsScalar(src_object, Generic) &&
             PyArray_HasArrayInterface(src_object, r)) {
        src = (PyArrayObject *)r;
    }
    else {
        PyArray_Descr* dtype;
        dtype = dest->descr;
        Py_INCREF(dtype);
        src = (PyArrayObject *)PyArray_FromAny(src_object, dtype, 0,
                                               dest->nd,
                                               FORTRAN_IF(dest),
                                               NULL);
    }
    if (src == NULL) {
        return -1;
    }

    ret = PyArray_MoveInto(dest, src);
    Py_DECREF(src);
    return ret;
}
开发者ID:Apkawa,项目名称:numpy-refactor,代码行数:55,代码来源:arrayobject.c

示例13: py_type_to_td

static td_tag_t py_type_to_td(PyObject *pVal)
{
    if (PyInt_Check(pVal)){
#if INT32_MAX==LONG_MAX
        return TD_INT32;
#else
        return longlong_to_td(PyInt_AS_LONG(pVal));
#endif
    }
    else if (PyLong_Check(pVal)){
        PyObject *pErr;
        long long v;
        v = PyLong_AsLongLong(pVal);
        if ( v == -1 && PyErr_Occurred()) {
            // could not eval as long long try unsigned long long later
            PyErr_Clear();
        }
        else {
        	return longlong_to_td(PyInt_AS_LONG(v));
        }

        unsigned long long vv;
        vv = PyLong_AsUnsignedLongLong(pVal);
        if ( v == (unsigned long long) -1 && PyErr_Occurred() ) {
            PyErr_Clear();
            return TD_OBJECT;
        }
        else {
            return TD_UINT64;
        }
    }
    else if (PyFloat_Check(pVal)) {
        return TD_DOUBLE;
    }
    else if (PyUnicode_Check(pVal)){
        PyObject *pStr;
        pStr = PyUnicode_AsUTF8String(pVal);
        if ( pStr && PyErr_Occurred() ){
            PyErr_Clear();
            return TD_OBJECT;
        }
        else {
            return TD_UTF8;
        }
    }
    else if (PyString_Check(pVal)) {
        return TD_UTF8;
    }
    else if (PyArray_Check(pVal)) {
        return TD_ARRAY;
    }
    return TD_OBJECT;
}
开发者ID:gvidaver,项目名称:xlang,代码行数:53,代码来源:td_python.c

示例14: PyGILState_Ensure

PyArrayObject *call_funGetData(PyObject *func, double size) 
{
	PyObject *args;
	PyObject *kwargs;
	PyObject *result;
	PyArrayObject *retval;

	/* Make sure we own the GIL */
	PyGILState_STATE state = PyGILState_Ensure();

	/* Verify that func is a proper callable */
	if (!PyCallable_Check(func)) {
		fprintf(stderr, "call_func: expected a callable\n");
		goto fail;
	}
	/* Build arguments */
	args = Py_BuildValue("(d)", size);
	kwargs = NULL;

	/* Call the function */
	result = PyObject_Call(func, args, kwargs);
	Py_DECREF(args);
	Py_XDECREF(kwargs);

	/* Check for Python exceptions (if any) */
	if (PyErr_Occurred()) {
		PyErr_Print();
		goto fail;
	}

  /* Verify the result is a float object */
  if (!PyArray_Check( result )) {
    fprintf( stderr, "call_func: callable didn't return a array\n" );
    goto fail;
  }

  retval = (PyArrayObject*)PyArray_FROM_OTF( result, NPY_DOUBLE, NPY_ARRAY_IN_ARRAY );

	/* Create the return value */
	//retval = (PyArrayObject *)(result);
  //col = (double*)PyArray_GETPTR2( retval, TICA_FIRST_ROW, 0);
	Py_DECREF(result);

	/* Restore previous GIL state and return */
	PyGILState_Release(state);
	return retval;

fail:
	Py_XDECREF(result);
	PyGILState_Release(state);
	return NULL;
	abort();   // Change to something more appropriate
}
开发者ID:clonker,项目名称:spscicomp,代码行数:53,代码来源:Tica_CExtension.cpp

示例15: python_convert

// assume numDerivVars x numDerivVars as helper in Hessian conversion
// and lower triangular storage in Hessians
// returns false if conversion failed
bool NRELPythonApplicInterface::python_convert(PyObject *pym, 
					   RealSymMatrix &rm)
{
  // for now, the numpy case isn't called (since handled in calling
  // Hessian array convert)
#ifdef DAKOTA_PYTHON_NUMPY
  if (userNumpyFlag) {
    if (!PyArray_Check(pym) || PyArray_NDIM(pym) != 2 || 
	PyArray_DIM(pym,0) != numDerivVars  ||
	PyArray_DIM(pym,1) != numDerivVars) {
      Cerr << "Python numpy array not 2D of size " << numDerivVars << "x" 
	   << numDerivVars << "." << std::endl;
      return(false);
    }
    PyArrayObject *pao = (PyArrayObject *) pym;
    for (int i=0; i<numDerivVars; ++i)
      for (int j=0; j<=i; ++j)
	rm(i,j) = *(double *)(pao->data + i*(pao->strides[0]) + 
			       j*(pao->strides[1]));
  }
  else
#endif
  {
    if (!PyList_Check(pym) || PyList_Size(pym) != numDerivVars) {
      Cerr << "Python matrix must have " << numDerivVars << "rows." <<std::endl;
      return(false);
    }
    PyObject *pyv, *val;
    for (int i=0; i<numDerivVars; ++i) {
      pyv = PyList_GetItem(pym, i);
      if (!PyList_Check(pyv) || PyList_Size(pyv) != numDerivVars) {
	Cerr << "Python vector must have length " << numDerivVars << "." 
	     << std::endl;
	return(false);
      }
      for (int j=0; j<=i; ++j) {
	val = PyList_GetItem(pyv, j);
	if (PyFloat_Check(val))
	  rm(i,j) = PyFloat_AsDouble(val);
	else if (PyInt_Check(val))
	  rm(i,j) = (double) PyInt_AsLong(val);
	else {
	  Cerr << "Unsupported Python data type converting vector." 
	       << std::endl;
	  Py_DECREF(val);
	  return(false);
	}
      }
    }

  }
  return(true);
}
开发者ID:JustinSGray,项目名称:DAKOTA_plugin,代码行数:56,代码来源:python_interface.cpp


注:本文中的PyArray_Check函数示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。