Skip to content

Commit

Permalink
yay truediv
Browse files Browse the repository at this point in the history
  • Loading branch information
bqpd committed Mar 3, 2020
1 parent bf3c94d commit d0ad1be
Show file tree
Hide file tree
Showing 17 changed files with 55 additions and 57 deletions.
4 changes: 2 additions & 2 deletions docs/source/examples/beam.py
Expand Up @@ -74,14 +74,14 @@ def setup(self, N=4):
L, EI, q = sol("L"), sol("EI"), sol("q")
x = np.linspace(0, mag(L), len(q))*ureg.m # position along beam
q = q[0] # assume uniform loading for the check below
w_exact = q/(24.*EI) * x**2 * (x**2 - 4*L*x + 6*L**2) # analytic soln
w_exact = q/(24*EI) * x**2 * (x**2 - 4*L*x + 6*L**2) # analytic soln
assert max(abs(w_gp - w_exact)) <= 1.1*ureg.cm

PLOT = False
if PLOT:
import matplotlib.pyplot as plt
x_exact = np.linspace(0, L, 1000)
w_exact = q/(24.*EI) * x_exact**2 * (x_exact**2 - 4*L*x_exact + 6*L**2)
w_exact = q/(24*EI) * x_exact**2 * (x_exact**2 - 4*L*x_exact + 6*L**2)
plt.plot(x, w_gp, color='red', linestyle='solid', marker='^',
markersize=8)
plt.plot(x_exact, w_exact, color='blue', linestyle='dashed')
Expand Down
4 changes: 2 additions & 2 deletions docs/source/examples/external_sp.py
Expand Up @@ -9,8 +9,8 @@
objective = y

constraints = [ExternalConstraint(x, y),
x <= np.pi/2.,
x >= np.pi/4.,
x <= np.pi/2,
x >= np.pi/4,
]

m = Model(objective, constraints)
Expand Down
4 changes: 2 additions & 2 deletions docs/source/examples/sin_approx_example.py
Expand Up @@ -9,8 +9,8 @@
objective = y

constraints = [y >= x,
x <= np.pi/2.,
x >= np.pi/4.,
x <= np.pi/2,
x >= np.pi/4,
]

