-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnotes.xml
7873 lines (7826 loc) · 407 KB
/
notes.xml
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
<sheaf title="Algebraic Algorithms"
subtitle="Introductory Number Theory and Abstract Algebra for Computer Science Applications"
author="Andrei Lapets"
authorlink="http://lapets.io"
>
<section title="Introduction, Background, and Motivation">
<subsection title="Overview">
<text><![CDATA[
When many real-world problems are addressed or solved mathematically and computationally, the details of those problems are abstracted away until they can be represented directly as idealized mathematical structures (e.g., numbers, sets, trees, graphs, matrices, and so on). In this course, we will study a collection of such idealized mathematical objects: integers, residues, groups, isomorphisms, and several others. We will see how these structures and their properties can be used for implementing useful computational solutions to problems such as random number generation, prime number generation, error correction, trusted and distributed storage and computation, secure communication, and others.
]]></text>
<paragraph><![CDATA[
In covering the material for this course, we will use the standard language and conventions for discussing these mathematical structures that have been developed by the community of mathematicians over the course of history. You will need to become familiar with these conventions in order to find, identify, and use the structures and techniques that have already been developed for representing and solving certain computational problems. At the same time, we will also learn how modern programming languages and programming paradigms can be used to implement these structures and algorithms both accessibly and efficiently.
]]></paragraph>
<paragraph><![CDATA[
The development and application of mathematics involves <i>abstraction</i>. A problem can be viewed at multiple levels of abstraction, and in developing mathematics humans have adopted a variety of techniques that allow them to successfully employ abstraction to study natural phenomena and solve problems.
]]></paragraph>
<table id="5a30782285aa11e5af63feff819cdc9f"><![CDATA[
<table class="fig_table">
<tr>
<td><b>symbolic</b></td>
<td><b>abstract meaning</b></td>
<td><b>concrete meaning in<br/>application domain</b></td>
</tr>
<tr>
<td>2 + 3</td>
<td>5</td>
<td>five objects</td>
</tr>
<tr>
<td>{(1, 2), (1, 3)}</td>
<td>acyclic graph</td>
<td>file system</td>
</tr>
<tr>
<td>{(1, 2), (2, 3), (3, 1)}</td>
<td>graph with cycle</td>
<td>network</td>
</tr>
<tr>
<td>{(0,1), (1,2), (2,0)}</td>
<td>permutation</td>
<td>random number sequence</td>
</tr>
</table>
]]></table>
<text><![CDATA[
The above illustrates the different levels of abstraction that may exist for a given problem. We employ a <i>language</i> of <i>symbols</i> to denote certain abstract structures, which may correspond to actual structures in the world. A string of symbols corresponds to a particular abstract object. Notice that the actual object being modeled and the abstract structure behave the same way, and that this behavior implies certain rules about how we can manipulate the symbols without changing the object that they name. For example, we can represent the same graph using the two strings of symbols "{(1,2), (2,3), (3,1)}" and "{(1,2), (2,3), (3,1)}", or the same number of objects using "2 + 3", "3 + 2", "1 + 4", and so on.
]]></text>
<paragraph><![CDATA[
In this course, we will begin to reviewing the terminology and concepts of logic, integer arithmetic, and set theory, which we will use throughout the course. We will then show that the algebraic properties of the integers also apply to congruence classes of integers (i.e., the properties of modular arithmetic operations), and we will derive and utilize theorems that have useful computer science applications (such as for generating random numbers and creating cryptographic protocols). We will then go further and show that some of the algebraic properties that hold in integer and modular arithmetic can also apply to any data structure, and we will study how to recognize and take advantage of these properties.
]]></paragraph>
</subsection>
<subsection title="Informal motivating example: random number generation">
<text hooks="math"><![CDATA[
Let us informally consider the problem of generating a sequence of random positive integers. Random number generators are needed in many situations and applications, including:
<ul>
<li>generating unique identifiers for database records, objects, etc.;</li>
<li>generating a one-time pad for a simple encryption scheme;</li>
<li>generating public and private keys for more sophisticated encryption and signature schemes;</li>
<li>simulation and approximation methods that employ random sampling (Monte-Carlo, and so on).</li>
</ul>
Different applications will impose different requirements on what is and is not a sufficiently "random" sequence of number. Suppose we adopt the following method:
<ul>
<li>%n_0 = a number in the range (inclusive) 0 to 5;</li>
<li>%n_{%i} = (2 \cdot %n_{%i-1} + 1) \mod 6.</li>
</ul>
We can consider another method:
<ul>
<li>%n_0 = an initial seed integer 10^4 > %n \geq 10^3;</li>
<li>%n_{%i} = only the last four digits of %n_{%i-1}^2.</li>
</ul>
Frequent repetition of a sequence may or may not be allowed in our given application. Does the above method produce repeating numbers? How often? For how many initial seeds? How do we choose a good seed? We can measure a physical process or component (a clock, a keyboard), but even under these circumstances we need a way to reason about the range of random values the measurement produces, and the range of random values the application requires. How do we begin to approach and formally characterize these aspects of the problem so that we are certain we are meeting the requirements imposed by the application?
]]></text>
<paragraph hooks="math"><![CDATA[
One way to model a random number generation process is to view it is a permutation. In fact, there is more than one way to view the process as a permutation. We could simply count up from 0 to %m and apply the same permutation to each 0 \leq %n \leq %m in order to produce the %nth random number in the sequence. Is there an efficient way (i.e., using no more memory than O(\log %m)) to compute a random number from each %n such that a number never repeats?
]]></paragraph>
<paragraph hooks="math"><![CDATA[
In this course we will learn about a variety of mathematical structures and their properties that will allow us to precisely specify the above problem and others like it, to identify what solutions are appropriate for such a problem, and to implement these solutions correctly and, where necessary, efficiently.
]]></paragraph>
</subsection>
</section>
<section title="Review of Logic with Sets, Relations, and Operators">
<text><![CDATA[
In this section, we will review several abstract structures and associated properties (and the symbolic language used to represent them) that you should have already encountered in past courses. Simultaneously, we will review one way in which these structures can be implemented and manipulated within the modern programming language Python.
]]></text>
<text><![CDATA[
As with most human languages that have developed organically over time, mathematics has a rich and often redundant vocabulary. We introduce many terms in this section that we will use consistently in this course. However, keep in mind that there are often other synonyms within mathematics and computer science for these structures.
]]></text>
<subsection title="Formulas without quantifiers">
<definition required="true" hooks="math" id="47254608df414ace8d04c630a2b15689">
<text><![CDATA[
A <i>logical formula</i> or <i>formula</i> is a string of symbols that follow a certain syntax. If the formula is written using a correct syntax, we can ask about its <i>meaning</i> (i.e., is the formula <i>true</i> or <i>false</i>). The symbols <i>or</i>, <i>and</i>, <i>not</i>, <i>implies</i>, and <i>iff</i> are <i>logical operators</i>.
]]></text>
<paragraph><![CDATA[
The basic building blocks (a.k.a., base cases) for formulas are <i>true</i>, <i>false</i>, and <i>predicates</i>. When a formula consists of only one of these (and no operators), it is an <i>atomic formula</i>. Like any formula, each atomic formula has a particular meaning (it is either true or it is false). Atomic formulas can be combined using logical operators to build up larger formulas. The table below provides a way to determine the meaning of a formula by breaking it down into its constituent parts.
]]></paragraph>
<table><![CDATA[
<table class="fig_table">
<tr>
<td><b>formula</b></td>
<td><b>meaning</b></td>
<td><b>example of one possible<br/>Python representation</b></td>
</tr>
<tr>
<td>true</td>
<td>always true</td>
<td><code>True</code></td>
</tr>
<tr>
<td>false</td>
<td>always false</td>
<td><code>False</code>
</td>
</tr>
<tr>
<td>%f_1 and %f_2</td>
<td>only true if both %f_1 and %f_2 are true</td>
<td><code>True and False</code></td>
</tr>
<tr>
<td>%f_1 or %f_2</td>
<td>true if %f_1 or %f_2 (or both) are true</td>
<td><code>True or (False and True)</code></td>
</tr>
<tr>
<td>%f_1 implies %f_2</td>
<td>
<ul style="margin-left:-22px;">
<li>if %f_1 is true, then %f_2 must be true</li>
<li>%f_1 is false, or %f_2 is true</li>
<li>%f_1 is "less than or equal to" %f_2<br/>(if false is 0 and true is 1)</li>
</ul>
</td>
<td><code>False <= True</code></td>
</tr>
<tr>
<td>%f_1 iff %f_2</td>
<td>
<ul style="margin-left:-22px;">
<li>%f_1 is true if and only if %f_2 is true</li>
<li>%f_1 and %f_2 are either<br/>both true or both false</li>
</ul>
</td>
<td><code>True == False</code></td>
</tr>
<tr>
<td>\neg %f</td>
<td>true if %f is false</td>
<td><code>not (True or (False and True))</code></td>
</tr>
<tr>
<td>( %f )</td>
<td>true if %f is true</td>
<td><code>(True and (not (False))</code></td>
</tr>
<tr>
<td>%[predicate example]%</td>
<td>depends on the definition<br/>of the predicate</td>
<td><code>isPrime(7)</code></td>
</tr>
</table>
]]></table>
<text><![CDATA[
A predicate can have zero or more arguments. Whether a given atomic formula consisting of a predicate that takes at least one argument is true or false depends on the arguments supplied to it. For example, we see above for the predicate <code>isPrime(?)</code> that takes one argument, the meaning of <code>isPrime(7)</code> should be true but the meaning of <code>isPrime(4)</code> should be false.
]]></text>
</definition>
<text hooks="math"><![CDATA[
The following table may help with gaining a good intuition for the meaning of the <i>implies</i> operator.
]]></text>
<table id="5a30890285aa11e5af63feff819cdc9f" hooks="math"><![CDATA[
<table class="fig_table">
<tr>
<td><b>meaning of<br/>left-hand side<br/>(premise)</b></td>
<td><b>meaning of<br/>right-hand side<br/>(conclusion)</b></td>
<td><b>meaning of<br/>entire formula</td>
<td><b>comments</td>
</tr>
<tr>
<td>true</td>
<td>true</td>
<td>true</td>
<td>if the premise is true and the conclusion<br/>is true, the claim of implication is true;<br/><br/>thus, the whole formula is true</td>
</tr>
<tr>
<td>true</td>
<td>false</td>
<td>false</td>
<td>if the premise is true but the conclusion is<br/>false, the conclusion is <i>not</i> implied<br/>by the premise, so the
claim of implication<br/>is false; thus, the formula is false</td>
</tr>
<tr>
<td>false</td>
<td>true</td>
<td>true</td>
<td>if the conclusion is true on its own, it doesn't matter<br/>that the premise is false, because <i>anything</i> implies<br/>an
independently true conclusion; thus, the claim<br/>of implication is true, and so is the<br/>entire formula</td>
</tr>
<tr>
<td>false</td>
<td>false</td>
<td>true</td>
<td>if we assume that a false premise is true, then "false"<br/>itself is "true"; in other words, false<br/>implies itself, so
the formula is true</td>
</tr>
</table>
]]></table>
<example required="true" hooks="math" id="0dd54f9ef9a248f4aaa7a99c88e62099">
<text><![CDATA[
Suppose we have the following formula involving two predicates %[the sun is visible]% and %[it is daytime]%:
\begin{eqnarray}
%[the sun is visible]% \Rightarrow %[it is daytime]%
\end{eqnarray}
This formula might describe a property of our real-world experience of a person that is in a particular fixed location on the surface of the Earth. We could state that the above formula is <i>always</i> true (i.e., it is always an accurate description of the system it describes). For every possible assignment of values to each variable, the above formula is indeed accurate, in that it is true exactly in those situations that might occur on Earth, and false in any situation that cannot occur:
]]></text>
<table><![CDATA[
<table class="fig_table">
<tr>
<td><b>%[the sun is visible]%</b></td>
<td><b>%[it is daytime]%</b></td>
<td><b>meaning</b></td>
<td><b>interpretation</b></td>
</tr>
<tr>
<td>true</td>
<td>true</td>
<td><i>true</i></td>
<td>a sunny day</td>
</tr>
<tr>
<td>true</td>
<td>false</td>
<td><i>false</i></td>
<td></td>
</tr>
<tr>
<td>false</td>
<td>true</td>
<td><i>true</i></td>
<td>a cloudy day</td>
</tr>
<tr>
<td>false</td>
<td>false</td>
<td><i>true</i></td>
<td>nighttime</td>
</tr>
</table>
]]></table>
<text><![CDATA[
In particular, only one set of values causes the formula to be false: if the sun is in the sky, but it is not daytime. This is indeed impossible; all the others are possible (it may be day or night, or it may be cloudy during the day). The <i>contrapositive</i> of the formula is true if the formula is true:
\begin{eqnarray}
\neg(%[it is daytime]%) \Rightarrow \neg(%[the sun is visible]%)
\end{eqnarray}
Notice that the <i>contrapositive</i> of the above is a direct result of the fact that if %[the sun is visible]% \Rightarrow %[it is daytime]% must be true, the rows in the truth table in which it is false must be ignored, and then the only possible row in the truth table in which %[it is daytime]% is false is the one in which %[the sun is visible]% is also false.
]]></text>
</example>
</subsection>
<subsection title="Terms: integers and term operators that take integer inputs">
<definition required="true" hooks="math" id="e601deb568ed46a1a1d741907a6dcfa9">
<text><![CDATA[
A <i>term</i> is a string of symbols that represents some kind of mathematical structure. In our case, terms will initially
represent integers or sets of integers. Terms may contain <i>term operators</i>. We can view these as functions that take
terms as input and return terms as output. The term operators for terms that represent integers with which we will
be working are +, -, \cdot, and mod.
]]></text>
<table><![CDATA[
<table class="fig_table">
<tr>
<td><b>term</b></td>
<td><b>what it represents</b></td>
<td><b>example of one possible<br/>Python representation</b></td>
</tr>
<tr>
<td>0</td>
<td>0</td>
<td><code>0</code></td>
</tr>
<tr>
<td>1</td>
<td>1</td>
<td><code>1</code></td>
</tr>
<tr>
<td>%z_1 + %z_2</td>
<td>the integer sum of %z_1 and %z_2</td>
<td><code>3 + 4</code>
</td>
</tr>
<tr>
<td>%z_1 %- %z_2</td>
<td>the integer difference of %z_1 and %z_2</td>
<td><code>(1 + 2) - 4</code>
</td>
</tr>
<tr>
<td>%z_1 \cdot %z_2</td>
<td>the integer product of %z_1 and %z_2</td>
<td><code>3 * 5</code>
</td>
</tr>
<tr>
<td>%z_1 \mod %z_2</td>
<td>the remainder of the integer quotient %z_1 / %z_2<br/>%z_1 - \lfloor %z_1/%z_2 \rfloor \cdot %z_2</td>
<td><code>17 % 5</code>
</td>
</tr>
<tr>
<td>%z_1^{%z_2}</td>
<td>product of %z_2 instances of %z_1</td>
<td><code>2**3</code><br/><code>pow(2,3)</code>
</td>
</tr>
</table>
]]></table>
</definition>
</subsection>
<subsection title="Formulas: relational operators and predicates dealing with integers">
<definition required="true" hooks="math" id="db9e874ec6b6475a93bc3ef4db0f2066">
<text><![CDATA[
A term can only appear in a formula if it is an argument to a <i>predicate</i>. A few common predicates involving integers are
represented using <i>relational operators</i> (e.g, \leq, \geq).
]]></text>
<table><![CDATA[
<table class="fig_table">
<tr>
<td><b>formula</b></td>
<td><b>what it represents</b></td>
<td><b>example of one possible<br/>Python representation</b></td>
</tr>
<tr>
<td>%z_1 = %z_2</td>
<td>true if %z_1 and %z_2<br/>have the same meaning;<br/>false otherwise</td>
<td><code>1 == 2</code></td>
</tr>
<tr>
<td>%z_1 < %z_2</td>
<td>true if %z_1 is less than %z_2;<br/>false otherwise</td>
<td><code>4 < 3</code></td>
</tr>
<tr>
<td>%z_1 > %z_2</td>
<td>true if %z_1 is greater than %z_2;<br/>false otherwise</td>
<td><code>4 > 3</code></td>
</tr>
<tr>
<td>%z_1 \leq %z_2</td>
<td>true if %z_1 is less than or equal to %z_2;<br/>false otherwise</td>
<td><code>4 <= 3</code></td>
</tr>
<tr>
<td>%z_1 \geq %z_2</td>
<td>true if %z_1 is greater than or equal to %z_2;<br/>false otherwise</td>
<td><code>4 >= 3</code></td>
</tr>
<tr>
<td>%z_1 \neq %z_2</td>
<td>true if %z_1 is not equal to %z_2;<br/>false otherwise</td>
<td><code>4 != 3</code></td>
</tr>
</table>
]]></table>
</definition>
<example required="true" hooks="math" id="3c944fff48f246aba9409f76e48ccef4">
<text><![CDATA[
We can define our own predicates as well. Notice that one way we can represent these in Python is by defining a function that
returns a boolean result.
]]></text>
<table><![CDATA[
<table class="fig_table">
<tr>
<td><b>predicate definition</b></td>
<td><b>example of one possible<br/>Python representation</b></td>
</tr>
<tr>
<td>%P(%x) iff %x > 0 and %x < 2</td>
<td><code>def P(x): return x > 0 and x < 2</code></td>
</tr>
<tr>
<td>%Q(%x) iff %x > 3</td>
<td><code>Q = lambda x: x > 3</code></td>
</tr>
</table>
]]></table>
<table><![CDATA[
<table class="fig_table">
<tr>
<td><b>formula</b></td>
<td><b>what it represents</b></td>
<td><b>example of one possible<br/>Python representation</b></td>
</tr>
<tr>
<td>%P(1)</td>
<td>true</td>
<td><code>P(1)</code></td>
</tr>
<tr>
<td>%P(1) or %P(2)</td>
<td>true</td>
<td><code>P(1) or P(2)</code>
</td>
</tr>
<tr>
<td>%Q(1) and %Q(2)</td>
<td>false</td>
<td><code>Q(1) and Q(2)</code>
</td>
</tr>
</table>
]]></table>
</example>
<text hooks="math"><![CDATA[
We will use the following predicates throughout the course.
]]></text>
<definition required="true" hooks="math" id="3f625b68bc8a426c80fe1c774e04c362"><![CDATA[
For any %x,%y \in \Z, %x | %y iff %y/%x \in \Z. If %x | %y, we then say that %x is a <i>factor</i> of %y.
]]></definition>
<definition required="true" hooks="math" id="2c7cc0e69813454eafc7128e5ac57055"><![CDATA[
For any %y \in \Z, %y is prime iff for any integer %x where 2 \leq %x < %y, it is not true that %x | %y. In other words, %y is prime if its only factors are 1 and %y (itself).
]]></definition>
<table id="5a307cd285aa11e5af63feff819cdc9f" hooks="math"><![CDATA[
<table class="fig_table">
<tr>
<td><b>formula</b></td>
<td><b>what it represents</b></td>
</tr>
<tr>
<td>%x | %y</td>
<td>
<ul style="margin-left:-22px;">
<li>%y / %x \in \Z</li>
<li>%x divides %y</li>
<li>%y is divisible by %x</li>
<li>%y is an integer multiple of %x</li>
<li>%y \mod %x = 0</li>
<li>%x is a factor of %y</li>
</ul>
</td>
</tr>
<tr>
<td>%y is prime</td>
<td>
<ul style="margin-left:-22px;">
<li>%y > 1 and<br/>%x | %y implies %x = 1 or %x = %y</li>
<li>%y > 1 and<br/>%y is divisible only by 1 and itself</li>
</ul>
</td>
</tr>
</table>
]]></table>
<example required="true" id="78a1e0ed892346a396f55cd665904b0d">
<text hooks="math"><![CDATA[
We can define the divisibility and primality predicates in Python in the following way:
]]></text>
<code class="py"><![CDATA[
def divides(x, y):
return y % x == 0 # The remainder of y/x is 0.
def prime(y):
for x in range(2,y):
if divides(x,y):
return False
return True
]]></code>
</example>
<example required="true" id="1231e0ed892346a396f55cd665904b0d">
<text hooks="math"><![CDATA[
We can gradually generalize our primality predicate from the <a href="#78a1e0ed892346a396f55cd665904b0d">previous example</a> to work for any other predicate. Note that we restate the property slightly: a number is prime if no smaller number can divide it evenly, so if we ever find one that doesn't satisfy this property, we immediately return <code>False</code>. This is effectively the implementation of a <i>quantifier</i>, which we <a href="#2cd418f2876c42d59a57e34bd6288f22">introduce</a> further below.
]]></text>
<code class="py"><![CDATA[
def doesNotDivide(x, y):
return y % x != 0 # The remainder of y/x is nonzero.
def prime(y):
for x in range(2,y):
if not doesNotDivide(x,y):
return False
return True
def checkAll(S, P):
for x in S:
if not P(x):
return False
return True
]]></code>
<text hooks="math"><![CDATA[
Given the above, it is now possible to get the same behavior provided by <code>prime()</code> by supplying appropriate arguments:
]]></text>
<code class="py"><![CDATA[
>>> checkAll(set(range(2,y)), lambda x: doesNotDivide(x,y))
]]></code>
</example>
<definition required="true" hooks="math" id="cdf2df96b7924749a37bcbc2f4415180"><![CDATA[
For any %x,%y \in \Z, %x is a <i>proper factor</i> of %y iff %y/%x \in \Z and %x < %y.
]]></definition>
</subsection>
<subsection title="Terms: finite sets of integers, term operators that take set inputs, and set comprehensions">
<definition required="true" hooks="math" id="8c1203dd65bb49868abc64ad5353725f">
<text><![CDATA[
A finite set of integers is an unordered, finite collection of zero or more integers with no duplicates. The following are examples of terms the meaning of which is a finite set of integers (with the exception of the set size terms, the meaning of
which is a positive integer).
]]></text>
<table><![CDATA[
<table class="fig_table">
<tr>
<td><b>term</b></td>
<td><b>what it represents</b></td>
<td><b>example of one possible<br/>Python representation</b></td>
</tr>
<tr>
<td>\emptyset</td>
<td>a set with no elements in it</td>
<td><code>set()</code></td>
</tr>
<tr>
<td>{1,2,3}</td>
<td>{1,2,3}</td>
<td><code>{1,2,3}</code></td>
</tr>
<tr>
<td>{2,..,5}</td>
<td>{2,3,4,5}</td>
<td><code>set(range(2,6))</code></td>
</tr>
<tr>
<td>{ %x | %x \in {1,2,3,4,5,6}, %x > 3 }</td>
<td>{4,5,6}</td>
<td><code>{x for x in {1,2,3,4,5,6} if x > 3}</code>
</td>
</tr>
<tr>
<td>|{1,2,3,4}|</td>
<td>4</td>
<td><code>len({1,2,3,4})</code>
</td>
</tr>
</table>
]]></table>
<text><![CDATA[
The following are term operators on terms the meaning of which is a finite set of integers.
]]></text>
<table><![CDATA[
<table class="fig_table">
<tr>
<td><b>term</b></td>
<td><b>what it represents</b></td>
<td><b>example of one possible<br/>Python representation</b></td>
</tr>
<tr>
<td>%S_1 \cup %S_2</td>
<td>{%z | %z \in \Z, %z \in %S_1 or %z \in %S_2}</td>
<td><code>{1,2,3}.union({4,5})</code><br/><code>{1,2,3} | {4,5}</code></td>
</tr>
<tr>
<td>%S_1 \cap %S_2</td>
<td>{%z | %z \in \Z, %z \in %S_1 and %z \in %S_2}</td>
<td><code>{1,2,3}.intersection({2,3,5})</code><br/><code>{1,2,3} & {2,3,5}</code></td>
</tr>
<tr>
<td>|%S|</td>
<td>the number of elements in %S</td>
<td><code>len({1,2,3})</code></td>
</tr>
</table>
]]></table>
</definition>
<text><![CDATA[
While the terms below do not represent <i>finite</i> sets of integers, we introduce the following two set terms in order to reference them throughout the notes.
]]></text>
<definition required="true" id="19dc317f45ec4ae18018ca5b17fce114">
<text hooks="math"><![CDATA[
Let \Z be the set of all integers, and let \N be the set of all non-negative integers (i.e., positive integers and 0).
]]></text>
<table id="5a3081a085aa11e5af63feff819cdc9f" hooks="math"><![CDATA[
<table class="fig_table">
<tr>
<td><b>term</b></td>
<td><b>what it represents</b></td>
</tr>
<tr>
<td>\N</td>
<td>{0, 1, 2, ...}</td>
</tr>
<tr>
<td>\Z</td>
<td>{..., -2, -1, 0, 1, 2, ...}</td>
</tr>
</table>
]]></table>
</definition>
</subsection>
<subsection title="Formulas: quantifiers over finite sets of integers">
<definition required="true" id="2cd418f2876c42d59a57e34bd6288f22">
<text hooks="math"><![CDATA[
Suppose we define the following two Python functions that take predicates (or, more specifically, functions that represent predicates) as input.
]]></text>
<code class="py"><![CDATA[
def forall(S, P):
for x in S:
if not P(x):
return False
return True
def exists(S, P):
for x in S:
if P(x):
return True
return False
]]></code>
<text hooks="math"><![CDATA[
We could redefine the above using comprehensions. We will also introduce a <code>subset()</code> operation on sets.
]]></text>
<code class="py"><![CDATA[
def forall(X, P):
S = {x for x in X if P(x)}
return len(S) == len(X)
def exists(X, P):
S = {x for x in X if P(x)}
return len(S) > 0
def subset(X,Y):
return forall(X, lambda x: x in Y)
]]></code>
<text hooks="math"><![CDATA[
Then we can introduce the following definitions and corresponding Python examples.
]]></text>
<table hooks="math"><![CDATA[
<table class="fig_table">
<tr>
<td><b>formula</b></td>
<td><b>what it represents</b></td>
<td><b>example of one possible<br/>Python representation</b></td>
</tr>
<tr>
<td>1 \in {1,2,3}</td>
<td>true</td>
<td><code>1 in {1,2,3}</code></td>
</tr>
<tr>
<td>4 \in {1,2,3}</td>
<td>false</td>
<td><code>4 in {1,2,3}</code></td>
</tr>
<tr>
<td>\forall %x \in {1,2,3}, %x > 0 and %x < 4</td>
<td>true</td>
<td><code>forall({1,2,3}, lambda x: x > 0 and x < 4)</code></td>
</tr>
<tr>
<td>\exists %x \in {1,2,3}, %x < 1 and %x > 3</td>
<td>false</td>
<td><code>exists({1,2,3}, lambda x: x < 1 or x > 3)</code></td>
</tr>
<tr>
<td>\forall %x \in \emptyset, %f</td>
<td>true</td>
<td></td>
</tr>
<tr>
<td>\exists %x \in \emptyset, %f</td>
<td>false</td>
<td></td>
</tr>
</table>
]]></table>
</definition>
<text hooks="math"><![CDATA[
Notice that when we quantify over an empty set with a universal quantifier \forall, the formula is always true. When we quantify over an empty set
with an existential quantifier, the formula is always false (since no element satisfying any formula could exist if no elements exist at all). We can see that the Python
functions for these quantifiers are consistent with this interpretation.
]]></text>
<fact required="true" hooks="math" id="3cd40be053794f17b638f78d66b7c7aa"><![CDATA[
Let %X = {%x_1 , ..., %x_{%n}} be a finite set and let %P be a predicate that applies to a single integer argument. Then we have the following correspondences between quantifiers and logical operators:
\begin{eqnarray}
\forall %x \in %X, %P(%x) %~ & iff & %~ %P(%x_1) and %P(%x_2) and %P(%x_3) and ... and %P(%x_{%n}) \\
\exists %x \in %X, %P(%x) %~ & iff & %~ %P(%x_1) or %P(%x_2) or %P(%x_3) or ... or %P(%x_{%n})
\end{eqnarray}
Notice that if %X is empty, the "base case" for \forall must be true (since that is the identity of the <b>and</b> logical operator), while the "base case" for \exists must be false (since that is the identity of the <b>or</b> logical operator).
]]></fact>
<exercise required="true" id="5917f6f5de7c46079190f4b6c4961ca3">
<text hooks="math"><![CDATA[
Implement Python functions that correspond to formulas which can be used to define each of the following statements about
a set %X and a predicate %P.
]]></text>
<unorderedlist>
<item>
<text hooks="math"><![CDATA[All the elements of a set %X satisfy the predicate %P.]]></text>
<code class="py"><![CDATA[
# We provide two equivalent implementations.
def all(X, P):
return forall(X, P)
def all(X, P):
S = {x for x in X if P(x)}
return len(S) == len(X)
]]></code>
</item>
<item>
<text hooks="math"><![CDATA[None of the elements of a set %X satisfy the predicate %P.]]></text>
<code class="py"><![CDATA[
# We provide two equivalent implementations.
def none(X, P):
return forall(X, lambda x: not P(x))
def none(X, P):
S = {x for x in X if P(x)}
return len(S) == 0
]]></code>
</item>
<item>
<text hooks="math"><![CDATA[At most one of the elements of a set %X satisfy the predicate %P.]]></text>
<code class="py"><![CDATA[
def atMostOne(X, P):
S = {x for x in X if P(x)}
return len(S) <= 1
]]></code>
</item>
<item>
<text hooks="math"><![CDATA[At least one of the elements of a set %X satisfy the predicate %P.]]></text>
<code class="py"><![CDATA[
# We provide two equivalent implementations.
def atLeastOne(X, P):
return exists(X, P)
def atLeastOne(X, P):
S = {x for x in X if P(x)}
return len(S) >= 1
]]></code>
</item>
</unorderedlist>
</exercise>
<exercise required="true" id="69acaf35c3e74e6593beb864d3f3ea6f">
<text hooks="math"><![CDATA[
Use quantifiers to implement a Python function corresponding to the predicate %[%p is prime]% for any integer %p.
]]></text>
<code class="py"><![CDATA[
def prime(p):
return p > 1 and forall(set(range(2, p)), lambda n: p % n != 0)
]]></code>
</exercise>
</subsection>
<subsection title="Formulas: predicates dealing with finite sets of integers">
<definition required="true" hooks="math" id="a53c6952efd64145a6fa15dd570b1d24">
<text><![CDATA[
The following are examples of formulas that contain relational operators dealing with finite sets of integers.
]]></text>
<table><![CDATA[
<table class="fig_table">
<tr>
<td><b>formula</b></td>
<td><b>what it represents</b></td>
<td><b>example of one possible<br/>Python representation</b></td>
</tr>
<tr>
<td>3 \in {1,2,3}</td>
<td>true</td>
<td><code>3 in {1,2,3}</code></td>
</tr>
<tr>
<td>{1,2} \subset {1,2,3}</td>
<td>true</td>
<td><code>subset({1,2}, {1,2,3})</code></td>
</tr>
<tr>
<td>{4,5} \subset {1,2,3}</td>
<td>false</td>
<td><code>subset({4,5}, {1,2,3})</code></td>
</tr>
</table>
]]></table>
<text><![CDATA[
Below are the general forms of formulas containing relational operators dealing with finite sets of integers.
]]></text>
<table><![CDATA[
<table class="fig_table">
<tr>
<td><b>formula</b></td>
<td><b>what it represents</b></td>
</tr>
<tr>
<td>%z \in %S</td>
<td>true if %z is an element of %S; false otherwise</td>
</tr>
<tr>
<td>%S_1 \subset %S_2</td>
<td>\forall %z \in %S_1, %z \in %S_2</td>
</tr>
<tr>
<td>%S_1 = %S_2</td>
<td>%S_1 \subset %S_2 and %S_2 \subset %S_1</td>
</tr>
</table>
]]></table>
</definition>
</subsection>
<subsection title="Terms: set products and binary relations">
<definition required="true" hooks="math" id="95e72ad05e66427281549720c9ed975f"><![CDATA[
The product of two sets %X and %Y is denoted %X \times %Y and is defined to be the set of <i>ordered</i> pairs
(%x,%y) for every possible combination of %x \in %X and %y \in %Y.
]]></definition>
<example required="true" hooks="math" id="6d31feaec48d48938ca705b2d75ab946">
<table><![CDATA[
<table class="fig_table">
<tr>
<td><b>term</b></td>
<td><b>what it represents</b></td>
<td><b>example of one possible<br/>Python representation</b></td>
</tr>
<tr>
<td>{1,2} \times {5,6,7}</td>
<td>{(1,5),(1,6),(1,7),(2,5),(2,6),(2,7)}</td>
<td><code>{ (x,y) for x in {1,2} for y in {4,5,6,7} }</code></td>
</tr>
</table>
]]></table>
</example>
<definition required="true" hooks="math" id="95e72ad05e66427281549720c9ed875f"><![CDATA[
A set %R is a <i>relation</i> between the sets %X and %Y if %R \subset %X \times %Y. We also say that a set %R is a relation on a set %X if %R \subset %X \times %X.
]]></definition>
<example required="true" hooks="math" id="95dff10354af483da4f3aad312b46d5c"><![CDATA[
Suppose we have the sets %X = {@a, @b, @c} and %Y = {@D, @E, @F}. Then one possible relation between %X and %Y is {(@a, @D), (@c, @E)}. One possible relation on %X is {(@a, @a), (@a, @b), (@a, @c), (@b, @b), (@c, @a)}.
]]></example>
</subsection>
<subsection title="Formulas: predicates dealing with relations">
<text hooks="math"><![CDATA[
There are several common properties that relations may possess.
]]></text>
<table id="5a30863285aa11e5af63feff819cdc9f" hooks="math"><![CDATA[
<table class="fig_table">
<tr>
<td><b>predicate</b></td>
<td><b>definition</b></td>
<td><b>visual example</b></td>
</tr>
<tr>
<td style="background-color:#F6F4D8;">%X \times %Y is the set product of %X and %Y</td>
<td style="background-color:#F6F4D8;">%X \times %Y = { (%x,%y) | %x \in %X, %y \in %Y }</td>
<td><div class="pql" style="height:160px; width:200px;">!relation({'a','b','c'}, {'x','y','z'}, {('a','x'),('a','y'),('a','z'),('b','x'),('b','y'),('b','z'),('c','x'),('c','y'),('c','z')})</div></td>
</tr>
<tr>
<td style="background-color:#F6F4D8;">%R is a relation between %X and %Y</td>
<td style="background-color:#F6F4D8;">%R \subset %X \times %Y</td>
<td><div class="pql" style="height:160px; width:200px;">!relation({'a','b','c'}, {'x','y','z'}, {('a','x'),('b','x'),('b','z'),('c','z')})</div></td>
</tr>
<tr>
<td>%R is a function from %X to %Y<br/>%R is a (many-to-one) map from %X to %Y</td>
<td>
%R is a relation between %X and %Y and<br/>
\forall %x \in %X,<br/>
there is at most one<br/>
%y \in %Y s.t. (%x,%y) \in %R
</td>
<td><div class="pql" style="height:160px; width:200px;">!relation({'a','b','c'}, {'x','y','z'}, {('a','x'),('b','x'),('c','z')})</div></td>
</tr>
<tr>
<td>%R is an injection from %X to %Y</td>
<td>
%R is a function from %X to %Y and<br/>
\forall %y \in %Y,<br/>
there is at most one<br/>
%x \in %X s.t. (%x,%y) \in %R
</td>
<td><div class="pql" style="height:160px; width:200px;">!relation({'a','b','c'}, {'x','y','z'}, {('a','x'),('b','y')})</div></td>
</tr>
<tr>
<td>%R is a surjection from %X to %Y</td>
<td>
%R is a function from %X to %Y and<br/>
\forall %y \in %Y,<br/>
there is at least one<br/>
%x \in %X s.t. (%x,%y) \in %R
</td>
<td><div class="pql" style="height:160px; width:200px;">!relation({'a','b','c','d'}, {'x','y','z'}, {('a','x'),('c','y'),('d','z')})</div></td>
</tr>
<tr>
<td style="background-color:#F6F4D8;">%R is a bijection between %X and %Y</td>
<td style="background-color:#F6F4D8;">%R is an injection from %X and %Y and<br/>%R is a surjection from %X and %Y</td>
<td><div class="pql" style="height:160px; width:200px;">!relation({'a','b','c'}, {'x','y','z'}, {('a','y'),('b','z'),('c','x')})</div></td>
</tr>
<tr>
<td style="background-color:#F6F4D8;">%R is a permutation on %X</td>
<td style="background-color:#F6F4D8;">
%R \subset %X \times %X and<br/>
%R is a bijection between %X and %X
</td>
<td><div class="pql" style="height:160px; width:200px;">!relation({'a','b','c'}, {('a','b'),('b','c'),('c','a')})</div></td>
</tr>
<tr>
<td style="background-color:#F6F4D8;">%R is a reflexive relation on %X</td>
<td style="background-color:#F6F4D8;">%R \subset %X \times %X and<br/>\forall %x \in %X, (%x,%x) \in %R </td>
<td><div class="pql" style="height:160px; width:200px;">!relation({'a','b','c'}, {('a','a'),('b','b'),('c','c')})</div></td>
</tr>
<tr>
<td style="background-color:#F6F4D8;">%R is a symmetric relation on %X</td>
<td style="background-color:#F6F4D8;">%R \subset %X \times %X and<br/>\forall %x \in %X, \forall %y \in %X, (%x,%y) \in %R implies (%y,%x) \in %R</td>
<td><div class="pql" style="height:160px; width:200px;">!relation({'a','b','c'}, {('a','b'),('b','a'),('c','c')})</div></td>
</tr>
<tr>
<td style="background-color:#F6F4D8;">%R is a transitive relation on %X</td>
<td style="background-color:#F6F4D8;">%R \subset %X \times %X and<br/>
\forall %x \in %X, \forall %y \in %X, \forall %z \in %X,<br/>
((%x,%y) \in %R and (%y,%z) \in %R) implies (%x,%z) \in %R
</td>
<td><div class="pql" style="height:160px; width:200px;">!relation({'a','b','c'}, {('a','b'),('b','c'),('a','c')})</div></td>
</tr>
<tr>
<td style="background-color:#F6F4D8;">%R is an equivalence relation on %X<br/>%R is a congruence relation on %X</td>
<td style="background-color:#F6F4D8;">%R \subset %X \times %X and<br/>
%R is a reflexive relation on %X and<br/>
%R is a symmetric relation on %X and<br/>
%R is a transitive relation on %X
</td>
<td><div class="pql" style="height:160px; width:200px;">!relation({'a','b','c'}, {('a','a'),('b','b'),('a','b'),('b','a'),('c','c')})</div></td>
</tr>
</table>
]]></table>
<!--
We introduce several terms that deal with the relationship between the relation and the members and subsets of the two sets that the relation involves.
<table class="fig_table">
<tr>
<td><b>predicate</b></td>
<td><b>required conditions</b></td>
</tr>
<tr>
<td>%X is the domain of %R between %X and %Y</td>
<td>%R is a function from %X to %Y</td>
</tr>
<tr>
<td>%Y is the codomain of %R between %X and %Y</td>
<td>%R is a function from %X to %Y</td>
</tr>
<tr>
<td>%B is the image of %R between %X and %Y</td>
<td>%R is a function from %X to %Y and<br/>
%B = {%y | %x \in %X, (%x',%y) \in %R, %x = %x'}</td>
</tr>
<tr>
<td>%B is the image of %x under %R between %X and %Y</td>
<td>%R is a function from %X to %Y and<br/>
%B = {%y | (%x,%y) \in %R}</td>
</tr>
<tr>
<td>%A is the pre-image of %y under %R between %X and %Y</td>
<td>%R is a function from %X to %Y and<br/>
%A = {%x | (%x,%y) \in %R}</td>
</tr>
</table>
<br/><br/>
<b>Exercise:</b> Implement Python functions that correspond to each of the structures above.
-->
<exercise required="true" id="5aa3dc1edbed4422b77ac051dff6f3b2">
<text hooks="math"><![CDATA[
Define the set of all even numbers between 0 and 100 (inclusive). There are at least two ways we can do this:
]]></text>
<code class="py"><![CDATA[
evens = { 2 * x for x in set(range(0,51)) }
evens = { x for x in set(range(0,101)) if x % 2 == 0 }
]]></code>
</exercise>
<exercise required="true" id="3bd4848251cb47c98a603e19d5beea64">
<text hooks="math"><![CDATA[
Implement a Python function that computes the set product of two sets <code>X</code> and <code>Y</code>.
]]></text>
<code class="py"><![CDATA[
def product(X, Y):
return { (x,y) for x in X for y in Y }
]]></code>
</exercise>
<exercise required="true" id="06bf17302b314e80837ad892aca57106">
<text hooks="math"><![CDATA[
Implement a Python function that takes a finite set of integers and builds the relation on that set correspondingto the operator relational operator \leq.
]]></text>
<code class="py"><![CDATA[
def leq(S):
return { (x, y) for x in S for y in S if x <= y }
]]></code>
</exercise>
<exercise required="true" id="46859e4a64eb47eba16fd09822382e83">
<text hooks="math"><![CDATA[
Implement a Python function that determines whether a relation <code>R</code> is a relation over a set <code>X</code>.
]]></text>
<code class="py"><![CDATA[
# Using our definition of subset().
def relation(R, X):
return subset(R, product(X, X))
# Using the built-in set implementation.
def relation(R, X):
return R.issubset(product(X, X))
]]></code>
</exercise>
<exercise required="true" id="79e0f3993b2e409f95882713c4ad5f5b">
<text hooks="math"><![CDATA[
One property of relations that is studied in other subject areas within computer science and mathematics is <i>asymmetry</i>. We say that %R is an asymmetric relation on a set %X if:
\begin{eqnarray}