def vrpmllpmbb_to_vxvyvz(vr, pmll, pmbb, l, b, d, XYZ=False, degree=False): """ NAME: vrpmllpmbb_to_vxvyvz PURPOSE: Transform velocities in the spherical Galactic coordinate frame to the rectangular Galactic coordinate frame Can take vector inputs INPUT: vr - line-of-sight velocity (km/s) pmll - proper motion in the Galactic longitude (mu_l * cos(b))(mas/yr) pmbb - proper motion in the Galactic lattitude (mas/yr) l - Galactic longitude b - Galactic lattitude d - distance (kpc) XYZ - (bool) If True, then l,b,d is actually X,Y,Z (rectangular Galactic coordinates) degree - (bool) if True, l and b are in degrees OUTPUT: (vx,vy,vz) in (km/s,km/s,km/s) HISTORY: 2009-10-24 - Written - Bovy (NYU) """ if sc.array(l).shape == (): return vrpmllpmbb_to_vxvyvz_single(vr, pmll, pmbb, l, b, d, XYZ, degree) else: function = sc.frompyfunc(vrpmllpmbb_to_vxvyvz_single, 8, 3) return sc.array(function(vr, pmll, pmbb, l, b, d, XYZ, degree), dtype=sc.float64).T
def pmllpmbb_to_pmrapmdec(pmll, pmbb, l, b, degree=False, epoch=2000.0): """ NAME: pmllpmbb_to_pmrapmdec PURPOSE: rotate proper motions in (l,b) into proper motions in (ra,dec) INPUT: pmll - proper motion in l (multplied with cos(b)) [mas/yr] pmbb - proper motion in b [mas/yr] l - Galactic longitude b - Galactic lattitude degree - if True, l and b are given in degrees (default=False) epoch - epoch of ra,dec (right now only 2000.0 and 1950.0 are supported) OUTPUT: (pmra,pmdec), for vector inputs [:,2] HISTORY: 2010-04-07 - Written - Bovy (NYU) """ if sc.array(pmll).shape == (): ra, dec = lb_to_radec(l, b, degree=degree, epoch=epoch) return pmllpmbb_to_pmrapmdec_single(pmll, pmbb, ra, dec, b, degree, epoch) else: radec = lb_to_radec(l, b, degree=degree, epoch=epoch) function = sc.frompyfunc(pmllpmbb_to_pmrapmdec_single, 7, 2) return sc.array(function(pmll, pmbb, radec[:, 0], radec[:, 1], b, degree, epoch), dtype=sc.float64).T
def pmrapmdec_to_pmllpmbb(pmra, pmdec, ra, dec, degree=False, epoch=2000.0): """ NAME: pmrapmdec_to_pmllpmbb PURPOSE: rotate proper motions in (ra,dec) into proper motions in (l,b) INPUT: pmra - proper motion in ra (multplied with cos(dec)) [mas/yr] pmdec - proper motion in dec [mas/yr] ra - right ascension dec - declination degree - if True, ra and dec are given in degrees (default=False) epoch - epoch of ra,dec (right now only 2000.0 and 1950.0 are supported) OUTPUT: (pmll,pmbb) for vector inputs [:,2] HISTORY: 2010-04-07 - Written - Bovy (NYU) """ if sc.array(pmra).shape == (): l, b = radec_to_lb(ra, dec, degree=degree, epoch=epoch) return pmrapmdec_to_pmllpmbb_single(pmra, pmdec, ra, dec, b, degree, epoch) else: lb = radec_to_lb(ra, dec, degree=degree, epoch=epoch) function = sc.frompyfunc(pmrapmdec_to_pmllpmbb_single, 7, 2) return sc.array(function(pmra, pmdec, ra, dec, lb[:, 1], degree, epoch), dtype=sc.float64).T
def GreensFunctionSquare(x,izero,W): ''' local Green's function electrons on a 2D square lattice, using elliptic integral of the first kind K(z) from mpmath ''' from mpmath import ellipk K = sp.frompyfunc(ellipk,1,1) x = x+1.0j*izero return sp.array(2.0*K((W/x)**2)/(sp.pi*x),dtype=complex)
def radec_to_lb(ra,dec,degree=False,epoch=2000.0): """ NAME: radec_to_lb PURPOSE: transform from equatorial coordinates to Galactic coordinates INPUT: ra - right ascension dec - declination degree - (Bool) if True, ra and dec are given in degree and l and b will be as well epoch - epoch of ra,dec (right now only 2000.0 and 1950.0 are supported) OUTPUT: l,b For vector inputs [:,2] HISTORY: 2009-11-12 - Written - Bovy (NYU) """ #First calculate the transformation matrix T theta,dec_ngp,ra_ngp= get_epoch_angles(epoch) T= sc.dot(sc.array([[sc.cos(theta),sc.sin(theta),0.],[sc.sin(theta),-sc.cos(theta),0.],[0.,0.,1.]]),sc.dot(sc.array([[-sc.sin(dec_ngp),0.,sc.cos(dec_ngp)],[0.,1.,0.],[sc.cos(dec_ngp),0.,sc.sin(dec_ngp)]]),sc.array([[sc.cos(ra_ngp),sc.sin(ra_ngp),0.],[-sc.sin(ra_ngp),sc.cos(ra_ngp),0.],[0.,0.,1.]]))) transform={} transform['T']= T if sc.array(ra).shape == (): return radec_to_lb_single(ra,dec,transform,degree) else: function= sc.frompyfunc(radec_to_lb_single,4,2) return sc.array(function(ra,dec,transform,degree),dtype=sc.float64).T
def pmrapmdec_to_pmllpmbb(pmra,pmdec,ra,dec,degree=False,epoch=2000.0): """ NAME: pmrapmdec_to_pmllpmbb PURPOSE: rotate proper motions in (ra,dec) into proper motions in (l,b) INPUT: pmra - proper motion in ra (multplied with cos(dec)) [mas/yr] pmdec - proper motion in dec [mas/yr] ra - right ascension dec - declination degree - if True, ra and dec are given in degrees (default=False) epoch - epoch of ra,dec (right now only 2000.0 and 1950.0 are supported) OUTPUT: (pmll,pmbb) for vector inputs [:,2] HISTORY: 2010-04-07 - Written - Bovy (NYU) """ if sc.array(pmra).shape == (): l,b = radec_to_lb(ra,dec,degree=degree,epoch=epoch) return pmrapmdec_to_pmllpmbb_single(pmra,pmdec,ra,dec,b,degree,epoch) else: lb = radec_to_lb(ra,dec,degree=degree,epoch=epoch) function= sc.frompyfunc(pmrapmdec_to_pmllpmbb_single,7,2) return sc.array(function(pmra,pmdec,ra,dec,lb[:,1],degree,epoch),dtype=sc.float64).T
def pmllpmbb_to_pmrapmdec(pmll,pmbb,l,b,degree=False,epoch=2000.0): """ NAME: pmllpmbb_to_pmrapmdec PURPOSE: rotate proper motions in (l,b) into proper motions in (ra,dec) INPUT: pmll - proper motion in l (multplied with cos(b)) [mas/yr] pmbb - proper motion in b [mas/yr] l - Galactic longitude b - Galactic lattitude degree - if True, l and b are given in degrees (default=False) epoch - epoch of ra,dec (right now only 2000.0 and 1950.0 are supported) OUTPUT: (pmra,pmdec), for vector inputs [:,2] HISTORY: 2010-04-07 - Written - Bovy (NYU) """ if sc.array(pmll).shape == (): ra,dec = lb_to_radec(l,b,degree=degree,epoch=epoch) return pmllpmbb_to_pmrapmdec_single(pmll,pmbb,ra,dec,b,degree,epoch) else: radec = lb_to_radec(l,b,degree=degree,epoch=epoch) function= sc.frompyfunc(pmllpmbb_to_pmrapmdec_single,7,2) return sc.array(function(pmll,pmbb,radec[:,0],radec[:,1],b,degree, epoch),dtype=sc.float64).T
def vxvyvz_to_vrpmllpmbb(vx,vy,vz,l,b,d,XYZ=False,degree=False): """ NAME: vxvyvz_to_vrpmllpmbb PURPOSE: Transform velocities in the rectangular Galactic coordinate frame to the spherical Galactic coordinate frame Can take vector inputs INPUT: vx - velocity towards the Galactic Center (km/s) vy - velocity in the direction of Galactic rotation (km/s) vz - velocity towards the North Galactic Pole (km/s) l - Galactic longitude b - Galactic lattitude d - distance (kpc) XYZ - (bool) If True, then l,b,d is actually X,Y,Z (rectangular Galactic coordinates) degree - (bool) if True, l and b are in degrees OUTPUT: (vr,pmll,pmbb) in (km/s,mas/yr,mas/yr); pmll = mu_l * cos(b) HISTORY: 2009-10-24 - Written - Bovy (NYU) """ if sc.array(l).shape == (): return vxvyvz_to_vrpmllpmbb_single(vx,vy,vz,l,b,d,XYZ,degree) else: function= sc.frompyfunc(vxvyvz_to_vrpmllpmbb_single,8,3) return sc.array(function(vx,vy,vz,l,b,d,XYZ,degree),dtype=sc.float64).T
def vrpmllpmbb_to_vxvyvz(vr,pmll,pmbb,l,b,d,XYZ=False,degree=False): """ NAME: vrpmllpmbb_to_vxvyvz PURPOSE: Transform velocities in the spherical Galactic coordinate frame to the rectangular Galactic coordinate frame Can take vector inputs INPUT: vr - line-of-sight velocity (km/s) pmll - proper motion in the Galactic longitude (mu_l * cos(b))(mas/yr) pmbb - proper motion in the Galactic lattitude (mas/yr) l - Galactic longitude b - Galactic lattitude d - distance (kpc) XYZ - (bool) If True, then l,b,d is actually X,Y,Z (rectangular Galactic coordinates) degree - (bool) if True, l and b are in degrees OUTPUT: (vx,vy,vz) in (km/s,km/s,km/s) HISTORY: 2009-10-24 - Written - Bovy (NYU) """ if sc.array(l).shape == (): return vrpmllpmbb_to_vxvyvz_single(vr,pmll,pmbb,l,b,d,XYZ,degree) else: function= sc.frompyfunc(vrpmllpmbb_to_vxvyvz_single,8,3) return sc.array(function(vr,pmll,pmbb,l,b,d,XYZ,degree),dtype=sc.float64).T
def vxvyvz_to_vrpmllpmbb(vx, vy, vz, l, b, d, XYZ=False, degree=False): """ NAME: vxvyvz_to_vrpmllpmbb PURPOSE: Transform velocities in the rectangular Galactic coordinate frame to the spherical Galactic coordinate frame Can take vector inputs INPUT: vx - velocity towards the Galactic Center (km/s) vy - velocity in the direction of Galactic rotation (km/s) vz - velocity towards the North Galactic Pole (km/s) l - Galactic longitude b - Galactic lattitude d - distance (kpc) XYZ - (bool) If True, then l,b,d is actually X,Y,Z (rectangular Galactic coordinates) degree - (bool) if True, l and b are in degrees OUTPUT: (vr,pmll,pmbb) in (km/s,mas/yr,mas/yr); pmll = mu_l * cos(b) HISTORY: 2009-10-24 - Written - Bovy (NYU) """ if sc.array(l).shape == (): return vxvyvz_to_vrpmllpmbb_single(vx, vy, vz, l, b, d, XYZ, degree) else: function = sc.frompyfunc(vxvyvz_to_vrpmllpmbb_single, 8, 3) return sc.array(function(vx, vy, vz, l, b, d, XYZ, degree), dtype=sc.float64).T
def lb_to_radec(l,b,degree=False,epoch=2000.0): """ NAME: lb_to_radec PURPOSE: transform from Galactic coordinates to equatorial coordinates INPUT: l - Galactic longitude b - Galactic lattitude degree - (Bool) if True, l and b are given in degree and ra and dec will be as well epoch - epoch of target ra,dec (right now only 2000.0 and 1950.0 are supported) OUTPUT: ra,dec For vector inputs [:,2] HISTORY: 2010-04-07 - Written - Bovy (NYU) """ #First calculate the transformation matrix T' theta,dec_ngp,ra_ngp= get_epoch_angles(epoch) T= sc.dot(sc.array([[sc.cos(ra_ngp),-sc.sin(ra_ngp),0.],[sc.sin(ra_ngp),sc.cos(ra_ngp),0.],[0.,0.,1.]]),sc.dot(sc.array([[-sc.sin(dec_ngp),0.,sc.cos(dec_ngp)],[0.,1.,0.],[sc.cos(dec_ngp),0.,sc.sin(dec_ngp)]]),sc.array([[sc.cos(theta),sc.sin(theta),0.],[sc.sin(theta),-sc.cos(theta),0.],[0.,0.,1.]]))) transform={} transform['T']= T if sc.array(l).shape == (): return lb_to_radec_single(l,b,transform,degree) else: function= sc.frompyfunc(lb_to_radec_single,4,2) return sc.array(function(l,b,transform,degree),dtype=sc.float64).T
def GreensFunctionSCubic(x,W): ''' local Green's function electrons on a 3D sc lattice ''' ## scipy version of hyp2f1 has precision issues arouns some points #from scipy.special import hyp2f1 #K = lambda k: hyp2f1(0.5,0.5,1.0,k)*sp.pi/2.0 from mpmath import ellipk #K = sp.vectorize(ellipk) K = sp.frompyfunc(ellipk,1,1) x = (3.0/W)*x+1e-12j # scaling the half-width and securing the correct branch cut A = 0.5+1.0/(2.0*x**2)*(3.0-sp.sqrt(x**2-9.0)*sp.sqrt(x**2-1.0)) B = A/(A-1.0) kpm1 = 0.25*B*sp.sqrt(4.0-B) kpm2 = 0.25*(2.0-B)*sp.sqrt(1.0-B) ellip1 = K(0.5+kpm1-kpm2) ellip2 = K(0.5-kpm1-kpm2) return sp.array((3.0/W)*4.0*sp.sqrt(1.0-0.75*A)/(1.0-A)*ellip1*ellip2/(sp.pi**2*x),dtype=complex)
def GreensFunctionSC(x,W): ''' local Green's function electrons on a 3D sc lattice ''' ## scipy version of hyp2f1 has precision issues arouns some points #from scipy.special import hyp2f1 #K = lambda k: hyp2f1(0.5,0.5,1.0,k)*sp.pi/2.0 from mpmath import ellipk #K = sp.vectorize(ellipk) K = sp.frompyfunc(ellipk,1,1) x = (3.0/W)*x+1e-12j # scaling the half-width and securing the correct branch cut A = 0.5+1.0/(2.0*x**2)*(3.0-sp.sqrt(x**2-9.0)*sp.sqrt(x**2-1.0)) B = A/(A-1.0) kpm1 = 0.25*B*sp.sqrt(4.0-B) kpm2 = 0.25*(2.0-B)*sp.sqrt(1.0-B) ellip1 = K(0.5+kpm1-kpm2) ellip2 = K(0.5-kpm1-kpm2) return sp.array((3.0/W)*4.0*sp.sqrt(1.0-0.75*A)/(1.0-A)*ellip1*ellip2/(sp.pi**2*x),dtype=complex)
def lb_to_radec(l, b, degree=False, epoch=2000.0): """ NAME: lb_to_radec PURPOSE: transform from Galactic coordinates to equatorial coordinates INPUT: l - Galactic longitude b - Galactic lattitude degree - (Bool) if True, l and b are given in degree and ra and dec will be as well epoch - epoch of target ra,dec (right now only 2000.0 and 1950.0 are supported) OUTPUT: ra,dec For vector inputs [:,2] HISTORY: 2010-04-07 - Written - Bovy (NYU) """ #First calculate the transformation matrix T' theta, dec_ngp, ra_ngp = get_epoch_angles(epoch) T = sc.dot( sc.array([[sc.cos(ra_ngp), -sc.sin(ra_ngp), 0.], [sc.sin(ra_ngp), sc.cos(ra_ngp), 0.], [0., 0., 1.]]), sc.dot( sc.array([[-sc.sin(dec_ngp), 0., sc.cos(dec_ngp)], [0., 1., 0.], [sc.cos(dec_ngp), 0., sc.sin(dec_ngp)]]), sc.array([[sc.cos(theta), sc.sin(theta), 0.], [sc.sin(theta), -sc.cos(theta), 0.], [0., 0., 1.]]))) transform = {} transform['T'] = T if sc.array(l).shape == (): return lb_to_radec_single(l, b, transform, degree) else: function = sc.frompyfunc(lb_to_radec_single, 4, 2) return sc.array(function(l, b, transform, degree), dtype=sc.float64).T
def XYZ_to_lbd(X,Y,Z,degree=False): """ NAME: XYZ_to_lbd PURPOSE: transform from rectangular Galactic coordinates to spherical Galactic coordinates works with vector inputs INPUT: X - component towards the Galactic Center (in kpc; though this obviously does not matter)) Y - component in the direction of Galactic rotation (in kpc) Z - component towards the North Galactic Pole (kpc) degree - (Bool) if True, return l and b in degrees OUTPUT: [l,b,d] in (rad,rad,kpc) For vector inputs [:,3] HISTORY: 2009-10-24 - Written - Bovy (NYU) """ if sc.array(X).shape == (): return XYZ_to_lbd_single(X,Y,Z,degree) else: function= sc.frompyfunc(XYZ_to_lbd_single,4,3) return sc.array(function(X,Y,Z,degree),dtype=sc.float64).T
def lbd_to_XYZ(l,b,d,degree=False): """ NAME: lbd_to_XYZ PURPOSE: transform from spherical Galactic coordinates to rectangular Galactic coordinates works with vector inputs INPUT: l - Galactic longitude (rad) b - Galactic lattitude (rad) d - distance (arbitrary units) degree - (bool) if True, l and b are in degrees OUTPUT: [X,Y,Z] in whatever units d was in For vector inputs [:,3] HISTORY: 2009-10-24- Written - Bovy (NYU) """ if sc.array(l).shape == (): return lbd_to_XYZ_single(l,b,d,degree) else: function= sc.frompyfunc(lbd_to_XYZ_single,4,3) return sc.array(function(l,b,d,degree),dtype=sc.float64).T
def lbd_to_XYZ(l, b, d, degree=False): """ NAME: lbd_to_XYZ PURPOSE: transform from spherical Galactic coordinates to rectangular Galactic coordinates works with vector inputs INPUT: l - Galactic longitude (rad) b - Galactic lattitude (rad) d - distance (arbitrary units) degree - (bool) if True, l and b are in degrees OUTPUT: [X,Y,Z] in whatever units d was in For vector inputs [:,3] HISTORY: 2009-10-24- Written - Bovy (NYU) """ if sc.array(l).shape == (): return lbd_to_XYZ_single(l, b, d, degree) else: function = sc.frompyfunc(lbd_to_XYZ_single, 4, 3) return sc.array(function(l, b, d, degree), dtype=sc.float64).T
def XYZ_to_lbd(X, Y, Z, degree=False): """ NAME: XYZ_to_lbd PURPOSE: transform from rectangular Galactic coordinates to spherical Galactic coordinates works with vector inputs INPUT: X - component towards the Galactic Center (in kpc; though this obviously does not matter)) Y - component in the direction of Galactic rotation (in kpc) Z - component towards the North Galactic Pole (kpc) degree - (Bool) if True, return l and b in degrees OUTPUT: [l,b,d] in (rad,rad,kpc) For vector inputs [:,3] HISTORY: 2009-10-24 - Written - Bovy (NYU) """ if sc.array(X).shape == (): return XYZ_to_lbd_single(X, Y, Z, degree) else: function = sc.frompyfunc(XYZ_to_lbd_single, 4, 3) return sc.array(function(X, Y, Z, degree), dtype=sc.float64).T
def radec_to_lb(ra, dec, degree=False, epoch=2000.0): """ NAME: radec_to_lb PURPOSE: transform from equatorial coordinates to Galactic coordinates INPUT: ra - right ascension dec - declination degree - (Bool) if True, ra and dec are given in degree and l and b will be as well epoch - epoch of ra,dec (right now only 2000.0 and 1950.0 are supported) OUTPUT: l,b For vector inputs [:,2] HISTORY: 2009-11-12 - Written - Bovy (NYU) """ #First calculate the transformation matrix T theta, dec_ngp, ra_ngp = get_epoch_angles(epoch) T = sc.dot( sc.array([[sc.cos(theta), sc.sin(theta), 0.], [sc.sin(theta), -sc.cos(theta), 0.], [0., 0., 1.]]), sc.dot( sc.array([[-sc.sin(dec_ngp), 0., sc.cos(dec_ngp)], [0., 1., 0.], [sc.cos(dec_ngp), 0., sc.sin(dec_ngp)]]), sc.array([[sc.cos(ra_ngp), sc.sin(ra_ngp), 0.], [-sc.sin(ra_ngp), sc.cos(ra_ngp), 0.], [0., 0., 1.]]))) transform = {} transform['T'] = T if sc.array(ra).shape == (): return radec_to_lb_single(ra, dec, transform, degree) else: function = sc.frompyfunc(radec_to_lb_single, 4, 2) return sc.array(function(ra, dec, transform, degree), dtype=sc.float64).T
for i in range(0,len(value)): x -= distribution[i] * BASE if x < 0: j = i break else: j = i return value[j] def sigmoid(y): return 1.0 / (1 + math.exp(-y)) def tanh(y): if abs(y) > 400: return 1.0 return (math.exp(y) - math.exp(-y)) / (math.exp(y) + math.exp(-y)) sigmoid_ufunc = sp.frompyfunc(sigmoid, 1, 1) tanh_ufunc = sp.frompyfunc(tanh, 1, 1) class EchoNetwork(object): def __init__(self, unit_type_ufunc=None, input_unit_amount=1, internal_unit_amount=100, output_unit_amount=1, has_feedback=False, state=None, input_matrix=None, internal_matrix=None, feedback_matrix=None,): if state: self.state = sp.array(state).T else: self.state = sp.rand(internal_unit_amount,1) self.unit_type_ufunc = unit_type_ufunc self.internal_unit_amount = internal_unit_amount self.input_unit_amount = input_unit_amount self.output_unit_amount = output_unit_amount
else: j = i return value[j] def sigmoid(y): return 1.0 / (1 + math.exp(-y)) def tanh(y): if abs(y) > 400: return 1.0 return (math.exp(y) - math.exp(-y)) / (math.exp(y) + math.exp(-y)) sigmoid_ufunc = sp.frompyfunc(sigmoid, 1, 1) tanh_ufunc = sp.frompyfunc(tanh, 1, 1) class EchoNetwork(object): def __init__( self, unit_type_ufunc=None, input_unit_amount=1, internal_unit_amount=100, output_unit_amount=1, has_feedback=False, state=None, input_matrix=None, internal_matrix=None, feedback_matrix=None,