Skip to content

Commit 149456f

Browse files
committed
Add iterator to compute the inverse coversed sine
1 parent d2ff1e3 commit 149456f

File tree

10 files changed

+920
-0
lines changed

10 files changed

+920
-0
lines changed
Lines changed: 143 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,143 @@
1+
<!--
2+
3+
@license Apache-2.0
4+
5+
Copyright (c) 2020 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+
# iterAcoversin
22+
23+
> Create an [iterator][mdn-iterator-protocol] which iteratively computes the [inverse coversed sine][@stdlib/math/base/special/acoversin].
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 iterAcoversin = require( '@stdlib/math/iter/special/acoversin' );
41+
```
42+
43+
#### iterAcoversin( iterator )
44+
45+
Returns an [iterator][mdn-iterator-protocol] which iteratively computes the [inverse coversed sine][@stdlib/math/base/special/acoversin].
46+
47+
```javascript
48+
var array2iterator = require( '@stdlib/array/to-iterator' );
49+
50+
var x = [ 0.0, 3.141592653589793/2.0, 3.141592653589793/6.0 ];
51+
var it = iterAcoversin( array2iterator( x ) );
52+
// returns <Object>
53+
54+
var r = it.next().value;
55+
// returns ~1.5708
56+
57+
r = it.next().value;
58+
// returns ~-0.6075
59+
60+
r = it.next().value;
61+
// returns ~0.4966
62+
63+
// ...
64+
```
65+
66+
The returned [iterator][mdn-iterator-protocol] protocol-compliant object has the following properties:
67+
68+
- **next**: function which returns an [iterator][mdn-iterator-protocol] protocol-compliant object containing the next iterated value (if one exists) assigned to a `value` property and a `done` property having a `boolean` value indicating whether the iterator is finished.
69+
- **return**: function which closes an [iterator][mdn-iterator-protocol] and returns a single (optional) argument in an [iterator][mdn-iterator-protocol] protocol-compliant object.
70+
71+
</section>
72+
73+
<!-- /.usage -->
74+
75+
<!-- Package usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
76+
77+
<section class="notes">
78+
79+
## Notes
80+
81+
- The domain of inverse coverse sine is restricted to `[-2,0]`. If an iterated value is outside of the domain, the returned [iterator][mdn-iterator-protocol] returns `NaN`.
82+
- If an iterated value is non-numeric (including `NaN`), the returned [iterator][mdn-iterator-protocol] returns `NaN`. If non-numeric iterated values are possible, you are advised to provide an [`iterator`][mdn-iterator-protocol] which type checks and handles non-numeric values accordingly.
83+
- If an environment supports `Symbol.iterator` **and** a provided [iterator][mdn-iterator-protocol] is iterable, the returned [iterator][mdn-iterator-protocol] is iterable.
84+
85+
</section>
86+
87+
<!-- /.notes -->
88+
89+
<!-- Package usage examples. -->
90+
91+
<section class="examples">
92+
93+
## Examples
94+
95+
<!-- eslint no-undef: "error" -->
96+
97+
```javascript
98+
var uniform = require( '@stdlib/random/iter/uniform' );
99+
var iterAcoversin = require( '@stdlib/math/iter/special/acoversin' );
100+
101+
// Create a seeded iterator for generating pseudorandom numbers:
102+
var rand = uniform( 0.0, 2.0, {
103+
'seed': 1234,
104+
'iter': 10
105+
});
106+
107+
// Create an iterator which consumes the pseudorandom number iterator:
108+
var it = iterAcoversin( rand );
109+
110+
// Perform manual iteration...
111+
var r;
112+
while ( true ) {
113+
r = it.next();
114+
if ( r.done ) {
115+
break;
116+
}
117+
console.log( r.value );
118+
}
119+
```
120+
121+
</section>
122+
123+
<!-- /.examples -->
124+
125+
<!-- 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. -->
126+
127+
<section class="references">
128+
129+
</section>
130+
131+
<!-- /.references -->
132+
133+
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
134+
135+
<section class="links">
136+
137+
[mdn-iterator-protocol]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Iteration_protocols#The_iterator_protocol
138+
139+
[@stdlib/math/base/special/acoversin]: https://github.com/stdlib-js/stdlib
140+
141+
</section>
142+
143+
<!-- /.links -->
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2020 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 uniform = require( '@stdlib/random/iter/uniform' );
25+
var isnan = require( '@stdlib/math/base/assert/is-nan' );
26+
var isIteratorLike = require( '@stdlib/assert/is-iterator-like' );
27+
var pkg = require( './../package.json' ).name;
28+
var iterAcoversin = require( './../lib' );
29+
30+
31+
// MAIN //
32+
33+
bench( pkg, function benchmark( b ) {
34+
var rand;
35+
var iter;
36+
var i;
37+
38+
rand = uniform( 0.0, 2.0 );
39+
40+
b.tic();
41+
for ( i = 0; i < b.iterations; i++ ) {
42+
iter = iterAcoversin( rand );
43+
if ( typeof iter !== 'object' ) {
44+
b.fail( 'should return an object' );
45+
}
46+
}
47+
b.toc();
48+
if ( !isIteratorLike( iter ) ) {
49+
b.fail( 'should return an iterator protocol-compliant object' );
50+
}
51+
b.pass( 'benchmark finished' );
52+
b.end();
53+
});
54+
55+
bench( pkg+'::iteration', function benchmark( b ) {
56+
var rand;
57+
var iter;
58+
var z;
59+
var i;
60+
61+
rand = uniform( 0.0, 2.0 );
62+
iter = iterAcoversin( rand );
63+
64+
b.tic();
65+
for ( i = 0; i < b.iterations; i++ ) {
66+
z = iter.next().value;
67+
if ( isnan( z ) ) {
68+
b.fail( 'should not return NaN' );
69+
}
70+
}
71+
b.toc();
72+
if ( isnan( z ) ) {
73+
b.fail( 'should not return NaN' );
74+
}
75+
b.pass( 'benchmark finished' );
76+
b.end();
77+
});
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
2+
{{alias}}( iterator )
3+
Returns an iterator which iteratively computes the inverse coversed sine.
4+
5+
The domain of inverse coversed sine is restricted to [0,2]. If an
6+
iterated value is outside of the domain, the returned iterator returns
7+
`NaN`.
8+
9+
If an environment supports Symbol.iterator and a provided iterator is
10+
iterable, the returned iterator is iterable.
11+
12+
Parameters
13+
----------
14+
iterator: Object
15+
Input iterator.
16+
17+
Returns
18+
-------
19+
iterator: Object
20+
Iterator.
21+
22+
iterator.next(): Function
23+
Returns an iterator protocol-compliant object containing the next
24+
iterated value (if one exists) and a boolean flag indicating whether the
25+
iterator is finished.
26+
27+
iterator.return( [value] ): Function
28+
Finishes an iterator and returns a provided value.
29+
30+
Examples
31+
--------
32+
> var it = {{alias}}( {{alias:@stdlib/random/iter/uniform}}( -2.0, 0.0 ) );
33+
> var r = it.next().value
34+
<number>
35+
> r = it.next().value
36+
<number>
37+
38+
See Also
39+
--------
40+
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
/*
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2020 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+
// TypeScript Version: 2.0
20+
21+
/// <reference types="@stdlib/types"/>
22+
23+
import { Iterator as Iter, IterableIterator } from '@stdlib/types/iter';
24+
25+
// Define a union type representing both iterable and non-iterable iterators:
26+
type Iterator = Iter | IterableIterator;
27+
28+
/**
29+
* Returns an iterator which iteratively computes the inverse coversed sine.
30+
*
31+
* ## Notes
32+
*
33+
* - The domain of inversed coversed sine is restricted to `[0,2]`. If an iterated value is outside of the domain, the returned iterator returns `NaN`.
34+
* - If an environment supports `Symbol.iterator` **and** a provided iterator is iterable, the returned iterator is iterable.
35+
*
36+
* @param iterator - input iterator
37+
* @returns iterator
38+
*
39+
* @example
40+
* var uniform = require( `@stdlib/random/iter/uniform` );
41+
*
42+
* var iter = iterAcoversin( uniform( 0.0, 2.0 ) );
43+
*
44+
* var r = iter.next().value;
45+
* // returns <number>
46+
*
47+
* r = iter.next().value;
48+
* // returns <number>
49+
*
50+
* r = iter.next().value;
51+
* // returns <number>
52+
*
53+
* // ...
54+
*/
55+
declare function iterAcoversin( iterator: Iterator ): Iterator;
56+
57+
58+
// EXPORTS //
59+
60+
export = iterAcoversin;
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
/*
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2020 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+
import iterAcoversin = require( './index' );
20+
21+
/**
22+
* Returns an iterator protocol-compliant object.
23+
*
24+
* @returns iterator protocol-compliant object
25+
*/
26+
function iterator() {
27+
return {
28+
'next': next
29+
};
30+
31+
/**
32+
* Implements the iterator protocol `next` method.
33+
*
34+
* @returns iterator protocol-compliant object
35+
*/
36+
function next() {
37+
return {
38+
'value': true,
39+
'done': false
40+
};
41+
}
42+
}
43+
44+
45+
// TESTS //
46+
47+
// The function returns an iterator...
48+
{
49+
iterAcoversin( iterator() ); // $ExpectType Iterator
50+
}
51+
52+
// The compiler throws an error if the function is provided a first argument which is not an iterator protocol-compliant object...
53+
{
54+
iterAcoversin( '5' ); // $ExpectError
55+
iterAcoversin( 5 ); // $ExpectError
56+
iterAcoversin( true ); // $ExpectError
57+
iterAcoversin( false ); // $ExpectError
58+
iterAcoversin( null ); // $ExpectError
59+
iterAcoversin( undefined ); // $ExpectError
60+
iterAcoversin( [] ); // $ExpectError
61+
iterAcoversin( {} ); // $ExpectError
62+
iterAcoversin( ( x: number ): number => x ); // $ExpectError
63+
}
64+
65+
// The compiler throws an error if the function is provided insufficient arguments...
66+
{
67+
iterAcoversin(); // $ExpectError
68+
}

0 commit comments

Comments
 (0)