
Security News
Deno 2.6 + Socket: Supply Chain Defense In Your CLI
Deno 2.6 introduces deno audit with a new --socket flag that plugs directly into Socket to bring supply chain security checks into the Deno CLI.
linspace-generator
Advanced tools
dependency-less MATLAB inspired generator of a linearly spaced vector of numbers
LinspaceGenerator is a dependency-less generator of a linearly spaced vector of numbers. It is inspired by MATLAB's linspace.
npm install --save linspace-generator
yarn add linspace-generator
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;
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';
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.
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.
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%.
FAQs
dependency-less MATLAB inspired generator of a linearly spaced vector of numbers
We found that linspace-generator demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Security News
Deno 2.6 introduces deno audit with a new --socket flag that plugs directly into Socket to bring supply chain security checks into the Deno CLI.

Security News
New DoS and source code exposure bugs in React Server Components and Next.js: what’s affected and how to update safely.

Security News
Socket CEO Feross Aboukhadijeh joins Software Engineering Daily to discuss modern software supply chain attacks and rising AI-driven security risks.