Skip to content

Commit 2501de9

Browse files
committed
feat: add ndarray/vector/float64
--- 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: passed - task: lint_package_json status: passed - task: lint_repl_help status: passed - task: lint_javascript_src status: passed - task: lint_javascript_cli status: na - task: lint_javascript_examples status: passed - task: lint_javascript_tests status: passed - task: lint_javascript_benchmarks status: passed - 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: passed - task: lint_license_headers status: passed ---
1 parent f75732b commit 2501de9

File tree

11 files changed

+2312
-0
lines changed

11 files changed

+2312
-0
lines changed
Lines changed: 196 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,196 @@
1+
<!--
2+
3+
@license Apache-2.0
4+
5+
Copyright (c) 2025 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+
# Float64Vector
22+
23+
> Create a double-precision floating-point vector (i.e., a one-dimensional [ndarray][@stdlib/ndarray/ctor]).
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 Float64Vector = require( '@stdlib/ndarray/vector/float64' );
41+
```
42+
43+
#### Float64Vector( \[options] )
44+
45+
Returns a one-dimensional double-precision floating-point [ndarray][@stdlib/ndarray/ctor].
46+
47+
```javascript
48+
var numel = require( '@stdlib/ndarray/numel' );
49+
50+
var arr = new Float64Vector();
51+
// returns <ndarray>
52+
53+
var len = numel( arr );
54+
// returns 0
55+
```
56+
57+
The function accepts the following options:
58+
59+
- **order**: specifies whether an [ndarray][@stdlib/ndarray/ctor] is `'row-major'` (C-style) or `'column-major'` (Fortran-style). Default: `'row-major'`.
60+
- **mode**: specifies how to handle indices which exceed array dimensions (see [`ndarray`][@stdlib/ndarray/ctor]). Default: `'throw'`.
61+
- **readonly**: boolean indicating whether an array should be **read-only**. Default: `false`.
62+
63+
#### Float64Vector( length\[, options] )
64+
65+
Returns a one-dimensional double-precision floating-point [ndarray][@stdlib/ndarray/ctor] having a specified `length`.
66+
67+
```javascript
68+
var numel = require( '@stdlib/ndarray/numel' );
69+
70+
var arr = new Float64Vector( 5 );
71+
// returns <ndarray>
72+
73+
var len1 = numel( arr );
74+
// returns 5
75+
```
76+
77+
#### Float64Vector( obj\[, options] )
78+
79+
Creates a one-dimensional double-precision floating-point [ndarray][@stdlib/ndarray/ctor] from an array-like object or iterable.
80+
81+
```javascript
82+
var numel = require( '@stdlib/ndarray/numel' );
83+
84+
var arr = new Float64Vector( [ 1.0, 2.0, 3.0 ] );
85+
// returns <ndarray>
86+
87+
var len1 = numel( arr );
88+
// returns 3
89+
```
90+
91+
#### Float64Vector( buffer\[, byteOffset\[, length]]\[, options] )
92+
93+
Returns a one-dimensional double-precision floating-point [ndarray][@stdlib/ndarray/ctor] view of an [`ArrayBuffer`][@stdlib/array/buffer].
94+
95+
```javascript
96+
var ArrayBuffer = require( '@stdlib/array/buffer' );
97+
var numel = require( '@stdlib/ndarray/numel' );
98+
99+
var buf = new ArrayBuffer( 64 );
100+
101+
var arr1 = new Float64Vector( buf );
102+
// returns <ndarray>
103+
104+
var len1 = numel( arr1 );
105+
// returns 8
106+
107+
var arr2 = new Float64Vector( buf, 16 );
108+
// returns <ndarray>
109+
110+
var len2 = numel( arr2 );
111+
// returns 6
112+
113+
var arr3 = new Float64Vector( buf, 16, 1 );
114+
// returns <ndarray>
115+
116+
var len3 = numel( arr3 );
117+
// returns 1
118+
```
119+
120+
</section>
121+
122+
<!-- /.usage -->
123+
124+
<!-- Package usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
125+
126+
<section class="notes">
127+
128+
</section>
129+
130+
<!-- /.notes -->
131+
132+
<!-- Package usage examples. -->
133+
134+
<section class="examples">
135+
136+
## Examples
137+
138+
<!-- eslint no-undef: "error" -->
139+
140+
```javascript
141+
var discreteUniform = require( '@stdlib/random/array/discrete-uniform' );
142+
var sum = require( '@stdlib/blas/ext/sum' );
143+
var map = require( '@stdlib/ndarray/map' );
144+
var Float64Vector = require( '@stdlib/ndarray/vector/float64' );
145+
146+
// Create a vector containing random values:
147+
var x = new Float64Vector( discreteUniform( 10, 0, 100 ) );
148+
149+
// Compute the sum:
150+
var v = sum( x );
151+
console.log( v.get() );
152+
153+
// Define a function which applies a threshold to individual values:
154+
function threshold( v ) {
155+
return ( v > 10 ) ? v : 0;
156+
}
157+
158+
// Apply threshold:
159+
var y = map( x, threshold );
160+
161+
// Recompute the sum:
162+
v = sum( y );
163+
console.log( v.get() );
164+
```
165+
166+
</section>
167+
168+
<!-- /.examples -->
169+
170+
<!-- 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. -->
171+
172+
<section class="references">
173+
174+
</section>
175+
176+
<!-- /.references -->
177+
178+
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
179+
180+
<section class="related">
181+
182+
</section>
183+
184+
<!-- /.related -->
185+
186+
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
187+
188+
<section class="links">
189+
190+
[@stdlib/array/buffer]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/array/buffer
191+
192+
[@stdlib/ndarray/ctor]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/ndarray/ctor
193+
194+
</section>
195+
196+
<!-- /.links -->
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2025 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 bench = require( '@stdlib/bench' );
24+
var isndarrayLike = require( '@stdlib/assert/is-ndarray-like' );
25+
var pkg = require( './../package.json' ).name;
26+
var Float64Vector = require( './../lib' );
27+
28+
29+
// MAIN //
30+
31+
bench( pkg, function benchmark( b ) {
32+
var arr;
33+
var i;
34+
b.tic();
35+
for ( i = 0; i < b.iterations; i++ ) {
36+
arr = new Float64Vector( 0 );
37+
if ( arr.length !== 0 ) {
38+
b.fail( 'should have length 0' );
39+
}
40+
}
41+
b.toc();
42+
if ( !isndarrayLike( arr ) ) {
43+
b.fail( 'should return an ndarray' );
44+
}
45+
b.pass( 'benchmark finished' );
46+
b.end();
47+
});
Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2025 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 bench = require( '@stdlib/bench' );
24+
var pow = require( '@stdlib/math/base/special/pow' );
25+
var isndarrayLike = require( '@stdlib/assert/is-ndarray-like' );
26+
var pkg = require( './../package.json' ).name;
27+
var Float64Vector = require( './../lib' );
28+
29+
30+
// FUNCTIONS //
31+
32+
/**
33+
* Creates a benchmark function.
34+
*
35+
* @private
36+
* @param {PositiveInteger} len - array length
37+
* @returns {Function} benchmark function
38+
*/
39+
function createBenchmark( len ) {
40+
return benchmark;
41+
42+
/**
43+
* Benchmark function.
44+
*
45+
* @private
46+
* @param {Benchmark} b - benchmark instance
47+
*/
48+
function benchmark( b ) {
49+
var arr;
50+
var i;
51+
52+
b.tic();
53+
for ( i = 0; i < b.iterations; i++ ) {
54+
arr = new Float64Vector( len );
55+
if ( arr.length !== len ) {
56+
b.fail( 'unexpected length' );
57+
}
58+
}
59+
b.toc();
60+
if ( !isndarrayLike( arr ) ) {
61+
b.fail( 'should return an ndarray' );
62+
}
63+
b.pass( 'benchmark finished' );
64+
b.end();
65+
}
66+
}
67+
68+
69+
// MAIN //
70+
71+
/**
72+
* Main execution sequence.
73+
*
74+
* @private
75+
*/
76+
function main() {
77+
var len;
78+
var min;
79+
var max;
80+
var f;
81+
var i;
82+
83+
min = 1; // 10^min
84+
max = 6; // 10^max
85+
86+
for ( i = min; i <= max; i++ ) {
87+
len = pow( 10, i );
88+
f = createBenchmark( len );
89+
bench( pkg+':size='+len, f );
90+
}
91+
}
92+
93+
main();

0 commit comments

Comments
 (0)