# Package to mollify a function?

**URL:** <https://discourse.julialang.org/t/package-to-mollify-a-function/94467>\
**Category:** General Usage\
**Created:** [February 11, 2023, 1:39pm UTC](https://discourse.julialang.org/t/package-to-mollify-a-function/94467 "2023-02-11T13:39:02Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Masacroso](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/masacroso/32/9412_2.png) [@Masacroso](https://discourse.julialang.org/u/Masacroso)\
**Post date:** [February 11, 2023, 1:39pm UTC](https://discourse.julialang.org/t/package-to-mollify-a-function/94467/1 "2023-02-11T13:39:02Z")

</div>

Hi, I was searching in the web but I didn’t found something about how to mollify a mathematical function, using any custom mollifier, in Julia. So my question is: there is a package to do that?

If not, there is a way to implement it? Thank you.

---

<div class="post-metadata">

**Author:** ![goerz](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/goerz/32/3269_2.png) [@goerz](https://discourse.julialang.org/u/goerz)\
**Post date:** [February 11, 2023, 1:47pm UTC](https://discourse.julialang.org/t/package-to-mollify-a-function/94467/2 "2023-02-11T13:47:25Z")

</div>

What does “mollify” mean?

---

<div class="post-metadata">

**Author:** ![Masacroso](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/masacroso/32/9412_2.png) [@Masacroso](https://discourse.julialang.org/u/Masacroso)\
**Post date:** [February 11, 2023, 1:49pm UTC](https://discourse.julialang.org/t/package-to-mollify-a-function/94467/3 "2023-02-11T13:49:09Z")

</div>

Apply a mollifier to a mathematical function. Take a look [here](https://en.wikipedia.org/wiki/Mollifier).

---

<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 11, 2023, 2:41pm UTC](https://discourse.julialang.org/t/package-to-mollify-a-function/94467/4 "2023-02-11T14:41:05Z")

</div>

> [@Masacroso](#):
>
> Hi, I was searching in the web but I didn’t found something about how to mollify a mathematical function, using any custom mollifier, in Julia.

Mollification is an integral, so in principle you could use any numerical integration package for this, e.g.

```julia
using QuadGK
mollify(f::Function, φ::Function, support::NTuple{2, Real}; kws...) =
    x -> quadgk(y -> f(x - y) * φ(y), support...; kws...)[1]

```

which returns a new mollified function smoothed by `φ` (given the `support` of `φ`). For example:

```julia
julia> ϕ(x) = exp(-inv(1-x^2)) / 0.44399381616807943782304892117
ϕ (generic function with 1 method)

julia> f̃ = mollify(sin, x -> ϕ(10x)*10, (-0.1, 0.1))
#2 (generic function with 1 method)

julia> f̃(3) - sin(3) # mollified - original
-0.00011153383927728355

```

where the mollifier is the standard [bump function](https://en.wikipedia.org/wiki/Bump_function) squeezed by a factor of 10.

This is relatively slow because it performs an integral for every evaluation of the new function. Also, the main utility of mollifiers is for smoothing functions with singularities/discontinuities (and singular distributions), and in this case it’s helpful to give `quadgk` [additional endpoints](https://juliamath.github.io/QuadGK.jl/stable/quadgk-examples/#Integrands-with-singularities-and-discontinuities) at the locations of any singularities. So you might want to pass an additional keyword argument indicating the locations of any singularities of `f` and add these to the integration domain as needed.

Even better might be to define a specialized method that uses the [ApproxFun.jl package](https://github.com/JuliaApproximation/ApproxFun.jl) to construct a `Fun` (a type of polynomial approximation) of the mollified function, so that it is subsequently quick to evaluate.

For particular classes of functions f(x), of course, there might be more efficient algorithms.  
For example, people use the term “discrete mollification” for functions defined at a set of discrete data points, e.g. [in this paper](https://www.tandfonline.com/doi/full/10.1080/17415977.2020.1733996), which is equivalent to mollification of a **piecewise constant** function f(x), and in this case there seem to be a variety of specialized algorithms.

Did you have a particular application in mind?

---

<div class="post-metadata">

**Author:** ![Masacroso](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/masacroso/32/9412_2.png) [@Masacroso](https://discourse.julialang.org/u/Masacroso)\
**Post date:** [February 11, 2023, 2:55pm UTC](https://discourse.julialang.org/t/package-to-mollify-a-function/94467/5 "2023-02-11T14:55:26Z")

</div>

The application is just for pedagogic reasons, not “real” work, just visualization of how a function looks when its mollified. Just to say: thank you very much for your time and your kindly help.

---

<div class="post-metadata">

**Author:** ![cjdoris](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cjdoris/32/213133_2.png) [@cjdoris](https://discourse.julialang.org/u/cjdoris)\
**Post date:** [February 11, 2023, 5:58pm UTC](https://discourse.julialang.org/t/package-to-mollify-a-function/94467/6 "2023-02-11T17:58:15Z")

</div>

AFAICT mollification is just another word for convolution? In which case, you can use FFTs to compute the convolved/mollified function on an evenly spaced set of points - which is exactly what you need if you’re plotting it.

---

<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 11, 2023, 6:26pm UTC](https://discourse.julialang.org/t/package-to-mollify-a-function/94467/7 "2023-02-11T18:26:06Z")

</div>

> [@cjdoris](#):
>
> AFAICT mollification is just another word for convolution?

It’s a convolution with a very special function: a “nascent delta function” that is also a C\_\infty test function with compact support, via which the convolution can be applied to arbitrary [distributions](https://en.wikipedia.org/wiki/Distribution_(mathematics)).

> [@cjdoris](#):
>
> In which case, you can use FFTs to compute the convolved/mollified function on an evenly spaced set of points

That’s not quite the same thing, because it computes a discrete convolution, and in particular it corresponds to approximating the convolution integral by a trapezoidal rule with equally spaced points. This is a poor approximation (very slowly converging) if you want to mollify functions with discontinuities or other singularities.
