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 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278 2279 2280 2281 2282 2283 2284 2285 2286 2287 2288 2289 2290 2291 2292 2293 2294 2295 2296 2297 2298 2299 2300 2301 2302 2303 2304 2305 2306 2307 2308 2309 2310 2311 2312 2313 2314 2315 2316 2317 2318 2319 2320 2321 2322 2323 2324 2325 2326 2327 2328 2329 2330 2331 2332 2333 2334 2335 2336 2337 2338 2339 2340 2341 2342 2343 2344 2345 2346 2347 2348 2349 2350 2351 2352 2353 2354 2355 2356 2357 2358 2359 2360 2361 2362 2363 2364 2365 2366 2367 2368 2369 2370 2371 2372 2373 2374 2375 2376 2377 2378 2379 2380 2381 2382 2383 2384 2385 2386 2387 2388 2389 2390 2391 2392 2393 2394 2395 2396 2397 2398 2399 2400 2401 2402 2403 2404 2405 2406 2407 2408 2409 2410 2411 2412 2413 2414 2415 2416 2417 2418 2419 2420 2421 2422 2423 2424 2425 2426 2427 2428 2429 2430 2431 2432 2433 2434 2435 2436 2437 2438 2439 2440 2441 2442 2443 2444 2445 2446 2447 2448 2449 2450 2451 2452 2453 2454 2455 2456 2457 2458 2459 2460 2461 2462 2463 2464 2465 2466 2467 2468 2469 2470 2471 2472 2473 2474 2475 2476 2477 2478 2479 2480 2481 2482 2483 2484 2485 2486 2487 2488 2489 2490 2491 2492 2493 2494 2495 2496 2497 2498 2499 2500 2501 2502 2503 2504 2505 2506 2507 2508 2509 2510 2511 2512 2513 2514 2515 2516 2517 2518 2519 2520 2521 2522 2523 2524 2525 2526 2527 2528 2529 2530 2531 2532 2533 2534 2535 2536 2537 2538 2539 2540 2541 2542 2543 2544 2545 2546 2547 2548 2549 2550 2551 2552 2553 2554 2555 2556 2557 2558 2559 2560 2561 2562 2563 2564 2565 2566 2567 2568 2569 2570 2571 2572 2573 2574 2575 2576 2577 2578 2579 2580 2581 2582 2583 2584 2585 2586 2587 2588 2589 2590 2591 2592 2593 2594 2595 2596 2597 2598 2599 2600 2601 2602 2603 2604 2605 2606 2607 2608 2609 2610 2611 2612 2613 2614 2615 2616 2617 2618 2619 2620 2621 2622 2623 2624 2625 2626 2627 2628 2629 2630 2631 2632 2633 2634 2635 2636 2637 2638 2639 2640 2641 2642 2643 2644 2645 2646 2647 2648 2649 2650 2651 2652 2653 2654 2655 2656 2657 2658 2659 2660 2661 2662 2663 2664 2665 2666 2667 2668 2669 2670 2671 2672 2673 2674 2675 2676 2677 2678 2679 2680 2681 2682 2683 2684 2685 2686 2687 2688 2689 2690 2691 2692 2693 2694 2695 2696 2697 2698 2699 2700 2701 2702 2703 2704 2705 2706 2707 2708 2709 2710 2711 2712 2713 2714 2715 2716 2717 2718 2719 2720 2721 2722 2723 2724 2725 2726 2727 2728 2729 2730 2731 2732 2733 2734 2735 2736 2737 2738 2739 2740 2741 2742 2743 2744 2745 2746 2747 2748 2749 2750 2751 2752 2753 2754 2755 2756 2757 2758 2759 2760 2761 2762 2763 2764 2765 2766 2767 2768 2769 2770 2771 2772 2773 2774 2775 2776 2777 2778 2779 2780 2781 2782 2783 2784 2785 2786 2787 2788 2789 2790 2791 2792 2793 2794 2795 2796 2797 2798 2799 2800 2801 2802 2803 2804 2805 2806 2807 2808 2809 2810 2811 2812 2813 2814 2815 2816 2817 2818 2819 2820 2821 2822 2823 2824 2825 2826 2827 2828 2829 2830 2831 2832 2833 2834 2835 2836 2837 2838 2839 2840 2841 2842 2843 2844 2845 2846 2847 2848 2849 2850 2851 2852 2853 2854 2855 2856 2857 2858 2859 2860 2861 2862 2863 2864 2865 2866 2867 2868 2869 2870 2871 2872 2873 2874 2875 2876 2877 2878 2879 2880 2881 2882 2883 2884 2885 2886 2887 2888 2889 2890 2891 2892 2893 2894 2895 2896 2897 2898 2899 2900 2901 2902 2903 2904 2905 2906 2907 2908 2909 2910 2911 2912 2913 2914 2915 2916 2917 2918 2919 2920 2921 2922 2923 2924 2925 2926 2927 2928 2929 2930 2931 2932 2933 2934 2935 2936 2937 2938 2939 2940 2941 2942 2943 2944 2945 2946 2947 2948 2949 2950 2951 2952 2953 2954 2955 2956 2957 2958 2959 2960 2961 2962 2963 2964 2965 2966 2967 2968 2969 2970 2971 2972 2973 2974 2975 2976 2977 2978 2979 2980 2981 2982 2983 2984 2985 2986 2987 2988 2989 2990 2991 2992 2993 2994 2995 2996 2997 2998 2999 3000 3001 3002 3003 3004 3005 3006 3007 3008 3009 3010 3011 3012 3013 3014 3015 3016 3017 3018 3019 3020 3021 3022 3023 3024 3025 3026 3027 3028 3029 3030 3031 3032 3033 3034 3035 3036 3037 3038 3039 3040 3041 3042 3043 3044 3045 3046 3047 3048 3049 3050 3051 3052 3053 3054 3055 3056 3057 3058 3059 3060 3061 3062 3063 3064 3065 3066 3067 3068 3069 3070 3071 3072 3073 3074 3075 3076 3077 3078 3079 3080 3081 3082 3083 3084 3085 3086 3087 3088 3089 3090 3091 3092 3093 3094 3095 3096 3097 3098 3099 3100 3101 3102 3103 3104 3105 3106 3107 3108 3109 3110 3111 3112 3113 3114 3115 3116 3117 3118 3119 3120 3121 3122 3123 3124 3125 3126 3127 3128 3129 3130 3131 3132 3133 3134 3135 3136 3137 3138 3139 3140 3141 3142 3143 3144 3145 3146 3147 3148 3149 3150 3151 3152 3153 3154 3155 3156 3157 3158 3159 3160 3161 3162 3163 3164 3165 3166 3167 3168 3169 3170 3171 3172 3173 3174 3175 3176 3177 3178 3179 3180 3181 3182 3183 3184 3185 3186 3187 3188 3189 3190 3191 3192 3193 3194 3195 3196 3197 3198 3199 3200 3201 3202 3203 3204 3205 3206 3207 3208 3209 3210 3211 3212 3213 3214 3215 3216 3217 3218 3219 3220 3221 3222 3223 3224 3225 3226 3227 3228 3229 3230 3231 3232 3233 3234 3235 3236 3237 3238 3239 3240 3241 3242 3243 3244 3245 3246 3247 3248 3249 3250 3251 3252 3253 3254 3255 3256 3257 3258 3259 3260 3261 3262 3263 3264 3265 3266 3267 3268 3269 3270 3271 3272 3273 3274 3275 3276 3277 3278 3279 3280 3281 3282 3283 3284 3285 3286 3287 3288 3289 3290 3291 3292 3293 3294 3295 3296 3297 3298 3299 3300 3301 3302 3303 3304 3305 3306 3307 3308 3309 3310 3311 3312 3313 3314 3315 3316 3317 3318 3319 3320 3321 3322 3323 3324 3325 3326 3327 3328 3329 3330 3331 3332 3333 3334 3335 3336 3337 3338 3339 3340 3341 3342 3343 3344 3345 3346 3347 3348 3349 3350 3351 3352 3353 3354 3355 3356 3357 3358 3359 3360 3361 3362 3363 3364 3365 3366 3367 3368 3369 3370 3371 3372 3373 3374 3375 3376 3377 3378 3379 3380 3381 3382 3383 3384 3385 3386 3387 3388 3389 3390 3391 3392 3393 3394 3395 3396 3397 3398 3399 3400 3401 3402 3403 3404 3405 3406 3407 3408 3409 3410 3411 3412 3413 3414 3415 3416 3417 3418 3419 3420 3421 3422 3423 3424 3425 3426 3427 3428 3429 3430 3431 3432 3433 3434 3435 3436 3437 3438 3439 3440 3441 3442 3443 3444 3445 3446 3447 3448 3449 3450 3451 3452 3453 3454 3455 3456 3457 3458 3459 3460 3461 3462 3463 3464 3465 3466 3467 3468 3469 3470 3471 3472 3473 3474 3475 3476 3477 3478 3479 3480 3481 3482 3483 3484 3485 3486 3487 3488 3489 3490 3491 3492 3493 3494 3495 3496 3497 3498 3499 3500 3501 3502 3503 3504 3505 3506 3507 3508 3509 3510 3511 3512 3513 3514 3515 3516 3517 3518 3519 3520 3521 3522 3523 3524 3525 3526 3527 3528 3529 3530 3531 3532 3533 3534 3535 3536 3537 3538 3539 3540 3541 3542 3543 3544 3545 3546 3547 3548 3549 3550 3551 3552 3553 3554 3555 3556 3557 3558 3559 3560 3561 3562 3563 3564 3565 3566 3567 3568 3569 3570 3571 3572 3573 3574 3575 3576 3577 3578 3579 3580 3581 3582 3583 3584 3585 3586 3587 3588 3589 3590 3591 3592 3593 3594 3595 3596 3597 3598 3599 3600 3601 3602 3603 3604 3605 3606 3607 3608 3609 3610 3611 3612 3613 3614 3615 3616 3617 3618 3619 3620 3621 3622 3623 3624 3625 3626 3627 3628 3629 3630 3631 3632 3633 3634 3635 3636 3637 3638 3639 3640 3641 3642 3643 3644 3645 3646 3647 3648 3649 3650 3651 3652 3653 3654 3655 3656 3657 3658 3659 3660 3661 3662 3663 3664 3665 3666 3667 3668 3669 3670 3671 3672 3673 3674 3675 3676 3677 3678 3679 3680 3681 3682 3683 3684 3685 3686 3687 3688 3689 3690 3691 3692 3693 3694 3695 3696 3697 3698 3699 3700 3701 3702 3703 3704 3705 3706 3707 3708 3709 3710 3711 3712 3713 3714 3715 3716 3717 3718 3719 3720 3721 3722 3723 3724 3725 3726 3727 3728 3729 3730 3731 3732 3733 3734 3735 3736 3737 3738 3739 3740 3741 3742 3743 3744 3745 3746 3747 3748 3749 3750 3751 3752 3753 3754 3755 3756 3757 3758 3759 3760 3761 3762 3763 3764 3765 3766 3767 3768 3769 3770 3771 3772 3773 3774 3775 3776 3777 3778 3779 3780 3781 3782 3783 3784 3785 3786 3787 3788 3789 3790 3791 3792 3793 3794 3795 3796 3797 3798 3799 3800 3801 3802 3803 3804 3805 3806 3807 3808 3809 3810 3811 3812 3813 3814 3815 3816 3817 3818 3819 3820 3821 3822 3823 3824 3825 3826 3827 3828 3829 3830 3831 3832 3833 3834 3835 3836 3837 3838 3839 3840 3841 3842 3843 3844 3845 3846 3847 3848 3849 3850 3851 3852 3853 3854 3855 3856 3857 3858 3859 3860 3861 3862 3863 3864 3865 3866 3867 3868 3869 3870 3871 3872 3873 3874 3875 3876 3877 3878 3879 3880 3881 3882 3883 3884 3885 3886 3887 3888 3889 3890 3891 3892 3893 3894 3895 3896 3897 3898 3899 3900 3901 3902 3903 3904 3905 3906 3907 3908 3909 3910 3911 3912 3913 3914 3915 3916 3917 3918 3919 3920 3921 3922 3923 3924 3925 3926 3927 3928 3929 3930 3931 3932 3933 3934 3935 3936 3937 3938 3939 3940 3941 3942 3943 3944 3945 3946 3947 3948 3949 3950 3951 3952 3953 3954 3955 3956 3957 3958 3959 3960 3961 3962 3963 3964 3965 3966 3967 3968 3969 3970 3971 3972 3973 3974 3975 3976 3977 3978 3979 3980 3981 3982 3983 3984 3985 3986 3987 3988 3989 3990 3991 3992 3993 3994 3995 3996 3997 3998 3999 4000 4001 4002 4003 4004 4005 4006 4007 4008 4009 4010 4011 4012 4013 4014 4015 4016 4017 4018 4019 4020 4021 4022 4023 4024 4025 4026 4027 4028 4029 4030 4031 4032 4033 4034 4035 4036 4037 4038 4039 4040 4041 4042 4043 4044 4045 4046 4047 4048 4049 4050 4051 4052 4053 4054 4055 4056 4057 4058 4059 4060 4061 4062 4063 4064 4065 4066 4067 4068 4069 4070 4071 4072 4073 4074 4075 4076 4077 4078 4079 4080 4081 4082 4083 4084 4085 4086 4087 4088 4089 4090 4091 4092 4093 4094 4095 4096 4097 4098 4099 4100 4101 4102 4103 4104 4105 4106 4107 4108 4109 4110 4111 4112 4113 4114 4115 4116 4117 4118 4119 4120 4121 4122 4123 4124 4125 4126 4127 4128 4129 4130 4131 4132 4133 4134 4135 4136 4137 4138 4139 4140 4141 4142 4143 4144 4145 4146 4147 4148 4149 4150 4151 4152 4153 4154 4155 4156 4157 4158 4159 4160 4161 4162 4163 4164 4165 4166 4167 4168 4169 4170 4171 4172 4173 4174 4175 4176 4177 4178 4179 4180 4181 4182 4183 4184 4185 4186 4187 4188 4189 4190 4191 4192 4193 4194 4195 4196 4197 4198 4199 4200 4201 4202 4203 4204 4205 4206 4207 4208 4209 4210 4211 4212 4213 4214 4215 4216 4217 4218 4219 4220 4221 4222 4223 4224 4225 4226 4227 4228 4229 4230 4231 4232 4233 4234 4235 4236 4237 4238 4239 4240 4241 4242 4243 4244 4245 4246 4247 4248 4249 4250 4251 4252 4253 4254 4255 4256 4257 4258 4259 4260 4261 4262 4263 4264 4265 4266 4267 4268 4269 4270 4271 4272 4273 4274 4275 4276 4277 4278 4279 4280 4281 4282 4283 4284 4285 4286 4287 4288 4289 4290 4291 4292 4293 4294 4295 4296 4297 4298 4299 4300 4301 4302 4303 4304 4305 4306 4307 4308 4309 4310 4311 4312 4313 4314 4315 4316 4317 4318 4319 4320 4321 4322 4323 4324 4325 4326 4327 4328 4329 4330 4331 4332 4333 4334 4335 4336 4337 4338 4339 4340 4341 4342 4343 4344 4345 4346 4347 4348 4349 4350 4351 4352 4353 4354 4355 4356 4357 4358 4359 4360 4361 4362 4363 4364 4365 4366 4367 4368 4369 4370 4371 4372 4373 4374 4375 4376 4377 4378 4379 4380 4381 4382 4383 4384 4385 4386 4387 4388 4389 4390 4391 4392 4393 4394 4395 4396 4397 4398 4399 4400 4401 4402 4403 4404 4405 4406 4407 4408 4409 4410 4411 4412 4413 4414 4415 4416 4417 4418 4419 4420 4421 4422 4423 4424 4425 4426 4427 4428 4429 4430 4431 4432 4433 4434 4435 4436 4437 4438 4439 4440 4441 4442 4443 4444 4445 4446 4447 4448 4449 4450 4451 4452 4453 4454 4455 4456 4457 4458 4459 4460 4461 4462 4463 4464 4465 4466 4467 4468 4469 4470 4471 4472 4473 4474 4475 4476 4477 4478 4479 4480 4481 4482 4483 4484 4485 4486 4487 4488 4489 4490 4491 4492 4493 4494 4495 4496 4497 4498 4499 4500 4501 4502 4503 4504 4505 4506 4507 4508 4509 4510 4511 4512 4513 4514 4515 4516 4517 4518 4519 4520 4521 4522 4523 4524 4525 4526 4527 4528 4529 4530 4531 4532 4533 4534 4535 4536 4537 4538 4539 4540 4541 4542 4543 4544 4545 4546 4547 4548 4549 4550 4551 4552 4553 4554 4555 4556 4557 4558 4559 4560 4561 4562 4563 4564 4565 4566 4567 4568 4569 4570 4571 4572 4573 4574 4575 4576 4577 4578 4579 4580 4581 4582 4583 4584 4585 4586 4587 4588 4589 4590 4591 4592 4593 4594 4595 4596 4597 4598 4599 4600 4601 4602 4603 4604 4605 4606 4607 4608 4609 4610 4611 4612 4613 4614 4615 4616 4617 4618 4619 4620 4621 4622 4623 4624 4625 4626 4627 4628 4629 4630 4631 4632 4633 4634 4635 4636 4637 4638 4639 4640 4641 4642 4643 4644 4645 4646 4647 4648 4649 4650 4651 4652 4653 4654 4655 4656 4657 4658 4659 4660 4661 4662 4663 4664 4665 4666 4667 4668 4669 4670 4671 4672 4673 4674 4675 4676 4677 4678 4679 4680 4681 4682 4683 4684 4685 4686 4687 4688 4689 4690 4691 4692 4693 4694 4695 4696 4697 4698 4699 4700 4701 4702 4703 4704 4705 4706 4707 4708 4709 4710 4711 4712 4713 4714 4715 4716 4717 4718 4719 4720 4721 4722 4723 4724 4725 4726 4727 4728 4729 4730 4731 4732 4733 4734 4735 4736 4737 4738 4739 4740 4741 4742 4743 4744 4745 4746 4747 4748 4749 4750 4751 4752 4753 4754 4755 4756 4757 4758 4759 4760 4761 4762 4763 4764 4765 4766 4767 4768 4769 4770 4771 4772 4773 4774 4775 4776 4777 4778 4779 4780 4781 4782 4783 4784 4785 4786 4787 4788 4789 4790 4791 4792 4793 4794 4795 4796 4797 4798 4799 4800 4801 4802 4803 4804 4805 4806 4807 4808 4809 4810 4811 4812 4813 4814 4815 4816 4817 4818 4819 4820 4821 4822 4823 4824 4825 4826 4827 4828 4829 4830 4831 4832 4833 4834 4835 4836 4837 4838 4839 4840 4841 4842 4843 4844 4845 4846 4847 4848 4849 4850 4851 4852 4853 4854 4855 4856 4857 4858 4859 4860 4861 4862 4863 4864 4865 4866 4867 4868 4869 4870 4871 4872 4873 4874 4875 4876 4877 4878 4879 4880 4881 4882 4883 4884 4885 4886 4887 4888 4889 4890 4891 4892 4893 4894 4895 4896 4897 4898 4899 4900 4901 4902 4903 4904 4905 4906 4907 4908 4909 4910 4911 4912 4913 4914 4915 4916 4917 4918 4919 4920 4921 4922 4923 4924 4925 4926 4927 4928 4929 4930 4931 4932 4933 4934 4935 4936 4937 4938 4939 4940 4941 4942 4943 4944 4945 4946 4947 4948 4949 4950 4951 4952 4953 4954 4955 4956 4957 4958 4959 4960 4961 4962 4963 4964 4965 4966 4967 4968 4969 4970 4971 4972 4973 4974 4975 4976 4977 4978 4979 4980 4981 4982 4983 4984 4985 4986 4987 4988 4989 4990 4991 4992 4993 4994 4995 4996 4997 4998 4999 5000 5001 5002 5003 5004 5005 5006 5007 5008 5009 5010 5011 5012 5013 5014 5015 5016 5017 5018 5019 5020 5021 5022 5023 5024 5025 5026 5027 5028 5029 5030 5031 5032 5033 5034 5035 5036 5037 5038 5039 5040 5041 5042 5043 5044 5045 5046 5047 5048 5049 5050 5051 5052 5053 5054 5055 5056 5057 5058 5059 5060 5061 5062 5063 5064 5065 5066 5067 5068 5069 5070 5071 5072 5073 5074 5075 5076 5077 5078 5079 5080 5081 5082 5083 5084 5085 5086 5087 5088 5089 5090 5091 5092 5093 5094 5095 5096 5097 5098 5099 5100 5101 5102 5103 5104 5105 5106 5107 5108 5109 5110 5111 5112 5113 5114 5115 5116 5117 5118 5119 5120 5121 5122 5123 5124 5125 5126 5127 5128 5129 5130 5131 5132 5133 5134 5135 5136 5137 5138 5139 5140 5141 5142 5143 5144 5145 5146 5147 5148 5149 5150 5151 5152 5153 5154 5155 5156 5157 5158 5159 5160 5161 5162 5163 5164 5165 5166 5167 5168 5169 5170 5171 5172 5173 5174 5175 5176 5177 5178 5179 5180 5181 5182 5183 5184 5185 5186 5187 5188 5189 5190 5191 5192 5193 5194 5195 5196 5197 5198 5199 5200 5201 5202 5203 5204 5205 5206 5207 5208 5209 5210 5211 5212 5213 5214 5215 5216 5217 5218 5219 5220 5221 5222 5223 5224 5225 5226 5227 5228 5229 5230 5231 5232 5233 5234 5235 5236 5237 5238 5239 5240 5241 5242 5243 5244 5245 5246 5247 5248 5249 5250 5251 5252 5253 5254 5255 5256 5257 5258 5259 5260 5261 5262 5263 5264 5265 5266 5267 5268 5269 5270 5271 5272 5273 5274 5275 5276 5277 5278 5279 5280 5281 5282 5283 5284 5285 5286 5287 5288 5289 5290 5291 5292 5293 5294 5295 5296 5297 5298 5299 5300 5301 5302 5303 5304 5305 5306 5307 5308 5309 5310 5311 5312 5313 5314 5315 5316 5317 5318 5319 5320 5321 5322 5323 5324 5325 5326 5327 5328 5329 5330 5331 5332 5333 5334 5335 5336 5337 5338 5339 5340 5341 5342 5343 5344 5345 5346 5347 5348 5349 5350 5351 5352 5353 5354 5355 5356 5357 5358 5359 5360 5361 5362 5363 5364 5365 5366 5367 5368 5369 5370 5371 5372 5373 5374 5375 5376 5377 5378 5379 5380 5381 5382 5383 5384 5385 5386 5387 5388 5389 5390 5391 5392 5393 5394 5395 5396 5397 5398 5399 5400 5401 5402 5403 5404 5405 5406 5407 5408 5409 5410 5411 5412 5413 5414 5415 5416 5417 5418 5419 5420 5421 5422 5423 5424 5425 5426 5427 5428 5429 5430 5431 5432 5433 5434 5435 5436 5437 5438 5439 5440 5441 5442 5443 5444 5445 5446 5447 5448 5449 5450 5451 5452 5453 5454 5455 5456 5457 5458 5459 5460 5461 5462 5463 5464 5465 5466 5467 5468 5469 5470 5471 5472 5473 5474 5475 5476 5477 5478 5479 5480 5481 5482 5483 5484 5485 5486 5487 5488 5489 5490 5491 5492 5493 5494 5495 5496 5497 5498 5499 5500 5501 5502 5503 5504 5505 5506 5507 5508 5509 5510 5511 5512 5513 5514 5515 5516 5517 5518 5519 5520 5521 5522 5523 5524 5525 5526 5527 5528 5529 5530 5531 5532 5533 5534 5535 5536 5537 5538 5539 5540 5541 5542 5543 5544 5545 5546 5547 5548 5549 5550 5551 5552 5553 5554 5555 5556 5557 5558 5559 5560 5561 5562 5563 5564 5565 5566 5567 5568 5569 5570 5571 5572 5573 5574 5575 5576 5577 5578 5579 5580 5581 5582 5583 5584 5585 5586 5587 5588 5589 5590 5591 5592 5593 5594 5595 5596 5597 5598 5599 5600 5601 5602 5603 5604 5605 5606 5607 5608 5609 5610 5611 5612 5613 5614 5615 5616 5617 5618 5619 5620 5621 5622 5623 5624 5625 5626 5627 5628 5629 5630 5631 5632 5633 5634 5635 5636 5637 5638 5639 5640 5641 5642 5643 5644 5645 5646 5647 5648 5649 5650 5651 5652 5653 5654 5655 5656 5657 5658 5659 5660 5661 5662 5663 5664 5665 5666 5667 5668 5669 5670 5671 5672 5673 5674 5675 5676 5677 5678 5679 5680 5681 5682 5683 5684 5685 5686 5687 5688 5689 5690 5691 5692 5693 5694 5695 5696 5697 5698 5699 5700 5701 5702 5703 5704 5705 5706 5707 5708 5709 5710 5711 5712 5713 5714 5715 5716 5717 5718 5719 5720 5721 5722 5723 5724 5725 5726 5727 5728 5729 5730 5731 5732 5733 5734 5735 5736 5737 5738 5739 5740 5741 5742 5743 5744 5745 5746 5747 5748 5749 5750 5751 5752 5753 5754 5755 5756 5757 5758 5759 5760 5761 5762 5763 5764 5765 5766 5767 5768 5769 5770 5771 5772 5773 5774 5775 5776 5777 5778 5779 5780 5781 5782 5783 5784 5785 5786 5787 5788 5789 5790 5791 5792 5793 5794 5795 5796 5797 5798 5799 5800 5801 5802 5803 5804 5805 5806 5807 5808 5809 5810 5811 5812 5813 5814 5815 5816 5817 5818 5819 5820 5821 5822 5823 5824 5825 5826 5827 5828 5829 5830 5831 5832 5833 5834 5835 5836 5837 5838 5839 5840 5841 5842 5843 5844 5845 5846 5847 5848 5849 5850 5851 5852 5853 5854 5855 5856 5857 5858 5859 5860 5861 5862 5863 5864 5865 5866 5867 5868 5869 5870 5871 5872 5873 5874 5875 5876 5877 5878 5879 5880 5881 5882 5883 5884 5885 5886 5887 5888 5889 5890 5891 5892 5893 5894 5895 5896 5897 5898 5899 5900 5901 5902 5903 5904 5905 5906 5907 5908 5909 5910 5911 5912 5913 5914 5915 5916 5917 5918 5919 5920 5921 5922 5923 5924 5925 5926 5927 5928 5929 5930 5931 5932 5933 5934 5935 5936 5937 5938 5939 5940 5941 5942 5943 5944 5945 5946 5947 5948 5949 5950 5951 5952 5953 5954 5955 5956 5957 5958 5959 5960 5961 5962 5963 5964 5965 5966 5967 5968 5969 5970 5971 5972 5973 5974 5975 5976 5977 5978 5979 5980 5981 5982 5983 5984 5985 5986 5987 5988 5989 5990 5991 5992 5993 5994 5995 5996 5997 5998 5999 6000 6001 6002 6003 6004 6005 6006 6007 6008 6009 6010 6011 6012 6013 6014 6015 6016 6017 6018 6019 6020 6021 6022 6023 6024 6025 6026 6027 6028 6029 6030 6031 6032 6033 6034 6035 6036 6037 6038 6039 6040 6041 6042 6043 6044 6045 6046 6047 6048 6049 6050 6051 6052 6053 6054 6055 6056 6057 6058 6059 6060 6061 6062 6063 6064 6065 6066 6067 6068 6069 6070 6071 6072 6073 6074 6075 6076 6077 6078 6079 6080 6081 6082 6083 6084 6085 6086 6087 6088 6089 6090 6091 6092 6093 6094 6095 6096 6097 6098 6099 6100 6101 6102 6103 6104 6105 6106 6107 6108 6109 6110 6111 6112 6113 6114 6115 6116 6117 6118 6119 6120 6121 6122 6123 6124 6125 6126 6127 6128 6129 6130 6131 6132 6133 6134 6135 6136 6137 6138 6139 6140 6141 6142 6143 6144 6145 6146 6147 6148 6149 6150 6151 6152 6153 6154 6155 6156 6157 6158 6159 6160 6161 6162 6163 6164 6165 6166 6167 6168 6169 6170 6171 6172 6173 6174 6175 6176 6177 6178 6179 6180 6181 6182 6183 6184 6185 6186 6187 6188 6189 6190 6191 6192 6193 6194 6195 6196 6197 6198 6199 6200 6201 6202 6203 6204 6205 6206 6207 6208 6209 6210 6211 6212 6213 6214 6215 6216 6217 6218 6219 6220 6221 6222 6223 6224 6225 6226 6227 6228 6229 6230 6231 6232 6233 6234 6235 6236 6237 6238 6239 6240 6241 6242 6243 6244 6245 6246 6247 6248 6249 6250 6251 6252 6253 6254 6255 6256 6257 6258 6259 6260 6261 6262 6263 6264 6265 6266 6267 6268 6269 6270 6271 6272 6273 6274 6275 6276 6277 6278 6279 6280 6281 6282 6283 6284 6285 6286 6287 6288 6289 6290 6291 6292 6293 6294 6295 6296 6297 6298 6299 6300 6301 6302 6303 6304 6305 6306 6307 6308 6309 6310 6311 6312 6313 6314 6315 6316 6317 6318 6319 6320 6321 6322 6323 6324 6325 6326 6327 6328 6329 6330 6331 6332 6333 6334 6335 6336 6337 6338 6339 6340 6341 6342 6343 6344 6345 6346 6347 6348 6349 6350 6351 6352 6353 6354 6355 6356 6357 6358 6359 6360 6361 6362 6363 6364 6365 6366 6367 6368 6369 6370 6371 6372 6373 6374 6375 6376 6377 6378 6379 6380 6381 6382 6383 6384 6385 6386 6387 6388 6389 6390 6391 6392 6393 6394 6395 6396 6397 6398 6399 6400 6401 6402 6403 6404 6405 6406 6407 6408 6409 6410 6411 6412 6413 6414 6415 6416 6417 6418 6419 6420 6421 6422 6423 6424 6425 6426 6427 6428 6429 6430 6431 6432 6433 6434 6435 6436 6437 6438 6439 6440 6441 6442 6443 6444 6445 6446 6447 6448 6449 6450 6451 6452 6453 6454 6455 6456 6457 6458 6459 6460 6461 6462 6463 6464 6465 6466 6467 6468 6469 6470 6471 6472 6473 6474 6475 6476 6477 6478 6479 6480 6481 6482 6483 6484 6485 6486 6487 6488 6489 6490 6491 6492 6493 6494 6495 6496 6497 6498 6499 6500 6501 6502 6503 6504 6505 6506 6507 6508 6509 6510 6511 6512 6513 6514 6515 6516 6517 6518 6519 6520 6521 6522 6523 6524 6525 6526 6527 6528 6529 6530 6531 6532 6533 6534 6535 6536 6537 6538 6539 6540 6541 6542 6543 6544 6545 6546 6547 6548 6549 6550 6551 6552 6553 6554 6555 6556 6557 6558 6559 6560 6561 6562 6563 6564 6565 6566 6567 6568 6569 6570 6571 6572 6573 6574 6575 6576 6577 6578 6579 6580 6581 6582 6583 6584 6585 6586 6587 6588 6589 6590 6591 6592 6593 6594 6595 6596 6597 6598 6599 6600 6601 6602 6603 6604 6605 6606 6607 6608 6609 6610 6611 6612 6613 6614 6615 6616 6617 6618 6619 6620 6621 6622 6623 6624 6625 6626 6627 6628 6629 6630 6631 6632 6633 6634 6635 6636 6637 6638 6639 6640 6641 6642 6643 6644 6645 6646 6647 6648 6649 6650 6651 6652 6653 6654 6655 6656 6657 6658 6659 6660 6661 6662 6663 6664 6665 6666 6667 6668 6669 6670 6671 6672 6673 6674 6675 6676 6677 6678 6679 6680 6681 6682 6683 6684 6685 6686 6687 6688 6689 6690 6691 6692 6693 6694 6695 6696 6697 6698 6699 6700 6701 6702 6703 6704 6705 6706 6707 6708 6709 6710 6711 6712 6713 6714 6715 6716 6717 6718 6719 6720 6721 6722 6723 6724 6725 6726 6727 6728 6729 6730 6731 6732 6733 6734 6735 6736 6737 6738 6739 6740 6741 6742 6743 6744 6745 6746 6747 6748 6749 6750 6751 6752 6753 6754 6755 6756 6757 6758 6759 6760 6761 6762 6763 6764 6765 6766 6767 6768 6769 6770 6771 6772 6773 6774 6775 6776 6777 6778 6779 6780 6781 6782 6783 6784 6785 6786 6787 6788 6789 6790 6791 6792 6793 6794 6795 6796 6797 6798 6799 6800 6801 6802 6803 6804 6805 6806 6807 6808 6809 6810 6811 6812 6813 6814 6815 6816 6817 6818 6819 6820 6821 6822 6823 6824 6825 6826 6827 6828 6829 6830 6831 6832 6833 6834 6835 6836 6837 6838 6839 6840 6841 6842 6843 6844 6845 6846 6847 6848 6849 6850 6851 6852 6853 6854 6855 6856 6857 6858 6859 6860 6861 6862 6863 6864 6865 6866 6867 6868 6869 6870 6871 6872 6873 6874 6875 6876 6877 6878 6879 6880 6881 6882 6883 6884 6885 6886 6887 6888 6889 6890 6891 6892 6893 6894 6895 6896 6897 6898 6899 6900 6901 6902 6903 6904 6905 6906 6907 6908 6909 6910 6911 6912 6913 6914 6915 6916 6917 6918 6919 6920 6921 6922 6923 6924 6925 6926 6927 6928 6929 6930 6931 6932 6933 6934 6935 6936 6937 6938 6939 6940 6941 6942 6943 6944 6945 6946 6947 6948 6949 6950 6951 6952 6953 6954 6955 6956 6957 6958 6959 6960 6961 6962 6963 6964 6965 6966 6967 6968 6969 6970 6971 6972 6973 6974 6975 6976 6977 6978 6979 6980 6981 6982 6983 6984 6985 6986 6987 6988 6989 6990 6991 6992 6993 6994 6995 6996 6997 6998 6999 7000 7001 7002 7003 7004 7005 7006 7007 7008 7009 7010 7011 7012 7013 7014 7015 7016 7017 7018 7019 7020 7021 7022 7023 7024 7025 7026 7027 7028 7029 7030 7031 7032 7033 7034 7035 7036 7037 7038 7039 7040 7041 7042 7043 7044 7045 7046 7047 7048 7049 7050 7051 7052 7053 7054 7055 7056 7057 7058 7059 7060 7061 7062 7063 7064 7065 7066 7067 7068 7069 7070 7071 7072 7073 7074 7075 7076 7077 7078 7079 7080 7081 7082 7083 7084 7085 7086 7087 7088 7089 7090 7091 7092 7093 7094 7095 7096 7097 7098 7099 7100 7101 7102 7103 7104 7105 7106 7107 7108 7109 7110 7111 7112 7113 7114 7115 7116 7117 7118 7119 7120 7121 7122 7123 7124 7125 7126 7127 7128 7129 7130 7131 7132 7133 7134 7135 7136 7137 7138 7139 7140 7141 7142 7143 7144 7145 7146 7147 7148 7149 7150 7151 7152 7153 7154 7155 7156 7157 7158 7159 7160 7161 7162 7163 7164 7165 7166 7167 7168 7169 7170 7171 7172 7173 7174 7175 7176 7177 7178 7179 7180 7181 7182 7183 7184 7185 7186 7187 7188 7189 7190 7191 7192 7193 7194 7195 7196 7197 7198 7199 7200 7201 7202 7203 7204 7205 7206 7207 7208 7209 7210 7211 7212 7213 7214 7215 7216 7217 7218 7219 7220 7221 7222 7223 7224 7225 7226 7227 7228 7229 7230 7231 7232 7233 7234 7235 7236 7237 7238 7239 7240 7241 7242 7243 7244 7245 7246 7247 7248 7249 7250 7251 7252 7253 7254 7255 7256 7257 7258 7259 7260 7261 7262 7263 7264 7265 7266 7267 7268 7269 7270 7271 7272 7273 7274 7275 7276 7277 7278 7279 7280 7281 7282 7283 7284 7285 7286 7287 7288 7289 7290 7291 7292 7293 7294 7295 7296 7297 7298 7299 7300 7301 7302 7303 7304 7305 7306 7307 7308 7309 7310 7311 7312 7313 7314 7315 7316 7317 7318 7319 7320 7321 7322 7323 7324 7325 7326 7327 7328 7329 7330 7331 7332 7333 7334 7335 7336 7337 7338 7339 7340 7341 7342 7343 7344 7345 7346 7347 7348 7349 7350 7351 7352 7353 7354 7355 7356 7357 7358 7359 7360 7361 7362 7363 7364 7365 7366 7367 7368 7369 7370 7371 7372 7373 7374 7375 7376 7377 7378 7379 7380 7381 7382 7383 7384 7385 7386 7387 7388 7389 7390 7391 7392 7393 7394 7395 7396 7397 7398 7399 7400 7401 7402 7403 7404 7405 7406 7407 7408 7409 7410 7411 7412 7413 7414 7415 7416 7417 7418 7419 7420 7421 7422 7423 7424 7425 7426 7427 7428 7429 7430 7431 7432 7433 7434 7435 7436 7437 7438 7439 7440 7441 7442 7443 7444 7445 7446 7447 7448 7449 7450 7451 7452 7453 7454 7455 7456 7457 7458 7459 7460 7461 7462 7463 7464 7465 7466 7467 7468 7469 7470 7471 7472 7473 7474 7475 7476 7477 7478 7479 7480 7481 7482 7483 7484 7485 7486 7487 7488 7489 7490 7491 7492 7493 7494 7495 7496 7497 7498 7499 7500 7501 7502 7503 7504 7505 7506 7507 7508 7509 7510 7511 7512 7513 7514 7515 7516 7517 7518 7519 7520 7521 7522 7523 7524 7525 7526 7527 7528 7529 7530 7531 7532 7533 7534 7535 7536 7537 7538 7539 7540 7541 7542 7543 7544 7545 7546 7547 7548 7549 7550 7551 7552 7553 7554 7555 7556 7557 7558 7559 7560 7561 7562 7563 7564 7565 7566 7567 7568 7569 7570 7571 7572 7573 7574 7575 7576 7577 7578 7579 7580 7581 7582 7583 7584 7585 7586 7587 7588 7589 7590 7591 7592 7593 7594 7595 7596 7597 7598 7599 7600 7601 7602 7603 7604 7605 7606 7607 7608 7609 7610 7611 7612 7613 7614 7615 7616 7617 7618 7619 7620 7621 7622 7623 7624 7625 7626 7627 7628 7629 7630 7631 7632 7633 7634 7635 7636 7637 7638 7639 7640 7641 7642 7643 7644 7645 7646 7647 7648 7649 7650 7651 7652 7653 7654 7655 7656 7657 7658 7659 7660 7661 7662 7663 7664 7665 7666 7667 7668 7669 7670 7671 7672 7673 7674 7675 7676 7677 7678 7679 7680 7681 7682 7683 7684 7685 7686 7687 7688 7689 7690 7691 7692 7693 7694 7695 7696 7697 7698 7699 7700 7701 7702 7703 7704 7705 7706 7707 7708 7709 7710 7711 7712 7713 7714 7715 7716 7717 7718 7719 7720 7721 7722 7723 7724 7725 7726 7727 7728 7729 7730 7731 7732 7733 7734 7735 7736 7737 7738 7739 7740 7741 7742 7743 7744 7745 7746 7747 7748 7749 7750 7751 7752 7753 7754 7755 7756 7757 7758 7759 7760 7761 7762 7763 7764 7765 7766 7767 7768 7769 7770 7771 7772 7773 7774 7775 7776 7777 7778 7779 7780 7781 7782 7783 7784 7785 7786 7787 7788 7789 7790 7791 7792 7793 7794 7795 7796 7797 7798 7799 7800 7801 7802 7803 7804 7805 7806 7807 7808 7809 7810 7811 7812 7813 7814 7815 7816 7817 7818 7819 7820 7821 7822 7823 7824 7825 7826 7827 7828 7829 7830 7831 7832 7833 7834 7835 7836 7837 7838 7839 7840 7841 7842 7843 7844 7845 7846 7847 7848 7849 7850 7851 7852 7853 7854 7855 7856 7857 7858 7859 7860 7861 7862 7863 7864 7865 7866 7867 7868 7869 7870 7871 7872 7873 7874 7875 7876 7877 7878 7879 7880 7881 7882 7883 7884 7885 7886 7887 7888 7889 7890 7891 7892 7893 7894 7895 7896 7897 7898 7899 7900 7901 7902 7903 7904 7905 7906 7907 7908 7909 7910 7911 7912 7913 7914 7915 7916 7917 7918 7919 7920 7921 7922 7923 7924 7925 7926 7927 7928 7929 7930 7931 7932 7933 7934 7935 7936 7937 7938 7939 7940 7941 7942 7943 7944 7945 7946 7947 7948 7949 7950 7951 7952 7953 7954 7955 7956 7957 7958 7959 7960 7961 7962 7963 7964 7965 7966 7967 7968 7969 7970 7971 7972 7973 7974 7975 7976 7977 7978 7979 7980 7981 7982 7983 7984 7985 7986 7987 7988 7989 7990 7991 7992 7993 7994 7995 7996 7997 7998 7999 8000 8001 8002 8003 8004 8005 8006 8007 8008 8009 8010 8011 8012 8013 8014 8015 8016 8017 8018 8019 8020 8021 8022 8023 8024 8025 8026 8027 8028 8029 8030 8031 8032 8033 8034 8035 8036 8037 8038 8039 8040 8041 8042 8043 8044 8045 8046 8047 8048 8049 8050 8051 8052 8053 8054 8055 8056 8057 8058 8059 8060 8061 8062 8063 8064 8065 8066 8067 8068 8069 8070 8071 8072 8073 8074 8075 8076 8077 8078 8079 8080 8081 8082 8083 8084 8085 8086 8087 8088 8089 8090 8091 8092 8093 8094 8095 8096 8097 8098 8099 8100 8101 8102 8103 8104 8105 8106 8107 8108 8109 8110 8111 8112 8113 8114 8115 8116 8117 8118 8119 8120 8121 8122 8123 8124 8125 8126 8127 8128 8129 8130 8131 8132 8133 8134 8135 8136 8137 8138 8139 8140 8141 8142 8143 8144 8145 8146 8147 8148 8149 8150 8151 8152 8153 8154 8155 8156 8157 8158 8159 8160 8161 8162 8163 8164 8165 8166 8167 8168 8169 8170 8171 8172 8173 8174 8175 8176 8177 8178 8179 8180 8181 8182 8183 8184 8185 8186 8187 8188 8189 8190 8191 8192 8193 8194 8195 8196 8197 8198 8199 8200 8201 8202 8203 8204 8205 8206 8207 8208 8209 8210 8211 8212 8213 8214 8215 8216 8217 8218 8219 8220 8221 8222 8223 8224 8225 8226 8227 8228 8229 8230 8231 8232 8233 8234 8235 8236 8237 8238 8239 8240 8241 8242 8243 8244 8245 8246 8247 8248 8249 8250 8251 8252 8253 8254 8255 8256 8257 8258 8259 8260 8261 8262 8263 8264 8265 8266 8267 8268 8269 8270 8271 8272 8273 8274 8275 8276 8277 8278 8279 8280 8281 8282 8283 8284 8285 8286 8287 8288 8289 8290 8291 8292 8293 8294 8295 8296 8297 8298 8299 8300 8301 8302 8303 8304 8305 8306 8307 8308 8309 8310 8311 8312 8313 8314 8315 8316 8317 8318 8319 8320 8321 8322 8323 8324 8325 8326 8327 8328 8329 8330 8331 8332 8333 8334 8335 8336 8337 8338 8339 8340 8341 8342 8343 8344 8345 8346 8347 8348 8349 8350 8351 8352 8353 8354 8355 8356 8357 8358 8359 8360 8361 8362 8363 8364 8365 8366 8367 8368 8369 8370 8371 8372 8373 8374 8375 8376 8377 8378 8379 8380 8381 8382 8383 8384 8385 8386 8387 8388 8389 8390 8391 8392 8393 8394 8395 8396 8397 8398 8399 8400 8401 8402 8403 8404 8405 8406 8407 8408 8409 8410 8411 8412 8413 8414 8415 8416 8417 8418 8419 8420 8421 8422 8423 8424 8425 8426 8427 8428 8429 8430 8431 8432 8433 8434 8435 8436 8437 8438 8439 8440 8441 8442 8443 8444 8445 8446 8447 8448 8449 8450 8451 8452 8453 8454 8455 8456 8457 8458 8459 8460 8461 8462 8463 8464 8465 8466 8467 8468 8469 8470 8471 8472 8473 8474 8475 8476 8477 8478 8479 8480 8481 8482 8483 8484 8485 8486 8487 8488 8489 8490 8491 8492 8493 8494 8495 8496 8497 8498 8499 8500 8501 8502 8503 8504 8505 8506 8507 8508 8509 8510 8511 8512 8513 8514 8515 8516 8517 8518 8519 8520 8521 8522 8523 8524 8525 8526 8527 8528 8529 8530 8531 8532 8533 8534 8535 8536 8537 8538 8539 8540 8541 8542 8543 8544 8545 8546 8547 8548 8549 8550 8551 8552 8553 8554 8555 8556 8557 8558 8559 8560 8561 8562 8563 8564 8565 8566 8567 8568 8569 8570 8571 8572 8573 8574 8575 8576 8577 8578 8579 8580 8581 8582 8583 8584 8585 8586 8587 8588 8589 8590 8591 8592 8593 8594 8595 8596 8597 8598 8599 8600 8601 8602 8603 8604 8605 8606 8607 8608 8609 8610 8611 8612 8613 8614 8615 8616 8617 8618 8619 8620 8621 8622 8623 8624 8625 8626 8627 8628 8629 8630 8631 8632 8633 8634 8635 8636 8637 8638 8639 8640 8641 8642 8643 8644 8645 8646 8647 8648 8649 8650 8651 8652 8653 8654 8655 8656 8657 8658 8659 8660 8661 8662 8663 8664 8665 8666 8667 8668 8669 8670 8671 8672 8673 8674 8675 8676 8677 8678 8679 8680 8681 8682 8683 8684 8685 8686 8687 8688 8689 8690 8691 8692 8693 8694 8695 8696 8697 8698 8699 8700 8701 8702 8703 8704 8705 8706 8707 8708 8709 8710 8711 8712 8713 8714 8715 8716 8717 8718 8719 8720 8721 8722 8723 8724 8725 8726 8727 8728 8729 8730 8731 8732 8733 8734 8735 8736 8737 8738 8739 8740 8741 8742 8743 8744 8745 8746 8747 8748 8749 8750 8751 8752 8753 8754 8755 8756 8757 8758 8759 8760 8761 8762 8763 8764 8765 8766 8767 8768 8769 8770 8771 8772 8773 8774 8775 8776 8777 8778 8779 8780 8781 8782 8783 8784 8785 8786 8787 8788 8789 8790 8791 8792 8793 8794 8795 8796 8797 8798 8799 8800 8801 8802 8803 8804 8805 8806 8807 8808 8809 8810 8811 8812 8813 8814 8815 8816 8817 8818 8819 8820 8821 8822 8823 8824 8825 8826 8827 8828 8829 8830 8831 8832 8833 8834 8835 8836 8837 8838 8839 8840 8841 8842 8843 8844 8845 8846 8847 8848 8849 8850 8851 8852 8853 8854 8855 8856 8857 8858 8859 8860 8861 8862 8863 8864 8865 8866 8867 8868 8869 8870 8871 8872 8873 8874 8875 8876 8877 8878 8879 8880 8881 8882 8883 8884 8885 8886 8887 8888 8889 8890 8891 8892 8893 8894 8895 8896 8897 8898 8899 8900 8901 8902 8903 8904 8905 8906 8907 8908 8909 8910 8911 8912 8913 8914 8915 8916 8917 8918 8919 8920 8921 8922 8923 8924 8925 8926 8927 8928 8929 8930 8931 8932 8933 8934 8935 8936 8937 8938 8939 8940 8941 8942 8943 8944 8945 8946 8947 8948 8949 8950 8951 8952 8953 8954 8955 8956 8957 8958 8959 8960 8961 8962 8963 8964 8965 8966 8967 8968 8969 8970 8971 8972 8973 8974 8975 8976 8977 8978 8979 8980 8981 8982 8983 8984 8985 8986 8987 8988 8989 8990 8991 8992 8993 8994 8995 8996 8997 8998 8999 9000 9001 9002 9003 9004 9005 9006 9007 9008 9009 9010 9011 9012 9013 9014 9015 9016 9017 9018 9019 9020 9021 9022 9023 9024 9025 9026 9027 9028 9029 9030 9031 9032 9033 9034 9035 9036 9037 9038 9039 9040 9041 9042 9043 9044 9045 9046 9047 9048 9049 9050 9051 9052 9053 9054 9055 9056 9057 9058 9059 9060 9061 9062 9063 9064 9065 9066 9067 9068 9069 9070 9071 9072 9073 9074 9075 9076 9077 9078 9079 9080 9081 9082 9083 9084 9085 9086 9087 9088 9089 9090 9091 9092 9093 9094 9095 9096 9097 9098 9099 9100 9101 9102 9103 9104 9105 9106 9107 9108 9109 9110 9111 9112 9113 9114 9115 9116 9117 9118 9119 9120 9121 9122 9123 9124 9125 9126 9127 9128 9129 9130 9131 9132 9133 9134 9135 9136 9137 9138 9139 9140 9141 9142 9143 9144 9145 9146 9147 9148 9149 9150 9151 9152 9153 9154 9155 9156 9157 9158 9159 9160 9161 9162 9163 9164 9165 9166 9167 9168 9169 9170 9171 9172 9173 9174 9175 9176 9177 9178 9179 9180 9181 9182 9183 9184 9185 9186 9187 9188 9189 9190 9191 9192 9193 9194 9195 9196 9197 9198 9199 9200 9201 9202 9203 9204 9205 9206 9207 9208 9209 9210 9211 9212 9213 9214 9215 9216 9217 9218 9219 9220 9221 9222 9223 9224 9225 9226 9227 9228 9229 9230 9231 9232 9233 9234 9235 9236 9237 9238 9239 9240 9241 9242 9243 9244 9245 9246 9247 9248 9249 9250 9251 9252 9253 9254 9255 9256 9257 9258 9259 9260 9261 9262 9263 9264 9265 9266 9267 9268 9269 9270 9271 9272 9273 9274 9275 9276 9277 9278 9279 9280 9281 9282 9283 9284 9285 9286 9287 9288 9289 9290 9291 9292 9293 9294 9295 9296 9297 9298 9299 9300 9301 9302 9303 9304 9305 9306 9307 9308 9309 9310 9311 9312 9313 9314 9315 9316 9317 9318 9319 9320 9321 9322 9323 9324 9325 9326 9327 9328 9329 9330 9331 9332 9333 9334 9335 9336 9337 9338 9339 9340 9341 9342 9343 9344 9345 9346 9347 9348 9349 9350 9351 9352 9353 9354 9355 9356 9357 9358 9359 9360 9361 9362 9363 9364 9365 9366 9367 9368 9369 9370 9371 9372 9373 9374 9375 9376 9377 9378 9379 9380 9381 9382 9383 9384 9385 9386 9387 9388 9389 9390 9391 9392 9393 9394 9395 9396 9397 9398 9399 9400 9401 9402 9403 9404 9405 9406 9407 9408 9409 9410 9411 9412 9413 9414 9415 9416 9417 9418 9419 9420 9421 9422 9423 9424 9425 9426 9427 9428 9429 9430 9431 9432 9433 9434 9435 9436 9437 9438 9439 9440 9441 9442 9443 9444 9445 9446 9447 9448 9449 9450 9451 9452 9453 9454 9455 9456 9457 9458 9459 9460 9461 9462 9463 9464 9465 9466 9467 9468 9469 9470 9471 9472 9473 9474 9475 9476 9477 9478 9479 9480 9481 9482 9483 9484 9485 9486 9487 9488 9489 9490 9491 9492 9493 9494 9495 9496 9497 9498 9499 9500 9501 9502 9503 9504 9505 9506 9507 9508 9509 9510 9511 9512 9513 9514 9515 9516 9517 9518 9519 9520 9521 9522 9523 9524 9525 9526 9527 9528 9529 9530 9531 9532 9533 9534 9535 9536 9537 9538 9539 9540 9541 9542 9543 9544 9545 9546 9547 9548 9549 9550 9551 9552 9553 9554 9555 9556 9557 9558 9559 9560 9561 9562 9563 9564 9565 9566 9567 9568 9569 9570 9571 9572 9573 9574 9575 9576 9577 9578 9579 9580 9581 9582 9583 9584 9585 9586 9587 9588 9589 9590 9591 9592 9593 9594 9595 9596 9597 9598 9599 9600 9601 9602 9603 9604 9605 9606 9607 9608 9609 9610 9611 9612 9613 9614 9615 9616 9617 9618 9619 9620 9621 9622 9623 9624 9625 9626 9627 9628 9629 9630 9631 9632 9633 9634 9635 9636 9637 9638 9639 9640 9641 9642 9643 9644 9645 9646 9647 9648 9649 9650 9651 9652 9653 9654 9655 9656 9657 9658 9659 9660 9661 9662 9663 9664 9665 9666 9667 9668 9669 9670 9671 9672 9673 9674 9675 9676 9677 9678 9679 9680 9681 9682 9683 9684 9685 9686 9687 9688 9689 9690 9691 9692 9693 9694 9695 9696 9697 9698 9699 9700 9701 9702 9703 9704 9705 9706 9707 9708 9709 9710 9711 9712 9713 9714 9715 9716 9717 9718 9719 9720 9721 9722 9723 9724 9725 9726 9727 9728 9729 9730 9731 9732 9733 9734 9735 9736 9737 9738 9739 9740 9741 9742 9743 9744 9745 9746 9747 9748 9749 9750 9751 9752 9753 9754 9755 9756 9757 9758 9759 9760 9761 9762 9763 9764 9765 9766 9767 9768 9769 9770 9771 9772 9773 9774 9775 9776 9777 9778 9779 9780 9781 9782 9783 9784 9785 9786 9787 9788 9789 9790 9791 9792 9793 9794 9795 9796 9797 9798 9799 9800 9801 9802 9803 9804 9805 9806 9807 9808 9809 9810 9811 9812 9813 9814 9815 9816 9817 9818 9819 9820 9821 9822 9823 9824 9825 9826 9827 9828 9829 9830 9831 9832 9833 9834 9835 9836 9837 9838 9839 9840 9841 9842 9843 9844 9845 9846 9847 9848 9849 9850 9851 9852 9853 9854 9855 9856 9857 9858 9859 9860 9861 9862 9863 9864 9865 9866 9867 9868 9869 9870 9871 9872 9873 9874 9875 9876 9877 9878 9879 9880 9881 9882 9883 9884 9885 9886 9887 9888 9889 9890 9891 9892 9893 9894 9895 9896 9897 9898 9899 9900 9901 9902 9903 9904 9905 9906 9907 9908 9909 9910 9911 9912 9913 9914 9915 9916 9917 9918 9919 9920 9921 9922 9923 9924 9925 9926 9927 9928 9929 9930 9931 9932 9933 9934 9935 9936 9937 9938 9939 9940 9941 9942 9943 9944 9945 9946 9947 9948 9949 9950 9951 9952 9953 9954 9955 9956 9957 9958 9959 9960 9961 9962 9963 9964 9965 9966 9967 9968 9969 9970 9971 9972 9973 9974 9975 9976 9977 9978 9979 9980 9981 9982 9983 9984 9985 9986 9987 9988 9989 9990 9991 9992 9993 9994 9995 9996 9997 9998 9999 10000 10001 10002 10003 10004 10005 10006 10007 10008 10009 10010 10011 10012 10013 10014 10015 10016 10017 10018 10019 10020 10021 10022 10023 10024 10025 10026 10027 10028 10029 10030 10031 10032 10033 10034 10035 10036 10037 10038 10039 10040 10041 10042 10043 10044 10045 10046 10047 10048 10049 10050 10051 10052 10053 10054 10055 10056 10057 10058 10059 10060 10061 10062 10063 10064 10065 10066 10067 10068 10069 10070 10071 10072 10073 10074 10075 10076 10077 10078 10079 10080 10081 10082 10083 10084 10085 10086 10087 10088 10089 10090 10091 10092 10093 10094 10095 10096 10097 10098 10099 10100 10101 10102 10103 10104 10105 10106 10107 10108 10109 10110 10111 10112 10113 10114 10115 10116 10117 10118 10119 10120 10121 10122 10123 10124 10125 10126 10127 10128 10129 10130 10131 10132 10133 10134 10135 10136 10137 10138 10139 10140 10141 10142 10143 10144 10145 10146 10147 10148 10149 10150 10151 10152 10153 10154 10155 10156 10157 10158 10159 10160 10161 10162 10163 10164 10165 10166 10167 10168 10169 10170 10171 10172 10173 10174 10175 10176 10177 10178 10179 10180 10181 10182 10183 10184 10185 10186 10187 10188 10189 10190 10191 10192 10193 10194 10195 10196 10197 10198 10199 10200 10201 10202 10203 10204 10205 10206 10207 10208 10209 10210 10211 10212 10213 10214 10215 10216 10217 10218 10219 10220 10221 10222 10223 10224 10225 10226 10227 10228 10229 10230 10231 10232 10233 10234 10235 10236 10237 10238 10239 10240 10241 10242 10243 10244 10245 10246 10247 10248 10249 10250 10251 10252 10253 10254 10255 10256 10257 10258 10259 10260 10261 10262 10263 10264 10265 10266 10267 10268 10269 10270 10271 10272 10273 10274 10275 10276 10277 10278 10279 10280 10281 10282 10283 10284 10285 10286 10287 10288 10289 10290 10291 10292 10293 10294 10295 10296 10297 10298 10299 10300 10301 10302 10303 10304 10305 10306 10307 10308 10309 10310 10311 10312 10313 10314 10315 10316 10317 10318 10319 10320 10321 10322 10323 10324 10325 10326 10327 10328 10329 10330 10331 10332 10333 10334 10335 10336 10337 10338 10339 10340 10341 10342 10343 10344 10345 10346 10347 10348 10349 10350 10351 10352 10353 10354 10355 10356 10357 10358 10359 10360 10361 10362 10363 10364 10365 10366 10367 10368 10369 10370 10371 10372 10373 10374 10375 10376 10377 10378 10379 10380 10381 10382 10383 10384 10385 10386 10387 10388 10389 10390 10391 10392 10393 10394 10395 10396 10397 10398 10399 10400 10401 10402 10403 10404 10405 10406 10407 10408 10409 10410 10411 10412 10413 10414 10415 10416 10417 10418 10419 10420 10421 10422 10423 10424 10425 10426 10427 10428 10429 10430 10431 10432 10433 10434 10435 10436 10437 10438 10439 10440 10441 10442 10443 10444 10445 10446 10447 10448 10449 10450 10451 10452 10453 10454 10455 10456 10457 10458 10459 10460 10461 10462 10463 10464 10465 10466 10467 10468 10469 10470 10471 10472 10473 10474 10475 10476 10477 10478 10479 10480 10481 10482 10483 10484 10485 10486 10487 10488 10489 10490 10491 10492 10493 10494 10495 10496 10497 10498 10499 10500 10501 10502 10503 10504 10505 10506 10507 10508 10509 10510 10511 10512 10513 10514 10515 10516 10517 10518 10519 10520 10521 10522 10523 10524 10525 10526 10527 10528 10529 10530 10531 10532 10533 10534 10535 10536 10537 10538 10539 10540 10541 10542 10543 10544 10545 10546 10547 10548 10549 10550 10551 10552 10553 10554 10555 10556 10557 10558 10559 10560 10561 10562 10563 10564 10565 10566 10567 10568 10569 10570 10571 10572 10573 10574 10575 10576 10577 10578 10579 10580 10581 10582 10583 10584 10585 10586 10587 10588 10589 10590 10591 10592 10593 10594 10595 10596 10597 10598 10599 10600 10601 10602 10603 10604 10605 10606 10607 10608 10609 10610 10611 10612 10613 10614 10615 10616 10617 10618 10619 10620 10621 10622 10623 10624 10625 10626 10627 10628 10629 10630 10631 10632 10633 10634 10635 10636 10637 10638 10639 10640 10641 10642 10643 10644 10645 10646 10647 10648 10649 10650 10651 10652 10653 10654 10655 10656 10657 10658 10659 10660 10661 10662 10663 10664 10665 10666 10667 10668 10669 10670 10671 10672 10673 10674 10675 10676 10677 10678 10679 10680 10681 10682 10683 10684 10685 10686 10687 10688 10689 10690 10691 10692 10693 10694 10695 10696 10697 10698 10699 10700 10701 10702 10703 10704 10705 10706 10707 10708 10709 10710 10711 10712 10713 10714 10715 10716 10717 10718 10719 10720 10721 10722 10723 10724 10725 10726 10727 10728 10729 10730 10731 10732 10733 10734 10735 10736 10737 10738 10739 10740 10741 10742 10743 10744 10745 10746 10747 10748 10749 10750 10751 10752 10753 10754 10755 10756 10757 10758 10759 10760 10761 10762 10763 10764 10765 10766 10767 10768 10769 10770 10771 10772 10773 10774 10775 10776 10777 10778 10779 10780 10781 10782 10783 10784 10785 10786 10787 10788 10789 10790 10791 10792 10793 10794 10795 10796 10797 10798 10799 10800 10801 10802 10803 10804 10805 10806 10807 10808 10809 10810 10811 10812 10813 10814 10815 10816 10817 10818 10819 10820 10821 10822 10823 10824 10825 10826 10827 10828 10829 10830 10831 10832 10833 10834 10835 10836 10837 10838 10839 10840 10841 10842 10843 10844 10845 10846 10847 10848 10849 10850 10851 10852 10853 10854 10855 10856 10857 10858 10859 10860 10861 10862 10863 10864 10865 10866 10867 10868 10869 10870 10871 10872 10873 10874 10875 10876 10877 10878 10879 10880 10881 10882 10883 10884 10885 10886 10887 10888 10889 10890 10891 10892 10893 10894 10895 10896 10897 10898 10899 10900 10901 10902 10903 10904 10905 10906 10907 10908 10909 10910 10911 10912 10913 10914 10915 10916 10917 10918 10919 10920 10921 10922 10923 10924 10925 10926 10927 10928 10929 10930 10931 10932 10933 10934 10935 10936 10937 10938 10939 10940 10941 10942 10943 10944 10945 10946 10947 10948 10949 10950 10951 10952 10953 10954 10955 10956 10957 10958 10959 10960 10961 10962 10963 10964 10965 10966 10967 10968 10969 10970 10971 10972 10973 10974 10975 10976 10977 10978 10979 10980 10981 10982 10983 10984 10985 10986 10987 10988 10989 10990 10991 10992 10993 10994 10995 10996 10997 10998 10999 11000 11001 11002 11003 11004 11005 11006 11007 11008 11009 11010 11011 11012 11013 11014 11015 11016 11017 11018 11019 11020 11021 11022 11023 11024 11025 11026 11027 11028 11029 11030 11031 11032 11033 11034 11035 11036 11037 11038 11039 11040 11041 11042 11043 11044 11045 11046 11047 11048 11049 11050 11051 11052 11053 11054 11055 11056 11057 11058 11059 11060 11061 11062 11063 11064 11065 11066 11067 11068 11069 11070 11071 11072 11073 11074 11075 11076 11077 11078 11079 11080 11081 11082 11083 11084 11085 11086 11087 11088 11089 11090 11091 11092 11093 11094 11095 11096 11097 11098 11099 11100 11101 11102 11103 11104 11105 11106 11107 11108 11109 11110 11111 11112 11113 11114 11115 11116 11117 11118 11119 11120 11121 11122 11123 11124 11125 11126 11127 11128 11129 11130 11131 11132 11133 11134 11135 11136 11137 11138 11139 11140 11141 11142 11143 11144 11145 11146 11147 11148 11149 11150 11151 11152 11153 11154 11155 11156 11157 11158 11159 11160 11161 11162 11163 11164 11165 11166 11167 11168 11169 11170 11171 11172 11173 11174 11175 11176 11177 11178 11179 11180 11181 11182 11183 11184 11185 11186 11187 11188 11189 11190 11191 11192 11193 11194 11195 11196 11197 11198 11199 11200 11201 11202 11203 11204 11205 11206 11207 11208 11209 11210 11211 11212 11213 11214 11215 11216 11217 11218 11219 11220 11221 11222 11223 11224 11225 11226 11227 11228 11229 11230 11231 11232 11233 11234 11235 11236 11237 11238 11239 11240 11241 11242 11243 11244 11245 11246 11247 11248 11249 11250 11251 11252 11253 11254 11255 11256 11257 11258 11259 11260 11261 11262 11263 11264 11265 11266 11267 11268 11269 11270 11271 11272 11273 11274 11275 11276 11277 11278 11279 11280 11281 11282 11283 11284 11285 11286 11287 11288 11289 11290 11291 11292 11293 11294 11295 11296 11297 11298 11299 11300 11301 11302 11303 11304 11305 11306 11307 11308 11309 11310 11311 11312 11313 11314 11315 11316 11317 11318 11319 11320 11321 11322 11323 11324 11325 11326 11327 11328 11329 11330 11331 11332 11333 11334 11335 11336 11337 11338 11339 11340 11341 11342 11343 11344 11345 11346 11347 11348 11349 11350 11351 11352 11353 11354 11355 11356 11357 11358 11359 11360 11361 11362 11363 11364 11365 11366 11367 11368 11369 11370 11371 11372 11373 11374 11375 11376 11377 11378 11379 11380 11381 11382 11383 11384 11385 11386 11387 11388 11389 11390 11391 11392 11393 11394 11395 11396 11397 11398 11399 11400 11401 11402 11403 11404 11405 11406 11407 11408 11409 11410 11411 11412 11413 11414 11415 11416 11417 11418 11419 11420 11421 11422 11423 11424 11425 11426 11427 11428 11429 11430 11431 11432 11433 11434 11435 11436 11437 11438 11439 11440 11441 11442 11443 11444 11445 11446 11447 11448 11449 11450 11451 11452 11453 11454 11455 11456 11457 11458 11459 11460 11461 11462 11463 11464 11465 11466 11467 11468 11469 11470 11471 11472 11473 11474 11475 11476 11477 11478 11479 11480 11481 11482 11483 11484 11485 11486 11487 11488 11489 11490 11491 11492 11493 11494 11495 11496 11497 11498 11499 11500 11501 11502 11503 11504 11505 11506 11507 11508 11509 11510 11511 11512 11513 11514 11515 11516 11517 11518 11519 11520 11521 11522 11523 11524 11525 11526 11527 11528 11529 11530 11531 11532 11533 11534 11535 11536 11537 11538 11539 11540 11541 11542 11543 11544 11545 11546 11547 11548 11549 11550 11551 11552 11553 11554 11555 11556 11557 11558 11559 11560 11561 11562 11563 11564 11565 11566 11567 11568 11569 11570 11571 11572 11573 11574 11575 11576 11577 11578 11579 11580 11581 11582 11583 11584 11585 11586 11587 11588 11589 11590 11591 11592 11593 11594 11595 11596 11597 11598 11599 11600 11601 11602 11603 11604 11605 11606 11607 11608 11609 11610 11611 11612 11613 11614 11615 11616 11617 11618 11619 11620 11621 11622 11623 11624 11625 11626 11627 11628 11629 11630 11631 11632 11633 11634 11635 11636 11637 11638 11639 11640 11641 11642 11643 11644 11645 11646 11647 11648 11649 11650 11651 11652 11653 11654 11655 11656 11657 11658 11659 11660 11661 11662 11663 11664 11665 11666 11667 11668 11669 11670 11671 11672 11673 11674 11675 11676 11677 11678 11679 11680 11681 11682 11683 11684 11685 11686 11687 11688 11689 11690 11691 11692 11693 11694 11695 11696 11697 11698 11699 11700 11701 11702 11703 11704 11705 11706 11707 11708 11709 11710 11711 11712 11713 11714 11715 11716 11717 11718 11719 11720 11721 11722 11723 11724 11725 11726 11727 11728 11729 11730 11731 11732 11733 11734 11735 11736 11737 11738 11739 11740 11741 11742 11743 11744 11745 11746 11747 11748 11749 11750 11751 11752 11753 11754 11755 11756 11757 11758 11759 11760 11761 11762 11763 11764 11765 11766 11767 11768 11769 11770 11771 11772 11773 11774 11775 11776 11777 11778 11779 11780 11781 11782 11783 11784 11785 11786 11787 11788 11789 11790 11791 11792 11793 11794 11795 11796 11797 11798 11799 11800 11801 11802 11803 11804 11805 11806 11807 11808 11809 11810 11811 11812 11813 11814 11815 11816 11817 11818 11819 11820 11821 11822 11823 11824 11825 11826 11827 11828 11829 11830 11831 11832 11833 11834 11835 11836 11837 11838 11839 11840 11841 11842 11843 11844 11845 11846 11847 11848 11849 11850 11851 11852 11853 11854 11855 11856 11857 11858 11859 11860 11861 11862 11863 11864 11865 11866 11867 11868 11869 11870 11871 11872 11873 11874 11875 11876 11877 11878 11879 11880 11881 11882 11883 11884 11885 11886 11887 11888 11889 11890 11891 11892 11893 11894 11895 11896 11897 11898 11899 11900 11901 11902 11903 11904 11905 11906 11907 11908 11909 11910 11911 11912 11913 11914 11915 11916 11917 11918 11919 11920 11921 11922 11923 11924 11925 11926 11927 11928 11929 11930 11931 11932 11933 11934 11935 11936 11937 11938 11939 11940 11941 11942 11943 11944 11945 11946 11947 11948 11949 11950 11951 11952 11953 11954 11955 11956 11957 11958 11959 11960 11961 11962 11963 11964 11965 11966 11967 11968 11969 11970 11971 11972 11973 11974 11975 11976 11977 11978 11979 11980 11981 11982 11983 11984 11985 11986 11987 11988 11989 11990 11991 11992 11993 11994 11995 11996 11997 11998 11999 12000 12001 12002 12003 12004 12005 12006 12007 12008 12009 12010 12011 12012 12013 12014 12015 12016 12017 12018 12019 12020 12021 12022 12023 12024 12025 12026 12027 12028 12029 12030 12031 12032 12033 12034 12035 12036 12037 12038 12039 12040 12041 12042 12043 12044 12045 12046 12047 12048 12049 12050 12051 12052 12053 12054 12055 12056 12057 12058 12059 12060 12061 12062 12063 12064 12065 12066 12067 12068 12069 12070 12071 12072 12073 12074 12075 12076 12077 12078 12079 12080 12081 12082 12083 12084 12085 12086 12087 12088 12089 12090 12091 12092 12093 12094 12095 12096 12097 12098 12099 12100 12101 12102 12103 12104 12105 12106 12107 12108 12109 12110 12111 12112 12113 12114 12115 12116 12117 12118 12119 12120 12121 12122 12123 12124 12125 12126 12127 12128 12129 12130 12131 12132 12133 12134 12135 12136 12137 12138 12139 12140 12141 12142 12143 12144 12145 12146 12147 12148 12149 12150 12151 12152 12153 12154 12155 12156 12157 12158 12159 12160 12161 12162 12163 12164 12165 12166 12167 12168 12169 12170 12171 12172 12173 12174 12175 12176 12177 12178 12179 12180 12181 12182 12183 12184 12185 12186 12187 12188 12189 12190 12191 12192 12193 12194 12195 12196 12197 12198 12199 12200 12201 12202 12203 12204 12205 12206 12207 12208 12209 12210 12211 12212 12213 12214 12215 12216 12217 12218 12219 12220 12221 12222 12223 12224 12225 12226 12227 12228 12229 12230 12231 12232 12233 12234 12235 12236 12237 12238 12239 12240 12241 12242 12243 12244 12245 12246 12247 12248 12249 12250 12251 12252 12253 12254 12255 12256 12257 12258 12259 12260 12261 12262 12263 12264 12265 12266 12267 12268 12269 12270 12271 12272 12273 12274 12275 12276 12277 12278 12279 12280 12281 12282 12283 12284 12285 12286 12287 12288 12289 12290 12291 12292 12293 12294 12295 12296 12297 12298 12299 12300 12301 12302 12303 12304 12305 12306 12307 12308 12309 12310 12311 12312 12313 12314 12315 12316 12317 12318 12319 12320 12321 12322 12323 12324 12325 12326 12327 12328 12329 12330 12331 12332 12333 12334 12335 12336 12337 12338 12339 12340 12341 12342 12343 12344 12345 12346 12347 12348 12349 12350 12351 12352 12353 12354 12355 12356 12357 12358 12359 12360 12361 12362 12363 12364 12365 12366 12367 12368 12369 12370 12371 12372 12373 12374 12375 12376 12377 12378 12379 12380 12381 12382 12383 12384 12385 12386 12387 12388 12389 12390 12391 12392 12393 12394 12395 12396 12397 12398 12399 12400 12401 12402 12403 12404 12405 12406 12407 12408 12409 12410 12411 12412 12413 12414 12415 12416 12417 12418 12419 12420 12421 12422 12423 12424 12425 12426 12427 12428 12429 12430 12431 12432 12433 12434 12435 12436 12437 12438 12439 12440 12441 12442 12443 12444 12445 12446 12447 12448 12449 12450 12451 12452 12453 12454 12455 12456 12457 12458 12459 12460 12461 12462 12463 12464 12465 12466 12467 12468 12469 12470 12471 12472 12473 12474 12475 12476 12477 12478 12479 12480 12481 12482 12483 12484 12485 12486 12487 12488 12489 12490 12491 12492 12493 12494 12495 12496 12497 12498 12499 12500 12501 12502 12503 12504 12505 12506 12507 12508 12509 12510 12511 12512 12513 12514 12515 12516 12517 12518 12519 12520 12521 12522 12523 12524 12525 12526 12527 12528 12529 12530 12531 12532 12533 12534 12535 12536 12537 12538 12539 12540 12541 12542 12543 12544 12545 12546 12547 12548 12549 12550 12551 12552 12553 12554 12555 12556 12557 12558 12559 12560 12561 12562 12563 12564 12565 12566 12567 12568 12569 12570 12571 12572 12573 12574 12575 12576 12577 12578 12579 12580 12581 12582 12583 12584 12585 12586 12587 12588 12589 12590 12591 12592 12593 12594 12595 12596 12597 12598 12599 12600 12601 12602 12603 12604 12605 12606 12607 12608 12609 12610 12611 12612 12613 12614 12615 12616 12617 12618 12619 12620 12621 12622 12623 12624 12625 12626 12627 12628 12629 12630 12631 12632 12633 12634 12635 12636 12637 12638 12639 12640 12641 12642 12643 12644 12645 12646 12647 12648 12649 12650 12651 12652 12653 12654 12655 12656 12657 12658 12659 12660 12661 12662 12663 12664 12665 12666 12667 12668 12669 12670 12671 12672 12673 12674 12675 12676 12677 12678 12679 12680 12681 12682 12683 12684 12685 12686 12687 12688 12689 12690 12691 12692 12693 12694 12695 12696 12697 12698 12699 12700 12701 12702 12703 12704 12705 12706 12707 12708 12709 12710 12711 12712 12713 12714 12715 12716 12717 12718 12719 12720 12721 12722 12723 12724 12725 12726 12727 12728 12729 12730 12731 12732 12733 12734 12735 12736 12737 12738 12739 12740 12741 12742 12743 12744 12745 12746 12747 12748 12749 12750 12751 12752 12753 12754 12755 12756 12757 12758 12759 12760 12761 12762 12763 12764 12765 12766 12767 12768 12769 12770 12771 12772 12773 12774 12775 12776 12777 12778 12779 12780 12781 12782 12783 12784 12785 12786 12787 12788 12789 12790 12791 12792 12793 12794 12795 12796 12797 12798 12799 12800 12801 12802 12803 12804 12805 12806 12807 12808 12809 12810 12811 12812 12813 12814 12815 12816 12817 12818 12819 12820 12821 12822 12823 12824 12825 12826 12827 12828 12829 12830 12831 12832 12833 12834 12835 12836 12837 12838 12839 12840 12841 12842 12843 12844 12845 12846 12847 12848 12849 12850 12851 12852 12853 12854 12855 12856 12857 12858 12859 12860 12861 12862 12863 12864 12865 12866 12867 12868 12869 12870 12871 12872 12873 12874 12875 12876 12877 12878 12879 12880 12881 12882 12883 12884 12885 12886 12887 12888 12889 12890 12891 12892 12893 12894 12895 12896 12897 12898 12899 12900 12901 12902 12903 12904 12905 12906 12907 12908 12909 12910 12911 12912 12913 12914 12915 12916 12917 12918 12919 12920 12921 12922 12923 12924 12925 12926 12927 12928 12929 12930 12931 12932 12933 12934 12935 12936 12937 12938 12939 12940 12941 12942 12943 12944 12945 12946 12947 12948 12949 12950 12951 12952 12953 12954 12955 12956 12957 12958 12959 12960 12961 12962 12963 12964 12965 12966 12967 12968 12969 12970 12971 12972 12973 12974 12975 12976 12977 12978 12979 12980 12981 12982 12983 12984 12985 12986 12987 12988 12989 12990 12991 12992 12993 12994 12995 12996 12997 12998 12999 13000 13001 13002 13003 13004 13005 13006 13007 13008 13009 13010 13011 13012 13013 13014 13015 13016 13017 13018 13019 13020 13021 13022 13023 13024 13025 13026 13027 13028 13029 13030 13031 13032 13033 13034 13035 13036 13037 13038 13039 13040 13041 13042 13043 13044 13045 13046 13047 13048 13049 13050 13051 13052 13053 13054 13055 13056 13057 13058 13059 13060 13061 13062 13063 13064 13065 13066 13067 13068 13069 13070 13071 13072 13073 13074 13075 13076 13077 13078 13079 13080 13081 13082 13083 13084 13085 13086 13087 13088 13089 13090 13091 13092 13093 13094 13095 13096 13097 13098 13099 13100 13101 13102 13103 13104 13105 13106 13107 13108 13109 13110 13111 13112 13113 13114 13115 13116 13117 13118 13119 13120 13121 13122 13123 13124 13125 13126 13127 13128 13129 13130 13131 13132 13133 13134 13135 13136 13137 13138 13139 13140 13141 13142 13143 13144 13145 13146 13147 13148 13149 13150 13151 13152 13153 13154 13155 13156 13157 13158 13159 13160 13161 13162 13163 13164 13165 13166 13167 13168 13169 13170 13171 13172 13173 13174 13175 13176 13177 13178 13179 13180 13181 13182 13183 13184 13185 13186 13187 13188 13189 13190 13191 13192 13193 13194 13195 13196 13197 13198 13199 13200 13201 13202 13203 13204 13205 13206 13207 13208 13209 13210 13211 13212 13213 13214 13215 13216 13217 13218 13219 13220 13221 13222 13223 13224 13225 13226 13227 13228 13229 13230 13231 13232 13233 13234 13235 13236 13237 13238 13239 13240 13241 13242 13243 13244 13245 13246 13247 13248 13249 13250 13251 13252 13253 13254 13255 13256 13257 13258 13259 13260 13261 13262 13263 13264 13265 13266 13267 13268 13269 13270 13271 13272 13273 13274 13275 13276 13277 13278 13279 13280 13281 13282 13283 13284 13285 13286 13287 13288 13289 13290 13291 13292 13293 13294 13295 13296 13297 13298 13299 13300 13301 13302 13303 13304 13305 13306 13307 13308 13309 13310 13311 13312 13313 13314 13315 13316 13317 13318 13319 13320 13321 13322 13323 13324 13325 13326 13327 13328 13329 13330 13331 13332 13333 13334 13335 13336 13337 13338 13339 13340 13341 13342 13343 13344 13345 13346 13347 13348 13349 13350 13351 13352 13353 13354 13355 13356 13357 13358 13359 13360 13361 13362 13363 13364 13365 13366 13367 13368 13369 13370 13371 13372 13373 13374 13375 13376 13377 13378 13379 13380 13381 13382 13383 13384 13385 13386 13387 13388 13389 13390 13391 13392 13393 13394 13395 13396 13397 13398 13399 13400 13401 13402 13403 13404 13405 13406 13407 13408 13409 13410 13411 13412 13413 13414 13415 13416 13417 13418 13419 13420 13421 13422 13423 13424 13425 13426 13427 13428 13429 13430 13431 13432 13433 13434 13435 13436 13437 13438 13439 13440 13441 13442 13443 13444 13445 13446 13447 13448 13449 13450 13451 13452 13453 13454 13455 13456 13457 13458 13459 13460 13461 13462 13463 13464 13465 13466 13467 13468 13469 13470 13471 13472 13473 13474 13475 13476 13477 13478 13479 13480 13481 13482 13483 13484 13485 13486 13487 13488 13489 13490 13491 13492 13493 13494 13495 13496 13497 13498 13499 13500 13501 13502 13503 13504 13505 13506 13507 13508 13509 13510 13511 13512 13513 13514 13515 13516 13517 13518 13519 13520 13521 13522 13523 13524 13525 13526 13527 13528 13529 13530 13531 13532 13533 13534 13535 13536 13537 13538 13539 13540 13541 13542 13543 13544 13545 13546 13547 13548 13549 13550 13551 13552 13553 13554 13555 13556 13557 13558 13559 13560 13561 13562 13563 13564 13565 13566 13567 13568 13569 13570 13571 13572 13573 13574 13575 13576 13577 13578 13579 13580 13581 13582 13583 13584 13585 13586 13587 13588 13589 13590 13591 13592 13593 13594 13595 13596 13597 13598 13599 13600 13601 13602 13603 13604 13605 13606 13607 13608 13609 13610 13611 13612 13613 13614 13615 13616 13617 13618 13619 13620 13621 13622 13623 13624 13625 13626 13627 13628 13629 13630 13631 13632 13633 13634 13635 13636 13637 13638 13639 13640 13641 13642 13643 13644 13645 13646 13647 13648 13649 13650 13651 13652 13653 13654 13655 13656 13657 13658 13659 13660 13661 13662 13663 13664 13665 13666 13667 13668 13669 13670 13671 13672 13673 13674 13675 13676 13677 13678 13679 13680 13681 13682 13683 13684 13685 13686 13687 13688 13689 13690 13691 13692 13693 13694 13695 13696 13697 13698 13699 13700 13701 13702 13703 13704 13705 13706 13707 13708 13709 13710 13711 13712 13713 13714 13715 13716 13717 13718 13719 13720 13721 13722 13723 13724 13725 13726 13727 13728 13729 13730 13731 13732 13733 13734 13735 13736 13737 13738 13739 13740 13741 13742 13743 13744 13745 13746 13747 13748 13749 13750 13751 13752 13753 13754 13755 13756 13757 13758 13759 13760 13761 13762 13763 13764 13765 13766 13767 13768 13769 13770 13771 13772 13773 13774 13775 13776 13777 13778 13779 13780 13781 13782 13783 13784 13785 13786 13787 13788 13789 13790 13791 13792 13793 13794 13795 13796 13797 13798 13799 13800 13801 13802 13803 13804 13805 13806 13807 13808 13809 13810 13811 13812 13813 13814 13815 13816 13817 13818 13819 13820 13821 13822 13823 13824 13825 13826 13827 13828 13829 13830 13831 13832 13833 13834 13835 13836 13837 13838 13839 13840 13841 13842 13843 13844 13845 13846 13847 13848 13849 13850 13851 13852 13853 13854 13855 13856 13857 13858 13859 13860 13861 13862 13863 13864 13865 13866 13867 13868 13869 13870 13871 13872 13873 13874 13875 13876 13877 13878 13879 13880 13881 13882 13883 13884 13885 13886 13887 13888 13889 13890 13891 13892 13893 13894 13895 13896 13897 13898 13899 13900 13901 13902 13903 13904 13905 13906 13907 13908 13909 13910 13911 13912 13913 13914 13915 13916 13917 13918 13919 13920 13921 13922 13923 13924 13925 13926 13927 13928 13929 13930 13931 13932 13933 13934 13935 13936 13937 13938 13939 13940 13941 13942 13943 13944 13945 13946 13947 13948 13949 13950 13951 13952 13953 13954 13955 13956 13957 13958 13959 13960 13961 13962 13963 13964 13965 13966 13967 13968 13969 13970 13971 13972 13973 13974 13975 13976 13977 13978 13979 13980 13981 13982 13983 13984 13985 13986 13987 13988 13989 13990 13991 13992 13993 13994 13995 13996 13997 13998 13999 14000 14001 14002 14003 14004 14005 14006 14007 14008 14009 14010 14011 14012 14013 14014 14015 14016 14017 14018 14019 14020 14021 14022 14023 14024 14025 14026 14027 14028 14029 14030 14031 14032 14033 14034 14035 14036 14037 14038 14039 14040 14041 14042 14043 14044 14045 14046 14047 14048 14049 14050 14051 14052 14053 14054 14055 14056 14057 14058 14059 14060 14061 14062 14063 14064 14065 14066 14067 14068 14069 14070 14071 14072 14073 14074 14075 14076 14077 14078 14079 14080 14081 14082 14083 14084 14085 14086 14087 14088 14089 14090 14091 14092 14093 14094 14095 14096 14097 14098 14099 14100 14101 14102 14103 14104 14105 14106 14107 14108 14109 14110 14111 14112 14113 14114 14115 14116 14117 14118 14119 14120 14121 14122 14123 14124 14125 14126 14127 14128 14129 14130 14131 14132 14133 14134 14135 14136 14137 14138 14139 14140 14141 14142 14143 14144 14145 14146 14147 14148 14149 14150 14151 14152 14153 14154 14155 14156 14157 14158 14159 14160 14161 14162 14163 14164 14165 14166 14167 14168 14169 14170 14171 14172 14173 14174 14175 14176 14177 14178 14179 14180 14181 14182 14183 14184 14185 14186 14187 14188 14189 14190 14191 14192 14193 14194 14195 14196 14197 14198 14199 14200 14201 14202 14203 14204 14205 14206 14207 14208 14209 14210 14211 14212 14213 14214 14215 14216 14217 14218 14219 14220 14221 14222 14223 14224 14225 14226 14227 14228 14229 14230 14231 14232 14233 14234 14235 14236 14237 14238 14239 14240 14241 14242 14243 14244 14245 14246 14247 14248 14249 14250 14251 14252 14253 14254 14255 14256 14257 14258 14259 14260 14261 14262 14263 14264 14265 14266 14267 14268 14269 14270 14271 14272 14273 14274 14275 14276 14277 14278 14279 14280 14281 14282 14283 14284 14285 14286 14287 14288 14289 14290 14291 14292 14293 14294 14295 14296 14297 14298 14299 14300 14301 14302 14303 14304 14305 14306 14307 14308 14309 14310 14311 14312 14313 14314 14315 14316 14317 14318 14319 14320 14321 14322 14323 14324 14325 14326 14327 14328 14329 14330 14331 14332 14333 14334 14335 14336 14337 14338 14339 14340 14341 14342 14343 14344 14345 14346 14347 14348 14349 14350 14351 14352 14353 14354 14355 14356 14357 14358 14359 14360 14361 14362 14363 14364 14365 14366 14367 14368 14369 14370 14371 14372 14373 14374 14375 14376 14377 14378 14379 14380 14381 14382 14383 14384 14385 14386 14387 14388 14389 14390 14391 14392 14393 14394 14395 14396 14397 14398 14399 14400 14401 14402 14403 14404 14405 14406 14407 14408 14409 14410 14411 14412 14413 14414 14415 14416 14417 14418 14419 14420 14421 14422 14423 14424 14425 14426 14427 14428 14429 14430 14431 14432 14433 14434 14435 14436 14437 14438 14439 14440 14441 14442 14443 14444 14445 14446 14447 14448 14449 14450 14451 14452 14453 14454 14455 14456 14457 14458 14459 14460 14461 14462 14463 14464 14465 14466 14467 14468 14469 14470 14471 14472 14473 14474 14475 14476 14477 14478 14479 14480 14481 14482 14483 14484 14485 14486 14487 14488 14489 14490 14491 14492 14493 14494 14495 14496 14497 14498 14499 14500 14501 14502 14503 14504 14505 14506 14507 14508 14509 14510 14511 14512 14513 14514 14515 14516 14517 14518 14519 14520 14521 14522 14523 14524 14525 14526 14527 14528 14529 14530 14531 14532 14533 14534 14535 14536 14537 14538 14539 14540 14541 14542 14543 14544 14545 14546 14547 14548 14549 14550 14551 14552 14553 14554 14555 14556 14557 14558 14559 14560 14561 14562 14563 14564 14565 14566 14567 14568 14569 14570 14571 14572 14573 14574 14575 14576 14577 14578 14579 14580 14581 14582 14583 14584 14585 14586 14587 14588 14589 14590 14591 14592 14593 14594 14595 14596 14597 14598 14599 14600 14601 14602 14603 14604 14605 14606 14607 14608 14609 14610 14611 14612 14613 14614 14615 14616 14617 14618 14619 14620 14621 14622 14623 14624 14625 14626 14627 14628 14629 14630 14631 14632 14633 14634 14635 14636 14637 14638 14639 14640 14641 14642 14643 14644 14645 14646 14647 14648 14649 14650 14651 14652 14653 14654 14655 14656 14657 14658 14659 14660 14661 14662 14663 14664 14665 14666 14667 14668 14669 14670 14671 14672 14673 14674 14675 14676 14677 14678 14679 14680 14681 14682 14683 14684 14685 14686 14687 14688 14689 14690 14691 14692 14693 14694 14695 14696 14697 14698 14699 14700 14701 14702 14703 14704 14705 14706 14707 14708 14709 14710 14711 14712 14713 14714 14715 14716 14717 14718 14719 14720 14721 14722 14723 14724 14725 14726 14727 14728 14729 14730 14731 14732 14733 14734 14735 14736 14737 14738 14739 14740 14741 14742 14743 14744 14745 14746 14747 14748 14749 14750 14751 14752 14753 14754 14755 14756 14757 14758 14759 14760 14761 14762 14763 14764 14765 14766 14767 14768 14769 14770 14771 14772 14773 14774 14775 14776 14777 14778 14779 14780 14781 14782 14783 14784 14785 14786 14787 14788 14789 14790 14791 14792 14793 14794 14795 14796 14797 14798 14799 14800 14801 14802 14803 14804 14805 14806 14807 14808 14809 14810 14811 14812 14813 14814 14815 14816 14817 14818 14819 14820 14821 14822 14823 14824 14825 14826 14827 14828 14829 14830 14831 14832 14833 14834 14835 14836 14837 14838 14839 14840 14841 14842 14843 14844 14845 14846 14847 14848 14849 14850 14851 14852 14853 14854 14855 14856 14857 14858 14859 14860 14861 14862 14863 14864 14865 14866 14867 14868 14869 14870 14871 14872 14873 14874 14875 14876 14877 14878 14879 14880 14881 14882 14883 14884 14885 14886 14887 14888 14889 14890 14891 14892 14893 14894 14895 14896 14897 14898 14899 14900 14901 14902 14903 14904 14905 14906 14907 14908 14909 14910 14911 14912 14913 14914 14915 14916 14917 14918 14919 14920 14921 14922 14923 14924 14925 14926 14927 14928 14929 14930 14931 14932 14933 14934 14935 14936 14937 14938 14939 14940 14941 14942 14943 14944 14945 14946 14947 14948 14949 14950 14951 14952 14953 14954 14955 14956 14957 14958 14959 14960 14961 14962 14963 14964 14965 14966 14967 14968 14969 14970 14971 14972 14973 14974 14975 14976 14977 14978 14979 14980 14981 14982 14983 14984 14985 14986 14987 14988 14989 14990 14991 14992 14993 14994 14995 14996 14997 14998 14999 15000 15001 15002 15003 15004 15005 15006 15007 15008 15009 15010 15011 15012 15013 15014 15015 15016 15017 15018 15019 15020 15021 15022 15023 15024 15025 15026 15027 15028 15029 15030 15031 15032 15033 15034 15035 15036 15037 15038 15039 15040 15041 15042 15043 15044 15045 15046 15047 15048 15049 15050 15051 15052 15053 15054 15055 15056 15057 15058 15059 15060 15061 15062 15063 15064 15065 15066 15067 15068 15069 15070 15071 15072 15073 15074 15075 15076 15077 15078 15079 15080 15081 15082 15083 15084 15085 15086 15087 15088 15089 15090 15091 15092 15093 15094 15095 15096 15097 15098 15099 15100 15101 15102 15103 15104 15105 15106 15107 15108 15109 15110 15111 15112 15113 15114 15115 15116 15117 15118 15119 15120 15121 15122 15123 15124 15125 15126 15127 15128 15129 15130 15131 15132 15133 15134 15135 15136 15137 15138 15139 15140 15141 15142 15143 15144 15145 15146 15147 15148 15149 15150 15151 15152 15153 15154 15155 15156 15157 15158 15159 15160 15161 15162 15163 15164 15165 15166 15167 15168 15169 15170 15171 15172 15173 15174 15175 15176 15177 15178 15179 15180 15181 15182 15183 15184 15185 15186 15187 15188 15189 15190 15191 15192 15193 15194 15195 15196 15197 15198 15199 15200 15201 15202 15203 15204 15205 15206 15207 15208 15209 15210 15211 15212 15213 15214 15215 15216 15217 15218 15219 15220 15221 15222 15223 15224 15225 15226 15227 15228 15229 15230 15231 15232 15233 15234 15235 15236 15237 15238 15239 15240 15241 15242 15243 15244 15245 15246 15247 15248 15249 15250 15251 15252 15253 15254 15255 15256 15257 15258 15259 15260 15261 15262 15263 15264 15265 15266 15267 15268 15269 15270 15271 15272 15273 15274 15275 15276 15277 15278 15279 15280 15281 15282 15283 15284 15285 15286 15287 15288 15289 15290 15291 15292 15293 15294 15295 15296 15297 15298 15299 15300 15301 15302 15303 15304 15305 15306 15307 15308 15309 15310 15311 15312 15313 15314 15315 15316 15317 15318
|
#include "stdafx.h"
#include <math.h>
#include "alglibinternal.h"
#include "alglibmisc.h"
#include "linalg.h"
#include "solvers.h"
#include "optimization.h"
#include "diffequations.h"
#include "specialfunctions.h"
#include "integration.h"
#include "statistics.h"
#include "interpolation.h"
#include "fasttransforms.h"
#include "dataanalysis.h"
using namespace alglib;
bool doc_test_bool(bool v, bool t)
{ return (v && t) || (!v && !t); }
bool doc_test_int(ae_int_t v, ae_int_t t)
{ return v==t; }
bool doc_test_real(double v, double t, double _threshold)
{
double s = _threshold>=0 ? 1.0 : fabs(t);
double threshold = fabs(_threshold);
return fabs(v-t)/s<=threshold;
}
bool doc_test_complex(alglib::complex v, alglib::complex t, double _threshold)
{
double s = _threshold>=0 ? 1.0 : alglib::abscomplex(t);
double threshold = fabs(_threshold);
return abscomplex(v-t)/s<=threshold;
}
bool doc_test_bool_vector(const boolean_1d_array &v, const boolean_1d_array &t)
{
ae_int_t i;
if( v.length()!=t.length() )
return false;
for(i=0; i<v.length(); i++)
if( v(i)!=t(i) )
return false;
return true;
}
bool doc_test_bool_matrix(const boolean_2d_array &v, const boolean_2d_array &t)
{
ae_int_t i, j;
if( v.rows()!=t.rows() )
return false;
if( v.cols()!=t.cols() )
return false;
for(i=0; i<v.rows(); i++)
for(j=0; j<v.cols(); j++)
if( v(i,j)!=t(i,j) )
return false;
return true;
}
bool doc_test_int_vector(const integer_1d_array &v, const integer_1d_array &t)
{
ae_int_t i;
if( v.length()!=t.length() )
return false;
for(i=0; i<v.length(); i++)
if( v(i)!=t(i) )
return false;
return true;
}
bool doc_test_int_matrix(const integer_2d_array &v, const integer_2d_array &t)
{
ae_int_t i, j;
if( v.rows()!=t.rows() )
return false;
if( v.cols()!=t.cols() )
return false;
for(i=0; i<v.rows(); i++)
for(j=0; j<v.cols(); j++)
if( v(i,j)!=t(i,j) )
return false;
return true;
}
bool doc_test_real_vector(const real_1d_array &v, const real_1d_array &t, double _threshold)
{
ae_int_t i;
if( v.length()!=t.length() )
return false;
for(i=0; i<v.length(); i++)
{
double s = _threshold>=0 ? 1.0 : fabs(t(i));
double threshold = fabs(_threshold);
if( fabs(v(i)-t(i))/s>threshold )
return false;
}
return true;
}
bool doc_test_real_matrix(const real_2d_array &v, const real_2d_array &t, double _threshold)
{
ae_int_t i, j;
if( v.rows()!=t.rows() )
return false;
if( v.cols()!=t.cols() )
return false;
for(i=0; i<v.rows(); i++)
for(j=0; j<v.cols(); j++)
{
double s = _threshold>=0 ? 1.0 : fabs(t(i,j));
double threshold = fabs(_threshold);
if( fabs(v(i,j)-t(i,j))/s>threshold )
return false;
}
return true;
}
bool doc_test_complex_vector(const complex_1d_array &v, const complex_1d_array &t, double _threshold)
{
ae_int_t i;
if( v.length()!=t.length() )
return false;
for(i=0; i<v.length(); i++)
{
double s = _threshold>=0 ? 1.0 : alglib::abscomplex(t(i));
double threshold = fabs(_threshold);
if( abscomplex(v(i)-t(i))/s>threshold )
return false;
}
return true;
}
bool doc_test_complex_matrix(const complex_2d_array &v, const complex_2d_array &t, double _threshold)
{
ae_int_t i, j;
if( v.rows()!=t.rows() )
return false;
if( v.cols()!=t.cols() )
return false;
for(i=0; i<v.rows(); i++)
for(j=0; j<v.cols(); j++)
{
double s = _threshold>=0 ? 1.0 : alglib::abscomplex(t(i,j));
double threshold = fabs(_threshold);
if( abscomplex(v(i,j)-t(i,j))/s>threshold )
return false;
}
return true;
}
template<class T>
void spoil_vector_by_adding_element(T &x)
{
ae_int_t i;
T y = x;
x.setlength(y.length()+1);
for(i=0; i<y.length(); i++)
x(i) = y(i);
x(y.length()) = 0;
}
template<class T>
void spoil_vector_by_deleting_element(T &x)
{
ae_int_t i;
T y = x;
x.setlength(y.length()-1);
for(i=0; i<y.length()-1; i++)
x(i) = y(i);
}
template<class T>
void spoil_matrix_by_adding_row(T &x)
{
ae_int_t i, j;
T y = x;
x.setlength(y.rows()+1, y.cols());
for(i=0; i<y.rows(); i++)
for(j=0; j<y.cols(); j++)
x(i,j) = y(i,j);
for(j=0; j<y.cols(); j++)
x(y.rows(),j) = 0;
}
template<class T>
void spoil_matrix_by_deleting_row(T &x)
{
ae_int_t i, j;
T y = x;
x.setlength(y.rows()-1, y.cols());
for(i=0; i<y.rows()-1; i++)
for(j=0; j<y.cols(); j++)
x(i,j) = y(i,j);
}
template<class T>
void spoil_matrix_by_adding_col(T &x)
{
ae_int_t i, j;
T y = x;
x.setlength(y.rows(), y.cols()+1);
for(i=0; i<y.rows(); i++)
for(j=0; j<y.cols(); j++)
x(i,j) = y(i,j);
for(i=0; i<y.rows(); i++)
x(i,y.cols()) = 0;
}
template<class T>
void spoil_matrix_by_deleting_col(T &x)
{
ae_int_t i, j;
T y = x;
x.setlength(y.rows(), y.cols()-1);
for(i=0; i<y.rows(); i++)
for(j=0; j<y.cols()-1; j++)
x(i,j) = y(i,j);
}
template<class T>
void spoil_vector_by_nan(T &x)
{
if( x.length()!=0 )
x(randominteger(x.length())) = fp_nan;
}
template<class T>
void spoil_vector_by_posinf(T &x)
{
if( x.length()!=0 )
x(randominteger(x.length())) = fp_posinf;
}
template<class T>
void spoil_vector_by_neginf(T &x)
{
if( x.length()!=0 )
x(randominteger(x.length())) = fp_neginf;
}
template<class T>
void spoil_matrix_by_nan(T &x)
{
if( x.rows()!=0 && x.cols()!=0 )
x(randominteger(x.rows()),randominteger(x.cols())) = fp_nan;
}
template<class T>
void spoil_matrix_by_posinf(T &x)
{
if( x.rows()!=0 && x.cols()!=0 )
x(randominteger(x.rows()),randominteger(x.cols())) = fp_posinf;
}
template<class T>
void spoil_matrix_by_neginf(T &x)
{
if( x.rows()!=0 && x.cols()!=0 )
x(randominteger(x.rows()),randominteger(x.cols())) = fp_neginf;
}
void function1_func(const real_1d_array &x, double &func, void *ptr)
{
//
// this callback calculates f(x0,x1) = 100*(x0+3)^4 + (x1-3)^4
//
func = 100*pow(x[0]+3,4) + pow(x[1]-3,4);
}
void function1_grad(const real_1d_array &x, double &func, real_1d_array &grad, void *ptr)
{
//
// this callback calculates f(x0,x1) = 100*(x0+3)^4 + (x1-3)^4
// and its derivatives df/d0 and df/dx1
//
func = 100*pow(x[0]+3,4) + pow(x[1]-3,4);
grad[0] = 400*pow(x[0]+3,3);
grad[1] = 4*pow(x[1]-3,3);
}
void function1_hess(const real_1d_array &x, double &func, real_1d_array &grad, real_2d_array &hess, void *ptr)
{
//
// this callback calculates f(x0,x1) = 100*(x0+3)^4 + (x1-3)^4
// its derivatives df/d0 and df/dx1
// and its Hessian.
//
func = 100*pow(x[0]+3,4) + pow(x[1]-3,4);
grad[0] = 400*pow(x[0]+3,3);
grad[1] = 4*pow(x[1]-3,3);
hess[0][0] = 1200*pow(x[0]+3,2);
hess[0][1] = 0;
hess[1][0] = 0;
hess[1][1] = 12*pow(x[1]-3,2);
}
void function1_fvec(const real_1d_array &x, real_1d_array &fi, void *ptr)
{
//
// this callback calculates
// f0(x0,x1) = 100*(x0+3)^4,
// f1(x0,x1) = (x1-3)^4
//
fi[0] = 10*pow(x[0]+3,2);
fi[1] = pow(x[1]-3,2);
}
void function1_jac(const real_1d_array &x, real_1d_array &fi, real_2d_array &jac, void *ptr)
{
//
// this callback calculates
// f0(x0,x1) = 100*(x0+3)^4,
// f1(x0,x1) = (x1-3)^4
// and Jacobian matrix J = [dfi/dxj]
//
fi[0] = 10*pow(x[0]+3,2);
fi[1] = pow(x[1]-3,2);
jac[0][0] = 20*(x[0]+3);
jac[0][1] = 0;
jac[1][0] = 0;
jac[1][1] = 2*(x[1]-3);
}
void function2_func(const real_1d_array &x, double &func, void *ptr)
{
//
// this callback calculates f(x0,x1) = (x0^2+1)^2 + (x1-1)^2
//
func = pow(x[0]*x[0]+1,2) + pow(x[1]-1,2);
}
void function2_grad(const real_1d_array &x, double &func, real_1d_array &grad, void *ptr)
{
//
// this callback calculates f(x0,x1) = (x0^2+1)^2 + (x1-1)^2
// and its derivatives df/d0 and df/dx1
//
func = pow(x[0]*x[0]+1,2) + pow(x[1]-1,2);
grad[0] = 4*(x[0]*x[0]+1)*x[0];
grad[1] = 2*(x[1]-1);
}
void function2_hess(const real_1d_array &x, double &func, real_1d_array &grad, real_2d_array &hess, void *ptr)
{
//
// this callback calculates f(x0,x1) = (x0^2+1)^2 + (x1-1)^2
// its gradient and Hessian
//
func = pow(x[0]*x[0]+1,2) + pow(x[1]-1,2);
grad[0] = 4*(x[0]*x[0]+1)*x[0];
grad[1] = 2*(x[1]-1);
hess[0][0] = 12*x[0]*x[0]+4;
hess[0][1] = 0;
hess[1][0] = 0;
hess[1][1] = 2;
}
void function2_fvec(const real_1d_array &x, real_1d_array &fi, void *ptr)
{
//
// this callback calculates
// f0(x0,x1) = x0^2+1
// f1(x0,x1) = x1-1
//
fi[0] = x[0]*x[0]+1;
fi[1] = x[1]-1;
}
void function2_jac(const real_1d_array &x, real_1d_array &fi, real_2d_array &jac, void *ptr)
{
//
// this callback calculates
// f0(x0,x1) = x0^2+1
// f1(x0,x1) = x1-1
// and Jacobian matrix J = [dfi/dxj]
//
fi[0] = x[0]*x[0]+1;
fi[1] = x[1]-1;
jac[0][0] = 2*x[0];
jac[0][1] = 0;
jac[1][0] = 0;
jac[1][1] = 1;
}
void nlcfunc1_jac(const real_1d_array &x, real_1d_array &fi, real_2d_array &jac, void *ptr)
{
//
// this callback calculates
//
// f0(x0,x1) = -x0+x1
// f1(x0,x1) = x0^2+x1^2-1
//
// and Jacobian matrix J = [dfi/dxj]
//
fi[0] = -x[0]+x[1];
fi[1] = x[0]*x[0] + x[1]*x[1] - 1.0;
jac[0][0] = -1.0;
jac[0][1] = +1.0;
jac[1][0] = 2*x[0];
jac[1][1] = 2*x[1];
}
void nlcfunc2_jac(const real_1d_array &x, real_1d_array &fi, real_2d_array &jac, void *ptr)
{
//
// this callback calculates
//
// f0(x0,x1,x2) = x0+x1
// f1(x0,x1,x2) = x2-exp(x0)
// f2(x0,x1,x2) = x0^2+x1^2-1
//
// and Jacobian matrix J = [dfi/dxj]
//
fi[0] = x[0]+x[1];
fi[1] = x[2]-exp(x[0]);
fi[2] = x[0]*x[0] + x[1]*x[1] - 1.0;
jac[0][0] = 1.0;
jac[0][1] = 1.0;
jac[0][2] = 0.0;
jac[1][0] = -exp(x[0]);
jac[1][1] = 0.0;
jac[1][2] = 1.0;
jac[2][0] = 2*x[0];
jac[2][1] = 2*x[1];
jac[2][2] = 0.0;
}
void nlcfunc2_sjac(const real_1d_array &x, real_1d_array &fi, sparsematrix &sjac, void *ptr)
{
//
// this callback calculates
//
// f0(x0,x1,x2) = x0+x1
// f1(x0,x1,x2) = x2-exp(x0)
// f2(x0,x1,x2) = x0^2+x1^2-1
//
// and Jacobian matrix J = [dfi/dxj].
//
// This callback returns Jacobian as a sparse CRS-based matrix. This format is intended
// for large-scale problems, it allows to solve otherwise intractable tasks with hundreds
// of thousands of variables. It will also work for our toy problem with just three variables,
// though.
//
//
// First, we calculate function vector fi[].
//
fi[0] = x[0]+x[1];
fi[1] = x[2]-exp(x[0]);
fi[2] = x[0]*x[0] + x[1]*x[1] - 1.0;
//
// After that we initialize sparse Jacobian. On entry to this function sjac is a sparse
// CRS matrix in a special initial state with N columns but no rows (such matrices can
// be created with the sparsecreatecrsempty() function ).
//
// Such matrices can be used only for sequential addition of rows and nonzero elements.
// You should add all rows that are expected (one for an objective and one per each
// nonlinear constraint). Insufficient or excessive rows will be treated as an error.
// Row elements must be added from left to right, i.e. column indexes must monotonically
// increase.
//
// NOTE: you should NOT reinitialize sjac with sparsecreate() or any other function. It
// is important that you append rows/cols to the matrix, but do not create a new
// instance of the matrix object. Doing so may cause hard-to-detect errors in
// the present or future ALGLIB versions.
//
sparseappendemptyrow(sjac);
sparseappendelement(sjac, 0, 1.0);
sparseappendelement(sjac, 1, 1.0);
sparseappendemptyrow(sjac);
sparseappendelement(sjac, 0, -exp(x[0]));
sparseappendelement(sjac, 2, 1.0);
sparseappendemptyrow(sjac);
sparseappendelement(sjac, 0, 2.0*x[0]);
sparseappendelement(sjac, 1, 2.0*x[1]);
}
void nlcfunc2_fvec(const real_1d_array &x, real_1d_array &fi, void *ptr)
{
//
// this callback calculates
//
// f0(x0,x1,x2) = x0+x1
// f1(x0,x1,x2) = x2-exp(x0)
// f2(x0,x1,x2) = x0^2+x1^2-1
//
fi[0] = x[0]+x[1];
fi[1] = x[2]-exp(x[0]);
fi[2] = x[0]*x[0] + x[1]*x[1] - 1.0;
}
void nsfunc1_jac(const real_1d_array &x, real_1d_array &fi, real_2d_array &jac, void *ptr)
{
//
// this callback calculates
//
// f0(x0,x1) = 2*|x0|+x1
//
// and Jacobian matrix J = [df0/dx0 df0/dx1]
//
fi[0] = 2*fabs(double(x[0]))+fabs(double(x[1]));
jac[0][0] = 2*alglib::sign(x[0]);
jac[0][1] = alglib::sign(x[1]);
}
void nsfunc1_fvec(const real_1d_array &x, real_1d_array &fi, void *ptr)
{
//
// this callback calculates
//
// f0(x0,x1) = 2*|x0|+x1
//
fi[0] = 2*fabs(double(x[0]))+fabs(double(x[1]));
}
void nsfunc2_jac(const real_1d_array &x, real_1d_array &fi, real_2d_array &jac, void *ptr)
{
//
// this callback calculates function vector
//
// f0(x0,x1) = 2*|x0|+x1
// f1(x0,x1) = x0-1
// f2(x0,x1) = -x1-1
//
// and Jacobian matrix J
//
// [ df0/dx0 df0/dx1 ]
// J = [ df1/dx0 df1/dx1 ]
// [ df2/dx0 df2/dx1 ]
//
fi[0] = 2*fabs(double(x[0]))+fabs(double(x[1]));
jac[0][0] = 2*alglib::sign(x[0]);
jac[0][1] = alglib::sign(x[1]);
fi[1] = x[0]-1;
jac[1][0] = 1;
jac[1][1] = 0;
fi[2] = -x[1]-1;
jac[2][0] = 0;
jac[2][1] = -1;
}
void bad_func(const real_1d_array &x, double &func, void *ptr)
{
//
// this callback calculates 'bad' function,
// i.e. function with incorrectly calculated derivatives
//
func = 100*pow(x[0]+3,4) + pow(x[1]-3,4);
}
void bad_grad(const real_1d_array &x, double &func, real_1d_array &grad, void *ptr)
{
//
// this callback calculates 'bad' function,
// i.e. function with incorrectly calculated derivatives
//
func = 100*pow(x[0]+3,4) + pow(x[1]-3,4);
grad[0] = 40*pow(x[0]+3,3);
grad[1] = 40*pow(x[1]-3,3);
}
void bad_hess(const real_1d_array &x, double &func, real_1d_array &grad, real_2d_array &hess, void *ptr)
{
//
// this callback calculates 'bad' function,
// i.e. function with incorrectly calculated derivatives
//
func = 100*pow(x[0]+3,4) + pow(x[1]-3,4);
grad[0] = 40*pow(x[0]+3,3);
grad[1] = 40*pow(x[1]-3,3);
hess[0][0] = 120*pow(x[0]+3,2);
hess[0][1] = 0;
hess[1][0] = 0;
hess[1][1] = 120*pow(x[1]-3,2);
}
void bad_fvec(const real_1d_array &x, real_1d_array &fi, void *ptr)
{
//
// this callback calculates 'bad' function,
// i.e. function with incorrectly calculated derivatives
//
fi[0] = 10*pow(x[0]+3,2);
fi[1] = pow(x[1]-3,2);
}
void bad_jac(const real_1d_array &x, real_1d_array &fi, real_2d_array &jac, void *ptr)
{
//
// this callback calculates 'bad' function,
// i.e. function with incorrectly calculated derivatives
//
fi[0] = 10*pow(x[0]+3,2);
fi[1] = pow(x[1]-3,2);
jac[0][0] = 2*(x[0]+3);
jac[0][1] = 1;
jac[1][0] = 0;
jac[1][1] = 20*(x[1]-3);
}
void function_cx_1_func(const real_1d_array &c, const real_1d_array &x, double &func, void *ptr)
{
// this callback calculates f(c,x)=exp(-c0*sqr(x0))
// where x is a position on X-axis and c is adjustable parameter
func = exp(-c[0]*pow(x[0],2));
}
void function_cx_1_grad(const real_1d_array &c, const real_1d_array &x, double &func, real_1d_array &grad, void *ptr)
{
// this callback calculates f(c,x)=exp(-c0*sqr(x0)) and gradient G={df/dc[i]}
// where x is a position on X-axis and c is adjustable parameter.
// IMPORTANT: gradient is calculated with respect to C, not to X
func = exp(-c[0]*pow(x[0],2));
grad[0] = -pow(x[0],2)*func;
}
void function_cx_1_hess(const real_1d_array &c, const real_1d_array &x, double &func, real_1d_array &grad, real_2d_array &hess, void *ptr)
{
// this callback calculates f(c,x)=exp(-c0*sqr(x0)), gradient G={df/dc[i]} and Hessian H={d2f/(dc[i]*dc[j])}
// where x is a position on X-axis and c is adjustable parameter.
// IMPORTANT: gradient/Hessian are calculated with respect to C, not to X
func = exp(-c[0]*pow(x[0],2));
grad[0] = -pow(x[0],2)*func;
hess[0][0] = pow(x[0],4)*func;
}
void ode_function_1_diff(const real_1d_array &y, double x, real_1d_array &dy, void *ptr)
{
// this callback calculates f(y[],x)=-y[0]
dy[0] = -y[0];
}
void int_function_1_func(double x, double xminusa, double bminusx, double &y, void *ptr)
{
// this callback calculates f(x)=exp(x)
y = exp(x);
}
void function_debt_func(const real_1d_array &c, const real_1d_array &x, double &func, void *ptr)
{
//
// this callback calculates f(c,x)=c[0]*(1+c[1]*(pow(x[0]-1999,c[2])-1))
//
func = c[0]*(1+c[1]*(pow(x[0]-1999,c[2])-1));
}
void s1_grad(const real_1d_array &x, double &func, real_1d_array &grad, void *ptr)
{
//
// this callback calculates f(x) = (1+x)^(-0.2) + (1-x)^(-0.3) + 1000*x and its gradient.
//
// function is trimmed when we calculate it near the singular points or outside of the [-1,+1].
// Note that we do NOT calculate gradient in this case.
//
if( (x[0]<=-0.999999999999) || (x[0]>=+0.999999999999) )
{
func = 1.0E+300;
return;
}
func = pow(1+x[0],-0.2) + pow(1-x[0],-0.3) + 1000*x[0];
grad[0] = -0.2*pow(1+x[0],-1.2) +0.3*pow(1-x[0],-1.3) + 1000;
}
void multiobjective2_fvec(const real_1d_array &x, real_1d_array &fi, void *ptr)
{
//
// this callback calculates the bi-objective target
//
// f0(x0,x1) = x0^2 + (x1-1)^2
// f1(x0,x1) = (x0-1(^2 + x1^2
//
fi[0] = x[0]*x[0]+(x[1]-1)*(x[1]-1);
fi[1] = (x[0]-1)*(x[0]-1)+x[1]*x[1];
}
void multiobjective2_jac(const real_1d_array &x, real_1d_array &fi, real_2d_array &jac, void *ptr)
{
//
// this callback calculates the bi-objective target
//
// f0(x0,x1) = x0^2 + (x1-1)^2
// f1(x0,x1) = (x0-1(^2 + x1^2
//
// and Jacobian matrix J = [dfi/dxj]
//
fi[0] = x[0]*x[0]+(x[1]-1)*(x[1]-1);
fi[1] = (x[0]-1)*(x[0]-1)+x[1]*x[1];
jac[0][0] = 2*x[0];
jac[0][1] = 2*(x[1]-1);
jac[1][0] = 2*(x[0]-1);
jac[1][1] = 2*x[1];
}
void multiobjective2constr_fvec(const real_1d_array &x, real_1d_array &fi, void *ptr)
{
//
// this callback calculates the bi-objective target
//
// f0(x0,x1) = x0^2 + (x1-1)^2
// f1(x0,x1) = (x0-1(^2 + x1^2
//
// nonlinear constraint function
//
// f2(x0,x1) = x0^2 + x1^2
//
fi[0] = x[0]*x[0]+(x[1]-1)*(x[1]-1);
fi[1] = (x[0]-1)*(x[0]-1)+x[1]*x[1];
fi[2] = x[0]*x[0]+x[1]*x[1];
}
void multiobjective2constr_jac(const real_1d_array &x, real_1d_array &fi, real_2d_array &jac, void *ptr)
{
//
// this callback calculates the bi-objective target
//
// f0(x0,x1) = x0^2 + (x1-1)^2
// f1(x0,x1) = (x0-1(^2 + x1^2
//
// nonlinear constraint function
//
// f2(x0,x1) = x0^2 + x1^2
//
// and Jacobian matrix J = [dfi/dxj]
//
fi[0] = x[0]*x[0]+(x[1]-1)*(x[1]-1);
fi[1] = (x[0]-1)*(x[0]-1)+x[1]*x[1];
fi[2] = x[0]*x[0]+x[1]*x[1];
jac[0][0] = 2*x[0];
jac[0][1] = 2*(x[1]-1);
jac[1][0] = 2*(x[0]-1);
jac[1][1] = 2*x[1];
jac[2][0] = 2*x[0];
jac[2][1] = 2*x[1];
}
int main()
{
bool _TotalResult = true;
bool _TestResult;
int _spoil_scenario;
printf("CPUID:%s%s%s\n", alglib_impl::ae_cpuid()&alglib_impl::CPU_SSE2 ? " sse2" : "", alglib_impl::ae_cpuid()&alglib_impl::CPU_AVX2 ? " avx2" : "", alglib_impl::ae_cpuid()&alglib_impl::CPU_FMA ? " fma" : "");
#if AE_OS==AE_WINDOWS
printf("OS: Windows\n");
#elif AE_OS==AE_POSIX
printf("OS: POSIX\n");
#else
printf("OS: unknown\n");
#endif
printf("C++ tests. Please wait...\n");
#if AE_MALLOC==AE_BASIC_STATIC_MALLOC
const ae_int_t _static_pool_size = 1000000;
ae_int_t _static_pool_used = 0, _static_pool_free = 0;
void *_static_pool = malloc(_static_pool_size);
alglib_impl::set_memory_pool(_static_pool, _static_pool_size);
alglib_impl::memory_pool_stats(&_static_pool_used, &_static_pool_free);
if( _static_pool_used!=0 || _static_pool_free<0.95*_static_pool_size || _static_pool_free>_static_pool_size )
{
_TotalResult = false;
printf("FAILURE: memory pool usage stats are inconsistent!\n");
return 1;
}
{
alglib::real_2d_array a("[[1,2],[3,4]]");
ae_int_t _static_pool_used2 = 0, _static_pool_free2 = 0;
alglib_impl::memory_pool_stats(&_static_pool_used2, &_static_pool_free2);
if( _static_pool_used2<=_static_pool_used ||
_static_pool_free2>=_static_pool_free ||
_static_pool_used+_static_pool_free!=_static_pool_used2+_static_pool_free2 )
{
_TotalResult = false;
printf("FAILURE: memory pool usage stats are inconsistent!\n");
return 1;
}
a.setlength(1,1); // make sure that destructor of /a/ is never called prior to this point
}
#endif
#ifdef AE_USE_ALLOC_COUNTER
printf("Allocation counter activated...\n");
alglib_impl::_use_alloc_counter = ae_true;
if( alglib_impl::_alloc_counter!=0 )
{
_TotalResult = false;
printf("FAILURE: alloc_counter is non-zero on start!\n");
}
{
{
alglib::real_1d_array x;
x.setlength(1);
if( alglib_impl::_alloc_counter==0 )
printf(":::: WARNING: ALLOC_COUNTER IS INACTIVE!!! :::::\n");
}
if( alglib_impl::_alloc_counter!=0 )
{
printf("FAILURE: alloc_counter does not decrease!\n");
return 1;
}
}
#endif
try
{
//
// TEST xdebug_t1
// Test initialization (out parameter) and update (shared parameter) for records
//
printf("0/165\n");
_TestResult = true;
try
{
xdebugrecord1 rec1;
xdebuginitrecord1(rec1);
_TestResult = _TestResult && doc_test_int(rec1.i, 1);
_TestResult = _TestResult && doc_test_complex(rec1.c, alglib::complex(1,+1), 0.00005);
_TestResult = _TestResult && doc_test_real_vector(rec1.a, "[2,3]", 0.0005);
xdebugupdaterecord1(rec1);
_TestResult = _TestResult && doc_test_int(rec1.i, 2);
_TestResult = _TestResult && doc_test_complex(rec1.c, alglib::complex(3,+4), 0.00005);
_TestResult = _TestResult && doc_test_real_vector(rec1.a, "[2,3,6]", 0.0005);
}
catch(ap_error)
{ _TestResult = false; }
if( !_TestResult)
{
printf("%-32s FAILED\n", "xdebug_t1");
fflush(stdout);
}
_TotalResult = _TotalResult && _TestResult;
//
// TEST ablas_d_gemm
// Matrix multiplication (single-threaded)
//
_TestResult = true;
try
{
real_2d_array a = "[[2,1],[1,3]]";
real_2d_array b = "[[2,1],[0,1]]";
real_2d_array c = "[[0,0],[0,0]]";
//
// rmatrixgemm() function allows us to calculate matrix product C:=A*B or
// to perform more general operation, C:=alpha*op1(A)*op2(B)+beta*C,
// where A, B, C are rectangular matrices, op(X) can be X or X^T,
// alpha and beta are scalars.
//
// This function:
// * can apply transposition and/or multiplication by scalar to operands
// * can use arbitrary part of matrices A/B (given by submatrix offset)
// * can store result into arbitrary part of C
// * for performance reasons requires C to be preallocated
//
// Parameters of this function are:
// * M, N, K - sizes of op1(A) (which is MxK), op2(B) (which
// is KxN) and C (which is MxN)
// * Alpha - coefficient before A*B
// * A, IA, JA - matrix A and offset of the submatrix
// * OpTypeA - transformation type:
// 0 - no transformation
// 1 - transposition
// * B, IB, JB - matrix B and offset of the submatrix
// * OpTypeB - transformation type:
// 0 - no transformation
// 1 - transposition
// * Beta - coefficient before C
// * C, IC, JC - preallocated matrix C and offset of the submatrix
//
// Below we perform simple product C:=A*B (alpha=1, beta=0)
//
// IMPORTANT: this function works with preallocated C, which must be large
// enough to store multiplication result.
//
ae_int_t m = 2;
ae_int_t n = 2;
ae_int_t k = 2;
double alpha = 1.0;
ae_int_t ia = 0;
ae_int_t ja = 0;
ae_int_t optypea = 0;
ae_int_t ib = 0;
ae_int_t jb = 0;
ae_int_t optypeb = 0;
double beta = 0.0;
ae_int_t ic = 0;
ae_int_t jc = 0;
rmatrixgemm(m, n, k, alpha, a, ia, ja, optypea, b, ib, jb, optypeb, beta, c, ic, jc);
_TestResult = _TestResult && doc_test_real_matrix(c, "[[4,3],[2,4]]", 0.0001);
//
// Now we try to apply some simple transformation to operands: C:=A*B^T
//
optypeb = 1;
rmatrixgemm(m, n, k, alpha, a, ia, ja, optypea, b, ib, jb, optypeb, beta, c, ic, jc);
_TestResult = _TestResult && doc_test_real_matrix(c, "[[5,1],[5,3]]", 0.0001);
}
catch(ap_error)
{ _TestResult = false; }
if( !_TestResult)
{
printf("%-32s FAILED\n", "ablas_d_gemm");
fflush(stdout);
}
_TotalResult = _TotalResult && _TestResult;
//
// TEST ablas_d_syrk
// Symmetric rank-K update (single-threaded)
//
_TestResult = true;
try
{
//
// rmatrixsyrk() function allows us to calculate symmetric rank-K update
// C := beta*C + alpha*A'*A, where C is square N*N matrix, A is square K*N
// matrix, alpha and beta are scalars. It is also possible to update by
// adding A*A' instead of A'*A.
//
// Parameters of this function are:
// * N, K - matrix size
// * Alpha - coefficient before A
// * A, IA, JA - matrix and submatrix offsets
// * OpTypeA - multiplication type:
// * 0 - A*A^T is calculated
// * 2 - A^T*A is calculated
// * Beta - coefficient before C
// * C, IC, JC - preallocated input/output matrix and submatrix offsets
// * IsUpper - whether upper or lower triangle of C is updated;
// this function updates only one half of C, leaving
// other half unchanged (not referenced at all).
//
// Below we will show how to calculate simple product C:=A'*A
//
// NOTE: beta=0 and we do not use previous value of C, but still it
// MUST be preallocated.
//
ae_int_t n = 2;
ae_int_t k = 1;
double alpha = 1.0;
ae_int_t ia = 0;
ae_int_t ja = 0;
ae_int_t optypea = 2;
double beta = 0.0;
ae_int_t ic = 0;
ae_int_t jc = 0;
bool isupper = true;
real_2d_array a = "[[1,2]]";
// preallocate space to store result
real_2d_array c = "[[0,0],[0,0]]";
// calculate product, store result into upper part of c
rmatrixsyrk(n, k, alpha, a, ia, ja, optypea, beta, c, ic, jc, isupper);
// output result.
// IMPORTANT: lower triangle of C was NOT updated!
_TestResult = _TestResult && doc_test_real_matrix(c, "[[1,2],[0,4]]", 0.0001);
}
catch(ap_error)
{ _TestResult = false; }
if( !_TestResult)
{
printf("%-32s FAILED\n", "ablas_d_syrk");
fflush(stdout);
}
_TotalResult = _TotalResult && _TestResult;
//
// TEST ablas_t_complex
// Basis test for complex matrix functions (correctness and presence of SMP support)
//
_TestResult = true;
try
{
complex_2d_array a;
complex_2d_array b;
complex_2d_array c;
// test cmatrixgemm()
a = "[[2i,1i],[1,3]]";
b = "[[2,1],[0,1]]";
c = "[[0,0],[0,0]]";
cmatrixgemm(2, 2, 2, 1.0, a, 0, 0, 0, b, 0, 0, 0, 0.0, c, 0, 0);
_TestResult = _TestResult && doc_test_complex_matrix(c, "[[4i,3i],[2,4]]", 0.0001);
}
catch(ap_error)
{ _TestResult = false; }
if( !_TestResult)
{
printf("%-32s FAILED\n", "ablas_t_complex");
fflush(stdout);
}
_TotalResult = _TotalResult && _TestResult;
//
// TEST sparse_d_1
// Basic operations with sparse matrices
//
_TestResult = true;
for(_spoil_scenario=-1; _spoil_scenario<1; _spoil_scenario++)
{
try
{
//
// This example demonstrates creation/initialization of the sparse matrix
// and matrix-vector multiplication.
//
// First, we have to create matrix and initialize it. Matrix is initially created
// in the Hash-Table format, which allows convenient initialization. We can modify
// Hash-Table matrix with sparseset() and sparseadd() functions.
//
// NOTE: Unlike CRS format, Hash-Table representation allows you to initialize
// elements in the arbitrary order. You may see that we initialize a[0][0] first,
// then move to the second row, and then move back to the first row.
//
sparsematrix s;
sparsecreate(2, 2, s);
sparseset(s, 0, 0, 2.0);
sparseset(s, 1, 1, 1.0);
sparseset(s, 0, 1, 1.0);
sparseadd(s, 1, 1, 4.0);
//
// Now S is equal to
// [ 2 1 ]
// [ 5 ]
// Lets check it by reading matrix contents with sparseget().
// You may see that with sparseget() you may read both non-zero
// and zero elements.
//
double v;
v = sparseget(s, 0, 0);
_TestResult = _TestResult && doc_test_real(v, 2.0000, 0.005);
v = sparseget(s, 0, 1);
_TestResult = _TestResult && doc_test_real(v, 1.0000, 0.005);
v = sparseget(s, 1, 0);
_TestResult = _TestResult && doc_test_real(v, 0.0000, 0.005);
v = sparseget(s, 1, 1);
_TestResult = _TestResult && doc_test_real(v, 5.0000, 0.005);
//
// After successful creation we can use our matrix for linear operations.
//
// However, there is one more thing we MUST do before using S in linear
// operations: we have to convert it from HashTable representation (used for
// initialization and dynamic operations) to CRS format with sparseconverttocrs()
// call. If you omit this call, ALGLIB will generate exception on the first
// attempt to use S in linear operations.
//
sparseconverttocrs(s);
//
// Now S is in the CRS format and we are ready to do linear operations.
// Lets calculate A*x for some x.
//
real_1d_array x = "[1,-1]";
if( _spoil_scenario==0 )
spoil_vector_by_deleting_element(x);
real_1d_array y = "[]";
sparsemv(s, x, y);
_TestResult = _TestResult && doc_test_real_vector(y, "[1.000,-5.000]", 0.005);
_TestResult = _TestResult && (_spoil_scenario==-1);
}
catch(ap_error)
{ _TestResult = _TestResult && (_spoil_scenario!=-1); }
}
if( !_TestResult)
{
printf("%-32s FAILED\n", "sparse_d_1");
fflush(stdout);
}
_TotalResult = _TotalResult && _TestResult;
//
// TEST sparse_d_crs
// Advanced topic: creation in the CRS format.
//
_TestResult = true;
for(_spoil_scenario=-1; _spoil_scenario<2; _spoil_scenario++)
{
try
{
//
// This example demonstrates creation/initialization of the sparse matrix in the
// CRS format.
//
// Hash-Table format used by default is very convenient (it allows easy
// insertion of elements, automatic memory reallocation), but has
// significant memory and performance overhead. Insertion of one element
// costs hundreds of CPU cycles, and memory consumption is several times
// higher than that of CRS.
//
// When you work with really large matrices and when you can tell in
// advance how many elements EXACTLY you need, it can be beneficial to
// create matrix in the CRS format from the very beginning.
//
// If you want to create matrix in the CRS format, you should:
// * use sparsecreatecrs() function
// * know row sizes in advance (number of non-zero entries in the each row)
// * initialize matrix with sparseset() - another function, sparseadd(), is not allowed
// * initialize elements from left to right, from top to bottom, each
// element is initialized only once.
//
sparsematrix s;
integer_1d_array row_sizes = "[2,2,2,1]";
if( _spoil_scenario==0 )
spoil_vector_by_deleting_element(row_sizes);
sparsecreatecrs(4, 4, row_sizes, s);
sparseset(s, 0, 0, 2.0);
sparseset(s, 0, 1, 1.0);
sparseset(s, 1, 1, 4.0);
sparseset(s, 1, 2, 2.0);
sparseset(s, 2, 2, 3.0);
sparseset(s, 2, 3, 1.0);
sparseset(s, 3, 3, 9.0);
//
// Now S is equal to
// [ 2 1 ]
// [ 4 2 ]
// [ 3 1 ]
// [ 9 ]
//
// We should point that we have initialized S elements from left to right,
// from top to bottom. CRS representation does NOT allow you to do so in
// the different order. Try to change order of the sparseset() calls above,
// and you will see that your program generates exception.
//
// We can check it by reading matrix contents with sparseget().
// However, you should remember that sparseget() is inefficient on
// CRS matrices (it may have to pass through all elements of the row
// until it finds element you need).
//
double v;
v = sparseget(s, 0, 0);
_TestResult = _TestResult && doc_test_real(v, 2.0000, 0.005);
v = sparseget(s, 2, 3);
_TestResult = _TestResult && doc_test_real(v, 1.0000, 0.005);
// you may see that you can read zero elements (which are not stored) with sparseget()
v = sparseget(s, 3, 2);
_TestResult = _TestResult && doc_test_real(v, 0.0000, 0.005);
//
// After successful creation we can use our matrix for linear operations.
// Lets calculate A*x for some x.
//
real_1d_array x = "[1,-1,1,-1]";
if( _spoil_scenario==1 )
spoil_vector_by_deleting_element(x);
real_1d_array y = "[]";
sparsemv(s, x, y);
_TestResult = _TestResult && doc_test_real_vector(y, "[1.000,-2.000,2.000,-9]", 0.005);
_TestResult = _TestResult && (_spoil_scenario==-1);
}
catch(ap_error)
{ _TestResult = _TestResult && (_spoil_scenario!=-1); }
}
if( !_TestResult)
{
printf("%-32s FAILED\n", "sparse_d_crs");
fflush(stdout);
}
_TotalResult = _TotalResult && _TestResult;
//
// TEST solve_real
// Solving dense linear equations
//
_TestResult = true;
for(_spoil_scenario=-1; _spoil_scenario<43; _spoil_scenario++)
{
try
{
//
// This example demonstrates solution of a dense real linear system
//
real_1d_array x;
integer_1d_array pivots;
densesolverreport rep;
//
// First, solve A*x=b with a feature-rich rmatrixsolve() which supports iterative improvement
// and condition number estimation
//
real_2d_array a = "[[4,2],[-1,3]]";
if( _spoil_scenario==0 )
spoil_matrix_by_nan(a);
if( _spoil_scenario==1 )
spoil_matrix_by_posinf(a);
if( _spoil_scenario==2 )
spoil_matrix_by_neginf(a);
if( _spoil_scenario==3 )
spoil_matrix_by_adding_row(a);
if( _spoil_scenario==4 )
spoil_matrix_by_adding_col(a);
if( _spoil_scenario==5 )
spoil_matrix_by_deleting_row(a);
if( _spoil_scenario==6 )
spoil_matrix_by_deleting_col(a);
real_1d_array b = "[8,5]";
if( _spoil_scenario==7 )
spoil_vector_by_nan(b);
if( _spoil_scenario==8 )
spoil_vector_by_posinf(b);
if( _spoil_scenario==9 )
spoil_vector_by_neginf(b);
if( _spoil_scenario==10 )
spoil_vector_by_adding_element(b);
if( _spoil_scenario==11 )
spoil_vector_by_deleting_element(b);
rmatrixsolve(a, b, x, rep);
_TestResult = _TestResult && doc_test_int(rep.terminationtype, 1);
_TestResult = _TestResult && doc_test_real_vector(x, "[1.0000, 2.0000]", 0.00005);
//
// Then, solve C*x=d with rmatrixsolvefast() which has lower overhead
//
real_2d_array c = "[[3,1],[2,4]]";
if( _spoil_scenario==12 )
spoil_matrix_by_nan(c);
if( _spoil_scenario==13 )
spoil_matrix_by_posinf(c);
if( _spoil_scenario==14 )
spoil_matrix_by_neginf(c);
if( _spoil_scenario==15 )
spoil_matrix_by_adding_row(c);
if( _spoil_scenario==16 )
spoil_matrix_by_adding_col(c);
if( _spoil_scenario==17 )
spoil_matrix_by_deleting_row(c);
if( _spoil_scenario==18 )
spoil_matrix_by_deleting_col(c);
real_1d_array d = "[2,-2]";
if( _spoil_scenario==19 )
spoil_vector_by_nan(d);
if( _spoil_scenario==20 )
spoil_vector_by_posinf(d);
if( _spoil_scenario==21 )
spoil_vector_by_neginf(d);
if( _spoil_scenario==22 )
spoil_vector_by_adding_element(d);
if( _spoil_scenario==23 )
spoil_vector_by_deleting_element(d);
rmatrixsolvefast(c, d);
_TestResult = _TestResult && doc_test_real_vector(d, "[1.0000, -1.0000]", 0.00005);
//
// Sometimes you have LU decomposition of the system matrix readily
// available. In such cases it is possible to save a lot of time by
// passing precomputed LU factors to rmatrixlusolve(). The only
// downside of such approach is that iterative refinement is unavailable
// because original (unmodified) form of the system matrix is unknown
// to ALGLIB.
//
// However, if you have BOTH original matrix and its LU decomposition,
// it is possible to use rmatrixmixedsolve() which accepts both matrix
// itself and its factors, and uses original matrix to refine solution
// obtained with LU factors.
//
real_2d_array e = "[[3,4],[2,4]]";
if( _spoil_scenario==24 )
spoil_matrix_by_nan(e);
if( _spoil_scenario==25 )
spoil_matrix_by_posinf(e);
if( _spoil_scenario==26 )
spoil_matrix_by_neginf(e);
if( _spoil_scenario==27 )
spoil_matrix_by_adding_row(e);
if( _spoil_scenario==28 )
spoil_matrix_by_adding_col(e);
if( _spoil_scenario==29 )
spoil_matrix_by_deleting_row(e);
if( _spoil_scenario==30 )
spoil_matrix_by_deleting_col(e);
real_2d_array lue = "[[3,4],[2,4]]";
if( _spoil_scenario==31 )
spoil_matrix_by_nan(lue);
if( _spoil_scenario==32 )
spoil_matrix_by_posinf(lue);
if( _spoil_scenario==33 )
spoil_matrix_by_neginf(lue);
if( _spoil_scenario==34 )
spoil_matrix_by_adding_row(lue);
if( _spoil_scenario==35 )
spoil_matrix_by_adding_col(lue);
if( _spoil_scenario==36 )
spoil_matrix_by_deleting_row(lue);
if( _spoil_scenario==37 )
spoil_matrix_by_deleting_col(lue);
real_1d_array f = "[2,0]";
if( _spoil_scenario==38 )
spoil_vector_by_nan(f);
if( _spoil_scenario==39 )
spoil_vector_by_posinf(f);
if( _spoil_scenario==40 )
spoil_vector_by_neginf(f);
if( _spoil_scenario==41 )
spoil_vector_by_adding_element(f);
if( _spoil_scenario==42 )
spoil_vector_by_deleting_element(f);
rmatrixlu(lue, pivots);
rmatrixlusolve(lue, pivots, f, x, rep);
_TestResult = _TestResult && doc_test_int(rep.terminationtype, 1);
_TestResult = _TestResult && doc_test_real_vector(x, "[2.0000, -1.0000]", 0.00005);
rmatrixmixedsolve(e, lue, pivots, f, x, rep);
_TestResult = _TestResult && doc_test_int(rep.terminationtype, 1);
_TestResult = _TestResult && doc_test_real_vector(x, "[2.0000, -1.0000]", 0.00005);
_TestResult = _TestResult && (_spoil_scenario==-1);
}
catch(ap_error)
{ _TestResult = _TestResult && (_spoil_scenario!=-1); }
}
if( !_TestResult)
{
printf("%-32s FAILED\n", "solve_real");
fflush(stdout);
}
_TotalResult = _TotalResult && _TestResult;
//
// TEST solve_ls
// Solving dense linear equations in the least squares sense
//
_TestResult = true;
for(_spoil_scenario=-1; _spoil_scenario<6; _spoil_scenario++)
{
try
{
real_1d_array x;
densesolverlsreport rep;
real_2d_array a = "[[4,2],[-1,3],[6,5]]";
if( _spoil_scenario==0 )
spoil_matrix_by_nan(a);
if( _spoil_scenario==1 )
spoil_matrix_by_posinf(a);
if( _spoil_scenario==2 )
spoil_matrix_by_neginf(a);
real_1d_array b = "[8,5,16]";
if( _spoil_scenario==3 )
spoil_vector_by_nan(b);
if( _spoil_scenario==4 )
spoil_vector_by_posinf(b);
if( _spoil_scenario==5 )
spoil_vector_by_neginf(b);
rmatrixsolvels(a, b, 0.0, x, rep);
_TestResult = _TestResult && doc_test_int(rep.terminationtype, 1);
_TestResult = _TestResult && doc_test_real_vector(x, "[1.0000, 2.0000]", 0.00005);
_TestResult = _TestResult && (_spoil_scenario==-1);
}
catch(ap_error)
{ _TestResult = _TestResult && (_spoil_scenario!=-1); }
}
if( !_TestResult)
{
printf("%-32s FAILED\n", "solve_ls");
fflush(stdout);
}
_TotalResult = _TotalResult && _TestResult;
//
// TEST solve_real_m
// Solving dense linear matrix equations
//
_TestResult = true;
for(_spoil_scenario=-1; _spoil_scenario<37; _spoil_scenario++)
{
try
{
//
// This example demonstrates solution of a dense real matrix system
//
real_2d_array x;
integer_1d_array pivots;
densesolverreport rep;
//
// First, solve A*X=B with a feature-rich rmatrixsolvem() which supports
// iterative improvement and condition number estimation. Here A is
// an N*N matrix, X is an N*M matrix, B is an N*M matrix.
//
real_2d_array a = "[[4,2],[-1,3]]";
if( _spoil_scenario==0 )
spoil_matrix_by_nan(a);
if( _spoil_scenario==1 )
spoil_matrix_by_posinf(a);
if( _spoil_scenario==2 )
spoil_matrix_by_neginf(a);
if( _spoil_scenario==3 )
spoil_matrix_by_adding_row(a);
if( _spoil_scenario==4 )
spoil_matrix_by_adding_col(a);
if( _spoil_scenario==5 )
spoil_matrix_by_deleting_row(a);
if( _spoil_scenario==6 )
spoil_matrix_by_deleting_col(a);
real_2d_array b = "[[8,10,4],[5,1,-1]]";
if( _spoil_scenario==7 )
spoil_matrix_by_nan(b);
if( _spoil_scenario==8 )
spoil_matrix_by_posinf(b);
if( _spoil_scenario==9 )
spoil_matrix_by_neginf(b);
rmatrixsolvem(a, b, true, x, rep);
_TestResult = _TestResult && doc_test_int(rep.terminationtype, 1);
_TestResult = _TestResult && doc_test_real_matrix(x, "[[1.0000, 2.0000,1.0000],[2.0000,1.0000,0.0000]]", 0.00005);
//
// Then, solve C*X=D with rmatrixsolvemfast() which has lower overhead
// due to condition number estimation and iterative refinement parts
// being dropped.
//
real_2d_array c = "[[3,1],[2,4]]";
if( _spoil_scenario==10 )
spoil_matrix_by_nan(c);
if( _spoil_scenario==11 )
spoil_matrix_by_posinf(c);
if( _spoil_scenario==12 )
spoil_matrix_by_neginf(c);
if( _spoil_scenario==13 )
spoil_matrix_by_adding_row(c);
if( _spoil_scenario==14 )
spoil_matrix_by_adding_col(c);
if( _spoil_scenario==15 )
spoil_matrix_by_deleting_row(c);
if( _spoil_scenario==16 )
spoil_matrix_by_deleting_col(c);
real_2d_array d = "[[2,1],[-2,4]]";
if( _spoil_scenario==17 )
spoil_matrix_by_nan(d);
if( _spoil_scenario==18 )
spoil_matrix_by_posinf(d);
if( _spoil_scenario==19 )
spoil_matrix_by_neginf(d);
rmatrixsolvemfast(c, d);
_TestResult = _TestResult && doc_test_real_matrix(d, "[[1.0000,0.0000],[-1.0000,1.0000]]", 0.00005);
//
// Sometimes you have LU decomposition of the system matrix readily
// available. In such cases it is possible to save a lot of time by
// passing precomputed LU factors to rmatrixlusolve(). The only
// downside of such approach is that iterative refinement is unavailable
// because original (unmodified) form of the system matrix is unknown
// to ALGLIB.
//
// However, if you have BOTH original matrix and its LU decomposition,
// it is possible to use rmatrixmixedsolve() which accepts both matrix
// itself and its factors, and uses original matrix to refine solution
// obtained with LU factors.
//
real_2d_array e = "[[3,4],[2,4]]";
if( _spoil_scenario==20 )
spoil_matrix_by_nan(e);
if( _spoil_scenario==21 )
spoil_matrix_by_posinf(e);
if( _spoil_scenario==22 )
spoil_matrix_by_neginf(e);
if( _spoil_scenario==23 )
spoil_matrix_by_adding_row(e);
if( _spoil_scenario==24 )
spoil_matrix_by_adding_col(e);
if( _spoil_scenario==25 )
spoil_matrix_by_deleting_row(e);
if( _spoil_scenario==26 )
spoil_matrix_by_deleting_col(e);
real_2d_array lue = "[[3,4],[2,4]]";
if( _spoil_scenario==27 )
spoil_matrix_by_nan(lue);
if( _spoil_scenario==28 )
spoil_matrix_by_posinf(lue);
if( _spoil_scenario==29 )
spoil_matrix_by_neginf(lue);
if( _spoil_scenario==30 )
spoil_matrix_by_adding_row(lue);
if( _spoil_scenario==31 )
spoil_matrix_by_adding_col(lue);
if( _spoil_scenario==32 )
spoil_matrix_by_deleting_row(lue);
if( _spoil_scenario==33 )
spoil_matrix_by_deleting_col(lue);
real_2d_array f = "[[2,5],[0,6]]";
if( _spoil_scenario==34 )
spoil_matrix_by_nan(f);
if( _spoil_scenario==35 )
spoil_matrix_by_posinf(f);
if( _spoil_scenario==36 )
spoil_matrix_by_neginf(f);
rmatrixlu(lue, pivots);
rmatrixlusolvem(lue, pivots, f, x, rep);
_TestResult = _TestResult && doc_test_int(rep.terminationtype, 1);
_TestResult = _TestResult && doc_test_real_matrix(x, "[[2.0000,-1.0000],[-1.0000,2.0000]]", 0.00005);
rmatrixmixedsolvem(e, lue, pivots, f, x, rep);
_TestResult = _TestResult && doc_test_int(rep.terminationtype, 1);
_TestResult = _TestResult && doc_test_real_matrix(x, "[[2.0000,-1.0000],[-1.0000,2.0000]]", 0.00005);
_TestResult = _TestResult && (_spoil_scenario==-1);
}
catch(ap_error)
{ _TestResult = _TestResult && (_spoil_scenario!=-1); }
}
if( !_TestResult)
{
printf("%-32s FAILED\n", "solve_real_m");
fflush(stdout);
}
_TotalResult = _TotalResult && _TestResult;
//
// TEST solve_complex
// Solving dense complex linear equations
//
_TestResult = true;
for(_spoil_scenario=-1; _spoil_scenario<43; _spoil_scenario++)
{
try
{
//
// This example demonstrates solution of a complex linear system
//
complex_1d_array x;
integer_1d_array pivots;
densesolverreport rep;
//
// First, solve A*x=b with a feature-rich cmatrixsolve() which supports iterative improvement
// and condition number estimation
//
complex_2d_array a = "[[-4,2i],[-1i,3]]";
if( _spoil_scenario==0 )
spoil_matrix_by_nan(a);
if( _spoil_scenario==1 )
spoil_matrix_by_posinf(a);
if( _spoil_scenario==2 )
spoil_matrix_by_neginf(a);
if( _spoil_scenario==3 )
spoil_matrix_by_adding_row(a);
if( _spoil_scenario==4 )
spoil_matrix_by_adding_col(a);
if( _spoil_scenario==5 )
spoil_matrix_by_deleting_row(a);
if( _spoil_scenario==6 )
spoil_matrix_by_deleting_col(a);
complex_1d_array b = "[8i,5]";
if( _spoil_scenario==7 )
spoil_vector_by_nan(b);
if( _spoil_scenario==8 )
spoil_vector_by_posinf(b);
if( _spoil_scenario==9 )
spoil_vector_by_neginf(b);
if( _spoil_scenario==10 )
spoil_vector_by_adding_element(b);
if( _spoil_scenario==11 )
spoil_vector_by_deleting_element(b);
cmatrixsolve(a, b, x, rep);
_TestResult = _TestResult && doc_test_int(rep.terminationtype, 1);
_TestResult = _TestResult && doc_test_complex_vector(x, "[-1.0000i, 2.0000]", 0.00005);
//
// Then, solve C*x=d with cmatrixsolvefast() which has lower overhead
//
complex_2d_array c = "[[3i,1],[2i,4]]";
if( _spoil_scenario==12 )
spoil_matrix_by_nan(c);
if( _spoil_scenario==13 )
spoil_matrix_by_posinf(c);
if( _spoil_scenario==14 )
spoil_matrix_by_neginf(c);
if( _spoil_scenario==15 )
spoil_matrix_by_adding_row(c);
if( _spoil_scenario==16 )
spoil_matrix_by_adding_col(c);
if( _spoil_scenario==17 )
spoil_matrix_by_deleting_row(c);
if( _spoil_scenario==18 )
spoil_matrix_by_deleting_col(c);
complex_1d_array d = "[2,-2]";
if( _spoil_scenario==19 )
spoil_vector_by_nan(d);
if( _spoil_scenario==20 )
spoil_vector_by_posinf(d);
if( _spoil_scenario==21 )
spoil_vector_by_neginf(d);
if( _spoil_scenario==22 )
spoil_vector_by_adding_element(d);
if( _spoil_scenario==23 )
spoil_vector_by_deleting_element(d);
cmatrixsolvefast(c, d);
_TestResult = _TestResult && doc_test_complex_vector(d, "[-1.0000i, -1.0000]", 0.00005);
//
// Sometimes you have LU decomposition of the system matrix readily
// available. In such cases it is possible to save a lot of time by
// passing precomputed LU factors to cmatrixlusolve(). The only
// downside of such approach is that iterative refinement is unavailable
// because original (unmodified) form of the system matrix is unknown
// to ALGLIB.
//
// However, if you have BOTH original matrix and its LU decomposition,
// it is possible to use cmatrixmixedsolve() which accepts both matrix
// itself and its factors, and uses original matrix to refine solution
// obtained with LU factors.
//
complex_2d_array e = "[[-3,4i],[2i,4]]";
if( _spoil_scenario==24 )
spoil_matrix_by_nan(e);
if( _spoil_scenario==25 )
spoil_matrix_by_posinf(e);
if( _spoil_scenario==26 )
spoil_matrix_by_neginf(e);
if( _spoil_scenario==27 )
spoil_matrix_by_adding_row(e);
if( _spoil_scenario==28 )
spoil_matrix_by_adding_col(e);
if( _spoil_scenario==29 )
spoil_matrix_by_deleting_row(e);
if( _spoil_scenario==30 )
spoil_matrix_by_deleting_col(e);
complex_2d_array lue = "[[-3,4i],[2i,4]]";
if( _spoil_scenario==31 )
spoil_matrix_by_nan(lue);
if( _spoil_scenario==32 )
spoil_matrix_by_posinf(lue);
if( _spoil_scenario==33 )
spoil_matrix_by_neginf(lue);
if( _spoil_scenario==34 )
spoil_matrix_by_adding_row(lue);
if( _spoil_scenario==35 )
spoil_matrix_by_adding_col(lue);
if( _spoil_scenario==36 )
spoil_matrix_by_deleting_row(lue);
if( _spoil_scenario==37 )
spoil_matrix_by_deleting_col(lue);
complex_1d_array f = "[2i,0]";
if( _spoil_scenario==38 )
spoil_vector_by_nan(f);
if( _spoil_scenario==39 )
spoil_vector_by_posinf(f);
if( _spoil_scenario==40 )
spoil_vector_by_neginf(f);
if( _spoil_scenario==41 )
spoil_vector_by_adding_element(f);
if( _spoil_scenario==42 )
spoil_vector_by_deleting_element(f);
cmatrixlu(lue, pivots);
cmatrixlusolve(lue, pivots, f, x, rep);
_TestResult = _TestResult && doc_test_int(rep.terminationtype, 1);
_TestResult = _TestResult && doc_test_complex_vector(x, "[-2.0000i, -1.0000]", 0.00005);
cmatrixmixedsolve(e, lue, pivots, f, x, rep);
_TestResult = _TestResult && doc_test_int(rep.terminationtype, 1);
_TestResult = _TestResult && doc_test_complex_vector(x, "[-2.0000i, -1.0000]", 0.00005);
_TestResult = _TestResult && (_spoil_scenario==-1);
}
catch(ap_error)
{ _TestResult = _TestResult && (_spoil_scenario!=-1); }
}
if( !_TestResult)
{
printf("%-32s FAILED\n", "solve_complex");
fflush(stdout);
}
_TotalResult = _TotalResult && _TestResult;
//
// TEST solve_complex_m
// Solving complex matrix equations
//
_TestResult = true;
for(_spoil_scenario=-1; _spoil_scenario<37; _spoil_scenario++)
{
try
{
//
// This example demonstrates solution of a dense complex matrix system
//
complex_2d_array x;
integer_1d_array pivots;
densesolverreport rep;
//
// First, solve A*X=B with a feature-rich cmatrixsolvem() which supports
// iterative improvement and condition number estimation. Here A is
// an N*N matrix, X is an N*M matrix, B is an N*M matrix.
//
complex_2d_array a = "[[4i,-2],[-1,3i]]";
if( _spoil_scenario==0 )
spoil_matrix_by_nan(a);
if( _spoil_scenario==1 )
spoil_matrix_by_posinf(a);
if( _spoil_scenario==2 )
spoil_matrix_by_neginf(a);
if( _spoil_scenario==3 )
spoil_matrix_by_adding_row(a);
if( _spoil_scenario==4 )
spoil_matrix_by_adding_col(a);
if( _spoil_scenario==5 )
spoil_matrix_by_deleting_row(a);
if( _spoil_scenario==6 )
spoil_matrix_by_deleting_col(a);
complex_2d_array b = "[[8i,10i,4i],[5,1,-1]]";
if( _spoil_scenario==7 )
spoil_matrix_by_nan(b);
if( _spoil_scenario==8 )
spoil_matrix_by_posinf(b);
if( _spoil_scenario==9 )
spoil_matrix_by_neginf(b);
cmatrixsolvem(a, b, true, x, rep);
_TestResult = _TestResult && doc_test_int(rep.terminationtype, 1);
_TestResult = _TestResult && doc_test_complex_matrix(x, "[[1.0000, 2.0000,1.0000],[-2.0000i,-1.0000i,0.0000]]", 0.00005);
//
// Then, solve C*X=D with cmatrixsolvemfast() which has lower overhead
// due to condition number estimation and iterative refinement parts
// being dropped.
//
complex_2d_array c = "[[3,1],[2,4]]";
if( _spoil_scenario==10 )
spoil_matrix_by_nan(c);
if( _spoil_scenario==11 )
spoil_matrix_by_posinf(c);
if( _spoil_scenario==12 )
spoil_matrix_by_neginf(c);
if( _spoil_scenario==13 )
spoil_matrix_by_adding_row(c);
if( _spoil_scenario==14 )
spoil_matrix_by_adding_col(c);
if( _spoil_scenario==15 )
spoil_matrix_by_deleting_row(c);
if( _spoil_scenario==16 )
spoil_matrix_by_deleting_col(c);
complex_2d_array d = "[[2,1],[-2,4]]";
if( _spoil_scenario==17 )
spoil_matrix_by_nan(d);
if( _spoil_scenario==18 )
spoil_matrix_by_posinf(d);
if( _spoil_scenario==19 )
spoil_matrix_by_neginf(d);
cmatrixsolvemfast(c, d);
_TestResult = _TestResult && doc_test_complex_matrix(d, "[[1.0000,0.0000],[-1.0000,1.0000]]", 0.00005);
//
// Sometimes you have LU decomposition of the system matrix readily
// available. In such cases it is possible to save a lot of time by
// passing precomputed LU factors to cmatrixlusolve(). The only
// downside of such approach is that iterative refinement is unavailable
// because original (unmodified) form of the system matrix is unknown
// to ALGLIB.
//
// However, if you have BOTH original matrix and its LU decomposition,
// it is possible to use cmatrixmixedsolve() which accepts both matrix
// itself and its factors, and uses original matrix to refine solution
// obtained with LU factors.
//
complex_2d_array e = "[[3,4],[2,4]]";
if( _spoil_scenario==20 )
spoil_matrix_by_nan(e);
if( _spoil_scenario==21 )
spoil_matrix_by_posinf(e);
if( _spoil_scenario==22 )
spoil_matrix_by_neginf(e);
if( _spoil_scenario==23 )
spoil_matrix_by_adding_row(e);
if( _spoil_scenario==24 )
spoil_matrix_by_adding_col(e);
if( _spoil_scenario==25 )
spoil_matrix_by_deleting_row(e);
if( _spoil_scenario==26 )
spoil_matrix_by_deleting_col(e);
complex_2d_array lue = "[[3,4],[2,4]]";
if( _spoil_scenario==27 )
spoil_matrix_by_nan(lue);
if( _spoil_scenario==28 )
spoil_matrix_by_posinf(lue);
if( _spoil_scenario==29 )
spoil_matrix_by_neginf(lue);
if( _spoil_scenario==30 )
spoil_matrix_by_adding_row(lue);
if( _spoil_scenario==31 )
spoil_matrix_by_adding_col(lue);
if( _spoil_scenario==32 )
spoil_matrix_by_deleting_row(lue);
if( _spoil_scenario==33 )
spoil_matrix_by_deleting_col(lue);
complex_2d_array f = "[[2,5],[0,6]]";
if( _spoil_scenario==34 )
spoil_matrix_by_nan(f);
if( _spoil_scenario==35 )
spoil_matrix_by_posinf(f);
if( _spoil_scenario==36 )
spoil_matrix_by_neginf(f);
cmatrixlu(lue, pivots);
cmatrixlusolvem(lue, pivots, f, x, rep);
_TestResult = _TestResult && doc_test_int(rep.terminationtype, 1);
_TestResult = _TestResult && doc_test_complex_matrix(x, "[[2.0000,-1.0000],[-1.0000,2.0000]]", 0.00005);
cmatrixmixedsolvem(e, lue, pivots, f, x, rep);
_TestResult = _TestResult && doc_test_int(rep.terminationtype, 1);
_TestResult = _TestResult && doc_test_complex_matrix(x, "[[2.0000,-1.0000],[-1.0000,2.0000]]", 0.00005);
_TestResult = _TestResult && (_spoil_scenario==-1);
}
catch(ap_error)
{ _TestResult = _TestResult && (_spoil_scenario!=-1); }
}
if( !_TestResult)
{
printf("%-32s FAILED\n", "solve_complex_m");
fflush(stdout);
}
_TotalResult = _TotalResult && _TestResult;
//
// TEST solve_spd
// Solving symmetric positive definite linear equations
//
_TestResult = true;
for(_spoil_scenario=-1; _spoil_scenario<18; _spoil_scenario++)
{
try
{
//
// This example demonstrates solution of a symmetric positive definite real system
//
real_1d_array x;
densesolverreport rep;
bool isupper = true;
//
// First, solve A*x=b with a feature-rich spdmatrixsolve() which supports iterative improvement
// and condition number estimation
//
real_2d_array a = "[[4,1],[1,4]]";
if( _spoil_scenario==0 )
spoil_matrix_by_adding_row(a);
if( _spoil_scenario==1 )
spoil_matrix_by_adding_col(a);
if( _spoil_scenario==2 )
spoil_matrix_by_deleting_row(a);
if( _spoil_scenario==3 )
spoil_matrix_by_deleting_col(a);
real_1d_array b = "[6,9]";
if( _spoil_scenario==4 )
spoil_vector_by_adding_element(b);
if( _spoil_scenario==5 )
spoil_vector_by_deleting_element(b);
spdmatrixsolve(a, isupper, b, x, rep);
_TestResult = _TestResult && doc_test_int(rep.terminationtype, 1);
_TestResult = _TestResult && doc_test_real_vector(x, "[1.0000, 2.0000]", 0.00005);
//
// Then, solve C*x=d with spdmatrixsolvefast() which has lower overhead
//
real_2d_array c = "[[3,1],[1,3]]";
if( _spoil_scenario==6 )
spoil_matrix_by_adding_row(c);
if( _spoil_scenario==7 )
spoil_matrix_by_adding_col(c);
if( _spoil_scenario==8 )
spoil_matrix_by_deleting_row(c);
if( _spoil_scenario==9 )
spoil_matrix_by_deleting_col(c);
real_1d_array d = "[2,-2]";
if( _spoil_scenario==10 )
spoil_vector_by_adding_element(d);
if( _spoil_scenario==11 )
spoil_vector_by_deleting_element(d);
spdmatrixsolvefast(c, isupper, d);
_TestResult = _TestResult && doc_test_real_vector(d, "[1.0000, -1.0000]", 0.00005);
//
// Sometimes you have Cholesky decomposition of the system matrix readily
// available. In such cases it is possible to save a lot of time by
// passing precomputed Cholesky factor to spdmatrixcholeskysolve(). The only
// downside of such approach is that iterative refinement is unavailable
// because original (unmodified) form of the system matrix is unknown
// to ALGLIB.
//
real_2d_array e = "[[3,2],[2,3]]";
if( _spoil_scenario==12 )
spoil_matrix_by_adding_row(e);
if( _spoil_scenario==13 )
spoil_matrix_by_adding_col(e);
if( _spoil_scenario==14 )
spoil_matrix_by_deleting_row(e);
if( _spoil_scenario==15 )
spoil_matrix_by_deleting_col(e);
real_1d_array f = "[4,1]";
if( _spoil_scenario==16 )
spoil_vector_by_adding_element(f);
if( _spoil_scenario==17 )
spoil_vector_by_deleting_element(f);
spdmatrixcholesky(e, isupper);
spdmatrixcholeskysolve(e, isupper, f, x, rep);
_TestResult = _TestResult && doc_test_int(rep.terminationtype, 1);
_TestResult = _TestResult && doc_test_real_vector(x, "[2.0000, -1.0000]", 0.00005);
_TestResult = _TestResult && (_spoil_scenario==-1);
}
catch(ap_error)
{ _TestResult = _TestResult && (_spoil_scenario!=-1); }
}
if( !_TestResult)
{
printf("%-32s FAILED\n", "solve_spd");
fflush(stdout);
}
_TotalResult = _TotalResult && _TestResult;
//
// TEST solve_hpd
// Solving Hermitian positive definite linear equations
//
_TestResult = true;
for(_spoil_scenario=-1; _spoil_scenario<18; _spoil_scenario++)
{
try
{
//
// This example demonstrates solution of a Hermitian positive definite complex system
//
complex_1d_array x;
densesolverreport rep;
bool isupper = true;
//
// First, solve A*x=b with a feature-rich hpdmatrixsolve() which supports iterative improvement
// and condition number estimation
//
complex_2d_array a = "[[4,1i],[-1i,4]]";
if( _spoil_scenario==0 )
spoil_matrix_by_adding_row(a);
if( _spoil_scenario==1 )
spoil_matrix_by_adding_col(a);
if( _spoil_scenario==2 )
spoil_matrix_by_deleting_row(a);
if( _spoil_scenario==3 )
spoil_matrix_by_deleting_col(a);
complex_1d_array b = "[6,-9i]";
if( _spoil_scenario==4 )
spoil_vector_by_adding_element(b);
if( _spoil_scenario==5 )
spoil_vector_by_deleting_element(b);
hpdmatrixsolve(a, isupper, b, x, rep);
_TestResult = _TestResult && doc_test_int(rep.terminationtype, 1);
_TestResult = _TestResult && doc_test_complex_vector(x, "[1.0000, -2.0000i]", 0.00005);
//
// Then, solve C*x=d with hpdmatrixsolvefast() which has lower overhead
//
complex_2d_array c = "[[3,-1i],[1i,3]]";
if( _spoil_scenario==6 )
spoil_matrix_by_adding_row(c);
if( _spoil_scenario==7 )
spoil_matrix_by_adding_col(c);
if( _spoil_scenario==8 )
spoil_matrix_by_deleting_row(c);
if( _spoil_scenario==9 )
spoil_matrix_by_deleting_col(c);
complex_1d_array d = "[-2i,-2]";
if( _spoil_scenario==10 )
spoil_vector_by_adding_element(d);
if( _spoil_scenario==11 )
spoil_vector_by_deleting_element(d);
hpdmatrixsolvefast(c, isupper, d);
_TestResult = _TestResult && doc_test_complex_vector(d, "[-1.0000i, -1.0000]", 0.00005);
//
// Sometimes you have Cholesky decomposition of the system matrix readily
// available. In such cases it is possible to save a lot of time by
// passing precomputed Cholesky factor to hpdmatrixcholeskysolve(). The only
// downside of such approach is that iterative refinement is unavailable
// because original (unmodified) form of the system matrix is unknown
// to ALGLIB.
//
complex_2d_array e = "[[3,2],[2,3]]";
if( _spoil_scenario==12 )
spoil_matrix_by_adding_row(e);
if( _spoil_scenario==13 )
spoil_matrix_by_adding_col(e);
if( _spoil_scenario==14 )
spoil_matrix_by_deleting_row(e);
if( _spoil_scenario==15 )
spoil_matrix_by_deleting_col(e);
complex_1d_array f = "[4,1]";
if( _spoil_scenario==16 )
spoil_vector_by_adding_element(f);
if( _spoil_scenario==17 )
spoil_vector_by_deleting_element(f);
hpdmatrixcholesky(e, isupper);
hpdmatrixcholeskysolve(e, isupper, f, x, rep);
_TestResult = _TestResult && doc_test_int(rep.terminationtype, 1);
_TestResult = _TestResult && doc_test_complex_vector(x, "[2.0000, -1.0000]", 0.00005);
_TestResult = _TestResult && (_spoil_scenario==-1);
}
catch(ap_error)
{ _TestResult = _TestResult && (_spoil_scenario!=-1); }
}
if( !_TestResult)
{
printf("%-32s FAILED\n", "solve_hpd");
fflush(stdout);
}
_TotalResult = _TotalResult && _TestResult;
//
// TEST solve_real_tst
// .
//
_TestResult = true;
for(_spoil_scenario=-1; _spoil_scenario<36; _spoil_scenario++)
{
try
{
real_1d_array x;
integer_1d_array pivots;
densesolverreport rep;
real_2d_array a = "[[4,2],[-1,3]]";
if( _spoil_scenario==0 )
spoil_matrix_by_nan(a);
if( _spoil_scenario==1 )
spoil_matrix_by_posinf(a);
if( _spoil_scenario==2 )
spoil_matrix_by_neginf(a);
if( _spoil_scenario==3 )
spoil_matrix_by_deleting_row(a);
if( _spoil_scenario==4 )
spoil_matrix_by_deleting_col(a);
real_1d_array b = "[8,5]";
if( _spoil_scenario==5 )
spoil_vector_by_nan(b);
if( _spoil_scenario==6 )
spoil_vector_by_posinf(b);
if( _spoil_scenario==7 )
spoil_vector_by_neginf(b);
if( _spoil_scenario==8 )
spoil_vector_by_deleting_element(b);
rmatrixsolve(a, 2, b, x, rep);
_TestResult = _TestResult && doc_test_int(rep.terminationtype, 1);
_TestResult = _TestResult && doc_test_real_vector(x, "[1.0000, 2.0000]", 0.00005);
real_2d_array c = "[[3,1],[2,4]]";
if( _spoil_scenario==9 )
spoil_matrix_by_nan(c);
if( _spoil_scenario==10 )
spoil_matrix_by_posinf(c);
if( _spoil_scenario==11 )
spoil_matrix_by_neginf(c);
if( _spoil_scenario==12 )
spoil_matrix_by_deleting_row(c);
if( _spoil_scenario==13 )
spoil_matrix_by_deleting_col(c);
real_1d_array d = "[2,-2]";
if( _spoil_scenario==14 )
spoil_vector_by_nan(d);
if( _spoil_scenario==15 )
spoil_vector_by_posinf(d);
if( _spoil_scenario==16 )
spoil_vector_by_neginf(d);
if( _spoil_scenario==17 )
spoil_vector_by_deleting_element(d);
rmatrixsolvefast(c, 2, d);
_TestResult = _TestResult && doc_test_real_vector(d, "[1.0000, -1.0000]", 0.00005);
real_2d_array e = "[[3,4],[2,4]]";
if( _spoil_scenario==18 )
spoil_matrix_by_nan(e);
if( _spoil_scenario==19 )
spoil_matrix_by_posinf(e);
if( _spoil_scenario==20 )
spoil_matrix_by_neginf(e);
if( _spoil_scenario==21 )
spoil_matrix_by_deleting_row(e);
if( _spoil_scenario==22 )
spoil_matrix_by_deleting_col(e);
real_2d_array lue = "[[3,4],[2,4]]";
if( _spoil_scenario==23 )
spoil_matrix_by_nan(lue);
if( _spoil_scenario==24 )
spoil_matrix_by_posinf(lue);
if( _spoil_scenario==25 )
spoil_matrix_by_neginf(lue);
if( _spoil_scenario==26 )
spoil_matrix_by_deleting_row(lue);
if( _spoil_scenario==27 )
spoil_matrix_by_deleting_col(lue);
real_1d_array f = "[2,0]";
if( _spoil_scenario==28 )
spoil_vector_by_nan(f);
if( _spoil_scenario==29 )
spoil_vector_by_posinf(f);
if( _spoil_scenario==30 )
spoil_vector_by_neginf(f);
if( _spoil_scenario==31 )
spoil_vector_by_deleting_element(f);
rmatrixlu(lue, 2, 2, pivots);
rmatrixlusolve(lue, pivots, 2, f, x, rep);
_TestResult = _TestResult && doc_test_int(rep.terminationtype, 1);
_TestResult = _TestResult && doc_test_real_vector(x, "[2.0000, -1.0000]", 0.00005);
rmatrixmixedsolve(e, lue, pivots, 2, f, x, rep);
_TestResult = _TestResult && doc_test_int(rep.terminationtype, 1);
_TestResult = _TestResult && doc_test_real_vector(x, "[2.0000, -1.0000]", 0.00005);
real_1d_array f1 = "[2,0]";
if( _spoil_scenario==32 )
spoil_vector_by_nan(f1);
if( _spoil_scenario==33 )
spoil_vector_by_posinf(f1);
if( _spoil_scenario==34 )
spoil_vector_by_neginf(f1);
if( _spoil_scenario==35 )
spoil_vector_by_deleting_element(f1);
rmatrixlusolvefast(lue, pivots, 2, f1);
_TestResult = _TestResult && doc_test_int(rep.terminationtype, 1);
_TestResult = _TestResult && doc_test_real_vector(f1, "[2.0000, -1.0000]", 0.00005);
_TestResult = _TestResult && (_spoil_scenario==-1);
}
catch(ap_error)
{ _TestResult = _TestResult && (_spoil_scenario!=-1); }
}
if( !_TestResult)
{
printf("%-32s FAILED\n", "solve_real_tst");
fflush(stdout);
}
_TotalResult = _TotalResult && _TestResult;
//
// TEST solve_real_m_test
// .
//
_TestResult = true;
for(_spoil_scenario=-1; _spoil_scenario<43; _spoil_scenario++)
{
try
{
real_2d_array x;
integer_1d_array pivots;
densesolverreport rep;
real_2d_array a = "[[4,2],[-1,3]]";
if( _spoil_scenario==0 )
spoil_matrix_by_nan(a);
if( _spoil_scenario==1 )
spoil_matrix_by_posinf(a);
if( _spoil_scenario==2 )
spoil_matrix_by_neginf(a);
if( _spoil_scenario==3 )
spoil_matrix_by_deleting_row(a);
if( _spoil_scenario==4 )
spoil_matrix_by_deleting_col(a);
real_2d_array b = "[[8,10,4],[5,1,-1]]";
if( _spoil_scenario==5 )
spoil_matrix_by_nan(b);
if( _spoil_scenario==6 )
spoil_matrix_by_posinf(b);
if( _spoil_scenario==7 )
spoil_matrix_by_neginf(b);
if( _spoil_scenario==8 )
spoil_matrix_by_deleting_row(b);
if( _spoil_scenario==9 )
spoil_matrix_by_deleting_col(b);
rmatrixsolvem(a, 2, b, 3, true, x, rep);
_TestResult = _TestResult && doc_test_int(rep.terminationtype, 1);
_TestResult = _TestResult && doc_test_real_matrix(x, "[[1.0000, 2.0000,1.0000],[2.0000,1.0000,0.0000]]", 0.00005);
real_2d_array c = "[[3,1],[2,4]]";
if( _spoil_scenario==10 )
spoil_matrix_by_nan(c);
if( _spoil_scenario==11 )
spoil_matrix_by_posinf(c);
if( _spoil_scenario==12 )
spoil_matrix_by_neginf(c);
if( _spoil_scenario==13 )
spoil_matrix_by_deleting_row(c);
if( _spoil_scenario==14 )
spoil_matrix_by_deleting_col(c);
real_2d_array d = "[[2,1],[-2,4]]";
if( _spoil_scenario==15 )
spoil_matrix_by_nan(d);
if( _spoil_scenario==16 )
spoil_matrix_by_posinf(d);
if( _spoil_scenario==17 )
spoil_matrix_by_neginf(d);
if( _spoil_scenario==18 )
spoil_matrix_by_deleting_row(d);
if( _spoil_scenario==19 )
spoil_matrix_by_deleting_col(d);
rmatrixsolvemfast(c, 2, d, 2);
_TestResult = _TestResult && doc_test_real_matrix(d, "[[1.0000,0.0000],[-1.0000,1.0000]]", 0.00005);
real_2d_array e = "[[3,4],[2,4]]";
if( _spoil_scenario==20 )
spoil_matrix_by_nan(e);
if( _spoil_scenario==21 )
spoil_matrix_by_posinf(e);
if( _spoil_scenario==22 )
spoil_matrix_by_neginf(e);
if( _spoil_scenario==23 )
spoil_matrix_by_deleting_row(e);
if( _spoil_scenario==24 )
spoil_matrix_by_deleting_col(e);
real_2d_array lue = "[[3,4],[2,4]]";
if( _spoil_scenario==25 )
spoil_matrix_by_nan(lue);
if( _spoil_scenario==26 )
spoil_matrix_by_posinf(lue);
if( _spoil_scenario==27 )
spoil_matrix_by_neginf(lue);
if( _spoil_scenario==28 )
spoil_matrix_by_deleting_row(lue);
if( _spoil_scenario==29 )
spoil_matrix_by_deleting_col(lue);
real_2d_array f = "[[2,5],[0,6]]";
if( _spoil_scenario==30 )
spoil_matrix_by_nan(f);
if( _spoil_scenario==31 )
spoil_matrix_by_posinf(f);
if( _spoil_scenario==32 )
spoil_matrix_by_neginf(f);
if( _spoil_scenario==33 )
spoil_matrix_by_deleting_row(f);
if( _spoil_scenario==34 )
spoil_matrix_by_deleting_col(f);
rmatrixlu(lue, 2, 2, pivots);
rmatrixlusolvem(lue, pivots, 2, f, 2, x, rep);
_TestResult = _TestResult && doc_test_int(rep.terminationtype, 1);
_TestResult = _TestResult && doc_test_real_matrix(x, "[[2.0000,-1.0000],[-1.0000,2.0000]]", 0.00005);
rmatrixmixedsolvem(e, lue, pivots, 2, f, 2, x, rep);
_TestResult = _TestResult && doc_test_int(rep.terminationtype, 1);
_TestResult = _TestResult && doc_test_real_matrix(x, "[[2.0000,-1.0000],[-1.0000,2.0000]]", 0.00005);
real_2d_array f1 = "[[2,5],[0,6]]";
if( _spoil_scenario==35 )
spoil_matrix_by_nan(f1);
if( _spoil_scenario==36 )
spoil_matrix_by_posinf(f1);
if( _spoil_scenario==37 )
spoil_matrix_by_neginf(f1);
rmatrixlusolvemfast(lue, pivots, f1);
_TestResult = _TestResult && doc_test_real_matrix(f1, "[[2.0000,-1.0000],[-1.0000,2.0000]]", 0.00005);
real_2d_array f2 = "[[2,5],[0,6]]";
if( _spoil_scenario==38 )
spoil_matrix_by_nan(f2);
if( _spoil_scenario==39 )
spoil_matrix_by_posinf(f2);
if( _spoil_scenario==40 )
spoil_matrix_by_neginf(f2);
if( _spoil_scenario==41 )
spoil_matrix_by_deleting_row(f2);
if( _spoil_scenario==42 )
spoil_matrix_by_deleting_col(f2);
rmatrixlusolvemfast(lue, pivots, 2, f2, 2);
_TestResult = _TestResult && doc_test_real_matrix(f2, "[[2.0000,-1.0000],[-1.0000,2.0000]]", 0.00005);
_TestResult = _TestResult && (_spoil_scenario==-1);
}
catch(ap_error)
{ _TestResult = _TestResult && (_spoil_scenario!=-1); }
}
if( !_TestResult)
{
printf("%-32s FAILED\n", "solve_real_m_test");
fflush(stdout);
}
_TotalResult = _TotalResult && _TestResult;
//
// TEST lincg_d_1
// Solution of sparse linear systems with CG
//
_TestResult = true;
for(_spoil_scenario=-1; _spoil_scenario<4; _spoil_scenario++)
{
try
{
//
// This example illustrates solution of sparse linear systems with
// conjugate gradient method.
//
// Suppose that we have linear system A*x=b with sparse symmetric
// positive definite A (represented by sparsematrix object)
// [ 5 1 ]
// [ 1 7 2 ]
// A = [ 2 8 1 ]
// [ 1 4 1 ]
// [ 1 4 ]
// and right part b
// [ 7 ]
// [ 17 ]
// b = [ 14 ]
// [ 10 ]
// [ 6 ]
// and we want to solve this system using sparse linear CG. In order
// to do so, we have to create left part (sparsematrix object) and
// right part (dense array).
//
// Initially, sparse matrix is created in the Hash-Table format,
// which allows easy initialization, but do not allow matrix to be
// used in the linear solvers. So after construction you should convert
// sparse matrix to CRS format (one suited for linear operations).
//
// It is important to note that in our example we initialize full
// matrix A, both lower and upper triangles. However, it is symmetric
// and sparse solver needs just one half of the matrix. So you may
// save about half of the space by filling only one of the triangles.
//
sparsematrix a;
sparsecreate(5, 5, a);
sparseset(a, 0, 0, 5.0);
sparseset(a, 0, 1, 1.0);
sparseset(a, 1, 0, 1.0);
sparseset(a, 1, 1, 7.0);
sparseset(a, 1, 2, 2.0);
sparseset(a, 2, 1, 2.0);
sparseset(a, 2, 2, 8.0);
sparseset(a, 2, 3, 1.0);
sparseset(a, 3, 2, 1.0);
sparseset(a, 3, 3, 4.0);
sparseset(a, 3, 4, 1.0);
sparseset(a, 4, 3, 1.0);
sparseset(a, 4, 4, 4.0);
//
// Now our matrix is fully initialized, but we have to do one more
// step - convert it from Hash-Table format to CRS format (see
// documentation on sparse matrices for more information about these
// formats).
//
// If you omit this call, ALGLIB will generate exception on the first
// attempt to use A in linear operations.
//
sparseconverttocrs(a);
//
// Initialization of the right part
//
real_1d_array b = "[7,17,14,10,6]";
if( _spoil_scenario==0 )
spoil_vector_by_nan(b);
if( _spoil_scenario==1 )
spoil_vector_by_posinf(b);
if( _spoil_scenario==2 )
spoil_vector_by_neginf(b);
if( _spoil_scenario==3 )
spoil_vector_by_deleting_element(b);
//
// Now we have to create linear solver object and to use it for the
// solution of the linear system.
//
// NOTE: lincgsolvesparse() accepts additional parameter which tells
// what triangle of the symmetric matrix should be used - upper
// or lower. Because we've filled both parts of the matrix, we
// can use any part - upper or lower.
//
lincgstate s;
lincgreport rep;
real_1d_array x;
lincgcreate(5, s);
lincgsolvesparse(s, a, true, b);
lincgresults(s, x, rep);
_TestResult = _TestResult && doc_test_int(rep.terminationtype, 1);
_TestResult = _TestResult && doc_test_real_vector(x, "[1.000,2.000,1.000,2.000,1.000]", 0.005);
_TestResult = _TestResult && (_spoil_scenario==-1);
}
catch(ap_error)
{ _TestResult = _TestResult && (_spoil_scenario!=-1); }
}
if( !_TestResult)
{
printf("%-32s FAILED\n", "lincg_d_1");
fflush(stdout);
}
_TotalResult = _TotalResult && _TestResult;
//
// TEST linlsqr_d_1
// Solution of sparse linear systems with CG
//
_TestResult = true;
for(_spoil_scenario=-1; _spoil_scenario<4; _spoil_scenario++)
{
try
{
//
// This example illustrates solution of sparse linear least squares problem
// with LSQR algorithm.
//
// Suppose that we have least squares problem min|A*x-b| with sparse A
// represented by sparsematrix object
// [ 1 1 ]
// [ 1 1 ]
// A = [ 2 1 ]
// [ 1 ]
// [ 1 ]
// and right part b
// [ 4 ]
// [ 2 ]
// b = [ 4 ]
// [ 1 ]
// [ 2 ]
// and we want to solve this system in the least squares sense using
// LSQR algorithm. In order to do so, we have to create left part
// (sparsematrix object) and right part (dense array).
//
// Initially, sparse matrix is created in the Hash-Table format,
// which allows easy initialization, but do not allow matrix to be
// used in the linear solvers. So after construction you should convert
// sparse matrix to CRS format (one suited for linear operations).
//
sparsematrix a;
sparsecreate(5, 2, a);
sparseset(a, 0, 0, 1.0);
sparseset(a, 0, 1, 1.0);
sparseset(a, 1, 0, 1.0);
sparseset(a, 1, 1, 1.0);
sparseset(a, 2, 0, 2.0);
sparseset(a, 2, 1, 1.0);
sparseset(a, 3, 0, 1.0);
sparseset(a, 4, 1, 1.0);
//
// Now our matrix is fully initialized, but we have to do one more
// step - convert it from Hash-Table format to CRS format (see
// documentation on sparse matrices for more information about these
// formats).
//
// If you omit this call, ALGLIB will generate exception on the first
// attempt to use A in linear operations.
//
sparseconverttocrs(a);
//
// Initialization of the right part
//
real_1d_array b = "[4,2,4,1,2]";
if( _spoil_scenario==0 )
spoil_vector_by_nan(b);
if( _spoil_scenario==1 )
spoil_vector_by_posinf(b);
if( _spoil_scenario==2 )
spoil_vector_by_neginf(b);
if( _spoil_scenario==3 )
spoil_vector_by_deleting_element(b);
//
// Now we have to create linear solver object and to use it for the
// solution of the linear system.
//
linlsqrstate s;
linlsqrreport rep;
real_1d_array x;
linlsqrcreate(5, 2, s);
linlsqrsolvesparse(s, a, b);
linlsqrresults(s, x, rep);
_TestResult = _TestResult && doc_test_int(rep.terminationtype, 4);
_TestResult = _TestResult && doc_test_real_vector(x, "[1.000,2.000]", 0.005);
_TestResult = _TestResult && (_spoil_scenario==-1);
}
catch(ap_error)
{ _TestResult = _TestResult && (_spoil_scenario!=-1); }
}
if( !_TestResult)
{
printf("%-32s FAILED\n", "linlsqr_d_1");
fflush(stdout);
}
_TotalResult = _TotalResult && _TestResult;
//
// TEST solvesks_d_1
// Solving low profile positive definite sparse systems with Skyline (SKS) solver
//
_TestResult = true;
for(_spoil_scenario=-1; _spoil_scenario<4; _spoil_scenario++)
{
try
{
//
// This example demonstrates creation/initialization of the sparse matrix
// in the SKS (Skyline) storage format and solution using SKS-based direct
// solver.
//
// NOTE: the SKS solver is intended for 'easy' tasks, i.e. low-profile positive
// definite systems (e.g. matrices with average bandwidth as low as 3),
// where it can avoid some overhead associated with more powerful supernodal
// Cholesky solver with AMD ordering.
//
// It is recommended to use more powerful solvers for more difficult problems:
// * sparsespdsolve() for larger sparse positive definite systems
// * sparsesolve() for general (nonsymmetric) linear systems
//
// First, we have to create matrix and initialize it. Matrix is created
// in the SKS format, using fixed bandwidth initialization function.
// Several points should be noted:
//
// 1. SKS sparse storage format also allows variable bandwidth matrices;
// we just do not want to overcomplicate this example.
//
// 2. SKS format requires you to specify matrix geometry prior to
// initialization of its elements with sparseset(). If you specified
// bandwidth=1, you can not change your mind afterwards and call
// sparseset() for non-existent elements.
//
// 3. Because SKS solver need just one triangle of SPD matrix, we can
// omit initialization of the lower triangle of our matrix.
//
ae_int_t n = 4;
ae_int_t bandwidth = 1;
sparsematrix s;
sparsecreatesksband(n, n, bandwidth, s);
sparseset(s, 0, 0, 2.0);
sparseset(s, 0, 1, 1.0);
sparseset(s, 1, 1, 3.0);
sparseset(s, 1, 2, 1.0);
sparseset(s, 2, 2, 3.0);
sparseset(s, 2, 3, 1.0);
sparseset(s, 3, 3, 2.0);
//
// Now we have symmetric positive definite 4x4 system width bandwidth=1:
//
// [ 2 1 ] [ x0]] [ 4 ]
// [ 1 3 1 ] [ x1 ] [ 10 ]
// [ 1 3 1 ] * [ x2 ] = [ 15 ]
// [ 1 2 ] [ x3 ] [ 11 ]
//
// After successful creation we can call SKS solver.
//
real_1d_array b = "[4,10,15,11]";
if( _spoil_scenario==0 )
spoil_vector_by_nan(b);
if( _spoil_scenario==1 )
spoil_vector_by_posinf(b);
if( _spoil_scenario==2 )
spoil_vector_by_neginf(b);
if( _spoil_scenario==3 )
spoil_vector_by_deleting_element(b);
sparsesolverreport rep;
real_1d_array x;
bool isuppertriangle = true;
sparsespdsolvesks(s, isuppertriangle, b, x, rep);
_TestResult = _TestResult && doc_test_real_vector(x, "[1.0000, 2.0000, 3.0000, 4.0000]", 0.00005);
_TestResult = _TestResult && (_spoil_scenario==-1);
}
catch(ap_error)
{ _TestResult = _TestResult && (_spoil_scenario!=-1); }
}
if( !_TestResult)
{
printf("%-32s FAILED\n", "solvesks_d_1");
fflush(stdout);
}
_TotalResult = _TotalResult && _TestResult;
//
// TEST sparse_solve_cholesky
// Solving positive definite sparse linear systems with the supernodal Cholesky solver
//
_TestResult = true;
for(_spoil_scenario=-1; _spoil_scenario<4; _spoil_scenario++)
{
try
{
//
// This example demonstrates creation/initialization of a sparse matrix and linear
// system solution using Cholesky-based direct solver. This solver can handle any
// problem sizes - from several tens of variables to millions of variables.
//
// First, we create a sparse matrix in the flexible hash table-based storage format,
// initialize it and convert to the CRS format. Because the matrix is symmetric,
// it is enough to specify only one triangle. The example below initializes the
// lower one.
//
ae_int_t n = 4;
sparsematrix s;
sparsecreate(n, n, 0, s);
sparseset(s, 0, 0, 2.0);
sparseset(s, 1, 0, 1.0);
sparseset(s, 1, 1, 3.0);
sparseset(s, 2, 1, 1.0);
sparseset(s, 2, 2, 3.0);
sparseset(s, 3, 2, 1.0);
sparseset(s, 3, 3, 2.0);
//
// Now we have symmetric positive definite 4x4 system
//
// [ 2 1 ] [ x0]] [ 4 ]
// [ 1 3 1 ] [ x1 ] [ 10 ]
// [ 1 3 1 ] * [ x2 ] = [ 15 ]
// [ 1 2 ] [ x3 ] [ 11 ]
//
// Now, it is time to call the solver.
//
real_1d_array b = "[4,10,15,11]";
if( _spoil_scenario==0 )
spoil_vector_by_nan(b);
if( _spoil_scenario==1 )
spoil_vector_by_posinf(b);
if( _spoil_scenario==2 )
spoil_vector_by_neginf(b);
if( _spoil_scenario==3 )
spoil_vector_by_deleting_element(b);
sparsesolverreport rep;
real_1d_array x;
bool isuppertriangle = false;
sparsespdsolve(s, isuppertriangle, b, x, rep);
_TestResult = _TestResult && doc_test_real_vector(x, "[1.0000, 2.0000, 3.0000, 4.0000]", 0.00005);
_TestResult = _TestResult && (_spoil_scenario==-1);
}
catch(ap_error)
{ _TestResult = _TestResult && (_spoil_scenario!=-1); }
}
if( !_TestResult)
{
printf("%-32s FAILED\n", "sparse_solve_cholesky");
fflush(stdout);
}
_TotalResult = _TotalResult && _TestResult;
//
// TEST sparse_solve
// Solving general sparse linear systems
//
_TestResult = true;
for(_spoil_scenario=-1; _spoil_scenario<4; _spoil_scenario++)
{
try
{
//
// This example demonstrates creation/initialization of a sparse matrix and linear
// system solution using a direct solver. This solver can handle any problem sizes
// - from several tens of variables to millions of variables.
//
// First, we create a sparse matrix in the flexible hash table-based storage format,
// initialize it and convert to the CRS format.
//
ae_int_t n = 4;
sparsematrix s;
sparsecreate(n, n, 0, s);
sparseset(s, 0, 0, 2.0);
sparseset(s, 0, 1, 1.0);
sparseset(s, 1, 0, 1.0);
sparseset(s, 1, 1, 3.0);
sparseset(s, 1, 2, -1.0);
sparseset(s, 2, 2, 3.0);
sparseset(s, 2, 3, 1.0);
sparseset(s, 3, 2, 1.0);
sparseset(s, 3, 3, 2.0);
//
// Now we have symmetric positive definite 4x4 system
//
// [ 2 1 ] [ x0]] [ 3 ]
// [ 1 3 -1 ] [ x1 ] [ 2 ]
// [ 3 1 ] * [ x2 ] = [ 8 ]
// [ 1 2 ] [ x3 ] [ 6 ]
//
// Now, it is time to call the solver. The sparsesolve() function supports several
// solvers, our recommendation is to choose the default one. In the current version
// it is a supernodal solver with static pivoting, followed by the iterative refinement.
//
real_1d_array b = "[3,2,8,6]";
if( _spoil_scenario==0 )
spoil_vector_by_nan(b);
if( _spoil_scenario==1 )
spoil_vector_by_posinf(b);
if( _spoil_scenario==2 )
spoil_vector_by_neginf(b);
if( _spoil_scenario==3 )
spoil_vector_by_deleting_element(b);
sparsesolverreport rep;
real_1d_array x;
ae_int_t solvertype = 0;
sparsesolve(s, b, solvertype, x, rep);
_TestResult = _TestResult && doc_test_real_vector(x, "[1.0000, 1.0000, 2.0000, 2.0000]", 0.00005);
_TestResult = _TestResult && (_spoil_scenario==-1);
}
catch(ap_error)
{ _TestResult = _TestResult && (_spoil_scenario!=-1); }
}
if( !_TestResult)
{
printf("%-32s FAILED\n", "sparse_solve");
fflush(stdout);
}
_TotalResult = _TotalResult && _TestResult;
//
// TEST matinv_d_r1
// Real matrix inverse
//
_TestResult = true;
for(_spoil_scenario=-1; _spoil_scenario<7; _spoil_scenario++)
{
try
{
real_2d_array a = "[[1,-1],[1,1]]";
if( _spoil_scenario==0 )
spoil_matrix_by_nan(a);
if( _spoil_scenario==1 )
spoil_matrix_by_posinf(a);
if( _spoil_scenario==2 )
spoil_matrix_by_neginf(a);
if( _spoil_scenario==3 )
spoil_matrix_by_adding_row(a);
if( _spoil_scenario==4 )
spoil_matrix_by_adding_col(a);
if( _spoil_scenario==5 )
spoil_matrix_by_deleting_row(a);
if( _spoil_scenario==6 )
spoil_matrix_by_deleting_col(a);
matinvreport rep;
rmatrixinverse(a, rep);
_TestResult = _TestResult && doc_test_int(rep.terminationtype, 1);
_TestResult = _TestResult && doc_test_real_matrix(a, "[[0.5,0.5],[-0.5,0.5]]", 0.00005);
_TestResult = _TestResult && doc_test_real(rep.r1, 0.5, 0.00005);
_TestResult = _TestResult && doc_test_real(rep.rinf, 0.5, 0.00005);
_TestResult = _TestResult && (_spoil_scenario==-1);
}
catch(ap_error)
{ _TestResult = _TestResult && (_spoil_scenario!=-1); }
}
if( !_TestResult)
{
printf("%-32s FAILED\n", "matinv_d_r1");
fflush(stdout);
}
_TotalResult = _TotalResult && _TestResult;
//
// TEST matinv_d_c1
// Complex matrix inverse
//
_TestResult = true;
for(_spoil_scenario=-1; _spoil_scenario<7; _spoil_scenario++)
{
try
{
complex_2d_array a = "[[1i,-1],[1i,1]]";
if( _spoil_scenario==0 )
spoil_matrix_by_nan(a);
if( _spoil_scenario==1 )
spoil_matrix_by_posinf(a);
if( _spoil_scenario==2 )
spoil_matrix_by_neginf(a);
if( _spoil_scenario==3 )
spoil_matrix_by_adding_row(a);
if( _spoil_scenario==4 )
spoil_matrix_by_adding_col(a);
if( _spoil_scenario==5 )
spoil_matrix_by_deleting_row(a);
if( _spoil_scenario==6 )
spoil_matrix_by_deleting_col(a);
matinvreport rep;
cmatrixinverse(a, rep);
_TestResult = _TestResult && doc_test_int(rep.terminationtype, 1);
_TestResult = _TestResult && doc_test_complex_matrix(a, "[[-0.5i,-0.5i],[-0.5,0.5]]", 0.00005);
_TestResult = _TestResult && doc_test_real(rep.r1, 0.5, 0.00005);
_TestResult = _TestResult && doc_test_real(rep.rinf, 0.5, 0.00005);
_TestResult = _TestResult && (_spoil_scenario==-1);
}
catch(ap_error)
{ _TestResult = _TestResult && (_spoil_scenario!=-1); }
}
if( !_TestResult)
{
printf("%-32s FAILED\n", "matinv_d_c1");
fflush(stdout);
}
_TotalResult = _TotalResult && _TestResult;
//
// TEST matinv_d_spd1
// SPD matrix inverse
//
_TestResult = true;
for(_spoil_scenario=-1; _spoil_scenario<4; _spoil_scenario++)
{
try
{
real_2d_array a = "[[2,1],[1,2]]";
if( _spoil_scenario==0 )
spoil_matrix_by_adding_row(a);
if( _spoil_scenario==1 )
spoil_matrix_by_adding_col(a);
if( _spoil_scenario==2 )
spoil_matrix_by_deleting_row(a);
if( _spoil_scenario==3 )
spoil_matrix_by_deleting_col(a);
matinvreport rep;
//
// The matrix is given by its upper and lower triangles
//
// [ 2 1 ]
// [ 1 2 ]
//
// However, spdmatrixinverse() accepts and modifies only one triangle - either
// the upper or the lower one. The other triangle is left untouched. In this example
// we modify the lower triangle. Thus, the inverse matrix is
//
// [ 2/3 -1/3 ]
// [ -1/3 2/3 ]
//
// but only lower triangle is returned, and the upper triangle is not modified:
//
// [ 2/3 1 ]
// [ -1/3 2/3 ]
//
//
bool isupper = false;
spdmatrixinverse(a, isupper, rep);
_TestResult = _TestResult && doc_test_int(rep.terminationtype, 1);
_TestResult = _TestResult && doc_test_real_matrix(a, "[[0.666666,1],[-0.333333,0.666666]]", 0.00005);
_TestResult = _TestResult && (_spoil_scenario==-1);
}
catch(ap_error)
{ _TestResult = _TestResult && (_spoil_scenario!=-1); }
}
if( !_TestResult)
{
printf("%-32s FAILED\n", "matinv_d_spd1");
fflush(stdout);
}
_TotalResult = _TotalResult && _TestResult;
//
// TEST matinv_d_hpd1
// HPD matrix inverse
//
_TestResult = true;
for(_spoil_scenario=-1; _spoil_scenario<4; _spoil_scenario++)
{
try
{
complex_2d_array a = "[[2,1],[1,2]]";
if( _spoil_scenario==0 )
spoil_matrix_by_adding_row(a);
if( _spoil_scenario==1 )
spoil_matrix_by_adding_col(a);
if( _spoil_scenario==2 )
spoil_matrix_by_deleting_row(a);
if( _spoil_scenario==3 )
spoil_matrix_by_deleting_col(a);
matinvreport rep;
//
// The matrix is given by its upper and lower triangles
//
// [ 2 1 ]
// [ 1 2 ]
//
// However, hpdmatrixinverse() accepts and modifies only one triangle - either
// the upper or the lower one. The other triangle is left untouched. In this example
// we modify the lower triangle. Thus, the inverse matrix is
//
// [ 2/3 -1/3 ]
// [ -1/3 2/3 ]
//
// but only lower triangle is returned, and the upper triangle is not modified:
//
// [ 2/3 1 ]
// [ -1/3 2/3 ]
//
//
bool isupper = false;
hpdmatrixinverse(a, isupper, rep);
_TestResult = _TestResult && doc_test_int(rep.terminationtype, 1);
_TestResult = _TestResult && doc_test_complex_matrix(a, "[[0.666666,1],[-0.333333,0.666666]]", 0.00005);
_TestResult = _TestResult && (_spoil_scenario==-1);
}
catch(ap_error)
{ _TestResult = _TestResult && (_spoil_scenario!=-1); }
}
if( !_TestResult)
{
printf("%-32s FAILED\n", "matinv_d_hpd1");
fflush(stdout);
}
_TotalResult = _TotalResult && _TestResult;
//
// TEST matinv_t_r1
// Real matrix inverse: singular matrix
//
_TestResult = true;
try
{
real_2d_array a = "[[1,-1],[-2,2]]";
matinvreport rep;
rmatrixinverse(a, rep);
_TestResult = _TestResult && doc_test_int(rep.terminationtype, -3);
_TestResult = _TestResult && doc_test_real(rep.r1, 0.0, 0.00005);
_TestResult = _TestResult && doc_test_real(rep.rinf, 0.0, 0.00005);
}
catch(ap_error)
{ _TestResult = false; }
if( !_TestResult)
{
printf("%-32s FAILED\n", "matinv_t_r1");
fflush(stdout);
}
_TotalResult = _TotalResult && _TestResult;
//
// TEST matinv_t_c1
// Complex matrix inverse: singular matrix
//
_TestResult = true;
try
{
complex_2d_array a = "[[1i,-1i],[-2,2]]";
matinvreport rep;
cmatrixinverse(a, rep);
_TestResult = _TestResult && doc_test_int(rep.terminationtype, -3);
_TestResult = _TestResult && doc_test_real(rep.r1, 0.0, 0.00005);
_TestResult = _TestResult && doc_test_real(rep.rinf, 0.0, 0.00005);
}
catch(ap_error)
{ _TestResult = false; }
if( !_TestResult)
{
printf("%-32s FAILED\n", "matinv_t_c1");
fflush(stdout);
}
_TotalResult = _TotalResult && _TestResult;
//
// TEST minlbfgs_d_1
// Nonlinear optimization by L-BFGS
//
_TestResult = true;
for(_spoil_scenario=-1; _spoil_scenario<15; _spoil_scenario++)
{
try
{
//
// This example demonstrates minimization of
//
// f(x,y) = 100*(x+3)^4+(y-3)^4
//
// using LBFGS method, with:
// * initial point x=[0,0]
// * unit scale being set for all variables (see minlbfgssetscale for more info)
// * stopping criteria set to "terminate after short enough step"
//
// First, we create optimizer object and tune its properties.
//
// IMPORTANT: the LBFGS optimizer supports parallel parallel numerical
// differentiation ('callback parallelism'). This feature,
// which is present in commercial ALGLIB editions greatly
// accelerates optimization with numerical differentiation of
// an expensive target functions.
//
// Callback parallelism is usually beneficial when computing a
// numerical gradient requires more than several milliseconds.
// This particular example, of course, is not suited for
// callback parallelism.
//
// See ALGLIB Reference Manual, 'Working with commercial version'
// section, and comments on minlbfgsoptimize() function for
// more information.
//
real_1d_array x = "[0,0]";
if( _spoil_scenario==0 )
spoil_vector_by_nan(x);
if( _spoil_scenario==1 )
spoil_vector_by_posinf(x);
if( _spoil_scenario==2 )
spoil_vector_by_neginf(x);
real_1d_array s = "[1,1]";
if( _spoil_scenario==3 )
spoil_vector_by_nan(s);
if( _spoil_scenario==4 )
spoil_vector_by_posinf(s);
if( _spoil_scenario==5 )
spoil_vector_by_neginf(s);
double epsg = 0;
if( _spoil_scenario==6 )
epsg = fp_nan;
if( _spoil_scenario==7 )
epsg = fp_posinf;
if( _spoil_scenario==8 )
epsg = fp_neginf;
double epsf = 0;
if( _spoil_scenario==9 )
epsf = fp_nan;
if( _spoil_scenario==10 )
epsf = fp_posinf;
if( _spoil_scenario==11 )
epsf = fp_neginf;
double epsx = 0.0000000001;
if( _spoil_scenario==12 )
epsx = fp_nan;
if( _spoil_scenario==13 )
epsx = fp_posinf;
if( _spoil_scenario==14 )
epsx = fp_neginf;
ae_int_t maxits = 0;
minlbfgsstate state;
minlbfgscreate(1, x, state);
minlbfgssetcond(state, epsg, epsf, epsx, maxits);
minlbfgssetscale(state, s);
//
// Optimize and examine results.
//
minlbfgsreport rep;
alglib::minlbfgsoptimize(state, function1_grad);
minlbfgsresults(state, x, rep);
_TestResult = _TestResult && doc_test_real_vector(x, "[-3,3]", 0.005);
_TestResult = _TestResult && (_spoil_scenario==-1);
}
catch(ap_error)
{ _TestResult = _TestResult && (_spoil_scenario!=-1); }
}
if( !_TestResult)
{
printf("%-32s FAILED\n", "minlbfgs_d_1");
fflush(stdout);
}
_TotalResult = _TotalResult && _TestResult;
//
// TEST minlbfgs_d_2
// Nonlinear optimization with additional settings and restarts
//
_TestResult = true;
for(_spoil_scenario=-1; _spoil_scenario<21; _spoil_scenario++)
{
try
{
//
// This example demonstrates minimization of f(x,y) = 100*(x+3)^4+(y-3)^4
// using LBFGS method.
//
// Several advanced techniques are demonstrated:
// * upper limit on step size
// * restart from new point
//
real_1d_array x = "[0,0]";
if( _spoil_scenario==0 )
spoil_vector_by_nan(x);
if( _spoil_scenario==1 )
spoil_vector_by_posinf(x);
if( _spoil_scenario==2 )
spoil_vector_by_neginf(x);
real_1d_array s = "[1,1]";
if( _spoil_scenario==3 )
spoil_vector_by_nan(s);
if( _spoil_scenario==4 )
spoil_vector_by_posinf(s);
if( _spoil_scenario==5 )
spoil_vector_by_neginf(s);
double epsg = 0;
if( _spoil_scenario==6 )
epsg = fp_nan;
if( _spoil_scenario==7 )
epsg = fp_posinf;
if( _spoil_scenario==8 )
epsg = fp_neginf;
double epsf = 0;
if( _spoil_scenario==9 )
epsf = fp_nan;
if( _spoil_scenario==10 )
epsf = fp_posinf;
if( _spoil_scenario==11 )
epsf = fp_neginf;
double epsx = 0.0000000001;
if( _spoil_scenario==12 )
epsx = fp_nan;
if( _spoil_scenario==13 )
epsx = fp_posinf;
if( _spoil_scenario==14 )
epsx = fp_neginf;
double stpmax = 0.1;
if( _spoil_scenario==15 )
stpmax = fp_nan;
if( _spoil_scenario==16 )
stpmax = fp_posinf;
if( _spoil_scenario==17 )
stpmax = fp_neginf;
ae_int_t maxits = 0;
minlbfgsstate state;
minlbfgsreport rep;
// create and tune optimizer
minlbfgscreate(1, x, state);
minlbfgssetcond(state, epsg, epsf, epsx, maxits);
minlbfgssetstpmax(state, stpmax);
minlbfgssetscale(state, s);
// Set up OptGuard integrity checker which catches errors
// like nonsmooth targets or errors in the analytic gradient.
//
// OptGuard is essential at the early prototyping stages.
//
// NOTE: gradient verification needs 3*N additional function
// evaluations; DO NOT USE IT IN THE PRODUCTION CODE
// because it leads to unnecessary slowdown of your app.
minlbfgsoptguardsmoothness(state);
minlbfgsoptguardgradient(state, 0.001);
// first run
alglib::minlbfgsoptimize(state, function1_grad);
minlbfgsresults(state, x, rep);
_TestResult = _TestResult && doc_test_real_vector(x, "[-3,3]", 0.005);
// second run - algorithm is restarted
x = "[10,10]";
if( _spoil_scenario==18 )
spoil_vector_by_nan(x);
if( _spoil_scenario==19 )
spoil_vector_by_posinf(x);
if( _spoil_scenario==20 )
spoil_vector_by_neginf(x);
minlbfgsrestartfrom(state, x);
alglib::minlbfgsoptimize(state, function1_grad);
minlbfgsresults(state, x, rep);
_TestResult = _TestResult && doc_test_real_vector(x, "[-3,3]", 0.005);
// check OptGuard integrity report. Why do we need it at all?
// Well, try breaking the gradient by adding 1.0 to some
// of its components - OptGuard should report it as error.
// And it may also catch unintended errors too :)
optguardreport ogrep;
minlbfgsoptguardresults(state, ogrep);
_TestResult = _TestResult && doc_test_bool(ogrep.badgradsuspected, false);
_TestResult = _TestResult && doc_test_bool(ogrep.nonc0suspected, false);
_TestResult = _TestResult && doc_test_bool(ogrep.nonc1suspected, false);
_TestResult = _TestResult && (_spoil_scenario==-1);
}
catch(ap_error)
{ _TestResult = _TestResult && (_spoil_scenario!=-1); }
}
if( !_TestResult)
{
printf("%-32s FAILED\n", "minlbfgs_d_2");
fflush(stdout);
}
_TotalResult = _TotalResult && _TestResult;
//
// TEST minlbfgs_numdiff
// Nonlinear optimization by L-BFGS with numerical differentiation
//
_TestResult = true;
for(_spoil_scenario=-1; _spoil_scenario<15; _spoil_scenario++)
{
try
{
//
// This example demonstrates minimization of
//
// f(x,y) = 100*(x+3)^4+(y-3)^4
//
// using numerical differentiation to calculate gradient.
//
// IMPORTANT: the LBFGS optimizer supports parallel parallel numerical
// differentiation ('callback parallelism'). This feature,
// which is present in commercial ALGLIB editions greatly
// accelerates optimization with numerical differentiation of
// an expensive target functions.
//
// Callback parallelism is usually beneficial when computing a
// numerical gradient requires more than several milliseconds.
// This particular example, of course, is not suited for
// callback parallelism.
//
// See ALGLIB Reference Manual, 'Working with commercial version'
// section, and comments on minlbfgsoptimize() function for
// more information.
//
real_1d_array x = "[0,0]";
if( _spoil_scenario==0 )
spoil_vector_by_nan(x);
if( _spoil_scenario==1 )
spoil_vector_by_posinf(x);
if( _spoil_scenario==2 )
spoil_vector_by_neginf(x);
double epsg = 0.0000000001;
if( _spoil_scenario==3 )
epsg = fp_nan;
if( _spoil_scenario==4 )
epsg = fp_posinf;
if( _spoil_scenario==5 )
epsg = fp_neginf;
double epsf = 0;
if( _spoil_scenario==6 )
epsf = fp_nan;
if( _spoil_scenario==7 )
epsf = fp_posinf;
if( _spoil_scenario==8 )
epsf = fp_neginf;
double epsx = 0;
if( _spoil_scenario==9 )
epsx = fp_nan;
if( _spoil_scenario==10 )
epsx = fp_posinf;
if( _spoil_scenario==11 )
epsx = fp_neginf;
double diffstep = 1.0e-6;
if( _spoil_scenario==12 )
diffstep = fp_nan;
if( _spoil_scenario==13 )
diffstep = fp_posinf;
if( _spoil_scenario==14 )
diffstep = fp_neginf;
ae_int_t maxits = 0;
minlbfgsstate state;
minlbfgsreport rep;
minlbfgscreatef(1, x, diffstep, state);
minlbfgssetcond(state, epsg, epsf, epsx, maxits);
alglib::minlbfgsoptimize(state, function1_func);
minlbfgsresults(state, x, rep);
_TestResult = _TestResult && doc_test_int(rep.terminationtype, 4);
_TestResult = _TestResult && doc_test_real_vector(x, "[-3,3]", 0.005);
_TestResult = _TestResult && (_spoil_scenario==-1);
}
catch(ap_error)
{ _TestResult = _TestResult && (_spoil_scenario!=-1); }
}
if( !_TestResult)
{
printf("%-32s FAILED\n", "minlbfgs_numdiff");
fflush(stdout);
}
_TotalResult = _TotalResult && _TestResult;
//
// TEST minlbfgs_t_1
// Test buffered results which use shared convention for one of its parameters
//
_TestResult = true;
for(_spoil_scenario=-1; _spoil_scenario<15; _spoil_scenario++)
{
try
{
real_1d_array x = "[0,0]";
if( _spoil_scenario==0 )
spoil_vector_by_nan(x);
if( _spoil_scenario==1 )
spoil_vector_by_posinf(x);
if( _spoil_scenario==2 )
spoil_vector_by_neginf(x);
real_1d_array s = "[1,1]";
if( _spoil_scenario==3 )
spoil_vector_by_nan(s);
if( _spoil_scenario==4 )
spoil_vector_by_posinf(s);
if( _spoil_scenario==5 )
spoil_vector_by_neginf(s);
double epsg = 0;
if( _spoil_scenario==6 )
epsg = fp_nan;
if( _spoil_scenario==7 )
epsg = fp_posinf;
if( _spoil_scenario==8 )
epsg = fp_neginf;
double epsf = 0;
if( _spoil_scenario==9 )
epsf = fp_nan;
if( _spoil_scenario==10 )
epsf = fp_posinf;
if( _spoil_scenario==11 )
epsf = fp_neginf;
double epsx = 0.0000000001;
if( _spoil_scenario==12 )
epsx = fp_nan;
if( _spoil_scenario==13 )
epsx = fp_posinf;
if( _spoil_scenario==14 )
epsx = fp_neginf;
ae_int_t maxits = 0;
minlbfgsstate state;
minlbfgscreate(1, x, state);
minlbfgssetcond(state, epsg, epsf, epsx, maxits);
minlbfgssetscale(state, s);
minlbfgsreport rep;
alglib::minlbfgsoptimize(state, function1_grad);
minlbfgsresults(state, x, rep);
minlbfgsresultsbuf(state, x, rep);
_TestResult = _TestResult && doc_test_real_vector(x, "[-3,3]", 0.005);
_TestResult = _TestResult && (_spoil_scenario==-1);
}
catch(ap_error)
{ _TestResult = _TestResult && (_spoil_scenario!=-1); }
}
if( !_TestResult)
{
printf("%-32s FAILED\n", "minlbfgs_t_1");
fflush(stdout);
}
_TotalResult = _TotalResult && _TestResult;
//
// TEST minbleic_d_1
// Nonlinear optimization with bound constraints
//
_TestResult = true;
for(_spoil_scenario=-1; _spoil_scenario<20; _spoil_scenario++)
{
try
{
//
// This example demonstrates minimization of
//
// f(x,y) = 100*(x+3)^4+(y-3)^4
//
// subject to box constraints
//
// -1<=x<=+1, -1<=y<=+1
//
// using BLEIC optimizer with:
// * initial point x=[0,0]
// * unit scale being set for all variables (see minbleicsetscale for more info)
// * stopping criteria set to "terminate after short enough step"
// * OptGuard integrity check being used to check problem statement
// for some common errors like nonsmoothness or bad analytic gradient
//
// First, we create optimizer object and tune its properties:
// * set box constraints
// * set variable scales
// * set stopping criteria
//
real_1d_array x = "[0,0]";
if( _spoil_scenario==0 )
spoil_vector_by_nan(x);
if( _spoil_scenario==1 )
spoil_vector_by_posinf(x);
if( _spoil_scenario==2 )
spoil_vector_by_neginf(x);
real_1d_array s = "[1,1]";
if( _spoil_scenario==3 )
spoil_vector_by_nan(s);
if( _spoil_scenario==4 )
spoil_vector_by_posinf(s);
if( _spoil_scenario==5 )
spoil_vector_by_neginf(s);
if( _spoil_scenario==6 )
spoil_vector_by_deleting_element(s);
real_1d_array bndl = "[-1,-1]";
if( _spoil_scenario==7 )
spoil_vector_by_nan(bndl);
if( _spoil_scenario==8 )
spoil_vector_by_deleting_element(bndl);
real_1d_array bndu = "[+1,+1]";
if( _spoil_scenario==9 )
spoil_vector_by_nan(bndu);
if( _spoil_scenario==10 )
spoil_vector_by_deleting_element(bndu);
double epsg = 0;
if( _spoil_scenario==11 )
epsg = fp_nan;
if( _spoil_scenario==12 )
epsg = fp_posinf;
if( _spoil_scenario==13 )
epsg = fp_neginf;
double epsf = 0;
if( _spoil_scenario==14 )
epsf = fp_nan;
if( _spoil_scenario==15 )
epsf = fp_posinf;
if( _spoil_scenario==16 )
epsf = fp_neginf;
double epsx = 0.000001;
if( _spoil_scenario==17 )
epsx = fp_nan;
if( _spoil_scenario==18 )
epsx = fp_posinf;
if( _spoil_scenario==19 )
epsx = fp_neginf;
ae_int_t maxits = 0;
minbleicstate state;
minbleiccreate(x, state);
minbleicsetbc(state, bndl, bndu);
minbleicsetscale(state, s);
minbleicsetcond(state, epsg, epsf, epsx, maxits);
//
// Then we activate OptGuard integrity checking.
//
// OptGuard monitor helps to catch common coding and problem statement
// issues, like:
// * discontinuity of the target function (C0 continuity violation)
// * nonsmoothness of the target function (C1 continuity violation)
// * erroneous analytic gradient, i.e. one inconsistent with actual
// change in the target/constraints
//
// OptGuard is essential for early prototyping stages because such
// problems often result in premature termination of the optimizer
// which is really hard to distinguish from the correct termination.
//
// IMPORTANT: GRADIENT VERIFICATION IS PERFORMED BY MEANS OF NUMERICAL
// DIFFERENTIATION. DO NOT USE IT IN PRODUCTION CODE!!!!!!!
//
// Other OptGuard checks add moderate overhead, but anyway
// it is better to turn them off when they are not needed.
//
minbleicoptguardsmoothness(state);
minbleicoptguardgradient(state, 0.001);
//
// Optimize and evaluate results
//
minbleicreport rep;
alglib::minbleicoptimize(state, function1_grad);
minbleicresults(state, x, rep);
_TestResult = _TestResult && doc_test_int(rep.terminationtype, 4);
_TestResult = _TestResult && doc_test_real_vector(x, "[-1,1]", 0.005);
//
// Check that OptGuard did not report errors
//
// NOTE: want to test OptGuard? Try breaking the gradient - say, add
// 1.0 to some of its components.
//
optguardreport ogrep;
minbleicoptguardresults(state, ogrep);
_TestResult = _TestResult && doc_test_bool(ogrep.badgradsuspected, false);
_TestResult = _TestResult && doc_test_bool(ogrep.nonc0suspected, false);
_TestResult = _TestResult && doc_test_bool(ogrep.nonc1suspected, false);
_TestResult = _TestResult && (_spoil_scenario==-1);
}
catch(ap_error)
{ _TestResult = _TestResult && (_spoil_scenario!=-1); }
}
if( !_TestResult)
{
printf("%-32s FAILED\n", "minbleic_d_1");
fflush(stdout);
}
_TotalResult = _TotalResult && _TestResult;
//
// TEST minbleic_d_2
// Nonlinear optimization with linear inequality constraints
//
_TestResult = true;
for(_spoil_scenario=-1; _spoil_scenario<22; _spoil_scenario++)
{
try
{
//
// This example demonstrates minimization of
//
// f(x,y) = 100*(x+3)^4+(y-3)^4
//
// subject to inequality constraints
//
// * x>=2 (posed as general linear constraint),
// * x+y>=6
//
// using BLEIC optimizer with
// * initial point x=[0,0]
// * unit scale being set for all variables (see minbleicsetscale for more info)
// * stopping criteria set to "terminate after short enough step"
// * OptGuard integrity check being used to check problem statement
// for some common errors like nonsmoothness or bad analytic gradient
//
// First, we create optimizer object and tune its properties:
// * set linear constraints
// * set variable scales
// * set stopping criteria
//
real_1d_array x = "[5,5]";
if( _spoil_scenario==0 )
spoil_vector_by_nan(x);
if( _spoil_scenario==1 )
spoil_vector_by_posinf(x);
if( _spoil_scenario==2 )
spoil_vector_by_neginf(x);
real_1d_array s = "[1,1]";
if( _spoil_scenario==3 )
spoil_vector_by_nan(s);
if( _spoil_scenario==4 )
spoil_vector_by_posinf(s);
if( _spoil_scenario==5 )
spoil_vector_by_neginf(s);
if( _spoil_scenario==6 )
spoil_vector_by_deleting_element(s);
real_2d_array c = "[[1,0,2],[1,1,6]]";
if( _spoil_scenario==7 )
spoil_matrix_by_nan(c);
if( _spoil_scenario==8 )
spoil_matrix_by_posinf(c);
if( _spoil_scenario==9 )
spoil_matrix_by_neginf(c);
if( _spoil_scenario==10 )
spoil_matrix_by_deleting_row(c);
if( _spoil_scenario==11 )
spoil_matrix_by_deleting_col(c);
integer_1d_array ct = "[1,1]";
if( _spoil_scenario==12 )
spoil_vector_by_deleting_element(ct);
minbleicstate state;
double epsg = 0;
if( _spoil_scenario==13 )
epsg = fp_nan;
if( _spoil_scenario==14 )
epsg = fp_posinf;
if( _spoil_scenario==15 )
epsg = fp_neginf;
double epsf = 0;
if( _spoil_scenario==16 )
epsf = fp_nan;
if( _spoil_scenario==17 )
epsf = fp_posinf;
if( _spoil_scenario==18 )
epsf = fp_neginf;
double epsx = 0.000001;
if( _spoil_scenario==19 )
epsx = fp_nan;
if( _spoil_scenario==20 )
epsx = fp_posinf;
if( _spoil_scenario==21 )
epsx = fp_neginf;
ae_int_t maxits = 0;
minbleiccreate(x, state);
minbleicsetlc(state, c, ct);
minbleicsetscale(state, s);
minbleicsetcond(state, epsg, epsf, epsx, maxits);
//
// Then we activate OptGuard integrity checking.
//
// OptGuard monitor helps to catch common coding and problem statement
// issues, like:
// * discontinuity of the target function (C0 continuity violation)
// * nonsmoothness of the target function (C1 continuity violation)
// * erroneous analytic gradient, i.e. one inconsistent with actual
// change in the target/constraints
//
// OptGuard is essential for early prototyping stages because such
// problems often result in premature termination of the optimizer
// which is really hard to distinguish from the correct termination.
//
// IMPORTANT: GRADIENT VERIFICATION IS PERFORMED BY MEANS OF NUMERICAL
// DIFFERENTIATION. DO NOT USE IT IN PRODUCTION CODE!!!!!!!
//
// Other OptGuard checks add moderate overhead, but anyway
// it is better to turn them off when they are not needed.
//
minbleicoptguardsmoothness(state);
minbleicoptguardgradient(state, 0.001);
//
// Optimize and evaluate results
//
minbleicreport rep;
alglib::minbleicoptimize(state, function1_grad);
minbleicresults(state, x, rep);
_TestResult = _TestResult && doc_test_int(rep.terminationtype, 4);
_TestResult = _TestResult && doc_test_real_vector(x, "[2,4]", 0.005);
//
// Check that OptGuard did not report errors
//
// NOTE: want to test OptGuard? Try breaking the gradient - say, add
// 1.0 to some of its components.
//
optguardreport ogrep;
minbleicoptguardresults(state, ogrep);
_TestResult = _TestResult && doc_test_bool(ogrep.badgradsuspected, false);
_TestResult = _TestResult && doc_test_bool(ogrep.nonc0suspected, false);
_TestResult = _TestResult && doc_test_bool(ogrep.nonc1suspected, false);
_TestResult = _TestResult && (_spoil_scenario==-1);
}
catch(ap_error)
{ _TestResult = _TestResult && (_spoil_scenario!=-1); }
}
if( !_TestResult)
{
printf("%-32s FAILED\n", "minbleic_d_2");
fflush(stdout);
}
_TotalResult = _TotalResult && _TestResult;
//
// TEST minbleic_numdiff
// Nonlinear optimization with bound constraints and numerical differentiation
//
_TestResult = true;
for(_spoil_scenario=-1; _spoil_scenario<23; _spoil_scenario++)
{
try
{
//
// This example demonstrates minimization of
//
// f(x,y) = 100*(x+3)^4+(y-3)^4
//
// subject to box constraints
//
// -1<=x<=+1, -1<=y<=+1
//
// using BLEIC optimizer with:
// * numerical differentiation being used
// * initial point x=[0,0]
// * unit scale being set for all variables (see minbleicsetscale for more info)
// * stopping criteria set to "terminate after short enough step"
// * OptGuard integrity check being used to check problem statement
// for some common errors like nonsmoothness or bad analytic gradient
//
// First, we create optimizer object and tune its properties:
// * set box constraints
// * set variable scales
// * set stopping criteria
//
real_1d_array x = "[0,0]";
if( _spoil_scenario==0 )
spoil_vector_by_nan(x);
if( _spoil_scenario==1 )
spoil_vector_by_posinf(x);
if( _spoil_scenario==2 )
spoil_vector_by_neginf(x);
real_1d_array s = "[1,1]";
if( _spoil_scenario==3 )
spoil_vector_by_nan(s);
if( _spoil_scenario==4 )
spoil_vector_by_posinf(s);
if( _spoil_scenario==5 )
spoil_vector_by_neginf(s);
if( _spoil_scenario==6 )
spoil_vector_by_deleting_element(s);
real_1d_array bndl = "[-1,-1]";
if( _spoil_scenario==7 )
spoil_vector_by_nan(bndl);
if( _spoil_scenario==8 )
spoil_vector_by_deleting_element(bndl);
real_1d_array bndu = "[+1,+1]";
if( _spoil_scenario==9 )
spoil_vector_by_nan(bndu);
if( _spoil_scenario==10 )
spoil_vector_by_deleting_element(bndu);
minbleicstate state;
double epsg = 0;
if( _spoil_scenario==11 )
epsg = fp_nan;
if( _spoil_scenario==12 )
epsg = fp_posinf;
if( _spoil_scenario==13 )
epsg = fp_neginf;
double epsf = 0;
if( _spoil_scenario==14 )
epsf = fp_nan;
if( _spoil_scenario==15 )
epsf = fp_posinf;
if( _spoil_scenario==16 )
epsf = fp_neginf;
double epsx = 0.000001;
if( _spoil_scenario==17 )
epsx = fp_nan;
if( _spoil_scenario==18 )
epsx = fp_posinf;
if( _spoil_scenario==19 )
epsx = fp_neginf;
ae_int_t maxits = 0;
double diffstep = 1.0e-6;
if( _spoil_scenario==20 )
diffstep = fp_nan;
if( _spoil_scenario==21 )
diffstep = fp_posinf;
if( _spoil_scenario==22 )
diffstep = fp_neginf;
minbleiccreatef(x, diffstep, state);
minbleicsetbc(state, bndl, bndu);
minbleicsetscale(state, s);
minbleicsetcond(state, epsg, epsf, epsx, maxits);
//
// Then we activate OptGuard integrity checking.
//
// Numerical differentiation always produces "correct" gradient
// (with some truncation error, but unbiased). Thus, we just have
// to check smoothness properties of the target: C0 and C1 continuity.
//
// Sometimes user accidentally tries to solve nonsmooth problems
// with smooth optimizer. OptGuard helps to detect such situations
// early, at the prototyping stage.
//
minbleicoptguardsmoothness(state);
//
// Optimize and evaluate results
//
minbleicreport rep;
alglib::minbleicoptimize(state, function1_func);
minbleicresults(state, x, rep);
_TestResult = _TestResult && doc_test_int(rep.terminationtype, 4);
_TestResult = _TestResult && doc_test_real_vector(x, "[-1,1]", 0.005);
//
// Check that OptGuard did not report errors
//
// Want to challenge OptGuard? Try to make your problem
// nonsmooth by replacing 100*(x+3)^4 by 100*|x+3| and
// re-run optimizer.
//
optguardreport ogrep;
minbleicoptguardresults(state, ogrep);
_TestResult = _TestResult && doc_test_bool(ogrep.nonc0suspected, false);
_TestResult = _TestResult && doc_test_bool(ogrep.nonc1suspected, false);
_TestResult = _TestResult && (_spoil_scenario==-1);
}
catch(ap_error)
{ _TestResult = _TestResult && (_spoil_scenario!=-1); }
}
if( !_TestResult)
{
printf("%-32s FAILED\n", "minbleic_numdiff");
fflush(stdout);
}
_TotalResult = _TotalResult && _TestResult;
//
// TEST minqp_d_u1
// Unconstrained dense quadratic programming
//
_TestResult = true;
for(_spoil_scenario=-1; _spoil_scenario<10; _spoil_scenario++)
{
try
{
//
// This example demonstrates minimization of F(x0,x1) = x0^2 + x1^2 -6*x0 - 4*x1
//
// Exact solution is [x0,x1] = [3,2]
//
// IMPORTANT: this solver minimizes following function:
//
// f(x) = 0.5*x'*A*x + b'*x.
//
// Note that quadratic term has 0.5 before it. So if you want to minimize
// quadratic function, you should rewrite it in such way that quadratic term
// is multiplied by 0.5 too.
//
// For example, our function is f(x)=x0^2+x1^2+..., but we rewrite it as
//
// f(x) = 0.5*(2*x0^2+2*x1^2) + ....
//
// and pass diag(2,2) as quadratic term - NOT diag(1,1)!
//
real_2d_array a = "[[2,0],[0,2]]";
if( _spoil_scenario==0 )
spoil_matrix_by_deleting_row(a);
if( _spoil_scenario==1 )
spoil_matrix_by_deleting_col(a);
real_1d_array b = "[-6,-4]";
if( _spoil_scenario==2 )
spoil_vector_by_nan(b);
if( _spoil_scenario==3 )
spoil_vector_by_posinf(b);
if( _spoil_scenario==4 )
spoil_vector_by_neginf(b);
if( _spoil_scenario==5 )
spoil_vector_by_deleting_element(b);
real_1d_array s = "[1,1]";
if( _spoil_scenario==6 )
spoil_vector_by_nan(s);
if( _spoil_scenario==7 )
spoil_vector_by_posinf(s);
if( _spoil_scenario==8 )
spoil_vector_by_neginf(s);
if( _spoil_scenario==9 )
spoil_vector_by_deleting_element(s);
bool isupper = true;
real_1d_array x;
minqpstate state;
minqpreport rep;
// create the solver, set quadratic/linear terms
minqpcreate(2, state);
minqpsetquadraticterm(state, a, isupper);
minqpsetlinearterm(state, b);
// Set the scale of the parameters.
// It is strongly recommended that you set the scale of your variables.
// Knowing their scales is essential for evaluation of stopping criteria
// and for preconditioning of the algorithm steps.
// You can find more information on scaling at http://www.alglib.net/optimization/scaling.php
//
// NOTE: for convex problems you may try using minqpsetscaleautodiag()
// which automatically determines variable scales.
minqpsetscale(state, s);
//
// Solve problem with the sparse interior-point method (sparse IPM) solver.
//
// This solver is intended for large-scale sparse problems with box and linear
// constraints, but it will work on such a toy problem too.
//
// Commercial ALGLIB can parallelize sparse Cholesky factorization which is the
// most time-consuming part of the algorithm. See the ALGLIB Reference Manual for
// more information on how to activate parallelism support.
//
// Default stopping criteria are used.
//
minqpsetalgosparseipm(state, 0.0);
minqpoptimize(state);
minqpresults(state, x, rep);
_TestResult = _TestResult && doc_test_real_vector(x, "[3,2]", 0.005);
//
// Solve problem with dense IPM solver.
//
// This solver is optimized for problems with dense linear constraints and/or
// dense quadratic term.
//
// Default stopping criteria are used.
//
minqpsetalgodenseipm(state, 0.0);
minqpoptimize(state);
minqpresults(state, x, rep);
_TestResult = _TestResult && doc_test_real_vector(x, "[3,2]", 0.005);
_TestResult = _TestResult && (_spoil_scenario==-1);
}
catch(ap_error)
{ _TestResult = _TestResult && (_spoil_scenario!=-1); }
}
if( !_TestResult)
{
printf("%-32s FAILED\n", "minqp_d_u1");
fflush(stdout);
}
_TotalResult = _TotalResult && _TestResult;
//
// TEST minqp_d_bc1
// Box constrained dense quadratic programming
//
_TestResult = true;
for(_spoil_scenario=-1; _spoil_scenario<14; _spoil_scenario++)
{
try
{
//
// This example demonstrates minimization of F(x0,x1) = x0^2 + x1^2 -6*x0 - 4*x1
// subject to Box constraints 0<=x0<=2.5, 0<=x1<=2.5
//
// Exact solution is [x0,x1] = [2.5,2]
//
// IMPORTANT: this solver minimizes following function:
//
// f(x) = 0.5*x'*A*x + b'*x.
//
// Note that quadratic term has 0.5 before it. So if you want to minimize
// quadratic function, you should rewrite it in such way that quadratic term
// is multiplied by 0.5 too.
//
// For example, our function is f(x)=x0^2+x1^2+..., but we rewrite it as
//
// f(x) = 0.5*(2*x0^2+2*x1^2) + ....
//
// and pass diag(2,2) as quadratic term - NOT diag(1,1)!
//
real_2d_array a = "[[2,0],[0,2]]";
if( _spoil_scenario==0 )
spoil_matrix_by_deleting_row(a);
if( _spoil_scenario==1 )
spoil_matrix_by_deleting_col(a);
real_1d_array b = "[-6,-4]";
if( _spoil_scenario==2 )
spoil_vector_by_nan(b);
if( _spoil_scenario==3 )
spoil_vector_by_posinf(b);
if( _spoil_scenario==4 )
spoil_vector_by_neginf(b);
if( _spoil_scenario==5 )
spoil_vector_by_deleting_element(b);
real_1d_array s = "[1,1]";
if( _spoil_scenario==6 )
spoil_vector_by_nan(s);
if( _spoil_scenario==7 )
spoil_vector_by_posinf(s);
if( _spoil_scenario==8 )
spoil_vector_by_neginf(s);
if( _spoil_scenario==9 )
spoil_vector_by_deleting_element(s);
real_1d_array bndl = "[0.0,0.0]";
if( _spoil_scenario==10 )
spoil_vector_by_nan(bndl);
if( _spoil_scenario==11 )
spoil_vector_by_deleting_element(bndl);
real_1d_array bndu = "[2.5,2.5]";
if( _spoil_scenario==12 )
spoil_vector_by_nan(bndu);
if( _spoil_scenario==13 )
spoil_vector_by_deleting_element(bndu);
bool isupper = true;
real_1d_array x;
minqpstate state;
minqpreport rep;
// create solver, set quadratic/linear terms
minqpcreate(2, state);
minqpsetquadraticterm(state, a, isupper);
minqpsetlinearterm(state, b);
minqpsetbc(state, bndl, bndu);
// Set scale of the parameters.
// It is strongly recommended that you set scale of your variables.
// Knowing their scales is essential for evaluation of stopping criteria
// and for preconditioning of the algorithm steps.
// You can find more information on scaling at http://www.alglib.net/optimization/scaling.php
//
// NOTE: for convex problems you may try using minqpsetscaleautodiag()
// which automatically determines variable scales.
minqpsetscale(state, s);
//
// Solve problem with the sparse interior-point method (sparse IPM) solver.
//
// This solver is intended for large-scale sparse problems with box and linear
// constraints, but it will work on such a toy problem too.
//
// Commercial ALGLIB can parallelize sparse Cholesky factorization which is the
// most time-consuming part of the algorithm. See the ALGLIB Reference Manual for
// more information on how to activate parallelism support.
//
// Default stopping criteria are used.
//
minqpsetalgosparseipm(state, 0.0);
minqpoptimize(state);
minqpresults(state, x, rep);
_TestResult = _TestResult && doc_test_real_vector(x, "[2.5,2]", 0.005);
//
// Solve problem with dense IPM solver.
//
// This solver is optimized for problems with dense linear constraints and/or
// dense quadratic term.
//
// Default stopping criteria are used.
//
minqpsetalgodenseipm(state, 0.0);
minqpoptimize(state);
minqpresults(state, x, rep);
_TestResult = _TestResult && doc_test_real_vector(x, "[2.5,2]", 0.005);
_TestResult = _TestResult && (_spoil_scenario==-1);
}
catch(ap_error)
{ _TestResult = _TestResult && (_spoil_scenario!=-1); }
}
if( !_TestResult)
{
printf("%-32s FAILED\n", "minqp_d_bc1");
fflush(stdout);
}
_TotalResult = _TotalResult && _TestResult;
//
// TEST minqp_d_lc1
// Linearly constrained dense quadratic programming
//
_TestResult = true;
for(_spoil_scenario=-1; _spoil_scenario<13; _spoil_scenario++)
{
try
{
//
// This example demonstrates minimization of F(x0,x1) = x0^2 + x1^2 -6*x0 - 4*x1
// subject to linear constraint x0+x1<=2
//
// Exact solution is [x0,x1] = [1.5,0.5]
//
// IMPORTANT: this solver minimizes following function:
//
// f(x) = 0.5*x'*A*x + b'*x.
//
// Note that quadratic term has 0.5 before it. So if you want to minimize
// quadratic function, you should rewrite it in such way that quadratic term
// is multiplied by 0.5 too.
//
// For example, our function is f(x)=x0^2+x1^2+..., but we rewrite it as
//
// f(x) = 0.5*(2*x0^2+2*x1^2) + ....
//
// and pass diag(2,2) as quadratic term - NOT diag(1,1)!
//
real_2d_array a = "[[2,0],[0,2]]";
if( _spoil_scenario==0 )
spoil_matrix_by_deleting_row(a);
if( _spoil_scenario==1 )
spoil_matrix_by_deleting_col(a);
real_1d_array b = "[-6,-4]";
if( _spoil_scenario==2 )
spoil_vector_by_nan(b);
if( _spoil_scenario==3 )
spoil_vector_by_posinf(b);
if( _spoil_scenario==4 )
spoil_vector_by_neginf(b);
if( _spoil_scenario==5 )
spoil_vector_by_deleting_element(b);
real_1d_array s = "[1,1]";
if( _spoil_scenario==6 )
spoil_vector_by_nan(s);
if( _spoil_scenario==7 )
spoil_vector_by_posinf(s);
if( _spoil_scenario==8 )
spoil_vector_by_neginf(s);
if( _spoil_scenario==9 )
spoil_vector_by_deleting_element(s);
real_2d_array c = "[[1.0,1.0,2.0]]";
if( _spoil_scenario==10 )
spoil_matrix_by_nan(c);
if( _spoil_scenario==11 )
spoil_matrix_by_posinf(c);
if( _spoil_scenario==12 )
spoil_matrix_by_neginf(c);
integer_1d_array ct = "[-1]";
bool isupper = true;
real_1d_array x;
minqpstate state;
minqpreport rep;
// create solver, set quadratic/linear terms
minqpcreate(2, state);
minqpsetquadraticterm(state, a, isupper);
minqpsetlinearterm(state, b);
minqpsetlc(state, c, ct);
// Set scale of the parameters.
// It is strongly recommended that you set scale of your variables.
// Knowing their scales is essential for evaluation of stopping criteria
// and for preconditioning of the algorithm steps.
// You can find more information on scaling at http://www.alglib.net/optimization/scaling.php
//
// NOTE: for convex problems you may try using minqpsetscaleautodiag()
// which automatically determines variable scales.
minqpsetscale(state, s);
//
// Solve problem with the sparse interior-point method (sparse IPM) solver.
//
// This solver is intended for large-scale sparse problems with box and linear
// constraints, but it will work on such a toy problem too.
//
// Commercial ALGLIB can parallelize sparse Cholesky factorization which is the
// most time-consuming part of the algorithm. See the ALGLIB Reference Manual for
// more information on how to activate parallelism support.
//
// Default stopping criteria are used.
//
minqpsetalgosparseipm(state, 0.0);
minqpoptimize(state);
minqpresults(state, x, rep);
_TestResult = _TestResult && doc_test_real_vector(x, "[1.5,0.5]", 0.005);
//
// Solve problem with dense IPM solver.
//
// This solver is optimized for problems with dense linear constraints and/or
// dense quadratic term.
//
// Default stopping criteria are used.
//
minqpsetalgodenseipm(state, 0.0);
minqpoptimize(state);
minqpresults(state, x, rep);
_TestResult = _TestResult && doc_test_real_vector(x, "[1.5,0.5]", 0.005);
_TestResult = _TestResult && (_spoil_scenario==-1);
}
catch(ap_error)
{ _TestResult = _TestResult && (_spoil_scenario!=-1); }
}
if( !_TestResult)
{
printf("%-32s FAILED\n", "minqp_d_lc1");
fflush(stdout);
}
_TotalResult = _TotalResult && _TestResult;
//
// TEST minqp_d_u2
// Unconstrained sparse quadratic programming
//
_TestResult = true;
for(_spoil_scenario=-1; _spoil_scenario<12; _spoil_scenario++)
{
try
{
//
// This example demonstrates minimization of F(x0,x1) = x0^2 + x1^2 -6*x0 - 4*x1,
// with quadratic term given by sparse matrix structure.
//
// Exact solution is [x0,x1] = [3,2]
//
// We provide algorithm with starting point, although in this case
// (dense matrix, no constraints) it can work without such information.
//
// IMPORTANT: this solver minimizes following function:
// f(x) = 0.5*x'*A*x + b'*x.
// Note that quadratic term has 0.5 before it. So if you want to minimize
// quadratic function, you should rewrite it in such way that quadratic term
// is multiplied by 0.5 too.
//
// For example, our function is f(x)=x0^2+x1^2+..., but we rewrite it as
// f(x) = 0.5*(2*x0^2+2*x1^2) + ....
// and pass diag(2,2) as quadratic term - NOT diag(1,1)!
//
sparsematrix a;
real_1d_array b = "[-6,-4]";
if( _spoil_scenario==0 )
spoil_vector_by_nan(b);
if( _spoil_scenario==1 )
spoil_vector_by_posinf(b);
if( _spoil_scenario==2 )
spoil_vector_by_neginf(b);
if( _spoil_scenario==3 )
spoil_vector_by_deleting_element(b);
real_1d_array x0 = "[0,1]";
if( _spoil_scenario==4 )
spoil_vector_by_nan(x0);
if( _spoil_scenario==5 )
spoil_vector_by_posinf(x0);
if( _spoil_scenario==6 )
spoil_vector_by_neginf(x0);
if( _spoil_scenario==7 )
spoil_vector_by_deleting_element(x0);
real_1d_array s = "[1,1]";
if( _spoil_scenario==8 )
spoil_vector_by_nan(s);
if( _spoil_scenario==9 )
spoil_vector_by_posinf(s);
if( _spoil_scenario==10 )
spoil_vector_by_neginf(s);
if( _spoil_scenario==11 )
spoil_vector_by_deleting_element(s);
real_1d_array x;
minqpstate state;
minqpreport rep;
// initialize sparsematrix structure
sparsecreate(2, 2, 0, a);
sparseset(a, 0, 0, 2.0);
sparseset(a, 1, 1, 2.0);
// create solver, set quadratic/linear terms
minqpcreate(2, state);
minqpsetquadratictermsparse(state, a, true);
minqpsetlinearterm(state, b);
minqpsetstartingpoint(state, x0);
// Set scale of the parameters.
// It is strongly recommended that you set scale of your variables.
// Knowing their scales is essential for evaluation of stopping criteria
// and for preconditioning of the algorithm steps.
// You can find more information on scaling at http://www.alglib.net/optimization/scaling.php
//
// NOTE: for convex problems you may try using minqpsetscaleautodiag()
// which automatically determines variable scales.
minqpsetscale(state, s);
//
// Solve problem with the sparse interior-point method (sparse IPM) solver.
//
// This solver is intended for large-scale sparse problems with box and linear
// constraints, but it will work on such a toy problem too.
//
// Commercial ALGLIB can parallelize sparse Cholesky factorization which is the
// most time-consuming part of the algorithm. See the ALGLIB Reference Manual for
// more information on how to activate parallelism support.
//
// Default stopping criteria are used.
//
minqpsetalgosparseipm(state, 0.0);
minqpoptimize(state);
minqpresults(state, x, rep);
_TestResult = _TestResult && doc_test_real_vector(x, "[3,2]", 0.005);
//
// Solve problem with dense IPM solver.
//
// This solver is optimized for problems with dense linear constraints and/or
// dense quadratic term.
//
// Default stopping criteria are used.
//
minqpsetalgodenseipm(state, 0.0);
minqpoptimize(state);
minqpresults(state, x, rep);
_TestResult = _TestResult && doc_test_real_vector(x, "[3,2]", 0.005);
_TestResult = _TestResult && (_spoil_scenario==-1);
}
catch(ap_error)
{ _TestResult = _TestResult && (_spoil_scenario!=-1); }
}
if( !_TestResult)
{
printf("%-32s FAILED\n", "minqp_d_u2");
fflush(stdout);
}
_TotalResult = _TotalResult && _TestResult;
//
// TEST minqp_d_nonconvex
// Nonconvex quadratic programming
//
_TestResult = true;
for(_spoil_scenario=-1; _spoil_scenario<18; _spoil_scenario++)
{
try
{
//
// This example demonstrates minimization of nonconvex function
// F(x0,x1) = -(x0^2+x1^2)
// subject to constraints x0,x1 in [1.0,2.0]
// Exact solution is [x0,x1] = [2,2].
//
// Non-convex problems are harder to solve than convex ones, and they
// may have more than one local minimum. However, ALGLIB solvers may deal
// with such problems (although they do not guarantee convergence to
// global minimum).
//
// IMPORTANT: this solver minimizes following function:
// f(x) = 0.5*x'*A*x + b'*x.
// Note that quadratic term has 0.5 before it. So if you want to minimize
// quadratic function, you should rewrite it in such way that quadratic term
// is multiplied by 0.5 too.
//
// For example, our function is f(x)=-(x0^2+x1^2), but we rewrite it as
// f(x) = 0.5*(-2*x0^2-2*x1^2)
// and pass diag(-2,-2) as quadratic term - NOT diag(-1,-1)!
//
real_2d_array a = "[[-2,0],[0,-2]]";
if( _spoil_scenario==0 )
spoil_matrix_by_deleting_row(a);
if( _spoil_scenario==1 )
spoil_matrix_by_deleting_col(a);
real_1d_array x0 = "[1,1]";
if( _spoil_scenario==2 )
spoil_vector_by_nan(x0);
if( _spoil_scenario==3 )
spoil_vector_by_posinf(x0);
if( _spoil_scenario==4 )
spoil_vector_by_neginf(x0);
if( _spoil_scenario==5 )
spoil_vector_by_deleting_element(x0);
real_1d_array s = "[1,1]";
if( _spoil_scenario==6 )
spoil_vector_by_nan(s);
if( _spoil_scenario==7 )
spoil_vector_by_posinf(s);
if( _spoil_scenario==8 )
spoil_vector_by_neginf(s);
if( _spoil_scenario==9 )
spoil_vector_by_deleting_element(s);
real_1d_array bndl = "[1.0,1.0]";
if( _spoil_scenario==10 )
spoil_vector_by_nan(bndl);
if( _spoil_scenario==11 )
spoil_vector_by_deleting_element(bndl);
real_1d_array bndu = "[2.0,2.0]";
if( _spoil_scenario==12 )
spoil_vector_by_nan(bndu);
if( _spoil_scenario==13 )
spoil_vector_by_deleting_element(bndu);
bool isupper = true;
real_1d_array x;
minqpstate state;
minqpreport rep;
// create solver, set quadratic/linear terms, constraints
minqpcreate(2, state);
minqpsetquadraticterm(state, a, isupper);
minqpsetstartingpoint(state, x0);
minqpsetbc(state, bndl, bndu);
// Set scale of the parameters.
// It is strongly recommended that you set scale of your variables.
// Knowing their scales is essential for evaluation of stopping criteria
// and for preconditioning of the algorithm steps.
// You can find more information on scaling at http://www.alglib.net/optimization/scaling.php
//
// NOTE: there also exists minqpsetscaleautodiag() function
// which automatically determines variable scales; however,
// it does NOT work for non-convex problems.
minqpsetscale(state, s);
//
// Solve problem with BLEIC-based QP solver.
//
// This solver is intended for problems with moderate (up to 50) number
// of general linear constraints and unlimited number of box constraints.
// It may solve non-convex problems as long as they are bounded from
// below under constraints.
//
// Default stopping criteria are used.
//
minqpsetalgobleic(state, 0.0, 0.0, 0.0, 0);
minqpoptimize(state);
minqpresults(state, x, rep);
_TestResult = _TestResult && doc_test_real_vector(x, "[2,2]", 0.005);
//
// Solve problem with DENSE-AUL solver.
//
// This solver is optimized for nonconvex problems with up to several thousands of
// variables and large amount of general linear constraints. Problems with
// less than 50 general linear constraints can be efficiently solved with
// BLEIC, problems with box-only constraints can be solved with QuickQP.
// However, DENSE-AUL will work in any (including unconstrained) case.
//
// Default stopping criteria are used.
//
minqpsetalgodenseaul(state, 1.0e-9, 1.0e+4, 5);
minqpoptimize(state);
minqpresults(state, x, rep);
_TestResult = _TestResult && doc_test_real_vector(x, "[2,2]", 0.005);
// Hmm... this problem is bounded from below (has solution) only under constraints.
// What it we remove them?
//
// You may see that BLEIC algorithm detects unboundedness of the problem,
// -4 is returned as completion code. However, DENSE-AUL is unable to detect
// such situation and it will cycle forever (we do not test it here).
real_1d_array nobndl = "[-inf,-inf]";
if( _spoil_scenario==14 )
spoil_vector_by_nan(nobndl);
if( _spoil_scenario==15 )
spoil_vector_by_deleting_element(nobndl);
real_1d_array nobndu = "[+inf,+inf]";
if( _spoil_scenario==16 )
spoil_vector_by_nan(nobndu);
if( _spoil_scenario==17 )
spoil_vector_by_deleting_element(nobndu);
minqpsetbc(state, nobndl, nobndu);
minqpsetalgobleic(state, 0.0, 0.0, 0.0, 0);
minqpoptimize(state);
minqpresults(state, x, rep);
_TestResult = _TestResult && doc_test_int(rep.terminationtype, -4);
_TestResult = _TestResult && (_spoil_scenario==-1);
}
catch(ap_error)
{ _TestResult = _TestResult && (_spoil_scenario!=-1); }
}
if( !_TestResult)
{
printf("%-32s FAILED\n", "minqp_d_nonconvex");
fflush(stdout);
}
_TotalResult = _TotalResult && _TestResult;
//
// TEST minlm_d_v
// Nonlinear least squares optimization using function vector only
//
_TestResult = true;
for(_spoil_scenario=-1; _spoil_scenario<9; _spoil_scenario++)
{
try
{
//
// This example demonstrates minimization of F(x0,x1) = f0^2+f1^2, where
//
// f0(x0,x1) = 10*(x0+3)^2
// f1(x0,x1) = (x1-3)^2
//
// using "V" mode of the Levenberg-Marquardt optimizer (function values only,
// no Jacobian information). The optimization algorithm uses function vector
//
// f[] = {f1,f2}
//
// No other information (Jacobian, gradient, etc.) is needed.
//
// IMPORTANT: the MINLM optimizer supports parallel parallel numerical
// differentiation ('callback parallelism'). This feature,
// which is present in commercial ALGLIB editions, greatly
// accelerates optimization with numerical differentiation of
// an expensive target functions.
//
// Callback parallelism is usually beneficial when computing a
// numerical gradient requires more than several milliseconds.
// This particular example, of course, is not suited for
// callback parallelism.
//
// If you solve a curve fitting problem, i.e. the function
// vector is actually the same function computed at different
// points of a data points space, then it may be better to use
// an LSFIT curve fitting solver, which offers more fine-grained
// parallelism due to knowledge of the problem structure. In
// particular, it can accelerate both numerical differentiation
// and problems with user-supplied gradients.
//
// See ALGLIB Reference Manual, 'Working with commercial version'
// section, and comments on minlmoptimize() function for
// more information.
//
real_1d_array x = "[0,0]";
if( _spoil_scenario==0 )
spoil_vector_by_nan(x);
if( _spoil_scenario==1 )
spoil_vector_by_posinf(x);
if( _spoil_scenario==2 )
spoil_vector_by_neginf(x);
real_1d_array s = "[1,1]";
if( _spoil_scenario==3 )
spoil_vector_by_nan(s);
if( _spoil_scenario==4 )
spoil_vector_by_posinf(s);
if( _spoil_scenario==5 )
spoil_vector_by_neginf(s);
double epsx = 0.0000000001;
if( _spoil_scenario==6 )
epsx = fp_nan;
if( _spoil_scenario==7 )
epsx = fp_posinf;
if( _spoil_scenario==8 )
epsx = fp_neginf;
ae_int_t maxits = 0;
minlmstate state;
minlmreport rep;
//
// Create optimizer, tell it to:
// * use numerical differentiation with step equal to 0.0001
// * use unit scale for all variables (s is a unit vector)
// * stop after short enough step (less than epsx)
//
minlmcreatev(2, x, 0.0001, state);
minlmsetcond(state, epsx, maxits);
minlmsetscale(state, s);
//
// A new feature: nonmonotonic steps!
//
// In theory, LM solver should be used only for smooth and continuous objectives. In practice,
// real life objectives are often results of some long numerical simulation and have defects
// like small discontinuous jumps, small noise or minor nonsmoothness. Such defects often
// prevent optimization progress because an uphill step may be required to move past the
// defect.
//
// Nonmonotonic steps allow to tolerate a minor and temporary increase in the objective,
// allowing progress beyond an obstacle. This feature is also essential for ill-conditioned
// targets - it allows the solver to jump through a curved valley instead of navigating along
// its bottom.
//
// However, sometimes nonmonotonic steps degrade convergence by allowing an optimizer to
// wander too far away from the solution, so this feature should be used only after careful
// testing.
//
// The code below sets the nonmonotonic memory length to 0 (which means traditional monotonic
// optimization). If you want to try a nonmonotonic optimization, use 2 or 3 as a recommended
// memory length.
//
minlmsetnonmonotonicsteps(state, 0);
//
// Optimize
//
alglib::minlmoptimize(state, function1_fvec);
//
// Test optimization results
//
// NOTE: because we use numerical differentiation, we do not
// verify Jacobian correctness - it is always "correct".
// However, if you switch to analytic gradient, consider
// checking it with OptGuard (see other examples).
//
minlmresults(state, x, rep);
_TestResult = _TestResult && doc_test_real_vector(x, "[-3,+3]", 0.005);
_TestResult = _TestResult && (_spoil_scenario==-1);
}
catch(ap_error)
{ _TestResult = _TestResult && (_spoil_scenario!=-1); }
}
if( !_TestResult)
{
printf("%-32s FAILED\n", "minlm_d_v");
fflush(stdout);
}
_TotalResult = _TotalResult && _TestResult;
//
// TEST minlm_d_vj
// Nonlinear least squares optimization using function vector and Jacobian
//
_TestResult = true;
for(_spoil_scenario=-1; _spoil_scenario<9; _spoil_scenario++)
{
try
{
//
// This example demonstrates minimization of F(x0,x1) = f0^2+f1^2, where
//
// f0(x0,x1) = 10*(x0+3)^2
// f1(x0,x1) = (x1-3)^2
//
// using "VJ" mode of the Levenberg-Marquardt optimizer. The optimization
// algorithm uses the function vector f[] = {f1,f2} and the Jacobian
// matrix J = {dfi/dxj}, both of them provided by user.
//
// IMPORTANT: the MINLM optimizer supports parallel numerical
// differentiation ('callback parallelism'). This feature,
// which is present in commercial ALGLIB editions, greatly
// accelerates optimization with numerical differentiation of
// an expensive target functions.
//
// Callback parallelism is usually beneficial when computing a
// numerical gradient requires more than several milliseconds.
// This particular example, of course, is not suited for
// callback parallelism.
//
// If you solve a curve fitting problem, i.e. the function
// vector is actually the same function computed at different
// points of a data points space, then it may be better to use
// an LSFIT curve fitting solver, which offers more fine-grained
// parallelism due to knowledge of the problem structure. In
// particular, it can accelerate both numerical differentiation
// and problems with user-supplied gradients.
//
// See ALGLIB Reference Manual, 'Working with commercial version'
// section, and comments on minlmoptimize() function for
// more information.
//
real_1d_array x = "[0,0]";
if( _spoil_scenario==0 )
spoil_vector_by_nan(x);
if( _spoil_scenario==1 )
spoil_vector_by_posinf(x);
if( _spoil_scenario==2 )
spoil_vector_by_neginf(x);
real_1d_array s = "[1,1]";
if( _spoil_scenario==3 )
spoil_vector_by_nan(s);
if( _spoil_scenario==4 )
spoil_vector_by_posinf(s);
if( _spoil_scenario==5 )
spoil_vector_by_neginf(s);
double epsx = 0.0000000001;
if( _spoil_scenario==6 )
epsx = fp_nan;
if( _spoil_scenario==7 )
epsx = fp_posinf;
if( _spoil_scenario==8 )
epsx = fp_neginf;
ae_int_t maxits = 0;
minlmstate state;
//
// Create optimizer, tell it to:
// * use analytic gradient provided by user
// * use unit scale for all variables (s is a unit vector)
// * stop after short enough step (less than epsx)
//
minlmcreatevj(2, x, state);
minlmsetcond(state, epsx, maxits);
minlmsetscale(state, s);
//
// A new feature: nonmonotonic steps!
//
// In theory, LM solver should be used only for smooth and continuous objectives. In practice,
// real life objectives are often results of some long numerical simulation and have defects
// like small discontinuous jumps, small noise or minor nonsmoothness. Such defects often
// prevent optimization progress because an uphill step may be required to move past the
// defect.
//
// Nonmonotonic steps allow to tolerate a minor and temporary increase in the objective,
// allowing progress beyond an obstacle. This feature is also essential for ill-conditioned
// targets - it allows the solver to jump through a curved valley instead of navigating along
// its bottom.
//
// However, sometimes nonmonotonic steps degrade convergence by allowing an optimizer to
// wander too far away from the solution, so this feature should be used only after careful
// testing.
//
// The code below sets the nonmonotonic memory length to 0 (which means traditional monotonic
// optimization). If you want to try a nonmonotonic optimization, use 2 or 3 as a recommended
// memory length.
//
minlmsetnonmonotonicsteps(state, 0);
//
// Optimize
//
alglib::minlmoptimize(state, function1_fvec, function1_jac);
//
// Test optimization results
//
minlmreport rep;
minlmresults(state, x, rep);
_TestResult = _TestResult && doc_test_real_vector(x, "[-3,+3]", 0.005);
_TestResult = _TestResult && (_spoil_scenario==-1);
}
catch(ap_error)
{ _TestResult = _TestResult && (_spoil_scenario!=-1); }
}
if( !_TestResult)
{
printf("%-32s FAILED\n", "minlm_d_vj");
fflush(stdout);
}
_TotalResult = _TotalResult && _TestResult;
//
// TEST minlm_d_vb
// Bound constrained nonlinear least squares optimization
//
_TestResult = true;
for(_spoil_scenario=-1; _spoil_scenario<13; _spoil_scenario++)
{
try
{
//
// This example demonstrates minimization of F(x0,x1) = f0^2+f1^2, where
//
// f0(x0,x1) = 10*(x0+3)^2
// f1(x0,x1) = (x1-3)^2
//
// with box constraints
//
// -1 <= x0 <= +1
// -1 <= x1 <= +1
//
// using "V" mode of the Levenberg-Marquardt optimizer. The optimization
// algorithm uses function vector f[] = {f1,f2}. No other information
// (Jacobian, gradient, etc.) is needed.
//
// IMPORTANT: the MINLM optimizer supports parallel parallel numerical
// differentiation ('callback parallelism'). This feature,
// which is present in commercial ALGLIB editions, greatly
// accelerates optimization with numerical differentiation of
// an expensive target functions.
//
// Callback parallelism is usually beneficial when computing a
// numerical gradient requires more than several milliseconds.
// This particular example, of course, is not suited for
// callback parallelism.
//
// If you solve a curve fitting problem, i.e. the function
// vector is actually the same function computed at different
// points of a data points space, then it may be better to use
// an LSFIT curve fitting solver, which offers more fine-grained
// parallelism due to knowledge of the problem structure. In
// particular, it can accelerate both numerical differentiation
// and problems with user-supplied gradients.
//
// See ALGLIB Reference Manual, 'Working with commercial version'
// section, and comments on minlmoptimize() function for
// more information.
//
real_1d_array x = "[0,0]";
if( _spoil_scenario==0 )
spoil_vector_by_nan(x);
if( _spoil_scenario==1 )
spoil_vector_by_posinf(x);
if( _spoil_scenario==2 )
spoil_vector_by_neginf(x);
real_1d_array s = "[1,1]";
if( _spoil_scenario==3 )
spoil_vector_by_nan(s);
if( _spoil_scenario==4 )
spoil_vector_by_posinf(s);
if( _spoil_scenario==5 )
spoil_vector_by_neginf(s);
real_1d_array bndl = "[-1,-1]";
if( _spoil_scenario==6 )
spoil_vector_by_nan(bndl);
if( _spoil_scenario==7 )
spoil_vector_by_deleting_element(bndl);
real_1d_array bndu = "[+1,+1]";
if( _spoil_scenario==8 )
spoil_vector_by_nan(bndu);
if( _spoil_scenario==9 )
spoil_vector_by_deleting_element(bndu);
double epsx = 0.0000000001;
if( _spoil_scenario==10 )
epsx = fp_nan;
if( _spoil_scenario==11 )
epsx = fp_posinf;
if( _spoil_scenario==12 )
epsx = fp_neginf;
ae_int_t maxits = 0;
minlmstate state;
//
// Create optimizer, tell it to:
// * use numerical differentiation with step equal to 1.0
// * use unit scale for all variables (s is a unit vector)
// * stop after short enough step (less than epsx)
// * set box constraints
//
minlmcreatev(2, x, 0.0001, state);
minlmsetbc(state, bndl, bndu);
minlmsetcond(state, epsx, maxits);
minlmsetscale(state, s);
//
// A new feature: nonmonotonic steps!
//
// In theory, LM solver should be used only for smooth and continuous objectives. In practice,
// real life objectives are often results of some long numerical simulation and have defects
// like small discontinuous jumps, small noise or minor nonsmoothness. Such defects often
// prevent optimization progress because an uphill step may be required to move past the
// defect.
//
// Nonmonotonic steps allow to tolerate a minor and temporary increase in the objective,
// allowing progress beyond an obstacle. This feature is also essential for ill-conditioned
// targets - it allows the solver to jump through a curved valley instead of navigating along
// its bottom.
//
// However, sometimes nonmonotonic steps degrade convergence by allowing an optimizer to
// wander too far away from the solution, so this feature should be used only after careful
// testing.
//
// The code below sets the nonmonotonic memory length to 0 (which means traditional monotonic
// optimization). If you want to try a nonmonotonic optimization, use 2 or 3 as a recommended
// memory length.
//
minlmsetnonmonotonicsteps(state, 0);
//
// Optimize
//
alglib::minlmoptimize(state, function1_fvec);
//
// Test optimization results
//
// NOTE: because we use numerical differentiation, we do not
// verify Jacobian correctness - it is always "correct".
// However, if you switch to analytic gradient, consider
// checking it with OptGuard (see other examples).
//
minlmreport rep;
minlmresults(state, x, rep);
_TestResult = _TestResult && doc_test_real_vector(x, "[-1,+1]", 0.005);
_TestResult = _TestResult && (_spoil_scenario==-1);
}
catch(ap_error)
{ _TestResult = _TestResult && (_spoil_scenario!=-1); }
}
if( !_TestResult)
{
printf("%-32s FAILED\n", "minlm_d_vb");
fflush(stdout);
}
_TotalResult = _TotalResult && _TestResult;
//
// TEST minlm_d_restarts
// Efficient restarts of LM optimizer
//
_TestResult = true;
for(_spoil_scenario=-1; _spoil_scenario<9; _spoil_scenario++)
{
try
{
//
// This example demonstrates minimization of F(x0,x1) = f0^2+f1^2, where
//
// f0(x0,x1) = 10*(x0+3)^2
// f1(x0,x1) = (x1-3)^2
//
// using several starting points and efficient restarts.
//
real_1d_array x;
double epsx = 0.0000000001;
if( _spoil_scenario==0 )
epsx = fp_nan;
if( _spoil_scenario==1 )
epsx = fp_posinf;
if( _spoil_scenario==2 )
epsx = fp_neginf;
ae_int_t maxits = 0;
minlmstate state;
minlmreport rep;
//
// create optimizer using minlmcreatev()
//
x = "[10,10]";
if( _spoil_scenario==3 )
spoil_vector_by_nan(x);
if( _spoil_scenario==4 )
spoil_vector_by_posinf(x);
if( _spoil_scenario==5 )
spoil_vector_by_neginf(x);
minlmcreatev(2, x, 0.0001, state);
minlmsetcond(state, epsx, maxits);
alglib::minlmoptimize(state, function1_fvec);
minlmresults(state, x, rep);
_TestResult = _TestResult && doc_test_real_vector(x, "[-3,+3]", 0.005);
//
// restart optimizer using minlmrestartfrom()
//
// we can use different starting point, different function,
// different stopping conditions, but the problem size
// must remain unchanged.
//
x = "[4,4]";
if( _spoil_scenario==6 )
spoil_vector_by_nan(x);
if( _spoil_scenario==7 )
spoil_vector_by_posinf(x);
if( _spoil_scenario==8 )
spoil_vector_by_neginf(x);
minlmrestartfrom(state, x);
alglib::minlmoptimize(state, function2_fvec);
minlmresults(state, x, rep);
_TestResult = _TestResult && doc_test_real_vector(x, "[0,1]", 0.005);
_TestResult = _TestResult && (_spoil_scenario==-1);
}
catch(ap_error)
{ _TestResult = _TestResult && (_spoil_scenario!=-1); }
}
if( !_TestResult)
{
printf("%-32s FAILED\n", "minlm_d_restarts");
fflush(stdout);
}
_TotalResult = _TotalResult && _TestResult;
//
// TEST mincg_d_1
// Nonlinear optimization by CG
//
_TestResult = true;
for(_spoil_scenario=-1; _spoil_scenario<15; _spoil_scenario++)
{
try
{
//
// This example demonstrates minimization of
//
// f(x,y) = 100*(x+3)^4+(y-3)^4
//
// using nonlinear conjugate gradient method with:
// * initial point x=[0,0]
// * unit scale being set for all variables (see mincgsetscale for more info)
// * stopping criteria set to "terminate after short enough step"
// * OptGuard integrity check being used to check problem statement
// for some common errors like nonsmoothness or bad analytic gradient
//
// First, we create optimizer object and tune its properties
//
real_1d_array x = "[0,0]";
if( _spoil_scenario==0 )
spoil_vector_by_nan(x);
if( _spoil_scenario==1 )
spoil_vector_by_posinf(x);
if( _spoil_scenario==2 )
spoil_vector_by_neginf(x);
real_1d_array s = "[1,1]";
if( _spoil_scenario==3 )
spoil_vector_by_nan(s);
if( _spoil_scenario==4 )
spoil_vector_by_posinf(s);
if( _spoil_scenario==5 )
spoil_vector_by_neginf(s);
double epsg = 0;
if( _spoil_scenario==6 )
epsg = fp_nan;
if( _spoil_scenario==7 )
epsg = fp_posinf;
if( _spoil_scenario==8 )
epsg = fp_neginf;
double epsf = 0;
if( _spoil_scenario==9 )
epsf = fp_nan;
if( _spoil_scenario==10 )
epsf = fp_posinf;
if( _spoil_scenario==11 )
epsf = fp_neginf;
double epsx = 0.0000000001;
if( _spoil_scenario==12 )
epsx = fp_nan;
if( _spoil_scenario==13 )
epsx = fp_posinf;
if( _spoil_scenario==14 )
epsx = fp_neginf;
ae_int_t maxits = 0;
mincgstate state;
mincgcreate(x, state);
mincgsetcond(state, epsg, epsf, epsx, maxits);
mincgsetscale(state, s);
//
// Activate OptGuard integrity checking.
//
// OptGuard monitor helps to catch common coding and problem statement
// issues, like:
// * discontinuity of the target function (C0 continuity violation)
// * nonsmoothness of the target function (C1 continuity violation)
// * erroneous analytic gradient, i.e. one inconsistent with actual
// change in the target/constraints
//
// OptGuard is essential for early prototyping stages because such
// problems often result in premature termination of the optimizer
// which is really hard to distinguish from the correct termination.
//
// IMPORTANT: GRADIENT VERIFICATION IS PERFORMED BY MEANS OF NUMERICAL
// DIFFERENTIATION. DO NOT USE IT IN PRODUCTION CODE!!!!!!!
//
// Other OptGuard checks add moderate overhead, but anyway
// it is better to turn them off when they are not needed.
//
mincgoptguardsmoothness(state);
mincgoptguardgradient(state, 0.001);
//
// Optimize and evaluate results
//
mincgreport rep;
alglib::mincgoptimize(state, function1_grad);
mincgresults(state, x, rep);
_TestResult = _TestResult && doc_test_real_vector(x, "[-3,3]", 0.005);
//
// Check that OptGuard did not report errors
//
// NOTE: want to test OptGuard? Try breaking the gradient - say, add
// 1.0 to some of its components.
//
optguardreport ogrep;
mincgoptguardresults(state, ogrep);
_TestResult = _TestResult && doc_test_bool(ogrep.badgradsuspected, false);
_TestResult = _TestResult && doc_test_bool(ogrep.nonc0suspected, false);
_TestResult = _TestResult && doc_test_bool(ogrep.nonc1suspected, false);
_TestResult = _TestResult && (_spoil_scenario==-1);
}
catch(ap_error)
{ _TestResult = _TestResult && (_spoil_scenario!=-1); }
}
if( !_TestResult)
{
printf("%-32s FAILED\n", "mincg_d_1");
fflush(stdout);
}
_TotalResult = _TotalResult && _TestResult;
//
// TEST mincg_d_2
// Nonlinear optimization with additional settings and restarts
//
_TestResult = true;
for(_spoil_scenario=-1; _spoil_scenario<21; _spoil_scenario++)
{
try
{
//
// This example demonstrates minimization of f(x,y) = 100*(x+3)^4+(y-3)^4
// with nonlinear conjugate gradient method.
//
// Several advanced techniques are demonstrated:
// * upper limit on step size
// * restart from new point
//
real_1d_array x = "[0,0]";
if( _spoil_scenario==0 )
spoil_vector_by_nan(x);
if( _spoil_scenario==1 )
spoil_vector_by_posinf(x);
if( _spoil_scenario==2 )
spoil_vector_by_neginf(x);
real_1d_array s = "[1,1]";
if( _spoil_scenario==3 )
spoil_vector_by_nan(s);
if( _spoil_scenario==4 )
spoil_vector_by_posinf(s);
if( _spoil_scenario==5 )
spoil_vector_by_neginf(s);
double epsg = 0;
if( _spoil_scenario==6 )
epsg = fp_nan;
if( _spoil_scenario==7 )
epsg = fp_posinf;
if( _spoil_scenario==8 )
epsg = fp_neginf;
double epsf = 0;
if( _spoil_scenario==9 )
epsf = fp_nan;
if( _spoil_scenario==10 )
epsf = fp_posinf;
if( _spoil_scenario==11 )
epsf = fp_neginf;
double epsx = 0.0000000001;
if( _spoil_scenario==12 )
epsx = fp_nan;
if( _spoil_scenario==13 )
epsx = fp_posinf;
if( _spoil_scenario==14 )
epsx = fp_neginf;
double stpmax = 0.1;
if( _spoil_scenario==15 )
stpmax = fp_nan;
if( _spoil_scenario==16 )
stpmax = fp_posinf;
if( _spoil_scenario==17 )
stpmax = fp_neginf;
ae_int_t maxits = 0;
mincgstate state;
mincgreport rep;
// create and tune optimizer
mincgcreate(x, state);
mincgsetscale(state, s);
mincgsetcond(state, epsg, epsf, epsx, maxits);
mincgsetstpmax(state, stpmax);
// Set up OptGuard integrity checker which catches errors
// like nonsmooth targets or errors in the analytic gradient.
//
// OptGuard is essential at the early prototyping stages.
//
// NOTE: gradient verification needs 3*N additional function
// evaluations; DO NOT USE IT IN THE PRODUCTION CODE
// because it leads to unnecessary slowdown of your app.
mincgoptguardsmoothness(state);
mincgoptguardgradient(state, 0.001);
// first run
alglib::mincgoptimize(state, function1_grad);
mincgresults(state, x, rep);
_TestResult = _TestResult && doc_test_real_vector(x, "[-3,3]", 0.005);
// second run - algorithm is restarted with mincgrestartfrom()
x = "[10,10]";
if( _spoil_scenario==18 )
spoil_vector_by_nan(x);
if( _spoil_scenario==19 )
spoil_vector_by_posinf(x);
if( _spoil_scenario==20 )
spoil_vector_by_neginf(x);
mincgrestartfrom(state, x);
alglib::mincgoptimize(state, function1_grad);
mincgresults(state, x, rep);
_TestResult = _TestResult && doc_test_real_vector(x, "[-3,3]", 0.005);
// check OptGuard integrity report. Why do we need it at all?
// Well, try breaking the gradient by adding 1.0 to some
// of its components - OptGuard should report it as error.
// And it may also catch unintended errors too :)
optguardreport ogrep;
mincgoptguardresults(state, ogrep);
_TestResult = _TestResult && doc_test_bool(ogrep.badgradsuspected, false);
_TestResult = _TestResult && doc_test_bool(ogrep.nonc0suspected, false);
_TestResult = _TestResult && doc_test_bool(ogrep.nonc1suspected, false);
_TestResult = _TestResult && (_spoil_scenario==-1);
}
catch(ap_error)
{ _TestResult = _TestResult && (_spoil_scenario!=-1); }
}
if( !_TestResult)
{
printf("%-32s FAILED\n", "mincg_d_2");
fflush(stdout);
}
_TotalResult = _TotalResult && _TestResult;
//
// TEST mincg_numdiff
// Nonlinear optimization by CG with numerical differentiation
//
_TestResult = true;
for(_spoil_scenario=-1; _spoil_scenario<18; _spoil_scenario++)
{
try
{
//
// This example demonstrates minimization of
//
// f(x,y) = 100*(x+3)^4+(y-3)^4
//
// using numerical differentiation to calculate gradient.
//
// We also show how to use OptGuard integrity checker to catch common
// problem statement errors like accidentally specifying nonsmooth target
// function.
//
// First, we set up optimizer...
//
real_1d_array x = "[0,0]";
if( _spoil_scenario==0 )
spoil_vector_by_nan(x);
if( _spoil_scenario==1 )
spoil_vector_by_posinf(x);
if( _spoil_scenario==2 )
spoil_vector_by_neginf(x);
real_1d_array s = "[1,1]";
if( _spoil_scenario==3 )
spoil_vector_by_nan(s);
if( _spoil_scenario==4 )
spoil_vector_by_posinf(s);
if( _spoil_scenario==5 )
spoil_vector_by_neginf(s);
double epsg = 0;
if( _spoil_scenario==6 )
epsg = fp_nan;
if( _spoil_scenario==7 )
epsg = fp_posinf;
if( _spoil_scenario==8 )
epsg = fp_neginf;
double epsf = 0;
if( _spoil_scenario==9 )
epsf = fp_nan;
if( _spoil_scenario==10 )
epsf = fp_posinf;
if( _spoil_scenario==11 )
epsf = fp_neginf;
double epsx = 0.0000000001;
if( _spoil_scenario==12 )
epsx = fp_nan;
if( _spoil_scenario==13 )
epsx = fp_posinf;
if( _spoil_scenario==14 )
epsx = fp_neginf;
double diffstep = 1.0e-6;
if( _spoil_scenario==15 )
diffstep = fp_nan;
if( _spoil_scenario==16 )
diffstep = fp_posinf;
if( _spoil_scenario==17 )
diffstep = fp_neginf;
ae_int_t maxits = 0;
mincgstate state;
mincgcreatef(x, diffstep, state);
mincgsetcond(state, epsg, epsf, epsx, maxits);
mincgsetscale(state, s);
//
// Then, we activate OptGuard integrity checking.
//
// Numerical differentiation always produces "correct" gradient
// (with some truncation error, but unbiased). Thus, we just have
// to check smoothness properties of the target: C0 and C1 continuity.
//
// Sometimes user accidentally tried to solve nonsmooth problems
// with smooth optimizer. OptGuard helps to detect such situations
// early, at the prototyping stage.
//
mincgoptguardsmoothness(state);
//
// Now we are ready to run the optimization
//
mincgreport rep;
alglib::mincgoptimize(state, function1_func);
mincgresults(state, x, rep);
_TestResult = _TestResult && doc_test_real_vector(x, "[-3,3]", 0.005);
//
// ...and to check OptGuard integrity report.
//
// Want to challenge OptGuard? Try to make your problem
// nonsmooth by replacing 100*(x+3)^4 by 100*|x+3| and
// re-run optimizer.
//
optguardreport ogrep;
mincgoptguardresults(state, ogrep);
_TestResult = _TestResult && doc_test_bool(ogrep.nonc0suspected, false);
_TestResult = _TestResult && doc_test_bool(ogrep.nonc1suspected, false);
_TestResult = _TestResult && (_spoil_scenario==-1);
}
catch(ap_error)
{ _TestResult = _TestResult && (_spoil_scenario!=-1); }
}
if( !_TestResult)
{
printf("%-32s FAILED\n", "mincg_numdiff");
fflush(stdout);
}
_TotalResult = _TotalResult && _TestResult;
//
// TEST mindf_gdemo_auto
// Nonlinearly constrained differential evolution
//
_TestResult = true;
for(_spoil_scenario=-1; _spoil_scenario<18; _spoil_scenario++)
{
try
{
//
// This example demonstrates minimization of
//
// f(x0,x1) = x0+x1
//
// subject to nonlinear constraints
//
// x0^2 + x1^2 - 1 <= 0
// x2-exp(x0) = 0
//
real_1d_array x0 = "[0,0,0]";
if( _spoil_scenario==0 )
spoil_vector_by_nan(x0);
if( _spoil_scenario==1 )
spoil_vector_by_posinf(x0);
if( _spoil_scenario==2 )
spoil_vector_by_neginf(x0);
real_1d_array s = "[1,1,1]";
if( _spoil_scenario==3 )
spoil_vector_by_nan(s);
if( _spoil_scenario==4 )
spoil_vector_by_posinf(s);
if( _spoil_scenario==5 )
spoil_vector_by_neginf(s);
mindfstate state;
mindfreport rep;
real_1d_array x1;
//
// Create optimizer object
//
mindfcreate(x0, state);
mindfsetscale(state, s);
//
// Choose one of the nonlinear programming solvers supported by MINDF
// optimizer.
//
// This example shows how to use GDEMO (Generalized Differential Evolution,
// MultiObjective) solver working in a single-objective mode. This solver
// uses an adaptive choice of DE parameters (crossover, weight and strategy),
// automatically choosing the most appropriate settings during the optimization.
//
// Thus, the only tunable parameters are:
// * iterations count
// * population size
// * algorithm profile (ROBUST or QUICK)
//
// The latter two parameters can be omitted, the solver will use a default
// population size (10*N in the current version) and a default profile (ROBUST
// one).
//
// Metaheuristics typically need hundreds or thousands of iterations to converge,
// in this example we set 200 iterations. Good values for population size
// are between 5*N and 20*N, with 5*N being recommended for easy problems
// and 20*N (or more) being recommended for difficult problems with multiple
// extrema, many cross-interacting variables and/or noise.
//
// In addition to these parameters it is also possible to specify a so-called
// 'profile' - a set of recommendations regarding decisions algorithm is allowed
// to make during parameters autotuning:
// * the ROBUST profile is a default option. The solver tries only conservative
// strategies that have low probability of a failure (stagnation far away
// from the solution)
// * the QUICK profile is intended to facilitate accelerated convergence on
// medium-complexity problems at the cost of (sometimes) having premature
// convergence on difficult multi-extremal problems. It most often results
// in a 2x-3x higher convergence speed than the ROBUST profile.
//
ae_int_t maxits = 200;
mindfsetalgogdemo(state, maxits);
mindfsetgdemoprofilerobust(state);
//
// Unlike smooth solvers, metaheuristics do not have well-defined stopping criteria.
// It is recommended to run the solver until iterations budget is exhausted.
//
// However, it is possible to let the solver stop early, when either:
// * subpopulation target values (2N+1 best individuals) are within
// EPS from the best one so far (function values seem to converge)
// * or 2N+1-subpopulation target values AND variable values are within EPS from
// the best solution so far
//
// Both conditions are heuristics that may fail. The fact that many candidate
// objective values have clustered within EPS of the best objective value so far
// usually means that we are somewhere within [0.1EPS,10EPS] away from the true
// solution; however, on difficult problems this condition may fire too early.
//
// Imposing an additional requirement that variable values have clustered too
// may prevent us from premature stopping. However, on multi-extremal and/or
// noisy problems too many individuals may be trapped away from the optimum,
// preventing this condition from activation.
//
// The summary is that stopping criteria are heavily problem-dependent.
//
mindfsetcondf(state, 0.00001);
//
// Set nonlinear constraints.
//
// ALGLIB supports any combination of box, linear and nonlinear
// constraints. This specific example uses only nonlinear ones.
//
// Since version 4.01, ALGLIB supports the most general form of
// nonlinear constraints: two-sided constraints NL<=C(x)<=NU, with
// elements being possibly infinite (means that this specific bound is
// ignored). It includes equality constraints, upper/lower inequality
// constraints, range constraints. In particular, a pair of constraints
//
// x2-exp(x0) = 0
// x0^2 + x1^2 - 1 <= 0
//
// can be specified by passing NL=[0,-INF], NU=[0,0] to mindfsetnlc2().
// Constraining functions themselves are passed as a part of a problem
// target vector (see below).
//
//
// Unlike smooth optimizers like SQP which naturally include linear and
// nonlinear constraints into the algorithm, derivative-free methods
// often need special strategies to deal with them, with each strategy
// having its own limitations:
//
// * an L2 penalty, which has good global constraint enforcement
// properties, but usually allows some moderate constraint violation
//
// * an L1 penalty, which has potential to enforce constraints exactly,
// but has somewhat weaker ability to move iterations from far away
// points closer to the feasible area. It also has somewhat harder
// numerical properties, needing more iterations to converge.
//
// * a combined L1/L2 penalty, which is a good compromise
//
// The code below sets constraints bounds and tells the solver to use
// a mixed L1/L2 penalized strategy.
//
// NOTE: box constraints require no special handling.
//
real_1d_array nl = "[0,-inf]";
if( _spoil_scenario==6 )
spoil_vector_by_nan(nl);
if( _spoil_scenario==7 )
spoil_vector_by_adding_element(nl);
if( _spoil_scenario==8 )
spoil_vector_by_deleting_element(nl);
real_1d_array nu = "[0,0]";
if( _spoil_scenario==9 )
spoil_vector_by_nan(nu);
if( _spoil_scenario==10 )
spoil_vector_by_adding_element(nu);
if( _spoil_scenario==11 )
spoil_vector_by_deleting_element(nu);
double rho1 = 5;
if( _spoil_scenario==12 )
rho1 = fp_nan;
if( _spoil_scenario==13 )
rho1 = fp_posinf;
if( _spoil_scenario==14 )
rho1 = fp_neginf;
double rho2 = 5;
if( _spoil_scenario==15 )
rho2 = fp_nan;
if( _spoil_scenario==16 )
rho2 = fp_posinf;
if( _spoil_scenario==17 )
rho2 = fp_neginf;
mindfsetnlc2(state, nl, nu);
mindfsetgdemopenalty(state, rho1, rho2);
//
// Optimize and test results.
//
// The optimizer object accepts vector function with its first component
// being a target and subsequent components being nonlinear constraints.
//
// So, our vector function has the following form
//
// {f0,f1,f2} = { x0+x1 , x2-exp(x0) , x0^2+x1^2-1 }
//
// with f0 being target function, f1 being equality constraint "f1=0",
// f2 being inequality constraint "f2<=0".
//
//
//
// The commercial ALGLIB has two important improvements over the free edition:
// * callback parallelism
// * SIMD kernels for candidate points generation
//
// Differential evolution evaluates objectives/constraints in large batches.
// When the batch takes more than several milliseconds to process, it makes
// sense to compute function values at different points in parallel. It is
// called 'callback parallelism'.
//
// Another useful performance improvement is an ability to utilize SIMD for
// massive random numbers generation and mutation/crossover. The performance
// impact of these operations can be noticeable when solving problems with
// cheap objectives.
//
alglib::mindfoptimize(state, nlcfunc2_fvec);
mindfresults(state, x1, rep);
_TestResult = _TestResult && doc_test_real_vector(x1, "[-0.70710,-0.70710,0.49306]", 0.005);
_TestResult = _TestResult && (_spoil_scenario==-1);
}
catch(ap_error)
{ _TestResult = _TestResult && (_spoil_scenario!=-1); }
}
if( !_TestResult)
{
printf("%-32s FAILED\n", "mindf_gdemo_auto");
fflush(stdout);
}
_TotalResult = _TotalResult && _TestResult;
//
// TEST minlp_basic
// Basic linear programming example
//
_TestResult = true;
for(_spoil_scenario=-1; _spoil_scenario<15; _spoil_scenario++)
{
try
{
//
// This example demonstrates how to minimize
//
// F(x0,x1) = -0.1*x0 - x1
//
// subject to box constraints
//
// -1 <= x0,x1 <= +1
//
// and general linear constraints
//
// x0 - x1 >= -1
// x0 + x1 <= 1
//
// We use dual simplex solver provided by ALGLIB for this task. Box
// constraints are specified by means of constraint vectors bndl and
// bndu (we have bndl<=x<=bndu). General linear constraints are
// specified as AL<=A*x<=AU, with AL/AU being 2x1 vectors and A being
// 2x2 matrix.
//
// NOTE: some/all components of AL/AU can be +-INF, same applies to
// bndl/bndu. You can also have AL[I]=AU[i] (as well as
// BndL[i]=BndU[i]).
//
real_2d_array a = "[[1,-1],[1,+1]]";
if( _spoil_scenario==0 )
spoil_matrix_by_nan(a);
if( _spoil_scenario==1 )
spoil_matrix_by_deleting_row(a);
if( _spoil_scenario==2 )
spoil_matrix_by_deleting_col(a);
real_1d_array al = "[-1,-inf]";
if( _spoil_scenario==3 )
spoil_vector_by_nan(al);
if( _spoil_scenario==4 )
spoil_vector_by_deleting_element(al);
real_1d_array au = "[+inf,+1]";
if( _spoil_scenario==5 )
spoil_vector_by_nan(au);
if( _spoil_scenario==6 )
spoil_vector_by_deleting_element(au);
real_1d_array c = "[-0.1,-1]";
if( _spoil_scenario==7 )
spoil_vector_by_nan(c);
if( _spoil_scenario==8 )
spoil_vector_by_deleting_element(c);
real_1d_array s = "[1,1]";
if( _spoil_scenario==9 )
spoil_vector_by_nan(s);
if( _spoil_scenario==10 )
spoil_vector_by_deleting_element(s);
real_1d_array bndl = "[-1,-1]";
if( _spoil_scenario==11 )
spoil_vector_by_nan(bndl);
if( _spoil_scenario==12 )
spoil_vector_by_deleting_element(bndl);
real_1d_array bndu = "[+1,+1]";
if( _spoil_scenario==13 )
spoil_vector_by_nan(bndu);
if( _spoil_scenario==14 )
spoil_vector_by_deleting_element(bndu);
real_1d_array x;
minlpstate state;
minlpreport rep;
minlpcreate(2, state);
//
// Set cost vector, box constraints, general linear constraints.
//
// Box constraints can be set in one call to minlpsetbc() or minlpsetbcall()
// (latter sets same constraints for all variables and accepts two scalars
// instead of two vectors).
//
// General linear constraints can be specified in several ways:
// * minlpsetlc2dense() - accepts dense 2D array as input; sometimes this
// approach is more convenient, although less memory-efficient.
// * minlpsetlc2() - accepts sparse matrix as input
// * minlpaddlc2dense() - appends one row to the current set of constraints;
// row being appended is specified as dense vector
// * minlpaddlc2() - appends one row to the current set of constraints;
// row being appended is specified as sparse set of elements
// Independently from specific function being used, LP solver uses sparse
// storage format for internal representation of constraints.
//
minlpsetcost(state, c);
minlpsetbc(state, bndl, bndu);
minlpsetlc2dense(state, a, al, au, 2);
//
// Set scale of the parameters.
//
// It is strongly recommended that you set scale of your variables.
// Knowing their scales is essential for evaluation of stopping criteria
// and for preconditioning of the algorithm steps.
// You can find more information on scaling at http://www.alglib.net/optimization/scaling.php
//
minlpsetscale(state, s);
//
// Solve with the sparse IPM.
//
// Commercial ALGLIB can parallelize sparse Cholesky factorization which is the
// most time-consuming part of the algorithm. See the ALGLIB Reference Manual for
// more information on how to activate parallelism support.
//
minlpsetalgoipm(state);
minlpoptimize(state);
minlpresults(state, x, rep);
_TestResult = _TestResult && doc_test_real_vector(x, "[0,1]", 0.0005);
_TestResult = _TestResult && (_spoil_scenario==-1);
}
catch(ap_error)
{ _TestResult = _TestResult && (_spoil_scenario!=-1); }
}
if( !_TestResult)
{
printf("%-32s FAILED\n", "minlp_basic");
fflush(stdout);
}
_TotalResult = _TotalResult && _TestResult;
//
// TEST nls_derivative_free
// Nonlinear least squares optimization using derivative-free algorithms
//
_TestResult = true;
for(_spoil_scenario=-1; _spoil_scenario<13; _spoil_scenario++)
{
try
{
//
// This example demonstrates minimization of F(x0,x1) = f0^2+f1^2, where
//
// f0(x0,x1) = 10*(x0+3)^2
// f1(x0,x1) = (x1-3)^2
//
// subject to box constraints
//
// -1 <= x0 <= +1
// -1 <= x1 <= +1
//
// using DFO mode of the NLS optimizer.
//
// IMPORTANT: the NLS optimizer supports parallel model evaluation
// ('callback parallelism'). This feature, which is present in
// commercial ALGLIB editions, greatly accelerates optimization
// when using a solver which issues batch requests, i.e.
// multiple requests for target values, which can be computed
// independently by different threads.
//
// Callback parallelism is usually beneficial when processing
// a batch request requires more than several milliseconds.
// This particular example, of course, is not suited for
// callback parallelism.
//
// It also requires the solver which issues requests in
// convenient batches, e.g. 2PS solver.
//
// See ALGLIB Reference Manual, 'Working with commercial version'
// section, and comments on nlsoptimize() function for more
// information.
//
real_1d_array x = "[0,0]";
if( _spoil_scenario==0 )
spoil_vector_by_nan(x);
if( _spoil_scenario==1 )
spoil_vector_by_posinf(x);
if( _spoil_scenario==2 )
spoil_vector_by_neginf(x);
real_1d_array s = "[1,1]";
if( _spoil_scenario==3 )
spoil_vector_by_nan(s);
if( _spoil_scenario==4 )
spoil_vector_by_posinf(s);
if( _spoil_scenario==5 )
spoil_vector_by_neginf(s);
real_1d_array bndl = "[-1,-1]";
if( _spoil_scenario==6 )
spoil_vector_by_nan(bndl);
if( _spoil_scenario==7 )
spoil_vector_by_deleting_element(bndl);
real_1d_array bndu = "[+1,+1]";
if( _spoil_scenario==8 )
spoil_vector_by_nan(bndu);
if( _spoil_scenario==9 )
spoil_vector_by_deleting_element(bndu);
double epsx = 0.0000001;
if( _spoil_scenario==10 )
epsx = fp_nan;
if( _spoil_scenario==11 )
epsx = fp_posinf;
if( _spoil_scenario==12 )
epsx = fp_neginf;
ae_int_t maxits = 0;
nlsstate state;
nlsreport rep;
//
// Create optimizer, tell it to:
// * use derivative-free mode
// * use unit scale for all variables (s is a unit vector)
// * stop after short enough step (less than epsx)
//
nlscreatedfo(2, x, state);
nlssetcond(state, epsx, maxits);
nlssetscale(state, s);
nlssetbc(state, bndl, bndu);
//
// Choose a derivative-free nonlinear least squares algorithm. ALGLIB
// supports the following solvers:
//
// * DFO-LSA - a modified version of DFO-LS (Cartis, Fiala, Marteau,
// Roberts), with "A" standing for ALGLIB in order to distinguish it
// from the original version. This algorithm achieves the smallest
// function evaluations count, but has relatively high iteration
// overhead and no callback parallelism potential (it issues target
// evaluation requests one by one, so they can not be parallelized).
// Recommended for expensive targets with no parallelism support.
//
// * 2PS (two-point stencil) - an easily parallelized algorithm
// developed by ALGLIB Project. It needs about 3x-4x more target
// evaluations than DFO-LSA (the ratio has no strong dependence on
// the problem size), however it issues target evaluation requests
// in large batches, so they can be computed in parallel. Additionally
// it has low iteration overhead, so it can be better suited for
// problems with cheap targets that DFO-LSA.
//
// Both solvers demonstrate quadratic convergence similarly to the
// Levenberg-Marquardt method.
//
// The summary is:
// * expensive target, no parallelism => DFO-LSA
// * expensive target, parallel callbacks => 2PS
// * inexpensive target => most likely 2PS, maybe DFO-LSA
//
// The code below sets the algorithm to be DFO-LSA, then switches
// it to 2PS.
//
nlssetalgodfolsa(state);
nlssetalgo2ps(state);
//
// Solve the problem.
//
// The code below does not use parallelism. If you want to activate
// callback parallelism, use commercial edition of ALGLIB and pass
// alglib::parallelcallbacks as an additional parameter to nlsoptimize().
//
// Callback parallelism is intended for expensive problems where one
// batch (~N target evaluations) takes tens and hundreds of milliseconds
// to compute.
//
alglib::nlsoptimize(state, function1_fvec);
//
// Test optimization results
//
nlsresults(state, x, rep);
_TestResult = _TestResult && doc_test_real_vector(x, "[-1,+1]", 0.005);
_TestResult = _TestResult && (_spoil_scenario==-1);
}
catch(ap_error)
{ _TestResult = _TestResult && (_spoil_scenario!=-1); }
}
if( !_TestResult)
{
printf("%-32s FAILED\n", "nls_derivative_free");
fflush(stdout);
}
_TotalResult = _TotalResult && _TestResult;
//
// TEST minnlc_d_inequality
// Nonlinearly constrained optimization (inequality constraints)
//
_TestResult = true;
for(_spoil_scenario=-1; _spoil_scenario<15; _spoil_scenario++)
{
try
{
//
// This example demonstrates minimization of
//
// f(x0,x1) = -x0+x1
//
// subject to box constraints
//
// x0>=0, x1>=0
//
// and a nonlinear inequality constraint
//
// x0^2 + x1^2 - 1 <= 0
//
// IMPORTANT: the MINNLC optimizer supports parallel numerical
// differentiation ('callback parallelism'). This feature,
// which is present in commercial ALGLIB editions, greatly
// accelerates optimization with numerical differentiation of
// an expensive target functions.
//
// Callback parallelism is usually beneficial when computing a
// numerical gradient requires more than several milliseconds.
// This particular example, of course, is not suited for
// callback parallelism.
//
// See ALGLIB Reference Manual, 'Working with commercial version'
// section, and comments on minnlcoptimize() function for
// more information.
//
real_1d_array x0 = "[0,0]";
if( _spoil_scenario==0 )
spoil_vector_by_nan(x0);
if( _spoil_scenario==1 )
spoil_vector_by_posinf(x0);
if( _spoil_scenario==2 )
spoil_vector_by_neginf(x0);
real_1d_array s = "[1,1]";
if( _spoil_scenario==3 )
spoil_vector_by_nan(s);
if( _spoil_scenario==4 )
spoil_vector_by_posinf(s);
if( _spoil_scenario==5 )
spoil_vector_by_neginf(s);
double epsx = 0.000001;
if( _spoil_scenario==6 )
epsx = fp_nan;
if( _spoil_scenario==7 )
epsx = fp_posinf;
if( _spoil_scenario==8 )
epsx = fp_neginf;
ae_int_t maxits = 0;
minnlcstate state;
//
// Create optimizer object and tune its settings:
// * epsx=0.000001 stopping condition for inner iterations
// * s=[1,1] all variables have unit scale; it is important to
// tell optimizer about scales of your variables - it
// greatly accelerates convergence and helps to perform
// some important integrity checks.
//
minnlccreate(2, x0, state);
minnlcsetcond(state, epsx, maxits);
minnlcsetscale(state, s);
//
// Choose one of nonlinear programming solvers supported by MINNLC
// optimizer.
//
// As of ALGLIB 4.01, the default (and recommended) option is to use a
// large-scale filter-based SQP solver, which can utilize sparsity of the
// problem and uses a limited-memory BFGS update in order to be able to
// deal with thousands of variables.
//
// Other options include:
// * SQP-BFGS (the same filter SQP solver relying on a dense BFGS update,
// not intended for anything beyond 100 variables)
// * AUL2 solver (a large-scale augmented Lagrangian solver for problems
// with cheap target functions)
// * SL1QP and SL1QP-BFGS legacy solvers which are similar to filter-based
// SQP/SQP-BFGS, but use a less robust L1 merit function to handle
// constraints
// * SLP a legacy sequential linear programming solver, scales badly
// beyond several tens of variables).
//
minnlcsetalgosqp(state);
//
// Set constraints:
//
// 1. box constraints are passed with minnlcsetbc() call. The solver also
// supports linear constraints with minnlcsetlc().
//
// 2. nonlinear constraints are more tricky - you can not "pack" a general
// nonlinear function into a double precision array. That's why
// minnlcsetnlc2() does not accept constraints itself - only constraint
// bounds are passed.
//
// Since version 4.01, ALGLIB supports the most general form of
// nonlinear constraints: two-sided constraints NL<=C(x)<=NU, with
// elements being possibly infinite (means that this specific bound is
// ignored). It includes equality constraints, upper/lower inequality
// constraints, range constraints. In particular, the constraint
//
// x0^2 + x1^2 - 1 <= 0
//
// can be specified by passing NL=[-INF], NU=[0] to minnlcsetnlc2().
//
// Constraining functions themselves are passed as part of a problem
// Jacobian (see below).
//
real_1d_array bndl = "[0,0]";
real_1d_array bndu = "[+inf,+inf]";
real_1d_array nl = "[-inf]";
if( _spoil_scenario==9 )
spoil_vector_by_nan(nl);
if( _spoil_scenario==10 )
spoil_vector_by_adding_element(nl);
if( _spoil_scenario==11 )
spoil_vector_by_deleting_element(nl);
real_1d_array nu = "[0]";
if( _spoil_scenario==12 )
spoil_vector_by_nan(nu);
if( _spoil_scenario==13 )
spoil_vector_by_adding_element(nu);
if( _spoil_scenario==14 )
spoil_vector_by_deleting_element(nu);
minnlcsetbc(state, bndl, bndu);
minnlcsetnlc2(state, nl, nu);
//
// Optimize and test results.
//
// Optimizer object accepts vector function and its Jacobian, with first
// component (Jacobian row) being target function, and next components
// (Jacobian rows) being nonlinear constraints.
//
// So, our vector function has form
//
// {f0,f1} = { -x0+x1 , x0^2+x1^2-1 }
//
// with Jacobian
//
// [ -1 +1 ]
// J = [ ]
// [ 2*x0 2*x1 ]
//
// with f0 being target function, f1 being constraining function. Number
// of equality/inequality constraints is specified by minnlcsetnlc2().
//
minnlcreport rep;
real_1d_array x1;
alglib::minnlcoptimize(state, nlcfunc1_jac);
minnlcresults(state, x1, rep);
_TestResult = _TestResult && doc_test_real_vector(x1, "[1.0000,0.0000]", 0.005);
_TestResult = _TestResult && (_spoil_scenario==-1);
}
catch(ap_error)
{ _TestResult = _TestResult && (_spoil_scenario!=-1); }
}
if( !_TestResult)
{
printf("%-32s FAILED\n", "minnlc_d_inequality");
fflush(stdout);
}
_TotalResult = _TotalResult && _TestResult;
//
// TEST minnlc_d_equality
// Nonlinearly constrained optimization (equality constraints)
//
_TestResult = true;
for(_spoil_scenario=-1; _spoil_scenario<15; _spoil_scenario++)
{
try
{
//
// This example demonstrates minimization of
//
// f(x0,x1) = -x0+x1
//
// subject to nonlinear equality constraint
//
// x0^2 + x1^2 - 1 = 0
//
// IMPORTANT: the MINNLC optimizer supports parallel numerical
// differentiation ('callback parallelism'). This feature,
// which is present in commercial ALGLIB editions, greatly
// accelerates optimization with numerical differentiation of
// an expensive target functions.
//
// Callback parallelism is usually beneficial when computing a
// numerical gradient requires more than several milliseconds.
// This particular example, of course, is not suited for
// callback parallelism.
//
// See ALGLIB Reference Manual, 'Working with commercial version'
// section, and comments on minnlcoptimize() function for
// more information.
//
real_1d_array x0 = "[1,1]";
if( _spoil_scenario==0 )
spoil_vector_by_nan(x0);
if( _spoil_scenario==1 )
spoil_vector_by_posinf(x0);
if( _spoil_scenario==2 )
spoil_vector_by_neginf(x0);
real_1d_array s = "[1,1]";
if( _spoil_scenario==3 )
spoil_vector_by_nan(s);
if( _spoil_scenario==4 )
spoil_vector_by_posinf(s);
if( _spoil_scenario==5 )
spoil_vector_by_neginf(s);
double epsx = 0.000001;
if( _spoil_scenario==6 )
epsx = fp_nan;
if( _spoil_scenario==7 )
epsx = fp_posinf;
if( _spoil_scenario==8 )
epsx = fp_neginf;
ae_int_t maxits = 0;
minnlcstate state;
//
// Create optimizer object and tune its settings:
// * epsx=0.000001 stopping condition for inner iterations
// * s=[1,1] all variables have unit scale
//
minnlccreate(2, x0, state);
minnlcsetcond(state, epsx, maxits);
minnlcsetscale(state, s);
//
// Choose one of nonlinear programming solvers supported by MINNLC
// optimizer.
//
// As of ALGLIB 4.01, the default (and recommended) option is to use a
// large-scale filter-based SQP solver, which can utilize sparsity of the
// problem and uses a limited-memory BFGS update in order to be able to
// deal with thousands of variables.
//
// Other options include:
// * SQP-BFGS (the same filter SQP solver relying on a dense BFGS update,
// not intended for anything beyond 100 variables)
// * AUL2 solver (a large-scale augmented Lagrangian solver for problems
// with cheap target functions)
// * SL1QP and SL1QP-BFGS legacy solvers which are similar to filter-based
// SQP/SQP-BFGS, but use a less robust L1 merit function to handle
// constraints
// * SLP a legacy sequential linear programming solver, scales badly
// beyond several tens of variables).
//
minnlcsetalgosqp(state);
//
// Set constraints:
//
// Since version 4.01, ALGLIB supports the most general form of
// nonlinear constraints: two-sided constraints NL<=C(x)<=NU, with
// elements being possibly infinite (means that this specific bound is
// ignored). It includes equality constraints, upper/lower inequality
// constraints, range constraints. In particular, the constraint
//
// x0^2 + x1^2 - 1 = 0
//
// can be specified by passing NL=[0], NU=[0] to minnlcsetnlc2().
//
// Constraining functions themselves are passed as part of a problem
// Jacobian (see below).
//
real_1d_array nl = "[0]";
if( _spoil_scenario==9 )
spoil_vector_by_nan(nl);
if( _spoil_scenario==10 )
spoil_vector_by_adding_element(nl);
if( _spoil_scenario==11 )
spoil_vector_by_deleting_element(nl);
real_1d_array nu = "[0]";
if( _spoil_scenario==12 )
spoil_vector_by_nan(nu);
if( _spoil_scenario==13 )
spoil_vector_by_adding_element(nu);
if( _spoil_scenario==14 )
spoil_vector_by_deleting_element(nu);
minnlcsetnlc2(state, nl, nu);
//
// Optimize and test results.
//
// Optimizer object accepts vector function and its Jacobian, with first
// component (Jacobian row) being target function, and next components
// (Jacobian rows) being nonlinear equality and inequality constraints.
//
// So, our vector function has form
//
// {f0,f1} = { -x0+x1 , x0^2+x1^2-1 }
//
// with Jacobian
//
// [ -1 +1 ]
// J = [ ]
// [ 2*x0 2*x1 ]
//
// with f0 being target function, f1 being constraining function. Number
// of equality/inequality constraints is specified by minnlcsetnlc2().
//
minnlcreport rep;
real_1d_array x1;
alglib::minnlcoptimize(state, nlcfunc1_jac);
minnlcresults(state, x1, rep);
_TestResult = _TestResult && doc_test_real_vector(x1, "[0.70710,-0.70710]", 0.005);
_TestResult = _TestResult && (_spoil_scenario==-1);
}
catch(ap_error)
{ _TestResult = _TestResult && (_spoil_scenario!=-1); }
}
if( !_TestResult)
{
printf("%-32s FAILED\n", "minnlc_d_equality");
fflush(stdout);
}
_TotalResult = _TotalResult && _TestResult;
//
// TEST minnlc_d_mixed
// Nonlinearly constrained optimization with mixed equality/inequality constraints
//
printf("50/165\n");
_TestResult = true;
for(_spoil_scenario=-1; _spoil_scenario<15; _spoil_scenario++)
{
try
{
//
// This example demonstrates minimization of
//
// f(x0,x1) = x0+x1
//
// subject to nonlinear inequality constraint
//
// x0^2 + x1^2 - 1 <= 0
//
// and nonlinear equality constraint
//
// x2-exp(x0) = 0
//
// IMPORTANT: the MINNLC optimizer supports parallel numerical
// differentiation ('callback parallelism'). This feature,
// which is present in commercial ALGLIB editions, greatly
// accelerates optimization with numerical differentiation of
// an expensive target functions.
//
// Callback parallelism is usually beneficial when computing a
// numerical gradient requires more than several milliseconds.
// This particular example, of course, is not suited for
// callback parallelism.
//
// See ALGLIB Reference Manual, 'Working with commercial version'
// section, and comments on minnlcoptimize() function for
// more information.
//
real_1d_array x0 = "[0,0,0]";
if( _spoil_scenario==0 )
spoil_vector_by_nan(x0);
if( _spoil_scenario==1 )
spoil_vector_by_posinf(x0);
if( _spoil_scenario==2 )
spoil_vector_by_neginf(x0);
real_1d_array s = "[1,1,1]";
if( _spoil_scenario==3 )
spoil_vector_by_nan(s);
if( _spoil_scenario==4 )
spoil_vector_by_posinf(s);
if( _spoil_scenario==5 )
spoil_vector_by_neginf(s);
double epsx = 0.000001;
if( _spoil_scenario==6 )
epsx = fp_nan;
if( _spoil_scenario==7 )
epsx = fp_posinf;
if( _spoil_scenario==8 )
epsx = fp_neginf;
ae_int_t maxits = 0;
minnlcstate state;
minnlcreport rep;
real_1d_array x1;
//
// Create optimizer object and tune its settings:
// * epsx=0.000001 stopping condition for inner iterations
// * s=[1,1] all variables have unit scale
// * upper limit on step length is specified (to avoid probing locations where exp() is large)
//
minnlccreate(3, x0, state);
minnlcsetcond(state, epsx, maxits);
minnlcsetscale(state, s);
minnlcsetstpmax(state, 10.0);
//
// Choose one of nonlinear programming solvers supported by MINNLC
// optimizer.
//
// As of ALGLIB 4.01, the default (and recommended) option is to use a
// large-scale filter-based SQP solver, which can utilize sparsity of the
// problem and uses a limited-memory BFGS update in order to be able to
// deal with thousands of variables.
//
// Other options include:
// * SQP-BFGS (the same filter SQP solver relying on a dense BFGS update,
// not intended for anything beyond 100 variables)
// * AUL2 solver (a large-scale augmented Lagrangian solver for problems
// with cheap target functions)
// * SL1QP and SL1QP-BFGS legacy solvers which are similar to filter-based
// SQP/SQP-BFGS, but use a less robust L1 merit function to handle
// constraints
// * SLP a legacy sequential linear programming solver, scales badly
// beyond several tens of variables).
//
minnlcsetalgosqp(state);
//
// Set constraints:
//
// Since version 4.01, ALGLIB supports the most general form of
// nonlinear constraints: two-sided constraints NL<=C(x)<=NU, with
// elements being possibly infinite (means that this specific bound is
// ignored). It includes equality constraints, upper/lower inequality
// constraints, range constraints. In particular, a pair of constraints
//
// x2-exp(x0) = 0
// x0^2 + x1^2 - 1 <= 0
//
// can be specified by passing NL=[0,-INF], NU=[0,0] to minnlcsetnlc2().
//
// Constraining functions themselves are passed as part of a problem
// Jacobian (see below).
//
real_1d_array nl = "[0,-inf]";
if( _spoil_scenario==9 )
spoil_vector_by_nan(nl);
if( _spoil_scenario==10 )
spoil_vector_by_adding_element(nl);
if( _spoil_scenario==11 )
spoil_vector_by_deleting_element(nl);
real_1d_array nu = "[0,0]";
if( _spoil_scenario==12 )
spoil_vector_by_nan(nu);
if( _spoil_scenario==13 )
spoil_vector_by_adding_element(nu);
if( _spoil_scenario==14 )
spoil_vector_by_deleting_element(nu);
minnlcsetnlc2(state, nl, nu);
//
// Optimize and test results.
//
// Optimizer object accepts vector function and its Jacobian, with first
// component (Jacobian row) being target function, and next components
// (Jacobian rows) being nonlinear equality and inequality constraints.
//
// So, our vector function has form
//
// {f0,f1,f2} = { x0+x1 , x2-exp(x0) , x0^2+x1^2-1 }
//
// with Jacobian
//
// [ +1 +1 0 ]
// J = [-exp(x0) 0 1 ]
// [ 2*x0 2*x1 0 ]
//
// with f0 being target function, f1 being equality constraint "f1=0",
// f2 being inequality constraint "f2<=0".
//
alglib::minnlcoptimize(state, nlcfunc2_jac);
minnlcresults(state, x1, rep);
_TestResult = _TestResult && doc_test_real_vector(x1, "[-0.70710,-0.70710,0.49306]", 0.005);
_TestResult = _TestResult && (_spoil_scenario==-1);
}
catch(ap_error)
{ _TestResult = _TestResult && (_spoil_scenario!=-1); }
}
if( !_TestResult)
{
printf("%-32s FAILED\n", "minnlc_d_mixed");
fflush(stdout);
}
_TotalResult = _TotalResult && _TestResult;
//
// TEST minnlc_d_sparse
// Nonlinearly constrained optimization with sparse Jacobian
//
_TestResult = true;
for(_spoil_scenario=-1; _spoil_scenario<15; _spoil_scenario++)
{
try
{
//
// This example demonstrates minimization of
//
// f(x0,x1) = x0+x1
//
// subject to nonlinear inequality constraint
//
// x0^2 + x1^2 - 1 <= 0
//
// and nonlinear equality constraint
//
// x2-exp(x0) = 0
//
// with their Jacobian being a sparse matrix.
//
// IMPORTANT: the MINNLC optimizer supports parallel numerical
// differentiation ('callback parallelism'). This feature,
// which is present in commercial ALGLIB editions, greatly
// accelerates optimization with numerical differentiation of
// an expensive target functions.
//
// Callback parallelism is usually beneficial when computing a
// numerical gradient requires more than several milliseconds.
// This particular example, of course, is not suited for
// callback parallelism.
//
// See ALGLIB Reference Manual, 'Working with commercial version'
// section, and comments on minnlcoptimize() function for
// more information.
//
real_1d_array x0 = "[0,0,0]";
if( _spoil_scenario==0 )
spoil_vector_by_nan(x0);
if( _spoil_scenario==1 )
spoil_vector_by_posinf(x0);
if( _spoil_scenario==2 )
spoil_vector_by_neginf(x0);
real_1d_array s = "[1,1,1]";
if( _spoil_scenario==3 )
spoil_vector_by_nan(s);
if( _spoil_scenario==4 )
spoil_vector_by_posinf(s);
if( _spoil_scenario==5 )
spoil_vector_by_neginf(s);
double epsx = 0.000001;
if( _spoil_scenario==6 )
epsx = fp_nan;
if( _spoil_scenario==7 )
epsx = fp_posinf;
if( _spoil_scenario==8 )
epsx = fp_neginf;
ae_int_t maxits = 0;
minnlcstate state;
minnlcreport rep;
real_1d_array x1;
//
// Create optimizer object and tune its settings:
// * epsx=0.000001 stopping condition for inner iterations
// * s=[1,1] all variables have unit scale
// * upper limit on step length is specified (to avoid probing locations where exp() is large)
//
minnlccreate(3, x0, state);
minnlcsetcond(state, epsx, maxits);
minnlcsetscale(state, s);
minnlcsetstpmax(state, 10.0);
//
// Choose one of nonlinear programming solvers supported by MINNLC
// optimizer.
//
// As of ALGLIB 4.02, the only solve which is fully sparse-capable is a
// large-scale filter-based SQP solver, which can utilize sparsity of the
// problem and uses a limited-memory BFGS update in order to be able to
// deal with thousands of variables.
//
minnlcsetalgosqp(state);
//
// Set constraints:
//
// Since version 4.01, ALGLIB supports the most general form of
// nonlinear constraints: two-sided constraints NL<=C(x)<=NU, with
// elements being possibly infinite (means that this specific bound is
// ignored). It includes equality constraints, upper/lower inequality
// constraints, range constraints. In particular, a pair of constraints
//
// x2-exp(x0) = 0
// x0^2 + x1^2 - 1 <= 0
//
// can be specified by passing NL=[0,-INF], NU=[0,0] to minnlcsetnlc2().
//
// Constraining functions themselves are passed as part of a problem
// Jacobian (see below).
//
real_1d_array nl = "[0,-inf]";
if( _spoil_scenario==9 )
spoil_vector_by_nan(nl);
if( _spoil_scenario==10 )
spoil_vector_by_adding_element(nl);
if( _spoil_scenario==11 )
spoil_vector_by_deleting_element(nl);
real_1d_array nu = "[0,0]";
if( _spoil_scenario==12 )
spoil_vector_by_nan(nu);
if( _spoil_scenario==13 )
spoil_vector_by_adding_element(nu);
if( _spoil_scenario==14 )
spoil_vector_by_deleting_element(nu);
minnlcsetnlc2(state, nl, nu);
//
// Optimize and test results.
//
// Optimizer object accepts vector function and its Jacobian, with first
// component (Jacobian row) being target function, and next components
// (Jacobian rows) being nonlinear equality and inequality constraints.
//
// So, our vector function has form
//
// {f0,f1,f2} = { x0+x1 , x2-exp(x0) , x0^2+x1^2-1 }
//
// with Jacobian
//
// [ +1 +1 0 ]
// J = [-exp(x0) 0 1 ]
// [ 2*x0 2*x1 0 ]
//
// with f0 being target function, f1 being equality constraint "f1=0",
// f2 being inequality constraint "f2<=0". The Jacobian is store as a
// sparse matrix. See comments on the callback for more information
// about working with sparse Jacobians.
//
alglib::minnlcoptimize(state, nlcfunc2_sjac);
minnlcresults(state, x1, rep);
_TestResult = _TestResult && doc_test_real_vector(x1, "[-0.70710,-0.70710,0.49306]", 0.005);
_TestResult = _TestResult && (_spoil_scenario==-1);
}
catch(ap_error)
{ _TestResult = _TestResult && (_spoil_scenario!=-1); }
}
if( !_TestResult)
{
printf("%-32s FAILED\n", "minnlc_d_sparse");
fflush(stdout);
}
_TotalResult = _TotalResult && _TestResult;
//
// TEST minnlc_d_numdiff
// Nonlinearly constrained optimization with numerical differentiation
//
_TestResult = true;
for(_spoil_scenario=-1; _spoil_scenario<22; _spoil_scenario++)
{
try
{
//
// This example demonstrates minimization of
//
// f(x0,x1) = x0+x1
//
// subject to box constraints
//
// 0<=x0<+inf, -inf<x1<+int, -inf<x2<+int
//
// nonlinear inequality constraint
//
// x0^2 + x1^2 - 1 <= 0
//
// and nonlinear equality constraint
//
// x2-exp(x0) = 0
//
// using numerical differentiation.
//
// IMPORTANT: the MINNLC optimizer supports parallel numerical
// differentiation ('callback parallelism'). This feature,
// which is present in commercial ALGLIB editions, greatly
// accelerates optimization with numerical differentiation of
// an expensive target functions.
//
// Callback parallelism is usually beneficial when computing a
// numerical gradient requires more than several milliseconds.
// This particular example, of course, is not suited for
// callback parallelism.
//
// See ALGLIB Reference Manual, 'Working with commercial version'
// section, and comments on minnlcoptimize() function for
// more information.
//
real_1d_array x0 = "[0,0,0]";
if( _spoil_scenario==0 )
spoil_vector_by_nan(x0);
if( _spoil_scenario==1 )
spoil_vector_by_posinf(x0);
if( _spoil_scenario==2 )
spoil_vector_by_neginf(x0);
real_1d_array s = "[1,1,1]";
if( _spoil_scenario==3 )
spoil_vector_by_nan(s);
if( _spoil_scenario==4 )
spoil_vector_by_posinf(s);
if( _spoil_scenario==5 )
spoil_vector_by_neginf(s);
double epsx = 0.000001;
if( _spoil_scenario==6 )
epsx = fp_nan;
if( _spoil_scenario==7 )
epsx = fp_posinf;
if( _spoil_scenario==8 )
epsx = fp_neginf;
double diffstep = 0.000001;
if( _spoil_scenario==9 )
diffstep = fp_nan;
if( _spoil_scenario==10 )
diffstep = fp_posinf;
if( _spoil_scenario==11 )
diffstep = fp_neginf;
ae_int_t maxits = 0;
minnlcstate state;
minnlcreport rep;
real_1d_array x1;
//
// Create optimizer object and tune its settings:
// * epsx=0.000001 stopping condition for inner iterations
// * diffstep=0.000001 numerical differentiation step (times variable scales)
// * s=[1,1] all variables have unit scale
//
minnlccreatef(3, x0, diffstep, state);
minnlcsetcond(state, epsx, maxits);
minnlcsetscale(state, s);
minnlcsetstpmax(state, 10.0);
//
// Choose one of nonlinear programming solvers supported by MINNLC
// optimizer.
//
// As of ALGLIB 4.01, the default (and recommended) option is to use a
// large-scale filter-based SQP solver, which can utilize sparsity of the
// problem and uses a limited-memory BFGS update in order to be able to
// deal with thousands of variables.
//
minnlcsetalgosqp(state);
//
// Set box constraints. ALGLIB respects box constraints and does not
// evaluate target outside of a box-constrained area, even during
// numerical differentiation. The finite difference formula is modified
// according to the current box constraints, if necessary.
//
real_1d_array bndl = "[0,-inf,-inf]";
if( _spoil_scenario==12 )
spoil_vector_by_nan(bndl);
if( _spoil_scenario==13 )
spoil_vector_by_deleting_element(bndl);
real_1d_array bndu = "[+inf,+inf,+inf]";
if( _spoil_scenario==14 )
spoil_vector_by_nan(bndu);
if( _spoil_scenario==15 )
spoil_vector_by_deleting_element(bndu);
minnlcsetbc(state, bndl, bndu);
//
// Set nonlinear constraints:
//
// Since version 4.01, ALGLIB supports the most general form of
// nonlinear constraints: two-sided constraints NL<=C(x)<=NU, with
// elements being possibly infinite (means that this specific bound is
// ignored). It includes equality constraints, upper/lower inequality
// constraints, range constraints. In particular, a pair of constraints
//
// x2-exp(x0) = 0
// x0^2 + x1^2 - 1 <= 0
//
// can be specified by passing NL=[0,-INF], NU=[0,0] to minnlcsetnlc2().
//
// Constraining functions themselves are passed as part of a problem
// function vector (see below).
//
real_1d_array nl = "[0,-inf]";
if( _spoil_scenario==16 )
spoil_vector_by_nan(nl);
if( _spoil_scenario==17 )
spoil_vector_by_adding_element(nl);
if( _spoil_scenario==18 )
spoil_vector_by_deleting_element(nl);
real_1d_array nu = "[0,0]";
if( _spoil_scenario==19 )
spoil_vector_by_nan(nu);
if( _spoil_scenario==20 )
spoil_vector_by_adding_element(nu);
if( _spoil_scenario==21 )
spoil_vector_by_deleting_element(nu);
minnlcsetnlc2(state, nl, nu);
//
// Optimize and test results.
//
// Optimizer object accepts vector function but not its Jacobian, with
// numerical differentiation used to compute Jacobian values. The
// first component of the function vector is a target function, and
// the next components are nonlinear constraints.
//
// So, our vector function has the form
//
// {f0,f1,f2} = { x0+x1 , x2-exp(x0) , x0^2+x1^2-1 }
//
// with f0 being target function, f1 being equality constraint "f1=0",
// f2 being inequality constraint "f2<=0".
//
alglib::minnlcoptimize(state, nlcfunc2_fvec);
minnlcresults(state, x1, rep);
_TestResult = _TestResult && doc_test_real_vector(x1, "[0,-1,1]", 0.005);
_TestResult = _TestResult && (_spoil_scenario==-1);
}
catch(ap_error)
{ _TestResult = _TestResult && (_spoil_scenario!=-1); }
}
if( !_TestResult)
{
printf("%-32s FAILED\n", "minnlc_d_numdiff");
fflush(stdout);
}
_TotalResult = _TotalResult && _TestResult;
//
// TEST minnlc_sparse_corner_cases
// Tests corner cases for sparse numdiff
//
_TestResult = true;
for(_spoil_scenario=-1; _spoil_scenario<22; _spoil_scenario++)
{
try
{
//
// Test numerical differentiation at the bounds of a box-constrained area.
// The AUL2 solver issues requests at the bounds slightly differently from (F)SQP,
// testing rarely used branches of the RCOMM-V2 interface.
//
real_1d_array x0 = "[0,0,0]";
if( _spoil_scenario==0 )
spoil_vector_by_nan(x0);
if( _spoil_scenario==1 )
spoil_vector_by_posinf(x0);
if( _spoil_scenario==2 )
spoil_vector_by_neginf(x0);
real_1d_array s = "[1,1,1]";
if( _spoil_scenario==3 )
spoil_vector_by_nan(s);
if( _spoil_scenario==4 )
spoil_vector_by_posinf(s);
if( _spoil_scenario==5 )
spoil_vector_by_neginf(s);
double epsx = 0.000001;
if( _spoil_scenario==6 )
epsx = fp_nan;
if( _spoil_scenario==7 )
epsx = fp_posinf;
if( _spoil_scenario==8 )
epsx = fp_neginf;
double diffstep = 0.000001;
if( _spoil_scenario==9 )
diffstep = fp_nan;
if( _spoil_scenario==10 )
diffstep = fp_posinf;
if( _spoil_scenario==11 )
diffstep = fp_neginf;
ae_int_t maxits = 0;
minnlcstate state;
minnlcreport rep;
real_1d_array x1;
minnlccreatef(3, x0, diffstep, state);
minnlcsetcond(state, epsx, maxits);
minnlcsetscale(state, s);
minnlcsetstpmax(state, 10.0);
minnlcsetalgoaul2(state, 5);
real_1d_array bndl = "[0,-inf,-inf]";
if( _spoil_scenario==12 )
spoil_vector_by_nan(bndl);
if( _spoil_scenario==13 )
spoil_vector_by_deleting_element(bndl);
real_1d_array bndu = "[+inf,+inf,+inf]";
if( _spoil_scenario==14 )
spoil_vector_by_nan(bndu);
if( _spoil_scenario==15 )
spoil_vector_by_deleting_element(bndu);
minnlcsetbc(state, bndl, bndu);
real_1d_array nl = "[0,-inf]";
if( _spoil_scenario==16 )
spoil_vector_by_nan(nl);
if( _spoil_scenario==17 )
spoil_vector_by_adding_element(nl);
if( _spoil_scenario==18 )
spoil_vector_by_deleting_element(nl);
real_1d_array nu = "[0,0]";
if( _spoil_scenario==19 )
spoil_vector_by_nan(nu);
if( _spoil_scenario==20 )
spoil_vector_by_adding_element(nu);
if( _spoil_scenario==21 )
spoil_vector_by_deleting_element(nu);
minnlcsetnlc2(state, nl, nu);
alglib::minnlcoptimize(state, nlcfunc2_fvec);
minnlcresults(state, x1, rep);
_TestResult = _TestResult && doc_test_real_vector(x1, "[0,-1,1]", 0.005);
_TestResult = _TestResult && (_spoil_scenario==-1);
}
catch(ap_error)
{ _TestResult = _TestResult && (_spoil_scenario!=-1); }
}
if( !_TestResult)
{
printf("%-32s FAILED\n", "minnlc_sparse_corner_cases");
fflush(stdout);
}
_TotalResult = _TotalResult && _TestResult;
//
// TEST minmo_biobjective
// Unconstrained biobjective optimization
//
_TestResult = true;
for(_spoil_scenario=-1; _spoil_scenario<4; _spoil_scenario++)
{
try
{
//
// This example demonstrates minimization of two targets
//
// f0(x0,x1) = x0^2 + (x1-1)^2
// f1(x0,x1) = (x0-1(^2 + x1^2
//
// These targets are Euclidean distances to (0,1) and (1,0) respectively, thus solutions
// to this problem occupy the straight line segment connecting these points. (Points
// outside of the line are Pareto non-optimal because one can always decrease both distances
// by moving closer to the line).
//
ae_int_t nvars = 2;
ae_int_t nobjectives = 2;
real_1d_array x0 = "[0,0]";
if( _spoil_scenario==0 )
spoil_vector_by_nan(x0);
if( _spoil_scenario==1 )
spoil_vector_by_posinf(x0);
if( _spoil_scenario==2 )
spoil_vector_by_neginf(x0);
if( _spoil_scenario==3 )
spoil_vector_by_deleting_element(x0);
ae_int_t frontsize = 10;
bool polishsolutions = true;
minmostate state;
minmocreate(nvars, nobjectives, x0, state);
//
// The solver is configured to compute 10 points approximating the Pareto front,
// and to polish solutions (i.e. use an additional optimization phase that improves
// accuracy on degenerate problems; not actually necessary for this simple example).
//
minmosetalgonbi(state, frontsize, polishsolutions);
//
// Optimize and test results.
//
// The optimization is performed using analytic (user-provided) Jacobian matrix.
// Use minmocreatef(), if you do not know analytic form of the Jacobian and want
// ALGLIB to perform numerical differentiation.
//
// We requested 10 Pareto-optimal points and we expect solver to compute all points
// (it is possible to return less if the solver was terminated)
//
minmoreport rep;
real_2d_array paretofront;
alglib::minmooptimize(state, multiobjective2_jac);
minmoresults(state, paretofront, frontsize, rep);
_TestResult = _TestResult && doc_test_int(frontsize, 10);
_TestResult = _TestResult && (_spoil_scenario==-1);
}
catch(ap_error)
{ _TestResult = _TestResult && (_spoil_scenario!=-1); }
}
if( !_TestResult)
{
printf("%-32s FAILED\n", "minmo_biobjective");
fflush(stdout);
}
_TotalResult = _TotalResult && _TestResult;
//
// TEST minmo_biobjective_constr
// Nonlinearly constrained biobjective optimization
//
_TestResult = true;
for(_spoil_scenario=-1; _spoil_scenario<8; _spoil_scenario++)
{
try
{
//
// This example demonstrates minimization of two targets
//
// f0(x0,x1) = x0^2 + (x1-1)^2
// f1(x0,x1) = (x0-1(^2 + x1^2
//
// subject to a nonlinear constraint
//
// f2(x0,x1) = x0^2 + x1^2 >= 1
//
// These targets are Euclidean distances to (0,1) and (1,0) respectively, thus solutions to this
// problem should occupy the straight line segment connecting these points. However, due to the
// nonlinear constraint being present, Pareto front has another shape.
//
ae_int_t nvars = 2;
ae_int_t nobjectives = 2;
real_1d_array x0 = "[0,0]";
if( _spoil_scenario==0 )
spoil_vector_by_nan(x0);
if( _spoil_scenario==1 )
spoil_vector_by_posinf(x0);
if( _spoil_scenario==2 )
spoil_vector_by_neginf(x0);
if( _spoil_scenario==3 )
spoil_vector_by_deleting_element(x0);
ae_int_t frontsize = 10;
bool polishsolutions = true;
real_1d_array lowerbnd = "[1]";
if( _spoil_scenario==4 )
spoil_vector_by_nan(lowerbnd);
if( _spoil_scenario==5 )
spoil_vector_by_deleting_element(lowerbnd);
real_1d_array upperbnd = "[+inf]";
if( _spoil_scenario==6 )
spoil_vector_by_nan(upperbnd);
if( _spoil_scenario==7 )
spoil_vector_by_deleting_element(upperbnd);
minmostate state;
minmocreate(nvars, nobjectives, x0, state);
minmosetnlc2(state, lowerbnd, upperbnd, 1);
//
// The solver is configured to compute 10 points approximating the Pareto front,
// and to polish solutions (i.e. use an additional optimization phase that improves
// accuracy on degenerate problems; not actually necessary for this simple example).
//
minmosetalgonbi(state, frontsize, polishsolutions);
//
// Optimize and test results.
//
// The optimization is performed using analytic (user-provided) Jacobian matrix.
// Use minmocreatef(), if you do not know analytic form of the Jacobian and want
// ALGLIB to perform numerical differentiation.
//
// We requested 10 Pareto-optimal points and we expect solver to compute all points
// (it is possible to return less if the solver was terminated)
//
minmoreport rep;
real_2d_array paretofront;
alglib::minmooptimize(state, multiobjective2constr_jac);
minmoresults(state, paretofront, frontsize, rep);
_TestResult = _TestResult && doc_test_int(frontsize, 10);
_TestResult = _TestResult && (_spoil_scenario==-1);
}
catch(ap_error)
{ _TestResult = _TestResult && (_spoil_scenario!=-1); }
}
if( !_TestResult)
{
printf("%-32s FAILED\n", "minmo_biobjective_constr");
fflush(stdout);
}
_TotalResult = _TotalResult && _TestResult;
//
// TEST minns_d_unconstrained
// Nonsmooth unconstrained optimization
//
_TestResult = true;
for(_spoil_scenario=-1; _spoil_scenario<15; _spoil_scenario++)
{
try
{
//
// This example demonstrates minimization of
//
// f(x0,x1) = 2*|x0|+|x1|
//
// using nonsmooth nonlinear optimizer.
//
real_1d_array x0 = "[1,1]";
if( _spoil_scenario==0 )
spoil_vector_by_nan(x0);
if( _spoil_scenario==1 )
spoil_vector_by_posinf(x0);
if( _spoil_scenario==2 )
spoil_vector_by_neginf(x0);
real_1d_array s = "[1,1]";
if( _spoil_scenario==3 )
spoil_vector_by_nan(s);
if( _spoil_scenario==4 )
spoil_vector_by_posinf(s);
if( _spoil_scenario==5 )
spoil_vector_by_neginf(s);
double epsx = 0.00001;
if( _spoil_scenario==6 )
epsx = fp_nan;
if( _spoil_scenario==7 )
epsx = fp_posinf;
if( _spoil_scenario==8 )
epsx = fp_neginf;
double radius = 0.1;
if( _spoil_scenario==9 )
radius = fp_nan;
if( _spoil_scenario==10 )
radius = fp_posinf;
if( _spoil_scenario==11 )
radius = fp_neginf;
double rho = 0.0;
if( _spoil_scenario==12 )
rho = fp_nan;
if( _spoil_scenario==13 )
rho = fp_posinf;
if( _spoil_scenario==14 )
rho = fp_neginf;
ae_int_t maxits = 0;
minnsstate state;
minnsreport rep;
real_1d_array x1;
//
// Create optimizer object, choose AGS algorithm and tune its settings:
// * radius=0.1 good initial value; will be automatically decreased later.
// * rho=0.0 penalty coefficient for nonlinear constraints; can be zero
// because we do not have such constraints
// * epsx=0.000001 stopping conditions
// * s=[1,1] all variables have unit scale
//
minnscreate(2, x0, state);
minnssetalgoags(state, radius, rho);
minnssetcond(state, epsx, maxits);
minnssetscale(state, s);
//
// Optimize and test results.
//
// Optimizer object accepts vector function and its Jacobian, with first
// component (Jacobian row) being target function, and next components
// (Jacobian rows) being nonlinear equality and inequality constraints
// (box/linear ones are passed separately by means of minnssetbc() and
// minnssetlc() calls).
//
// If you do not have nonlinear constraints (exactly our situation), then
// you will have one-component function vector and 1xN Jacobian matrix.
//
// So, our vector function has form
//
// {f0} = { 2*|x0|+|x1| }
//
// with Jacobian
//
// [ ]
// J = [ 2*sign(x0) sign(x1) ]
// [ ]
//
// NOTE: nonsmooth optimizer requires considerably more function
// evaluations than smooth solver - about 2N times more. Using
// numerical differentiation introduces additional (multiplicative)
// 2N speedup.
//
// It means that if smooth optimizer WITH user-supplied gradient
// needs 100 function evaluations to solve 50-dimensional problem,
// then AGS solver with user-supplied gradient will need about 10.000
// function evaluations, and with numerical gradient about 1.000.000
// function evaluations will be performed.
//
// NOTE: AGS solver used by us can handle nonsmooth and nonconvex
// optimization problems. It has convergence guarantees, i.e. it will
// converge to stationary point of the function after running for some
// time.
//
// However, it is important to remember that "stationary point" is not
// equal to "solution". If your problem is convex, everything is OK.
// But nonconvex optimization problems may have "flat spots" - large
// areas where gradient is exactly zero, but function value is far away
// from optimal. Such areas are stationary points too, and optimizer
// may be trapped here.
//
// "Flat spots" are nonsmooth equivalent of the saddle points, but with
// orders of magnitude worse properties - they may be quite large and
// hard to avoid. All nonsmooth optimizers are prone to this kind of the
// problem, because it is impossible to automatically distinguish "flat
// spot" from true solution.
//
// This note is here to warn you that you should be very careful when
// you solve nonsmooth optimization problems. Visual inspection of
// results is essential.
//
alglib::minnsoptimize(state, nsfunc1_jac);
minnsresults(state, x1, rep);
_TestResult = _TestResult && doc_test_real_vector(x1, "[0.0000,0.0000]", 0.005);
_TestResult = _TestResult && (_spoil_scenario==-1);
}
catch(ap_error)
{ _TestResult = _TestResult && (_spoil_scenario!=-1); }
}
if( !_TestResult)
{
printf("%-32s FAILED\n", "minns_d_unconstrained");
fflush(stdout);
}
_TotalResult = _TotalResult && _TestResult;
//
// TEST minns_d_diff
// Nonsmooth unconstrained optimization with numerical differentiation
//
_TestResult = true;
for(_spoil_scenario=-1; _spoil_scenario<18; _spoil_scenario++)
{
try
{
//
// This example demonstrates minimization of
//
// f(x0,x1) = 2*|x0|+|x1|
//
// using nonsmooth nonlinear optimizer with numerical
// differentiation provided by ALGLIB.
//
// NOTE: nonsmooth optimizer requires considerably more function
// evaluations than smooth solver - about 2N times more. Using
// numerical differentiation introduces additional (multiplicative)
// 2N speedup.
//
// It means that if smooth optimizer WITH user-supplied gradient
// needs 100 function evaluations to solve 50-dimensional problem,
// then AGS solver with user-supplied gradient will need about 10.000
// function evaluations, and with numerical gradient about 1.000.000
// function evaluations will be performed.
//
real_1d_array x0 = "[1,1]";
if( _spoil_scenario==0 )
spoil_vector_by_nan(x0);
if( _spoil_scenario==1 )
spoil_vector_by_posinf(x0);
if( _spoil_scenario==2 )
spoil_vector_by_neginf(x0);
real_1d_array s = "[1,1]";
if( _spoil_scenario==3 )
spoil_vector_by_nan(s);
if( _spoil_scenario==4 )
spoil_vector_by_posinf(s);
if( _spoil_scenario==5 )
spoil_vector_by_neginf(s);
double epsx = 0.00001;
if( _spoil_scenario==6 )
epsx = fp_nan;
if( _spoil_scenario==7 )
epsx = fp_posinf;
if( _spoil_scenario==8 )
epsx = fp_neginf;
double diffstep = 0.000001;
if( _spoil_scenario==9 )
diffstep = fp_nan;
if( _spoil_scenario==10 )
diffstep = fp_posinf;
if( _spoil_scenario==11 )
diffstep = fp_neginf;
double radius = 0.1;
if( _spoil_scenario==12 )
radius = fp_nan;
if( _spoil_scenario==13 )
radius = fp_posinf;
if( _spoil_scenario==14 )
radius = fp_neginf;
double rho = 0.0;
if( _spoil_scenario==15 )
rho = fp_nan;
if( _spoil_scenario==16 )
rho = fp_posinf;
if( _spoil_scenario==17 )
rho = fp_neginf;
ae_int_t maxits = 0;
minnsstate state;
minnsreport rep;
real_1d_array x1;
//
// Create optimizer object, choose AGS algorithm and tune its settings:
// * radius=0.1 good initial value; will be automatically decreased later.
// * rho=0.0 penalty coefficient for nonlinear constraints; can be zero
// because we do not have such constraints
// * epsx=0.000001 stopping conditions
// * s=[1,1] all variables have unit scale
//
minnscreatef(2, x0, diffstep, state);
minnssetalgoags(state, radius, rho);
minnssetcond(state, epsx, maxits);
minnssetscale(state, s);
//
// Optimize and test results.
//
// Optimizer object accepts vector function, with first component
// being target function, and next components being nonlinear equality
// and inequality constraints (box/linear ones are passed separately
// by means of minnssetbc() and minnssetlc() calls).
//
// If you do not have nonlinear constraints (exactly our situation), then
// you will have one-component function vector.
//
// So, our vector function has form
//
// {f0} = { 2*|x0|+|x1| }
//
alglib::minnsoptimize(state, nsfunc1_fvec);
minnsresults(state, x1, rep);
_TestResult = _TestResult && doc_test_real_vector(x1, "[0.0000,0.0000]", 0.005);
_TestResult = _TestResult && (_spoil_scenario==-1);
}
catch(ap_error)
{ _TestResult = _TestResult && (_spoil_scenario!=-1); }
}
if( !_TestResult)
{
printf("%-32s FAILED\n", "minns_d_diff");
fflush(stdout);
}
_TotalResult = _TotalResult && _TestResult;
//
// TEST minns_d_bc
// Nonsmooth box constrained optimization
//
_TestResult = true;
for(_spoil_scenario=-1; _spoil_scenario<17; _spoil_scenario++)
{
try
{
//
// This example demonstrates minimization of
//
// f(x0,x1) = 2*|x0|+|x1|
//
// subject to box constraints
//
// 1 <= x0 < +INF
// -INF <= x1 < +INF
//
// using nonsmooth nonlinear optimizer.
//
real_1d_array x0 = "[1,1]";
if( _spoil_scenario==0 )
spoil_vector_by_nan(x0);
if( _spoil_scenario==1 )
spoil_vector_by_posinf(x0);
if( _spoil_scenario==2 )
spoil_vector_by_neginf(x0);
real_1d_array s = "[1,1]";
if( _spoil_scenario==3 )
spoil_vector_by_nan(s);
if( _spoil_scenario==4 )
spoil_vector_by_posinf(s);
if( _spoil_scenario==5 )
spoil_vector_by_neginf(s);
real_1d_array bndl = "[1,-inf]";
if( _spoil_scenario==6 )
spoil_vector_by_nan(bndl);
real_1d_array bndu = "[+inf,+inf]";
if( _spoil_scenario==7 )
spoil_vector_by_nan(bndu);
double epsx = 0.00001;
if( _spoil_scenario==8 )
epsx = fp_nan;
if( _spoil_scenario==9 )
epsx = fp_posinf;
if( _spoil_scenario==10 )
epsx = fp_neginf;
double radius = 0.1;
if( _spoil_scenario==11 )
radius = fp_nan;
if( _spoil_scenario==12 )
radius = fp_posinf;
if( _spoil_scenario==13 )
radius = fp_neginf;
double rho = 0.0;
if( _spoil_scenario==14 )
rho = fp_nan;
if( _spoil_scenario==15 )
rho = fp_posinf;
if( _spoil_scenario==16 )
rho = fp_neginf;
ae_int_t maxits = 0;
minnsstate state;
minnsreport rep;
real_1d_array x1;
//
// Create optimizer object, choose AGS algorithm and tune its settings:
// * radius=0.1 good initial value; will be automatically decreased later.
// * rho=0.0 penalty coefficient for nonlinear constraints; can be zero
// because we do not have such constraints
// * epsx=0.000001 stopping conditions
// * s=[1,1] all variables have unit scale
//
minnscreate(2, x0, state);
minnssetalgoags(state, radius, rho);
minnssetcond(state, epsx, maxits);
minnssetscale(state, s);
//
// Set box constraints.
//
// General linear constraints are set in similar way (see comments on
// minnssetlc() function for more information).
//
// You may combine box, linear and nonlinear constraints in one optimization
// problem.
//
minnssetbc(state, bndl, bndu);
//
// Optimize and test results.
//
// Optimizer object accepts vector function and its Jacobian, with first
// component (Jacobian row) being target function, and next components
// (Jacobian rows) being nonlinear equality and inequality constraints
// (box/linear ones are passed separately by means of minnssetbc() and
// minnssetlc() calls).
//
// If you do not have nonlinear constraints (exactly our situation), then
// you will have one-component function vector and 1xN Jacobian matrix.
//
// So, our vector function has form
//
// {f0} = { 2*|x0|+|x1| }
//
// with Jacobian
//
// [ ]
// J = [ 2*sign(x0) sign(x1) ]
// [ ]
//
// NOTE: nonsmooth optimizer requires considerably more function
// evaluations than smooth solver - about 2N times more. Using
// numerical differentiation introduces additional (multiplicative)
// 2N speedup.
//
// It means that if smooth optimizer WITH user-supplied gradient
// needs 100 function evaluations to solve 50-dimensional problem,
// then AGS solver with user-supplied gradient will need about 10.000
// function evaluations, and with numerical gradient about 1.000.000
// function evaluations will be performed.
//
// NOTE: AGS solver used by us can handle nonsmooth and nonconvex
// optimization problems. It has convergence guarantees, i.e. it will
// converge to stationary point of the function after running for some
// time.
//
// However, it is important to remember that "stationary point" is not
// equal to "solution". If your problem is convex, everything is OK.
// But nonconvex optimization problems may have "flat spots" - large
// areas where gradient is exactly zero, but function value is far away
// from optimal. Such areas are stationary points too, and optimizer
// may be trapped here.
//
// "Flat spots" are nonsmooth equivalent of the saddle points, but with
// orders of magnitude worse properties - they may be quite large and
// hard to avoid. All nonsmooth optimizers are prone to this kind of the
// problem, because it is impossible to automatically distinguish "flat
// spot" from true solution.
//
// This note is here to warn you that you should be very careful when
// you solve nonsmooth optimization problems. Visual inspection of
// results is essential.
//
//
alglib::minnsoptimize(state, nsfunc1_jac);
minnsresults(state, x1, rep);
_TestResult = _TestResult && doc_test_real_vector(x1, "[1.0000,0.0000]", 0.005);
_TestResult = _TestResult && (_spoil_scenario==-1);
}
catch(ap_error)
{ _TestResult = _TestResult && (_spoil_scenario!=-1); }
}
if( !_TestResult)
{
printf("%-32s FAILED\n", "minns_d_bc");
fflush(stdout);
}
_TotalResult = _TotalResult && _TestResult;
//
// TEST minns_d_nlc
// Nonsmooth nonlinearly constrained optimization
//
_TestResult = true;
for(_spoil_scenario=-1; _spoil_scenario<15; _spoil_scenario++)
{
try
{
//
// This example demonstrates minimization of
//
// f(x0,x1) = 2*|x0|+|x1|
//
// subject to combination of equality and inequality constraints
//
// x0 = 1
// x1 >= -1
//
// using nonsmooth nonlinear optimizer. Although these constraints
// are linear, we treat them as general nonlinear ones in order to
// demonstrate nonlinearly constrained optimization setup.
//
real_1d_array x0 = "[1,1]";
if( _spoil_scenario==0 )
spoil_vector_by_nan(x0);
if( _spoil_scenario==1 )
spoil_vector_by_posinf(x0);
if( _spoil_scenario==2 )
spoil_vector_by_neginf(x0);
real_1d_array s = "[1,1]";
if( _spoil_scenario==3 )
spoil_vector_by_nan(s);
if( _spoil_scenario==4 )
spoil_vector_by_posinf(s);
if( _spoil_scenario==5 )
spoil_vector_by_neginf(s);
double epsx = 0.00001;
if( _spoil_scenario==6 )
epsx = fp_nan;
if( _spoil_scenario==7 )
epsx = fp_posinf;
if( _spoil_scenario==8 )
epsx = fp_neginf;
double radius = 0.1;
if( _spoil_scenario==9 )
radius = fp_nan;
if( _spoil_scenario==10 )
radius = fp_posinf;
if( _spoil_scenario==11 )
radius = fp_neginf;
double rho = 50.0;
if( _spoil_scenario==12 )
rho = fp_nan;
if( _spoil_scenario==13 )
rho = fp_posinf;
if( _spoil_scenario==14 )
rho = fp_neginf;
ae_int_t maxits = 0;
minnsstate state;
minnsreport rep;
real_1d_array x1;
//
// Create optimizer object, choose AGS algorithm and tune its settings:
// * radius=0.1 good initial value; will be automatically decreased later.
// * rho=50.0 penalty coefficient for nonlinear constraints. It is your
// responsibility to choose good one - large enough that it
// enforces constraints, but small enough in order to avoid
// extreme slowdown due to ill-conditioning.
// * epsx=0.000001 stopping conditions
// * s=[1,1] all variables have unit scale
//
minnscreate(2, x0, state);
minnssetalgoags(state, radius, rho);
minnssetcond(state, epsx, maxits);
minnssetscale(state, s);
//
// Set general nonlinear constraints.
//
// This part is more tricky than working with box/linear constraints - you
// can not "pack" general nonlinear function into double precision array.
// That's why minnssetnlc() does not accept constraints itself - only
// constraint COUNTS are passed: first parameter is number of equality
// constraints, second one is number of inequality constraints.
//
// As for constraining functions - these functions are passed as part
// of problem Jacobian (see below).
//
// NOTE: MinNS optimizer supports arbitrary combination of boundary, general
// linear and general nonlinear constraints. This example does not
// show how to work with general linear constraints, but you can
// easily find it in documentation on minnlcsetlc() function.
//
minnssetnlc(state, 1, 1);
//
// Optimize and test results.
//
// Optimizer object accepts vector function and its Jacobian, with first
// component (Jacobian row) being target function, and next components
// (Jacobian rows) being nonlinear equality and inequality constraints
// (box/linear ones are passed separately by means of minnssetbc() and
// minnssetlc() calls).
//
// Nonlinear equality constraints have form Gi(x)=0, inequality ones
// have form Hi(x)<=0, so we may have to "normalize" constraints prior
// to passing them to optimizer (right side is zero, constraints are
// sorted, multiplied by -1 when needed).
//
// So, our vector function has form
//
// {f0,f1,f2} = { 2*|x0|+|x1|, x0-1, -x1-1 }
//
// with Jacobian
//
// [ 2*sign(x0) sign(x1) ]
// J = [ 1 0 ]
// [ 0 -1 ]
//
// which means that we have optimization problem
//
// min{f0} subject to f1=0, f2<=0
//
// which is essentially same as
//
// min { 2*|x0|+|x1| } subject to x0=1, x1>=-1
//
// NOTE: AGS solver used by us can handle nonsmooth and nonconvex
// optimization problems. It has convergence guarantees, i.e. it will
// converge to stationary point of the function after running for some
// time.
//
// However, it is important to remember that "stationary point" is not
// equal to "solution". If your problem is convex, everything is OK.
// But nonconvex optimization problems may have "flat spots" - large
// areas where gradient is exactly zero, but function value is far away
// from optimal. Such areas are stationary points too, and optimizer
// may be trapped here.
//
// "Flat spots" are nonsmooth equivalent of the saddle points, but with
// orders of magnitude worse properties - they may be quite large and
// hard to avoid. All nonsmooth optimizers are prone to this kind of the
// problem, because it is impossible to automatically distinguish "flat
// spot" from true solution.
//
// This note is here to warn you that you should be very careful when
// you solve nonsmooth optimization problems. Visual inspection of
// results is essential.
//
alglib::minnsoptimize(state, nsfunc2_jac);
minnsresults(state, x1, rep);
_TestResult = _TestResult && doc_test_real_vector(x1, "[1.0000,0.0000]", 0.005);
_TestResult = _TestResult && (_spoil_scenario==-1);
}
catch(ap_error)
{ _TestResult = _TestResult && (_spoil_scenario!=-1); }
}
if( !_TestResult)
{
printf("%-32s FAILED\n", "minns_d_nlc");
fflush(stdout);
}
_TotalResult = _TotalResult && _TestResult;
//
// TEST minbc_d_1
// Nonlinear optimization with box constraints
//
_TestResult = true;
for(_spoil_scenario=-1; _spoil_scenario<20; _spoil_scenario++)
{
try
{
//
// This example demonstrates minimization of
//
// f(x,y) = 100*(x+3)^4+(y-3)^4
//
// subject to box constraints
//
// -1<=x<=+1, -1<=y<=+1
//
// using MinBC optimizer with:
// * initial point x=[0,0]
// * unit scale being set for all variables (see minbcsetscale for more info)
// * stopping criteria set to "terminate after short enough step"
// * OptGuard integrity check being used to check problem statement
// for some common errors like nonsmoothness or bad analytic gradient
//
// First, we create optimizer object and tune its properties:
// * set box constraints
// * set variable scales
// * set stopping criteria
//
real_1d_array x = "[0,0]";
if( _spoil_scenario==0 )
spoil_vector_by_nan(x);
if( _spoil_scenario==1 )
spoil_vector_by_posinf(x);
if( _spoil_scenario==2 )
spoil_vector_by_neginf(x);
real_1d_array s = "[1,1]";
if( _spoil_scenario==3 )
spoil_vector_by_nan(s);
if( _spoil_scenario==4 )
spoil_vector_by_posinf(s);
if( _spoil_scenario==5 )
spoil_vector_by_neginf(s);
if( _spoil_scenario==6 )
spoil_vector_by_deleting_element(s);
real_1d_array bndl = "[-1,-1]";
if( _spoil_scenario==7 )
spoil_vector_by_nan(bndl);
if( _spoil_scenario==8 )
spoil_vector_by_deleting_element(bndl);
real_1d_array bndu = "[+1,+1]";
if( _spoil_scenario==9 )
spoil_vector_by_nan(bndu);
if( _spoil_scenario==10 )
spoil_vector_by_deleting_element(bndu);
minbcstate state;
double epsg = 0;
if( _spoil_scenario==11 )
epsg = fp_nan;
if( _spoil_scenario==12 )
epsg = fp_posinf;
if( _spoil_scenario==13 )
epsg = fp_neginf;
double epsf = 0;
if( _spoil_scenario==14 )
epsf = fp_nan;
if( _spoil_scenario==15 )
epsf = fp_posinf;
if( _spoil_scenario==16 )
epsf = fp_neginf;
double epsx = 0.000001;
if( _spoil_scenario==17 )
epsx = fp_nan;
if( _spoil_scenario==18 )
epsx = fp_posinf;
if( _spoil_scenario==19 )
epsx = fp_neginf;
ae_int_t maxits = 0;
minbccreate(x, state);
minbcsetbc(state, bndl, bndu);
minbcsetscale(state, s);
minbcsetcond(state, epsg, epsf, epsx, maxits);
//
// Then we activate OptGuard integrity checking.
//
// OptGuard monitor helps to catch common coding and problem statement
// issues, like:
// * discontinuity of the target function (C0 continuity violation)
// * nonsmoothness of the target function (C1 continuity violation)
// * erroneous analytic gradient, i.e. one inconsistent with actual
// change in the target/constraints
//
// OptGuard is essential for early prototyping stages because such
// problems often result in premature termination of the optimizer
// which is really hard to distinguish from the correct termination.
//
// IMPORTANT: GRADIENT VERIFICATION IS PERFORMED BY MEANS OF NUMERICAL
// DIFFERENTIATION. DO NOT USE IT IN PRODUCTION CODE!!!!!!!
//
// Other OptGuard checks add moderate overhead, but anyway
// it is better to turn them off when they are not needed.
//
minbcoptguardsmoothness(state);
minbcoptguardgradient(state, 0.001);
//
// Optimize and evaluate results
//
minbcreport rep;
alglib::minbcoptimize(state, function1_grad);
minbcresults(state, x, rep);
_TestResult = _TestResult && doc_test_real_vector(x, "[-1,1]", 0.005);
//
// Check that OptGuard did not report errors
//
// NOTE: want to test OptGuard? Try breaking the gradient - say, add
// 1.0 to some of its components.
//
optguardreport ogrep;
minbcoptguardresults(state, ogrep);
_TestResult = _TestResult && doc_test_bool(ogrep.badgradsuspected, false);
_TestResult = _TestResult && doc_test_bool(ogrep.nonc0suspected, false);
_TestResult = _TestResult && doc_test_bool(ogrep.nonc1suspected, false);
_TestResult = _TestResult && (_spoil_scenario==-1);
}
catch(ap_error)
{ _TestResult = _TestResult && (_spoil_scenario!=-1); }
}
if( !_TestResult)
{
printf("%-32s FAILED\n", "minbc_d_1");
fflush(stdout);
}
_TotalResult = _TotalResult && _TestResult;
//
// TEST minbc_numdiff
// Nonlinear optimization with bound constraints and numerical differentiation
//
_TestResult = true;
for(_spoil_scenario=-1; _spoil_scenario<23; _spoil_scenario++)
{
try
{
//
// This example demonstrates minimization of
//
// f(x,y) = 100*(x+3)^4+(y-3)^4
//
// subject to box constraints
//
// -1<=x<=+1, -1<=y<=+1
//
// using MinBC optimizer with:
// * numerical differentiation being used
// * initial point x=[0,0]
// * unit scale being set for all variables (see minbcsetscale for more info)
// * stopping criteria set to "terminate after short enough step"
// * OptGuard integrity check being used to check problem statement
// for some common errors like nonsmoothness or bad analytic gradient
//
real_1d_array x = "[0,0]";
if( _spoil_scenario==0 )
spoil_vector_by_nan(x);
if( _spoil_scenario==1 )
spoil_vector_by_posinf(x);
if( _spoil_scenario==2 )
spoil_vector_by_neginf(x);
real_1d_array s = "[1,1]";
if( _spoil_scenario==3 )
spoil_vector_by_nan(s);
if( _spoil_scenario==4 )
spoil_vector_by_posinf(s);
if( _spoil_scenario==5 )
spoil_vector_by_neginf(s);
if( _spoil_scenario==6 )
spoil_vector_by_deleting_element(s);
real_1d_array bndl = "[-1,-1]";
if( _spoil_scenario==7 )
spoil_vector_by_nan(bndl);
if( _spoil_scenario==8 )
spoil_vector_by_deleting_element(bndl);
real_1d_array bndu = "[+1,+1]";
if( _spoil_scenario==9 )
spoil_vector_by_nan(bndu);
if( _spoil_scenario==10 )
spoil_vector_by_deleting_element(bndu);
minbcstate state;
double epsg = 0;
if( _spoil_scenario==11 )
epsg = fp_nan;
if( _spoil_scenario==12 )
epsg = fp_posinf;
if( _spoil_scenario==13 )
epsg = fp_neginf;
double epsf = 0;
if( _spoil_scenario==14 )
epsf = fp_nan;
if( _spoil_scenario==15 )
epsf = fp_posinf;
if( _spoil_scenario==16 )
epsf = fp_neginf;
double epsx = 0.000001;
if( _spoil_scenario==17 )
epsx = fp_nan;
if( _spoil_scenario==18 )
epsx = fp_posinf;
if( _spoil_scenario==19 )
epsx = fp_neginf;
ae_int_t maxits = 0;
double diffstep = 1.0e-6;
if( _spoil_scenario==20 )
diffstep = fp_nan;
if( _spoil_scenario==21 )
diffstep = fp_posinf;
if( _spoil_scenario==22 )
diffstep = fp_neginf;
//
// Now we are ready to actually optimize something:
// * first we create optimizer
// * we add boundary constraints
// * we tune stopping conditions
// * and, finally, optimize and obtain results...
//
minbccreatef(x, diffstep, state);
minbcsetbc(state, bndl, bndu);
minbcsetscale(state, s);
minbcsetcond(state, epsg, epsf, epsx, maxits);
//
// Then we activate OptGuard integrity checking.
//
// Numerical differentiation always produces "correct" gradient
// (with some truncation error, but unbiased). Thus, we just have
// to check smoothness properties of the target: C0 and C1 continuity.
//
// Sometimes user accidentally tries to solve nonsmooth problems
// with smooth optimizer. OptGuard helps to detect such situations
// early, at the prototyping stage.
//
minbcoptguardsmoothness(state);
//
// Optimize and evaluate results
//
minbcreport rep;
alglib::minbcoptimize(state, function1_func);
minbcresults(state, x, rep);
_TestResult = _TestResult && doc_test_real_vector(x, "[-1,1]", 0.005);
//
// Check that OptGuard did not report errors
//
// Want to challenge OptGuard? Try to make your problem
// nonsmooth by replacing 100*(x+3)^4 by 100*|x+3| and
// re-run optimizer.
//
optguardreport ogrep;
minbcoptguardresults(state, ogrep);
_TestResult = _TestResult && doc_test_bool(ogrep.nonc0suspected, false);
_TestResult = _TestResult && doc_test_bool(ogrep.nonc1suspected, false);
_TestResult = _TestResult && (_spoil_scenario==-1);
}
catch(ap_error)
{ _TestResult = _TestResult && (_spoil_scenario!=-1); }
}
if( !_TestResult)
{
printf("%-32s FAILED\n", "minbc_numdiff");
fflush(stdout);
}
_TotalResult = _TotalResult && _TestResult;
//
// TEST nneighbor_d_1
// Nearest neighbor search, KNN queries
//
_TestResult = true;
for(_spoil_scenario=-1; _spoil_scenario<3; _spoil_scenario++)
{
try
{
real_2d_array a = "[[0,0],[0,1],[1,0],[1,1]]";
if( _spoil_scenario==0 )
spoil_matrix_by_nan(a);
if( _spoil_scenario==1 )
spoil_matrix_by_posinf(a);
if( _spoil_scenario==2 )
spoil_matrix_by_neginf(a);
ae_int_t nx = 2;
ae_int_t ny = 0;
ae_int_t normtype = 2;
kdtree kdt;
real_1d_array x;
real_1d_array x1;
real_2d_array r = "[[]]";
ae_int_t k;
kdtreebuild(a, nx, ny, normtype, kdt);
x = "[-1,0]";
k = kdtreequeryknn(kdt, x, 1);
_TestResult = _TestResult && doc_test_int(k, 1);
kdtreequeryresultsx(kdt, r);
_TestResult = _TestResult && doc_test_real_matrix(r, "[[0,0]]", 0.05);
x1 = "[+0.9,0.1]";
k = kdtreequeryknn(kdt, x1, 1);
_TestResult = _TestResult && doc_test_int(k, 1);
kdtreequeryresultsx(kdt, r);
_TestResult = _TestResult && doc_test_real_matrix(r, "[[1,0]]", 0.05);
_TestResult = _TestResult && (_spoil_scenario==-1);
}
catch(ap_error)
{ _TestResult = _TestResult && (_spoil_scenario!=-1); }
}
if( !_TestResult)
{
printf("%-32s FAILED\n", "nneighbor_d_1");
fflush(stdout);
}
_TotalResult = _TotalResult && _TestResult;
//
// TEST nneighbor_t_2
// Subsequent queries; buffered functions must use previously allocated storage (if large enough), so buffer may contain some info from previous call
//
_TestResult = true;
for(_spoil_scenario=-1; _spoil_scenario<3; _spoil_scenario++)
{
try
{
real_2d_array a = "[[0,0],[0,1],[1,0],[1,1]]";
if( _spoil_scenario==0 )
spoil_matrix_by_nan(a);
if( _spoil_scenario==1 )
spoil_matrix_by_posinf(a);
if( _spoil_scenario==2 )
spoil_matrix_by_neginf(a);
ae_int_t nx = 2;
ae_int_t ny = 0;
ae_int_t normtype = 2;
kdtree kdt;
real_1d_array x;
real_2d_array rx = "[[]]";
ae_int_t k;
kdtreebuild(a, nx, ny, normtype, kdt);
x = "[+2,0]";
k = kdtreequeryknn(kdt, x, 2, true);
_TestResult = _TestResult && doc_test_int(k, 2);
kdtreequeryresultsx(kdt, rx);
_TestResult = _TestResult && doc_test_real_matrix(rx, "[[1,0],[1,1]]", 0.05);
x = "[-2,0]";
k = kdtreequeryknn(kdt, x, 1, true);
_TestResult = _TestResult && doc_test_int(k, 1);
kdtreequeryresultsx(kdt, rx);
_TestResult = _TestResult && doc_test_real_matrix(rx, "[[0,0],[1,1]]", 0.05);
_TestResult = _TestResult && (_spoil_scenario==-1);
}
catch(ap_error)
{ _TestResult = _TestResult && (_spoil_scenario!=-1); }
}
if( !_TestResult)
{
printf("%-32s FAILED\n", "nneighbor_t_2");
fflush(stdout);
}
_TotalResult = _TotalResult && _TestResult;
//
// TEST nneighbor_d_2
// Serialization of KD-trees
//
_TestResult = true;
for(_spoil_scenario=-1; _spoil_scenario<3; _spoil_scenario++)
{
try
{
real_2d_array a = "[[0,0],[0,1],[1,0],[1,1]]";
if( _spoil_scenario==0 )
spoil_matrix_by_nan(a);
if( _spoil_scenario==1 )
spoil_matrix_by_posinf(a);
if( _spoil_scenario==2 )
spoil_matrix_by_neginf(a);
ae_int_t nx = 2;
ae_int_t ny = 0;
ae_int_t normtype = 2;
kdtree kdt0;
kdtree kdt1;
std::string s;
real_1d_array x;
real_2d_array r0 = "[[]]";
real_2d_array r1 = "[[]]";
//
// Build tree and serialize it
//
kdtreebuild(a, nx, ny, normtype, kdt0);
alglib::kdtreeserialize(kdt0, s);
alglib::kdtreeunserialize(s, kdt1);
//
// Compare results from KNN queries
//
x = "[-1,0]";
kdtreequeryknn(kdt0, x, 1);
kdtreequeryresultsx(kdt0, r0);
kdtreequeryknn(kdt1, x, 1);
kdtreequeryresultsx(kdt1, r1);
_TestResult = _TestResult && doc_test_real_matrix(r0, "[[0,0]]", 0.05);
_TestResult = _TestResult && doc_test_real_matrix(r1, "[[0,0]]", 0.05);
_TestResult = _TestResult && (_spoil_scenario==-1);
}
catch(ap_error)
{ _TestResult = _TestResult && (_spoil_scenario!=-1); }
}
if( !_TestResult)
{
printf("%-32s FAILED\n", "nneighbor_d_2");
fflush(stdout);
}
_TotalResult = _TotalResult && _TestResult;
//
// TEST odesolver_d1
// Solving y'=-y with ODE solver
//
_TestResult = true;
for(_spoil_scenario=-1; _spoil_scenario<13; _spoil_scenario++)
{
try
{
real_1d_array y = "[1]";
if( _spoil_scenario==0 )
spoil_vector_by_nan(y);
if( _spoil_scenario==1 )
spoil_vector_by_posinf(y);
if( _spoil_scenario==2 )
spoil_vector_by_neginf(y);
if( _spoil_scenario==3 )
spoil_vector_by_deleting_element(y);
real_1d_array x = "[0, 1, 2, 3]";
if( _spoil_scenario==4 )
spoil_vector_by_nan(x);
if( _spoil_scenario==5 )
spoil_vector_by_posinf(x);
if( _spoil_scenario==6 )
spoil_vector_by_neginf(x);
double eps = 0.00001;
if( _spoil_scenario==7 )
eps = fp_nan;
if( _spoil_scenario==8 )
eps = fp_posinf;
if( _spoil_scenario==9 )
eps = fp_neginf;
double h = 0;
if( _spoil_scenario==10 )
h = fp_nan;
if( _spoil_scenario==11 )
h = fp_posinf;
if( _spoil_scenario==12 )
h = fp_neginf;
odesolverstate s;
ae_int_t m;
real_1d_array xtbl;
real_2d_array ytbl;
odesolverreport rep;
odesolverrkck(y, x, eps, h, s);
alglib::odesolversolve(s, ode_function_1_diff);
odesolverresults(s, m, xtbl, ytbl, rep);
_TestResult = _TestResult && doc_test_int(m, 4);
_TestResult = _TestResult && doc_test_real_vector(xtbl, "[0, 1, 2, 3]", 0.005);
_TestResult = _TestResult && doc_test_real_matrix(ytbl, "[[1], [0.367], [0.135], [0.050]]", 0.005);
_TestResult = _TestResult && (_spoil_scenario==-1);
}
catch(ap_error)
{ _TestResult = _TestResult && (_spoil_scenario!=-1); }
}
if( !_TestResult)
{
printf("%-32s FAILED\n", "odesolver_d1");
fflush(stdout);
}
_TotalResult = _TotalResult && _TestResult;
//
// TEST matdet_d_1
// Determinant calculation, real matrix, short form
//
_TestResult = true;
for(_spoil_scenario=-1; _spoil_scenario<7; _spoil_scenario++)
{
try
{
real_2d_array b = "[[1,2],[2,1]]";
if( _spoil_scenario==0 )
spoil_matrix_by_nan(b);
if( _spoil_scenario==1 )
spoil_matrix_by_posinf(b);
if( _spoil_scenario==2 )
spoil_matrix_by_neginf(b);
if( _spoil_scenario==3 )
spoil_matrix_by_adding_row(b);
if( _spoil_scenario==4 )
spoil_matrix_by_adding_col(b);
if( _spoil_scenario==5 )
spoil_matrix_by_deleting_row(b);
if( _spoil_scenario==6 )
spoil_matrix_by_deleting_col(b);
double a;
a = rmatrixdet(b);
_TestResult = _TestResult && doc_test_real(a, -3, 0.0001);
_TestResult = _TestResult && (_spoil_scenario==-1);
}
catch(ap_error)
{ _TestResult = _TestResult && (_spoil_scenario!=-1); }
}
if( !_TestResult)
{
printf("%-32s FAILED\n", "matdet_d_1");
fflush(stdout);
}
_TotalResult = _TotalResult && _TestResult;
//
// TEST matdet_d_2
// Determinant calculation, real matrix, full form
//
_TestResult = true;
for(_spoil_scenario=-1; _spoil_scenario<5; _spoil_scenario++)
{
try
{
real_2d_array b = "[[5,4],[4,5]]";
if( _spoil_scenario==0 )
spoil_matrix_by_nan(b);
if( _spoil_scenario==1 )
spoil_matrix_by_posinf(b);
if( _spoil_scenario==2 )
spoil_matrix_by_neginf(b);
if( _spoil_scenario==3 )
spoil_matrix_by_deleting_row(b);
if( _spoil_scenario==4 )
spoil_matrix_by_deleting_col(b);
double a;
a = rmatrixdet(b, 2);
_TestResult = _TestResult && doc_test_real(a, 9, 0.0001);
_TestResult = _TestResult && (_spoil_scenario==-1);
}
catch(ap_error)
{ _TestResult = _TestResult && (_spoil_scenario!=-1); }
}
if( !_TestResult)
{
printf("%-32s FAILED\n", "matdet_d_2");
fflush(stdout);
}
_TotalResult = _TotalResult && _TestResult;
//
// TEST matdet_d_3
// Determinant calculation, complex matrix, short form
//
_TestResult = true;
for(_spoil_scenario=-1; _spoil_scenario<7; _spoil_scenario++)
{
try
{
complex_2d_array b = "[[1+1i,2],[2,1-1i]]";
if( _spoil_scenario==0 )
spoil_matrix_by_nan(b);
if( _spoil_scenario==1 )
spoil_matrix_by_posinf(b);
if( _spoil_scenario==2 )
spoil_matrix_by_neginf(b);
if( _spoil_scenario==3 )
spoil_matrix_by_adding_row(b);
if( _spoil_scenario==4 )
spoil_matrix_by_adding_col(b);
if( _spoil_scenario==5 )
spoil_matrix_by_deleting_row(b);
if( _spoil_scenario==6 )
spoil_matrix_by_deleting_col(b);
alglib::complex a;
a = cmatrixdet(b);
_TestResult = _TestResult && doc_test_complex(a, -2, 0.0001);
_TestResult = _TestResult && (_spoil_scenario==-1);
}
catch(ap_error)
{ _TestResult = _TestResult && (_spoil_scenario!=-1); }
}
if( !_TestResult)
{
printf("%-32s FAILED\n", "matdet_d_3");
fflush(stdout);
}
_TotalResult = _TotalResult && _TestResult;
//
// TEST matdet_d_4
// Determinant calculation, complex matrix, full form
//
_TestResult = true;
for(_spoil_scenario=-1; _spoil_scenario<5; _spoil_scenario++)
{
try
{
alglib::complex a;
complex_2d_array b = "[[5i,4],[4i,5]]";
if( _spoil_scenario==0 )
spoil_matrix_by_nan(b);
if( _spoil_scenario==1 )
spoil_matrix_by_posinf(b);
if( _spoil_scenario==2 )
spoil_matrix_by_neginf(b);
if( _spoil_scenario==3 )
spoil_matrix_by_deleting_row(b);
if( _spoil_scenario==4 )
spoil_matrix_by_deleting_col(b);
a = cmatrixdet(b, 2);
_TestResult = _TestResult && doc_test_complex(a, alglib::complex(0,9), 0.0001);
_TestResult = _TestResult && (_spoil_scenario==-1);
}
catch(ap_error)
{ _TestResult = _TestResult && (_spoil_scenario!=-1); }
}
if( !_TestResult)
{
printf("%-32s FAILED\n", "matdet_d_4");
fflush(stdout);
}
_TotalResult = _TotalResult && _TestResult;
//
// TEST matdet_d_5
// Determinant calculation, complex matrix with zero imaginary part, short form
//
_TestResult = true;
for(_spoil_scenario=-1; _spoil_scenario<7; _spoil_scenario++)
{
try
{
alglib::complex a;
complex_2d_array b = "[[9,1],[2,1]]";
if( _spoil_scenario==0 )
spoil_matrix_by_nan(b);
if( _spoil_scenario==1 )
spoil_matrix_by_posinf(b);
if( _spoil_scenario==2 )
spoil_matrix_by_neginf(b);
if( _spoil_scenario==3 )
spoil_matrix_by_adding_row(b);
if( _spoil_scenario==4 )
spoil_matrix_by_adding_col(b);
if( _spoil_scenario==5 )
spoil_matrix_by_deleting_row(b);
if( _spoil_scenario==6 )
spoil_matrix_by_deleting_col(b);
a = cmatrixdet(b);
_TestResult = _TestResult && doc_test_complex(a, 7, 0.0001);
_TestResult = _TestResult && (_spoil_scenario==-1);
}
catch(ap_error)
{ _TestResult = _TestResult && (_spoil_scenario!=-1); }
}
if( !_TestResult)
{
printf("%-32s FAILED\n", "matdet_d_5");
fflush(stdout);
}
_TotalResult = _TotalResult && _TestResult;
//
// TEST matdet_t_0
// Determinant calculation, real matrix, full form
//
_TestResult = true;
for(_spoil_scenario=-1; _spoil_scenario<5; _spoil_scenario++)
{
try
{
double a;
real_2d_array b = "[[3,4],[-4,3]]";
if( _spoil_scenario==0 )
spoil_matrix_by_nan(b);
if( _spoil_scenario==1 )
spoil_matrix_by_posinf(b);
if( _spoil_scenario==2 )
spoil_matrix_by_neginf(b);
if( _spoil_scenario==3 )
spoil_matrix_by_deleting_row(b);
if( _spoil_scenario==4 )
spoil_matrix_by_deleting_col(b);
a = rmatrixdet(b, 2);
_TestResult = _TestResult && doc_test_real(a, 25, 0.0001);
_TestResult = _TestResult && (_spoil_scenario==-1);
}
catch(ap_error)
{ _TestResult = _TestResult && (_spoil_scenario!=-1); }
}
if( !_TestResult)
{
printf("%-32s FAILED\n", "matdet_t_0");
fflush(stdout);
}
_TotalResult = _TotalResult && _TestResult;
//
// TEST matdet_t_1
// Determinant calculation, real matrix, LU, short form
//
_TestResult = true;
for(_spoil_scenario=-1; _spoil_scenario<9; _spoil_scenario++)
{
try
{
double a;
real_2d_array b = "[[1,2],[2,5]]";
if( _spoil_scenario==0 )
spoil_matrix_by_nan(b);
if( _spoil_scenario==1 )
spoil_matrix_by_posinf(b);
if( _spoil_scenario==2 )
spoil_matrix_by_neginf(b);
if( _spoil_scenario==3 )
spoil_matrix_by_adding_row(b);
if( _spoil_scenario==4 )
spoil_matrix_by_adding_col(b);
if( _spoil_scenario==5 )
spoil_matrix_by_deleting_row(b);
if( _spoil_scenario==6 )
spoil_matrix_by_deleting_col(b);
integer_1d_array p = "[1,1]";
if( _spoil_scenario==7 )
spoil_vector_by_adding_element(p);
if( _spoil_scenario==8 )
spoil_vector_by_deleting_element(p);
a = rmatrixludet(b, p);
_TestResult = _TestResult && doc_test_real(a, -5, 0.0001);
_TestResult = _TestResult && (_spoil_scenario==-1);
}
catch(ap_error)
{ _TestResult = _TestResult && (_spoil_scenario!=-1); }
}
if( !_TestResult)
{
printf("%-32s FAILED\n", "matdet_t_1");
fflush(stdout);
}
_TotalResult = _TotalResult && _TestResult;
//
// TEST matdet_t_2
// Determinant calculation, real matrix, LU, full form
//
_TestResult = true;
for(_spoil_scenario=-1; _spoil_scenario<6; _spoil_scenario++)
{
try
{
double a;
real_2d_array b = "[[5,4],[4,5]]";
if( _spoil_scenario==0 )
spoil_matrix_by_nan(b);
if( _spoil_scenario==1 )
spoil_matrix_by_posinf(b);
if( _spoil_scenario==2 )
spoil_matrix_by_neginf(b);
if( _spoil_scenario==3 )
spoil_matrix_by_deleting_row(b);
if( _spoil_scenario==4 )
spoil_matrix_by_deleting_col(b);
integer_1d_array p = "[0,1]";
if( _spoil_scenario==5 )
spoil_vector_by_deleting_element(p);
a = rmatrixludet(b, p, 2);
_TestResult = _TestResult && doc_test_real(a, 25, 0.0001);
_TestResult = _TestResult && (_spoil_scenario==-1);
}
catch(ap_error)
{ _TestResult = _TestResult && (_spoil_scenario!=-1); }
}
if( !_TestResult)
{
printf("%-32s FAILED\n", "matdet_t_2");
fflush(stdout);
}
_TotalResult = _TotalResult && _TestResult;
//
// TEST matdet_t_3
// Determinant calculation, complex matrix, full form
//
_TestResult = true;
for(_spoil_scenario=-1; _spoil_scenario<5; _spoil_scenario++)
{
try
{
alglib::complex a;
complex_2d_array b = "[[5i,4],[-4,5i]]";
if( _spoil_scenario==0 )
spoil_matrix_by_nan(b);
if( _spoil_scenario==1 )
spoil_matrix_by_posinf(b);
if( _spoil_scenario==2 )
spoil_matrix_by_neginf(b);
if( _spoil_scenario==3 )
spoil_matrix_by_deleting_row(b);
if( _spoil_scenario==4 )
spoil_matrix_by_deleting_col(b);
a = cmatrixdet(b, 2);
_TestResult = _TestResult && doc_test_complex(a, -9, 0.0001);
_TestResult = _TestResult && (_spoil_scenario==-1);
}
catch(ap_error)
{ _TestResult = _TestResult && (_spoil_scenario!=-1); }
}
if( !_TestResult)
{
printf("%-32s FAILED\n", "matdet_t_3");
fflush(stdout);
}
_TotalResult = _TotalResult && _TestResult;
//
// TEST matdet_t_4
// Determinant calculation, complex matrix, LU, short form
//
_TestResult = true;
for(_spoil_scenario=-1; _spoil_scenario<9; _spoil_scenario++)
{
try
{
alglib::complex a;
complex_2d_array b = "[[1,2],[2,5i]]";
if( _spoil_scenario==0 )
spoil_matrix_by_nan(b);
if( _spoil_scenario==1 )
spoil_matrix_by_posinf(b);
if( _spoil_scenario==2 )
spoil_matrix_by_neginf(b);
if( _spoil_scenario==3 )
spoil_matrix_by_adding_row(b);
if( _spoil_scenario==4 )
spoil_matrix_by_adding_col(b);
if( _spoil_scenario==5 )
spoil_matrix_by_deleting_row(b);
if( _spoil_scenario==6 )
spoil_matrix_by_deleting_col(b);
integer_1d_array p = "[1,1]";
if( _spoil_scenario==7 )
spoil_vector_by_adding_element(p);
if( _spoil_scenario==8 )
spoil_vector_by_deleting_element(p);
a = cmatrixludet(b, p);
_TestResult = _TestResult && doc_test_complex(a, alglib::complex(0,-5), 0.0001);
_TestResult = _TestResult && (_spoil_scenario==-1);
}
catch(ap_error)
{ _TestResult = _TestResult && (_spoil_scenario!=-1); }
}
if( !_TestResult)
{
printf("%-32s FAILED\n", "matdet_t_4");
fflush(stdout);
}
_TotalResult = _TotalResult && _TestResult;
//
// TEST matdet_t_5
// Determinant calculation, complex matrix, LU, full form
//
_TestResult = true;
for(_spoil_scenario=-1; _spoil_scenario<6; _spoil_scenario++)
{
try
{
alglib::complex a;
complex_2d_array b = "[[5,4i],[4,5]]";
if( _spoil_scenario==0 )
spoil_matrix_by_nan(b);
if( _spoil_scenario==1 )
spoil_matrix_by_posinf(b);
if( _spoil_scenario==2 )
spoil_matrix_by_neginf(b);
if( _spoil_scenario==3 )
spoil_matrix_by_deleting_row(b);
if( _spoil_scenario==4 )
spoil_matrix_by_deleting_col(b);
integer_1d_array p = "[0,1]";
if( _spoil_scenario==5 )
spoil_vector_by_deleting_element(p);
a = cmatrixludet(b, p, 2);
_TestResult = _TestResult && doc_test_complex(a, 25, 0.0001);
_TestResult = _TestResult && (_spoil_scenario==-1);
}
catch(ap_error)
{ _TestResult = _TestResult && (_spoil_scenario!=-1); }
}
if( !_TestResult)
{
printf("%-32s FAILED\n", "matdet_t_5");
fflush(stdout);
}
_TotalResult = _TotalResult && _TestResult;
//
// TEST autogk_d1
// Integrating f=exp(x) by adaptive integrator
//
_TestResult = true;
for(_spoil_scenario=-1; _spoil_scenario<6; _spoil_scenario++)
{
try
{
//
// This example demonstrates integration of f=exp(x) on [0,1]:
// * first, autogkstate is initialized
// * then we call integration function
// * and finally we obtain results with autogkresults() call
//
double a = 0;
if( _spoil_scenario==0 )
a = fp_nan;
if( _spoil_scenario==1 )
a = fp_posinf;
if( _spoil_scenario==2 )
a = fp_neginf;
double b = 1;
if( _spoil_scenario==3 )
b = fp_nan;
if( _spoil_scenario==4 )
b = fp_posinf;
if( _spoil_scenario==5 )
b = fp_neginf;
autogkstate s;
double v;
autogkreport rep;
autogksmooth(a, b, s);
alglib::autogkintegrate(s, int_function_1_func);
autogkresults(s, v, rep);
_TestResult = _TestResult && doc_test_real(v, 1.7182, 0.005);
_TestResult = _TestResult && (_spoil_scenario==-1);
}
catch(ap_error)
{ _TestResult = _TestResult && (_spoil_scenario!=-1); }
}
if( !_TestResult)
{
printf("%-32s FAILED\n", "autogk_d1");
fflush(stdout);
}
_TotalResult = _TotalResult && _TestResult;
//
// TEST basestat_d_base
// Basic functionality (moments, adev, median, percentile)
//
_TestResult = true;
for(_spoil_scenario=-1; _spoil_scenario<6; _spoil_scenario++)
{
try
{
real_1d_array x = "[0,1,4,9,16,25,36,49,64,81]";
if( _spoil_scenario==0 )
spoil_vector_by_nan(x);
if( _spoil_scenario==1 )
spoil_vector_by_posinf(x);
if( _spoil_scenario==2 )
spoil_vector_by_neginf(x);
double mean;
double variance;
double skewness;
double kurtosis;
double adev;
double p;
double v;
//
// Here we demonstrate calculation of sample moments
// (mean, variance, skewness, kurtosis)
//
samplemoments(x, mean, variance, skewness, kurtosis);
_TestResult = _TestResult && doc_test_real(mean, 28.5, 0.01);
_TestResult = _TestResult && doc_test_real(variance, 801.1667, 0.01);
_TestResult = _TestResult && doc_test_real(skewness, 0.5751, 0.01);
_TestResult = _TestResult && doc_test_real(kurtosis, -1.2666, 0.01);
//
// Average deviation
//
sampleadev(x, adev);
_TestResult = _TestResult && doc_test_real(adev, 23.2, 0.01);
//
// Median and percentile
//
samplemedian(x, v);
_TestResult = _TestResult && doc_test_real(v, 20.5, 0.01);
p = 0.5;
if( _spoil_scenario==3 )
p = fp_nan;
if( _spoil_scenario==4 )
p = fp_posinf;
if( _spoil_scenario==5 )
p = fp_neginf;
samplepercentile(x, p, v);
_TestResult = _TestResult && doc_test_real(v, 20.5, 0.01);
_TestResult = _TestResult && (_spoil_scenario==-1);
}
catch(ap_error)
{ _TestResult = _TestResult && (_spoil_scenario!=-1); }
}
if( !_TestResult)
{
printf("%-32s FAILED\n", "basestat_d_base");
fflush(stdout);
}
_TotalResult = _TotalResult && _TestResult;
//
// TEST basestat_d_c2
// Correlation (covariance) between two random variables
//
_TestResult = true;
for(_spoil_scenario=-1; _spoil_scenario<10; _spoil_scenario++)
{
try
{
//
// We have two samples - x and y, and want to measure dependency between them
//
real_1d_array x = "[0,1,4,9,16,25,36,49,64,81]";
if( _spoil_scenario==0 )
spoil_vector_by_nan(x);
if( _spoil_scenario==1 )
spoil_vector_by_posinf(x);
if( _spoil_scenario==2 )
spoil_vector_by_neginf(x);
if( _spoil_scenario==3 )
spoil_vector_by_adding_element(x);
if( _spoil_scenario==4 )
spoil_vector_by_deleting_element(x);
real_1d_array y = "[0,1,2,3,4,5,6,7,8,9]";
if( _spoil_scenario==5 )
spoil_vector_by_nan(y);
if( _spoil_scenario==6 )
spoil_vector_by_posinf(y);
if( _spoil_scenario==7 )
spoil_vector_by_neginf(y);
if( _spoil_scenario==8 )
spoil_vector_by_adding_element(y);
if( _spoil_scenario==9 )
spoil_vector_by_deleting_element(y);
double v;
//
// Three dependency measures are calculated:
// * covariation
// * Pearson correlation
// * Spearman rank correlation
//
v = cov2(x, y);
_TestResult = _TestResult && doc_test_real(v, 82.5, 0.001);
v = pearsoncorr2(x, y);
_TestResult = _TestResult && doc_test_real(v, 0.9627, 0.001);
v = spearmancorr2(x, y);
_TestResult = _TestResult && doc_test_real(v, 1.000, 0.001);
_TestResult = _TestResult && (_spoil_scenario==-1);
}
catch(ap_error)
{ _TestResult = _TestResult && (_spoil_scenario!=-1); }
}
if( !_TestResult)
{
printf("%-32s FAILED\n", "basestat_d_c2");
fflush(stdout);
}
_TotalResult = _TotalResult && _TestResult;
//
// TEST basestat_d_cm
// Correlation (covariance) between components of random vector
//
_TestResult = true;
for(_spoil_scenario=-1; _spoil_scenario<3; _spoil_scenario++)
{
try
{
//
// X is a sample matrix:
// * I-th row corresponds to I-th observation
// * J-th column corresponds to J-th variable
//
real_2d_array x = "[[1,0,1],[1,1,0],[-1,1,0],[-2,-1,1],[-1,0,9]]";
if( _spoil_scenario==0 )
spoil_matrix_by_nan(x);
if( _spoil_scenario==1 )
spoil_matrix_by_posinf(x);
if( _spoil_scenario==2 )
spoil_matrix_by_neginf(x);
real_2d_array c;
//
// Three dependency measures are calculated:
// * covariation
// * Pearson correlation
// * Spearman rank correlation
//
// Result is stored into C, with C[i,j] equal to correlation
// (covariance) between I-th and J-th variables of X.
//
covm(x, c);
_TestResult = _TestResult && doc_test_real_matrix(c, "[[1.80,0.60,-1.40],[0.60,0.70,-0.80],[-1.40,-0.80,14.70]]", 0.01);
pearsoncorrm(x, c);
_TestResult = _TestResult && doc_test_real_matrix(c, "[[1.000,0.535,-0.272],[0.535,1.000,-0.249],[-0.272,-0.249,1.000]]", 0.01);
spearmancorrm(x, c);
_TestResult = _TestResult && doc_test_real_matrix(c, "[[1.000,0.556,-0.306],[0.556,1.000,-0.750],[-0.306,-0.750,1.000]]", 0.01);
_TestResult = _TestResult && (_spoil_scenario==-1);
}
catch(ap_error)
{ _TestResult = _TestResult && (_spoil_scenario!=-1); }
}
if( !_TestResult)
{
printf("%-32s FAILED\n", "basestat_d_cm");
fflush(stdout);
}
_TotalResult = _TotalResult && _TestResult;
//
// TEST basestat_d_cm2
// Correlation (covariance) between two random vectors
//
_TestResult = true;
for(_spoil_scenario=-1; _spoil_scenario<6; _spoil_scenario++)
{
try
{
//
// X and Y are sample matrices:
// * I-th row corresponds to I-th observation
// * J-th column corresponds to J-th variable
//
real_2d_array x = "[[1,0,1],[1,1,0],[-1,1,0],[-2,-1,1],[-1,0,9]]";
if( _spoil_scenario==0 )
spoil_matrix_by_nan(x);
if( _spoil_scenario==1 )
spoil_matrix_by_posinf(x);
if( _spoil_scenario==2 )
spoil_matrix_by_neginf(x);
real_2d_array y = "[[2,3],[2,1],[-1,6],[-9,9],[7,1]]";
if( _spoil_scenario==3 )
spoil_matrix_by_nan(y);
if( _spoil_scenario==4 )
spoil_matrix_by_posinf(y);
if( _spoil_scenario==5 )
spoil_matrix_by_neginf(y);
real_2d_array c;
//
// Three dependency measures are calculated:
// * covariation
// * Pearson correlation
// * Spearman rank correlation
//
// Result is stored into C, with C[i,j] equal to correlation
// (covariance) between I-th variable of X and J-th variable of Y.
//
covm2(x, y, c);
_TestResult = _TestResult && doc_test_real_matrix(c, "[[4.100,-3.250],[2.450,-1.500],[13.450,-5.750]]", 0.01);
pearsoncorrm2(x, y, c);
_TestResult = _TestResult && doc_test_real_matrix(c, "[[0.519,-0.699],[0.497,-0.518],[0.596,-0.433]]", 0.01);
spearmancorrm2(x, y, c);
_TestResult = _TestResult && doc_test_real_matrix(c, "[[0.541,-0.649],[0.216,-0.433],[0.433,-0.135]]", 0.01);
_TestResult = _TestResult && (_spoil_scenario==-1);
}
catch(ap_error)
{ _TestResult = _TestResult && (_spoil_scenario!=-1); }
}
if( !_TestResult)
{
printf("%-32s FAILED\n", "basestat_d_cm2");
fflush(stdout);
}
_TotalResult = _TotalResult && _TestResult;
//
// TEST basestat_t_base
// Tests ability to detect errors in inputs
//
_TestResult = true;
for(_spoil_scenario=-1; _spoil_scenario<34; _spoil_scenario++)
{
try
{
double mean;
double variance;
double skewness;
double kurtosis;
double adev;
double p;
double v;
//
// first, we test short form of functions
//
real_1d_array x1 = "[0,1,4,9,16,25,36,49,64,81]";
if( _spoil_scenario==0 )
spoil_vector_by_nan(x1);
if( _spoil_scenario==1 )
spoil_vector_by_posinf(x1);
if( _spoil_scenario==2 )
spoil_vector_by_neginf(x1);
samplemoments(x1, mean, variance, skewness, kurtosis);
real_1d_array x2 = "[0,1,4,9,16,25,36,49,64,81]";
if( _spoil_scenario==3 )
spoil_vector_by_nan(x2);
if( _spoil_scenario==4 )
spoil_vector_by_posinf(x2);
if( _spoil_scenario==5 )
spoil_vector_by_neginf(x2);
sampleadev(x2, adev);
real_1d_array x3 = "[0,1,4,9,16,25,36,49,64,81]";
if( _spoil_scenario==6 )
spoil_vector_by_nan(x3);
if( _spoil_scenario==7 )
spoil_vector_by_posinf(x3);
if( _spoil_scenario==8 )
spoil_vector_by_neginf(x3);
samplemedian(x3, v);
real_1d_array x4 = "[0,1,4,9,16,25,36,49,64,81]";
if( _spoil_scenario==9 )
spoil_vector_by_nan(x4);
if( _spoil_scenario==10 )
spoil_vector_by_posinf(x4);
if( _spoil_scenario==11 )
spoil_vector_by_neginf(x4);
p = 0.5;
if( _spoil_scenario==12 )
p = fp_nan;
if( _spoil_scenario==13 )
p = fp_posinf;
if( _spoil_scenario==14 )
p = fp_neginf;
samplepercentile(x4, p, v);
//
// and then we test full form
//
real_1d_array x5 = "[0,1,4,9,16,25,36,49,64,81]";
if( _spoil_scenario==15 )
spoil_vector_by_nan(x5);
if( _spoil_scenario==16 )
spoil_vector_by_posinf(x5);
if( _spoil_scenario==17 )
spoil_vector_by_neginf(x5);
if( _spoil_scenario==18 )
spoil_vector_by_deleting_element(x5);
samplemoments(x5, 10, mean, variance, skewness, kurtosis);
real_1d_array x6 = "[0,1,4,9,16,25,36,49,64,81]";
if( _spoil_scenario==19 )
spoil_vector_by_nan(x6);
if( _spoil_scenario==20 )
spoil_vector_by_posinf(x6);
if( _spoil_scenario==21 )
spoil_vector_by_neginf(x6);
if( _spoil_scenario==22 )
spoil_vector_by_deleting_element(x6);
sampleadev(x6, 10, adev);
real_1d_array x7 = "[0,1,4,9,16,25,36,49,64,81]";
if( _spoil_scenario==23 )
spoil_vector_by_nan(x7);
if( _spoil_scenario==24 )
spoil_vector_by_posinf(x7);
if( _spoil_scenario==25 )
spoil_vector_by_neginf(x7);
if( _spoil_scenario==26 )
spoil_vector_by_deleting_element(x7);
samplemedian(x7, 10, v);
real_1d_array x8 = "[0,1,4,9,16,25,36,49,64,81]";
if( _spoil_scenario==27 )
spoil_vector_by_nan(x8);
if( _spoil_scenario==28 )
spoil_vector_by_posinf(x8);
if( _spoil_scenario==29 )
spoil_vector_by_neginf(x8);
if( _spoil_scenario==30 )
spoil_vector_by_deleting_element(x8);
p = 0.5;
if( _spoil_scenario==31 )
p = fp_nan;
if( _spoil_scenario==32 )
p = fp_posinf;
if( _spoil_scenario==33 )
p = fp_neginf;
samplepercentile(x8, 10, p, v);
_TestResult = _TestResult && (_spoil_scenario==-1);
}
catch(ap_error)
{ _TestResult = _TestResult && (_spoil_scenario!=-1); }
}
if( !_TestResult)
{
printf("%-32s FAILED\n", "basestat_t_base");
fflush(stdout);
}
_TotalResult = _TotalResult && _TestResult;
//
// TEST basestat_t_covcorr
// Tests ability to detect errors in inputs
//
_TestResult = true;
for(_spoil_scenario=-1; _spoil_scenario<126; _spoil_scenario++)
{
try
{
double v;
real_2d_array c;
//
// 2-sample short-form cov/corr are tested
//
real_1d_array x1 = "[0,1,4,9,16,25,36,49,64,81]";
if( _spoil_scenario==0 )
spoil_vector_by_nan(x1);
if( _spoil_scenario==1 )
spoil_vector_by_posinf(x1);
if( _spoil_scenario==2 )
spoil_vector_by_neginf(x1);
if( _spoil_scenario==3 )
spoil_vector_by_adding_element(x1);
if( _spoil_scenario==4 )
spoil_vector_by_deleting_element(x1);
real_1d_array y1 = "[0,1,2,3,4,5,6,7,8,9]";
if( _spoil_scenario==5 )
spoil_vector_by_nan(y1);
if( _spoil_scenario==6 )
spoil_vector_by_posinf(y1);
if( _spoil_scenario==7 )
spoil_vector_by_neginf(y1);
if( _spoil_scenario==8 )
spoil_vector_by_adding_element(y1);
if( _spoil_scenario==9 )
spoil_vector_by_deleting_element(y1);
v = cov2(x1, y1);
real_1d_array x2 = "[0,1,4,9,16,25,36,49,64,81]";
if( _spoil_scenario==10 )
spoil_vector_by_nan(x2);
if( _spoil_scenario==11 )
spoil_vector_by_posinf(x2);
if( _spoil_scenario==12 )
spoil_vector_by_neginf(x2);
if( _spoil_scenario==13 )
spoil_vector_by_adding_element(x2);
if( _spoil_scenario==14 )
spoil_vector_by_deleting_element(x2);
real_1d_array y2 = "[0,1,2,3,4,5,6,7,8,9]";
if( _spoil_scenario==15 )
spoil_vector_by_nan(y2);
if( _spoil_scenario==16 )
spoil_vector_by_posinf(y2);
if( _spoil_scenario==17 )
spoil_vector_by_neginf(y2);
if( _spoil_scenario==18 )
spoil_vector_by_adding_element(y2);
if( _spoil_scenario==19 )
spoil_vector_by_deleting_element(y2);
v = pearsoncorr2(x2, y2);
real_1d_array x3 = "[0,1,4,9,16,25,36,49,64,81]";
if( _spoil_scenario==20 )
spoil_vector_by_nan(x3);
if( _spoil_scenario==21 )
spoil_vector_by_posinf(x3);
if( _spoil_scenario==22 )
spoil_vector_by_neginf(x3);
if( _spoil_scenario==23 )
spoil_vector_by_adding_element(x3);
if( _spoil_scenario==24 )
spoil_vector_by_deleting_element(x3);
real_1d_array y3 = "[0,1,2,3,4,5,6,7,8,9]";
if( _spoil_scenario==25 )
spoil_vector_by_nan(y3);
if( _spoil_scenario==26 )
spoil_vector_by_posinf(y3);
if( _spoil_scenario==27 )
spoil_vector_by_neginf(y3);
if( _spoil_scenario==28 )
spoil_vector_by_adding_element(y3);
if( _spoil_scenario==29 )
spoil_vector_by_deleting_element(y3);
v = spearmancorr2(x3, y3);
//
// 2-sample full-form cov/corr are tested
//
real_1d_array x1a = "[0,1,4,9,16,25,36,49,64,81]";
if( _spoil_scenario==30 )
spoil_vector_by_nan(x1a);
if( _spoil_scenario==31 )
spoil_vector_by_posinf(x1a);
if( _spoil_scenario==32 )
spoil_vector_by_neginf(x1a);
if( _spoil_scenario==33 )
spoil_vector_by_deleting_element(x1a);
real_1d_array y1a = "[0,1,2,3,4,5,6,7,8,9]";
if( _spoil_scenario==34 )
spoil_vector_by_nan(y1a);
if( _spoil_scenario==35 )
spoil_vector_by_posinf(y1a);
if( _spoil_scenario==36 )
spoil_vector_by_neginf(y1a);
if( _spoil_scenario==37 )
spoil_vector_by_deleting_element(y1a);
v = cov2(x1a, y1a, 10);
real_1d_array x2a = "[0,1,4,9,16,25,36,49,64,81]";
if( _spoil_scenario==38 )
spoil_vector_by_nan(x2a);
if( _spoil_scenario==39 )
spoil_vector_by_posinf(x2a);
if( _spoil_scenario==40 )
spoil_vector_by_neginf(x2a);
if( _spoil_scenario==41 )
spoil_vector_by_deleting_element(x2a);
real_1d_array y2a = "[0,1,2,3,4,5,6,7,8,9]";
if( _spoil_scenario==42 )
spoil_vector_by_nan(y2a);
if( _spoil_scenario==43 )
spoil_vector_by_posinf(y2a);
if( _spoil_scenario==44 )
spoil_vector_by_neginf(y2a);
if( _spoil_scenario==45 )
spoil_vector_by_deleting_element(y2a);
v = pearsoncorr2(x2a, y2a, 10);
real_1d_array x3a = "[0,1,4,9,16,25,36,49,64,81]";
if( _spoil_scenario==46 )
spoil_vector_by_nan(x3a);
if( _spoil_scenario==47 )
spoil_vector_by_posinf(x3a);
if( _spoil_scenario==48 )
spoil_vector_by_neginf(x3a);
if( _spoil_scenario==49 )
spoil_vector_by_deleting_element(x3a);
real_1d_array y3a = "[0,1,2,3,4,5,6,7,8,9]";
if( _spoil_scenario==50 )
spoil_vector_by_nan(y3a);
if( _spoil_scenario==51 )
spoil_vector_by_posinf(y3a);
if( _spoil_scenario==52 )
spoil_vector_by_neginf(y3a);
if( _spoil_scenario==53 )
spoil_vector_by_deleting_element(y3a);
v = spearmancorr2(x3a, y3a, 10);
//
// vector short-form cov/corr are tested.
//
real_2d_array x4 = "[[1,0,1],[1,1,0],[-1,1,0],[-2,-1,1],[-1,0,9]]";
if( _spoil_scenario==54 )
spoil_matrix_by_nan(x4);
if( _spoil_scenario==55 )
spoil_matrix_by_posinf(x4);
if( _spoil_scenario==56 )
spoil_matrix_by_neginf(x4);
covm(x4, c);
real_2d_array x5 = "[[1,0,1],[1,1,0],[-1,1,0],[-2,-1,1],[-1,0,9]]";
if( _spoil_scenario==57 )
spoil_matrix_by_nan(x5);
if( _spoil_scenario==58 )
spoil_matrix_by_posinf(x5);
if( _spoil_scenario==59 )
spoil_matrix_by_neginf(x5);
pearsoncorrm(x5, c);
real_2d_array x6 = "[[1,0,1],[1,1,0],[-1,1,0],[-2,-1,1],[-1,0,9]]";
if( _spoil_scenario==60 )
spoil_matrix_by_nan(x6);
if( _spoil_scenario==61 )
spoil_matrix_by_posinf(x6);
if( _spoil_scenario==62 )
spoil_matrix_by_neginf(x6);
spearmancorrm(x6, c);
//
// vector full-form cov/corr are tested.
//
real_2d_array x7 = "[[1,0,1],[1,1,0],[-1,1,0],[-2,-1,1],[-1,0,9]]";
if( _spoil_scenario==63 )
spoil_matrix_by_nan(x7);
if( _spoil_scenario==64 )
spoil_matrix_by_posinf(x7);
if( _spoil_scenario==65 )
spoil_matrix_by_neginf(x7);
if( _spoil_scenario==66 )
spoil_matrix_by_deleting_row(x7);
if( _spoil_scenario==67 )
spoil_matrix_by_deleting_col(x7);
covm(x7, 5, 3, c);
real_2d_array x8 = "[[1,0,1],[1,1,0],[-1,1,0],[-2,-1,1],[-1,0,9]]";
if( _spoil_scenario==68 )
spoil_matrix_by_nan(x8);
if( _spoil_scenario==69 )
spoil_matrix_by_posinf(x8);
if( _spoil_scenario==70 )
spoil_matrix_by_neginf(x8);
if( _spoil_scenario==71 )
spoil_matrix_by_deleting_row(x8);
if( _spoil_scenario==72 )
spoil_matrix_by_deleting_col(x8);
pearsoncorrm(x8, 5, 3, c);
real_2d_array x9 = "[[1,0,1],[1,1,0],[-1,1,0],[-2,-1,1],[-1,0,9]]";
if( _spoil_scenario==73 )
spoil_matrix_by_nan(x9);
if( _spoil_scenario==74 )
spoil_matrix_by_posinf(x9);
if( _spoil_scenario==75 )
spoil_matrix_by_neginf(x9);
if( _spoil_scenario==76 )
spoil_matrix_by_deleting_row(x9);
if( _spoil_scenario==77 )
spoil_matrix_by_deleting_col(x9);
spearmancorrm(x9, 5, 3, c);
//
// cross-vector short-form cov/corr are tested.
//
real_2d_array x10 = "[[1,0,1],[1,1,0],[-1,1,0],[-2,-1,1],[-1,0,9]]";
if( _spoil_scenario==78 )
spoil_matrix_by_nan(x10);
if( _spoil_scenario==79 )
spoil_matrix_by_posinf(x10);
if( _spoil_scenario==80 )
spoil_matrix_by_neginf(x10);
real_2d_array y10 = "[[2,3],[2,1],[-1,6],[-9,9],[7,1]]";
if( _spoil_scenario==81 )
spoil_matrix_by_nan(y10);
if( _spoil_scenario==82 )
spoil_matrix_by_posinf(y10);
if( _spoil_scenario==83 )
spoil_matrix_by_neginf(y10);
covm2(x10, y10, c);
real_2d_array x11 = "[[1,0,1],[1,1,0],[-1,1,0],[-2,-1,1],[-1,0,9]]";
if( _spoil_scenario==84 )
spoil_matrix_by_nan(x11);
if( _spoil_scenario==85 )
spoil_matrix_by_posinf(x11);
if( _spoil_scenario==86 )
spoil_matrix_by_neginf(x11);
real_2d_array y11 = "[[2,3],[2,1],[-1,6],[-9,9],[7,1]]";
if( _spoil_scenario==87 )
spoil_matrix_by_nan(y11);
if( _spoil_scenario==88 )
spoil_matrix_by_posinf(y11);
if( _spoil_scenario==89 )
spoil_matrix_by_neginf(y11);
pearsoncorrm2(x11, y11, c);
real_2d_array x12 = "[[1,0,1],[1,1,0],[-1,1,0],[-2,-1,1],[-1,0,9]]";
if( _spoil_scenario==90 )
spoil_matrix_by_nan(x12);
if( _spoil_scenario==91 )
spoil_matrix_by_posinf(x12);
if( _spoil_scenario==92 )
spoil_matrix_by_neginf(x12);
real_2d_array y12 = "[[2,3],[2,1],[-1,6],[-9,9],[7,1]]";
if( _spoil_scenario==93 )
spoil_matrix_by_nan(y12);
if( _spoil_scenario==94 )
spoil_matrix_by_posinf(y12);
if( _spoil_scenario==95 )
spoil_matrix_by_neginf(y12);
spearmancorrm2(x12, y12, c);
//
// cross-vector full-form cov/corr are tested.
//
real_2d_array x13 = "[[1,0,1],[1,1,0],[-1,1,0],[-2,-1,1],[-1,0,9]]";
if( _spoil_scenario==96 )
spoil_matrix_by_nan(x13);
if( _spoil_scenario==97 )
spoil_matrix_by_posinf(x13);
if( _spoil_scenario==98 )
spoil_matrix_by_neginf(x13);
if( _spoil_scenario==99 )
spoil_matrix_by_deleting_row(x13);
if( _spoil_scenario==100 )
spoil_matrix_by_deleting_col(x13);
real_2d_array y13 = "[[2,3],[2,1],[-1,6],[-9,9],[7,1]]";
if( _spoil_scenario==101 )
spoil_matrix_by_nan(y13);
if( _spoil_scenario==102 )
spoil_matrix_by_posinf(y13);
if( _spoil_scenario==103 )
spoil_matrix_by_neginf(y13);
if( _spoil_scenario==104 )
spoil_matrix_by_deleting_row(y13);
if( _spoil_scenario==105 )
spoil_matrix_by_deleting_col(y13);
covm2(x13, y13, 5, 3, 2, c);
real_2d_array x14 = "[[1,0,1],[1,1,0],[-1,1,0],[-2,-1,1],[-1,0,9]]";
if( _spoil_scenario==106 )
spoil_matrix_by_nan(x14);
if( _spoil_scenario==107 )
spoil_matrix_by_posinf(x14);
if( _spoil_scenario==108 )
spoil_matrix_by_neginf(x14);
if( _spoil_scenario==109 )
spoil_matrix_by_deleting_row(x14);
if( _spoil_scenario==110 )
spoil_matrix_by_deleting_col(x14);
real_2d_array y14 = "[[2,3],[2,1],[-1,6],[-9,9],[7,1]]";
if( _spoil_scenario==111 )
spoil_matrix_by_nan(y14);
if( _spoil_scenario==112 )
spoil_matrix_by_posinf(y14);
if( _spoil_scenario==113 )
spoil_matrix_by_neginf(y14);
if( _spoil_scenario==114 )
spoil_matrix_by_deleting_row(y14);
if( _spoil_scenario==115 )
spoil_matrix_by_deleting_col(y14);
pearsoncorrm2(x14, y14, 5, 3, 2, c);
real_2d_array x15 = "[[1,0,1],[1,1,0],[-1,1,0],[-2,-1,1],[-1,0,9]]";
if( _spoil_scenario==116 )
spoil_matrix_by_nan(x15);
if( _spoil_scenario==117 )
spoil_matrix_by_posinf(x15);
if( _spoil_scenario==118 )
spoil_matrix_by_neginf(x15);
if( _spoil_scenario==119 )
spoil_matrix_by_deleting_row(x15);
if( _spoil_scenario==120 )
spoil_matrix_by_deleting_col(x15);
real_2d_array y15 = "[[2,3],[2,1],[-1,6],[-9,9],[7,1]]";
if( _spoil_scenario==121 )
spoil_matrix_by_nan(y15);
if( _spoil_scenario==122 )
spoil_matrix_by_posinf(y15);
if( _spoil_scenario==123 )
spoil_matrix_by_neginf(y15);
if( _spoil_scenario==124 )
spoil_matrix_by_deleting_row(y15);
if( _spoil_scenario==125 )
spoil_matrix_by_deleting_col(y15);
spearmancorrm2(x15, y15, 5, 3, 2, c);
_TestResult = _TestResult && (_spoil_scenario==-1);
}
catch(ap_error)
{ _TestResult = _TestResult && (_spoil_scenario!=-1); }
}
if( !_TestResult)
{
printf("%-32s FAILED\n", "basestat_t_covcorr");
fflush(stdout);
}
_TotalResult = _TotalResult && _TestResult;
//
// TEST idw_d_mstab
// Simple model built with IDW-MSTAB algorithm
//
_TestResult = true;
for(_spoil_scenario=-1; _spoil_scenario<3; _spoil_scenario++)
{
try
{
//
// This example illustrates basic concepts of the IDW models:
// creation and evaluation.
//
// Suppose that we have set of 2-dimensional points with associated
// scalar function values, and we want to build an IDW model using
// our data.
//
// NOTE: we can work with N-dimensional models and vector-valued functions too :)
//
// Typical sequence of steps is given below:
// 1. we create IDW builder object
// 2. we attach our dataset to the IDW builder and tune algorithm settings
// 3. we generate IDW model
// 4. we use IDW model instance (evaluate, serialize, etc.)
//
double v;
//
// Step 1: IDW builder creation.
//
// We have to specify dimensionality of the space (2 or 3) and
// dimensionality of the function (scalar or vector).
//
// New builder object is empty - it has not dataset and uses
// default model construction settings
//
idwbuilder builder;
idwbuildercreate(2, 1, builder);
//
// Step 2: dataset addition
//
// XY contains two points - x0=(-1,0) and x1=(+1,0) -
// and two function values f(x0)=2, f(x1)=3.
//
real_2d_array xy = "[[-1,0,2],[+1,0,3]]";
if( _spoil_scenario==0 )
spoil_matrix_by_nan(xy);
if( _spoil_scenario==1 )
spoil_matrix_by_posinf(xy);
if( _spoil_scenario==2 )
spoil_matrix_by_neginf(xy);
idwbuildersetpoints(builder, xy);
//
// Step 3: choose IDW algorithm and generate model
//
// We use modified stabilized IDW algorithm with following parameters:
// * SRad - set to 5.0 (search radius must be large enough)
//
// IDW-MSTAB algorithm is a state-of-the-art implementation of IDW which
// is competitive with RBFs and bicubic splines. See comments on the
// idwbuildersetalgomstab() function for more information.
//
idwmodel model;
idwreport rep;
idwbuildersetalgomstab(builder, 5.0);
idwfit(builder, model, rep);
//
// Step 4: model was built, evaluate its value
//
v = idwcalc2(model, 1.0, 0.0);
_TestResult = _TestResult && doc_test_real(v, 3.000, 0.005);
_TestResult = _TestResult && (_spoil_scenario==-1);
}
catch(ap_error)
{ _TestResult = _TestResult && (_spoil_scenario!=-1); }
}
if( !_TestResult)
{
printf("%-32s FAILED\n", "idw_d_mstab");
fflush(stdout);
}
_TotalResult = _TotalResult && _TestResult;
//
// TEST idw_d_serialize
// IDW model serialization/unserialization
//
_TestResult = true;
for(_spoil_scenario=-1; _spoil_scenario<3; _spoil_scenario++)
{
try
{
//
// This example shows how to serialize and unserialize IDW model.
//
// Suppose that we have set of 2-dimensional points with associated
// scalar function values, and we have built an IDW model using
// our data.
//
// This model can be serialized to string or stream. ALGLIB supports
// flexible (un)serialization, i.e. you can move serialized model
// representation between different machines (32-bit or 64-bit),
// different CPU architectures (x86/64, ARM) or even different
// programming languages supported by ALGLIB (C#, C++, ...).
//
// Our first step is to build model, evaluate it at point (1,0),
// and serialize it to string.
//
std::string s;
double v;
real_2d_array xy = "[[-1,0,2],[+1,0,3]]";
if( _spoil_scenario==0 )
spoil_matrix_by_nan(xy);
if( _spoil_scenario==1 )
spoil_matrix_by_posinf(xy);
if( _spoil_scenario==2 )
spoil_matrix_by_neginf(xy);
idwbuilder builder;
idwmodel model;
idwmodel model2;
idwreport rep;
idwbuildercreate(2, 1, builder);
idwbuildersetpoints(builder, xy);
idwbuildersetalgomstab(builder, 5.0);
idwfit(builder, model, rep);
v = idwcalc2(model, 1.0, 0.0);
_TestResult = _TestResult && doc_test_real(v, 3.000, 0.005);
//
// Serialization + unserialization to a different instance
// of the model class.
//
alglib::idwserialize(model, s);
alglib::idwunserialize(s, model2);
//
// Evaluate unserialized model at the same point
//
v = idwcalc2(model2, 1.0, 0.0);
_TestResult = _TestResult && doc_test_real(v, 3.000, 0.005);
_TestResult = _TestResult && (_spoil_scenario==-1);
}
catch(ap_error)
{ _TestResult = _TestResult && (_spoil_scenario!=-1); }
}
if( !_TestResult)
{
printf("%-32s FAILED\n", "idw_d_serialize");
fflush(stdout);
}
_TotalResult = _TotalResult && _TestResult;
//
// TEST polint_d_calcdiff
// Interpolation and differentiation using barycentric representation
//
_TestResult = true;
for(_spoil_scenario=-1; _spoil_scenario<12; _spoil_scenario++)
{
try
{
//
// Here we demonstrate polynomial interpolation and differentiation
// of y=x^2-x sampled at [0,1,2]. Barycentric representation of polynomial is used.
//
real_1d_array x = "[0,1,2]";
if( _spoil_scenario==0 )
spoil_vector_by_nan(x);
if( _spoil_scenario==1 )
spoil_vector_by_posinf(x);
if( _spoil_scenario==2 )
spoil_vector_by_neginf(x);
if( _spoil_scenario==3 )
spoil_vector_by_adding_element(x);
if( _spoil_scenario==4 )
spoil_vector_by_deleting_element(x);
real_1d_array y = "[0,0,2]";
if( _spoil_scenario==5 )
spoil_vector_by_nan(y);
if( _spoil_scenario==6 )
spoil_vector_by_posinf(y);
if( _spoil_scenario==7 )
spoil_vector_by_neginf(y);
if( _spoil_scenario==8 )
spoil_vector_by_adding_element(y);
if( _spoil_scenario==9 )
spoil_vector_by_deleting_element(y);
double t = -1;
if( _spoil_scenario==10 )
t = fp_posinf;
if( _spoil_scenario==11 )
t = fp_neginf;
double v;
double dv;
double d2v;
barycentricinterpolant p;
// barycentric model is created
polynomialbuild(x, y, p);
// barycentric interpolation is demonstrated
v = barycentriccalc(p, t);
_TestResult = _TestResult && doc_test_real(v, 2.0, 0.00005);
// barycentric differentation is demonstrated
barycentricdiff1(p, t, v, dv);
_TestResult = _TestResult && doc_test_real(v, 2.0, 0.00005);
_TestResult = _TestResult && doc_test_real(dv, -3.0, 0.00005);
// second derivatives with barycentric representation
barycentricdiff1(p, t, v, dv);
_TestResult = _TestResult && doc_test_real(v, 2.0, 0.00005);
_TestResult = _TestResult && doc_test_real(dv, -3.0, 0.00005);
barycentricdiff2(p, t, v, dv, d2v);
_TestResult = _TestResult && doc_test_real(v, 2.0, 0.00005);
_TestResult = _TestResult && doc_test_real(dv, -3.0, 0.00005);
_TestResult = _TestResult && doc_test_real(d2v, 2.0, 0.00005);
_TestResult = _TestResult && (_spoil_scenario==-1);
}
catch(ap_error)
{ _TestResult = _TestResult && (_spoil_scenario!=-1); }
}
if( !_TestResult)
{
printf("%-32s FAILED\n", "polint_d_calcdiff");
fflush(stdout);
}
_TotalResult = _TotalResult && _TestResult;
//
// TEST polint_d_conv
// Conversion between power basis and barycentric representation
//
_TestResult = true;
for(_spoil_scenario=-1; _spoil_scenario<5; _spoil_scenario++)
{
try
{
//
// Here we demonstrate conversion of y=x^2-x
// between power basis and barycentric representation.
//
real_1d_array a = "[0,-1,+1]";
if( _spoil_scenario==0 )
spoil_vector_by_nan(a);
if( _spoil_scenario==1 )
spoil_vector_by_posinf(a);
if( _spoil_scenario==2 )
spoil_vector_by_neginf(a);
double t = 2;
if( _spoil_scenario==3 )
t = fp_posinf;
if( _spoil_scenario==4 )
t = fp_neginf;
real_1d_array a2;
double v;
barycentricinterpolant p;
//
// a=[0,-1,+1] is decomposition of y=x^2-x in the power basis:
//
// y = 0 - 1*x + 1*x^2
//
// We convert it to the barycentric form.
//
polynomialpow2bar(a, p);
// now we have barycentric interpolation; we can use it for interpolation
v = barycentriccalc(p, t);
_TestResult = _TestResult && doc_test_real(v, 2.0, 0.005);
// we can also convert back from barycentric representation to power basis
polynomialbar2pow(p, a2);
_TestResult = _TestResult && doc_test_real_vector(a2, "[0,-1,+1]", 0.005);
_TestResult = _TestResult && (_spoil_scenario==-1);
}
catch(ap_error)
{ _TestResult = _TestResult && (_spoil_scenario!=-1); }
}
if( !_TestResult)
{
printf("%-32s FAILED\n", "polint_d_conv");
fflush(stdout);
}
_TotalResult = _TotalResult && _TestResult;
//
// TEST polint_d_spec
// Polynomial interpolation on special grids (equidistant, Chebyshev I/II)
//
_TestResult = true;
for(_spoil_scenario=-1; _spoil_scenario<11; _spoil_scenario++)
{
try
{
//
// Temporaries:
// * values of y=x^2-x sampled at three special grids:
// * equdistant grid spanning [0,2], x[i] = 2*i/(N-1), i=0..N-1
// * Chebyshev-I grid spanning [-1,+1], x[i] = 1 + Cos(PI*(2*i+1)/(2*n)), i=0..N-1
// * Chebyshev-II grid spanning [-1,+1], x[i] = 1 + Cos(PI*i/(n-1)), i=0..N-1
// * barycentric interpolants for these three grids
// * vectors to store coefficients of quadratic representation
//
real_1d_array y_eqdist = "[0,0,2]";
if( _spoil_scenario==0 )
spoil_vector_by_nan(y_eqdist);
if( _spoil_scenario==1 )
spoil_vector_by_posinf(y_eqdist);
if( _spoil_scenario==2 )
spoil_vector_by_neginf(y_eqdist);
real_1d_array y_cheb1 = "[-0.116025,0.000000,1.616025]";
if( _spoil_scenario==3 )
spoil_vector_by_nan(y_cheb1);
if( _spoil_scenario==4 )
spoil_vector_by_posinf(y_cheb1);
if( _spoil_scenario==5 )
spoil_vector_by_neginf(y_cheb1);
real_1d_array y_cheb2 = "[0,0,2]";
if( _spoil_scenario==6 )
spoil_vector_by_nan(y_cheb2);
if( _spoil_scenario==7 )
spoil_vector_by_posinf(y_cheb2);
if( _spoil_scenario==8 )
spoil_vector_by_neginf(y_cheb2);
barycentricinterpolant p_eqdist;
barycentricinterpolant p_cheb1;
barycentricinterpolant p_cheb2;
real_1d_array a_eqdist;
real_1d_array a_cheb1;
real_1d_array a_cheb2;
//
// First, we demonstrate construction of barycentric interpolants on
// special grids. We unpack power representation to ensure that
// interpolant was built correctly.
//
// In all three cases we should get same quadratic function.
//
polynomialbuildeqdist(0.0, 2.0, y_eqdist, p_eqdist);
polynomialbar2pow(p_eqdist, a_eqdist);
_TestResult = _TestResult && doc_test_real_vector(a_eqdist, "[0,-1,+1]", 0.00005);
polynomialbuildcheb1(-1, +1, y_cheb1, p_cheb1);
polynomialbar2pow(p_cheb1, a_cheb1);
_TestResult = _TestResult && doc_test_real_vector(a_cheb1, "[0,-1,+1]", 0.00005);
polynomialbuildcheb2(-1, +1, y_cheb2, p_cheb2);
polynomialbar2pow(p_cheb2, a_cheb2);
_TestResult = _TestResult && doc_test_real_vector(a_cheb2, "[0,-1,+1]", 0.00005);
//
// Now we demonstrate polynomial interpolation without construction
// of the barycentricinterpolant structure.
//
// We calculate interpolant value at x=-2.
// In all three cases we should get same f=6
//
double t = -2;
if( _spoil_scenario==9 )
t = fp_posinf;
if( _spoil_scenario==10 )
t = fp_neginf;
double v;
v = polynomialcalceqdist(0.0, 2.0, y_eqdist, t);
_TestResult = _TestResult && doc_test_real(v, 6.0, 0.00005);
v = polynomialcalccheb1(-1, +1, y_cheb1, t);
_TestResult = _TestResult && doc_test_real(v, 6.0, 0.00005);
v = polynomialcalccheb2(-1, +1, y_cheb2, t);
_TestResult = _TestResult && doc_test_real(v, 6.0, 0.00005);
_TestResult = _TestResult && (_spoil_scenario==-1);
}
catch(ap_error)
{ _TestResult = _TestResult && (_spoil_scenario!=-1); }
}
if( !_TestResult)
{
printf("%-32s FAILED\n", "polint_d_spec");
fflush(stdout);
}
_TotalResult = _TotalResult && _TestResult;
//
// TEST polint_t_1
// Polynomial interpolation, full list of parameters.
//
_TestResult = true;
for(_spoil_scenario=-1; _spoil_scenario<10; _spoil_scenario++)
{
try
{
real_1d_array x = "[0,1,2]";
if( _spoil_scenario==0 )
spoil_vector_by_nan(x);
if( _spoil_scenario==1 )
spoil_vector_by_posinf(x);
if( _spoil_scenario==2 )
spoil_vector_by_neginf(x);
if( _spoil_scenario==3 )
spoil_vector_by_deleting_element(x);
real_1d_array y = "[0,0,2]";
if( _spoil_scenario==4 )
spoil_vector_by_nan(y);
if( _spoil_scenario==5 )
spoil_vector_by_posinf(y);
if( _spoil_scenario==6 )
spoil_vector_by_neginf(y);
if( _spoil_scenario==7 )
spoil_vector_by_deleting_element(y);
double t = -1;
if( _spoil_scenario==8 )
t = fp_posinf;
if( _spoil_scenario==9 )
t = fp_neginf;
barycentricinterpolant p;
double v;
polynomialbuild(x, y, 3, p);
v = barycentriccalc(p, t);
_TestResult = _TestResult && doc_test_real(v, 2.0, 0.00005);
_TestResult = _TestResult && (_spoil_scenario==-1);
}
catch(ap_error)
{ _TestResult = _TestResult && (_spoil_scenario!=-1); }
}
if( !_TestResult)
{
printf("%-32s FAILED\n", "polint_t_1");
fflush(stdout);
}
_TotalResult = _TotalResult && _TestResult;
//
// TEST polint_t_2
// Polynomial interpolation, full list of parameters.
//
_TestResult = true;
for(_spoil_scenario=-1; _spoil_scenario<6; _spoil_scenario++)
{
try
{
real_1d_array y = "[0,0,2]";
if( _spoil_scenario==0 )
spoil_vector_by_nan(y);
if( _spoil_scenario==1 )
spoil_vector_by_posinf(y);
if( _spoil_scenario==2 )
spoil_vector_by_neginf(y);
if( _spoil_scenario==3 )
spoil_vector_by_deleting_element(y);
double t = -1;
if( _spoil_scenario==4 )
t = fp_posinf;
if( _spoil_scenario==5 )
t = fp_neginf;
barycentricinterpolant p;
double v;
polynomialbuildeqdist(0.0, 2.0, y, 3, p);
v = barycentriccalc(p, t);
_TestResult = _TestResult && doc_test_real(v, 2.0, 0.00005);
_TestResult = _TestResult && (_spoil_scenario==-1);
}
catch(ap_error)
{ _TestResult = _TestResult && (_spoil_scenario!=-1); }
}
if( !_TestResult)
{
printf("%-32s FAILED\n", "polint_t_2");
fflush(stdout);
}
_TotalResult = _TotalResult && _TestResult;
//
// TEST polint_t_3
// Polynomial interpolation, full list of parameters.
//
_TestResult = true;
for(_spoil_scenario=-1; _spoil_scenario<6; _spoil_scenario++)
{
try
{
real_1d_array y = "[-0.116025,0.000000,1.616025]";
if( _spoil_scenario==0 )
spoil_vector_by_nan(y);
if( _spoil_scenario==1 )
spoil_vector_by_posinf(y);
if( _spoil_scenario==2 )
spoil_vector_by_neginf(y);
if( _spoil_scenario==3 )
spoil_vector_by_deleting_element(y);
double t = -1;
if( _spoil_scenario==4 )
t = fp_posinf;
if( _spoil_scenario==5 )
t = fp_neginf;
barycentricinterpolant p;
double v;
polynomialbuildcheb1(-1.0, +1.0, y, 3, p);
v = barycentriccalc(p, t);
_TestResult = _TestResult && doc_test_real(v, 2.0, 0.00005);
_TestResult = _TestResult && (_spoil_scenario==-1);
}
catch(ap_error)
{ _TestResult = _TestResult && (_spoil_scenario!=-1); }
}
if( !_TestResult)
{
printf("%-32s FAILED\n", "polint_t_3");
fflush(stdout);
}
_TotalResult = _TotalResult && _TestResult;
//
// TEST polint_t_4
// Polynomial interpolation, full list of parameters.
//
_TestResult = true;
for(_spoil_scenario=-1; _spoil_scenario<12; _spoil_scenario++)
{
try
{
real_1d_array y = "[0,0,2]";
if( _spoil_scenario==0 )
spoil_vector_by_nan(y);
if( _spoil_scenario==1 )
spoil_vector_by_posinf(y);
if( _spoil_scenario==2 )
spoil_vector_by_neginf(y);
if( _spoil_scenario==3 )
spoil_vector_by_deleting_element(y);
double t = -2;
if( _spoil_scenario==4 )
t = fp_posinf;
if( _spoil_scenario==5 )
t = fp_neginf;
double a = -1;
if( _spoil_scenario==6 )
a = fp_nan;
if( _spoil_scenario==7 )
a = fp_posinf;
if( _spoil_scenario==8 )
a = fp_neginf;
double b = +1;
if( _spoil_scenario==9 )
b = fp_nan;
if( _spoil_scenario==10 )
b = fp_posinf;
if( _spoil_scenario==11 )
b = fp_neginf;
barycentricinterpolant p;
double v;
polynomialbuildcheb2(a, b, y, 3, p);
v = barycentriccalc(p, t);
_TestResult = _TestResult && doc_test_real(v, 6.0, 0.00005);
_TestResult = _TestResult && (_spoil_scenario==-1);
}
catch(ap_error)
{ _TestResult = _TestResult && (_spoil_scenario!=-1); }
}
if( !_TestResult)
{
printf("%-32s FAILED\n", "polint_t_4");
fflush(stdout);
}
_TotalResult = _TotalResult && _TestResult;
//
// TEST polint_t_5
// Polynomial interpolation, full list of parameters.
//
_TestResult = true;
for(_spoil_scenario=-1; _spoil_scenario<6; _spoil_scenario++)
{
try
{
real_1d_array y = "[0,0,2]";
if( _spoil_scenario==0 )
spoil_vector_by_nan(y);
if( _spoil_scenario==1 )
spoil_vector_by_posinf(y);
if( _spoil_scenario==2 )
spoil_vector_by_neginf(y);
if( _spoil_scenario==3 )
spoil_vector_by_deleting_element(y);
double t = -1;
if( _spoil_scenario==4 )
t = fp_posinf;
if( _spoil_scenario==5 )
t = fp_neginf;
double v;
v = polynomialcalceqdist(0.0, 2.0, y, 3, t);
_TestResult = _TestResult && doc_test_real(v, 2.0, 0.00005);
_TestResult = _TestResult && (_spoil_scenario==-1);
}
catch(ap_error)
{ _TestResult = _TestResult && (_spoil_scenario!=-1); }
}
if( !_TestResult)
{
printf("%-32s FAILED\n", "polint_t_5");
fflush(stdout);
}
_TotalResult = _TotalResult && _TestResult;
//
// TEST polint_t_6
// Polynomial interpolation, full list of parameters.
//
_TestResult = true;
for(_spoil_scenario=-1; _spoil_scenario<12; _spoil_scenario++)
{
try
{
real_1d_array y = "[-0.116025,0.000000,1.616025]";
if( _spoil_scenario==0 )
spoil_vector_by_nan(y);
if( _spoil_scenario==1 )
spoil_vector_by_posinf(y);
if( _spoil_scenario==2 )
spoil_vector_by_neginf(y);
if( _spoil_scenario==3 )
spoil_vector_by_deleting_element(y);
double t = -1;
if( _spoil_scenario==4 )
t = fp_posinf;
if( _spoil_scenario==5 )
t = fp_neginf;
double a = -1;
if( _spoil_scenario==6 )
a = fp_nan;
if( _spoil_scenario==7 )
a = fp_posinf;
if( _spoil_scenario==8 )
a = fp_neginf;
double b = +1;
if( _spoil_scenario==9 )
b = fp_nan;
if( _spoil_scenario==10 )
b = fp_posinf;
if( _spoil_scenario==11 )
b = fp_neginf;
double v;
v = polynomialcalccheb1(a, b, y, 3, t);
_TestResult = _TestResult && doc_test_real(v, 2.0, 0.00005);
_TestResult = _TestResult && (_spoil_scenario==-1);
}
catch(ap_error)
{ _TestResult = _TestResult && (_spoil_scenario!=-1); }
}
if( !_TestResult)
{
printf("%-32s FAILED\n", "polint_t_6");
fflush(stdout);
}
_TotalResult = _TotalResult && _TestResult;
//
// TEST polint_t_7
// Polynomial interpolation, full list of parameters.
//
_TestResult = true;
for(_spoil_scenario=-1; _spoil_scenario<12; _spoil_scenario++)
{
try
{
real_1d_array y = "[0,0,2]";
if( _spoil_scenario==0 )
spoil_vector_by_nan(y);
if( _spoil_scenario==1 )
spoil_vector_by_posinf(y);
if( _spoil_scenario==2 )
spoil_vector_by_neginf(y);
if( _spoil_scenario==3 )
spoil_vector_by_deleting_element(y);
double t = -2;
if( _spoil_scenario==4 )
t = fp_posinf;
if( _spoil_scenario==5 )
t = fp_neginf;
double a = -1;
if( _spoil_scenario==6 )
a = fp_nan;
if( _spoil_scenario==7 )
a = fp_posinf;
if( _spoil_scenario==8 )
a = fp_neginf;
double b = +1;
if( _spoil_scenario==9 )
b = fp_nan;
if( _spoil_scenario==10 )
b = fp_posinf;
if( _spoil_scenario==11 )
b = fp_neginf;
double v;
v = polynomialcalccheb2(a, b, y, 3, t);
_TestResult = _TestResult && doc_test_real(v, 6.0, 0.00005);
_TestResult = _TestResult && (_spoil_scenario==-1);
}
catch(ap_error)
{ _TestResult = _TestResult && (_spoil_scenario!=-1); }
}
if( !_TestResult)
{
printf("%-32s FAILED\n", "polint_t_7");
fflush(stdout);
}
_TotalResult = _TotalResult && _TestResult;
//
// TEST polint_t_8
// Polynomial interpolation: y=x^2-x, equidistant grid, barycentric form
//
_TestResult = true;
for(_spoil_scenario=-1; _spoil_scenario<5; _spoil_scenario++)
{
try
{
real_1d_array y = "[0,0,2]";
if( _spoil_scenario==0 )
spoil_vector_by_nan(y);
if( _spoil_scenario==1 )
spoil_vector_by_posinf(y);
if( _spoil_scenario==2 )
spoil_vector_by_neginf(y);
double t = -1;
if( _spoil_scenario==3 )
t = fp_posinf;
if( _spoil_scenario==4 )
t = fp_neginf;
barycentricinterpolant p;
double v;
polynomialbuildeqdist(0.0, 2.0, y, p);
v = barycentriccalc(p, t);
_TestResult = _TestResult && doc_test_real(v, 2.0, 0.00005);
_TestResult = _TestResult && (_spoil_scenario==-1);
}
catch(ap_error)
{ _TestResult = _TestResult && (_spoil_scenario!=-1); }
}
if( !_TestResult)
{
printf("%-32s FAILED\n", "polint_t_8");
fflush(stdout);
}
_TotalResult = _TotalResult && _TestResult;
//
// TEST polint_t_9
// Polynomial interpolation: y=x^2-x, Chebyshev grid (first kind), barycentric form
//
_TestResult = true;
for(_spoil_scenario=-1; _spoil_scenario<11; _spoil_scenario++)
{
try
{
real_1d_array y = "[-0.116025,0.000000,1.616025]";
if( _spoil_scenario==0 )
spoil_vector_by_nan(y);
if( _spoil_scenario==1 )
spoil_vector_by_posinf(y);
if( _spoil_scenario==2 )
spoil_vector_by_neginf(y);
double t = -1;
if( _spoil_scenario==3 )
t = fp_posinf;
if( _spoil_scenario==4 )
t = fp_neginf;
double a = -1;
if( _spoil_scenario==5 )
a = fp_nan;
if( _spoil_scenario==6 )
a = fp_posinf;
if( _spoil_scenario==7 )
a = fp_neginf;
double b = +1;
if( _spoil_scenario==8 )
b = fp_nan;
if( _spoil_scenario==9 )
b = fp_posinf;
if( _spoil_scenario==10 )
b = fp_neginf;
barycentricinterpolant p;
double v;
polynomialbuildcheb1(a, b, y, p);
v = barycentriccalc(p, t);
_TestResult = _TestResult && doc_test_real(v, 2.0, 0.00005);
_TestResult = _TestResult && (_spoil_scenario==-1);
}
catch(ap_error)
{ _TestResult = _TestResult && (_spoil_scenario!=-1); }
}
if( !_TestResult)
{
printf("%-32s FAILED\n", "polint_t_9");
fflush(stdout);
}
_TotalResult = _TotalResult && _TestResult;
//
// TEST polint_t_10
// Polynomial interpolation: y=x^2-x, Chebyshev grid (second kind), barycentric form
//
_TestResult = true;
for(_spoil_scenario=-1; _spoil_scenario<11; _spoil_scenario++)
{
try
{
real_1d_array y = "[0,0,2]";
if( _spoil_scenario==0 )
spoil_vector_by_nan(y);
if( _spoil_scenario==1 )
spoil_vector_by_posinf(y);
if( _spoil_scenario==2 )
spoil_vector_by_neginf(y);
double t = -2;
if( _spoil_scenario==3 )
t = fp_posinf;
if( _spoil_scenario==4 )
t = fp_neginf;
double a = -1;
if( _spoil_scenario==5 )
a = fp_nan;
if( _spoil_scenario==6 )
a = fp_posinf;
if( _spoil_scenario==7 )
a = fp_neginf;
double b = +1;
if( _spoil_scenario==8 )
b = fp_nan;
if( _spoil_scenario==9 )
b = fp_posinf;
if( _spoil_scenario==10 )
b = fp_neginf;
barycentricinterpolant p;
double v;
polynomialbuildcheb2(a, b, y, p);
v = barycentriccalc(p, t);
_TestResult = _TestResult && doc_test_real(v, 6.0, 0.00005);
_TestResult = _TestResult && (_spoil_scenario==-1);
}
catch(ap_error)
{ _TestResult = _TestResult && (_spoil_scenario!=-1); }
}
if( !_TestResult)
{
printf("%-32s FAILED\n", "polint_t_10");
fflush(stdout);
}
_TotalResult = _TotalResult && _TestResult;
//
// TEST polint_t_11
// Polynomial interpolation: y=x^2-x, equidistant grid
//
_TestResult = true;
for(_spoil_scenario=-1; _spoil_scenario<5; _spoil_scenario++)
{
try
{
real_1d_array y = "[0,0,2]";
if( _spoil_scenario==0 )
spoil_vector_by_nan(y);
if( _spoil_scenario==1 )
spoil_vector_by_posinf(y);
if( _spoil_scenario==2 )
spoil_vector_by_neginf(y);
double t = -1;
if( _spoil_scenario==3 )
t = fp_posinf;
if( _spoil_scenario==4 )
t = fp_neginf;
double v;
v = polynomialcalceqdist(0.0, 2.0, y, t);
_TestResult = _TestResult && doc_test_real(v, 2.0, 0.00005);
_TestResult = _TestResult && (_spoil_scenario==-1);
}
catch(ap_error)
{ _TestResult = _TestResult && (_spoil_scenario!=-1); }
}
if( !_TestResult)
{
printf("%-32s FAILED\n", "polint_t_11");
fflush(stdout);
}
_TotalResult = _TotalResult && _TestResult;
//
// TEST polint_t_12
// Polynomial interpolation: y=x^2-x, Chebyshev grid (first kind)
//
printf("100/165\n");
_TestResult = true;
for(_spoil_scenario=-1; _spoil_scenario<11; _spoil_scenario++)
{
try
{
real_1d_array y = "[-0.116025,0.000000,1.616025]";
if( _spoil_scenario==0 )
spoil_vector_by_nan(y);
if( _spoil_scenario==1 )
spoil_vector_by_posinf(y);
if( _spoil_scenario==2 )
spoil_vector_by_neginf(y);
double t = -1;
if( _spoil_scenario==3 )
t = fp_posinf;
if( _spoil_scenario==4 )
t = fp_neginf;
double a = -1;
if( _spoil_scenario==5 )
a = fp_nan;
if( _spoil_scenario==6 )
a = fp_posinf;
if( _spoil_scenario==7 )
a = fp_neginf;
double b = +1;
if( _spoil_scenario==8 )
b = fp_nan;
if( _spoil_scenario==9 )
b = fp_posinf;
if( _spoil_scenario==10 )
b = fp_neginf;
double v;
v = polynomialcalccheb1(a, b, y, t);
_TestResult = _TestResult && doc_test_real(v, 2.0, 0.00005);
_TestResult = _TestResult && (_spoil_scenario==-1);
}
catch(ap_error)
{ _TestResult = _TestResult && (_spoil_scenario!=-1); }
}
if( !_TestResult)
{
printf("%-32s FAILED\n", "polint_t_12");
fflush(stdout);
}
_TotalResult = _TotalResult && _TestResult;
//
// TEST polint_t_13
// Polynomial interpolation: y=x^2-x, Chebyshev grid (second kind)
//
_TestResult = true;
for(_spoil_scenario=-1; _spoil_scenario<11; _spoil_scenario++)
{
try
{
real_1d_array y = "[0,0,2]";
if( _spoil_scenario==0 )
spoil_vector_by_nan(y);
if( _spoil_scenario==1 )
spoil_vector_by_posinf(y);
if( _spoil_scenario==2 )
spoil_vector_by_neginf(y);
double t = -2;
if( _spoil_scenario==3 )
t = fp_posinf;
if( _spoil_scenario==4 )
t = fp_neginf;
double a = -1;
if( _spoil_scenario==5 )
a = fp_nan;
if( _spoil_scenario==6 )
a = fp_posinf;
if( _spoil_scenario==7 )
a = fp_neginf;
double b = +1;
if( _spoil_scenario==8 )
b = fp_nan;
if( _spoil_scenario==9 )
b = fp_posinf;
if( _spoil_scenario==10 )
b = fp_neginf;
double v;
v = polynomialcalccheb2(a, b, y, t);
_TestResult = _TestResult && doc_test_real(v, 6.0, 0.00005);
_TestResult = _TestResult && (_spoil_scenario==-1);
}
catch(ap_error)
{ _TestResult = _TestResult && (_spoil_scenario!=-1); }
}
if( !_TestResult)
{
printf("%-32s FAILED\n", "polint_t_13");
fflush(stdout);
}
_TotalResult = _TotalResult && _TestResult;
//
// TEST spline1d_d_linear
// Piecewise linear spline interpolation
//
_TestResult = true;
for(_spoil_scenario=-1; _spoil_scenario<12; _spoil_scenario++)
{
try
{
//
// We use piecewise linear spline to interpolate f(x)=x^2 sampled
// at 5 equidistant nodes on [-1,+1].
//
real_1d_array x = "[-1.0,-0.5,0.0,+0.5,+1.0]";
if( _spoil_scenario==0 )
spoil_vector_by_nan(x);
if( _spoil_scenario==1 )
spoil_vector_by_posinf(x);
if( _spoil_scenario==2 )
spoil_vector_by_neginf(x);
if( _spoil_scenario==3 )
spoil_vector_by_adding_element(x);
if( _spoil_scenario==4 )
spoil_vector_by_deleting_element(x);
real_1d_array y = "[+1.0,0.25,0.0,0.25,+1.0]";
if( _spoil_scenario==5 )
spoil_vector_by_nan(y);
if( _spoil_scenario==6 )
spoil_vector_by_posinf(y);
if( _spoil_scenario==7 )
spoil_vector_by_neginf(y);
if( _spoil_scenario==8 )
spoil_vector_by_adding_element(y);
if( _spoil_scenario==9 )
spoil_vector_by_deleting_element(y);
double t = 0.25;
if( _spoil_scenario==10 )
t = fp_posinf;
if( _spoil_scenario==11 )
t = fp_neginf;
double v;
spline1dinterpolant s;
// build spline
spline1dbuildlinear(x, y, s);
// calculate S(0.25) - it is quite different from 0.25^2=0.0625
v = spline1dcalc(s, t);
_TestResult = _TestResult && doc_test_real(v, 0.125, 0.00005);
_TestResult = _TestResult && (_spoil_scenario==-1);
}
catch(ap_error)
{ _TestResult = _TestResult && (_spoil_scenario!=-1); }
}
if( !_TestResult)
{
printf("%-32s FAILED\n", "spline1d_d_linear");
fflush(stdout);
}
_TotalResult = _TotalResult && _TestResult;
//
// TEST spline1d_d_cubic
// Cubic spline interpolation
//
_TestResult = true;
for(_spoil_scenario=-1; _spoil_scenario<10; _spoil_scenario++)
{
try
{
//
// We use cubic spline to interpolate f(x)=x^2 sampled
// at 5 equidistant nodes on [-1,+1].
//
// First, we use default boundary conditions ("parabolically terminated
// spline") because cubic spline built with such boundary conditions
// will exactly reproduce any quadratic f(x).
//
// Then we try to use natural boundary conditions
// d2S(-1)/dx^2 = 0.0
// d2S(+1)/dx^2 = 0.0
// and see that such spline interpolated f(x) with small error.
//
real_1d_array x = "[-1.0,-0.5,0.0,+0.5,+1.0]";
if( _spoil_scenario==0 )
spoil_vector_by_nan(x);
if( _spoil_scenario==1 )
spoil_vector_by_posinf(x);
if( _spoil_scenario==2 )
spoil_vector_by_neginf(x);
if( _spoil_scenario==3 )
spoil_vector_by_deleting_element(x);
real_1d_array y = "[+1.0,0.25,0.0,0.25,+1.0]";
if( _spoil_scenario==4 )
spoil_vector_by_nan(y);
if( _spoil_scenario==5 )
spoil_vector_by_posinf(y);
if( _spoil_scenario==6 )
spoil_vector_by_neginf(y);
if( _spoil_scenario==7 )
spoil_vector_by_deleting_element(y);
double t = 0.25;
if( _spoil_scenario==8 )
t = fp_posinf;
if( _spoil_scenario==9 )
t = fp_neginf;
double v;
spline1dinterpolant s;
ae_int_t natural_bound_type = 2;
//
// Test exact boundary conditions: build S(x), calculare S(0.25)
// (almost same as original function)
//
spline1dbuildcubic(x, y, s);
v = spline1dcalc(s, t);
_TestResult = _TestResult && doc_test_real(v, 0.0625, 0.00001);
//
// Test natural boundary conditions: build S(x), calculare S(0.25)
// (small interpolation error)
//
spline1dbuildcubic(x, y, 5, natural_bound_type, 0.0, natural_bound_type, 0.0, s);
v = spline1dcalc(s, t);
_TestResult = _TestResult && doc_test_real(v, 0.0580, 0.0001);
_TestResult = _TestResult && (_spoil_scenario==-1);
}
catch(ap_error)
{ _TestResult = _TestResult && (_spoil_scenario!=-1); }
}
if( !_TestResult)
{
printf("%-32s FAILED\n", "spline1d_d_cubic");
fflush(stdout);
}
_TotalResult = _TotalResult && _TestResult;
//
// TEST spline1d_d_monotone
// Monotone interpolation
//
_TestResult = true;
for(_spoil_scenario=-1; _spoil_scenario<10; _spoil_scenario++)
{
try
{
//
// Spline built witn spline1dbuildcubic() can be non-monotone even when
// Y-values form monotone sequence. Say, for x=[0,1,2] and y=[0,1,1]
// cubic spline will monotonically grow until x=1.5 and then start
// decreasing.
//
// That's why ALGLIB provides special spline construction function
// which builds spline which preserves monotonicity of the original
// dataset.
//
// NOTE: in case original dataset is non-monotonic, ALGLIB splits it
// into monotone subsequences and builds piecewise monotonic spline.
//
real_1d_array x = "[0,1,2]";
if( _spoil_scenario==0 )
spoil_vector_by_nan(x);
if( _spoil_scenario==1 )
spoil_vector_by_posinf(x);
if( _spoil_scenario==2 )
spoil_vector_by_neginf(x);
if( _spoil_scenario==3 )
spoil_vector_by_adding_element(x);
if( _spoil_scenario==4 )
spoil_vector_by_deleting_element(x);
real_1d_array y = "[0,1,1]";
if( _spoil_scenario==5 )
spoil_vector_by_nan(y);
if( _spoil_scenario==6 )
spoil_vector_by_posinf(y);
if( _spoil_scenario==7 )
spoil_vector_by_neginf(y);
if( _spoil_scenario==8 )
spoil_vector_by_adding_element(y);
if( _spoil_scenario==9 )
spoil_vector_by_deleting_element(y);
spline1dinterpolant s;
// build spline
spline1dbuildmonotone(x, y, s);
// calculate S at x = [-0.5, 0.0, 0.5, 1.0, 1.5, 2.0]
// you may see that spline is really monotonic
double v;
v = spline1dcalc(s, -0.5);
_TestResult = _TestResult && doc_test_real(v, 0.0000, 0.00005);
v = spline1dcalc(s, 0.0);
_TestResult = _TestResult && doc_test_real(v, 0.0000, 0.00005);
v = spline1dcalc(s, +0.5);
_TestResult = _TestResult && doc_test_real(v, 0.5000, 0.00005);
v = spline1dcalc(s, 1.0);
_TestResult = _TestResult && doc_test_real(v, 1.0000, 0.00005);
v = spline1dcalc(s, 1.5);
_TestResult = _TestResult && doc_test_real(v, 1.0000, 0.00005);
v = spline1dcalc(s, 2.0);
_TestResult = _TestResult && doc_test_real(v, 1.0000, 0.00005);
_TestResult = _TestResult && (_spoil_scenario==-1);
}
catch(ap_error)
{ _TestResult = _TestResult && (_spoil_scenario!=-1); }
}
if( !_TestResult)
{
printf("%-32s FAILED\n", "spline1d_d_monotone");
fflush(stdout);
}
_TotalResult = _TotalResult && _TestResult;
//
// TEST spline1d_d_griddiff
// Differentiation on the grid using cubic splines
//
_TestResult = true;
for(_spoil_scenario=-1; _spoil_scenario<10; _spoil_scenario++)
{
try
{
//
// We use cubic spline to do grid differentiation, i.e. having
// values of f(x)=x^2 sampled at 5 equidistant nodes on [-1,+1]
// we calculate derivatives of cubic spline at nodes WITHOUT
// CONSTRUCTION OF SPLINE OBJECT.
//
// There are efficient functions spline1dgriddiffcubic() and
// spline1dgriddiff2cubic() for such calculations.
//
// We use default boundary conditions ("parabolically terminated
// spline") because cubic spline built with such boundary conditions
// will exactly reproduce any quadratic f(x).
//
// Actually, we could use natural conditions, but we feel that
// spline which exactly reproduces f() will show us more
// understandable results.
//
real_1d_array x = "[-1.0,-0.5,0.0,+0.5,+1.0]";
if( _spoil_scenario==0 )
spoil_vector_by_nan(x);
if( _spoil_scenario==1 )
spoil_vector_by_posinf(x);
if( _spoil_scenario==2 )
spoil_vector_by_neginf(x);
if( _spoil_scenario==3 )
spoil_vector_by_adding_element(x);
if( _spoil_scenario==4 )
spoil_vector_by_deleting_element(x);
real_1d_array y = "[+1.0,0.25,0.0,0.25,+1.0]";
if( _spoil_scenario==5 )
spoil_vector_by_nan(y);
if( _spoil_scenario==6 )
spoil_vector_by_posinf(y);
if( _spoil_scenario==7 )
spoil_vector_by_neginf(y);
if( _spoil_scenario==8 )
spoil_vector_by_adding_element(y);
if( _spoil_scenario==9 )
spoil_vector_by_deleting_element(y);
real_1d_array d1;
real_1d_array d2;
//
// We calculate first derivatives: they must be equal to 2*x
//
spline1dgriddiffcubic(x, y, d1);
_TestResult = _TestResult && doc_test_real_vector(d1, "[-2.0, -1.0, 0.0, +1.0, +2.0]", 0.0001);
//
// Now test griddiff2, which returns first AND second derivatives.
// First derivative is 2*x, second is equal to 2.0
//
spline1dgriddiff2cubic(x, y, d1, d2);
_TestResult = _TestResult && doc_test_real_vector(d1, "[-2.0, -1.0, 0.0, +1.0, +2.0]", 0.0001);
_TestResult = _TestResult && doc_test_real_vector(d2, "[ 2.0, 2.0, 2.0, 2.0, 2.0]", 0.0001);
_TestResult = _TestResult && (_spoil_scenario==-1);
}
catch(ap_error)
{ _TestResult = _TestResult && (_spoil_scenario!=-1); }
}
if( !_TestResult)
{
printf("%-32s FAILED\n", "spline1d_d_griddiff");
fflush(stdout);
}
_TotalResult = _TotalResult && _TestResult;
//
// TEST spline1d_d_convdiff
// Resampling using cubic splines
//
_TestResult = true;
for(_spoil_scenario=-1; _spoil_scenario<11; _spoil_scenario++)
{
try
{
//
// We use cubic spline to do resampling, i.e. having
// values of f(x)=x^2 sampled at 5 equidistant nodes on [-1,+1]
// we calculate values/derivatives of cubic spline on
// another grid (equidistant with 9 nodes on [-1,+1])
// WITHOUT CONSTRUCTION OF SPLINE OBJECT.
//
// There are efficient functions spline1dconvcubic(),
// spline1dconvdiffcubic() and spline1dconvdiff2cubic()
// for such calculations.
//
// We use default boundary conditions ("parabolically terminated
// spline") because cubic spline built with such boundary conditions
// will exactly reproduce any quadratic f(x).
//
// Actually, we could use natural conditions, but we feel that
// spline which exactly reproduces f() will show us more
// understandable results.
//
real_1d_array x_old = "[-1.0,-0.5,0.0,+0.5,+1.0]";
if( _spoil_scenario==0 )
spoil_vector_by_nan(x_old);
if( _spoil_scenario==1 )
spoil_vector_by_posinf(x_old);
if( _spoil_scenario==2 )
spoil_vector_by_neginf(x_old);
if( _spoil_scenario==3 )
spoil_vector_by_deleting_element(x_old);
real_1d_array y_old = "[+1.0,0.25,0.0,0.25,+1.0]";
if( _spoil_scenario==4 )
spoil_vector_by_nan(y_old);
if( _spoil_scenario==5 )
spoil_vector_by_posinf(y_old);
if( _spoil_scenario==6 )
spoil_vector_by_neginf(y_old);
if( _spoil_scenario==7 )
spoil_vector_by_deleting_element(y_old);
real_1d_array x_new = "[-1.00,-0.75,-0.50,-0.25,0.00,+0.25,+0.50,+0.75,+1.00]";
if( _spoil_scenario==8 )
spoil_vector_by_nan(x_new);
if( _spoil_scenario==9 )
spoil_vector_by_posinf(x_new);
if( _spoil_scenario==10 )
spoil_vector_by_neginf(x_new);
real_1d_array y_new;
real_1d_array d1_new;
real_1d_array d2_new;
//
// First, conversion without differentiation.
//
//
spline1dconvcubic(x_old, y_old, x_new, y_new);
_TestResult = _TestResult && doc_test_real_vector(y_new, "[1.0000, 0.5625, 0.2500, 0.0625, 0.0000, 0.0625, 0.2500, 0.5625, 1.0000]", 0.0001);
//
// Then, conversion with differentiation (first derivatives only)
//
//
spline1dconvdiffcubic(x_old, y_old, x_new, y_new, d1_new);
_TestResult = _TestResult && doc_test_real_vector(y_new, "[1.0000, 0.5625, 0.2500, 0.0625, 0.0000, 0.0625, 0.2500, 0.5625, 1.0000]", 0.0001);
_TestResult = _TestResult && doc_test_real_vector(d1_new, "[-2.0, -1.5, -1.0, -0.5, 0.0, 0.5, 1.0, 1.5, 2.0]", 0.0001);
//
// Finally, conversion with first and second derivatives
//
//
spline1dconvdiff2cubic(x_old, y_old, x_new, y_new, d1_new, d2_new);
_TestResult = _TestResult && doc_test_real_vector(y_new, "[1.0000, 0.5625, 0.2500, 0.0625, 0.0000, 0.0625, 0.2500, 0.5625, 1.0000]", 0.0001);
_TestResult = _TestResult && doc_test_real_vector(d1_new, "[-2.0, -1.5, -1.0, -0.5, 0.0, 0.5, 1.0, 1.5, 2.0]", 0.0001);
_TestResult = _TestResult && doc_test_real_vector(d2_new, "[2.0, 2.0, 2.0, 2.0, 2.0, 2.0, 2.0, 2.0, 2.0]", 0.0001);
_TestResult = _TestResult && (_spoil_scenario==-1);
}
catch(ap_error)
{ _TestResult = _TestResult && (_spoil_scenario!=-1); }
}
if( !_TestResult)
{
printf("%-32s FAILED\n", "spline1d_d_convdiff");
fflush(stdout);
}
_TotalResult = _TotalResult && _TestResult;
//
// TEST lsfit_d_nlf
// Nonlinear fitting using function value only
//
_TestResult = true;
for(_spoil_scenario=-1; _spoil_scenario<24; _spoil_scenario++)
{
try
{
//
// In this example we demonstrate exponential fitting by
//
// f(x) = exp(-c*x^2)
//
// using numerical differentiation.
//
// IMPORTANT: the LSFIT optimizer supports parallel model evaluation and
// parallel numerical differentiation ('callback parallelism').
// This feature, which is present in commercial ALGLIB editions
// greatly accelerates fits with large datasets and/or
// expensive target functions.
//
// Callback parallelism is usually beneficial when a single
// pass over the entire dataset requires more than several
// milliseconds. This particular example, of course, is not
// suited for callback parallelism.
//
// See ALGLIB Reference Manual, 'Working with commercial version'
// section, and comments on lsfitfit() function for more
// information.
//
real_2d_array x = "[[-1],[-0.8],[-0.6],[-0.4],[-0.2],[0],[0.2],[0.4],[0.6],[0.8],[1.0]]";
if( _spoil_scenario==0 )
spoil_matrix_by_nan(x);
if( _spoil_scenario==1 )
spoil_matrix_by_posinf(x);
if( _spoil_scenario==2 )
spoil_matrix_by_neginf(x);
if( _spoil_scenario==3 )
spoil_matrix_by_deleting_row(x);
if( _spoil_scenario==4 )
spoil_matrix_by_deleting_col(x);
real_1d_array y = "[0.223130, 0.382893, 0.582748, 0.786628, 0.941765, 1.000000, 0.941765, 0.786628, 0.582748, 0.382893, 0.223130]";
if( _spoil_scenario==5 )
spoil_vector_by_nan(y);
if( _spoil_scenario==6 )
spoil_vector_by_posinf(y);
if( _spoil_scenario==7 )
spoil_vector_by_neginf(y);
if( _spoil_scenario==8 )
spoil_vector_by_adding_element(y);
if( _spoil_scenario==9 )
spoil_vector_by_deleting_element(y);
real_1d_array c = "[0.3]";
if( _spoil_scenario==10 )
spoil_vector_by_nan(c);
if( _spoil_scenario==11 )
spoil_vector_by_posinf(c);
if( _spoil_scenario==12 )
spoil_vector_by_neginf(c);
double epsx = 0.000001;
if( _spoil_scenario==13 )
epsx = fp_nan;
if( _spoil_scenario==14 )
epsx = fp_posinf;
if( _spoil_scenario==15 )
epsx = fp_neginf;
ae_int_t maxits = 0;
lsfitstate state;
lsfitreport rep;
double diffstep = 0.0001;
if( _spoil_scenario==16 )
diffstep = fp_nan;
if( _spoil_scenario==17 )
diffstep = fp_posinf;
if( _spoil_scenario==18 )
diffstep = fp_neginf;
//
// Fitting without weights
//
lsfitcreatef(x, y, c, diffstep, state);
lsfitsetcond(state, epsx, maxits);
alglib::lsfitfit(state, function_cx_1_func);
lsfitresults(state, c, rep);
_TestResult = _TestResult && doc_test_int(rep.terminationtype, 2);
_TestResult = _TestResult && doc_test_real_vector(c, "[1.5]", 0.05);
//
// Fitting with weights
// (you can change weights and see how it changes result)
//
real_1d_array w = "[1,1,1,1,1,1,1,1,1,1,1]";
if( _spoil_scenario==19 )
spoil_vector_by_nan(w);
if( _spoil_scenario==20 )
spoil_vector_by_posinf(w);
if( _spoil_scenario==21 )
spoil_vector_by_neginf(w);
if( _spoil_scenario==22 )
spoil_vector_by_adding_element(w);
if( _spoil_scenario==23 )
spoil_vector_by_deleting_element(w);
lsfitcreatewf(x, y, w, c, diffstep, state);
lsfitsetcond(state, epsx, maxits);
alglib::lsfitfit(state, function_cx_1_func);
lsfitresults(state, c, rep);
_TestResult = _TestResult && doc_test_int(rep.terminationtype, 2);
_TestResult = _TestResult && doc_test_real_vector(c, "[1.5]", 0.05);
_TestResult = _TestResult && (_spoil_scenario==-1);
}
catch(ap_error)
{ _TestResult = _TestResult && (_spoil_scenario!=-1); }
}
if( !_TestResult)
{
printf("%-32s FAILED\n", "lsfit_d_nlf");
fflush(stdout);
}
_TotalResult = _TotalResult && _TestResult;
//
// TEST lsfit_d_nlfg
// Nonlinear fitting using gradient
//
_TestResult = true;
for(_spoil_scenario=-1; _spoil_scenario<21; _spoil_scenario++)
{
try
{
//
// In this example we demonstrate exponential fitting by
//
// f(x) = exp(-c*x^2)
//
// using function value and gradient (with respect to c).
//
// IMPORTANT: the LSFIT optimizer supports parallel model evaluation and
// parallel numerical differentiation ('callback parallelism').
// This feature, which is present in commercial ALGLIB editions
// greatly accelerates fits with large datasets and/or
// expensive target functions.
//
// Callback parallelism is usually beneficial when a single
// pass over the entire dataset requires more than several
// milliseconds. This particular example, of course, is not
// suited for callback parallelism.
//
// See ALGLIB Reference Manual, 'Working with commercial version'
// section, and comments on lsfitfit() function for more
// information.
//
real_2d_array x = "[[-1],[-0.8],[-0.6],[-0.4],[-0.2],[0],[0.2],[0.4],[0.6],[0.8],[1.0]]";
if( _spoil_scenario==0 )
spoil_matrix_by_nan(x);
if( _spoil_scenario==1 )
spoil_matrix_by_posinf(x);
if( _spoil_scenario==2 )
spoil_matrix_by_neginf(x);
if( _spoil_scenario==3 )
spoil_matrix_by_deleting_row(x);
if( _spoil_scenario==4 )
spoil_matrix_by_deleting_col(x);
real_1d_array y = "[0.223130, 0.382893, 0.582748, 0.786628, 0.941765, 1.000000, 0.941765, 0.786628, 0.582748, 0.382893, 0.223130]";
if( _spoil_scenario==5 )
spoil_vector_by_nan(y);
if( _spoil_scenario==6 )
spoil_vector_by_posinf(y);
if( _spoil_scenario==7 )
spoil_vector_by_neginf(y);
if( _spoil_scenario==8 )
spoil_vector_by_adding_element(y);
if( _spoil_scenario==9 )
spoil_vector_by_deleting_element(y);
real_1d_array c = "[0.3]";
if( _spoil_scenario==10 )
spoil_vector_by_nan(c);
if( _spoil_scenario==11 )
spoil_vector_by_posinf(c);
if( _spoil_scenario==12 )
spoil_vector_by_neginf(c);
double epsx = 0.000001;
if( _spoil_scenario==13 )
epsx = fp_nan;
if( _spoil_scenario==14 )
epsx = fp_posinf;
if( _spoil_scenario==15 )
epsx = fp_neginf;
ae_int_t maxits = 0;
lsfitstate state;
lsfitreport rep;
//
// Fitting without weights
//
lsfitcreatefg(x, y, c, state);
lsfitsetcond(state, epsx, maxits);
alglib::lsfitfit(state, function_cx_1_func, function_cx_1_grad);
lsfitresults(state, c, rep);
_TestResult = _TestResult && doc_test_int(rep.terminationtype, 2);
_TestResult = _TestResult && doc_test_real_vector(c, "[1.5]", 0.05);
//
// Fitting with weights
// (you can change weights and see how it changes result)
//
real_1d_array w = "[1,1,1,1,1,1,1,1,1,1,1]";
if( _spoil_scenario==16 )
spoil_vector_by_nan(w);
if( _spoil_scenario==17 )
spoil_vector_by_posinf(w);
if( _spoil_scenario==18 )
spoil_vector_by_neginf(w);
if( _spoil_scenario==19 )
spoil_vector_by_adding_element(w);
if( _spoil_scenario==20 )
spoil_vector_by_deleting_element(w);
lsfitcreatewfg(x, y, w, c, state);
lsfitsetcond(state, epsx, maxits);
alglib::lsfitfit(state, function_cx_1_func, function_cx_1_grad);
lsfitresults(state, c, rep);
_TestResult = _TestResult && doc_test_int(rep.terminationtype, 2);
_TestResult = _TestResult && doc_test_real_vector(c, "[1.5]", 0.05);
_TestResult = _TestResult && (_spoil_scenario==-1);
}
catch(ap_error)
{ _TestResult = _TestResult && (_spoil_scenario!=-1); }
}
if( !_TestResult)
{
printf("%-32s FAILED\n", "lsfit_d_nlfg");
fflush(stdout);
}
_TotalResult = _TotalResult && _TestResult;
//
// TEST lsfit_d_nlfb
// Bound contstrained nonlinear fitting using function value only
//
_TestResult = true;
for(_spoil_scenario=-1; _spoil_scenario<23; _spoil_scenario++)
{
try
{
//
// In this example we demonstrate exponential fitting by
//
// f(x) = exp(-c*x^2)
//
// subject to box constraints
//
// 0.0 <= c <= 1.0
//
// using function value only. An unconstrained solution is c=1.5, but because of
// constraints we should get c=1.0 (at the boundary).
//
// IMPORTANT: the LSFIT optimizer supports parallel model evaluation and
// parallel numerical differentiation ('callback parallelism').
// This feature, which is present in commercial ALGLIB editions
// greatly accelerates fits with large datasets and/or
// expensive target functions.
//
// Callback parallelism is usually beneficial when a single
// pass over the entire dataset requires more than several
// milliseconds. This particular example, of course, is not
// suited for callback parallelism.
//
// See ALGLIB Reference Manual, 'Working with commercial version'
// section, and comments on lsfitfit() function for more
// information.
//
real_2d_array x = "[[-1],[-0.8],[-0.6],[-0.4],[-0.2],[0],[0.2],[0.4],[0.6],[0.8],[1.0]]";
if( _spoil_scenario==0 )
spoil_matrix_by_nan(x);
if( _spoil_scenario==1 )
spoil_matrix_by_posinf(x);
if( _spoil_scenario==2 )
spoil_matrix_by_neginf(x);
if( _spoil_scenario==3 )
spoil_matrix_by_deleting_row(x);
if( _spoil_scenario==4 )
spoil_matrix_by_deleting_col(x);
real_1d_array y = "[0.223130, 0.382893, 0.582748, 0.786628, 0.941765, 1.000000, 0.941765, 0.786628, 0.582748, 0.382893, 0.223130]";
if( _spoil_scenario==5 )
spoil_vector_by_nan(y);
if( _spoil_scenario==6 )
spoil_vector_by_posinf(y);
if( _spoil_scenario==7 )
spoil_vector_by_neginf(y);
if( _spoil_scenario==8 )
spoil_vector_by_adding_element(y);
if( _spoil_scenario==9 )
spoil_vector_by_deleting_element(y);
real_1d_array c = "[0.3]";
if( _spoil_scenario==10 )
spoil_vector_by_nan(c);
if( _spoil_scenario==11 )
spoil_vector_by_posinf(c);
if( _spoil_scenario==12 )
spoil_vector_by_neginf(c);
real_1d_array bndl = "[0.0]";
if( _spoil_scenario==13 )
spoil_vector_by_nan(bndl);
if( _spoil_scenario==14 )
spoil_vector_by_deleting_element(bndl);
real_1d_array bndu = "[1.0]";
if( _spoil_scenario==15 )
spoil_vector_by_nan(bndu);
if( _spoil_scenario==16 )
spoil_vector_by_deleting_element(bndu);
double epsx = 0.000001;
if( _spoil_scenario==17 )
epsx = fp_nan;
if( _spoil_scenario==18 )
epsx = fp_posinf;
if( _spoil_scenario==19 )
epsx = fp_neginf;
ae_int_t maxits = 0;
lsfitstate state;
lsfitreport rep;
double diffstep = 0.0001;
if( _spoil_scenario==20 )
diffstep = fp_nan;
if( _spoil_scenario==21 )
diffstep = fp_posinf;
if( _spoil_scenario==22 )
diffstep = fp_neginf;
lsfitcreatef(x, y, c, diffstep, state);
lsfitsetbc(state, bndl, bndu);
lsfitsetcond(state, epsx, maxits);
alglib::lsfitfit(state, function_cx_1_func);
lsfitresults(state, c, rep);
_TestResult = _TestResult && doc_test_real_vector(c, "[1.0]", 0.05);
_TestResult = _TestResult && (_spoil_scenario==-1);
}
catch(ap_error)
{ _TestResult = _TestResult && (_spoil_scenario!=-1); }
}
if( !_TestResult)
{
printf("%-32s FAILED\n", "lsfit_d_nlfb");
fflush(stdout);
}
_TotalResult = _TotalResult && _TestResult;
//
// TEST lsfit_d_nlscale
// Nonlinear fitting with custom scaling and bound constraints
//
_TestResult = true;
for(_spoil_scenario=-1; _spoil_scenario<27; _spoil_scenario++)
{
try
{
//
// In this example we demonstrate fitting by
//
// f(x) = c[0]*(1+c[1]*((x-1999)^c[2]-1))
//
// subject to box constraints
//
// -INF < c[0] < +INF
// -10 <= c[1] <= +10
// 0.1 <= c[2] <= 2.0
//
// The data we want to fit are time series of Japan national debt
// collected from 2000 to 2008 measured in USD (dollars, not
// millions of dollars).
//
// Our variables are:
// c[0] - debt value at initial moment (2000),
// c[1] - direction coefficient (growth or decrease),
// c[2] - curvature coefficient.
// You may see that our variables are badly scaled - first one
// is order of 10^12, and next two are somewhere about 1 in
// magnitude. Such problem is difficult to solve without some
// kind of scaling.
// That is exactly where lsfitsetscale() function can be used.
// We set scale of our variables to [1.0E12, 1, 1], which allows
// us to easily solve this problem.
//
// You can try commenting out lsfitsetscale() call - and you will
// see that algorithm will fail to converge.
//
// IMPORTANT: the LSFIT optimizer supports parallel model evaluation and
// parallel numerical differentiation ('callback parallelism').
// This feature, which is present in commercial ALGLIB editions
// greatly accelerates fits with large datasets and/or
// expensive target functions.
//
// Callback parallelism is usually beneficial when a single
// pass over the entire dataset requires more than several
// milliseconds. This particular example, of course, is not
// suited for callback parallelism.
//
// See ALGLIB Reference Manual, 'Working with commercial version'
// section, and comments on lsfitfit() function for more
// information.
//
real_2d_array x = "[[2000],[2001],[2002],[2003],[2004],[2005],[2006],[2007],[2008]]";
if( _spoil_scenario==0 )
spoil_matrix_by_nan(x);
if( _spoil_scenario==1 )
spoil_matrix_by_posinf(x);
if( _spoil_scenario==2 )
spoil_matrix_by_neginf(x);
if( _spoil_scenario==3 )
spoil_matrix_by_deleting_row(x);
if( _spoil_scenario==4 )
spoil_matrix_by_deleting_col(x);
real_1d_array y = "[4323239600000.0, 4560913100000.0, 5564091500000.0, 6743189300000.0, 7284064600000.0, 7050129600000.0, 7092221500000.0, 8483907600000.0, 8625804400000.0]";
if( _spoil_scenario==5 )
spoil_vector_by_nan(y);
if( _spoil_scenario==6 )
spoil_vector_by_posinf(y);
if( _spoil_scenario==7 )
spoil_vector_by_neginf(y);
if( _spoil_scenario==8 )
spoil_vector_by_adding_element(y);
if( _spoil_scenario==9 )
spoil_vector_by_deleting_element(y);
real_1d_array c = "[1.0e+13, 1, 1]";
if( _spoil_scenario==10 )
spoil_vector_by_nan(c);
if( _spoil_scenario==11 )
spoil_vector_by_posinf(c);
if( _spoil_scenario==12 )
spoil_vector_by_neginf(c);
double epsx = 1.0e-5;
if( _spoil_scenario==13 )
epsx = fp_nan;
if( _spoil_scenario==14 )
epsx = fp_posinf;
if( _spoil_scenario==15 )
epsx = fp_neginf;
real_1d_array bndl = "[-inf, -10, 0.1]";
if( _spoil_scenario==16 )
spoil_vector_by_nan(bndl);
if( _spoil_scenario==17 )
spoil_vector_by_deleting_element(bndl);
real_1d_array bndu = "[+inf, +10, 2.0]";
if( _spoil_scenario==18 )
spoil_vector_by_nan(bndu);
if( _spoil_scenario==19 )
spoil_vector_by_deleting_element(bndu);
real_1d_array s = "[1.0e+12, 1, 1]";
if( _spoil_scenario==20 )
spoil_vector_by_nan(s);
if( _spoil_scenario==21 )
spoil_vector_by_posinf(s);
if( _spoil_scenario==22 )
spoil_vector_by_neginf(s);
if( _spoil_scenario==23 )
spoil_vector_by_deleting_element(s);
ae_int_t maxits = 0;
lsfitstate state;
lsfitreport rep;
double diffstep = 1.0e-5;
if( _spoil_scenario==24 )
diffstep = fp_nan;
if( _spoil_scenario==25 )
diffstep = fp_posinf;
if( _spoil_scenario==26 )
diffstep = fp_neginf;
lsfitcreatef(x, y, c, diffstep, state);
lsfitsetcond(state, epsx, maxits);
lsfitsetbc(state, bndl, bndu);
lsfitsetscale(state, s);
alglib::lsfitfit(state, function_debt_func);
lsfitresults(state, c, rep);
_TestResult = _TestResult && doc_test_int(rep.terminationtype, 2);
_TestResult = _TestResult && doc_test_real_vector(c, "[4.142560e+12, 0.434240, 0.565376]", -0.005);
_TestResult = _TestResult && (_spoil_scenario==-1);
}
catch(ap_error)
{ _TestResult = _TestResult && (_spoil_scenario!=-1); }
}
if( !_TestResult)
{
printf("%-32s FAILED\n", "lsfit_d_nlscale");
fflush(stdout);
}
_TotalResult = _TotalResult && _TestResult;
//
// TEST lsfit_d_lin
// Unconstrained (general) linear least squares fitting with and without weights
//
_TestResult = true;
for(_spoil_scenario=-1; _spoil_scenario<13; _spoil_scenario++)
{
try
{
//
// In this example we demonstrate linear fitting by f(x|a) = a*exp(0.5*x).
//
// We have:
// * y - vector of experimental data
// * fmatrix - matrix of basis functions calculated at sample points
// Actually, we have only one basis function F0 = exp(0.5*x).
//
real_2d_array fmatrix = "[[0.606531],[0.670320],[0.740818],[0.818731],[0.904837],[1.000000],[1.105171],[1.221403],[1.349859],[1.491825],[1.648721]]";
if( _spoil_scenario==0 )
spoil_matrix_by_nan(fmatrix);
if( _spoil_scenario==1 )
spoil_matrix_by_posinf(fmatrix);
if( _spoil_scenario==2 )
spoil_matrix_by_neginf(fmatrix);
real_1d_array y = "[1.133719, 1.306522, 1.504604, 1.554663, 1.884638, 2.072436, 2.257285, 2.534068, 2.622017, 2.897713, 3.219371]";
if( _spoil_scenario==3 )
spoil_vector_by_nan(y);
if( _spoil_scenario==4 )
spoil_vector_by_posinf(y);
if( _spoil_scenario==5 )
spoil_vector_by_neginf(y);
if( _spoil_scenario==6 )
spoil_vector_by_adding_element(y);
if( _spoil_scenario==7 )
spoil_vector_by_deleting_element(y);
real_1d_array c;
lsfitreport rep;
//
// Linear fitting without weights
//
lsfitlinear(y, fmatrix, c, rep);
_TestResult = _TestResult && doc_test_real_vector(c, "[1.98650]", 0.00005);
//
// Linear fitting with individual weights.
// Slightly different result is returned.
//
real_1d_array w = "[1.414213, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]";
if( _spoil_scenario==8 )
spoil_vector_by_nan(w);
if( _spoil_scenario==9 )
spoil_vector_by_posinf(w);
if( _spoil_scenario==10 )
spoil_vector_by_neginf(w);
if( _spoil_scenario==11 )
spoil_vector_by_adding_element(w);
if( _spoil_scenario==12 )
spoil_vector_by_deleting_element(w);
lsfitlinearw(y, w, fmatrix, c, rep);
_TestResult = _TestResult && doc_test_real_vector(c, "[1.983354]", 0.00005);
_TestResult = _TestResult && (_spoil_scenario==-1);
}
catch(ap_error)
{ _TestResult = _TestResult && (_spoil_scenario!=-1); }
}
if( !_TestResult)
{
printf("%-32s FAILED\n", "lsfit_d_lin");
fflush(stdout);
}
_TotalResult = _TotalResult && _TestResult;
//
// TEST lsfit_d_linc
// Constrained (general) linear least squares fitting with and without weights
//
_TestResult = true;
for(_spoil_scenario=-1; _spoil_scenario<20; _spoil_scenario++)
{
try
{
//
// In this example we demonstrate linear fitting by f(x|a,b) = a*x+b
// with simple constraint f(0)=0.
//
// We have:
// * y - vector of experimental data
// * fmatrix - matrix of basis functions sampled at [0,1] with step 0.2:
// [ 1.0 0.0 ]
// [ 1.0 0.2 ]
// [ 1.0 0.4 ]
// [ 1.0 0.6 ]
// [ 1.0 0.8 ]
// [ 1.0 1.0 ]
// first column contains value of first basis function (constant term)
// second column contains second basis function (linear term)
// * cmatrix - matrix of linear constraints:
// [ 1.0 0.0 0.0 ]
// first two columns contain coefficients before basis functions,
// last column contains desired value of their sum.
// So [1,0,0] means "1*constant_term + 0*linear_term = 0"
//
real_1d_array y = "[0.072436,0.246944,0.491263,0.522300,0.714064,0.921929]";
if( _spoil_scenario==0 )
spoil_vector_by_nan(y);
if( _spoil_scenario==1 )
spoil_vector_by_posinf(y);
if( _spoil_scenario==2 )
spoil_vector_by_neginf(y);
if( _spoil_scenario==3 )
spoil_vector_by_adding_element(y);
if( _spoil_scenario==4 )
spoil_vector_by_deleting_element(y);
real_2d_array fmatrix = "[[1,0.0],[1,0.2],[1,0.4],[1,0.6],[1,0.8],[1,1.0]]";
if( _spoil_scenario==5 )
spoil_matrix_by_nan(fmatrix);
if( _spoil_scenario==6 )
spoil_matrix_by_posinf(fmatrix);
if( _spoil_scenario==7 )
spoil_matrix_by_neginf(fmatrix);
if( _spoil_scenario==8 )
spoil_matrix_by_adding_row(fmatrix);
if( _spoil_scenario==9 )
spoil_matrix_by_adding_col(fmatrix);
if( _spoil_scenario==10 )
spoil_matrix_by_deleting_row(fmatrix);
if( _spoil_scenario==11 )
spoil_matrix_by_deleting_col(fmatrix);
real_2d_array cmatrix = "[[1,0,0]]";
if( _spoil_scenario==12 )
spoil_matrix_by_nan(cmatrix);
if( _spoil_scenario==13 )
spoil_matrix_by_posinf(cmatrix);
if( _spoil_scenario==14 )
spoil_matrix_by_neginf(cmatrix);
real_1d_array c;
lsfitreport rep;
//
// Constrained fitting without weights
//
lsfitlinearc(y, fmatrix, cmatrix, c, rep);
_TestResult = _TestResult && doc_test_real_vector(c, "[0,0.932933]", 0.0005);
//
// Constrained fitting with individual weights
//
real_1d_array w = "[1, 1.414213, 1, 1, 1, 1]";
if( _spoil_scenario==15 )
spoil_vector_by_nan(w);
if( _spoil_scenario==16 )
spoil_vector_by_posinf(w);
if( _spoil_scenario==17 )
spoil_vector_by_neginf(w);
if( _spoil_scenario==18 )
spoil_vector_by_adding_element(w);
if( _spoil_scenario==19 )
spoil_vector_by_deleting_element(w);
lsfitlinearwc(y, w, fmatrix, cmatrix, c, rep);
_TestResult = _TestResult && doc_test_real_vector(c, "[0,0.938322]", 0.0005);
_TestResult = _TestResult && (_spoil_scenario==-1);
}
catch(ap_error)
{ _TestResult = _TestResult && (_spoil_scenario!=-1); }
}
if( !_TestResult)
{
printf("%-32s FAILED\n", "lsfit_d_linc");
fflush(stdout);
}
_TotalResult = _TotalResult && _TestResult;
//
// TEST lsfit_d_pol
// Unconstrained polynomial fitting
//
_TestResult = true;
for(_spoil_scenario=-1; _spoil_scenario<20; _spoil_scenario++)
{
try
{
//
// This example demonstrates polynomial fitting.
//
// Fitting is done by two (M=2) functions from polynomial basis:
// f0 = 1
// f1 = x
// Basically, it just a linear fit; more complex polynomials may be used
// (e.g. parabolas with M=3, cubic with M=4), but even such simple fit allows
// us to demonstrate polynomialfit() function in action.
//
// We have:
// * x set of abscissas
// * y experimental data
//
// Additionally we demonstrate weighted fitting, where second point has
// more weight than other ones.
//
real_1d_array x = "[0.0,0.1,0.2,0.3,0.4,0.5,0.6,0.7,0.8,0.9,1.0]";
if( _spoil_scenario==0 )
spoil_vector_by_nan(x);
if( _spoil_scenario==1 )
spoil_vector_by_posinf(x);
if( _spoil_scenario==2 )
spoil_vector_by_neginf(x);
if( _spoil_scenario==3 )
spoil_vector_by_adding_element(x);
if( _spoil_scenario==4 )
spoil_vector_by_deleting_element(x);
real_1d_array y = "[0.00,0.05,0.26,0.32,0.33,0.43,0.60,0.60,0.77,0.98,1.02]";
if( _spoil_scenario==5 )
spoil_vector_by_nan(y);
if( _spoil_scenario==6 )
spoil_vector_by_posinf(y);
if( _spoil_scenario==7 )
spoil_vector_by_neginf(y);
if( _spoil_scenario==8 )
spoil_vector_by_adding_element(y);
if( _spoil_scenario==9 )
spoil_vector_by_deleting_element(y);
ae_int_t m = 2;
double t = 2;
if( _spoil_scenario==10 )
t = fp_posinf;
if( _spoil_scenario==11 )
t = fp_neginf;
barycentricinterpolant p;
polynomialfitreport rep;
double v;
//
// Fitting without individual weights
//
// NOTE: result is returned as barycentricinterpolant structure.
// if you want to get representation in the power basis,
// you can use barycentricbar2pow() function to convert
// from barycentric to power representation (see docs for
// POLINT subpackage for more info).
//
polynomialfit(x, y, m, p, rep);
v = barycentriccalc(p, t);
_TestResult = _TestResult && doc_test_real(v, 2.011, 0.002);
//
// Fitting with individual weights
//
// NOTE: slightly different result is returned
//
real_1d_array w = "[1,1.414213562,1,1,1,1,1,1,1,1,1]";
if( _spoil_scenario==12 )
spoil_vector_by_nan(w);
if( _spoil_scenario==13 )
spoil_vector_by_posinf(w);
if( _spoil_scenario==14 )
spoil_vector_by_neginf(w);
if( _spoil_scenario==15 )
spoil_vector_by_adding_element(w);
if( _spoil_scenario==16 )
spoil_vector_by_deleting_element(w);
real_1d_array xc = "[]";
if( _spoil_scenario==17 )
spoil_vector_by_adding_element(xc);
real_1d_array yc = "[]";
if( _spoil_scenario==18 )
spoil_vector_by_adding_element(yc);
integer_1d_array dc = "[]";
if( _spoil_scenario==19 )
spoil_vector_by_adding_element(dc);
polynomialfitwc(x, y, w, xc, yc, dc, m, p, rep);
v = barycentriccalc(p, t);
_TestResult = _TestResult && doc_test_real(v, 2.023, 0.002);
_TestResult = _TestResult && (_spoil_scenario==-1);
}
catch(ap_error)
{ _TestResult = _TestResult && (_spoil_scenario!=-1); }
}
if( !_TestResult)
{
printf("%-32s FAILED\n", "lsfit_d_pol");
fflush(stdout);
}
_TotalResult = _TotalResult && _TestResult;
//
// TEST lsfit_d_polc
// Constrained polynomial fitting
//
_TestResult = true;
for(_spoil_scenario=-1; _spoil_scenario<29; _spoil_scenario++)
{
try
{
//
// This example demonstrates polynomial fitting.
//
// Fitting is done by two (M=2) functions from polynomial basis:
// f0 = 1
// f1 = x
// with simple constraint on function value
// f(0) = 0
// Basically, it just a linear fit; more complex polynomials may be used
// (e.g. parabolas with M=3, cubic with M=4), but even such simple fit allows
// us to demonstrate polynomialfit() function in action.
//
// We have:
// * x set of abscissas
// * y experimental data
// * xc points where constraints are placed
// * yc constraints on derivatives
// * dc derivative indices
// (0 means function itself, 1 means first derivative)
//
real_1d_array x = "[1.0,1.0]";
if( _spoil_scenario==0 )
spoil_vector_by_nan(x);
if( _spoil_scenario==1 )
spoil_vector_by_posinf(x);
if( _spoil_scenario==2 )
spoil_vector_by_neginf(x);
if( _spoil_scenario==3 )
spoil_vector_by_adding_element(x);
if( _spoil_scenario==4 )
spoil_vector_by_deleting_element(x);
real_1d_array y = "[0.9,1.1]";
if( _spoil_scenario==5 )
spoil_vector_by_nan(y);
if( _spoil_scenario==6 )
spoil_vector_by_posinf(y);
if( _spoil_scenario==7 )
spoil_vector_by_neginf(y);
if( _spoil_scenario==8 )
spoil_vector_by_adding_element(y);
if( _spoil_scenario==9 )
spoil_vector_by_deleting_element(y);
real_1d_array w = "[1,1]";
if( _spoil_scenario==10 )
spoil_vector_by_nan(w);
if( _spoil_scenario==11 )
spoil_vector_by_posinf(w);
if( _spoil_scenario==12 )
spoil_vector_by_neginf(w);
if( _spoil_scenario==13 )
spoil_vector_by_adding_element(w);
if( _spoil_scenario==14 )
spoil_vector_by_deleting_element(w);
real_1d_array xc = "[0]";
if( _spoil_scenario==15 )
spoil_vector_by_nan(xc);
if( _spoil_scenario==16 )
spoil_vector_by_posinf(xc);
if( _spoil_scenario==17 )
spoil_vector_by_neginf(xc);
if( _spoil_scenario==18 )
spoil_vector_by_adding_element(xc);
if( _spoil_scenario==19 )
spoil_vector_by_deleting_element(xc);
real_1d_array yc = "[0]";
if( _spoil_scenario==20 )
spoil_vector_by_nan(yc);
if( _spoil_scenario==21 )
spoil_vector_by_posinf(yc);
if( _spoil_scenario==22 )
spoil_vector_by_neginf(yc);
if( _spoil_scenario==23 )
spoil_vector_by_adding_element(yc);
if( _spoil_scenario==24 )
spoil_vector_by_deleting_element(yc);
integer_1d_array dc = "[0]";
if( _spoil_scenario==25 )
spoil_vector_by_adding_element(dc);
if( _spoil_scenario==26 )
spoil_vector_by_deleting_element(dc);
double t = 2;
if( _spoil_scenario==27 )
t = fp_posinf;
if( _spoil_scenario==28 )
t = fp_neginf;
ae_int_t m = 2;
barycentricinterpolant p;
polynomialfitreport rep;
double v;
polynomialfitwc(x, y, w, xc, yc, dc, m, p, rep);
v = barycentriccalc(p, t);
_TestResult = _TestResult && doc_test_real(v, 2.000, 0.001);
_TestResult = _TestResult && (_spoil_scenario==-1);
}
catch(ap_error)
{ _TestResult = _TestResult && (_spoil_scenario!=-1); }
}
if( !_TestResult)
{
printf("%-32s FAILED\n", "lsfit_d_polc");
fflush(stdout);
}
_TotalResult = _TotalResult && _TestResult;
//
// TEST lsfit_d_spline
// Unconstrained fitting by penalized regression spline
//
_TestResult = true;
for(_spoil_scenario=-1; _spoil_scenario<10; _spoil_scenario++)
{
try
{
//
// In this example we demonstrate penalized spline fitting of noisy data
//
// We have:
// * x - abscissas
// * y - vector of experimental data, straight line with small noise
//
real_1d_array x = "[0.00,0.10,0.20,0.30,0.40,0.50,0.60,0.70,0.80,0.90]";
if( _spoil_scenario==0 )
spoil_vector_by_nan(x);
if( _spoil_scenario==1 )
spoil_vector_by_posinf(x);
if( _spoil_scenario==2 )
spoil_vector_by_neginf(x);
if( _spoil_scenario==3 )
spoil_vector_by_adding_element(x);
if( _spoil_scenario==4 )
spoil_vector_by_deleting_element(x);
real_1d_array y = "[0.10,0.00,0.30,0.40,0.30,0.40,0.62,0.68,0.75,0.95]";
if( _spoil_scenario==5 )
spoil_vector_by_nan(y);
if( _spoil_scenario==6 )
spoil_vector_by_posinf(y);
if( _spoil_scenario==7 )
spoil_vector_by_neginf(y);
if( _spoil_scenario==8 )
spoil_vector_by_adding_element(y);
if( _spoil_scenario==9 )
spoil_vector_by_deleting_element(y);
double v;
spline1dinterpolant s;
spline1dfitreport rep;
//
// Fit with VERY small amount of smoothing (eps = 1.0E-9)
// and large number of basis functions (M=50).
//
// With such small regularization penalized spline almost fully reproduces function values
//
spline1dfit(x, y, 50, 0.000000001, s, rep);
_TestResult = _TestResult && doc_test_int(rep.terminationtype, 1);
v = spline1dcalc(s, 0.0);
_TestResult = _TestResult && doc_test_real(v, 0.10, 0.01);
//
// Fit with VERY large amount of smoothing eps=1000000
// and large number of basis functions (M=50).
//
// With such regularization our spline should become close to the straight line fit.
// We will compare its value in x=1.0 with results obtained from such fit.
//
spline1dfit(x, y, 50, 1000000, s, rep);
_TestResult = _TestResult && doc_test_int(rep.terminationtype, 1);
v = spline1dcalc(s, 1.0);
_TestResult = _TestResult && doc_test_real(v, 0.969, 0.001);
//
// In real life applications you may need some moderate degree of fitting,
// so we try to fit once more with eps=0.1.
//
spline1dfit(x, y, 50, 0.1, s, rep);
_TestResult = _TestResult && doc_test_int(rep.terminationtype, 1);
_TestResult = _TestResult && (_spoil_scenario==-1);
}
catch(ap_error)
{ _TestResult = _TestResult && (_spoil_scenario!=-1); }
}
if( !_TestResult)
{
printf("%-32s FAILED\n", "lsfit_d_spline");
fflush(stdout);
}
_TotalResult = _TotalResult && _TestResult;
//
// TEST lsfit_t_polfit_1
// Polynomial fitting, full list of parameters.
//
_TestResult = true;
for(_spoil_scenario=-1; _spoil_scenario<10; _spoil_scenario++)
{
try
{
real_1d_array x = "[0.0,0.1,0.2,0.3,0.4,0.5,0.6,0.7,0.8,0.9,1.0]";
if( _spoil_scenario==0 )
spoil_vector_by_nan(x);
if( _spoil_scenario==1 )
spoil_vector_by_posinf(x);
if( _spoil_scenario==2 )
spoil_vector_by_neginf(x);
if( _spoil_scenario==3 )
spoil_vector_by_deleting_element(x);
real_1d_array y = "[0.00,0.05,0.26,0.32,0.33,0.43,0.60,0.60,0.77,0.98,1.02]";
if( _spoil_scenario==4 )
spoil_vector_by_nan(y);
if( _spoil_scenario==5 )
spoil_vector_by_posinf(y);
if( _spoil_scenario==6 )
spoil_vector_by_neginf(y);
if( _spoil_scenario==7 )
spoil_vector_by_deleting_element(y);
ae_int_t m = 2;
double t = 2;
if( _spoil_scenario==8 )
t = fp_posinf;
if( _spoil_scenario==9 )
t = fp_neginf;
barycentricinterpolant p;
polynomialfitreport rep;
double v;
polynomialfit(x, y, 11, m, p, rep);
v = barycentriccalc(p, t);
_TestResult = _TestResult && doc_test_real(v, 2.011, 0.002);
_TestResult = _TestResult && (_spoil_scenario==-1);
}
catch(ap_error)
{ _TestResult = _TestResult && (_spoil_scenario!=-1); }
}
if( !_TestResult)
{
printf("%-32s FAILED\n", "lsfit_t_polfit_1");
fflush(stdout);
}
_TotalResult = _TotalResult && _TestResult;
//
// TEST lsfit_t_polfit_2
// Polynomial fitting, full list of parameters.
//
_TestResult = true;
for(_spoil_scenario=-1; _spoil_scenario<14; _spoil_scenario++)
{
try
{
real_1d_array x = "[0.0,0.1,0.2,0.3,0.4,0.5,0.6,0.7,0.8,0.9,1.0]";
if( _spoil_scenario==0 )
spoil_vector_by_nan(x);
if( _spoil_scenario==1 )
spoil_vector_by_posinf(x);
if( _spoil_scenario==2 )
spoil_vector_by_neginf(x);
if( _spoil_scenario==3 )
spoil_vector_by_deleting_element(x);
real_1d_array y = "[0.00,0.05,0.26,0.32,0.33,0.43,0.60,0.60,0.77,0.98,1.02]";
if( _spoil_scenario==4 )
spoil_vector_by_nan(y);
if( _spoil_scenario==5 )
spoil_vector_by_posinf(y);
if( _spoil_scenario==6 )
spoil_vector_by_neginf(y);
if( _spoil_scenario==7 )
spoil_vector_by_deleting_element(y);
real_1d_array w = "[1,1.414213562,1,1,1,1,1,1,1,1,1]";
if( _spoil_scenario==8 )
spoil_vector_by_nan(w);
if( _spoil_scenario==9 )
spoil_vector_by_posinf(w);
if( _spoil_scenario==10 )
spoil_vector_by_neginf(w);
if( _spoil_scenario==11 )
spoil_vector_by_deleting_element(w);
real_1d_array xc = "[]";
real_1d_array yc = "[]";
integer_1d_array dc = "[]";
ae_int_t m = 2;
double t = 2;
if( _spoil_scenario==12 )
t = fp_posinf;
if( _spoil_scenario==13 )
t = fp_neginf;
barycentricinterpolant p;
polynomialfitreport rep;
double v;
polynomialfitwc(x, y, w, 11, xc, yc, dc, 0, m, p, rep);
v = barycentriccalc(p, t);
_TestResult = _TestResult && doc_test_real(v, 2.023, 0.002);
_TestResult = _TestResult && (_spoil_scenario==-1);
}
catch(ap_error)
{ _TestResult = _TestResult && (_spoil_scenario!=-1); }
}
if( !_TestResult)
{
printf("%-32s FAILED\n", "lsfit_t_polfit_2");
fflush(stdout);
}
_TotalResult = _TotalResult && _TestResult;
//
// TEST lsfit_t_polfit_3
// Polynomial fitting, full list of parameters.
//
_TestResult = true;
for(_spoil_scenario=-1; _spoil_scenario<23; _spoil_scenario++)
{
try
{
real_1d_array x = "[1.0,1.0]";
if( _spoil_scenario==0 )
spoil_vector_by_nan(x);
if( _spoil_scenario==1 )
spoil_vector_by_posinf(x);
if( _spoil_scenario==2 )
spoil_vector_by_neginf(x);
if( _spoil_scenario==3 )
spoil_vector_by_deleting_element(x);
real_1d_array y = "[0.9,1.1]";
if( _spoil_scenario==4 )
spoil_vector_by_nan(y);
if( _spoil_scenario==5 )
spoil_vector_by_posinf(y);
if( _spoil_scenario==6 )
spoil_vector_by_neginf(y);
if( _spoil_scenario==7 )
spoil_vector_by_deleting_element(y);
real_1d_array w = "[1,1]";
if( _spoil_scenario==8 )
spoil_vector_by_nan(w);
if( _spoil_scenario==9 )
spoil_vector_by_posinf(w);
if( _spoil_scenario==10 )
spoil_vector_by_neginf(w);
if( _spoil_scenario==11 )
spoil_vector_by_deleting_element(w);
real_1d_array xc = "[0]";
if( _spoil_scenario==12 )
spoil_vector_by_nan(xc);
if( _spoil_scenario==13 )
spoil_vector_by_posinf(xc);
if( _spoil_scenario==14 )
spoil_vector_by_neginf(xc);
if( _spoil_scenario==15 )
spoil_vector_by_deleting_element(xc);
real_1d_array yc = "[0]";
if( _spoil_scenario==16 )
spoil_vector_by_nan(yc);
if( _spoil_scenario==17 )
spoil_vector_by_posinf(yc);
if( _spoil_scenario==18 )
spoil_vector_by_neginf(yc);
if( _spoil_scenario==19 )
spoil_vector_by_deleting_element(yc);
integer_1d_array dc = "[0]";
if( _spoil_scenario==20 )
spoil_vector_by_deleting_element(dc);
ae_int_t m = 2;
double t = 2;
if( _spoil_scenario==21 )
t = fp_posinf;
if( _spoil_scenario==22 )
t = fp_neginf;
barycentricinterpolant p;
polynomialfitreport rep;
double v;
polynomialfitwc(x, y, w, 2, xc, yc, dc, 1, m, p, rep);
v = barycentriccalc(p, t);
_TestResult = _TestResult && doc_test_real(v, 2.000, 0.001);
_TestResult = _TestResult && (_spoil_scenario==-1);
}
catch(ap_error)
{ _TestResult = _TestResult && (_spoil_scenario!=-1); }
}
if( !_TestResult)
{
printf("%-32s FAILED\n", "lsfit_t_polfit_3");
fflush(stdout);
}
_TotalResult = _TotalResult && _TestResult;
//
// TEST lsfit_t_4pl
// 4-parameter logistic fitting
//
_TestResult = true;
for(_spoil_scenario=-1; _spoil_scenario<8; _spoil_scenario++)
{
try
{
real_1d_array x = "[1,2,3,4,5,6,7,8]";
if( _spoil_scenario==0 )
spoil_vector_by_nan(x);
if( _spoil_scenario==1 )
spoil_vector_by_posinf(x);
if( _spoil_scenario==2 )
spoil_vector_by_neginf(x);
if( _spoil_scenario==3 )
spoil_vector_by_deleting_element(x);
real_1d_array y = "[0.06313223,0.44552624,0.61838364,0.71385108,0.77345838,0.81383140,0.84280033,0.86449822]";
if( _spoil_scenario==4 )
spoil_vector_by_nan(y);
if( _spoil_scenario==5 )
spoil_vector_by_posinf(y);
if( _spoil_scenario==6 )
spoil_vector_by_neginf(y);
if( _spoil_scenario==7 )
spoil_vector_by_deleting_element(y);
ae_int_t n = 8;
double a;
double b;
double c;
double d;
lsfitreport rep;
//
// Test logisticfit4() on carefully designed data with a priori known answer.
//
logisticfit4(x, y, n, a, b, c, d, rep);
_TestResult = _TestResult && doc_test_real(a, -1.000, 0.01);
_TestResult = _TestResult && doc_test_real(b, 1.200, 0.01);
_TestResult = _TestResult && doc_test_real(c, 0.900, 0.01);
_TestResult = _TestResult && doc_test_real(d, 1.000, 0.01);
//
// Evaluate model at point x=0.5
//
double v;
v = logisticcalc4(0.5, a, b, c, d);
_TestResult = _TestResult && doc_test_real(v, -0.33874308, 0.001);
_TestResult = _TestResult && (_spoil_scenario==-1);
}
catch(ap_error)
{ _TestResult = _TestResult && (_spoil_scenario!=-1); }
}
if( !_TestResult)
{
printf("%-32s FAILED\n", "lsfit_t_4pl");
fflush(stdout);
}
_TotalResult = _TotalResult && _TestResult;
//
// TEST lsfit_t_5pl
// 5-parameter logistic fitting
//
_TestResult = true;
for(_spoil_scenario=-1; _spoil_scenario<8; _spoil_scenario++)
{
try
{
real_1d_array x = "[1,2,3,4,5,6,7,8]";
if( _spoil_scenario==0 )
spoil_vector_by_nan(x);
if( _spoil_scenario==1 )
spoil_vector_by_posinf(x);
if( _spoil_scenario==2 )
spoil_vector_by_neginf(x);
if( _spoil_scenario==3 )
spoil_vector_by_deleting_element(x);
real_1d_array y = "[0.1949776139,0.5710060208,0.726002637,0.8060434158,0.8534547965,0.8842071579,0.9054773317,0.9209088299]";
if( _spoil_scenario==4 )
spoil_vector_by_nan(y);
if( _spoil_scenario==5 )
spoil_vector_by_posinf(y);
if( _spoil_scenario==6 )
spoil_vector_by_neginf(y);
if( _spoil_scenario==7 )
spoil_vector_by_deleting_element(y);
ae_int_t n = 8;
double a;
double b;
double c;
double d;
double g;
lsfitreport rep;
//
// Test logisticfit5() on carefully designed data with a priori known answer.
//
logisticfit5(x, y, n, a, b, c, d, g, rep);
_TestResult = _TestResult && doc_test_real(a, -1.000, 0.01);
_TestResult = _TestResult && doc_test_real(b, 1.200, 0.01);
_TestResult = _TestResult && doc_test_real(c, 0.900, 0.01);
_TestResult = _TestResult && doc_test_real(d, 1.000, 0.01);
_TestResult = _TestResult && doc_test_real(g, 1.200, 0.01);
//
// Evaluate model at point x=0.5
//
double v;
v = logisticcalc5(0.5, a, b, c, d, g);
_TestResult = _TestResult && doc_test_real(v, -0.2354656824, 0.001);
_TestResult = _TestResult && (_spoil_scenario==-1);
}
catch(ap_error)
{ _TestResult = _TestResult && (_spoil_scenario!=-1); }
}
if( !_TestResult)
{
printf("%-32s FAILED\n", "lsfit_t_5pl");
fflush(stdout);
}
_TotalResult = _TotalResult && _TestResult;
//
// TEST parametric_rdp
// Parametric Ramer-Douglas-Peucker approximation
//
_TestResult = true;
for(_spoil_scenario=-1; _spoil_scenario<7; _spoil_scenario++)
{
try
{
//
// We use RDP algorithm to approximate parametric 2D curve given by
// locations in t=0,1,2,3 (see below), which form piecewise linear
// trajectory through D-dimensional space (2-dimensional in our example).
//
// |
// |
// - * * X2................X3
// | .
// | .
// - * * . * * * *
// | .
// | .
// - * X1 * * * *
// | .....
// | ....
// X0----|-----|-----|-----|-----|-----|---
//
ae_int_t npoints = 4;
ae_int_t ndimensions = 2;
real_2d_array x = "[[0,0],[2,1],[3,3],[6,3]]";
if( _spoil_scenario==0 )
spoil_matrix_by_nan(x);
if( _spoil_scenario==1 )
spoil_matrix_by_posinf(x);
if( _spoil_scenario==2 )
spoil_matrix_by_neginf(x);
if( _spoil_scenario==3 )
spoil_matrix_by_deleting_row(x);
if( _spoil_scenario==4 )
spoil_matrix_by_deleting_col(x);
//
// Approximation of parametric curve is performed by another parametric curve
// with lesser amount of points. It allows to work with "compressed"
// representation, which needs smaller amount of memory. Say, in our example
// (we allow points with error smaller than 0.8) approximation will have
// just two sequential sections connecting X0 with X2, and X2 with X3.
//
// |
// |
// - * * X2................X3
// | .
// | .
// - * . * * * *
// | .
// | .
// - . X1 * * * *
// | .
// | .
// X0----|-----|-----|-----|-----|-----|---
//
//
real_2d_array y;
integer_1d_array idxy;
ae_int_t nsections;
ae_int_t limitcnt = 0;
double limiteps = 0.8;
if( _spoil_scenario==5 )
limiteps = fp_posinf;
if( _spoil_scenario==6 )
limiteps = fp_neginf;
parametricrdpfixed(x, npoints, ndimensions, limitcnt, limiteps, y, idxy, nsections);
_TestResult = _TestResult && doc_test_int(nsections, 2);
_TestResult = _TestResult && doc_test_int_vector(idxy, "[0,2,3]");
_TestResult = _TestResult && (_spoil_scenario==-1);
}
catch(ap_error)
{ _TestResult = _TestResult && (_spoil_scenario!=-1); }
}
if( !_TestResult)
{
printf("%-32s FAILED\n", "parametric_rdp");
fflush(stdout);
}
_TotalResult = _TotalResult && _TestResult;
//
// TEST spline2d_bilinear
// Bilinear spline interpolation
//
_TestResult = true;
for(_spoil_scenario=-1; _spoil_scenario<16; _spoil_scenario++)
{
try
{
//
// We use bilinear spline to interpolate f(x,y)=x^2+2*y^2 sampled
// at (x,y) from [0.0, 0.5, 1.0] X [0.0, 1.0].
//
real_1d_array x = "[0.0, 0.5, 1.0]";
if( _spoil_scenario==0 )
spoil_vector_by_nan(x);
if( _spoil_scenario==1 )
spoil_vector_by_posinf(x);
if( _spoil_scenario==2 )
spoil_vector_by_neginf(x);
if( _spoil_scenario==3 )
spoil_vector_by_deleting_element(x);
real_1d_array y = "[0.0, 1.0]";
if( _spoil_scenario==4 )
spoil_vector_by_nan(y);
if( _spoil_scenario==5 )
spoil_vector_by_posinf(y);
if( _spoil_scenario==6 )
spoil_vector_by_neginf(y);
if( _spoil_scenario==7 )
spoil_vector_by_deleting_element(y);
real_1d_array f = "[0.00,0.25,1.00,2.00,2.25,3.00]";
if( _spoil_scenario==8 )
spoil_vector_by_nan(f);
if( _spoil_scenario==9 )
spoil_vector_by_posinf(f);
if( _spoil_scenario==10 )
spoil_vector_by_neginf(f);
if( _spoil_scenario==11 )
spoil_vector_by_deleting_element(f);
double vx = 0.25;
if( _spoil_scenario==12 )
vx = fp_posinf;
if( _spoil_scenario==13 )
vx = fp_neginf;
double vy = 0.50;
if( _spoil_scenario==14 )
vy = fp_posinf;
if( _spoil_scenario==15 )
vy = fp_neginf;
double v;
spline2dinterpolant s;
// build spline
spline2dbuildbilinearv(x, 3, y, 2, f, 1, s);
// calculate S(0.25,0.50)
v = spline2dcalc(s, vx, vy);
_TestResult = _TestResult && doc_test_real(v, 1.1250, 0.00005);
_TestResult = _TestResult && (_spoil_scenario==-1);
}
catch(ap_error)
{ _TestResult = _TestResult && (_spoil_scenario!=-1); }
}
if( !_TestResult)
{
printf("%-32s FAILED\n", "spline2d_bilinear");
fflush(stdout);
}
_TotalResult = _TotalResult && _TestResult;
//
// TEST spline2d_bicubic
// Bilinear spline interpolation
//
_TestResult = true;
for(_spoil_scenario=-1; _spoil_scenario<16; _spoil_scenario++)
{
try
{
//
// We use bilinear spline to interpolate f(x,y)=x^2+2*y^2 sampled
// at (x,y) from [0.0, 0.5, 1.0] X [0.0, 1.0].
//
real_1d_array x = "[0.0, 0.5, 1.0]";
if( _spoil_scenario==0 )
spoil_vector_by_nan(x);
if( _spoil_scenario==1 )
spoil_vector_by_posinf(x);
if( _spoil_scenario==2 )
spoil_vector_by_neginf(x);
if( _spoil_scenario==3 )
spoil_vector_by_deleting_element(x);
real_1d_array y = "[0.0, 1.0]";
if( _spoil_scenario==4 )
spoil_vector_by_nan(y);
if( _spoil_scenario==5 )
spoil_vector_by_posinf(y);
if( _spoil_scenario==6 )
spoil_vector_by_neginf(y);
if( _spoil_scenario==7 )
spoil_vector_by_deleting_element(y);
real_1d_array f = "[0.00,0.25,1.00,2.00,2.25,3.00]";
if( _spoil_scenario==8 )
spoil_vector_by_nan(f);
if( _spoil_scenario==9 )
spoil_vector_by_posinf(f);
if( _spoil_scenario==10 )
spoil_vector_by_neginf(f);
if( _spoil_scenario==11 )
spoil_vector_by_deleting_element(f);
double vx = 0.25;
if( _spoil_scenario==12 )
vx = fp_posinf;
if( _spoil_scenario==13 )
vx = fp_neginf;
double vy = 0.50;
if( _spoil_scenario==14 )
vy = fp_posinf;
if( _spoil_scenario==15 )
vy = fp_neginf;
double v;
double dx;
double dy;
spline2dinterpolant s;
// build spline
spline2dbuildbicubicv(x, 3, y, 2, f, 1, s);
// calculate S(0.25,0.50)
v = spline2dcalc(s, vx, vy);
_TestResult = _TestResult && doc_test_real(v, 1.0625, 0.00005);
// calculate derivatives
spline2ddiff(s, vx, vy, v, dx, dy);
_TestResult = _TestResult && doc_test_real(v, 1.0625, 0.00005);
_TestResult = _TestResult && doc_test_real(dx, 0.5000, 0.00005);
_TestResult = _TestResult && doc_test_real(dy, 2.0000, 0.00005);
_TestResult = _TestResult && (_spoil_scenario==-1);
}
catch(ap_error)
{ _TestResult = _TestResult && (_spoil_scenario!=-1); }
}
if( !_TestResult)
{
printf("%-32s FAILED\n", "spline2d_bicubic");
fflush(stdout);
}
_TotalResult = _TotalResult && _TestResult;
//
// TEST spline2d_fit_blocklls
// Fitting bicubic spline to irregular data
//
_TestResult = true;
for(_spoil_scenario=-1; _spoil_scenario<5; _spoil_scenario++)
{
try
{
//
// We use bicubic spline to reproduce f(x,y)=1/(1+x^2+2*y^2) sampled
// at irregular points (x,y) from [-1,+1]*[-1,+1]
//
// We have 5 such points, located approximately at corners of the area
// and its center - but not exactly at the grid. Thus, we have to FIT
// the spline, i.e. to solve least squares problem
//
real_2d_array xy = "[[-0.987,-0.902,0.359],[0.948,-0.992,0.347],[-1.000,1.000,0.333],[1.000,0.973,0.339],[0.017,0.180,0.968]]";
if( _spoil_scenario==0 )
spoil_matrix_by_nan(xy);
if( _spoil_scenario==1 )
spoil_matrix_by_posinf(xy);
if( _spoil_scenario==2 )
spoil_matrix_by_neginf(xy);
if( _spoil_scenario==3 )
spoil_matrix_by_deleting_row(xy);
if( _spoil_scenario==4 )
spoil_matrix_by_deleting_col(xy);
//
// First step is to create spline2dbuilder object and set its properties:
// * d=1 means that we create vector-valued spline with 1 component
// * we specify dataset xy
// * we rely on automatic selection of interpolation area
// * we tell builder that we want to use 5x5 grid for an underlying spline
// * we choose least squares solver named BlockLLS and configure it by
// telling that we want to apply zero nonlinearity penalty.
//
// NOTE: you can specify non-zero lambdav if you want to make your spline
// more "rigid", i.e. to penalize nonlinearity.
//
// NOTE: ALGLIB has two solvers which fit bicubic splines to irregular data,
// one of them is BlockLLS and another one is FastDDM. Former is
// intended for moderately sized grids (up to 512x512 nodes, although
// it may take up to few minutes); it is the most easy to use and
// control spline fitting function in the library. Latter, FastDDM,
// is intended for efficient solution of large-scale problems
// (up to 100.000.000 nodes). Both solvers can be parallelized, but
// FastDDM is much more efficient. See comments for more information.
//
spline2dbuilder builder;
ae_int_t d = 1;
double lambdav = 0.000;
spline2dbuildercreate(d, builder);
spline2dbuildersetpoints(builder, xy, 5);
spline2dbuildersetgrid(builder, 5, 5);
spline2dbuildersetalgoblocklls(builder, lambdav);
//
// Now we are ready to fit and evaluate our results
//
spline2dinterpolant s;
spline2dfitreport rep;
spline2dfit(builder, s, rep);
// evaluate results - function value at the grid is reproduced exactly
double v;
v = spline2dcalc(s, -1, 1);
_TestResult = _TestResult && doc_test_real(v, 0.333000, 0.005);
// check maximum error - it must be nearly zero
_TestResult = _TestResult && doc_test_real(rep.maxerror, 0.000, 0.005);
_TestResult = _TestResult && (_spoil_scenario==-1);
}
catch(ap_error)
{ _TestResult = _TestResult && (_spoil_scenario!=-1); }
}
if( !_TestResult)
{
printf("%-32s FAILED\n", "spline2d_fit_blocklls");
fflush(stdout);
}
_TotalResult = _TotalResult && _TestResult;
//
// TEST spline2d_unpack
// Unpacking bilinear spline
//
_TestResult = true;
for(_spoil_scenario=-1; _spoil_scenario<12; _spoil_scenario++)
{
try
{
//
// We build bilinear spline for f(x,y)=x+2*y+3*xy for (x,y) in [0,1].
// Then we demonstrate how to unpack it.
//
real_1d_array x = "[0.0, 1.0]";
if( _spoil_scenario==0 )
spoil_vector_by_nan(x);
if( _spoil_scenario==1 )
spoil_vector_by_posinf(x);
if( _spoil_scenario==2 )
spoil_vector_by_neginf(x);
if( _spoil_scenario==3 )
spoil_vector_by_deleting_element(x);
real_1d_array y = "[0.0, 1.0]";
if( _spoil_scenario==4 )
spoil_vector_by_nan(y);
if( _spoil_scenario==5 )
spoil_vector_by_posinf(y);
if( _spoil_scenario==6 )
spoil_vector_by_neginf(y);
if( _spoil_scenario==7 )
spoil_vector_by_deleting_element(y);
real_1d_array f = "[0.00,1.00,2.00,6.00]";
if( _spoil_scenario==8 )
spoil_vector_by_nan(f);
if( _spoil_scenario==9 )
spoil_vector_by_posinf(f);
if( _spoil_scenario==10 )
spoil_vector_by_neginf(f);
if( _spoil_scenario==11 )
spoil_vector_by_deleting_element(f);
real_2d_array c;
ae_int_t m;
ae_int_t n;
ae_int_t d;
spline2dinterpolant s;
// build spline
spline2dbuildbilinearv(x, 2, y, 2, f, 1, s);
// unpack and test
spline2dunpackv(s, m, n, d, c);
_TestResult = _TestResult && doc_test_real_matrix(c, "[[0, 1, 0, 1, 0,2,0,0, 1,3,0,0, 0,0,0,0, 0,0,0,0, 1]]", 0.00005);
_TestResult = _TestResult && (_spoil_scenario==-1);
}
catch(ap_error)
{ _TestResult = _TestResult && (_spoil_scenario!=-1); }
}
if( !_TestResult)
{
printf("%-32s FAILED\n", "spline2d_unpack");
fflush(stdout);
}
_TotalResult = _TotalResult && _TestResult;
//
// TEST spline2d_copytrans
// Copy and transform
//
_TestResult = true;
for(_spoil_scenario=-1; _spoil_scenario<16; _spoil_scenario++)
{
try
{
//
// We build bilinear spline for f(x,y)=x+2*y for (x,y) in [0,1].
// Then we apply several transformations to this spline.
//
real_1d_array x = "[0.0, 1.0]";
if( _spoil_scenario==0 )
spoil_vector_by_nan(x);
if( _spoil_scenario==1 )
spoil_vector_by_posinf(x);
if( _spoil_scenario==2 )
spoil_vector_by_neginf(x);
if( _spoil_scenario==3 )
spoil_vector_by_deleting_element(x);
real_1d_array y = "[0.0, 1.0]";
if( _spoil_scenario==4 )
spoil_vector_by_nan(y);
if( _spoil_scenario==5 )
spoil_vector_by_posinf(y);
if( _spoil_scenario==6 )
spoil_vector_by_neginf(y);
if( _spoil_scenario==7 )
spoil_vector_by_deleting_element(y);
real_1d_array f = "[0.00,1.00,2.00,3.00]";
if( _spoil_scenario==8 )
spoil_vector_by_nan(f);
if( _spoil_scenario==9 )
spoil_vector_by_posinf(f);
if( _spoil_scenario==10 )
spoil_vector_by_neginf(f);
if( _spoil_scenario==11 )
spoil_vector_by_deleting_element(f);
spline2dinterpolant s;
spline2dinterpolant snew;
double v;
spline2dbuildbilinearv(x, 2, y, 2, f, 1, s);
// copy spline, apply transformation x:=2*xnew, y:=4*ynew
// evaluate at (xnew,ynew) = (0.25,0.25) - should be same as (x,y)=(0.5,1.0)
spline2dcopy(s, snew);
spline2dlintransxy(snew, 2.0, 0.0, 4.0, 0.0);
v = spline2dcalc(snew, 0.25, 0.25);
_TestResult = _TestResult && doc_test_real(v, 2.500, 0.00005);
// copy spline, apply transformation SNew:=2*S+3
spline2dcopy(s, snew);
spline2dlintransf(snew, 2.0, 3.0);
v = spline2dcalc(snew, 0.5, 1.0);
_TestResult = _TestResult && doc_test_real(v, 8.000, 0.00005);
//
// Same example, but for vector spline (f0,f1) = {x+2*y, 2*x+y}
//
real_1d_array f2 = "[0.00,0.00, 1.00,2.00, 2.00,1.00, 3.00,3.00]";
if( _spoil_scenario==12 )
spoil_vector_by_nan(f2);
if( _spoil_scenario==13 )
spoil_vector_by_posinf(f2);
if( _spoil_scenario==14 )
spoil_vector_by_neginf(f2);
if( _spoil_scenario==15 )
spoil_vector_by_deleting_element(f2);
real_1d_array vr;
spline2dbuildbilinearv(x, 2, y, 2, f2, 2, s);
// copy spline, apply transformation x:=2*xnew, y:=4*ynew
spline2dcopy(s, snew);
spline2dlintransxy(snew, 2.0, 0.0, 4.0, 0.0);
spline2dcalcv(snew, 0.25, 0.25, vr);
_TestResult = _TestResult && doc_test_real_vector(vr, "[2.500,2.000]", 0.00005);
// copy spline, apply transformation SNew:=2*S+3
spline2dcopy(s, snew);
spline2dlintransf(snew, 2.0, 3.0);
spline2dcalcv(snew, 0.5, 1.0, vr);
_TestResult = _TestResult && doc_test_real_vector(vr, "[8.000,7.000]", 0.00005);
_TestResult = _TestResult && (_spoil_scenario==-1);
}
catch(ap_error)
{ _TestResult = _TestResult && (_spoil_scenario!=-1); }
}
if( !_TestResult)
{
printf("%-32s FAILED\n", "spline2d_copytrans");
fflush(stdout);
}
_TotalResult = _TotalResult && _TestResult;
//
// TEST spline2d_vector
// Copy and transform
//
_TestResult = true;
for(_spoil_scenario=-1; _spoil_scenario<12; _spoil_scenario++)
{
try
{
//
// We build bilinear vector-valued spline (f0,f1) = {x+2*y, 2*x+y}
// Spline is built using function values at 2x2 grid: (x,y)=[0,1]*[0,1]
// Then we perform evaluation at (x,y)=(0.1,0.3)
//
real_1d_array x = "[0.0, 1.0]";
if( _spoil_scenario==0 )
spoil_vector_by_nan(x);
if( _spoil_scenario==1 )
spoil_vector_by_posinf(x);
if( _spoil_scenario==2 )
spoil_vector_by_neginf(x);
if( _spoil_scenario==3 )
spoil_vector_by_deleting_element(x);
real_1d_array y = "[0.0, 1.0]";
if( _spoil_scenario==4 )
spoil_vector_by_nan(y);
if( _spoil_scenario==5 )
spoil_vector_by_posinf(y);
if( _spoil_scenario==6 )
spoil_vector_by_neginf(y);
if( _spoil_scenario==7 )
spoil_vector_by_deleting_element(y);
real_1d_array f = "[0.00,0.00, 1.00,2.00, 2.00,1.00, 3.00,3.00]";
if( _spoil_scenario==8 )
spoil_vector_by_nan(f);
if( _spoil_scenario==9 )
spoil_vector_by_posinf(f);
if( _spoil_scenario==10 )
spoil_vector_by_neginf(f);
if( _spoil_scenario==11 )
spoil_vector_by_deleting_element(f);
spline2dinterpolant s;
real_1d_array vr;
spline2dbuildbilinearv(x, 2, y, 2, f, 2, s);
spline2dcalcv(s, 0.1, 0.3, vr);
_TestResult = _TestResult && doc_test_real_vector(vr, "[0.700,0.500]", 0.00005);
_TestResult = _TestResult && (_spoil_scenario==-1);
}
catch(ap_error)
{ _TestResult = _TestResult && (_spoil_scenario!=-1); }
}
if( !_TestResult)
{
printf("%-32s FAILED\n", "spline2d_vector");
fflush(stdout);
}
_TotalResult = _TotalResult && _TestResult;
//
// TEST spline3d_trilinear
// Trilinear spline interpolation
//
_TestResult = true;
for(_spoil_scenario=-1; _spoil_scenario<22; _spoil_scenario++)
{
try
{
//
// We use trilinear spline to interpolate f(x,y,z)=x+xy+z sampled
// at (x,y,z) from [0.0, 1.0] X [0.0, 1.0] X [0.0, 1.0].
//
// We store x, y and z-values at local arrays with same names.
// Function values are stored in the array F as follows:
// f[0] (x,y,z) = (0,0,0)
// f[1] (x,y,z) = (1,0,0)
// f[2] (x,y,z) = (0,1,0)
// f[3] (x,y,z) = (1,1,0)
// f[4] (x,y,z) = (0,0,1)
// f[5] (x,y,z) = (1,0,1)
// f[6] (x,y,z) = (0,1,1)
// f[7] (x,y,z) = (1,1,1)
//
real_1d_array x = "[0.0, 1.0]";
if( _spoil_scenario==0 )
spoil_vector_by_nan(x);
if( _spoil_scenario==1 )
spoil_vector_by_posinf(x);
if( _spoil_scenario==2 )
spoil_vector_by_neginf(x);
if( _spoil_scenario==3 )
spoil_vector_by_deleting_element(x);
real_1d_array y = "[0.0, 1.0]";
if( _spoil_scenario==4 )
spoil_vector_by_nan(y);
if( _spoil_scenario==5 )
spoil_vector_by_posinf(y);
if( _spoil_scenario==6 )
spoil_vector_by_neginf(y);
if( _spoil_scenario==7 )
spoil_vector_by_deleting_element(y);
real_1d_array z = "[0.0, 1.0]";
if( _spoil_scenario==8 )
spoil_vector_by_nan(z);
if( _spoil_scenario==9 )
spoil_vector_by_posinf(z);
if( _spoil_scenario==10 )
spoil_vector_by_neginf(z);
if( _spoil_scenario==11 )
spoil_vector_by_deleting_element(z);
real_1d_array f = "[0,1,0,2,1,2,1,3]";
if( _spoil_scenario==12 )
spoil_vector_by_nan(f);
if( _spoil_scenario==13 )
spoil_vector_by_posinf(f);
if( _spoil_scenario==14 )
spoil_vector_by_neginf(f);
if( _spoil_scenario==15 )
spoil_vector_by_deleting_element(f);
double vx = 0.50;
if( _spoil_scenario==16 )
vx = fp_posinf;
if( _spoil_scenario==17 )
vx = fp_neginf;
double vy = 0.50;
if( _spoil_scenario==18 )
vy = fp_posinf;
if( _spoil_scenario==19 )
vy = fp_neginf;
double vz = 0.50;
if( _spoil_scenario==20 )
vz = fp_posinf;
if( _spoil_scenario==21 )
vz = fp_neginf;
double v;
spline3dinterpolant s;
// build spline
spline3dbuildtrilinearv(x, 2, y, 2, z, 2, f, 1, s);
// calculate S(0.5,0.5,0.5)
v = spline3dcalc(s, vx, vy, vz);
_TestResult = _TestResult && doc_test_real(v, 1.2500, 0.00005);
_TestResult = _TestResult && (_spoil_scenario==-1);
}
catch(ap_error)
{ _TestResult = _TestResult && (_spoil_scenario!=-1); }
}
if( !_TestResult)
{
printf("%-32s FAILED\n", "spline3d_trilinear");
fflush(stdout);
}
_TotalResult = _TotalResult && _TestResult;
//
// TEST spline3d_vector
// Vector-valued trilinear spline interpolation
//
_TestResult = true;
for(_spoil_scenario=-1; _spoil_scenario<22; _spoil_scenario++)
{
try
{
//
// We use trilinear vector-valued spline to interpolate {f0,f1}={x+xy+z,x+xy+yz+z}
// sampled at (x,y,z) from [0.0, 1.0] X [0.0, 1.0] X [0.0, 1.0].
//
// We store x, y and z-values at local arrays with same names.
// Function values are stored in the array F as follows:
// f[0] f0, (x,y,z) = (0,0,0)
// f[1] f1, (x,y,z) = (0,0,0)
// f[2] f0, (x,y,z) = (1,0,0)
// f[3] f1, (x,y,z) = (1,0,0)
// f[4] f0, (x,y,z) = (0,1,0)
// f[5] f1, (x,y,z) = (0,1,0)
// f[6] f0, (x,y,z) = (1,1,0)
// f[7] f1, (x,y,z) = (1,1,0)
// f[8] f0, (x,y,z) = (0,0,1)
// f[9] f1, (x,y,z) = (0,0,1)
// f[10] f0, (x,y,z) = (1,0,1)
// f[11] f1, (x,y,z) = (1,0,1)
// f[12] f0, (x,y,z) = (0,1,1)
// f[13] f1, (x,y,z) = (0,1,1)
// f[14] f0, (x,y,z) = (1,1,1)
// f[15] f1, (x,y,z) = (1,1,1)
//
real_1d_array x = "[0.0, 1.0]";
if( _spoil_scenario==0 )
spoil_vector_by_nan(x);
if( _spoil_scenario==1 )
spoil_vector_by_posinf(x);
if( _spoil_scenario==2 )
spoil_vector_by_neginf(x);
if( _spoil_scenario==3 )
spoil_vector_by_deleting_element(x);
real_1d_array y = "[0.0, 1.0]";
if( _spoil_scenario==4 )
spoil_vector_by_nan(y);
if( _spoil_scenario==5 )
spoil_vector_by_posinf(y);
if( _spoil_scenario==6 )
spoil_vector_by_neginf(y);
if( _spoil_scenario==7 )
spoil_vector_by_deleting_element(y);
real_1d_array z = "[0.0, 1.0]";
if( _spoil_scenario==8 )
spoil_vector_by_nan(z);
if( _spoil_scenario==9 )
spoil_vector_by_posinf(z);
if( _spoil_scenario==10 )
spoil_vector_by_neginf(z);
if( _spoil_scenario==11 )
spoil_vector_by_deleting_element(z);
real_1d_array f = "[0,0, 1,1, 0,0, 2,2, 1,1, 2,2, 1,2, 3,4]";
if( _spoil_scenario==12 )
spoil_vector_by_nan(f);
if( _spoil_scenario==13 )
spoil_vector_by_posinf(f);
if( _spoil_scenario==14 )
spoil_vector_by_neginf(f);
if( _spoil_scenario==15 )
spoil_vector_by_deleting_element(f);
double vx = 0.50;
if( _spoil_scenario==16 )
vx = fp_posinf;
if( _spoil_scenario==17 )
vx = fp_neginf;
double vy = 0.50;
if( _spoil_scenario==18 )
vy = fp_posinf;
if( _spoil_scenario==19 )
vy = fp_neginf;
double vz = 0.50;
if( _spoil_scenario==20 )
vz = fp_posinf;
if( _spoil_scenario==21 )
vz = fp_neginf;
spline3dinterpolant s;
// build spline
spline3dbuildtrilinearv(x, 2, y, 2, z, 2, f, 2, s);
// calculate S(0.5,0.5,0.5) - we have vector of values instead of single value
real_1d_array v;
spline3dcalcv(s, vx, vy, vz, v);
_TestResult = _TestResult && doc_test_real_vector(v, "[1.2500,1.5000]", 0.00005);
_TestResult = _TestResult && (_spoil_scenario==-1);
}
catch(ap_error)
{ _TestResult = _TestResult && (_spoil_scenario!=-1); }
}
if( !_TestResult)
{
printf("%-32s FAILED\n", "spline3d_vector");
fflush(stdout);
}
_TotalResult = _TotalResult && _TestResult;
//
// TEST rbf_d_hrbf
// Simple model built with HRBF algorithm
//
_TestResult = true;
for(_spoil_scenario=-1; _spoil_scenario<3; _spoil_scenario++)
{
try
{
//
// This example illustrates basic concepts of the RBF models: creation, modification,
// evaluation.
//
// Suppose that we have set of 2-dimensional points with associated
// scalar function values, and we want to build a RBF model using
// our data.
//
// NOTE: we can work with 3D models too :)
//
// Typical sequence of steps is given below:
// 1. we create RBF model object
// 2. we attach our dataset to the RBF model and tune algorithm settings
// 3. we rebuild RBF model using QNN algorithm on new data
// 4. we use RBF model (evaluate, serialize, etc.)
//
double v;
//
// Step 1: RBF model creation.
//
// We have to specify dimensionality of the space (2 or 3) and
// dimensionality of the function (scalar or vector).
//
// New model is empty - it can be evaluated,
// but we just get zero value at any point.
//
rbfmodel model;
rbfcreate(2, 1, model);
v = rbfcalc2(model, 0.0, 0.0);
_TestResult = _TestResult && doc_test_real(v, 0.000, 0.005);
//
// Step 2: we add dataset.
//
// XY contains two points - x0=(-1,0) and x1=(+1,0) -
// and two function values f(x0)=2, f(x1)=3.
//
// We added points, but model was not rebuild yet.
// If we call rbfcalc2(), we still will get 0.0 as result.
//
real_2d_array xy = "[[-1,0,2],[+1,0,3]]";
if( _spoil_scenario==0 )
spoil_matrix_by_nan(xy);
if( _spoil_scenario==1 )
spoil_matrix_by_posinf(xy);
if( _spoil_scenario==2 )
spoil_matrix_by_neginf(xy);
rbfsetpoints(model, xy);
v = rbfcalc2(model, 0.0, 0.0);
_TestResult = _TestResult && doc_test_real(v, 0.000, 0.005);
//
// Step 3: rebuild model
//
// After we've configured model, we should rebuild it -
// it will change coefficients stored internally in the
// rbfmodel structure.
//
// We use hierarchical RBF algorithm with following parameters:
// * RBase - set to 1.0
// * NLayers - three layers are used (although such simple problem
// does not need more than 1 layer)
// * LambdaReg - is set to zero value, no smoothing is required
//
rbfreport rep;
rbfsetalgohierarchical(model, 1.0, 3, 0.0);
rbfbuildmodel(model, rep);
_TestResult = _TestResult && doc_test_int(rep.terminationtype, 1);
//
// Step 4: model was built
//
// After call of rbfbuildmodel(), rbfcalc2() will return
// value of the new model.
//
v = rbfcalc2(model, 0.0, 0.0);
_TestResult = _TestResult && doc_test_real(v, 2.500, 0.005);
_TestResult = _TestResult && (_spoil_scenario==-1);
}
catch(ap_error)
{ _TestResult = _TestResult && (_spoil_scenario!=-1); }
}
if( !_TestResult)
{
printf("%-32s FAILED\n", "rbf_d_hrbf");
fflush(stdout);
}
_TotalResult = _TotalResult && _TestResult;
//
// TEST rbf_d_vector
// Working with vector functions
//
_TestResult = true;
for(_spoil_scenario=-1; _spoil_scenario<6; _spoil_scenario++)
{
try
{
//
// Suppose that we have set of 2-dimensional points with associated VECTOR
// function values, and we want to build a RBF model using our data.
//
// Typical sequence of steps is given below:
// 1. we create RBF model object
// 2. we attach our dataset to the RBF model and tune algorithm settings
// 3. we rebuild RBF model using new data
// 4. we use RBF model (evaluate, serialize, etc.)
//
real_1d_array x;
real_1d_array y;
//
// Step 1: RBF model creation.
//
// We have to specify dimensionality of the space (equal to 2) and
// dimensionality of the function (2-dimensional vector function).
//
// New model is empty - it can be evaluated,
// but we just get zero value at any point.
//
rbfmodel model;
rbfcreate(2, 2, model);
x = "[+1,+1]";
if( _spoil_scenario==0 )
spoil_vector_by_nan(x);
if( _spoil_scenario==1 )
spoil_vector_by_posinf(x);
if( _spoil_scenario==2 )
spoil_vector_by_neginf(x);
rbfcalc(model, x, y);
_TestResult = _TestResult && doc_test_real_vector(y, "[0.000,0.000]", 0.005);
//
// Step 2: we add dataset.
//
// XY arrays containt four points:
// * (x0,y0) = (+1,+1), f(x0,y0)=(0,-1)
// * (x1,y1) = (+1,-1), f(x1,y1)=(-1,0)
// * (x2,y2) = (-1,-1), f(x2,y2)=(0,+1)
// * (x3,y3) = (-1,+1), f(x3,y3)=(+1,0)
//
real_2d_array xy = "[[+1,+1,0,-1],[+1,-1,-1,0],[-1,-1,0,+1],[-1,+1,+1,0]]";
if( _spoil_scenario==3 )
spoil_matrix_by_nan(xy);
if( _spoil_scenario==4 )
spoil_matrix_by_posinf(xy);
if( _spoil_scenario==5 )
spoil_matrix_by_neginf(xy);
rbfsetpoints(model, xy);
// We added points, but model was not rebuild yet.
// If we call rbfcalc(), we still will get 0.0 as result.
rbfcalc(model, x, y);
_TestResult = _TestResult && doc_test_real_vector(y, "[0.000,0.000]", 0.005);
//
// Step 3: rebuild model
//
// We use hierarchical RBF algorithm with following parameters:
// * RBase - set to 1.0
// * NLayers - three layers are used (although such simple problem
// does not need more than 1 layer)
// * LambdaReg - is set to zero value, no smoothing is required
//
// After we've configured model, we should rebuild it -
// it will change coefficients stored internally in the
// rbfmodel structure.
//
rbfreport rep;
rbfsetalgohierarchical(model, 1.0, 3, 0.0);
rbfbuildmodel(model, rep);
_TestResult = _TestResult && doc_test_int(rep.terminationtype, 1);
//
// Step 4: model was built
//
// After call of rbfbuildmodel(), rbfcalc() will return
// value of the new model.
//
rbfcalc(model, x, y);
_TestResult = _TestResult && doc_test_real_vector(y, "[0.000,-1.000]", 0.005);
_TestResult = _TestResult && (_spoil_scenario==-1);
}
catch(ap_error)
{ _TestResult = _TestResult && (_spoil_scenario!=-1); }
}
if( !_TestResult)
{
printf("%-32s FAILED\n", "rbf_d_vector");
fflush(stdout);
}
_TotalResult = _TotalResult && _TestResult;
//
// TEST rbf_d_polterm
// RBF models - working with polynomial term
//
_TestResult = true;
for(_spoil_scenario=-1; _spoil_scenario<3; _spoil_scenario++)
{
try
{
//
// This example show how to work with polynomial term
//
// Suppose that we have set of 2-dimensional points with associated
// scalar function values, and we want to build a RBF model using
// our data.
//
// We use hierarchical RBF algorithm with following parameters:
// * RBase - set to 1.0
// * NLayers - three layers are used (although such simple problem
// does not need more than 1 layer)
// * LambdaReg - is set to zero value, no smoothing is required
//
double v;
rbfmodel model;
real_2d_array xy = "[[-1,0,2],[+1,0,3]]";
if( _spoil_scenario==0 )
spoil_matrix_by_nan(xy);
if( _spoil_scenario==1 )
spoil_matrix_by_posinf(xy);
if( _spoil_scenario==2 )
spoil_matrix_by_neginf(xy);
rbfreport rep;
rbfcreate(2, 1, model);
rbfsetpoints(model, xy);
rbfsetalgohierarchical(model, 1.0, 3, 0.0);
//
// By default, RBF model uses linear term. It means that model
// looks like
// f(x,y) = SUM(RBF[i]) + a*x + b*y + c
// where RBF[i] is I-th radial basis function and a*x+by+c is a
// linear term. Having linear terms in a model gives us:
// (1) improved extrapolation properties
// (2) linearity of the model when data can be perfectly fitted
// by the linear function
// (3) linear asymptotic behavior
//
// Our simple dataset can be modelled by the linear function
// f(x,y) = 0.5*x + 2.5
// and rbfbuildmodel() with default settings should preserve this
// linearity.
//
ae_int_t nx;
ae_int_t ny;
ae_int_t nc;
ae_int_t modelversion;
real_2d_array xwr;
real_2d_array c;
rbfbuildmodel(model, rep);
_TestResult = _TestResult && doc_test_int(rep.terminationtype, 1);
rbfunpack(model, nx, ny, xwr, nc, c, modelversion);
_TestResult = _TestResult && doc_test_real_matrix(c, "[[0.500,0.000,2.500]]", 0.005);
// asymptotic behavior of our function is linear
v = rbfcalc2(model, 1000.0, 0.0);
_TestResult = _TestResult && doc_test_real(v, 502.50, 0.05);
//
// Instead of linear term we can use constant term. In this case
// we will get model which has form
// f(x,y) = SUM(RBF[i]) + c
// where RBF[i] is I-th radial basis function and c is a constant,
// which is equal to the average function value on the dataset.
//
// Because we've already attached dataset to the model the only
// thing we have to do is to call rbfsetconstterm() and then
// rebuild model with rbfbuildmodel().
//
rbfsetconstterm(model);
rbfbuildmodel(model, rep);
_TestResult = _TestResult && doc_test_int(rep.terminationtype, 1);
rbfunpack(model, nx, ny, xwr, nc, c, modelversion);
_TestResult = _TestResult && doc_test_real_matrix(c, "[[0.000,0.000,2.500]]", 0.005);
// asymptotic behavior of our function is constant
v = rbfcalc2(model, 1000.0, 0.0);
_TestResult = _TestResult && doc_test_real(v, 2.500, 0.005);
//
// Finally, we can use zero term. Just plain RBF without polynomial
// part:
// f(x,y) = SUM(RBF[i])
// where RBF[i] is I-th radial basis function.
//
rbfsetzeroterm(model);
rbfbuildmodel(model, rep);
_TestResult = _TestResult && doc_test_int(rep.terminationtype, 1);
rbfunpack(model, nx, ny, xwr, nc, c, modelversion);
_TestResult = _TestResult && doc_test_real_matrix(c, "[[0.000,0.000,0.000]]", 0.005);
// asymptotic behavior of our function is just zero constant
v = rbfcalc2(model, 1000.0, 0.0);
_TestResult = _TestResult && doc_test_real(v, 0.000, 0.005);
_TestResult = _TestResult && (_spoil_scenario==-1);
}
catch(ap_error)
{ _TestResult = _TestResult && (_spoil_scenario!=-1); }
}
if( !_TestResult)
{
printf("%-32s FAILED\n", "rbf_d_polterm");
fflush(stdout);
}
_TotalResult = _TotalResult && _TestResult;
//
// TEST rbf_d_serialize
// Serialization/unserialization
//
_TestResult = true;
for(_spoil_scenario=-1; _spoil_scenario<3; _spoil_scenario++)
{
try
{
//
// This example show how to serialize and unserialize RBF model
//
// Suppose that we have set of 2-dimensional points with associated
// scalar function values, and we want to build a RBF model using
// our data. Then we want to serialize it to string and to unserialize
// from string, loading to another instance of RBF model.
//
// Here we assume that you already know how to create RBF models.
//
std::string s;
double v;
rbfmodel model0;
rbfmodel model1;
real_2d_array xy = "[[-1,0,2],[+1,0,3]]";
if( _spoil_scenario==0 )
spoil_matrix_by_nan(xy);
if( _spoil_scenario==1 )
spoil_matrix_by_posinf(xy);
if( _spoil_scenario==2 )
spoil_matrix_by_neginf(xy);
rbfreport rep;
// model initialization
rbfcreate(2, 1, model0);
rbfsetpoints(model0, xy);
rbfsetalgohierarchical(model0, 1.0, 3, 0.0);
rbfbuildmodel(model0, rep);
_TestResult = _TestResult && doc_test_int(rep.terminationtype, 1);
//
// Serialization - it looks easy,
// but you should carefully read next section.
//
alglib::rbfserialize(model0, s);
alglib::rbfunserialize(s, model1);
// both models return same value
v = rbfcalc2(model0, 0.0, 0.0);
_TestResult = _TestResult && doc_test_real(v, 2.500, 0.005);
v = rbfcalc2(model1, 0.0, 0.0);
_TestResult = _TestResult && doc_test_real(v, 2.500, 0.005);
//
// Previous section shows that model state is saved/restored during
// serialization. However, some properties are NOT serialized.
//
// Serialization saves/restores RBF model, but it does NOT saves/restores
// settings which were used to build current model. In particular, dataset
// which was used to build model, is not preserved.
//
// What does it mean in for us?
//
// Do you remember this sequence: rbfcreate-rbfsetpoints-rbfbuildmodel?
// First step creates model, second step adds dataset and tunes model
// settings, third step builds model using current dataset and model
// construction settings.
//
// If you call rbfbuildmodel() without calling rbfsetpoints() first, you
// will get empty (zero) RBF model. In our example, model0 contains
// dataset which was added by rbfsetpoints() call. However, model1 does
// NOT contain dataset - because dataset is NOT serialized.
//
// This, if we call rbfbuildmodel(model0,rep), we will get same model,
// which returns 2.5 at (x,y)=(0,0). However, after same call model1 will
// return zero - because it contains RBF model (coefficients), but does NOT
// contain dataset which was used to build this model.
//
// Basically, it means that:
// * serialization of the RBF model preserves anything related to the model
// EVALUATION
// * but it does NOT creates perfect copy of the original object.
//
rbfbuildmodel(model0, rep);
v = rbfcalc2(model0, 0.0, 0.0);
_TestResult = _TestResult && doc_test_real(v, 2.500, 0.005);
rbfbuildmodel(model1, rep);
v = rbfcalc2(model1, 0.0, 0.0);
_TestResult = _TestResult && doc_test_real(v, 0.000, 0.005);
_TestResult = _TestResult && (_spoil_scenario==-1);
}
catch(ap_error)
{ _TestResult = _TestResult && (_spoil_scenario!=-1); }
}
if( !_TestResult)
{
printf("%-32s FAILED\n", "rbf_d_serialize");
fflush(stdout);
}
_TotalResult = _TotalResult && _TestResult;
//
// TEST fft_complex_d1
// Complex FFT: simple example
//
_TestResult = true;
for(_spoil_scenario=-1; _spoil_scenario<3; _spoil_scenario++)
{
try
{
//
// first we demonstrate forward FFT:
// [1i,1i,1i,1i] is converted to [4i, 0, 0, 0]
//
complex_1d_array z = "[1i,1i,1i,1i]";
if( _spoil_scenario==0 )
spoil_vector_by_nan(z);
if( _spoil_scenario==1 )
spoil_vector_by_posinf(z);
if( _spoil_scenario==2 )
spoil_vector_by_neginf(z);
fftc1d(z);
_TestResult = _TestResult && doc_test_complex_vector(z, "[4i,0,0,0]", 0.0001);
//
// now we convert [4i, 0, 0, 0] back to [1i,1i,1i,1i]
// with backward FFT
//
fftc1dinv(z);
_TestResult = _TestResult && doc_test_complex_vector(z, "[1i,1i,1i,1i]", 0.0001);
_TestResult = _TestResult && (_spoil_scenario==-1);
}
catch(ap_error)
{ _TestResult = _TestResult && (_spoil_scenario!=-1); }
}
if( !_TestResult)
{
printf("%-32s FAILED\n", "fft_complex_d1");
fflush(stdout);
}
_TotalResult = _TotalResult && _TestResult;
//
// TEST fft_complex_d2
// Complex FFT: advanced example
//
_TestResult = true;
for(_spoil_scenario=-1; _spoil_scenario<3; _spoil_scenario++)
{
try
{
//
// first we demonstrate forward FFT:
// [0,1,0,1i] is converted to [1+1i, -1-1i, -1-1i, 1+1i]
//
complex_1d_array z = "[0,1,0,1i]";
if( _spoil_scenario==0 )
spoil_vector_by_nan(z);
if( _spoil_scenario==1 )
spoil_vector_by_posinf(z);
if( _spoil_scenario==2 )
spoil_vector_by_neginf(z);
fftc1d(z);
_TestResult = _TestResult && doc_test_complex_vector(z, "[1+1i, -1-1i, -1-1i, 1+1i]", 0.0001);
//
// now we convert result back with backward FFT
//
fftc1dinv(z);
_TestResult = _TestResult && doc_test_complex_vector(z, "[0,1,0,1i]", 0.0001);
_TestResult = _TestResult && (_spoil_scenario==-1);
}
catch(ap_error)
{ _TestResult = _TestResult && (_spoil_scenario!=-1); }
}
if( !_TestResult)
{
printf("%-32s FAILED\n", "fft_complex_d2");
fflush(stdout);
}
_TotalResult = _TotalResult && _TestResult;
//
// TEST fft_real_d1
// Real FFT: simple example
//
_TestResult = true;
for(_spoil_scenario=-1; _spoil_scenario<3; _spoil_scenario++)
{
try
{
//
// first we demonstrate forward FFT:
// [1,1,1,1] is converted to [4, 0, 0, 0]
//
real_1d_array x = "[1,1,1,1]";
if( _spoil_scenario==0 )
spoil_vector_by_nan(x);
if( _spoil_scenario==1 )
spoil_vector_by_posinf(x);
if( _spoil_scenario==2 )
spoil_vector_by_neginf(x);
complex_1d_array f;
real_1d_array x2;
fftr1d(x, f);
_TestResult = _TestResult && doc_test_complex_vector(f, "[4,0,0,0]", 0.0001);
//
// now we convert [4, 0, 0, 0] back to [1,1,1,1]
// with backward FFT
//
fftr1dinv(f, x2);
_TestResult = _TestResult && doc_test_real_vector(x2, "[1,1,1,1]", 0.0001);
_TestResult = _TestResult && (_spoil_scenario==-1);
}
catch(ap_error)
{ _TestResult = _TestResult && (_spoil_scenario!=-1); }
}
if( !_TestResult)
{
printf("%-32s FAILED\n", "fft_real_d1");
fflush(stdout);
}
_TotalResult = _TotalResult && _TestResult;
//
// TEST fft_real_d2
// Real FFT: advanced example
//
_TestResult = true;
for(_spoil_scenario=-1; _spoil_scenario<3; _spoil_scenario++)
{
try
{
//
// first we demonstrate forward FFT:
// [1,2,3,4] is converted to [10, -2+2i, -2, -2-2i]
//
// note that output array is self-adjoint:
// * f[0] = conj(f[0])
// * f[1] = conj(f[3])
// * f[2] = conj(f[2])
//
real_1d_array x = "[1,2,3,4]";
if( _spoil_scenario==0 )
spoil_vector_by_nan(x);
if( _spoil_scenario==1 )
spoil_vector_by_posinf(x);
if( _spoil_scenario==2 )
spoil_vector_by_neginf(x);
complex_1d_array f;
real_1d_array x2;
fftr1d(x, f);
_TestResult = _TestResult && doc_test_complex_vector(f, "[10, -2+2i, -2, -2-2i]", 0.0001);
//
// now we convert [10, -2+2i, -2, -2-2i] back to [1,2,3,4]
//
fftr1dinv(f, x2);
_TestResult = _TestResult && doc_test_real_vector(x2, "[1,2,3,4]", 0.0001);
//
// remember that F is self-adjoint? It means that we can pass just half
// (slightly larger than half) of F to inverse real FFT and still get our result.
//
// I.e. instead [10, -2+2i, -2, -2-2i] we pass just [10, -2+2i, -2] and everything works!
//
// NOTE: in this case we should explicitly pass array length (which is 4) to ALGLIB;
// if not, it will automatically use array length to determine FFT size and
// will erroneously make half-length FFT.
//
f = "[10, -2+2i, -2]";
fftr1dinv(f, 4, x2);
_TestResult = _TestResult && doc_test_real_vector(x2, "[1,2,3,4]", 0.0001);
_TestResult = _TestResult && (_spoil_scenario==-1);
}
catch(ap_error)
{ _TestResult = _TestResult && (_spoil_scenario!=-1); }
}
if( !_TestResult)
{
printf("%-32s FAILED\n", "fft_real_d2");
fflush(stdout);
}
_TotalResult = _TotalResult && _TestResult;
//
// TEST fft_complex_e1
// error detection in backward FFT
//
_TestResult = true;
for(_spoil_scenario=-1; _spoil_scenario<3; _spoil_scenario++)
{
try
{
complex_1d_array z = "[0,2,0,-2]";
if( _spoil_scenario==0 )
spoil_vector_by_nan(z);
if( _spoil_scenario==1 )
spoil_vector_by_posinf(z);
if( _spoil_scenario==2 )
spoil_vector_by_neginf(z);
fftc1dinv(z);
_TestResult = _TestResult && doc_test_complex_vector(z, "[0,1i,0,-1i]", 0.0001);
_TestResult = _TestResult && (_spoil_scenario==-1);
}
catch(ap_error)
{ _TestResult = _TestResult && (_spoil_scenario!=-1); }
}
if( !_TestResult)
{
printf("%-32s FAILED\n", "fft_complex_e1");
fflush(stdout);
}
_TotalResult = _TotalResult && _TestResult;
//
// TEST clst_ahc
// Simple hierarchical clusterization with Euclidean distance function
//
_TestResult = true;
for(_spoil_scenario=-1; _spoil_scenario<3; _spoil_scenario++)
{
try
{
//
// The very simple clusterization example
//
// We have a set of points in 2D space:
// (P0,P1,P2,P3,P4) = ((1,1),(1,2),(4,1),(2,3),(4,1.5))
//
// |
// | P3
// |
// | P1
// | P4
// | P0 P2
// |-------------------------
//
// We want to perform Agglomerative Hierarchic Clusterization (AHC),
// using complete linkage (default algorithm) and Euclidean distance
// (default metric).
//
// In order to do that, we:
// * create clusterizer with clusterizercreate()
// * set points XY and metric (2=Euclidean) with clusterizersetpoints()
// * run AHC algorithm with clusterizerrunahc
//
// You may see that clusterization itself is a minor part of the example,
// most of which is dominated by comments :)
//
clusterizerstate s;
ahcreport rep;
real_2d_array xy = "[[1,1],[1,2],[4,1],[2,3],[4,1.5]]";
if( _spoil_scenario==0 )
spoil_matrix_by_nan(xy);
if( _spoil_scenario==1 )
spoil_matrix_by_posinf(xy);
if( _spoil_scenario==2 )
spoil_matrix_by_neginf(xy);
clusterizercreate(s);
clusterizersetpoints(s, xy, 2);
clusterizerrunahc(s, rep);
//
// Now we've built our clusterization tree. Rep.z contains information which
// is required to build dendrogram. I-th row of rep.z represents one merge
// operation, with first cluster to merge having index rep.z[I,0] and second
// one having index rep.z[I,1]. Merge result has index NPoints+I.
//
// Clusters with indexes less than NPoints are single-point initial clusters,
// while ones with indexes from NPoints to 2*NPoints-2 are multi-point
// clusters created during merges.
//
// In our example, Z=[[2,4], [0,1], [3,6], [5,7]]
//
// It means that:
// * first, we merge C2=(P2) and C4=(P4), and create C5=(P2,P4)
// * then, we merge C2=(P0) and C1=(P1), and create C6=(P0,P1)
// * then, we merge C3=(P3) and C6=(P0,P1), and create C7=(P0,P1,P3)
// * finally, we merge C5 and C7 and create C8=(P0,P1,P2,P3,P4)
//
// Thus, we have following dendrogram:
//
// ------8-----
// | |
// | ----7----
// | | |
// ---5--- | ---6---
// | | | | |
// P2 P4 P3 P0 P1
//
_TestResult = _TestResult && doc_test_int_matrix(rep.z, "[[2,4],[0,1],[3,6],[5,7]]");
//
// We've built dendrogram above by reordering our dataset.
//
// Without such reordering it would be impossible to build dendrogram without
// intersections. Luckily, ahcreport structure contains two additional fields
// which help to build dendrogram from your data:
// * rep.p, which contains permutation applied to dataset
// * rep.pm, which contains another representation of merges
//
// In our example we have:
// * P=[3,4,0,2,1]
// * PZ=[[0,0,1,1,0,0],[3,3,4,4,0,0],[2,2,3,4,0,1],[0,1,2,4,1,2]]
//
// Permutation array P tells us that P0 should be moved to position 3,
// P1 moved to position 4, P2 moved to position 0 and so on:
//
// (P0 P1 P2 P3 P4) => (P2 P4 P3 P0 P1)
//
// Merges array PZ tells us how to perform merges on the sorted dataset.
// One row of PZ corresponds to one merge operations, with first pair of
// elements denoting first of the clusters to merge (start index, end
// index) and next pair of elements denoting second of the clusters to
// merge. Clusters being merged are always adjacent, with first one on
// the left and second one on the right.
//
// For example, first row of PZ tells us that clusters [0,0] and [1,1] are
// merged (single-point clusters, with first one containing P2 and second
// one containing P4). Third row of PZ tells us that we merge one single-
// point cluster [2,2] with one two-point cluster [3,4].
//
// There are two more elements in each row of PZ. These are the helper
// elements, which denote HEIGHT (not size) of left and right subdendrograms.
// For example, according to PZ, first two merges are performed on clusterization
// trees of height 0, while next two merges are performed on 0-1 and 1-2
// pairs of trees correspondingly.
//
_TestResult = _TestResult && doc_test_int_vector(rep.p, "[3,4,0,2,1]");
_TestResult = _TestResult && doc_test_int_matrix(rep.pm, "[[0,0,1,1,0,0],[3,3,4,4,0,0],[2,2,3,4,0,1],[0,1,2,4,1,2]]");
_TestResult = _TestResult && (_spoil_scenario==-1);
}
catch(ap_error)
{ _TestResult = _TestResult && (_spoil_scenario!=-1); }
}
if( !_TestResult)
{
printf("%-32s FAILED\n", "clst_ahc");
fflush(stdout);
}
_TotalResult = _TotalResult && _TestResult;
//
// TEST clst_kmeans
// Simple k-means clusterization
//
_TestResult = true;
for(_spoil_scenario=-1; _spoil_scenario<3; _spoil_scenario++)
{
try
{
//
// The very simple clusterization example
//
// We have a set of points in 2D space:
// (P0,P1,P2,P3,P4) = ((1,1),(1,2),(4,1),(2,3),(4,1.5))
//
// |
// | P3
// |
// | P1
// | P4
// | P0 P2
// |-------------------------
//
// We want to perform k-means++ clustering with K=2.
//
// In order to do that, we:
// * create clusterizer with clusterizercreate()
// * set points XY and metric (must be Euclidean, distype=2) with clusterizersetpoints()
// * (optional) set number of restarts from random positions to 5
// * run k-means algorithm with clusterizerrunkmeans()
//
// You may see that clusterization itself is a minor part of the example,
// most of which is dominated by comments :)
//
clusterizerstate s;
kmeansreport rep;
real_2d_array xy = "[[1,1],[1,2],[4,1],[2,3],[4,1.5]]";
if( _spoil_scenario==0 )
spoil_matrix_by_nan(xy);
if( _spoil_scenario==1 )
spoil_matrix_by_posinf(xy);
if( _spoil_scenario==2 )
spoil_matrix_by_neginf(xy);
clusterizercreate(s);
clusterizersetpoints(s, xy, 2);
clusterizersetkmeanslimits(s, 5, 0);
clusterizerrunkmeans(s, 2, rep);
//
// We've performed clusterization, and it succeeded (completion code is +1).
//
// Now first center is stored in the first row of rep.c, second one is stored
// in the second row. rep.cidx can be used to determine which center is
// closest to some specific point of the dataset.
//
_TestResult = _TestResult && doc_test_int(rep.terminationtype, 1);
// We called clusterizersetpoints() with disttype=2 because k-means++
// algorithm does NOT support metrics other than Euclidean. But what if we
// try to use some other metric?
//
// We change metric type by calling clusterizersetpoints() one more time,
// and try to run k-means algo again. It fails.
//
clusterizersetpoints(s, xy, 0);
clusterizerrunkmeans(s, 2, rep);
_TestResult = _TestResult && doc_test_int(rep.terminationtype, -5);
_TestResult = _TestResult && (_spoil_scenario==-1);
}
catch(ap_error)
{ _TestResult = _TestResult && (_spoil_scenario!=-1); }
}
if( !_TestResult)
{
printf("%-32s FAILED\n", "clst_kmeans");
fflush(stdout);
}
_TotalResult = _TotalResult && _TestResult;
//
// TEST clst_linkage
// Clusterization with different linkage types
//
_TestResult = true;
for(_spoil_scenario=-1; _spoil_scenario<3; _spoil_scenario++)
{
try
{
//
// We have a set of points in 1D space:
// (P0,P1,P2,P3,P4) = (1, 3, 10, 16, 20)
//
// We want to perform Agglomerative Hierarchic Clusterization (AHC),
// using either complete or single linkage and Euclidean distance
// (default metric).
//
// First two steps merge P0/P1 and P3/P4 independently of the linkage type.
// However, third step depends on linkage type being used:
// * in case of complete linkage P2=10 is merged with [P0,P1]
// * in case of single linkage P2=10 is merged with [P3,P4]
//
clusterizerstate s;
ahcreport rep;
real_2d_array xy = "[[1],[3],[10],[16],[20]]";
if( _spoil_scenario==0 )
spoil_matrix_by_nan(xy);
if( _spoil_scenario==1 )
spoil_matrix_by_posinf(xy);
if( _spoil_scenario==2 )
spoil_matrix_by_neginf(xy);
integer_1d_array cidx;
integer_1d_array cz;
clusterizercreate(s);
clusterizersetpoints(s, xy, 2);
// use complete linkage, reduce set down to 2 clusters.
// print clusterization with clusterizergetkclusters(2).
// P2 must belong to [P0,P1]
clusterizersetahcalgo(s, 0);
clusterizerrunahc(s, rep);
clusterizergetkclusters(rep, 2, cidx, cz);
_TestResult = _TestResult && doc_test_int_vector(cidx, "[1,1,1,0,0]");
// use single linkage, reduce set down to 2 clusters.
// print clusterization with clusterizergetkclusters(2).
// P2 must belong to [P2,P3]
clusterizersetahcalgo(s, 1);
clusterizerrunahc(s, rep);
clusterizergetkclusters(rep, 2, cidx, cz);
_TestResult = _TestResult && doc_test_int_vector(cidx, "[0,0,1,1,1]");
_TestResult = _TestResult && (_spoil_scenario==-1);
}
catch(ap_error)
{ _TestResult = _TestResult && (_spoil_scenario!=-1); }
}
if( !_TestResult)
{
printf("%-32s FAILED\n", "clst_linkage");
fflush(stdout);
}
_TotalResult = _TotalResult && _TestResult;
//
// TEST clst_distance
// Clusterization with different metric types
//
_TestResult = true;
for(_spoil_scenario=-1; _spoil_scenario<3; _spoil_scenario++)
{
try
{
//
// We have three points in 4D space:
// (P0,P1,P2) = ((1, 2, 1, 2), (6, 7, 6, 7), (7, 6, 7, 6))
//
// We want to try clustering them with different distance functions.
// Distance function is chosen when we add dataset to the clusterizer.
// We can choose several distance types - Euclidean, city block, Chebyshev,
// several correlation measures or user-supplied distance matrix.
//
// Here we'll try three distances: Euclidean, Pearson correlation,
// user-supplied distance matrix. Different distance functions lead
// to different choices being made by algorithm during clustering.
//
clusterizerstate s;
ahcreport rep;
ae_int_t disttype;
real_2d_array xy = "[[1, 2, 1, 2], [6, 7, 6, 7], [7, 6, 7, 6]]";
if( _spoil_scenario==0 )
spoil_matrix_by_nan(xy);
if( _spoil_scenario==1 )
spoil_matrix_by_posinf(xy);
if( _spoil_scenario==2 )
spoil_matrix_by_neginf(xy);
clusterizercreate(s);
// With Euclidean distance function (disttype=2) two closest points
// are P1 and P2, thus:
// * first, we merge P1 and P2 to form C3=[P1,P2]
// * second, we merge P0 and C3 to form C4=[P0,P1,P2]
disttype = 2;
clusterizersetpoints(s, xy, disttype);
clusterizerrunahc(s, rep);
_TestResult = _TestResult && doc_test_int_matrix(rep.z, "[[1,2],[0,3]]");
// With Pearson correlation distance function (disttype=10) situation
// is different - distance between P0 and P1 is zero, thus:
// * first, we merge P0 and P1 to form C3=[P0,P1]
// * second, we merge P2 and C3 to form C4=[P0,P1,P2]
disttype = 10;
clusterizersetpoints(s, xy, disttype);
clusterizerrunahc(s, rep);
_TestResult = _TestResult && doc_test_int_matrix(rep.z, "[[0,1],[2,3]]");
// Finally, we try clustering with user-supplied distance matrix:
// [ 0 3 1 ]
// P = [ 3 0 3 ], where P[i,j] = dist(Pi,Pj)
// [ 1 3 0 ]
//
// * first, we merge P0 and P2 to form C3=[P0,P2]
// * second, we merge P1 and C3 to form C4=[P0,P1,P2]
real_2d_array d = "[[0,3,1],[3,0,3],[1,3,0]]";
clusterizersetdistances(s, d, true);
clusterizerrunahc(s, rep);
_TestResult = _TestResult && doc_test_int_matrix(rep.z, "[[0,2],[1,3]]");
_TestResult = _TestResult && (_spoil_scenario==-1);
}
catch(ap_error)
{ _TestResult = _TestResult && (_spoil_scenario!=-1); }
}
if( !_TestResult)
{
printf("%-32s FAILED\n", "clst_distance");
fflush(stdout);
}
_TotalResult = _TotalResult && _TestResult;
//
// TEST clst_kclusters
// Obtaining K top clusters from clusterization tree
//
_TestResult = true;
for(_spoil_scenario=-1; _spoil_scenario<3; _spoil_scenario++)
{
try
{
//
// We have a set of points in 2D space:
// (P0,P1,P2,P3,P4) = ((1,1),(1,2),(4,1),(2,3),(4,1.5))
//
// |
// | P3
// |
// | P1
// | P4
// | P0 P2
// |-------------------------
//
// We perform Agglomerative Hierarchic Clusterization (AHC) and we want
// to get top K clusters from clusterization tree for different K.
//
clusterizerstate s;
ahcreport rep;
real_2d_array xy = "[[1,1],[1,2],[4,1],[2,3],[4,1.5]]";
if( _spoil_scenario==0 )
spoil_matrix_by_nan(xy);
if( _spoil_scenario==1 )
spoil_matrix_by_posinf(xy);
if( _spoil_scenario==2 )
spoil_matrix_by_neginf(xy);
integer_1d_array cidx;
integer_1d_array cz;
clusterizercreate(s);
clusterizersetpoints(s, xy, 2);
clusterizerrunahc(s, rep);
// with K=5, every points is assigned to its own cluster:
// C0=P0, C1=P1 and so on...
clusterizergetkclusters(rep, 5, cidx, cz);
_TestResult = _TestResult && doc_test_int_vector(cidx, "[0,1,2,3,4]");
// with K=1 we have one large cluster C0=[P0,P1,P2,P3,P4,P5]
clusterizergetkclusters(rep, 1, cidx, cz);
_TestResult = _TestResult && doc_test_int_vector(cidx, "[0,0,0,0,0]");
// with K=3 we have three clusters C0=[P3], C1=[P2,P4], C2=[P0,P1]
clusterizergetkclusters(rep, 3, cidx, cz);
_TestResult = _TestResult && doc_test_int_vector(cidx, "[2,2,1,0,1]");
_TestResult = _TestResult && (_spoil_scenario==-1);
}
catch(ap_error)
{ _TestResult = _TestResult && (_spoil_scenario!=-1); }
}
if( !_TestResult)
{
printf("%-32s FAILED\n", "clst_kclusters");
fflush(stdout);
}
_TotalResult = _TotalResult && _TestResult;
//
// TEST randomforest_cls
// Simple classification with random forests
//
_TestResult = true;
for(_spoil_scenario=-1; _spoil_scenario<3; _spoil_scenario++)
{
try
{
//
// The very simple classification example: classify points (x,y) in 2D space
// as ones with x>=0 and ones with x<0 (y is ignored, but our classifier
// has to find out it).
//
// First, we have to create decision forest builder object, load dataset and
// specify training settings. Our dataset is specified as matrix, which has
// following format:
//
// x0 y0 class0
// x1 y1 class1
// x2 y2 class2
// ....
//
// Here xi and yi can be any values (and in fact you can have any number of
// independent variables), and classi MUST be integer number in [0,NClasses)
// range. In our example we denote points with x>=0 as class #0, and
// ones with negative xi as class #1.
//
// NOTE: if you want to solve regression problem, specify NClasses=1. In
// this case last column of xy can be any numeric value.
//
// For the sake of simplicity, our example includes only 4-point dataset.
// However, random forests are able to cope with extremely large datasets
// having millions of examples.
//
decisionforestbuilder builder;
ae_int_t nvars = 2;
ae_int_t nclasses = 2;
ae_int_t npoints = 4;
real_2d_array xy = "[[1,1,0],[1,-1,0],[-1,1,1],[-1,-1,1]]";
if( _spoil_scenario==0 )
spoil_matrix_by_nan(xy);
if( _spoil_scenario==1 )
spoil_matrix_by_posinf(xy);
if( _spoil_scenario==2 )
spoil_matrix_by_neginf(xy);
dfbuildercreate(builder);
dfbuildersetdataset(builder, xy, npoints, nvars, nclasses);
// in our example we train decision forest using full sample - it allows us
// to get zero classification error. However, in practical applications smaller
// values are used: 50%, 25%, 5% or even less.
dfbuildersetsubsampleratio(builder, 1.0);
// we train random forest with just one tree; again, in real life situations
// you typically need from 50 to 500 trees.
ae_int_t ntrees = 1;
decisionforest forest;
dfreport rep;
dfbuilderbuildrandomforest(builder, ntrees, forest, rep);
// with such settings (100% of the training set is used) you can expect
// zero classification error. Beautiful results, but remember - in real life
// you do not need zero TRAINING SET error, you need good generalization.
_TestResult = _TestResult && doc_test_real(rep.relclserror, 0.0000, 0.00005);
// now, let's perform some simple processing with dfprocess()
real_1d_array x = "[+1,0]";
real_1d_array y = "[]";
dfprocess(forest, x, y);
_TestResult = _TestResult && doc_test_real_vector(y, "[+1,0]", 0.0005);
// another option is to use dfprocess0() which returns just first component
// of the output vector y. ideal for regression problems and binary classifiers.
double y0;
y0 = dfprocess0(forest, x);
_TestResult = _TestResult && doc_test_real(y0, 1.000, 0.0005);
// finally, you can use dfclassify() which returns most probable class index (i.e. argmax y[i]).
ae_int_t i;
i = dfclassify(forest, x);
_TestResult = _TestResult && doc_test_int(i, 0);
_TestResult = _TestResult && (_spoil_scenario==-1);
}
catch(ap_error)
{ _TestResult = _TestResult && (_spoil_scenario!=-1); }
}
if( !_TestResult)
{
printf("%-32s FAILED\n", "randomforest_cls");
fflush(stdout);
}
_TotalResult = _TotalResult && _TestResult;
//
// TEST randomforest_reg
// Simple regression with decision forest
//
_TestResult = true;
for(_spoil_scenario=-1; _spoil_scenario<3; _spoil_scenario++)
{
try
{
//
// The very simple regression example: model f(x,y)=x+y
//
// First, we have to create DF builder object, load dataset and specify
// training settings. Our dataset is specified as matrix, which has following
// format:
//
// x0 y0 f0
// x1 y1 f1
// x2 y2 f2
// ....
//
// Here xi and yi can be any values, and fi is a dependent function value.
//
// NOTE: you can also solve classification problems with DF models, see
// another example for this unit.
//
decisionforestbuilder builder;
ae_int_t nvars = 2;
ae_int_t nclasses = 1;
ae_int_t npoints = 4;
real_2d_array xy = "[[1,1,+2],[1,-1,0],[-1,1,0],[-1,-1,-2]]";
if( _spoil_scenario==0 )
spoil_matrix_by_nan(xy);
if( _spoil_scenario==1 )
spoil_matrix_by_posinf(xy);
if( _spoil_scenario==2 )
spoil_matrix_by_neginf(xy);
dfbuildercreate(builder);
dfbuildersetdataset(builder, xy, npoints, nvars, nclasses);
// in our example we train decision forest using full sample - it allows us
// to get zero classification error. However, in practical applications smaller
// values are used: 50%, 25%, 5% or even less.
dfbuildersetsubsampleratio(builder, 1.0);
// we train random forest with just one tree; again, in real life situations
// you typically need from 50 to 500 trees.
ae_int_t ntrees = 1;
decisionforest model;
dfreport rep;
dfbuilderbuildrandomforest(builder, ntrees, model, rep);
// with such settings (full sample is used) you can expect zero RMS error on the
// training set. Beautiful results, but remember - in real life you do not
// need zero TRAINING SET error, you need good generalization.
_TestResult = _TestResult && doc_test_real(rep.rmserror, 0.0000, 0.00005);
// now, let's perform some simple processing with dfprocess()
real_1d_array x = "[+1,+1]";
real_1d_array y = "[]";
dfprocess(model, x, y);
_TestResult = _TestResult && doc_test_real_vector(y, "[+2]", 0.0005);
// another option is to use dfprocess0() which returns just first component
// of the output vector y. ideal for regression problems and binary classifiers.
double y0;
y0 = dfprocess0(model, x);
_TestResult = _TestResult && doc_test_real(y0, 2.000, 0.0005);
// there also exist another convenience function, dfclassify(),
// but it does not work for regression problems - it always returns -1.
ae_int_t i;
i = dfclassify(model, x);
_TestResult = _TestResult && doc_test_int(i, -1);
_TestResult = _TestResult && (_spoil_scenario==-1);
}
catch(ap_error)
{ _TestResult = _TestResult && (_spoil_scenario!=-1); }
}
if( !_TestResult)
{
printf("%-32s FAILED\n", "randomforest_reg");
fflush(stdout);
}
_TotalResult = _TotalResult && _TestResult;
//
// TEST linreg_d_basic
// Linear regression used to build the very basic model and unpack coefficients
//
_TestResult = true;
try
{
//
// In this example we demonstrate linear fitting by f(x|a) = a*exp(0.5*x).
//
// We have:
// * xy - matrix of basic function values (exp(0.5*x)) and expected values
//
real_2d_array xy = "[[0.606531,1.133719],[0.670320,1.306522],[0.740818,1.504604],[0.818731,1.554663],[0.904837,1.884638],[1.000000,2.072436],[1.105171,2.257285],[1.221403,2.534068],[1.349859,2.622017],[1.491825,2.897713],[1.648721,3.219371]]";
ae_int_t nvars;
linearmodel model;
lrreport rep;
real_1d_array c;
lrbuildz(xy, 11, 1, model, rep);
lrunpack(model, c, nvars);
_TestResult = _TestResult && doc_test_real_vector(c, "[1.98650,0.00000]", 0.00005);
}
catch(ap_error)
{ _TestResult = false; }
if( !_TestResult)
{
printf("%-32s FAILED\n", "linreg_d_basic");
fflush(stdout);
}
_TotalResult = _TotalResult && _TestResult;
//
// TEST filters_d_sma
// SMA(k) filter
//
_TestResult = true;
for(_spoil_scenario=-1; _spoil_scenario<3; _spoil_scenario++)
{
try
{
//
// Here we demonstrate SMA(k) filtering for time series.
//
real_1d_array x = "[5,6,7,8]";
if( _spoil_scenario==0 )
spoil_vector_by_nan(x);
if( _spoil_scenario==1 )
spoil_vector_by_posinf(x);
if( _spoil_scenario==2 )
spoil_vector_by_neginf(x);
//
// Apply filter.
// We should get [5, 5.5, 6.5, 7.5] as result
//
filtersma(x, 2);
_TestResult = _TestResult && doc_test_real_vector(x, "[5,5.5,6.5,7.5]", 0.00005);
_TestResult = _TestResult && (_spoil_scenario==-1);
}
catch(ap_error)
{ _TestResult = _TestResult && (_spoil_scenario!=-1); }
}
if( !_TestResult)
{
printf("%-32s FAILED\n", "filters_d_sma");
fflush(stdout);
}
_TotalResult = _TotalResult && _TestResult;
//
// TEST filters_d_ema
// EMA(alpha) filter
//
_TestResult = true;
for(_spoil_scenario=-1; _spoil_scenario<3; _spoil_scenario++)
{
try
{
//
// Here we demonstrate EMA(0.5) filtering for time series.
//
real_1d_array x = "[5,6,7,8]";
if( _spoil_scenario==0 )
spoil_vector_by_nan(x);
if( _spoil_scenario==1 )
spoil_vector_by_posinf(x);
if( _spoil_scenario==2 )
spoil_vector_by_neginf(x);
//
// Apply filter.
// We should get [5, 5.5, 6.25, 7.125] as result
//
filterema(x, 0.5);
_TestResult = _TestResult && doc_test_real_vector(x, "[5,5.5,6.25,7.125]", 0.00005);
_TestResult = _TestResult && (_spoil_scenario==-1);
}
catch(ap_error)
{ _TestResult = _TestResult && (_spoil_scenario!=-1); }
}
if( !_TestResult)
{
printf("%-32s FAILED\n", "filters_d_ema");
fflush(stdout);
}
_TotalResult = _TotalResult && _TestResult;
//
// TEST filters_d_lrma
// LRMA(k) filter
//
_TestResult = true;
for(_spoil_scenario=-1; _spoil_scenario<3; _spoil_scenario++)
{
try
{
//
// Here we demonstrate LRMA(3) filtering for time series.
//
real_1d_array x = "[7,8,8,9,12,12]";
if( _spoil_scenario==0 )
spoil_vector_by_nan(x);
if( _spoil_scenario==1 )
spoil_vector_by_posinf(x);
if( _spoil_scenario==2 )
spoil_vector_by_neginf(x);
//
// Apply filter.
// We should get [7.0000, 8.0000, 8.1667, 8.8333, 11.6667, 12.5000] as result
//
filterlrma(x, 3);
_TestResult = _TestResult && doc_test_real_vector(x, "[7.0000,8.0000,8.1667,8.8333,11.6667,12.5000]", 0.00005);
_TestResult = _TestResult && (_spoil_scenario==-1);
}
catch(ap_error)
{ _TestResult = _TestResult && (_spoil_scenario!=-1); }
}
if( !_TestResult)
{
printf("%-32s FAILED\n", "filters_d_lrma");
fflush(stdout);
}
_TotalResult = _TotalResult && _TestResult;
//
// TEST ssa_d_basic
// Simple SSA analysis demo
//
printf("150/165\n");
_TestResult = true;
for(_spoil_scenario=-1; _spoil_scenario<3; _spoil_scenario++)
{
try
{
//
// Here we demonstrate SSA trend/noise separation for some toy problem:
// small monotonically growing series X are analyzed with 3-tick window
// and "top-K" version of SSA, which selects K largest singular vectors
// for analysis, with K=1.
//
ssamodel s;
real_1d_array x = "[0,0.5,1,1,1.5,2]";
if( _spoil_scenario==0 )
spoil_vector_by_nan(x);
if( _spoil_scenario==1 )
spoil_vector_by_posinf(x);
if( _spoil_scenario==2 )
spoil_vector_by_neginf(x);
//
// First, we create SSA model, set its properties and add dataset.
//
// We use window with width=3 and configure model to use direct SSA
// algorithm - one which runs exact O(N*W^2) analysis - to extract
// one top singular vector. Well, it is toy problem :)
//
// NOTE: SSA model may store and analyze more than one sequence
// (say, different sequences may correspond to data collected
// from different devices)
//
ssacreate(s);
ssasetwindow(s, 3);
ssaaddsequence(s, x);
ssasetalgotopkdirect(s, 1);
//
// Now we begin analysis. Internally SSA model stores everything it needs:
// data, settings, solvers and so on. Right after first call to analysis-
// related function it will analyze dataset, build basis and perform analysis.
//
// Subsequent calls to analysis functions will reuse previously computed
// basis, unless you invalidate it by changing model settings (or dataset).
//
real_1d_array trend;
real_1d_array noise;
ssaanalyzesequence(s, x, trend, noise);
_TestResult = _TestResult && doc_test_real_vector(trend, "[0.3815,0.5582,0.7810,1.0794,1.5041,2.0105]", 0.005);
_TestResult = _TestResult && (_spoil_scenario==-1);
}
catch(ap_error)
{ _TestResult = _TestResult && (_spoil_scenario!=-1); }
}
if( !_TestResult)
{
printf("%-32s FAILED\n", "ssa_d_basic");
fflush(stdout);
}
_TotalResult = _TotalResult && _TestResult;
//
// TEST ssa_d_forecast
// Simple SSA forecasting demo
//
_TestResult = true;
for(_spoil_scenario=-1; _spoil_scenario<3; _spoil_scenario++)
{
try
{
//
// Here we demonstrate SSA forecasting on some toy problem with clearly
// visible linear trend and small amount of noise.
//
ssamodel s;
real_1d_array x = "[0.05,0.96,2.04,3.11,3.97,5.03,5.98,7.02,8.02]";
if( _spoil_scenario==0 )
spoil_vector_by_nan(x);
if( _spoil_scenario==1 )
spoil_vector_by_posinf(x);
if( _spoil_scenario==2 )
spoil_vector_by_neginf(x);
//
// First, we create SSA model, set its properties and add dataset.
//
// We use window with width=3 and configure model to use direct SSA
// algorithm - one which runs exact O(N*W^2) analysis - to extract
// two top singular vectors. Well, it is toy problem :)
//
// NOTE: SSA model may store and analyze more than one sequence
// (say, different sequences may correspond to data collected
// from different devices)
//
ssacreate(s);
ssasetwindow(s, 3);
ssaaddsequence(s, x);
ssasetalgotopkdirect(s, 2);
//
// Now we begin analysis. Internally SSA model stores everything it needs:
// data, settings, solvers and so on. Right after first call to analysis-
// related function it will analyze dataset, build basis and perform analysis.
//
// Subsequent calls to analysis functions will reuse previously computed
// basis, unless you invalidate it by changing model settings (or dataset).
//
// In this example we show how to use ssaforecastlast() function, which
// predicts changed in the last sequence of the dataset. If you want to
// perform prediction for some other sequence, use ssaforecastsequence().
//
real_1d_array trend;
ssaforecastlast(s, 3, trend);
//
// Well, we expected it to be [9,10,11]. There exists some difference,
// which can be explained by the artificial noise in the dataset.
//
_TestResult = _TestResult && doc_test_real_vector(trend, "[9.0005,9.9322,10.8051]", 0.005);
_TestResult = _TestResult && (_spoil_scenario==-1);
}
catch(ap_error)
{ _TestResult = _TestResult && (_spoil_scenario!=-1); }
}
if( !_TestResult)
{
printf("%-32s FAILED\n", "ssa_d_forecast");
fflush(stdout);
}
_TotalResult = _TotalResult && _TestResult;
//
// TEST ssa_d_realtime
// Real-time SSA algorithm with fast incremental updates
//
_TestResult = true;
for(_spoil_scenario=-1; _spoil_scenario<9; _spoil_scenario++)
{
try
{
//
// Suppose that you have a constant stream of incoming data, and you want
// to regularly perform singular spectral analysis of this stream.
//
// One full run of direct algorithm costs O(N*Width^2) operations, so
// the more points you have, the more it costs to rebuild basis from
// scratch.
//
// Luckily we have incremental SSA algorithm which can perform quick
// updates of already computed basis in O(K*Width^2) ops, where K
// is a number of singular vectors extracted. Usually it is orders of
// magnitude faster than full update of the basis.
//
// In this example we start from some initial dataset x0. Then we
// start appending elements one by one to the end of the last sequence.
//
// NOTE: direct algorithm also supports incremental updates, but
// with O(Width^3) cost. Typically K<<Width, so specialized
// incremental algorithm is still faster.
//
ssamodel s1;
real_2d_array a1;
real_1d_array sv1;
ae_int_t w;
ae_int_t k;
real_1d_array x0 = "[0.009,0.976,1.999,2.984,3.977,5.002]";
if( _spoil_scenario==0 )
spoil_vector_by_nan(x0);
if( _spoil_scenario==1 )
spoil_vector_by_posinf(x0);
if( _spoil_scenario==2 )
spoil_vector_by_neginf(x0);
ssacreate(s1);
ssasetwindow(s1, 3);
ssaaddsequence(s1, x0);
// set algorithm to the real-time version of top-K, K=2
ssasetalgotopkrealtime(s1, 2);
// one more interesting feature of the incremental algorithm is "power-up" cycle.
// even with incremental algorithm initial basis calculation costs O(N*Width^2) ops.
// if such startup cost is too high for your real-time app, then you may divide
// initial basis calculation across several model updates. It results in better
// latency at the price of somewhat lesser precision during first few updates.
ssasetpoweruplength(s1, 3);
// now, after we prepared everything, start to add incoming points one by one;
// in the real life, of course, we will perform some work between subsequent update
// (analyze something, predict, and so on).
//
// After each append we perform one iteration of the real-time solver. Usually
// one iteration is more than enough to update basis. If you have REALLY tight
// performance constraints, you may specify fractional amount of iterations,
// which means that iteration is performed with required probability.
double updateits = 1.0;
if( _spoil_scenario==3 )
updateits = fp_nan;
if( _spoil_scenario==4 )
updateits = fp_posinf;
if( _spoil_scenario==5 )
updateits = fp_neginf;
ssaappendpointandupdate(s1, 5.951, updateits);
ssagetbasis(s1, a1, sv1, w, k);
ssaappendpointandupdate(s1, 7.074, updateits);
ssagetbasis(s1, a1, sv1, w, k);
ssaappendpointandupdate(s1, 7.925, updateits);
ssagetbasis(s1, a1, sv1, w, k);
ssaappendpointandupdate(s1, 8.992, updateits);
ssagetbasis(s1, a1, sv1, w, k);
ssaappendpointandupdate(s1, 9.942, updateits);
ssagetbasis(s1, a1, sv1, w, k);
ssaappendpointandupdate(s1, 11.051, updateits);
ssagetbasis(s1, a1, sv1, w, k);
ssaappendpointandupdate(s1, 11.965, updateits);
ssagetbasis(s1, a1, sv1, w, k);
ssaappendpointandupdate(s1, 13.047, updateits);
ssagetbasis(s1, a1, sv1, w, k);
ssaappendpointandupdate(s1, 13.970, updateits);
ssagetbasis(s1, a1, sv1, w, k);
// Ok, we have our basis in a1[] and singular values at sv1[].
// But is it good enough? Let's print it.
_TestResult = _TestResult && doc_test_real_matrix(a1, "[[0.510607,0.753611],[0.575201,0.058445],[0.639081,-0.654717]]", 0.0005);
// Ok, two vectors with 3 components each.
// But how to understand that is it really good basis?
// Let's compare it with direct SSA algorithm on the entire sequence.
ssamodel s2;
real_2d_array a2;
real_1d_array sv2;
real_1d_array x2 = "[0.009,0.976,1.999,2.984,3.977,5.002,5.951,7.074,7.925,8.992,9.942,11.051,11.965,13.047,13.970]";
if( _spoil_scenario==6 )
spoil_vector_by_nan(x2);
if( _spoil_scenario==7 )
spoil_vector_by_posinf(x2);
if( _spoil_scenario==8 )
spoil_vector_by_neginf(x2);
ssacreate(s2);
ssasetwindow(s2, 3);
ssaaddsequence(s2, x2);
ssasetalgotopkdirect(s2, 2);
ssagetbasis(s2, a2, sv2, w, k);
// it is exactly the same as one calculated with incremental approach!
_TestResult = _TestResult && doc_test_real_matrix(a2, "[[0.510607,0.753611],[0.575201,0.058445],[0.639081,-0.654717]]", 0.0005);
_TestResult = _TestResult && (_spoil_scenario==-1);
}
catch(ap_error)
{ _TestResult = _TestResult && (_spoil_scenario!=-1); }
}
if( !_TestResult)
{
printf("%-32s FAILED\n", "ssa_d_realtime");
fflush(stdout);
}
_TotalResult = _TotalResult && _TestResult;
//
// TEST mcpd_simple1
// Simple unconstrained MCPD model (no entry/exit states)
//
_TestResult = true;
for(_spoil_scenario=-1; _spoil_scenario<6; _spoil_scenario++)
{
try
{
//
// The very simple MCPD example
//
// We have a loan portfolio. Our loans can be in one of two states:
// * normal loans ("good" ones)
// * past due loans ("bad" ones)
//
// We assume that:
// * loans can transition from any state to any other state. In
// particular, past due loan can become "good" one at any moment
// with same (fixed) probability. Not realistic, but it is toy example :)
// * portfolio size does not change over time
//
// Thus, we have following model
// state_new = P*state_old
// where
// ( p00 p01 )
// P = ( )
// ( p10 p11 )
//
// We want to model transitions between these two states using MCPD
// approach (Markov Chains for Proportional/Population Data), i.e.
// to restore hidden transition matrix P using actual portfolio data.
// We have:
// * poportional data, i.e. proportion of loans in the normal and past
// due states (not portfolio size measured in some currency, although
// it is possible to work with population data too)
// * two tracks, i.e. two sequences which describe portfolio
// evolution from two different starting states: [1,0] (all loans
// are "good") and [0.8,0.2] (only 80% of portfolio is in the "good"
// state)
//
mcpdstate s;
mcpdreport rep;
real_2d_array p;
real_2d_array track0 = "[[1.00000,0.00000],[0.95000,0.05000],[0.92750,0.07250],[0.91738,0.08263],[0.91282,0.08718]]";
if( _spoil_scenario==0 )
spoil_matrix_by_nan(track0);
if( _spoil_scenario==1 )
spoil_matrix_by_posinf(track0);
if( _spoil_scenario==2 )
spoil_matrix_by_neginf(track0);
real_2d_array track1 = "[[0.80000,0.20000],[0.86000,0.14000],[0.88700,0.11300],[0.89915,0.10085]]";
if( _spoil_scenario==3 )
spoil_matrix_by_nan(track1);
if( _spoil_scenario==4 )
spoil_matrix_by_posinf(track1);
if( _spoil_scenario==5 )
spoil_matrix_by_neginf(track1);
mcpdcreate(2, s);
mcpdaddtrack(s, track0);
mcpdaddtrack(s, track1);
mcpdsolve(s);
mcpdresults(s, p, rep);
//
// Hidden matrix P is equal to
// ( 0.95 0.50 )
// ( )
// ( 0.05 0.50 )
// which means that "good" loans can become "bad" with 5% probability,
// while "bad" loans will return to good state with 50% probability.
//
_TestResult = _TestResult && doc_test_real_matrix(p, "[[0.95,0.50],[0.05,0.50]]", 0.005);
_TestResult = _TestResult && (_spoil_scenario==-1);
}
catch(ap_error)
{ _TestResult = _TestResult && (_spoil_scenario!=-1); }
}
if( !_TestResult)
{
printf("%-32s FAILED\n", "mcpd_simple1");
fflush(stdout);
}
_TotalResult = _TotalResult && _TestResult;
//
// TEST mcpd_simple2
// Simple MCPD model (no entry/exit states) with equality constraints
//
_TestResult = true;
for(_spoil_scenario=-1; _spoil_scenario<6; _spoil_scenario++)
{
try
{
//
// Simple MCPD example
//
// We have a loan portfolio. Our loans can be in one of three states:
// * normal loans
// * past due loans
// * charged off loans
//
// We assume that:
// * normal loan can stay normal or become past due (but not charged off)
// * past due loan can stay past due, become normal or charged off
// * charged off loan will stay charged off for the rest of eternity
// * portfolio size does not change over time
// Not realistic, but it is toy example :)
//
// Thus, we have following model
// state_new = P*state_old
// where
// ( p00 p01 )
// P = ( p10 p11 )
// ( p21 1 )
// i.e. four elements of P are known a priori.
//
// Although it is possible (given enough data) to In order to enforce
// this property we set equality constraints on these elements.
//
// We want to model transitions between these two states using MCPD
// approach (Markov Chains for Proportional/Population Data), i.e.
// to restore hidden transition matrix P using actual portfolio data.
// We have:
// * poportional data, i.e. proportion of loans in the current and past
// due states (not portfolio size measured in some currency, although
// it is possible to work with population data too)
// * two tracks, i.e. two sequences which describe portfolio
// evolution from two different starting states: [1,0,0] (all loans
// are "good") and [0.8,0.2,0.0] (only 80% of portfolio is in the "good"
// state)
//
mcpdstate s;
mcpdreport rep;
real_2d_array p;
real_2d_array track0 = "[[1.000000,0.000000,0.000000],[0.950000,0.050000,0.000000],[0.927500,0.060000,0.012500],[0.911125,0.061375,0.027500],[0.896256,0.060900,0.042844]]";
if( _spoil_scenario==0 )
spoil_matrix_by_nan(track0);
if( _spoil_scenario==1 )
spoil_matrix_by_posinf(track0);
if( _spoil_scenario==2 )
spoil_matrix_by_neginf(track0);
real_2d_array track1 = "[[0.800000,0.200000,0.000000],[0.860000,0.090000,0.050000],[0.862000,0.065500,0.072500],[0.851650,0.059475,0.088875],[0.838805,0.057451,0.103744]]";
if( _spoil_scenario==3 )
spoil_matrix_by_nan(track1);
if( _spoil_scenario==4 )
spoil_matrix_by_posinf(track1);
if( _spoil_scenario==5 )
spoil_matrix_by_neginf(track1);
mcpdcreate(3, s);
mcpdaddtrack(s, track0);
mcpdaddtrack(s, track1);
mcpdaddec(s, 0, 2, 0.0);
mcpdaddec(s, 1, 2, 0.0);
mcpdaddec(s, 2, 2, 1.0);
mcpdaddec(s, 2, 0, 0.0);
mcpdsolve(s);
mcpdresults(s, p, rep);
//
// Hidden matrix P is equal to
// ( 0.95 0.50 )
// ( 0.05 0.25 )
// ( 0.25 1.00 )
// which means that "good" loans can become past due with 5% probability,
// while past due loans will become charged off with 25% probability or
// return back to normal state with 50% probability.
//
_TestResult = _TestResult && doc_test_real_matrix(p, "[[0.95,0.50,0.00],[0.05,0.25,0.00],[0.00,0.25,1.00]]", 0.005);
_TestResult = _TestResult && (_spoil_scenario==-1);
}
catch(ap_error)
{ _TestResult = _TestResult && (_spoil_scenario!=-1); }
}
if( !_TestResult)
{
printf("%-32s FAILED\n", "mcpd_simple2");
fflush(stdout);
}
_TotalResult = _TotalResult && _TestResult;
//
// TEST knn_cls
// Simple classification with KNN model
//
_TestResult = true;
for(_spoil_scenario=-1; _spoil_scenario<3; _spoil_scenario++)
{
try
{
//
// The very simple classification example: classify points (x,y) in 2D space
// as ones with x>=0 and ones with x<0 (y is ignored, but our classifier
// has to find out it).
//
// First, we have to create KNN builder object, load dataset and specify
// training settings. Our dataset is specified as matrix, which has following
// format:
//
// x0 y0 class0
// x1 y1 class1
// x2 y2 class2
// ....
//
// Here xi and yi can be any values (and in fact you can have any number of
// independent variables), and classi MUST be integer number in [0,NClasses)
// range. In our example we denote points with x>=0 as class #0, and
// ones with negative xi as class #1.
//
// NOTE: if you want to solve regression problem, specify dataset in similar
// format, but with dependent variable(s) instead of class labels. You
// can have dataset with multiple dependent variables, by the way!
//
// For the sake of simplicity, our example includes only 4-point dataset and
// really simple K=1 nearest neighbor search. Industrial problems typically
// need larger values of K.
//
knnbuilder builder;
ae_int_t nvars = 2;
ae_int_t nclasses = 2;
ae_int_t npoints = 4;
real_2d_array xy = "[[1,1,0],[1,-1,0],[-1,1,1],[-1,-1,1]]";
if( _spoil_scenario==0 )
spoil_matrix_by_nan(xy);
if( _spoil_scenario==1 )
spoil_matrix_by_posinf(xy);
if( _spoil_scenario==2 )
spoil_matrix_by_neginf(xy);
knnbuildercreate(builder);
knnbuildersetdatasetcls(builder, xy, npoints, nvars, nclasses);
// we build KNN model with k=1 and eps=0 (exact k-nn search is performed)
ae_int_t k = 1;
double eps = 0;
knnmodel model;
knnreport rep;
knnbuilderbuildknnmodel(builder, k, eps, model, rep);
// with such settings (k=1 is used) you can expect zero classification
// error on training set. Beautiful results, but remember - in real life
// you do not need zero TRAINING SET error, you need good generalization.
_TestResult = _TestResult && doc_test_real(rep.relclserror, 0.0000, 0.00005);
// now, let's perform some simple processing with knnprocess()
real_1d_array x = "[+1,0]";
real_1d_array y = "[]";
knnprocess(model, x, y);
_TestResult = _TestResult && doc_test_real_vector(y, "[+1,0]", 0.0005);
// another option is to use knnprocess0() which returns just first component
// of the output vector y. ideal for regression problems and binary classifiers.
double y0;
y0 = knnprocess0(model, x);
_TestResult = _TestResult && doc_test_real(y0, 1.000, 0.0005);
// finally, you can use knnclassify() which returns most probable class index (i.e. argmax y[i]).
ae_int_t i;
i = knnclassify(model, x);
_TestResult = _TestResult && doc_test_int(i, 0);
_TestResult = _TestResult && (_spoil_scenario==-1);
}
catch(ap_error)
{ _TestResult = _TestResult && (_spoil_scenario!=-1); }
}
if( !_TestResult)
{
printf("%-32s FAILED\n", "knn_cls");
fflush(stdout);
}
_TotalResult = _TotalResult && _TestResult;
//
// TEST knn_reg
// Simple classification with KNN model
//
_TestResult = true;
for(_spoil_scenario=-1; _spoil_scenario<3; _spoil_scenario++)
{
try
{
//
// The very simple regression example: model f(x,y)=x+y
//
// First, we have to create KNN builder object, load dataset and specify
// training settings. Our dataset is specified as matrix, which has following
// format:
//
// x0 y0 f0
// x1 y1 f1
// x2 y2 f2
// ....
//
// Here xi and yi can be any values, and fi is a dependent function value.
// By the way, with KNN algorithm you can even model functions with multiple
// dependent variables!
//
// NOTE: you can also solve classification problems with KNN models, see
// another example for this unit.
//
// For the sake of simplicity, our example includes only 4-point dataset and
// really simple K=1 nearest neighbor search. Industrial problems typically
// need larger values of K.
//
knnbuilder builder;
ae_int_t nvars = 2;
ae_int_t nout = 1;
ae_int_t npoints = 4;
real_2d_array xy = "[[1,1,+2],[1,-1,0],[-1,1,0],[-1,-1,-2]]";
if( _spoil_scenario==0 )
spoil_matrix_by_nan(xy);
if( _spoil_scenario==1 )
spoil_matrix_by_posinf(xy);
if( _spoil_scenario==2 )
spoil_matrix_by_neginf(xy);
knnbuildercreate(builder);
knnbuildersetdatasetreg(builder, xy, npoints, nvars, nout);
// we build KNN model with k=1 and eps=0 (exact k-nn search is performed)
ae_int_t k = 1;
double eps = 0;
knnmodel model;
knnreport rep;
knnbuilderbuildknnmodel(builder, k, eps, model, rep);
// with such settings (k=1 is used) you can expect zero RMS error on the
// training set. Beautiful results, but remember - in real life you do not
// need zero TRAINING SET error, you need good generalization.
_TestResult = _TestResult && doc_test_real(rep.rmserror, 0.0000, 0.00005);
// now, let's perform some simple processing with knnprocess()
real_1d_array x = "[+1,+1]";
real_1d_array y = "[]";
knnprocess(model, x, y);
_TestResult = _TestResult && doc_test_real_vector(y, "[+2]", 0.0005);
// another option is to use knnprocess0() which returns just first component
// of the output vector y. ideal for regression problems and binary classifiers.
double y0;
y0 = knnprocess0(model, x);
_TestResult = _TestResult && doc_test_real(y0, 2.000, 0.0005);
// there also exist another convenience function, knnclassify(),
// but it does not work for regression problems - it always returns -1.
ae_int_t i;
i = knnclassify(model, x);
_TestResult = _TestResult && doc_test_int(i, -1);
_TestResult = _TestResult && (_spoil_scenario==-1);
}
catch(ap_error)
{ _TestResult = _TestResult && (_spoil_scenario!=-1); }
}
if( !_TestResult)
{
printf("%-32s FAILED\n", "knn_reg");
fflush(stdout);
}
_TotalResult = _TotalResult && _TestResult;
//
// TEST nn_regr
// Regression problem with one output (2=>1)
//
_TestResult = true;
for(_spoil_scenario=-1; _spoil_scenario<3; _spoil_scenario++)
{
try
{
//
// The very simple example on neural network: network is trained to reproduce
// small 2x2 multiplication table.
//
// NOTE: we use network with excessive amount of neurons, which guarantees
// almost exact reproduction of the training set. Generalization ability
// of such network is rather low, but we are not concerned with such
// questions in this basic demo.
//
mlptrainer trn;
multilayerperceptron network;
mlpreport rep;
//
// Training set:
// * one row corresponds to one record A*B=C in the multiplication table
// * first two columns store A and B, last column stores C
//
// [1 * 1 = 1]
// [1 * 2 = 2]
// [2 * 1 = 2]
// [2 * 2 = 4]
//
real_2d_array xy = "[[1,1,1],[1,2,2],[2,1,2],[2,2,4]]";
if( _spoil_scenario==0 )
spoil_matrix_by_nan(xy);
if( _spoil_scenario==1 )
spoil_matrix_by_posinf(xy);
if( _spoil_scenario==2 )
spoil_matrix_by_neginf(xy);
//
// Network is created.
// Trainer object is created.
// Dataset is attached to trainer object.
//
mlpcreatetrainer(2, 1, trn);
mlpcreate1(2, 5, 1, network);
mlpsetdataset(trn, xy, 4);
//
// Network is trained with 5 restarts from random positions
//
mlptrainnetwork(trn, network, 5, rep);
//
// 2*2=?
//
real_1d_array x = "[2,2]";
real_1d_array y = "[0]";
mlpprocess(network, x, y);
_TestResult = _TestResult && doc_test_real_vector(y, "[4.000]", 0.05);
_TestResult = _TestResult && (_spoil_scenario==-1);
}
catch(ap_error)
{ _TestResult = _TestResult && (_spoil_scenario!=-1); }
}
if( !_TestResult)
{
printf("%-32s FAILED\n", "nn_regr");
fflush(stdout);
}
_TotalResult = _TotalResult && _TestResult;
//
// TEST nn_regr_n
// Regression problem with multiple outputs (2=>2)
//
_TestResult = true;
for(_spoil_scenario=-1; _spoil_scenario<3; _spoil_scenario++)
{
try
{
//
// Network with 2 inputs and 2 outputs is trained to reproduce vector function:
// (x0,x1) => (x0+x1, x0*x1)
//
// Informally speaking, we want neural network to simultaneously calculate
// both sum of two numbers and their product.
//
// NOTE: we use network with excessive amount of neurons, which guarantees
// almost exact reproduction of the training set. Generalization ability
// of such network is rather low, but we are not concerned with such
// questions in this basic demo.
//
mlptrainer trn;
multilayerperceptron network;
mlpreport rep;
//
// Training set. One row corresponds to one record [A,B,A+B,A*B].
//
// [ 1 1 1+1 1*1 ]
// [ 1 2 1+2 1*2 ]
// [ 2 1 2+1 2*1 ]
// [ 2 2 2+2 2*2 ]
//
real_2d_array xy = "[[1,1,2,1],[1,2,3,2],[2,1,3,2],[2,2,4,4]]";
if( _spoil_scenario==0 )
spoil_matrix_by_nan(xy);
if( _spoil_scenario==1 )
spoil_matrix_by_posinf(xy);
if( _spoil_scenario==2 )
spoil_matrix_by_neginf(xy);
//
// Network is created.
// Trainer object is created.
// Dataset is attached to trainer object.
//
mlpcreatetrainer(2, 2, trn);
mlpcreate1(2, 5, 2, network);
mlpsetdataset(trn, xy, 4);
//
// Network is trained with 5 restarts from random positions
//
mlptrainnetwork(trn, network, 5, rep);
//
// 2+1=?
// 2*1=?
//
real_1d_array x = "[2,1]";
real_1d_array y = "[0,0]";
mlpprocess(network, x, y);
_TestResult = _TestResult && doc_test_real_vector(y, "[3.000,2.000]", 0.05);
_TestResult = _TestResult && (_spoil_scenario==-1);
}
catch(ap_error)
{ _TestResult = _TestResult && (_spoil_scenario!=-1); }
}
if( !_TestResult)
{
printf("%-32s FAILED\n", "nn_regr_n");
fflush(stdout);
}
_TotalResult = _TotalResult && _TestResult;
//
// TEST nn_cls2
// Binary classification problem
//
_TestResult = true;
for(_spoil_scenario=-1; _spoil_scenario<3; _spoil_scenario++)
{
try
{
//
// Suppose that we want to classify numbers as positive (class 0) and negative
// (class 1). We have training set which includes several strictly positive
// or negative numbers - and zero.
//
// The problem is that we are not sure how to classify zero, so from time to
// time we mark it as positive or negative (with equal probability). Other
// numbers are marked in pure deterministic setting. How will neural network
// cope with such classification task?
//
// NOTE: we use network with excessive amount of neurons, which guarantees
// almost exact reproduction of the training set. Generalization ability
// of such network is rather low, but we are not concerned with such
// questions in this basic demo.
//
mlptrainer trn;
multilayerperceptron network;
mlpreport rep;
real_1d_array x = "[0]";
real_1d_array y = "[0,0]";
//
// Training set. One row corresponds to one record [A => class(A)].
//
// Classes are denoted by numbers from 0 to 1, where 0 corresponds to positive
// numbers and 1 to negative numbers.
//
// [ +1 0]
// [ +2 0]
// [ -1 1]
// [ -2 1]
// [ 0 0] !! sometimes we classify 0 as positive, sometimes as negative
// [ 0 1] !!
//
real_2d_array xy = "[[+1,0],[+2,0],[-1,1],[-2,1],[0,0],[0,1]]";
if( _spoil_scenario==0 )
spoil_matrix_by_nan(xy);
if( _spoil_scenario==1 )
spoil_matrix_by_posinf(xy);
if( _spoil_scenario==2 )
spoil_matrix_by_neginf(xy);
//
//
// When we solve classification problems, everything is slightly different from
// the regression ones:
//
// 1. Network is created. Because we solve classification problem, we use
// mlpcreatec1() function instead of mlpcreate1(). This function creates
// classifier network with SOFTMAX-normalized outputs. This network returns
// vector of class membership probabilities which are normalized to be
// non-negative and sum to 1.0
//
// 2. We use mlpcreatetrainercls() function instead of mlpcreatetrainer() to
// create trainer object. Trainer object process dataset and neural network
// slightly differently to account for specifics of the classification
// problems.
//
// 3. Dataset is attached to trainer object. Note that dataset format is slightly
// different from one used for regression.
//
mlpcreatetrainercls(1, 2, trn);
mlpcreatec1(1, 5, 2, network);
mlpsetdataset(trn, xy, 6);
//
// Network is trained with 5 restarts from random positions
//
mlptrainnetwork(trn, network, 5, rep);
//
// Test our neural network on strictly positive and strictly negative numbers.
//
// IMPORTANT! Classifier network returns class membership probabilities instead
// of class indexes. Network returns two values (probabilities) instead of one
// (class index).
//
// Thus, for +1 we expect to get [P0,P1] = [1,0], where P0 is probability that
// number is positive (belongs to class 0), and P1 is probability that number
// is negative (belongs to class 1).
//
// For -1 we expect to get [P0,P1] = [0,1]
//
// Following properties are guaranteed by network architecture:
// * P0>=0, P1>=0 non-negativity
// * P0+P1=1 normalization
//
x = "[1]";
mlpprocess(network, x, y);
_TestResult = _TestResult && doc_test_real_vector(y, "[1.000,0.000]", 0.05);
x = "[-1]";
mlpprocess(network, x, y);
_TestResult = _TestResult && doc_test_real_vector(y, "[0.000,1.000]", 0.05);
//
// But what our network will return for 0, which is between classes 0 and 1?
//
// In our dataset it has two different marks assigned (class 0 AND class 1).
// So network will return something average between class 0 and class 1:
// 0 => [0.5, 0.5]
//
x = "[0]";
mlpprocess(network, x, y);
_TestResult = _TestResult && doc_test_real_vector(y, "[0.500,0.500]", 0.05);
_TestResult = _TestResult && (_spoil_scenario==-1);
}
catch(ap_error)
{ _TestResult = _TestResult && (_spoil_scenario!=-1); }
}
if( !_TestResult)
{
printf("%-32s FAILED\n", "nn_cls2");
fflush(stdout);
}
_TotalResult = _TotalResult && _TestResult;
//
// TEST nn_cls3
// Multiclass classification problem
//
_TestResult = true;
for(_spoil_scenario=-1; _spoil_scenario<3; _spoil_scenario++)
{
try
{
//
// Suppose that we want to classify numbers as positive (class 0) and negative
// (class 1). We also have one more class for zero (class 2).
//
// NOTE: we use network with excessive amount of neurons, which guarantees
// almost exact reproduction of the training set. Generalization ability
// of such network is rather low, but we are not concerned with such
// questions in this basic demo.
//
mlptrainer trn;
multilayerperceptron network;
mlpreport rep;
real_1d_array x = "[0]";
real_1d_array y = "[0,0,0]";
//
// Training set. One row corresponds to one record [A => class(A)].
//
// Classes are denoted by numbers from 0 to 2, where 0 corresponds to positive
// numbers, 1 to negative numbers, 2 to zero
//
// [ +1 0]
// [ +2 0]
// [ -1 1]
// [ -2 1]
// [ 0 2]
//
real_2d_array xy = "[[+1,0],[+2,0],[-1,1],[-2,1],[0,2]]";
if( _spoil_scenario==0 )
spoil_matrix_by_nan(xy);
if( _spoil_scenario==1 )
spoil_matrix_by_posinf(xy);
if( _spoil_scenario==2 )
spoil_matrix_by_neginf(xy);
//
//
// When we solve classification problems, everything is slightly different from
// the regression ones:
//
// 1. Network is created. Because we solve classification problem, we use
// mlpcreatec1() function instead of mlpcreate1(). This function creates
// classifier network with SOFTMAX-normalized outputs. This network returns
// vector of class membership probabilities which are normalized to be
// non-negative and sum to 1.0
//
// 2. We use mlpcreatetrainercls() function instead of mlpcreatetrainer() to
// create trainer object. Trainer object process dataset and neural network
// slightly differently to account for specifics of the classification
// problems.
//
// 3. Dataset is attached to trainer object. Note that dataset format is slightly
// different from one used for regression.
//
mlpcreatetrainercls(1, 3, trn);
mlpcreatec1(1, 5, 3, network);
mlpsetdataset(trn, xy, 5);
//
// Network is trained with 5 restarts from random positions
//
mlptrainnetwork(trn, network, 5, rep);
//
// Test our neural network on strictly positive and strictly negative numbers.
//
// IMPORTANT! Classifier network returns class membership probabilities instead
// of class indexes. Network returns three values (probabilities) instead of one
// (class index).
//
// Thus, for +1 we expect to get [P0,P1,P2] = [1,0,0],
// for -1 we expect to get [P0,P1,P2] = [0,1,0],
// and for 0 we will get [P0,P1,P2] = [0,0,1].
//
// Following properties are guaranteed by network architecture:
// * P0>=0, P1>=0, P2>=0 non-negativity
// * P0+P1+P2=1 normalization
//
x = "[1]";
mlpprocess(network, x, y);
_TestResult = _TestResult && doc_test_real_vector(y, "[1.000,0.000,0.000]", 0.05);
x = "[-1]";
mlpprocess(network, x, y);
_TestResult = _TestResult && doc_test_real_vector(y, "[0.000,1.000,0.000]", 0.05);
x = "[0]";
mlpprocess(network, x, y);
_TestResult = _TestResult && doc_test_real_vector(y, "[0.000,0.000,1.000]", 0.05);
_TestResult = _TestResult && (_spoil_scenario==-1);
}
catch(ap_error)
{ _TestResult = _TestResult && (_spoil_scenario!=-1); }
}
if( !_TestResult)
{
printf("%-32s FAILED\n", "nn_cls3");
fflush(stdout);
}
_TotalResult = _TotalResult && _TestResult;
//
// TEST nn_trainerobject
// Advanced example on trainer object
//
_TestResult = true;
for(_spoil_scenario=-1; _spoil_scenario<6; _spoil_scenario++)
{
try
{
//
// Trainer object is used to train network. It stores dataset, training settings,
// and other information which is NOT part of neural network. You should use
// trainer object as follows:
// (1) you create trainer object and specify task type (classification/regression)
// and number of inputs/outputs
// (2) you add dataset to the trainer object
// (3) you may change training settings (stopping criteria or weight decay)
// (4) finally, you may train one or more networks
//
// You may interleave stages 2...4 and repeat them many times. Trainer object
// remembers its internal state and can be used several times after its creation
// and initialization.
//
mlptrainer trn;
//
// Stage 1: object creation.
//
// We have to specify number of inputs and outputs. Trainer object can be used
// only for problems with same number of inputs/outputs as was specified during
// its creation.
//
// In case you want to train SOFTMAX-normalized network which solves classification
// problems, you must use another function to create trainer object:
// mlpcreatetrainercls().
//
// Below we create trainer object which can be used to train regression networks
// with 2 inputs and 1 output.
//
mlpcreatetrainer(2, 1, trn);
//
// Stage 2: specification of the training set
//
// By default trainer object stores empty dataset. So to solve your non-empty problem
// you have to set dataset by passing to trainer dense or sparse matrix.
//
// One row of the matrix corresponds to one record A*B=C in the multiplication table.
// First two columns store A and B, last column stores C
//
// [1 * 1 = 1] [ 1 1 1 ]
// [1 * 2 = 2] [ 1 2 2 ]
// [2 * 1 = 2] = [ 2 1 2 ]
// [2 * 2 = 4] [ 2 2 4 ]
//
real_2d_array xy = "[[1,1,1],[1,2,2],[2,1,2],[2,2,4]]";
if( _spoil_scenario==0 )
spoil_matrix_by_nan(xy);
if( _spoil_scenario==1 )
spoil_matrix_by_posinf(xy);
if( _spoil_scenario==2 )
spoil_matrix_by_neginf(xy);
mlpsetdataset(trn, xy, 4);
//
// Stage 3: modification of the training parameters.
//
// You may modify parameters like weights decay or stopping criteria:
// * we set moderate weight decay
// * we choose iterations limit as stopping condition (another condition - step size -
// is zero, which means than this condition is not active)
//
double wstep = 0.000;
if( _spoil_scenario==3 )
wstep = fp_nan;
if( _spoil_scenario==4 )
wstep = fp_posinf;
if( _spoil_scenario==5 )
wstep = fp_neginf;
ae_int_t maxits = 100;
mlpsetdecay(trn, 0.01);
mlpsetcond(trn, wstep, maxits);
//
// Stage 4: training.
//
// We will train several networks with different architecture using same trainer object.
// We may change training parameters or even dataset, so different networks are trained
// differently. But in this simple example we will train all networks with same settings.
//
// We create and train three networks:
// * network 1 has 2x1 architecture (2 inputs, no hidden neurons, 1 output)
// * network 2 has 2x5x1 architecture (2 inputs, 5 hidden neurons, 1 output)
// * network 3 has 2x5x5x1 architecture (2 inputs, two hidden layers, 1 output)
//
// NOTE: these networks solve regression problems. For classification problems you
// should use mlpcreatec0/c1/c2 to create neural networks which have SOFTMAX-
// normalized outputs.
//
multilayerperceptron net1;
multilayerperceptron net2;
multilayerperceptron net3;
mlpreport rep;
mlpcreate0(2, 1, net1);
mlpcreate1(2, 5, 1, net2);
mlpcreate2(2, 5, 5, 1, net3);
mlptrainnetwork(trn, net1, 5, rep);
mlptrainnetwork(trn, net2, 5, rep);
mlptrainnetwork(trn, net3, 5, rep);
_TestResult = _TestResult && (_spoil_scenario==-1);
}
catch(ap_error)
{ _TestResult = _TestResult && (_spoil_scenario!=-1); }
}
if( !_TestResult)
{
printf("%-32s FAILED\n", "nn_trainerobject");
fflush(stdout);
}
_TotalResult = _TotalResult && _TestResult;
//
// TEST nn_crossvalidation
// Cross-validation
//
_TestResult = true;
for(_spoil_scenario=-1; _spoil_scenario<3; _spoil_scenario++)
{
try
{
//
// This example shows how to perform cross-validation with ALGLIB
//
mlptrainer trn;
multilayerperceptron network;
mlpreport rep;
//
// Training set: f(x)=1/(x^2+1)
// One row corresponds to one record [x,f(x)]
//
real_2d_array xy = "[[-2.0,0.2],[-1.6,0.3],[-1.3,0.4],[-1,0.5],[-0.6,0.7],[-0.3,0.9],[0,1],[2.0,0.2],[1.6,0.3],[1.3,0.4],[1,0.5],[0.6,0.7],[0.3,0.9]]";
if( _spoil_scenario==0 )
spoil_matrix_by_nan(xy);
if( _spoil_scenario==1 )
spoil_matrix_by_posinf(xy);
if( _spoil_scenario==2 )
spoil_matrix_by_neginf(xy);
//
// Trainer object is created.
// Dataset is attached to trainer object.
//
// NOTE: it is not good idea to perform cross-validation on sample
// as small as ours (13 examples). It is done for demonstration
// purposes only. Generalization error estimates won't be
// precise enough for practical purposes.
//
mlpcreatetrainer(1, 1, trn);
mlpsetdataset(trn, xy, 13);
//
// The key property of the cross-validation is that it estimates
// generalization properties of neural ARCHITECTURE. It does NOT
// estimates generalization error of some specific network which
// is passed to the k-fold CV routine.
//
// In our example we create 1x4x1 neural network and pass it to
// CV routine without training it. Original state of the network
// is not used for cross-validation - each round is restarted from
// random initial state. Only geometry of network matters.
//
// We perform 5 restarts from different random positions for each
// of the 10 cross-validation rounds.
//
mlpcreate1(1, 4, 1, network);
mlpkfoldcv(trn, network, 5, 10, rep);
//
// Cross-validation routine stores estimates of the generalization
// error to MLP report structure. You may examine its fields and
// see estimates of different errors (RMS, CE, Avg).
//
// Because cross-validation is non-deterministic, in our manual we
// can not say what values will be stored to rep after call to
// mlpkfoldcv(). Every CV round will return slightly different
// estimates.
//
_TestResult = _TestResult && (_spoil_scenario==-1);
}
catch(ap_error)
{ _TestResult = _TestResult && (_spoil_scenario!=-1); }
}
if( !_TestResult)
{
printf("%-32s FAILED\n", "nn_crossvalidation");
fflush(stdout);
}
_TotalResult = _TotalResult && _TestResult;
//
// TEST nn_ensembles_es
// Early stopping ensembles
//
_TestResult = true;
for(_spoil_scenario=-1; _spoil_scenario<3; _spoil_scenario++)
{
try
{
//
// This example shows how to train early stopping ensebles.
//
mlptrainer trn;
mlpensemble ensemble;
mlpreport rep;
//
// Training set: f(x)=1/(x^2+1)
// One row corresponds to one record [x,f(x)]
//
real_2d_array xy = "[[-2.0,0.2],[-1.6,0.3],[-1.3,0.4],[-1,0.5],[-0.6,0.7],[-0.3,0.9],[0,1],[2.0,0.2],[1.6,0.3],[1.3,0.4],[1,0.5],[0.6,0.7],[0.3,0.9]]";
if( _spoil_scenario==0 )
spoil_matrix_by_nan(xy);
if( _spoil_scenario==1 )
spoil_matrix_by_posinf(xy);
if( _spoil_scenario==2 )
spoil_matrix_by_neginf(xy);
//
// Trainer object is created.
// Dataset is attached to trainer object.
//
// NOTE: it is not good idea to use early stopping ensemble on sample
// as small as ours (13 examples). It is done for demonstration
// purposes only. Ensemble training algorithm won't find good
// solution on such small sample.
//
mlpcreatetrainer(1, 1, trn);
mlpsetdataset(trn, xy, 13);
//
// Ensemble is created and trained. Each of 50 network is trained
// with 5 restarts.
//
mlpecreate1(1, 4, 1, 50, ensemble);
mlptrainensemblees(trn, ensemble, 5, rep);
_TestResult = _TestResult && (_spoil_scenario==-1);
}
catch(ap_error)
{ _TestResult = _TestResult && (_spoil_scenario!=-1); }
}
if( !_TestResult)
{
printf("%-32s FAILED\n", "nn_ensembles_es");
fflush(stdout);
}
_TotalResult = _TotalResult && _TestResult;
//
// TEST nn_parallel
// Parallel training
//
_TestResult = true;
for(_spoil_scenario=-1; _spoil_scenario<3; _spoil_scenario++)
{
try
{
//
// This example shows how to use parallel functionality of ALGLIB.
// We generate simple 1-dimensional regression problem and show how
// to use parallel training, parallel cross-validation, parallel
// training of neural ensembles.
//
// We assume that you already know how to use ALGLIB in serial mode
// and concentrate on its parallel capabilities.
//
// NOTE: it is not good idea to use parallel features on sample as small
// as ours (13 examples). It is done only for demonstration purposes.
//
mlptrainer trn;
multilayerperceptron network;
mlpensemble ensemble;
mlpreport rep;
real_2d_array xy = "[[-2.0,0.2],[-1.6,0.3],[-1.3,0.4],[-1,0.5],[-0.6,0.7],[-0.3,0.9],[0,1],[2.0,0.2],[1.6,0.3],[1.3,0.4],[1,0.5],[0.6,0.7],[0.3,0.9]]";
if( _spoil_scenario==0 )
spoil_matrix_by_nan(xy);
if( _spoil_scenario==1 )
spoil_matrix_by_posinf(xy);
if( _spoil_scenario==2 )
spoil_matrix_by_neginf(xy);
mlpcreatetrainer(1, 1, trn);
mlpsetdataset(trn, xy, 13);
mlpcreate1(1, 4, 1, network);
mlpecreate1(1, 4, 1, 50, ensemble);
//
// Below we demonstrate how to perform:
// * parallel training of individual networks
// * parallel cross-validation
// * parallel training of neural ensembles
//
// In order to use multithreading, you have to:
// 1) Install SMP edition of ALGLIB.
// 2) This step is specific for C++ users: you should activate OS-specific
// capabilities of ALGLIB by defining AE_OS=AE_POSIX (for *nix systems)
// or AE_OS=AE_WINDOWS (for Windows systems).
// C# users do not have to perform this step because C# programs are
// portable across different systems without OS-specific tuning.
// 3) Tell ALGLIB that you want it to use multithreading by means of
// setnworkers() call:
// * alglib::setnworkers(0) = use all cores
// * alglib::setnworkers(-1) = leave one core unused
// * alglib::setnworkers(-2) = leave two cores unused
// * alglib::setnworkers(+2) = use 2 cores (even if you have more)
// During runtime ALGLIB will automatically determine whether it is
// feasible to start worker threads and split your task between cores.
//
alglib::setnworkers(+2);
//
// First, we perform parallel training of individual network with 5
// restarts from random positions. These 5 rounds of training are
// executed in parallel manner, with best network chosen after
// training.
//
// ALGLIB can use additional way to speed up computations - divide
// dataset into smaller subsets and process these subsets
// simultaneously. It allows us to efficiently parallelize even
// single training round. This operation is performed automatically
// for large datasets, but our toy dataset is too small.
//
mlptrainnetwork(trn, network, 5, rep);
//
// Then, we perform parallel 10-fold cross-validation, with 5 random
// restarts per each CV round. I.e., 5*10=50 networks are trained
// in total. All these operations can be parallelized.
//
// NOTE: again, ALGLIB can parallelize calculation of gradient
// over entire dataset - but our dataset is too small.
//
mlpkfoldcv(trn, network, 5, 10, rep);
//
// Finally, we train early stopping ensemble of 50 neural networks,
// each of them is trained with 5 random restarts. I.e., 5*50=250
// networks aretrained in total.
//
mlptrainensemblees(trn, ensemble, 5, rep);
_TestResult = _TestResult && (_spoil_scenario==-1);
}
catch(ap_error)
{ _TestResult = _TestResult && (_spoil_scenario!=-1); }
}
if( !_TestResult)
{
printf("%-32s FAILED\n", "nn_parallel");
fflush(stdout);
}
_TotalResult = _TotalResult && _TestResult;
printf("165/165\n");
}
catch(...)
{
printf("Unhandled exception was raised!\n");
return 1;
}
#ifdef AE_USE_ALLOC_COUNTER
printf("Allocation counter checked... ");
#ifdef _ALGLIB_HAS_WORKSTEALING
alglib_impl::ae_free_disposed_items();
#endif
if( alglib_impl::_alloc_counter!=0 )
{
printf("FAILURE: alloc_counter is non-zero on end!\n");
return 1;
}
else
printf("OK\n");
#endif
return _TotalResult ? 0 : 1;
}
|