Repository: sympy/sympy

Issue:
Logical operators in octave_code
When expressions include logical operators, like &, octave_code uses the wrong logical operator in the produced code. For example the following code:

    from sympy import Piecewise
    from sympy.abc import x
    from sympy.printing import octave_code
    print octave_code(Piecewise((1, (x > 0) & (x < 1)), (0, True)))

produces output

    ((x > 0 && x < 1).*(1) + (~(x > 0 && x < 1)).*(0))

The logical ands (&&) should be single ampersands (&) so that the expression works with vector arguments x in Octave/MATLAB 


Discussion:

Patch:
diff --git a/sympy/printing/octave.py b/sympy/printing/octave.py
index 5cef62c675..b07c0f7ab8 100644
--- a/sympy/printing/octave.py
+++ b/sympy/printing/octave.py
@@ -45,8 +45,8 @@ class OctaveCodePrinter(CodePrinter):
     language = "Octave"
 
     _operators = {
-        'and': '&&',
-        'or': '||',
+        'and': '&',
+        'or': '|',
         'not': '~',
     }
 


Test patch:
diff --git a/sympy/printing/tests/test_octave.py b/sympy/printing/tests/test_octave.py
index 4c9a542583..17d57acca7 100644
--- a/sympy/printing/tests/test_octave.py
+++ b/sympy/printing/tests/test_octave.py
@@ -129,13 +129,13 @@ def test_constants_other():
 
 
 def test_boolean():
-    assert mcode(x & y) == "x && y"
-    assert mcode(x | y) == "x || y"
+    assert mcode(x & y) == "x & y"
+    assert mcode(x | y) == "x | y"
     assert mcode(~x) == "~x"
-    assert mcode(x & y & z) == "x && y && z"
-    assert mcode(x | y | z) == "x || y || z"
-    assert mcode((x & y) | z) == "z || x && y"
-    assert mcode((x | y) & z) == "z && (x || y)"
+    assert mcode(x & y & z) == "x & y & z"
+    assert mcode(x | y | z) == "x | y | z"
+    assert mcode((x & y) | z) == "z | x & y"
+    assert mcode((x | y) & z) == "z & (x | y)"
 
 
 def test_Matrices():