On Wed, 2015-09-30 at 13:04, Kristoffer Carlsson <kcarlsso...@gmail.com> wrote:
> Because it would imo raise the barrier to use a lot. A new type is sort of
> viral in the sense that if you use it, it is likely that you should use the
> type everywhere you are using Voigt Tensors.
>
> I want this to be directly usable even if you have written all your FEM
> code with Vector{Float64} and just want to use this in a few places.

I can see this, kinda.  But if you go the macro-route then instead of
just changing the type in one place you now need to annotate the usages
in many places.

> # Constructs the final expression
> function vexp(e::Expr)
>        exps = initiate(e::Expr)
>     sub!(e)
>     f_exp = :()
>     for exp in exps
>         f_exp = quote
>             $f_exp
>             $exp
>         end
>     end
>     f_exp = quote
>         $f_exp
>         $e
>     end
>     return f_exp
> end

Yes, this will create nested begin-end blocks.  also note that
`:().head==:tuple`.

Try:

exps = initiate(e::Expr)
f_exp = quote end
append!(f_exp.args, exps)
push!(f_exp.args, e)

> macro v(e)
>     vexp(e)
> end
>
>
>
> On Wednesday, September 30, 2015 at 11:30:04 AM UTC+2, Mauro wrote:
>>
>> 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 <kcarl...@gmail.com
>> <javascript:>> 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
>> >>
>>

Reply via email to