-
Notifications
You must be signed in to change notification settings - Fork 0
/
AlignmentRepa.hs
2983 lines (2851 loc) · 127 KB
/
AlignmentRepa.hs
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
{-# LANGUAGE RankNTypes, BangPatterns #-}
module AlignmentRepa (
HistogramRepa(..),
HistogramRepaRed(..),
HistogramRepaVec(..),
HistogramRepaRedVec(..),
HistoryRepa(..),
TransformRepa(..),
histogramRepaEmpty,
histogramRepaVecEmpty,
arraysHistogramRepa,
histogramRepasSystem,
vectorHistogramRepasHistogramRepaVec,
vectorHistogramRepasHistogramRepaVec_u,
histogramRepaVecsVectorHistogramRepas,
vectorHistogramRepaRedsHistogramRepaRedVec_u,
histogramRepaRedVecsVectorHistogramRepaReds,
systemsHistogramsHistogramRepa,
systemsHistogramRepasHistogram,
histogramRepaVecsSum,
histogramRepaVecsFaclnsRepaVecs,
setSetVarsHistogramRepasPartition_u,
setSetVarsHistogramRepaVecsPartitionVec_u,
setSetVarsHistogramRepaVecsPartitionVec_u_1,
varsHistogramRepaVecsRollVec_u,
sumsHistogramRepa4VecsRollMapPairsHistogramRepa4VecsSum_u,
sumsHistogramRepa4VecsRollMapPairsHistogramRepa4VecsSum_u_1,
sumsHistogramRepasRollMapPairsHistogramRepasSum_u,
varsSourcesTargetsRollsHistogramRepaVecsHistogramRepaVecRollsCopyVec_u,
varsSourcesTargetsRollsHistogramRepaVecsHistogramRepaVecRollsCopyVec_u_1,
histogramRepaVecsRollMax,
setVarsHistogramRepasReduce,
setVarsHistogramRepasReduce_1,
setVarsHistogramRepasReduce_2,
setVarsHistogramRepasReduce_3,
setVarsHistogramRepasReduce_4,
varsHistogramRepaVecsReduceSingleVec_u,
varsHistogramRepa4VecsReduceSingle_u,
histogramRepasRed_u,
histogramRepaVecsRedVec,
histogramRepa4VecsRed_u,
varsHistogramRepaRedVecsSingleVec_u,
varsHistogramRepaRedsSingle_u,
setVarsHistogramRepaRedsRed,
setSetVarsHistogramRepasPartitionRed_u,
setSetVarsHistogramRepasPartitionRed_u_1,
setSetVarsHistogramRepaVecsPartitionRedVec_u,
setSetVarsHistogramRepaVecsPartitionRedVec_u_1,
histogramRepaRedsIndependent,
histogramRepaRedsIndependent_1,
histogramRepaRedVecsIndependent_u,
histogramRepaRedVecsIndependent_u_1,
setSetVarsHistogramRepaVecsPartitionIndependentVec_u,
setSetVarsHistogramRepaPairStorablesPartitionIndependentPair_u,
parametersHistogramRepaVecsSetTuplePartitionTop_u,
parametersHistogramRepaVecsSetTuplePartitionTopByM_u,
historyRepaEmpty,
arraysHistoryRepa_u,
arraysHistoryRepaCardinal_u,
vectorHistoryRepasConcat_u,
systemsHistoriesHistoryRepa,
systemsHistoriesHistoryRepa_u,
systemsHistoryRepasHistory_u,
historyRepasSize,
historyRepasDimension,
historyRepasSetVariable,
setVarsHistoryRepasCountApproxs,
setVarsHistoryRepasReduce,
setVarsHistoryRepasReduce_1,
setVarsHistoryRepasReduce_2,
setVarsHistoryRepasReduce_3,
setVarsHistoryRepaStorablesReduce,
historyRepasTransformRepasApply_u,
historyRepasListTransformRepasApply,
listVariablesListTransformRepasSort,
listVariablesListTransformRepasSort_1,
historyRepasListTransformRepasApply_u,
systemsFudsHistoryRepasMultiply,
systemsFudsHistoryRepasMultiply_u,
systemsDecompFudsHistoryRepasMultiply,
systemsDecompFudsHistoryRepasMultiply_r,
systemsDecompFudsHistoryRepasSetVariablesListHistogramLeaf,
systemsDecompFudsHistoryRepasHistoriesQuery,
systemsDecompFudsHistoryRepasHistoryRepasQuery,
systemsDecompFudMultipliesHistoryRepasHistoriesQuery,
systemsDecompFudMultipliesHistoryRepasHistoryRepasQuery,
systemsDecompFudMultipliesHistoryRepasHistoryRepasQueryAny,
systemsDecompFudsHistoryRepasHistoryRepasSetVariablesTest,
systemsDecompFudsHistoryRepasHistoryRepasSetVariablesTest_1,
systemsDecompFudsHistoryRepasHistoryRepasSetVariablesTest_2,
systemsDecompFudsHistoryRepasHistoryRepasSetVariablesTest_3,
systemsDecompFudsHistoryRepasDecompFudReduced,
historyRepasRed,
setVarsHistoryRepasRed,
setVarsHistoryRepasHistoryRepaReduced,
eventsHistoryRepasHistoryRepaSelection,
historyRepasHistoryRepasHistoryRepaSelection_u,
historyRepasListsList,
systemsListVariablesListsListsHistoryRepa,
systemsListVariablesListsListsHistoryRepa_u,
systemsTransformsTransformRepa,
systemsTransformsTransformRepa_u,
vectorPairsTop,
parametersSetVarsHistoryRepasSetSetVarsAlignedTop,
parametersSetVarsHistoryRepasSetSetVarsAlignedTop_u,
parametersSetVarsHistoryRepasSetSetVarsAlignedTop_1,
parametersSetVarsHistoryRepasSetSetVarsAlignedTop_u_1,
parametersSetVarsHistoryRepasSetSetVarsAlignedTop_2,
parametersSetVarsHistoryRepasSetSetVarsAlignedTop_u_2,
parametersSetVarsSetSetVarsHistoryRepasSetSetVarsAlignedTop,
parametersSetVarsSetSetVarsHistoryRepasSetSetVarsAlignedTop_u,
parametersSetVarsSetSetVarsHistoryRepasSetSetVarsAlignedTop_1,
parametersSetVarsSetSetVarsHistoryRepasSetSetVarsAlignedTop_u_1,
parametersSetVarsSetSetVarsHistoryRepasSetSetVarsAlignedTop_u_2,
parametersSetVarsSetSetVarsHistoryRepasSetSetVarsAlignedDenseTop,
parametersSetVarsSetSetVarsHistoryRepasSetSetVarsAlignedDenseTop_u,
parametersSetVarsSetSetVarsHistoryRepasSetSetVarsAlignedExcludeHiddenDenseTop_u
)
where
import Control.Monad
import Control.Monad.ST
import Data.STRef
import Data.Int
import Data.List as List
import qualified Data.Set as Set
import qualified Data.Map as Map
import qualified Data.IntMap as IntMap
import qualified Data.Vector as V
import qualified Data.Vector.Algorithms.Merge as VA
import qualified Data.Vector.Unboxed as UV
import qualified Data.Vector.Unboxed.Mutable as MV
import qualified Data.Vector.Storable as SV
import qualified Data.Vector.Storable.Mutable as SMV
import Data.Array.Repa as R
import GHC.Real
import Foreign.Ptr
import Foreign.C.Types
import System.IO.Unsafe
import AlignmentRepaVShape
import AlignmentUtil
import Alignment
import AlignmentApprox
data HistogramRepa = HistogramRepa {
histogramRepasVectorVar :: !(V.Vector Variable),
histogramRepasMapVarInt :: Map.Map Variable Int,
histogramRepasArray :: !(Array U VShape Double)}
deriving (Eq, Read, Show)
instance Ord HistogramRepa where
compare _ _ = EQ
data HistogramRepaVec = HistogramRepaVec {
histogramRepaVecsVectorVar :: !(V.Vector Variable),
histogramRepaVecsMapVarInt :: Map.Map Variable Int,
histogramRepaVecsSize :: !Double,
histogramRepaVecsShape :: !VShape,
histogramRepaVecsArray :: !(V.Vector (UV.Vector Double))}
deriving (Eq, Read, Show)
instance Ord HistogramRepaVec where
compare _ _ = EQ
data HistogramRepaRed = HistogramRepaRed {
histogramRepaRedsVectorVar :: !(V.Vector Variable),
histogramRepaRedsMapVarInt :: Map.Map Variable Int,
histogramRepaRedsShape :: !VShape,
histogramRepaRedsVectorArray :: !(V.Vector (UV.Vector Double))}
deriving (Eq, Read, Show)
instance Ord HistogramRepaRed where
compare _ _ = EQ
data HistogramRepaRedVec = HistogramRepaRedVec {
histogramRepaRedVecsVectorVar :: !(V.Vector Variable),
histogramRepaRedVecsMapVarInt :: Map.Map Variable Int,
histogramRepaRedVecsSize :: !Double,
histogramRepaRedVecsShape :: !VShape,
histogramRepaRedVecsVectorArray :: !(V.Vector (V.Vector (UV.Vector Double)))}
deriving (Eq, Read, Show)
data HistoryRepa = HistoryRepa {
historyRepasVectorVar :: !(V.Vector Variable),
historyRepasMapVarInt :: Map.Map Variable Int,
historyRepasShape :: !VShape,
historyRepasArray :: !(Array U DIM2 Int16)}
deriving (Eq, Read, Show)
instance Ord HistoryRepa where
compare _ _ = EQ
data TransformRepa = TransformRepa {
transformRepasVectorVar :: !(V.Vector Variable),
transformRepasMapVarInt :: Map.Map Variable Int,
transformRepasVarDerived :: !Variable,
transformRepasValency :: !Int16,
transformRepasArray :: !(Array U VShape Int16)}
deriving (Eq, Read, Show)
instance Ord TransformRepa where
compare _ _ = EQ
histogramRepaEmpty :: HistogramRepa
histogramRepaEmpty = HistogramRepa vempty mempty (llrr vsempty [])
where
vsempty = UV.empty
llrr = R.fromListUnboxed
mempty = Map.empty
vempty = V.empty
histogramRepaVecEmpty :: HistogramRepaVec
histogramRepaVecEmpty = HistogramRepaVec vempty mempty 0 vsempty vempty
where
vsempty = UV.empty
mempty = Map.empty
vempty = V.empty
arraysHistogramRepa :: Array U VShape Double -> HistogramRepa
arraysHistogramRepa rr = HistogramRepa (llvv n vv) mvv rr
where
n = rank (extent rr)
vv = List.map VarIndex [0 .. n-1]
mvv = llmm (zip vv [0..])
llmm = Map.fromList
llvv = V.fromListN
vectorHistogramRepasHistogramRepaVec :: Double -> V.Vector HistogramRepa -> Maybe HistogramRepaVec
vectorHistogramRepasHistogramRepaVec z vrr
| V.null vrr = Nothing
| not $ V.and $ V.map (\(HistogramRepa _ _ ss) -> extent ss == svv) vrr = Nothing
| otherwise = Just $ vrrrrv z vrr
where
HistogramRepa vvv mvv rr = vrr V.! 0
svv = extent rr
vrrrrv = vectorHistogramRepasHistogramRepaVec_u
vectorHistogramRepasHistogramRepaVec_u :: Double -> V.Vector HistogramRepa -> HistogramRepaVec
vectorHistogramRepasHistogramRepaVec_u z vrr = HistogramRepaVec vvv mvv z svv (V.map rraa vrr)
where
HistogramRepa vvv mvv rr = vrr V.! 0
svv = extent rr
rraa = R.toUnboxed . histogramRepasArray
histogramRepaVecsVectorHistogramRepas :: HistogramRepaVec -> V.Vector HistogramRepa
histogramRepaVecsVectorHistogramRepas rrv = V.map (\aa -> HistogramRepa vvv mvv (R.fromUnboxed svv aa)) vaa
where
HistogramRepaVec vvv mvv _ svv vaa = rrv
vectorHistogramRepaRedsHistogramRepaRedVec_u :: Double -> V.Vector HistogramRepaRed -> HistogramRepaRedVec
vectorHistogramRepaRedsHistogramRepaRedVec_u z vrr = HistogramRepaRedVec vvv mvv z svv (V.map rraa vrr)
where
HistogramRepaRed vvv mvv svv rr = vrr V.! 0
rraa = histogramRepaRedsVectorArray
histogramRepaRedVecsVectorHistogramRepaReds :: HistogramRepaRedVec -> V.Vector HistogramRepaRed
histogramRepaRedVecsVectorHistogramRepaReds rrv = V.map (\aa -> HistogramRepaRed vvv mvv svv aa) vaa
where
HistogramRepaRedVec vvv mvv z svv vaa = rrv
histogramRepasSystem :: HistogramRepa -> System
histogramRepasSystem aa = lluu [(v, llqq (List.map ValIndex [0 .. sh UV.! i - 1])) | (i,v) <- zip [0..] (vvll vv)]
where
vv = histogramRepasVectorVar aa
rr = histogramRepasArray aa
sh = extent rr
lluu = listsSystem_u
vvll = V.toList
llqq = Set.fromList
systemsHistogramsHistogramRepa :: System -> Histogram -> Maybe HistogramRepa
systemsHistogramsHistogramRepa uu aa
| aa /= empty && (vars aa `subset` uvars uu) =
Just $ HistogramRepa (llvv vv) mvv (llrr sh (elems nn))
| otherwise = Nothing
where
vv = qqll $ vars aa
mvv = llmm (zip vv [0..])
mm = llmm [(v, llmm (zip (qqll ww) [0..])) | v <- vv, let ww = uvals uu v]
sh = shapeOfList [Map.size (mm Map.! v) | v <- vv] :: VShape
nn = llim (+) $ [(i,0) | i <- [0 .. size sh - 1]] List.++
[(toIndex sh (shapeOfList [mm Map.! v Map.! (ss `sat` v) | v <- vv] :: VShape), fromRational c) |
(ss,c) <- aall aa]
empty = histogramEmpty
aall = histogramsList
vars = histogramsVars
sat ss v = fromJust $ statesVarsValue ss v
uvals uu v = fromJust $ systemsVarsSetValue uu v
uvars = systemsVars
uull = systemsList
llrr = R.fromListUnboxed
elems = IntMap.elems
llim = IntMap.fromListWith
llmm :: forall k a. Ord k => [(k, a)] -> Map.Map k a
llmm = Map.fromList
qqll = Set.toList
subset = Set.isSubsetOf
llvv = V.fromList
systemsHistogramRepasHistogram :: System -> HistogramRepa -> Maybe Histogram
systemsHistogramRepasHistogram uu aa
| vvqq vv `subset` uvars uu && sh == sh' =
llaa [(llss [(v,w) | (j,k) <- zip [0..] ss, let v = vv V.! j, let w = mm Map.! v V.! k], toRational c) |
(i,c) <- zip [0..] (rrll rr), let ss = listOfShape (fromIndex sh i)]
| otherwise = Nothing
where
vv = histogramRepasVectorVar aa
rr = histogramRepasArray aa
mm = llmm [(v, llvv (qqll ww)) | v <- V.toList vv, let ww = uvals uu v]
sh = shapeOfList [V.length (mm Map.! v) | v <- vvll vv] :: VShape
sh' = extent rr
llaa = listsHistogram
llss = listsState
uvals uu v = fromJust $ systemsVarsSetValue uu v
uvars = systemsVars
uull = systemsList
rrll = R.toList
llmm = Map.fromList
qqll = Set.toList
vvqq = Set.fromList . V.toList
subset = Set.isSubsetOf
llvv = V.fromList
vvll = V.toList
historyRepaEmpty :: HistoryRepa
historyRepaEmpty = HistoryRepa vempty mempty vsempty (llrr (Z :. 0 :. 0) [])
where
vsempty = UV.empty
llrr = R.fromListUnboxed
mempty = Map.empty
vempty = V.empty
arraysHistoryRepa_u :: VShape -> Array U DIM2 Int16 -> HistoryRepa
arraysHistoryRepa_u svv rr = HistoryRepa (llvv n vv) mvv svv rr
where
Z :. n :. z = extent rr
vv = List.map VarIndex [0 .. n-1]
mvv = llmm (zip vv [0..])
llmm = Map.fromList
llvv = V.fromListN
arraysHistoryRepaCardinal_u :: VShape -> Array U DIM2 Int16 -> HistoryRepa
arraysHistoryRepaCardinal_u svv rr = HistoryRepa (llvv n vv) mvv svv rr
where
Z :. n :. z = extent rr
vv = List.map (VarInt .toInteger) [1 .. n]
mvv = llmm (zip vv [0..])
llmm = Map.fromList
llvv = V.fromListN
systemsHistoriesHistoryRepa :: System -> History -> Maybe HistoryRepa
systemsHistoriesHistoryRepa uu hh
| hh /= empty && (vars hh `subset` uvars uu) =
Just $ HistoryRepa (llvv vv) mvv (llvu sh) (computeS (R.transpose (llrr sh' nn)))
| otherwise = Nothing
where
vv = qqll $ vars hh
mvv = llmm (zip vv [0..])
sh = [uval uu v | v <- vv]
mm = llmm [(v, llmm (zip (qqll ww) [0..])) | v <- vv, let ww = uvals uu v]
sh' = Z :. size hh :. card (vars hh)
nn = [mm Map.! v Map.! u | (_,ss) <- hhll hh, (v,u) <- ssll ss]
size = fromInteger . historiesSize
empty = historyEmpty
hhll = historiesList
vars = historiesVars
ssll = statesList
uvals uu v = fromJust $ systemsVarsSetValue uu v
uval uu v = card $ fromJust $ systemsVarsSetValue uu v
uvars = systemsVars
uull = systemsList
llrr = R.fromListUnboxed
llmm :: forall k a. Ord k => [(k, a)] -> Map.Map k a
llmm = Map.fromList
card = Set.size
qqll = Set.toList
subset = Set.isSubsetOf
llvv = V.fromList
llvu = UV.fromList
systemsHistoriesHistoryRepa_u :: System -> History -> HistoryRepa
systemsHistoriesHistoryRepa_u uu hh = HistoryRepa (llvv vv) mvv (llvu sh) (computeS (R.transpose (llrr sh' nn)))
where
vv = qqll $ vars hh
mvv = llmm (zip vv [0..])
sh = [uval uu v | v <- vv]
mm = llmm [(v, llmm (zip (qqll ww) [0..])) | v <- vv, let ww = uvals uu v]
sh' = Z :. size hh :. card (vars hh)
nn = [mm Map.! v Map.! u | (_,ss) <- hhll hh, (v,u) <- ssll ss]
size = fromInteger . historiesSize
hhll = historiesList
vars = historiesVars
ssll = statesList
uvals uu v = fromJust $ systemsVarsSetValue uu v
uval uu v = card $ fromJust $ systemsVarsSetValue uu v
uull = systemsList
llrr = R.fromListUnboxed
llmm :: forall k a. Ord k => [(k, a)] -> Map.Map k a
llmm = Map.fromList
card = Set.size
qqll = Set.toList
llvv = V.fromList
llvu = UV.fromList
-- AYOR
systemsHistoryRepasHistory_u :: System -> HistoryRepa -> Maybe History
systemsHistoryRepasHistory_u uu aa
| vvqq vv `subset` uvars uu =
llhh [(IdInt (toInteger (i+1)), llss [(v,w) | j <- [0..n-1], let v = vv V.! j,
let k = rr R.! (Z :. j :. i), let w = mm Map.! v V.! (fromIntegral k)]) | i <- [0..z-1]]
| otherwise = Nothing
where
vv = historyRepasVectorVar aa
rr = historyRepasArray aa
mm = llmm [(v, llvv (qqll ww)) | v <- V.toList vv, let ww = uvals uu v]
Z :. n :. z = extent rr
llhh = listsHistory
llss = listsState
uvals uu v = fromJust $ systemsVarsSetValue uu v
uvars = systemsVars
uull = systemsList
llmm = Map.fromList
qqll = Set.toList
vvqq = Set.fromList . V.toList
subset = Set.isSubsetOf
llvv = V.fromList
vvll = V.toList
systemsTransformsTransformRepa :: System -> Transform -> Maybe TransformRepa
systemsTransformsTransformRepa uu tt
| tt /= empty && isOneFunc uu tt && card (der tt) == 1 =
Just $ TransformRepa (llvv vv) mvv w (fromIntegral (uval uu w)) (llrr sh (elems nn))
| otherwise = Nothing
where
vv = qqll $ und tt
mvv = llmm (zip vv [0..])
mm = llmm [(v, llmm (zip (qqll ww) [0..])) | v <- w:vv, let ww = uvals uu v]
sh = shapeOfList [Map.size (mm Map.! v) | v <- vv] :: VShape
w = Set.findMin (der tt)
nn = llim [(toIndex sh (shapeOfList [mm Map.! v Map.! (ss `sat` v) | v <- vv] :: VShape),
fromIntegral (mm Map.! w Map.! (ss `sat` w))) | (ss,_) <- aall (ttaa tt)]
isOneFunc = systemsTransformsIsOneFunc
empty = transformEmpty
und = transformsUnderlying
der = transformsDerived
ttaa = transformsHistogram
aall = histogramsList
sat ss v = fromJust $ statesVarsValue ss v
uvals uu v = fromJust $ systemsVarsSetValue uu v
uval uu v = card $ fromJust $ systemsVarsSetValue uu v
uull = systemsList
llrr = R.fromListUnboxed
elems = IntMap.elems
llim = IntMap.fromList
llmm :: forall k a. Ord k => [(k, a)] -> Map.Map k a
llmm = Map.fromList
qqll = Set.toList
card = Set.size
llvv = V.fromList
systemsTransformsTransformRepa_u :: System -> Transform -> TransformRepa
systemsTransformsTransformRepa_u uu tt = TransformRepa (llvv vv) mvv w (fromIntegral (uval uu w)) (llrr sh (elems nn))
where
vv = qqll $ und tt
mvv = llmm (zip vv [0..])
mm = llmm [(v, llmm (zip (qqll ww) [0..])) | v <- w:vv, let ww = uvals uu v]
sh = shapeOfList [Map.size (mm Map.! v) | v <- vv] :: VShape
w = Set.findMin (der tt)
nn = llim [(toIndex sh (shapeOfList [mm Map.! v Map.! (ss `sat` v) | v <- vv] :: VShape),
fromIntegral (mm Map.! w Map.! (ss `sat` w))) | (ss,_) <- aall (ttaa tt)]
und = transformsUnderlying
der = transformsDerived
ttaa = transformsHistogram
aall = histogramsList
sat ss v = fromJust $ statesVarsValue ss v
uvals uu v = fromJust $ systemsVarsSetValue uu v
uval uu v = card $ uvals uu v
uull = systemsList
llrr = R.fromListUnboxed
elems = IntMap.elems
llim = IntMap.fromList
llmm :: forall k a. Ord k => [(k, a)] -> Map.Map k a
llmm = Map.fromList
qqll = Set.toList
card = Set.size
llvv = V.fromList
histogramRepaVecsSum :: HistogramRepaVec -> UV.Vector Double
histogramRepaVecsSum (HistogramRepaVec _ _ _ _ vaa) = V.convert (V.map UV.sum vaa)
histogramRepaVecsFaclnsRepaVecs :: HistogramRepaVec -> HistogramRepaVec
histogramRepaVecsFaclnsRepaVecs (HistogramRepaVec vvv mvv _ svv !vaa) =
HistogramRepaVec vvv mvv 1 svv (V.map (UV.map (\x -> logGamma (x + 1))) vaa)
setSetVarsHistogramRepasPartition_u :: Set.Set (Set.Set Variable) -> HistogramRepa -> HistogramRepa
setSetVarsHistogramRepasPartition_u pp aa = rraa rr'
where
HistogramRepa vvv mvv !rr = aa
!svv = extent rr
!n = rank svv
!vpp = llvv $ [qqvv cc | cc <- qqll pp]
!ppp = V.map (\vcc -> V.convert $ V.map (mvv Map.!) vcc) vpp
!spp = V.map (\pcc -> perm svv pcc) ppp
!sxx = V.convert $ V.map R.size spp
!pvv = llvu $ [mvv Map.! v | cc <- qqll pp, v <- qqll cc]
!pww = llvu $ snd $ unzip $ sort $ zip (vull pvv) [0..]
rr' = R.computeS $ R.reshape sxx $ R.backpermute (perm svv pvv) (\iww -> perm iww pww) rr
rraa = arraysHistogramRepa
perm = UV.unsafeBackpermute
vull = UV.toList
llvu = UV.fromList
qqvv = llvv . qqll
llvv = V.fromList
vvll = V.toList
qqll :: forall a. Set.Set a -> [a]
qqll = Set.toList
setSetVarsHistogramRepaVecsPartitionVec_u_1 :: Set.Set (Set.Set Variable) -> HistogramRepaVec -> HistogramRepaVec
setSetVarsHistogramRepaVecsPartitionVec_u_1 pp rrv = HistogramRepaVec vyy myy z syy vbb
where
HistogramRepaVec vvv mvv z svv vaa = rrv
!v = R.size svv
!n = rank svv
!p = V.length vaa
!vpp = llvv $ [qqvv cc | cc <- qqll pp]
!m = V.length vpp
yy = List.map VarIndex [0 .. m-1]
vyy = llvv yy
myy = llmm (zip yy [0..])
!ppp = V.map (\vcc -> V.convert $ V.map (mvv Map.!) vcc) vpp
!spp = V.map (\pcc -> perm svv pcc) ppp
!syy = V.convert $ V.map R.size spp
!pvv = llvu $ [mvv Map.! v | cc <- qqll pp, v <- qqll cc]
!sww = perm svv pvv
!vbb = runST $ do
vbb <- V.replicateM p (MV.replicate v 0)
rvv <- newSTRef (UV.replicate n 0)
forM_ [0 .. v-1] $ (\j -> do
ivv <- readSTRef rvv
let !i = R.toIndex sww $ perm ivv pvv
forM_ [0 .. p-1] $ (\l -> do
MV.unsafeWrite (vbb V.! l) i (vaa V.! l UV.! j))
writeSTRef rvv (incIndex svv ivv))
V.mapM UV.unsafeFreeze vbb
llmm = Map.fromList
perm = UV.unsafeBackpermute
qqvv = llvv . qqll
llvv = V.fromList
vvll = V.toList
qqll :: forall a. Set.Set a -> [a]
qqll = Set.toList
llvu = UV.fromList
setSetVarsHistogramRepaVecsPartitionVec_u :: Set.Set (Set.Set Variable) -> HistogramRepaVec -> HistogramRepaVec
setSetVarsHistogramRepaVecsPartitionVec_u !pp !rrv = HistogramRepaVec vyy myy z syy vbb
where
HistogramRepaVec vvv mvv z svv vaa = rrv
!v = R.size svv
!n = rank svv
!p = V.length vaa
!vpp = llvv $ [qqvv cc | cc <- qqll pp]
!m = V.length vpp
yy = List.map VarIndex [0 .. m-1]
vyy = llvv yy
myy = llmm (zip yy [0..])
!ppp = V.map (\vcc -> V.convert $ V.map (mvv Map.!) vcc) vpp
!spp = V.map (\pcc -> perm svv pcc) ppp
!syy = V.convert $ V.map R.size spp
!pvv = llvu $ [mvv Map.! v | cc <- qqll pp, v <- qqll cc]
!sww = perm svv pvv
!vbb = runST $ do
vbb <- V.replicateM p (MV.replicate v 0)
!ivv <- MV.replicate n 0
forM_ [0 .. v-1] $ (\j -> do
!i <- toIndexPermM sww pvv ivv
forM_ [0 .. p-1] $ (\l -> do
MV.unsafeWrite (V.unsafeIndex vbb l) i (UV.unsafeIndex (V.unsafeIndex vaa l) j))
incIndexM_ svv ivv)
V.mapM UV.unsafeFreeze vbb
llmm = Map.fromList
perm = UV.unsafeBackpermute
qqvv = llvv . qqll
llvv = V.fromList
vvll = V.toList
qqll :: forall a. Set.Set a -> [a]
qqll = Set.toList
llvu = UV.fromList
sumsHistogramRepa4VecsRollMapPairsHistogramRepa4VecsSum_u :: Double -> HistogramRepaVec ->
(UV.Vector Int, UV.Vector Int) -> HistogramRepaVec -> UV.Vector Double
sumsHistogramRepa4VecsRollMapPairsHistogramRepa4VecsSum_u !a !aav (!rs,!rt) !rrv = bb
where
HistogramRepaVec _ _ _ svv vaa = aav
HistogramRepaVec _ _ _ syy vrr = rrv
!d = UV.unsafeIndex svv 0
!r = UV.unsafeIndex syy 0
[!a1,!a2,!b1,!b2] = V.toList vaa
[!ra1,!ra2,!rb1,!rb2] = V.toList vrr
!bb = UV.create $ do
!bb <- MV.replicate r 0
forM_ [0 .. r-1] $ (\j -> do
let !s = UV.unsafeIndex rs j
let !t = UV.unsafeIndex rt j
MV.unsafeWrite bb j (a
+ UV.unsafeIndex ra1 j - UV.unsafeIndex a1 s - UV.unsafeIndex a1 t
- UV.unsafeIndex ra2 j + UV.unsafeIndex a2 s + UV.unsafeIndex a2 t
- UV.unsafeIndex rb1 j + UV.unsafeIndex b1 s + UV.unsafeIndex b1 t
+ UV.unsafeIndex rb2 j - UV.unsafeIndex b2 s - UV.unsafeIndex b2 t))
return bb
sumsHistogramRepa4VecsRollMapPairsHistogramRepa4VecsSum_u_1 :: UV.Vector Double -> HistogramRepaVec ->
(UV.Vector Int, UV.Vector Int) -> HistogramRepaVec -> UV.Vector Double
sumsHistogramRepa4VecsRollMapPairsHistogramRepa4VecsSum_u_1 !av !aav (!rs,!rt) !rrv = bb
where
HistogramRepaVec _ _ _ _ vaa = aav
HistogramRepaVec _ _ _ _ vrr = rrv
[!f1,!f2,!g1,!g2] = UV.toList av
[!a1,!a2,!b1,!b2] = V.toList vaa
[!ra1,!ra2,!rb1,!rb2] = V.toList vrr
!bb = (UV.imap (\i x -> x - a1 UV.! (rs UV.! i) - a1 UV.! (rt UV.! i) + f1) ra1 `sub`
UV.imap (\i x -> x - a2 UV.! (rs UV.! i) - a2 UV.! (rt UV.! i) + f2) ra2 `sub`
UV.imap (\i x -> x - b1 UV.! (rs UV.! i) - b1 UV.! (rt UV.! i) + g1) rb1 `add`
UV.imap (\i x -> x - b2 UV.! (rs UV.! i) - b2 UV.! (rt UV.! i) + g2) rb2)
add = UV.zipWith (+)
sub = UV.zipWith (-)
sumsHistogramRepasRollMapPairsHistogramRepasSum_u :: Double -> HistogramRepa ->
(UV.Vector Int, UV.Vector Int) -> HistogramRepa -> UV.Vector Double
sumsHistogramRepasRollMapPairsHistogramRepasSum_u !a !aav (!rs,!rt) !rrv = bb
where
HistogramRepa _ _ aa = aav
HistogramRepa _ _ rr = rrv
!syy = extent rr
!aa' = R.toUnboxed aa
!rr' = R.toUnboxed rr
!r = UV.unsafeIndex syy 0
!bb = UV.create $ do
!bb <- MV.replicate r 0
forM_ [0 .. r-1] $ (\j -> do
let !s = UV.unsafeIndex rs j
let !t = UV.unsafeIndex rt j
MV.unsafeWrite bb j (a + UV.unsafeIndex rr' j - UV.unsafeIndex aa' s - UV.unsafeIndex aa' t))
return bb
setVarsHistogramRepasReduce :: Set.Set Variable -> HistogramRepa -> HistogramRepa
setVarsHistogramRepasReduce kk aa
| V.null vjj = aa
| V.null vkk = HistogramRepa vempty mempty (llrr vsempty [sumAllS rr])
| otherwise = HistogramRepa vkk mkk rr'
where
HistogramRepa vvv mvv !rr = aa
!vv = llqq $ vvll vvv
!svv = extent rr
!n = rank svv
!vkk = llvv $ qqll (kk `cap` vv)
mkk = llmm (zip (vvll vkk) [0..])
!vjj = llvv $ qqll (vv `minus` kk)
!pkk = llvu $ vvll $ V.map (mvv Map.!) vkk
!skk = perm svv pkk
!rr' = R.fromUnboxed skk $ UV.create $ do
!mv <- MV.replicate (R.size skk) 0
!ivv <- MV.replicate n 0
UV.forM_ (toUnboxed rr) $ \a -> do
!i <- toIndexPermM skk pkk ivv
!c <- MV.unsafeRead mv i
MV.unsafeWrite mv i (c+a)
incIndexM_ svv ivv
return mv
vsempty = UV.empty
llrr = R.fromListUnboxed
perm = UV.unsafeBackpermute
llmm = Map.fromList
mempty = Map.empty
qqll = Set.toList
llqq = Set.fromList
minus = Set.difference
cap = Set.intersection
vempty = V.empty
llvv = V.fromList
vvll = V.toList
llvu = UV.fromList
setVarsHistogramRepasReduce_1 :: Set.Set Variable -> HistogramRepa -> HistogramRepa
setVarsHistogramRepasReduce_1 kk aa
| V.null vjj = aa
| V.null vkk = HistogramRepa vempty mempty (llrr vsempty [sumAllS rr])
| otherwise = HistogramRepa vkk mkk rr'
where
vvv = histogramRepasVectorVar aa
vv = llqq $ vvll vvv
rr = histogramRepasArray aa
mvv = histogramRepasMapVarInt aa
svv = extent rr
vkk = llvv $ qqll (kk `cap` vv)
mkk = llmm (zip (vvll vkk) [0..])
vjj = llvv $ qqll (vv `minus` kk)
!pkk = llvu $ vvll $ V.map (mvv Map.!) vkk
!pjj = llvu $ vvll $ V.map (mvv Map.!) vjj
!qvv = llvu $ snd $ unzip $ sort $ zip (vull $ pkk UV.++ pjj) [0..]
!skk = perm svv pkk
!sjj = perm svv pjj
!syy = skk R.:. UV.foldl' (*) 1 (perm svv pjj)
rr' = sumS $ R.backpermute syy back rr
back !(ikk R.:. i) = perm (ikk UV.++ ijj) qvv
where
!ijj = R.fromIndex sjj i
vsempty = UV.empty
llrr = R.fromListUnboxed
perm = UV.unsafeBackpermute
llmm = Map.fromList
mempty = Map.empty
qqll = Set.toList
llqq = Set.fromList
cap = Set.intersection
minus = Set.difference
vempty = V.empty
llvv = V.fromList
vvll = V.toList
llvu = UV.fromList
vull = UV.toList
setVarsHistogramRepasReduce_2 :: Set.Set Variable -> HistogramRepa -> HistogramRepa
setVarsHistogramRepasReduce_2 kk aa
| V.null vjj = aa
| V.null vkk = HistogramRepa vempty mempty (llrr vsempty [sumAllS rr])
| otherwise = HistogramRepa vkk mkk rr'
where
HistogramRepa vvv mvv !rr = aa
vv = llqq $ vvll vvv
!svv = extent rr
vkk = llvv $ qqll (kk `cap` vv)
mkk = llmm (zip (vvll vkk) [0..])
vjj = llvv $ qqll (vv `minus` kk)
!pkk = llvu $ vvll $ V.map (mvv Map.!) vkk
!skk = perm svv pkk
!rr' = R.fromUnboxed skk $ UV.create $ do
mv <- MV.replicate (R.size skk) 0
mapM_ (\(i,a) -> do c <- MV.read mv i; MV.write mv i (c+a))
[(R.toIndex skk (perm ivv pkk),a) |
(!j,!a) <- zip [0..] (UV.toList (toUnboxed rr)), let !ivv = R.fromIndex svv j]
return mv
vsempty = UV.empty
llrr = R.fromListUnboxed
perm = UV.unsafeBackpermute
llmm = Map.fromList
mempty = Map.empty
qqll = Set.toList
llqq = Set.fromList
minus = Set.difference
cap = Set.intersection
vempty = V.empty
llvv = V.fromList
vvll = V.toList
llvu = UV.fromList
setVarsHistogramRepasReduce_3 :: Set.Set Variable -> HistogramRepa -> HistogramRepa
setVarsHistogramRepasReduce_3 kk aa
| V.null vjj = aa
| V.null vkk = HistogramRepa vempty mempty (llrr vsempty [sumAllS rr])
| otherwise = HistogramRepa vkk mkk rr'
where
HistogramRepa vvv mvv !rr = aa
vv = llqq $ vvll vvv
!svv = extent rr
!n = rank svv
vkk = llvv $ qqll (kk `cap` vv)
mkk = llmm (zip (vvll vkk) [0..])
vjj = llvv $ qqll (vv `minus` kk)
!pkk = llvu $ vvll $ V.map (mvv Map.!) vkk
!skk = perm svv pkk
!rr' = R.fromUnboxed skk $ UV.create $ do
mv <- MV.replicate (R.size skk) 0
mapM_ (\(i,a) -> do c <- MV.read mv i; MV.write mv i (c+a))
[(R.toIndex skk (perm ivv pkk),a) |
(!ivv,!a) <- zip (linc svv (UV.replicate n 0)) (UV.toList (toUnboxed rr))]
return mv
linc !svv !ivv = let !jvv = incIndex svv ivv in ivv : linc svv jvv
vsempty = UV.empty
llrr = R.fromListUnboxed
perm = UV.unsafeBackpermute
llmm = Map.fromList
mempty = Map.empty
qqll = Set.toList
llqq = Set.fromList
minus = Set.difference
cap = Set.intersection
vempty = V.empty
llvv = V.fromList
vvll = V.toList
llvu = UV.fromList
setVarsHistogramRepasReduce_4 :: Set.Set Variable -> HistogramRepa -> HistogramRepa
setVarsHistogramRepasReduce_4 kk aa
| V.null vjj = aa
| V.null vkk = HistogramRepa vempty mempty (llrr vsempty [sumAllS rr])
| otherwise = HistogramRepa vkk mkk rr'
where
HistogramRepa vvv mvv !rr = aa
!vv = llqq $ vvll vvv
!svv = extent rr
!n = rank svv
!vkk = llvv $ qqll (kk `cap` vv)
mkk = llmm (zip (vvll vkk) [0..])
!vjj = llvv $ qqll (vv `minus` kk)
!pkk = llvu $ vvll $ V.map (mvv Map.!) vkk
!skk = perm svv pkk
!rr' = R.fromUnboxed skk $ UV.create $ do
!mv <- MV.replicate (R.size skk) 0
!xvv <- newSTRef (UV.replicate n 0)
UV.forM_ (toUnboxed rr) $ \a -> do
!ivv <- readSTRef xvv
let !i = R.toIndex skk (perm ivv pkk)
!c <- MV.read mv i
MV.write mv i (c+a)
writeSTRef xvv (incIndex svv ivv)
return mv
vsempty = UV.empty
llrr = R.fromListUnboxed
perm = UV.unsafeBackpermute
llmm = Map.fromList
mempty = Map.empty
qqll = Set.toList
llqq = Set.fromList
minus = Set.difference
cap = Set.intersection
vempty = V.empty
llvv = V.fromList
vvll = V.toList
llvu = UV.fromList
varsHistogramRepaVecsReduceSingleVec_u :: Int -> HistogramRepaVec -> HistogramRepaVec
varsHistogramRepaVecsReduceSingleVec_u !u !rrv = HistogramRepaVec vyy myy z syy vbb
where
HistogramRepaVec vvv mvv z svv vaa = rrv
!v = R.size svv
!n = rank svv
!d = UV.unsafeIndex svv u
!x = V.unsafeIndex vvv u
vyy = V.singleton x
myy = Map.singleton x 0
!syy = UV.singleton d
!pvv = UV.singleton u
!p = V.length vaa
!vbb = runST $ do
vbb <- V.replicateM p (MV.replicate d 0)
!ivv <- MV.replicate n 0
forM_ [0 .. v-1] $ (\j -> do
!i <- toIndexPermM syy pvv ivv
forM_ [0 .. p-1] $ (\l -> do
let !mv = V.unsafeIndex vbb l
c <- MV.unsafeRead mv i
let !a = UV.unsafeIndex (V.unsafeIndex vaa l) j
MV.unsafeWrite mv i (c+a))
incIndexM_ svv ivv)
V.mapM UV.unsafeFreeze vbb
varsHistogramRepa4VecsReduceSingle_u :: Int -> HistogramRepaVec -> HistogramRepa
varsHistogramRepa4VecsReduceSingle_u !u !rrv = HistogramRepa vyy myy bb
where
HistogramRepaVec vvv mvv _ svv vaa = rrv
!v = R.size svv
!n = rank svv
!d = UV.unsafeIndex svv u
!x = V.unsafeIndex vvv u
vyy = V.singleton x
myy = Map.singleton x 0
!syy = UV.singleton d
!va1 = V.unsafeIndex vaa 0
!va2 = V.unsafeIndex vaa 1
!vb1 = V.unsafeIndex vaa 2
!vb2 = V.unsafeIndex vaa 3
!bb = R.fromUnboxed syy $ UV.create $ do
!bb <- MV.replicate d 0
!ivv <- MV.replicate n 0
forM_ [0 .. v-1] $ (\j -> do
i <- MV.unsafeRead ivv u
c <- MV.unsafeRead bb i
let !a1 = UV.unsafeIndex va1 j
let !a2 = UV.unsafeIndex va2 j
let !b1 = UV.unsafeIndex vb1 j
let !b2 = UV.unsafeIndex vb2 j
MV.unsafeWrite bb i (c+a1-a2-b1+b2)
incIndexM_ svv ivv)
return bb
histogramRepasRed_u :: Double -> HistogramRepa -> HistogramRepaRed
histogramRepasRed_u z aa = HistogramRepaRed vvv mvv svv lrr
where
HistogramRepa vvv mvv !rr = aa
!svv = extent rr
!n = rank svv
!f = 1 / z
!lrr = runST $ do
vrr <- V.generateM n (\i -> MV.replicate (svv UV.! i) 0)
mapM_ (\(mw,i,a) -> do c <- MV.read mw i; MV.write mw i (c+a))
[(mw,(ivv UV.! k),a) | (!j,!a) <- zip [0..] (UV.toList (toUnboxed rr)),
let !ivv = R.fromIndex svv j, !k <- [0..n-1], let !mw = vrr V.! k]
mapM_ (\(mw,i) -> do c <- MV.read mw i; MV.write mw i (c*f))
[(mw,i) | f /= 1, !k <- [0..n-1], let !mw = vrr V.! k, !i <- [0.. (svv UV.! k)-1]]
V.mapM UV.unsafeFreeze vrr
histogramRepaVecsRedVec :: HistogramRepaVec -> HistogramRepaRedVec
histogramRepaVecsRedVec !rrv = HistogramRepaRedVec vvv mvv z svv vxx
where
HistogramRepaVec vvv mvv z svv vaa = rrv
!v = R.size svv
!n = rank svv
!p = V.length vaa
!vxx = runST $ do
vxx <- V.replicateM p (V.generateM n (\i -> MV.replicate (UV.unsafeIndex svv i) 0))
!ivv <- MV.replicate n 0
forM_ [0 .. v-1] $ (\j -> do
forM_ [0 .. n-1] $ (\k -> do
!i <- MV.unsafeRead ivv k
forM_ [0 .. p-1] $ (\l -> do
let !mv = V.unsafeIndex (V.unsafeIndex vxx l) k
c <- MV.unsafeRead mv i
let !a = UV.unsafeIndex (V.unsafeIndex vaa l) j
MV.unsafeWrite mv i (c+a)))
incIndexM_ svv ivv)
V.mapM (V.mapM UV.unsafeFreeze) vxx
histogramRepa4VecsRed_u :: HistogramRepaVec -> HistogramRepaRed
histogramRepa4VecsRed_u !aav = HistogramRepaRed vvv mvv svv vbb
where
HistogramRepaVec vvv mvv _ svv vaa = aav
!v = R.size svv
!n = rank svv
!va1 = V.unsafeIndex vaa 0
!va2 = V.unsafeIndex vaa 1
!vb1 = V.unsafeIndex vaa 2
!vb2 = V.unsafeIndex vaa 3
!vbb = runST $ do
vbb <- V.generateM n (\i -> MV.replicate (UV.unsafeIndex svv i) 0)
!ivv <- MV.replicate n 0
forM_ [0 .. v-1] $ (\j -> do
let !a1 = UV.unsafeIndex va1 j
let !a2 = UV.unsafeIndex va2 j
let !b1 = UV.unsafeIndex vb1 j
let !b2 = UV.unsafeIndex vb2 j
forM_ [0 .. n-1] $ (\k -> do
i <- MV.unsafeRead ivv k
let !bb = V.unsafeIndex vbb k
c <- MV.unsafeRead bb i
MV.unsafeWrite bb i (c+a1-a2-b1+b2))
incIndexM_ svv ivv)
V.mapM UV.unsafeFreeze vbb
varsHistogramRepaRedVecsSingleVec_u :: Int -> HistogramRepaRedVec -> HistogramRepaVec
varsHistogramRepaRedVecsSingleVec_u !u !xxv = HistogramRepaVec vyy myy z syy vbb
where
HistogramRepaRedVec vvv mvv z svv vxx = xxv
!d = UV.unsafeIndex svv u
!x = V.unsafeIndex vvv u
vyy = V.singleton x
myy = Map.singleton x 0
!syy = UV.singleton d
!vbb = V.map (\xx -> V.unsafeIndex xx u) vxx
varsHistogramRepaRedsSingle_u :: Int -> HistogramRepaRed -> HistogramRepa
varsHistogramRepaRedsSingle_u !u !xxv = HistogramRepa vyy myy bb
where
HistogramRepaRed vvv mvv svv vxx = xxv
!d = UV.unsafeIndex svv u
!x = V.unsafeIndex vvv u
vyy = V.singleton x
myy = Map.singleton x 0
!syy = UV.singleton d
!bb = R.fromUnboxed syy $ V.unsafeIndex vxx u
setSetVarsHistogramRepasPartitionRed_u :: Double -> Set.Set (Set.Set Variable) -> HistogramRepa -> HistogramRepaRed
setSetVarsHistogramRepasPartitionRed_u z pp aa = HistogramRepaRed vxx mxx sxx lrr
where
HistogramRepa vvv mvv !rr = aa
!svv = extent rr
!n = rank svv
!f = 1 / z
!vpp = llvv $ [qqvv cc | cc <- qqll pp]
!m = V.length vpp
xx = List.map VarIndex [0 .. m-1]
vxx = llvv xx
mxx = llmm (zip xx [0..])
!ppp = V.map (\vcc -> V.convert $ V.map (mvv Map.!) vcc) vpp
!spp = V.map (\pcc -> perm svv pcc) ppp
!sxx = V.convert $ V.map R.size spp
!lrr = runST $ do
vrr <- V.generateM m (\i -> MV.replicate (sxx UV.! i) 0)
mapM_ (\(mw,i,a) -> do c <- MV.read mw i; MV.write mw i (c+a))
[(vrr V.! k, (R.toIndex (spp V.! k) $ perm ivv (ppp V.! k)), a) |
(!j,!a) <- zip [0..] (UV.toList (toUnboxed rr)), let !ivv = R.fromIndex svv j, !k <- [0..m-1]]
mapM_ (\(mw,i) -> do c <- MV.read mw i; MV.write mw i (c*f))
[(vrr V.! k, i) | f /= 1, !k <- [0..m-1], !i <- [0.. (sxx UV.! k)-1]]
V.mapM UV.unsafeFreeze vrr
llmm = Map.fromList
perm = UV.unsafeBackpermute
qqvv = llvv . qqll
llvv = V.fromList
vvll = V.toList
qqll :: forall a. Set.Set a -> [a]
qqll = Set.toList