Convert complex roots into standard float form without affecting quality of result
<p>I have a transformed stress tensor, its principle values are coming in complex form:</p>

<pre><code>#simple roots calculated by characteristic equation
def  princ_cherac(sten):
    sxx,syy,szz,sxy,syz,szx = sten
    H1 = (sxx+syy+szz)/3.
    H2 = (syz**2 + szx**2 + sxy**2  - syy*szz - szz*sxx - sxx-syy)/3.
    H3 = (  2*syz*szx*sxy  +  sxx*syy*szz   -  sxx*syz*syz  - syy*szx*szx - szz*sxy*sxy    )/2. 
    from numpy.polynomial import Polynomial as P 
    p = P([2*H3,3*H2,3*H1,-1])  
    S3,S2,S1 = p.roots()
</code></pre>

<p>but it produces complex roots. For my further calculations, the complex form cannot work. How can I convert from the complex form while not losing any information on the results, for example, <code>S3 =-32.894653311352783-28.288180652364915j</code>; how to change it so that the answer represents it but in normal float-like form?</p>

<p>There is a mistake in your implementation of <code>H2</code>.  You have the last term as <code>sxx-syy</code>, but that should be <code>sxx*syy</code>.</p>

<p>By the way, you'll likely get more accurate results by using <a href="http://docs.scipy.org/doc/numpy/reference/generated/numpy.linalg.eigvalsh.html" rel="nofollow"><code>numpy.linalg.eigvalsh</code></a> to find the values.  For example, here's a full 3x3 tensor; <code>coeffs</code> are the values to be passed to <code>princ_cherac</code>:</p>

<pre><code>In [200]: tensor = np.array([[2, 1, 1], [1, 2, 1], [1, 1, 2]])

In [201]: coeffs = tensor.ravel()[[0, 4, 8, 1, 5, 2]]

In [202]: tensor
Out[202]: 
array([[2, 1, 1],
       [1, 2, 1],
       [1, 1, 2]])

In [203]: coeffs
Out[203]: array([2, 2, 2, 1, 1, 1])
</code></pre>

<p>Compute the values using <code>eigvalsh</code>.</p>

<pre><code>In [204]: np.linalg.eigvalsh(tensor)
Out[204]: array([ 1.,  1.,  4.])
</code></pre>

<p>Compare that to the values produced by the corrected <code>princ_cherac</code>:</p>

<pre><code>In [205]: princ_cherac(coeffs)
Out[205]: (0.9999999619754828, 1.0000000380245182, 4.0)
</code></pre>

python numpy scipy