The R Project SVN R

Rev

Rev 88828 | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 88828 Rev 88866
Line 61... Line 61...
61
#include <Defn.h>		/* -> ../include/R_ext/Complex.h */
61
#include <Defn.h>		/* -> ../include/R_ext/Complex.h */
62
#include <Internal.h>
62
#include <Internal.h>
63
#include <Rmath.h>
63
#include <Rmath.h>
64
 
64
 
65
#include "arithmetic.h"		/* complex_*  */
65
#include "arithmetic.h"		/* complex_*  */
66
#include <complex.h>
66
#include <complex.h>            /* incl "complex math.h" */
67
#include "Rcomplex.h"		/* I, SET_C99_COMPLEX, toC99 */
67
#include "Rcomplex.h"		/* I, SET_C99_COMPLEX, toC99 */
68
#include <R_ext/Itermacros.h>
68
#include <R_ext/Itermacros.h>
69
 
69
 
70
 
70
 
71
/* interval at which to check interrupts, a guess */
71
/* interval at which to check interrupts, a guess */
Line 460... Line 460...
460
#ifndef HAVE_CASIN
460
#ifndef HAVE_CASIN
461
#define casin R_casin
461
#define casin R_casin
462
static double complex casin(double complex z)
462
static double complex casin(double complex z)
463
{
463
{
464
    /* A&S 4.4.37 */
464
    /* A&S 4.4.37 */
465
    double alpha, t1, t2, x = creal(z), y = cimag(z), ri;
465
    double x = creal(z), y = cimag(z),
466
    t1 = 0.5 * hypot(x + 1, y);
466
	t1 = 0.5 * hypot(x + 1, y),
467
    t2 = 0.5 * hypot(x - 1, y);
467
	t2 = 0.5 * hypot(x - 1, y),
468
    alpha = t1 + t2;
468
	alpha = t1 + t2,
469
    ri = log(alpha + sqrt(alpha*alpha - 1));
469
	ri = log(alpha + sqrt(alpha*alpha - 1));
470
    /* This comes from
470
    /* This comes from
471
       'z_asin() is continuous from below if x >= 1
471
       'z_asin() is continuous from below if x >= 1
472
	and continuous from above if x <= -1.'
472
	and continuous from above if x <= -1.'
473
    */
473
    */
474
    if(y < 0 || (y == 0 && x > 1)) ri *= -1;
474
    if(y < 0 || (y == 0 && x > 1)) ri *= -1;
Line 583... Line 583...
583
static double complex z_atanh(double complex z)
583
static double complex z_atanh(double complex z)
584
{
584
{
585
    return -I * z_atan(z * I);
585
    return -I * z_atan(z * I);
586
}
586
}
587
 
587
 
-
 
588
#ifdef HAVE_CABS
-
 
589
# define R_CABS(Z) cabs(Z)
-
 
590
#else
-
 
591
# define R_CABS(Z) hypot(creal(Z), cimag(Z))
-
 
592
#endif
-
 
593
 
-
 
594
 
588
static bool cmath1(double complex (*f)(double complex),
595
static bool cmath1(double complex (*f)(double complex),
589
		       const Rcomplex *x, Rcomplex *y, R_xlen_t n)
596
		       const Rcomplex *x, Rcomplex *y, R_xlen_t n)
590
{
597
{
591
    R_xlen_t i;
598
    R_xlen_t i;
592
    bool naflag = false;
599
    bool naflag = false;
Line 690... Line 697...
690
	/* Complex Functions of Two Arguments */
697
	/* Complex Functions of Two Arguments */
691
 
698
 
692
typedef void (*cm2_fun)(Rcomplex *, Rcomplex *, Rcomplex *);
699
typedef void (*cm2_fun)(Rcomplex *, Rcomplex *, Rcomplex *);
693
attribute_hidden SEXP complex_math2(SEXP call, SEXP op, SEXP args, SEXP env)
700
attribute_hidden SEXP complex_math2(SEXP call, SEXP op, SEXP args, SEXP env)
694
{
701
{
695
    R_xlen_t i, n, na, nb, ia, ib;
-
 
696
    Rcomplex ai, bi, *y;
-
 
697
    const Rcomplex *a, *b;
-
 
698
    SEXP sa, sb, sy;
-
 
699
    bool naflag = false;
-
 
700
    cm2_fun f;
702
    cm2_fun f;
701
 
-
 
702
    switch (PRIMVAL(op)) {
703
    switch (PRIMVAL(op)) {
703
    case 0: /* atan2 */
704
    case 0: /* atan2 */
704
	f = z_atan2; break;
705
	f = z_atan2; break;
705
    case 10001: /* round */
706
    case 10001: /* round */
706
	f = z_rround; break;
707
	f = z_rround; break;
707
    case 10002: /* passed from do_log1arg */
708
    case 10002: /* passed from do_log1arg */
708
    case 10010:
709
    case 10010: /*   "     "    " */
709
    case 10003: /* passed from do_log */
710
    case 10003: /* passed from do_log */
710
	f = z_logbase; break;
711
	f = z_logbase; break;
711
    case 10004: /* signif */
712
    case 10004: /* signif */
712
	f = z_prec; break;
713
	f = z_prec; break;
713
    default:
714
    default:
714
	error_return(_("unimplemented complex function"));
715
	error_return(_("unimplemented complex function"));
715
    }
716
    }
716
 
717
 
-
 
718
    SEXP
717
    PROTECT(sa = coerceVector(CAR(args), CPLXSXP));
719
	sa = PROTECT(coerceVector(CAR(args), CPLXSXP)),
718
    PROTECT(sb = coerceVector(CADR(args), CPLXSXP));
720
	sb = PROTECT(coerceVector(CADR(args), CPLXSXP));
719
    na = XLENGTH(sa); nb = XLENGTH(sb);
721
    R_xlen_t na = XLENGTH(sa), nb = XLENGTH(sb), n;
720
    if ((na == 0) || (nb == 0)) {
722
    if ((na == 0) || (nb == 0)) {
721
	UNPROTECT(2);
723
	UNPROTECT(2);
722
	return(allocVector(CPLXSXP, 0));
724
	return(allocVector(CPLXSXP, 0));
723
    }
725
    }
724
    n = (na < nb) ? nb : na;
726
    n = (na < nb) ? nb : na;
725
    PROTECT(sy = allocVector(CPLXSXP, n));
727
    SEXP sy = PROTECT(allocVector(CPLXSXP, n));
-
 
728
    const Rcomplex
726
    a = COMPLEX_RO(sa); b = COMPLEX_RO(sb);
729
	*a = COMPLEX_RO(sa),
727
    y = COMPLEX(sy);
730
	*b = COMPLEX_RO(sb);
-
 
731
    Rcomplex ai, bi, *y = COMPLEX(sy);
-
 
732
    R_xlen_t i, ia, ib;
-
 
733
    bool naflag = false;
728
    MOD_ITERATE2(n, na, nb, i, ia, ib, {
734
    MOD_ITERATE2(n, na, nb, i, ia, ib, {
729
	ai = a[ia]; bi = b[ib];
735
	ai = a[ia]; bi = b[ib];
730
	if(ISNA(ai.r) && ISNA(ai.i) &&
736
	if(ISNA(ai.r) && ISNA(ai.i) &&
731
	   ISNA(bi.r) && ISNA(bi.i)) {
737
	   ISNA(bi.r) && ISNA(bi.i)) {
732
	    y[i].r = NA_REAL; y[i].i = NA_REAL;
738
	    y[i].r = NA_REAL; y[i].i = NA_REAL;
Line 789... Line 795...
789
static void R_cpolyroot(double *opr, double *opi, int *degree,
795
static void R_cpolyroot(double *opr, double *opi, int *degree,
790
			double *zeror, double *zeroi, bool *fail);
796
			double *zeror, double *zeroi, bool *fail);
791
 
797
 
792
attribute_hidden SEXP do_polyroot(SEXP call, SEXP op, SEXP args, SEXP rho)
798
attribute_hidden SEXP do_polyroot(SEXP call, SEXP op, SEXP args, SEXP rho)
793
{
799
{
794
    SEXP z, zr, zi, r, rr, ri;
-
 
795
    bool fail;
-
 
796
    int degree, i, n;
-
 
797
 
-
 
798
    checkArity(op, args);
800
    checkArity(op, args);
799
    z = CAR(args);
801
    SEXP z = CAR(args);
800
    switch(TYPEOF(z)) {
802
    switch(TYPEOF(z)) {
801
    case CPLXSXP:
803
    case CPLXSXP:
802
	PROTECT(z);
804
	PROTECT(z);
803
	break;
805
	break;
804
    case REALSXP:
806
    case REALSXP:
Line 807... Line 809...
807
	PROTECT(z = coerceVector(z, CPLXSXP));
809
	PROTECT(z = coerceVector(z, CPLXSXP));
808
	break;
810
	break;
809
    default:
811
    default:
810
	UNIMPLEMENTED_TYPE("polyroot", z);
812
	UNIMPLEMENTED_TYPE("polyroot", z);
811
    }
813
    }
-
 
814
 
-
 
815
    int i, n;
812
#ifdef LONG_VECTOR_SUPPORT
816
#ifdef LONG_VECTOR_SUPPORT
813
    R_xlen_t nn = XLENGTH(z);
817
    R_xlen_t nn = XLENGTH(z);
814
    if (nn > R_SHORT_LEN_MAX) error("long vectors are not supported");
818
    if (nn > R_SHORT_LEN_MAX) error("long vectors are not supported");
815
    n = (int) nn;
819
    n = (int) nn;
816
#else
820
#else
817
    n = LENGTH(z);
821
    n = LENGTH(z);
818
#endif
822
#endif
819
    const Rcomplex *pz = COMPLEX_RO(z);
823
    const Rcomplex *pz = COMPLEX_RO(z);
820
    degree = 0;
824
    int degree = 0; // := max{i; z[i] != 0}
821
    for(i = 0; i < n; i++) {
825
    for(i = 0; i < n; i++) {
822
	if(pz[i].r!= 0.0 || pz[i].i != 0.0) degree = i;
826
	if(pz[i].r!= 0.0 || pz[i].i != 0.0) degree = i;
823
    }
827
    }
824
    n = degree + 1; /* omit trailing zeroes */
828
    n = degree + 1; /* omit trailing zeroes */
-
 
829
    SEXP r;
825
    if(degree >= 1) {
830
    if(degree >= 1) {
-
 
831
	SEXP zr, zi, rr, ri;
826
	PROTECT(rr = allocVector(REALSXP, n));
832
	PROTECT(rr = allocVector(REALSXP, n));
827
	PROTECT(ri = allocVector(REALSXP, n));
833
	PROTECT(ri = allocVector(REALSXP, n));
828
	PROTECT(zr = allocVector(REALSXP, n));
834
	PROTECT(zr = allocVector(REALSXP, n));
829
	PROTECT(zi = allocVector(REALSXP, n));
835
	PROTECT(zi = allocVector(REALSXP, n));
830
 
836
 
Line 837... Line 843...
837
	    if(!R_FINITE(pz[i].r) || !R_FINITE(pz[i].i))
843
	    if(!R_FINITE(pz[i].r) || !R_FINITE(pz[i].i))
838
		error(_("invalid polynomial coefficient"));
844
		error(_("invalid polynomial coefficient"));
839
	    p_zr[degree-i] = pz[i].r;
845
	    p_zr[degree-i] = pz[i].r;
840
	    p_zi[degree-i] = pz[i].i;
846
	    p_zi[degree-i] = pz[i].i;
841
	}
847
	}
-
 
848
	bool fail;
842
	R_cpolyroot(p_zr, p_zi, &degree, p_rr, p_ri, &fail);
849
	R_cpolyroot(p_zr, p_zi, &degree, p_rr, p_ri, &fail);
843
	if(fail) error(_("root finding code failed"));
850
	if(fail) error(_("root finding code failed"));
844
	UNPROTECT(2);
851
	UNPROTECT(2);
845
	r = allocVector(CPLXSXP, degree);
852
	r = allocVector(CPLXSXP, degree);
846
	Rcomplex *pr = COMPLEX(r);
853
	Rcomplex *pr = COMPLEX(r);