How to convert fmpq to Int

v = fmpq[1,0,0] vector,

how to convert this to just [1,0,0]

What is fmpq? Please read:

fmpq = frequently making puzzling questions

1 Like

if the denominator() = 1

  • Then use the numerator()
  • Else Ouch …
julia> using Nemo
julia> v = fmpq[1,0,0]
3-element Vector{fmpq}:
 1
 0
 0
julia> biv =BigInt[]
BigInt[]
julia> for a in v
           n = numerator(a)
           d = denominator(a)  
           if isone(d)
             push!(biv, BigInt(n) )  
           else
             println("ouch ..")
           end
       end
julia> println(biv)
BigInt[1, 0, 0]
1 Like

hint: if your question is a nemo related

  • Then you can add a nemo tag for your question.

See other nemo related questions:

Nemo types

And you can check the source code

1 Like

better version

using Nemo
int_numerator_fmpq(v) = ifelse( unique!(denominator.(v)) == [fmpz(1)], Int.(numerator.(v)), missing ); 

int_numerator_fmpq( fmpq[1,0,0]                  )
int_numerator_fmpq( fmpq[1//3, 9//2 , 1//1 ]     )
int_numerator_fmpq( fmpq[3//3, 999//999, 0//2 ]  )

log:

julia> int_numerator_fmpq(v) = ifelse( unique!(denominator.(v)) == [fmpz(1)], Int.(numerator.(v)), missing );

julia> int_numerator_fmpq( fmpq[1,0,0]               )
3-element Vector{Int64}:
 1
 0
 0

julia> int_numerator_fmpq( fmpq[1//3, 9//2 , 1//1 ]  )
missing

julia> int_numerator_fmpq( fmpq[3//3, 999//999, 0//2 ]  )
3-element Vector{Int64}:
 1
 1
 0
1 Like