DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
  1. DZone
  2. Coding
  3. JavaScript
  4. WebAssembly Cephes: Mathematical Special Functions in JavaScript

WebAssembly Cephes: Mathematical Special Functions in JavaScript

JavaScript isn't typically thought of as a data language, but maybe it should be. See how the open source Cephes project is making JS a competitor to R and Python.

Andreas Madsen user avatar by
Andreas Madsen
·
Oct. 01, 18 · Tutorial
Like (1)
Save
Tweet
Share
3.95K Views

Join the DZone community and get the full member experience.

Join For Free

Introduction

A lot of the mathematical implementations we do at NearForm depends on so-called mathematical special functions. There are no clearly defined criteria for what a special function is, but suffice to say they are functions that often turn up in mathematics and most of them are notoriously hard to compute. Often they are defined as the solution to a well-posed problem. However, the solution itself is not easy to compute.

JavaScript is severely lacking in implementations of these special functions, especially trusted implementations. If you are lucky, you can find a basic implementation in npm. However, the implementations often have numerous undocumented issues and only work somewhat well for a small and undocumented input range.

In R these functions are often built-in, and in Python, SciPy implements these functions. Deep within the SciPy source code, we find the Cephes library which is what implements many of these functions.

Cephes is a pure C library, that doesn't depend on anything, including the C standard library. This makes it ideal to be compiled to WebAssembly using emscripten, as it means there won't be calls between JavaScript and WebAssembly which could otherwise cause performance issues.

We have released the WebAssembly version of Cephes on npm, see https://www.npmjs.com/package/cephes. So all you need is to install it with npm install cephes.

Using our Cephes module is very simple, as we have wrapped each WebAssembly function in a custom JavaScript function that takes care of all the memory management.

const cephes = require('cephes');
const value = cephes.gamma(2);

Examples

Below are just a few examples of the 125 available functions. You can see all of them in our README file.

All of the values that make up the graph are calculated dynamically in your browser. This gives you an idea of just how fast Cephes in WebAssembly is.

1. The Bessel function is the solution to the equation:

Image title

The Bessel function is typically used in partial differential equations, where will often be a fixed integer.

Image title

2. The Gamma function is used in many probability distributions.

Image title

As its value increases very quickly, it is often the function that is used in practice. Cephes has dedicated implementations for both. The DiGamma function =, is its derivative and also appear frequently in probably theory: 

Image title

Image title

3. The beta distribution is central in a lot of Bayesian statistics.

Image title

The cumulative distribution function is notoriously hard to compute, but this function is also implemented by Cephes.

Image title

Image title

4. The Airy function, Ai(x), is the solution to the differential equation.

Image title

It is often used in physics such as quantum mechanics. Cephes also defines it compimentary function Bi(x) and their derivatives and Ai'(x) and Bi'(x).

Image title


Cephes as an Open Source Project

Whilst Cephes lacks a general license, its author, Stephen L. Moshier, has kindly granted us permission to license it as BSD-3-Clause. Whilst the library continues to be maintained, it doesn't use some of the more modern approximation methods for many of the special functions.

The good news is that R and SciPy are working together on making a better Open Source library of these special functions. But it will take a while before that library is ready and until then Cephes is by far the best choice.

Ceph (software) WebAssembly JavaScript Open source

Published at DZone with permission of Andreas Madsen, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • Beginners’ Guide to Run a Linux Server Securely
  • Exploring the Benefits of Cloud Computing: From IaaS, PaaS, SaaS to Google Cloud, AWS, and Microsoft
  • Best Practices for Writing Clean and Maintainable Code
  • AIOps Being Powered by Robotic Data Automation

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: