将矩阵从Nemo.jl转换为普通的Julia Array? [英] Convert matrix from Nemo.jl to ordinary Julia Array?

查看:69
本文介绍了将矩阵从Nemo.jl转换为普通的Julia Array?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在这里复制此答案的以下代码段作为示例:

I copy here the following code snippet from this answer as an example:

using Nemo   # install with Pkg.add("Nemo")

S = MatrixSpace(ZZ, 3, 4)
mm = rand(-10:10,3,4)
m = S(mm)
(bmat,d) = nullspace(m)

现在,bmat的类型为Nemo.fmpz_mat.我想将其转换为普通的Julia Matrix{Int}.我尝试了Matrix{Int}(bmat),但是它不起作用.

Now bmat is of type Nemo.fmpz_mat. I want to convert it to an ordinary Julia Matrix{Int}. I tried Matrix{Int}(bmat) but it doesn't work.

另请参阅: https://github.com/wbhart/Nemo.jl/Issues/57

推荐答案

我定义了自己的convert:

function Base.convert(::Type{Matrix{Int}}, x::Nemo.fmpz_mat)
    m,n = size(x)
    mat = Int[x[i,j] for i = 1:m, j = 1:n]
    return mat
end

Base.convert(::Type{Matrix}, x::Nemo.fmpz_mat) = convert(Matrix{Int}, x)

这篇关于将矩阵从Nemo.jl转换为普通的Julia Array?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