Skip to content

Commit

Permalink
Merge pull request #935 from Li-Xiang-Ideal/patch-1
Browse files Browse the repository at this point in the history
Update tensors.py
  • Loading branch information
rocky authored Nov 16, 2023
2 parents 42be678 + 3500cab commit e7b7cca
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 3 deletions.
7 changes: 4 additions & 3 deletions CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,10 @@ New Builtins
++++++++++++


* `Elements`
* `RealAbs` and `RealSign`
* `RealValuedNumberQ`
* ``Elements``
* ``LeviCivitaTensor``
* ``RealAbs`` and ``RealSign``
* ``RealValuedNumberQ``


Compatibility
Expand Down
48 changes: 48 additions & 0 deletions mathics/builtin/tensors.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,18 @@
"""


from sympy.combinatorics import Permutation
from sympy.utilities.iterables import permutations

from mathics.core.atoms import Integer, String
from mathics.core.attributes import A_FLAT, A_ONE_IDENTITY, A_PROTECTED
from mathics.core.builtin import BinaryOperator, Builtin
from mathics.core.convert.python import from_python
from mathics.core.evaluation import Evaluation
from mathics.core.expression import Expression
from mathics.core.list import ListExpression
from mathics.core.symbols import Atom, Symbol, SymbolFalse, SymbolTrue
from mathics.core.systemsymbols import SymbolRule, SymbolSparseArray
from mathics.eval.parts import get_part


Expand Down Expand Up @@ -490,3 +495,46 @@ def eval(self, m, evaluation: Evaluation):
else:
result[col_index].append(item)
return ListExpression(*[ListExpression(*row) for row in result])


class LeviCivitaTensor(Builtin):
"""
<url>:Levi-Civita tensor:https://en.wikipedia.org/wiki/Levi-Civita_symbol</url> \
(<url>:WMA link:https://reference.wolfram.com/language/ref/LeviCivitaTensor.html</url>)
<dl>
<dt>'LeviCivitaTensor[$d$]'
<dd>gives the $d$-dimensional Levi-Civita totally antisymmetric tensor.
</dl>
>> LeviCivitaTensor[3]
= SparseArray[Automatic, {3, 3, 3}, 0, {{1, 2, 3} -> 1, {1, 3, 2} -> -1, {2, 1, 3} -> -1, {2, 3, 1} -> 1, {3, 1, 2} -> 1, {3, 2, 1} -> -1}]
>> LeviCivitaTensor[3, List]
= {{{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}}}
"""

rules = {
"LeviCivitaTensor[d_Integer]/; Greater[d, 0]": "LeviCivitaTensor[d, SparseArray]",
"LeviCivitaTensor[d_Integer, List] /; Greater[d, 0]": "LeviCivitaTensor[d, SparseArray] // Normal",
}

summary_text = "give the Levi-Civita tensor with a given dimension"

def eval(self, d, type, evaluation: Evaluation):
"LeviCivitaTensor[d_Integer, type_]"

if isinstance(d, Integer) and type == SymbolSparseArray:
d = d.get_int_value()
perms = list(permutations([i for i in range(1, d + 1)]))
rules = [
Expression(
SymbolRule,
from_python(p),
from_python(Permutation.from_sequence(p).signature()),
)
for p in perms
]
return Expression(
SymbolSparseArray, from_python(rules), from_python([d] * d)
)

0 comments on commit e7b7cca

Please sign in to comment.