# How do I use MATLAB.jl and vpa?

**URL:** <https://discourse.julialang.org/t/how-do-i-use-matlab-jl-and-vpa/18595>\
**Category:** Numerics\
**Created:** [December 12, 2018, 1:57pm UTC](https://discourse.julialang.org/t/how-do-i-use-matlab-jl-and-vpa/18595 "2018-12-12T13:57:58Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Noel\_Araujo](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/noel_araujo/32/4862_2.png) [@Noel\_Araujo](https://discourse.julialang.org/u/Noel_Araujo)\
**Post date:** [December 12, 2018, 1:57pm UTC](https://discourse.julialang.org/t/how-do-i-use-matlab-jl-and-vpa/18595/1 "2018-12-12T13:57:58Z")

</div>

Hello everyone,

I am want to do something conceptually simple using MATLAB.jl package. I have a BigFloat matrix in Julia, and I want to send to Matlab to compute its inverse/determinant/eigenvalues with arbitrary-precision arithmetic (vpa) and return back to Julia.  
Assuming that Matlab is already in local PATH, the following example should work:

```julia
using MATLAB
using LinearAlgebra
using GenericLinearAlgebra

m = n = 2
a = rand(m, n)
x = mxarray(a)

# works OK
inv(a)  
mat"inv($x)"

det(a)
mat"det($x)"

eigen(a)
mat"eig($x)"

# NOT OK with big numbers
b = big.(a)
y = mxarray(b)
# Maybe a better way to write it could be
# mat"y = vpa($b,60)" 

det(b) 
mat"det($y)" # returns: Undefined function 'det' for input arguments of type 'cell'.

eigen(b) # ? how to make it work ?
mat"eig($y)" # returns a bigger message complaining about types

```

By the error messages I guess that MATLAB.jl convert Julia type in some data structure, and send to Matlab to be executed. Any ideas of how to handle my example with BigFloat ?

Thank you

---

<div class="post-metadata">

**Author:** ![stillyslalom](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stillyslalom/32/45687_2.png) [@stillyslalom](https://discourse.julialang.org/u/stillyslalom)\
**Post date:** [December 12, 2018, 4:53pm UTC](https://discourse.julialang.org/t/how-do-i-use-matlab-jl-and-vpa/18595/2 "2018-12-12T16:53:22Z")

</div>

Matlab’s variable-precision arithmetic library likely uses a different representation for its floats than GMP, and they probably can’t be translated without some amount of effort. Is there some reason you don’t want to do the calculations in Julia? These all work fine:

```julia
using LinearAlgebra, GenericLinearAlgebra

A = big.(rand(3,3) + I)
inv(A)
det(A)
eigvals(A) 

```

---

<div class="post-metadata">

**Author:** ![Noel\_Araujo](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/noel_araujo/32/4862_2.png) [@Noel\_Araujo](https://discourse.julialang.org/u/Noel_Araujo)\
**Post date:** [December 12, 2018, 6:10pm UTC](https://discourse.julialang.org/t/how-do-i-use-matlab-jl-and-vpa/18595/3 "2018-12-12T18:10:16Z")

</div>

I am doing a mixture codes, some are already written in Matlab and uses vpa. I am just afraid to use BigFloat and don’t have the level of precision that I have with vpa.

By the way, how can I make `eigvecs()` to work with BigFloats ?

---

<div class="post-metadata">

**Author:** ![stillyslalom](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stillyslalom/32/45687_2.png) [@stillyslalom](https://discourse.julialang.org/u/stillyslalom)\
**Post date:** [December 12, 2018, 6:24pm UTC](https://discourse.julialang.org/t/how-do-i-use-matlab-jl-and-vpa/18595/4 "2018-12-12T18:24:01Z")

</div>

BigFloat has arbitrary precision. It looks like ArbitraryLinearAlgebra is limited to Hermitian matrices for eigenvectors:

```julia
using LinearAlgebra, GenericLinearAlgebra
setprecision(80)

A = rand(BigFloat,3,3) + I
hA = Hermitian(A*A')
eigvecs(hA)

```

---

<div class="post-metadata">

**Author:** ![Noel\_Araujo](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/noel_araujo/32/4862_2.png) [@Noel\_Araujo](https://discourse.julialang.org/u/Noel_Araujo)\
**Post date:** [December 12, 2018, 6:41pm UTC](https://discourse.julialang.org/t/how-do-i-use-matlab-jl-and-vpa/18595/5 "2018-12-12T18:41:53Z")

</div>

I need to work with non-hermitian matrices and I will use matlab meanwhile to do the job - I hope that someday this operation can be done in Julia, it will help my a lot.

Thank you by the reply

---

<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:** [December 12, 2018, 8:12pm UTC](https://discourse.julialang.org/t/how-do-i-use-matlab-jl-and-vpa/18595/6 "2018-12-12T20:12:44Z")

</div>

Another option is to code it up yourself, using a standard algorithm from a book like Golub and Van Loan: that’s more or less what I did with [GenericSVD.jl](https://github.com/JuliaLinearAlgebra/GenericSVD.jl) (you may even be able to reuse some of that code).

---

<div class="post-metadata">

**Author:** ![stillyslalom](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stillyslalom/32/45687_2.png) [@stillyslalom](https://discourse.julialang.org/u/stillyslalom)\
**Post date:** [December 12, 2018, 10:21pm UTC](https://discourse.julialang.org/t/how-do-i-use-matlab-jl-and-vpa/18595/7 "2018-12-12T22:21:22Z")

</div>

Using inverse iteration:

```julia
using LinearAlgebra, GenericLinearAlgebra
import LinearAlgebra.eigen

function eigen(A::Array{T,2}) where T <: Union{BigFloat,Complex{BigFloat}}
    ni, nj = size(A)
    Λ = eigvals(A)
    if norm(imag(Λ)) < eps(BigFloat)
        Λ = real(Λ)
    end
    
    V = rand(eltype(Λ), nj, nj)
    bkp = similar(V, nj)
    for j = 1:nj
        Δ = BigFloat(Inf)
        bk = view(V,:,j)
        f = qr(A - Λ[j]*I)
        i = 0
        while Δ > eps(BigFloat) && i < 10
            bkp .= f\bk
            ck = norm(bkp)
            bkp ./= ck
            Δ = norm(bkp - bk)
            bk .= bkp
            i += 1
        end
    end
    return Λ, V
end

```
