# View a scalar as a vector/array

**URL:** https://discourse.julialang.org/t/view-a-scalar-as-a-vector-array/62535
**Category:** New to Julia
**Tags:** question, views
**Created:** [June 7, 2021, 5:45pm UTC](https://discourse.julialang.org/t/view-a-scalar-as-a-vector-array/62535 "2021-06-07T17:45:30Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![nchisholm](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nchisholm/32/37414_2.png) [@nchisholm](https://discourse.julialang.org/u/nchisholm)
#### Post date: [June 7, 2021, 5:45pm UTC](https://discourse.julialang.org/t/view-a-scalar-as-a-vector-array/62535/1 "2021-06-07T17:45:30Z")

</div>

Is there is any built-in mechanism in Julia to “view” a scalar quantity, like a number, as an array? I am thinking something like as follows, given that the shape of `b` and `c` could be broadcast against `A`.

```julia
A = ones(3, 3)
b = 1
c = ones(3)

A .+ b == A .+ c # works, since b and c can each be broadcast to the shape of A
B = broadcast_to(b, (3,3))
C = broadcast_to(c, (3,3))
A + B == a .+ B # true
A + C == a .+ C # true

```

I think `numpy` has a function `broadcast_to` like I am illustrating here. This could be useful if, e.g., you were solving a matrix equation with a constant vector on the right-hand side.

```julia
A = ones(3, 3)
b = broadcast_to(1, (3,))
x = A \ b
x == A .\ ones(3)

```

Note that, consistent with other broadcast functions, doing `A .\ b` broadcasts `b=1` to a 3x3 matrix of `1`s, so `A .\ b == A \ ones(3,3)`.

---

<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: [June 7, 2021, 5:53pm UTC](https://discourse.julialang.org/t/view-a-scalar-as-a-vector-array/62535/2 "2021-06-07T17:53:26Z")

</div>

Sounds like you want [https://github.com/JuliaArrays/FillArrays.jl](https://github.com/JuliaArrays/FillArrays.jl)

---

<div class="post-metadata">

### Author: ![DNF](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dnf/32/10191_2.png) [@DNF](https://discourse.julialang.org/u/DNF)
#### Post date: [June 7, 2021, 7:03pm UTC](https://discourse.julialang.org/t/view-a-scalar-as-a-vector-array/62535/3 "2021-06-07T19:03:31Z")

</div>

> [@nchisholm](#):
>
> Note that, consistent with other broadcast functions, doing `A .\ b` broadcasts `b=1` to a 3x3 matrix of `1` s, so `A .\ b == A \ ones(3,3)` .

What? Unless I misunderstand, this is wrong:

```julia
jl> A = rand(3,3);

jl> A .\ ones(3, 3)
3×3 Matrix{Float64}:
 4.47187 475.907 4.43528
 1.494 2.97416 6.40232
 2.93817 1.11483 3.30267

jl> A \ ones(3, 3)
3×3 Matrix{Float64}:
  0.872241 0.872241 0.872241
 -0.422581 -0.422581 -0.422581
  3.57412 3.57412 3.57412

```

It doesn’t just broadcast the shapes, but also does _elementwise_ operations.

---

<div class="post-metadata">

### Author: ![nchisholm](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nchisholm/32/37414_2.png) [@nchisholm](https://discourse.julialang.org/u/nchisholm)
#### Post date: [June 7, 2021, 9:08pm UTC](https://discourse.julialang.org/t/view-a-scalar-as-a-vector-array/62535/4 "2021-06-07T21:08:13Z")

</div>

> [@DNF](#):
>
> It doesn’t just broadcast the shapes, but also does _elementwise_ operations.

Yes you are right and thanks for pointing that out – beginner mistake for me thinking about broadcasting backwards.

```julia
julia> A = rand(3, 3)
3×3 Matrix{Float64}:
 0.96678 0.876051 0.849013
 0.966259 0.257086 0.658442
 0.818418 0.825752 0.210322

julia> A .\ ones(3,3) == A .\ ones(3) == A .\ 1 # All elementwise inverse of each element of A
true

A \ ones(3,3) == A .\ 1
false

```

> [@stevengj](#):
>
> Sounds like you want [GitHub - JuliaArrays/FillArrays.jl: Julia package for lazily representing matrices filled with a single entry](https://github.com/JuliaArrays/FillArrays.jl)

Indeed. Thanks for your reply.
