buoyrina commited on
Commit
99f0fa7
·
1 Parent(s): 0b3c072

matrix-vector

Browse files
Files changed (2) hide show
  1. app.py +85 -0
  2. requirements.txt +44 -0
app.py ADDED
@@ -0,0 +1,85 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import numpy as np
3
+ import matplotlib.pyplot as plt
4
+ from io import BytesIO
5
+ from PIL import Image
6
+ def matrix_vector_multiplication_visualization(matrix, vector):
7
+ try:
8
+ # Parse inputs
9
+ matrix = np.array([[float(x) for x in row.split(",")] for row in matrix.split(";")])
10
+ vector = np.array([float(x) for x in vector.split(",")])
11
+
12
+ # Ensure the matrix is 2x2 and the vector is 2D
13
+ if matrix.shape != (2, 2):
14
+ return "Error: Matrix must be 2x2.", None
15
+ if vector.shape != (2,):
16
+ return "Error: Vector must be 2D.", None
17
+
18
+ # Perform matrix-vector multiplication
19
+ transformed_vector = np.dot(matrix, vector)
20
+
21
+ # Create a grid for visualization
22
+ x = np.linspace(-1, 1, 10)
23
+ y = np.linspace(-1, 1, 10)
24
+ X, Y = np.meshgrid(x, y)
25
+ grid = np.vstack([X.flatten(), Y.flatten()])
26
+ transformed_grid = np.dot(matrix, grid).reshape(2, -1, 10)
27
+
28
+ # Create the plot
29
+ fig, ax = plt.subplots(figsize=(6, 6))
30
+
31
+ # Plot the grid before and after transformation
32
+ for i in range(grid.shape[1]):
33
+ ax.plot([grid[0, i], transformed_grid[0, i]], [grid[1, i], transformed_grid[1, i]],
34
+ color="gray", linewidth=0.5, alpha=0.7)
35
+
36
+ # Plot the original vector
37
+ ax.quiver(0, 0, vector[0], vector[1], angles="xy", scale_units="xy", scale=1, color="red", label="Original Vector")
38
+
39
+ # Plot the transformed vector
40
+ ax.quiver(0, 0, transformed_vector[0], transformed_vector[1], angles="xy", scale_units="xy", scale=1, color="blue", label="Transformed Vector")
41
+
42
+ # Plot settings
43
+ ax.axhline(0, color='black', linewidth=0.5)
44
+ ax.axvline(0, color='black', linewidth=0.5)
45
+ ax.set_xlim(-2, 2)
46
+ ax.set_ylim(-2, 2)
47
+ ax.set_aspect('equal')
48
+ ax.grid(True)
49
+ ax.legend()
50
+ ax.set_title("Matrix-Vector Multiplication Visualization")
51
+
52
+ # Save the plot to a BytesIO object
53
+ buf = BytesIO()
54
+ plt.savefig(buf, format="png")
55
+ buf.seek(0)
56
+ plt.close(fig)
57
+
58
+ return f"Transformed Vector: {transformed_vector.tolist()}", Image.open(buf)
59
+ except Exception as e:
60
+ return f"Error: {str(e)}", None
61
+
62
+ # Create the Gradio app
63
+ with gr.Blocks() as app:
64
+ gr.Markdown("## Matrix-Vector Multiplication Visualization")
65
+ gr.Markdown("""
66
+ - Enter a **2x2 matrix** as `a,b;c,d` (rows separated by semicolons).
67
+ - Enter a **2D vector** as `x,y`.
68
+ - See the original vector (red), transformed vector (blue), and grid transformation.
69
+ """)
70
+
71
+ with gr.Row():
72
+ matrix_input = gr.Textbox(label="Matrix (2x2, e.g., 1,0;0,1)", placeholder="e.g., 1,0;0,1")
73
+ vector_input = gr.Textbox(label="Vector (2D, e.g., 1,1)", placeholder="e.g., 1,1")
74
+
75
+ output_text = gr.Textbox(label="Result")
76
+ output_image = gr.Image(label="Visualization")
77
+
78
+ calculate_button = gr.Button("Visualize")
79
+ calculate_button.click(
80
+ fn=matrix_vector_multiplication_visualization,
81
+ inputs=[matrix_input, vector_input],
82
+ outputs=[output_text, output_image]
83
+ )
84
+
85
+ app.launch()
requirements.txt ADDED
@@ -0,0 +1,44 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+
2
+ # Usage: pip install -r requirements.txt
3
+
4
+ # Base ----------------------------------------
5
+ hydra-core>=1.2.0
6
+ matplotlib>=3.2.2
7
+ numpy>=1.18.5
8
+ opencv-python>=4.1.1
9
+ Pillow>=7.1.2
10
+ PyYAML>=5.3.1
11
+ requests>=2.23.0
12
+ scipy>=1.4.1
13
+ tqdm>=4.64.0
14
+
15
+ # Logging -------------------------------------
16
+ tensorboard>=2.4.1
17
+ # clearml
18
+ # comet
19
+
20
+ # Plotting ------------------------------------
21
+ pandas>=1.1.4
22
+ seaborn>=0.11.0
23
+
24
+ # Export --------------------------------------
25
+ # coremltools>=6.0 # CoreML export
26
+ # onnx>=1.12.0 # ONNX export
27
+ # onnx-simplifier>=0.4.1 # ONNX simplifier
28
+ # nvidia-pyindex # TensorRT export
29
+ # nvidia-tensorrt # TensorRT export
30
+ # scikit-learn==0.19.2 # CoreML quantization
31
+ # tensorflow>=2.4.1 # TF exports (-cpu, -aarch64, -macos)
32
+ # tensorflowjs>=3.9.0 # TF.js export
33
+ # openvino-dev # OpenVINO export
34
+
35
+ # Extras --------------------------------------
36
+ ipython # interactive notebook
37
+ psutil # system utilization
38
+ thop>=0.1.1 # FLOPs computation
39
+ # albumentations>=1.0.3
40
+ # pycocotools>=2.0.6 # COCO mAP
41
+ # roboflow
42
+
43
+ # HUB -----------------------------------------
44
+ GitPython>=3.1.24