File indexing completed on 2024-05-05 03:48:44
0001 /* 0002 File : NSLIntTest.cpp 0003 Project : LabPlot 0004 Description : NSL Tests for numerical integration 0005 -------------------------------------------------------------------- 0006 SPDX-FileCopyrightText: 2019 Stefan Gerlach <stefan.gerlach@uni.kn> 0007 0008 SPDX-License-Identifier: GPL-2.0-or-later 0009 */ 0010 0011 #include "NSLIntTest.h" 0012 0013 extern "C" { 0014 #include "backend/nsl/nsl_int.h" 0015 } 0016 0017 // ############################################################################## 0018 // ################# rule integral/area tests 0019 // ############################################################################## 0020 0021 const int N = 5; 0022 double rxdata[] = {1, 2, 3, 5, 7}; 0023 0024 void NSLIntTest::testRectangle_integral() { 0025 double ydata[] = {2, 2, 2, -2, -2}; 0026 0027 int status = nsl_int_rectangle(rxdata, ydata, N, 0); 0028 QCOMPARE(status, 0); 0029 QCOMPARE(ydata[N - 1], 4.); 0030 } 0031 0032 void NSLIntTest::testRectangle_area() { 0033 double ydata[] = {2, 2, 2, -2, -2}; 0034 0035 int status = nsl_int_rectangle(rxdata, ydata, N, 1); 0036 QCOMPARE(status, 0); 0037 QCOMPARE(ydata[N - 1], 12.); 0038 } 0039 0040 void NSLIntTest::testTrapezoid_integral() { 0041 double ydata[] = {1, 2, 3, -1, -3}; 0042 0043 int status = nsl_int_trapezoid(rxdata, ydata, N, 0); 0044 QCOMPARE(status, 0); 0045 QCOMPARE(ydata[N - 1], 2.); 0046 } 0047 0048 void NSLIntTest::testTrapezoid_area() { 0049 double ydata[] = {1, 2, 3, -1, -3}; 0050 0051 int status = nsl_int_trapezoid(rxdata, ydata, N, 1); 0052 QCOMPARE(status, 0); 0053 QCOMPARE(ydata[N - 1], 10.5); 0054 } 0055 0056 void NSLIntTest::test3Point_integral() { 0057 double ydata[] = {1, 2, 3, -1, -3}; 0058 0059 int np = (int)nsl_int_simpson(rxdata, ydata, N, 0); 0060 QCOMPARE(np, 3); 0061 QCOMPARE(ydata[np - 1], 4 / 3.); 0062 } 0063 0064 void NSLIntTest::test4Point_integral() { 0065 double xdata2[] = {1, 2, 3, 5, 7, 8, 9}; 0066 double ydata[] = {2, 2, 2, 2, 2, 2, 2, 2}; 0067 const int n = 7; 0068 0069 int np = (int)nsl_int_simpson_3_8(xdata2, ydata, n, 0); 0070 QCOMPARE(np, 3); 0071 QCOMPARE(ydata[np - 1], 16.); 0072 } 0073 0074 // ############################################################################## 0075 // ################# performance 0076 // ############################################################################## 0077 0078 void NSLIntTest::testPerformanceRectangle() { 0079 const int n = 1e6; 0080 QScopedArrayPointer<double> xdata(new double[n]); 0081 QScopedArrayPointer<double> ydata(new double[n]); 0082 0083 for (int i = 0; i < n; i++) 0084 xdata[i] = (double)i; 0085 0086 QBENCHMARK { 0087 for (int i = 0; i < n; i++) 0088 ydata[i] = 1.; 0089 int status = nsl_int_rectangle(xdata.data(), ydata.data(), n, 0); 0090 QCOMPARE(status, 0); 0091 } 0092 QCOMPARE(ydata[n - 1], (double)(n - 1)); 0093 } 0094 0095 void NSLIntTest::testPerformanceTrapezoid() { 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_trapezoid(xdata.data(), ydata.data(), n, 0); 0107 QCOMPARE(status, 0); 0108 } 0109 QCOMPARE(ydata[n - 1], (double)(n - 1)); 0110 } 0111 0112 void NSLIntTest::testPerformance3Point() { 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 int np = 1; 0121 QBENCHMARK { 0122 for (int i = 0; i < n; i++) 0123 ydata[i] = 1.; 0124 np = (int)nsl_int_simpson(xdata.data(), ydata.data(), n, 0); 0125 QCOMPARE(np, n / 2 + 1); 0126 } 0127 QCOMPARE(ydata[np - 1], (double)(n - 1)); 0128 } 0129 0130 void NSLIntTest::testPerformance4Point() { 0131 const int n = 1e6; 0132 QScopedArrayPointer<double> xdata(new double[n]); 0133 QScopedArrayPointer<double> ydata(new double[n]); 0134 0135 for (int i = 0; i < n; i++) 0136 xdata[i] = (double)i; 0137 0138 int np = 1; 0139 QBENCHMARK { 0140 for (int i = 0; i < n; i++) 0141 ydata[i] = 1.; 0142 np = (int)nsl_int_simpson_3_8(xdata.data(), ydata.data(), n, 0); 0143 QCOMPARE(np, n / 3 + 1); 0144 } 0145 0146 // TODO: 0147 // QCOMPARE(ydata[np - 1], (double)(n - 1)); 0148 printf("%.15g %.15g\n", ydata[np - 1], (double)(n - 1)); 0149 } 0150 0151 QTEST_MAIN(NSLIntTest)