Was ist der schnellste Weg, um alle N-Bit-Graucodes mit Numpy zu erzeugen?Python

Python-Programme
Anonymous
 Was ist der schnellste Weg, um alle N-Bit-Graucodes mit Numpy zu erzeugen?

Post by Anonymous »

My goal is to create images using gray codes, an example would be this:
Image

It is all modulo 64 groups in gray codes in polar form.
Now of Natürlich kenne ich die einfache Zuordnung n ^ (n >> 1) aus Binary, aber ich hatte effizientere Möglichkeiten gefunden, Graucodes direkt zu generieren als die Verwendung dieser Zuordnung. Da jedoch binäre Codes verwandt sind, werde ich auch Binärcodes postieren. Ich möchte es so effizient wie möglich und es muss in Numpy implementiert und nur in Numpy implementiert werden, keine anderen Bibliotheken sind erlaubt. Da ich Scipy , Pil , cv2 , Matplotlib , numba installiert habe, benötigen alle Versionen von Numpy und das Aktualisieren eines Bruchs der Abhängigkeit eines anderen, und alle bieten eine Reihe von Methoden. Ich versuche gerade, mich mit Numpy vertraut zu machen, also habe ich diese Herausforderung erfunden, um mich zu lernen. Bisher habe ich festgestellt, dass Np.unpackbits die effizienteste Methode ist, um binäre Bits einer Zahl zu erhalten, aber es funktioniert nur mit np.uint8 , das ist leicht zu lösen. NP.Unpackbits ist es weniger effizient, ihn von Binary in Grau -Code zu konvertieren als direkte Grey -Codes.

Code: Select all

np.concatenate(arrs, axis=-1)
schlägt np.hstack (arrs) und np.concatenate (arrs) .reshape ((w, h)). Und irgendwie kann das Initialisieren eines Arrays und dann zu einzelnen Spalten mit einer Schleife effizienter sein als die Verwendung von NP.Concatenat . Effizient als nur durch die Spalten durchlaufen und die Spalte anwenden und für die Spalte anwenden. Insbesondere (a & b [:, keine]). T.Astype (bool) ist viel effizienter als (a [:, none] & b) .Astype (bool) .

Code: Select all

import numpy as np

lo = 1
hi = 8
UINT_BITS = {}
for dtype in (np.uint8, np.uint16, np.uint32, np.uint64):
for i in range(lo, hi + 1):
UINT_BITS[i] = dtype

lo = hi + 1
hi  None:
if not (n and isinstance(n, int)):
raise ValueError(f"Argument {n} is not a valid bit width")

def binary_codes_0(n: int) -> np.ndarray:
validate(n)
count = 1 >= 1
rect[:, i] = np.tile(
np.concatenate([np.zeros(r, dtype=bool), np.ones(r, dtype=bool)]), count
)
r  1), dtype=bool), np.ones(r, dtype=bool)]
),
1  np.ndarray:
validate(n)
chunks = np.array([(0,), (1,)], dtype=bool)
l = 2
for _ in range(n - 1):
chunks = np.concatenate(
[
np.concatenate([np.zeros((l, 1), dtype=bool), chunks], axis=-1),
np.concatenate([np.ones((l, 1), dtype=bool), chunks], axis=-1),
]
)
l  np.ndarray:
numbers = np.arange(1  np.ndarray:
return np.unpackbits(
np.arange(1  np.ndarray:
validate(n)
return np.array(np.meshgrid(*[(0, 1)] * n, indexing="ij")).reshape((n, 1  np.ndarray:
numbers = np.arange((count := 1 > 1)
return (
np.concatenate([(gray & 1  np.ndarray:
numbers = np.arange((count := 1 > 1)
result = np.zeros((count, n), dtype=bool)
for i in range(n):
result[:, i] = gray & (count := count >> 1)

return result

def gray_codes_2(n: int) -> np.ndarray:
validate(n)
binary = binary_codes_6(n)
shifted = np.roll(binary, 1, axis=-1)
shifted[:, 0] = 0
return binary ^ shifted

def gray_codes_3(n: int) ->  np.ndarray:
validate(n)
gray = np.array([(0,), (1,)], dtype=bool)
l = 2
for _ in range(n - 1):
gray = np.concatenate(
[
np.concatenate([np.zeros((l, 1), dtype=bool), gray], axis=-1),
np.concatenate([np.ones((l, 1), dtype=bool), gray[::-1]], axis=-1),
]
)
l 
import numpy as np
zeros = np.zeros(524288, dtype=bool)
ones = np.ones(524288, dtype=bool)
zeros1 = np.zeros((524288, 32), dtype=bool)
ones1 = np.ones((524288, 32), dtype=bool)
million = [list(range(i*4096, i*4096+4096)) for i in range(256)]
numbers = np.arange(1 
Is there a more efficient way to generate all n-bit gray codes?

I have figured out how to use np.meshgrid
kartesische Produkte durchführen, und es ist viel langsamer als erwartet. Ich habe den obigen Code bearbeitet, um ihn einzuschließen. < /P>

Code: Select all

In [82]: %timeit binary_codes_7(16)
6.96 ms ± 249 μs per loop (mean ± std. dev. of 7 runs, 100 loops each)

In [83]: %timeit binary_codes_5(16)
1.74 ms ± 36.4 μs per loop (mean ± std. dev. of 7 runs, 1,000 loops each)

In [84]: %timeit binary_codes_3(16)
1.65 ms ± 15.8 μs per loop (mean ± std. dev. of 7 runs, 1,000 loops each)

In [85]: %timeit np.meshgrid(*[(0, 1)] * 16, indexing="ij")
4.33 ms ± 49.5 μs per loop (mean ± std. dev.  of 7 runs, 100 loops each)

In [86]: np.all(np.array(np.meshgrid(*[(0, 1)] * 5, indexing="ij")).reshape((5, 32)).T == binary_codes_3(5))
Out[86]: np.True_
< /code>
Now I have implemented everything I can think of.

At this point I have realized this problem is extremely simple, we don't even need any bit operations at all.
In both binary and gray codes, each cell can only have two values, zero and one. It can only be one or zero.
Now if we have np.zeros((1 
But this supposedly is slow (I haven't benchmarked this), however if this is implemented in numpy
dann denke ich, dass dies der effizienteste Algorithmus für diese Art von Sequenzen ist. Die Frage ist, wie ich das implementieren kann?

Quick Reply

Change Text Case: 
   
  • Similar Topics
    Replies
    Views
    Last post