I see. Why are you not making a new type to allow getindex overloading? That would be more work but maybe cleaner?
On Wed, 2015-09-30 at 11:10, Kristoffer Carlsson <kcarlsso...@gmail.com> wrote: > I am not sure that I want Ragged Arrays, In my code I have symmetric 3x3 > matrices stored as vectors of length 6 and unsymmetric 3x3 matrices stored > as vectors of length 9. > > As I know you do some FEM Mauro I can describe my problem more > specifically. I am writing a package do more easily deal with Voigt tensors > and I want to add the option of accessing them just like they where > matrices. Since different people use different order of their indices I > want the user to be able to set what order the offdiagonal indices will be > stored in the Voigt vector. I have a global state in my module that maps > [i,j] -> k where k is the index in the vector. > > I will try your macro now! Thanks a lot for the help. > > > > > On Wednesday, September 30, 2015 at 10:57:14 AM UTC+2, Mauro wrote: >> >> > Hello everyone, >> > >> > I am trying to write a macro that transforms an expression like: >> > >> > @foo v[1,2] + v[1,3]*v[2,4] -> v[I[1,2]] + v[I[1,3]]*v[I[2,4]] >> > >> > Basically, for each getindex I want to insert a lookup of the index in >> some >> > other variable. >> > The reason for this is that I am working with matrices encoded as >> vectors >> > and I want to still call them like matrices. The "I" vector in the code >> > is the lookup from two dimensional indexing to one dimensional. >> > >> > v is just a normal Vector so I cannot extend getindex which is why I >> > thought a macro would be the cleanest solution. >> > >> > I am a bit lost how to start so any help would be appreciated. >> >> Sounds like you may want to use >> https://github.com/mbauman/RaggedArrays.jl (or my more primitive >> Ragged.jl) >> >> If not, this may work: >> >> function sub!(e::Expr) >> if e.head==:ref >> for a in e.args >> sub!(a) >> end >> newe = :($(e.args[1])[I[$(e.args[2:end]...)]]) >> e.args = newe.args >> else >> for a in e.args >> sub!(a) >> end >> end >> e >> end >> sub!(s) = s >> >> e = :(v[1,2] + v[1,3]*v[2,4]) >> sub!(e) >> macro foo(e) >> sub!(e) >> end >>