示例#1
0
def hermine_interpolating_polynomial(knots, values, derivatives):
    '''Generates Hermite interpolating polynomial.

    Parameters:
        knots: [float]
            Two points on the x-axis.
        values: [float]
            The desired values at the given points.
        derivatives: [float]
            The desired derivatives at the given points.

    Returns:
        polynomial: Polynomial
            The generated Hermite interpolating polynomial.

    '''

    x0, x1 = knots
    p0, p1 = values
    m0, m1 = derivatives

    coef_unscaled = h00 * p0 + h01 * p1 + (h10 * m0 + h11 * m1) * (x1 - x0)

    # domain and window together with convert() from numpy.polynomial
    # are used as a linear mapping t = (x - knot) / (knot_ - knot)
    polynomial = Polynomial(coef_unscaled, domain=[x0, x1], window=[0, 1])
    polynomial = polynomial.convert()

    return polynomial
示例#2
0
文件: predictor.py 项目: mhvk/pulsar
    def polynomial(self,
                   index,
                   rphase=None,
                   deriv=0,
                   t0=None,
                   time_unit=u.min,
                   out_unit=None,
                   convert=False):
        """Prediction polynomial set up for times in MJD

        Parameters
        ----------
        index : int or float
            index into the polyco table (or MJD for finding closest)
        rphase : None or 'fraction' or float
            phase zero point; if None, use the one stored in polyco.
            (Those are typically large, so one looses some precision.)
            Can also set 'fraction' to use the stored one modulo 1, which is
            fine for folding, but breaks cycle count continuity between sets.
        deriv : int
            derivative of phase to take (1=frequency, 2=fdot, etc.); default 0

        Returns
        -------
        polynomial : Polynomial
            set up for MJDs between mjd_mid ± span

        Notes
        -----
        Units for the polynomial are cycles/second**deriv.  Taking a derivative
        outside will be per day (e.g., self.polynomial(1).deriv() gives
        frequencies in cycles/day)
        """

        out_unit = out_unit or time_unit

        try:
            index = index.__index__()
        except (AttributeError, TypeError):
            index = self.searchclosest(index)
        window = np.array([-1, 1]) * self['span'][index] / 2 * u.min

        polynomial = Polynomial(self['coeff'][index], window.value,
                                window.value)
        polynomial.coef[1] += self['f0'][index] * 60.

        if deriv == 0:
            if rphase is None:
                polynomial.coef[0] += self['rphase'][index]
            elif rphase == 'fraction':
                polynomial.coef[0] += self['rphase'][index] % 1
            else:
                polynomial.coef[0] = rphase
        else:
            polynomial = polynomial.deriv(deriv)
            polynomial.coef /= u.min.to(out_unit)**deriv

        if t0 is None:
            dt = 0. * time_unit
        elif not hasattr(t0, 'jd1') and t0 == 0:
            dt = (-self['mjd_mid'][index] * u.day).to(time_unit)
        else:
            dt = ((t0 - Time(self['mjd_mid'][index], format='mjd',
                             scale='utc')).jd * u.day).to(time_unit)

        polynomial.domain = (window.to(time_unit) - dt).value

        if convert:
            return polynomial.convert()
        else:
            return polynomial
示例#3
0
    def polynomial(self,
                   index,
                   rphase=None,
                   deriv=0,
                   t0=None,
                   time_unit=u.min,
                   out_unit=None,
                   convert=False):
        """Prediction polynomial set up for times in MJD

        Parameters
        ----------
        index : int or float
            index into the polyco table (or MJD for finding closest)
        rphase : None or 'fraction' or 'ignore' or float
            Phase zero point; if None, use the one stored in polyco.
            (Those are typically large, so one looses some precision.)
            Can also set 'fraction' to use the stored one modulo 1, which is
            fine for folding, but breaks cycle count continuity between sets,
            'ignore' for just keeping the value stored in the coefficients,
            or a value that should replace the zero point.
        deriv : int
            derivative of phase to take (1=frequency, 2=fdot, etc.); default 0

        Returns
        -------
        polynomial : Polynomial
            set up for MJDs between mjd_mid +/- span

        Notes
        -----
        Units for the polynomial are cycles/second**deriv.  Taking a derivative
        outside will be per day (e.g., self.polynomial(1).deriv() gives
        frequencies in cycles/day)
        """

        out_unit = out_unit or time_unit

        try:
            index = index.__index__()
        except (AttributeError, TypeError):
            index = self.searchclosest(index)
        window = np.array([-1, 1]) * self['span'][index] / 2

        polynomial = Polynomial(self['coeff'][index], window.value,
                                window.value)
        polynomial.coef[1] += self['f0'][index].to_value(u.cycle / u.minute)

        if deriv == 0:
            if rphase is None:
                polynomial.coef[0] += self['rphase'][index].value
            elif rphase == 'fraction':
                polynomial.coef[0] += self['rphase']['frac'][index].value % 1
            elif rphase != 'ignore':
                polynomial.coef[0] = rphase
        else:
            polynomial = polynomial.deriv(deriv)
            polynomial.coef /= u.min.to(out_unit)**deriv

        if t0 is not None:
            dt = Time(t0, format='mjd') - self['mjd_mid'][index]
            polynomial.domain = (window - dt).to(time_unit).value

        if convert:
            return polynomial.convert()
        else:
            return polynomial
示例#4
0
    def polynomial(self, index, rphase=None, deriv=0, t0=None, time_unit=u.min, out_unit=None, convert=False):
        """Prediction polynomial set up for times in MJD

        Parameters
        ----------
        index : int or float
            index into the polyco table (or MJD for finding closest)
        rphase : None or 'fraction' or float
            phase zero point; if None, use the one stored in polyco.
            (Those are typically large, so one looses some precision.)
            Can also set 'fraction' to use the stored one modulo 1, which is
            fine for folding, but breaks phase continuity between sets.
        deriv : int
            derivative of phase to take (1=frequency, 2=fdot, etc.); default 0

        Returns
        -------
        polynomial : Polynomial
            set up for MJDs between mjd_mid ± span

        Notes
        -----
        Units for the polynomial are cycles/second**deriv.  Taking a derivative
        outside will be per day (e.g., self.polynomial(1).deriv() gives
        frequencies in cycles/day)
        """

        out_unit = out_unit or time_unit

        try:
            window = np.array([-1, 1]) * self["span"][index] / 2 * u.min
        except (IndexError, TypeError):
            # assume index is really a Time or MJD
            index = self.searchclosest(index)
            window = np.array([-1, 1]) * self["span"][index] / 2 * u.min

        polynomial = Polynomial(self["coeff"][index], window.value, window.value)
        polynomial.coef[1] += self["f0"][index] * 60.0

        if deriv == 0:
            if rphase is None:
                polynomial.coef[0] += self["rphase"][index]
            elif rphase == "fraction":
                polynomial.coef[0] += self["rphase"][index] % 1
            else:
                polynomial.coef[0] = rphase
        else:
            polynomial = polynomial.deriv(deriv)
            polynomial.coef /= u.min.to(out_unit) ** deriv

        if t0 is None:
            dt = 0.0 * time_unit
        elif not hasattr(t0, "jd1") and t0 == 0:
            dt = (-self["mjd_mid"][index] * u.day).to(time_unit)
        else:
            dt = ((t0 - Time(self["mjd_mid"][index], format="mjd", scale="utc")).jd * u.day).to(time_unit)

        polynomial.domain = (window.to(time_unit) - dt).value

        if convert:
            return polynomial.convert()
        else:
            return polynomial