Skip to content

Commit f890c8f

Browse files
committed
Add utility to cast a function's return value to a complex number
1 parent b56f567 commit f890c8f

File tree

10 files changed

+1819
-0
lines changed

10 files changed

+1819
-0
lines changed
Lines changed: 136 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,136 @@
1+
<!--
2+
3+
@license Apache-2.0
4+
5+
Copyright (c) 2022 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+
# castReturn
22+
23+
> Wrap a function and cast a function's return value to a complex number.
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 castReturn = require( '@stdlib/complex/base/cast-return' );
41+
```
42+
43+
#### castReturn( fcn, nargs, ctor )
44+
45+
Returns a function which wraps a function and casts a function's return value to a complex number.
46+
47+
```javascript
48+
var Complex64 = require( '@stdlib/complex/float32' );
49+
var addf = require( '@stdlib/math/base/ops/addf' );
50+
51+
var f = castReturn( addf, 2, Complex64 );
52+
// returns <Function>
53+
```
54+
55+
The function accepts the following arguments:
56+
57+
- **fcn**: the function to wrap.
58+
- **nargs**: the number of arguments to be provided to the wrapped function.
59+
- **ctor**: complex number constructor for converting real numbers to complex numbers.
60+
61+
</section>
62+
63+
<!-- /.usage -->
64+
65+
<!-- Package usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
66+
67+
<section class="notes">
68+
69+
## Notes
70+
71+
- The returned function **assumes** that the wrapped function returns either a real or complex number.
72+
- The returned function **assumes** that, if a return value is non-numeric (i.e., not of type `number`), then the return value is a complex number. The returned function does **not** verify that non-numeric return values are, in fact, complex number objects. The returned function returns non-numeric return values from the wrapped function without modification.
73+
74+
</section>
75+
76+
<!-- /.notes -->
77+
78+
<!-- Package usage examples. -->
79+
80+
<section class="examples">
81+
82+
## Examples
83+
84+
<!-- eslint no-undef: "error" -->
85+
86+
```javascript
87+
var Complex64 = require( '@stdlib/complex/float32' );
88+
var addf = require( '@stdlib/math/base/ops/addf' );
89+
var realf = require( '@stdlib/complex/realf' );
90+
var imagf = require( '@stdlib/complex/imagf' );
91+
var castReturn = require( '@stdlib/complex/base/cast-return' );
92+
93+
var f = castReturn( addf, 2, Complex64 );
94+
95+
// ...
96+
97+
var z = f( 3.0, 4.0 );
98+
// returns <Complex64>
99+
100+
var re = realf( z );
101+
// returns 7.0
102+
103+
var im = imagf( z );
104+
// returns 0.0
105+
106+
console.log( '%d + %di', re, im );
107+
// => '7 + 0i'
108+
```
109+
110+
</section>
111+
112+
<!-- /.examples -->
113+
114+
<!-- 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. -->
115+
116+
<section class="references">
117+
118+
</section>
119+
120+
<!-- /.references -->
121+
122+
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
123+
124+
<section class="related">
125+
126+
</section>
127+
128+
<!-- /.related -->
129+
130+
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
131+
132+
<section class="links">
133+
134+
</section>
135+
136+
<!-- /.links -->

0 commit comments

Comments
 (0)