def sum_1(val, rowIndices, n, p): S = np.zeros(n, dtype = val.dtype) for i in range(n): left = rowIndices[i] right = rowIndices[i+1] S[i] += _sum(val[left:right]) return S
def is_all_finite(arr): r"""Check if the array values are all finite. Args: arr (array_like): sequence of values. Returns: bool: ``True`` if values are all finite. """ return isfinite(_sum(asarray(arr)))
def mean_1(val, rowIndices, n, p): A = zeros(n, dtype = val.dtype) for i in range(n): left = rowIndices[i] right = rowIndices[i+1] A[i] += _sum(val[left:right]) if A[i] > 0: A[i] /= p return A
def cost(rv): """compute cost from a 1-d array of model parameters, where: cost = | sum( infeasibility ) | """ # converting rv to scenario points = scenario() points.load(rv, pts) # calculate infeasibility Rv = graphical_distance(model, points, ytol=cutoff, ipop=ipop, \ imax=imax, **kwds) v = infeasibility(Rv, cutoff) # converting v to E return _sum(v) #XXX: abs ?
def smallestDivisor(self, nums: List[int], threshold: int) -> int: lower, upper = 1, max(nums) nums_array = array(nums) while lower < upper: mid = (lower + upper) >> 1 res = _sum(ceil(nums_array / mid)) if res > threshold: lower = mid + 1 else: upper = mid return lower
def lu_slogdet(LU): r"""Natural logarithm of a LU decomposition. Args: LU (tuple): LU decomposition. Returns: tuple: sign and log-determinant. """ LU = (asarray(LU[0], float), asarray(LU[1], float)) adet = _sum(log(_abs(LU[0].diagonal()))) s = prod(sign(LU[0].diagonal())) nrows_exchange = LU[1].size - _sum( LU[1] == arange(LU[1].size, dtype="int32")) odd = nrows_exchange % 2 == 1 if odd: s *= -1.0 return (s, adet)
def _calculate_g_raw_at_t(self, time): """ Calculates the Gauss Coefficients in raw ordering given the parameters calculated by inverval() and _bspline(). Parameters ---------- time: Returns ------- Gauss Coefficients for a particular time in raw ordering. """ gt = self.gt b = self.bspline(time) i = self._interval(time) bo = self.bspl_order g_raw = _sum(b[i:i+bo]*gt[:, i:i+bo], axis=1) return g_raw
def _calculate_SAgh_raw_at_t(self, time): """ Calculates the second time derivatives of Gauss Coefficients in raw ordering given the parameters calculated by inverval() and _bspline(). Parameters ---------- time: date in years to calculate SAg_raw Returns ------- SAg_raw: Second time derivatives of Gauss Coefficients for a particular time in raw ordering. """ gt = self.gt SAb = self.bspline.d2(time) i = self._interval(time) bo = self.bspl_order SAg_raw = _sum(SAb[i:i+bo]*gt[:, i:i+bo], axis=1) return SAg_raw
def trace2(A, B): r"""Trace of :math:`\mathrm A \mathrm B`. Args: A (array_like): Left-hand side. B (array_like): Right-hand side. Returns: float: Trace of :math:`\mathrm A \mathrm B`. """ A = asarray(A, float) B = asarray(B, float) layout_error = "Wrong matrix layout." if not (len(A.shape) == 2 and len(B.shape) == 2): raise ValueError(layout_error) if not (A.shape[1] == B.shape[0] and A.shape[0] == B.shape[1]): raise ValueError(layout_error) return _sum(A.T * B)
addlocker_write_running(outputHEAD+betanum2betastr(0)+'.h5') nextsave_quit = False while True: if beta >= _max(sim_tdmrg['save_list']): output = outputHEAD+'{0}.h5'.format(betanum2betastr(beta)) print("It shouldn't start with BETA MAX") break if _max(sim_tdmrg['dw_one_serie'])>sim_tdmrg['stop_dw_limit']: nextsave_quit = True print("PRECISION MAX REACH") start = clock.time() apply_eleven_layer(dmps,dgateIT,sim_tdmrg) sim_tdmrg['dw_total'] += _sum(sim_tdmrg['dw_one_serie']) print("discarded weight in one step :",sim_tdmrg['dw_one_serie']) print("discarded weight accumulate :",sim_tdmrg['dw_total']) try: elapsed = (clock.time() - start) except: elapsed = float('nan') sim_tdmrg['right_direction'] = (not sim_tdmrg['right_direction']) sim_tdmrg['odd_bonds'] = (not sim_tdmrg['odd_bonds']) beta = round(beta+2*dbeta,4) if beta>=_max(sim_tdmrg['save_list']): print("BETA MAX REACH")
addlocker_write_running(outputHEAD+'00.0000.h5') nextsave_quit = False while True: if time >= _max(sim_tmpo['save_list']): output = outputHEAD+'{0}.h5'.format(timenum2timestr(time)) print("It shouldn't start with TIME MAX") break if _max(sim_tmpo['dw_one_serie'])>sim_tmpo['stop_dw_limit']: nextsave_quit = True print("PRECISION MAX REACH") start = clock.time() apply_eleven_layer(dmps,dgateRT,sim_tmpo) sim_tmpo['dw_total'] += _sum(sim_tmpo['dw_one_serie']) print("discarded weight in one step :",sim_tmpo['dw_one_serie']) print("discarded weight accumulate :",sim_tmpo['dw_total']) try: elapsed = (clock.time() - start) except: elapsed = float('nan') sim_tmpo['right_direction'] = (not sim_tmpo['right_direction']) sim_tmpo['odd_bonds'] = (not sim_tmpo['odd_bonds']) time = round(time+dtime,4) if time>=_max(sim_tmpo['save_list']): print("TIME MAX REACH")