# Linear Algebra - Unit vectors e\_i

**URL:** <https://discourse.julialang.org/t/linear-algebra-unit-vectors-e-i/10409>\
**Category:** Specific Domains\
**Created:** [April 18, 2018, 10:14am UTC](https://discourse.julialang.org/t/linear-algebra-unit-vectors-e-i/10409 "2018-04-18T10:14:44Z")\
**Posts on this page:** 16\
**Page:** 1

<div class="post-metadata">

**Author:** ![v-i-s-h](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/v-i-s-h/32/46152_2.png) [@v-i-s-h](https://discourse.julialang.org/u/v-i-s-h)\
**Post date:** [April 18, 2018, 10:14am UTC](https://discourse.julialang.org/t/linear-algebra-unit-vectors-e-i/10409/1 "2018-04-18T10:14:44Z")

</div>

Hi,

Is there an easy way to add a standard unit vector (e\_i, which is vector of length ‘l’ with all zeros except for ith position?  
Example: Let `x` be a vector of length `5`. I want to add e\_3 (= [0 0 1 0 0]) to it.  
Right now I’m doing it as

```julia
y = x + eye(5)[:,3]

```

Is there any nice way to do it like `x + e[3]` kind of syntax?

Regards,  
vish

---

<div class="post-metadata">

**Author:** ![kristoffer.carlsson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kristoffer.carlsson/32/22_2.png) [@kristoffer.carlsson](https://discourse.julialang.org/u/kristoffer.carlsson)\
**Post date:** [April 18, 2018, 10:19am UTC](https://discourse.julialang.org/t/linear-algebra-unit-vectors-e-i/10409/2 "2018-04-18T10:19:40Z")

</div>

> [@v-i-s-h](#):
>
> Example: Let x be a vector of length 5. I want to add e3e\_3 (= [0 0 1 0 0]) to it.

`x[3] += 1`?

---

<div class="post-metadata">

**Author:** ![v-i-s-h](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/v-i-s-h/32/46152_2.png) [@v-i-s-h](https://discourse.julialang.org/u/v-i-s-h)\
**Post date:** [April 18, 2018, 10:32am UTC](https://discourse.julialang.org/t/linear-algebra-unit-vectors-e-i/10409/3 "2018-04-18T10:32:26Z")

</div>

My actual use case is a little more complicated: I need to do

```julia
y = (1-c) * x + c * e_3

```

---

<div class="post-metadata">

**Author:** ![kristoffer.carlsson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kristoffer.carlsson/32/22_2.png) [@kristoffer.carlsson](https://discourse.julialang.org/u/kristoffer.carlsson)\
**Post date:** [April 18, 2018, 10:37am UTC](https://discourse.julialang.org/t/linear-algebra-unit-vectors-e-i/10409/4 "2018-04-18T10:37:02Z")

</div>

```julia
y = (1-c) * x
y[3] += c

```

maybe

---

<div class="post-metadata">

**Author:** ![v-i-s-h](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/v-i-s-h/32/46152_2.png) [@v-i-s-h](https://discourse.julialang.org/u/v-i-s-h)\
**Post date:** [April 18, 2018, 10:45am UTC](https://discourse.julialang.org/t/linear-algebra-unit-vectors-e-i/10409/5 "2018-04-18T10:45:03Z")

</div>

Yeah… that’s one way. But it’s still two lines 🙁  
But is there any way to do this in one line?  
Or May be I should settle with this.

---

<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:** [April 18, 2018, 10:46am UTC](https://discourse.julialang.org/t/linear-algebra-unit-vectors-e-i/10409/6 "2018-04-18T10:46:30Z")

</div>

> [@v-i-s-h](#):
>
> But is there any way to do this in one line?

```julia
y = (1-c) * x; y[3] += c

```

:troll:

---

<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:** [April 18, 2018, 11:06am UTC](https://discourse.julialang.org/t/linear-algebra-unit-vectors-e-i/10409/8 "2018-04-18T11:06:05Z")

</div>

> [@v-i-s-h](#):
>
> May be I should settle with this.

Possibly yes. There is a trade-off between brevity and transparency; introducing special syntax for all of these things in `Base` is probably not worth it. If, however, you do this a lot in a package, you could write a tiny function for it. Especially if you want to make it generic so that it can handle `StaticVector` and other special types.

---

<div class="post-metadata">

**Author:** ![antoine-levitt](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/antoine-levitt/32/4008_2.png) [@antoine-levitt](https://discourse.julialang.org/u/antoine-levitt)\
**Post date:** [April 18, 2018, 11:17am UTC](https://discourse.julialang.org/t/linear-algebra-unit-vectors-e-i/10409/9 "2018-04-18T11:17:57Z")

</div>

It could maybe be useful to have a lazy array, like I (UniformScaling), defined by e(i)[j] = i == j ? 1 : 0. Then one could define I[:,i] to be equal to that, for instance. That could be a neat little package.

---

<div class="post-metadata">

**Author:** ![yuyichao](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yuyichao/32/20_2.png) [@yuyichao](https://discourse.julialang.org/u/yuyichao)\
**Post date:** [April 18, 2018, 11:47am UTC](https://discourse.julialang.org/t/linear-algebra-unit-vectors-e-i/10409/10 "2018-04-18T11:47:52Z")

</div>

`y = (1 - c) .* x .+ c .* (1:5 .== 3)`

---

<div class="post-metadata">

**Author:** ![cortner](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cortner/32/204_2.png) [@cortner](https://discourse.julialang.org/u/cortner)\
**Post date:** [April 18, 2018, 1:26pm UTC](https://discourse.julialang.org/t/linear-algebra-unit-vectors-e-i/10409/11 "2018-04-18T13:26:17Z")

</div>

I’ve often wanted this myself. Julia prides itself in being readable and close to Mathematical notation so I think this was a perfectly fair question.

---

<div class="post-metadata">

**Author:** ![jlperla](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jlperla/32/34332_2.png) [@jlperla](https://discourse.julialang.org/u/jlperla)\
**Post date:** [April 18, 2018, 2:31pm UTC](https://discourse.julialang.org/t/linear-algebra-unit-vectors-e-i/10409/12 "2018-04-18T14:31:15Z")

</div>

Yes, I think this would be an excellent addition to FillArrays, along with the sort of stuff I mentioned in [Possibility of a generically sized vector of 1s and 0s similar to the UniformScalingOperator in Juila v0.7](https://discourse.julialang.org/t/possibility-of-a-generically-sized-vector-of-1s-and-0s-similar-to-the-uniformscalingoperator-in-juila-v0-7/10297)

A bold e (as an optional function name in those cases) is one possible syntactic sugar

---

<div class="post-metadata">

**Author:** ![v-i-s-h](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/v-i-s-h/32/46152_2.png) [@v-i-s-h](https://discourse.julialang.org/u/v-i-s-h)\
**Post date:** [April 18, 2018, 2:32pm UTC](https://discourse.julialang.org/t/linear-algebra-unit-vectors-e-i/10409/13 "2018-04-18T14:32:37Z")

</div>

I think for the moment @yuyichao answer is the closest possible solution for this.  
And @antoine-levitt also has some interesting stuff!

---

<div class="post-metadata">

**Author:** ![antoine-levitt](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/antoine-levitt/32/4008_2.png) [@antoine-levitt](https://discourse.julialang.org/u/antoine-levitt)\
**Post date:** [April 18, 2018, 2:48pm UTC](https://discourse.julialang.org/t/linear-algebra-unit-vectors-e-i/10409/14 "2018-04-18T14:48:21Z")

</div>

Another possible use case for a similar lazy array: the totally antisymmetric tensor (Levi-Civita symbol), with bonus points for implementing fast contractions with it.

---

<div class="post-metadata">

**Author:** ![Elrod](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/elrod/32/22461_2.png) [@Elrod](https://discourse.julialang.org/u/Elrod)\
**Post date:** [April 18, 2018, 3:20pm UTC](https://discourse.julialang.org/t/linear-algebra-unit-vectors-e-i/10409/15 "2018-04-18T15:20:54Z")

</div>

```julia
struct lazy_e{T} <: AbstractVector{T}
    i::Int
    n::Int
end
lazy_e(i::Int, n::Int, ::Type{T} = Int) where T = lazy_e{T}(i, n)
function Base.getindex(e::lazy_e{T}, i) where T
    @boundscheck @assert i <= e.n
    ifelse(i == e.i, one(T), zero(T))
end
Base.size(e::lazy_e) = (e.n,)

```

Then on Julia 0.7:

```julia
julia> fe(x) = (@inbounds d = x' * lazy_e(3,4); d)
fe (generic function with 1 method)

julia> using StaticArrays, BenchmarkTools

julia> x = @SVector randn(4)
4-element SArray{Tuple{4},Float64,1,4}:
 -0.02571662495110571 
 -0.2620502046882458  
 -0.8456889511406026  
  0.017055806672449558

julia> y = randn(4)
4-element Array{Float64,1}:
 -1.858893446204762 
  1.883085443252701 
 -0.7957485906904918
 -0.5190800039346426

julia> @btime fe($x)
  1.520 ns (0 allocations: 0 bytes)
-0.8456889511406026

julia> @btime fe($y)
  8.301 ns (0 allocations: 0 bytes)
-0.7957485906904918

julia> lazy_e(2,5)
5-element lazy_e{Int64}:
 0
 1
 0
 0
 0

julia> lazy_e(2,5)'
1×5 LinearAlgebra.Adjoint{Int64,lazy_e{Int64}}:
 0 1 0 0 0

```

EDIT:  
Ideally, you’d do this with a for loop and `@eval`, and perhaps some other helper macros, but:

```julia
Base.:*(A::Matrix, e::lazy_e) = A[:, e.i]

Base.:*(A::Adjoint{T,Matrix{T}}, e::lazy_e) where T = A[:, e.i]
Base.:*(A::Transpose{T,Matrix{T}}, e::lazy_e) where T = A[:, e.i]
Base.:*(A::Adjoint{T,Vector{T}}, e::lazy_e) where T = A[e.i]
Base.:*(A::Transpose{T,Vector{T}}, e::lazy_e) where T = A[e.i]

Base.:*(A::Adjoint{T,lazy_e}, e::Matrix{T}) where T = A[e.i, :]
Base.:*(A::Transpose{T,lazy_e}, e::Matrix{T}) where T = A[e.i, :]
Base.:*(A::Adjoint{T,lazy_e}, e::Vector{T}) where T = A[e.i]
Base.:*(A::Transpose{T,lazy_e}, e::Vector{T}) where T = A[e.i]

```

I had to get really specific for dispatch to actually favor the newly defined multiplication methods, and I’m not sure why. This yielded:

```julia
julia> z = randn(3,4)
3×4 Array{Float64,2}:
  0.478915 -0.542889 0.142261 -0.594415
  0.509348 0.63491 1.77353 -0.545189
 -0.659932 0.995295 1.706 -0.291947

julia> @btime $z * lazy_e(2,4)
  26.288 ns (1 allocation: 112 bytes)
3-element Array{Float64,1}:
 -0.5428894820340465
  0.6349099814045577
  0.9952951252882498

julia> @btime $z[:,2]
  26.266 ns (1 allocation: 112 bytes)
3-element Array{Float64,1}:
 -0.5428894820340465
  0.6349099814045577
  0.9952951252882498

```

The matrix multiplication functions could be edited to return views if you.

---

<div class="post-metadata">

**Author:** ![foobar\_lv2](https://avatars.discourse-cdn.com/v4/letter/f/ee59a6/32.png) [@foobar\_lv2](https://discourse.julialang.org/u/foobar_lv2)\
**Post date:** [April 18, 2018, 7:43pm UTC](https://discourse.julialang.org/t/linear-algebra-unit-vectors-e-i/10409/16 "2018-04-18T19:43:44Z")

</div>

@Elrod  
I guess you rather want

```julia
struct lazy_e{T} <: AbstractVector{T}
    i::Int
    n::Int
    val::T 
end

```

at least for `isbits(T)`, in order to quickly (without looping, independent of `n`) scale a unit vector and in-place add it to some other vector (specialize `broadcast!`).

---

<div class="post-metadata">

**Author:** ![rocco\_sprmnt21](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rocco_sprmnt21/32/20127_2.png) [@rocco\_sprmnt21](https://discourse.julialang.org/u/rocco_sprmnt21)\
**Post date:** [May 27, 2023, 10:45am UTC](https://discourse.julialang.org/t/linear-algebra-unit-vectors-e-i/10409/17 "2023-05-27T10:45:34Z")

</div>

```julia

unit(n)=eachcol(I(n))

unit(n,i)= unit(n)[i]

```
