pytmpinv 1.1.3__tar.gz → 1.2.1__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.1.3
3
+ Version: 1.2.1
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.1.3"
3
+ version = "1.2.1"
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.1.3
3
+ Version: 1.2.1
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.1.3"
1
+ __version__ = "1.2.1"
2
2
 
3
3
  from .tmpinv import tmpinv
4
4
 
@@ -77,6 +77,7 @@ def TMPinvSolve(
77
77
  M: Sequence[Sequence[float]] | None = None,
78
78
  b_row: Sequence[float] | None = None,
79
79
  b_col: Sequence[float] | None = None,
80
+ b_lim: Sequence[float] | None = None,
80
81
  b_val: Sequence[float] | None = None,
81
82
  i: int = 1,
82
83
  j: int = 1,
@@ -140,6 +141,9 @@ def TMPinvSolve(
140
141
  from block-wise marginal totals. Likewise, `M` must be a unique row subset
141
142
  of an identity matrix (i.e., diagonal-only). Arbitrary or non-diagonal model
142
143
  matrices cannot be mapped to reduced blocks, making the model infeasible.
144
+ This function may also receive internally generated keyword arguments
145
+ (`b_lim`, `C_lim`) from `tmpinv()`. These arguments are ignored in the
146
+ reduced model given its replacement of `S`.
143
147
  """
144
148
  # (m), (p) Process the parameters, assert conformity, and get dimensions
145
149
  if b_row is None or b_col is None:
@@ -150,12 +154,12 @@ def TMPinvSolve(
150
154
  raise TMPinvInputError("b_row and b_col must not contain inf or NaN.")
151
155
  b_row = np.asarray(b_row, dtype=np.float64).reshape(-1, 1)
152
156
  b_col = np.asarray(b_col, dtype=np.float64).reshape(-1, 1)
153
- m = b_row.shape[0]
154
- p = b_col.shape[0]
157
+ m = b_row.shape[0] * i
158
+ p = b_col.shape[0] * j
155
159
  if S is not None:
156
160
  S = np.asarray(S, dtype=np.float64)
157
- if S.shape[0] != m + p:
158
- raise TMPinvInputError(f"S must have exactly {m + p} rows.")
161
+ if S.shape != (m + p, m + p):
162
+ raise TMPinvInputError(f"S must have shape {(m+p, m+p)}.")
159
163
  if (not np.all((S == -1) | (S == 0) | (S == 1)) or
160
164
  np.abs(S).sum(axis=0).max() > 1 or
161
165
  np.abs(S).sum(axis=1).max() > 1):
@@ -179,7 +183,8 @@ def TMPinvSolve(
179
183
  raise TMPinvInputError(f"M and b_val must have the same number "
180
184
  f"of rows: "
181
185
  f"{M.shape[0]} vs {b_val.shape[0]}")
182
- kw = {k: v for k, v in kwargs.items() if k not in {"b", "S", "M",
186
+ kw = {k: v for k, v in kwargs.items() if k not in {"b_lim", "b",
187
+ "C_lim", "S", "M",
183
188
  "m", "p",
184
189
  "i", "j",
185
190
  "zero_diagonal",
@@ -189,11 +194,15 @@ def TMPinvSolve(
189
194
  if reduced is None:
190
195
  result = TMPinvResult(True, None, np.empty((m, p), dtype=float))
191
196
  b_blocks = [b_row, b_col]
197
+ if kwargs.get("b_lim") is not None:
198
+ b_blocks.append(np.asarray(kwargs["b_lim"],
199
+ dtype=np.float64).reshape(-1, 1))
192
200
  if b_val is not None:
193
201
  b_blocks.append(b_val)
194
202
  b = np.vstack(b_blocks)
195
203
  result.model = CLSP().solve(*args,
196
204
  problem='ap', b=b,
205
+ C=kwargs.get("C_lim"),
197
206
  S=S,
198
207
  M=M,
199
208
  m=m,
@@ -328,13 +337,26 @@ def tmpinv(
328
337
  bounds = bounds * n_cells # replicate (low, high)
329
338
  if all(l is None and h is None for l, h in bounds):
330
339
  return result # finish if unbounded
331
- if any((l is not None and l < 0) or
332
- (h is not None and h < 0) for l, h in bounds):
333
- raise TMPinvInputError("Negative lower or upper bounds are not "
334
- "allowed in tabular matrix programs.")
335
- kw = {k: v for k, v in kwargs.items() if k not in {"M", "b_val"}}
336
340
 
337
341
  # (result) Perform bound-constrained iterative refinement
342
+ kw = {k: v for k, v in kwargs.items() if k not in {"b_lim", "b_val",
343
+ "C_lim", "S", "M"}}
344
+ b_lim = np.vstack([np.array([h if h is not None else np.inf for l, h
345
+ in bounds]).reshape(-1, 1),
346
+ np.array([l if l is not None else -np.inf for l, h
347
+ in bounds]).reshape(-1, 1)])
348
+ C_lim = np.vstack([np.tile(np.eye(n_cells), (2,1))])
349
+ S = (kwargs.get("S") if kwargs.get("S") is not None
350
+ else np.zeros((len(kwargs["b_row"]) + len(kwargs["b_col"]), 0)))
351
+ S = np.vstack([np.hstack([S, np.zeros((S.shape[0], 2 * n_cells))]),
352
+ np.hstack([np.zeros((n_cells, S.shape[1])),
353
+ np.eye(n_cells), np.zeros((n_cells,
354
+ n_cells))]),
355
+ -np.hstack([np.zeros((n_cells, S.shape[1] + n_cells)),
356
+ np.eye(n_cells)])])
357
+ finite_rows = np.isfinite(b_lim[:, 0]) # drop rows with ±np.inf
358
+ nonzero_cols = ~np.all(S[finite_rows, :] == 0,
359
+ axis=0) # reduce S width
338
360
  for _ in range(iteration_limit):
339
361
  M_idx, b_val = [], []
340
362
  x = result.x.reshape(-1, 1)
@@ -347,7 +369,11 @@ def tmpinv(
347
369
  b_val.append(v)
348
370
  if len(M_idx) < n_cells:
349
371
  M = np.eye(n_cells, dtype=np.float64)[M_idx]
350
- result = TMPinvSolve(*args, M=M, b_val=b_val, **kw)
372
+ result = TMPinvSolve(*args, C_lim=(C_lim[finite_rows, :]),
373
+ S=(S[finite_rows, :])[:,nonzero_cols],
374
+ M=M,
375
+ b_lim=(b_lim[finite_rows, :]),
376
+ b_val=b_val, **kw)
351
377
  else:
352
378
  break
353
379
 
File without changes
File without changes
File without changes