pytmpinv 1.1.1__tar.gz → 1.1.2__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.1
3
+ Version: 1.1.2
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
@@ -186,7 +186,7 @@ A diagonal sign slack (surplus) matrix with entries in *{0, ±1}*.
186
186
  The first `m` diagonal entries correspond to row constraints, and the remaining `p` to column constraints. Please note that, in the reduced model, `S` is ignored: slack behavior is derived implicitly from block-wise marginal totals.
187
187
 
188
188
  `M` : *array_like* of shape *(k, m * p)*, optional
189
- A model matrix with entries in *{0, 1}*. Each row defines a linear restriction on the flattened solution matrix. The corresponding right-hand side values must be provided in `b_val`. This block is used to encode known cell values. Please note that, in the reduced model, `M` must be a row subset of an identity matrix (i.e., diagonal-only). Arbitrary or non-diagonal model matrices cannot be mapped to reduced blocks, making the model infeasible.
189
+ A model matrix with entries in *{0, 1}*. Each row defines a linear restriction on the flattened solution matrix. The corresponding right-hand side values must be provided in `b_val`. This block is used to encode known cell values. Please note that, in the reduced model, `M` must be a unique row subset of an identity matrix (i.e., diagonal-only). Arbitrary or non-diagonal model matrices cannot be mapped to reduced blocks, making the model infeasible.
190
190
 
191
191
  `b_row` : *array_like* of shape *(m,)*
192
192
  Right-hand side vector of row totals. Please note that both `b_row` and `b_col` must be provided.
@@ -163,7 +163,7 @@ A diagonal sign slack (surplus) matrix with entries in *{0, ±1}*.
163
163
  The first `m` diagonal entries correspond to row constraints, and the remaining `p` to column constraints. Please note that, in the reduced model, `S` is ignored: slack behavior is derived implicitly from block-wise marginal totals.
164
164
 
165
165
  `M` : *array_like* of shape *(k, m * p)*, optional
166
- A model matrix with entries in *{0, 1}*. Each row defines a linear restriction on the flattened solution matrix. The corresponding right-hand side values must be provided in `b_val`. This block is used to encode known cell values. Please note that, in the reduced model, `M` must be a row subset of an identity matrix (i.e., diagonal-only). Arbitrary or non-diagonal model matrices cannot be mapped to reduced blocks, making the model infeasible.
166
+ A model matrix with entries in *{0, 1}*. Each row defines a linear restriction on the flattened solution matrix. The corresponding right-hand side values must be provided in `b_val`. This block is used to encode known cell values. Please note that, in the reduced model, `M` must be a unique row subset of an identity matrix (i.e., diagonal-only). Arbitrary or non-diagonal model matrices cannot be mapped to reduced blocks, making the model infeasible.
167
167
 
168
168
  `b_row` : *array_like* of shape *(m,)*
169
169
  Right-hand side vector of row totals. Please note that both `b_row` and `b_col` must be provided.
@@ -1,6 +1,6 @@
1
1
  [project]
2
2
  name = "pytmpinv"
3
- version = "1.1.1"
3
+ version = "1.1.2"
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.1
3
+ Version: 1.1.2
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
@@ -186,7 +186,7 @@ A diagonal sign slack (surplus) matrix with entries in *{0, ±1}*.
186
186
  The first `m` diagonal entries correspond to row constraints, and the remaining `p` to column constraints. Please note that, in the reduced model, `S` is ignored: slack behavior is derived implicitly from block-wise marginal totals.
187
187
 
188
188
  `M` : *array_like* of shape *(k, m * p)*, optional
189
- A model matrix with entries in *{0, 1}*. Each row defines a linear restriction on the flattened solution matrix. The corresponding right-hand side values must be provided in `b_val`. This block is used to encode known cell values. Please note that, in the reduced model, `M` must be a row subset of an identity matrix (i.e., diagonal-only). Arbitrary or non-diagonal model matrices cannot be mapped to reduced blocks, making the model infeasible.
189
+ A model matrix with entries in *{0, 1}*. Each row defines a linear restriction on the flattened solution matrix. The corresponding right-hand side values must be provided in `b_val`. This block is used to encode known cell values. Please note that, in the reduced model, `M` must be a unique row subset of an identity matrix (i.e., diagonal-only). Arbitrary or non-diagonal model matrices cannot be mapped to reduced blocks, making the model infeasible.
190
190
 
191
191
  `b_row` : *array_like* of shape *(m,)*
192
192
  Right-hand side vector of row totals. Please note that both `b_row` and `b_col` must be provided.
@@ -1,4 +1,4 @@
1
- __version__ = "1.1.1"
1
+ __version__ = "1.1.2"
2
2
 
3
3
  from .tmpinv import tmpinv
4
4
 
