Changeset 5179
- Timestamp:
- Sep 29, 2005, 9:40:59 AM (21 years ago)
- File:
-
- 1 edited
-
trunk/psLib/src/math/psSpline.c (modified) (8 diffs)
Legend:
- Unmodified
- Added
- Removed
-
trunk/psLib/src/math/psSpline.c
r5155 r5179 7 7 * splines. 8 8 * 9 * @version $Revision: 1.1 29$ $Name: not supported by cvs2svn $10 * @date $Date: 2005-09-2 7 23:16:59 $9 * @version $Revision: 1.130 $ $Name: not supported by cvs2svn $ 10 * @date $Date: 2005-09-29 19:40:59 $ 11 11 * 12 12 * Copyright 2004-2005 Maui High Performance Computing Center, University of Hawaii … … 491 491 PS_ASSERT_VECTOR_NON_NULL(y, NULL); 492 492 PS_ASSERT_VECTOR_TYPE_F32_OR_F64(y, NULL); 493 PS_ASSERT_VECTOR_NON_NULL(x, NULL);494 PS_ASSERT_VECTOR_TYPE_F32_OR_F64(x, NULL);495 PS_ASSERT_VECTORS_SIZE_EQUAL(x, y, NULL);496 493 psS32 numSplines = (y->n)-1; 497 498 494 psTrace(__func__, 5, "numSplines is %d\n", numSplines); 499 495 // 500 496 // The following code ensures that xPtr and yPtr points to a psF32 psVector. 501 497 // 502 psVector *xF32 = NULL;503 498 psVector *xPtr = NULL; 504 if (PS_TYPE_F64 == x->type.type) { 505 xF32 = psVectorAlloc(y->n, PS_TYPE_F32); 506 for (psS32 i = 0 ; i < x->n ; i++) { 507 xF32->data.F32[i] = (psF32) x->data.F64[i]; 499 if (x != NULL) { 500 // Convert x to F32 if necessary. 501 PS_ASSERT_VECTOR_NON_NULL(x, NULL); 502 PS_ASSERT_VECTOR_TYPE_F32_OR_F64(x, NULL); 503 PS_ASSERT_VECTORS_SIZE_EQUAL(x, y, NULL); 504 if (PS_TYPE_F64 == x->type.type) { 505 xPtr = psVectorAlloc(y->n, PS_TYPE_F32); 506 for (psS32 i = 0 ; i < x->n ; i++) { 507 xPtr->data.F32[i] = (psF32) x->data.F64[i]; 508 } 509 } else if (PS_TYPE_F32 == x->type.type) { 510 xPtr = (psVector *) x; 508 511 } 509 xPtr = xF32; 510 } else if (PS_TYPE_F32 == x->type.type) { 511 xPtr = (psVector *) x; 512 } 513 514 psVector *yF32 = NULL; 512 } else { 513 // Allocate an index vector for x 514 xPtr = psVectorCreate(NULL, 0.0, (psF64) y->n, 1.0, PS_TYPE_F32); 515 printf("x, y is (%d, %d)\n", (psS32) xPtr->n, (psS32) y->n); 516 xPtr = xPtr; 517 } 518 515 519 psVector *yPtr = NULL; 520 // Convert y to F32 if necessary. 516 521 if (PS_TYPE_F64 == y->type.type) { 517 y F32= psVectorAlloc(y->n, PS_TYPE_F32);522 yPtr = psVectorAlloc(y->n, PS_TYPE_F32); 518 523 for (psS32 i = 0 ; i < y->n ; i++) { 519 y F32->data.F32[i] = (psF32) y->data.F64[i];524 yPtr->data.F32[i] = (psF32) y->data.F64[i]; 520 525 } 521 yPtr = yF32;522 526 } else { 523 527 yPtr = (psVector *) y; … … 531 535 spline->spline = (psPolynomial1D **) psAlloc(numSplines * sizeof(psPolynomial1D *)); 532 536 for (psS32 i=0;i<numSplines;i++) { 533 (spline->spline)[i] = psPolynomial1DAlloc(3, PS_POLYNOMIAL_ORD); 534 } 535 // 536 // XXX: Ensure that the knots are distinct, and monotonic. 537 // XXX: Use a vector dup macro, or function here. 538 // 539 spline->knots = psVectorAlloc(x->n, PS_TYPE_F32); 540 for (psS32 i = 0 ; i < x->n ; i++) { 541 spline->knots->data.F32[i] = xPtr->data.F32[i]; 542 } 537 spline->spline[i] = psPolynomial1DAlloc(3, PS_POLYNOMIAL_ORD); 538 } 539 spline->knots = psVectorCopy(NULL, xPtr, PS_TYPE_F32); 543 540 // 544 541 // Generate the second derivatives at each data point. … … 554 551 psF32 H = xPtr->data.F32[i+1] - xPtr->data.F32[i]; 555 552 if (fabs(H) <= FLT_EPSILON) { 556 p rintf("XXX: Generate error:x data points are not distinct (%d %d).\n", i, i+1);553 psError(PS_ERR_UNKNOWN, false, "x data points are not distinct (%d %d).\n", i, i+1); 557 554 } 558 555 psTrace(__func__, 6, "x data (%f - %f) (%f)\n", xPtr->data.F32[i], xPtr->data.F32[i+1], H); 559 560 556 // 561 557 // ******** Calculate 0-order term ******** 562 558 // 563 559 // From (1) 564 (spline->spline[i])->coeff[0] = (yPtr->data.F32[i] * xPtr->data.F32[i+1]/H);560 spline->spline[i]->coeff[0] = yPtr->data.F32[i] * xPtr->data.F32[i+1]/H; 565 561 // From (2) 566 ((spline->spline[i])->coeff[0])-= ((yPtr->data.F32[i+1] * xPtr->data.F32[i])/H);562 spline->spline[i]->coeff[0]-= (yPtr->data.F32[i+1] * xPtr->data.F32[i])/H; 567 563 // From (3) 568 564 psF32 tmp = (xPtr->data.F32[i+1] * xPtr->data.F32[i+1] * xPtr->data.F32[i+1]) / (H * H * H); 569 tmp-= (xPtr->data.F32[i+1] / H);570 tmp*= (spline->p_psDeriv2)[i] * H * H / 6.0;571 ((spline->spline[i])->coeff[0])+= tmp;565 tmp-= xPtr->data.F32[i+1] / H; 566 tmp*= spline->p_psDeriv2[i] * H * H / 6.0; 567 spline->spline[i]->coeff[0]+= tmp; 572 568 // From (4) 573 569 tmp = -(xPtr->data.F32[i] * xPtr->data.F32[i] * xPtr->data.F32[i]) / (H * H * H); 574 tmp+= (xPtr->data.F32[i] / H);575 tmp*= (spline->p_psDeriv2)[i+1] * H * H / 6.0;576 ( (spline->spline[i])->coeff[0])+= tmp;570 tmp+= xPtr->data.F32[i] / H; 571 tmp*= spline->p_psDeriv2[i+1] * H * H / 6.0; 572 (spline->spline[i]->coeff[0])+= tmp; 577 573 578 574 // … … 580 576 // 581 577 // From (1) 582 (spline->spline[i])->coeff[1] = -(yPtr->data.F32[i]) / H;578 spline->spline[i]->coeff[1] = -(yPtr->data.F32[i]) / H; 583 579 // From (2) 584 ((spline->spline[i])->coeff[1])+= (yPtr->data.F32[i+1] / H);580 spline->spline[i]->coeff[1]+= yPtr->data.F32[i+1] / H; 585 581 // From (3) 586 tmp = -3.0 * (xPtr->data.F32[i+1] * xPtr->data.F32[i+1])/ (H * H * H);582 tmp = -3.0 * xPtr->data.F32[i+1] * xPtr->data.F32[i+1] / (H * H * H); 587 583 tmp+= (1.0 / H); 588 tmp*= ((spline->p_psDeriv2)[i])* H * H / 6.0;589 ((spline->spline[i])->coeff[1])+= tmp;584 tmp*= spline->p_psDeriv2[i] * H * H / 6.0; 585 spline->spline[i]->coeff[1]+= tmp; 590 586 // From (4) 591 tmp = 3.0 * (xPtr->data.F32[i] * xPtr->data.F32[i])/ (H * H * H);592 tmp-= (1.0 / H);593 tmp*= ((spline->p_psDeriv2)[i+1])* H * H / 6.0;594 ((spline->spline[i])->coeff[1])+= tmp;587 tmp = 3.0 * xPtr->data.F32[i] * xPtr->data.F32[i] / (H * H * H); 588 tmp-= 1.0 / H; 589 tmp*= spline->p_psDeriv2[i+1] * H * H / 6.0; 590 spline->spline[i]->coeff[1]+= tmp; 595 591 596 592 // … … 598 594 // 599 595 // From (3) 600 (spline->spline[i])->coeff[2] = ((spline->p_psDeriv2)[i]) * 3.0 * xPtr->data.F32[i+1] / (6.0 * H);596 spline->spline[i]->coeff[2] = ((spline->p_psDeriv2)[i]) * 3.0 * xPtr->data.F32[i+1] / (6.0 * H); 601 597 // From (4) 602 ( (spline->spline[i])->coeff[2])-= (((spline->p_psDeriv2)[i+1]) * 3.0 * xPtr->data.F32[i] / (6.0 * H));598 (spline->spline[i]->coeff[2])-= (((spline->p_psDeriv2)[i+1]) * 3.0 * xPtr->data.F32[i] / (6.0 * H)); 603 599 604 600 // … … 606 602 // 607 603 // From (3) 608 (spline->spline[i])->coeff[3] = -((spline->p_psDeriv2)[i]) / (6.0 * H);604 spline->spline[i]->coeff[3] = -((spline->p_psDeriv2)[i]) / (6.0 * H); 609 605 // From (4) 610 ( (spline->spline[i])->coeff[3])+= ((spline->p_psDeriv2)[i+1]) / (6.0 * H);611 612 psTrace(__func__, 6, "(spline->spline[%u])->coeff[0] is %f\n", i, (spline->spline[i])->coeff[0]);613 psTrace(__func__, 6, "(spline->spline[%u])->coeff[1] is %f\n", i, (spline->spline[i])->coeff[1]);614 psTrace(__func__, 6, "(spline->spline[%u])->coeff[2] is %f\n", i, (spline->spline[i])->coeff[2]);615 psTrace(__func__, 6, "(spline->spline[%u])->coeff[3] is %f\n", i, (spline->spline[i])->coeff[3]);616 } 617 618 if ( PS_TYPE_F64 == x->type.type) {619 psFree(x F32);606 (spline->spline[i]->coeff[3])+= ((spline->p_psDeriv2)[i+1]) / (6.0 * H); 607 608 psTrace(__func__, 6, "(spline->spline[%u])->coeff[0] is %f\n", i, spline->spline[i]->coeff[0]); 609 psTrace(__func__, 6, "(spline->spline[%u])->coeff[1] is %f\n", i, spline->spline[i]->coeff[1]); 610 psTrace(__func__, 6, "(spline->spline[%u])->coeff[2] is %f\n", i, spline->spline[i]->coeff[2]); 611 psTrace(__func__, 6, "(spline->spline[%u])->coeff[3] is %f\n", i, spline->spline[i]->coeff[3]); 612 } 613 614 if ((x == NULL) || (PS_TYPE_F64 == x->type.type)) { 615 psFree(xPtr); 620 616 } 621 617 if (PS_TYPE_F64 == y->type.type) { 622 psFree(y F32);618 psFree(yPtr); 623 619 } 624 620 psTrace(__func__, 3, "---- %s() end ----\n", __func__); … … 637 633 the spline fit functions require F32 and F64. 638 634 639 XXX: This only works if spline 0>knots if psF32. Must we add support for psU32 and635 XXX: This only works if spline->knots if psF32. Must we add support for psU32 and 640 636 psF64? 641 637 *****************************************************************************/
Note:
See TracChangeset
for help on using the changeset viewer.
