chiark / gitweb /
move some util tests from comments into separate files
authorAmro <amroamroamro@gmail.com>
Tue, 13 Sep 2016 17:43:27 +0000 (20:43 +0300)
committerAmro <amroamroamro@gmail.com>
Tue, 13 Sep 2016 18:24:29 +0000 (21:24 +0300)
util/mt19937ar.c
util/mt19937ar_test.c [new file with mode: 0644]
util/sobolseq.c
util/sobolseq_test.c [new file with mode: 0644]

index d0b90ac777c146d598879d76d8c2c6a8b78628d9..1ce950bdddb83e315fb3c2d09ff638c71a755672 100644 (file)
@@ -230,24 +230,3 @@ double nlopt_nrand(double mean, double stddev)
     return mean + v1 * sqrt(-2 * log(s) / s) * stddev;
   }
 }
-
-#if 0
-#include <stdio.h>
-int main(void)
-{
-    int i;
-    uint32_t init[4]={0x123, 0x234, 0x345, 0x456}, length=4;
-    init_by_array(init, length);
-    printf("1000 outputs of nlopt_genrand_int32()\n");
-    for (i=0; i<1000; i++) {
-      printf("%10lu ", nlopt_genrand_int32());
-      if (i%5==4) printf("\n");
-    }
-    printf("\n1000 outputs of genrand_real2()\n");
-    for (i=0; i<1000; i++) {
-      printf("%10.8f ", genrand_real2());
-      if (i%5==4) printf("\n");
-    }
-    return 0;
-}
-#endif
diff --git a/util/mt19937ar_test.c b/util/mt19937ar_test.c
new file mode 100644 (file)
index 0000000..29d89a9
--- /dev/null
@@ -0,0 +1,25 @@
+#include <stdio.h>
+#include "nlopt-util.h"
+
+int main(void)
+{
+    int i;
+    /*
+    uint32_t init[4]={0x123, 0x234, 0x345, 0x456}, length=4;
+    nlopt_init_by_array(init, length);
+    */
+    nlopt_init_genrand(5489UL);
+    /* nlopt_genrand_int32 */
+    printf("1000 outputs of nlopt_iurand()\n");
+    for (i=0; i<1000; i++) {
+        printf("%10d ", nlopt_iurand(0x7fff));
+        if (i%5==4) printf("\n");
+    }
+    /* genrand_real2 */
+    printf("\n1000 outputs of nlopt_urand()\n");
+    for (i=0; i<1000; i++) {
+        printf("%10.8f ", nlopt_urand(0,1));
+        if (i%5==4) printf("\n");
+    }
+    return 0;
+}
index b3bee05f9f8da363c09312588dd5bd36b64b78ba..b0eb82203f043fa0df5baf01dc47c3644f24fdb5 100644 (file)
@@ -243,57 +243,3 @@ void nlopt_sobol_skip(nlopt_sobol s, unsigned n, double *x)
          while (k-- > 0) sobol_gen(s, x);
      }
 }
