0

I am very new to Julia language, so I started to read the documentation and all built-in functions. Now, I am learning one github project for my work. Since I am more comfortable with Python, I tried to translate Julia's code to python by my understanding, but I got a few weird syntaxes that I didn't understand and I got stuck with them. Can anyone point me out the meaning of those syntaxes? Thanks in advance!

syntax that I don't understand

those julia code line that I didn't understand because I didn't find them either in documentations.

var1 = Tuple{Integer, Vector}[]

here we declare object var 1, what's a real example for that? what's the python version?

also if X::Matrix, n::Int, then what's the meaning of ? in the below? How should I code this in python?

K = [( i >= j ? dot(view(X,:,i), view(X,:,j)) : 0.0 )::Float64 for i=1:n, j=1:n]

how should we code up this in python?

Also, I am not sure about meaning of -> in below:

for i=1:n 
      id_i = find(x -> x[1] == i, var1)
      xi_i_list =  map(x -> x[2], var1[id_i])

how should we translate this into python?

lastly, I just don't understand the meaning of .> in below:

act= zeros(100)
alpha = zeros(10)

  for i=1:100
    idx = find(x::Tuple{Integer, Vector} -> x[1] == i, var1)
    act[i] = sum(alpha[idx] .> 1e-3)

As a newbie, I am trying to understand the role of find(), map(). To the best, I wish I could write the above Julia code with Python. But I have a hard time understanding the code. Can anyone give possible interpretations and corresponding python codes for learning purposes? Thanks in advance!

1

1 Answer 1

3

First of all, the Julia documentation offers a list of Noteworthy differences from Python. Now to each question:

var1 = Tuple{Integer, Vector}[]

here we declare object var 1, what's a real example for that? what's the python version?

Vector, which is sugar for Array{T,1} where T, means a 1D array with elements of any type. Tuple{Integer, Vector} is thus a tuple with an Integer and a Vector, like, (1, [1, 2]) for example. var1 is just an empty vector of such tuples. You can push! elements like the latter into var1 to create a "real" example:

julia> var1 = Tuple{Integer, Vector}[]
Tuple{Integer,Array{T,1} where T}[]

julia> push!(var1, (1, [1, 2]))
1-element Array{Tuple{Integer,Array{T,1} where T},1}:
 (1, [1, 2])

julia> push!(var1, (2, [3.0, "foo", 4]))
2-element Array{Tuple{Integer,Array{T,1} where T},1}:
 (1, [1, 2])
 (2, Any[3.0, "foo", 4])

what's the meaning of ?

You can type ? to access the "help" mode in julia, and then ask it what ? is. From its documentation:

a ? b : c

Short form for conditionals; read "if a, evaluate b otherwise evaluate c". Also known as the ternary operator.

This syntax is equivalent to if a; b else c end, but is often used to emphasize the value b-or-c which is being used as part of a larger expression, rather than the side effects that evaluating b or c may have.

See the manual section on control flow for more details.

Examples
julia> x = 1; y = 2;

julia> println(x > y ? "x is larger" : "y is larger")
y is larger

not sure about meaning of ->

This is just to create an anonymous function.


I just don't understand the meaning of .>

This is just the element-by-element "greater than" operator >. See the documentation on dotted operators for more details.

Sign up to request clarification or add additional context in comments.

4 Comments

Thank you. How should we simplify this: K = [( i >= j ? dot(view(X,:,i), view(X,:,j)) : 0.0 )::Float64 for i=1:n, j=1:n]? how can we write this line in python? if idi=collect(1:10), then what's the meaning of this: map(10 -> idi(1, 10), collect(1:12))? Could you show me use case of find() and map() in python version? Thanks a lot!
Well I would have written it as K = LowerTriangular(x⋅y for x in eachcol(X), y in eachcol(X)). Not sure about python.
Not sure what your map(10 -> idi(1, 10) means, since the 10 on the left is not a variable.
And not sure about python, no, sorry.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.