Repository: sympy/sympy

Issue:
Polynomial solve with GoldenRatio causes Traceback
Found during issue 11538 investigation. The GoldenRatio::NumberSymbol is the only NumberSymbol currently treated as an algebraic expression. However when solve is called on the GoldenRatio a Traceback occurs where GoldenRatio is not being treated algebraically. 

```
Python 2.7.12 (v2.7.12:d33e0cf91556, Jun 27 2016, 15:19:22) [MSC v.1500 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import sympy
>>> x,y=sympy.symbols('x y')
>>> e1=x+y+1
>>> e2=x+sympy.GoldenRatio
>>> sympy.solve([e1,e2],x,y)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "sympy\solvers\solvers.py", line 1053, in solve
    solution = _solve_system(f, symbols, **flags)
  File "sympy\solvers\solvers.py", line 1660, in _solve_system
    poly = g.as_poly(*symbols, extension=True)
  File "sympy\core\basic.py", line 699, in as_poly
    poly = Poly(self, *gens, **args)
  File "sympy\polys\polytools.py", line 89, in __new__
    return cls._from_expr(rep, opt)
  File "sympy\polys\polytools.py", line 200, in _from_expr
    return cls._from_dict(rep, opt)
  File "sympy\polys\polytools.py", line 144, in _from_dict
    domain, rep = construct_domain(rep, opt=opt)
  File "sympy\polys\constructor.py", line 238, in construct_domain
    result = _construct_simple(coeffs, opt)
  File "sympy\polys\constructor.py", line 45, in _construct_simple
    domain, result = _construct_algebraic(coeffs, opt)
  File "sympy\polys\constructor.py", line 93, in _construct_algebraic
    g, span, H = primitive_element(exts, ex=True, polys=True)
  File "sympy\polys\numberfields.py", line 857, in primitive_element
    f = minpoly(ext, y)
  File "sympy\polys\numberfields.py", line 657, in minimal_polynomial
    result = _minpoly_compose(ex, x, dom)
  File "sympy\polys\numberfields.py", line 578, in _minpoly_compose
    raise NotAlgebraic("%s doesn't seem to be an algebraic element" % ex)
sympy.polys.polyerrors.NotAlgebraic: GoldenRatio doesn't seem to be an algebraic element
```


Discussion:

Patch:
diff --git a/README.rst b/README.rst
index 2526eeb825..5e39c7aa2f 100644
--- a/README.rst
+++ b/README.rst
@@ -105,17 +105,13 @@ the mpmath installation guide:
 
 https://github.com/fredrik-johansson/mpmath#1-download--installation
 
-To install SymPy using PyPI, run the following command::
+To install SymPy itself, then simply run::
 
-    $ pip install sympy
-
-To install SymPy from GitHub source, first clone SymPy using ``git``::
-
-    $ git clone https://github.com/sympy/sympy.git
+    $ python setup.py install
 
-Then, in the ``sympy`` repository that you cloned, simply run::
+If you install it system-wide, you may need to prefix the previous command with ``sudo``::
 
-    $ python setup.py install
+    $ sudo python setup.py install
 
 See https://docs.sympy.org/dev/install.html for more information.
 
diff --git a/sympy/matrices/eigen.py b/sympy/matrices/eigen.py
index cd28826bda..5469e6adf3 100644
--- a/sympy/matrices/eigen.py
+++ b/sympy/matrices/eigen.py
@@ -322,12 +322,10 @@ def _is_diagonalizable(M, reals_only=False, dotprodsimp=None, **kwargs):
 
     reals_only : bool, optional
         If ``True``, it tests whether the matrix can be diagonalized
-        to contain only real numbers on the diagonal.
-
-
-        If ``False``, it tests whether the matrix can be diagonalized
-        at all, even with numbers that may not be real.
+        without complex numbers. (Orthogonally diagonalizable)
 
+        If ``False``, it tests whether the matrix can be unitarily
+        diagonalizable.
 
     dotprodsimp : bool, optional
         Specifies whether intermediate term algebraic simplification
@@ -350,7 +348,8 @@ def _is_diagonalizable(M, reals_only=False, dotprodsimp=None, **kwargs):
     >>> M.is_diagonalizable()
     False
 
-    Example of a matrix that is diagonalized in terms of non-real entries:
+    Example of a unitarily diagonalizable, but not orthogonally
+    diagonalizable:
 
     >>> M = Matrix([[0, 1], [-1, 0]])
     >>> M.is_diagonalizable(reals_only=False)
@@ -385,7 +384,8 @@ def _is_diagonalizable(M, reals_only=False, dotprodsimp=None, **kwargs):
     if all(e.is_real for e in M) and M.is_symmetric():
         return True
 
-    if all(e.is_complex for e in M) and M.is_hermitian:
+    if all(e.is_complex for e in M) and M.is_hermitian \
+            and not reals_only:
         return True
 
     return _is_diagonalizable_with_eigen(M, reals_only=reals_only,
diff --git a/sympy/polys/numberfields.py b/sympy/polys/numberfields.py
index ff501a91e0..d10f04eb1e 100644
--- a/sympy/polys/numberfields.py
+++ b/sympy/polys/numberfields.py
@@ -3,9 +3,10 @@
 from __future__ import print_function, division
 
 from sympy import (
-    S, Rational, AlgebraicNumber,
+    S, Rational, AlgebraicNumber, GoldenRatio, TribonacciConstant,
     Add, Mul, sympify, Dummy, expand_mul, I, pi
 )
+from sympy.functions import sqrt, cbrt
 from sympy.core.compatibility import reduce
 from sympy.core.exprtools import Factors
 from sympy.core.function import _mexpand
@@ -517,6 +518,22 @@ def _minpoly_compose(ex, x, dom):
     if ex is I:
         _, factors = factor_list(x**2 + 1, x, domain=dom)
         return x**2 + 1 if len(factors) == 1 else x - I
+
+    if ex is GoldenRatio:
+        _, factors = factor_list(x**2 - x - 1, x, domain=dom)
+        if len(factors) == 1:
+            return x**2 - x - 1
+        else:
+            return _choose_factor(factors, x, (1 + sqrt(5))/2, dom=dom)
+
+    if ex is TribonacciConstant:
+        _, factors = factor_list(x**3 - x**2 - x - 1, x, domain=dom)
+        if len(factors) == 1:
+            return x**3 - x**2 - x - 1
+        else:
+            fac = (1 + cbrt(19 - 3*sqrt(33)) + cbrt(19 + 3*sqrt(33))) / 3
+            return _choose_factor(factors, x, fac, dom=dom)
+
     if hasattr(dom, 'symbols') and ex in dom.symbols:
         return x - ex
 


Test patch:
diff --git a/sympy/polys/tests/test_numberfields.py b/sympy/polys/tests/test_numberfields.py
index 23c8179638..6de615df33 100644
--- a/sympy/polys/tests/test_numberfields.py
+++ b/sympy/polys/tests/test_numberfields.py
@@ -1,7 +1,7 @@
 """Tests for computational algebraic number field theory. """
 
 from sympy import (S, Rational, Symbol, Poly, sqrt, I, oo, Tuple, expand,
-    pi, cos, sin, exp)
+    pi, cos, sin, exp, GoldenRatio, TribonacciConstant, cbrt)
 
 from sympy.testing.pytest import raises, slow
 
@@ -149,6 +149,14 @@ def test_minimal_polynomial():
     assert minimal_polynomial(I, x, domain=QQ) == x**2 + 1
     assert minimal_polynomial(I, x, domain='QQ(y)') == x**2 + 1
 
+    #issue 11553
+    assert minimal_polynomial(GoldenRatio, x) == x**2 - x - 1
+    assert minimal_polynomial(TribonacciConstant + 3, x) == x**3 - 10*x**2 + 32*x - 34
+    assert minimal_polynomial(GoldenRatio, x, domain=QQ.algebraic_field(sqrt(5))) == \
+            2*x - sqrt(5) - 1
+    assert minimal_polynomial(TribonacciConstant, x, domain=QQ.algebraic_field(cbrt(19 - 3*sqrt(33)))) == \
+    48*x - 19*(19 - 3*sqrt(33))**Rational(2, 3) - 3*sqrt(33)*(19 - 3*sqrt(33))**Rational(2, 3) \
+    - 16*(19 - 3*sqrt(33))**Rational(1, 3) - 16
 
 def test_minimal_polynomial_hi_prec():
     p = 1/sqrt(1 - 9*sqrt(2) + 7*sqrt(3) + Rational(1, 10)**30)
diff --git a/sympy/solvers/tests/test_solvers.py b/sympy/solvers/tests/test_solvers.py
index a07f2d6dde..20c4420927 100644
--- a/sympy/solvers/tests/test_solvers.py
+++ b/sympy/solvers/tests/test_solvers.py
@@ -5,7 +5,7 @@
     erfcinv, exp, im, log, pi, re, sec, sin,
     sinh, solve, solve_linear, sqrt, sstr, symbols, sympify, tan, tanh,
     root, atan2, arg, Mul, SparseMatrix, ask, Tuple, nsolve, oo,
-    E, cbrt, denom, Add, Piecewise)
+    E, cbrt, denom, Add, Piecewise, GoldenRatio, TribonacciConstant)
 
 from sympy.core.function import nfloat
 from sympy.solvers import solve_linear_system, solve_linear_system_LU, \
@@ -2131,3 +2131,11 @@ def test_issue_17949():
     assert solve(exp(-x+x**2), x) == []
     assert solve(exp(+x-x**2), x) == []
     assert solve(exp(-x-x**2), x) == []
+
+
+def test_issue_11553():
+    eq1 = x + y + 1
+    eq2 = x + GoldenRatio
+    assert solve([eq1, eq2], x, y) == {x: -GoldenRatio, y: -1 + GoldenRatio}
+    eq3 = x + 2 + TribonacciConstant
+    assert solve([eq1, eq3], x, y) == {x: -2 - TribonacciConstant, y: 1 + TribonacciConstant}