English 中文(简体)
Dispatch cannot match a method when defining the dimension type of `NTuple()`
原标题:
  • 时间:2023-07-24 02:30:08
  •  标签:
  • julia

I m trying to figure out how to define the dimensions of a tuple when calling a function or contructing a struct when the arguments contain a NTuple type. If I leave the dimension variable (which defines the size of the Tuple) "free" (<:Any), everything works and it is of an Integer subtype. If I try to define it as any Integer subtype I get an error that the dispatch cannot match a method.

Here s a concrete example: Say that I want to generate several variables of a vector or tuple type and force the same dimensions nDims. Here is a "stupified" version where all other things are simplified:

struct GridStruct{nDims}
    NumCells :: NTuple{nDims, Integer}
    DomainLength :: NTuple{nDims, AbstractFloat}
    x :: Base.AbstractVecOrTuple{AbstractFloat}
    y :: Base.AbstractVecOrTuple{AbstractFloat}
    z :: Base.AbstractVecOrTuple{AbstractFloat}

    function GridStruct(DomainLength::NTuple{nDims,AbstractFloat},NumCells::NTuple{nDims,Integer}) where nDims
        
        x=GenerateCoordinate(NumCells[1], DomainLength[1]);
        y=GenerateCoordinate(NumCells[2], DomainLength[2]);
        z=GenerateCoordinate(NumCells[3], DomainLength[3]);

        new{typeof(nDims)}(NumCells,DomainLength,xf,xc,yf,yc,zf,zc)
    end
 end

You can set GenerateCoordinate to be whatever you want, like a simple uniform linear distribution:

GenerateCoordinate(N::Integer,L::AbstractFloat)=Base._linspace(0.0,L,N)

My question revolves around the definition of nDims here: when I let it be "free" (so not defined", everything works fine and when I test the type of nDims - it is indeed a subtype of Integer (Int64 on my system) and has the expected value of 3 (as this is a 3D space). If I try to define nDims in any way, say nDims<:Integer, I get an error that there is "no method" which matches:

ERROR: MethodError: no method matching Main.GridGenModule.GridStruct(::Tuple{Float64, Float64, Float64}, ::Tuple{Int64, Int64, Int64})

If I use nDims<:Any, it obiously works - but there is nothing between ::Number and ::Any that I know of... And from what I can see, it is not any other type either. I m trying to understand why defining nDims (i.e. nDims<:Integer) causes issues.

I ve seen this question that is related but there was no explanation or content regarding the issue I m asking about. I know how to get it to work, this is more for me to understand the "mechanics" of this.

Thank you so much!

问题回答

暂无回答




相关问题
刚开始和结束的les

我想利用开端和终点拖车来构筑CartesianIndices。 例如,我想在开端=(2,3)和端=(4,5)的情况下建造Cartesian Indices((2,4,3:5)。

Julia symbolic differentiation

Though I ve looked through several pages documenting various differentiation tools in Julia, I have yet to find the following simple functionality. I want to define a function which is differentiable ...

Julia: Return Minimum Date in DataFrame

The question is fairly simple. How do I return the minimum purchase date for each customer using Tidier? using Tidier, DataFrames, Plots, CSV #params f = "path" df = CSV.File(f) |> ...

Reading hex string file in Julia

In python, I do import numpy as np data = np.memmap( mydata.bin , dtype= <i4 , mode= r ) How can I implement this in Julia? I was reading about readbytes and htol but couldn t figure out.

热门标签