use "is None" to check for default args

numpy.any(numpy.equal(x, None)) is more general, because
`numpy.array(None) is not None`, but checking for that doesn't make much
sense if you're already type-checking
This commit is contained in:
Jan Petykiewicz 2022-10-04 17:27:44 -07:00
parent 52df24ad98
commit 4e240988c9
5 changed files with 25 additions and 25 deletions

View File

@ -25,7 +25,7 @@ def power_iteration(
Returns: Returns:
(Largest-magnitude eigenvalue, Corresponding eigenvector estimate) (Largest-magnitude eigenvalue, Corresponding eigenvector estimate)
""" """
if numpy.any(numpy.equal(guess_vector, None)): if guess_vector is None:
v = numpy.random.rand(operator.shape[0]) + 1j * numpy.random.rand(operator.shape[0]) v = numpy.random.rand(operator.shape[0]) + 1j * numpy.random.rand(operator.shape[0])
else: else:
v = guess_vector v = guess_vector

View File

@ -45,7 +45,7 @@ def e_full(
curls = ch(mu * ce(e)) # type: ignore # mu = None ok because we don't return the function curls = ch(mu * ce(e)) # type: ignore # mu = None ok because we don't return the function
return curls - omega ** 2 * epsilon * e return curls - omega ** 2 * epsilon * e
if numpy.any(numpy.equal(mu, None)): if mu is None:
return op_1 return op_1
else: else:
return op_mu return op_mu
@ -82,7 +82,7 @@ def eh_full(
return (ch(h) - 1j * omega * epsilon * e, return (ch(h) - 1j * omega * epsilon * e,
ce(e) + 1j * omega * mu * h) # type: ignore # mu=None ok ce(e) + 1j * omega * mu * h) # type: ignore # mu=None ok
if numpy.any(numpy.equal(mu, None)): if mu is None:
return op_1 return op_1
else: else:
return op_mu return op_mu
@ -114,7 +114,7 @@ def e2h(
def e2h_mu(e: cfdfield_t) -> cfdfield_t: def e2h_mu(e: cfdfield_t) -> cfdfield_t:
return ce(e) / (-1j * omega * mu) # type: ignore # mu=None ok return ce(e) / (-1j * omega * mu) # type: ignore # mu=None ok
if numpy.any(numpy.equal(mu, None)): if mu is None:
return e2h_1_1 return e2h_1_1
else: else:
return e2h_mu return e2h_mu
@ -149,7 +149,7 @@ def m2j(
J = ch(m) / (-1j * omega) J = ch(m) / (-1j * omega)
return J return J
if numpy.any(numpy.equal(mu, None)): if mu is None:
return m2j_1 return m2j_1
else: else:
return m2j_mu return m2j_mu

View File

@ -77,18 +77,18 @@ def e_full(
ch = curl_back(dxes[1]) ch = curl_back(dxes[1])
ce = curl_forward(dxes[0]) ce = curl_forward(dxes[0])
if numpy.any(numpy.equal(pec, None)): if pec is None:
pe = sparse.eye(epsilon.size) pe = sparse.eye(epsilon.size)
else: else:
pe = sparse.diags(numpy.where(pec, 0, 1)) # Set pe to (not PEC) pe = sparse.diags(numpy.where(pec, 0, 1)) # Set pe to (not PEC)
if numpy.any(numpy.equal(pmc, None)): if pmc is None:
pm = sparse.eye(epsilon.size) pm = sparse.eye(epsilon.size)
else: else:
pm = sparse.diags(numpy.where(pmc, 0, 1)) # set pm to (not PMC) pm = sparse.diags(numpy.where(pmc, 0, 1)) # set pm to (not PMC)
e = sparse.diags(epsilon) e = sparse.diags(epsilon)
if numpy.any(numpy.equal(mu, None)): if mu is None:
m_div = sparse.eye(epsilon.size) m_div = sparse.eye(epsilon.size)
else: else:
m_div = sparse.diags(1 / mu) m_div = sparse.diags(1 / mu)
@ -161,12 +161,12 @@ def h_full(
ch = curl_back(dxes[1]) ch = curl_back(dxes[1])
ce = curl_forward(dxes[0]) ce = curl_forward(dxes[0])
if numpy.any(numpy.equal(pec, None)): if pec is None:
pe = sparse.eye(epsilon.size) pe = sparse.eye(epsilon.size)
else: else:
pe = sparse.diags(numpy.where(pec, 0, 1)) # set pe to (not PEC) pe = sparse.diags(numpy.where(pec, 0, 1)) # set pe to (not PEC)
if numpy.any(numpy.equal(pmc, None)): if pmc is None:
pm = sparse.eye(epsilon.size) pm = sparse.eye(epsilon.size)
else: else:
pm = sparse.diags(numpy.where(pmc, 0, 1)) # Set pe to (not PMC) pm = sparse.diags(numpy.where(pmc, 0, 1)) # Set pe to (not PMC)
@ -227,19 +227,19 @@ def eh_full(
Returns: Returns:
Sparse matrix containing the wave operator. Sparse matrix containing the wave operator.
""" """
if numpy.any(numpy.equal(pec, None)): if pec is None:
pe = sparse.eye(epsilon.size) pe = sparse.eye(epsilon.size)
else: else:
pe = sparse.diags(numpy.where(pec, 0, 1)) # set pe to (not PEC) pe = sparse.diags(numpy.where(pec, 0, 1)) # set pe to (not PEC)
if numpy.any(numpy.equal(pmc, None)): if pmc is None:
pm = sparse.eye(epsilon.size) pm = sparse.eye(epsilon.size)
else: else:
pm = sparse.diags(numpy.where(pmc, 0, 1)) # set pm to (not PMC) pm = sparse.diags(numpy.where(pmc, 0, 1)) # set pm to (not PMC)
iwe = pe @ (1j * omega * sparse.diags(epsilon)) @ pe iwe = pe @ (1j * omega * sparse.diags(epsilon)) @ pe
iwm = 1j * omega iwm = 1j * omega
if not numpy.any(numpy.equal(mu, None)): if mu is not None:
iwm *= sparse.diags(mu) iwm *= sparse.diags(mu)
iwm = pm @ iwm @ pm iwm = pm @ iwm @ pm
@ -274,10 +274,10 @@ def e2h(
""" """
op = curl_forward(dxes[0]) / (-1j * omega) op = curl_forward(dxes[0]) / (-1j * omega)
if not numpy.any(numpy.equal(mu, None)): if mu is not None:
op = sparse.diags(1 / mu) @ op op = sparse.diags(1 / mu) @ op
if not numpy.any(numpy.equal(pmc, None)): if pmc is not None:
op = sparse.diags(numpy.where(pmc, 0, 1)) @ op op = sparse.diags(numpy.where(pmc, 0, 1)) @ op
return op return op
@ -302,7 +302,7 @@ def m2j(
""" """
op = curl_back(dxes[1]) / (1j * omega) op = curl_back(dxes[1]) / (1j * omega)
if not numpy.any(numpy.equal(mu, None)): if mu is not None:
op = op @ sparse.diags(1 / mu) op = op @ sparse.diags(1 / mu)
return op return op

View File

@ -239,7 +239,7 @@ def operator_e(
Returns: Returns:
Sparse matrix representation of the operator. Sparse matrix representation of the operator.
""" """
if numpy.any(numpy.equal(mu, None)): if mu is None:
mu = numpy.ones_like(epsilon) mu = numpy.ones_like(epsilon)
Dfx, Dfy = deriv_forward(dxes[0]) Dfx, Dfy = deriv_forward(dxes[0])
@ -306,7 +306,7 @@ def operator_h(
Returns: Returns:
Sparse matrix representation of the operator. Sparse matrix representation of the operator.
""" """
if numpy.any(numpy.equal(mu, None)): if mu is None:
mu = numpy.ones_like(epsilon) mu = numpy.ones_like(epsilon)
Dfx, Dfy = deriv_forward(dxes[0]) Dfx, Dfy = deriv_forward(dxes[0])
@ -513,7 +513,7 @@ def hxy2h(
Dfx, Dfy = deriv_forward(dxes[0]) Dfx, Dfy = deriv_forward(dxes[0])
hxy2hz = sparse.hstack((Dfx, Dfy)) / (1j * wavenumber) hxy2hz = sparse.hstack((Dfx, Dfy)) / (1j * wavenumber)
if not numpy.any(numpy.equal(mu, None)): if mu is not None:
mu_parts = numpy.split(mu, 3) mu_parts = numpy.split(mu, 3)
mu_xy = sparse.diags(numpy.hstack((mu_parts[0], mu_parts[1]))) mu_xy = sparse.diags(numpy.hstack((mu_parts[0], mu_parts[1])))
mu_z_inv = sparse.diags(1 / mu_parts[2]) mu_z_inv = sparse.diags(1 / mu_parts[2])
@ -547,7 +547,7 @@ def exy2e(
Dbx, Dby = deriv_back(dxes[1]) Dbx, Dby = deriv_back(dxes[1])
exy2ez = sparse.hstack((Dbx, Dby)) / (1j * wavenumber) exy2ez = sparse.hstack((Dbx, Dby)) / (1j * wavenumber)
if not numpy.any(numpy.equal(epsilon, None)): if epsilon is not None:
epsilon_parts = numpy.split(epsilon, 3) epsilon_parts = numpy.split(epsilon, 3)
epsilon_xy = sparse.diags(numpy.hstack((epsilon_parts[0], epsilon_parts[1]))) epsilon_xy = sparse.diags(numpy.hstack((epsilon_parts[0], epsilon_parts[1])))
epsilon_z_inv = sparse.diags(1 / epsilon_parts[2]) epsilon_z_inv = sparse.diags(1 / epsilon_parts[2])
@ -580,7 +580,7 @@ def e2h(
Sparse matrix representation of the operator. Sparse matrix representation of the operator.
""" """
op = curl_e(wavenumber, dxes) / (-1j * omega) op = curl_e(wavenumber, dxes) / (-1j * omega)
if not numpy.any(numpy.equal(mu, None)): if mu is not None:
op = sparse.diags(1 / mu) @ op op = sparse.diags(1 / mu) @ op
return op return op
@ -675,7 +675,7 @@ def h_err(
eps_inv = sparse.diags(1 / epsilon) eps_inv = sparse.diags(1 / epsilon)
if numpy.any(numpy.equal(mu, None)): if mu is None:
op = ce @ eps_inv @ ch @ h - omega ** 2 * h op = ce @ eps_inv @ ch @ h - omega ** 2 * h
else: else:
op = ce @ eps_inv @ ch @ h - omega ** 2 * (mu * h) op = ce @ eps_inv @ ch @ h - omega ** 2 * (mu * h)
@ -708,7 +708,7 @@ def e_err(
ce = curl_e(wavenumber, dxes) ce = curl_e(wavenumber, dxes)
ch = curl_h(wavenumber, dxes) ch = curl_h(wavenumber, dxes)
if numpy.any(numpy.equal(mu, None)): if mu is None:
op = ch @ ce @ e - omega ** 2 * (epsilon * e) op = ch @ ce @ e - omega ** 2 * (epsilon * e)
else: else:
mu_inv = sparse.diags(1 / mu) mu_inv = sparse.diags(1 / mu)

View File

@ -40,7 +40,7 @@ def vec(f: Union[fdfield_t, cfdfield_t, ArrayLike, None]) -> Union[vfdfield_t, v
Returns: Returns:
1D ndarray containing the linearized field (or `None`) 1D ndarray containing the linearized field (or `None`)
""" """
if numpy.any(numpy.equal(f, None)): if f is None:
return None return None
return numpy.ravel(f, order='C') return numpy.ravel(f, order='C')
@ -72,7 +72,7 @@ def unvec(v: Union[vfdfield_t, vcfdfield_t, None], shape: Sequence[int]) -> Unio
Returns: Returns:
`[f_x, f_y, f_z]` where each `f_` is a `len(shape)` dimensional ndarray (or `None`) `[f_x, f_y, f_z]` where each `f_` is a `len(shape)` dimensional ndarray (or `None`)
""" """
if numpy.any(numpy.equal(v, None)): if v is None:
return None return None
return v.reshape((3, *shape), order='C') return v.reshape((3, *shape), order='C')