Skip to content

Commit 3743809

Browse files
committed
feat: add initial implementation
--- type: pre_commit_static_analysis_report description: Results of running static analysis checks when committing changes. report: - task: lint_filenames status: passed - task: lint_editorconfig status: passed - task: lint_markdown status: na - task: lint_package_json status: na - task: lint_repl_help status: na - task: lint_javascript_src status: na - task: lint_javascript_cli status: na - task: lint_javascript_examples status: passed - task: lint_javascript_tests status: na - task: lint_javascript_benchmarks status: na - task: lint_python status: na - task: lint_r status: na - task: lint_c_src status: na - task: lint_c_examples status: na - task: lint_c_benchmarks status: na - task: lint_c_tests_fixtures status: na - task: lint_shell status: na - task: lint_typescript_declarations status: passed - task: lint_typescript_tests status: na - task: lint_license_headers status: passed ---
1 parent a410628 commit 3743809

File tree

6 files changed

+441
-0
lines changed

6 files changed

+441
-0
lines changed
Lines changed: 137 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,137 @@
1+
<!--
2+
3+
@license Apache-2.0
4+
5+
Copyright (c) 2026 The Stdlib Authors.
6+
7+
Licensed under the Apache License, Version 2.0 (the "License");
8+
you may not use this file except in compliance with the License.
9+
You may obtain a copy of the License at
10+
11+
http://www.apache.org/licenses/LICENSE-2.0
12+
13+
Unless required by applicable law or agreed to in writing, software
14+
distributed under the License is distributed on an "AS IS" BASIS,
15+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16+
See the License for the specific language governing permissions and
17+
limitations under the License.
18+
19+
-->
20+
21+
# eye
22+
23+
> Create a two-dimensional array with ones on the kth diagonal and zeros elsewhere
24+
25+
<!-- Section to include introductory text. Make sure to keep an empty line after the intro `section` element and another before the `/section` close. -->
26+
27+
<section class="intro">
28+
29+
</section>
30+
31+
<!-- /.intro -->
32+
33+
<!-- Package usage documentation. -->
34+
35+
<section class="usage">
36+
37+
## Usage
38+
39+
```javascript
40+
var eye = require( '@stdlib/ndarray/base/eye' );
41+
```
42+
43+
#### eye( dtype, rows, cols, k, order )
44+
45+
Creates a two-dimensional array with ones on the kth diagonal and zeros elsewhere.
46+
47+
```javascript
48+
var getShape = require( '@stdlib/ndarray/shape' );
49+
var getDType = require( '@stdlib/ndarray/dtype' );
50+
51+
var x = eye( 'float64', 3, 3, 0, 'row-major');
52+
// returns <ndarray>
53+
54+
var sh = getShape( x );
55+
// returns [ 3, 3 ]
56+
57+
var dt = String( getDType( x ) );
58+
// returns 'float64'
59+
60+
var v = x.get( 0, 0 );
61+
// returns 1.0
62+
```
63+
64+
The function accepts the following arguments:
65+
66+
- **dtype**: underlying [data type][@stdlib/ndarray/dtypes].
67+
- **rows**: number of rows.
68+
- **cols**: number of columns.
69+
- **k**: diagonal index. positive refers to upper diagonal, and negative refers to lower diagonal.
70+
- **order**: specifies whether an [ndarray][@stdlib/ndarray/base/ctor] is `'row-major'` (C-style) or `'column-major'` (Fortran-style).
71+
72+
73+
</section>
74+
75+
<!-- /.usage -->
76+
77+
<!-- Package usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
78+
79+
<section class="notes">
80+
81+
## Notes
82+
83+
84+
</section>
85+
86+
<!-- /.notes -->
87+
88+
<!-- Package usage examples. -->
89+
90+
<section class="examples">
91+
92+
## Examples
93+
94+
<!-- eslint no-undef: "error" -->
95+
96+
```javascript
97+
var eye = require( '@stdlib/ndarray/base/eye' );
98+
var ndarray2array = require( '@stdlib/ndarray/to-array' );
99+
100+
var x = eye( 'float64', 3, 3, 1, 'row-major');
101+
console.log( ndarray2array( x ) );
102+
103+
x = eye( 'complex64', 3, 3, 1, 'row-major');
104+
console.log( ndarray2array( x ) );
105+
```
106+
107+
</section>
108+
109+
<!-- /.examples -->
110+
111+
<!-- Section to include cited references. If references are included, add a horizontal rule *before* the section. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
112+
113+
<section class="references">
114+
115+
</section>
116+
117+
<!-- /.references -->
118+
119+
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
120+
121+
<section class="related">
122+
123+
</section>
124+
125+
<!-- /.related -->
126+
127+
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
128+
129+
<section class="links">
130+
131+
[@stdlib/ndarray/base/ctor]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/ndarray/base/ctor
132+
133+
[@stdlib/ndarray/dtypes]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/ndarray/dtypes
134+
135+
</section>
136+
137+
<!-- /.links -->
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2026 The Stdlib Authors.
5+
*
6+
* Licensed under the Apache License, Version 2.0 (the "License");
7+
* you may not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
19+
'use strict';
20+
21+
var ndarray2array = require( '@stdlib/ndarray/to-array' );
22+
var eye = require( './../lib' );
23+
24+
var x = eye( 'float64', 3, 3, 0, 'row-major');
25+
console.log( ndarray2array( x ) );
26+
27+
x = eye( 'complex64', 3, 3, 0, 'row-major');
28+
console.log( ndarray2array( x ) );
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2026 The Stdlib Authors.
5+
*
6+
* Licensed under the Apache License, Version 2.0 (the "License");
7+
* you may not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
19+
'use strict';
20+
21+
/**
22+
* Create a two-dimensional array with ones on the kth diagonal and zeros elsewhere.
23+
*
24+
* @module @stdlib/ndarray/base/eye
25+
*
26+
* @example
27+
* var getShape = require( '@stdlib/ndarray/shape' );
28+
* var getDType = require( '@stdlib/ndarray/dtype' );
29+
* var eye = require( '@stdlib/ndarray/base/eye' );
30+
*
31+
* var x = eye( 'float64', 3, 3, 0, 'row-major');
32+
* // returns <ndarray>
33+
*
34+
* var sh = getShape( x );
35+
* // returns [ 3, 3 ]
36+
*
37+
* var dt = String( getDType( x ) );
38+
* // returns 'float64'
39+
*
40+
* var v = x.get( 0, 0 );
41+
* // returns 1.0
42+
*/
43+
44+
// MODULES //
45+
46+
var main = require( './main.js' );
47+
48+
49+
// EXPORTS //
50+
51+
module.exports = main;
Lines changed: 132 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,132 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2026 The Stdlib Authors.
5+
*
6+
* Licensed under the Apache License, Version 2.0 (the "License");
7+
* you may not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
19+
'use strict';
20+
21+
// MODULES //
22+
23+
var ndarray2object = require( '@stdlib/ndarray/base/ndarraylike2object' );
24+
var resolveStr = require( '@stdlib/ndarray/base/dtype-resolve-str' );
25+
var isComplexDataType = require( '@stdlib/ndarray/base/assert/is-complex-floating-point-data-type' );
26+
var ctorComplex = require( '@stdlib/complex/ctors' );
27+
var max = require( '@stdlib/math/base/special/max' );
28+
var min = require( '@stdlib/math/base/special/min' );
29+
var zeros = require( '@stdlib/ndarray/base/zeros' );
30+
31+
32+
// FUNCTIONS //
33+
34+
/**
35+
* Default buffer setter function.
36+
*
37+
* @private
38+
* @param {ArrayBufferLike} buf - array buffer
39+
* @param {Number} idx - array index
40+
* @param {Number|ComplexLike} v - value
41+
*/
42+
function setter( buf, idx, v ) {
43+
buf[ idx ] = v;
44+
}
45+
46+
47+
// MAIN //
48+
49+
/**
50+
* Creates a two-dimensional array with ones on the kth diagonal and zeros elsewhere.
51+
*
52+
* @param {*} dtype - output array data type
53+
* @param {Number} rows - output array number of rows
54+
* @param {Number} cols - output array number of columns
55+
* @param {Number} k - diagonal index. positive refers to upper diagonal, and negative refers to lower diagonal
56+
* @param {string} order - memory layout (either row-major or column-major)
57+
* @returns {ndarray} ndarray
58+
*
59+
* @example
60+
* var getShape = require( '@stdlib/ndarray/shape' );
61+
* var getDType = require( '@stdlib/ndarray/dtype' );
62+
*
63+
* var x = eye( 'float64', 3, 3, 0, 'row-major');
64+
* // returns <ndarray>
65+
*
66+
* var sh = getShape( x );
67+
* // returns [ 3, 3 ]
68+
*
69+
* var dt = String( getDType( x ) );
70+
* // returns 'float64'
71+
*
72+
* var v = x.get( 0, 0 );
73+
* // returns 1.0
74+
*/
75+
function eye( dtype, rows, cols, k, order ) {
76+
var ctor;
77+
var tmp;
78+
var len;
79+
var buf;
80+
var set;
81+
var sr;
82+
var sc;
83+
var ix;
84+
var dx;
85+
var i;
86+
var v;
87+
var r;
88+
var c;
89+
var x;
90+
91+
x = zeros( dtype, [ rows, cols ], order );
92+
93+
dtype = resolveStr( dtype );
94+
if ( isComplexDataType( dtype ) ) {
95+
ctor = ctorComplex( dtype );
96+
v = new ctor( 1.0, 0.0 );
97+
} else {
98+
v = 1.0;
99+
}
100+
101+
// TODO: Use better approach instead of allocating a new object
102+
tmp = ndarray2object( x );
103+
if ( tmp.accessorProtocol === true ) {
104+
set = tmp.accessors[1];
105+
} else {
106+
set = setter;
107+
}
108+
109+
if ( k < 0 ) {
110+
r = -k;
111+
c = 0;
112+
} else {
113+
r = 0;
114+
c = k;
115+
}
116+
len = max( 0, min( rows - r, cols - c ) );
117+
118+
sr = x.strides[ 0 ];
119+
sc = x.strides[ 1 ];
120+
ix = x.offset + (r * sr) + (c * sc); // offset + (row * row_stride) + (col * col_stride)
121+
dx = sr + sc;
122+
123+
buf = x.data;
124+
for ( i = 0; i < len; i++ ) {
125+
set( buf, ix, v );
126+
ix += dx;
127+
}
128+
129+
return x;
130+
}
131+
132+
module.exports = eye;

0 commit comments

Comments
 (0)