Int / Float type inference in constructor

I defined a struct as follows:

struct inverse{T<:Number}
function inverse{T}(x0::Tuple{...,T,...}) where T<:Number
x = convert_to_x(x0)
y = inv(x)

where the constructor is incomplete. The idea is that y=inv(x), both are square matrices with the same eltype. The input is some nested tuple that contains column vectors (or tuples), I first convert it to a matrix then take the inverse. The problem is when x0 is Int-valued, x becomes Int-valued, and y is float-valued. I can attempt to promote (x,y) to float-valued, but the difficulty is the type T will be inferenced as Int, so the struct object still contains Int-valued arrays. How am I supposed to force the promotion without knowing a-priori the promoted type T?

Does it have to be an inner constructor? If not, there is no problem, I think.

function inverse(x0)
  x = [...]
  y = [...]

Thanks. I had problems with outer constructor. It looks like if this struct is nested in another struct as a field, only the inner constructor is called when the nesting struct is created, in which case I get an error complaining “no matching method for blah blah …” even though that particular method is defined as an outer constructor and works from the REPL.

This is mostly unrelated to your question, but you want Matrix{T} not Array{T}

1 Like

Why do you want this? I’m very curious to hear a good reason for a numerical matrix inverse.

1 Like

You are right. Mostly we don’t want to explicitly compute matrix inverse. My situation is that those are 2x2 SMatrix which are used frequently. It makes the code look cleaner w/o much affecting performance.