示例#1
0
    def eff(r):
        if r * cosir > 1:
            eff_r = r**2 * cosir - 1
        else:
            eff_r = (r**2 * cosir * 2 / np.pi *
                     asin(y(r))) - (2 / np.pi * asin(y(r) * r * cosir))

        return eff_r
示例#2
0
def cartesian_to_angular(x, y, z):
    """Converts from cartesian to angular coordinates
	"""

    norm = np.sqrt(x**2 + y**2 + z**2)

    x = x / norm
    y = y / norm
    z = z / norm

    dec = np.asin(z)
    ra = np.atan2(y, x)

    ra = ra * rad2deg
    dec = dec * rad2deg

    return numpy.array([ra, dec])
示例#3
0
def test_against_uncertainties_package():
    try:
        from uncertainties import ufloat
        from uncertainties import umath
        from math import sqrt as math_sqrt
    except ImportError:
        return
    X, varX = 0.5, 0.04
    Y, varY = 3, 0.09
    N = 3
    ux = ufloat(X, math_sqrt(varX))
    uy = ufloat(Y, math_sqrt(varY))

    def _compare(result, u):
        Z, varZ = result
        assert abs(Z-u.n)/u.n < 1e-13 and (varZ-u.s**2)/u.s**2 < 1e-13, \
            "expected (%g,%g) got (%g,%g)"%(u.n,u.s**2,Z,varZ)

    def _check_pow(u):
        _compare(pow(X, varX, N), u)

    def _check_unary(op, u):
        _compare(op(X, varX), u)

    def _check_binary(op, u):
        _compare(op(X, varX, Y, varY), u)

    _check_pow(ux**N)
    _check_binary(add, ux+uy)
    _check_binary(sub, ux-uy)
    _check_binary(mul, ux*uy)
    _check_binary(div, ux/uy)
    _check_binary(pow2, ux**uy)

    _check_unary(exp, umath.exp(ux))
    _check_unary(log, umath.log(ux))
    _check_unary(sin, umath.sin(ux))
    _check_unary(cos, umath.cos(ux))
    _check_unary(tan, umath.tan(ux))
    _check_unary(arcsin, umath.asin(ux))
    _check_unary(arccos, umath.acos(ux))
    _check_unary(arctan, umath.atan(ux))
    _check_binary(arctan2, umath.atan2(ux, uy))
示例#4
0
def test_against_uncertainties_package():
    try:
        from uncertainties import ufloat
        from uncertainties import umath
        from math import sqrt as math_sqrt
    except ImportError:
        return
    X, varX = 0.5, 0.04
    Y, varY = 3, 0.09
    N = 3
    ux = ufloat(X, math_sqrt(varX))
    uy = ufloat(Y, math_sqrt(varY))

    def _compare(result, u):
        Z, varZ = result
        assert abs(Z-u.n)/u.n < 1e-13 and (varZ-u.s**2)/u.s**2 < 1e-13, \
            "expected (%g,%g) got (%g,%g)"%(u.n, u.s**2, Z, varZ)

    def _check_pow(u):
        _compare(pow(X, varX, N), u)

    def _check_unary(op, u):
        _compare(op(X, varX), u)

    def _check_binary(op, u):
        _compare(op(X, varX, Y, varY), u)

    _check_pow(ux**N)
    _check_binary(add, ux + uy)
    _check_binary(sub, ux - uy)
    _check_binary(mul, ux * uy)
    _check_binary(div, ux / uy)
    _check_binary(pow2, ux**uy)

    _check_unary(exp, umath.exp(ux))
    _check_unary(log, umath.log(ux))
    _check_unary(sin, umath.sin(ux))
    _check_unary(cos, umath.cos(ux))
    _check_unary(tan, umath.tan(ux))
    _check_unary(arcsin, umath.asin(ux))
    _check_unary(arccos, umath.acos(ux))
    _check_unary(arctan, umath.atan(ux))
    _check_binary(arctan2, umath.atan2(ux, uy))
示例#5
0
def transit_duration(P, Rp, a, b=0, e=0, w=90, inc=None, total=True):
    
    """
    Function to calculate the total (T14) or full (T23) transit duration
    
    Parameters:
    ----------
    
    P: Period of planet orbit in days

    Rp: Radius of the planet in units of stellar radius

    b: Impact parameter of the planet transit [0, 1+Rp]

    a: scaled semi-major axis of the planet in units of solar radius
    
    inc: inclination of the orbit. Optional. If given b is not used. if None, b is used to calculate inc
    
    total: if True calculate the the total transit duration T14, else calculate duration of full transit T23

    Returns
    -------
    Tdur: duration of transit in same unit as P   
    
    """
    #eqn 30 and 31 of Kipping 2010 https://doi.org/10.1111/j.1365-2966.2010.16894.x
        
    factor = (1-e**2)/(1+e*sin(radians(w)))
    if inc == None:
        inc = inclination(b,a,e,w)
    
    if total is False:
        Rp = -Rp
        
    sini = sin(radians(inc))
    cosi = cos(radians(inc))
    
    denom = a*factor*sini
    
    tdur= (P/np.pi) * (factor**2/sqrt(1-e**2)) * (asin ( sqrt((1+Rp)**2 - (a*factor*cosi)**2)/ denom ) )
    
    return  tdur
示例#6
0
def CartToSphU(cn, ce, cd):
    '''
	CartToSphU converts from Cartesian to spherical coordinates

	CartToSphU(cn,ce,cd) returns the trend (trd)
	and plunge (plg) of a line for input north (cn),
	east (ce), and down (cd) direction cosines.
	Notice that the input direction cosines have uncertainties

	NOTE: Trend and plunge are returned in radians and they
	have uncertainties in radians

	CartToSphU uses function ZeroTwoPi
	It also uses the uncertainties package from
	Eric O. Lebigot
	
	Based on Python function CartToSph
	'''
    pi = math.pi
    # Plunge
    plg = umath.asin(cd)  # Eq. 4.13a

    # Trend: If north direction cosine is zero, trend
    # is east or west. Choose which one by the sign of
    # the east direction cosine
    if cn == 0.0:
        if ce < 0.0:
            trd = 3.0 / 2.0 * pi  # Eq. 4.14d, trend is west
        else:
            trd = pi / 2.0  # Eq. 4.14c, trend is east
    # Else
    else:
        trd = umath.atan(ce / cn)  # Eq. 4.14a
        if cn < 0.0:
            # Add pi
            trd = trd + pi  # Eq. 4.14b
        # Make sure trend is between 0 and 2*pi
        trd = ZeroTwoPi(trd)

    return trd, plg
示例#7
0
def calculate_l1_l2(h6, h7, d5, d6, l):
    '''Returns the distance along (l2) and perpendicular (l1) to the steer axis from the
    front wheel center to the handlebar reference point.

    Parameters
    ----------
    h6 : float
        Distance from the table to the top of the front axle.
    h7 : float
        Distance from the table to the top of the handlebar reference circle.
    d5 : float
        Diameter of the front axle.
    d6 : float
        Diameter of the handlebar reference circle.
    l : float
        Outer distance from the front axle to the handlebar reference circle.

    Returns
    -------
    l1 : float
        The distance from the front wheel center to the handlebar reference
        center perpendicular to the steer axis. The positive sense is if the
        handlebar reference point is more forward than the front wheel center
        relative to the steer axis normal.
    l2 : float
       The distance from the front wheel center to the handlebar reference
       center parallel to the steer axis. The positive sense is if the
       handlebar reference point is above the front wheel center with reference
       to the steer axis.

    '''
    r5 = d5 / 2.
    r6 = d6 / 2.
    l1 = h7 - h6 + r5 - r6
    l0 = l - r5 - r6
    gamma = umath.asin(l1 / l0)
    l2 = l0 * umath.cos(gamma)
    return l1, l2
示例#8
0
def calculate_l1_l2(h6, h7, d5, d6, l):
    '''Returns the distance along (l2) and perpendicular (l1) to the steer axis from the
    front wheel center to the handlebar reference point.

    Parameters
    ----------
    h6 : float
        Distance from the table to the top of the front axle.
    h7 : float
        Distance from the table to the top of the handlebar reference circle.
    d5 : float
        Diameter of the front axle.
    d6 : float
        Diameter of the handlebar reference circle.
    l : float
        Outer distance from the front axle to the handlebar reference circle.

    Returns
    -------
    l1 : float
        The distance from the front wheel center to the handlebar reference
        center perpendicular to the steer axis. The positive sense is if the
        handlebar reference point is more forward than the front wheel center
        relative to the steer axis normal.
    l2 : float
       The distance from the front wheel center to the handlebar reference
       center parallel to the steer axis. The positive sense is if the
       handlebar reference point is above the front wheel center with reference
       to the steer axis.

    '''
    r5 = d5 / 2.
    r6 = d6 / 2.
    l1 = h7 - h6 + r5 - r6
    l0 = l - r5 - r6
    gamma = umath.asin(l1 / l0)
    l2 = l0 * umath.cos(gamma)
    return l1, l2
示例#9
0
        else:
            try:
                __x = InterpretNum(__s, Dict(locals()))
                return __x
            except Exception as e:
                print("Number not correct:  %s" % str(e))

# To utilize Marv's code, here are definitions for the macros and
# constants/variables he uses.
ABS = abs
SGN = lambda a: -1 if a < 0 else 0 if a == 0 else 1
DPR = 180/pi
RPD = pi/180
COSD = lambda a:  cos(a*RPD)
SIND = lambda a:  sin(a*RPD)
ASND = lambda a:  DPR*asin(a if abs(a) < 1 else SGN(a))
ACSD = lambda a:  DPR*acos(a if abs(a) < 1 else SGN(a))

def Calculate(__vars, __d):
    # Get vars into our local namespace
    if __vars is not None:
        for __k in __vars:
            if len(__k) > 2 and __k[:2] == "__":
                continue
            exec("%s = __vars['%s']" % (__k, __k))
    try:
        if radius and angle:
            if dbg:
                print("radius, angle", sig(radius), sig(angle))
            z = radius*COSD(0.5*angle)
            height = radius - z
示例#10
0
    def get_summary(self, nodmx=False):
        """Return a human-readable summary of the Fitter results.
        
        Parameters
        ----------
        nodmx : bool
            Set to True to suppress printing DMX parameters in summary
        """

        # Need to check that fit has been done first!
        if not hasattr(self, "covariance_matrix"):
            log.warning(
                "fit_toas() has not been run, so pre-fit and post-fit will be the same!"
            )

        from uncertainties import ufloat
        import uncertainties.umath as um

        # First, print fit quality metrics
        s = "Fitted model using {} method with {} free parameters to {} TOAs\n".format(
            self.method, len(self.get_fitparams()), self.toas.ntoas)
        s += "Prefit residuals Wrms = {}, Postfit residuals Wrms = {}\n".format(
            self.resids_init.rms_weighted(), self.resids.rms_weighted())
        s += "Chisq = {:.3f} for {} d.o.f. for reduced Chisq of {:.3f}\n".format(
            self.resids.chi2, self.resids.dof, self.resids.chi2_reduced)
        s += "\n"

        # Next, print the model parameters
        s += "{:<14s} {:^20s} {:^28s} {}\n".format("PAR", "Prefit", "Postfit",
                                                   "Units")
        s += "{:<14s} {:>20s} {:>28s} {}\n".format("=" * 14, "=" * 20,
                                                   "=" * 28, "=" * 5)
        for pn in list(self.get_allparams().keys()):
            if nodmx and pn.startswith("DMX"):
                continue
            prefitpar = getattr(self.model_init, pn)
            par = getattr(self.model, pn)
            if par.value is not None:
                if isinstance(par, strParameter):
                    s += "{:14s} {:>20s} {:28s} {}\n".format(
                        pn, prefitpar.value, "", par.units)
                elif isinstance(par, AngleParameter):
                    # Add special handling here to put uncertainty into arcsec
                    if par.frozen:
                        s += "{:14s} {:>20s} {:>28s} {} \n".format(
                            pn, str(prefitpar.quantity), "", par.units)
                    else:
                        if par.units == u.hourangle:
                            uncertainty_unit = pint.hourangle_second
                        else:
                            uncertainty_unit = u.arcsec
                        s += "{:14s} {:>20s}  {:>16s} +/- {:.2g} \n".format(
                            pn,
                            str(prefitpar.quantity),
                            str(par.quantity),
                            par.uncertainty.to(uncertainty_unit),
                        )

                else:
                    # Assume a numerical parameter
                    if par.frozen:
                        s += "{:14s} {:20g} {:28s} {} \n".format(
                            pn, prefitpar.value, "", par.units)
                    else:
                        # s += "{:14s} {:20g} {:20g} {:20.2g} {} \n".format(
                        #     pn,
                        #     prefitpar.value,
                        #     par.value,
                        #     par.uncertainty.value,
                        #     par.units,
                        # )
                        s += "{:14s} {:20g} {:28SP} {} \n".format(
                            pn,
                            prefitpar.value,
                            ufloat(par.value, par.uncertainty.value),
                            par.units,
                        )
        # Now print some useful derived parameters
        s += "\nDerived Parameters:\n"
        if hasattr(self.model, "F0"):
            F0 = self.model.F0.quantity
            if not self.model.F0.frozen:
                p, perr = pint.utils.pferrs(F0, self.model.F0.uncertainty)
                s += "Period = {} +/- {}\n".format(p.to(u.s), perr.to(u.s))
            else:
                s += "Period = {}\n".format((1.0 / F0).to(u.s))
        if hasattr(self.model, "F1"):
            F1 = self.model.F1.quantity
            if not any([self.model.F1.frozen, self.model.F0.frozen]):
                p, perr, pd, pderr = pint.utils.pferrs(
                    F0, self.model.F0.uncertainty, F1,
                    self.model.F1.uncertainty)
                s += "Pdot = {} +/- {}\n".format(
                    pd.to(u.dimensionless_unscaled),
                    pderr.to(u.dimensionless_unscaled))
                brakingindex = 3
                s += "Characteristic age = {:.4g} (braking index = {})\n".format(
                    pint.utils.pulsar_age(F0, F1, n=brakingindex),
                    brakingindex)
                s += "Surface magnetic field = {:.3g}\n".format(
                    pint.utils.pulsar_B(F0, F1))
                s += "Magnetic field at light cylinder = {:.4g}\n".format(
                    pint.utils.pulsar_B_lightcyl(F0, F1))
                I_NS = I = 1.0e45 * u.g * u.cm**2
                s += "Spindown Edot = {:.4g} (I={})\n".format(
                    pint.utils.pulsar_edot(F0, F1, I=I_NS), I_NS)

        if hasattr(self.model, "PX"):
            if not self.model.PX.frozen:
                s += "\n"
                px = ufloat(
                    self.model.PX.quantity.to(u.arcsec).value,
                    self.model.PX.uncertainty.to(u.arcsec).value,
                )
                s += "Parallax distance = {:.3uP} pc\n".format(1.0 / px)

        # Now binary system derived parameters
        binary = None
        for x in self.model.components:
            if x.startswith("Binary"):
                binary = x
        if binary is not None:
            s += "\n"
            s += "Binary model {}\n".format(binary)

            if binary.startswith("BinaryELL1"):
                if not any([
                        self.model.EPS1.frozen,
                        self.model.EPS2.frozen,
                        self.model.TASC.frozen,
                        self.model.PB.frozen,
                ]):
                    eps1 = ufloat(
                        self.model.EPS1.quantity.value,
                        self.model.EPS1.uncertainty.value,
                    )
                    eps2 = ufloat(
                        self.model.EPS2.quantity.value,
                        self.model.EPS2.uncertainty.value,
                    )
                    tasc = ufloat(
                        # This is a time in MJD
                        self.model.TASC.quantity.mjd,
                        self.model.TASC.uncertainty.to(u.d).value,
                    )
                    pb = ufloat(
                        self.model.PB.quantity.to(u.d).value,
                        self.model.PB.uncertainty.to(u.d).value,
                    )
                    s += "Conversion from ELL1 parameters:\n"
                    ecc = um.sqrt(eps1**2 + eps2**2)
                    s += "ECC = {:P}\n".format(ecc)
                    om = um.atan2(eps1, eps2) * 180.0 / np.pi
                    if om < 0.0:
                        om += 360.0
                    s += "OM  = {:P}\n".format(om)
                    t0 = tasc + pb * om / 360.0
                    s += "T0  = {:SP}\n".format(t0)

                    s += pint.utils.ELL1_check(
                        self.model.A1.quantity,
                        ecc.nominal_value,
                        self.resids.rms_weighted(),
                        self.toas.ntoas,
                        outstring=True,
                    )
                    s += "\n"

                # Masses and inclination
                if not any([self.model.PB.frozen, self.model.A1.frozen]):
                    pbs = ufloat(
                        self.model.PB.quantity.to(u.s).value,
                        self.model.PB.uncertainty.to(u.s).value,
                    )
                    a1 = ufloat(
                        self.model.A1.quantity.to(pint.ls).value,
                        self.model.A1.uncertainty.to(pint.ls).value,
                    )
                    fm = 4.0 * np.pi**2 * a1**3 / (4.925490947e-6 * pbs**2)
                    s += "Mass function = {:SP} Msun\n".format(fm)
                    mcmed = pint.utils.companion_mass(
                        self.model.PB.quantity,
                        self.model.A1.quantity,
                        inc=60.0 * u.deg,
                        mpsr=1.4 * u.solMass,
                    )
                    mcmin = pint.utils.companion_mass(
                        self.model.PB.quantity,
                        self.model.A1.quantity,
                        inc=90.0 * u.deg,
                        mpsr=1.4 * u.solMass,
                    )
                    s += "Companion mass min, median (assuming Mpsr = 1.4 Msun) = {:.4f}, {:.4f} Msun\n".format(
                        mcmin, mcmed)

                if hasattr(self.model, "SINI"):
                    if not self.model.SINI.frozen:
                        si = ufloat(
                            self.model.SINI.quantity.value,
                            self.model.SINI.uncertainty.value,
                        )
                        s += "From SINI in model:\n"
                        s += "    cos(i) = {:SP}\n".format(um.sqrt(1 - si**2))
                        s += "    i = {:SP} deg\n".format(
                            um.asin(si) * 180.0 / np.pi)

                    psrmass = pint.utils.pulsar_mass(
                        self.model.PB.quantity,
                        self.model.A1.quantity,
                        self.model.M2.quantity,
                        np.arcsin(self.model.SINI.quantity),
                    )
                    s += "Pulsar mass (Shapiro Delay) = {}".format(psrmass)

        return s
