How to translate this phyton code to julia with efficienty(short code,easy to read write)? What about a append!() symbol in julia to "concatenate" vector?

question
proposal

#1

Hi I was thinking this kind of code is short and easy to write is there any ways to do this in Julia? thanks

#Python code of Trinomial triangle
row = [1]
for i in range(8):
    print(row)
    row = [sum(t) for t in zip([0,0]+row, [0]+row+[0], row+[0,0])]

#output

[1]
[1, 1, 1]
[1, 2, 3, 2, 1]
[1, 3, 6, 7, 6, 3, 1]
[1, 4, 10, 16, 19, 16, 10, 4, 1]
[1, 5, 15, 30, 45, 51, 45, 30, 15, 5, 1]
[1, 6, 21, 50, 90, 126, 141, 126, 90, 50, 21, 6, 1]
[1, 7, 28, 77, 161, 266, 357, 393, 357, 266, 161, 77, 28, 7, 1]

Thanks you


#2

For a direct translation:

julia> row = [1]
1-element Array{Int64,1}:
 1

julia> for i in 1:8
           println(row)
           global row = [sum(t) for t in zip([0; 0; row], [0; row; 0], [row; 0; 0])]
       end
[1]
[1, 1, 1]
[1, 2, 3, 2, 1]
[1, 3, 6, 7, 6, 3, 1]
[1, 4, 10, 16, 19, 16, 10, 4, 1]
[1, 5, 15, 30, 45, 51, 45, 30, 15, 5, 1]
[1, 6, 21, 50, 90, 126, 141, 126, 90, 50, 21, 6, 1]
[1, 7, 28, 77, 161, 266, 357, 393, 357, 266, 161, 77, 28, 7, 1]

#3

Thanks you, I didn’t think about the [;;] notation, I am not use to it.
I was thinking to much about the python strange “[1]+[0] = [1,0]”.
Have a nice day


#4

Is there any reason for not having a symbol in Julia who work like + and vectors in python? I mean a symbol who does the same has appends!()?

Julia have the * for strings.

Thank you


#5

Here is some reading on that subject: https://github.com/JuliaLang/julia/pull/22461


#6

Consistency with mathematics. In mathematics, + on vectors doesn’t append them, it adds them as vectors. I.e. [1,2,3] + [2,3,4] == [3,5,7] rather than [1,2,3] + [2,3,4] == [1,2,3,2,3,4].


#7

What about using another symbol witch is not + or *?

I guess [a;b] is still concise and work perfectly fine.

Thanks


#8

You mean like ++?


#9

I was speaking about any symbol really if it is not use yet in the language for other things.

I find it very nice that you use ∪ for union and ∩ for intersect for example.

Maybe there is a symbol out there who can suggest this append!() function.

But really this is not a big deal , I think the [;;;] syntax is working fine , I just forgot about it for a moment there.


#10

About what is desirable to write:

julia> row=[1]
1-element Array{Int64,1}:
 1

julia> @btime [0;0;$row]
  2.716 μs (28 allocations: 1.13 KiB)
3-element Array{Int64,1}:
 0
 0
 1

julia> @btime [[0,0];$row]
  57.540 ns (2 allocations: 208 bytes)
3-element Array{Int64,1}:
 0
 0
 1

#11

The difference is

julia> @btime vcat(0, 0, $[1])
  2.921 μs (28 allocations: 1.13 KiB)
3-element Array{Int64,1}:
 0
 0
 1

julia> @btime vcat([0, 0], $[1])
  81.220 ns (2 allocations: 208 bytes)
3-element Array{Int64,1}:
 0
 0
 1

I think that there is a fast path for vcat with only Array arguments.