def spline_matrix(x, xi, yi, bc_type="not-a-knot"): """calculate spline interpolation""" xi_m = spline_xi_matrix(xi) # (N_range, 4, N_yi) x_m = spline_x_matrix(x, xi) # (..., N_range, 4) x_m = tf.expand_dims(x_m, axis=-1) m = tf.reduce_sum(xi_m * x_m, axis=[-3, -2]) return tf.reduce_sum(tf.cast(m, yi.dtype) * yi, axis=-1)
def interp(self, m): # q = data_extra[self.outs[0]]["|q|"] # a = self.a() p = self.point_value() zeros = tf.zeros_like(m) ones = tf.ones_like(m) def poly_i(i, xi): tmp = zeros for j in range(i - 1, i + 1): if j < 0 or j > self.interp_N - 1: continue r = ones for k in range(j, j + 2): if k == i: continue r = r * (m - xi[k]) / (xi[i] - xi[k]) r = tf.where((m >= xi[j]) & (m < xi[j + 1]), r, zeros) tmp = tmp + r return tmp h = tf.stack( [poly_i(i, self.points) for i in range(1, self.interp_N - 1)], axis=-1, ) h = tf.stop_gradient(h) p_r = tf.math.real(p) p_i = tf.math.imag(p) ret_r = tf.reduce_sum(h * p_r, axis=-1) ret_i = tf.reduce_sum(h * p_i, axis=-1) return tf.complex(ret_r, ret_i)
def interp(self, m): zeros = tf.zeros_like(m) p = self.point_value() p_r = tf.math.real(p) p_i = tf.math.imag(p) xi_m = self.h_matrix x_m = spline_x_matrix(m, self.points) x_m = tf.expand_dims(x_m, axis=-1) m_xi = tf.reduce_sum(xi_m * x_m, axis=[-3, -2]) m_xi = tf.stop_gradient(m_xi) ret_r = tf.reduce_sum(tf.cast(m_xi, p_r.dtype) * p_r, axis=-1) ret_i = tf.reduce_sum(tf.cast(m_xi, p_i.dtype) * p_i, axis=-1) return tf.complex(ret_r, ret_i)
def get_amp(self, data, data_c=None, **kwargs): m = data["m"] mass1 = self.mass1() mass2 = self.mass2() width1 = self.width1() width2 = self.width2() q = data_c["|q|"] mdaughter1 = kwargs["all_data"]["particle"][self.decay[0].outs[0]]["m"] mdaughter2 = kwargs["all_data"]["particle"][self.decay[0].outs[1]]["m"] q1 = get_relative_p(mass1, mdaughter1, mdaughter2) q2 = get_relative_p(mass2, mdaughter1, mdaughter2) mlist = tf.stack([mass1, mass2]) wlist = tf.stack([width1, width2]) qlist = tf.stack([q1, q2]) Klist = [] for mi, wi, qi in zip(mlist, wlist, qlist): rw = Gamma(m, wi, q, qi, self.bw_l, mi, self.d) Klist.append(mi * rw / (mi**2 - m**2)) KK = tf.reduce_sum(Klist, axis=0) KK += self.alpha() beta_term = self.get_beta( m=m, mlist=mlist, wlist=wlist, q=q, qlist=qlist, Klist=Klist, **kwargs, ) MM = tf.complex(np.float64(1), -KK) MM = beta_term / MM return MM + self.KNR()
def do_spline_hmatrix(h_matrix, y, m, idx): ai, bi, ci, di = tf.unstack(tf.reduce_sum(h_matrix * y, axis=-1), axis=0) a, b, c, d = ( tf.gather(ai, idx), tf.gather(bi, idx), tf.gather(ci, idx), tf.gather(di, idx), ) ret = a + m * (b + m * (c + d * m)) return ret
def interp(self, m): p = self.point_value() ones = tf.ones_like(m) zeros = tf.zeros_like(m) def add_f(x, bl, br): return tf.where((x > bl) & (x <= br), ones, zeros) x_bin = tf.stack( [ add_f( m, (self.points[i] + self.points[i + 1]) / 2, (self.points[i + 1] + self.points[i + 2]) / 2, ) for i in range(self.interp_N - 2) ], axis=-1, ) p_r = tf.math.real(p) p_i = tf.math.imag(p) x_bin = tf.stop_gradient(x_bin) ret_r = tf.reduce_sum(x_bin * p_r, axis=-1) ret_i = tf.reduce_sum(x_bin * p_i, axis=-1) return tf.complex(ret_r, ret_i)
def interp(self, m): # q = data_extra[self.outs[0]]["|q|"] # a = self.a() zeros = tf.zeros_like(m) p = tf.abs(self.point_value()) def add_f(x, bl, br, pl, pr): return tf.where( (x > bl) & (x <= br), (x - bl) / (br - bl) * (pr - pl) + pl, zeros, ) ret = [ add_f(m, self.points[i], self.points[i + 1], p[i], p[i + 1]) for i in range(self.interp_N - 1) ] return tf.complex(tf.reduce_sum(ret, axis=0), zeros)
def interp(self, m): zeros = tf.zeros_like(m) p = self.point_value() xs = [] def poly_i(i): x = 1.0 for j in range(self.interp_N): if i == j: continue x = (x * (m - self.points[j]) / (self.points[i] - self.points[j])) return x xs = tf.stack([poly_i(i) for i in range(self.interp_N)], axis=-1) zeros = tf.zeros_like(xs) xs = tf.complex(xs, zeros) ret = tf.reduce_sum(xs[:, 1:-1] * p, axis=-1) return ret