2

I have a Fortran function like below,

subroutine f(i,weight)
integer(kind=i8) :: i
real(kind=r8) :: weight(i,i)
return 
end

The matrix weight is a i by i matrix, and i is also an argument.

I want the same thing in Julia, I did below

function f(i::Int64, weight::Array{Float64,2}(undef,i,i))
return nothing
end

However it just give me an error,

UndefVarError: i not defined

But if I just do below without the weight matrix as an argument, it does not give error,

function f(i::Int64)
return nothing
end

But obviously this is not what I want.

Or, the following also no error,

function f(i::Int64, weight::Array{Float64,2})
return nothing
end

But this is not what I want. I want the weight matrix in the argument have particular dimensions i by i.

Do I have to do things like

function f(i::Int64, weight::Array{Float64,2})
weight = Array{Float64,2}(undef,i,i)
return nothing
end

But again, I want the weight in the argument have the i by i definition. How to do that in Julia?

Thank you very much indeed!

2
  • Why pass i as an argument? Why not just get it from the size of the matrix? i = size(weight, 1) should do the trick. Commented Aug 4, 2021 at 7:45
  • @tholy Not really, it is just an illustration code. Int64 i have other use. The array weight have to be (i,i) size. it is also kind of a protection. In this example, you cannot put any array in the argument, it has to be (i,i) size. i needs to be determined first. Commented Aug 4, 2021 at 8:11

1 Answer 1

3

Standard Array does not have its size as a part of type specification. If you want this feature use https://github.com/JuliaArrays/StaticArrays.jl instead.

However, most of the time it is just enough to write:

function f(i::Int, weight::Matrix{Float64})
    @assert size(weight) == (i, i)
end

This time the check is made within a function and not at method dispatch time, but typically this is enough.

If you strictly would require the the behavior you want at dispatch time then the method signature using https://github.com/JuliaArrays/StaticArrays.jl would be:

function f(i::Val{T}, weight::SMatrix{T, T, Float64}) where T::Int
end

Note that I need to wrap i in Val to make sure the number passed is available to the compiler and is not only a run-time value.

Sign up to request clarification or add additional context in comments.

2 Comments

Thank you so much! By the way, could you please me know where can I find those @ tricks? I see you used @ assert size, sometimes I also see @eval, etc. But not sure where to find a place such that all the usage of @ is duscussed.
Probably there is no single place. @ signals a macro, see docs.julialang.org/en/v1/manual/metaprogramming/#man-macros. They can be found in the Julia manual the same way as functions can be found - in the parts of the documentation where they fit (there is nothing in-built with @ - you can define your own macros also).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.