executable-engineering 0.1.33__py3-none-any.whl → 0.1.35__py3-none-any.whl

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.
@@ -5,11 +5,21 @@ from .closedoptimization_module import OptimizerClosed
5
5
  from .openoptimization_module import OptimizerOpen
6
6
  from .gradientoptimization_module import OptimizerGrad
7
7
  from .numerical_error_module import python_internal_binary, decimal_to_binary
8
- from .linearsystems_module import visual_solve_2d, visualize_conditioning
8
+ from .linearsystems_module import visual_solve_2d, visualize_conditioning, visualize_matrix_norms, jacobi_step, gauss_seidel_step, sor_step, visualize_convergence_2d, create_diagonally_dominant_matrix, iter_solve, steepest_descent, conjugate_gradient, IterationTracker
9
9
 
10
10
  __all__ = [
11
11
  "visual_solve_2d",
12
12
  "visualize_conditioning",
13
+ "visualize_matrix_norms",
14
+ "jacobi_step",
15
+ "gauss_seidel_step",
16
+ "sor_step",
17
+ "visualize_convergence_2d",
18
+ "create_diagonally_dominant_matrix",
19
+ "iter_solve",
20
+ "steepest_descent",
21
+ "conjugate_gradient",
22
+ "IterationTracker",
13
23
  "init_weights",
14
24
  "tanh",
15
25
  "tanh_derivative",
@@ -167,3 +167,276 @@ def visualize_conditioning(A):
167
167
  )
168
168
 
169
169
  return fig
