コード例 #1
0
ファイル: spmatrixmodule.c プロジェクト: r35krag0th/pysparse
/******************************************************************************
 *                                                                            *
 * SpMatrix_ParseVecOpArgs -- parse arguments                                 *
 *                                                                            *
 *   parses arguments of Python functions, that expect two one dimensional    *
 *   Py_Array objects.                                                        *
 *                                                                            *
 *   This function should not be used, since it doesn't allow free the        *
 *   created objects.                                                         *
 *                                                                            *
 *   Use the macro SPMATRIX_PARSE_ARGS_ARR_ARR defined in spmatrix.h instead. *
 *                                                                            *
 ******************************************************************************/
static int
SpMatrix_ParseVecOpArgs(PyObject *args, double **x_data, double **y_data, int n) {
  PyObject *x_obj, *y_obj;
  int nx, ny, res;

  /* parse input arguments */
  if (!PyArg_ParseTuple(args, "OO", &x_obj, &y_obj))
    return -1;

  /* Make sure that x and b are continous double arrays */
  res = PyArray_As1D((PyObject **)&x_obj, (char **)x_data, &nx, PyArray_DOUBLE);
  if (res == -1) {
    PyErr_SetString(PyExc_ValueError, "Unable to convert first argument to double array");
    return -1;
  }
  res = PyArray_As1D((PyObject **)&y_obj, (char **)y_data, &ny, PyArray_DOUBLE);
  if (res == -1) {
    PyErr_SetString(PyExc_ValueError, "Unable to convert second argument to double array");
    return -1;
  }

  /* check operand shapes */
  if (nx != n || ny != n) {
    PyErr_SetString(PyExc_ValueError, "incompatible operand shapes");
    return -1;
  }

  return 0;
}
コード例 #2
0
ファイル: _ppgplot.c プロジェクト: bamford/ppgplot
static PyObject *
tofloatvector (PyObject *o, float **v, int *vsz)
{
    PyArrayObject *a1, *af1, *af2;
    int ownedaf1=0;

    /* Check if args are arrays. */
    if (!PyArray_Check(o)) {
		PyErr_SetString(PpgTYPEErr,"object is not an array");
		return(NULL);
    }
    a1 = (PyArrayObject *)o;
    /* Check if args are vectors. */
    if (a1->nd != 1) {
		PyErr_SetString(PpgTYPEErr,"object is not a vector");
		return(NULL);
    }
    
#ifdef DEBUG_TOARRAY
    fprintf(stderr,"(tofloatvector): array type = %d\n",a1->descr->type_num);
#endif

    switch (a1->descr->type_num) {
    case PyArray_FLOAT:
		af1 = a1;
		break;
    case PyArray_CHAR:
#ifndef USE_NUMARRAY 
    case PyArray_UBYTE: 
#endif
#ifndef USE_NUMPY 
    case PyArray_SBYTE:
#endif
    case PyArray_SHORT:
    case PyArray_INT:
#ifndef USE_NUMARRAY
    case PyArray_LONG:
#endif
    case PyArray_DOUBLE:
		if (!(af1 = (PyArrayObject *)PyArray_Cast(a1,PyArray_FLOAT))) {
			PyErr_SetString(PpgTYPEErr,"cannot cast vector to floats");
			return(NULL);
		}
		ownedaf1 = 1;
		break;
    default:
		PyErr_SetString(PpgTYPEErr,"cannot cast vector to floats");
		return(NULL);
		break;
    }
    
#ifdef DEBUG_TOARRAY
    fprintf(stderr,"(tofloatvector): array type = %d\n",a1->descr->type_num);
#endif
    
    af2 = af1;
    if (PyArray_As1D((PyObject **)&af2, (char **)v, vsz,
					 PyArray_FLOAT) == -1) {
		af2 = NULL;
    }
    
    if (ownedaf1) { Py_DECREF(af1); }

    return((PyObject *)af2);
}