In [1]:
import numpy as np
def comb(shape, delta, offset):
shape = np.array(shape)
assert shape.size <= 3
g = np.zeros(shape)
if shape.size == 1:
g[offset::delta] = 1
elif shape.size == 2:
g[offset[0]::delta[0], offset[1]::delta[1]] = 1
elif shape.size == 3:
g[offset[0]::delta[0], offset[1]::delta[1], offset[2]::delta[2]] = 1
return g
In [1]:
testing = (__name__ == "__main__")
if testing:
! jupyter nbconvert --to python comb.ipynb
import numpy as np
import sys,os
import matplotlib.image as mpimg
ia898path = os.path.abspath('../../')
if ia898path not in sys.path:
sys.path.append(ia898path)
import ia898.src as ia
In [2]:
if testing:
u1 = ia.comb(10, 3, 2)
print('u1=',u1)
u2 = ia.comb((10,), 3, 2)
print('u2=',u2)
In [3]:
if testing:
u3 = ia.comb((7,9), (1,2), (0,1))
print('u3=\n',u3)
In [4]:
if testing:
u4 = ia.comb((4,5,9), (2,1,2), (1,0,1))
print(u4)
N-dimension:
$$ \begin{matrix} u\left( x_{0},x_{1},\cdots ,x_{N-1}\right) =\sum _{i_{0}=0}^{\infty}\sum _{i_{1}=0}^{\infty}\cdots \sum _{i_{N-1}=0}^{\infty}\delta \left( x_{0}-\left( k_{0}i_{0}+o_{0}\right) ,x_{1}-\left( k_{1}i_{1}+o_{1}\right) ,\cdots ,x_{N-1}-\left(k_{N-1}i_{N-1}+o_{N-1}\right) \right)\\ \left( x_{0},x_{1},\cdots ,x_{N-1}\right) \in \left[ \left( 0,0,\cdots ,0\right) ,\left( S_{0}-1,S_{1}-1,\cdots ,S_{N-1}-1\right) \right] \end{matrix} $$
In [5]:
if testing:
print('testing comb')
print(repr(ia.comb(10, 3, 2)) == repr(np.array(
[0., 0., 1., 0., 0., 1., 0., 0., 1., 0.])))
print(repr(ia.comb((7,9), (3,4), (3,2))) == repr(np.array(
[[0., 0., 0., 0., 0., 0., 0., 0., 0.],
[0., 0., 0., 0., 0., 0., 0., 0., 0.],
[0., 0., 0., 0., 0., 0., 0., 0., 0.],
[0., 0., 1., 0., 0., 0., 1., 0., 0.],
[0., 0., 0., 0., 0., 0., 0., 0., 0.],
[0., 0., 0., 0., 0., 0., 0., 0., 0.],
[0., 0., 1., 0., 0., 0., 1., 0., 0.]])))
In [ ]:
In [ ]: