Why this talk?
Como fisique no estoy formado para producir código "productivo"
Resolver un problema de cinemática
$F = m*a$
Coordenadas cartesianas :
$t, x, y$
Coordenadas polares:
$t, r, \theta$
'homunculo'
'homunculo'
👤 ✉️
'homunculo'.upper()
'HOMUNCULO'
👤 💌
'homunculo'.split('o')
['h', 'muncul', '']
👤 💌 🖃 👤 ✉️ 🧧 👤
Refactoring is the process of changing a software
system in such a way that it does not alter the external
behavior of the code yet improves its internal structure.
— Martin Fowler
Refactoring
import numpy as np
import matplotlib.pyplot as plt
t,x = np.loadtxt('a_1.txt')
def f(x,t,y):
return ( x[0] * t**2 + x[1] * t**4 + x[2] - y )
from scipy.optimize import least_squares
x0 = np.array([0, 0, 0])
res = least_squares(f, x0, args=(t,x), verbose=2, loss='cauchy', method='dogbox',ftol=1e-10)
y = res.x[0] * t**4 + res.x[1] * t**2 + res.x[2]
plt.plot(t,x,'ok')
plt.plot(t,y,'r')
ax = plt.gca()
ax.set_ylim(-2,2)
import numpy as np
import matplotlib.pyplot as plt
t,x = np.loadtxt('a_1.txt')
def f(x,t,y):
return ( x[0] * t**2 + x[1] * t**4 + x[2] - y )
from scipy.optimize import least_squares
x0 = np.array([0, 0, 0])
res = least_squares(f, x0, args=(t,x), verbose=2, loss='cauchy', method='dogbox',ftol=1e-10)
y = res.x[0] * t**4 + res.x[1] * t**2 + res.x[2]
plt.plot(t,x,'ok')
plt.plot(t,y,'r')
ax = plt.gca()
ax.set_ylim(-2,2)
Objects should be open for extension, but closed for modification.
import numpy as np
import matplotlib.pyplot as plt
t,x = np.loadtxt('a_1.txt')
def f(x,t,y):
return ( x[0] * t**2 + x[1] * t**4 + x[2] - y )
from scipy.optimize import least_squares
x0 = np.array([0, 0, 0])
res = least_squares(f, x0, args=(t,x), verbose=2, loss='cauchy', method='dogbox',ftol=1e-10)
y = res.x[0] * t**4 + res.x[1] * t**2 + res.x[2]
plt.plot(t,x,'ok')
plt.plot(t,y,'r')
ax = plt.gca()
ax.set_ylim(-2,2)
for each desired change, make the change easy (warning: this may be hard), then make the easy change
— Kent Beck (@KentBeck) September 25, 2012
$ H = \frac{p^2}{2*m} $
$ H = \frac{p^2}{2*m} + q^2 $
$ H = \frac{p^2}{2*m} + q^2 + ... + V(p,q) $
Programming objects/computing objects
Pensar a objeto como pequeñas entidades inteligentes con las cuales podemos interactuar mediante mensajes
Código no testeado es código que no existe