fxtensor-salmon 0.2.1__tar.gz → 0.2.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.
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: fxtensor_salmon
3
- Version: 0.2.1
3
+ Version: 0.2.2
4
4
  Summary: Markov Tensor Module
5
5
  Author-email: Tadachika Oki <sakesnare@gmail.com>
6
6
  License: Copyright 2025 Tadachika Oki
@@ -128,7 +128,7 @@ tensor2 = FXTensor(
128
128
 
129
129
  # Tensor product: P(X,Y) = P(X) ⊗ P(Y)
130
130
  result = tensor1.tensor_product(tensor2)
131
- assert result.labels == (None, [['a', 'b'], ['x', 'y', 'z']])
131
+ assert result.labels == ([], [['a', 'b'], ['x', 'y', 'z']])
132
132
  ```
133
133
 
134
134
  ## Simple Example: Weather Forecast (Labeled)
@@ -178,38 +178,99 @@ Model the joint probability of **Season** (Spring, Summer, Other) and **Weather*
178
178
 
179
179
  ### Key Method Applications
180
180
 
181
- #### `marginalization(start_B)`
181
+ #### `from_json(json_data)`
182
+
183
+ Creates a tensor from JSON data, loading profile and data to instantiate an FXTensor.
182
184
 
183
185
  ```python
184
- # Get P(Season | Location) by marginalizing Weather
185
- season_tensor = process_tensor.marginalization(start_B=2)
186
- assert season_tensor.labels == ([['Urban', 'Rural']], [['Spring', 'Summer', 'Other']])
186
+ json_data = {
187
+ "profile": [[['a', 'b']], [['x', 'y']]],
188
+ "data": [[0.2, 0.8], [0.6, 0.4]]
189
+ }
190
+ tensor = FXTensor.from_json(json_data)
191
+ assert tensor.labels == ([['a', 'b']], [['x', 'y']])
192
+ ```
193
+
194
+ #### `from_strands(profile, strands)`
195
+
196
+ Creates a tensor from strands (sparse string representations of non-zero elements).
197
+
198
+ ```python
199
+ profile = [[['a', 'b']], [['x', 'y', 'z']]]
200
+ strands = {
201
+ "[[['a']], [['x']]]": 0.1,
202
+ "[[['a']], [['y']]]": 0.2,
203
+ "[[['a']], [['z']]]": 0.7,
204
+ "[[['b']], [['x']]]": 0.3,
205
+ "[[['b']], [['y']]]": 0.3,
206
+ "[[['b']], [['z']]]": 0.4
207
+ }
208
+ tensor = FXTensor.from_strands(profile, strands)
209
+ assert tensor.labels == ([['a', 'b']], [['x', 'y', 'z']])
187
210
  ```
188
211
 
189
- #### `discard_prefix(start_B)`
212
+ #### `identity_tensor(list_x)`
213
+
214
+ Creates an identity tensor for the given dimensions, supporting labeled or numeric inputs.
215
+
216
+ ```python
217
+ labels = [['a', 'b']]
218
+ id_tensor = FXTensor.identity_tensor(labels)
219
+ assert id_tensor.labels == ([labels], [labels])
220
+ ```
221
+
222
+ #### `unit_tensor(dims)`
223
+
224
+ Creates a unit state tensor (all-ones vector) for the given dimensions.
225
+
226
+ ```python
227
+ dims = [2, 3]
228
+ unit = FXTensor.unit_tensor(dims)
229
+ assert unit.profile == [[], dims]
230
+ assert np.all(unit.data == 1)
231
+ ```
232
+
233
+ #### `delta_tensor(dims)`
234
+
235
+ Creates a delta tensor (identity matrix) for the given dimensions, used for copying.
190
236
 
191
237
  ```python
192
- # Get P(Weather | Location) by marginalizing Season
193
- weather_tensor = process_tensor.discard_prefix(start_B=2)
194
- assert weather_tensor.labels == ([['Urban', 'Rural']], [['Sunny', 'Rainy']])
238
+ dims = [2]
239
+ delta = FXTensor.delta_tensor(dims)
240
+ assert delta.profile == [[dims], [dims]]
195
241
  ```
196
242
 
197
- #### `conditionalization(start_B)`
243
+ #### `conditionalization(concat_start_index)`
244
+
245
+ Creates a conditional probability distribution from a joint state by dividing at the specified index in codomain.
198
246
 
199
247
  ```python
200
248
  # Compute P(Weather | Location, Season)
201
- cond_tensor = process_tensor.conditionalization(start_B=2)
249
+ cond_tensor = process_tensor.conditionalization(concat_start_index=2)
202
250
  assert cond_tensor.labels == ([['Urban', 'Rural'], ['Spring', 'Summer', 'Other']], [['Sunny', 'Rainy']])
203
251
  ```
204
252
 
205
- #### `tensor_product(other)`
253
+ #### `marginalization(start_B)`
254
+
255
+ Marginalizes out part of the codomain by summing over it.
256
+
257
+ ```python
258
+ # Get P(Season | Location) by marginalizing Weather
259
+ season_tensor = process_tensor.marginalization(start_B=2)
260
+ assert season_tensor.labels == ([['Urban', 'Rural']], [['Spring', 'Summer', 'Other']])
261
+ ```
262
+
263
+ #### `jointification(other)`
264
+
265
+ Creates a joint state from two state tensors.
206
266
 
207
267
  ```python
208
- # Add Traffic (Low, High) system
268
+ # Define another state tensor
209
269
  traffic_labels = ['Low', 'High']
210
270
  traffic_state = FXTensor([[], [traffic_labels]], data=np.array([0.7, 0.3]))
211
- joint_tensor = process_tensor.tensor_product(traffic_state)
212
- assert joint_tensor.labels == (None, [['Urban', 'Rural'], ['Spring', 'Summer', 'Other'], ['Sunny', 'Rainy'], ['Low', 'High']])
271
+ # Create joint state
272
+ joint_state = process_tensor.jointification(traffic_state)
273
+ assert joint_state.labels == ([], [['Urban', 'Rural'], ['Spring', 'Summer', 'Other'], ['Sunny', 'Rainy'], ['Low', 'High']])
213
274
  ```
214
275
 
215
276
  ## Theoretical Background: Relation to Markov Categories
@@ -225,7 +286,7 @@ The `fxtensor-salmon` library is designed based on the **Markov Category**, a fr
225
286
 
226
287
  1. **Composition (`composition`)**: Combines morphisms `f: A -> B` and `g: B -> C`. Corresponds to connecting wires in string diagrams.
227
288
  2. **Tensor Product (`tensor_product`)**: Combines independent systems. Represented as side-by-side diagrams.
228
- 3. **Discard (`marginalization`, `discard_prefix`)**: Sums over output axes to eliminate them.
289
+ 3. **Discard (`exclamation`)**: Sums over output axes to eliminate them. `exclamation(list_x)` creates a discarding tensor for the specified dimensions.
229
290
  4. **Copy (`delta_tensor`)**: Deterministic copying operation.
230
291
 
231
292
  ### Probabilistic Properties
@@ -104,7 +104,7 @@ tensor2 = FXTensor(
104
104
 
105
105
  # Tensor product: P(X,Y) = P(X) ⊗ P(Y)
106
106
  result = tensor1.tensor_product(tensor2)
107
- assert result.labels == (None, [['a', 'b'], ['x', 'y', 'z']])
107
+ assert result.labels == ([], [['a', 'b'], ['x', 'y', 'z']])
108
108
  ```
109
109
 
110
110
  ## Simple Example: Weather Forecast (Labeled)
@@ -154,38 +154,99 @@ Model the joint probability of **Season** (Spring, Summer, Other) and **Weather*
154
154
 
155
155
  ### Key Method Applications
156
156
 
157
- #### `marginalization(start_B)`
157
+ #### `from_json(json_data)`
158
+
159
+ Creates a tensor from JSON data, loading profile and data to instantiate an FXTensor.
158
160
 
159
161
  ```python
160
- # Get P(Season | Location) by marginalizing Weather
161
- season_tensor = process_tensor.marginalization(start_B=2)
162
- assert season_tensor.labels == ([['Urban', 'Rural']], [['Spring', 'Summer', 'Other']])
162
+ json_data = {
163
+ "profile": [[['a', 'b']], [['x', 'y']]],
164
+ "data": [[0.2, 0.8], [0.6, 0.4]]
165
+ }
166
+ tensor = FXTensor.from_json(json_data)
167
+ assert tensor.labels == ([['a', 'b']], [['x', 'y']])
168
+ ```
169
+
170
+ #### `from_strands(profile, strands)`
171
+
172
+ Creates a tensor from strands (sparse string representations of non-zero elements).
173
+
174
+ ```python
175
+ profile = [[['a', 'b']], [['x', 'y', 'z']]]
176
+ strands = {
177
+ "[[['a']], [['x']]]": 0.1,
178
+ "[[['a']], [['y']]]": 0.2,
179
+ "[[['a']], [['z']]]": 0.7,
180
+ "[[['b']], [['x']]]": 0.3,
181
+ "[[['b']], [['y']]]": 0.3,
182
+ "[[['b']], [['z']]]": 0.4
183
+ }
184
+ tensor = FXTensor.from_strands(profile, strands)
185
+ assert tensor.labels == ([['a', 'b']], [['x', 'y', 'z']])
163
186
  ```
164
187
 
165
- #### `discard_prefix(start_B)`
188
+ #### `identity_tensor(list_x)`
189
+
190
+ Creates an identity tensor for the given dimensions, supporting labeled or numeric inputs.
191
+
192
+ ```python
193
+ labels = [['a', 'b']]
194
+ id_tensor = FXTensor.identity_tensor(labels)
195
+ assert id_tensor.labels == ([labels], [labels])
196
+ ```
197
+
198
+ #### `unit_tensor(dims)`
199
+
200
+ Creates a unit state tensor (all-ones vector) for the given dimensions.
201
+
202
+ ```python
203
+ dims = [2, 3]
204
+ unit = FXTensor.unit_tensor(dims)
205
+ assert unit.profile == [[], dims]
206
+ assert np.all(unit.data == 1)
207
+ ```
208
+
209
+ #### `delta_tensor(dims)`
210
+
211
+ Creates a delta tensor (identity matrix) for the given dimensions, used for copying.
166
212
 
167
213
  ```python
168
- # Get P(Weather | Location) by marginalizing Season
169
- weather_tensor = process_tensor.discard_prefix(start_B=2)
170
- assert weather_tensor.labels == ([['Urban', 'Rural']], [['Sunny', 'Rainy']])
214
+ dims = [2]
215
+ delta = FXTensor.delta_tensor(dims)
216
+ assert delta.profile == [[dims], [dims]]
171
217
  ```
172
218
 
173
- #### `conditionalization(start_B)`
219
+ #### `conditionalization(concat_start_index)`
220
+
221
+ Creates a conditional probability distribution from a joint state by dividing at the specified index in codomain.
174
222
 
175
223
  ```python
176
224
  # Compute P(Weather | Location, Season)
177
- cond_tensor = process_tensor.conditionalization(start_B=2)
225
+ cond_tensor = process_tensor.conditionalization(concat_start_index=2)
178
226
  assert cond_tensor.labels == ([['Urban', 'Rural'], ['Spring', 'Summer', 'Other']], [['Sunny', 'Rainy']])
179
227
  ```
180
228
 
181
- #### `tensor_product(other)`
229
+ #### `marginalization(start_B)`
230
+
231
+ Marginalizes out part of the codomain by summing over it.
232
+
233
+ ```python
234
+ # Get P(Season | Location) by marginalizing Weather
235
+ season_tensor = process_tensor.marginalization(start_B=2)
236
+ assert season_tensor.labels == ([['Urban', 'Rural']], [['Spring', 'Summer', 'Other']])
237
+ ```
238
+
239
+ #### `jointification(other)`
240
+
241
+ Creates a joint state from two state tensors.
182
242
 
183
243
  ```python
184
- # Add Traffic (Low, High) system
244
+ # Define another state tensor
185
245
  traffic_labels = ['Low', 'High']
186
246
  traffic_state = FXTensor([[], [traffic_labels]], data=np.array([0.7, 0.3]))
187
- joint_tensor = process_tensor.tensor_product(traffic_state)
188
- assert joint_tensor.labels == (None, [['Urban', 'Rural'], ['Spring', 'Summer', 'Other'], ['Sunny', 'Rainy'], ['Low', 'High']])
247
+ # Create joint state
248
+ joint_state = process_tensor.jointification(traffic_state)
249
+ assert joint_state.labels == ([], [['Urban', 'Rural'], ['Spring', 'Summer', 'Other'], ['Sunny', 'Rainy'], ['Low', 'High']])
189
250
  ```
190
251
 
191
252
  ## Theoretical Background: Relation to Markov Categories
@@ -201,7 +262,7 @@ The `fxtensor-salmon` library is designed based on the **Markov Category**, a fr
201
262
 
202
263
  1. **Composition (`composition`)**: Combines morphisms `f: A -> B` and `g: B -> C`. Corresponds to connecting wires in string diagrams.
203
264
  2. **Tensor Product (`tensor_product`)**: Combines independent systems. Represented as side-by-side diagrams.
204
- 3. **Discard (`marginalization`, `discard_prefix`)**: Sums over output axes to eliminate them.
265
+ 3. **Discard (`exclamation`)**: Sums over output axes to eliminate them. `exclamation(list_x)` creates a discarding tensor for the specified dimensions.
205
266
  4. **Copy (`delta_tensor`)**: Deterministic copying operation.
206
267
 
207
268
  ### Probabilistic Properties
@@ -1,12 +1,13 @@
1
1
  # pyproject.toml
2
2
 
3
+
3
4
  [build-system]
4
5
  requires = ["setuptools>=61.0"]
5
6
  build-backend = "setuptools.build_meta"
6
7
 
7
8
  [project]
8
9
  name = "fxtensor_salmon"
9
- version = "0.2.1"
10
+ version = "0.2.2"
10
11
  authors = [
11
12
  { name="Tadachika Oki", email="sakesnare@gmail.com" },
12
13
  ]
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: fxtensor_salmon
3
- Version: 0.2.1
3
+ Version: 0.2.2
4
4
  Summary: Markov Tensor Module
5
5
  Author-email: Tadachika Oki <sakesnare@gmail.com>
6
6
  License: Copyright 2025 Tadachika Oki
@@ -128,7 +128,7 @@ tensor2 = FXTensor(
128
128
 
129
129
  # Tensor product: P(X,Y) = P(X) ⊗ P(Y)
130
130
  result = tensor1.tensor_product(tensor2)
131
- assert result.labels == (None, [['a', 'b'], ['x', 'y', 'z']])
131
+ assert result.labels == ([], [['a', 'b'], ['x', 'y', 'z']])
132
132
  ```
133
133
 
134
134
  ## Simple Example: Weather Forecast (Labeled)
@@ -178,38 +178,99 @@ Model the joint probability of **Season** (Spring, Summer, Other) and **Weather*
178
178
 
179
179
  ### Key Method Applications
180
180
 
181
- #### `marginalization(start_B)`
181
+ #### `from_json(json_data)`
182
+
183
+ Creates a tensor from JSON data, loading profile and data to instantiate an FXTensor.
182
184
 
183
185
  ```python
184
- # Get P(Season | Location) by marginalizing Weather
185
- season_tensor = process_tensor.marginalization(start_B=2)
186
- assert season_tensor.labels == ([['Urban', 'Rural']], [['Spring', 'Summer', 'Other']])
186
+ json_data = {
187
+ "profile": [[['a', 'b']], [['x', 'y']]],
188
+ "data": [[0.2, 0.8], [0.6, 0.4]]
189
+ }
190
+ tensor = FXTensor.from_json(json_data)
191
+ assert tensor.labels == ([['a', 'b']], [['x', 'y']])
192
+ ```
193
+
194
+ #### `from_strands(profile, strands)`
195
+
196
+ Creates a tensor from strands (sparse string representations of non-zero elements).
197
+
198
+ ```python
199
+ profile = [[['a', 'b']], [['x', 'y', 'z']]]
200
+ strands = {
201
+ "[[['a']], [['x']]]": 0.1,
202
+ "[[['a']], [['y']]]": 0.2,
203
+ "[[['a']], [['z']]]": 0.7,
204
+ "[[['b']], [['x']]]": 0.3,
205
+ "[[['b']], [['y']]]": 0.3,
206
+ "[[['b']], [['z']]]": 0.4
207
+ }
208
+ tensor = FXTensor.from_strands(profile, strands)
209
+ assert tensor.labels == ([['a', 'b']], [['x', 'y', 'z']])
187
210
  ```
188
211
 
189
- #### `discard_prefix(start_B)`
212
+ #### `identity_tensor(list_x)`
213
+
214
+ Creates an identity tensor for the given dimensions, supporting labeled or numeric inputs.
215
+
216
+ ```python
217
+ labels = [['a', 'b']]
218
+ id_tensor = FXTensor.identity_tensor(labels)
219
+ assert id_tensor.labels == ([labels], [labels])
220
+ ```
221
+
222
+ #### `unit_tensor(dims)`
223
+
224
+ Creates a unit state tensor (all-ones vector) for the given dimensions.
225
+
226
+ ```python
227
+ dims = [2, 3]
228
+ unit = FXTensor.unit_tensor(dims)
229
+ assert unit.profile == [[], dims]
230
+ assert np.all(unit.data == 1)
231
+ ```
232
+
233
+ #### `delta_tensor(dims)`
234
+
235
+ Creates a delta tensor (identity matrix) for the given dimensions, used for copying.
190
236
 
191
237
  ```python
192
- # Get P(Weather | Location) by marginalizing Season
193
- weather_tensor = process_tensor.discard_prefix(start_B=2)
194
- assert weather_tensor.labels == ([['Urban', 'Rural']], [['Sunny', 'Rainy']])
238
+ dims = [2]
239
+ delta = FXTensor.delta_tensor(dims)
240
+ assert delta.profile == [[dims], [dims]]
195
241
  ```
196
242
 
197
- #### `conditionalization(start_B)`
243
+ #### `conditionalization(concat_start_index)`
244
+
245
+ Creates a conditional probability distribution from a joint state by dividing at the specified index in codomain.
198
246
 
199
247
  ```python
200
248
  # Compute P(Weather | Location, Season)
201
- cond_tensor = process_tensor.conditionalization(start_B=2)
249
+ cond_tensor = process_tensor.conditionalization(concat_start_index=2)
202
250
  assert cond_tensor.labels == ([['Urban', 'Rural'], ['Spring', 'Summer', 'Other']], [['Sunny', 'Rainy']])
203
251
  ```
204
252
 
205
- #### `tensor_product(other)`
253
+ #### `marginalization(start_B)`
254
+
255
+ Marginalizes out part of the codomain by summing over it.
256
+
257
+ ```python
258
+ # Get P(Season | Location) by marginalizing Weather
259
+ season_tensor = process_tensor.marginalization(start_B=2)
260
+ assert season_tensor.labels == ([['Urban', 'Rural']], [['Spring', 'Summer', 'Other']])
261
+ ```
262
+
263
+ #### `jointification(other)`
264
+
265
+ Creates a joint state from two state tensors.
206
266
 
207
267
  ```python
208
- # Add Traffic (Low, High) system
268
+ # Define another state tensor
209
269
  traffic_labels = ['Low', 'High']
210
270
  traffic_state = FXTensor([[], [traffic_labels]], data=np.array([0.7, 0.3]))
211
- joint_tensor = process_tensor.tensor_product(traffic_state)
212
- assert joint_tensor.labels == (None, [['Urban', 'Rural'], ['Spring', 'Summer', 'Other'], ['Sunny', 'Rainy'], ['Low', 'High']])
271
+ # Create joint state
272
+ joint_state = process_tensor.jointification(traffic_state)
273
+ assert joint_state.labels == ([], [['Urban', 'Rural'], ['Spring', 'Summer', 'Other'], ['Sunny', 'Rainy'], ['Low', 'High']])
213
274
  ```
214
275
 
215
276
  ## Theoretical Background: Relation to Markov Categories
@@ -225,7 +286,7 @@ The `fxtensor-salmon` library is designed based on the **Markov Category**, a fr
225
286
 
226
287
  1. **Composition (`composition`)**: Combines morphisms `f: A -> B` and `g: B -> C`. Corresponds to connecting wires in string diagrams.
227
288
  2. **Tensor Product (`tensor_product`)**: Combines independent systems. Represented as side-by-side diagrams.
228
- 3. **Discard (`marginalization`, `discard_prefix`)**: Sums over output axes to eliminate them.
289
+ 3. **Discard (`exclamation`)**: Sums over output axes to eliminate them. `exclamation(list_x)` creates a discarding tensor for the specified dimensions.
229
290
  4. **Copy (`delta_tensor`)**: Deterministic copying operation.
230
291
 
231
292
  ### Probabilistic Properties
File without changes