1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123
|
/**************************************************************************
begin : April 19 2004
version : 1.0
copyright : (C) 2004 by Gleb Beliakov
email : gleb@deakin.edu.au
An example of how to use lint package
this program:
1. randomly generates data
2. Builds the interpolant
3. computes the value of the interpolant and compates it with the test
data (model function)
4. reports preprocessing and evaluation time and the accuracy of
approximation
* *
* Gleb Beliakov, 2004 *
* *
* 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 PURPOSE. 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 <cstdlib>
#include <iostream>
using namespace std;
#include "interpol.h"
int dim=2;
int npts=1500;
// generates a random number (real) between x and y
double myrand(double x, double y)
{
if(x>y)
return y + ((x-y)*rand())/(RAND_MAX);
else if(x<y)
return x + ((y-x)*rand())/(RAND_MAX);
return x;
}
// test function, here just a product of sin(2x)sin(2y),...
double fun2(double* dat)
{
// return 0;
int j;
double s=1;
for(j=0;j<dim;j++)
s*=sin(2*dat[j]);
return s;
}
int main(int argc, char *argv[])
{
int j,i;
double *x, *XData, *YData;
// arrays to store the data
x=(double*)malloc((dim+1)*sizeof(double));
XData=(double*)malloc(dim*npts*sizeof(double));
YData=(double*)malloc(npts*sizeof(double));
// generate data randomly
for(i=0;i<npts;i++) {
for(j=0;j<dim;j++) {
x[j]=myrand(3.0,0);
XData[i*dim + j]=x[j];
}
YData[i]=fun2(x);
}
STCInterpolant LipInt;
ResetTime();
// set Lipschitz const
LipInt.SetConstants(10.00);
LipInt.SetData(dim,npts, XData, YData,1);
cout<< ElapsedTime()<< " (s) - Preprocessing time" <<endl;
ResetTime();
LipInt.Construct();
free(XData); free(YData);
// testing evaluation
int k2,K2=1000; // how many function evaluations
double w,w1, err, err2; // compute the error of approximation
err2=err=0;
for(k2=0;k2<K2;k2++) {
for(j=0;j<dim;j++) x[j]=myrand(3.0,0); // randomly choose a test point
w=LipInt.Value(dim,x); // evaluate the interpolant
w1=fun2(x); // the true function
w=fabs(w-w1); // compute the error
if(err<w) err=w;
err2+=w*w;
}
err2=sqrt(err2/K2); // average error RMSE
cout<< ElapsedTime()<< " (s) evaluation, average time is "<< ElapsedTime()/K2 <<endl;
cout << "max error "<<err<<endl;
cout << "av error "<<err2<<endl;
return 0;
}
|