5

If I have some declared some Points in Julia (p_1,...,p_n). Is there some function or algorithm for kronecker delta (f_i(p_j)=1 if i=j and f_i(p_j)=0 if i != j)

It would be very helpful.

Thank you so much.

François Févotte
  • 19,520
  • 4
  • 51
  • 74
Tio Miserias
  • 485
  • 2
  • 8

2 Answers2

8

If you want a kronecker delta function you can use the ==(x,y) function (as indicated by @mbauman in the comments).

julia> δ(x,y) = ==(x,y)
δ (generic function with 1 method)

julia> δ(1,1)
true

julia> δ(1,2)
false

Note that this returns true or false instead of 1 and 0 but the former are essentially equal to the latter and will behave in the same way, for example ==(1,1) * 2 will give 2. In fact, true isa Integer in Julia.

Another option might be to use I the (lazy) identity matrix built into Julia (LinearAlgebra that is):

julia> using LinearAlgebra

julia> I[1,1]
true

julia> I[1,2]
false
carstenbauer
  • 9,817
  • 1
  • 27
  • 40
0

For an arbitrary number of arguments, you can do:

δ(x, y) = ==(x, y)

function δ(x, y, z...)
    !δ(x, y) && return false
    for i in z
        !δ(x, i) && return false
    end
    return true
end

or if you make the convention that δ(x) := true:

δ(x, y) = ==(x, y)

function δ(z...)
    for i in z
        !δ(z[1], i) && return false
    end
    return true
end
amrods
  • 2,029
  • 1
  • 17
  • 28