m = Model(objective, constraints)
Expand Down
4 changes: 2 additions & 2 deletions docs/source/examples/sp_to_gp_sweep.py
Expand Up @@ -22,7 +22,7 @@ def SimPleAC():
tau = Variable("\\tau", 0.12, "-", "airfoil thickness to chord ratio")
W_W_coeff1 = Variable("W_{W_{coeff1}}", 2e-5, "1/m",
"wing weight coefficent 1") # 12e-5 originally
W_W_coeff2 = Variable("W_{W_{coeff2}}", 60., "Pa",
W_W_coeff2 = Variable("W_{W_{coeff2}}", 60, "Pa",
"wing weight coefficent 2")

# Dimensional constants
Expand Down Expand Up @@ -91,7 +91,7 @@ def SimPleAC():
# Wing weight model
constraints += [
W_w_surf >= W_W_coeff2 * S,
W_w_strc**2. >= W_W_coeff1**2/tau**2 * N_ult**2*A**3*(V_f_fuse*g*rho_f
W_w_strc**2 >= W_W_coeff1**2/tau**2 * N_ult**2*A**3*(V_f_fuse*g*rho_f
+ W_0)*W*S,
W_w >= W_w_surf + W_w_strc]

Expand Down
2 changes: 1 addition & 1 deletion docs/source/ipynb/BEMT.ipynb
Expand Up @@ -389,7 +389,7 @@
" r: array([[ 0.21773709, 0.58869686, 0.74669362, 0.87573369, 1. ]]),\n",
" dC_T: array([[ 0.00373315, 0.00373315, 0.00373315, 0.00373315, 0.00373315]]),\n",
" dC_P: array([[ 0.00037037, 0.00035362, 0.00035362, 0.00035362, 0.00031661]]),\n",
" \\xi: array([[ 2000., 2000., 2000., 2000., 2000.]]),\n",
" \\xi: array([[ 2000, 2000, 2000, 2000, 2000]]),\n",
" P: array([ 43582.46513974]),\n",
" \\Omega: array([ 55.55730973]),\n",
" A: array([ 201.06192911]),\n",
Expand Down
2 changes: 1 addition & 1 deletion docs/source/ipynb/Fuel/Fuel.ipynb
Expand Up @@ -231,7 +231,7 @@
" p >= 2.2,\n",
" tau <= 0.25,\n",
" M_rbar >= W_tw*A*p/(24*gpkit.units.N),\n",
" .92**2/2.*w*tau**2*t_cap >= I_cap * wunits + .92*w*tau*t_cap**2,\n",
" .92**2/2*w*tau**2*t_cap >= I_cap * wunits + .92*w*tau*t_cap**2,\n",
" 8 >= N_lift*M_rbar*A*q**2*tau/S/I_cap/sigma_max * munits,\n",
" 12 >= A*W_tw*N_lift*q**2/tau/S/t_web/sigma_maxshear,\n",
" nu**3.94 >= .86*p**-2.38 + .14*p**0.56,\n",
Expand Down
2 changes: 1 addition & 1 deletion docs/source/ipynb/Fuel/Fuel.rst
Expand Up @@ -161,7 +161,7 @@ return and sprint flights.
p >= 1.9,
tau <= 0.15,
M_rbar >= W_tw*A*p/(24*gpkit.units.N),
.92**2/2.*w*tau**2*t_cap >= I_cap * gpkit.units.m**-4 + .92*w*tau*t_cap**2,
.92**2/2*w*tau**2*t_cap >= I_cap * gpkit.units.m**-4 + .92*w*tau*t_cap**2,
8 >= N_lift*M_rbar*A*q**2*tau/S/I_cap/sigma_max * gpkit.units.parse_expression('Pa*m**6'),
12 >= A*W_tw*N_lift*q**2/tau/S/t_web/sigma_maxshear,
nu**3.94 >= .86*p**-2.38 + .14*p**0.56,
Expand Down
4 changes: 2 additions & 2 deletions gpkit/build.py
Expand Up @@ -86,7 +86,7 @@ class MosekCLI(SolverBackend):
name = "mosek_cli"

def look(self): # pylint: disable=too-many-return-statements
"Looks in default install locations for a mosek before 9."
"Looks in default install locations for a mosek before version 9."
already_in_path = self.run()
if already_in_path:
return already_in_path
Expand Down Expand Up @@ -156,7 +156,7 @@ class MosekConif(SolverBackend):
name = "mosek_conif"

def look(self):
"Attempts to import mosek, version >= 9."
"Attempts to import a mosek supporting exponential cones."
try:
log("# Trying to import mosek...")
import mosek
Expand Down
10 changes: 4 additions & 6 deletions gpkit/constraints/gp.py
Expand Up @@ -401,19 +401,17 @@ def _almost_equal(num1, num2):
for mi in self.m_idxs[1:]:
if primal_exp_vals[mi].sum() > 1 + tol:
raise Infeasible("Primal solution violates constraint: %s is "
"greater than 1." % primal_exp_vals[mi].sum())
"greater than 1" % primal_exp_vals[mi].sum())

# check dual sol #
# note: follows dual formulation in section 3.1 of
# http://web.mit.edu/~whoburg/www/papers/hoburg_phd_thesis.pdf
if not _almost_equal(self.nu_by_posy[0].sum(), 1.):
if not _almost_equal(self.nu_by_posy[0].sum(), 1):
raise Infeasible("Dual variables associated with objective sum"
" to %s, not 1." % self.nu_by_posy[0].sum())
" to %s, not 1" % self.nu_by_posy[0].sum())
if any(nu < 0):
minnu = min(nu)
if minnu > -tol/1000.: # HACK, see issue 528
print("Allowing negative dual variables up to %s." % minnu)
else:
if minnu < -tol/1000:
raise Infeasible("Dual solution has negative entries as"
" large as %s." % minnu)
if any(np.abs(A.T.dot(nu)) > tol):
Expand Down
2 changes: 1 addition & 1 deletion gpkit/tests/t_constraints.py
Expand Up @@ -130,7 +130,7 @@ def test_sub_tol(self):
y = Variable('y')
z = Variable('z')
PosynomialInequality.feastol = 1e-5
m = Model(z, [x == z, x >= y], {x: 1., y: 1.0001})
m = Model(z, [x == z, x >= y], {x: 1, y: 1.0001})
self.assertRaises(PrimalInfeasible, m.solve, verbosity=0)
PosynomialInequality.feastol = 1e-3
self.assertEqual(m.substitutions('x'), m.solve(verbosity=0)('x'))
Expand Down
14 changes: 7 additions & 7 deletions gpkit/tests/t_nomials.py
Expand Up @@ -42,7 +42,7 @@ def test_init(self):

# non-positive c raises
self.assertRaises(InvalidPosynomial, Monomial, -2)
self.assertRaises(InvalidPosynomial, Monomial, -1.)
self.assertRaises(InvalidPosynomial, Monomial, -1)
self.assertRaises(InvalidPosynomial, Monomial, 0)
self.assertRaises(InvalidPosynomial, Monomial, 0.0)

Expand Down Expand Up @@ -191,9 +191,9 @@ def test_pow(self):

def test_numerical_precision(self):
"not sure what to test here, placeholder for now"
c1, c2 = 1/700., 123e8
c1, c2 = 1/700, 123e8
m1 = Monomial({"x": 2, "y": 1}, c1)
m2 = Monomial({"y": -1, "z": 3/2.}, c2)
m2 = Monomial({"y": -1, "z": 3/2}, c2)
self.assertEqual(math.log((m1**4 * m2**3).c), # pylint: disable=no-member
4*math.log(c1) + 3*math.log(c2))

Expand Down Expand Up @@ -239,7 +239,7 @@ def test_chop(self):
x = Variable("x")
y = Variable("y")
with SignomialsEnabled():
c = x + 5.*y**2 - 0.2*x*y**0.78
c = x + 5*y**2 - 0.2*x*y**0.78
monomials = c.chop()
with self.assertRaises(InvalidPosynomial):
c.chop()
Expand Down Expand Up @@ -373,9 +373,9 @@ def test_integer_division(self):
x = Variable("x")
y = Variable("y")
p = 4*x + y
self.assertEqual(p/3, p/3.)
equiv1 = all((p/3).cs == [1./3., 4./3.])
equiv2 = all((p/3).cs == [4./3., 1./3.])
self.assertEqual(p/3, p/3)
equiv1 = all((p/3).cs == [1/3, 4/3])
equiv2 = all((p/3).cs == [4/3, 1/3])
self.assertTrue(equiv1 or equiv2)

def test_diff(self):
Expand Down
2 changes: 1 addition & 1 deletion gpkit/tests/t_solution_array.py
Expand Up @@ -100,7 +100,7 @@ class TestResultsTable(unittest.TestCase):
def test_nan_printing(self):
"""Test that solution prints when it contains nans"""
x = VarKey(name='x')
data = {x: np.array([np.nan, 1., 1., 1., 1.])}
data = {x: np.array([np.nan, 1, 1, 1, 1])}
title = "Free variables"
printstr = "\n".join(var_table(data, title))
self.assertTrue(" - " in printstr) # nan is printed as " - "
Expand Down
2 changes: 1 addition & 1 deletion gpkit/tests/t_sub.py
Expand Up @@ -223,7 +223,7 @@ def test_calcconst(self):
m.substitutions.update({t_day: ("sweep", [6, 8, 9, 13])})
sol = m.solve(verbosity=0)
npt.assert_allclose(sol["sensitivities"]["constants"][t_day],
[-1./3, -0.5, -0.6, +1], 1e-5)
[-1/3, -0.5, -0.6, +1], 1e-5)
self.assertEqual(len(sol["cost"]), 4)
npt.assert_allclose([float(l) for l in
(sol(t_day) + sol(t_night))/gpkit.ureg.hours], 24)
Expand Down
18 changes: 9 additions & 9 deletions gpkit/tests/t_tools.py
Expand Up @@ -89,7 +89,7 @@ def test_dual_objective(self):
eqns = [L >= 1, W >= 1,
L*W == 10]
N = 4
ws = Variable("w_{CO}", ("sweep", np.linspace(1./N, 1-1./N, N)), "-")
ws = Variable("w_{CO}", ("sweep", np.linspace(1/N, 1-1/N, N)), "-")
w_s = Variable("v_{CO}", lambda c: 1-c[ws], "-")
obj = ws*(L+W) + w_s*(W**-1 * L**-3)
m = Model(obj, eqns)
Expand All @@ -102,24 +102,24 @@ def test_te_exp_minus1(self):
"""Test Taylor expansion of e^x - 1"""
x = Variable('x')
self.assertEqual(te_exp_minus1(x, 1), x)
self.assertEqual(te_exp_minus1(x, 3), x + x**2/2. + x**3/6.)
self.assertEqual(te_exp_minus1(x, 3), x + x**2/2 + x**3/6)
self.assertEqual(te_exp_minus1(x, 0), 0)
# make sure x was not modified
self.assertEqual(x, Variable('x'))
# try for VectorVariable too
y = VectorVariable(3, 'y')
self.assertEqual(te_exp_minus1(y, 1), y)
self.assertEqual(te_exp_minus1(y, 3), y + y**2/2. + y**3/6.)
self.assertEqual(te_exp_minus1(y, 3), y + y**2/2 + y**3/6)
self.assertEqual(te_exp_minus1(y, 0), 0)
# make sure y was not modified
self.assertEqual(y, VectorVariable(3, 'y'))

