About stdlib...
We believe in a future in which the web is a preferred environment for numerical computation. To help realize this future, we've built stdlib. stdlib is a standard library, with an emphasis on numerical and scientific computation, written in JavaScript (and C) for execution in browsers and in Node.js.
The library is fully decomposable, being architected in such a way that you can swap out and mix and match APIs and functionality to cater to your exact preferences and use cases.
When you use stdlib, you can be absolutely certain that you are using the most thorough, rigorous, well-written, studied, documented, tested, measured, and high-quality code out there.
To join us in bringing numerical computing to the web, get started by checking us out on GitHub, and please consider financially supporting stdlib. We greatly appreciate your continued support!
Return a new ndarray filled with logarithmically spaced values over a specified interval along one or more ndarray dimensions.
npm install @stdlib/blas-ext-logspaceAlternatively,
- To load the package in a website via a
scripttag without installation and bundlers, use the ES Module available on theesmbranch (see README). - If you are using Deno, visit the
denobranch (see README for usage intructions). - For use in Observable, or in browser/node environments, use the Universal Module Definition (UMD) build available on the
umdbranch (see README).
The branches.md file summarizes the available branches and displays a diagram illustrating their relationships.
To view installation and usage instructions specific to each branch build, be sure to explicitly navigate to the respective README files on each branch, as linked to above.
var logspace = require( '@stdlib/blas-ext-logspace' );Returns a new ndarray filled with logarithmically spaced values over a specified interval along one or more ndarray dimensions.
var x = logspace( [ 4 ], 10.0, 0.0, 3.0 );
// returns <ndarray>[ 1.0, 10.0, 100.0, 1000.0 ]The function has the following parameters:
- shape: array shape.
- base: base of the logarithmic scale. May be either a number or an ndarray having a real-valued or "generic" data type. If provided an ndarray, the value must have a shape which is broadcast-compatible with the complement of the shape defined by
options.dims. For example, given the input shape[2, 3, 4]andoptions.dims=[0], a base ndarray must have a shape which is broadcast-compatible with the shape[3, 4]. Similarly, when performing the operation over all elements in a provided input shape, a base ndarray must be a zero-dimensional ndarray. - start: exponent of the starting value, where the starting value is given by
base^start. May be either a number, a complex number, or an ndarray having a numeric or "generic" data type. If provided an ndarray, the value must have a shape which is broadcast-compatible with the complement of the shape defined byoptions.dims. For example, given the input shape[2, 3, 4]andoptions.dims=[0], a start ndarray must have a shape which is broadcast-compatible with the shape[3, 4]. Similarly, when performing the operation over all elements in a provided input shape, a start ndarray must be a zero-dimensional ndarray. - stop: exponent of the final value, where the final value is given by
base^stop. May be either a number, a complex number, or an ndarray having a numeric or "generic" data type. If provided an ndarray, the value must have a shape which is broadcast-compatible with the complement of the shape defined byoptions.dims. For example, given the input shape[2, 3, 4]andoptions.dims=[0], a stop ndarray must have a shape which is broadcast-compatible with the shape[3, 4]. Similarly, when performing the operation over all elements in a provided input shape, a stop ndarray must be a zero-dimensional ndarray. - endpoint: specifies whether to include the
base^stopvalue when writing values to the output ndarray (optional). May be either a boolean or an ndarray having a boolean or "generic" data type. If provided an ndarray, the value must have a shape which is broadcast-compatible with the complement of the shape defined byoptions.dims. For example, given the input shape[2, 3, 4]andoptions.dims=[0], an endpoint ndarray must have a shape which is broadcast-compatible with the shape[3, 4]. Similarly, when performing the operation over all elements in a provided input shape, an endpoint ndarray must be a zero-dimensional ndarray. Default:true. - options: function options (optional).
The function accepts the following options:
- dims: list of dimensions over which to perform operation. If not provided, the function generates logarithmically spaced values along the last dimension. Default:
[-1]. - dtype: output ndarray data type. If both
startandstopare real-valued, the output array data type may be any floating-point data type or "generic". However, if eitherstartorstopare complex-valued, the output array data type must be a complex floating-point data type or "generic". If a data type is provided,startandstopare cast to the specified data type. If a data type is not provided, the default output array data type is determined by applying type promotion rules to thebase,start, andstopdata types. - order: specifies whether an ndarray is
'row-major'(C-style) or'column-major'(Fortran-style). Ifbase,start,stop, andendpointare scalar values, the default order is'row-major'. Ifbase,start,stop, and/orendpointarrays have the same memory layout, the default order is the same layout. Otherwise, the default order is'row-major'. - mode: specifies how to handle indices which exceed array dimensions (see
ndarray). Default:'throw'. - submode: a mode array which specifies for each dimension how to handle subscripts which exceed array dimensions (see
ndarray). If provided fewer modes than dimensions, the function recycles modes using modulo arithmetic. Default:[ options.mode ].
By default, the function always includes the base^stop value in the list of values written to an output ndarray. To exclude the base^stop value, provide an endpoint argument.
var x = logspace( [ 4 ], 10.0, 0.0, 4.0, false );
// returns <ndarray>[ 1.0, 10.0, 100.0, 1000.0 ]When provided scalar or zero-dimensional ndarray base, start, stop, and endpoint arguments, the values are broadcast across all elements in the shape defined by the complement of those dimensions specified by options.dims. To specify separate sub-array configurations, provide non-zero-dimensional ndarray arguments.
var array = require( '@stdlib/ndarray-array' );
var BooleanArray = require( '@stdlib/array-bool' );
var start = array( [ 0.0, 3.0 ] );
var end = array( [ 2.0, 6.0 ] );
var endpoint = array( new BooleanArray( [ true, false ] ) );
var x = logspace( [ 2, 3 ], 10.0, start, end, endpoint );
// returns <ndarray>[ [ 1.0, 10.0, 100.0 ], [ 1000.0, 10000.0, 100000.0 ] ]By default, the function generates logarithmically spaced values along the last dimension of an output ndarray. To perform the operation over specific dimensions, provide a dims option.
var x = logspace( [ 2, 2 ], 10.0, 0.0, 3.0, {
'dims': [ 0, 1 ]
});
// returns <ndarray>[ [ 1.0, 10.0 ], [ 100.0, 1000.0 ] ]To specify the output ndarray data type, provide a dtype option.
var x = logspace( [ 4 ], 10.0, 0.0, 3.0, {
'dtype': 'float32'
});
// returns <ndarray>[ 1.0, 10.0, 100.0, 1000.0 ]Fills an ndarray with logarithmically spaced values over a specified interval along one or more ndarray dimensions.
var zeros = require( '@stdlib/ndarray-zeros' );
var x = zeros( [ 4 ] );
// returns <ndarray>
var out = logspace.assign( x, 10.0, 0.0, 3.0 );
// returns <ndarray>[ 1.0, 10.0, 100.0, 1000.0 ]
var bool = ( x === out );
// returns trueThe function has the following parameters:
- out: output ndarray. Must have a floating-point or "generic" data type.
- base: base of the logarithmic scale. May be either a number or an ndarray having a real-valued or "generic" data type. If provided an ndarray, the value must have a shape which is broadcast-compatible with the complement of the shape defined by
options.dims. For example, given the input shape[2, 3, 4]andoptions.dims=[0], a base ndarray must have a shape which is broadcast-compatible with the shape[3, 4]. Similarly, when performing the operation over all elements in a provided output ndarray, a base ndarray must be a zero-dimensional ndarray. - start: exponent of the starting value, where the starting value is given by
base^start. May be either a number, a complex number, or an ndarray having a numeric or "generic" data type. If provided an ndarray, the value must have a shape which is broadcast-compatible with the complement of the shape defined byoptions.dims. For example, given the input shape[2, 3, 4]andoptions.dims=[0], a start ndarray must have a shape which is broadcast-compatible with the shape[3, 4]. Similarly, when performing the operation over all elements in a provided output ndarray, a start ndarray must be a zero-dimensional ndarray. - stop: exponent of the final value, where the final value is given by
base^stop. May be either a number, a complex number, or an ndarray having a numeric or "generic" data type. If provided an ndarray, the value must have a shape which is broadcast-compatible with the complement of the shape defined byoptions.dims. For example, given the input shape[2, 3, 4]andoptions.dims=[0], a stop ndarray must have a shape which is broadcast-compatible with the shape[3, 4]. Similarly, when performing the operation over all elements in a provided output ndarray, a stop ndarray must be a zero-dimensional ndarray. - endpoint: specifies whether to include the
base^stopvalue when writing values to the output ndarray (optional). May be either a boolean or an ndarray having a boolean or "generic" data type. If provided an ndarray, the value must have a shape which is broadcast-compatible with the complement of the shape defined byoptions.dims. For example, given the input shape[2, 3, 4]andoptions.dims=[0], an endpoint ndarray must have a shape which is broadcast-compatible with the shape[3, 4]. Similarly, when performing the operation over all elements in a provided output ndarray, an endpoint ndarray must be a zero-dimensional ndarray. Default:true. - options: function options (optional).
The function accepts the following options:
- dims: list of dimensions over which to perform operation. If not provided, the function generates logarithmically spaced values along the last dimension. Default:
[-1].
-
Let
Mbe the number of logarithmically spaced values to be written along one or more ndarray dimensions. The spacing between the exponents is thus given byΔ = (stop-start)/(M-1)and the generated values are equal to
base^(start+Δ*i)fori = 0, 1, ..., M-1. -
If an output ndarray has a single element and the function is supposed to include the
base^stopvalue, the set of values written to an output ndarray only includes thebase^stopvalue, but not thebase^startvalue. -
Otherwise, when an output ndarray has a single element and the function is not supposed to include the
base^stopvalue, the set of values written to an output ndarray only includes thebase^startvalue, but not thebase^stopvalue. -
For a real-valued output ndarray, if
startis less thanstop, the exponents are generated in ascending order, and, ifstartis greater thanstop, the exponents are generated in descending order. -
When an output ndarray contains at least two values and the function is supposed to include the
base^stopvalue, the set of values written to an output ndarray is guaranteed to include thebase^startandbase^stopvalues. Beware, however, that values betweenbase^startandbase^stopare subject to floating-point rounding errors. -
When writing to a complex floating-point output ndarray, real-valued start and stop values are treated as complex numbers having a real component equaling the provided value and having an imaginary component equaling zero.
-
When generating logarithmically spaced complex floating-point numbers, generated values follow a logarithmic spiral in the complex plane, where imaginary components of
startandstopcontrol phase rotation. -
The base of the logarithmic scale must be real-valued, even when writing to a complex floating-point output ndarray.
-
Both
startandstopare cast to the data type of the output ndarray. -
The function iterates over ndarray elements according to the memory layout of an output ndarray. Accordingly, performance degradation is possible when operating over multiple dimensions of a large non-contiguous multi-dimensional output ndarray. In such scenarios, one may want to copy an output ndarray to contiguous memory before filling with logarithmically spaced values.
var ndarray2array = require( '@stdlib/ndarray-to-array' );
var array = require( '@stdlib/ndarray-array' );
var logspace = require( '@stdlib/blas-ext-logspace' );
// Create two vectors defining exponent bounds:
var start = array( [ 0.0, 1.0, 2.0, 3.0, 4.0 ] );
var end = array( [ 4.0, 5.0, 6.0, 7.0, 8.0 ] );
// Create a grid:
var out = logspace( [ 5, 5 ], 2.0, start, end, true );
console.log( ndarray2array( out ) );
// Generate logarithmically spaced values over multiple dimensions:
out = logspace( [ 5, 5 ], 2.0, 0.0, 24.0, true, {
'dims': [ 0, 1 ]
});
console.log( ndarray2array( out ) );
// Generate logarithmically spaced values over multiple dimensions in column-major order:
out = logspace( [ 5, 5 ], 2.0, 0.0, 24.0, true, {
'dims': [ 0, 1 ],
'order': 'column-major'
});
console.log( ndarray2array( out ) );This package is part of stdlib, a standard library for JavaScript and Node.js, with an emphasis on numerical and scientific computing. The library provides a collection of robust, high performance libraries for mathematics, statistics, streams, utilities, and more.
For more information on the project, filing bug reports and feature requests, and guidance on how to develop stdlib, see the main project repository.
See LICENSE.
Copyright © 2016-2026. The Stdlib Authors.