Giter Club home page Giter Club logo

numpy-hilbert-curve's Introduction

numpy-hilbert-curve

This is a numpy-based implementation of Hilbert curves, for up to a few tens of dimensions. A Hilbert curve is a continuous space-filling curve that lets you map from a single dimension into multiple dimensions. In two dimensions, you get curves that look like this:

2d Hilbert Curves

In three dimensions, you get curves that look like this:

3d Hilbert Curves

This is working entirely in terms of integers, so the size of the (hyper-) cube reflects the number of bits per dimension. You could normalize this to put it into the unit hypercube with floating point numbers.

The mechanics of the implementation rely on the Gray-code "correction" procedure presented in

Skilling, J. (2004, April). Programming the Hilbert curve. In AIP Conference Proceedings (Vol. 707, No. 1, pp. 381-387). American Institute of Physics.

This paper does a lot of bit twiddling in C. I replicate this with "bool-twiddling" in numpy, but of course numpy represents a bool value with 8 bits, so don't expect it to really have the same performance as the equivalent C code. However, my goal here (rather than write it in Cython) was to make it easy to integrate with code you're already writing in Python with numpy.

Installation

You can install this via PyPI:

$ pip install numpy-hilbert-curve

Usage

The basic usage looks like this:

import numpy as np
from hilbert import decode, encode

# Turn an ndarray of Hilber integers into locations.
# 2 is the number of dimensions, 3 is the number of bits per dimension
locs = decode(np.array([1,2,3]), 2, 3)

print(locs)
# prints [[0 1]
#         [1 1]
#         [1 0]]

# You can go the other way also, of course.
H = encode(locs, 2, 3)

print(H)
# prints array([1, 2, 3], dtype=uint64)

The reason things like Hilbert curves are interesting is because they preserve some amount of locality. In this figure, I'm gradually changing the color in the Hilbert integers, using the "copper" colormap:

2d Color Hilbert Curves

In this picture, I'm doing the same thing in 3d:

3d Color Hilbert Curves

numpy-hilbert-curve's People

Contributors

rpadams avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.