Changeset 1953 for trunk/psLib/src/dataManip/psMinimize.c
- Timestamp:
- Oct 4, 2004, 3:43:58 PM (22 years ago)
- File:
-
- 1 edited
-
trunk/psLib/src/dataManip/psMinimize.c (modified) (13 diffs)
Legend:
- Unmodified
- Added
- Removed
-
trunk/psLib/src/dataManip/psMinimize.c
r1945 r1953 9 9 * @author George Gusciora, MHPCC 10 10 * 11 * @version $Revision: 1.5 1$ $Name: not supported by cvs2svn $12 * @date $Date: 2004-10-0 3 23:35:47$11 * @version $Revision: 1.52 $ $Name: not supported by cvs2svn $ 12 * @date $Date: 2004-10-05 01:43:58 $ 13 13 * 14 14 * Copyright 2004 Maui High Performance Computing Center, University of Hawaii … … 24 24 /* INCLUDE FILES */ 25 25 /*****************************************************************************/ 26 #include <stdlib.h>27 #include <stdio.h>28 #include <string.h>29 #include <stdarg.h>30 #include <float.h>31 #include <math.h>32 33 #include <gsl/gsl_multifit_nlin.h>34 #include <gsl/gsl_multimin.h>35 #include <gsl/gsl_rng.h>36 #include <gsl/gsl_randist.h>37 #include <gsl/gsl_vector.h>38 #include <gsl/gsl_blas.h>39 40 #include "psMemory.h"41 #include "psVector.h"42 #include "psImage.h"43 #include "psTrace.h"44 #include "psLogMsg.h"45 #include "psError.h"46 #include "psAbort.h"47 #include "psFunctions.h"48 26 #include "psMinimize.h" 49 #include "psMatrix.h"50 #include "psConstants.h"51 52 27 /*****************************************************************************/ 53 28 /* DEFINE STATEMENTS */ … … 655 630 } 656 631 632 #define GEN_STATIC_RECYCLED_VECTOR(NAME, SIZE, TYPE) \ 633 static psVector *NAME = NULL; \ 634 NAME = psVectorRecycle(NAME, SIZE, TYPE); \ 635 p_psMemSetPersistent(NAME, true); \ 636 p_psMemSetPersistent(NAME->data.V, true); \ 637 657 638 658 639 /****************************************************************************** … … 661 642 coefficients of that polynomial. 662 643 663 XXX: yErr is currently ignored.664 665 XXX: must add type F32 (currently F64 only). 666 667 XXX:Use private name?668 *****************************************************************************/644 XXX: 645 yErr is currently ignored. 646 647 XXX: 648 Use private name? 649 *****************************************************************************/ 669 650 psPolynomial1D *p_psVectorFitPolynomial1DCheby(psPolynomial1D* myPoly, 670 651 const psVector* restrict x, … … 675 656 int k; 676 657 int n = x->n; 677 psVector *f = psVectorAlloc(n, PS_TYPE_F64);678 658 double fac; 679 659 double sum; 680 // XXX: Use static memory here. 681 psScalar *tmpScalar = psScalarAlloc(0.0, PS_TYPE_F32); 660 GEN_STATIC_RECYCLED_VECTOR(f, n, PS_TYPE_F64); 682 661 psScalar *fScalar; 662 psScalar tmpScalar; 663 tmpScalar.type.type = PS_TYPE_F64; 683 664 684 665 // XXX: These assignments appear too simple to warrant code and … … 690 671 double bpa = 0.5 * (max+min); // 0 691 672 692 // XXX: Eliminate this later by generating a F64 version of the693 // LaGrange interpolation routines.694 PS_VECTOR_F64_TO_F32(x, x32);695 PS_VECTOR_F64_TO_F32(y, y32);696 697 673 // In this loop, we first calculate the values of X for which the 698 674 // Chebyshev polynomials are zero (see NR, section 5.4). Then we … … 707 683 double Y = cos(M_PI * (0.5 + ((float) i)) / ((float) n)); 708 684 double X = (Y + bma + bpa) - 1.0; 709 tmpScalar ->data.F32 = (float)X;685 tmpScalar.data.F64 = X; 710 686 711 687 // We interpolate against are tabluated x,y vectors to determine the 712 688 // function value at X. 713 fScalar = p_psVectorInterpolate((psVector *) x32, (psVector *) y32, 714 3, tmpScalar); 715 716 f->data.F64[i] = (double) fScalar->data.F32; 689 fScalar = p_psVectorInterpolate((psVector *) x, 690 (psVector *) y, 691 3, 692 &tmpScalar); 693 694 f->data.F64[i] = fScalar->data.F64; 717 695 psFree(fScalar); 718 696 … … 724 702 // We have the values for f() at the zero points, we now calculate the 725 703 // coefficients of the Chebyshev polynomial: NR 5.8.7. 704 726 705 fac = 2.0/((float) n); 727 728 706 // XXX: is this loop bound correct? 729 707 for (j=0;j<myPoly->n;j++) { … … 737 715 } 738 716 739 // XXX: Must free memory.740 psFree(f);741 psFree(x32);742 psFree(y32);743 psFree(tmpScalar);744 745 717 return(myPoly); 746 718 } … … 750 722 polynomial of degree myPoly to the data points (x, y) and return the 751 723 coefficients of that polynomial. 752 753 XXX: must add type F32 (currently F64 only).754 724 755 725 XXX: Use private name? … … 875 845 polynomial. 876 846 877 XXX: must add type F32 (currently F64 only).847 XXX: type F32 is done via vector conversion only. 878 848 *****************************************************************************/ 879 849 psPolynomial1D* psVectorFitPolynomial1D(psPolynomial1D* myPoly, … … 882 852 const psVector* restrict yErr) 883 853 { 884 bool mustFreeMyYErr = false;885 bool mustFreemyX = false;886 854 int i; 887 855 psPolynomial1D *tmpPoly; 888 psVector *myX = NULL;889 psVector *myYErr = NULL;890 891 856 PS_CHECK_NULL_1DPOLY_ACTION(myPoly, 0); 892 857 PS_CHECK_NULL_VECTOR_ACTION(y, 0); 893 858 PS_CHECK_EMPTY_VECTOR_ACTION(y, 0); 859 psVector *x64 = NULL; 860 psVector *y64 = NULL; 861 psVector *yErr64 = NULL; 862 static psVector *x64Static = NULL; 863 static psVector *y64Static = NULL; 864 static psVector *yErr64Static = NULL; 865 866 if (y->type.type == PS_TYPE_F64) { 867 y64 = (psVector *) y; 868 } else if (y->type.type == PS_TYPE_F32) { 869 y64Static = psVectorRecycle(y64Static, y->n, PS_TYPE_F64); 870 p_psMemSetPersistent(y64Static, true); 871 p_psMemSetPersistent(y64Static->data.V, true); 872 y64 = y64Static; 873 } else { 874 // XXX: psError() bad type. 875 psAbort(__func__, "Bad type for y64 (%d)", y->type.type); 876 } 877 894 878 895 879 // If yErr==NULL, set all errors equal. 896 880 if (yErr == NULL) { 897 myYErr = psVectorAlloc(y->n, y->type.type); 898 mustFreeMyYErr = true; 899 900 if (y->type.type == PS_TYPE_F32) { 901 for (i=0;i<myYErr->n;i++) { 902 myYErr->data.F32[i] = 1.0; 903 } 904 } else if (y->type.type == PS_TYPE_F64) { 905 for (i=0;i<myYErr->n;i++) { 906 myYErr->data.F64[i] = 1.0; 907 } 908 } 881 yErr64Static = psVectorRecycle(yErr64Static, y->n, PS_TYPE_F64); 882 p_psMemSetPersistent(yErr64Static, true); 883 p_psMemSetPersistent(yErr64Static->data.V, true); 884 885 for (i=0;i<yErr64Static->n;i++) { 886 yErr64Static->data.F64[i] = 1.0; 887 } 888 yErr64 = yErr64Static; 909 889 } else { 910 myYErr = (psVector *) yErr; 911 } 912 913 // If x==NULL, create an myX vector with x values set to (0:n), and if 890 if (yErr->type.type == PS_TYPE_F64) { 891 yErr64 = (psVector *) yErr; 892 } else if (yErr->type.type == PS_TYPE_F32) { 893 yErr64Static = psVectorRecycle(yErr64Static, yErr->n, PS_TYPE_F64); 894 p_psMemSetPersistent(yErr64Static, true); 895 p_psMemSetPersistent(yErr64Static->data.V, true); 896 } else { 897 // XXX: psError() bad type. 898 psAbort(__func__, "Bad type for yErr64"); 899 } 900 } 901 902 // If x==NULL, create an x64 vector with x values set to (0:n), and if 914 903 // this is a Chebyshev polynomial, we must scale to (-1:1). 915 916 904 // XXX: Verify that this is the correct action. 917 905 if (x == NULL) { 918 myX = psVectorAlloc(y->n, y->type.type); 919 mustFreemyX = true; 920 921 if (y->type.type == PS_TYPE_F32) { 922 if (myPoly->type == PS_POLYNOMIAL_ORD) { 923 for (i=0;i<myX->n;i++) { 924 myX->data.F32[i] = (float) i; 925 } 926 } else if (myPoly->type == PS_POLYNOMIAL_CHEB) { 927 float min = 0.0; 928 float max = (float) (y->n - 1); 929 930 for (i=0;i<myX->n;i++) { 931 myX->data.F32[i] = (((float) i) - 0.5 * (min + max)) / 932 (0.5 * (max - min)); 933 } 934 } 935 } else if (y->type.type == PS_TYPE_F64) { 936 if (myPoly->type == PS_POLYNOMIAL_ORD) { 937 for (i=0;i<myX->n;i++) { 938 myX->data.F64[i] = (float) i; 939 } 940 } else if (myPoly->type == PS_POLYNOMIAL_CHEB) { 941 double min = 0.0; 942 double max = (double) (y->n - 1); 943 944 for (i=0;i<myX->n;i++) { 945 myX->data.F64[i] = (((float) i) - 0.5 * (min + max)) / 946 (0.5 * (max - min)); 947 } 906 x64Static = psVectorRecycle(x64Static, y->n, PS_TYPE_F64); 907 p_psMemSetPersistent(x64Static, true); 908 p_psMemSetPersistent(x64Static->data.V, true); 909 x64 = x64Static; 910 911 if (myPoly->type == PS_POLYNOMIAL_ORD) { 912 for (i=0;i<x64->n;i++) { 913 x64->data.F64[i] = (float) i; 914 } 915 } else if (myPoly->type == PS_POLYNOMIAL_CHEB) { 916 double min = 0.0; 917 double max = (double) (y->n - 1); 918 919 for (i=0;i<x64->n;i++) { 920 x64->data.F64[i] = (((double) i) - 0.5 * (min + max)) / 921 (0.5 * (max - min)); 948 922 } 949 923 } 950 924 } else { 951 myX = (psVector *) x; 952 } 953 954 PS_CHECK_VECTOR_SIZE_EQUAL(y, myX); 955 PS_CHECK_VECTOR_SIZE_EQUAL(y, myYErr); 925 if (x->type.type == PS_TYPE_F64) { 926 x64 = (psVector *) x; 927 } else if (x->type.type == PS_TYPE_F32) { 928 x64Static = psVectorRecycle(x64Static, x->n, PS_TYPE_F64); 929 p_psMemSetPersistent(x64Static, true); 930 p_psMemSetPersistent(x64Static->data.V, true); 931 x64 = x64Static; 932 } else { 933 // XXX: psError() bad type. 934 psAbort(__func__, "Bad type for x64"); 935 } 936 } 937 938 PS_CHECK_VECTOR_SIZE_EQUAL(y64, x64); 939 PS_CHECK_VECTOR_SIZE_EQUAL(y64, yErr64); 956 940 957 941 // Call the appropriate vector fitting routine. 958 942 if (myPoly->type == PS_POLYNOMIAL_CHEB) { 959 tmpPoly = p_psVectorFitPolynomial1DCheby(myPoly, myX, y, myYErr);943 tmpPoly = p_psVectorFitPolynomial1DCheby(myPoly, x64, y64, yErr64); 960 944 } else if (myPoly->type == PS_POLYNOMIAL_ORD) { 961 tmpPoly = p_psVectorFitPolynomial1DOrd(myPoly, myX, y, myYErr);945 tmpPoly = p_psVectorFitPolynomial1DOrd(myPoly, x64, y64, yErr64); 962 946 } else { 963 947 // XXX: psErrorMsg() … … 966 950 967 951 // Free any allocated memory. 968 if (mustFreeMyYErr == true) { 969 psFree(myYErr); 970 } 971 if (mustFreemyX == true) { 972 psFree(myX); 973 } 952 /* 953 if ((yErr == NULL) || (yErr->type.type != PS_TYPE_F64)) { 954 psFree(yErr64); 955 } 956 if ((x == NULL) || (x->type.type != PS_TYPE_F64)) { 957 psFree(x64); 958 } 959 if ((y == NULL) || (y->type.type != PS_TYPE_F64)) { 960 psFree(y64); 961 } 962 */ 974 963 975 964 return(myPoly);
Note:
See TracChangeset
for help on using the changeset viewer.
