@@ -48,10 +48,39 @@ and associated documentation files (the “Software”), to deal in the Software
48
48
f [x_ SparseArray , y_ SparseArray , z_ SparseArray ] := (++ W [[## ]] & @@@ tup [x , y , z ]; );
49
49
50
50
(* recall x, y, or z *)
51
- f [_ , y_ SparseArray , z_ SparseArray ] := f [ Plus @@ ( W [[All , #1 , #2 ]] & @@@ tup [y , z ])];
52
- f [x_ SparseArray , _ , z_ SparseArray ] := f [ Plus @@ ( W [[#1 , All , #2 ]] & @@@ tup [x , z ])];
53
- f [x_ SparseArray , y_ SparseArray , _ ] := f [ Plus @@ ( W [[#1 , #2 , All ]] & @@@ tup [x , y ])];
51
+ f [Verbatim [ _ ] , y_ SparseArray , z_ SparseArray ] := f [ Plus @@ ( W [[All , #1 , #2 ]] & @@@ tup [y , z ])];
52
+ f [x_ SparseArray , Verbatim [ _ ] , z_ SparseArray ] := f [ Plus @@ ( W [[#1 , All , #2 ]] & @@@ tup [x , z ])];
53
+ f [x_ SparseArray , y_ SparseArray , Verbatim [ _ ] ] := f [ Plus @@ ( W [[#1 , #2 , All ]] & @@@ tup [x , y ])];
54
54
55
55
];
56
56
57
57
58
+
59
+ (* Wrapper for TriadicMemory which automatically encodes any Mathematica expression to random sparse arrays *)
60
+
61
+
62
+ TriadicMemoryEncoding [f_ Symbol , {n_ Integer , p_ Integer } ] :=
63
+
64
+ Module [ {T , encode , decode , pos },
65
+
66
+ TriadicMemory [ T , {n , p }];
67
+
68
+ pos [x_ SparseArray ] := Sort [Flatten [x ["NonzeroPositions" ]]];
69
+
70
+ encode [x_ SparseArray ] := x ;
71
+ encode [Verbatim [_ ]] = _ ;
72
+ encode [Null ] = SparseArray [{0 }, {n }];
73
+ encode [s_ ] :=
74
+ encode [s ] = Module [ {r = T []}, decode [pos [r ]] = s ; r ];
75
+
76
+ decode [SparseArray [{0 }, {n }]] = Null ;
77
+ decode [x_ SparseArray ] := Module [ {r }, r = decode [ pos [x ]]; If [ Head [r ] === decode , x , r ]];
78
+ decode [Null ] = Null ;
79
+
80
+ f [] = T []; (* random generator *)
81
+
82
+ f [x_ , y_ , z_ ] := decode [ T [ encode [x ], encode [y ], encode [z ]]];
83
+
84
+ ];
85
+
86
+
0 commit comments