The R Project SVN R

Rev

Rev 77216 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
45446 ripley 1
/*
42320 ripley 2
 *  R : A Computer Language for Statistical Data Analysis
67595 ripley 3
 *  Copyright (C) 1998--2014	    The R Core Team.
42320 ripley 4
 *
5
 *  This program is free software; you can redistribute it and/or modify
6
 *  it under the terms of the GNU General Public License as published by
7
 *  the Free Software Foundation; either version 2 of the License, or
8
 *  (at your option) any later version.
9
 *
10
 *  This program is distributed in the hope that it will be useful,
11
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
 *  GNU General Public License for more details.
14
 *
15
 *  You should have received a copy of the GNU General Public License
16
 *  along with this program; if not, a copy is available at
68947 ripley 17
 *  https://www.R-project.org/Licenses/
42320 ripley 18
 */
19
 
2632 maechler 20
SEXP do_math1(SEXP, SEXP, SEXP, SEXP);
21
SEXP do_math2(SEXP, SEXP, SEXP, SEXP);
22
SEXP do_math3(SEXP, SEXP, SEXP, SEXP);
23
SEXP do_math4(SEXP, SEXP, SEXP, SEXP);
7612 maechler 24
#ifdef WHEN_MATH5_IS_THERE
25
 SEXP do_math5(SEXP, SEXP, SEXP, SEXP);
26
#endif
2632 maechler 27
SEXP do_cmathfuns(SEXP, SEXP, SEXP, SEXP);
28
 
29
SEXP complex_math1(SEXP, SEXP, SEXP, SEXP);
30
SEXP complex_math2(SEXP, SEXP, SEXP, SEXP);
41713 ripley 31
SEXP complex_unary(ARITHOP_TYPE, SEXP, SEXP);
10718 maechler 32
SEXP complex_binary(ARITHOP_TYPE, SEXP, SEXP);
63462 luke 33
 
66605 luke 34
double R_pow(double x, double y);
35
static R_INLINE double R_POW(double x, double y) /* handle x ^ 2 inline */
36
{
37
    return y == 2.0 ? x * x : R_pow(x, y);
38
}
39
 
40
/* some systems get this wrong, possibly depend on what libs are loaded */
41
static R_INLINE double R_log(double x) {
69953 luke 42
    return x > 0 ? log(x) : x == 0 ? R_NegInf : R_NaN;
66605 luke 43
}
44
 
45
/* Note that the behaviour of log(0) required is not necessarily that
46
   mandated by C99 (-HUGE_VAL), and the behaviour of log(x < 0) is
47
   optional in C99.  Some systems return -Inf for log(x < 0), e.g.
48
   libsunmath on Solaris.
49
*/
50
static R_INLINE double logbase(double x, double base)
51
{
52
#ifdef HAVE_LOG10
69963 luke 53
    if(base == 10) return x > 0 ? log10(x) : x == 0 ? R_NegInf : R_NaN;
66605 luke 54
#endif
55
#ifdef HAVE_LOG2
69963 luke 56
    if(base == 2) return x > 0 ? log2(x) : x == 0 ? R_NegInf : R_NaN;
66605 luke 57
#endif
58
    return R_log(x) / R_log(base);
59
}
60
 
61
SEXP do_log_builtin(SEXP call, SEXP op, SEXP args, SEXP env);
62
 
63462 luke 63
/* for binary operations */
63543 luke 64
/* adapted from Radford Neal's pqR */
63462 luke 65
static R_INLINE SEXP R_allocOrReuseVector(SEXP s1, SEXP s2,
66
					  SEXPTYPE type , R_xlen_t n)
67
{
68
    R_xlen_t n1 = XLENGTH(s1);
69
    R_xlen_t n2 = XLENGTH(s2);
70
 
71
    /* Try to use space for 2nd arg if both same length, so 1st argument's
72
       attributes will then take precedence when copied. */
73
 
74
    if (n == n2) {
65020 luke 75
        if (TYPEOF(s2) == type && NO_REFERENCES(s2)) {
63543 luke 76
	    if (ATTRIB(s2) != R_NilValue)
77
		/* need to remove 'names' attribute if present to
77216 maechler 78
		   match what copyMostAttrib does. copyMostAttrib()
63543 luke 79
		   also skips 'dim' and 'dimnames' but those, here
80
		   since those, if present, will be replaced by
81
		   attribute cleanup code in R_Binary) */
63511 luke 82
		setAttrib(s2, R_NamesSymbol, R_NilValue);
63462 luke 83
            return s2;
63511 luke 84
	}
63462 luke 85
        else
86
            /* Can use 1st arg's space only if 2nd arg has no attributes, else
87
               we may not get attributes of result right. */
65020 luke 88
            if (n == n1 && TYPEOF(s1) == type && NO_REFERENCES(s1)
63462 luke 89
		&& ATTRIB(s2) == R_NilValue)
90
                return s1;
91
    }
65020 luke 92
    else if (n == n1 && TYPEOF(s1) == type && NO_REFERENCES(s1))
63462 luke 93
	return s1;
94
 
95
    return allocVector(type, n);
96
}
66662 ripley 97
 
70093 ripley 98
// we document that tanpi(0.5) is NaN, but TS 18661-4:2015
70779 ripley 99
// does not require this and the Solaris and macOS versions give Inf.
66662 ripley 100
double Rtanpi(double);