jobject JySync_Init_JyComplex_From_PyComplex(PyObject* src, jclass subtype) { env(NULL); return (*env)->NewObject(env, pyComplexClass, pyComplex_by2DoubleConstructor, PyComplex_RealAsDouble(src), PyComplex_ImagAsDouble(src)); }
static MPC_Object * GMPy_MPC_From_PyComplex(PyObject *obj, mpfr_prec_t rprec, mpfr_prec_t iprec, CTXT_Object *context) { MPC_Object *result; assert(PyComplex_Check(obj)); CHECK_CONTEXT(context); if (rprec == 0) rprec = GET_REAL_PREC(context); else if (rprec == 1) rprec = DBL_MANT_DIG; if (iprec == 0) iprec = GET_IMAG_PREC(context); else if (iprec == 1) rprec = DBL_MANT_DIG; if ((result = GMPy_MPC_New(rprec, iprec, context))) { result->rc = mpc_set_d_d(result->c, PyComplex_RealAsDouble(obj), PyComplex_ImagAsDouble(obj), GET_MPC_ROUND(context)); if (rprec != 1 || iprec != 1) { GMPY_MPC_CHECK_RANGE(result, context); } GMPY_MPC_SUBNORMALIZE(result, context); GMPY_MPC_EXCEPTIONS(result, context); } return result; }
static int pyobject_to_ligotimegps(PyObject *obj, LIGOTimeGPS *gps) { if(pylal_LIGOTimeGPS_Check(obj)) { *gps = ((pylal_LIGOTimeGPS *) obj)->gps; } else if(PyInt_Check(obj)) { XLALGPSSet(gps, PyInt_AsLong(obj), 0); } else if(PyLong_Check(obj)) { XLALGPSSet(gps, PyLong_AsLongLong(obj), 0); } else if(PyFloat_Check(obj)) { XLALGPSSetREAL8(gps, PyFloat_AsDouble(obj)); } else if(PyComplex_Check(obj)) { if(PyComplex_ImagAsDouble(obj) != 0.0) { XLALGPSSet(gps, 0, 0); PyErr_SetObject(PyExc_ValueError, obj); return 0; } XLALGPSSetREAL8(gps, PyComplex_RealAsDouble(obj)); } else { PyObject *s_attr = PyObject_GetAttrString(obj, "seconds"); PyObject *n_attr = PyObject_GetAttrString(obj, "nanoseconds"); XLALGPSSet(gps, PyInt_AsLong(s_attr), PyInt_AsLong(n_attr)); Py_XDECREF(s_attr); Py_XDECREF(n_attr); if(PyErr_Occurred()) { PyErr_SetObject(PyExc_TypeError, obj); return 0; } } return 1; }
int extractComplexList(PyObject *list_in, double2** p_array_out, int argnum) { //Checking input type if (!PyList_Check(list_in)) { char errmsg[37]; sprintf(errmsg,"Argument %d must be a list of complex\n", argnum);fflush(0); printf("Argument %d must be a list of complex\n", argnum);fflush(0); fprintf(stderr, "%s", errmsg); PyErr_SetString(PyExc_TypeError, errmsg); return -1; } if (!PyComplex_Check(PyList_GetItem(list_in,0))) { char errmsg[37]; sprintf(errmsg,"Argument %d must be a list of complex\n", argnum);fflush(0); printf("Argument %d must be a list of complex\n", argnum);fflush(0); fprintf(stderr, "%s", errmsg); PyErr_SetString(PyExc_TypeError, errmsg); return -1; } int list_size = PyList_Size(list_in); PyObject* iter = PyObject_GetIter(list_in); *p_array_out = (double2*)malloc(sizeof(double2)*list_size); double2* array_out = *p_array_out; int q; PyObject* item; for (q=0;q<PyList_Size(list_in);q++) { item = PyIter_Next(iter); array_out[q].x = PyComplex_RealAsDouble(item); array_out[q].y = PyComplex_ImagAsDouble(item); } return 0; }
std::complex<T> complex_from_python(PyObject* p, boost::python::type<T>) { expect_complex(p); return std::complex<T>( static_cast<T>(PyComplex_RealAsDouble(p)), static_cast<T>(PyComplex_ImagAsDouble(p))); }
//--------------------------------------------------------------------------- template<> double FromPyObject<double>(PyObject *O) { if(PyComplex_Check(O)) if(PyComplex_ImagAsDouble(O) != 0) PyErr_SetString(PyExc_TypeError, "complex number has an imaginary part"); else return PyComplex_RealAsDouble(O); return PyFloat_AsDouble(O); }
std::complex<T> complex_from_python(PyObject* p, boost::python::type<T>) { if (PyInt_Check(p)) return std::complex<T>(PyInt_AS_LONG(p)); if (PyLong_Check(p)) return std::complex<T>(PyLong_AsDouble(p)); if (PyFloat_Check(p)) return std::complex<T>(PyFloat_AS_DOUBLE(p)); expect_complex(p); return std::complex<T>( static_cast<T>(PyComplex_RealAsDouble(p)), static_cast<T>(PyComplex_ImagAsDouble(p))); }
/* wrvr is the complement of rdvr that uses uvputvr_c to write variables of * various types. Accepts numpy arrays for writing. */ PyObject * UVObject_wrvr(UVObject *self, PyObject *args) { char *name, *type, value[MAXVAR]; char *st; PyObject *wr_val; PyArrayObject *wr_arr=NULL; if (!PyArg_ParseTuple(args, "ssO", &name, &type, &wr_val)) return NULL; if (PyArray_Check(wr_val)) { wr_arr = (PyArrayObject *) wr_val; CHK_ARRAY_RANK(wr_arr,1); } try { switch (type[0]) { case 'a': CHK_STRING(wr_val); st = PyString_AsString(wr_val); uvputvr_c(self->tno,H_BYTE,name, st, PyString_Size(wr_val)+1); break; case 'j': STORE_IA(NPY_LONG,H_INT2,CHK_INT,short,PyInt_AsLong); break; case 'i': STORE_IA(NPY_LONG,H_INT,CHK_INT,int,PyInt_AsLong); break; case 'r': STORE_IA(NPY_FLOAT,H_REAL,CHK_FLOAT,float,PyFloat_AsDouble); break; case 'd': STORE_IA(NPY_DOUBLE,H_DBLE,CHK_FLOAT,double,PyFloat_AsDouble); break; case 'c': if (PyArray_Check(wr_val)) { CHK_ARRAY_TYPE(wr_arr,NPY_CDOUBLE); uvputvr_c(self->tno,H_CMPLX,name,(char *)wr_arr->data, DIM(wr_arr,0)); } else { CHK_COMPLEX(wr_val); ((double *)value)[0] = PyComplex_RealAsDouble(wr_val); ((double *)value)[1] = PyComplex_ImagAsDouble(wr_val); uvputvr_c(self->tno,H_CMPLX,name,value,1); } break; default: PyErr_Format(PyExc_ValueError,"unknown var type: %c",type[0]); return NULL; } Py_INCREF(Py_None); return Py_None; } catch (MiriadError &e) { PyErr_Format(PyExc_RuntimeError, e.get_message()); return NULL; } }
static int format_complex_internal(PyObject *value, const InternalFormatSpec *format, _PyUnicodeWriter *writer) { double re; double im; char *re_buf = NULL; /* buffer returned from PyOS_double_to_string */ char *im_buf = NULL; /* buffer returned from PyOS_double_to_string */ InternalFormatSpec tmp_format = *format; Py_ssize_t n_re_digits; Py_ssize_t n_im_digits; Py_ssize_t n_re_remainder; Py_ssize_t n_im_remainder; Py_ssize_t n_re_total; Py_ssize_t n_im_total; int re_has_decimal; int im_has_decimal; int precision, default_precision = 6; Py_UCS4 type = format->type; Py_ssize_t i_re; Py_ssize_t i_im; NumberFieldWidths re_spec; NumberFieldWidths im_spec; int flags = 0; int result = -1; Py_UCS4 maxchar = 127; enum PyUnicode_Kind rkind; void *rdata; Py_UCS4 re_sign_char = '\0'; Py_UCS4 im_sign_char = '\0'; int re_float_type; /* Used to see if we have a nan, inf, or regular float. */ int im_float_type; int add_parens = 0; int skip_re = 0; Py_ssize_t lpad; Py_ssize_t rpad; Py_ssize_t total; PyObject *re_unicode_tmp = NULL; PyObject *im_unicode_tmp = NULL; /* Locale settings, either from the actual locale or from a hard-code pseudo-locale */ LocaleInfo locale = STATIC_LOCALE_INFO_INIT; if (format->precision > INT_MAX) { PyErr_SetString(PyExc_ValueError, "precision too big"); goto done; } precision = (int)format->precision; /* Zero padding is not allowed. */ if (format->fill_char == '0') { PyErr_SetString(PyExc_ValueError, "Zero padding is not allowed in complex format " "specifier"); goto done; } /* Neither is '=' alignment . */ if (format->align == '=') { PyErr_SetString(PyExc_ValueError, "'=' alignment flag is not allowed in complex format " "specifier"); goto done; } re = PyComplex_RealAsDouble(value); if (re == -1.0 && PyErr_Occurred()) goto done; im = PyComplex_ImagAsDouble(value); if (im == -1.0 && PyErr_Occurred()) goto done; if (format->alternate) flags |= Py_DTSF_ALT; if (type == '\0') { /* Omitted type specifier. Should be like str(self). */ type = 'r'; default_precision = 0; if (re == 0.0 && copysign(1.0, re) == 1.0) skip_re = 1; else add_parens = 1; } if (type == 'n') /* 'n' is the same as 'g', except for the locale used to format the result. We take care of that later. */ type = 'g'; if (precision < 0) precision = default_precision; else if (type == 'r') type = 'g'; /* Cast "type", because if we're in unicode we need to pass a 8-bit char. This is safe, because we've restricted what "type" can be. */ re_buf = PyOS_double_to_string(re, (char)type, precision, flags, &re_float_type); if (re_buf == NULL) goto done; im_buf = PyOS_double_to_string(im, (char)type, precision, flags, &im_float_type); if (im_buf == NULL) goto done; n_re_digits = strlen(re_buf); n_im_digits = strlen(im_buf); /* Since there is no unicode version of PyOS_double_to_string, just use the 8 bit version and then convert to unicode. */ re_unicode_tmp = _PyUnicode_FromASCII(re_buf, n_re_digits); if (re_unicode_tmp == NULL) goto done; i_re = 0; im_unicode_tmp = _PyUnicode_FromASCII(im_buf, n_im_digits); if (im_unicode_tmp == NULL) goto done; i_im = 0; /* Is a sign character present in the output? If so, remember it and skip it */ if (PyUnicode_READ_CHAR(re_unicode_tmp, i_re) == '-') { re_sign_char = '-'; ++i_re; --n_re_digits; } if (PyUnicode_READ_CHAR(im_unicode_tmp, i_im) == '-') { im_sign_char = '-'; ++i_im; --n_im_digits; } /* Determine if we have any "remainder" (after the digits, might include decimal or exponent or both (or neither)) */ parse_number(re_unicode_tmp, i_re, i_re + n_re_digits, &n_re_remainder, &re_has_decimal); parse_number(im_unicode_tmp, i_im, i_im + n_im_digits, &n_im_remainder, &im_has_decimal); /* Determine the grouping, separator, and decimal point, if any. */ if (get_locale_info(format->type == 'n' ? LT_CURRENT_LOCALE : (format->thousands_separators ? LT_DEFAULT_LOCALE : LT_NO_LOCALE), &locale) == -1) goto done; /* Turn off any padding. We'll do it later after we've composed the numbers without padding. */ tmp_format.fill_char = '\0'; tmp_format.align = '<'; tmp_format.width = -1; /* Calculate how much memory we'll need. */ n_re_total = calc_number_widths(&re_spec, 0, re_sign_char, re_unicode_tmp, i_re, i_re + n_re_digits, n_re_remainder, re_has_decimal, &locale, &tmp_format, &maxchar); /* Same formatting, but always include a sign, unless the real part is * going to be omitted, in which case we use whatever sign convention was * requested by the original format. */ if (!skip_re) tmp_format.sign = '+'; n_im_total = calc_number_widths(&im_spec, 0, im_sign_char, im_unicode_tmp, i_im, i_im + n_im_digits, n_im_remainder, im_has_decimal, &locale, &tmp_format, &maxchar); if (skip_re) n_re_total = 0; /* Add 1 for the 'j', and optionally 2 for parens. */ calc_padding(n_re_total + n_im_total + 1 + add_parens * 2, format->width, format->align, &lpad, &rpad, &total); if (lpad || rpad) maxchar = Py_MAX(maxchar, format->fill_char); if (_PyUnicodeWriter_Prepare(writer, total, maxchar) == -1) goto done; rkind = writer->kind; rdata = writer->data; /* Populate the memory. First, the padding. */ result = fill_padding(writer, n_re_total + n_im_total + 1 + add_parens * 2, format->fill_char, lpad, rpad); if (result == -1) goto done; if (add_parens) { PyUnicode_WRITE(rkind, rdata, writer->pos, '('); writer->pos++; } if (!skip_re) { result = fill_number(writer, &re_spec, re_unicode_tmp, i_re, i_re + n_re_digits, NULL, 0, 0, &locale, 0); if (result == -1) goto done; } result = fill_number(writer, &im_spec, im_unicode_tmp, i_im, i_im + n_im_digits, NULL, 0, 0, &locale, 0); if (result == -1) goto done; PyUnicode_WRITE(rkind, rdata, writer->pos, 'j'); writer->pos++; if (add_parens) { PyUnicode_WRITE(rkind, rdata, writer->pos, ')'); writer->pos++; } writer->pos += rpad; done: PyMem_Free(re_buf); PyMem_Free(im_buf); Py_XDECREF(re_unicode_tmp); Py_XDECREF(im_unicode_tmp); free_locale_info(&locale); return result; }
template<> complex __to_ss(PyObject *p) { return mcomplex(PyComplex_RealAsDouble(p), PyComplex_ImagAsDouble(p)); }
/* hwrite supports writing to header items of all types, using the various * hwrite_c calls. Writes one item per call. */ PyObject * WRAP_hwrite(PyObject *self, PyObject *args) { int item_hdl, offset, iostat; char *type; PyObject *val; int in; short sh; long lg; float fl; double db; float cx[2]; char *st; if (!PyArg_ParseTuple(args, "iiOs", &item_hdl, &offset, &val, &type)) return NULL; try { switch (type[0]) { case 'a': CHK_STRING(val); st = PyString_AsString(val); in = PyString_Size(val); // # bytes to write hwriteb_c(item_hdl, st, offset, in, &iostat); CHK_IO(iostat); offset = H_BYTE_SIZE * in; break; case 'i': CHK_INT(val); in = (int) PyInt_AsLong(val); hwritei_c(item_hdl, &in, offset, H_INT_SIZE, &iostat); CHK_IO(iostat); offset = H_INT_SIZE; break; case 'j': CHK_INT(val); sh = (short) PyInt_AsLong(val); hwritej_c(item_hdl, &sh, offset, H_INT2_SIZE, &iostat); CHK_IO(iostat); offset = H_INT2_SIZE; break; case 'l': CHK_LONG(val); lg = PyLong_AsLong(val); hwritel_c(item_hdl, &lg, offset, H_INT8_SIZE, &iostat); CHK_IO(iostat); offset = H_INT8_SIZE; break; case 'r': CHK_FLOAT(val); fl = (float) PyFloat_AsDouble(val); hwriter_c(item_hdl, &fl, offset, H_REAL_SIZE, &iostat); CHK_IO(iostat); offset = H_REAL_SIZE; break; case 'd': CHK_FLOAT(val); db = PyFloat_AsDouble(val); hwrited_c(item_hdl, &db, offset, H_DBLE_SIZE, &iostat); CHK_IO(iostat); offset = H_DBLE_SIZE; break; case 'c': CHK_COMPLEX(val); cx[0] = (float) PyComplex_RealAsDouble(val); cx[1] = (float) PyComplex_ImagAsDouble(val); hwritec_c(item_hdl, cx, offset, H_CMPLX_SIZE, &iostat); CHK_IO(iostat); offset = H_CMPLX_SIZE; break; default: PyErr_Format(PyExc_ValueError, "unknown item type: %c",type[0]); return NULL; } return PyInt_FromLong(offset); } catch (MiriadError &e) { PyErr_Format(PyExc_RuntimeError, e.get_message()); return NULL; } }
void python_to_mathematica_object(PyObject *obj) { if(PyBool_Check(obj)) { if(obj==Py_True) MLPutSymbol(stdlink, "True"); else MLPutSymbol(stdlink, "False"); return; } if(PyInt_Check(obj)) { MLPutLongInteger(stdlink, PyInt_AsLong(obj)); return; } if(PyLong_Check(obj)) { #ifdef PYTHON25 Py_ssize_t length; #else int length; #endif char *str, *mat_expr; PyObject *long_as_str; long_as_str = PyObject_CallMethod(obj, "__str__", NULL); PyString_AsStringAndSize(long_as_str, &str, &length); MLPutFunction(stdlink, "ToExpression", 1); MLPutString(stdlink, str); Py_DECREF(long_as_str); return; } if(obj==Py_None) { MLPutSymbol(stdlink, "Null"); return; } if(PyFloat_Check(obj)) { MLPutDouble(stdlink, (double)PyFloat_AsDouble(obj)); return; } if(PyComplex_Check(obj)) { MLPutFunction(stdlink, "Complex", 2); MLPutDouble(stdlink, (double)PyComplex_RealAsDouble(obj)); MLPutDouble(stdlink, (double)PyComplex_ImagAsDouble(obj)); return; } if(PyString_Check(obj)) { char *str; #ifdef PYTHON25 Py_ssize_t length; #else int length; #endif PyString_AsStringAndSize(obj, &str, &length); MLPutByteString(stdlink, (unsigned char *)str, length); return; } if(PyUnicode_Check(obj)) { MLPutUnicodeString(stdlink, PyUnicode_AsUnicode(obj), PyUnicode_GetSize(obj) ); return; } if(PyTuple_Check(obj)) { mat_process_iterable_object(obj, "Can't get iterator for 'tuple'"); return; } if(PyList_Check(obj)) { mat_process_iterable_object(obj, "Can't get iterator for 'list'"); return; } #ifndef PYTHON23 if(PyObject_TypeCheck(obj, &PySet_Type)) { mat_process_iterable_object(obj, "Can't get iterator for 'set'"); return; } #endif if(PyDict_Check(obj)) { PyObject *items; items = PyDict_Items(obj); python_to_mathematica_object(items); Py_DECREF(items); return; } // This should ideally print info, like type of the object, that // can't be converted. MLPutString(stdlink, "Object type can't be converted!"); }