Here is something short:
```Julia
bv(i,n) = [j==i for j in 1:n]
```
This returns a vector of `Bool`. In Julia, `Bool` is a numeric type that is
automatically promoted to `Int` / `Float` / etc. where necessary. If you
want, you can add an explicit type (e.g. `Float64`) in front of the array
comprehension.

-erik


On Fri, Sep 9, 2016 at 1:16 PM, Matt Bauman <mbau...@gmail.com> wrote:

> You could implement:
>
> Base.getindex(U::UniformScaling, I::AbstractArray, j::Int) = [U[i,j] for
> i in I]
> Base.getindex(U::UniformScaling, i::Int, J::AbstractArray) = [U[i,j] for
> j in J]
> Base.getindex(U::UniformScaling, I::AbstractArray, J::AbstractArray) = [U[
> i,j] for i in I, j in J]
>
> Then:
>
> julia> I[1:4, 2]
> 4-element Array{Int64,1}:
>  0
>  1
>  0
>  0
>
>
> On Friday, September 9, 2016 at 11:58:44 AM UTC-5, Steven G. Johnson wrote:
>>
>> On Friday, September 9, 2016 at 11:56:58 AM UTC-4, Christoph Ortner wrote:
>>>
>>> I predict that - right now - somebody is writing an answer explaining
>>> why this is terrible and you need an abstract array type with lazy
>>> evaluation. ;)
>>
>>
>> Well, we already have the built-in constant I.   I[i,j] is the j-th
>> element of the i-th unit vector, computed lazily, i.e. the Kronecker delta
>> function.
>>
>> But yes, if you need unit vectors a lot you may be doing something wrong.
>>
>


-- 
Erik Schnetter <schnet...@gmail.com>
http://www.perimeterinstitute.ca/personal/eschnetter/

Reply via email to