-
-/************************************************************************/
-
-/* compile with -DSOBOLSEQ_TEST for test program */
-#ifdef SOBOLSEQ_TEST
-#include <stdio.h>
-#include <time.h>
-
-/* test integrand from Joe and Kuo paper ... integrates to 1 */
-static double testfunc(unsigned n, const double *x)
-{
-     double f = 1;
-     unsigned j;
-     for (j = 1; j <= n; ++j) {
-         double cj = pow((double) j, 0.3333333333333333333);
-         f *= (fabs(4*x[j-1] - 2) + cj) / (1 + cj);
-     }
-     return f;
-}
-
-int main(int argc, char **argv)
-{
-     unsigned n, j, i, sdim;
-     static double x[MAXDIM];
-     double testint_sobol = 0, testint_rand = 0;
-     nlopt_sobol s;
-     if (argc < 3) { 
-         fprintf(stderr, "Usage: %s <sdim> <ngen>\n", argv[0]);
-         return 1;
-     }
-     nlopt_init_genrand(time(NULL));
-     sdim = atoi(argv[1]);
-     s = nlopt_sobol_create(sdim);
-     n = atoi(argv[2]);
-     nlopt_sobol_skip(s, n, x);
-     for (j = 1; j <= n; ++j) {
-         nlopt_sobol_next01(s, x);
-         testint_sobol += testfunc(sdim, x);
-         if (j < 100) {
-              printf("x[%d]: %g", j, x[0]);
-              for (i = 1; i < sdim; ++i) printf(", %g", x[i]);
-              printf("\n");
-         }
-         for (i = 0; i < sdim; ++i) x[i] = nlopt_urand(0.,1.);
-         testint_rand += testfunc(sdim, x);
-     }
-     nlopt_sobol_destroy(s);
-     printf("Test integral = %g using Sobol, %g using pseudorandom.\n",
-           testint_sobol / n, testint_rand / n);
-     printf("        error = %g using Sobol, %g using pseudorandom.\n",
-           testint_sobol / n - 1, testint_rand / n - 1);
-     return 0;
-}
-#endif
diff --git a/util/sobolseq_test.c b/util/sobolseq_test.c
new file mode 100644 (file)
index 0000000..0936ae1
--- /dev/null
@@ -0,0 +1,75 @@
+/* Copyright (c) 2007 Massachusetts Institute of Technology
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining
+ * a copy of this software and associated documentation files (the
+ * "Software"), to deal in the Software without restriction, including
+ * without limitation the rights to use, copy, modify, merge, publish,
+ * distribute, sublicense, and/or sell copies of the Software, and to
+ * permit persons to whom the Software is furnished to do so, subject to
+ * the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be
+ * included in all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+ * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
+ * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
+ * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
+ * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+ */
+
+#include <stdlib.h>
+#include <stdio.h>
+#include <math.h>
+#include <time.h>
+#include "nlopt-util.h"
+
+#define MAXDIM 1111
+
+/* test integrand from Joe and Kuo paper ... integrates to 1 */
+static double testfunc(unsigned n, const double *x)
+{
+    double f = 1;
+    unsigned j;
+    for (j = 1; j <= n; ++j) {
+        double cj = pow((double) j, 0.3333333333333333333);
+        f *= (fabs(4*x[j-1] - 2) + cj) / (1 + cj);
+    }
+    return f;
+}
+
+int main(int argc, char **argv)
+{
+    unsigned n, j, i, sdim;
+    static double x[MAXDIM];
+    double testint_sobol = 0, testint_rand = 0;
+    nlopt_sobol s;
+    if (argc < 3) {
+        fprintf(stderr, "Usage: %s <sdim> <ngen>\n", argv[0]);
+        return 1;
+    }
+    nlopt_init_genrand(time(NULL));
+    sdim = atoi(argv[1]);
+    s = nlopt_sobol_create(sdim);
+    n = atoi(argv[2]);
+    nlopt_sobol_skip(s, n, x);
+    for (j = 1; j <= n; ++j) {
+        nlopt_sobol_next01(s, x);
+        testint_sobol += testfunc(sdim, x);
+        if (j < 100) {
+            printf("x[%u]: %g", j, x[0]);
+            for (i = 1; i < sdim; ++i) printf(", %g", x[i]);
+            printf("\n");
+        }
+        for (i = 0; i < sdim; ++i) x[i] = nlopt_urand(0.,1.);
+        testint_rand += testfunc(sdim, x);
+    }
+    nlopt_sobol_destroy(s);
+    printf("Test integral = %g using Sobol, %g using pseudorandom.\n",
+        testint_sobol / n, testint_rand / n);
+    printf("        error = %g using Sobol, %g using pseudorandom.\n",
+        testint_sobol / n - 1, testint_rand / n - 1);
+    return 0;
+}