{"id":15422982,"url":"https://github.com/letmaik/xndarray","last_synced_at":"2025-04-19T15:31:15.033Z","repository":{"id":57401985,"uuid":"57374504","full_name":"letmaik/xndarray","owner":"letmaik","description":"▦ Multidimensional arrays with semantics in JavaScript","archived":false,"fork":false,"pushed_at":"2016-10-07T10:16:44.000Z","size":64,"stargazers_count":8,"open_issues_count":0,"forks_count":0,"subscribers_count":4,"default_branch":"master","last_synced_at":"2025-02-24T00:06:10.977Z","etag":null,"topics":["coordinates","javascript","multidimensional-arrays","ndarray","semantics"],"latest_commit_sha":null,"homepage":"https://letmaik.github.io/xndarray/","language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"other","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/letmaik.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2016-04-29T09:48:15.000Z","updated_at":"2019-10-11T22:09:43.000Z","dependencies_parsed_at":"2022-09-15T18:40:39.941Z","dependency_job_id":null,"html_url":"https://github.com/letmaik/xndarray","commit_stats":null,"previous_names":["neothemachine/xndarray"],"tags_count":3,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/letmaik%2Fxndarray","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/letmaik%2Fxndarray/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/letmaik%2Fxndarray/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/letmaik%2Fxndarray/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/letmaik","download_url":"https://codeload.github.com/letmaik/xndarray/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":241470357,"owners_count":19968039,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["coordinates","javascript","multidimensional-arrays","ndarray","semantics"],"created_at":"2024-10-01T17:40:00.163Z","updated_at":"2025-03-02T06:32:05.889Z","avatar_url":"https://github.com/letmaik.png","language":"JavaScript","readme":"# xndarray\n\nMultidimensional arrays with semantics in JavaScript.\n\n## Introduction\n\n\n\n## Install\n\nxndarray works on browsers and any tool following the CommonJS/node module conventions.\n\nA minified browser version of this library is available in the [GitHub releases](https://github.com/neothemachine/xndarray/releases) as well as on the [jsDelivr CDN](http://www.jsdelivr.com/projects/xndarray). It can be included like that:\n```html\n\u003cscript src=\"//cdn.jsdelivr.net/xndarray/0.3/xndarray.min.js\"\u003e\u003c/script\u003e\n\u003cscript\u003e\nvar arr = xndarray(...)\n\u003c/script\u003e\n```\n\n## API\n\nIf you use xndarray within a CommonJS/node environment, then import the constructor as follows:\n```js\nvar xndarray = require('xndarray')\n```\nWhen using the minified browser version, then this constructor is made available globally under the same name.\n\n### Constructor\n\n#### `xndarray(data, {shape, names, coords, stride, offset})`\n\n- `data` is a 1D array storage. It is either an instance of `Array`, a typed array, or an object that implements `get()`, `set()`, `.length`\n- `shape` is the shape of the view as an array of integers (Default: `[data.length]`)\n- `names` is an array of dimension names (Default: `['dim_0','dim_1',...]`)\n- `coords` is a coordinates map of 1D array storages. Each key is a (dimension) name and each value is either an instance of `Array`, a typed array, an [ndarray][ndarray], or an object that implements `get()`, `set()`, `.length` (Default: `{dim_0: [0,1,2,...], dim_1: [0,1,2,...],...}`)\n- `stride` is the resulting stride of the view. (Default: row major)\n- `offset` is the offset to start the view (Default: 0)\n\n```js\nvar arr = xndarray([1,2,3,4,5,6], {\n  shape: [2,3],\n  names: ['y','x'],\n  coords: {\n    y: [10,12,14],\n    x: [100,101,102],\n    t: [new Date('2001-01-01')]\n  }\n})\n\n// arr == 1 2 3\n//        4 5 6\n```\n\nIn the example, there are coordinates for the `y` and `x` dimensions plus an extra coordinate named `t`.\nExtra coordinates are ignored in operations like slicing and carried over unchanged.\n\n##### 0D arrays\n\nYou can construct 0D arrays by defining an empty shape without dimension names:\n```js\nvar arr = xndarray([5], {shape: [], coords: {time: [new Date('2001-01-01')]})\n```\n\n#### `xndarray(ndarr, {names, coords})`\n\nThis constructor variant wraps existing [ndarray][ndarray] objects.\n\n- `ndarr` is an [ndarray][ndarray] object.\n- `names` is an array of dimension names (Default: `['dim_0','dim_1',...]`)\n- `coords` is a coordinates map of 1D array storages. Each key is a (dimension) name and each value is either an instance of `Array`, a typed array, an [ndarray][ndarray], or an object that implements `get()`, `set()`, `.length` (Default: `{dim_0: [0,1,2,...], dim_1: [0,1,2,...],...}`)\n\nxndarray is fully compatible with [ndarray][ndarray] and can directly wrap such objects:\n```js\nvar nd = ndarray([1,2,3,4], [2,2])\nvar xnd = xndarray(nd, {\n  names: ['y','x'],\n  coords: {\n    y: [10,12,14],\n    x: [100,101,102],\n    t: [new Date('2001-01-01')]\n  }\n})\n```\n\nAll [ndarray modules](http://scijs.net/packages/) can directly be used on xndarray objects:\n```js\nvar unpack = require('ndarray-unpack')\nvar nd2 = unpack(xnd) // [[1,2],[3,4]]\n```\n[ndarray][ndarray] functions that return a new [ndarray][ndarray] object will not have any xndarray functionality and have to be wrapped again.\n\n### Members\n\nMembers originating from [ndarray][ndarray]:\n- `array.data` - The underlying 1D storage for the multidimensional array\n- `array.shape` - The shape of the array\n- `array.dimension` - Dimension of the array as an integer (equals `array.shape.length`)\n- `array.size` - Size of the array in logical elements (equals `array.shape[0]*array.shape[1]*...`)\n- `array.stride` - The layout of the array in memory\n- `array.offset` - The starting offset of the array in memory\n- `array.dtype` - String representing the underlying data type\n- `array.order` - Order of the stride of the array, sorted in ascending length\n\nAdditional members:\n- `array.names` - The dimension names. A string array of length `array.dimension`.\n- `array.coords` - The coordinates. A Map from (dimension) name to 1D [ndarrays][ndarray].\n\n### Element access\n\n#### `array.get(i,j,...)` / `array.xget({x: i, y: j, ...})`\n\n```js\nvar arr = xndarray([1,2,3,4,5,6], {shape: [2,3], names: ['y','x']})\n\n// arr.get(0, 1)\nvar v = arr.xget({y: 0, x: 1}) \n\n// v == 2\n```\n\n#### `array.set(i,j,...,v)` / `array.xset({x: i, y: j, ...}, v)`\n\n```js\nvar arr = xndarray([1,2,3,4,5,6], {shape: [2,3], names: ['y','x']})\n\n// arr.set(1, 1, 8)\narr.xset({y: 1, x: 1}, 8)\n\n// arr == 1 2 3\n//        4 8 6\n```\n\n#### `array.index(i,j,...)` / `array.xindex({x: i, y: j, ...})`\n\n```js\nvar arr = xndarray([1,2,3,4,5,6], {shape: [2,3], names: ['y','x']})\n\n// arr.index(1, 0)\nvar idx = arr.xindex({y: 1, x: 0})\n\n// idx == 3\n```\n\n### Slicing\n\n#### `array.lo(i,j,...)` / `array.xlo({x: i, y: j, ...})`\n\n```js\nvar arr = xndarray([1,2,3,4,5,6], {\n  shape: [2,3],\n  names: ['y','x'],\n  coords: {\n    y: [10,12,14],\n    x: [100,101,102]\n  }\n})\n\n// arr == 1 2 3\n//        4 5 6\n\n// arr.lo(null, 1)\nvar a = arr.xlo({x: 1})\n\n// a == 2 3\n//      5 6\n// a.coords.get('y') == 10 12 14\n// a.coords.get('x') == 101 102\n```\n\n#### `array.hi(i,j,...)` / `array.xhi({x: i, y: j, ...})`\n\n```js\nvar arr = xndarray([1,2,3,4,5,6], {\n  shape: [2,3],\n  names: ['y','x'],\n  coords: {\n    y: [10,12,14],\n    x: [100,101,102]\n  }\n})\n\n// arr.hi(null, 2)\nvar a = arr.xhi({x: 2})\n\n// a == 1 2\n//      4 5\n// a.coords.get('y') == 10 12 14\n// a.coords.get('x') == 100 101\n```\n\n#### `array.step(i,j,...)` / `array.xstep({x: i, y: j, ...})`\n\n```js\nvar arr = xndarray([1,2,3,4,5,6], {\n  shape: [2,3],\n  names: ['y','x'],\n  coords: {\n    y: [10,12,14],\n    x: [100,101,102]\n  }\n})\n\n// arr.step(null, 2)\nvar a = arr.xstep({x: 2})\n\n// a == 1 3\n//      4 6\n// a.coords.get('y') == 10 12 14\n// a.coords.get('x') == 100 102\n```\n\n#### `array.transpose(p0, p1, ...)` / `array.xtranspose('x','y',...)`\n\nThe transpose/xtranspose functions change the axis order.\nThis has no relevance if you only work with x-prefixed functions since they\nwork directly on axis names.\n\n```js\nvar arr = xndarray([1,2,3,4,5,6], {shape: [2,3], names: ['y','x']})\n\n// arr.transpose(1, 0)\nvar a = arr.xtranspose('x', 'y')\n\n// a == 1 4\n//      2 5\n//      3 6\n// \n// a.names == ['x','y']\n```\n\n#### `array.pick(i,j,...)` / `array.xpick({x: i, y: j, ...})`\n\n```js\nvar arr = xndarray([1,2,3,4,5,6], {\n  shape: [2,3],\n  names: ['y','x'],\n  coords: {\n    y: [10,12,14],\n    x: [100,101,102]\n  }\n})\n\n// arr.pick(null, 1)\nvar a = arr.xpick({x: 1})\n\n// a == 2 5\n// a.dimension == 1\n// a.names == ['y']\n// a.coords.get('y') == 10 12 14\n// a.coords.get('x') == 101\n```\n\nNote that the `x` coordinates get reduced to a single value in the example. \n\n## Acknowledgments\n\nThis library is inspired by the Python packages [PyHRF](http://pyhrf.org) (see [pyhrf.ndarray.xndarray class](http://pyhrf.org/autodoc/pyhrf.ndarray.html#pyhrf.ndarray.xndarray)) and [xarray](http://xarray.pydata.org).\nIt is based on and compatible with the [ndarray][ndarray] JavaScript library.\n\n\n[ndarray]: https://github.com/scijs/ndarray \"ndarray\"\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fletmaik%2Fxndarray","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fletmaik%2Fxndarray","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fletmaik%2Fxndarray/lists"}