Skip to content

Commit e3c3225

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 e3c3225

File tree

6 files changed

+439
-0
lines changed

6 files changed

+439
-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: 130 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,130 @@
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 isComplexDataType = require( '@stdlib/ndarray/base/assert/is-complex-floating-point-data-type' );
25+
var ctorComplex = require( '@stdlib/complex/ctors' );
26+
var max = require( '@stdlib/math/base/special/max' );
27+
var min = require( '@stdlib/math/base/special/min' );
28+
var zeros = require( '@stdlib/ndarray/base/zeros' );
29+
30+
31+
// FUNCTIONS //
32+
33+
/**
34+
* Default buffer setter function.
35+
*
36+
* @private
37+
* @param {ArrayBufferLike} buf - array buffer
38+
* @param {Number} idx - array index
39+
* @param {Number|ComplexLike} v - value
40+
*/
41+
function setter( buf, idx, v ) {
42+
buf[ idx ] = v;
43+
}
44+
45+
46+
// MAIN //
47+
48+
/**
49+
* Creates a two-dimensional array with ones on the kth diagonal and zeros elsewhere.
50+
*
51+
* @param {*} dtype - output array data type
52+
* @param {Number} rows - output array number of rows
53+
* @param {Number} cols - output array number of columns
54+
* @param {Number} k - diagonal index. positive refers to upper diagonal, and negative refers to lower diagonal
55+
* @param {string} order - memory layout (either row-major or column-major)
56+
* @returns {ndarray} ndarray
57+
*
58+
* @example
59+
* var getShape = require( '@stdlib/ndarray/shape' );
60+
* var getDType = require( '@stdlib/ndarray/dtype' );
61+
*
62+
* var x = eye( 'float64', 3, 3, 0, 'row-major');
63+
* // returns <ndarray>
64+
*
65+
* var sh = getShape( x );
66+
* // returns [ 3, 3 ]
67+
*
68+
* var dt = String( getDType( x ) );
69+
* // returns 'float64'
70+
*
71+
* var v = x.get( 0, 0 );
72+
* // returns 1.0
73+
*/
74+
function eye( dtype, rows, cols, k, order ) {
75+
var ctor;
76+
var tmp;
77+
var len;
78+
var buf;
79+
var set;
80+
var sr;
81+
var sc;
82+
var ix;
83+
var dx;
84+
var i;
85+
var v;
86+
var r;
87+
var c;
88+
var x;
89+
90+
x = zeros( dtype, [ rows, cols ], order );
91+
92+
if ( isComplexDataType( x.dtype ) ) {
93+
ctor = ctorComplex( x.dtype );
94+
v = new ctor( 1.0, 0.0 );
95+
} else {
96+
v = 1.0;
97+
}
98+
99+
// TODO: Use better approach instead of allocating a new object
100+
tmp = ndarray2object( x );
101+
if ( tmp.accessorProtocol === true ) {
102+
set = tmp.accessors[1];
103+
} else {
104+
set = setter;
105+
}
106+
107+
if ( k < 0 ) {
108+
r = -k;
109+
c = 0;
110+
} else {
111+
r = 0;
112+
c = k;
113+
}
114+
len = max( 0, min( rows - r, cols - c ) );
115+
116+
sr = x.strides[ 0 ];
117+
sc = x.strides[ 1 ];
118+
ix = x.offset + (r * sr) + (c * sc); // offset + (row * row_stride) + (col * col_stride)
119+
dx = sr + sc;
120+
121+
buf = x.data;
122+
for ( i = 0; i < len; i++ ) {
123+
set( buf, ix, v );
124+
ix += dx;
125+
}
126+
127+
return x;
128+
}
129+
130+
module.exports = eye;

0 commit comments

Comments
 (0)