Repository: sympy/sympy

Issue:
IndexError from jordan_form
I get an error computing the Jordan form of this Matrix:
```julia
In [6]: M = Matrix([ 
   ...: [exp(t**3/3 - t**2/2)/2 + exp(t**3/3 + t**2/2)/2, exp(t**3/3 - t**2/2)/2 - exp(t**3/3 + t**2/2)/2], 
   ...: [exp(t**3/3 - t**2/2)/2 - exp(t**3/3 + t**2/2)/2, exp(t**3/3 - t**2/2)/2 + exp(t**3/3 + t**2/2)/2]])                      

In [7]: M                                                                                                                         
Out[7]: 
⎡  3    2     3    2    3    2     3    2⎤
⎢ t    t     t    t    t    t     t    t ⎥
⎢ ── - ──    ── + ──   ── - ──    ── + ──⎥
⎢ 3    2     3    2    3    2     3    2 ⎥
⎢ℯ          ℯ         ℯ          ℯ       ⎥
⎢──────── + ────────  ──────── - ────────⎥
⎢   2          2         2          2    ⎥
⎢                                        ⎥
⎢  3    2     3    2    3    2     3    2⎥
⎢ t    t     t    t    t    t     t    t ⎥
⎢ ── - ──    ── + ──   ── - ──    ── + ──⎥
⎢ 3    2     3    2    3    2     3    2 ⎥
⎢ℯ          ℯ         ℯ          ℯ       ⎥
⎢──────── - ────────  ──────── + ────────⎥
⎣   2          2         2          2    ⎦

In [8]: M.jordan_form()                                                                                                           
---------------------------------------------------------------------------
IndexError                                Traceback (most recent call last)
<ipython-input-8-192ccf6137fc> in <module>
----> 1 M.jordan_form()

~/current/sympy/sympy/sympy/matrices/matrices.py in jordan_form(self, calc_transform, **kwargs)
   1806             if not calc_transform:
   1807                 return restore_floats(jordan_mat)
-> 1808             jordan_basis = [eig_mat(eig, 1).nullspace()[0] for eig in blocks]
   1809             basis_mat = mat.hstack(*jordan_basis)
   1810             return restore_floats(basis_mat, jordan_mat)

~/current/sympy/sympy/sympy/matrices/matrices.py in <listcomp>(.0)
   1806             if not calc_transform:
   1807                 return restore_floats(jordan_mat)
-> 1808             jordan_basis = [eig_mat(eig, 1).nullspace()[0] for eig in blocks]
   1809             basis_mat = mat.hstack(*jordan_basis)
   1810             return restore_floats(basis_mat, jordan_mat)

IndexError: list index out of range
```
Looks like nullspace returns an empty list:
```julia
In [17]: M = Matrix([ 
    ...: [sqrt((exp(2*t**2) - 2*exp(t**2) + 1)*exp(t**2*(2*t/3 - 1)))/2 - (exp(t**2) + 1)*exp(t**2*(2*t - 3)/6)/2 + exp(t**3/3 - t
    ...: **2/2)/2 + exp(t**3/3 + t**2/2)/2,                                                                                       
    ...:                     exp(t**3/3 - t**2/2)/2 - exp(t**3/3 + t**2/2)/2], 
    ...: [                                                                                                          exp(t**3/3 - t
    ...: **2/2)/2 - exp(t**3/3 + t**2/2)/2, sqrt((exp(2*t**2) - 2*exp(t**2) + 1)*exp(t**2*(2*t/3 - 1)))/2 - (exp(t**2) + 1)*exp(t*
    ...: *2*(2*t - 3)/6)/2 + exp(t**3/3 - t**2/2)/2 + exp(t**3/3 + t**2/2)/2]])                                                   

In [18]: M                                                                                                                        
Out[18]: 
⎡     _____________________________________                 2               3    2     3    2                                   
⎢    ╱                         2 ⎛2⋅t    ⎞                 t ⋅(2⋅t - 3)    t    t     t    t                                    
⎢   ╱  ⎛    2      ⎛ 2⎞    ⎞  t ⋅⎜─── - 1⎟    ⎛ ⎛ 2⎞    ⎞  ────────────    ── - ──    ── + ──                                   
⎢  ╱   ⎜ 2⋅t       ⎝t ⎠    ⎟     ⎝ 3     ⎠    ⎜ ⎝t ⎠    ⎟       6          3    2     3    2                                    
⎢╲╱    ⎝ℯ     - 2⋅ℯ     + 1⎠⋅ℯ                ⎝ℯ     + 1⎠⋅ℯ               ℯ          ℯ                                          
⎢────────────────────────────────────────── - ───────────────────────── + ──────── + ────────                                   
⎢                    2                                    2                  2          2                                       
⎢                                                                                                                               
⎢                                      3    2     3    2                                            ____________________________
⎢                                     t    t     t    t                                            ╱                         2 ⎛
⎢                                     ── - ──    ── + ──                                          ╱  ⎛    2      ⎛ 2⎞    ⎞  t ⋅⎜
⎢                                     3    2     3    2                                          ╱   ⎜ 2⋅t       ⎝t ⎠    ⎟     ⎝
⎢                                    ℯ          ℯ                                              ╲╱    ⎝ℯ     - 2⋅ℯ     + 1⎠⋅ℯ    
⎢                                    ──────── - ────────                                       ─────────────────────────────────
⎣                                       2          2                                                               2            

     3    2     3    2                                     ⎤
    t    t     t    t                                      ⎥
    ── - ──    ── + ──                                     ⎥
    3    2     3    2                                      ⎥
   ℯ          ℯ                                            ⎥
   ──────── - ────────                                     ⎥
      2          2                                         ⎥
                                                           ⎥
_________                 2               3    2     3    2⎥
2⋅t    ⎞                 t ⋅(2⋅t - 3)    t    t     t    t ⎥
─── - 1⎟    ⎛ ⎛ 2⎞    ⎞  ────────────    ── - ──    ── + ──⎥
 3     ⎠    ⎜ ⎝t ⎠    ⎟       6          3    2     3    2 ⎥
            ⎝ℯ     + 1⎠⋅ℯ               ℯ          ℯ       ⎥
───────── - ───────────────────────── + ──────── + ────────⎥
                        2                  2          2    ⎦

In [19]: M.nullspace()                                                                                                            
Out[19]: []
```
After simplifying the nullspace is found:
```julia
In [20]: M.simplify()                                                                                                             

In [21]: M                                                                                                                        
Out[21]: 
⎡      __________________________________                                          ⎤
⎢     ╱                           3                                3    2          ⎥
⎢    ╱                         2⋅t     2                          t    t           ⎥
⎢   ╱   ⎛    2      ⎛ 2⎞    ⎞  ──── - t              ⎛     ⎛ 2⎞⎞  ── - ──          ⎥
⎢  ╱    ⎜ 2⋅t       ⎝t ⎠    ⎟   3                    ⎜     ⎝t ⎠⎟  3    2           ⎥
⎢╲╱     ⎝ℯ     - 2⋅ℯ     + 1⎠⋅ℯ                      ⎝1 - ℯ    ⎠⋅ℯ                 ⎥
⎢────────────────────────────────────────            ────────────────────          ⎥
⎢                   2                                         2                    ⎥
⎢                                                                                  ⎥
⎢                                                __________________________________⎥
⎢                        3    2                 ╱                           3      ⎥
⎢                       t    t                 ╱                         2⋅t     2 ⎥
⎢          ⎛     ⎛ 2⎞⎞  ── - ──               ╱   ⎛    2      ⎛ 2⎞    ⎞  ──── - t  ⎥
⎢          ⎜     ⎝t ⎠⎟  3    2               ╱    ⎜ 2⋅t       ⎝t ⎠    ⎟   3        ⎥
⎢          ⎝1 - ℯ    ⎠⋅ℯ                   ╲╱     ⎝ℯ     - 2⋅ℯ     + 1⎠⋅ℯ          ⎥
⎢          ────────────────────            ────────────────────────────────────────⎥
⎣                   2                                         2                    ⎦

In [22]: M.nullspace()                                                                                                            
Out[22]: 
⎡⎡                        3    2          ⎤⎤
⎢⎢                       t    t           ⎥⎥
⎢⎢          ⎛     ⎛ 2⎞⎞  ── - ──          ⎥⎥
⎢⎢          ⎜     ⎝t ⎠⎟  3    2           ⎥⎥
⎢⎢         -⎝1 - ℯ    ⎠⋅ℯ                 ⎥⎥
⎢⎢────────────────────────────────────────⎥⎥
⎢⎢      __________________________________⎥⎥
⎢⎢     ╱                           3      ⎥⎥
⎢⎢    ╱                         2⋅t     2 ⎥⎥
⎢⎢   ╱   ⎛    2      ⎛ 2⎞    ⎞  ──── - t  ⎥⎥
⎢⎢  ╱    ⎜ 2⋅t       ⎝t ⎠    ⎟   3        ⎥⎥
⎢⎢╲╱     ⎝ℯ     - 2⋅ℯ     + 1⎠⋅ℯ          ⎥⎥
⎢⎢                                        ⎥⎥
⎣⎣                   1                    ⎦⎦
```
I guess this is a problem in nullspace then.

Discussion:

Patch:
diff --git a/sympy/core/add.py b/sympy/core/add.py
index 6a3d10ac0e..2090206494 100644
--- a/sympy/core/add.py
+++ b/sympy/core/add.py
@@ -604,7 +604,7 @@ def _eval_is_zero(self):
         nz = []
         z = 0
         im_or_z = False
-        im = False
+        im = 0
         for a in self.args:
             if a.is_extended_real:
                 if a.is_zero:
@@ -614,7 +614,7 @@ def _eval_is_zero(self):
                 else:
                     return
             elif a.is_imaginary:
-                im = True
+                im += 1
             elif (S.ImaginaryUnit*a).is_extended_real:
                 im_or_z = True
             else:
@@ -625,10 +625,11 @@ def _eval_is_zero(self):
             return None
         b = self.func(*nz)
         if b.is_zero:
-            if not im_or_z and not im:
-                return True
-            if im and not im_or_z:
-                return False
+            if not im_or_z:
+                if im == 0:
+                    return True
+                elif im == 1:
+                    return False
         if b.is_zero is False:
             return False
 


Test patch:
diff --git a/sympy/core/tests/test_arit.py b/sympy/core/tests/test_arit.py
index 2cd5e484bb..bdc1a1223f 100644
--- a/sympy/core/tests/test_arit.py
+++ b/sympy/core/tests/test_arit.py
@@ -2307,3 +2307,8 @@ def test__neg__():
 
 def test_issue_18507():
     assert Mul(zoo, zoo, 0) is nan
+
+
+def test_issue_17130():
+    e = Add(b, -b, I, -I, evaluate=False)
+    assert e.is_zero is None # ideally this would be True