# ArgumentError: invalid argument #4 to LAPACK call

**URL:** <https://discourse.julialang.org/t/argumenterror-invalid-argument-4-to-lapack-call/46455>\
**Category:** General Usage\
**Tags:** lapack\
**Created:** [September 11, 2020, 1:44pm UTC](https://discourse.julialang.org/t/argumenterror-invalid-argument-4-to-lapack-call/46455 "2020-09-11T13:44:17Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![PhillipBC](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/phillipbc/32/9166_2.png) [@PhillipBC](https://discourse.julialang.org/u/PhillipBC)\
**Post date:** [September 11, 2020, 1:44pm UTC](https://discourse.julialang.org/t/argumenterror-invalid-argument-4-to-lapack-call/46455/1 "2020-09-11T13:44:17Z")

</div>

This problem arose using Julia 1.4.2, and I then got version 1.5.1 and the problem is still there.

If I create a matrix, then get its eigenvalues, then plot the eigenvalues, then get the eigenvalues again, I get the error:

```julia
ArgumentError: invalid argument #4 to LAPACK call

Stacktrace:
 [1] chklapackerror at D:\buildbot\worker\package_win64\build\usr\share\julia\stdlib\v1.5\LinearAlgebra\src\lapack.jl:36 [inlined]
 [2] geevx!(::Char, ::Char, ::Char, ::Char, ::Array{Float64,2}) at D:\buildbot\worker\package_win64\build\usr\share\julia\stdlib\v1.5\LinearAlgebra\src\lapack.jl:2082
 [3] eigvals!(::Array{Float64,2}; permute::Bool, scale::Bool, sortby::typeof(LinearAlgebra.eigsortby)) at D:\buildbot\worker\package_win64\build\usr\share\julia\stdlib\v1.5\LinearAlgebra\src\eigen.jl:293
 [4] eigvals! at D:\buildbot\worker\package_win64\build\usr\share\julia\stdlib\v1.5\LinearAlgebra\src\eigen.jl:292 [inlined]
 [5] #eigvals#73 at D:\buildbot\worker\package_win64\build\usr\share\julia\stdlib\v1.5\LinearAlgebra\src\eigen.jl:326 [inlined]
 [6] eigvals(::Array{Float64,2}) at D:\buildbot\worker\package_win64\build\usr\share\julia\stdlib\v1.5\LinearAlgebra\src\eigen.jl:326
 [7] top-level scope at In[242]:1
 [8] include_string(::Function, ::Module, ::String, ::String) at .\loading.jl:1091

```

This was the easiest way I could reproduce the problem:

```julia
using LinearAlgebra
using Plots; pyplot();

A = [0.475 0.5 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0;
0.5 -0.475 0.5 0.5 0.0 0.0 0.0 0.0 0.0 0.0;
0.0 0.5 0.0 0.0 0.5 0.0 0.0 0.0 0.0 0.0;
0.0 0.5 0.0 -0.475 0.5 0.0 0.5 0.0 0.0 0.0;
0.0 0.0 0.5 0.0 -0.95 0.0 0.0 0.5 0.0 0.0;
0.0 0.0 0.0 0.0 0.5 0.0 0.0 0.0 0.5 0.0;
0.0 0.0 0.0 0.5 0.0 0.0 0.0 0.5 0.0 0.0;
0.0 0.0 0.0 0.0 0.5 0.0 0.5 -0.475 0.5 0.0;
0.0 0.0 0.0 0.0 0.0 0.5 0.0 0.5 -0.4749999999999999 0.5;
0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.5 0.475]
e1 = eigvals(A);
Plots.plot(e1)

```

Then calling eigvals again leads to the errror above:

```julia
e1 = eigvals(A)

ArgumentError: invalid argument #4 to LAPACK call

Stacktrace:
 [1] chklapackerror at D:\buildbot\worker\package_win64\build\usr\share\julia\stdlib\v1.5\LinearAlgebra\src\lapack.jl:36 [inlined]
 [2] geevx!(::Char, ::Char, ::Char, ::Char, ::Array{Float64,2}) at D:\buildbot\worker\package_win64\build\usr\share\julia\stdlib\v1.5\LinearAlgebra\src\lapack.jl:2082
 [3] eigvals!(::Array{Float64,2}; permute::Bool, scale::Bool, sortby::typeof(LinearAlgebra.eigsortby)) at D:\buildbot\worker\package_win64\build\usr\share\julia\stdlib\v1.5\LinearAlgebra\src\eigen.jl:293
 [4] eigvals! at D:\buildbot\worker\package_win64\build\usr\share\julia\stdlib\v1.5\LinearAlgebra\src\eigen.jl:292 [inlined]
 [5] #eigvals#73 at D:\buildbot\worker\package_win64\build\usr\share\julia\stdlib\v1.5\LinearAlgebra\src\eigen.jl:326 [inlined]
 [6] eigvals(::Array{Float64,2}) at D:\buildbot\worker\package_win64\build\usr\share\julia\stdlib\v1.5\LinearAlgebra\src\eigen.jl:326
 [7] top-level scope at In[242]:1
 [8] include_string(::Function, ::Module, ::String, ::String) at .\loading.jl:1091

```

* * *

The original error I was getting with my actual code,where I was calculating eigenvalue statistics and plotting a histogram with PyPlot.hist(), was:

```julia
LAPACKException(1)

Stacktrace:
 [1] chklapackerror at D:\buildbot\worker\package_win64\build\usr\share\julia\stdlib\v1.5\LinearAlgebra\src\lapack.jl:38 [inlined]
 [2] syevr!(::Char, ::Char, ::Char, ::Array{Float64,2}, ::Float64, ::Float64, ::Int64, ::Int64, ::Float64) at D:\buildbot\worker\package_win64\build\usr\share\julia\stdlib\v1.5\LinearAlgebra\src\lapack.jl:5068
 [3] eigvals! at D:\buildbot\worker\package_win64\build\usr\share\julia\stdlib\v1.5\LinearAlgebra\src\symmetric.jl:731 [inlined]
 [4] eigvals!(::Array{Float64,2}; permute::Bool, scale::Bool, sortby::typeof(LinearAlgebra.eigsortby)) at D:\buildbot\worker\package_win64\build\usr\share\julia\stdlib\v1.5\LinearAlgebra\src\eigen.jl:292
 [5] eigvals! at D:\buildbot\worker\package_win64\build\usr\share\julia\stdlib\v1.5\LinearAlgebra\src\eigen.jl:292 [inlined]
 [6] #eigvals#73 at D:\buildbot\worker\package_win64\build\usr\share\julia\stdlib\v1.5\LinearAlgebra\src\eigen.jl:326 [inlined]
 [7] eigvals at D:\buildbot\worker\package_win64\build\usr\share\julia\stdlib\v1.5\LinearAlgebra\src\eigen.jl:326 [inlined]
 [8] get_stats(::Int64, ::Int64, ::Float64, ::Float64, ::Float64, ::Float64, ::Int64, ::Float64, ::Array{Float64,1}) at .\In[162]:12
 [9] top-level scope at In[197]:1
 [10] include_string(::Function, ::Module, ::String, ::String) at .\loading.jl:1091

```

But the minimal reproduction gave a slightly different error,.

---

<div class="post-metadata">

**Author:** ![jamblejoe](https://avatars.discourse-cdn.com/v4/letter/j/ee7513/32.png) [@jamblejoe](https://discourse.julialang.org/u/jamblejoe)\
**Post date:** [September 15, 2020, 12:47pm UTC](https://discourse.julialang.org/t/argumenterror-invalid-argument-4-to-lapack-call/46455/2 "2020-09-15T12:47:44Z")

</div>

I can reproduce on Windows, Julia 1.4.2 and 1.5.1 as well using

```julia
using LinearAlgebra
using Plots; pyplot()
n = 8
A = rand(n,n)
e = eigvals(A)
plot(e)
eigvals(A)

```

```julia
 ** On entry to DGEBAL parameter number 3 had an illegal value
 ** On entry to DGEHRD parameter number 3 had an illegal value
 ** On entry to DHSEQR parameter number 5 had an illegal value
ERROR: ArgumentError: invalid argument #5 to LAPACK call

```

Another invocation of `eigvals(A)` works fine. When doing `plot(e)` again, the error reappears.

Further, for `n<8`, the error does not appear.

I cannot reproduce this on Ubuntu 16.04. BLAS libraries are the standard openBLAS libraries shipped with Julia, for both the Windows and Ubuntu version.

---

<div class="post-metadata">

**Author:** ![ctkelley](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ctkelley/32/10684_2.png) [@ctkelley](https://discourse.julialang.org/u/ctkelley)\
**Post date:** [September 15, 2020, 2:00pm UTC](https://discourse.julialang.org/t/argumenterror-invalid-argument-4-to-lapack-call/46455/3 "2020-09-15T14:00:29Z")

</div>

Tried this and, aside from the complaint I expected, all went ok on a Mac with Julia 1.5.1. PyPlot does not seem to have an idea about plotting complex numbers when invoked with plot. I expected dots on the complex plane, but no joy.

```julia
using LinearAlgebra
using PyPlot
n = 8
A = rand(n,n)
e = eigvals(A)
plot(e)
eigvals(A)

```

and I got this

```julia
julia> n = 8
8

julia> A = rand(n,n)
8×8 Array{Float64,2}:
 8.02692e-01 4.28226e-01 3.70771e-01 … 4.01601e-01 2.58023e-01
 6.53753e-01 8.44660e-01 5.17042e-01 6.56434e-01 8.51823e-01
 8.16275e-01 1.40932e-01 3.45856e-01 2.60633e-01 8.39385e-02
 4.98498e-01 9.10384e-01 5.39722e-01 9.21009e-01 8.19976e-01
 3.97537e-01 5.39128e-01 6.88891e-02 6.68506e-01 4.97311e-01
 1.69636e-01 8.83659e-01 8.39805e-01 … 9.58478e-02 9.11797e-01
 1.32829e-01 4.76917e-01 6.74946e-01 7.29042e-02 6.22964e-02
 7.45336e-01 4.97381e-01 4.64289e-02 3.09182e-01 2.65707e-01

julia> e = eigvals(A)
8-element Array{Complex{Float64},1}:
 -8.46321e-01 + 0.00000e+00im
 -2.40214e-01 - 5.90445e-02im
 -2.40214e-01 + 5.90445e-02im
  1.62488e-01 - 3.16177e-01im
  1.62488e-01 + 3.16177e-01im
  1.95171e-01 + 0.00000e+00im
  4.69261e-01 + 0.00000e+00im
  4.08752e+00 + 0.00000e+00im

julia> plot(e)
/Users/ctk/.julia/conda/3/lib/python3.7/site-packages/numpy/core/_asarray.py:85: ComplexWarning: Casting complex values to real discards the imaginary part
  return array(a, dtype, copy=False, order=order)
1-element Array{PyCall.PyObject,1}:
 PyObject <matplotlib.lines.Line2D object at 0x7ffb50a32b10>

```

---

<div class="post-metadata">

**Author:** ![PetrKryslUCSD](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/petrkryslucsd/32/215825_2.png) [@PetrKryslUCSD](https://discourse.julialang.org/u/PetrKryslUCSD)\
**Post date:** [September 15, 2020, 3:16pm UTC](https://discourse.julialang.org/t/argumenterror-invalid-argument-4-to-lapack-call/46455/4 "2020-09-15T15:16:26Z")

</div>

![image](https://global.discourse-cdn.com/julialang/original/3X/3/d/3d03cdc0d4be86300f86842db6773059273465f0.png)

```julia
using LinearAlgebra
using PlotlyJS
n = 8
A = rand(n,n)
e = eigvals(A)
plot(real(e), imag(e), mode = "markers")
eigvals(A)

```

On Windows 10, with Julia 1.5.1. So I would say the problem is due to pyplot.

---

<div class="post-metadata">

**Author:** ![PhillipBC](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/phillipbc/32/9166_2.png) [@PhillipBC](https://discourse.julialang.org/u/PhillipBC)\
**Post date:** [September 15, 2020, 3:26pm UTC](https://discourse.julialang.org/t/argumenterror-invalid-argument-4-to-lapack-call/46455/5 "2020-09-15T15:26:07Z")

</div>

Indeed it would seem that it is a problem with PyPlot on Windows.  
Using a different backend for Plots (GR) results in no error also.

There is some recent activity on posts with similar errors to @jamblejoe

```julia
** On entry to DHSEQR parameter number 4 had an illegal value

```

but in the python community, i.e. [https://github.com/statsmodels/statsmodels/issues/3596](https://github.com/statsmodels/statsmodels/issues/3596)  
They seem to point to a recent Windows update causing a problem with OpenBLAS.

This would be inline with the errors I am getting, as I was not getting any for the last year or so doing the same calculations until recently.

---

<div class="post-metadata">

**Author:** ![simonbyrne](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/simonbyrne/32/19_2.png) [@simonbyrne](https://discourse.julialang.org/u/simonbyrne)\
**Post date:** [September 15, 2020, 3:31pm UTC](https://discourse.julialang.org/t/argumenterror-invalid-argument-4-to-lapack-call/46455/6 "2020-09-15T15:31:26Z")

</div>

Argument #4 is a character (see [`dgeevx`](http://www.netlib.org/lapack/explore-html/d9/d8e/group__double_g_eeigen_ga4e35e1d4e9b63ba9eef4ba8aff3debae.html)), so this might be the Fortran character argument bug?:  
[https://github.com/JuliaLang/julia/issues/32870](https://github.com/JuliaLang/julia/issues/32870)

---

<div class="post-metadata">

**Author:** ![simonbyrne](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/simonbyrne/32/19_2.png) [@simonbyrne](https://discourse.julialang.org/u/simonbyrne)\
**Post date:** [September 15, 2020, 3:38pm UTC](https://discourse.julialang.org/t/argumenterror-invalid-argument-4-to-lapack-call/46455/7 "2020-09-15T15:38:03Z")

</div>

Ah, it could be that by calling into python that you somehow end up calling the LAPACK used by numpy (or the Julia LAPACK uses the numpy BLAS)?

---

<div class="post-metadata">

**Author:** ![PhillipBC](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/phillipbc/32/9166_2.png) [@PhillipBC](https://discourse.julialang.org/u/PhillipBC)\
**Post date:** [September 15, 2020, 3:51pm UTC](https://discourse.julialang.org/t/argumenterror-invalid-argument-4-to-lapack-call/46455/8 "2020-09-15T15:51:02Z")

</div>

Yes I think it could be something along these lines.  
I tried to use MKL instead of openBLAS but I kept getting a permissions error when building the MKL package. Like this: [https://github.com/JuliaLang/julia/issues/30608](https://github.com/JuliaLang/julia/issues/30608)
