トップ 差分 一覧 ソース 検索 ヘルプ RSS ログイン

BugTrack-R備忘録/54

R備忘録 /状態空間モデリング/donlp2/その他のメモ

R備忘録 - 記事一覧

Writing R Extensions(Version 2.7.1 (2008-06-23)):::6 R API: Cコードへのエントリーポイント

  • 投稿者: みゅ
  • カテゴリ: なし
  • 優先度: 普通
  • 状態: 完了
  • 日時: 2010年01月10日 16時25分03秒

内容

  • 適当訳、参照は自己責任でお願いします・・・

6 R API: Cコードへのエントリーポイント

6.1 メモリーの確保

6.1.1 Transient storage allocation

6.1.2 User-controlled memory

6.2 Error handling

6.2.1 Error handling from FORTRAN

6.3 Random number generation

6.4 Missing and IEEE special values

6.5 Printing

6.5.1 Printing from FORTRAN

6.6 Calling C from FORTRAN and vice versa

6.7 Numerical analysis subroutines

6.7.1 Distribution functions

double dnorm(double x, double mu, double sigma, int give_log);
double pnorm(double x, double mu, double sigma, int lower_tail, int give_log);
double qnorm(double p, double mu, double sigma, int lower_tail, int log_p);
double rnorm(double mu, double sigma);
  • For reference, the following table gives the basic name (to be prefixed by ‘d’, ‘p’, ‘q’ or ‘r’ apart from the exceptions noted) and distribution-specific arguments for the complete set of distributions.
beta beta a, b
non-central beta nbeta a, b, ncp
binomial binom n, p
Cauchy cauchy location, scale
chi-squared chisq df
non-central chi-squared nchisq df, ncp
exponential exp scale (and not rate)
F f n1, n2
non-central F nf n1, n2, ncp
gamma gamma shape, scale
geometric geom p
hypergeometric hyper NR, NB, n
logistic logis location, scale
lognormal lnorm logmean, logsd
negative binomial nbinom size, prob
normal norm mu, sigma
Poisson pois lambda
Student’s t t n
non-central t nt df, delta
Studentized range tukey (*) rr, cc, df
uniform unif a, b
Weibull weibull shape, scale
Wilcoxon rank sum wilcox m, n
Wilcoxon signed rank signrank n

6.7.2 Mathematical functions

double gammafn (double x) [Function]
double lgammafn (double x) [Function]
double digamma (double x) [Function]
double trigamma (double x) [Function]
double tetragamma (double x) [Function]
double pentagamma (double x) [Function]
double psigamma (double x, double deriv) [Function]
  • The Gamma function, the natural logarithm of its absolute value and first four derivatives and the n-th derivative of Psi, the digamma function.
double beta (double a, double b) [Function]
double lbeta (double a, double b) [Function]
  • The (complete) Beta function and its natural logarithm.
double choose (double n, double k) [Function]
double lchoose (double n, double k) [Function]
  • The number of combinations of k items chosen from from n and its natural logarithm. k is rounded to the nearest integer (with a warning if needed).
double bessel_i (double x, double nu, double expo) [Function]
double bessel_j (double x, double nu) [Function]
double bessel_k (double x, double nu, double expo) [Function]
double bessel_y (double x, double nu) [Function]
  • Bessel functions of types I, J, K and Y with index nu. For bessel_i and bessel_k there is the option to return exp(-x) I(x; nu) or exp(x) K(x; nu) if expo is 2. (Use expo == 1 for unscaled values.)

6.7.3 Numerical Utilities

6.7.4 Mathematical constants

6.8 Optimization

  • optimが元となるCコードに直接アクセスすることができる.ユーザーは最小化したい関数を次の定義で与える必要がある.
typedef double optimfn(int n, double *par, void *ex);
  • 1番目のパラメータは2番目のパラメータで与える変数の数である.The third argument is a pointer passed down from the calling routine, normally used to carry auxiliary information.
  • いくつかの方法では以下の微係数も必要になる.
typedef void optimgr(int n, double *par, double *gr, void *ex);
  • これはgr引数を通じて(微係数が)返される.No function is provided for finite-differencing, nor for approximating the Hessian at the result.
  • インターフェース(ヘッダーファイル‘R_ext/Applic.h’で定義されている)は
  • Nelder Mead:
void nmmin(int n, double *xin, double *x, double *Fmin, optimfn fn,
    int *fail, double abstol, double intol, void *ex,
    double alpha, double beta, double gamma, int trace,
    int *fncount, int maxit);
  • BFGS:
void vmmin(int n, double *x, double *Fmin,
    optimfn fn, optimgr gr, int maxit, int trace,
    int *mask, double abstol, double reltol, int nREPORT,
    void *ex, int *fncount, int *grcount, int *fail);
  • Conjugate gradients:
void cgmin(int n, double *xin, double *x, double *Fmin,
    optimfn fn, optimgr gr, int *fail, double abstol,
    double intol, void *ex, int type, int trace,
    int *fncount, int *grcount, int maxit);
  • Limited-memory BFGS with bounds:
void lbfgsb(int n, int lmm, double *x, double *lower,
    double *upper, int *nbd, double *Fmin, optimfn fn,
    optimgr gr, int *fail, void *ex, double factr,
    double pgtol, int *fncount, int *grcount,
    int maxit, char *msg, int trace, int nREPORT);
  • Simulated annealing:
void samin(int n, double *x, double *Fmin, optimfn fn, int maxit,
    int tmax, double temp, int trace, void *ex);
  • 多くの引数が複数のタイプの手法で共通である.nはパラメーター(訳者 注:changing variablesのこと)xの数である.xinはxの初期値であり、xに最終結果がセットされて戻ってくる.また関数の最終の値がFminにセットされる.他のパラメーターの説明は関数「optim」のヘルプページで見つかるだろう.nbdの値についてはソースコード「src/appl/lbfgsb.c」を見よ.そこにはどのような境界が使われるかを特定している.

6.9 Integration

typedef void integr_fn(double *x, int n, void *ex);
  • There are interfaces (defined in header ‘R_ext/Applic.h’) for definite and for indefinite integrals. ‘Indefinite’ means that at least one of the integration boundaries is not finite.
  • Finite:
void Rdqags(integr_fn f, void *ex, double *a, double *b,
double *epsabs, double *epsrel,
double *result, double *abserr, int *neval, int *ier,
int *limit, int *lenw, int *last,
int *iwork, double *work);
  • Indefinite:
void Rdqagi(integr_fn f, void *ex, double *bound, int *inf,
double *epsabs, double *epsrel,
double *result, double *abserr, int *neval, int *ier,
int *limit, int *lenw, int *last,
int *iwork, double *work);

6.10 Utility functions

6.11 Re-encoding

6.12 Allowing interrupts

6.13 Platform and version information

6.14 Inlining C functions

6.15 Controlling visibility

6.16 Using these functions in your own C code

6.17 Organization of header files

コメント