Skip to content

Commit

Permalink
For fractional phase with variables, accept an integer or fraction fo…
Browse files Browse the repository at this point in the history
…llowed by a variable (without an intervening "*").

For example: `3alpha` and `1/2beta` as shorthands for `3*alpha` and `1/2*beta`. The former are how these phases are displayed in the UI, so it makes intuitive sense to accept them as input.

See issue #148.
  • Loading branch information
dlyongemallo committed Oct 31, 2023
1 parent db88e7a commit 034c974
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 2 deletions.
9 changes: 9 additions & 0 deletions test/test_editor_base_panel.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,15 @@ def _new_var(name: str) -> Poly:
# Test a fractional phase specified with variables.
assert (string_to_fraction('a*b', _new_var) ==
Poly([(1, Term([(Var('a', types_dict), 1), (Var('b', types_dict), 1)]))]))
assert (string_to_fraction('2*a', _new_var) ==
Poly([(2, Term([(Var('a', types_dict), 1)]))]))
assert (string_to_fraction('2a', _new_var) ==
Poly([(2, Term([(Var('a', types_dict), 1)]))]))
assert (string_to_fraction('3/2a', _new_var) ==
Poly([(3/2, Term([(Var('a', types_dict), 1)]))]))
assert (string_to_fraction('3a+2b', _new_var) ==
Poly([(3, Term([(Var('a', types_dict), 1)])), (2, Term([(Var('b', types_dict), 1)]))]))


# Test bad input.
with pytest.raises(ValueError):
Expand Down
2 changes: 1 addition & 1 deletion zxlive/editor_base_panel.py
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ def vert_double_clicked(self, v: VT) -> None:

else:
prompt = "Enter desired phase value (in units of pi):"
error_msg = "Please enter a valid input (e.g., 1/2, 2, 0.25, a)."
error_msg = "Please enter a valid input (e.g., 1/2, 2, 0.25, 2a+b)."

input_, ok = QInputDialog.getText(
self, "Change Phase", prompt
Expand Down
2 changes: 1 addition & 1 deletion zxlive/parse_poly.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

poly_grammar = Lark("""
start : "(" start ")" | term ("+" term)*
term : factor ("*" factor)*
term : (intf | frac)? factor ("*" factor)*
?factor : intf | frac | pi | pifrac | var
var : CNAME
intf : INT
Expand Down

0 comments on commit 034c974

Please sign in to comment.