pytmpinv 1.0.2__tar.gz → 1.0.3__tar.gz

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.

Potentially problematic release.


This version of pytmpinv might be problematic. Click here for more details.

@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: pytmpinv
3
- Version: 1.0.2
3
+ Version: 1.0.3
4
4
  Summary: Tabular Matrix Problems via Pseudoinverse Estimation
5
5
  Author-email: The Economist <29724411+econcz@users.noreply.github.com>
6
6
  License-Expression: MIT
@@ -1,6 +1,6 @@
1
1
  [project]
2
2
  name = "pytmpinv"
3
- version = "1.0.2"
3
+ version = "1.0.3"
4
4
  description = "Tabular Matrix Problems via Pseudoinverse Estimation"
5
5
  readme = "README.md"
6
6
  requires-python = ">=3.10"
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: pytmpinv
3
- Version: 1.0.2
3
+ Version: 1.0.3
4
4
  Summary: Tabular Matrix Problems via Pseudoinverse Estimation
5
5
  Author-email: The Economist <29724411+econcz@users.noreply.github.com>
6
6
  License-Expression: MIT
@@ -1,4 +1,4 @@
1
- __version__ = "1.0.2"
1
+ __version__ = "1.0.3"
2
2
 
3
3
  from .tmpinv import tmpinv
4
4
 
@@ -1,3 +1,4 @@
1
+ import warnings
1
2
  import numpy as np
2
3
  from math import ceil
3
4
  from typing import Sequence
@@ -151,12 +152,16 @@ def TMPinvSolve(
151
152
  np.abs(S).sum(axis=1).max() > 1):
152
153
  raise TMPinvInputError("S must be a zero-padded subset of ±I.")
153
154
  if M is not None:
155
+ if b_val is None:
156
+ raise TMPinvInputError("Both M and b_val must be defined.")
154
157
  M = np.asarray(M, dtype=np.float64)
155
158
  if M.ndim == 1:
156
159
  M = M.reshape(1, -1)
157
160
  if M.shape[1] != m * p:
158
161
  raise TMPinvInputError(f"M must have exactly {m * p} columns.")
159
162
  if b_val is not None:
163
+ if M is None:
164
+ raise TMPinvInputError("Both M and b_val must be defined.")
160
165
  if not np.isfinite(b_val).all():
161
166
  raise TMPinvInputError("b_val must not contain inf or NaN.")
162
167
  b_val = np.asarray(b_val, dtype=np.float64).reshape(-1, 1)
@@ -165,6 +170,10 @@ def TMPinvSolve(
165
170
  raise TMPinvInputError(f"M and b_val must have the same number "
166
171
  f"of rows: "
167
172
  f"{M.shape[0]} vs {b_val.shape[0]}")
173
+ kw = {k: v for k, v in kwargs.items() if k not in {"b", "S", "M",
174
+ "m", "p",
175
+ "i", "j",
176
+ "zero_diagonal"}}
168
177
 
169
178
  # perform full estimation and return the result
170
179
  if reduced is None:
@@ -173,13 +182,14 @@ def TMPinvSolve(
173
182
  if b_val is not None:
174
183
  b_blocks.append(b_val)
175
184
  b = np.vstack(b_blocks)
176
- result.model = CLSP().solve(problem='ap', b=b, S=S, M=M,
185
+ result.model = CLSP().solve(*args,
186
+ problem='ap', b=b, S=S, M=M,
177
187
  m=m,
178
188
  p=p,
179
189
  i=i,
180
190
  j=j,
181
191
  zero_diagonal=zero_diagonal,
182
- *args, **kwargs)
192
+ **kw)
183
193
  result.x = result.model.x
184
194
 
185
195
  # perform reduced estimation and return the result
@@ -191,12 +201,27 @@ def TMPinvSolve(
191
201
  result = TMPinvResult(False, [], np.empty((m, p), dtype=float))
192
202
  m_subset = reduced[0] - 1
193
203
  p_subset = reduced[1] - 1
204
+ if zero_diagonal: # not processed by CLSP
205
+ M_diag = np.zeros((min(m, p), m * p))
206
+ b_diag = np.zeros((min(m, p), 1))
207
+ for k in range(min(m, p)):
208
+ M_diag[k, k * p + k] = 1
209
+ M, idx = np.unique(M_diag.copy() if M is None or M.size == 0
210
+ else np.vstack([M, M_diag]),
211
+ axis=0, return_index=True)
212
+ b_val = (b_diag.copy() if b_val is None or b_val.size == 0
213
+ else np.vstack([b_val, b_diag])
214
+ )[idx.reshape(-1)]
215
+ del M_diag, b_diag, idx
194
216
  if M is not None:
195
217
  if not all(np.count_nonzero(row) == 1 and
196
218
  np.all((row == 0) | (row == 1)) for row in M):
197
219
  raise TMPinvInputError("M must be a row subset of the identity "
198
220
  "matrix in the reduced model.")
199
221
  M = (np.abs(np.sign(M)) * b_val).sum(axis=0).reshape(m, p)
222
+ if S is not None:
223
+ warnings.warn("User-provided S is ignored in the reduced model.",
224
+ UserWarning)
200
225
  for row_block in range(ceil(m / m_subset)):
201
226
  for col_block in range(ceil(p / p_subset)):
202
227
  m_start = row_block * m_subset
@@ -220,13 +245,14 @@ def TMPinvSolve(
220
245
  else:
221
246
  M_subset, b_val_subset = None, None
222
247
  b = np.vstack(b_blocks)
223
- tmp = CLSP().solve(problem='ap', b=b, S=S, M=M_subset,
248
+ tmp = CLSP().solve(*args,
249
+ problem='ap', b=b, S=S, M=M_subset,
224
250
  m=m_end - m_start,
225
251
  p=p_end - p_start,
226
252
  i=i,
227
253
  j=j,
228
254
  zero_diagonal=False,
229
- *args, **kwargs)
255
+ **kw)
230
256
  result.model.append(tmp)
231
257
  result.x[m_start:m_end, p_start:p_end] = tmp.x
232
258
 
@@ -285,9 +311,9 @@ def tmpinv(
285
311
  (h is not None and h < 0) for l, h in bounds):
286
312
  raise TMPinvInputError("Negative lower or upper bounds are not "
287
313
  "allowed in tabular matrix programs.")
314
+ kw = {k: v for k, v in kwargs.items() if k not in {"M", "b_val"}}
288
315
 
289
316
  # (result) Perform bound-constrained iterative refinement
290
- kw = {k: v for k, v in kwargs.items() if k not in {"M", "b_val"}}
291
317
  for _ in range(iteration_limit):
292
318
  M_idx, b_val = [], []
293
319
  x = result.x.reshape(-1, 1)
@@ -300,7 +326,7 @@ def tmpinv(
300
326
  b_val.append(v)
301
327
  if len(M_idx) < n_cells:
302
328
  M = np.eye(n_cells, dtype=np.float64)[M_idx]
303
- result = TMPinvSolve(M=M, b_val=b_val, *args, **kw)
329
+ result = TMPinvSolve(*args, M=M, b_val=b_val, **kw)
304
330
  else:
305
331
  break
306
332
 
File without changes
File without changes
File without changes