Comparing version
{ | ||
"name": "zeros", | ||
"version": "0.0.0", | ||
"version": "1.0.0", | ||
"description": "Initialize an ndarray with zeros", | ||
@@ -10,9 +10,9 @@ "main": "zeros.js", | ||
"dependencies": { | ||
"ndarray": "~1.0.0" | ||
"ndarray": "^1.0.16" | ||
}, | ||
"devDependencies": { | ||
"tape": "~1.0.4" | ||
"tape": "^4.0.0" | ||
}, | ||
"scripts": { | ||
"test": "tap test/*.js" | ||
"test": "tape test/*.js" | ||
}, | ||
@@ -19,0 +19,0 @@ "repository": { |
@@ -14,2 +14,16 @@ zeros | ||
It also accepts an optional dtype as specified in the [ndarray](https://github.com/scijs/ndarray) docs. For example, | ||
```javascript | ||
//Creates a 64x64 ndarray over a float32array filled with 0 | ||
var x = zeros([64, 64], 'float32') | ||
``` | ||
This makes it easy to create an array with the same size and dtype as an existing array: | ||
```javascript | ||
var x = zeros([64, 64], 'float32') | ||
var y = zeros( x.shape, x.dtype ) | ||
``` | ||
If you find yourself frequently creating the same array over and over though, [ndarray-scratch](https://github.com/mikolalysenko/ndarray-scratch) may be a better option. | ||
## Install | ||
@@ -20,6 +34,21 @@ Just use npm: | ||
### `require("zeros")(shape)` | ||
### `require("zeros")(shape[, dtype])` | ||
Creates an ndarray with the given shape that is initialized to zero | ||
* `shape` is the shape of the resulting array | ||
* `dtype` is the data type of the array to allocate. Must be one of: | ||
+ `"array"` | ||
+ `"uint8"` | ||
+ `"uint16"` | ||
+ `"uint32"` | ||
+ `"int8"` | ||
+ `"int16"` | ||
+ `"int32"` | ||
+ `"float"` | ||
+ `"float32"` | ||
+ `"double"` | ||
+ `"float64"` | ||
+ `"data"` | ||
+ `"uint8_clamped"` | ||
+ `"buffer"` | ||
@@ -26,0 +55,0 @@ **Returns** An array which is initialized to 0 |
43
zeros.js
@@ -5,8 +5,41 @@ "use strict" | ||
module.exports = function zeros(shape) { | ||
var sz = 1 | ||
function dtypeToType(dtype) { | ||
switch(dtype) { | ||
case 'uint8': | ||
return Uint8Array; | ||
case 'uint16': | ||
return Uint16Array; | ||
case 'uint32': | ||
return Uint32Array; | ||
case 'int8': | ||
return Int8Array; | ||
case 'int16': | ||
return Int16Array; | ||
case 'int32': | ||
return Int32Array; | ||
case 'float': | ||
case 'float32': | ||
return Float32Array; | ||
case 'double': | ||
case 'float64': | ||
return Float64Array; | ||
case 'uint8_clamped': | ||
return Uint8ClampedArray; | ||
case 'generic': | ||
case 'buffer': | ||
case 'data': | ||
case 'dataview': | ||
return ArrayBuffer; | ||
case 'array': | ||
return Array; | ||
} | ||
} | ||
module.exports = function zeros(shape, dtype) { | ||
dtype = dtype || 'float64'; | ||
var sz = 1; | ||
for(var i=0; i<shape.length; ++i) { | ||
sz *= shape[i] | ||
sz *= shape[i]; | ||
} | ||
return ndarray(new Float64Array(sz), shape) | ||
} | ||
return ndarray(new (dtypeToType(dtype))(sz), shape); | ||
} |
Deprecated
MaintenanceThe maintainer of the package marked it as deprecated. This could indicate that a single version should not be used, or that the package is no longer maintained and any new vulnerabilities will not be fixed.
Found 1 instance in 1 package
Trivial Package
Supply chain riskPackages less than 10 lines of code are easily copied into your own project and may not warrant the additional supply chain risk of an external dependency.
Found 1 instance in 1 package
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
Found 1 instance in 1 package
4230
58.66%41
412.5%0
-100%1
-50%57
103.57%0
-100%Updated