Skip to content

Instantly share code, notes, and snippets.

@astrojuanlu
Created May 13, 2012 19:08
Show Gist options
  • Select an option

  • Save astrojuanlu/2689795 to your computer and use it in GitHub Desktop.

Select an option

Save astrojuanlu/2689795 to your computer and use it in GitHub Desktop.
Símbolo de Levi-Civita en Python
# coding: utf-8
# Símbolo de Levi-Civita en Python utilizando listas por comprensión
# Fórmula extraída de http://en.wikipedia.org/wiki/Levi-Civita_symbol#Three_dimensions
# Válido para Python 2 y 3
#
# Juan Luis Cano Rodríguez <juanlu001@gmail.com>
from __future__ import print_function
import numpy as np
e = np.array([[[int((i - j) * (j - k) * (k - i) / 2) for k in range(3)] for j in range(3)] for i in range(3)])
print(e)
[[[ 0. 0. 0.]
[ 0. 0. 1.]
[ 0. -1. 0.]]
[[ 0. 0. -1.]
[ 0. 0. 0.]
[ 1. 0. 0.]]
[[ 0. 1. 0.]
[-1. 0. 0.]
[ 0. 0. 0.]]]
@simone-romiti
Copy link
Copy Markdown

I would like to point out that the Levi-Civita tensor is available in sympy.
Here's a code equivalent to the one above.

from sympy import LeviCivita
import numpy as np
eps = np.array([[[ float(LeviCivita(i,j,k)) for k in range(3)] for j in range(3)] for i in range(3)])
print(eps)

The generalization to higher dimensions is done by adding extra dimensions and increasing the bounds of each range in the loops.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment