eyeling 1.5.42 → 1.6.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/examples/cobalt-kepler-kitchen.n3 +220 -0
- package/examples/jade-eigen-loom.n3 +322 -0
- package/examples/not-includes.n3 +32 -0
- package/examples/output/cobalt-kepler-kitchen.n3 +7112 -0
- package/examples/output/cranberry-calculus.n3 +495 -495
- package/examples/output/jade-eigen-loom.n3 +4749 -0
- package/examples/output/not-includes.n3 +69 -0
- package/examples/output/ruby-runge-workshop.n3 +628 -0
- package/examples/output/topaz-markov-mill.n3 +4231 -0
- package/examples/output/traffic-skos-aggregate.n3 +3298 -0
- package/examples/output/ultramarine-simpson-forge.n3 +3936 -0
- package/examples/ruby-runge-workshop.n3 +256 -0
- package/examples/topaz-markov-mill.n3 +260 -0
- package/examples/traffic-skos-aggregate.n3 +319 -0
- package/examples/ultramarine-simpson-forge.n3 +178 -0
- package/eyeling.js +203 -133
- package/package.json +1 -1
|
@@ -0,0 +1,256 @@
|
|
|
1
|
+
# ================================================================
|
|
2
|
+
# Heavy-Math N3 Demo: "Ruby Runge Workshop"
|
|
3
|
+
# ------------------------------------------------
|
|
4
|
+
# A math-stress N3 ruleset for polynomial interpolation (Lagrange form),
|
|
5
|
+
# plus numerical differentiation and a simple root search check.
|
|
6
|
+
#
|
|
7
|
+
# What it does:
|
|
8
|
+
# 1) Given a set of (x_i, y_i) points, computes Lagrange basis weights
|
|
9
|
+
# at a query x0 and outputs interpolated y(x0):
|
|
10
|
+
#
|
|
11
|
+
# y(x0) = Σ_i y_i * Π_{j≠i} (x0 - x_j)/(x_i - x_j)
|
|
12
|
+
#
|
|
13
|
+
# This exercises lots of nested products/quotients and list collection.
|
|
14
|
+
#
|
|
15
|
+
# 2) Estimates derivative at x0 by symmetric difference:
|
|
16
|
+
# y'(x0) ≈ (y(x0+h) - y(x0-h)) / (2h)
|
|
17
|
+
#
|
|
18
|
+
# 3) Flags if y(x0) crosses zero between two bracketing x-values
|
|
19
|
+
# (a basic “root bracket” detector).
|
|
20
|
+
#
|
|
21
|
+
# Notes:
|
|
22
|
+
# - Uses only: math:, list:, log:
|
|
23
|
+
# - Designed as a strict-datatype regression workload.
|
|
24
|
+
# ================================================================
|
|
25
|
+
|
|
26
|
+
@prefix : <http://example.org/ruby-runge#> .
|
|
27
|
+
@prefix math: <http://www.w3.org/2000/10/swap/math#> .
|
|
28
|
+
@prefix list: <http://www.w3.org/2000/10/swap/list#> .
|
|
29
|
+
@prefix log: <http://www.w3.org/2000/10/swap/log#> .
|
|
30
|
+
|
|
31
|
+
############
|
|
32
|
+
# DATA
|
|
33
|
+
############
|
|
34
|
+
|
|
35
|
+
:Interp1
|
|
36
|
+
:points (
|
|
37
|
+
[ :id 1 ; :x -2.0 ; :y 7.0 ]
|
|
38
|
+
[ :id 2 ; :x -1.0 ; :y 1.0 ]
|
|
39
|
+
[ :id 3 ; :x 0.0 ; :y -1.0 ]
|
|
40
|
+
[ :id 4 ; :x 1.0 ; :y 1.0 ]
|
|
41
|
+
[ :id 5 ; :x 2.0 ; :y 7.0 ]
|
|
42
|
+
) ;
|
|
43
|
+
:x0 0.6 ;
|
|
44
|
+
:h 0.001 ;
|
|
45
|
+
:bracketA 0.0 ;
|
|
46
|
+
:bracketB 1.0 .
|
|
47
|
+
|
|
48
|
+
############
|
|
49
|
+
# Helper: compute product of a collected list
|
|
50
|
+
# (We just use math:productPairwise by folding: collectAll + math:product works
|
|
51
|
+
# in many engines, but to stay portable we explicitly fold with list:member.)
|
|
52
|
+
# We'll implement "sum of products" directly without defining new builtins.
|
|
53
|
+
############
|
|
54
|
+
|
|
55
|
+
############
|
|
56
|
+
# (1) LAGRANGE INTERPOLATION y(x0)
|
|
57
|
+
############
|
|
58
|
+
|
|
59
|
+
{
|
|
60
|
+
:Interp1 :points ?pts ; :x0 ?x0 .
|
|
61
|
+
|
|
62
|
+
# Collect each term_i = y_i * basis_i(x0)
|
|
63
|
+
( ?term {
|
|
64
|
+
?pts list:member ?pi .
|
|
65
|
+
?pi :x ?xi ; :y ?yi .
|
|
66
|
+
|
|
67
|
+
# Collect factors f_ij = (x0 - xj)/(xi - xj) for all j != i
|
|
68
|
+
( ?f {
|
|
69
|
+
?pts list:member ?pj .
|
|
70
|
+
?pj :x ?xj .
|
|
71
|
+
?xj math:notEqualTo ?xi .
|
|
72
|
+
|
|
73
|
+
(?x0 ?xj) math:difference ?num .
|
|
74
|
+
(?xi ?xj) math:difference ?den .
|
|
75
|
+
(?num ?den) math:quotient ?f .
|
|
76
|
+
} ?factors
|
|
77
|
+
) log:collectAllIn _:lag1 .
|
|
78
|
+
|
|
79
|
+
# Multiply all factors in ?factors to get basis_i
|
|
80
|
+
# basis = Π f
|
|
81
|
+
# We'll do it by collecting logs, but simplest is:
|
|
82
|
+
?factors math:product ?basis .
|
|
83
|
+
|
|
84
|
+
(?yi ?basis) math:product ?term .
|
|
85
|
+
} ?terms
|
|
86
|
+
) log:collectAllIn _:lag0 .
|
|
87
|
+
|
|
88
|
+
# y0 = Σ term_i
|
|
89
|
+
?terms math:sum ?y0 .
|
|
90
|
+
}
|
|
91
|
+
=>
|
|
92
|
+
{
|
|
93
|
+
:Interp1 :yAtX0 ?y0 .
|
|
94
|
+
} .
|
|
95
|
+
|
|
96
|
+
############
|
|
97
|
+
# (2) SYMMETRIC DIFFERENCE DERIVATIVE
|
|
98
|
+
# y'(x0) ≈ (y(x0+h) - y(x0-h)) / (2h)
|
|
99
|
+
#
|
|
100
|
+
# We compute yPlus and yMinus using the same Lagrange mechanism.
|
|
101
|
+
############
|
|
102
|
+
|
|
103
|
+
# y(x0+h)
|
|
104
|
+
{
|
|
105
|
+
:Interp1 :points ?pts ; :x0 ?x0 ; :h ?h .
|
|
106
|
+
(?x0 ?h) math:sum ?xPlus .
|
|
107
|
+
|
|
108
|
+
( ?term {
|
|
109
|
+
?pts list:member ?pi .
|
|
110
|
+
?pi :x ?xi ; :y ?yi .
|
|
111
|
+
|
|
112
|
+
( ?f {
|
|
113
|
+
?pts list:member ?pj .
|
|
114
|
+
?pj :x ?xj .
|
|
115
|
+
?xj math:notEqualTo ?xi .
|
|
116
|
+
(?xPlus ?xj) math:difference ?num .
|
|
117
|
+
(?xi ?xj) math:difference ?den .
|
|
118
|
+
(?num ?den) math:quotient ?f .
|
|
119
|
+
} ?factors
|
|
120
|
+
) log:collectAllIn _:d1 .
|
|
121
|
+
?factors math:product ?basis .
|
|
122
|
+
(?yi ?basis) math:product ?term .
|
|
123
|
+
} ?terms
|
|
124
|
+
) log:collectAllIn _:d0 .
|
|
125
|
+
?terms math:sum ?yPlus .
|
|
126
|
+
}
|
|
127
|
+
=>
|
|
128
|
+
{
|
|
129
|
+
:Interp1 :yAtXPlus ?yPlus ;
|
|
130
|
+
:xPlus ?xPlus .
|
|
131
|
+
} .
|
|
132
|
+
|
|
133
|
+
# y(x0-h)
|
|
134
|
+
{
|
|
135
|
+
:Interp1 :points ?pts ; :x0 ?x0 ; :h ?h .
|
|
136
|
+
(?x0 ?h) math:difference ?xMinus .
|
|
137
|
+
|
|
138
|
+
( ?term {
|
|
139
|
+
?pts list:member ?pi .
|
|
140
|
+
?pi :x ?xi ; :y ?yi .
|
|
141
|
+
|
|
142
|
+
( ?f {
|
|
143
|
+
?pts list:member ?pj .
|
|
144
|
+
?pj :x ?xj .
|
|
145
|
+
?xj math:notEqualTo ?xi .
|
|
146
|
+
(?xMinus ?xj) math:difference ?num .
|
|
147
|
+
(?xi ?xj) math:difference ?den .
|
|
148
|
+
(?num ?den) math:quotient ?f .
|
|
149
|
+
} ?factors
|
|
150
|
+
) log:collectAllIn _:e1 .
|
|
151
|
+
?factors math:product ?basis .
|
|
152
|
+
(?yi ?basis) math:product ?term .
|
|
153
|
+
} ?terms
|
|
154
|
+
) log:collectAllIn _:e0 .
|
|
155
|
+
?terms math:sum ?yMinus .
|
|
156
|
+
}
|
|
157
|
+
=>
|
|
158
|
+
{
|
|
159
|
+
:Interp1 :yAtXMinus ?yMinus ;
|
|
160
|
+
:xMinus ?xMinus .
|
|
161
|
+
} .
|
|
162
|
+
|
|
163
|
+
# derivative from yPlus, yMinus
|
|
164
|
+
{
|
|
165
|
+
:Interp1 :yAtXPlus ?yP ; :yAtXMinus ?yM ; :h ?h .
|
|
166
|
+
(?yP ?yM) math:difference ?dy .
|
|
167
|
+
(2.0 ?h) math:product ?twoH .
|
|
168
|
+
(?dy ?twoH) math:quotient ?dydx .
|
|
169
|
+
}
|
|
170
|
+
=>
|
|
171
|
+
{
|
|
172
|
+
:Interp1 :derivativeAtX0 ?dydx .
|
|
173
|
+
} .
|
|
174
|
+
|
|
175
|
+
############
|
|
176
|
+
# (3) ROOT BRACKET CHECK between bracketA and bracketB
|
|
177
|
+
# If y(a) and y(b) have opposite signs, mark a bracket.
|
|
178
|
+
#
|
|
179
|
+
# We compute y(a) and y(b) by interpolation too (same points set).
|
|
180
|
+
############
|
|
181
|
+
|
|
182
|
+
# y at bracketA
|
|
183
|
+
{
|
|
184
|
+
:Interp1 :points ?pts ; :bracketA ?xa .
|
|
185
|
+
|
|
186
|
+
( ?term {
|
|
187
|
+
?pts list:member ?pi .
|
|
188
|
+
?pi :x ?xi ; :y ?yi .
|
|
189
|
+
|
|
190
|
+
( ?f {
|
|
191
|
+
?pts list:member ?pj .
|
|
192
|
+
?pj :x ?xj .
|
|
193
|
+
?xj math:notEqualTo ?xi .
|
|
194
|
+
(?xa ?xj) math:difference ?num .
|
|
195
|
+
(?xi ?xj) math:difference ?den .
|
|
196
|
+
(?num ?den) math:quotient ?f .
|
|
197
|
+
} ?factors
|
|
198
|
+
) log:collectAllIn _:ra1 .
|
|
199
|
+
?factors math:product ?basis .
|
|
200
|
+
(?yi ?basis) math:product ?term .
|
|
201
|
+
} ?terms
|
|
202
|
+
) log:collectAllIn _:ra0 .
|
|
203
|
+
?terms math:sum ?ya .
|
|
204
|
+
}
|
|
205
|
+
=>
|
|
206
|
+
{
|
|
207
|
+
:Interp1 :yAtBracketA ?ya .
|
|
208
|
+
} .
|
|
209
|
+
|
|
210
|
+
# y at bracketB
|
|
211
|
+
{
|
|
212
|
+
:Interp1 :points ?pts ; :bracketB ?xb .
|
|
213
|
+
|
|
214
|
+
( ?term {
|
|
215
|
+
?pts list:member ?pi .
|
|
216
|
+
?pi :x ?xi ; :y ?yi .
|
|
217
|
+
|
|
218
|
+
( ?f {
|
|
219
|
+
?pts list:member ?pj .
|
|
220
|
+
?pj :x ?xj .
|
|
221
|
+
?xj math:notEqualTo ?xi .
|
|
222
|
+
(?xb ?xj) math:difference ?num .
|
|
223
|
+
(?xi ?xj) math:difference ?den .
|
|
224
|
+
(?num ?den) math:quotient ?f .
|
|
225
|
+
} ?factors
|
|
226
|
+
) log:collectAllIn _:rb1 .
|
|
227
|
+
?factors math:product ?basis .
|
|
228
|
+
(?yi ?basis) math:product ?term .
|
|
229
|
+
} ?terms
|
|
230
|
+
) log:collectAllIn _:rb0 .
|
|
231
|
+
?terms math:sum ?yb .
|
|
232
|
+
}
|
|
233
|
+
=>
|
|
234
|
+
{
|
|
235
|
+
:Interp1 :yAtBracketB ?yb .
|
|
236
|
+
} .
|
|
237
|
+
|
|
238
|
+
# bracket detection via sign test: ya*yb < 0
|
|
239
|
+
{
|
|
240
|
+
:Interp1 :bracketA ?xa ; :bracketB ?xb ;
|
|
241
|
+
:yAtBracketA ?ya ; :yAtBracketB ?yb .
|
|
242
|
+
(?ya ?yb) math:product ?prod .
|
|
243
|
+
?prod math:lessThan 0.0 .
|
|
244
|
+
}
|
|
245
|
+
=>
|
|
246
|
+
{
|
|
247
|
+
:Interp1 :rootBracket [ :a ?xa ; :b ?xb ; :ya ?ya ; :yb ?yb ] .
|
|
248
|
+
} .
|
|
249
|
+
|
|
250
|
+
############
|
|
251
|
+
# STRICTNESS REGRESSION HOOK (should fail in strict math)
|
|
252
|
+
############
|
|
253
|
+
# {
|
|
254
|
+
# "0.6"^^<http://example.org/not-a-number> math:sum 1.0 .
|
|
255
|
+
# } => { :bad :datatype :accepted . } .
|
|
256
|
+
|
|
@@ -0,0 +1,260 @@
|
|
|
1
|
+
# ================================================================
|
|
2
|
+
# Heavy-Math N3 Demo: "Topaz Markov Mill (No-Log Edition)"
|
|
3
|
+
# ------------------------------------------------
|
|
4
|
+
# Markov chain workload that stays inside the W3C N3 math builtins.
|
|
5
|
+
#
|
|
6
|
+
# What it does:
|
|
7
|
+
# 1) Validates row-stochasticity (row sums = 1)
|
|
8
|
+
# 2) Propagates π0 -> π1 -> π2 -> π3
|
|
9
|
+
# 3) Computes P^2 row for A (extra matrix workload)
|
|
10
|
+
# 4) For each πt, computes:
|
|
11
|
+
# - sumSq = Σ p_i^2
|
|
12
|
+
# - gini = 1 - sumSq
|
|
13
|
+
# - effN = 1 / sumSq (effective number of states)
|
|
14
|
+
# - TV = 0.5 * Σ |p_i - 1/3|
|
|
15
|
+
# - L2 = sqrt( Σ (p_i - 1/3)^2 )
|
|
16
|
+
#
|
|
17
|
+
# Builtins used: math:, list:
|
|
18
|
+
# ================================================================
|
|
19
|
+
|
|
20
|
+
@prefix : <http://example.org/topaz-markov#> .
|
|
21
|
+
@prefix math: <http://www.w3.org/2000/10/swap/math#> .
|
|
22
|
+
@prefix list: <http://www.w3.org/2000/10/swap/list#> .
|
|
23
|
+
|
|
24
|
+
############
|
|
25
|
+
# DATA
|
|
26
|
+
############
|
|
27
|
+
|
|
28
|
+
:MC1
|
|
29
|
+
:states ( :A :B :C ) ;
|
|
30
|
+
|
|
31
|
+
# Transition matrix P, encoded as explicit row nodes
|
|
32
|
+
:row [ :from :A ; :pA 0.80 ; :pB 0.15 ; :pC 0.05 ] ;
|
|
33
|
+
:row [ :from :B ; :pA 0.10 ; :pB 0.70 ; :pC 0.20 ] ;
|
|
34
|
+
:row [ :from :C ; :pA 0.25 ; :pB 0.25 ; :pC 0.50 ] ;
|
|
35
|
+
|
|
36
|
+
# Initial distribution π0 over (A,B,C)
|
|
37
|
+
:pi0 [ :pA 0.60 ; :pB 0.30 ; :pC 0.10 ] .
|
|
38
|
+
|
|
39
|
+
############
|
|
40
|
+
# (1) ROW-STOCHASTIC CHECK: pA+pB+pC = 1.0
|
|
41
|
+
############
|
|
42
|
+
|
|
43
|
+
{
|
|
44
|
+
:MC1 :row ?r .
|
|
45
|
+
?r :pA ?a ; :pB ?b ; :pC ?c .
|
|
46
|
+
(?a ?b) math:sum ?ab .
|
|
47
|
+
(?ab ?c) math:sum ?sum .
|
|
48
|
+
?sum math:equalTo 1.0 .
|
|
49
|
+
}
|
|
50
|
+
=>
|
|
51
|
+
{
|
|
52
|
+
:MC1 :rowOk ?r .
|
|
53
|
+
} .
|
|
54
|
+
|
|
55
|
+
############
|
|
56
|
+
# (2) ONE-STEP PROPAGATION (explicit for π0->π1, π1->π2, π2->π3)
|
|
57
|
+
############
|
|
58
|
+
|
|
59
|
+
# π1 = π0 P
|
|
60
|
+
{
|
|
61
|
+
:MC1 :pi0 ?pi .
|
|
62
|
+
:MC1 :row ?rA . ?rA :from :A ; :pA ?AA ; :pB ?AB ; :pC ?AC .
|
|
63
|
+
:MC1 :row ?rB . ?rB :from :B ; :pA ?BA ; :pB ?BB ; :pC ?BC .
|
|
64
|
+
:MC1 :row ?rC . ?rC :from :C ; :pA ?CA ; :pB ?CB ; :pC ?CC .
|
|
65
|
+
|
|
66
|
+
?pi :pA ?pA ; :pB ?pB ; :pC ?pC .
|
|
67
|
+
|
|
68
|
+
# π1A
|
|
69
|
+
(?pA ?AA) math:product ?tAA .
|
|
70
|
+
(?pB ?BA) math:product ?tBA .
|
|
71
|
+
(?pC ?CA) math:product ?tCA .
|
|
72
|
+
(?tAA ?tBA) math:sum ?s1 .
|
|
73
|
+
(?s1 ?tCA) math:sum ?pi1A .
|
|
74
|
+
|
|
75
|
+
# π1B
|
|
76
|
+
(?pA ?AB) math:product ?tAB .
|
|
77
|
+
(?pB ?BB) math:product ?tBB .
|
|
78
|
+
(?pC ?CB) math:product ?tCB .
|
|
79
|
+
(?tAB ?tBB) math:sum ?s2 .
|
|
80
|
+
(?s2 ?tCB) math:sum ?pi1B .
|
|
81
|
+
|
|
82
|
+
# π1C
|
|
83
|
+
(?pA ?AC) math:product ?tAC .
|
|
84
|
+
(?pB ?BC) math:product ?tBC .
|
|
85
|
+
(?pC ?CC) math:product ?tCC .
|
|
86
|
+
(?tAC ?tBC) math:sum ?s3 .
|
|
87
|
+
(?s3 ?tCC) math:sum ?pi1C .
|
|
88
|
+
}
|
|
89
|
+
=>
|
|
90
|
+
{
|
|
91
|
+
:MC1 :pi1 [ :pA ?pi1A ; :pB ?pi1B ; :pC ?pi1C ] .
|
|
92
|
+
} .
|
|
93
|
+
|
|
94
|
+
# π2 = π1 P
|
|
95
|
+
{
|
|
96
|
+
:MC1 :pi1 ?pi .
|
|
97
|
+
:MC1 :row ?rA . ?rA :from :A ; :pA ?AA ; :pB ?AB ; :pC ?AC .
|
|
98
|
+
:MC1 :row ?rB . ?rB :from :B ; :pA ?BA ; :pB ?BB ; :pC ?BC .
|
|
99
|
+
:MC1 :row ?rC . ?rC :from :C ; :pA ?CA ; :pB ?CB ; :pC ?CC .
|
|
100
|
+
|
|
101
|
+
?pi :pA ?pA ; :pB ?pB ; :pC ?pC .
|
|
102
|
+
|
|
103
|
+
(?pA ?AA) math:product ?tAA .
|
|
104
|
+
(?pB ?BA) math:product ?tBA .
|
|
105
|
+
(?pC ?CA) math:product ?tCA .
|
|
106
|
+
(?tAA ?tBA) math:sum ?s1 .
|
|
107
|
+
(?s1 ?tCA) math:sum ?pi2A .
|
|
108
|
+
|
|
109
|
+
(?pA ?AB) math:product ?tAB .
|
|
110
|
+
(?pB ?BB) math:product ?tBB .
|
|
111
|
+
(?pC ?CB) math:product ?tCB .
|
|
112
|
+
(?tAB ?tBB) math:sum ?s2 .
|
|
113
|
+
(?s2 ?tCB) math:sum ?pi2B .
|
|
114
|
+
|
|
115
|
+
(?pA ?AC) math:product ?tAC .
|
|
116
|
+
(?pB ?BC) math:product ?tBC .
|
|
117
|
+
(?pC ?CC) math:product ?tCC .
|
|
118
|
+
(?tAC ?tBC) math:sum ?s3 .
|
|
119
|
+
(?s3 ?tCC) math:sum ?pi2C .
|
|
120
|
+
}
|
|
121
|
+
=>
|
|
122
|
+
{
|
|
123
|
+
:MC1 :pi2 [ :pA ?pi2A ; :pB ?pi2B ; :pC ?pi2C ] .
|
|
124
|
+
} .
|
|
125
|
+
|
|
126
|
+
# π3 = π2 P
|
|
127
|
+
{
|
|
128
|
+
:MC1 :pi2 ?pi .
|
|
129
|
+
:MC1 :row ?rA . ?rA :from :A ; :pA ?AA ; :pB ?AB ; :pC ?AC .
|
|
130
|
+
:MC1 :row ?rB . ?rB :from :B ; :pA ?BA ; :pB ?BB ; :pC ?BC .
|
|
131
|
+
:MC1 :row ?rC . ?rC :from :C ; :pA ?CA ; :pB ?CB ; :pC ?CC .
|
|
132
|
+
|
|
133
|
+
?pi :pA ?pA ; :pB ?pB ; :pC ?pC .
|
|
134
|
+
|
|
135
|
+
(?pA ?AA) math:product ?tAA .
|
|
136
|
+
(?pB ?BA) math:product ?tBA .
|
|
137
|
+
(?pC ?CA) math:product ?tCA .
|
|
138
|
+
(?tAA ?tBA) math:sum ?s1 .
|
|
139
|
+
(?s1 ?tCA) math:sum ?pi3A .
|
|
140
|
+
|
|
141
|
+
(?pA ?AB) math:product ?tAB .
|
|
142
|
+
(?pB ?BB) math:product ?tBB .
|
|
143
|
+
(?pC ?CB) math:product ?tCB .
|
|
144
|
+
(?tAB ?tBB) math:sum ?s2 .
|
|
145
|
+
(?s2 ?tCB) math:sum ?pi3B .
|
|
146
|
+
|
|
147
|
+
(?pA ?AC) math:product ?tAC .
|
|
148
|
+
(?pB ?BC) math:product ?tBC .
|
|
149
|
+
(?pC ?CC) math:product ?tCC .
|
|
150
|
+
(?tAC ?tBC) math:sum ?s3 .
|
|
151
|
+
(?s3 ?tCC) math:sum ?pi3C .
|
|
152
|
+
}
|
|
153
|
+
=>
|
|
154
|
+
{
|
|
155
|
+
:MC1 :pi3 [ :pA ?pi3A ; :pB ?pi3B ; :pC ?pi3C ] .
|
|
156
|
+
} .
|
|
157
|
+
|
|
158
|
+
############
|
|
159
|
+
# (3) MATRIX POWER WORKLOAD: A-row of P^2
|
|
160
|
+
############
|
|
161
|
+
|
|
162
|
+
{
|
|
163
|
+
:MC1 :row ?rA . ?rA :from :A ; :pA ?AA ; :pB ?AB ; :pC ?AC .
|
|
164
|
+
:MC1 :row ?rB . ?rB :from :B ; :pA ?BA ; :pB ?BB ; :pC ?BC .
|
|
165
|
+
:MC1 :row ?rC . ?rC :from :C ; :pA ?CA ; :pB ?CB ; :pC ?CC .
|
|
166
|
+
|
|
167
|
+
# (A,A)
|
|
168
|
+
(?AA ?AA) math:product ?AA_AA .
|
|
169
|
+
(?AB ?BA) math:product ?AB_BA .
|
|
170
|
+
(?AC ?CA) math:product ?AC_CA .
|
|
171
|
+
(?AA_AA ?AB_BA) math:sum ?sAA .
|
|
172
|
+
(?sAA ?AC_CA) math:sum ?P2AA .
|
|
173
|
+
|
|
174
|
+
# (A,B)
|
|
175
|
+
(?AA ?AB) math:product ?AA_AB .
|
|
176
|
+
(?AB ?BB) math:product ?AB_BB .
|
|
177
|
+
(?AC ?CB) math:product ?AC_CB .
|
|
178
|
+
(?AA_AB ?AB_BB) math:sum ?sAB .
|
|
179
|
+
(?sAB ?AC_CB) math:sum ?P2AB .
|
|
180
|
+
|
|
181
|
+
# (A,C)
|
|
182
|
+
(?AA ?AC) math:product ?AA_AC .
|
|
183
|
+
(?AB ?BC) math:product ?AB_BC .
|
|
184
|
+
(?AC ?CC) math:product ?AC_CC .
|
|
185
|
+
(?AA_AC ?AB_BC) math:sum ?sAC .
|
|
186
|
+
(?sAC ?AC_CC) math:sum ?P2AC .
|
|
187
|
+
}
|
|
188
|
+
=>
|
|
189
|
+
{
|
|
190
|
+
:MC1 :P2rowA [ :pA ?P2AA ; :pB ?P2AB ; :pC ?P2AC ] .
|
|
191
|
+
} .
|
|
192
|
+
|
|
193
|
+
############
|
|
194
|
+
# (4) METRICS NODES (ground, no existentials-in-disguise)
|
|
195
|
+
############
|
|
196
|
+
|
|
197
|
+
# Create metric “anchors” for each step
|
|
198
|
+
{ :MC1 :pi0 ?pi . } => { :MC1 :metrics [ :t 0 ; :pi ?pi ] . } .
|
|
199
|
+
{ :MC1 :pi1 ?pi . } => { :MC1 :metrics [ :t 1 ; :pi ?pi ] . } .
|
|
200
|
+
{ :MC1 :pi2 ?pi . } => { :MC1 :metrics [ :t 2 ; :pi ?pi ] . } .
|
|
201
|
+
{ :MC1 :pi3 ?pi . } => { :MC1 :metrics [ :t 3 ; :pi ?pi ] . } .
|
|
202
|
+
|
|
203
|
+
# Compute (gini, effN, TV, L2) for each metrics node
|
|
204
|
+
{
|
|
205
|
+
:MC1 :metrics ?m .
|
|
206
|
+
?m :pi ?pi .
|
|
207
|
+
?pi :pA ?a ; :pB ?b ; :pC ?c .
|
|
208
|
+
|
|
209
|
+
# uniform u = 1/3
|
|
210
|
+
(1.0 3.0) math:quotient ?u .
|
|
211
|
+
|
|
212
|
+
# sumSq = a^2 + b^2 + c^2
|
|
213
|
+
(?a 2.0) math:exponentiation ?a2 .
|
|
214
|
+
(?b 2.0) math:exponentiation ?b2 .
|
|
215
|
+
(?c 2.0) math:exponentiation ?c2 .
|
|
216
|
+
(?a2 ?b2) math:sum ?ab2 .
|
|
217
|
+
(?ab2 ?c2) math:sum ?sumSq .
|
|
218
|
+
|
|
219
|
+
# gini = 1 - sumSq
|
|
220
|
+
(1.0 ?sumSq) math:difference ?gini .
|
|
221
|
+
|
|
222
|
+
# effN = 1 / sumSq
|
|
223
|
+
(1.0 ?sumSq) math:quotient ?effN .
|
|
224
|
+
|
|
225
|
+
# TV = 0.5 * (|a-u| + |b-u| + |c-u|)
|
|
226
|
+
(?a ?u) math:difference ?da . ?da math:absoluteValue ?ada .
|
|
227
|
+
(?b ?u) math:difference ?db . ?db math:absoluteValue ?adb .
|
|
228
|
+
(?c ?u) math:difference ?dc . ?dc math:absoluteValue ?adc .
|
|
229
|
+
(?ada ?adb) math:sum ?s1 .
|
|
230
|
+
(?s1 ?adc) math:sum ?sAbs .
|
|
231
|
+
(0.5 ?sAbs) math:product ?tv .
|
|
232
|
+
|
|
233
|
+
# L2 = sqrt( (a-u)^2 + (b-u)^2 + (c-u)^2 )
|
|
234
|
+
(?da 2.0) math:exponentiation ?da2 .
|
|
235
|
+
(?db 2.0) math:exponentiation ?db2 .
|
|
236
|
+
(?dc 2.0) math:exponentiation ?dc2 .
|
|
237
|
+
(?da2 ?db2) math:sum ?s2 .
|
|
238
|
+
(?s2 ?dc2) math:sum ?s3 .
|
|
239
|
+
(?s3 0.5) math:exponentiation ?l2 .
|
|
240
|
+
}
|
|
241
|
+
=>
|
|
242
|
+
{
|
|
243
|
+
?m :sumSq ?sumSq ;
|
|
244
|
+
:gini ?gini ;
|
|
245
|
+
:effectiveStates ?effN ;
|
|
246
|
+
:tvToUniform ?tv ;
|
|
247
|
+
:l2ToUniform ?l2 .
|
|
248
|
+
} .
|
|
249
|
+
|
|
250
|
+
# Optional: declare convergence when TV < 0.01
|
|
251
|
+
{
|
|
252
|
+
:MC1 :metrics ?m .
|
|
253
|
+
?m :t ?t ; :tvToUniform ?tv .
|
|
254
|
+
?tv math:lessThan 0.01 .
|
|
255
|
+
}
|
|
256
|
+
=>
|
|
257
|
+
{
|
|
258
|
+
:MC1 :convergedBy ?t .
|
|
259
|
+
} .
|
|
260
|
+
|