mirror of
https://github.com/python/cpython.git
synced 2025-09-26 18:29:57 +00:00
complex -> Py_complex
This commit is contained in:
parent
f5030abca8
commit
9e720e390a
2 changed files with 96 additions and 96 deletions
|
@ -29,137 +29,137 @@
|
||||||
/* First, the C functions that do the real work */
|
/* First, the C functions that do the real work */
|
||||||
|
|
||||||
/* constants */
|
/* constants */
|
||||||
static complex c_1 = {1., 0.};
|
static Py_complex c_1 = {1., 0.};
|
||||||
static complex c_half = {0.5, 0.};
|
static Py_complex c_half = {0.5, 0.};
|
||||||
static complex c_i = {0., 1.};
|
static Py_complex c_i = {0., 1.};
|
||||||
static complex c_i2 = {0., 0.5};
|
static Py_complex c_i2 = {0., 0.5};
|
||||||
static complex c_mi = {0., -1.};
|
static Py_complex c_mi = {0., -1.};
|
||||||
static complex c_pi2 = {M_PI/2., 0.};
|
static Py_complex c_pi2 = {M_PI/2., 0.};
|
||||||
|
|
||||||
/* forward declarations */
|
/* forward declarations */
|
||||||
staticforward complex c_log();
|
staticforward Py_complex c_log();
|
||||||
staticforward complex c_prodi();
|
staticforward Py_complex c_prodi();
|
||||||
staticforward complex c_sqrt();
|
staticforward Py_complex c_sqrt();
|
||||||
|
|
||||||
|
|
||||||
static complex c_acos(x)
|
static Py_complex c_acos(x)
|
||||||
complex x;
|
Py_complex x;
|
||||||
{
|
{
|
||||||
return c_neg(c_prodi(c_log(c_sum(x,c_prod(c_i,
|
return c_neg(c_prodi(c_log(c_sum(x,c_prod(c_i,
|
||||||
c_sqrt(c_diff(c_1,c_prod(x,x))))))));
|
c_sqrt(c_diff(c_1,c_prod(x,x))))))));
|
||||||
}
|
}
|
||||||
|
|
||||||
static complex c_acosh(x)
|
static Py_complex c_acosh(x)
|
||||||
complex x;
|
Py_complex x;
|
||||||
{
|
{
|
||||||
return c_log(c_sum(x,c_prod(c_i,
|
return c_log(c_sum(x,c_prod(c_i,
|
||||||
c_sqrt(c_diff(c_1,c_prod(x,x))))));
|
c_sqrt(c_diff(c_1,c_prod(x,x))))));
|
||||||
}
|
}
|
||||||
|
|
||||||
static complex c_asin(x)
|
static Py_complex c_asin(x)
|
||||||
complex x;
|
Py_complex x;
|
||||||
{
|
{
|
||||||
return c_neg(c_prodi(c_log(c_sum(c_prod(c_i,x),
|
return c_neg(c_prodi(c_log(c_sum(c_prod(c_i,x),
|
||||||
c_sqrt(c_diff(c_1,c_prod(x,x)))))));
|
c_sqrt(c_diff(c_1,c_prod(x,x)))))));
|
||||||
}
|
}
|
||||||
|
|
||||||
static complex c_asinh(x)
|
static Py_complex c_asinh(x)
|
||||||
complex x;
|
Py_complex x;
|
||||||
{
|
{
|
||||||
return c_neg(c_log(c_diff(c_sqrt(c_sum(c_1,c_prod(x,x))),x)));
|
return c_neg(c_log(c_diff(c_sqrt(c_sum(c_1,c_prod(x,x))),x)));
|
||||||
}
|
}
|
||||||
|
|
||||||
static complex c_atan(x)
|
static Py_complex c_atan(x)
|
||||||
complex x;
|
Py_complex x;
|
||||||
{
|
{
|
||||||
return c_prod(c_i2,c_log(c_quot(c_sum(c_i,x),c_diff(c_i,x))));
|
return c_prod(c_i2,c_log(c_quot(c_sum(c_i,x),c_diff(c_i,x))));
|
||||||
}
|
}
|
||||||
|
|
||||||
static complex c_atanh(x)
|
static Py_complex c_atanh(x)
|
||||||
complex x;
|
Py_complex x;
|
||||||
{
|
{
|
||||||
return c_prod(c_half,c_log(c_quot(c_sum(c_1,x),c_diff(c_1,x))));
|
return c_prod(c_half,c_log(c_quot(c_sum(c_1,x),c_diff(c_1,x))));
|
||||||
}
|
}
|
||||||
|
|
||||||
static complex c_cos(x)
|
static Py_complex c_cos(x)
|
||||||
complex x;
|
Py_complex x;
|
||||||
{
|
{
|
||||||
complex r;
|
Py_complex r;
|
||||||
r.real = cos(x.real)*cosh(x.imag);
|
r.real = cos(x.real)*cosh(x.imag);
|
||||||
r.imag = -sin(x.real)*sinh(x.imag);
|
r.imag = -sin(x.real)*sinh(x.imag);
|
||||||
return r;
|
return r;
|
||||||
}
|
}
|
||||||
|
|
||||||
static complex c_cosh(x)
|
static Py_complex c_cosh(x)
|
||||||
complex x;
|
Py_complex x;
|
||||||
{
|
{
|
||||||
complex r;
|
Py_complex r;
|
||||||
r.real = cos(x.imag)*cosh(x.real);
|
r.real = cos(x.imag)*cosh(x.real);
|
||||||
r.imag = sin(x.imag)*sinh(x.real);
|
r.imag = sin(x.imag)*sinh(x.real);
|
||||||
return r;
|
return r;
|
||||||
}
|
}
|
||||||
|
|
||||||
static complex c_exp(x)
|
static Py_complex c_exp(x)
|
||||||
complex x;
|
Py_complex x;
|
||||||
{
|
{
|
||||||
complex r;
|
Py_complex r;
|
||||||
double l = exp(x.real);
|
double l = exp(x.real);
|
||||||
r.real = l*cos(x.imag);
|
r.real = l*cos(x.imag);
|
||||||
r.imag = l*sin(x.imag);
|
r.imag = l*sin(x.imag);
|
||||||
return r;
|
return r;
|
||||||
}
|
}
|
||||||
|
|
||||||
static complex c_log(x)
|
static Py_complex c_log(x)
|
||||||
complex x;
|
Py_complex x;
|
||||||
{
|
{
|
||||||
complex r;
|
Py_complex r;
|
||||||
double l = hypot(x.real,x.imag);
|
double l = hypot(x.real,x.imag);
|
||||||
r.imag = atan2(x.imag, x.real);
|
r.imag = atan2(x.imag, x.real);
|
||||||
r.real = log(l);
|
r.real = log(l);
|
||||||
return r;
|
return r;
|
||||||
}
|
}
|
||||||
|
|
||||||
static complex c_log10(x)
|
static Py_complex c_log10(x)
|
||||||
complex x;
|
Py_complex x;
|
||||||
{
|
{
|
||||||
complex r;
|
Py_complex r;
|
||||||
double l = hypot(x.real,x.imag);
|
double l = hypot(x.real,x.imag);
|
||||||
r.imag = atan2(x.imag, x.real)/log(10.);
|
r.imag = atan2(x.imag, x.real)/log(10.);
|
||||||
r.real = log10(l);
|
r.real = log10(l);
|
||||||
return r;
|
return r;
|
||||||
}
|
}
|
||||||
|
|
||||||
static complex c_prodi(x)
|
static Py_complex c_prodi(x)
|
||||||
complex x;
|
Py_complex x;
|
||||||
{
|
{
|
||||||
complex r;
|
Py_complex r;
|
||||||
r.real = -x.imag;
|
r.real = -x.imag;
|
||||||
r.imag = x.real;
|
r.imag = x.real;
|
||||||
return r;
|
return r;
|
||||||
}
|
}
|
||||||
|
|
||||||
static complex c_sin(x)
|
static Py_complex c_sin(x)
|
||||||
complex x;
|
Py_complex x;
|
||||||
{
|
{
|
||||||
complex r;
|
Py_complex r;
|
||||||
r.real = sin(x.real)*cosh(x.imag);
|
r.real = sin(x.real)*cosh(x.imag);
|
||||||
r.imag = cos(x.real)*sinh(x.imag);
|
r.imag = cos(x.real)*sinh(x.imag);
|
||||||
return r;
|
return r;
|
||||||
}
|
}
|
||||||
|
|
||||||
static complex c_sinh(x)
|
static Py_complex c_sinh(x)
|
||||||
complex x;
|
Py_complex x;
|
||||||
{
|
{
|
||||||
complex r;
|
Py_complex r;
|
||||||
r.real = cos(x.imag)*sinh(x.real);
|
r.real = cos(x.imag)*sinh(x.real);
|
||||||
r.imag = sin(x.imag)*cosh(x.real);
|
r.imag = sin(x.imag)*cosh(x.real);
|
||||||
return r;
|
return r;
|
||||||
}
|
}
|
||||||
|
|
||||||
static complex c_sqrt(x)
|
static Py_complex c_sqrt(x)
|
||||||
complex x;
|
Py_complex x;
|
||||||
{
|
{
|
||||||
complex r;
|
Py_complex r;
|
||||||
double s,d;
|
double s,d;
|
||||||
if (x.real == 0. && x.imag == 0.)
|
if (x.real == 0. && x.imag == 0.)
|
||||||
r = x;
|
r = x;
|
||||||
|
@ -182,10 +182,10 @@ static complex c_sqrt(x)
|
||||||
return r;
|
return r;
|
||||||
}
|
}
|
||||||
|
|
||||||
static complex c_tan(x)
|
static Py_complex c_tan(x)
|
||||||
complex x;
|
Py_complex x;
|
||||||
{
|
{
|
||||||
complex r;
|
Py_complex r;
|
||||||
double sr,cr,shi,chi;
|
double sr,cr,shi,chi;
|
||||||
double rs,is,rc,ic;
|
double rs,is,rc,ic;
|
||||||
double d;
|
double d;
|
||||||
|
@ -203,10 +203,10 @@ static complex c_tan(x)
|
||||||
return r;
|
return r;
|
||||||
}
|
}
|
||||||
|
|
||||||
static complex c_tanh(x)
|
static Py_complex c_tanh(x)
|
||||||
complex x;
|
Py_complex x;
|
||||||
{
|
{
|
||||||
complex r;
|
Py_complex r;
|
||||||
double si,ci,shr,chr;
|
double si,ci,shr,chr;
|
||||||
double rs,is,rc,ic;
|
double rs,is,rc,ic;
|
||||||
double d;
|
double d;
|
||||||
|
@ -242,9 +242,9 @@ math_error()
|
||||||
static object *
|
static object *
|
||||||
math_1(args, func)
|
math_1(args, func)
|
||||||
object *args;
|
object *args;
|
||||||
complex (*func) FPROTO((complex));
|
Py_complex (*func) FPROTO((Py_complex));
|
||||||
{
|
{
|
||||||
complex x;
|
Py_complex x;
|
||||||
if (!PyArg_ParseTuple(args, "D", &x))
|
if (!PyArg_ParseTuple(args, "D", &x))
|
||||||
return NULL;
|
return NULL;
|
||||||
errno = 0;
|
errno = 0;
|
||||||
|
|
|
@ -56,48 +56,48 @@ extern double pow PROTO((double, double));
|
||||||
/* elementary operations on complex numbers */
|
/* elementary operations on complex numbers */
|
||||||
|
|
||||||
static int c_error;
|
static int c_error;
|
||||||
static complex c_1 = {1., 0.};
|
static Py_complex c_1 = {1., 0.};
|
||||||
|
|
||||||
complex c_sum(a,b)
|
Py_complex c_sum(a,b)
|
||||||
complex a,b;
|
Py_complex a,b;
|
||||||
{
|
{
|
||||||
complex r;
|
Py_complex r;
|
||||||
r.real = a.real + b.real;
|
r.real = a.real + b.real;
|
||||||
r.imag = a.imag + b.imag;
|
r.imag = a.imag + b.imag;
|
||||||
return r;
|
return r;
|
||||||
}
|
}
|
||||||
|
|
||||||
complex c_diff(a,b)
|
Py_complex c_diff(a,b)
|
||||||
complex a,b;
|
Py_complex a,b;
|
||||||
{
|
{
|
||||||
complex r;
|
Py_complex r;
|
||||||
r.real = a.real - b.real;
|
r.real = a.real - b.real;
|
||||||
r.imag = a.imag - b.imag;
|
r.imag = a.imag - b.imag;
|
||||||
return r;
|
return r;
|
||||||
}
|
}
|
||||||
|
|
||||||
complex c_neg(a)
|
Py_complex c_neg(a)
|
||||||
complex a;
|
Py_complex a;
|
||||||
{
|
{
|
||||||
complex r;
|
Py_complex r;
|
||||||
r.real = -a.real;
|
r.real = -a.real;
|
||||||
r.imag = -a.imag;
|
r.imag = -a.imag;
|
||||||
return r;
|
return r;
|
||||||
}
|
}
|
||||||
|
|
||||||
complex c_prod(a,b)
|
Py_complex c_prod(a,b)
|
||||||
complex a,b;
|
Py_complex a,b;
|
||||||
{
|
{
|
||||||
complex r;
|
Py_complex r;
|
||||||
r.real = a.real*b.real - a.imag*b.imag;
|
r.real = a.real*b.real - a.imag*b.imag;
|
||||||
r.imag = a.real*b.imag + a.imag*b.real;
|
r.imag = a.real*b.imag + a.imag*b.real;
|
||||||
return r;
|
return r;
|
||||||
}
|
}
|
||||||
|
|
||||||
complex c_quot(a,b)
|
Py_complex c_quot(a,b)
|
||||||
complex a,b;
|
Py_complex a,b;
|
||||||
{
|
{
|
||||||
complex r;
|
Py_complex r;
|
||||||
double d = b.real*b.real + b.imag*b.imag;
|
double d = b.real*b.real + b.imag*b.imag;
|
||||||
if (d == 0.)
|
if (d == 0.)
|
||||||
c_error = 1;
|
c_error = 1;
|
||||||
|
@ -106,10 +106,10 @@ complex c_quot(a,b)
|
||||||
return r;
|
return r;
|
||||||
}
|
}
|
||||||
|
|
||||||
complex c_pow(a,b)
|
Py_complex c_pow(a,b)
|
||||||
complex a,b;
|
Py_complex a,b;
|
||||||
{
|
{
|
||||||
complex r;
|
Py_complex r;
|
||||||
double vabs,len,at,phase;
|
double vabs,len,at,phase;
|
||||||
if (b.real == 0. && b.imag == 0.) {
|
if (b.real == 0. && b.imag == 0.) {
|
||||||
r.real = 1.;
|
r.real = 1.;
|
||||||
|
@ -136,12 +136,12 @@ complex c_pow(a,b)
|
||||||
return r;
|
return r;
|
||||||
}
|
}
|
||||||
|
|
||||||
static complex c_powu(x, n)
|
static Py_complex c_powu(x, n)
|
||||||
complex x;
|
Py_complex x;
|
||||||
long n;
|
long n;
|
||||||
{
|
{
|
||||||
complex r = c_1;
|
Py_complex r = c_1;
|
||||||
complex p = x;
|
Py_complex p = x;
|
||||||
long mask = 1;
|
long mask = 1;
|
||||||
while (mask > 0 && n >= mask) {
|
while (mask > 0 && n >= mask) {
|
||||||
if (n & mask)
|
if (n & mask)
|
||||||
|
@ -152,11 +152,11 @@ static complex c_powu(x, n)
|
||||||
return r;
|
return r;
|
||||||
}
|
}
|
||||||
|
|
||||||
static complex c_powi(x, n)
|
static Py_complex c_powi(x, n)
|
||||||
complex x;
|
Py_complex x;
|
||||||
long n;
|
long n;
|
||||||
{
|
{
|
||||||
complex cn;
|
Py_complex cn;
|
||||||
|
|
||||||
if (n > 100 || n < -100) {
|
if (n > 100 || n < -100) {
|
||||||
cn.real = (double) n;
|
cn.real = (double) n;
|
||||||
|
@ -171,7 +171,7 @@ static complex c_powi(x, n)
|
||||||
}
|
}
|
||||||
|
|
||||||
PyObject *
|
PyObject *
|
||||||
PyComplex_FromCComplex(complex cval)
|
PyComplex_FromCComplex(Py_complex cval)
|
||||||
{
|
{
|
||||||
register complexobject *op = (complexobject *) malloc(sizeof(complexobject));
|
register complexobject *op = (complexobject *) malloc(sizeof(complexobject));
|
||||||
if (op == NULL)
|
if (op == NULL)
|
||||||
|
@ -184,7 +184,7 @@ PyComplex_FromCComplex(complex cval)
|
||||||
|
|
||||||
PyObject *
|
PyObject *
|
||||||
PyComplex_FromDoubles(double real, double imag) {
|
PyComplex_FromDoubles(double real, double imag) {
|
||||||
complex c;
|
Py_complex c;
|
||||||
c.real = real;
|
c.real = real;
|
||||||
c.imag = imag;
|
c.imag = imag;
|
||||||
return PyComplex_FromCComplex(c);
|
return PyComplex_FromCComplex(c);
|
||||||
|
@ -208,9 +208,9 @@ PyComplex_ImagAsDouble(PyObject *op) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
complex
|
Py_complex
|
||||||
PyComplex_AsCComplex(PyObject *op) {
|
PyComplex_AsCComplex(PyObject *op) {
|
||||||
complex cv;
|
Py_complex cv;
|
||||||
if (PyComplex_Check(op)) {
|
if (PyComplex_Check(op)) {
|
||||||
return ((PyComplexObject *)op)->cval;
|
return ((PyComplexObject *)op)->cval;
|
||||||
} else {
|
} else {
|
||||||
|
@ -266,8 +266,8 @@ complex_compare(v, w)
|
||||||
{
|
{
|
||||||
/* Note: "greater" and "smaller" have no meaning for complex numbers,
|
/* Note: "greater" and "smaller" have no meaning for complex numbers,
|
||||||
but Python requires that they be defined nevertheless. */
|
but Python requires that they be defined nevertheless. */
|
||||||
complex i = v->cval;
|
Py_complex i = v->cval;
|
||||||
complex j = w->cval;
|
Py_complex j = w->cval;
|
||||||
if (i.real == j.real && i.imag == j.imag)
|
if (i.real == j.real && i.imag == j.imag)
|
||||||
return 0;
|
return 0;
|
||||||
else if (i.real != j.real)
|
else if (i.real != j.real)
|
||||||
|
@ -348,7 +348,7 @@ complex_div(v, w)
|
||||||
complexobject *v;
|
complexobject *v;
|
||||||
complexobject *w;
|
complexobject *w;
|
||||||
{
|
{
|
||||||
complex quot;
|
Py_complex quot;
|
||||||
c_error = 0;
|
c_error = 0;
|
||||||
quot = c_quot(v->cval,w->cval);
|
quot = c_quot(v->cval,w->cval);
|
||||||
if (c_error == 1) {
|
if (c_error == 1) {
|
||||||
|
@ -365,8 +365,8 @@ complex_pow(v, w, z)
|
||||||
object *w;
|
object *w;
|
||||||
complexobject *z;
|
complexobject *z;
|
||||||
{
|
{
|
||||||
complex p;
|
Py_complex p;
|
||||||
complex exponent;
|
Py_complex exponent;
|
||||||
long int_exponent;
|
long int_exponent;
|
||||||
|
|
||||||
if ((object *)z!=None) {
|
if ((object *)z!=None) {
|
||||||
|
@ -394,7 +394,7 @@ static object *
|
||||||
complex_neg(v)
|
complex_neg(v)
|
||||||
complexobject *v;
|
complexobject *v;
|
||||||
{
|
{
|
||||||
complex neg;
|
Py_complex neg;
|
||||||
neg.real = -v->cval.real;
|
neg.real = -v->cval.real;
|
||||||
neg.imag = -v->cval.imag;
|
neg.imag = -v->cval.imag;
|
||||||
return newcomplexobject(neg);
|
return newcomplexobject(neg);
|
||||||
|
@ -427,7 +427,7 @@ complex_coerce(pv, pw)
|
||||||
object **pv;
|
object **pv;
|
||||||
object **pw;
|
object **pw;
|
||||||
{
|
{
|
||||||
complex cval;
|
Py_complex cval;
|
||||||
cval.imag = 0.;
|
cval.imag = 0.;
|
||||||
if (is_intobject(*pw)) {
|
if (is_intobject(*pw)) {
|
||||||
cval.real = (double)getintvalue(*pw);
|
cval.real = (double)getintvalue(*pw);
|
||||||
|
@ -485,7 +485,7 @@ complex_new(self, args)
|
||||||
object *self;
|
object *self;
|
||||||
object *args;
|
object *args;
|
||||||
{
|
{
|
||||||
complex cval;
|
Py_complex cval;
|
||||||
|
|
||||||
cval.imag = 0.;
|
cval.imag = 0.;
|
||||||
if (!PyArg_ParseTuple(args, "d|d", &cval.real, &cval.imag))
|
if (!PyArg_ParseTuple(args, "d|d", &cval.real, &cval.imag))
|
||||||
|
@ -497,7 +497,7 @@ static object *
|
||||||
complex_conjugate(self)
|
complex_conjugate(self)
|
||||||
object *self;
|
object *self;
|
||||||
{
|
{
|
||||||
complex c = ((complexobject *)self)->cval;
|
Py_complex c = ((complexobject *)self)->cval;
|
||||||
c.imag = -c.imag;
|
c.imag = -c.imag;
|
||||||
return newcomplexobject(c);
|
return newcomplexobject(c);
|
||||||
}
|
}
|
||||||
|
@ -513,7 +513,7 @@ complex_getattr(self, name)
|
||||||
complexobject *self;
|
complexobject *self;
|
||||||
char *name;
|
char *name;
|
||||||
{
|
{
|
||||||
complex cval;
|
Py_complex cval;
|
||||||
if (strcmp(name, "real") == 0)
|
if (strcmp(name, "real") == 0)
|
||||||
return (object *)newfloatobject(self->cval.real);
|
return (object *)newfloatobject(self->cval.real);
|
||||||
else if (strcmp(name, "imag") == 0)
|
else if (strcmp(name, "imag") == 0)
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue