# suppress negligible complex numpy eigenvalues?

• Last Update :
• Techknowledgy :

In general, to retrieve the real part of numbers in an array is as easy as:

```>>> np.real(np.array([1 + 1 j, 2 + 1 j]))
array([1., 2.])```

Suggestion : 2

I am calculating the eigenvalues of a anycodings_linear-algebra covariance matrix, which is real and anycodings_linear-algebra symmetric positive semi-definite. Therefore, anycodings_linear-algebra the eigenvalues and eigenvectors should all anycodings_linear-algebra be real, however numpy.linalg.eig() is anycodings_linear-algebra returning complex values with (almost) zero anycodings_linear-algebra imaginary components.,I think there is some machine precision anycodings_linear-algebra issue here, as clearly the imaginary anycodings_linear-algebra components are negligible (and given that my anycodings_linear-algebra covariance matrix is real pos semi-def).,The covariance matrix is too large to post anycodings_linear-algebra here, but the eigenvalues come out as ,I think the best solution for this anycodings_python specific case is to use @PaulPanzer's anycodings_python suggestion, that is np.linalg.eigh. This anycodings_python works directly for Hermitian matrices, anycodings_python and thus will have only real Eigen anycodings_python values, exactly this specific use case.

The covariance matrix is too large to post anycodings_linear-algebra here, but the eigenvalues come out as

`[1.38174e01 + 00 j, 9.00153e00 + 00 j, ....]`

In general, to retrieve the real part of anycodings_python numbers in an array is as easy as:

```>>> np.real(np.array([1 + 1 j, 2 + 1 j]))
array([1., 2.])```

Suggestion : 3

Compute the eigenvalues and right eigenvectors of a square array.,Broadcasting rules apply, see the numpy.linalg documentation for details.,This is implemented using the _geev LAPACK routines which compute the eigenvalues and eigenvectors of general square arrays.,eigenvalues and eigenvectors of a real symmetric or complex Hermitian (conjugate symmetric) array.

```>>> from numpy
import linalg as LA```
```>>> w, v = LA.eig(np.diag((1, 2, 3))) >>>
w;
v
array([1., 2., 3.])
array([
[1., 0., 0.],
[0., 1., 0.],
[0., 0., 1.]
])```
```>>> w, v = LA.eig(np.array([
[1, -1],
[1, 1]
])) >>>
w;
v
array([1. + 1. j, 1. - 1. j])
array([
[0.70710678 + 0. j, 0.70710678 - 0. j],
[0. - 0.70710678 j, 0. + 0.70710678 j]
])```
```>>> a = np.array([
[1, 1 j],
[-1 j, 1]
]) >>>
w, v = LA.eig(a) >>>
w;
v
array([2. + 0. j, 0. + 0. j])
array([
[0. + 0.70710678 j, 0.70710678 + 0. j], # may vary[0.70710678 + 0. j, -0. + 0.70710678 j]
])```
```>>> a = np.array([
[1 + 1e-9, 0],
[0, 1 - 1e-9]
]) >>>
# Theor.e - values are 1 + /- 1e-9 >>>
w, v = LA.eig(a) >>>
w;
v
array([1., 1.])
array([
[1., 0.],
[0., 1.]
])```

Suggestion : 4

Created: 2021-09-22 Wed 16:53

```print(2 + 4)
print(8.1 - 5)```
```print(5 * 4)
print(3.1 * 2)```
```print(4.0 / 2.0)
print(1.0 / 3.1)```
```print(4 / 2)
print(1 / 3)```
```print(4 // 2)
print(1 // 3)```
```print(3. ** 2)
print(3 ** 2)
print(2 ** 0.5)```

Suggestion : 5

NumPy supports Python's built-in data types (such as int, float, bool, complex, and str). It also introduces its own scalar data types:,ndarray.dtype: data type of the elements. Recall that ndarray contains elements of the same type (unlike Python's array list). You can use the Python built-in types such as int, float, bool, str and complex; or the NumPy's types, such as int8, int16, int32, int64, uint8, uint16, uint32, uint64, float32, float64, complex64, complex128, with the specified bit-size.,You can also apply comparison operators, such as ==, !=, <, <=, >, >=, element-wise. This is not supported in Python's list.,NumPy provides another function called genfromtxt() to handle structured arrays. For example, create the following CSV file called data1.csv with missing data points and header:

```fig1, ax1 = plt.subplots() #
default one row and one column```
current figure
```ax = plt.gca() # Get the current axes handle
fig = plt.gcf() # Get the current figure handle```
`plot()`
```>>> plt.plot([1, 2, 3, 4, 5], [5, 1, 2, 4, 3],
color = 'green', marker = 'o', markerfacecolor = 'blue', markersize = 12, linestyle = 'dashed') >>>
plt.show()```
`set_xxx()`
```>>> line, = plt.plot([1, 2, 3, 4, 5], [5, 1, 2, 4, 3])
# plot() returns a list of Line2D objects - an one - item list in this plot
# Retrieve a reference to the Line2D by unpack an one - item list >>>
line.set_color('y') # same as line.set_color('yellow') >>>
line.set_linestyle('-.') # same as line.set_linestyle('dashdot') >>>
line.set_marker('*') # star marker >>>
plt.show()```
`setp()`
```>>> lines = plt.plot([1, 2, 3, 4, 5], [5, 1, 2, 4, 3], [1, 2, 3, 4, 5], [2, 4, 6, 3, 4]) # 2-item list
>>> lines
[<matplotlib.lines.Line2D object at ...>, <matplotlib.lines.Line2D object at ...>]
>>> plt.setp(lines, color='r', marker='+') # Applicable to single line or list of lines
[None, None, None, None]
>>> plt.show()```
`[low, high)`
```>>> help(np.random.rand) >>>
m1 = np.random.rand(2, 3) # Specify the dimensions >>>
m1
array([
[0.57877041, 0.93898599, 0.15998744],
[0.5195182, 0.79441764, 0.47046495]
]) >>>
m1.dtype
dtype('float64')

>>>
help(np.random.random) >>>
m2 = np.random.random() # One sample(
default) >>>
m2
0.8530312529958475 # Scalar, NOT array
>>>
m3 = np.random.random(5) # 1 D ndarray >>>
m3
array([0.31007576, 0.21615439, 0.26983623, 0.44427757, 0.35548085]) >>>
m4 = np.random.random((2, 4)) # ndarray of given shape >>>
m4
array([
[0.45519034, 0.97199324, 0.49615973, 0.5377464],
[0.1057191, 0.900195, 0.7685127, 0.23238175]
])

>>>
help(np.random.uniform) >>>
m5 = np.random.uniform(5, 10, (2, 4)) # low, high, shape >>>
m5
array([
[8.39092855, 5.95135548, 7.21166273, 6.46086279],
[9.7510942, 5.99099363, 9.9313887, 6.75191231]
])

>>>
help(np.random.randint) >>>
m6 = np.random.randint(1, 101, (2, 4)) >>>
m6
array([
[68, 97, 84, 55],
[49, 57, 28, 87]
]) >>>
m7 = np.random.randint(1, 101, 10) >>>
m7
array([37, 34, 57, 60, 26, 34, 46, 73, 59, 96]) >>>
m8 = np.random.randint(50, size = (2, 5)) #[0, 50) >>>
m8
array([
[16, 48, 9, 3, 22],
[19, 20, 16, 17, 11]
])```

Suggestion : 6

The MO coefficients are found as the generalized eigenvectors of the Fock Matrix,,Appropriate for open-shell systems where spin-contamination is problem. Sometimes more difficult to converge, and assumes uniformly positive spin polarization (the alpha and beta doubly-occupied orbitals are identical).,which writes a Wavefunction object converted (serialized) to a numpy file called my_mos.npy. The restart can then be done as follows:,The key difficulty in the SCF procedure is treatment of the four-index ERI contributions to the Fock Matrix. A number of algorithms are available in PSI4 for these terms. The algorithm is selected by the SCF_TYPE keyword, which may be one of the following

```molecule {
0 3
O
O 1 1.21
}

set {
basis cc - pvdz
reference uhf
scf_type direct
}

energy('scf')```
```                         Total Energy Delta E RMS | [F, P] |

@DF - UHF iter 0: -149.80032977420572 - 1.49800e+02 1.48808e-01
@DF - UHF iter 1: -149.59496320631871 2.05367e-01 2.58009e-02
@DF - UHF iter 2: -149.62349901753706 - 2.85358e-02 6.68980e-03 DIIS
@DF - UHF iter 3: -149.62639942687878 - 2.90041e-03 2.19285e-03 DIIS
@DF - UHF iter 4: -149.62689561367233 - 4.96187e-04 5.99497e-04 DIIS
@DF - UHF iter 5: -149.62694151275420 - 4.58991e-05 1.27338e-04 DIIS
@DF - UHF iter 6: -149.62694337910040 - 1.86635e-06 1.65616e-05 DIIS
@DF - UHF iter 7: -149.62694340915198 - 3.00516e-08 2.68990e-06 DIIS
@DF - UHF iter 8: -149.62694340999315 - 8.41169e-10 2.61249e-07 DIIS

DF guess converged.
...

@UHF iter 9: -149.62730705472407 - 3.63645e-04 8.63697e-05 DIIS
@UHF iter 10: -149.62730737348096 - 3.18757e-07 1.50223e-05 DIIS
@UHF iter 11: -149.62730738537113 - 1.18902e-08 3.80466e-06 DIIS
@UHF iter 12: -149.62730738624032 - 8.69193e-10 7.06634e-07 DIIS```
```molecule {
He
}

set basis sto - 3 g

energy('scf')```
```molecule h {
0 2 # Neutral doublet
H
}```
```h.set_molecular_charge(0)
h.set_multiplicity(2)```
```molecule h {
0 2
H
symmetry c1
}```