pytmpinv 1.0.3__tar.gz → 1.0.4__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.3
3
+ Version: 1.0.4
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
@@ -114,6 +114,10 @@ If *True*, enforces the zero diagonal.
114
114
  `reduced` : *tuple* of *(int, int)*, optional
115
115
  Dimensions of the reduced problem. If specified, the problem is estimated as a set of reduced problems constructed from contiguous submatrices of the original table. For example, `reduced` = *(6, 6)* implies *5×5* data blocks with *1* slack row and *1* slack column each (edge blocks may be smaller).
116
116
 
117
+ `symmetric` : *bool*, default = *False*
118
+ If True, enforces symmetry of the estimated solution matrix as: x = 0.5 * (x + x.T)
119
+ Symmetrization can slightly alter row/column totals. For exact symmetry under all constraints, add explicit symmetry constraints to M in a full-model solve instead of using this flag.
120
+
117
121
  `bounds` : *sequence* of *(low, high)*, optional
118
122
  Bounds on cell values. If a single tuple *(low, high)* is given, it is applied to all `m` * `p` cells. Example: *(0, None)*.
119
123
 
@@ -91,6 +91,10 @@ If *True*, enforces the zero diagonal.
91
91
  `reduced` : *tuple* of *(int, int)*, optional
92
92
  Dimensions of the reduced problem. If specified, the problem is estimated as a set of reduced problems constructed from contiguous submatrices of the original table. For example, `reduced` = *(6, 6)* implies *5×5* data blocks with *1* slack row and *1* slack column each (edge blocks may be smaller).
93
93
 
94
+ `symmetric` : *bool*, default = *False*
95
+ If True, enforces symmetry of the estimated solution matrix as: x = 0.5 * (x + x.T)
96
+ Symmetrization can slightly alter row/column totals. For exact symmetry under all constraints, add explicit symmetry constraints to M in a full-model solve instead of using this flag.
97
+
94
98
  `bounds` : *sequence* of *(low, high)*, optional
95
99
  Bounds on cell values. If a single tuple *(low, high)* is given, it is applied to all `m` * `p` cells. Example: *(0, None)*.
96
100
 
@@ -1,6 +1,6 @@
1
1
  [project]
2
2
  name = "pytmpinv"
3
- version = "1.0.3"
3
+ version = "1.0.4"
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.3
3
+ Version: 1.0.4
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
@@ -114,6 +114,10 @@ If *True*, enforces the zero diagonal.
114
114
  `reduced` : *tuple* of *(int, int)*, optional
115
115
  Dimensions of the reduced problem. If specified, the problem is estimated as a set of reduced problems constructed from contiguous submatrices of the original table. For example, `reduced` = *(6, 6)* implies *5×5* data blocks with *1* slack row and *1* slack column each (edge blocks may be smaller).
116
116
 
117
+ `symmetric` : *bool*, default = *False*
118
+ If True, enforces symmetry of the estimated solution matrix as: x = 0.5 * (x + x.T)
119
+ Symmetrization can slightly alter row/column totals. For exact symmetry under all constraints, add explicit symmetry constraints to M in a full-model solve instead of using this flag.
120
+
117
121
  `bounds` : *sequence* of *(low, high)*, optional
118
122
  Bounds on cell values. If a single tuple *(low, high)* is given, it is applied to all `m` * `p` cells. Example: *(0, None)*.
119
123
 
@@ -1,4 +1,4 @@
1
- __version__ = "1.0.3"
1
+ __version__ = "1.0.4"
2
2
 
3
3
  from .tmpinv import tmpinv
4
4
 
@@ -82,6 +82,7 @@ def TMPinvSolve(
82
82
  j: int = 1,
83
83
  zero_diagonal: bool = False,
84
84
  reduced: tuple[int, int] | None = None,
85
+ symmetric: bool = False,
85
86
  *args, **kwargs
86
87
  ) -> TMPinvResult:
87
88
  """
@@ -120,6 +121,12 @@ def TMPinvSolve(
120
121
  constructed from contiguous submatrices of the original table.
121
122
  For example, reduced = (6, 6) implies 5×5 data blocks with 1 slack
122
123
  row and 1 slack column each (edge blocks may be smaller).
124
+ symmetric : bool, default = False
125
+ If True, enforces symmetry of the estimated solution matrix as:
126
+ x = 0.5 * (x + x.T)
127
+ Symmetrization can slightly alter row/column totals. For exact symmetry
128
+ under all constraints, add explicit symmetry constraints to M in a
129
+ full-model solve instead of using this flag.
123
130
 
124
131
  Returns
125
132
  -------
@@ -147,6 +154,8 @@ def TMPinvSolve(
147
154
  p = b_col.shape[0]
148
155
  if S is not None:
149
156
  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.")
150
159
  if (not np.all((S == -1) | (S == 0) | (S == 1)) or
151
160
  np.abs(S).sum(axis=0).max() > 1 or
152
161
  np.abs(S).sum(axis=1).max() > 1):
@@ -173,7 +182,8 @@ def TMPinvSolve(
173
182
  kw = {k: v for k, v in kwargs.items() if k not in {"b", "S", "M",
174
183
  "m", "p",
175
184
  "i", "j",
176
- "zero_diagonal"}}
185
+ "zero_diagonal",
186
+ "symmetric"}}
177
187
 
178
188
  # perform full estimation and return the result
179
189
  if reduced is None:
@@ -256,6 +266,13 @@ def TMPinvSolve(
256
266
  result.model.append(tmp)
257
267
  result.x[m_start:m_end, p_start:p_end] = tmp.x
258
268
 
269
+ # enforce symmetry
270
+ if symmetric:
271
+ if result.x.shape[0] != result.x.shape[1]:
272
+ raise TMPinvInputError("symmetric=True requires a square matrix "
273
+ "(m == p).")
274
+ result.x = float(0.5) * (result.x + result.x.T)
275
+
259
276
  return result
260
277
 
261
278
  def tmpinv(
@@ -339,4 +356,9 @@ def tmpinv(
339
356
  x[(x < x_lb - tolerance) | (x > x_ub + tolerance)] = replace_value
340
357
  result.x = x.reshape(result.x.shape)
341
358
 
359
+ # enforce (final) symmetry
360
+ if kwargs.get("symmetric", False):
361
+ if result.x.shape[0] == result.x.shape[1]:
362
+ result.x = float(0.5) * (result.x + result.x.T)
363
+
342
364
  return result
File without changes
File without changes