-
Notifications
You must be signed in to change notification settings - Fork 3
/
causet.py
1185 lines (1091 loc) · 47.2 KB
/
causet.py
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
#!/usr/bin/env python
'''
Created on 20 Jul 2020
@author: Christoph Minz
@license: BSD 3-Clause
'''
from __future__ import annotations
from typing import Set, Iterable, List, Any, Tuple, Iterator, Union, Optional
from causets.causetevent import CausetEvent # @UnresolvedImport
import numpy as np
import itertools
class Causet(object):
'''
Causal set class to handle operations of a set of `CausetEvent`.
'''
_events: Set[CausetEvent]
def __init__(self, eventSet: Set[CausetEvent] = set()) -> None:
'''
Generates a Causet class instance from a set of `CausetEvent`. The
`CausetEvent` instances are not checked for logical consistency.
'''
while True:
l: int = len(eventSet)
eventSet = Causet.ConeOf(eventSet)
if len(eventSet) == l:
break
self._events: Set[CausetEvent] = eventSet
def __iter__(self) -> Iterator[CausetEvent]:
return iter(self._events)
def __repr__(self) -> str:
return repr(self._events)
@staticmethod
def FromPermutation(P: List[int], labelFormat: Optional[str] = None) -> \
'Causet':
'''
Generates a causal set from the list `P` of permuted integers that
represent a bi-poset (also known as 2D order) that can be embedded in
an Alexandrov subset of 2D Minkowski spacetime.
If the optional argument `labelFormat = None` (default) the integer
values are used to label the `CausetEvent`. Use an empty string '' not
to label any `CausetEvent`, or a format string, for example
'my label {.2f}'.
'''
eventList: List[CausetEvent] = [CausetEvent()] * len(P)
eLabel: Any
for i in range(len(P)):
u: int = i + 1
v: int = P.index(u) + 1
if labelFormat is None:
eLabel = u
elif labelFormat == '':
eLabel = None
else:
eLabel = labelFormat.format(u)
eventList[i] = CausetEvent(
past={eventList[j] for j in range(i)
if P.index(j + 1) + 1 < v},
label=eLabel)
return Causet(set(eventList))
@staticmethod
def NewChain(n: int, labelFormat: Optional[str] = None) -> 'Causet':
'''
Generates a causal set of `n` instances of `CausetEvent` in a causal
chain.
For the optional argument `labelFormat`, see `FromPermutation`.
'''
return Causet.FromPermutation(list(range(1, n + 1)), labelFormat)
@staticmethod
def NewAntichain(n: int, labelFormat: Optional[str] = None) -> 'Causet':
'''
Generates a causal set with `n` spacelike separated `CausetEvent`.
For the optional argument `labelFormat`, see `FromPermutation`.
'''
return Causet.FromPermutation(list(range(n, 0, -1)), labelFormat)
@staticmethod
def NewSimplex(d: int, includeCentralFace: bool = True) -> 'Causet':
'''
Generates a causal set that represents light travelling along the faces
of a d-simplex, where `d` is the space dimension.
'''
vertices: List[CausetEvent] = [CausetEvent(label=str(i))
for i in range(1, d + 2)]
eventSet: Set[CausetEvent] = set(vertices)
for facenumber in range(2, d + 1):
for face_vertices in itertools.combinations(vertices, facenumber):
face_label: str = '-'.join(e.Label
for e in face_vertices)
face_past: Set[CausetEvent] = set()
for pastface_vertices in itertools.combinations(
face_vertices, facenumber - 1):
label: str = '-'.join(e.Label for e in pastface_vertices)
face_past.update({e for e in eventSet if e.Label == label})
eventSet.add(CausetEvent(past=face_past, label=face_label))
if includeCentralFace and (d > 0):
eventSet.add(CausetEvent(past=eventSet.copy(),
label='-'.join(e.Label
for e in vertices)))
return Causet(eventSet)
@staticmethod
def NewFence(length: int, height: int = 1, closed: bool = True) -> \
'Causet':
'''
Generates a fence causal set of `length` (with `(height + 1) * length`
many `CausetEvent`). If `closed` (default), the fence needs flat
spacetime of dimension 1 + 2 to be embedded, otherwise it can also be
embedded in flat spacetime of dimension 1 + 1.
'''
if (length < 1) or (height < 0):
return Causet(set())
elif length == 1:
return Causet.NewChain(height + 1)
else:
loop: List[CausetEvent] = [CausetEvent(label=l)
for l in range(1, length + 1)]
eventSet: Set[CausetEvent] = set(loop)
for h in range(1, height + 1):
offset: int = h * length + 1
next_loop: List[CausetEvent]
if closed:
next_loop = [CausetEvent(past={loop[l - 1], loop[l]},
label=l + offset)
for l in range(length)]
else:
next_loop = [CausetEvent(past={loop[0]}, label=offset)] \
+ [CausetEvent(past={loop[l - 1], loop[l]},
label=l + offset)
for l in range(1, length)]
eventSet.update(next_loop)
loop = next_loop
return Causet(eventSet)
@staticmethod
def NewCrown(length: int = 3) -> 'Causet':
'''
This function is implemented for convenience. It redirects to
`Causet.NewFence` with the default values `height=1` and `closed=True`.
'''
return Causet.NewFence(length, height=1, closed=True)
@staticmethod
def NewKROrder(n: int, rng=np.random.default_rng()):
'''
Returns a new Causet with 3 layers where the first and third layer have
`n` elements and the second layer has `2 * n` events.
Each event in the second layer is linked to a random number of
(possibly zero) events in the first layer. Each event in the third
layer is linked to a random number of (possibly zero) events in the
second layer.
'''
raise NotImplementedError()
@staticmethod
def FromPastMatrix(C: np.ndarray) -> 'Causet':
'''
Converts a logical matrix into a `Causet` object. The entry `C[i, j]`
has to be True or 1 if the event with index j is in the (link) past of
event with index i. If the matrix has less rows than columns, empty
rows are added after the last row. However, if the matrix has more rows
than columns, a ValueError is raised. A ValueError is also raised if
the matrix contains causal loops.
'''
rowcount: int = C.shape[0]
colcount: int = C.shape[1]
if colcount < rowcount:
raise ValueError('The specified matrix cannot be extended ' +
'to a square matrix.')
events: np.ndarray = np.array([CausetEvent(label=i)
for i in range(1, colcount + 1)])
e: CausetEvent
for i in range(rowcount):
e = events[i]
past: Set[CausetEvent] = set(events[np.where(C[i, :])[0]])
future: Set[CausetEvent] = set(events[np.where(C[:, i])[0]])
if (past & future) or (e in past) or (e in future):
raise ValueError('The causet is not anti-symmetric.')
e._prec = past
e._succ = future
# complete pasts and futures (if the input contains links only)
for i in range(colcount):
e = events[i]
e._prec = Causet.PastOf(e._prec, includePresent=True)
e._succ = Causet.FutureOf(e._succ, includePresent=True)
return Causet(set(events))
@staticmethod
def FromFutureMatrix(C: np.ndarray) -> 'Causet':
'''
Returns `FromPastMatrix` of the transposed input.
'''
return Causet.FromPastMatrix(C.T)
@staticmethod
def FromTextFile(filename: Any, isPastMatrix: bool = True,
delimiter: str = ',', **kwargs) -> 'Causet':
'''
Passes the filename and delimiter (and further keyword arguments) to
the `genfromtxt` function of `numpy`. The resulting logical matrix is
parsed with `FromPastMatrix` or `FromFutureMatrix`.
'''
C: np.ndarray = np.genfromtxt(filename, dtype=int,
delimiter=delimiter, **kwargs)
C = C.astype(bool)
if isPastMatrix:
return Causet.FromPastMatrix(C)
else:
return Causet.FromFutureMatrix(C)
@staticmethod
def merge(pastSet: Iterable, futureSet: Iterable,
disjoint: bool = False) -> 'Causet':
'''
Returns a new Causet instance that joins the event sets `pastSet` and
`futureSet`.
If not `disjoint` (default), then the event of `pastSet` are also
assigned to the the past of every event in `futureSet` and vice versa.
'''
if not disjoint: # add pastSet as past of futureSet
for p in pastSet:
for f in futureSet:
p._addToFuture(f)
f._addToPast(p)
return Causet(set(pastSet) | set(futureSet))
def add(self, eventSet: Iterable, unlink: bool = False) -> None:
'''
Adds all the events of the (causal) set `eventSet` (`Causet` or
`Set[CausetEvent]`) to this instance.
'''
self._events.update(eventSet)
if unlink:
for e in self._events:
e.unlink()
if hasattr(self, '__diagram_coords'):
delattr(self, '__diagram_coords')
def discard(self, eventSet: Iterable, unlink: bool = False) -> None:
'''
Discards all the events of the (causal) set `eventSet` (`Causet` or
`Set[CausetEvent]`) from this instance.
'''
self._events.difference_update(eventSet)
if unlink:
for e in self._events:
e.unlink()
if hasattr(self, '__diagram_coords'):
delattr(self, '__diagram_coords')
@staticmethod
def len(other: 'Causet') -> int:
'''
Returns the number of events (set cardinality) of some Causet instance.
'''
return len(other._events)
@property
def Card(self) -> int:
'''
Returns the number of events (set cardinality) in this instance.
'''
return len(self._events)
def link(self) -> None:
'''
Computes the causal links between all events.
'''
# clear links:
for e in self._events:
e._lsucc = set()
# compute links:
for b in self._events:
b._lprec = {a for a in b._prec if CausetEvent.isLink(a, b)}
for a in b._lprec:
a._lsucc.add(b)
def unlink(self) -> None:
'''
Force all `CausetEvent` instances to reset their link memory.
'''
for e in self._events:
if e.hasBeenLinked():
e.unlink()
def LinkCount(self, eventSet: Optional[Set[CausetEvent]] = None) -> int:
'''
Returns the number of links between all events in `eventSet` (or in
this instance if `eventSet is None`).
'''
if eventSet is None:
return sum([e.LinkPastCard for e in self._events])
else:
return sum([len(e.LinkPast & eventSet) for e in eventSet])
def PastMatrix(self, labeledEvents: Optional[List[CausetEvent]] = None,
dtype: Any = bool) -> np.ndarray:
'''
Returns the logical causal past matrix such that `C[i, j]` is True if
the event with index j is in the past of event with index i.
The events are indexed by `labeledEvents` (by default sorted by
causality).
'''
if labeledEvents is None:
labeledEvents = self.sortedByCausality()
l: int = len(labeledEvents)
C: np.ndarray = np.zeros((l, l), dtype)
for i, a in enumerate(labeledEvents):
for j, b in enumerate(labeledEvents):
C[i, j] = a > b
return C
def saveAsCSV(self, filename: str) -> None:
'''
Saves the causal past matrix of this object to a text file with
delimiter ','.
'''
C: np.ndarray = self.PastMatrix().astype(int)
np.savetxt(filename, C, fmt='%.0f', delimiter=',')
def FutureMatrix(self, labeledEvents: Optional[List[CausetEvent]] = None,
dtype: Any = bool) -> np.ndarray:
'''
Returns the transpose of `PastMatrix`.
'''
return self.PastMatrix(labeledEvents, dtype).T
def LinkPastMatrix(self, labelling: Optional[List[CausetEvent]] = None,
dtype: Any = bool) -> np.ndarray:
'''
Returns the logical link past matrix such that `C[i, j]` is True if the
event with index j is linked in the past to event with index i.
The events are indexed with `labelling` (by default sorted by
causality).
'''
if labelling is None:
labelling = self.sortedByCausality()
l: int = len(labelling)
C: np.ndarray = np.zeros((l, l), dtype)
for i, a in enumerate(labelling):
for j, b in enumerate(labelling):
C[i, j] = a.isPastLink(b)
return C
def LinkFutureMatrix(self, labelling: Optional[List[CausetEvent]] = None,
dtype: Any = bool) -> np.ndarray:
'''
Returns the transpose of `LinkPastMatrix`.
'''
return self.LinkPastMatrix(labelling, dtype).T
def find(self, label: Any) -> CausetEvent:
'''
Returns the first event with the given `label`. If no event can be
found, it raises a `ValueError`.
'''
for e in self._events:
if e.Label == label:
return e
raise ValueError(f'No event with label {label} found.')
def findAny(self, *labels: Iterable[Any]) -> Iterator[CausetEvent]:
'''
Returns an iterator of events labelled by any value in `labels`.
'''
for e in self._events:
if e.Label in labels:
yield e
def findAll(self, *labels: Iterable[Any]) -> Set[CausetEvent]:
'''
Returns a set of events labelled by any value in `labels`.
'''
return {e for e in self._events if e.Label in labels}
def __contains__(self, other: CausetEvent) -> bool:
return other in self._events
def __sub__(self, other: Iterable[CausetEvent]) -> Set[CausetEvent]:
return self._events - set(other)
def __rsub__(self, other: Iterable[CausetEvent]) -> Set[CausetEvent]:
return self._events - set(other)
def __or__(self, other: Iterable[CausetEvent]) -> Set[CausetEvent]:
return self._events | set(other)
def __ror__(self, other: Iterable[CausetEvent]) -> Set[CausetEvent]:
return self._events | set(other)
def __and__(self, other: Iterable[CausetEvent]) -> Set[CausetEvent]:
return self._events & set(other)
def __rand__(self, other: Iterable[CausetEvent]) -> Set[CausetEvent]:
return self._events & set(other)
def __xor__(self, other: Iterable[CausetEvent]) -> Set[CausetEvent]:
return self._events ^ set(other)
def __rxor__(self, other: Iterable[CausetEvent]) -> Set[CausetEvent]:
return self._events ^ set(other)
def difference(self, other: Iterable[CausetEvent]) -> Set[CausetEvent]:
return self._events.difference(set(other))
def intersection(self, other: Iterable[CausetEvent]) -> Set[CausetEvent]:
return self._events.intersection(set(other))
def symmetric_difference(self, other: Iterable[CausetEvent]) -> \
Set[CausetEvent]:
return self._events.symmetric_difference(set(other))
def union(self, other: Iterable[CausetEvent]) -> Set[CausetEvent]:
return self._events.union(set(other))
def isChain(self, events: Optional[Iterable[CausetEvent]] = None) -> bool:
'''
Tests if this instance or `CausetEvent` is a causal chain.
'''
c: int
if events is None:
c = self.Card
for e in self._events:
if e.ConeCard != c:
return False
else:
events = set(events)
c = len(events)
for e in events:
if len(e.Cone & events) != c:
return False
return True
def isPath(self, events: Optional[Iterable[CausetEvent]] = None) -> bool:
'''
Tests if this instance or `CausetEvent` is a causal path.
'''
if events is None:
if self.Card == 0:
return False
else:
return self.isChain()
else:
events = set(events)
if len(events) <= 1:
return len(events) == 1
extremal: int = 0
for e in events:
e_linkcount: int = len(e.LinkCone & events)
if (e_linkcount < 1) or (e_linkcount > 2):
return False
elif e_linkcount == 1:
extremal += 1
if extremal > 2:
return False
return True
def isAntichain(self, events: Optional[Iterable[CausetEvent]] = None) -> \
bool:
'''
Tests if this instance of `CausetEvent` is a causal anti-chain.
'''
if events is None:
for e in self._events:
if e.ConeCard != 1:
return False
else:
events = set(events)
for e in events:
if len(e.Cone & events) != 1:
return False
return True
@property
def PastInf(self) -> Set[CausetEvent]:
'''
Returns the set of events without any past event (past infinity).
'''
return {e for e in self._events if e.PastCard == 0}
@property
def FutureInf(self) -> Set[CausetEvent]:
'''
Returns the set of events without any future event (future
infinity).
'''
return {e for e in self._events if e.FutureCard == 0}
@property
def PastInfCard(self) -> int:
'''
Returns the number of events without any past event (past infinity).
'''
return sum(1 for e in self._events if e.PastCard == 0)
@property
def FutureInfCard(self) -> int:
'''
Returns the number of event without any future event (future infinity).
'''
return sum(1 for e in self._events if e.FutureCard == 0)
@staticmethod
def PastInfOf(eventSet: Set[CausetEvent]) -> Set[CausetEvent]:
'''
Returns a subset of event without any past event (past infinity) in
`eventSet`.
'''
return {e for e in eventSet if not (e.Past & eventSet)}
@staticmethod
def FutureInfOf(eventSet: Set[CausetEvent]) -> Set[CausetEvent]:
'''
Returns a subset of event without any future event (future infinity) in
`eventSet`.
'''
return {e for e in eventSet if not (e.Future & eventSet)}
@staticmethod
def PastInfCardOf(eventSet: Set[CausetEvent]) -> int:
'''
Returns the number of event without any past event (past infinity) in
`eventSet`.
'''
return sum(1 for e in eventSet if not (e.Past & eventSet))
@staticmethod
def FutureInfCardOf(eventSet: Set[CausetEvent]) -> int:
'''
Returns the number of event without any future event (future infinity)
in `eventSet`.
'''
return sum(1 for e in eventSet if not (e.Future & eventSet))
@staticmethod
def PastOf(eventSet: Set[CausetEvent], includePresent: bool = False,
intersect: bool = False) -> Set[CausetEvent]:
'''
Returns the set of events that are in the past of `eventSet`.
'''
newEventSet: Set[CausetEvent] = set()
if includePresent and intersect:
for e in eventSet:
newEventSet &= e.PresentOrPast
elif intersect:
for e in eventSet:
newEventSet &= e.Past
else:
for e in eventSet:
newEventSet |= e.Past
if includePresent:
newEventSet |= eventSet
return newEventSet
@staticmethod
def FutureOf(eventSet: Set[CausetEvent], includePresent: bool = False,
intersect: bool = False) -> Set[CausetEvent]:
'''
Returns the set of events that are in the future of `eventSet`.
'''
newEventSet: Set[CausetEvent] = set()
if includePresent and intersect:
for e in eventSet:
newEventSet &= e.PresentOrFuture
elif intersect:
for e in eventSet:
newEventSet &= e.Future
else:
for e in eventSet:
newEventSet |= e.Future
if includePresent:
newEventSet |= eventSet
return newEventSet
@staticmethod
def ConeOf(eventSet: Set[CausetEvent], includePresent: bool = True,
intersect: bool = False) -> Set[CausetEvent]:
'''
Returns the set of events that are in the cone of `eventSet`.
'''
newEventSet: Set[CausetEvent] = set()
if includePresent and intersect:
for e in eventSet:
newEventSet &= e.Cone
elif intersect:
for e in eventSet:
newEventSet &= (e.Past | e.Future)
else:
for e in eventSet:
newEventSet |= e.Past | e.Future
if includePresent:
newEventSet |= eventSet
return newEventSet
def SpacelikeTo(self, eventSet: Set[CausetEvent]) -> Set[CausetEvent]:
'''
Returns the set of events that are spacelike separated to `eventSet`.
'''
return self._events - self.ConeOf(eventSet, includePresent=True)
@staticmethod
def Interval(a: CausetEvent, b: CausetEvent,
includeBoundary: bool = True) -> Set[CausetEvent]:
'''
Returns the causal interval (Alexandrov set) between events `a` and `b`
or an empty set if not `a <= b`.
If `includeBoundary == True` (default), the events `a` and `b` are
included in the interval.
'''
if not a <= b:
return set()
elif a == b:
return {a}
elif includeBoundary:
return a.PresentOrFuture & b.PresentOrPast
else:
return a.Future & b.Past
@staticmethod
def IntervalCard(a: CausetEvent, b: CausetEvent,
includeBoundary: bool = True) -> int:
'''
Returns the cardinality of the causal interval (Alexandrov set) between
events `a` and `b` or 0 if not `a <= b`.
If `includeBoundary == True` (default), the events `a` and `b` are
included in the interval.
'''
if not a <= b:
return 0
elif a == b:
return 1
else:
return len(a.Future & b.Past) + 2 * int(includeBoundary)
@staticmethod
def PerimetralEvents(a: CausetEvent, b: CausetEvent) -> Set[CausetEvent]:
'''
Returns the events that are linked between event `a` and `b`, with `a`
in the past and `b` in the future, or an empty set if there are no such
event.
'''
if not (a < b):
return set()
else:
return a.LinkFuture & b.LinkPast
@staticmethod
def PerimetralEventCount(a: CausetEvent, b: CausetEvent) -> int:
'''
Returns the number of events that are linked between event `a` and `b`,
with `a` in the past and `b` in the future.
'''
if not (a < b):
return 0
else:
return len(a.LinkFuture & b.LinkPast)
@staticmethod
def InternalEvents(a: CausetEvent, b: CausetEvent) -> Set[CausetEvent]:
'''
Returns the events that are not in `a` rank 2 path from event `a` to
event `b`, or an empty set if there are no such event.
'''
if not (a < b):
return set()
else:
return (a.Future & b.Past) - \
Causet.PerimetralEvents(a, b)
@staticmethod
def InternalEventCount(a: CausetEvent, b: CausetEvent) -> int:
'''
Returns the number of events that are not in a rank 2 path from event
`a` to event `b`.
'''
if not (a < b):
return 0
else:
return len(a.Future & b.Past) - \
Causet.PerimetralEventCount(a, b)
def CentralAntichain(self, e: Optional[CausetEvent] = None) -> \
Set[CausetEvent]:
'''
Returns the set of events that forms `a` maximal antichain with event
that have a similar past and future cardinality (like event `e` if
specified).
'''
# Compute the absolute sizes of past minus future cones:
diff: int
if e is None:
diff = 0
else:
diff = e.PastCard - e.FutureCard
sizeList: np.ndarray = np.array([
abs(e.PastCard - e.FutureCard - diff) for e in self._events])
sizes = np.unique(sizeList)
# Find maximal antichain of event that minimises the sizes:
eventSet: Set[CausetEvent] = set()
for size in sizes:
for i, e in enumerate(self._events):
if (sizeList[i] == size) and not (e.Cone & eventSet):
eventSet.add(e)
return eventSet
@staticmethod
def Layers(eventSet: Set[CausetEvent], first: int, last: int = None) -> \
Set[CausetEvent]:
'''
Returns the layers of `eventSet` with layer number from `first` to
`last`. If `last` is None (default), `last` is set to `first`.
Past layers have a negative layer number, 0 stands for the present
layer (eventSet itself), and future layer have a positive layer number.
'''
if last is None:
last = first
if (len(eventSet) == 0) or (first > last):
return set()
newEventSet: Set[CausetEvent] = set()
n: int
if first <= 0:
_last: int = min(0, last)
for a in Causet.PastOf(eventSet, includePresent=True):
setB: Set[CausetEvent] = a.Future & eventSet
if setB:
n = -(max(Causet.IntervalCard(a, b)
for b in setB) - 1)
else:
n = 0
if (n >= first) and (n <= _last):
newEventSet.add(a)
if last > 0:
_first: int = max(first, 0)
for b in Causet.FutureOf(eventSet, includePresent=True):
setA: Set[CausetEvent] = b.Past & eventSet
if setA:
n = max(Causet.IntervalCard(a, b)
for a in setA) - 1
else:
n = 0
if (n >= _first) and (n <= last):
newEventSet.add(b)
return newEventSet
@staticmethod
def LayerNumbers(eventList: List[CausetEvent], reverse: bool = False) -> \
List[int]:
'''
Returns a list of layer numbers for the list of events `eventList`.
If not `reverse` (default), the layer numbers are non-negative and
increasing from the past infinity of `eventList`. If reverse, the layer
numbers are non-positive and decreasing from the future infinity of
`eventList`.
'''
eventSet: Set[CausetEvent] = set(eventList)
if len(eventSet) == 0:
return []
lnums: List[int] = [0] * len(eventList)
if reverse:
for i, a in enumerate(eventList):
setB: Set[CausetEvent] = a.Future & eventSet
if setB:
lnums[i] = -(max(Causet.IntervalCard(a, b)
for b in (a.Future & eventSet)) - 1)
else:
for i, b in enumerate(eventList):
setA: Set[CausetEvent] = b.Past & eventSet
if setA:
lnums[i] = max(Causet.IntervalCard(a, b)
for a in (b.Past & eventSet)) - 1
return lnums
@staticmethod
def Ranks(eventSet: Set[CausetEvent], first: int,
last: Optional[int] = None) -> Set[CausetEvent]:
'''
Returns the ranks of `eventSet` with rank number from `first` to
`last`. If `last` is None (default), `last` is set to `first`. Past
ranks have a negative rank number, 0 stands for the present rank
(`eventSet` itself), and future ranks have a positive rank number.
'''
if last is None:
last = first
if (len(eventSet) == 0) or (first > last):
return set()
newEventSet: Set[CausetEvent] = set()
if first <= 0:
_last: int = min(0, last)
for a in Causet.PastOf(eventSet, includePresent=True):
setB: Set[CausetEvent] = a.Future & eventSet
if setB:
n = -max(int(a.Rank(b)) for b in setB)
else:
n = 0
if (n >= first) and (n <= _last):
newEventSet.add(a)
if last > 0:
_first: int = max(first, 0)
for b in Causet.FutureOf(eventSet, includePresent=True):
setA: Set[CausetEvent] = b.Past & eventSet
if setA:
n = max(int(a.Rank(b)) for a in setA)
else:
n = 0
if (n >= _first) and (n <= last):
newEventSet.add(b)
return newEventSet
@staticmethod
def RankNumbers(eventList: List[CausetEvent], reverse: bool = False) -> \
List[int]:
'''
Returns a list of rank numbers for the list of events `eventList`. If
not `reverse` (default), the rank numbers are non-negative and
increasing from the past infinity of `eventList`. If reverse, the rank
numbers are non-positive and decreasing from the future infinity of
`eventList`.
'''
eventSet: Set[CausetEvent] = set(eventList)
if len(eventSet) == 0:
return []
lnums: List[int] = [0] * len(eventList)
if reverse:
for i, a in enumerate(eventList):
setB: Set[CausetEvent] = a.Future & eventSet
if setB:
lnums[i] = -max(int(a.Rank(b)) for b in setB)
else:
for i, b in enumerate(eventList):
setA: Set[CausetEvent] = b.Past & eventSet
if setA:
lnums[i] = max(int(a.Rank(b)) for a in setA)
return lnums
@staticmethod
def Paths(a: CausetEvent, b: CausetEvent,
length: Union[str, int, List[int]] = 'any') -> \
Iterator[List[CausetEvent]]:
'''
Iterates over all paths (list of CausetEvent) from events `a` to event
`b` that have a specific `length`. As optional argument, the `length`
can be specified with the following meanings:
'any': paths of any `length` (default)
'min': paths of minimal `length`
'max' or 'timegeo': paths of maximal `length` (timelike geodesics)
A single int value sets a fixed `length`. A list of two int values sets
an accepted minimum and maximum of the `length`.
'''
find_min: bool = False
find_max: bool = False
min_len: int = 0
max_len: int = -1
if isinstance(length, str):
find_min = length == 'min'
find_max = length in {'max', 'timegeo'}
elif isinstance(length, int):
min_len, max_len = length, length
elif isinstance(length, list):
min_len, max_len = length[0], length[-1]
else:
raise ValueError(
'The optional argument \'length\' must be of ' +
'type str, int or List[int].')
# handle trivial paths:
if not (a <= b):
return
elif a is b:
if (min_len <= 1) and ((1 <= max_len) or (max_len == -1)):
yield [a]
elif a.isFutureLink(b):
if (min_len <= 2) and ((2 <= max_len) or (max_len == -1)):
yield [a, b]
elif (3 <= max_len) or (max_len == -1):
# handle longer paths:
b_linked: Set[CausetEvent] = a.Future & b.LinkPast
def Paths_find(path_a: List[CausetEvent], a: CausetEvent,
l: int) -> Iterator[List[CausetEvent]]:
nonlocal min_len, max_len
a_linked: Set[CausetEvent] = a.LinkFuture & b.Past
perimetral: Set[CausetEvent] = a_linked & b_linked
internal: Set[CausetEvent] = a_linked - perimetral
perimetral_count: int = len(perimetral)
internal_count: int = len(internal)
# path step along perimetral event:
if (min_len <= l) and (perimetral_count > 0) and \
(not find_max or (internal_count == 0)):
for e in perimetral:
yield path_a + [e, b]
if find_min:
if (l <= max_len) or (max_len == -1):
max_len = l # local minimum
return
# path step along internal event:
if find_max:
min_len = max(min_len, l) # local maximum
if l == max_len:
return
l += 1
for e in internal:
for p in Paths_find(path_a + [e], e, l):
yield p
if find_min or find_max:
# first extract all paths and find minimal/maximal length:
P: List[List[CausetEvent]] = list(Paths_find([a], a, 3))
if find_min:
for p in P:
if len(p) == max_len:
yield p
else:
for p in P:
if len(p) == min_len:
yield p
else: # iterate over all paths in the given range:
for p in Paths_find([a], a, 3):
yield p
def SmallestIntervalCard(self, a: CausetEvent, b: CausetEvent,
searching: Optional[Set[CausetEvent]] = None,
intersecting: Optional[Set[CausetEvent]] =
None) -> int:
'''
For `a <= b`, it returns the cardinality of the interval from `a` to
`b`.
For `a > b`, it returns the cardinality of the interval from `b` to
`a`.
When `a` is spacelike to `b`, it returns the smallest cardinality among
the intervals from one event in the past of `a` and `b` to one event in
the future of `a` and `b`.
The optional argument 'searching' provides the set of events of start
and end points of any causal interval.
The optional argument 'intersecting' provides the set of events that is
intersected with the interval before the cardinality is computed.
Default for both is the entire causet.
If either no common past event or no common future event is in the
'searching' set (or the entire causet), the entire past or future is
considered, respectively. If neither a common past event nor a common
future event exist, 0 is returned.
'''
if a > b:
a, b = b, a
if a <= b:
if (searching is None) or \
(a in searching and b in searching):
return self.IntervalCard(a, b) if intersecting is None \
else len(self.Interval(a, b) & intersecting)
elif a in searching: # but not b in searching
return a.FutureCard if intersecting is None \
else len(a.Future & intersecting)
elif b in searching: # but not a in searching
return b.PastCard if intersecting is None \
else len(b.Past & intersecting)
if searching is None:
searching = self._events
pastIntersect = a.Past & b.Past & searching
futureIntersect = a.Future & b.Future & searching
if not pastIntersect and not futureIntersect:
return 0
elif not pastIntersect: # but futureIntersect
return min(e.PastCard for e in futureIntersect) \
if intersecting is None \
else min(len(e.Past & intersecting) for e in futureIntersect)
elif not futureIntersect: # but pastIntersect
return min(e.FutureCard for e in pastIntersect) \
if intersecting is None \
else min(len(e.Future & intersecting) for e in pastIntersect)
elif intersecting is None: # pastIntersect and futureIntersect
return min(self.IntervalCard(e_p, e_f)
for e_p in pastIntersect
for e_f in futureIntersect)
else: # pastIntersect and futureIntersect
return min(len(self.Interval(e_p, e_f) & intersecting)
for e_p in pastIntersect
for e_f in futureIntersect)
def DistanceMatrix(self, antichain: Union[List[CausetEvent],
Tuple[CausetEvent, ...],
np.ndarray],
counting: str = 'ziczac',
recursive: bool = True) -> np.ndarray:
'''
Computes a symmetric matrix (ndarray of int) from counting the
distances between every pair of events from the `antichain`. The rows