170
+
171
+
172
+ def visualize_matrix_norms(A):
173
+ """
174
+ Visualizes matrix norms by plotting the transformation of a unit circle
175
+ and overlaying bounding circles based on different matrix norms.
176
+
177
+ Args:
178
+ A: (2, 2) array-like matrix
179
+ """
180
+ import numpy as np
181
+ import plotly.graph_objects as go
182
+
183
+ A = np.array(A, dtype=float)
184
+ if A.shape != (2, 2):
185
+ raise ValueError("visualize_matrix_norms requires a 2x2 matrix.")
186
+
187
+ # 1. Calculate the norms
188
+ norm_2 = np.linalg.norm(A, ord=2)
189
+ norm_F = np.linalg.norm(A, ord='fro')
190
+ norm_inf = np.linalg.norm(A, ord=np.inf)
191
+
192
+ # 2. Transform the unit circle
193
+ theta = np.linspace(0, 2*np.pi, 200)
194
+ circle = np.array([np.cos(theta), np.sin(theta)])
195
+ ellipse = A @ circle
196
+
197
+ # 3. Create circles for the norms
198
+ circle_2 = norm_2 * circle
199
+ circle_F = norm_F * circle
200
+ circle_inf = norm_inf * circle
201
+
202
+ # 4. Plot!
203
+ fig = go.Figure()
204
+
205
+ # Plot the transformed ellipse
206
+ fig.add_trace(go.Scatter(x=ellipse[0], y=ellipse[1], mode='lines', name='A * Unit Circle (Ellipse)', line=dict(color='blue', width=3)))
207
+
208
+ # Plot the norm bounding circles
209
+ fig.add_trace(go.Scatter(x=circle_2[0], y=circle_2[1], mode='lines', name=f'2-Norm (Radius {norm_2:.2f})', line=dict(color='green', dash='dash')))
210
+ fig.add_trace(go.Scatter(x=circle_F[0], y=circle_F[1], mode='lines', name=f'Frobenius Norm (Radius {norm_F:.2f})', line=dict(color='orange', dash='dash')))
211
+ fig.add_trace(go.Scatter(x=circle_inf[0], y=circle_inf[1], mode='lines', name=f'Infinity Norm (Radius {norm_inf:.2f})', line=dict(color='red', dash='dash')))
212
+
213
+ # Setup axes
214
+ max_val = norm_inf * 1.1
215
+ fig.update_xaxes(range=[-max_val, max_val])
216
+ fig.update_yaxes(range=[-max_val, max_val], scaleanchor="x", scaleratio=1)
217
+ fig.update_layout(title="Matrix Norms as Bounds on Geometric Distortion", width=700, height=700)
218
+
219
+ return fig
220
+
221
+ def jacobi_step(A, b, x):
222
+ """Performs a single Jacobi iteration step."""
223
+ n = len(x)
224
+ x_new = np.zeros_like(x)
225
+ for i in range(n):
226
+ x_new[i] = (b[i] - np.dot(A[i, :i], x[:i]) - np.dot(A[i, i + 1:], x[i + 1:])) / A[i, i]
227
+ return x_new
228
+
229
+ def gauss_seidel_step(A, b, x, omega = 1):
230
+ """Performs a single Gauss-Seidel iteration step."""
231
+ n = len(x)
232
+ for i in range(n):
233
+ x[i] = (b[i] - np.dot(A[i, :i], x[:i]) - np.dot(A[i, i + 1:], x[i + 1:])) / A[i, i]
234
+ return x
235
+
236
+ def sor_step(A, b, x, omega=1.05):
237
+ """Performs a single SOR iteration step."""
238
+ n = len(x)
239
+ for i in range(n):
240
+ x[i] = (1 - omega) * x[i] + (omega / A[i, i]) * (b[i] - np.dot(A[i, :i], x[:i]) - np.dot(A[i, i + 1:], x[i + 1:]))
241
+ return x
242
+
243
+ def visualize_convergence_2d(A, b, iterations, surface_type='residual'):
244
+ """
245
+ Visualizes the convergence path of an iterative solver over a 2D contour surface.
246
+
247
+ Args:
248
+ A: (2, 2) coefficient matrix.
249
+ b: (2,) right-hand side vector.
250
+ iterations: List or array of (2,) vectors representing the guess at each step.
251
+ surface_type: 'residual' plots ||Ax - b||. 'quadratic' plots 0.5 x^T A x - b^T x.
252
+ """
253
+ import numpy as np
254
+ import plotly.graph_objects as go
255
+
256
+ A = np.array(A, dtype=float)
257
+ b = np.array(b, dtype=float).flatten()
258
+ iterations = np.array(iterations)
259
+
260
+ if A.shape != (2, 2):
261
+ raise ValueError("visualize_convergence_2d only supports 2D systems.")
262
+
263
+ try:
264
+ x_true = np.linalg.solve(A, b)
265
+ except np.linalg.LinAlgError:
266
+ x_true = np.zeros(2)
267
+
268
+ # Determine bounds
269
+ all_points = np.vstack([iterations, x_true])
270
+ min_x, max_x = np.min(all_points[:, 0]), np.max(all_points[:, 0])
271
+ min_y, max_y = np.min(all_points[:, 1]), np.max(all_points[:, 1])
272
+
273
+ # Add padding
274
+ pad_x = max((max_x - min_x) * 0.3, 0.5)
275
+ pad_y = max((max_y - min_y) * 0.3, 0.5)
276
+
277
+ x_range = np.linspace(min_x - pad_x, max_x + pad_x, 100)
278
+ y_range = np.linspace(min_y - pad_y, max_y + pad_y, 100)
279
+ X, Y = np.meshgrid(x_range, y_range)
280
+
281
+ if surface_type == 'residual':
282
+ # ||Ax - b||
283
+ Z = np.sqrt((A[0,0]*X + A[0,1]*Y - b[0])**2 + (A[1,0]*X + A[1,1]*Y - b[1])**2)
284
+ title = "Convergence Path over Residual Surface ||Ax - b||"
285
+ elif surface_type == 'quadratic':
286
+ # 0.5 x^T A x - b^T x
287
+ Z = 0.5 * (A[0,0]*X**2 + (A[0,1] + A[1,0])*X*Y + A[1,1]*Y**2) - (b[0]*X + b[1]*Y)
288
+ title = "Convergence Path over Quadratic Surface f(x)"
289
+ else:
290
+ raise ValueError("surface_type must be 'residual' or 'quadratic'")
291
+
292
+ fig = go.Figure()
293
+
294
+ # Contour plot
295
+ fig.add_trace(go.Contour(
296
+ x=x_range, y=y_range, z=Z,
297
+ colorscale='Viridis',
298
+ contours=dict(showlabels=True, labelfont=dict(size=12, color='white')),
299
+ showscale=False,
300
+ opacity=0.7
301
+ ))
302
+
303
+ # Convergence path
304
+ import numpy as np
305
+ import plotly.express as px
306
+
307
+ # We want to draw each segment with a color from the colormap
308
+ if len(iterations) > 1:
309
+ colors = px.colors.sample_colorscale('magma', np.linspace(0, 1, len(iterations)-1))
310
+ for i in range(len(iterations)-1):
311
+ fig.add_trace(go.Scatter(
312
+ x=iterations[i:i+2, 0], y=iterations[i:i+2, 1],
313
+ mode='lines',
314
+ line=dict(color=colors[i], width=3),
315
+ showlegend=False
316
+ ))
317
+
318
+ # Add a dummy trace just to show the colorbar
319
+ fig.add_trace(go.Scatter(
320
+ x=[None], y=[None],
321
+ mode='markers',
322
+ marker=dict(
323
+ size=0,
324
+ color=[0, len(iterations)-1],
325
+ colorscale='magma',
326
+ showscale=True,
327
+ colorbar=dict(title="Iteration", len=0.5, y=0.5, x=1.1)
328
+ ),
329
+ showlegend=False
330
+ ))
331
+ else:
332
+ # Just in case there is no path
333
+ fig.add_trace(go.Scatter(
334
+ x=iterations[:, 0], y=iterations[:, 1],
335
+ mode='markers',
336
+ marker=dict(size=8, color='white'),
337
+ name='Iterations'
338
+ ))
339
+
340
+
341
+
342
+ fig.update_layout(
343
+ title=title,
344
+ width=700, height=600,
345
+ xaxis_title="x₁",
346
+ yaxis_title="x₂",
347
+ yaxis=dict(scaleanchor="x", scaleratio=1),
348
+ margin=dict(l=20, r=20, t=50, b=20)
349
+ )
350
+ return fig
351
+
352
+ def create_diagonally_dominant_matrix(n):
353
+ """Creates a diagonally dominant random matrix.
354
+
355
+ Args:
356
+ n: The size of the matrix.
357
+
358
+ Returns:
359
+ A diagonally dominant random matrix.
360
+ """
361
+ import numpy as np
362
+ A = np.random.rand(n, n)
363
+ for i in range(n):
364
+ A[i, i] = np.sum(np.abs(A[i, :])) + np.random.rand()
365
+ return A
366
+
367
+ def iter_solve(A, b, method, tol=1e-6, track_history=False):
368
+ """
369
+ General wrapper function that repeatedly applies an iterative step
370
+ until the residual error falls below a tolerance.
371
+ """
372
+ import numpy as np
373
+ x = np.zeros(len(b))
374
+ max_iter = 1000
375
+
376
+ if track_history:
377
+ iterations = [x.copy()]
378
+
379
+ for i in range(max_iter):
380
+ x_new = method(A, b, x.copy())
381
+ if track_history:
382
+ iterations.append(x_new.copy())
383
+
384
+ if np.linalg.norm(A @ x_new - b) < tol:
385
+ print(f'Converged after {i+1} iterations.')
386
+ break
387
+ x = x_new.copy()
388
+
389
+ if track_history:
390
+ return x, iterations
391
+ return x
392
+
393
+ def steepest_descent(A, b, x0, tol=1e-6, max_iter=100, track_history=False):
394
+ """Solves a linear system Ax = b using the method of steepest descent."""
395
+ import numpy as np
396
+ x = x0.copy()
397
+ if track_history:
398
+ iterations = [x.copy()]
399
+
400
+ for i in range(max_iter):
401
+ r = A @ x - b
402
+ if np.linalg.norm(r) < tol:
403
+ if track_history: print(f'Converged after {i+1} iterations.')
404
+ break
405
+ alpha = np.dot(r, r) / np.dot(r, A @ r)
406
+ x = x - alpha * r
407
+ if track_history:
408
+ iterations.append(x.copy())
409
+
410
+ if track_history:
411
+ return x, iterations
412
+ return x
413
+
414
+ def conjugate_gradient(A, b, x0, tol=1e-6, track_history=False):
415
+ """Wrapper around SciPy CG to track iterations if needed."""
416
+ import numpy as np
417
+ from scipy.sparse.linalg import cg
418
+
419
+ if track_history:
420
+ iterations = [x0.copy()]
421
+ def callback(xk):
422
+ iterations.append(np.copy(xk))
423
+ solution, info = cg(A, b, x0=x0, tol=tol, callback=callback)
424
+ return solution, np.array(iterations)
425
+ else:
426
+ solution, info = cg(A, b, x0=x0, tol=tol)
427
+ return solution
428
+
429
+ class IterationTracker:
430
+ """
431
+ A clean callback tracker for native SciPy solvers (cg, gmres, etc.).
432
+ """
433
+ def __init__(self, x0=None):
434
+ import numpy as np
435
+ if x0 is not None:
436
+ self.iterations = [np.array(x0).copy()]
437
+ else:
438
+ self.iterations = []
439
+
440
+ def __call__(self, xk):
441
+ import numpy as np
442
+ self.iterations.append(np.array(xk).copy())
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: executable_engineering
3
- Version: 0.1.33
3
+ Version: 0.1.35
4
4
  Summary: Demonstrative tools for the Executable Engineering textbook
