To upload files, please first save the app
import streamlit as st
import matplotlib.pyplot as plt
import numpy as np
class Vector2D:
def __init__(self, x, y):
self.x = x
self.y = y
def __add__(self, otro):
return Vector2D(self.x + otro.x, self.y + otro.y)
def __sub__(self, otro):
return Vector2D(self.x - otro.x, self.y - otro.y)
def __str__(self):
return f"({self.x}, {self.y})"
def producto_escalar(self, otro):
return self.x * otro.x + self.y * otro.y
def producto(self, escalar):
return Vector2D(self.x * escalar, self.y * escalar)
def graficar(self, otro, resultado, operacion):
fig, ax = plt.subplots()
ax.axhline(0, color='black', linewidth=0.5)
ax.axvline(0, color='black', linewidth=0.5)
ax.grid()
# Graficar los vectores
ax.quiver(0, 0, self.x, self.y, angles='xy', scale_units='xy', scale=1, color='r', label='v1')
ax.quiver(0, 0, otro.x, otro.y, angles='xy', scale_units='xy', scale=1, color='b', label='v2')
ax.quiver(0, 0, resultado.x, resultado.y, angles='xy', scale_units='xy', scale=1, color='g', label='Resultado')
ax.set_xlim(-10, 10)
ax.set_ylim(-10, 10)
ax.legend()
ax.set_title(f"Gráfica de {operacion}")
return fig
# --- Streamlit App ---
st.title("Operaciones con Vectores 2D")
# Entradas para los vectores
x1 = st.number_input("Ingrese x para el primer vector", step=1)
y1 = st.number_input("Ingrese y para el primer vector", step=1)
x2 = st.number_input("Ingrese x para el segundo vector", step=1)
y2 = st.number_input("Ingrese y para el segundo vector", step=1)
v1 = Vector2D(x1, y1)
v2 = Vector2D(x2, y2)
#
escalar = st.number_input("Ingrese un escalar para multiplicar", step=1)
#
vector_a_multiplicar = st.selectbox("Seleccione el vector para multiplicar por el escalar", ["v1", "v2"])
#
suma = v1 + v2
resta = v1 - v2
producto = v1.producto_escalar(v2)
st.write("Suma:", suma)
st.write("Resta:", resta)
st.write(f"Producto escalar: {producto}")
#
if vector_a_multiplicar == "v1":
producto_v1 = v1.producto(escalar)
st.write(f"Producto de v1 por {escalar}:", producto_v1)
st.pyplot(v1.graficar(v2, suma, "Suma"))
st.pyplot(v1.graficar(v2, resta, "Resta"))
st.pyplot(v1.graficar(Vector2D(0, 0), producto_v1, "Producto de v1 por escalar"))
else:
producto_v2 = v2.producto(escalar)
st.write(f"Producto de v2 por {escalar}:", producto_v2)
st.pyplot(v1.graficar(v2, suma, "Suma"))
st.pyplot(v1.graficar(v2, resta, "Resta"))
st.pyplot(v2.graficar(Vector2D(0, 0), producto_v2, "Producto de v2 por escalar"))
Hi! I can help you with any questions about Streamlit and Python. What would you like to know?