From 8abbb143f2826fc3dc2a12f76fdb9502b3349800 Mon Sep 17 00:00:00 2001 From: zhanglw0521 Date: Sun, 12 Jan 2025 12:36:15 +0100 Subject: [PATCH] Optimize gram implementation Avoid ComplexF64 allocation for real-valued matrices --- src/O3_alternative.jl | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/src/O3_alternative.jl b/src/O3_alternative.jl index 1d4ed1d..f7c1b11 100644 --- a/src/O3_alternative.jl +++ b/src/O3_alternative.jl @@ -504,8 +504,8 @@ function re_rpe(n::SVector{N,Int64},l::SVector{N,Int64},L::Int64) where N return UMatrix, FMatrix, MMmat, MM end -function gram(X) - G = zeros(ComplexF64, size(X,1), size(X,1)) +function gram(X::Matrix{SVector{N,T}}) where {N,T} + G = zeros(T, size(X,1), size(X,1)) for i = 1:size(X,1) for j = i:size(X,1) G[i,j] = sum(dot(X[i,t], X[j,t]') for t = 1:size(X,2)) @@ -515,7 +515,9 @@ function gram(X) return G end - function rpe_basis_new(nn::SVector{N, Int64}, ll::SVector{N, Int64}, L::Int64) where N + gram(X::Matrix{<:Number}) = X * X' + +function rpe_basis_new(nn::SVector{N, Int64}, ll::SVector{N, Int64}, L::Int64) where N t_re = @elapsed UMatrix, FMatrix, MMmat, MM = re_rpe(nn, ll, L) @show t_re # should be removed in the final version U, S, V = svd(gram(FMatrix))