def __str__(self): thestr = "0" var = self.variable # Remove leading zeros coeffs = self.coeffs[NX.logical_or.accumulate(self.coeffs != 0)] N = len(coeffs) - 1 def fmt_float(q): s = '%.4g' % q if s.endswith('.0000'): s = s[:-5] return s for k in range(len(coeffs)): if not iscomplex(coeffs[k]): coefstr = fmt_float(real(coeffs[k])) elif real(coeffs[k]) == 0: coefstr = '%sj' % fmt_float(imag(coeffs[k])) else: coefstr = '(%s + %sj)' % (fmt_float(real( coeffs[k])), fmt_float(imag(coeffs[k]))) power = (N - k) if power == 0: if coefstr != '0': newstr = '%s' % (coefstr, ) else: if k == 0: newstr = '0' else: newstr = '' elif power == 1: if coefstr == '0': newstr = '' elif coefstr == 'b': newstr = var else: newstr = '%s %s' % (coefstr, var) else: if coefstr == '0': newstr = '' elif coefstr == 'b': newstr = '%s**%d' % ( var, power, ) else: newstr = '%s %s**%d' % (coefstr, var, power) if k > 0: if newstr != '': if newstr.startswith('-'): thestr = "%s - %s" % (thestr, newstr[1:]) else: thestr = "%s + %s" % (thestr, newstr) else: thestr = newstr return _raise_power(thestr)
def __str__(self): thestr = "0" var = self.variable # Remove leading zeros coeffs = self.coeffs[NX.logical_or.accumulate(self.coeffs != 0)] N = len(coeffs) - 1 def fmt_float(q): s = "%.4g" % q if s.endswith(".0000"): s = s[:-5] return s for k in range(len(coeffs)): if not iscomplex(coeffs[k]): coefstr = fmt_float(real(coeffs[k])) elif real(coeffs[k]) == 0: coefstr = "%sj" % fmt_float(imag(coeffs[k])) else: coefstr = "(%s + %sj)" % ( fmt_float(real(coeffs[k])), fmt_float(imag(coeffs[k])), ) power = N - k if power == 0: if coefstr != "0": newstr = "%s" % (coefstr,) else: if k == 0: newstr = "0" else: newstr = "" elif power == 1: if coefstr == "0": newstr = "" elif coefstr == "b": newstr = var else: newstr = "%s %s" % (coefstr, var) else: if coefstr == "0": newstr = "" elif coefstr == "b": newstr = "%s**%d" % (var, power) else: newstr = "%s %s**%d" % (coefstr, var, power) if k > 0: if newstr != "": if newstr.startswith("-"): thestr = "%s - %s" % (thestr, newstr[1:]) else: thestr = "%s + %s" % (thestr, newstr) else: thestr = newstr return _raise_power(thestr)
def tb(self, v0, throw): # La altura final es la altura de las manos,la inicial es 0 pq parte de un rebote p = phy.PosOfProyectil(self.V_hand, 0, v0 * (self.r**throw.Bounces)) res = min(p.roots) if iscomplex(res): raise Exception( 'la pelota no alcanza la altura {0} con la velocidad {1}'. format(self.V_hand, v0 * (self.r**throw.Bounces))) return res
def __str__(self): thestr = "0" var = self.variable # Remove leading zeros coeffs = self.coeffs[NX.logical_or.accumulate(self.coeffs != 0)] N = len(coeffs)-1 def fmt_float(q): s = '%.4g' % q if s.endswith('.0000'): s = s[:-5] return s for k in range(len(coeffs)): if not iscomplex(coeffs[k]): coefstr = fmt_float(real(coeffs[k])) elif real(coeffs[k]) == 0: coefstr = '%sj' % fmt_float(imag(coeffs[k])) else: coefstr = '(%s + %sj)' % (fmt_float(real(coeffs[k])), fmt_float(imag(coeffs[k]))) power = (N-k) if power == 0: if coefstr != '0': newstr = '%s' % (coefstr,) else: if k == 0: newstr = '0' else: newstr = '' elif power == 1: if coefstr == '0': newstr = '' elif coefstr == 'b': newstr = var else: newstr = '%s %s' % (coefstr, var) else: if coefstr == '0': newstr = '' elif coefstr == 'b': newstr = '%s**%d' % (var, power,) else: newstr = '%s %s**%d' % (coefstr, var, power) if k > 0: if newstr != '': if newstr.startswith('-'): thestr = "%s - %s" % (thestr, newstr[1:]) else: thestr = "%s + %s" % (thestr, newstr) else: thestr = newstr return _raise_power(thestr)
def __str__(self): thestr = "0" var = self.variable # Remove leading zeros coeffs = self.coeffs[NX.logical_or.accumulate(self.coeffs != 0)] N = len(coeffs) - 1 def fmt_float(q): s = "%.4g" % q if s.endswith(".0000"): s = s[:-5] return s for k in range(len(coeffs)): if not iscomplex(coeffs[k]): coefstr = fmt_float(real(coeffs[k])) elif real(coeffs[k]) == 0: coefstr = "%sj" % fmt_float(imag(coeffs[k])) else: coefstr = "(%s + %sj)" % (fmt_float(real(coeffs[k])), fmt_float(imag(coeffs[k]))) power = N - k if power == 0: if coefstr != "0": newstr = "%s" % (coefstr,) else: if k == 0: newstr = "0" else: newstr = "" elif power == 1: if coefstr == "0": newstr = "" elif coefstr == "b": newstr = var else: newstr = "%s %s" % (coefstr, var) else: if coefstr == "0": newstr = "" elif coefstr == "b": newstr = "%s**%d" % (var, power) else: newstr = "%s %s**%d" % (coefstr, var, power) if k > 0: if newstr != "": if newstr.startswith("-"): thestr = "%s - %s" % (thestr, newstr[1:]) else: thestr = "%s + %s" % (thestr, newstr) else: thestr = newstr return _raise_power(thestr)
def test_pass(self): z = np.array([-1j, 1, 0]) res = iscomplex(z) assert_array_equal(res, [1, 0, 0])
def test_fail(self): z = np.array([-1, 0, 1]) res = iscomplex(z) assert_(not np.sometrue(res, axis=0))
def fun_LL_norm(params, getder=True): #Computes the log likelihood of the noise parameters. Also returns #the partial derivatives of each of the parameters (i.e. the #gradient), which are required by minimize.m and other efficient #optimization algorithms. input_type = 'tensor' if type(params) is np.ndarray: input_type = 'numpy' params = torch.tensor(params) elif type(params) is list: input_type = 'list' params = torch.tensor(params) nvox = noise.shape[1] ntrials = noise.shape[0] tau = params[0:-2] sig = params[-2] rho = params[-1] omi, NormConst = invSNC(W[:, 0:p['nchan']], tau, sig, rho, True) XXt = torch.mm(noise.t(), noise) negloglik = 0.5 * (MatProdTrace(XXt, omi) + ntrials * NormConst) negloglik = negloglik.item() if iscomplex(negloglik): negloglik = inf #If we encounter a degenerate solution (indicated by complex-valued likelihoods), make sure that the likelihood goes to infinity. if (torch.cat((tau, sig.unsqueeze(-1)), 0) < 0.001).any(): negloglik = inf if rho.abs() > 0.999999: negloglik = inf loglik = -negloglik if getder: der = torch.empty_like(params) ss = sqrt(ntrials) U = torch.mm(omi, noise.t()) / ss dom = torch.mm( omi, torch.eye(nvox) - torch.mm(((1 / ntrials) * XXt), omi)) JI = 1 - torch.eye(nvox) R = torch.eye(nvox) * (1 - rho) + rho der[0:-2] = torch.mm(2 * (dom * R), tau.unsqueeze(-1)).squeeze() der[-1] = (dom * (tau.unsqueeze(-1) * tau.unsqueeze(0)) * JI).sum() der[-2] = 2 * sig * MatProdTrace( torch.mm(W[:, 0:p['nchan']].t(), omi), W[:, 0:p['nchan']] ) - sqrt(2 * sig) * (torch.mm(U.t(), W[:, 0:p['nchan']])**2).sum() der *= -0.5 * ntrials if input_type == 'numpy': der = der.numpy() elif input_type == 'list': der = der.tolist() return loglik, der else: return loglik