How to use _combine_constraints method in pandera

Best Python code snippet using pandera_python

truncation.py

Source:truncation.py Github

copy

Full Screen

...185 if chi_max is not None:186 # keep at most chi_max values187 good2 = np.zeros(len(piv), dtype=np.bool_)188 good2[-chi_max:] = True189 good = _combine_constraints(good, good2, "chi_max")190 if chi_min is not None and chi_min > 1:191 # keep at most chi_max values192 good2 = np.ones(len(piv), dtype=np.bool_)193 good2[-chi_min + 1:] = False194 good = _combine_constraints(good, good2, "chi_min")195 if deg_tol:196 # don't cut between values (cut-1, cut) with ``log(S[cut]/S[cut-1]) < deg_tol``197 # this is equivalent to198 # ``(S[cut] - S[cut-1])/S[cut-1] < exp(deg_tol) - 1 = deg_tol + O(deg_tol^2)``199 good2 = np.empty(len(piv), np.bool_)200 good2[0] = True201 good2[1:] = np.greater_equal(logS[1:] - logS[:-1], deg_tol)202 good = _combine_constraints(good, good2, "degeneracy_tol")203 if svd_min is not None:204 # keep only values S[i] >= svd_min205 good2 = np.greater_equal(logS, np.log(svd_min))206 good = _combine_constraints(good, good2, "svd_min")207 if trunc_cut is not None:208 good2 = (np.cumsum(S[piv]**2) > trunc_cut * trunc_cut)209 good = _combine_constraints(good, good2, "trunc_cut")210 cut = np.nonzero(good)[0][0] # smallest possible cut: keep as many S as allowed211 mask = np.zeros(len(S), dtype=np.bool_)212 np.put(mask, piv[cut:], True)213 norm_new = np.linalg.norm(S[mask])214 return mask, norm_new, TruncationError.from_S(S[np.logical_not(mask)]),215def svd_theta(theta, trunc_par, qtotal_LR=[None, None], inner_labels=['vR', 'vL']):216 """Performs SVD of a matrix `theta` (= the wavefunction) and truncates it.217 Perform a singular value decomposition (SVD) with :func:`~tenpy.linalg.np_conserved.svd`218 and truncates with :func:`truncate`.219 The result is an approximation220 ``theta ~= tensordot(U.scale_axis(S*renormalization, 1), VH, axes=1)``221 Parameters222 ----------223 theta : :class:`~tenpy.linalg.np_conserved.Array`, shape ``(M, N)``224 The matrix, on which the singular value decomposition (SVD) is performed.225 Usually, `theta` represents the wavefunction, such that the SVD is a Schmidt decomposition.226 trunc_par : dict227 truncation parameters as described in :func:`truncate`.228 qtotalLR : (charges, charges)229 The total charges for the returned `U` and `VH`.230 inner_labels : (string, string)231 Labels for the `U` and `VH` on the newly-created bond.232 Returns233 -------234 U : :class:`~tenpy.linalg.np_conserved.Array`235 Matrix with left singular vectors as columns.236 Shape ``(M, M)`` or ``(M, K)`` depending on `full_matrices`.237 S : 1D ndarray238 The singluar values of the array.239 If no `cutoff` is given, it has lenght ``min(M, N)``.240 Normalized to ``np.linalg.norm(S)==1``.241 VH : :class:`~tenpy.linalg.np_conserved.Array`242 Matrix with right singular vectors as rows.243 Shape ``(N, N)`` or ``(K, N)`` depending on `full_matrices`.244 err : :class:`TruncationError`245 The truncation error introduced.246 renormalization : float247 Factor, by which S was renormalized.248 """249 U, S, VH = npc.svd(theta,250 full_matrices=False,251 compute_uv=True,252 qtotal_LR=qtotal_LR,253 inner_labels=inner_labels)254 renormalization = np.linalg.norm(S)255 S = S / renormalization256 piv, new_norm, err = truncate(S, trunc_par)257 new_len_S = np.sum(piv, dtype=np.int_)258 if new_len_S * 100 < len(S) and (trunc_par['chi_max'] is None259 or new_len_S != trunc_par['chi_max']):260 msg = "Catastrophic reduction in chi: {0:d} -> {1:d}".format(len(S), new_len_S)261 # NANs are excluded in npc.svd262 UHU = npc.tensordot(U.conj(), U, axes=[[0], [0]])263 msg += " |U^d U - 1| = {0:f}".format(npc.norm(UHU - npc.eye_like(UHU)))264 VHV = npc.tensordot(VH, VH.conj(), axes=[[1], [1]])265 msg += " |V V - 1| = {0:f}".format(npc.norm(VHV - npc.eye_like(VHV)))266 warnings.warn(msg, stacklevel=2)267 S = S[piv] / new_norm268 renormalization *= new_norm269 U.iproject(piv, axes=1) # U = U[:, piv]270 VH.iproject(piv, axes=0) # VH = VH[piv, :]271 return U, S, VH, err, renormalization272def _combine_constraints(good1, good2, warn):273 """return logical_and(good1, good2) if there remains at least one `True` entry.274 Otherwise print a warning and return just `good1`.275 """276 res = np.logical_and(good1, good2)277 if np.any(res):278 return res279 warnings.warn("truncation: can't satisfy constraint for " + warn, stacklevel=3)...

Full Screen

Full Screen

test_translator.py

Source:test_translator.py Github

copy

Full Screen

...20 m = three_parameter_problem.m21 assert y.size == m22def test_translate_equality_constraints(three_parameter_problem):23 translator = Translator(three_parameter_problem)24 translated_eqcon = translator._combine_constraints("eq")25 # Get the Jacobian of the translated eqcon26 x = np.zeros(translated_eqcon.dim)27 jac1 = translated_eqcon.jac(x)[:, :-1]28 # Get the Jacobian of the original constraint29 x1 = np.zeros(three_parameter_problem.shape[0])30 x2 = np.zeros(three_parameter_problem.shape[1])31 jac0 = three_parameter_problem.constraints[0].jac(x1, x2)32 assert np.isclose(jac1, jac0).all()33def test_translate_inequality_constraint(three_parameter_problem):34 translator = Translator(three_parameter_problem)35 translated_incon = translator._combine_constraints("ineq")36 n1 = three_parameter_problem.shape[0]37 # Get Jacobian of the original inequality constraint38 y = np.zeros(three_parameter_problem.constraints[1].dim)39 jac0 = three_parameter_problem.constraints[1].jac(y)40 # Get corresponding part of the Jacobian of the translated inequality constraint.41 x = np.zeros(translated_incon.dim)42 jac1 = translated_incon.jac(x)43 assert np.isclose(jac1[:, n1:-1], jac0).all()44def test_translate_unconstrained(unconstrained_problem):45 translator = Translator(unconstrained_problem)46 eqcon = translator._combine_constraints("eq")47 incon = translator._combine_constraints("ineq")48 assert isinstance(eqcon, NullConstraint)...

Full Screen

Full Screen

Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run pandera automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful