Skip to content

Commit ce7dc28

Browse files
committed
Initial commit
0 parents  commit ce7dc28

File tree

6 files changed

+184
-0
lines changed

6 files changed

+184
-0
lines changed

LICENSE

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
Copyright (c) 2016 Ricky Reusser
2+
3+
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
4+
5+
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
6+
7+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

README.md

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
# ndarray-linspace [![Build Status](https://travis-ci.org/scijs/ndarray-linspace.svg)](https://travis-ci.org/scijs/ndarray-linspace) [![npm version](https://badge.fury.io/js/ndarray-linspace.svg)](https://badge.fury.io/js/ndarray-linspace) [![js-standard-style](https://img.shields.io/badge/code%20style-standard-brightgreen.svg)](http://standardjs.com/)
2+
3+
> Fill an ndarray with equally spaced values
4+
5+
## Example
6+
7+
```javascript
8+
9+
linspace(2, 3, 4, {endpoint: false})
10+
// => [ 2, 2.25, 2.5, 2.75 ]
11+
12+
linspace(2, 3, 5, {endpoint: false})
13+
// => [ 2, 2.25, 2.5, 2.75, 3 ]
14+
15+
linspace(2, 4, 10, {dtype: 'int8'})
16+
// => [ 2, 2, 2, 2, 2, 3, 3, 3, 3, 4 ]
17+
18+
var y = pool.zeros([5])
19+
linspace(y, 1, 3, 5)
20+
// y => [ 1, 1.5, 2, 2.5, 3 ]
21+
22+
var y = pool.zeros([2, 2])
23+
linspace(y, 0, 1, 2)
24+
// y => [ 0, 0 ]
25+
// [ 1, 1 ]
26+
27+
```
28+
29+
## Installation
30+
31+
```bash
32+
npm install ndarray-linspace
33+
```
34+
35+
## API
36+
37+
#### require('ndarray-linspace')([output,] start, end, steps[, options])
38+
An array of equally spaced values.
39+
40+
Arguments:
41+
- `output` (options): if provided, the destination array to be filled with values. If the number of dimensions is greater than one, then the length of the first dimension must match the number of values and the entire ndarray will be filled with the linspace corresponding to the index of the first dimension.
42+
- `start`: starting value of the interval
43+
- `end`: ending value of the interval
44+
- `steps`: number of divisions
45+
- `options` (optional): A hash of options. Options are:
46+
- `dtype`: dtype of output array (only used if output array not specified)
47+
- `endpoint` (boolean): whether the output contains the endpoint of the interval
48+
49+
## License
50+
© 2015 Ricky Reusser. MIT License.

examples/index.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
'use strict'
2+
3+
var linspace = require('../')
4+
5+
console.log(linspace(2, 3, 10, {endpoint: false}))
6+
7+
console.log(linspace(2, 4, 10, {dtype: 'int8'}))

index.js

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
'use strict'
2+
3+
var pool = require('ndarray-scratch')
4+
var fill = require('ndarray-fill')
5+
6+
module.exports = linspace
7+
8+
var defaults = {
9+
endpoint: true,
10+
dtype: 'double'
11+
}
12+
13+
function linspace () {
14+
var start, end, n, options, output
15+
16+
if (Number.isFinite(arguments[0])) {
17+
start = arguments[0]
18+
end = arguments[1]
19+
n = arguments[2]
20+
options = Object.assign({}, defaults, arguments[3] || {})
21+
output = pool.zeros([n], options.dtype)
22+
} else {
23+
output = arguments[0]
24+
start = arguments[1]
25+
end = arguments[2]
26+
n = arguments[3]
27+
options = Object.assign({}, defaults, arguments[4] || {})
28+
29+
if (output.shape[0] !== n) {
30+
throw new Error('number of values (' + n + ') must match the first dimension of the output (' + output.shape[0] + ')')
31+
}
32+
}
33+
34+
var d = (end - start) / (n - (options.endpoint ? 1 : 0))
35+
fill(output, function (i) {
36+
return start + i * d
37+
})
38+
39+
return output
40+
}

package.json

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
{
2+
"name": "ndarray-linspace",
3+
"version": "1.0.0",
4+
"description": "Fill an ndarray with equally spaced values",
5+
"main": "index.js",
6+
"scripts": {
7+
"test": "standard && mocha test/*"
8+
},
9+
"keywords": [
10+
"ndarray",
11+
"linspace"
12+
],
13+
"repository": {
14+
"type": "git",
15+
"url": "git://github.com/scijs/ndarray-linspace.git"
16+
},
17+
"author": "Ricky Reusser",
18+
"license": "MIT",
19+
"devDependencies": {
20+
"chai": "^3.4.1",
21+
"mocha": "^2.3.4",
22+
"ndarray": "^1.0.18",
23+
"ndarray-tests": "^1.3.0",
24+
"standard": "^5.4.1"
25+
},
26+
"dependencies": {
27+
"ndarray-fill": "^1.0.1",
28+
"ndarray-scratch": "^1.2.0"
29+
}
30+
}

test/index.js

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
/* global describe, it */
2+
'use strict'
3+
4+
var ndarray = require('ndarray')
5+
var ndt = require('ndarray-tests')
6+
var linspace = require('../')
7+
var assert = require('chai').assert
8+
var pool = require('ndarray-scratch')
9+
10+
describe('linspace', function () {
11+
it('create a new linspace with and endpoint', function () {
12+
var x = linspace(0, 1, 11)
13+
var y = ndarray([0, 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1])
14+
assert(ndt.equal(x, y, 1e-8))
15+
})
16+
17+
it('create a new linspace without and endpoint', function () {
18+
var x = linspace(0, 1, 10, {endpoint: false})
19+
var y = ndarray([0, 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9])
20+
assert(ndt.equal(x, y, 1e-8))
21+
})
22+
23+
it('allows a dtype', function () {
24+
var x = linspace(0, 10, 5, {dtype: 'int8'})
25+
var y = ndarray([0, 2, 5, 7, 10])
26+
assert(ndt.equal(x, y, 1e-8))
27+
})
28+
29+
it('writes in place if output array provided', function () {
30+
var x = pool.zeros([10])
31+
linspace(x, 0, 1, 10, {endpoint: false})
32+
var y = ndarray([0, 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9])
33+
assert(ndt.equal(x, y, 1e-8))
34+
})
35+
36+
it('fills in more than one dimension', function () {
37+
var x = pool.zeros([2, 2])
38+
linspace(x, 0, 1, 2)
39+
var y = ndarray([0, 0, 1, 1], [2, 2])
40+
assert(ndt.equal(x, y, 1e-8))
41+
})
42+
43+
it("throws an error if the size of the first dimension doesn't match the number of points", function () {
44+
var x = pool.zeros([2,2])
45+
assert.throws(function () {
46+
linspace(x, 0, 1, 10)
47+
}, Error, 'must match')
48+
})
49+
})
50+

0 commit comments

Comments
 (0)