Skip to content

Instantly share code, notes, and snippets.

@nbecker
Created December 23, 2021 15:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nbecker/76ac6e37652e7b0424886f5c9cbfa33d to your computer and use it in GitHub Desktop.
Save nbecker/76ac6e37652e7b0424886f5c9cbfa33d to your computer and use it in GitHub Desktop.
transonic bounds check
import numpy as np
from transonic import boost
A = "complex[:]"
S = "int[:]"
@boost(boundscheck=True)
def call1 (map: A, s: int):
return map[s]
@boost
def call2 (map: A, syms: 'int[:] or int32[:]'):
out = np.empty(len(syms), dtype=complex)
for i in range (len (syms)):
out[i] = map[syms[i]]
return out
class constellation:
#map: A
def __init__(self, map: A):
self.map = np.require (map, dtype=complex)
self.bps = int (np.log2 (len (self.map)))
def __call__(self, s):
if hasattr (s, '__len__'):
return call2 (self.map, s)
else:
return call1 (self.map, s)
def __getitem__(self, s):
return self.__call__(s)
@property
def size(self):
return len(self.map)
@property
def bits_per_sym(self):
return self.bps
from constellation import constellation
import numpy as np
A = np.array
u = A([1+0j, -1+0j])
c = constellation (u)
syms = A ([0,1])
xconst = c(syms)
x = c(0)
c[10] # should be caught by boundscheck
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment