# How can I code the Bernstein operator in Mathematica?

Given a function $f(x) \in C[0,1]$, we define the $n$th Bernstein Operator $T_n$ by

$(T_nf)(t) = \sum_{k=0}^n {{n}\choose{k}}f\left(\frac{k}{n}\right)t^k(1 - t)^{n - k}$.

I'm trying to code this in Mathematica, but I don't see how it can be done to take an unknown function as an argument. My current working code is

b[f[x_]_, n_, t_] := Sum[(n!/(k!*(n - k)!))*f(k/n)*t^k*(1 - t)^(n - k), {k, 0, n}]


but this doesn't seem to work. Can anyone offer a way to take an undefined function as an input?

Edit: Though the code below in the comments works spectacularly, the code also works as-is, so long as one remembers to change $f(k/n)$ to $f[k/n]$, so that Mathematica knows to evaluate $f$ at $k/n$.

• bop[n_Integer, f_, t_] := Sum[Binomial[n, k] Function[t, f][k/n] t^k (1 - t)^(n - k), {k, 0, n}], and then try bop[5, Exp[t], t]. Mar 28, 2018 at 20:01
• That worked perfectly! Thank you! Mar 28, 2018 at 20:11
• Also for reference: BernsteinBasis[] is built-in, so you can modify my code to shorten it further. Mar 28, 2018 at 20:15
• Welcome! To make the most of Mma.SE start by taking the tour now. It will help us to help you if you write an excellent question. Edit if improvable, show due diligence, give brief context, include minimal working example of code and data in formatted form. As you receive give back, vote and answer questions, keep the site useful, be kind, correct mistakes and share what you have learned. Jun 16, 2018 at 10:44

Using a slightly different notation, consistent with mathworld and literature

ClearAll[BernsteinOperator];
BernsteinOperator[n_Integer][f_, x_Symbol] := Sum[
Function[x, f][i/n] BernsteinBasis[n, i, x]
, {i, 0, n}
]

PiecewiseExpand[BernsteinOperator[a Exp[b x], x]] Plot[
Evaluate@{
Exp[x],
BernsteinOperator[Exp[x], x]
}
, {x, -0.5, 1.5}
, PlotTheme -> "Scientific"
, PlotStyle -> {Gray, Red}
] 