# How to enforce minimum dimensions for arrays that might be scalars?

**URL:** <https://discourse.julialang.org/t/how-to-enforce-minimum-dimensions-for-arrays-that-might-be-scalars/53860>\
**Category:** General Usage\
**Tags:** question, linearalgebra\
**Created:** [January 24, 2021, 11:46am UTC](https://discourse.julialang.org/t/how-to-enforce-minimum-dimensions-for-arrays-that-might-be-scalars/53860 "2021-01-24T11:46:58Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![marty0801](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/marty0801/32/12715_2.png) [@marty0801](https://discourse.julialang.org/u/marty0801)\
**Post date:** [January 24, 2021, 11:46am UTC](https://discourse.julialang.org/t/how-to-enforce-minimum-dimensions-for-arrays-that-might-be-scalars/53860/1 "2021-01-24T11:46:58Z")

</div>

I have a function expecting a `Matrix` and a `Vector` argument:

```julia
function f(m::Matrix, v::Vector) 
...
end

```

Inside `f` performs some linear algebra. My problem: I need `f` to work when its arguments are scalars as well. How should I handle this?

If this were numpy I might wrap the arguments in numpy arrays and use the `ndim=` keyword argument to enforce the shape of each. Does Julia have a similar facility?

---

<div class="post-metadata">

**Author:** ![mcabbott](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mcabbott/32/6603_2.png) [@mcabbott](https://discourse.julialang.org/u/mcabbott)\
**Post date:** [January 24, 2021, 11:59am UTC](https://discourse.julialang.org/t/how-to-enforce-minimum-dimensions-for-arrays-that-might-be-scalars/53860/2 "2021-01-24T11:59:03Z")

</div>

If the operations `...` will work equally well for numbers, then you can define the function as e.g. `f(m::AbstractMatrix, v::Union{Number, AbstractVector})` or even just `f(m, v)`. A different version will still be compiled for each combination of concrete types.

If not, and you need a separate implementation, and it’s cleanest to make another method `f(m::Number, v::Number)` to contain this.

You can re-use the matrix implementation by writing perhaps `f(m::Number, v::Number) = f(hcat(m), [v]) |> only` i.e. creating a 1x1 matrix and a vector, calling the first method, and then (perhaps) unwrapping back to a number. But very likely you can do something more efficient working directly with scalars instead.

---

<div class="post-metadata">

**Author:** ![marty0801](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/marty0801/32/12715_2.png) [@marty0801](https://discourse.julialang.org/u/marty0801)\
**Post date:** [January 24, 2021, 4:04pm UTC](https://discourse.julialang.org/t/how-to-enforce-minimum-dimensions-for-arrays-that-might-be-scalars/53860/3 "2021-01-24T16:04:16Z")

</div>

Thanks!

---

<div class="post-metadata">

**Author:** ![rdeits](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rdeits/32/286_2.png) [@rdeits](https://discourse.julialang.org/u/rdeits)\
**Post date:** [January 24, 2021, 7:27pm UTC](https://discourse.julialang.org/t/how-to-enforce-minimum-dimensions-for-arrays-that-might-be-scalars/53860/4 "2021-01-24T19:27:58Z")

</div>

It’s very common in Matlab and NumPy to write functions which “vectorize” over one or more arguments, supporting scalar or vector operations, where the vector version just involves applying the scalar version to each element. This is rarely necessary or helpful in Julia. If that’s what you’re doing here (it’s hard to tell from your description), then I would suggest avoiding the issue completely and _only_ implementing `f(x::AbstractMatrix, v::Number)` and then using broadcasting when you _call_ your function to apply it elementwise.

For example, I’m imagining your function is something like:

```julia
julia> function f_scalar(x, v)
         v * x
       end
f_scalar (generic function with 1 method)

julia> function f_vector(x, v)
         [v_i * x for v_i in v]
       end
f_vector (generic function with 1 method)

```

where `f_vector` just applies the same operation for each element in the vector `v`.

If this is what you’re trying to achieve, then I would suggest that you not write `f_vector` at all. All you need is `f_scalar`, and you can apply it to `v` of any dimension via broadcasting with `.`

```julia
# Scalar `v`, returns a single `Matrix` result
julia> f_scalar(ones(2, 2), 1.5)
2×2 Array{Float64,2}:
 1.5 1.5
 1.5 1.5

# Vector `v`. Returns a vector of matrices, one for each element of `v`
julia> f_scalar.(Ref(ones(2, 2)), [1.5, 2.5])
2-element Array{Array{Float64,2},1}:
 [1.5 1.5; 1.5 1.5]
 [2.5 2.5; 2.5 2.5]

# Matrix `v`. Returns a matrix of matrices, one for each element of `v`:
julia> f_scalar.(Ref(ones(2, 2)), [1.5 2.5; 3.5 4.5])
2×2 Array{Array{Float64,2},2}:
 [1.5 1.5; 1.5 1.5] [2.5 2.5; 2.5 2.5]
 [3.5 3.5; 3.5 3.5] [4.5 4.5; 4.5 4.5]

```

The `Ref` means to treat the first argument as a scalar, passing it whole to each call to `f_scalar`.

This is a much more common approach in Julia, and it results in more efficient code with less work. For more info, see: [More Dots: Syntactic Loop Fusion in Julia](https://julialang.org/blog/2017/01/moredots/)
