Skip to content

Commit

Permalink
added latin/greek keywords to index command
Browse files Browse the repository at this point in the history
  • Loading branch information
kennethsible committed May 15, 2024
1 parent 638c225 commit 79c767a
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 42 deletions.
10 changes: 5 additions & 5 deletions .github/workflows/main.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ jobs:
- name: install python
uses: actions/setup-python@v4
with:
python-version: 3.7
python-version: 3.8.x
- name: install sympy (latest stable version)
run: python -m pip install ipython sympy
- name: perform unit testing
Expand All @@ -24,7 +24,7 @@ jobs:
- name: install python
uses: actions/setup-python@v4
with:
python-version: 3.7
python-version: 3.8.x
- name: install sympy (latest stable version)
run: python -m pip install ipython sympy
- name: perform unit testing
Expand All @@ -38,7 +38,7 @@ jobs:
- name: install python
uses: actions/setup-python@v4
with:
python-version: 3.7
python-version: 3.8.x
- name: install sympy (latest stable version)
run: python -m pip install ipython sympy
- name: perform unit testing
Expand All @@ -52,7 +52,7 @@ jobs:
- name: install python
uses: actions/setup-python@v4
with:
python-version: 3.6
python-version: 3.8.x
- name: install sympy (earliest supported version)
run: python -m pip install ipython sympy==1.2
- name: perform unit testing
Expand All @@ -66,7 +66,7 @@ jobs:
- name: install python
uses: actions/setup-python@v4
with:
python-version: 3.8
python-version: 3.8.x
- name: install ipython
run: python -m pip install ipython
- name: install sympy (latest development version)
Expand Down
57 changes: 30 additions & 27 deletions nrpylatex/core/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,13 @@
from collections import OrderedDict
import re

LATIN_ALPHABET = [chr(i) for i in range(97, 123)]
GREEK_ALPHABET = [
'alpha', 'beta', 'gamma', 'delta', 'epsilon', 'zeta', 'eta', 'theta',
'iota', 'kappa', 'lambda', 'mu', 'nu', 'xi', 'omikron', 'pi', 'rho',
'sigma', 'tau', 'upsilon', 'phi', 'chi', 'psi', 'omega'
]

class Parser:

_namespace, _property = OrderedDict(), {}
Expand All @@ -33,12 +40,8 @@ def initialize(reset=False):
if reset: Parser._namespace.clear()
Parser._property['replace'] = []
Parser._property['coord'] = CoordinateSystem('x')
Parser._property['index'] = {chr(i): 3 for i in range(97, 123)}
Parser._property['index'].update({i: 4 for i in [
'alpha', 'beta', 'gamma', 'delta', 'epsilon', 'zeta', 'eta', 'theta',
'iota', 'kappa', 'lambda', 'mu', 'nu', 'xi', 'omikron', 'pi', 'rho',
'sigma', 'tau', 'upsilon', 'phi', 'chi', 'psi', 'omega'
]})
Parser._property['index'] = {index: 3 for index in LATIN_ALPHABET}
Parser._property['index'].update({index: 4 for index in GREEK_ALPHABET})
Parser._property['ignore'] = ['\\left', '\\right', '{}', '&']
Parser._property['metric'] = {'': '', 'bar': '', 'hat': '', 'tilde': ''}
Parser._property['suffix'] = 'dD'
Expand Down Expand Up @@ -149,10 +152,10 @@ def _declare(self):
while True:
symbols.append(self.scanner.lexeme)
self.expect('IDENTIFIER')
if self.peek('LDASH') or self.peek('LINEBREAK'): break
if self.peek('DBL_DASH') or self.peek('LINEBREAK'): break
dimension = symmetry = weight = suffix = metric = None
const = zeros = False
while self.accept('LDASH'):
while self.accept('DBL_DASH'):
if self.accept('CONST_OPT'):
const = True
elif self.accept('ZEROS_OPT'):
Expand Down Expand Up @@ -257,7 +260,7 @@ def _replace(self):
self.scanner.lex()
self.accept('LINEBREAK')

# <COORD> -> <COORD_KWD> { <IDENTIFIER> }*
# <COORD> -> <COORD_KWD> ( { <IDENTIFIER> }+ | <DEFAULT_KWD> )
def _coord(self):
self.expect('COORD_KWD')
del self._property['coord'][:]
Expand All @@ -272,33 +275,33 @@ def _coord(self):
(sentence[position], position), sentence, position)
self._property['coord'].append(symbol)
if self.peek('LINEBREAK'): break
else:
elif self.accept('DEFAULT_KWD'):
self._property['coord'] = CoordinateSystem('x')
else:
sentence, (position, _) = self.scanner.sentence, self.scanner.prev_state
raise ParserError('unexpected \'%s\' at position %d' %
(sentence[position], position), sentence, position)
self.accept('LINEBREAK')

# <INDEX> -> <INDEX_KWD> { <IDENTIFIER> [ '..' <IDENTIFIER> ] }* '--' <DIM_OPT> <INTEGER>
# <INDEX> -> <INDEX_KWD> ( { <IDENTIFIER> }+ | <LATIN_KWD> | <GREEK_KWD> ) '--' <DIM_OPT> <INTEGER>
def _index(self):
self.expect('INDEX_KWD')
indices = []
if self.peek('IDENTIFIER'):
while True:
sentence, (position, _) = self.scanner.sentence, self.scanner.prev_state
index_1 = self.scanner.lexeme
indices.append(self.scanner.lexeme)
self.expect('IDENTIFIER')
if self.accept('LDOTS'):
index_2 = self.scanner.lexeme
self.expect('IDENTIFIER')
try:
indices.extend([chr(i) for i in range(ord(index_1), ord(index_2) + 1)])
except TypeError:
raise ParserError('unsupported index range \'%s\' at position %d' %
(sentence[position:self.scanner.position], position), sentence, position)
else:
indices.append(index_1)
if self.peek('LDASH'): break
if self.peek('DBL_DASH'): break
elif self.accept('LATIN_KWD'):
indices.extend([index for index in LATIN_ALPHABET])
elif self.accept('GREEK_KWD'):
indices.extend([index for index in GREEK_ALPHABET])
else:
indices.extend([index for index in self._property['index']])
self.expect('LDASH')
sentence, (position, _) = self.scanner.sentence, self.scanner.prev_state
raise ParserError('unexpected \'%s\' at position %d' %
(sentence[position], position), sentence, position)
self.expect('DBL_DASH')
self.expect('DIM_OPT')
dimension = self.scanner.lexeme
self.expect('INTEGER')
Expand All @@ -313,10 +316,10 @@ def _metric(self):
while True:
symbols.append(self.scanner.lexeme)
self.expect('IDENTIFIER')
if self.peek('LDASH') or self.peek('LINEBREAK'): break
if self.peek('DBL_DASH') or self.peek('LINEBREAK'): break
dimension = suffix = None
zeros = False
while self.accept('LDASH'):
while self.accept('DBL_DASH'):
if self.accept('ZEROS_OPT'):
zeros = True
else:
Expand Down
6 changes: 4 additions & 2 deletions nrpylatex/core/scanner.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,7 @@ def __init__(self):
('PERCENT', r'\%'),
('STRING', r'\"[^\"]*\"'),
('ARROW', r'\-\>'),
('LDASH', r'\-\-'),
('LDOTS', r'\.\.'),
('DBL_DASH', r'\-\-'),
('SYMMETRY', symmetry),
('ZEROS_OPT', r'zeros'),
('CONST_OPT', r'const'),
Expand All @@ -37,7 +36,10 @@ def __init__(self):
('SUFFIX_OPT', r'suffix'),
('METRIC_OPT', r'metric'),
('COORD_KWD', r'coord'),
('DEFAULT_KWD', r'default'),
('INDEX_KWD', r'index'),
('LATIN_KWD', r'latin'),
('GREEK_KWD', r'greek'),
('IDENTIFIER', alphabet + r'((%s)|[_0-9])*' % alphabet),
('INTEGER', r'[0-9]+'),
('NEWLINE', r'\\{2}')]
Expand Down
17 changes: 9 additions & 8 deletions nrpylatex/tests/test_parse_latex.py
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ def test_recursive_replacement(self):
def test_product_rule(self):
self.assertEqual(
set(parse_latex(r"""
% declare index --dim 2
% declare index latin --dim 2
% declare vU wU --dim 2 --suffix dD
T^{ab}_c = \partial_c (v^a w^b)
""")),
Expand All @@ -142,7 +142,7 @@ def test_product_rule(self):
def test_upwind_suffix(self):
self.assertEqual(
set(parse_latex(r"""
% declare index --dim 2
% declare index latin --dim 2
% declare vU --dim 2 --suffix dD
% declare w --const
T^a_c = % suffix dupD
Expand All @@ -157,7 +157,7 @@ def test_upwind_suffix(self):
def test_inference_covdrv(self):
self.assertEqual(
set(parse_latex(r"""
% declare index --dim 4
% declare index latin --dim 4
% declare metric gDD --dim 4 --suffix dD
% declare vU --dim 4 --suffix dD
T^{ab} = \nabla^b v^a
Expand All @@ -169,7 +169,7 @@ def test_inference_pardrv(self):
self.assertEqual(
set(parse_latex(r"""
% declare coord x y
% declare index --dim 2
% declare index latin --dim 2
% declare uD --zeros --dim 2
u_x = x^2 + 2x \\
u_y = y\sqrt{x}
Expand All @@ -186,7 +186,7 @@ def test_inference_pardrv(self):
def test_notation_pardrv(self):
self.assertEqual(
set(parse_latex(r"""
% declare index --dim 2
% declare index latin --dim 2
% declare vD uD wD --dim 2 --suffix dD
T_{abc} = ((v_a + u_a)_{,b} - w_{a,b})_{,c}
""")),
Expand All @@ -199,7 +199,8 @@ def test_notation_pardrv(self):
def test_spherical_riemann(self):
parse_latex(r"""
% declare coord theta phi
% declare index --dim 2
% declare index latin --dim 2
% declare index greek --dim 2
% declare metric gDD --zeros --dim 2
% declare r --const
% g_{0 0} = r^2 \\
Expand Down Expand Up @@ -563,7 +564,7 @@ def test_inverse_covariant(self):
for DIM in range(2, 5):
parse_latex(r"""
% declare metric gDD --dim {DIM}
% declare index --dim {DIM}
% declare index latin --dim {DIM}
T^a_c = g^{{ab}} g_{{bc}}
""".format(DIM=DIM))
for i in range(DIM):
Expand All @@ -574,7 +575,7 @@ def test_inverse_contravariant(self):
for DIM in range(2, 5):
parse_latex(r"""
% declare metric gUU --dim {DIM}
% declare index --dim {DIM}
% declare index latin --dim {DIM}
T^a_c = g^{{ab}} g_{{bc}}
""".format(DIM=DIM))
for i in range(DIM):
Expand Down

0 comments on commit 79c767a

Please sign in to comment.