5
5
  Home-page: https://github.com/themintlab/ExecutableEngineering/tree/main/executable_engineering
6
6
  Author: Michael Welland
@@ -1,14 +1,14 @@
1
- executable_engineering/__init__.py,sha256=jrrsfub0C6cM1R9r7Gw97tW5CxX5kNEoYkvxlHzSJjU,1166
1
+ executable_engineering/__init__.py,sha256=GiYYijxAfiee44SRLXjMsFCa3xOPWtUx0tnlWm4G6TM,1596
2
2
  executable_engineering/closedoptimization_module.py,sha256=i9KkdulyCZmriYKlx0CuUetjoMmxJm9ADWgStU3w85w,9273
3
3
  executable_engineering/closedrootfinder_module.py,sha256=ozccVABOrTiqcdkkqwAWRNo04MsDC0Z_f_tfukl3q20,5706
4
4
  executable_engineering/gradientoptimization_module.py,sha256=-2fErE9cemSEPeV9PlWOf4cF7Tr6DjilEw_NxvE-JYw,6601
5
- executable_engineering/linearsystems_module.py,sha256=JNKcHPUxOBJ4xoFYZyUFwbEOGGD_gi6zRQfcJKYBb5E,6080
5
+ executable_engineering/linearsystems_module.py,sha256=4fJyuFGUdm2FKiydDD-_vAh1O9XEH8f0puBKWkGtlqM,15101
6
6
  executable_engineering/neuralnet_module.py,sha256=Hu9QFoMyODg4RCEpHmT9zTwzjcVFYM8ZLYtbB3CbrsA,10102
7
7
  executable_engineering/numerical_error_module.py,sha256=wxfYP8u29c19I2D6MxpQV3zDMV0DpS1QUqFpp3f6eGE,1204
8
8
  executable_engineering/openoptimization_module.py,sha256=OCPyrynkTo1It50RoT7irnL72aQn6FexE_R1SfFYxlg,8590
9
9
  executable_engineering/openrootfinder_module.py,sha256=rmXDi0XdrDYdLZdPFgYCihMg_4NF1D71i6kh8Kx-V2w,5802
10
- executable_engineering-0.1.33.dist-info/licenses/LICENSE,sha256=Tg4nrt8rFhgpkPbIA8SWoCR3MlGLw0NufB8AE6-TH_o,1074
11
- executable_engineering-0.1.33.dist-info/METADATA,sha256=bZoPtBOuuAakueT9uX15GfwOIJeQaEw3YDkewAiKjaM,1648
12
- executable_engineering-0.1.33.dist-info/WHEEL,sha256=YVMoNqKzERt-wjUZwJ33xBGAwnFl-4cqbYkTtWa4itE,91
13
- executable_engineering-0.1.33.dist-info/top_level.txt,sha256=0U5ATV5WGTLvN9rRSjQ54TyL-ztTyXaN3FzZ_QMq4HM,23
14
- executable_engineering-0.1.33.dist-info/RECORD,,
10
+ executable_engineering-0.1.35.dist-info/licenses/LICENSE,sha256=Tg4nrt8rFhgpkPbIA8SWoCR3MlGLw0NufB8AE6-TH_o,1074
11
+ executable_engineering-0.1.35.dist-info/METADATA,sha256=g6kHizAHnhfStOWrQIt53yY59SAdmTmiojVJ2hTvA1I,1648
12
+ executable_engineering-0.1.35.dist-info/WHEEL,sha256=YVMoNqKzERt-wjUZwJ33xBGAwnFl-4cqbYkTtWa4itE,91
13
+ executable_engineering-0.1.35.dist-info/top_level.txt,sha256=0U5ATV5WGTLvN9rRSjQ54TyL-ztTyXaN3FzZ_QMq4HM,23
14
+ executable_engineering-0.1.35.dist-info/RECORD,,