25 unsigned long long ans = 1;
30 for(
unsigned int i = 1; i <= k; ++i )
34 if( ans > std::numeric_limits< unsigned int >::max() )
44 unsigned int mcols = 0;
45 for(
unsigned int i = 0; i <= degree; ++i )
47 unsigned int temp =
nchoosek( kvars - 1 + i, kvars - 1 );
50 std::cout <<
"overflow to compute nchoosek n= " << kvars - 1 + i <<
" k= " << kvars - 1 << std::endl;
61 std::vector< double >& basis )
64 basis.reserve( len - basis.capacity() + basis.size() );
65 size_t iend = basis.size();
67 size_t istr = basis.size();
75 std::vector< size_t > varspos( kvars );
77 for(
int ivar = 0; ivar < kvars; ++ivar )
79 basis.push_back( vars[ivar] );
80 varspos[ivar] = iend++;
83 for(
int ideg = 2; ideg <= degree; ++ideg )
86 for(
int ivar = 0; ivar < kvars; ++ivar )
88 size_t varpreend = iend;
89 for(
size_t ilast = varspos[ivar]; ilast < preend; ++ilast )
91 basis.push_back( vars[ivar] * basis[ilast] );
94 varspos[ivar] = varpreend;
97 assert( len == iend - istr );
104 std::vector< double >& V )
107 V.reserve( mrows * ncols - V.capacity() + V.size() );
108 size_t istr = V.size(), icol = 0;
110 for(
int irow = 0; irow < mrows; ++irow )
119 std::vector< size_t > varspos( kvars );
121 for(
int ivar = 0; ivar < kvars; ++ivar )
123 for(
int irow = 0; irow < mrows; ++irow )
125 V.push_back( us[irow * kvars + ivar] );
127 varspos[ivar] = icol++;
130 for(
int ideg = 2; ideg <= degree; ++ideg )
132 size_t preendcol = icol;
133 for(
int ivar = 0; ivar < kvars; ++ivar )
135 size_t varpreend = icol;
136 for(
size_t ilast = varspos[ivar]; ilast < preendcol; ++ilast )
138 for(
int irow = 0; irow < mrows; ++irow )
140 V.push_back( us[irow * kvars + ivar] * V[istr + irow + ilast * mrows] );
144 varspos[ivar] = varpreend;
147 assert( icol == ncols );
153 double* v =
new double[mrows];
154 for(
int i = 0; i < ncols; i++ )
156 for(
int j = 0; j < mrows; j++ )
157 v[j] = V[mrows * i + j];
167 for(
int j = 0; j < mrows; j++ )
168 V[mrows * i + j] = V[mrows * i + j] / ts[i];
176 for(
int k = 0; k < ncols; k++ )
178 for(
int j = 0; j < bncols; j++ )
181 for(
int i = k; i < mrows; i++ )
182 t2 += Q[mrows * k + i] * bs[mrows * j + i];
185 for(
int i = k; i < mrows; i++ )
186 bs[mrows * j + i] -= t2 * Q[mrows * k + i];
195 double* v =
new double[mrows];
197 for(
int k = 0; k < ncols; k++ )
201 for(
int j = 0; j < nv; j++ )
202 v[j] = V[mrows * k + ( j + k )];
206 for(
int j = 0; j < nv; j++ )
207 t2 = t2 + v[j] * v[j];
209 double t = sqrt( t2 );
214 vnrm = sqrt( 2 * ( t2 + v[0] * t ) );
219 vnrm = sqrt( 2 * ( t2 - v[0] * t ) );
225 for(
int j = 0; j < nv; j++ )
229 for(
int j = k; j < ncols; j++ )
232 for(
int i = 0; i < nv; i++ )
233 t2 = t2 + v[i] * V[mrows * j + ( i + k )];
237 for(
int i = 0; i < nv; i++ )
238 V[mrows * j + ( i + k )] = V[mrows * j + ( i + k )] - t2 * v[i];
241 D[k] = V[mrows * k + k];
243 for(
int i = 0; i < nv; i++ )
244 V[mrows * k + ( i + k )] = v[i];
246 if( ( fabs( D[k] ) ) < tol && ( *rank == ncols ) )
259 std::cout.precision(16);
260 std::cout<<
"Before backsolve "<<std::endl;
261 std::cout<<
" V = "<<std::endl;
262 for (
int k=0; k< ncols; k++){
263 for (
int j=0; j<mrows; ++j){
264 std::cout<<
R[mrows*k+j]<<std::endl;
266 std::cout<<std::endl;
268 std::cout<<std::endl;
271 std::cout<<
"bs = "<<std::endl;
272 std::cout<<std::endl;
273 for (
int k=0; k< bncols; k++){
274 for (
int j=0; j<mrows; ++j){
275 std::cout<<
" "<<bs[mrows*k+j]<<std::endl;
278 std::cout<<std::endl;
281 for(
int k = 0; k < bncols; k++ )
283 for(
int j = ncols - 1; j >= 0; j-- )
285 for(
int i = j + 1; i < ncols; ++i )
286 bs[mrows * k + j] = bs[mrows * k + j] -
R[mrows * i + j] * bs[mrows * k + i];
288 assert(
R[mrows * j + j] != 0 );
289 bs[mrows * k + j] = bs[mrows * k + j] /
R[mrows * j + j];
293 for(
int k = 0; k < bncols; k++ )
295 for(
int j = 0; j < ncols; ++j )
296 bs[mrows * k + j] = bs[mrows * k + j] / ws[j];
311 if( ncols < 1 ) std::cout <<
"ERROR: Invalid input to safeguarded polyfit backsolve routine.\n";
313 std::cout.precision(12);
314 std::cout<<
"Before backsolve "<<std::endl;
315 std::cout<<
" V = "<<std::endl;
316 for (
int k=0; k< ncols; k++){
317 for (
int j=0; j<mrows; ++j){
318 std::cout<<
R[mrows*k+j]<<std::endl;
320 std::cout<<std::endl;
322 std::cout<<std::endl;
325 std::cout<<
"bs = "<<std::endl;
326 std::cout<<std::endl;
327 for (
int k=0; k< bncols; k++){
328 for (
int j=0; j<mrows; ++j){
329 std::cout<<
" "<<bs[mrows*k+j]<<std::endl;
332 std::cout<<std::endl;
336 std::cout<<
"Input ws = [ ";
337 for (
int k=0; k< ncols; k++){
338 std::cout<<ws[k]<<
", ";
340 std::cout<<
" ] "<<std::endl;
342 std::cout <<
"R: " <<
R <<
"size: [" << mrows <<
"," << ncols <<
"]" << std::endl;
343 std::cout <<
"bs: " << bs <<
"size: [" << mrows <<
"," << bncols <<
"]" << std::endl;
344 std::cout <<
"ws: " << ws <<
"size: [" << ncols <<
"," << 1 <<
"]" << std::endl;
345 std::cout <<
"degree_out: " << degree_out << std::endl;
348 int deg, numcols = 0;
350 for(
int k = 0; k < bncols; k++ )
355 numcols = deg + 1 - interp;
357 numcols = ( deg + 2 ) * ( deg + 1 ) / 2 - interp;
359 assert( numcols <= ncols );
361 std::vector< double > bs_bak( numcols );
365 for(
int i = 0; i < numcols; i++ )
367 assert( mrows * k + i < mrows * bncols );
368 bs_bak.at( i ) = bs[mrows * k + i];
374 int cend = numcols - 1;
375 bool downgrade =
false;
379 assert(
dim > 0 &&
dim < 3 );
381 for(
int d = deg; d >= 0; d-- )
390 cstart = ( ( d + 1 ) * d ) / 2;
395 for(
int j = cend; j >= cstart; j-- )
397 assert( mrows * k + j < mrows * bncols );
398 for(
int i = j + 1; i < numcols; ++i )
400 assert( mrows * k + i < mrows * bncols );
401 assert( mrows * i + j < mrows * ncols );
402 bs[mrows * k + j] = bs[mrows * k + j] -
R[mrows * i + j] * bs[mrows * k + i];
404 assert( mrows * j + j < mrows * ncols );
405 bs[mrows * k + j] = bs[mrows * k + j] /
R[mrows * j + j];
409 if( d >= 2 && d < deg )
416 assert( mrows * cstart + cstart < mrows * ncols );
417 double tb = bs_bak.at( cstart ) /
R[mrows * cstart + cstart];
418 assert( mrows * k + cstart < mrows * bncols );
419 if( fabs( bs[mrows * k + cstart] - tb ) > ( 1 + tol ) * fabs( tb ) )
430 std::vector< double > tb( cend - cstart + 1 );
431 for(
int j = 0; j <= ( cend - cstart ); j++ )
433 tb.at( j ) = bs_bak.at( cstart + j );
436 for(
int j = cend; j >= cstart; j-- )
438 int jind = j - cstart;
440 for(
int i = j + 1; i <= cend; ++i )
442 assert( mrows * i + j < mrows * ncols );
443 tb.at( jind ) = tb.at( jind ) -
R[mrows * i + j] * tb.at( i - cstart );
445 assert( mrows * j + j < mrows * ncols );
446 tb.at( jind ) = tb.at( jind ) /
R[mrows * j + j];
447 assert( mrows * k + j < mrows * bncols );
448 double err = fabs( bs[mrows * k + j] - tb.at( jind ) );
450 if( ( err > tol ) && ( err >= ( 1 + tol ) * fabs( tb.at( jind ) ) ) )
457 if( downgrade )
break;
472 numcols = ( deg + 2 ) * ( deg + 1 ) / 2;
474 for(
int i = 0; i < numcols; i++ )
476 assert( mrows * k + i < mrows * bncols );
477 bs[mrows * k + i] = bs_bak.at( i );
481 assert( k < bncols );
484 for(
int i = 0; i < numcols; i++ )
488 bs[mrows * k + i] = bs[mrows * k + i] / ws[i];
491 for(
int i = numcols; i < mrows; i++ )
494 bs[mrows * k + i] = 0;
505 for(
int i = 0; i < len; ++i )
517 for(
int i = 0; i < len; ++i )
525 c[0] = a[1] * b[2] - a[2] * b[1];
526 c[1] = a[2] * b[0] - a[0] * b[2];
527 c[2] = a[0] * b[1] - a[1] * b[0];
537 for(
int i = 0; i < len; ++i )
551 for(
int k = 0; k < len; k++ )
552 w = std::max( w, fabs( a[k] ) );
560 for(
int k = 0; k < len; k++ )
562 s += ( a[k] / w ) * ( a[k] / w );
575 double nrm = 0, mx = 0;
576 for(
int i = 0; i < len; ++i )
578 mx = std::max( fabs( a[i] ), mx );
582 for(
int i = 0; i < len; ++i )
588 for(
int i = 0; i < len; ++i )
590 nrm += ( a[i] / mx ) * ( a[i] / mx );
592 nrm = mx * sqrt( nrm );
597 for(
int i = 0; i < len; ++i )
607 for(
int i = 0; i < len; ++i )
609 res += ( a[i] - b[i] ) * ( a[i] - b[i] );
624 for(
int i = 0; i < len; ++i )
636 for(
int i = 0; i < len; ++i )
644 for(
int i = 0; i < len; ++i )
646 c[i] = a[i] - innerp * b[i] / bnrm;
661 for(
int i = 0; i < len; ++i )
663 c[i] = mu * a[i] + psi * b[i];
668 const double* cornercoords,
670 const double* currcoords,
671 double* naturalcoords )
673 assert(
dim == 2 ||
dim == 3 );
674 double a = 0, b = 0, d = 0, tol = 1e-12;
675 for(
int i = 0; i <
dim; ++i )
677 a += ( cornercoords[
dim + i] - cornercoords[i] ) * ( cornercoords[
dim + i] - cornercoords[i] );
678 b += ( cornercoords[
dim + i] - cornercoords[i] ) * ( cornercoords[2 *
dim + i] - cornercoords[i] );
679 d += ( cornercoords[2 *
dim + i] - cornercoords[i] ) * ( cornercoords[2 *
dim + i] - cornercoords[i] );
681 double det = a * d - b * b;
683 for(
int ipt = 0; ipt < npts; ++ipt )
686 for(
int i = 0; i <
dim; ++i )
688 e += ( cornercoords[
dim + i] - cornercoords[i] ) * ( currcoords[ipt *
dim + i] - cornercoords[i] );
689 f += ( cornercoords[2 *
dim + i] - cornercoords[i] ) * ( currcoords[ipt *
dim + i] - cornercoords[i] );
691 naturalcoords[ipt * 3 + 1] = ( e * d - b * f ) / det;
692 naturalcoords[ipt * 3 + 2] = ( a * f - b * e ) / det;
693 naturalcoords[ipt * 3] = 1 - naturalcoords[ipt * 3 + 1] - naturalcoords[ipt * 3 + 2];
694 if( naturalcoords[ipt * 3] < -tol || naturalcoords[ipt * 3 + 1] < -tol || naturalcoords[ipt * 3 + 2] < -tol )
696 std::cout <<
"Corners: \n";
697 std::cout << cornercoords[0] <<
"\t" << cornercoords[1] <<
"\t" << cornercoords[3] << std::endl;
698 std::cout << cornercoords[3] <<
"\t" << cornercoords[4] <<
"\t" << cornercoords[5] << std::endl;
699 std::cout << cornercoords[6] <<
"\t" << cornercoords[7] <<
"\t" << cornercoords[8] << std::endl;
700 std::cout <<
"Candidate: \n";
701 std::cout << currcoords[ipt *
dim] <<
"\t" << currcoords[ipt *
dim + 1] <<
"\t" << currcoords[ipt *
dim + 2]
705 assert( fabs( naturalcoords[ipt * 3] + naturalcoords[ipt * 3 + 1] + naturalcoords[ipt * 3 + 2] - 1 ) < tol );
706 for(
int i = 0; i <
dim; ++i )
708 assert( fabs( naturalcoords[ipt * 3] * cornercoords[i] +
709 naturalcoords[ipt * 3 + 1] * cornercoords[
dim + i] +
710 naturalcoords[ipt * 3 + 2] * cornercoords[2 *
dim + i] - currcoords[ipt *
dim + i] ) < tol );