In [1]:
%load_ext watermark

In [2]:
%watermark


30/06/2014 21:28:32

CPython 3.4.1
IPython 2.1.0

compiler   : GCC 4.2.1 (Apple Inc. build 5577)
system     : Darwin
release    : 13.2.0
machine    : x86_64
processor  : i386
CPU cores  : 2
interpreter: 64bit

[More information](http://nbviewer.ipython.org/github/rasbt/python_reference/blob/master/ipython_magic/watermark.ipynb) about the `watermark` magic command extension.



Counting points inside a hypercube


Given a vector of coordinates, we want to determine how many points lie inside a hypercube (a hypercube is the more general concept of a 3-dimensional cube in $d$ dimensions):

\begin{equation} \begin{pmatrix} x_i\\ x_{i+1} \\ ... \\ x_{d} \end{pmatrix} \end{equation}

E.g., in a 3-dimensional case in a kartesian coordinate system:

\begin{equation} \begin{pmatrix} x\\ y\\ z \end{pmatrix} \end{equation}

To approach this problem more mathematically, we would use the following equation to count the samples $k_n$ within this hypercube, where $\phi$ is our so-called window function:

$\phi(\pmb u) = \Bigg[ \begin{array}{ll} 1 & \quad |u_j| \leq 1/2 \; ;\quad \quad j = 1, ..., d \\ 0 & \quad otherwise \end{array}$

for a hypercube of unit length 1 centered at the coordinate system's origin. What this function basically does is assigning a value 1 to a sample point if it lies within 1/2 of the edges of the hypercube, and 0 if lies outside (note that the evaluation is done for all dimensions of the sample point).

If we extend on this concept, we can define a more general equation that applies to hypercubes of any length $h_n$ that are centered at $\pmb x$:

$k_n = \sum\limits_{i=1}^{n} \phi \bigg( \frac{\pmb x - \pmb x_i}{h_n} \bigg)$

where $\pmb u = \bigg( \frac{\pmb x - \pmb x_i}{h_n} \bigg)$



Implementation


In [5]:
def inside_hypercube(x_vec, unit_len=1):
    """ Function to check if a point lies inside a hypercube."""
    for ele in x_vec:
        if abs(ele) > (unit_len/2):
            return False
    return True



Example 3D-hypercubes

So let us visualize such an simple example: a typical 3-dimensional unit hypercube ($h_1 = 1$) and 10 sample points, where 3 of them lie within the hypercube (red triangles), and the other 7 outside (black dots).


In [6]:
samples_3d = [
            [0, 0, 0], [0.2, 0.2, 0.2], [0.1, -0.1, -0.3],
            [-1.2,0.3,-0.3], [0.8,-0.82,-0.9], [1, 0.6, -0.7],
            [0.8,0.7,0.2], [0.7,-0.8,-0.45], [-0.3, 0.6, 0.9],
            [0.7,-0.6,-0.8]
            ]

In [4]:
%matplotlib inline

In [7]:
inside = []
outside = []
for vec in samples_3d:
    if inside_hypercube(vec, unit_len=1):
        inside.append(vec)
    else:
        outside.append(vec)

In [9]:
from mpl_toolkits.mplot3d import Axes3D
import matplotlib.pyplot as plt
import numpy as np
from itertools import product, combinations
fig = plt.figure(figsize=(7,7))
ax = fig.gca(projection='3d')
ax.set_aspect("equal")


for row in inside:
    ax.scatter(row[0], row[1], row[2], color="r", s=50, marker='^')

for row in outside:    
    ax.scatter(row[0], row[1], row[2], color="k", s=50)

# Plot Cube centered at the coordinate origin X=0, Y=0, Z=0
h = [-0.5, 0.5]
for s, e in combinations(np.array(list(product(h,h,h))), 2):
    if np.sum(np.abs(s-e)) == h[1]-h[0]:
        ax.plot3D(*zip(s,e), color="g")
        
ax.set_xlim(-1.5, 1.5)
ax.set_ylim(-1.5, 1.5)
ax.set_zlim(-1.5, 1.5)

plt.show()