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

linspace-generator

v0.2.0

Published

dependency-less MATLAB inspired generator of a linearly spaced vector of numbers

Downloads

14

Readme

Build Status npm version Downloads

linspace-generator

LinspaceGenerator is a dependency-less generator of a linearly spaced vector of numbers. It is inspired by MATLAB's linspace.


Installing

  • with npm:
npm install --save linspace-generator
  • with yarn
yarn add linspace-generator

Typings

This package is written in TypeScript. The following types are declared and exported:

export declare type LinspaceAsArray = (x1: number, x2: number, n?: number) => number[];
export declare function linspaceGenerator(x1: number, x2: number, n?: number): IterableIterator<number>;
export declare const linspaceAsArray: LinspaceAsArray;

How to import

Depending on your application, you might want to only either import the generator itself, or the thin wrapper that converts the generator function into an array.

// imports the generator itself
import { linspaceGenerator } from 'linspace-generator';
// imports the array-wrapper equivalent of the generator
import { linspaceAsArray } from 'linspace-generator';

Usage

Just take a look at the signature of the method:

/**
 * Generate linearly spaced vector of numbers. x1 and x2 define the interval over which linspace
 * generates points. x1 and x2 can be real or integer, and x2 can be either larger or smaller
 * than x1. If x2 is smaller than x1, then the vector contains descending values.
 * - If n is 1, linspace returns x2.
 * - If n is zero or negative, linspace doesn't yield anything
 * - n must be integer
 * @param x1 First point interval
 * @param x2 Last point interval
 * @param n Number of points, specified as an integer scalar
 */
function* linspaceGenerator(x1: number, x2: number, n: number = 100): IterableIterator<number>;

/**
 * Returns the array version of linspaceGenerator.
 * @param x1 First point of the interval
 * @param x2 Last point of the interval
 * @param n Number of points, specified as an integer scalar
 */
const linspaceAsArray = (x1: number, x2: number, n?: number) => number[];

For a reminder of how Generator Functions work, please refer to the official documentation.

Example

Consider the case in which you need to create 6 equally spaced numbers in the interval [0,1]. The desired result will have the following shape (with indexes in the upper row, and values in the bottom row).

| 0 | 1 | 2 | 3 | 4 | 5 | | ---- | ---- | ---- | ---- | ---- | ---- | | 0 | 0.2 | 0.4 | 0.6 | 0.8 | 1 |

The situation above translates to the following code:

const arr: number[];
const getter = linspaceGenerator(0, 1, 6);
let result: IteratorResult<number>;
while (!(result = getter.next()).done) {
  arr.push(result.value);
}
console.log(arr); // [0, 0.2, 0.4, 0.6, 0.8, 1]

Or, if you need to utilize the array directly:

const arr: number[] = linspaceAsArray(0, 1, 6);
console.log(arr); // [0, 0.2, 0.4, 0.6, 0.8, 1]

Note that if the number n of points to generate is 1, linspace returns the last point of the interval x2. Also, if n <= 0, linspaceGenerator doesn't yield anything, and linspaceAsArray returns [].

Please take a look at the tests to check out every possible nuance and other example of using this package.

Related packages

  • fixed-math: utility function that converts a decimal number using fixed-point notation, without using the expensive Number.toFixed
  • is-equally-spaced: utility function that given an array of numbers, evaluates wether or not every element is equally spaced, i.e. if every subsequent couple of numbers in the array has the same distance.

Contributing

Of course PRs are welcome! Before contributing, however, please be sure to run npm run test:ci or yarn test:ci, in order to check if the code you wrote respects the linting conventions and if it doesn't break any test. Please try to keep the unit test code coverage at 100%.