I am writing n-dimensional algorithms and it would be convenient to call this function with either numbers or matrices.
Could this method be added to Base? Any counter-argument?
If you're working on n
-dimensional arrays and you need to support the n=0
case, Julia has a 0-dimensional array type Array{T, 0}
:
julia> fill(4)
0-dimensional Array{Int64, 0}:
4
that might work for your application.
Would it have the same performance benefits of a Number though? Stack allocation?
Ideally the algorithm would retain its performance with Number inputs
mydiag(x::AbstractArray) = diag(x)
mydiag(x::Number) = x
Yes. My question is if there is a problem in defining this in Base?
I don't really see why we'd put this in base. I think it'd be better to just define your own mydiag
like Fredrik showed
Mathematically speaking, doesn't it make sense to extend the method?
I've seen Number used in places that expect matrices or vectors as inputs.
Numbers are not matrices or vectors. They're zero dimensional objects. diag
is specifically defined on matrices
I thought that numbers could be seen as linear algebras over themselves.
In that case one can introduce a coord system and identify the number with itself. Consequently defining diag too
But if people already see a problem with this on Zulip. The chances it gets accepted in Base are low
Last updated: Jan 29 2025 at 04:38 UTC