static PyObject * GMPy_Complex_Add(PyObject *x, PyObject *y, CTXT_Object *context) { MPC_Object *result = NULL; CHECK_CONTEXT(context); if (!(result = GMPy_MPC_New(0, 0, context))) { /* LCOV_EXCL_START */ return NULL; /* LCOV_EXCL_STOP */ } if (MPC_Check(x) && MPC_Check(y)) { SET_MPC_MPC_WAS_NAN(context, x, y); result->rc = mpc_add(result->c, MPC(x), MPC(y), GET_MPC_ROUND(context)); _GMPy_MPC_Cleanup(&result, context); return (PyObject*)result; } if (IS_COMPLEX(x) && IS_COMPLEX(y)) { MPC_Object *tempx = NULL, *tempy = NULL; if (!(tempx = GMPy_MPC_From_Complex(x, 1, 1, context)) || !(tempy = GMPy_MPC_From_Complex(y, 1, 1, context))) { /* LCOV_EXCL_START */ Py_XDECREF((PyObject*)tempx); Py_XDECREF((PyObject*)tempy); Py_DECREF((PyObject*)result); return NULL; /* LCOV_EXCL_STOP */ } SET_MPC_MPC_WAS_NAN(context, tempx, tempy); result->rc = mpc_add(result->c, tempx->c, tempy->c, GET_MPC_ROUND(context)); Py_DECREF((PyObject*)tempx); Py_DECREF((PyObject*)tempy); _GMPy_MPC_Cleanup(&result, context); return (PyObject*)result; } /* LCOV_EXCL_START */ Py_DECREF((PyObject*)result); SYSTEM_ERROR("Internal error in GMPy_Complex_Add()."); return NULL; /* LCOV_EXCL_STOP */ }
static PyObject * GMPy_Complex_Polar(PyObject *x, CTXT_Object *context) { PyObject *tempx, *abs, *phase, *result; CHECK_CONTEXT(context); if (!(tempx = (PyObject*)GMPy_MPC_From_Complex(x, 1, 1, context))) { return NULL; } abs = GMPy_Complex_Abs(tempx, context); phase = GMPy_Complex_Phase(tempx, context); Py_DECREF(tempx); result = PyTuple_New(2); if (!abs || !phase || !result) { Py_XDECREF(abs); Py_XDECREF(phase); Py_XDECREF(result); return NULL; } PyTuple_SET_ITEM(result, 0, abs); PyTuple_SET_ITEM(result, 1, phase); return result; }
static PyObject * GMPy_Complex_Mul_2exp(PyObject *x, PyObject *y, CTXT_Object *context) { MPC_Object *result, *tempx; unsigned long exp = 0; CHECK_CONTEXT(context); exp = c_ulong_From_Integer(y); if (exp == (unsigned long)(-1) && PyErr_Occurred()) { return NULL; } result = GMPy_MPC_New(0, 0, context); tempx = GMPy_MPC_From_Complex(x, 1, 1, context); if (!result || !tempx) { Py_XDECREF((PyObject*)result); Py_XDECREF((PyObject*)tempx); return NULL; } result->rc = mpc_mul_2ui(result->c, tempx->c, exp, GET_MPC_ROUND(context)); Py_DECREF((PyObject*)tempx); GMPY_MPC_CLEANUP(result, context, "mul_2exp()"); return (PyObject*)result; }
static PyObject * GMPy_Complex_Is_NAN(PyObject *x, CTXT_Object *context) { MPC_Object *tempx; int res; if (MPC_Check(x)) { res = MPC_IS_NAN_P(x); } else { CHECK_CONTEXT(context); if (!(tempx = GMPy_MPC_From_Complex(x, 1, 1, context))) { return NULL; } res = MPC_IS_NAN_P(tempx); Py_DECREF((PyObject*)tempx); } if (res) { Py_RETURN_TRUE; } else { Py_RETURN_FALSE; } }
static PyObject * GMPy_Complex_Abs(PyObject *x, CTXT_Object *context) { MPFR_Object *result = NULL; MPC_Object *tempx = NULL; CHECK_CONTEXT(context); if (!(tempx = GMPy_MPC_From_Complex(x, 1, 1, context)) || !(result = GMPy_MPFR_New(0, context))) { /* LCOV_EXCL_START */ Py_XDECREF((PyObject*)tempx); Py_XDECREF((PyObject*)result); return NULL; /* LCOV_EXCL_STOP */ } mpfr_clear_flags(); SET_MPC_WAS_NAN(context, tempx); result->rc = mpc_abs(result->f, tempx->c, GET_MPC_ROUND(context)); Py_DECREF((PyObject*)tempx); _GMPy_MPFR_Cleanup(&result, context); return (PyObject*)result; }
static PyObject * GMPy_Context_Digits(PyObject *self, PyObject *args) { PyObject *arg0, *tuple, *temp, *result; Py_ssize_t argc; argc = PyTuple_GET_SIZE(args); if (argc == 0) { TYPE_ERROR("digits() requires at least one argument"); return NULL; } if (argc > 3) { TYPE_ERROR("digits() accepts at most three arguments"); return NULL; } arg0 = PyTuple_GET_ITEM(args, 0); if (!(tuple = PyTuple_GetSlice(args, 1, argc))) { return NULL; } if (IS_INTEGER(arg0)) { temp = (PyObject*)GMPy_MPZ_From_Integer(arg0, NULL); result = GMPy_MPZ_Digits_Method(temp, tuple); Py_DECREF(temp); Py_DECREF(tuple); return result; } if (IS_RATIONAL(arg0)) { temp = (PyObject*)GMPy_MPQ_From_Rational(arg0, NULL); result = GMPy_MPQ_Digits_Method(temp, tuple); Py_DECREF(temp); Py_DECREF(tuple); return result; } if (IS_REAL(arg0)) { temp = (PyObject*)GMPy_MPFR_From_Real(arg0, 1, NULL); result = GMPy_MPFR_Digits_Method(temp, tuple); Py_DECREF(temp); Py_DECREF(tuple); return result; } if (IS_COMPLEX(arg0)) { temp = (PyObject*)GMPy_MPC_From_Complex(arg0, 1, 1, NULL); result = GMPy_MPC_Digits_Method(temp, tuple); Py_DECREF(temp); Py_DECREF(tuple); return result; } TYPE_ERROR("digits() argument type not supported"); return NULL; }
static PyObject * GMPy_Complex_Sub(PyObject *x, PyObject *y, CTXT_Object *context) { MPC_Object *result = NULL; CHECK_CONTEXT(context); if (!(result = GMPy_MPC_New(0, 0, context))) return NULL; if (MPC_Check(x) && MPC_Check(y)) { result->rc = mpc_sub(result->c, MPC(x), MPC(y), GET_MPC_ROUND(context)); goto done; } if (IS_COMPLEX(x) && IS_COMPLEX(y)) { MPC_Object *tempx, *tempy; tempx = GMPy_MPC_From_Complex(x, 1, 1, context); tempy = GMPy_MPC_From_Complex(y, 1, 1, context); if (!tempx || !tempy) { Py_XDECREF((PyObject*)tempx); Py_XDECREF((PyObject*)tempy); Py_DECREF((PyObject*)result); return NULL; } result->rc = mpc_sub(result->c, tempx->c, tempy->c, GET_MPC_ROUND(context)); Py_DECREF((PyObject*)tempx); Py_DECREF((PyObject*)tempy); goto done; } Py_DECREF((PyObject*)result); Py_RETURN_NOTIMPLEMENTED; done: GMPY_MPC_CLEANUP(result, context, "subtraction"); return (PyObject*)result; }
int GMPy_MPC_convert_arg(PyObject *arg, PyObject **ptr) { MPC_Object *newob = GMPy_MPC_From_Complex(arg, 0, 0, NULL); if (newob) { *ptr = (PyObject*)newob; return 1; } else { TYPE_ERROR("can't convert argument to 'mpc'"); return 0; } }
static PyObject * GMPy_Complex_Sin_Cos(PyObject *x, CTXT_Object *context) { PyObject *result, *tempx; CHECK_CONTEXT(context); if (!(tempx = (PyObject*)GMPy_MPC_From_Complex(x, 1, 1, context))) { return NULL; } result = _GMPy_MPC_Sin_Cos(tempx, context); Py_DECREF(tempx); return result; }
static PyObject * GMPy_Complex_Proj(PyObject *x, CTXT_Object *context) { MPC_Object *result, *tempx; CHECK_CONTEXT(context); result = GMPy_MPC_New(0, 0, context); tempx = GMPy_MPC_From_Complex(x, 1, 1, context); if (!result || !tempx) { return NULL; } result->rc = mpc_proj(result->c, tempx->c, GET_MPC_ROUND(context)); Py_DECREF((PyObject*)tempx); GMPY_MPC_CLEANUP(result, context, "proj()"); return (PyObject*)result; }
static PyObject * GMPy_Complex_Norm(PyObject *x, CTXT_Object *context) { MPFR_Object *result; MPC_Object *tempx; CHECK_CONTEXT(context); result = GMPy_MPFR_New(0, context); tempx = GMPy_MPC_From_Complex(x, 1, 1, context); if (!result || !tempx) { return NULL; } mpfr_clear_flags(); result->rc = mpc_norm(result->f, tempx->c, GET_MPFR_ROUND(context)); Py_DECREF((PyObject*)tempx); GMPY_MPFR_CLEANUP(result, context, "norm()"); return (PyObject*)result; }
static PyObject * GMPy_Complex_Pow(PyObject *base, PyObject *exp, PyObject *mod, CTXT_Object *context) { MPC_Object *tempb = NULL, *tempe = NULL, *result= NULL; MPFR_Object *tempf = NULL; MPZ_Object *tempz = NULL; if (mod != Py_None) { TYPE_ERROR("pow() 3rd argument not allowed unless all arguments are integers"); return NULL; } CHECK_CONTEXT(context); result = GMPy_MPC_New(0, 0, context); tempb = GMPy_MPC_From_Complex(base, 1, 1, context); if (!result || !tempb) { goto err; } mpfr_clear_flags(); if (PyIntOrLong_Check(exp)) { int error; long temp_exp = GMPy_Integer_AsLongAndError(exp, &error); if (!error) { result->rc = mpc_pow_si(result->c, tempb->c, temp_exp, GET_MPC_ROUND(context)); } else { mpz_t tempzz; mpz_inoc(tempzz); mpz_set_PyIntOrLong(tempzz, exp); result->rc = mpc_pow_z(result->c, tempb->c, tempzz, GET_MPC_ROUND(context)); mpz_cloc(tempzz); } } else if (IS_INTEGER(exp)) { if (!(tempz = GMPy_MPZ_From_Integer(exp, context))) { goto err; } result->rc = mpc_pow_z(result->c, tempb->c, tempz->z, GET_MPC_ROUND(context)); } else if (IS_REAL(exp)) { if (!(tempf = GMPy_MPFR_From_Real(exp, 1, context))) { goto err; } result->rc = mpc_pow_fr(result->c, tempb->c, tempf->f, GET_MPC_ROUND(context)); } else { if (!(tempe = GMPy_MPC_From_Complex(exp, 1, 1, context))) { goto err; } result->rc = mpc_pow(result->c, tempb->c, tempe->c, GET_MPC_ROUND(context)); } GMPY_MPC_CLEANUP(result, context, "pow()"); Py_XDECREF((PyObject*)tempz); Py_XDECREF((PyObject*)tempf); Py_XDECREF((PyObject*)tempe); Py_XDECREF((PyObject*)tempb); return (PyObject*)result; err: Py_XDECREF((PyObject*)result); Py_XDECREF((PyObject*)tempz); Py_XDECREF((PyObject*)tempf); Py_XDECREF((PyObject*)tempe); Py_XDECREF((PyObject*)tempb); return NULL; }