# Eye in julia 0.7

**URL:** <https://discourse.julialang.org/t/eye-in-julia-0-7/9820>\
**Category:** New to Julia\
**Created:** [March 19, 2018, 5:30pm UTC](https://discourse.julialang.org/t/eye-in-julia-0-7/9820 "2018-03-19T17:30:21Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![Jean\_Michel](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jean_michel/32/8282_2.png) [@Jean\_Michel](https://discourse.julialang.org/u/Jean_Michel)\
**Post date:** [March 19, 2018, 5:30pm UTC](https://discourse.julialang.org/t/eye-in-julia-0-7/9820/1 "2018-03-19T17:30:21Z")

</div>

I think I found a replacement for `eye` in julia 7.0

```julia
julia> c=rand(Int8,4,4)
4×4 Array{Int8,2}:
   44 -79 36 116
  -14 -64 -49 -52
   77 -110 -20 118
 -119 -57 -78 90

julia> @btime Matrix{eltype($c)}(I,size($c))
  47.614 ns (1 allocation: 112 bytes)
4×4 Array{Int8,2}:
 1 0 0 0
 0 1 0 0
 0 0 1 0
 0 0 0 1

julia> @btime $c^0
  33.817 ns (1 allocation: 112 bytes)
4×4 Array{Int8,2}:
 1 0 0 0
 0 1 0 0
 0 0 1 0
 0 0 0 1

```

Am I right? Is `c^0` a good replacement for `eye(c)` for square matrices?

---

<div class="post-metadata">

**Author:** ![ExpandingMan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/expandingman/32/866_2.png) [@ExpandingMan](https://discourse.julialang.org/u/ExpandingMan)\
**Post date:** [March 19, 2018, 5:52pm UTC](https://discourse.julialang.org/t/eye-in-julia-0-7/9820/2 "2018-03-19T17:52:27Z")

</div>

I do find it annoying that the constructor for a properly typed `I` in 0.7 is so verbose.

I think it is intended that you try to use `I` itself wherever possible, rather than constructing a new matrix, and the thinking was probably that the latter case is rare enough that it doesn’t need condensed syntax.

Anyway, I’m seeing that for larger matrices `c^0` is actually (very slightly) slower than the `Matrix` constructor. In cases like this where the performance differences are very slight it depends to be best to use the intended functionality, though I’m afraid I can’t comment further on exactly what `c^0` is doing.

---

<div class="post-metadata">

**Author:** ![mbauman](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mbauman/32/31082_2.png) [@mbauman](https://discourse.julialang.org/u/mbauman)\
**Post date:** [March 19, 2018, 6:00pm UTC](https://discourse.julialang.org/t/eye-in-julia-0-7/9820/3 "2018-03-19T18:00:26Z")

</div>

`c^0` just boils down to a call to `one(c)`. It’s slightly different than `Matrix{eltype(c)}(I, size(c))` in that it returns the multiplicative identity — which might be different for unitful quantities.

If `one(x)` is faster than `Matrix{T}(I, size(x))`, then that’s a performance bug in the `Matrix` constructor.

---

<div class="post-metadata">

**Author:** ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)\
**Post date:** [March 19, 2018, 6:02pm UTC](https://discourse.julialang.org/t/eye-in-julia-0-7/9820/4 "2018-03-19T18:02:24Z")

</div>

You can just use `one(A)` or `oneunit(A)` (depending on how you want to handle the dimensionful case) for the equivalent of `eye(A)` for square matrices.

(`eye` also worked for non-square matrices, but I don’t know of a non-verbose equivalent on 0.7.)

---

<div class="post-metadata">

**Author:** ![Jean\_Michel](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jean_michel/32/8282_2.png) [@Jean\_Michel](https://discourse.julialang.org/u/Jean_Michel)\
**Post date:** [March 19, 2018, 6:33pm UTC](https://discourse.julialang.org/t/eye-in-julia-0-7/9820/5 "2018-03-19T18:33:55Z")

</div>

with the same example as above

```julia
julia> @btime one($c)
  27.680 ns (1 allocation: 112 bytes)

```

quite a bit faster than the `Matrix` constructor…

---

<div class="post-metadata">

**Author:** ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)\
**Post date:** [March 20, 2018, 1:01pm UTC](https://discourse.julialang.org/t/eye-in-julia-0-7/9820/6 "2018-03-20T13:01:26Z")

</div>

The cost of either is trivial in itself, and is dwarfed by anything meaningful you may do with the matrix, eg

```julia
using BenchmarkTools
c = rand(Int8, 10, 10)
altone(A) = Matrix{eltype(A)}(I,size(A))
twice(A) = one(A) * 2
alttwice(A) = altone(A) * 2

```

gives

```julia
julia> @btime twice($c);
  142.838 ns (2 allocations: 1.06 KiB)

julia> @btime alttwice($c);
  157.760 ns (2 allocations: 1.06 KiB)

```

---

<div class="post-metadata">

**Author:** ![Jean\_Michel](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jean_michel/32/8282_2.png) [@Jean\_Michel](https://discourse.julialang.org/u/Jean_Michel)\
**Post date:** [March 20, 2018, 1:06pm UTC](https://discourse.julialang.org/t/eye-in-julia-0-7/9820/7 "2018-03-20T13:06:57Z")

</div>

Agreed, but the question is: when I have a square matrix `A`, should I type the suggested replacement `Matrix{eltype(A)}(I,size(A))` for `eye(A)` while `one(A)` seems to give the same result and is faster?

---

<div class="post-metadata">

**Author:** ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)\
**Post date:** [March 20, 2018, 1:46pm UTC](https://discourse.julialang.org/t/eye-in-julia-0-7/9820/8 "2018-03-20T13:46:05Z")

</div>

My understanding was that the suggested [solution](https://discourse.julialang.org/t/eye-in-julia-0-7/9820/4) is `one`, so it is not clear to me why one would use something else.

---

<div class="post-metadata">

**Author:** ![Jean\_Michel](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jean_michel/32/8282_2.png) [@Jean\_Michel](https://discourse.julialang.org/u/Jean_Michel)\
**Post date:** [March 20, 2018, 1:47pm UTC](https://discourse.julialang.org/t/eye-in-julia-0-7/9820/9 "2018-03-20T13:47:52Z")

</div>

```julia
julia> eye(rand(4,4))
WARNING: Base.eye is deprecated: it has been moved to the standard library package `LinearAlgebra`.
Add `using LinearAlgebra` to your imports.
 in module Main
┌ Warning: `eye(A::AbstractMatrix{T}) where T` has been deprecated in favor of `I` and `Matrix` constructors. For a direct replacement, consider `Matrix{eltype(A)}(I, size(A))`.If `eltype(A)` element type is not necessary, consider the shorter `Matrix(I, size(A))` (with default `eltype(I)` `Bool`).
│ caller = top-level scope
└ @ Core :0
4×4 Array{Float64,2}:
 1.0 0.0 0.0 0.0
 0.0 1.0 0.0 0.0
 0.0 0.0 1.0 0.0
 0.0 0.0 0.0 1.0

```

---

<div class="post-metadata">

**Author:** ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)\
**Post date:** [March 20, 2018, 1:49pm UTC](https://discourse.julialang.org/t/eye-in-julia-0-7/9820/10 "2018-03-20T13:49:26Z")

</div>

I agree that suggesting `one(A)` would be a better choice there.

---

<div class="post-metadata">

**Author:** ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)\
**Post date:** [March 20, 2018, 2:35pm UTC](https://discourse.julialang.org/t/eye-in-julia-0-7/9820/11 "2018-03-20T14:35:56Z")

</div>

> [@Tamas\_Papp](#):
>
> I agree that suggesting one(A) would be a better choice there.

No, because `one` only works for square matrices.

---

<div class="post-metadata">

**Author:** ![joshualeond](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/joshualeond/32/230_2.png) [@joshualeond](https://discourse.julialang.org/u/joshualeond)\
**Post date:** [March 20, 2018, 2:44pm UTC](https://discourse.julialang.org/t/eye-in-julia-0-7/9820/12 "2018-03-20T14:44:01Z")

</div>

Just out of curiosity, what’s the motivation for deprecating `eye`?

---

<div class="post-metadata">

**Author:** ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)\
**Post date:** [March 20, 2018, 2:46pm UTC](https://discourse.julialang.org/t/eye-in-julia-0-7/9820/13 "2018-03-20T14:46:37Z")

</div>

Certainly, but asking for a multiplicative identity is a probably the most common case.

---

<div class="post-metadata">

**Author:** ![fredrikekre](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fredrikekre/32/1688_2.png) [@fredrikekre](https://discourse.julialang.org/u/fredrikekre)\
**Post date:** [March 20, 2018, 3:11pm UTC](https://discourse.julialang.org/t/eye-in-julia-0-7/9820/14 "2018-03-20T15:11:21Z")

</div>

> [@joshualeond](#):
>
> Just out of curiosity, what’s the motivation for deprecating eye?

See [make I(n) behave as `Diagonal` / `eye`? · Issue #23156 · JuliaLang/julia · GitHub](https://github.com/JuliaLang/julia/issues/23156) and the linked issues, PR’s

---

<div class="post-metadata">

**Author:** ![tkoolen](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tkoolen/32/1603_2.png) [@tkoolen](https://discourse.julialang.org/u/tkoolen)\
**Post date:** [March 20, 2018, 3:45pm UTC](https://discourse.julialang.org/t/eye-in-julia-0-7/9820/15 "2018-03-20T15:45:47Z")

</div>

I’ve always wondered why there isn’t something like

```julia
struct Identity{T} <: AbstractMatrix{T}
    n::Int
    m::Int # perhaps
end

```

i.e., a dedicated `isbits` identity matrix type that is an `AbstractMatrix`, and has both size and `eltype`. This type would also be useful for cases such as [https://github.com/JuliaLang/julia/issues/23156#issuecomment-340216693](https://github.com/JuliaLang/julia/issues/23156#issuecomment-340216693).

I understand that part of the objective of the change was to reduce the number of ways to create an identity matrix, but I find the typelessness and shapelessness of `I` only useful in a subset of cases, and the `Diagonal` version allocates (unless you make a special `isbits` `AbstractVector` type for the diagonal).

---

<div class="post-metadata">

**Author:** ![mbauman](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mbauman/32/31082_2.png) [@mbauman](https://discourse.julialang.org/u/mbauman)\
**Post date:** [March 20, 2018, 3:57pm UTC](https://discourse.julialang.org/t/eye-in-julia-0-7/9820/16 "2018-03-20T15:57:08Z")

</div>

I’d be curious to hear what cases you have that require shape but not mutability.

---

<div class="post-metadata">

**Author:** ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)\
**Post date:** [March 20, 2018, 4:07pm UTC](https://discourse.julialang.org/t/eye-in-julia-0-7/9820/17 "2018-03-20T16:07:48Z")

</div>

> [@Tamas\_Papp](#):
>
> Certainly, but asking for a multiplicative identity is a probably the most common case.

The deprecation has to give code that _always_ works, not just code that works in the common case.

---

<div class="post-metadata">

**Author:** ![tkoolen](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tkoolen/32/1603_2.png) [@tkoolen](https://discourse.julialang.org/u/tkoolen)\
**Post date:** [March 20, 2018, 4:08pm UTC](https://discourse.julialang.org/t/eye-in-julia-0-7/9820/18 "2018-03-20T16:08:08Z")

</div>

Well, [https://github.com/JuliaLang/julia/issues/23156#issuecomment-340216693](https://github.com/JuliaLang/julia/issues/23156#issuecomment-340216693) for one. It could also be used as an implicit shape check in stuff like

```julia
I33 = Identity{Float64}(3, 3)
[rand(4, 4) I33;
 I33 rand(4, 4)]

```

This would work if `I33` were replaced with `I`, but there may be cases where that behavior is actually not desired.

---

<div class="post-metadata">

**Author:** ![Jean\_Michel](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jean_michel/32/8282_2.png) [@Jean\_Michel](https://discourse.julialang.org/u/Jean_Michel)\
**Post date:** [March 20, 2018, 4:12pm UTC](https://discourse.julialang.org/t/eye-in-julia-0-7/9820/19 "2018-03-20T16:12:17Z")

</div>

Should not the deprecation help the programmer as much as possible? Here the most common subcase  
has a nicer/shorter deprecation, so should not this be mentioned in the deprecation message?

---

<div class="post-metadata">

**Author:** ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)\
**Post date:** [March 20, 2018, 4:16pm UTC](https://discourse.julialang.org/t/eye-in-julia-0-7/9820/20 "2018-03-20T16:16:37Z")

</div>

> [@Jean\_Michel](#):
>
> Should not the deprecation help the programmer as much as possible? Here the most common subcase has a nicer/shorter deprecation, so should not this be mentioned in the deprecation message?

Auto-deprecations are done in Julia with `@deprecate oldfunc(args...) newfunc(newargs...)`, where `newfunc` should give the same result as `oldfunc` in all cases.

It is possible to make custom deprecation messages, but it is more work and we usually don’t bother (since the deprecation message will go away entirely in the subsequent release anyway).

Note also that, in this particular case, you are really strongly encouraged to re-think constructing a dense identity matrix entirely — there is almost always a better way to do things. What did you need `eye` for anyway?

[Next page](https://discourse.julialang.org/t/eye-in-julia-0-7/9820.md?page=2)
