2
votes

I decided to try julia a few days ago and tried to translate one of my python projects to julia. I understand that using the type system is crucial for good performance. However, I have something like this in python:

class Phonon(object):
    # it has an attribute called D which looks like
    # D = {'on_site': [D00, D11, D22, D33 ...], 'lead':{'l': [Dl00, Dl01, Dl11], 'r': [Dr00, Dr01, Dr11]},'couple': [D01, D12, D23 ...], 'lead_center':{'l': Dlcl, 'r': Dlcr}}
    # all D00, D11, D22 matrices are numpy arrays

If I translate this into julia, it would be:

type Phonon:
    D::Dict{ASCIIString, Any}
end

It seems that compiler cannot get much infomation about what phonons are. So my question is: How do julia people organize their complex data?

1
how about use composite type and zip D? - Gnimuc
I tried, but D['onsite'] is still Array{Any, 1}. Then I have to define another composite type. It sounds not good. - Chong Wang
what does your numpy array look like? - Gnimuc
@GnimucKey, all are square arrays of a fixed size. - Chong Wang

1 Answers

3
votes

if i understood you properly, you may want something like this:

type PhononDict{T<:Number}
    on_site::Vector{Matrix{T}}
    lead::Dict{ASCIIString, Vector{Matrix{T}}}
    couple::Vector{Matrix{T}}
    lead_center::Dict{ASCIIString, Matrix{T}}
end

i assumed that the element type of your numpy array <: Number, you can adjust it to something like T<:Union{Int64, Float64} instead.

the key problem here is lead::Dict, so D::Dict{ASCIIString, Any}:

julia> typejoin(Array, Dict)
Any

i suggest to change D into a composite type and then you can pass more info to compiler. more information concerning parametric types.