 Using varargs

I have the following code:

circ(x) = x./sqrt(sum(x .* x))

x -> cat(circ(x), circ(x); dims = 1)

but I want to be able to create a function where I input a number and it concatenates that number of circ(x)s.

so for example:

function Ncircs(n)
#some way to make cat() have as its parameter circ n number of times
end

and I could call Ncircs(2) and get
x -> cat(circ(x), circ(x); dims = 1)
or Ncircs(3) and get
x -> cat(circ(x), circ(x), circ(x); dims = 1)
or Ncircs(4) and get
x -> cat(circ(x), circ(x), circ(x), circ(x); dims = 1)

etc.

Is there a way of doing this? Do I have to use a macro?

You can just return x -> repeat(circ(x), n). Any reason you specifically specify dims? Is x not a regular Vector?

1 Like

Also note the availability of the LinearAlgebra.normalize function, which does the same thing as circ.

1 Like

When I try using repeat, I get the following error

LoadError: MethodError: no method matching Float32(::Tracker.TrackedReal{Float32})

but if I do

reduce(vcat, Iterators.repeated(circ(x), n))

it works fine, the only issue is that reduce does not work if I want to sometimes be able to pass in zero for n

How could I address this?

Could you provide a minimal working example of what exactly you are trying to do, otherwise people can’t really help you?

1 Like

I was able to address it by simply using a special case if clause for n being zero where it just did nothing as I wanted it to.