-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLPSTerm.py
321 lines (261 loc) · 8.92 KB
/
LPSTerm.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
# --------------------------------------------------------------------------------------------------
# Neural Network Analysis Framework
#
# Copyright(c) Microsoft Corporation
# All rights reserved.
#
# MIT License
#
# Permission is hereby granted, free of charge, to any person obtaining a copy of this software and
# associated documentation files (the "Software"), to deal in the Software without restriction,
# including without limitation the rights to use, copy, modify, merge, publish, distribute,
# sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all copies or
# substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT
# NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
# DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
# --------------------------------------------------------------------------------------------------
from System.Collections.Generic import *
from System.Linq import *
from System.Text import *
from System.Threading.Tasks import *
from System.Threading import *
from System.Diagnostics import *
from MathNet.Numerics import *
from MathNet.Numerics.LinearAlgebra import *
from MathNet.Numerics.LinearAlgebra.Double import *
# Type-class trick, thanks to Claudio Russo ([email protected])
class Num(object):
def AddMul(self, tgt, src, d):
pass
# tgt += src*d;
def Add(self, tgt, src):
pass
# tgt += src;
def Add(self, tgt, d):
pass
# tgt += d;
def Mul(self, tgt, d):
pass
# tgt *= d;
def Const(self, d):
pass
def CreateVector(self, capacity):
pass
class NumInstDouble(Num):
def AddMul(self, tgt, src, d):
tgt += src * d
def Add(self, tgt, src):
tgt += src
def Mul(self, tgt, d):
tgt *= d
def Const(self, d):
return d
def CreateVector(self, capacity):
return SparseVector.Create(capacity, 0.0)
#public struct NumInstLPSTermVec : Num<LPSTerm,LPSTerm[]>
#{
# public void AddMul(ref LPSTerm tgt, LPSTerm src, double d) { tgt.AddMul(src, d); }
# public void Add(ref LPSTerm tgt, LPSTerm src) { tgt.append(src); }
# public void Add(ref LPSTerm tgt, double d) { tgt.append(d);}
# public void Mul(ref LPSTerm tgt, double d) { tgt.Mul(d); }
# public LPSTerm Const(double d) { return LPSTerm.Const(d); }
# public LPSTerm[] CreateVector(int capacity)
# {
# var coeffs = DenseMatrix.Create(capacity, LPSTerm.TotalVarCount(), 0.0);
# var interc = DenseVector.Create(capacity, 0.0);
# return new LPSTerm[](coeffs, interc);
# }
#}
class NumInstLPSTermArr(Num):
def AddMul(self, tgt, src, d):
tgt.AddMul(src, d)
def Add(self, tgt, src):
tgt.append(src)
def Add(self, tgt, d):
tgt.append(d)
def Mul(self, tgt, d):
tgt.Mul(d)
def Const(self, d):
return LPSTerm.Const(d)
def CreateVector(self, capacity):
vec = [None] * capacity
i = 0
while i < capacity:
vec[i] = LPSTerm.Const(0.0)
i += 1
return vec
class VCInfo(object): # = new ThreadLocal<Vector<double>>();
def __init__(self, total_varcount):
self._varcount_ = 0
self._total_varcount_ = 0
self._total_varcount_ = total_varcount
self._tempmultstorage = ThreadLocal[Vector]()
class LPSTerm(object):
""" <summary>
Representation of a linear term, like 0.3 x0 + 0.0 x1 + .... 4.3 xn + interecept_
We use a Dictionary of coefficients from variable positions (0 for x0, etc).
Keys without a corresponding entry are meant to have coefficient 0.0.
</summary>
"""
# Set to null to ensure someone does call InitVariableFactory() below first!
def IdentityMatrix(howmany):
#Matrix<double> coeffs = DenseMatrix.CreateIdentity(howmany);
#Vector<double> interc = DenseVector.Create(howmany, 0.0);
# return new LPSTerm[](coeffs, interc);
terms = [None] * howmany
pos = 0
i = 0
while i < howmany:
coeffs = SparseVector.Create(howmany, 0.0)
coeffs[pos += 1] = 1.0
terms[i] = LPSTerm(coeffs, 0.0)
i += 1
return terms
IdentityMatrix = staticmethod(IdentityMatrix)
def UnderlyingMatrix(terms):
# Stopwatch s = new Stopwatch();
# s.Start();
res = SparseMatrix.Create(terms.Length, LPSTerm.TotalVarCount(), 0.0)
i = 0
while i < terms.Length:
res.SetRow(i, terms[i].GetCoefficients())
i += 1
# s.Stop();
# print "To underlying matrix: {0} milliseconds",s.ElapsedMilliseconds;
return res
UnderlyingMatrix = staticmethod(UnderlyingMatrix)
def UnderlyingTransposeMatrix(terms):
res = DenseMatrix.Create(LPSTerm.TotalVarCount(), terms.Length, 0.0)
i = 0
while i < terms.Length:
res.SetColumn(i, terms[i].GetCoefficients())
i += 1
return res
UnderlyingTransposeMatrix = staticmethod(UnderlyingTransposeMatrix)
def UnderlyingIntercept(terms):
intercept = DenseVector.Create(terms.Length, 0.0)
i = 0
while i < terms.Length:
intercept[i] = terms[i].Intercept
i += 1
return intercept
UnderlyingIntercept = staticmethod(UnderlyingIntercept)
def FromUnderlyingAlgebra(outm, outv):
ret = Array.CreateInstance(LPSTerm, outm.RowCount)
i = 0
while i < outm.RowCount:
ret[i] = LPSTerm(outm.Row(i), outv[i])
i += 1
return ret
FromUnderlyingAlgebra = staticmethod(FromUnderlyingAlgebra)
def FromUnderlyingTransposeAlgebra(outm, outv):
ret = Array.CreateInstance(LPSTerm, outm.ColumnCount)
i = 0
while i < outm.ColumnCount:
ret[i] = LPSTerm(outm.Column(i), outv[i])
i += 1
return ret
FromUnderlyingTransposeAlgebra = staticmethod(FromUnderlyingTransposeAlgebra)
def FreshVariables(howmany):
tmp = [None] * howmany
i = 0
while i < howmany:
tmp[i] = LPSTerm.FreshVariable()
i += 1
return tmp
FreshVariables = staticmethod(FreshVariables)
def FreshVariable():
tmp = LPSTerm()
tmp.coefficients_[self._vcinfo_.varcount_] = 1.0
tmp.intercept_ = 0.0
self._vcinfo_.varcount_ += 1
return tmp
FreshVariable = staticmethod(FreshVariable)
def GetVariableFactoryState():
return self._vcinfo_
GetVariableFactoryState = staticmethod(GetVariableFactoryState)
def ResetVariableFactory(total_variables):
self._vcinfo_ = VCInfo(total_variables)
self._vcinfo_.varcount_ = 0
self._vcinfo_.total_varcount_ = total_variables
ResetVariableFactory = staticmethod(ResetVariableFactory)
def RestoreVariableFactory(info):
self._vcinfo_ = info
RestoreVariableFactory = staticmethod(RestoreVariableFactory)
def TotalVarCount():
return self._vcinfo_.total_varcount_
TotalVarCount = staticmethod(TotalVarCount)
def __init__(self):
self._addmulcounter = 0
self._vcinfo_ = None
self._coefficients_ = SparseVector.Create(self._vcinfo_.total_varcount_, 0.0)
self._intercept_ = 0.0
def __init__(self):
self._addmulcounter = 0
self._vcinfo_ = None
self._coefficients_ = SparseVector.Create(self._vcinfo_.total_varcount_, 0.0)
self._intercept_ = 0.0
def get_VarCount(self):
return self._vcinfo_.varcount_
VarCount = property(fget=get_VarCount)
def Clear(self):
self._coefficients_.Clear()
def Densify(self):
self._coefficients_ = DenseVector.OfVector(self._coefficients_)
def Sparsify(self):
self._coefficients_ = SparseVector.OfVector(self._coefficients_)
def GetCoefficients(self):
return self._coefficients_
def GetCoefficient(self, i):
return self._coefficients_[i]
def SetCoefficient(self, i, d):
self._coefficients_[i] = d
def get_Intercept(self):
return self._intercept_
def set_Intercept(self, value):
self._intercept_ = value
Intercept = property(fget=get_Intercept, fset=set_Intercept)
def ToString(self):
ret = ""
i = 0
while i < self._vcinfo_.total_varcount_:
ret += self.GetCoefficient(i) + "*X" + i + " + "
i += 1
ret += self.Intercept
return ret
# this += v
def Add(self, v):
self._coefficients_.append(v.coefficients_, self._coefficients_)
self._intercept_ += v.intercept_
def Sub(self, v):
self._coefficients_ -= v.coefficients_
self._intercept_ -= v.intercept_
def Add(self, d):
self._intercept_ += d
# this += d*v
def AddMul(self, v, d):
v.coefficients_.Multiply(d, self._vcinfo_.tempmultstorage.Value)
self._coefficients_.append(self._vcinfo_.tempmultstorage.Value, self._coefficients_)
self._intercept_ += d * v.intercept_
self._addmulcounter += 1
def AddMulVec(self, v_coeffm, v_intcps, d_vec): # this += v1*d1 + .... vn*dn
#Matrix<double> v_coeffm = LPSTerm.UnderlyingMatrix(v);
#Vector<double> v_intcps = LPSTerm.UnderlyingIntercept(v);
mul = d_vec * v_coeffm
self._coefficients_.append(mul, self._coefficients_)
self._intercept_ += v_intcps * d_vec
def Const(d):
v = LPSTerm()
v.intercept_ = d
return v
Const = staticmethod(Const)
def Mul(self, d):
self._coefficients_ *= d
self._intercept_ *= d