Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update tensors.py #935

Merged
merged 7 commits into from
Nov 16, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)
)
Loading