# Differentiating through Chebyshev coeff basis

**URL:** https://discourse.julialang.org/t/differentiating-through-chebyshev-coeff-basis/110234
**Category:** General Usage
**Created:** [February 15, 2024, 4:47am UTC](https://discourse.julialang.org/t/differentiating-through-chebyshev-coeff-basis/110234 "2024-02-15T04:47:51Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![isentropic](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/isentropic/32/11894_2.png) [@isentropic](https://discourse.julialang.org/u/isentropic)
#### Post date: [February 15, 2024, 4:47am UTC](https://discourse.julialang.org/t/differentiating-through-chebyshev-coeff-basis/110234/1 "2024-02-15T04:47:52Z")

</div>

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](https://github.com/JuliaMath/FastChebInterp.jl/issues/10)

> **[GitHub - JuliaMath/FastChebInterp.jl: fast multidimensional Chebyshev...](https://github.com/JuliaMath/FastChebInterp.jl)**
>
> fast multidimensional Chebyshev interpolation and regression in Julia - JuliaMath/FastChebInterp.jl

Let’a say I have a function like `relu` which I approximated via chebyshev interpolation for [-1; 1]

```julia
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

```julia

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

---

<div class="post-metadata">

### Author: ![John\_Gibson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/john_gibson/32/5321_2.png) [@John\_Gibson](https://discourse.julialang.org/u/John_Gibson)
#### Post date: [February 15, 2024, 4:57am UTC](https://discourse.julialang.org/t/differentiating-through-chebyshev-coeff-basis/110234/2 "2024-02-15T04:57:36Z")

</div>

Generally with Chebyshev polynomials you want to use an FFT to convert between gridpoint values and polynomial coefficients, and to do differentiation as a map between sets of polynomial coefficients. Have a look at [GitHub - JuliaApproximation/ApproxFun.jl: Julia package for function approximation](https://github.com/JuliaApproximation/ApproxFun.jl)

---

<div class="post-metadata">

### Author: ![isentropic](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/isentropic/32/11894_2.png) [@isentropic](https://discourse.julialang.org/u/isentropic)
#### Post date: [February 15, 2024, 4:59am UTC](https://discourse.julialang.org/t/differentiating-through-chebyshev-coeff-basis/110234/3 "2024-02-15T04:59:58Z")

</div>

My coefficents should be in Chebyshev basis though. I want

f(x) \approx \sum\_{n} a\_n T\_n(x)

and then differentiate df / da

---

<div class="post-metadata">

### Author: ![Yuan-Ru-Lin](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yuan-ru-lin/32/46068_2.png) [@Yuan-Ru-Lin](https://discourse.julialang.org/u/Yuan-Ru-Lin)
#### Post date: [February 15, 2024, 11:45am UTC](https://discourse.julialang.org/t/differentiating-through-chebyshev-coeff-basis/110234/4 "2024-02-15T11:45:35Z")

</div>

Isn’t \frac{\mathrm{d}f}{\mathrm{d}a\_i}(x) just T\_i(x)?

---

<div class="post-metadata">

### Author: ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)
#### Post date: [February 15, 2024, 8:20pm UTC](https://discourse.julialang.org/t/differentiating-through-chebyshev-coeff-basis/110234/5 "2024-02-15T20:20:16Z")

</div>

If you just want the values T\_n(x) for a given x and n = 0,1,\ldots,m, you can do it by calling

```julia
FastChebInterp.chebvandermonde([x], -1, +1, m)[1,:]

```

since `chebvandermonde` takes a length n array of `-1 ≤ x[k] ≤ +1` points and returns the n \times (m+1) “Chebyshev–Vandermonde” matrix:

\begin{pmatrix} T\_0(x[1]) & T\_1(x[1]) & \cdots & T\_m(x[1]) \\ T\_0(x[2]) & T\_1(x[2]) & \cdots & T\_m(x[2]) \\ \vdots & \vdots & \ddots & \vdots \\ T\_0(x[n]) & T\_1(x[n]) & \cdots & T\_m(x[n]) \\ \end{pmatrix}

(This is done by a recurrence, so it has linear \Theta(mn) cost. You can also pass `lb, ub` instead of `-1, +1` and it will rescale the coordinates from `[lb, ub]` to `[-1,+1]` for you.)
