The order of operations of linear algebra in Julia

If I have a team y = A*B*x, where Aand Bare large matrices, and xand yare vectors whether Yulia blank y = ((A*B)*x)or y = (A*(B*x))?

The second option should be the best, since it should highlight an additional vector, and not a large matrix.

+4
source share
1 answer

The best way to check this type is to dump the lowered code with @code_loweredmacro:

julia> @code_lowered A * B * x
CodeInfo(:(begin 
        nothing
        return (Core._apply)(Base.afoldl, (Core.tuple)(Base.*, (a * b) * c), xs)
    end))

Like many other languages, Julia does y = (A*B)*xinstead y = A*(B*x), so you need to use parsers explicitly to reduce selection.

julia> using BenchmarkTools

julia> @btime $A * ($B * $x);
  6.800 μs (2 allocations: 1.75 KiB)

julia> @btime $A * $B * $x;
  45.453 μs (3 allocations: 79.08 KiB)
+4
source

Source: https://habr.com/ru/post/1680925/


All Articles