示例#11
0
文件: tri.py 项目: akipta/hobbyutil
def Test():
    '''The following test cases came from the sample problems at
    http://www.mathsisfun.com/algebra/trig-solving-triangles.html
    '''
    eps, r2d, d2r = 1e-14, 180/pi, pi/180
    d = {
        "angle_measure" : d2r,
    }
    # sss
    d["vars"] = {
        "S1" : 6,
        "S2" : 7,
        "S3" : 8,
    }
    d["problem_type"] = "sss"
    SolveProblem(d)
    k = d["solution"]
    assert abs(k["A1"] - acos(77/112)) < eps
    assert abs(k["A2"] - (pi - k["A3"] - k["A1"])) < eps
    assert abs(k["A3"] - acos(1/4)) < eps
    # ssa
    d["vars"] = {
        "S1" : 8,
        "S2" : 13,
        "A1" : 31,  # Angle in degrees
    }
    d["problem_type"] = "ssa"
    SolveProblem(d)
    k = d["solution"]
    a2 = asin(13*sin(31*d2r)/8)
    assert abs(k["A2"] - a2) < eps
    a3 = pi - k["A2"] - k["A1"]
    assert abs(k["A3"] - a3) < eps
    assert abs(k["S3"] - sin(a3)*8/sin(31*d2r)) < eps
    # Check other solution
    a2_2 = pi - asin(13*sin(31*d2r)/8)
    assert abs(k["A2_2"] - a2_2) < eps
    a3_2 = pi - k["A2_2"] - k["A1_2"]
    assert abs(k["A3_2"] - a3_2) < eps
    assert abs(k["S3_2"] - sin(a3_2)*8/sin(31*d2r)) < eps
    # sas
    d["vars"] = {
        "S1" : 5,
        "S2" : 7,
        "A1" : 49,  # Angle in degrees
    }
    d["problem_type"] = "sas"
    SolveProblem(d)
    k = d["solution"]
    # asa
    d["vars"] = {
        "S1" : 9,
        "A1" : 76,  # Angle in degrees
        "A2" : 34,  # Angle in degrees
    }
    d["problem_type"] = "asa"
    SolveProblem(d)
    k = d["solution"]
    assert abs(k["S2"] - 9*sin(34*d2r)/sin(70*d2r)) < eps
    assert abs(k["S1"] - 9*sin(76*d2r)/sin(70*d2r)) < eps
    assert abs(k["A3"] - 70*d2r) < eps
    # saa
    d["vars"] = {
        "S1" : 7,
        "A1" : 62,  # Angle in degrees
        "A2" : 35,  # Angle in degrees
    }
    d["problem_type"] = "saa"
    SolveProblem(d)
    k = d["solution"]
    assert abs(k["A3"] - 83*d2r) < eps
    assert abs(k["S2"] - 7*sin(35*d2r)/sin(62*d2r)) < eps
    assert abs(k["S3"] - 7*sin(83*d2r)/sin(62*d2r)) < eps
    out("Tests passed")