@@ -137,8 +137,8 @@ def TMPinvSolve(
137
137
  Notes
138
138
  ----
139
139
  In the reduced model, `S` is ignored. Slack behavior is derived implicitly
140
- from block-wise marginal totals. Likewise, `M` must be a row subset of an
141
- identity matrix (i.e., diagonal-only). Arbitrary or non-diagonal model
140
+ from block-wise marginal totals. Likewise, `M` must be a unique row subset
141
+ of an identity matrix (i.e., diagonal-only). Arbitrary or non-diagonal model
142
142
  matrices cannot be mapped to reduced blocks, making the model infeasible.
143
143
  """
144
144
  # (m), (p) Process the parameters, assert conformity, and get dimensions
@@ -193,12 +193,14 @@ def TMPinvSolve(
193
193
  b_blocks.append(b_val)
194
194
  b = np.vstack(b_blocks)
195
195
  result.model = CLSP().solve(*args,
196
- problem='ap', b=b, S=S, M=M,
197
- m=m,
198
- p=p,
199
- i=i,
200
- j=j,
201
- zero_diagonal=zero_diagonal,
196
+ problem='ap', b=b,
197
+ S=S,
198
+ M=M,
199
+ m=m,
200
+ p=p,
201
+ i=i,
202
+ j=j,
203
+ zero_diagonal=zero_diagonal,
202
204
  **kw)
203
205
  result.x = result.model.x
204
206
 
@@ -213,22 +215,28 @@ def TMPinvSolve(
213
215
  p_subset = reduced[1] - 1
214
216
  if zero_diagonal: # not processed by CLSP
215
217
  M_diag = np.zeros((min(m, p), m * p))
216
- b_diag = np.zeros((min(m, p), 0))
218
+ b_diag = np.zeros((min(m, p), 1))
217
219
  for k in range(min(m, p)):
218
220
  M_diag[k, k * p + k] = 1
219
221
  M, idx = np.unique(M_diag.copy() if M is None or M.size == 0
220
222
  else np.vstack([M, M_diag]),
221
223
  axis=0, return_index=True)
222
- b_val = (b_diag.copy() if b_val is None or b_val.size == 0
224
+ b_val = (b_diag.copy() if b_val is None or b_val.size == 0
223
225
  else np.vstack([b_val, b_diag])
224
- )[idx.reshape(-1)]
226
+ )[idx.reshape(-1)]
225
227
  del M_diag, b_diag, idx
228
+
226
229
  if M is not None:
227
- if not all(np.count_nonzero(row) == 1 and
228
- np.all((row == 0) | (row == 1)) for row in M):
229
- raise TMPinvInputError("M must be a row subset of the identity "
230
- "matrix in the reduced model.")
231
- M = (np.abs(np.sign(M)) * b_val).sum(axis=0).reshape(m, p)
230
+ if not ((np.isclose(M, 0) | np.isclose(M, 1)).all() and
231
+ (np.isclose(M, 1).sum(axis=1) == 1).all() and
232
+ (np.isclose(M, 1).sum(axis=0) <= 1).all()):
233
+ raise TMPinvInputError("M must be a unique row subset of the "
234
+ "identity matrix in the reduced model.")
235
+ X_true = np.full((m, p), np.nan, dtype=np.float64)
236
+ for idx, row in enumerate(M):
237
+ col = np.argmax(row)
238
+ r, c = divmod(col, p) # M has m * p columns
239
+ X_true[r, c] = b_val.ravel()[idx]
232
240
  if S is not None:
233
241
  warnings.warn("User-provided S is ignored in the reduced model.",
234
242
  UserWarning)
@@ -238,30 +246,26 @@ def TMPinvSolve(
238
246
  m_end = min(m_start + m_subset, m)
239
247
  p_start = col_block * p_subset
240
248
  p_end = min(p_start + p_subset, p)
241
- S = np.ones(((m_end - m_start) + (p_end - p_start), 1))
242
- b_blocks = [b_row[m_start:m_end].reshape(-1, 1),
249
+ S_subset = np.ones(((m_end - m_start) + (p_end - p_start), 1))
250
+ b_subset = [b_row[m_start:m_end].reshape(-1, 1),
243
251
  b_col[p_start:p_end].reshape(-1, 1)]
244
- M_subset, b_val_subset = None, None
245
- if M is not None:
246
- M_subset = np.diag(np.sign(M[m_start:m_end,
247
- p_start:p_end]).flatten())
248
- b_val_subset = M[m_start:m_end,
249
- p_start:p_end].reshape(-1, 1)
250
- if np.any(M_subset): # drop zero rows
251
- nonzero_rows = np.diag(M_subset) != 0
252
- M_subset = M_subset[nonzero_rows]
253
- b_val_subset = b_val_subset[nonzero_rows].reshape(-1, 1)
254
- b_blocks.append(b_val_subset)
255
- else:
256
- M_subset, b_val_subset = None, None
257
- b = np.vstack(b_blocks)
252
+ M_subset = None
253
+ if M is not None:
254
+ subset = X_true[m_start:m_end, p_start:p_end].ravel()
255
+ non_empty = ~np.isnan(subset)
256
+ if non_empty.any():
257
+ M_subset = np.eye(subset.size,
258
+ dtype=np.float64)[non_empty]
259
+ b_subset.append(subset[non_empty].reshape(-1, 1))
258
260
  tmp = CLSP().solve(*args,
259
- problem='ap', b=b, S=S, M=M_subset,
260
- m=m_end - m_start,
261
- p=p_end - p_start,
262
- i=i,
263
- j=j,
264
- zero_diagonal=False,
261
+ problem='ap', b=np.vstack(b_subset),
262
+ S=S_subset,
263
+ M=M_subset,
264
+ m=m_end - m_start,
265
+ p=p_end - p_start,
266
+ i=i,
267
+ j=j,
268
+ zero_diagonal=False,
265
269
  **kw)
266
270
  result.model.append(tmp)
267
271
  result.x[m_start:m_end, p_start:p_end] = tmp.x
File without changes
File without changes