24 static bool use_last =
false;
41 x1 = 2.0 *
ranf() - 1.0;
42 x2 = 2.0 *
ranf() - 1.0;
43 w =
sq( x1 ) +
sq( x2 );
46 w = sqrt( ( -2.0 * log( w ) ) / w );
59 return (
double)qrand() / ( (double)RAND_MAX + 1.0 );
64 double* intercept,
double* sigma,
double* correlation,
72 double sumchi_sq = 0.0;
75 for (
int i = 0; i < arraysize; i++ ) sumy += (*y)[ i ];
77 average = sumy / arraysize;
79 for (
int i = 0; i < arraysize; i++ )
82 sumxy += (*x)[ i ] * (*y)[ i ];
83 sumx_sq += (*x)[ i ] * (*x)[ i ];
84 sumy_sq += (*y)[ i ] * (*y)[ i ];
86 sumchi_sq += ( (*y)[ i ] - average ) * ( (*y)[ i ] - average );
89 *slope = ( arraysize * sumxy - sumx * sumy ) /
90 ( arraysize * sumx_sq - sumx * sumx );
92 *intercept = ( sumy - *slope * sumx ) / arraysize;
94 *correlation = ( arraysize * sumxy - sumx * sumy ) /
95 ( sqrt( arraysize * sumx_sq - sumx * sumx ) *
96 sqrt( arraysize * sumy_sq - sumy * sumy )
99 *sigma = sqrt( sumchi_sq ) / arraysize;
152 bool interp,
double& xgiven,
double& ygiven,
153 double* xnear,
double* ynear,
double* zs,
double* znear )
155 int jmin = npoints / 2;
156 int jend = npoints - 1;
157 double xnorm = qAbs( xs[ 0 ] - xs[ jend ] );
158 double ynorm = qAbs( ys[ 0 ] - ys[ jend ] );
159 xnorm = ( xnorm == 0.0 ) ? 1.0 : ( 1.0 / xnorm );
160 ynorm = ( ynorm == 0.0 ) ? 1.0 : ( 1.0 / ynorm );
161 double xdif = ( xs[ 0 ] - xgiven ) * xnorm;
162 double ydif = ( ys[ 0 ] - ygiven ) * ynorm;
163 double dmin = sqrt(
sq( xdif ) +
sq( ydif ) );
166 for (
int jj = 0; jj < npoints; jj++ )
168 xdif = ( xs[ jj ] - xgiven ) * xnorm;
169 ydif = ( ys[ jj ] - ygiven ) * ynorm;
170 double dval = sqrt(
sq( xdif ) +
sq( ydif ) );
181 int jmin1 = jmin - 1;
182 int jmin2 = jmin + 1;
188 xdif = ( xs[ jmin2 ] - xgiven ) * xnorm;
189 ydif = ( ys[ jmin2 ] - ygiven ) * ynorm;
190 dmin2 = sqrt(
sq( xdif ) +
sq( ydif ) );
193 else if ( jmin2 == npoints )
196 xdif = ( xs[ jmin2 ] - xgiven ) * xnorm;
197 ydif = ( ys[ jmin2 ] - ygiven ) * ynorm;
198 dmin2 = sqrt(
sq( xdif ) +
sq( ydif ) );
206 xdif = ( xs[ jmin1 ] - xgiven ) * xnorm;
207 ydif = ( ys[ jmin1 ] - ygiven ) * ynorm;
208 dmin1 = sqrt(
sq( xdif ) +
sq( ydif ) );
209 xdif = ( xs[ jmin2 ] - xgiven ) * xnorm;
210 ydif = ( ys[ jmin2 ] - ygiven ) * ynorm;
211 dmin2 = sqrt(
sq( xdif ) +
sq( ydif ) );
212 double ratio = dmin1 / ( dmin + dmin1 );
213 double ratio2 = 1.0 - ratio;
214 double x1 = xs[ jmin ] * ratio + xs[ jmin1 ] * ratio2;
215 double y1 = ys[ jmin ] * ratio + ys[ jmin1 ] * ratio2;
216 ratio = dmin2 / ( dmin + dmin2 );
217 ratio2 = 1.0 - ratio;
218 double x2 = xs[ jmin ] * ratio + xs[ jmin2 ] * ratio2;
219 double y2 = ys[ jmin ] * ratio + ys[ jmin2 ] * ratio2;
220 xdif = ( x1 - xgiven ) * xnorm;
221 ydif = ( y1 - ygiven ) * ynorm;
222 double dmin1g = sqrt(
sq( xdif ) +
sq( ydif ) );
223 xdif = ( x2 - xgiven ) * xnorm;
224 ydif = ( y2 - ygiven ) * ynorm;
225 double dmin2g = sqrt(
sq( xdif ) +
sq( ydif ) );
227 if ( dmin2g > dmin1g )
238 double ratio = dmin2 / ( dmin + dmin2 );
239 double ratio2 = 1.0 - ratio;
241 if ( xnear != NULL && ynear != NULL )
243 *xnear = xs[ jmin ] * ratio + xs[ jmin2 ] * ratio2;
244 *ynear = ys[ jmin ] * ratio + ys[ jmin2 ] * ratio2;
247 if ( zs != NULL && znear != NULL )
249 *znear = zs[ jmin ] * ratio + zs[ jmin2 ] * ratio2;
255 if ( xnear != NULL && ynear != NULL )
261 if ( zs != NULL && znear != NULL )
273 double& slope2,
double& intcp2,
double* xisec,
double* yisec )
275 bool isect = ( slope1 != slope2 );
279 *xisec = ( intcp2 - intcp1 ) / ( slope1 - slope2 );
280 *yisec = *xisec * slope1 + intcp1;
290 double* x2s,
double* y2s,
int npoint2,
double* xisec,
double* yisec )
306 bool isect =
intersect( slope1, intcp1, slope2, intcp2, xisec, yisec );
321 pep.
a = sequence.count(
"a", Qt::CaseInsensitive );
322 pep.
b = sequence.count(
"b", Qt::CaseInsensitive );
323 pep.
c = sequence.count(
"c", Qt::CaseInsensitive );
324 pep.
d = sequence.count(
"d", Qt::CaseInsensitive );
325 pep.
e = sequence.count(
"e", Qt::CaseInsensitive );
326 pep.
f = sequence.count(
"f", Qt::CaseInsensitive );
327 pep.
g = sequence.count(
"g", Qt::CaseInsensitive );
328 pep.
h = sequence.count(
"h", Qt::CaseInsensitive );
329 pep.
i = sequence.count(
"i", Qt::CaseInsensitive );
330 pep.
j = sequence.count(
"j", Qt::CaseInsensitive );
331 pep.
k = sequence.count(
"k", Qt::CaseInsensitive );
332 pep.
l = sequence.count(
"l", Qt::CaseInsensitive );
333 pep.
m = sequence.count(
"m", Qt::CaseInsensitive );
334 pep.
n = sequence.count(
"n", Qt::CaseInsensitive );
335 pep.
o = sequence.count(
"o", Qt::CaseInsensitive );
336 pep.
p = sequence.count(
"p", Qt::CaseInsensitive );
337 pep.
q = sequence.count(
"q", Qt::CaseInsensitive );
338 pep.
r = sequence.count(
"r", Qt::CaseInsensitive );
339 pep.
s = sequence.count(
"s", Qt::CaseInsensitive );
340 pep.
t = sequence.count(
"t", Qt::CaseInsensitive );
342 pep.
v = sequence.count(
"v", Qt::CaseInsensitive );
343 pep.
w = sequence.count(
"w", Qt::CaseInsensitive );
344 pep.
x = sequence.count(
"x", Qt::CaseInsensitive );
345 pep.
x += sequence.count(
"?" );
346 pep.
y = sequence.count(
"y", Qt::CaseInsensitive );
347 pep.
z = sequence.count(
"z", Qt::CaseInsensitive );
348 pep.
dab = sequence.count(
"+" );
349 pep.
dpr = sequence.count(
"@" );
352 pep.
g + pep.
h + pep.
i + pep.
j + pep.
k + pep.
l +
353 pep.
m + pep.
n + pep.
o + pep.
p + pep.
q + pep.
r +
354 pep.
s + pep.
t + pep.
v + pep.
w + pep.
x +
480 pep.
vbar = ( ( pep.
weight / pep.
mw ) + 4.25e-4 * ( temperature - 25 ) );
489 double xi_max = 1.000028e-3 ;
490 double c0 = 999.83952 ;
491 double c1 = 16.945176 ;
492 double c2 = -7.9870401e-3 ;
493 double c3 = -46.170461e-6 ;
494 double c4 = 105.56302e-9 ;
495 double c5 = -280.54253e-12 ;
496 double b = 16.879850e-3 ;
527 xi_max * ( c0 + c1 * t + c2 * t2 + c3 * t3 + c4 * t4 + c5 * t5 ) /
576 exponent = ( c0 / ( c1 + c2 * t20 + c3 *
sq( t20 ) ) ) - c4;
588 exponent = ( c1 * t20 - c2 *
sq( t20 ) ) / ( t + c3 );
623 double exponent = -
sq( ( x - mean ) / sigma ) / 2.0;
624 return exp( exponent ) / sqrt( 2.0 * M_PI *
sq( sigma ) );
628 const QVector< US_DataIO::EditedData >& dataList )
630 int size = dataList[ 0 ].scanData.size();
632 for (
int ii = 1; ii < dataList.size(); ii++ )
633 size += dataList[ ii ].scanData.size();
637 QVector< double > vecx( size );
638 QVector< double > vecy( size );
639 double* x = vecx.data();
640 double* y = vecy.data();
644 for (
int i = 0; i < dataList.size(); i++ )
648 for (
int j = 0; j < e->
scanData.size(); j++ )
650 x[ count ] = e->
scanData[ j ].omega2t;
651 y[ count ] = e->
scanData[ j ].seconds;
663 QTime now = QTime::currentTime();
665 uint seed = now.msec()
666 + now.second() * 1000
667 + now.minute() * 60000
668 + now.hour() * 3600000;
693 double z = fabs( x );
694 double t = 1.0 / ( 1.0 + 0.5 * z );
696 double ans = t * exp( -z *
705 ( -0.82215223 + t * 0.17087277
716 return x >= 0.0 ? ans : 2.0 - ans;
729 int pfeas, ret = 0, iz, jz;
730 double d1, d2, sm, up, ss;
731 int k, j=0, l, izmax=0, ii, jj=0, ip;
732 double temp, wmax, t, alpha, asave, dummy, unorm, ztest, cc;
735 if ( m <= 0 || n <= 0 || a == NULL || b == NULL || x == NULL )
return 2;
738 QVector< double > wVec;
739 QVector< double > zVec;
762 if ( indexp != NULL )
770 if ( w == NULL || zz == NULL || index == NULL )
return 2;
773 for(
int k = 0; k < n; k++ )
791 while ( iz1 <= iz2 && nsetp < m )
794 for ( iz = iz1; iz <= iz2; iz++ )
797 ja_dim1 = j * a_dim1;
800 for( l = npp1; l < m; l++ )
801 sm += a[ l + ja_dim1 ] * b[ l ];
809 for ( wmax = 0.0, iz = iz1; iz <= iz2; iz++ )
821 if ( wmax <= 0.0 )
break;
830 ja_dim1 = j * a_dim1;
831 asave = a[ npp1 + ja_dim1 ];
832 _nnls_h12( 1, npp1, npp1 + 1, m, &a[ ja_dim1 ],
833 1, &up, &dummy, 1, 1, 0 );
838 for ( l = 0; l < nsetp; l++ )
840 d1 = a[ l + ja_dim1 ];
844 unorm = sqrt( unorm );
846 d2 = unorm + ( d1 = a[ npp1 + ja_dim1 ], fabs( d1 ) ) * 0.01;
848 if ( ( d2 - unorm ) > 0.0 )
853 for ( l = 0; l < m; l++ ) zz[ l ] =b[ l ];
855 _nnls_h12( 2, npp1, npp1 + 1, m, &a[ ja_dim1 ],
856 1, &up, zz, 1, 1, 1 );
858 ztest = zz[ npp1 ] / a[ npp1 + ja_dim1 ];
861 if ( ztest > 0.0 )
break;
866 a[ npp1 + ja_dim1 ] = asave;
871 if ( wmax <= 0.0 )
break;
877 for ( l = 0; l < m; ++l ) b[ l ] =zz[ l ];
879 index[ iz ] = index[ iz1 ];
886 for ( jz = iz1; jz <= iz2; jz++ )
889 _nnls_h12( 2, nsetp - 1, npp1, m, &a[ ja_dim1 ],
890 1, &up, &a[ jj * a_dim1 ], 1, a_dim1, 1 );
894 for ( l = npp1; l < m; l++ )
895 a[ l + ja_dim1 ] = 0.0;
900 for ( l = 0; l < nsetp; l++ )
902 ip = nsetp - ( l + 1 );
905 for ( ii = 0; ii <= ip; ii++ )
906 zz[ ii ] -= a[ ii + jj * a_dim1 ] * zz[ ip + 1 ];
909 zz[ ip ] /= a[ ip + jj *a_dim1 ];
913 while ( ++iter < itmax )
918 for ( alpha = 2.0, ip = 0; ip < nsetp; ip++ )
921 if ( zz[ ip ] <= 0.0 )
923 t = -x[ l ] / ( zz[ ip ] - x[ l ]);
935 if ( alpha == 2.0 )
break;
940 for ( ip = 0; ip < nsetp; ip++ )
943 x[ l ] += alpha * ( zz[ ip ] - x[ l ] );
955 if ( jj != ( nsetp - 1 ) )
958 for ( j = jj + 1; j < nsetp; j++ )
961 int iia_dim1 = ii * a_dim1;
964 _nnls_g1( a [ j - 1 + iia_dim1 ], a[ j + iia_dim1 ],
965 &cc, &ss, &a[ j - 1 + iia_dim1 ] );
967 for ( a[ j + iia_dim1 ] = 0.0, l = 0; l < n; l++ )
970 int jla_dim1 = j + l * a_dim1;
972 temp = a[ jla_dim1 - 1 ];
973 a[ jla_dim1 - 1 ] = cc * temp + ss * a[ jla_dim1 ];
974 a[ jla_dim1 ] = -ss * temp + cc * a[ jla_dim1 ];
979 b[ j - 1 ] = cc * temp + ss * b[ j ];
980 b[ j ] = -ss * temp + cc * b[ j ];
993 for( jj = 0; jj < nsetp; jj++ )
1002 }
while ( pfeas == 0 );
1005 for ( k = 0; k < m; k++ )
1008 for ( l = 0; l < nsetp; l++ )
1010 ip = nsetp - ( l + 1 );
1012 for ( ii = 0; ii <= ip; ii++ )
1013 zz[ ii ] -= a[ ii + jj * a_dim1 ] * zz[ ip + 1 ];
1016 zz[ ip ] /= a[ ip + jj * a_dim1 ];
1026 for ( ip = 0; ip < nsetp; ip++ )
1038 for ( k = npp1; k < m; k++ )
1039 sm += ( b[ k ] * b[ k ] );
1041 for( j = 0; j < n; j++ )
1044 if ( rnorm != NULL ) *rnorm = sqrt( sm );
1059 double* sterm,
double* sig )
1065 if ( fabs( a ) > fabs( b ) )
1069 yr = sqrt( d1 * d1 + 1.0 );
1071 *cterm = ( a >= 0.0 ? fabs( d1 ) : -fabs( d1 ) );
1072 *sterm = ( *cterm ) * xr;
1073 *sig = fabs( a ) * yr;
1075 else if ( b != 0.0 )
1079 yr = sqrt( d1*d1 + 1.0 );
1081 *sterm = ( b >= 0.0 ? fabs( d1 ) : -fabs( d1 ) );
1082 *cterm = ( *sterm ) * xr;
1083 *sig = fabs( b ) * yr;
1129 double d1, d2, b, clinv, cl, sm;
1130 int incr, k, j, i2, i3, i4;
1133 if ( mode != 1 && mode != 2 )
return 1;
1135 if ( m < 1 || u == NULL || u_dim1 < 1 || cm == NULL )
return 2;
1137 if ( lpivot < 0 || lpivot >= l1 || l1 >= m )
return 0;
1140 cl = ( d1 = u[ lpivot * u_dim1 ], fabs( d1 ) );
1144 if ( cl <= 0.0 )
return 0;
1148 for ( j = l1; j < m; j++ )
1150 d2 = ( d1 = u[ j * u_dim1 ], fabs( d1 ) );
1151 if ( d2 > cl ) cl=d2;
1154 if ( cl <= 0.0 )
return 0;
1159 d1 = u[ lpivot * u_dim1 ] * clinv;
1162 for( j = l1; j < m; j++ )
1164 d1 = u[ j * u_dim1 ] * clinv;
1169 if ( u[ lpivot * u_dim1 ] > 0.0 ) cl = -cl;
1171 *up = u[ lpivot * u_dim1 ] - cl;
1172 u[ lpivot * u_dim1 ] = cl;
1175 if ( ncv <= 0 )
return 0;
1177 b = (*up) * u[ lpivot * u_dim1 ];
1180 if ( b >= 0.0 )
return 0;
1183 i2 = 1 -icv + ice * lpivot;
1184 incr = ice * ( l1 - lpivot );
1186 for ( j = 0; j < ncv; j++ )
1191 sm = cm[ i2 - 1 ] * (*up);
1193 for ( k = l1; k < m; k++ )
1195 sm += cm[ i3 - 1 ] * u[ k * u_dim1 ];
1202 cm[ i2 - 1 ] += sm * (*up);
1204 for ( k = l1; k < m; k++ )
1206 cm[ i4 - 1 ] += sm * u[ k * u_dim1 ];
1217 if ( smooth <= 1 )
return;
1222 int points = array.size();
1224 QVector< double > temp_array( points );
1225 QVector< double > y_weights( smooth );
1233 double increment = 2.0 / (double)smooth;
1236 for (
int i = 1; i < smooth; i++ )
1238 x_weight += increment;
1245 for (
int j = 0; j < smooth; j++ )
1252 for (
int k = j - 1; k >= 0; k-- )
1255 sum += y_weights[ position ] * temp_array[ k ];
1256 sum_y += y_weights[ position ];
1262 for (
int k = j; k < j + smooth; k++ )
1264 sum += y_weights[ position ] * temp_array[ k ];
1265 sum_y += y_weights[ position ];
1270 array[ j ] = sum / sum_y;
1274 for (
int j = smooth; j < points - smooth; j++ )
1281 for (
int k = j - 1; k >= j - smooth + 1; k-- )
1284 sum += y_weights[ position ] * temp_array[ k ];
1285 sum_y += y_weights[ position ];
1291 for (
int k = j; k < j + smooth; k++ )
1293 sum += y_weights[ position ] * temp_array[ k ];
1294 sum_y += y_weights[ position ];
1299 array[ j ] = sum / sum_y;
1306 for (
int j = points - smooth; j < points; j++ )
1313 for (
int k = j - 1; k >= j - smooth + 1; k-- )
1316 sum += y_weights[ position ] * temp_array[ k ];
1317 sum_y += y_weights[ position ];
1323 for (
int k = j; k < points; k++ )
1325 sum += y_weights[ position ] * temp_array[ k ];
1326 sum_y += y_weights[ position ];
1331 array[ j ] = sum / sum_y;
1342 for (
int ii = 0; ii < solution.
analyteInfo.size(); ii++ )
1344 double vb20 = solution.
analyteInfo[ ii ].analyte.vbar20;
1350 vbsum += ( vbar * wt );
1355 cvbar = vbsum / wtsum;
1366 const int min_grid = 10;
1367 const int min_reps = 1;
1369 const int max_grid = 200;
1370 const int min_subg = 40;
1371 const int max_subg = 200;
1372 const int max_reps = 40;
1375 const int max_grid = 800;
1376 const int min_subg = 10;
1377 const int max_subg = 800;
1378 const int max_reps = 160;
1382 ngrid_s = qMin( max_grid, qMax( min_grid, ngrid_s ) );
1383 ngrid_k = qMin( max_grid, qMax( min_grid, ngrid_k ) );
1386 int nreps_g = qRound( pow( (
double)( ngrid_s * ngrid_k ), 0.25 ) );
1389 double grfact = 1.0;
1392 for (
int ii = 0; ii < tgrfact.count(); ii++ )
1394 if ( tgrfact[ ii ].startsWith(
"grfact=" ) )
1395 grfact = QString( tgrfact[ ii ]) .section(
"=", 1, 1 ).toDouble();
1397 nreps_g = qRound( grfact * nreps_g );
1403 int nsubg_s = ( ngrid_s + nreps_g - 1 ) / nreps_g;
1404 int nsubg_k = ( ngrid_k + nreps_g - 1 ) / nreps_g;
1407 while ( ( nsubg_s * nsubg_k ) > max_subg && nreps_g < max_reps )
1410 nsubg_s = ( ngrid_s + nreps_g - 1 ) / nreps_g;
1411 nsubg_k = ( ngrid_k + nreps_g - 1 ) / nreps_g;
1415 while ( ( nsubg_s * nsubg_k ) < min_subg && nreps_g > min_reps )
1418 nsubg_s = ( ngrid_s + nreps_g - 1 ) / nreps_g;
1419 nsubg_k = ( ngrid_k + nreps_g - 1 ) / nreps_g;
1424 ngrid_s = nsubg_s * nreps_g;
1425 ngrid_k = nsubg_k * nreps_g;