def test_te_secant(self):
"Test Taylor expansion of secant(var)"
x = Variable('x')
self.assertEqual(te_secant(x, 1), 1 + x**2/2.)
self.assertEqual(te_secant(x, 1), 1 + x**2/2)
a = te_secant(x, 2)
b = 1 + x**2/2. + 5*x**4/24.
b = 1 + x**2/2 + 5*x**4/24
self.assertTrue(all([abs(val) <= 1e-10
for val in (a.hmap - b.hmap).values()])) # pylint:disable=no-member
self.assertEqual(te_secant(x, 0), 1)
Expand All @@ -128,23 +128,23 @@ def test_te_secant(self):
# try for VectorVariable too
y = VectorVariable(3, 'y')
self.assertTrue(te_secant(y, 0) == 1) # truthy bc monomial constraint
self.assertTrue(all(te_secant(y, 1) == 1 + y**2/2.))
self.assertTrue(all(te_secant(y, 2) == 1 + y**2/2. + 5*y**4/24.))
self.assertTrue(all(te_secant(y, 1) == 1 + y**2/2))
self.assertTrue(all(te_secant(y, 2) == 1 + y**2/2 + 5*y**4/24))
# make sure y was not modified
self.assertEqual(y, VectorVariable(3, 'y'))

def test_te_tangent(self):
"Test Taylor expansion of tangent(var)"
x = Variable('x')
self.assertEqual(te_tangent(x, 1), x)
self.assertEqual(te_tangent(x, 3), x + x**3/3. + 2*x**5/15.)
self.assertEqual(te_tangent(x, 3), x + x**3/3 + 2*x**5/15)
self.assertEqual(te_tangent(x, 0), 0)
# make sure x was not modified
self.assertEqual(x, Variable('x'))
# try for VectorVariable too
y = VectorVariable(3, 'y')
self.assertEqual(te_tangent(y, 1), y)
self.assertEqual(te_tangent(y, 3), y + y**3/3. + 2*y**5/15.)
self.assertEqual(te_tangent(y, 3), y + y**3/3 + 2*y**5/15)
self.assertEqual(te_tangent(y, 0), 0)
# make sure y was not modified
self.assertEqual(y, VectorVariable(3, 'y'))
Expand Down
4 changes: 2 additions & 2 deletions gpkit/tests/t_vars.py
Expand Up @@ -52,7 +52,7 @@ def test_ast(self): # pylint: disable=too-many-statements
# print(w >= x) # TODO: this always prints the vector on the left
self.assertEqual(str(3*(x + y)*z), "3·(x[:] + y[:])·z[:]")
nni = 3
ii = np.tile(np.arange(1., nni+1.), a.shape[1:]+(1,)).T
ii = np.tile(np.arange(1, nni+1), a.shape[1:]+(1,)).T
self.assertEqual(str(w*NomialArray(ii)/nni)[:4], "w·[[")
self.assertEqual(str(w*NomialArray(ii)/nni)[-4:], "]]/3")
self.assertEqual(str(NomialArray(ii)*w/nni)[:2], "[[")
Expand Down Expand Up @@ -221,7 +221,7 @@ def test_init(self):

# test inspired by issue 137
N = 20
x_arr = np.arange(0, 5., 5./N) + 1e-6
x_arr = np.arange(0, 5, 5/N) + 1e-6
x = VectorVariable(N, 'x', x_arr, 'm', "Beam Location")

def test_constraint_creation_units(self):
Expand Down
2 changes: 1 addition & 1 deletion gpkit/tools/autosweep.py
Expand Up @@ -37,7 +37,7 @@ class BinarySweepTree: # pylint: disable=too-many-instance-attributes

def __init__(self, bounds, sols, sweptvar, costposy):
if len(bounds) != 2:
raise ValueError("bounds must be of length 2.")
raise ValueError("bounds must be of length 2")
if bounds[1] <= bounds[0]:
raise ValueError("bounds[0] must be smaller than bounds[1].")
self.bounds = bounds
Expand Down
32 changes: 16 additions & 16 deletions gpkit/tools/tools.py
Expand Up @@ -60,7 +60,7 @@ def te_secant(var, nterm):
E2n = np.append(E2n, E2n_add)

res = 1
factorial_denom = 1.
factorial_denom = 1
for i in range(1, nterm + 1):
factorial_denom *= ((2*i)*(2*i-1))
res = res + var**(2*i) * E2n[i-1] / factorial_denom
Expand All @@ -87,21 +87,21 @@ def te_tangent(var, nterm):
" 15 terms")

# The first 15 Bernoulli Numbers
B2n = np.asarray([1/6.,
-1/30.,
1/42.,
-1/30.,
5/66.,
-691/2730.,
7/6.,
-3617/510.,
43867/798.,
-174611/330.,
854513/138.,
-236364091/2730.,
8553103/6.,
-23749461029/870.,
8615841276005/14322.])
B2n = np.asarray([1/6,
-1/30,
1/42,
-1/30,
5/66,
-691/2730,
7/6,
-3617/510,
43867/798,
-174611/330,
854513/138,
-236364091/2730,
8553103/6,
-23749461029/870,
8615841276005/14322])

res = 0
factorial_denom = 1
Expand Down

0 comments on commit d0ad1be

Please sign in to comment.