# 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:** 9\
**Page:** 3

<div class="post-metadata">

**Author:** ![uje](https://avatars.discourse-cdn.com/v4/letter/u/5f9b8f/32.png) [@uje](https://discourse.julialang.org/u/uje)\
**Post date:** [July 15, 2022, 9:09am UTC](https://discourse.julialang.org/t/seeking-an-isvector-function/80173/41 "2022-07-15T09:09:28Z")

</div>

These 3 liners solves your issue in an **easy** way.

```julia
function isvector(x::T) where T <: AbstractArray
    isone(length(size(x))) && return true
    (!isone(length(size(x))) && isone(size(x)[1])) && return true
    (!isone(length(size(x))) && isone(size(x)[2])) && return true
end

```

```julia
isvector([1 2 3 4])

```

true

```julia
isvector([1, 2, 3, 4])

```

true

```julia
isvector([1 2 3 4]')

```

true

```julia
isvector([1, 2, 3, 4]')

```

true

```julia
isvector([1 2; 3 4])

```

false

---

<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 15, 2022, 4:11pm UTC](https://discourse.julialang.org/t/seeking-an-isvector-function/80173/42 "2022-07-15T16:11:53Z")

</div>

> [@ellocco](#):
>
> I do not have a clue, why your function is `awkward`

I believe that the comment was in regard to your function `_transform_scalar_to_one_element_vec!`. In my previous post I showed why such a function is not necessary, if you let Julia’s type system and dispatch do the work for you.

---

<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, 4:18pm UTC](https://discourse.julialang.org/t/seeking-an-isvector-function/80173/43 "2022-07-15T16:18:57Z")

</div>

The problem with some packages is, they do not complain, if the input parameters do not satisfy the necessary format. In case of `PlotlyJS` there is just no stem-line in your plot, if the input is a scalar instead of a one-element vector, no error-massage, but also no stem-line ☹  
Therefore, it make sense, to make the situation more robust, and act in case your input to a third party package is scalar when a one-element vector is expected.

---

<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 15, 2022, 4:34pm UTC](https://discourse.julialang.org/t/seeking-an-isvector-function/80173/44 "2022-07-15T16:34:39Z")

</div>

Please take a look at the methods in my previous post. They guarantee that if one calls the `my_stem_plot` function with a scalar for either argument, it gets converted to a one-element vector, before calling the method of `my_stem_plot` that accepts only vectors.

This is a cleaner, more Julian way to approach the problem you want to solve.

---

<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 18, 2022, 8:53pm UTC](https://discourse.julialang.org/t/seeking-an-isvector-function/80173/45 "2022-07-18T20:53:20Z")

</div>

Hi Peter,  
thanks for your snippets!  
I do not know if you are familiar with plotting by means of `PlotlyJS`.  
I usually use my own functions, to configure my plots.  
The notation for a single stem line is as follows:

```julia
stem(; x = [1], y = [2], name = "single stemline")

```

A whole plot function might look like this, how would you integrate your function into this  
plot fuction?

```julia
using PlotlyJS

function _plot_single_spectra(num_::Int)
	_amplitudes_Shunt = rand(num_)
	_frequencies_Shunt = collect(range(1, num_))
	_amplitudes_Hall = rand(num_)
	_frequencies_Hall = collect(range(1, num_))

    line_amplitudes_shunt = scatter(; x =_frequencies_Shunt, y = _amplitudes_Shunt, name = "Shunt")
    line_amplitudes_hall = scatter(; x =_frequencies_Hall, y = _amplitudes_Hall, name = "Hall", yaxis="y2")
    stem_frequ_Shunt_assumed = stem(; x = [0.4*num_], y = [1], name = "f_Shunt(assumed)")
    stem_frequ_Hall_assumed = stem(; x = [0.6*num_], y = [1], name = "f_Hall(assumed)", yaxis="y2")
    data = [line_amplitudes_shunt, line_amplitudes_hall, stem_frequ_Shunt_assumed, stem_frequ_Hall_assumed]
    mylayout = Layout(
        title_text = "FFT spectrum",
        xaxis_title_text = "Frequency / Hz",
        yaxis_title_text = "Amplitude Shunt",
		# yaxis_type = "log",
        yaxis2 = PlotlyJS.attr(
            title = "Amplitude Hall",
			# type = "log",
            overlaying = "y",
            side = "right"
            )
        )
    # ---
    return Plot(data, mylayout)
end

# ---
fn_plot = "c:/tmp/plt/test.pdf"
num_ = round(Int, 0.01 * 24999876)

# ---
hdl_plt = _plot_single_spectra(num_)

```

---

<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 18, 2022, 9:26pm UTC](https://discourse.julialang.org/t/seeking-an-isvector-function/80173/46 "2022-07-18T21:26:16Z")

</div>

I was not familiar with `PlotlyJS` and I agree the situation is a bit messier than I had assumed. But if you’re worried about accidentally using scalars for `x` and `y` then I guess I would use the following definitions:

```julia
mystem(x::AbstractVector, y::AbstractVector; kwargs...) = stem(; x, y, kwargs...)
mystem(x::Number, y::Number; kwargs...) = mystem([x], [y]; kwargs...)

```

and then in your function `_plot_single_spectra` I would replace the two calls to `stem` with the folllowing calls:

```julia
stem_frequ_Shunt_assumed = mystem(0.4*num_, 1; name = "f_Shunt(assumed)")
stem_frequ_Hall_assumed = mystem(0.6*num_, 1; name = "f_Hall(assumed)", yaxis="y2")

```

or you could retain the square brackets around the x and y values, if desired.

edit: Note that i corrected a couple of typos

---

<div class="post-metadata">

**Author:** ![stephancb](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stephancb/32/14243_2.png) [@stephancb](https://discourse.julialang.org/u/stephancb)\
**Post date:** [July 19, 2022, 8:55am UTC](https://discourse.julialang.org/t/seeking-an-isvector-function/80173/47 "2022-07-19T08:55:50Z")

</div>

Can’t you just

```julia
function plotstem(x, y)
    plot(stem(; x=isempty(size(x)) ? [x] : x, y=isempty(size(y)) ? [y] : y))
end

```

?

---

<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 19, 2022, 10:00pm UTC](https://discourse.julialang.org/t/seeking-an-isvector-function/80173/48 "2022-07-19T22:00:19Z")

</div>

Here is an alternative that retains exactly the same syntax as your existing `stem` calls:

```julia
mystem2(; x, y, kwargs...) = stem(; x = n2v(x), y = n2v(y), kwargs...)
n2v(x::Number) = [x]
n2v(x::AbstractVector) = length(x) == 1 ? x : throw(ArgumentError("length must be 1"))

```

With these, you can just substitute `mystem2` for `stem` in your function `_plot_single_spectra`. It will work equally well if you call mystem2 with `x` and/or `y` being either a number or a 1-element vector. Otherwise you will get an error.

---

<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 20, 2022, 1:24pm UTC](https://discourse.julialang.org/t/seeking-an-isvector-function/80173/49 "2022-07-20T13:24:01Z")

</div>

Stefan, after studying `PlotlyJS` a bit and iterating a bit more, I think I have a solution you will like. The only change necessary in your code is to replace `using PlotlyJS` by the following:

```julia

function stem(; x=nothing, y, kwargs...)
    if isnothing(x)
        return PlotlyBase.stem(; y = n2v(y), kwargs...)
    else
        return PlotlyBase.stem(; x = n2v(x), y = n2v(y), kwargs...)
    end
end
n2v(x::Number) = [x]
n2v(x::AbstractVector) = isone(length(x)) ? x : throw(ArgumentError("length must be 1"))

using PlotlyJS

```

It is important that the definition of `stem` above precede `using PlotlyJS`. Now you can call `stem` exactly as before, except that the `x` and `y` keyword arguments can be either numbers or single-element vectors. For example, suppose you forget to surround the `y` argument with braces:

```julia
julia> num_ = round(Int, 0.01 * 24999876)
249999

julia> stem_frequ_Hall_assumed = stem(; x = [0.6*num_], y = 1, name = "f_Hall(assumed)", yaxis="y2")
scatter with fields error_y, hoverinfo, marker, mode, name, text, type, x, y, and yaxis

```

If we examine the fields we see that `y` is a 1-element vector as desired:

```julia
julia> stem_frequ_Hall_assumed.fields
Dict{Symbol, Any} with 10 entries:
  :x => [1.49999e5]
  :mode => "markers"
  :y => [1]
  :type => "scatter"
  :name => "f_Hall(assumed)"
  :hoverinfo => "text"
  :error_y => Dict{Symbol, Any}(:color=>"grey", :width=>0, :symmetric=>false, :type=>"data", :visible=>true, :array=>[0], :thickness=>1, :arrayminus=>[1])
  :text => [1]
  :yaxis => "y2"
  :marker => Dict{Any, Any}(:size=>10)

```

It took me a while to get to this solution–for me that is the nature of developing software. It’s a process of iterative refinement. Usually, the refinements are completed out of public view (like making sausage 😉).

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