Comparing version 0.11.5 to 0.11.6
{ | ||
"name": "numjs", | ||
"version": "0.11.5", | ||
"version": "0.11.6", | ||
"description": "Like NumPy, in JavaScript", | ||
@@ -5,0 +5,0 @@ "main": "src/index.js", |
@@ -309,3 +309,2 @@ [![Build Status](https://travis-ci.org/nicolaspanel/numjs.png)](https://travis-ci.org/nicolaspanel/numjs) [![npm version](https://badge.fury.io/js/numjs.svg)](https://badge.fury.io/js/numjs) [![Bower version](https://badge.fury.io/bo/numjs.svg)](https://badge.fury.io/bo/numjs) [![Built with Grunt](https://cdn.gruntjs.com/builtwith.svg)](http://gruntjs.com/) | ||
> | ||
``` | ||
@@ -436,2 +435,3 @@ | ||
``` | ||
The shape of an array can be changed with various commands: | ||
@@ -453,4 +453,22 @@ ```js | ||
[ 9, 10, 11]]) | ||
> | ||
``` | ||
Since `a` is matrix we may want its diagonal: | ||
```js | ||
> nj.diag(a) | ||
array([ 0, 5, 10]) | ||
> | ||
``` | ||
### Identity matrix | ||
The identity array is a square array with ones on the main diagonal: | ||
```js | ||
> nj.identity(3) | ||
array([[ 1, 0, 0], | ||
[ 0, 1, 0], | ||
[ 0, 0, 1]]) | ||
``` | ||
### Concatenate different arrays | ||
@@ -668,2 +686,1 @@ | ||
__NumJs__ is built on top of [ndarray](http://scijs.net/packages/#scijs/ndarray) and uses many [scijs packages](http://scijs.net/packages/) | ||
@@ -638,2 +638,24 @@ 'use strict'; | ||
/** | ||
* Extract a diagonal or construct a diagonal array. | ||
* | ||
* @param {Array|NdArray} x | ||
* @returns {NdArray} a view a of the original array when possible, a new array otherwise | ||
*/ | ||
function diag (x) { | ||
return NdArray.new(x).diag(); | ||
} | ||
/** | ||
* The identity array is a square array with ones on the main diagonal. | ||
* @param {number} Number of rows (and columns) in n x n output. | ||
* @param {(String|Object)} [dtype=Array] The type of the output array. | ||
* @return {Array} n x n array with its main diagonal set to one, and all other elements 0 | ||
*/ | ||
function identity (n, dtype) { | ||
var arr = zeros([n, n], dtype); | ||
for (var i = 0; i < n; i++) arr.set(i, i, 1); | ||
return arr; | ||
} | ||
module.exports = { | ||
@@ -688,2 +710,4 @@ config: CONF, | ||
ifft: ifft, | ||
diag: diag, | ||
identity: identity, | ||
int8: function (array) { return NdArray.new(array, 'int8'); }, | ||
@@ -690,0 +714,0 @@ uint8: function (array) { return NdArray.new(array, 'uint8'); }, |
@@ -698,2 +698,26 @@ 'use strict'; | ||
NdArray.prototype.diag = function () { | ||
var d = this.ndim; | ||
if (d === 1) { | ||
// input is a vector => return a diagonal matrix | ||
var T = _.getType(this.dtype); | ||
var shape = [this.shape[0], this.shape[0]]; | ||
var arr = new NdArray(new T(_.shapeSize(shape)), shape); | ||
if (arr.dtype === 'array') { | ||
ops.assigns(arr.selection, 0); | ||
} | ||
for (var i = 0; i < this.shape[0]; i++) arr.set(i, i, this.get(i)); | ||
return arr; | ||
} | ||
var mshape = this.shape; | ||
var mstride = this.selection.stride; | ||
var nshape = (1 << 30); | ||
var nstride = 0; | ||
for (var i = 0; i < d; ++i) { | ||
nshape = Math.min(nshape, mshape[i]) | 0; | ||
nstride += mstride[i]; | ||
} | ||
return new NdArray(this.selection.data, [nshape], [nstride], this.selection.offset); | ||
}; | ||
NdArray.prototype.iteraxis = function (axis, cb) { | ||
@@ -700,0 +724,0 @@ var shape = this.shape; |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
711975
2587
683