//#define DEBUG 1 /* Authors Martin Schlather, schlath@hsu-hh.de Collection of auxiliary functions Copyright (C) 2001 -- 2006 Martin Schlather, This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ #include #include #include #include "auxiliary.h" bool is_diag(double *aniso, int dim) { int diag = dim + 1, size = dim * dim, i; bool notdiag=false; for (i=0; i-1.0)) return 0.0; if (x<=0) return RF_NAN; // not programmed yet logx = log(0.5 * x); x1=1.5; x2=nu+1.5; sign=1.0; if (x2 > 0.0) { dummy = (nu + 1.0) * logx - lgammafn(x1) - lgammafn(x2); if (expscaled) dummy -= x; res = exp(dummy); } else { if ( (double) ((int) (x1-0.5)) != x1-0.5 ) return RF_NAN; res=pow(0.5 * x, nu + 1.0) / (gammafn(x1) * gammafn(x2)); if (expscaled) res *= exp(-x); if ((dummy= res) <0) { dummy = -dummy; sign = -1.0; } dummy = log(dummy); } logx *= 2.0; do { if (x2<0) { sign = -sign; } dummy += logx - log(x1) - log(fabs(x2)); res += sign * exp(dummy); x1 += 1.0; x2 += 1.0; sign = factor_sign * sign; } while (exp(dummy) > fabs(res) * epsilon); return res; } void vectordist(double *v, int *Dim, double *dist, int *diag){ int m, n, d, l, dim, r, lr, dr, add; add = (*diag==0) ? 1 : 0; l = Dim[0]; dim = Dim[1] * l; lr = (l * (l + 1 - 2 * add)) / 2; for (r=0, m=0; m y[d]; return false; } void order(int *pos, int start, int end) { int randpos, pivot, left, right, pivotpos, swap; if( start < end ) { //GetRNGstate();randpos = start + (int) (UNIFORM_RANDOM * (end-start+1)); PutRNGstate(); randpos = (int) (0.5 * (start + end)); pivot = pos[randpos]; pos[randpos] = pos[start]; pos[start] = pivot; pivotpos=start; left = start; right=end+1; while (left < right) { while (++left < right && smaller(pos[left], pivot)) pivotpos++; while (--right > left && greater(pos[right], pivot)); if (left < right) { swap=pos[left]; pos[left]=pos[right]; pos[right]=swap; pivotpos++; } } pos[start] = pos[pivotpos]; pos[pivotpos] = pivot; order(pos, start, pivotpos-1 ); order(pos, pivotpos + 1, end ); } } void ordering(double *d, int len, int dim, int *pos) { /* quicksort algorithm, slightly modified: does not sort the data, but d[pos] will be ordered NOTE: pos must have the values 0,1,2,...,start-end ! (orderdouble is a kind of sorting pos according to the variable d) */ int i; for (i=0; i