File indexing completed on 2024-05-12 15:28:34

0001 /***************************************************************************
0002     File                 : NSLIntTest.cpp
0003     Project              : LabPlot
0004     Description          : NSL Tests for numerical integration
0005     --------------------------------------------------------------------
0006     Copyright            : (C) 2019 Stefan Gerlach (stefan.gerlach@uni.kn)
0007  ***************************************************************************/
0008 
0009 /***************************************************************************
0010  *                                                                         *
0011  *  This program is free software; you can redistribute it and/or modify   *
0012  *  it under the terms of the GNU General Public License as published by   *
0013  *  the Free Software Foundation; either version 2 of the License, or      *
0014  *  (at your option) any later version.                                    *
0015  *                                                                         *
0016  *  This program is distributed in the hope that it will be useful,        *
0017  *  but WITHOUT ANY WARRANTY; without even the implied warranty of         *
0018  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the          *
0019  *  GNU General Public License for more details.                           *
0020  *                                                                         *
0021  *   You should have received a copy of the GNU General Public License     *
0022  *   along with this program; if not, write to the Free Software           *
0023  *   Foundation, Inc., 51 Franklin Street, Fifth Floor,                    *
0024  *   Boston, MA  02110-1301  USA                                           *
0025  *                                                                         *
0026  ***************************************************************************/
0027 
0028 #include "NSLIntTest.h"
0029 
0030 extern "C" {
0031 #include "backend/nsl/nsl_int.h"
0032 }
0033 
0034 //##############################################################################
0035 //#################  rule integral/area tests
0036 //##############################################################################
0037 
0038 const int N = 5;
0039 double xdata[] = {1, 2, 3, 5, 7};
0040 
0041 void NSLIntTest::testRectangle_integral() {
0042     double ydata[] = {2, 2, 2, -2, -2};
0043 
0044     int status = nsl_int_rectangle(xdata, ydata, N, 0);
0045     QCOMPARE(status, 0);
0046     QCOMPARE(ydata[N - 1], 4.);
0047 }
0048 
0049 void NSLIntTest::testRectangle_area() {
0050     double ydata[] = {2, 2, 2, -2, -2};
0051 
0052     int status = nsl_int_rectangle(xdata, ydata, N, 1);
0053     QCOMPARE(status, 0);
0054     QCOMPARE(ydata[N - 1], 12.);
0055 }
0056 
0057 void NSLIntTest::testTrapezoid_integral() {
0058     double ydata[] = {1, 2, 3, -1, -3};
0059 
0060     int status = nsl_int_trapezoid(xdata, ydata, N, 0);
0061     QCOMPARE(status, 0);
0062     QCOMPARE(ydata[N - 1], 2.);
0063 }
0064 
0065 void NSLIntTest::testTrapezoid_area() {
0066     double ydata[] = {1, 2, 3, -1, -3};
0067 
0068     int status = nsl_int_trapezoid(xdata, ydata, N, 1);
0069     QCOMPARE(status, 0);
0070     QCOMPARE(ydata[N - 1], 10.5);
0071 }
0072 
0073 void NSLIntTest::test3Point_integral() {
0074     double ydata[] = {1, 2, 3, -1, -3};
0075 
0076     int np = (int)nsl_int_simpson(xdata, ydata, N, 0);
0077     QCOMPARE(np, 3);
0078     QCOMPARE(ydata[np - 1], 4/3.);
0079 }
0080 
0081 void NSLIntTest::test4Point_integral() {
0082     double xdata2[]={1, 2, 3, 5, 7, 8, 9};
0083     double ydata[] = {2, 2, 2, 2, 2, 2, 2, 2};
0084     const int n = 7;
0085 
0086     int np = (int)nsl_int_simpson_3_8(xdata2, ydata, n, 0);
0087     QCOMPARE(np, 3);
0088     QCOMPARE(ydata[np - 1], 16.);
0089 }
0090 
0091 //##############################################################################
0092 //#################  performance
0093 //##############################################################################
0094 
0095 void NSLIntTest::testPerformanceRectangle() {
0096     const int n = 1e6;
0097     QScopedArrayPointer<double> xdata(new double[n]);
0098     QScopedArrayPointer<double> ydata(new double[n]);
0099 
0100     for (int i = 0;  i < n; i++)
0101         xdata[i] = (double)i;
0102 
0103     QBENCHMARK {
0104         for (int i = 0;  i < n; i++)
0105             ydata[i] = 1.;
0106         int status = nsl_int_rectangle(xdata.data(), ydata.data(), n, 0);
0107         QCOMPARE(status, 0);
0108     }
0109     QCOMPARE(ydata[n - 1], (double)(n - 1));
0110 }
0111 
0112 void NSLIntTest::testPerformanceTrapezoid() {
0113     const int n = 1e6;
0114     QScopedArrayPointer<double> xdata(new double[n]);
0115     QScopedArrayPointer<double> ydata(new double[n]);
0116 
0117     for (int i = 0;  i < n; i++)
0118         xdata[i] = (double)i;
0119 
0120     QBENCHMARK {
0121         for (int i = 0;  i < n; i++)
0122             ydata[i] = 1.;
0123         int status = nsl_int_trapezoid(xdata.data(), ydata.data(), n, 0);
0124         QCOMPARE(status, 0);
0125     }
0126     QCOMPARE(ydata[n - 1], (double)(n - 1));
0127 }
0128 
0129 void NSLIntTest::testPerformance3Point() {
0130     const int n = 1e6;
0131     QScopedArrayPointer<double> xdata(new double[n]);
0132     QScopedArrayPointer<double> ydata(new double[n]);
0133 
0134     for (int i = 0;  i < n; i++)
0135         xdata[i] = (double)i;
0136 
0137     int np = 1;
0138     QBENCHMARK {
0139         for (int i = 0;  i < n; i++)
0140             ydata[i] = 1.;
0141         np = (int)nsl_int_simpson(xdata.data(), ydata.data(), n, 0);
0142         QCOMPARE(np, n/2 + 1);
0143     }
0144     QCOMPARE(ydata[np - 1], (double)(n - 1));
0145 }
0146 
0147 void NSLIntTest::testPerformance4Point() {
0148     const int n = 1e6;
0149     QScopedArrayPointer<double> xdata(new double[n]);
0150     QScopedArrayPointer<double> ydata(new double[n]);
0151 
0152     for (int i = 0;  i < n; i++)
0153         xdata[i] = (double)i;
0154 
0155     int np = 1;
0156     QBENCHMARK {
0157         for (int i = 0;  i < n; i++)
0158             ydata[i] = 1.;
0159         np = (int)nsl_int_simpson_3_8(xdata.data(), ydata.data(), n, 0);
0160         QCOMPARE(np, n/3 + 1);
0161     }
0162 
0163     //TODO:
0164     //QCOMPARE(ydata[np - 1], (double)(n - 1));
0165     printf("%.15g %.15g\n", ydata[np - 1], (double)(n-1));
0166 }
0167 
0168 QTEST_MAIN(NSLIntTest)