-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmisc-math.html
More file actions
1310 lines (1294 loc) · 46 KB
/
misc-math.html
File metadata and controls
1310 lines (1294 loc) · 46 KB
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
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html lang="en" xml:lang="en" xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="content-type" content="text/html;charset=UTF-8"/>
<link rel="icon" type="image/gif" href="/favicon.gif"/>
<link rel="apple-touch-icon" sizes="120x120" href="touch-icon-iphone-retina.png" />
<link rel="apple-touch-icon" sizes="152x152" href="touch-icon-ipad-retina.png" />
<title>Lorem Ipsum</title>
<script type="text/javascript"
src="http://cdn.mathjax.org/mathjax/latest/MathJax.js?config=TeX-AMS-MML_HTMLorMML">
</script>
<style type="text/css" id="internal-style">
@import url(hyperpolyglot.css);
</style>
<meta http-equiv="content-type" content="text/html;charset=UTF-8"/>
<meta http-equiv="content-language" content="en"/>
</head>
<body>
<div id="container-wrap-wrap">
<div id="container-wrap">
<div id="container">
<div id="header">
<h1><a href="/"><span>Hyperpolyglot</span></a></h1>
</div>
<div id="content-wrap">
<div id="main-content">
<div id="page-title">Lorem Ipsum</div>
<div id="page-content">
<table class="wiki-content-table">
<tr>
<th></th>
<th><a href="#gnuplot">gnuplot</a></th>
<th><a href="#glpk">glpk</a></th>
</tr>
<tr>
<td><a name="version-used"></a><a href="#version-used-note">version used</a><br />
<span style="white-space: pre-wrap;"> </span></td>
<td><span style="color: gray"><em>5.0</em></span></td>
<td><span style="color: gray"><em>4.52</em></span></td>
</tr>
<tr>
<td><a name="show-version"></a><a href="#show-version-note">show version</a><br />
<span style="white-space: pre-wrap;"> </span></td>
<td>$ gnuplot <span style="white-space: pre-wrap;">--</span>version</td>
<td>$ glpsol <span style="white-space: pre-wrap;">--</span>version</td>
</tr>
<tr>
<th colspan="3"><a name="grammar-invocation"></a><a href="#grammar-invocation-note">grammar and invocation</a></th>
</tr>
<tr>
<th></th>
<th>gnuplot</th>
<th>glpk</th>
</tr>
<tr>
<td><a name="interpreter"></a><a href="#interpreter-note">interpreter</a><br />
<span style="white-space: pre-wrap;"> </span></td>
<td>$ cat hello.gnuplot<br />
print "Hello, World!"<br />
<br />
$ gnuplot hello.gnuplot<br />
Hello, World!</td>
<td>$ cat hello.model<br />
printf "Hello, World!\n";<br />
end;<br />
<br />
$ glpsol -m hello.model<br />
Hello, World!</td>
</tr>
<tr>
<td><a name="repl"></a><a href="#repl-note">repl</a><br />
<span style="white-space: pre-wrap;"> </span></td>
<td>$ gnuplot</td>
<td><span style="color: gray"><em>none</em></span></td>
</tr>
<tr>
<td><a name="compiler"></a><a href="#compiler-note">compiler</a></td>
<td><span style="color: gray"><em>none</em></span></td>
<td><span style="color: gray"><em>none</em></span></td>
</tr>
<tr>
<td><a name="cmd-line-program"></a><a href="#cmd-line-program-note">command line program</a><br />
<span style="white-space: pre-wrap;"> </span></td>
<td>$ gnuplot -e 'print 1 + 1;'</td>
<td><span style="color: gray"><em>none</em></span></td>
</tr>
<tr>
<td><a name="block-delimiters"></a><a href="#block-delimiters-note">block delimiters</a><br />
<span style="white-space: pre-wrap;"> </span></td>
<td>{ }</td>
<td>{ }<br />
<br />
<span style="color: gray"><em>Curly braces are used to delimit the block of statements in the body of a</em> for <em>statement.</em></span><br />
<br />
<span style="color: gray"><em>Curly braces are also used in indexing expressions and set literals.</em></span></td>
</tr>
<tr>
<td><a name="stmt-separator"></a><a href="#stmt-separator-note">statement separator</a></td>
<td><span style="color: gray"><em>newline or</em></span> ;<br />
<br />
<span style="color: gray"><em>A newline preceded by a backslash is ignored.</em></span></td>
<td>;</td>
</tr>
<tr>
<td><a name="eol-comment"></a><a href="#eol-comment-note">end-of-line comment</a><br />
<span style="white-space: pre-wrap;"> </span></td>
<td># <span style="color: gray"><em>comment</em></span></td>
<td># <span style="color: gray"><em>comment</em></span></td>
</tr>
<tr>
<td><a name="multiple-line-comment"></a><a href="#multiple-line-comment-note">multiple line comment</a><br />
<span style="white-space: pre-wrap;"> </span></td>
<td><span style="color: gray"><em>none</em></span></td>
<td>/* <span style="color: gray"><em>comment line</em></span><br />
<span style="color: gray"><em>another comment</em></span> */</td>
</tr>
<tr>
<th colspan="3"><a name="var-expr"></a><a href="#var-expr-note">variables and expressions</a></th>
</tr>
<tr>
<th></th>
<th>gnuplot</th>
<th>glpk</th>
</tr>
<tr>
<td><a name="assignment"></a><a href="#assignment-note">assignment</a></td>
<td>x = 3.14</td>
<td><span style="color: gray"># parameters are write once:</span><br />
param pi := 3.14;</td>
</tr>
<tr>
<td><a name="null"></a><a href="#null-note">null</a><br />
<span style="white-space: pre-wrap;"> </span></td>
<td><span style="color: gray"><em>none</em></span></td>
<td><span style="color: gray"><em>none</em></span></td>
</tr>
<tr>
<td><a name="null-test"></a><a href="#null-test-note">null test</a><br />
<span style="white-space: pre-wrap;"> </span></td>
<td><span style="color: gray"><em>none</em></span></td>
<td><span style="color: gray"><em>none</em></span></td>
</tr>
<tr>
<td><a name="undef-var"></a><a href="#undef-var-note">undefined variable access</a><br />
<span style="white-space: pre-wrap;"> </span></td>
<td><span style="color: gray"><em>causes error</em></span></td>
<td><span style="color: gray"><em>causes error</em></span></td>
</tr>
<tr>
<td><a name="undef-var-test"></a><a href="#undef-var-test-note">undefined variable test</a></td>
<td>exists("x")</td>
<td><span style="color: gray"><em>none</em></span></td>
</tr>
<tr>
<td><a name="rm-var-binding"></a><a href="#rm-var-binding-note">remove variable binding</a></td>
<td>undefine x</td>
<td><span style="color: gray"><em>none</em></span></td>
</tr>
<tr>
<td><a name="cond-expr"></a><a href="#cond-expr-note">conditional expression</a><br />
<span style="white-space: pre-wrap;"> </span></td>
<td>x > 0 : x : -x</td>
<td>if x > 0 then x else -x</td>
</tr>
<tr>
<th colspan="3"><a name="arithmetic-logic"></a><a href="#arithmetic-logic-note">arithmetic and logic</a></th>
</tr>
<tr>
<th></th>
<th>gnuplot</th>
<th>glpk</th>
</tr>
<tr>
<td><a name="true-false"></a><a href="#true-false-note">true and false</a><br />
<span style="white-space: pre-wrap;"> </span></td>
<td>1 0</td>
<td>1 0<br />
<br />
<span style="color: gray"><em>The boolean values are distinct from the integers 1 and 0. When displayed by</em> printf '%s' <em>they render as</em> T <em>and</em> F. <em>There are no boolean literals, however.</em></span></td>
</tr>
<tr>
<td><a name="falsehoods"></a><a href="#falsehoods-note">falsehoods</a><br />
<span style="white-space: pre-wrap;"> </span></td>
<td>0<br />
<br />
<span style="color: gray">0.0 <em>is true</em></span></td>
<td>0 0.0</td>
</tr>
<tr>
<td><a name="logical-op"></a><a href="#logical-op-note">logical operators</a></td>
<td>@@&& </td>
<td> !@</td>
<td>and or not<br />
<br />
@@&& </td>
<td> !@</td>
</tr>
<tr>
<td><a name="relational-op"></a><a href="#relational-op-note">relational operators</a></td>
<td>== != < > <= >=<br />
<br />
<span style="color: gray"># string comparision:</span><br />
eq ne</td>
<td><span style="white-space: pre-wrap;">=</span> <> < > <= >=<br />
<br />
== <span style="color: gray">//and//</span> != <span style="color: gray">//are synonyms of//</span> = <span style="color: gray">//and//</span> <> </td>
</tr>
<tr>
<td><a name="arith-op"></a><a href="#arith-op-note">arithmetic operators</a> <br />
<br />
<span style="color: gray"><em>addition, subtraction, multiplication, division, modulus</em></span></td>
<td>+ - * / %</td>
<td>+ - * / mod</td>
</tr>
<tr>
<td><a name="int-div"></a><a href="#int-div-note">integer division</a><br />
<span style="white-space: pre-wrap;"> </span></td>
<td>int(x) / int(y)</td>
<td>x div y</td>
</tr>
<tr>
<td><a name="int-div-zero"></a><a href="#int-div-zero-note">integer division by zero</a></td>
<td><span style="color: gray"><em>error</em></span></td>
<td><span style="color: gray"><em>error</em></span></td>
</tr>
<tr>
<td><a name="float-div"></a><a href="#float-div-note">float division</a></td>
<td>(1.0 * x) / y</td>
<td>7 / 3</td>
</tr>
<tr>
<td><a name="float-div-zero"></a><a href="#float-div-zero-note">float division by zero</a></td>
<td><span style="color: gray"><em>error</em></span></td>
<td><span style="color: gray"><em>error</em></span></td>
</tr>
<tr>
<td><a name="power"></a><a href="#power-note">power</a></td>
<td>2 ** 32</td>
<td>2 ^ 32<br />
2 ** 32</td>
</tr>
<tr>
<td><a name="sqrt"></a><a href="#sqrt-note">sqrt</a></td>
<td>sqrt(2)</td>
<td>sqrt(2)</td>
</tr>
<tr>
<td><a name="sqrt-negative-one"></a><a href="#sqrt-negative-one-note">sqrt -1</a><br />
<span style="white-space: pre-wrap;"> </span></td>
<td><span style="color: gray"># evaluates to {0.0, 1.0}:</span><br />
sqrt(-1) </td>
<td><span style="color: gray"># error:</span><br />
sqrt(-1)</td>
</tr>
<tr>
<td><a name="transcendental-func"></a><a href="#transcendental-func-note">transcendental functions</a></td>
<td>exp() log log10()<br />
sin() cos() tan()<br />
asin() acos() atan()<br />
atan2()</td>
<td>exp() log() log10()<br />
sin() cos() <span style="color: gray"><em>none</em></span><br />
<span style="color: gray"><em>none</em></span> <span style="color: gray"><em>none</em></span> atan()<br />
atan(<span style="color: gray"><em>y</em></span>, <span style="color: gray"><em>x</em></span>)</td>
</tr>
<tr>
<td><a name="transcendental-const"></a><a href="#transcendental-const-note">transcendental constants</a><br />
<span style="color: gray"><em>e and pi</em></span></td>
<td>exp(1)<br />
pi</td>
<td>exp(1)<br />
4 * atan(1)</td>
</tr>
<tr>
<td><a name="float-truncation"></a><a href="#float-truncation-note">float truncation</a><br />
<span style="color: gray"><em>round towards zero, round to nearest integer, round down, round up</em></span></td>
<td>int(x)<br />
<span style="color: gray"><em>??</em></span><br />
floor(x)<br />
ceil(x)</td>
<td>trunc(x)<br />
round(x)<br />
floor(x)<br />
ceil(x)<br />
<br />
<span style="color: gray"># truncate/round to 2 decimal places:</span><br />
trunc(x, 2)<br />
round(x, 2)</td>
</tr>
<tr>
<td><a name="absolute-val"></a><a href="#absolute-val-note">absolute value</a><br />
<span style="color: gray"><em>and signum</em></span></td>
<td>abs(x)<br />
sgn(x)</td>
<td>abs(x)<br />
<span style="color: gray"><em>none</em></span></td>
</tr>
<tr>
<td><a name="int-overflow"></a><a href="#int-overflow-note">integer overflow</a><br />
<span style="white-space: pre-wrap;"> </span></td>
<td><span style="color: gray"><em>32 bit signed integers are used. Operations which overflow return a float.</em></span> </td>
<td><span style="color: gray"><em>conversion to float</em></span></td>
</tr>
<tr>
<td><a name="float-overflow"></a><a href="#float-overflow-note">float overflow</a><br />
<span style="white-space: pre-wrap;"> </span></td>
<td><span style="color: gray"><em>64 bit double precision floats are used. Operations which overflow raise an error.</em></span></td>
<td><span style="color: gray"><em>error</em></span></td>
</tr>
<tr>
<td><a name="complex-construction"></a><a href="#complex-construction-note">complex construction</a><br />
<span style="white-space: pre-wrap;"> </span></td>
<td>z = {1.0, 2.0}</td>
<td><span style="color: gray"><em>none</em></span></td>
</tr>
<tr>
<td><a name="complex-decomposition"></a><a href="#complex-decomposition-note">complex decomposition</a><br />
<span style="color: gray"><em>real and imaginary part, argument and modulus, conjugate</em></span></td>
<td>real(z)<br />
imag(z)<br />
arg(z)<br />
abs(z)<br />
<span style="color: gray"><em>??</em></span></td>
<td><span style="color: gray"><em>none</em></span></td>
</tr>
<tr>
<td><a name="random-num"></a><a href="#random-num-note">random number</a><br />
<span style="color: gray"><em>uniform integer, uniform float, standard normal</em></span></td>
<td>floor(rand(0) * 100)<br />
rand(0)<br />
<span style="color: gray"><em>none</em></span></td>
<td><span style="color: gray"># Irand224() generates integer in [0, 2^24):</span><br />
Irand224() mod 100<br />
Uniform01()<br />
Normal01()</td>
</tr>
<tr>
<td><a name="random-seed"></a><a href="#random-seed-note">random seed</a></td>
<td>_ = rand(17)</td>
<td>$ glpsol <span style="white-space: pre-wrap;">--</span>seed 17 -m foo.model</td>
</tr>
<tr>
<td><a name="bit-op"></a><a href="#bit-op-note">bit operators</a><br />
<span style="white-space: pre-wrap;"> </span></td>
<td><span style="white-space: pre-wrap;"><< >> & | ^ ~</span></td>
<td><span style="color: gray"><em>none</em></span></td>
</tr>
<tr>
<td><a name="binary-octal-hex-literals"></a><a href="#binary-octal-hex-literals-note">binary, octal, and hex literals</a></td>
<td><span style="color: gray"><em>none</em></span><br />
052<br />
0x2a</td>
<td><span style="color: gray"><em>none</em></span></td>
</tr>
<tr>
<th colspan="3"><a name="strings"></a><a href="#strings-note">strings</a></th>
</tr>
<tr>
<th></th>
<th>gnuplot</th>
<th>glpk</th>
</tr>
<tr>
<td><a href="#string-literals">string literals</a></td>
<td>"don't say \"no\""<br />
'don''t say "no"'</td>
<td>"don't say ""no"""<br />
'don''t say "no"'<br />
<br />
<span style="color: gray"><em>Quotes can be omitted in the data section when the contents of the string are alphanumeric or match the character class</em> [-+_.]</span></td>
</tr>
<tr>
<td><a href="#string-newline">newline in literal</a></td>
<td><span style="color: gray"><em>no</em></span></td>
<td><span style="color: gray"><em>no</em></span></td>
</tr>
<tr>
<td><a href="#string-escapes">string literal escapes</a></td>
<td><span style="color: gray"># in double quotes:</span><br />
\n \r \t \<span style="color: gray"><em>o</em></span> \<span style="color: gray"><em>oo</em></span> \<span style="color: gray"><em>ooo</em></span></td>
<td><span style="color: gray"># in printf format:</span><br />
\n \t</td>
</tr>
<tr>
<td><a href="#string-length">length</a></td>
<td>strlen("lorem ipsum")</td>
<td>length("lorem ipsum")</td>
</tr>
<tr>
<td><a href="#string-concatenate">concatenate</a></td>
<td>"lorem " . "ipsum"</td>
<td>"lorem " & "ipsum"</td>
</tr>
<tr>
<td><a href="#index-substring">index of substring</a></td>
<td><span style="color: gray"># indices start at 1;<br />
# returns 0 if not found:</span><br />
# strstrt("foo bar", "bar", </td>
<td><span style="color: gray"><em>none</em></span></td>
</tr>
<tr>
<td><a href="#extract-substring">extract substring</a></td>
<td>substr("foo bar", 5, 8)</td>
<td>substr("foo bar", 5, 3)</td>
</tr>
<tr>
<td><a name="fmt-str"></a><a href="#fmt-str-note">format string</a></td>
<td>sprintf("%d %.2f %s", 7, 3.1415, "foo")</td>
<td><span style="color: gray"><em>none</em></span></td>
</tr>
<tr>
<td><a name="num-to-str"></a><a href="#num-to-str-note">number to string</a></td>
<td>"foo" . 4</td>
<td><span style="color: gray"><em>none</em></span></td>
</tr>
<tr>
<td><a name="str-to-num"></a><a href="#str-to-num-note">string to number</a></td>
<td>"4" + 7<br />
<br />
<span style="color: gray"># causes error:</span><br />
"foo" + 7</td>
<td><span style="color: gray"><em>none</em></span></td>
</tr>
<tr>
<th colspan="3"><a name="dates-time"></a><a href="#dates-time-note">dates and time</a></th>
</tr>
<tr>
<th></th>
<th>gnuplot</th>
<th>glpk</th>
</tr>
<tr>
<td><a name="current-time-unix-epoch"></a><a href="#current-time-unix-epoch-note">current time as unix epoch</a></td>
<td><span style="color: gray"># seconds since Jan 1, 2000</span><br />
<span style="color: gray"># add 946684800.0 to get unix epoch:</span><br />
now = time(0)<br />
<br />
<span style="color: gray"># as float:</span><br />
real_now = time(0.0)</td>
<td>gmtime()</td>
</tr>
<tr>
<td><a name="fmt-time"></a><a href="#fmt-time-note">format time</a></td>
<td>strftime('%Y-%m-%d %H:%M:%S', now)</td>
<td>time2str(gmtime(), '%Y-%m-%d %H:%M:%S')</td>
</tr>
<tr>
<td><a name="parse-time"></a><a href="#parse-time-note">parse time</a></td>
<td>s = "2014-11-30 03:53:59"<br />
t = strptime('%Y-%m-%d %H:%M:%S', s)</td>
<td>str2time("2014-12-01 19:38:09", "%Y-%m-%d %H:%M:%S")</td>
</tr>
<tr>
<td><a name="sleep"></a><a href="#sleep-note">sleep</a></td>
<td>pause 10<br />
<br />
<span style="color: gray"># with message:</span><br />
pause 10 'sleeping 10s<span style="white-space: pre-wrap;">...</span>'</td>
<td><span style="color: gray"><em>none</em></span></td>
</tr>
<tr>
<th colspan="3"><a name="multidimensional-arrays"></a><a href="#multidimensional-arrays-note">multidimensional arrays</a></th>
</tr>
<tr>
<th></th>
<th>gnuplot</th>
<th>glpk</th>
</tr>
<tr>
<td>2d literal</td>
<td><span style="color: gray"><em>in separate file; one line for each data point; whitespace delimited:</em></span><br />
<br />
$ cat data.txt<br />
7 8<br />
9 10<br />
11 12</td>
<td><span style="color: gray"># declare in model section:</span><br />
param a {1..2, 1..2};<br />
<br />
<span style="color: gray"># define in data section:</span><br />
param a: 1<span style="white-space: pre-wrap;"> </span>2 :=<br />
<span style="white-space: pre-wrap;"> </span><span style="white-space: pre-wrap;"> </span><span style="white-space: pre-wrap;"> </span>1<span style="white-space: pre-wrap;"> </span>7<span style="white-space: pre-wrap;"> </span>8<br />
<span style="white-space: pre-wrap;"> </span><span style="white-space: pre-wrap;"> </span><span style="white-space: pre-wrap;"> </span>2<span style="white-space: pre-wrap;"> </span>9 10;</td>
</tr>
<tr>
<td>3d literal</td>
<td><span style="color: gray"><em>in separate file; one line for each data point:</em></span><br />
<br />
$ cat data.txt<br />
# city data:<br />
"Los Angeles" 3 7<br />
"New York" 8 4<br />
"San Francisco" 12 1<br />
<br />
<span style="color: gray"><em>comment lines are ignored; data which contains whitespace must be quoted</em></span></td>
<td> </td>
</tr>
<tr>
<th colspan="3"><a name="sets"></a><a href="#sets-note">sets</a></th>
</tr>
<tr>
<th></th>
<th>gnuplot</th>
<th>glpk</th>
</tr>
<tr>
<td>literal</td>
<td> </td>
<td>set digits := {1, 2, 3, 4, 5};</td>
</tr>
<tr>
<td>set from arithmetic sequence<br />
<br />
<span style="color: gray"><em>unit difference, non-unit difference</em></span></td>
<td> </td>
<td>set digits := {1 .. 5};<br />
<br />
set leap_years := {2000 .. 2096 by 4};</td>
</tr>
<tr>
<td>membership</td>
<td> </td>
<td>set s := {1, 2, 3};<br />
set str := if 4 in s then 'yes' else 'no';</td>
</tr>
<tr>
<td>intersection</td>
<td> </td>
<td>set s1 := {1, 2, 3};<br />
set s2 := {2, 3, 4, 5};<br />
<br />
set s3 := s1 inter s2;</td>
</tr>
<tr>
<td>union</td>
<td> </td>
<td>set s3 := s1 union s2;</td>
</tr>
<tr>
<td>relative complement, symmetric difference</td>
<td> </td>
<td>set s3 := s1 diff s2;<br />
set s4 := s1 symdiff s2;</td>
</tr>
<tr>
<td>cartesian product</td>
<td> </td>
<td><span style="color: gray"># 12 pairs:</span><br />
set s3 := s1 cross s2;</td>
</tr>
<tr>
<td>map</td>
<td> </td>
<td>set s := 1 .. 10;<br />
set squares := setof {i in s} i * i;</td>
</tr>
<tr>
<td>reduce</td>
<td> </td>
<td>set s := 1 .. 10;<br />
<br />
param n1 := min {i in s} i;<span style="white-space: pre-wrap;"> </span><span style="color: gray"># 1</span><br />
param n2 := max {i in s} i;<span style="white-space: pre-wrap;"> </span><span style="color: gray"># 10</span><br />
param n3 := sum {i in s} i;<span style="white-space: pre-wrap;"> </span><span style="color: gray"># 55</span><br />
param n4 := prod {i in s} i;<span style="white-space: pre-wrap;"> </span><span style="color: gray"># 10!</span><br />
<br />
<span style="color: gray"># sum of squares:</span><br />
param n5 := sum {i in s} i * i;</td>
</tr>
<tr>
<td>universal test</td>
<td> </td>
<td>set str1 := if (forall {i in s} i > 5) then 'yes' else 'no';</td>
</tr>
<tr>
<td>existential test</td>
<td> </td>
<td>set str2 = if (exists {i in s} i > 5) then 'yes' else 'no';</td>
</tr>
<tr>
<th colspan="3"><a name="functions"></a><a href="#functions-note">functions</a></th>
</tr>
<tr>
<th></th>
<th>gnuplot</th>
<th>glpk</th>
</tr>
<tr>
<td><a href="#function-definition">define function</a></td>
<td><span style="color: gray"># body of function must be an expression:</span><br />
add(x, y) = x + y</td>
<td><span style="color: gray"><em>none</em></span></td>
</tr>
<tr>
<td><a href="#function-invocation">invoke function</a></td>
<td>add(3, 7)</td>
<td><span style="color: gray"><em>none</em></span></td>
</tr>
<tr>
<td><a href="#list-functions">list functions</a></td>
<td>show functions</td>
<td><span style="color: gray"><em>none</em></span></td>
</tr>
<tr>
<td><a name="missing-arg"></a><a href="#missing-arg-note">missing argument behavior</a></td>
<td><span style="color: gray"><em>error</em></span></td>
<td><span style="color: gray"><em>none</em></span></td>
</tr>
<tr>
<td><a name="extra-arg"></a><a href="#extra-arg-note">extra argument behavior</a></td>
<td><span style="color: gray"><em>error</em></span></td>
<td><span style="color: gray"><em>none</em></span></td>
</tr>
<tr>
<td><a href="#return-value">return value</a></td>
<td><span style="color: gray"><em>value of the expression which is the function body</em></span></td>
<td><span style="color: gray"><em>none</em></span></td>
</tr>
<tr>
<th colspan="3"><a name="execution-control"></a><a href="#execution-control-note">execution control</a></th>
</tr>
<tr>
<th></th>
<th>gnuplot</th>
<th>glpk</th>
</tr>
<tr>
<td><a href="#if">if</a></td>
<td>if (x > 0) {<br />
<span style="white-space: pre-wrap;"> </span>print "positive"<br />
}<br />
else {<br />
<span style="white-space: pre-wrap;"> </span>if (x < 0) {<br />
<span style="white-space: pre-wrap;"> </span><span style="white-space: pre-wrap;"> </span>print "negative"<br />
<span style="white-space: pre-wrap;"> </span>}<br />
<span style="white-space: pre-wrap;"> </span>else {<br />
<span style="white-space: pre-wrap;"> </span><span style="white-space: pre-wrap;"> </span>print "zero"<br />
<span style="white-space: pre-wrap;"> </span>}<br />
}</td>
<td><span style="color: gray"><em>none</em></span></td>
</tr>
<tr>
<td><a href="#while">while</a></td>
<td>i = 0<br />
<br />
while (i < 10) {<br />
<span style="white-space: pre-wrap;"> </span>print i<br />
<span style="white-space: pre-wrap;"> </span>i = i + 1<br />
}</td>
<td><span style="color: gray"><em>none</em></span></td>
</tr>
<tr>
<td><a href="#for">for</a></td>
<td>do for [i = 1:10] {<br />
<span style="white-space: pre-wrap;"> </span>print i<br />
}</td>
<td>for {i in {1..10<br />
{<br />
<span style="white-space: pre-wrap;"> </span>printf "%d\n", i;<br />
}</td>
</tr>
<tr>
<td>break continue</td>
<td> </td>
<td> </td>
</tr>
<tr>
<td>raise exception</td>
<td> </td>
<td> </td>
</tr>
<tr>
<th colspan="3"><a name="file-handles"></a><a href="#file-handles-note">file handles</a></th>
</tr>
<tr>
<th></th>
<th>gnuplot</th>
<th>glpk</th>
</tr>
<tr>
<td>write line to stdout</td>
<td>print "Hello, World!"</td>
<td>printf "Hello, World!\n";</td>
</tr>
<tr>
<td>write formatted string to stdout</td>
<td> </td>
<td>printf "%d %.2f %s", 7, 3.1415, 'foo';</td>
</tr>
<tr>
<td>write to file</td>
<td> </td>
<td><span style="color: gray"># overwrites contents of /tmp/foo.txt:</span><br />
printf "lorem ispum" > "/tmp/foo.txt";</td>
</tr>
<tr>
<td>append to file</td>
<td> </td>
<td>printf "dolor sit amet" <span style="white-space: pre-wrap;">>></span> "/tmp/foo.txt";</td>
</tr>
<tr>
<td>pretty print</td>
<td> </td>
<td>set s := {1, 2, 3, 4, 5};<br />
param x := 3;<br />
param str symbolic := 'lorem ispum';<br />
<br />
display "set: ", s,<br />
<span style="white-space: pre-wrap;"> </span>"param: ", x,<br />
<span style="white-space: pre-wrap;"> </span>"string: ", str;</td>
</tr>
<tr>
<th colspan="3"><a name="directories"></a><a href="#directories-note">directories</a></th>
</tr>
<tr>
<th></th>
<th>gnuplot</th>
<th>glpk</th>
</tr>
<tr>
<td>working directory</td>
<td><span style="color: gray"># writes working dir to stdout:</span><br />
pwd<br />
<br />
cd "/tmp"</td>
<td><span style="color: gray"><em>none</em></span></td>
</tr>
<tr>
<th colspan="3"><a name="libraries-namespaces"></a><a href="#libraries-namespaces-note">libraries and namespaces</a></th>
</tr>
<tr>
<th></th>
<th>gnuplot</th>
<th>glpk</th>
</tr>
<tr>
<td><a href="#load">load</a></td>
<td>$ cat foo.gnuplot<br />
print "loaded!"<br />
<br />
$ cat main.gnuplot<br />
load 'foo.gnuplot'<br />
<br />
$ gnuplot main.gnuplot<br />
loaded!</td>
<td><span style="color: gray"><em>none</em></span></td>
</tr>
<tr>
<th colspan="3"><a name="processes-environment"></a><a href="#processes-environment-note">processes and environment</a></th>
</tr>
<tr>
<th></th>
<th>gnuplot</th>
<th>glpk</th>
</tr>
<tr>
<td><a name="cmd-line-arg"></a><a href="#cmd-line-arg-note">command line arguments</a></td>
<td>$ cat test.gnuplot<br />
print foo<br />
print bar<br />
<br />
$ gnuplot -e 'foo=3; bar=4' test.gnuplot</td>
<td><span style="color: gray"><em>none</em></span></td>
</tr>
<tr>
<td><a name="exit"></a><a href="#exit-note">exit</a></td>
<td><span style="color: gray"># no way to set exit status:</span><br />
exit<br />
<br />
<span style="color: gray"># or:</span><br />
quit</td>
<td><span style="color: gray"><em>none</em></span></td>
</tr>
<tr>
<td><a name="cmd-subst"></a><a href="#cmd-subst-note">command substitution</a></td>
<td>ls_output = system("ls")</td>
<td><span style="color: gray"><em>none</em></span></td>
</tr>
<tr>
<th colspan="3"><a name="optimization"></a><a href="#optimization-note">optimization</a></th>
</tr>
<tr>
<th></th>
<th>gnuplot</th>
<th>glpk</th>
</tr>
<tr>
<td><a name="linear-min"></a><a href="#linear-min-note">linear minimization</a></td>
<td> </td>
<td>var x1;<br />
var x2;<br />
var x3;<br />
<br />
minimize obj: x1 + x2 + x3;<br />
<br />
c1: x1 + x2 >= 1;<br />
c2: x2 + x3 >= 1;<br />
c3: x1 + x3 >= 1;<br />
<br />
solve;<br />
display obj, x1, x2, x3;<br />
end;</td>
</tr>
<tr>
<td><a name="decision-var-array"></a><a href="#decision-vary-array-note">decision variable array</a></td>
<td> </td>
<td>set I := {1 .. 3};<br />
var x{I};<br />
<br />
minimize obj: sum{i in I} x[i];<br />
<br />
c1: x[1] + x[2] >= 1;<br />
c2: x[2] + x[3] >= 1;<br />
c3: x[1] + x[3] >= 1;<br />
<br />
solve;<br />
display obj, x;<br />
end;</td>
</tr>
<tr>
<td><a name="linear-max"></a><a href="#linear-max-note">linear maximization</a></td>
<td> </td>
<td>set I := {1 .. 3};<br />
var x{I};<br />
<br />
maximize obj: sum{i in I} x[i];<br />
<br />
c1: x[1] + x[2] <= 1;<br />
c2: x[2] + x[3] <= 1;<br />
c3: x[1] + x[3] <= 1;<br />
<br />
solve;<br />
display obj, x;<br />
end;</td>
</tr>
<tr>
<td><a name="var-declaration-constraint"></a><a href="#var-declaration-constraint-note">constraint in variable declaration</a></td>
<td> </td>
<td>set I := {1 .. 3};<br />
var x{I} >= 0;<br />
<br />
minimize obj: sum{i in I} x[i];<br />
<br />
c1: -x[1] + x[2] >= 1;<br />
c2: x[2] + x[3] >= 1;<br />
c3: -x[1] + x[3] >= 1;<br />
<br />
solve;<br />
display obj, x;<br />
end;</td>
</tr>
<tr>
<td><a name="opt-unbounded-behavior"></a><a href="#opt-unbounded-behavior-note">unbounded behavior</a><br />
<span style="white-space: pre-wrap;"> </span></td>
<td> </td>
<td><span style="color: gray"># Stops execution after solve statement with "LP HAS UNBOUNDED PRIMAL SOLUTION" message.</span></td>
</tr>
<tr>
<td><a name="opt-infeasible-behavior"></a><a href="#opt-infeasible-behavior-note">infeasible behavior</a></td>
<td> </td>
<td><span style="color: gray"># Stops execution after solve statement with "LP HAS NO PRIMAL FEASIBLE SOLUTION" message.</span></td>
</tr>
<tr>
<td><a name="int-decision-var"></a><a href="#int-decision-var-note">integer decision variable</a></td>
<td> </td>
<td>set I := {1 .. 3};<br />
var x{I} integer;<br />
<br />
minimize obj: sum{i in I} x[i];<br />
<br />
c1: x[1] + x[2] >= 1;<br />
c2: x[2] + x[3] >= 1;<br />
c3: x[1] + x[3] >= 1;<br />
<br />
solve;<br />
display obj, x;<br />
end;</td>
</tr>
<tr>
<td><a name="int-binary-var"></a><a href="#int-binary-var-note">binary decision variable</a></td>
<td> </td>
<td>set I := {1 .. 3};<br />
var x{I} binary;<br />
<br />
minimize obj: sum{i in I} x[i];<br />
<br />
c1: x[1] + x[2] >= 1;<br />
c2: x[2] + x[3] >= 1;<br />
c3: x[1] + x[3] >= 1;<br />
<br />
solve;<br />
display obj, x;<br />
end;</td>
</tr>
<tr>
<th colspan="3"><a name="linear-regression"></a><a href="#linear-regression-note">linear regression</a></th>
</tr>
<tr>
<th></th>
<th>gnuplot</th>
<th>glpk</th>
</tr>
<tr>
<th colspan="3"><a name="bar-charts"></a><a href="#bar-charts-note">bar charts</a></th>
</tr>
<tr>
<th></th>
<th>gnuplot</th>
<th>glpk</th>
</tr>
<tr>
<td><a name="vertical-bar-chart"></a><img src="http://cdn.hyperpolyglot.org/images/vertical-bar-chart.jpg" alt="http://cdn.hyperpolyglot.org/images/vertical-bar-chart.jpg" class="image" /><br />
<a href="#vertical-bar-chart-note">vertical bar chart</a></td>
<td><span style="color: gray"># echo $'7\n3\n8\n5\n5' > data.txt</span><br />
<br />
set terminal png size 400,300<br />
set output "barchart.png"<br />
set boxwidth 0.9<br />
set style fill solid 1.0<br />
plot [0:6] [0:9] "data.txt" \<br />
<span style="white-space: pre-wrap;"> </span>using 0:1 notitle with boxes</td>
<td> </td>
</tr>
<tr>
<td><a name="horizontal-bar-chart"></a><img src="http://cdn.hyperpolyglot.org/images/horizontal-bar-chart.jpg" alt="http://cdn.hyperpolyglot.org/images/horizontal-bar-chart.jpg" class="image" /><br />
<a href="#horizontal-bar-chart-note">horizontal bar chart</a></td>
<td> </td>
<td> </td>
</tr>
<tr>
<td><a name="stacked-bar-chart"></a><img src="http://cdn.hyperpolyglot.org/images/stacked-bar-chart.jpg" alt="http://cdn.hyperpolyglot.org/images/stacked-bar-chart.jpg" class="image" /><br />
<a href="#stacked-bar-chart-note">stacked bar chart</a></td>
<td> </td>
<td> </td>
</tr>
<tr>
<td><a name="grouped-bar-chart"></a><img src="http://cdn.hyperpolyglot.org/images/grouped-bar-chart.jpg" alt="http://cdn.hyperpolyglot.org/images/grouped-bar-chart.jpg" class="image" /><br />
<a href="#grouped-bar-chart-note">grouped bar chart</a></td>
<td> </td>
<td> </td>
</tr>
<tr>
<td><a name="pie-chart"></a><img src="http://cdn.hyperpolyglot.org/images/pie-chart.jpg" alt="http://cdn.hyperpolyglot.org/images/pie-chart.jpg" class="image" /><br />
<a href="#pie-chart-note">pie chart</a></td>
<td><span style="color: gray"><em>see note</em></span></td>
<td> </td>
</tr>
<tr>
<td><a name="histogram"></a><img src="http://cdn.hyperpolyglot.org/images/histogram.jpg" alt="http://cdn.hyperpolyglot.org/images/histogram.jpg" class="image" /><br />
<a href="#histogram-note">histogram</a></td>
<td><span style="color: gray"># awk '{ print length($0)}' /usr/share/dict/words > hist.txt</span><br />
<br />
set terminal png size 400,300<br />
set output "histogram.png"<br />
binwidth=1<br />
bin(x,width)=width*floor(x/width)<br />
unset key<br />
plot "hist.txt" \<br />
<span style="white-space: pre-wrap;"> </span>using (bin($1,binwidth)):(1.0) \<br />
<span style="white-space: pre-wrap;"> </span>smooth freq with boxes</td>
<td> </td>
</tr>
<tr>
<td><a name="box-plot"></a><img src="http://cdn.hyperpolyglot.org/images/box-plot.jpg" alt="http://cdn.hyperpolyglot.org/images/box-plot.jpg" class="image" /><br />
<a href="#box-plot">box plot</a></td>
<td><span style="color: gray"># seq 100 | awk '{print rand() ** 3}' > data.txt</span><br />
<br />
set terminal png size 400,300<br />
set output "boxplot.png"<br />
set style boxplot outliers pointtype 7<br />
set style data boxplot<br />
set boxwidth 0.2<br />
plot [0.5:1.5] [0.0:1.1] "data.txt" \<br />
<span style="white-space: pre-wrap;"> </span>using (1.0):1:(0.3) notitle</td>
<td> </td>
</tr>
<tr>
<th colspan="3"><a name="scatter-plots"></a><a href="#scatter-plots-note">scatter plots</a></th>
</tr>
<tr>
<th></th>
<th>gnuplot</th>
<th>glpk</th>
</tr>
<tr>
<td><a name="scatter-plot"></a><img src="http://cdn.hyperpolyglot.org/images/scatter-plot.jpg" alt="http://cdn.hyperpolyglot.org/images/scatter-plot.jpg" class="image" /><br />
<a href="#scatter-plot-note">scatter plot</a></td>
<td><span style="color: gray"># seq 50 | awk '{x=rand(); print x,x+rand()}' > data.txt</span><br />
<br />
set terminal png size 400,300<br />
set output "scatter.png"<br />
unset key<br />
plot "data.txt"</td>