npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

sm-integral

v1.0.1

Published

Compute definite and improper integrals using Romberg Integration. Easily integrate JavaScript functions.

Downloads

99

Readme

sm-integral

Build Status

Compute definite and improper integrals with Romberg Integration. Easily and accurately integrate JavaScript functions.

Features

  • Easily evaluate definite and improper integrals.
  • Control the order of accuracy (see "Order of Accuracy").
  • Fast computation time (see "Tests").
  • Divide-by-zero safeguard.

Install

npm install sm-integral

Usage

Below, we demonstrate how to evaluate the following integrals:

const Integral = require('sm-integral');

//definite function to integrate
function f (x) {
  return 1 / (x * x);
}

//definite integral
console.log(Integral.integrate(f, 5, 10)); //0.1

//improper integral
console.log(Integral.integrate(f, "-inf", -10)); //0.1

Configuration

The integrate() function has the following parameters.

class Integral {
  //...
  integrate(f, a, b, e = 18) {
    //...
  }
}
  • f is the JavaScript function to be integrated.
  • a is the lower bound of the integral (must either be a number or "inf"/"-inf").
  • b is the upper bound of the integral (must either be a number or "inf"/"-inf").
  • e is the order of accuracy (must be a positive even integer - if it is odd, it will be incremented). The default value of 18 is enough to evaluate most integrals to at least +-1e-9 accuracy (see "Tests").

Order of Accuracy

To have an order of accuracy e means that the returnedValue when computing

Tests

In the test folder, 8 integrals are tested with the default e=18 parameter. The 8 evaluations were completed within 30ms, each producing results accurate to at least +-1e-9.

describe('sm-integral', () => {
  it('Integrating the constant function f(x)=x/x exactly.', () => {
    function f (x) {
      return x / x; // 1
    }
    assert.equal(Integral.integrate(f, 0, 10, 18), 10,
      'Evaluate constant function f(x)=x/x without error.');
  });
  it('Integrating the constant function f(x)=x/x exactly, but with a > b.', () => {
    function f (x) {
      return x / x; // 1
    }
    assert.equal(Integral.integrate(f, 5, -20, 18), -25,
      'Evaluate constant function f(x)=x/x without error where a > b.');
  });
  it('Integrating the linear function f(x)=0.28*x-127 exactly.', () => {
    function f (x) {
      return 0.28 * x - 127;
    }
    assert.equal(Integral.integrate(f, -100, 500, 18), -42600,
      'Evaluate linear function f(x)=0.28*x+127 without error.');
  });
  it('Integrating the quadratic function f(x)=0.56*x*x+0.7*x+17 with order of accuracy of 18.', () => {
    function f (x) {
      return 0.7 * x * x + 0.56 * x + 17;
    }
    assert.closeTo(Integral.integrate(f, 5, 13, 18), 659 + 59 / 75, 1e-9,
      'Evaluate quadratic function f(x)=0.7*x*x+0.56*x+17 with' +
      ' order of accuracy 18.');
  });
  it('Integrating the quadratic function f(x)=1/(x*x).', () => {
    function f (x) {
      return 1 / (x * x);
    }
    assert.closeTo(Integral.integrate(f, 5, 10, 18), 0.1, 1e-9,
      'Evaluate quadratic function f(x)=1/(x*x)' +
      'with order of accuracy 18.');
  });
  it('Integrating the cubic function f(x)=56.1*x*x*x-2*x+1 with order of accuracy of 18.', () => {
    function f (x) {
      return 56.1 * x * x * x - 2 * x + 1;
    }
    assert.closeTo(Integral.integrate(f, -2, 21, 18), 2726957 + 5 / 8, 1e-9,
      'Evaluate cubic function f(x)=56.1*x*x*x-2*x+1 with' +
      ' order of accuracy 18.');
  });
  it('Integrating the error function with order of accuracy 18.', () => {
    function f (x) {
      return Math.pow(Math.E, 1 / (x * x));
    }
    assert.closeTo(Integral.integrate(f, 1, 4, 18), 3.954384577738, 1e-9,
      'Evaluate error function with' +
      ' order of accuracy 18.');
  });
  it('Testing improper integral on f(x)=1/(x*x)', () => {
    function f (x) {
      return 1 / (x * x);
    }
    assert.closeTo(Integral.integrate(f, '-inf', -10, 18), 0.1, 1e-9,
      'Evaluate improper integral of' +
      ' f(x)=1/(x*x) with order of accuracy 18.');
  });
});