File size: 5,228 Bytes
97aa5af | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 | """ sinc(t) := sin(t) / t """
import torch
from torch import sin, cos
def sinc1(t):
""" sinc1: t -> sin(t)/t """
e = 0.01
r = torch.zeros_like(t)
a = torch.abs(t)
s = a < e
c = (s == 0)
t2 = t[s] ** 2
r[s] = 1 - t2/6*(1 - t2/20*(1 - t2/42)) # Taylor series O(t^8)
r[c] = sin(t[c]) / t[c]
return r
def sinc1_dt(t):
""" d/dt(sinc1) """
e = 0.01
r = torch.zeros_like(t)
a = torch.abs(t)
s = a < e
c = (s == 0)
t2 = t ** 2
r[s] = -t[s]/3*(1 - t2[s]/10*(1 - t2[s]/28*(1 - t2[s]/54))) # Taylor series O(t^8)
r[c] = cos(t[c])/t[c] - sin(t[c])/t2[c]
return r
def sinc1_dt_rt(t):
""" d/dt(sinc1) / t """
e = 0.01
r = torch.zeros_like(t)
a = torch.abs(t)
s = a < e
c = (s == 0)
t2 = t ** 2
r[s] = -1/3*(1 - t2[s]/10*(1 - t2[s]/28*(1 - t2[s]/54))) # Taylor series O(t^8)
r[c] = (cos(t[c]) / t[c] - sin(t[c]) / t2[c]) / t[c]
return r
def rsinc1(t):
""" rsinc1: t -> t/sinc1(t) """
e = 0.01
r = torch.zeros_like(t)
a = torch.abs(t)
s = a < e
c = (s == 0)
t2 = t[s] ** 2
r[s] = (((31*t2)/42 + 7)*t2/60 + 1)*t2/6 + 1 # Taylor series O(t^8)
r[c] = t[c] / sin(t[c])
return r
def rsinc1_dt(t):
""" d/dt(rsinc1) """
e = 0.01
r = torch.zeros_like(t)
a = torch.abs(t)
s = a < e
c = (s == 0)
t2 = t[s] ** 2
r[s] = ((((127*t2)/30 + 31)*t2/28 + 7)*t2/30 + 1)*t[s]/3 # Taylor series O(t^8)
r[c] = 1/sin(t[c]) - (t[c]*cos(t[c]))/(sin(t[c])*sin(t[c]))
return r
def rsinc1_dt_csc(t):
""" d/dt(rsinc1) / sin(t) """
e = 0.01
r = torch.zeros_like(t)
a = torch.abs(t)
s = a < e
c = (s == 0)
t2 = t[s] ** 2
r[s] = t2*(t2*((4*t2)/675 + 2/63) + 2/15) + 1/3 # Taylor series O(t^8)
r[c] = (1/sin(t[c]) - (t[c]*cos(t[c]))/(sin(t[c])*sin(t[c]))) / sin(t[c])
return r
def sinc2(t):
""" sinc2: t -> (1 - cos(t)) / (t**2) """
e = 0.01
r = torch.zeros_like(t)
a = torch.abs(t)
s = a < e
c = (s == 0)
t2 = t ** 2
r[s] = 1/2*(1-t2[s]/12*(1-t2[s]/30*(1-t2[s]/56))) # Taylor series O(t^8)
r[c] = (1-cos(t[c]))/t2[c]
return r
def sinc2_dt(t):
""" d/dt(sinc2) """
e = 0.01
r = torch.zeros_like(t)
a = torch.abs(t)
s = a < e
c = (s == 0)
t2 = t ** 2
r[s] = -t[s]/12*(1 - t2[s]/5*(1.0/3 - t2[s]/56*(1.0/2 - t2[s]/135))) # Taylor series O(t^8)
r[c] = sin(t[c])/t2[c] - 2*(1-cos(t[c]))/(t2[c]*t[c])
return r
def sinc3(t):
""" sinc3: t -> (t - sin(t)) / (t**3) """
e = 0.01
r = torch.zeros_like(t)
a = torch.abs(t)
s = a < e
c = (s == 0)
t2 = t[s] ** 2
r[s] = 1/6*(1-t2/20*(1-t2/42*(1-t2/72))) # Taylor series O(t^8)
r[c] = (t[c]-sin(t[c]))/(t[c]**3)
return r
def sinc3_dt(t):
""" d/dt(sinc3) """
e = 0.01
r = torch.zeros_like(t)
a = torch.abs(t)
s = a < e
c = (s == 0)
t2 = t[s] ** 2
r[s] = -t[s]/60*(1 - t2/21*(1 - t2/24*(1.0/2 - t2/165))) # Taylor series O(t^8)
r[c] = (3*sin(t[c]) - t[c]*(cos(t[c]) + 2))/(t[c]**4)
return r
def sinc4(t):
""" sinc4: t -> 1/t^2 * (1/2 - sinc2(t))
= 1/t^2 * (1/2 - (1 - cos(t))/t^2)
"""
e = 0.01
r = torch.zeros_like(t)
a = torch.abs(t)
s = a < e
c = (s == 0)
t2 = t ** 2
r[s] = 1/24*(1-t2/30*(1-t2/56*(1-t2/90))) # Taylor series O(t^8)
r[c] = (0.5 - (1 - cos(t))/t2) / t2
class Sinc1_autograd(torch.autograd.Function):
@staticmethod
def forward(ctx, theta):
ctx.save_for_backward(theta)
return sinc1(theta)
@staticmethod
def backward(ctx, grad_output):
theta, = ctx.saved_tensors
grad_theta = None
if ctx.needs_input_grad[0]:
grad_theta = grad_output * sinc1_dt(theta).to(grad_output)
return grad_theta
Sinc1 = Sinc1_autograd.apply
class RSinc1_autograd(torch.autograd.Function):
@staticmethod
def forward(ctx, theta):
ctx.save_for_backward(theta)
return rsinc1(theta)
@staticmethod
def backward(ctx, grad_output):
theta, = ctx.saved_tensors
grad_theta = None
if ctx.needs_input_grad[0]:
grad_theta = grad_output * rsinc1_dt(theta).to(grad_output)
return grad_theta
RSinc1 = RSinc1_autograd.apply
class Sinc2_autograd(torch.autograd.Function):
@staticmethod
def forward(ctx, theta):
ctx.save_for_backward(theta)
return sinc2(theta)
@staticmethod
def backward(ctx, grad_output):
theta, = ctx.saved_tensors
grad_theta = None
if ctx.needs_input_grad[0]:
grad_theta = grad_output * sinc2_dt(theta).to(grad_output)
return grad_theta
Sinc2 = Sinc2_autograd.apply
class Sinc3_autograd(torch.autograd.Function):
@staticmethod
def forward(ctx, theta):
ctx.save_for_backward(theta)
return sinc3(theta)
@staticmethod
def backward(ctx, grad_output):
theta, = ctx.saved_tensors
grad_theta = None
if ctx.needs_input_grad[0]:
grad_theta = grad_output * sinc3_dt(theta).to(grad_output)
return grad_theta
Sinc3 = Sinc3_autograd.apply
#EOF
|