# Seeking an "isvector" function

**URL:** <https://discourse.julialang.org/t/seeking-an-isvector-function/80173>\
**Category:** New to Julia\
**Created:** [April 27, 2022, 10:40pm UTC](https://discourse.julialang.org/t/seeking-an-isvector-function/80173 "2022-04-27T22:40:04Z")\
**Posts on this page:** 20\
**Page:** 2

<div class="post-metadata">

**Author:** ![albheim](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/albheim/32/34660_2.png) [@albheim](https://discourse.julialang.org/u/albheim)\
**Post date:** [July 1, 2022, 9:42am UTC](https://discourse.julialang.org/t/seeking-an-isvector-function/80173/21 "2022-07-01T09:42:40Z")

</div>

What do you want it to do?

In you example you had the same body for all three functions, so if that is how you will use it you could just create a union of the types you want to allow as

```julia
isvector(x::Union{Array{<:Number}, Number}) = count(>(1), size(x)) ≤ 1

```

or something like that which should catch all the cases.

If you want to handle the three cases differently you just create three methods that will then be used for the different types

```julia
isvector(x::Vector{<:Number}) = true
isvector(x::Array{<:Number}) = count(>(1), size(x)) ≤ 1
isvector(x::Number) = false

```

---

<div class="post-metadata">

**Author:** ![ellocco](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ellocco/32/31331_2.png) [@ellocco](https://discourse.julialang.org/u/ellocco)\
**Post date:** [July 1, 2022, 10:45am UTC](https://discourse.julialang.org/t/seeking-an-isvector-function/80173/22 "2022-07-01T10:45:11Z")

</div>

Maybe I do not understand your proposal to define a `Union` here is my code that work currently only for vectors:

```julia
function MyLib_isvector(_val::Union{Vector{<:Number}, Array{<:Number}, Number})
    return count(>(1), size(_val)) ≤ 1
end  

mat2D = [1 2 3; 4 5 6]
mat1D = [1 2 3]
vec_ = [1,2,3]

isvector = MyLib_isvector(mat2D)
isvector = MyLib_isvector(mat1D) 
isvector = MyLib_isvector(vec_) 

```

---

<div class="post-metadata">

**Author:** ![albheim](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/albheim/32/34660_2.png) [@albheim](https://discourse.julialang.org/u/albheim)\
**Post date:** [July 1, 2022, 11:01am UTC](https://discourse.julialang.org/t/seeking-an-isvector-function/80173/23 "2022-07-01T11:01:08Z")

</div>

It might very well be that I just don’t understand what you want.

When I run the code you have I get `false, true, true` and I thought that was what you wanted? As long as there is at most one dimension that is larger than 1, it is a vector? Running it on a number I also get `true` which could be what you want according to that definition?

What is it that only work for vectors for you? What is the expected output?

---

<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:** [July 1, 2022, 11:06am UTC](https://discourse.julialang.org/t/seeking-an-isvector-function/80173/24 "2022-07-01T11:06:59Z")

</div>

> [@ellocco](#):
>
> How can I define `MyLib_isvector()` in a way that it except three input types:

The advice in the above discussion is that one doesn’t need, and shouldn’t use, a function that detects if something is similar to a vector. Instead one should use broadcasting.

Maybe your use case is different, so if you can say something more about it, perhaps you can get better advice. What do you need this for?

---

<div class="post-metadata">

**Author:** ![ellocco](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ellocco/32/31331_2.png) [@ellocco](https://discourse.julialang.org/u/ellocco)\
**Post date:** [July 1, 2022, 1:42pm UTC](https://discourse.julialang.org/t/seeking-an-isvector-function/80173/25 "2022-07-01T13:42:34Z")

</div>

> [@albheim](#):
>
> When I run the code you have I get `false, true, true` and I thought that was what you wanted?

I do not know exactly why I had an error message.  
After a restart it works fine 🙂  
Thanks a lot!

---

<div class="post-metadata">

**Author:** ![ellocco](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ellocco/32/31331_2.png) [@ellocco](https://discourse.julialang.org/u/ellocco)\
**Post date:** [July 1, 2022, 2:00pm UTC](https://discourse.julialang.org/t/seeking-an-isvector-function/80173/26 "2022-07-01T14:00:43Z")

</div>

I do not understand why a function might be a bad idea and how the alternative via broadcast looks like exactly. During testing I saw that the function `MyLib_isvector` in the initial version could not distinguish between a scalar and a vector, therefore, I added the proposal from @fredrikekre to use `isa(x, AbstractVector)`, here is the result:

```julia
function MyLib_isvector(_val::Union{Vector{<:Number}, Array{<:Number}, Number})
    if count(>(1), size(_val)) ≤ 1
        if isa(_val, AbstractVector)
            _isvector = true
        else
            _isvector = false
        end
    else
        _isvector = false
    end
    return _isvector
end

```

---

<div class="post-metadata">

**Author:** ![lmiq](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lmiq/32/18314_2.png) [@lmiq](https://discourse.julialang.org/u/lmiq)\
**Post date:** [July 1, 2022, 2:51pm UTC](https://discourse.julialang.org/t/seeking-an-isvector-function/80173/27 "2022-07-01T14:51:58Z")

</div>

Don’t you want simply something like this?

```julia
julia> isvector(val) = false
isvector (generic function with 1 method)

julia> isvector(val::AbstractVector) = true
isvector (generic function with 2 methods)

julia> isvector(1)
false

julia> isvector([1,2,3])
true

julia> isvector(zeros(3,3))
false

```

---

<div class="post-metadata">

**Author:** ![ellocco](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ellocco/32/31331_2.png) [@ellocco](https://discourse.julialang.org/u/ellocco)\
**Post date:** [July 1, 2022, 3:09pm UTC](https://discourse.julialang.org/t/seeking-an-isvector-function/80173/28 "2022-07-01T15:09:06Z")

</div>

> [@lmiq](#):
>
> ```julia
> julia> isvector(val) = false
> isvector (generic function with 1 method)
> 
> julia> isvector(val::AbstractVector) = true
> isvector (generic function with 2 methods)
> 
> ```

That is really cool!!!  
And not easy to understand for me.  
The first method defines the standard / default value.  
And the second is going to be taken, if the input parameter format belongs to the group of `AbstractVector`. Is this a correct summary?  
Currently, the function `isvector()`is not defined, but it could be, that in one package  
or in an upcoming version of Julia this function exist.  
I wonder, if there is a way to restrict the definition only to these situations, in which the command does not already exist?

---

<div class="post-metadata">

**Author:** ![lmiq](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lmiq/32/18314_2.png) [@lmiq](https://discourse.julialang.org/u/lmiq)\
**Post date:** [July 1, 2022, 3:14pm UTC](https://discourse.julialang.org/t/seeking-an-isvector-function/80173/29 "2022-07-01T15:14:01Z")

</div>

The most specific method is called for the input given. It is the basis of how multiple dispatch works in Julia:

[https://docs.julialang.org/en/v1/manual/methods/](https://docs.julialang.org/en/v1/manual/methods/)

---

<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:** [July 1, 2022, 3:29pm UTC](https://discourse.julialang.org/t/seeking-an-isvector-function/80173/30 "2022-07-01T15:29:02Z")

</div>

> [@ellocco](#):
>
> I do not understand why a function might be a bad idea and how the alternative via broadcast looks like exactly.

By bad idea, I mean that it’s almost certainly not the right way to solve most problems in Julia. In Matlab such a function is useful, and does exist, I think, but in Julia it does not, and will probably never exist in the Base language.

I cannot imagine any problem where such a function would be useful in Julia, because you would either use multiple dispatch or broadcasting to solve it. If you look further up in this thread there is an example of how to solve a problem with broadcasting.

I suspect this is an example of an [XY-problem](https://xyproblem.info/) If you explain what you want to achieve _beyond_ the vector detection, we could probably help you find a better way.

---

<div class="post-metadata">

**Author:** ![ellocco](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ellocco/32/31331_2.png) [@ellocco](https://discourse.julialang.org/u/ellocco)\
**Post date:** [July 1, 2022, 3:46pm UTC](https://discourse.julialang.org/t/seeking-an-isvector-function/80173/31 "2022-07-01T15:46:52Z")

</div>

In may case the `fft()` failed the error message was:

```julia
ERROR: FFTW could not create plan

```

I figured out the input vector was constructed via a multiplication:

```julia
Yk = fft(X_ .* Y_)

```

In some situations `X_` became a matrix(n, 1)  
This resulted sometimes (sic!) in the error above, the same occurred in other situations:  
Vector is fine matrix(n, 1) fails, in such cases a function “isvector()” is quite handy.

---

<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:** [July 1, 2022, 4:39pm UTC](https://discourse.julialang.org/t/seeking-an-isvector-function/80173/32 "2022-07-01T16:39:59Z")

</div>

You can perhaps restrict `X_` to be a vector at an earlier point. The problem probably lies somewhere else, the real question is how does `X_` sometimes end up as a matrix?

But if you know that `X_` is always a vector or an Nx1 matrix, you can simply do

```julia
fft(vec(X_) .* Y_) 

```

or `fft(vec(X_ .* Y_) )`. The `vec` operation is nearly zero cost.

---

<div class="post-metadata">

**Author:** ![ellocco](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ellocco/32/31331_2.png) [@ellocco](https://discourse.julialang.org/u/ellocco)\
**Post date:** [July 1, 2022, 4:52pm UTC](https://discourse.julialang.org/t/seeking-an-isvector-function/80173/33 "2022-07-01T16:52:28Z")

</div>

the command `vec()` is in my top-20 list of commands .-)  
This is the way I dial with such situations.  
The reason is in this example a third party module.

---

<div class="post-metadata">

**Author:** ![ellocco](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ellocco/32/31331_2.png) [@ellocco](https://discourse.julialang.org/u/ellocco)\
**Post date:** [July 14, 2022, 12:01pm UTC](https://discourse.julialang.org/t/seeking-an-isvector-function/80173/34 "2022-07-14T12:01:30Z")

</div>

For the purpose of stem-plots by means of the package “PlotlyJS”  
I need to ensure that the input is of type vector, this is true also if there is only one `stem` (one tuple x,y) I would like to plot.  
Now I thought of a function that ensures that x,y-Values are each of the type `Vector{}` for this purpose I wrote a [!-function](https://docs.julialang.org/en/v1/manual/style-guide/#bang-convention) that should do the job, but I have not figured out what the trick is with these functions that can modify their arguments. Below my code:

```julia
# --- "isvector()" one function with two methods
isvector(val) = false # definition of 1st method of func isvector()
isvector(val::AbstractVector) = true # definition of 2nd method of func isvector() 
# --- convertion function, be sure a one element variable is a one element vector:
function _transform_scalar_to_one_element_vec! end
function _transform_scalar_to_one_element_vec!(_oneNumber::Union{Vector{<:Number}, Array{<:Number}, Number})
    _isvector(val) = false # definition of 1st method of func isvector()
    _isvector(val::AbstractVector) = true # definition of 2nd method of func isvector() 
    # --- check that variable consists only of one number:
    only(_oneNumber)
    if ~_isvector(_oneNumber) 
        _oneNumber = [_oneNumber]
        println("_transform_scalar_to_one_element_vec!: converted to vector")
    else
        println("_transform_scalar_to_one_element_vec!: is already vector")
    end
    return _oneNumber
end
a = 1.1
isvector(a)
b = _transform_scalar_to_one_element_vec!(a)
isvector(a)
typeof(a)
isvector(b)

```

---

<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:** [July 14, 2022, 3:08pm UTC](https://discourse.julialang.org/t/seeking-an-isvector-function/80173/35 "2022-07-14T15:08:22Z")

</div>

This seems very awkward. But if you must:

```julia
ensure_singular_vec(x) = [only(x)]

```

---

<div class="post-metadata">

**Author:** ![PeterSimon](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/petersimon/32/25193_2.png) [@PeterSimon](https://discourse.julialang.org/u/PeterSimon)\
**Post date:** [July 14, 2022, 3:42pm UTC](https://discourse.julialang.org/t/seeking-an-isvector-function/80173/36 "2022-07-14T15:42:43Z")

</div>

Agreed, quite awkward. How about something like this instead?

```julia
function my_stem_plot(x::AbstractVector, y::AbstractVector)
  # Do the plotting here
end

my_stem_plot(x::Number, y::Number) = my_stem_plot([x], [y])
my_stem_plot(x::Number, y) = my_stem_plot([x], y)
my_stem_plot(x, y::Number) = my_stem_plot(x, [y])
my_stem_plot(x::Tuple) = my_stem_plot(x...)

```

Edit: Added a method to disambiguate the case where both arguments are scalars.

---

<div class="post-metadata">

**Author:** ![pdeffebach](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pdeffebach/32/10320_2.png) [@pdeffebach](https://discourse.julialang.org/u/pdeffebach)\
**Post date:** [July 14, 2022, 4:29pm UTC](https://discourse.julialang.org/t/seeking-an-isvector-function/80173/37 "2022-07-14T16:29:24Z")

</div>

As a side note, this is not a proper use of `!`, since you aren’t mutating the input. Also, `!` is meaningless with number inputs, since they are immutable, yet you can input that into your function because of your wide type signature.

---

<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:** [July 14, 2022, 6:13pm UTC](https://discourse.julialang.org/t/seeking-an-isvector-function/80173/38 "2022-07-14T18:13:56Z")

</div>

> [@ellocco](#):
>
> `Union{Vector{<:Number}, Array{<:Number}, Number}`

BTW, this is the same as `Union{Array{<:Number}, Number}`.

---

<div class="post-metadata">

**Author:** ![ellocco](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ellocco/32/31331_2.png) [@ellocco](https://discourse.julialang.org/u/ellocco)\
**Post date:** [July 15, 2022, 7:31am UTC](https://discourse.julialang.org/t/seeking-an-isvector-function/80173/39 "2022-07-15T07:31:24Z")

</div>

> [@DNF](#):
>
> This seems very awkward. But if you must:
> 
> ```julia
> ensure_singular_vec(x) = [only(x)]
> 
> ```

I do not have a clue, why your function is `awkward`; this might be related to my limited English skills. Your code works fine for my! Thanks 🙂

---

<div class="post-metadata">

**Author:** ![ellocco](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ellocco/32/31331_2.png) [@ellocco](https://discourse.julialang.org/u/ellocco)\
**Post date:** [July 15, 2022, 7:37am UTC](https://discourse.julialang.org/t/seeking-an-isvector-function/80173/40 "2022-07-15T07:37:05Z")

</div>

> [@DNF](#):
>
> BTW, this is the same as `Union{Array{<:Number}, Number}`.

Thanks a lot! 🙂

[Previous page](https://discourse.julialang.org/t/seeking-an-isvector-function/80173.md?page=1)

[Next page](https://discourse.julialang.org/t/seeking-an-isvector-function/80173.md?page=3)
