Re: [julia-users] Re: Periodic or cyclic arrays possible?
The expression `(i-1) % length + 1` is not correct for negative `i`. You have to use `mod` instead of `%`. Julia has a function `mod1(x,y)` that is essentially defined as `mod(x-1, y) +1`, so that's what you probably want to use. -erik On Sat, Feb 6, 2016 at 8:40 PM, Cedric St-Jeanwrote: > You can define our own datatype to do this. It's one of the most fundamental > tasks in Julia! > > immutable CircularArray{T} > arr::Vector{T} > end > > Base.getindex(ca::CircularArray, i) = ca.arr[(i-1) % length(ca.arr) + 1] > Base.setindex(...) = ... > ... > > a = CircularArray([1,2,3]) > a[14] # yields 2 > > Cédric > > > On Saturday, February 6, 2016 at 7:08:03 PM UTC-5, Ferran Mazzanti wrote: >> >> Hi folks, >> >> I was wondering if it is possible to use in a simple way cyclic arrays in >> Julia? What I'm after is sometbing that understands that the next element in >> a[] after end is a[1], so a[end+1]=a[1], a[end+2]=a[2] etc... I know I can >> index the array with the remainder operator % to achieve this same result, >> but I wonder if one can declare the array directly in one way or another to >> achieve this directly. >> >> Thanks in advance, >> >> Ferran. -- Erik Schnetter http://www.perimeterinstitute.ca/personal/eschnetter/
Re: [julia-users] Re: Periodic or cyclic arrays possible?
I highly recommend reading the Interfaces chapter[1] for a walk-through on creating your own array type. Another great trick that you can use is `A[mod1(x, end)]`. 1. http://docs.julialang.org/en/release-0.4/manual/interfaces/ On Sunday, February 7, 2016 at 10:52:45 AM UTC-5, Erik Schnetter wrote: > > The expression `(i-1) % length + 1` is not correct for negative `i`. > You have to use `mod` instead of `%`. > > Julia has a function `mod1(x,y)` that is essentially defined as > `mod(x-1, y) +1`, so that's what you probably want to use. > > -erik > > On Sat, Feb 6, 2016 at 8:40 PM, Cedric St-Jean> wrote: > > You can define our own datatype to do this. It's one of the most > fundamental > > tasks in Julia! > > > > immutable CircularArray{T} > > arr::Vector{T} > > end > > > > Base.getindex(ca::CircularArray, i) = ca.arr[(i-1) % length(ca.arr) + 1] > > Base.setindex(...) = ... > > ... > > > > a = CircularArray([1,2,3]) > > a[14] # yields 2 > > > > Cédric > > > > > > On Saturday, February 6, 2016 at 7:08:03 PM UTC-5, Ferran Mazzanti > wrote: > >> > >> Hi folks, > >> > >> I was wondering if it is possible to use in a simple way cyclic arrays > in > >> Julia? What I'm after is sometbing that understands that the next > element in > >> a[] after end is a[1], so a[end+1]=a[1], a[end+2]=a[2] etc... I know I > can > >> index the array with the remainder operator % to achieve this same > result, > >> but I wonder if one can declare the array directly in one way or > another to > >> achieve this directly. > >> > >> Thanks in advance, > >> > >> Ferran. > > > > -- > Erik Schnetter > http://www.perimeterinstitute.ca/personal/eschnetter/ >
[julia-users] Re: Periodic or cyclic arrays possible?
You can define our own datatype to do this. It's one of the most fundamental tasks in Julia! immutable CircularArray{T} arr::Vector{T} end Base.getindex(ca::CircularArray, i) = ca.arr[(i-1) % length(ca.arr) + 1] Base.setindex(...) = ... ... a = CircularArray([1,2,3]) a[14] # yields 2 Cédric On Saturday, February 6, 2016 at 7:08:03 PM UTC-5, Ferran Mazzanti wrote: > > Hi folks, > > I was wondering if it is possible to use in a simple way cyclic arrays in > Julia? What I'm after is sometbing that understands that the next element > in a[] after end is a[1], so a[end+1]=a[1], a[end+2]=a[2] etc... I know I > can index the array with the remainder operator % to achieve this same > result, but I wonder if one can declare the array directly in one way or > another to achieve this directly. > > Thanks in advance, > > Ferran. > >