示例#12
0
文件: tri.py 项目: akipta/hobbyutil
def SolveProblem(d):
    # Get our needed variables
    for k in d["vars"]:
        if k in set(("S1", "S2", "S3", "A1", "A2", "A3")):
            exec("%s = d['vars']['%s']" % (k, k))
    angle_conv = d["angle_measure"]  # Converts angle measure to radians
    prob = d["problem_type"]
    try:
        if prob == "sss":
            # Law of cosines to find two angles, angle law to find third.
            A1 = acos((S2**2 + S3**2 - S1**2)/(2*S2*S3))
            A2 = acos((S1**2 + S3**2 - S2**2)/(2*S1*S3))
            A3 = pi - A1 - A2
        elif prob == "ssa":
            # Law of sines to find the other two angles and remaining
            # side.  Note it can have two solutions (the second solution's
            # data will be in the variables s1_2, s2_2, etc.).
            A1 *= angle_conv  # Make sure angle is in radians
            A2 = asin((S2/S1*sin(A1)))
            A3 = pi - A1 - A2
            S3 = S2*sin(A3)/sin(A2)
            # Check for other solution
            A1_2 = A1
            A2_2 = pi - A2
            A3_2 = pi - A1_2 - A2_2
            if A1_2 + A2_2 + A3_2 > pi:
                # Second solution not possible
                del A1_2
                del A2_2
                del A3_2
            else:
                # Second solution is possible
                S1_2 = S1
                S2_2 = S2
                S3_2 = S2_2*sin(A3_2)/sin(A2_2)
        elif prob == "sas":
            # Law of cosines to find third side; law of sines to find
            # another angle; angle law for other angle.  Note we rename
            # the incoming angle to be consistent with a solution diagram.
            A3 = A1*angle_conv  # Make sure angle is in radians
            S3 = sqrt(S1**2 + S2**2 - 2*S1*S2*cos(A3))
            A2 = asin(S2*sin(A3)/S3)
            A1 = pi - A2 - A3
        elif prob == "asa":
            # Third angle from angle law; law of sines for other two
            # sides.  Note we rename the sides for consistency with a
            # diagram.
            A1 *= angle_conv  # Make sure angle is in radians
            A2 *= angle_conv  # Make sure angle is in radians
            A3 = pi - A1 - A2
            S3 = S1
            S2 = S3*sin(A2)/sin(A3)
            S1 = S3*sin(A1)/sin(A3)
        elif prob == "saa":
            # Third angle from angle law; law of sines for other two
            # sides. 
            A1 *= angle_conv  # Make sure angle is in radians
            A2 *= angle_conv  # Make sure angle is in radians
            A3 = pi - A1 - A2
            S2 = S1*sin(A2)/sin(A1)
            S3 = S1*sin(A3)/sin(A1)
        else:
            raise ValueError("Bug:  unrecognized problem")
    except UnboundLocalError as e:
        s = str(e)
        loc = s.find("'")
        s = s[loc + 1:]
        loc = s.find("'")
        s = s[:loc]
        Error("Variable '%s' not defined" % s)
    except ValueError as e:
        msg = "Can't solve the problem:\n"
        msg += "  Error:  %s" % str(e)
        Error(msg)
    # Collect solution information
    solution = {}
    vars = set((
        "S1", "S2", "S3", "A1", "A2", "A3",
        "S1_2", "S2_2", "S3_2", "A1_2", "A2_2", "A3_2",
    ))
    for k in vars:
        if k in locals():
            exec("solution['%s'] = %s" % (k, k))
    d["solution"] = solution