From c7b89294aa178b7d291521aa6d99262c114d8afa Mon Sep 17 00:00:00 2001 From: Xiansheng Cai Date: Sun, 1 May 2022 22:14:31 -0400 Subject: [PATCH] optimize use of factorial in barycheb.jl --- src/barycheb.jl | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/src/barycheb.jl b/src/barycheb.jl index 00da556..3282c4e 100644 --- a/src/barycheb.jl +++ b/src/barycheb.jl @@ -54,13 +54,26 @@ function invvandermonde(order) return inv(transpose(vmat)) end +@inline function divfactorial(j, i) + if i == 0 + return 1 + elseif i == 1 + return 1/j + elseif i == -1 + return j-1 + else + return factorial(j-1) / factorial(j+i-1) + end +end + + function weightcoef(a, i::Int, n) # integrate when i=1; differentiate when i=-1 b = zeros(Float64, n) for j in 1:n if j+i-1 > 0 # b[j] = a^(j+i-1)/(j+i-1) - b[j] = a^(j+i-1) * factorial(j-1) / factorial(j+i-1) + b[j] = a^(j+i-1) * divfactorial(j, i) elseif j+i-1 == 0 b[j] = 1 else