Home > OS >  An Efficient Way (Fastest) to Calculate a Quadratic Form in Julia
An Efficient Way (Fastest) to Calculate a Quadratic Form in Julia

Time:01-10

I would like to calculate a quadratic form: x' Q y in Julia.
What would be the most efficient way to calculate this for the cases:

  1. No assumption.
  2. Q is symmetric.
  3. x and y are the same (x = y).
  4. Both Q is symmetric and x = y.

I know Julia has dot(). But I wonder if it is faster than BLAS call.

CodePudding user response:

It is a BLAS call. (you can use @edit to confirm this).

CodePudding user response:

If your matrix is symmetric use the Symmetric wrapper to improve performance (a different method is called then):

julia> a = rand(10000); b = rand(10000);

julia> x = rand(10000, 10000); x = (x   x') / 2;

julia> y = Symmetric(x);

julia> @btime dot($a, $x, $b);
  47.000 ms (0 allocations: 0 bytes)

julia> @btime dot($a, $y, $b);
  27.392 ms (0 allocations: 0 bytes)

If x is the same as y see Octavian.jl benchmarks

  •  Tags:  
  • Related