# Scalar and Arrays: indexing the same way?

**URL:** https://discourse.julialang.org/t/scalar-and-arrays-indexing-the-same-way/111409
**Category:** General Usage
**Tags:** question, arrays
**Created:** [March 10, 2024, 1:46am UTC](https://discourse.julialang.org/t/scalar-and-arrays-indexing-the-same-way/111409 "2024-03-10T01:46:57Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![hsgg](https://avatars.discourse-cdn.com/v4/letter/h/838e76/32.png) [@hsgg](https://discourse.julialang.org/u/hsgg)
#### Post date: [March 10, 2024, 1:46am UTC](https://discourse.julialang.org/t/scalar-and-arrays-indexing-the-same-way/111409/1 "2024-03-10T01:46:57Z")

</div>

I have a situation where an array of objects, say galaxies, can have either the same value for a property, or each has a different value. I would like to use the same function in both cases. For example, if the property is `weights`,

```julia
function do_something(galaxies::Array; weights=1)

    output_array = zeros(size(galaxies))

    for i=1:length(galaxies)
        w = weights[i] # doesn't work if weights is a scalar
        # ... more complicated stuff here
        output_array[i] += w
    end

    return output_array
end

galaxies = rand(5)
do_something(galaxies, weights=1)
do_something(galaxies, weights=rand(length(galaxies)))

```

So every galaxy by default has the same weight `1`, but it will also be common to give an array of weights, one for each galaxy.

What is a good way to make `weights` work with both a scalar and an array?

As a potential solution, I was thinking of creating a special `ArrayScalar` struct that returns a constant no matter what indices are given to it. But it seems like overkill.

Another option would be to turn the scalar into `weights = ones(length(weights))`. But that seems wasteful.

Any other ways of dealing with this?

---

<div class="post-metadata">

### Author: ![mkitti](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mkitti/32/12459_2.png) [@mkitti](https://discourse.julialang.org/u/mkitti)
#### Post date: [March 10, 2024, 2:13am UTC](https://discourse.julialang.org/t/scalar-and-arrays-indexing-the-same-way/111409/2 "2024-03-10T02:13:02Z")

</div>

There is a package called FillArrays.jl:

> **[GitHub - JuliaArrays/FillArrays.jl: Julia package for lazily representing...](https://github.com/JuliaArrays/FillArrays.jl)**
>
> Julia package for lazily representing matrices filled with a single entry - JuliaArrays/FillArrays.jl

---

<div class="post-metadata">

### Author: ![hsgg](https://avatars.discourse-cdn.com/v4/letter/h/838e76/32.png) [@hsgg](https://discourse.julialang.org/u/hsgg)
#### Post date: [March 10, 2024, 2:45am UTC](https://discourse.julialang.org/t/scalar-and-arrays-indexing-the-same-way/111409/3 "2024-03-10T02:45:29Z")

</div>

That was fast! Thanks!

---

<div class="post-metadata">

### Author: ![screw\_dog](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/screw_dog/32/48119_2.png) [@screw\_dog](https://discourse.julialang.org/u/screw_dog)
#### Post date: [March 10, 2024, 3:31am UTC](https://discourse.julialang.org/t/scalar-and-arrays-indexing-the-same-way/111409/4 "2024-03-10T03:31:03Z")

</div>

This is also a classic use of Julia’s multiple dispatch - you want a function that behaves slightly different depending on the type of a parameter.

One way would be to factor out the weight access:

```julia
get_weight(weights::Integer, index) = weights
get_weight(weights::Vector, index) = weights[index]

function do_something(galaxies::Array; weights=1)

    output_array = zeros(size(galaxies))

    for i=1:length(galaxies)
        w = get_weight(weights, i)
        # ... more complicated stuff here
        output_array[i] += w
    end

    return output_array
end

```

The nice thing about this is there is no performance cost, the compiler will produce two versions of this depending on the type of `weights` and call the appropriate one. And no data overhead.

---

<div class="post-metadata">

### Author: ![screw\_dog](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/screw_dog/32/48119_2.png) [@screw\_dog](https://discourse.julialang.org/u/screw_dog)
#### Post date: [March 10, 2024, 3:36am UTC](https://discourse.julialang.org/t/scalar-and-arrays-indexing-the-same-way/111409/5 "2024-03-10T03:36:03Z")

</div>

Actually, on reflection this is also possibly a good use of broadcasting. Julia’s broadcasting already handles the case of when one argument is either a vector or a scalar so you could rewrite as:

```julia
function do_something(galaxy, weight)
    # ... more complicated stuff here
    return calculated_value
end
# supply default value for weight
do_something(galaxy) = do_something(galaxy, 1)

galaxies = rand(5)
single_weight = do_something.(galaxies, 1)
multiple_weights = do_something.(galaxies, rand(5))

```

---

<div class="post-metadata">

### Author: ![hsgg](https://avatars.discourse-cdn.com/v4/letter/h/838e76/32.png) [@hsgg](https://discourse.julialang.org/u/hsgg)
#### Post date: [March 10, 2024, 4:36am UTC](https://discourse.julialang.org/t/scalar-and-arrays-indexing-the-same-way/111409/6 "2024-03-10T04:36:05Z")

</div>

> this is also possibly a good use of broadcasting

Unfortunately, the `more complicated stuff here` doesn’t lend itself easily to broadcasting.
