I asked a similar question in slack before and now I have formulated my question much better. Maybe a related issue here chainrules for chebpoly constructors · Issue #10 · JuliaMath/FastChebInterp.jl · GitHub

Let’a say I have a function like `relu`

which I approximated via chebyshev interpolation for [-1; 1]

```
using FastChebInterp
x = chebpoints(200, -1, 1)
c = chebinterp(relu.(x), -1, 1)
```

The problem is that I need the derivative in terms of the coefficients of the approximation, which is not what the package provides `chebgradient(c, x)`

I tried making my own version as follows

```
function mychebyshevt(n, x)
if n == 0
return 1.0
elseif n == 1
return x
else
return 2x * mychebyshevt(n - 1, x) - mychebyshevt(n - 2, x)
end
end
function chebyshev(coeffs, x)
ans = 0.0
for (i, a) in enumerate(coeffs)
n = i - 1
ans += a * mychebyshevt(n, x)
end
return ans
end
t = randn(4)
x = rand()
@show ChebyshevT(t)(x) ≈ chebyshev(t, x)
dt, dx = gradient(chebyshev, t, x)
# dt is what I need
```

The problem is that this is awfully slow, and any tips to make this faster would be appreciated. I think the way I’m performing the evaluation of the poolynomial is too naive and inefficient