-
-
Notifications
You must be signed in to change notification settings - Fork 916
Add support for computing the derivative of cardinal sine (cosc) #257
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: develop
Are you sure you want to change the base?
Changes from 11 commits
c94c371
5ea499a
b91e77b
806b7f7
62640bd
97d18ae
ec4b0d9
725c50d
f290a9a
97a13f0
074c112
9dddabc
2a7ead2
d7a8fbc
e46f5bb
e1f162e
39b4271
4a7956b
44c83db
d4110d3
896aa3e
cd20a3e
de907d5
c5c1c6d
34458aa
bc2e756
c25707c
1d48003
315f32c
29979de
d4a71eb
5ffcf6c
e6098ec
c5c77cd
2497ce9
0030253
afcb37d
bf9caf1
9195a5e
0c18983
648ac84
934611d
c965fcb
c4ee758
a68872f
cda14c1
f8ae62b
b5b9382
d531f0c
b936ee5
c4ca79e
a7d4cbe
d6053e3
2b65a31
02bcbf7
3f75a51
ba05453
5b9a8fa
dfe4c8e
5bd0e32
9fe282b
d2de7f8
c1bbb8c
34be402
ecaea40
b4af5f4
182d473
49f9b1c
c0b69cb
86b038c
8223567
0a391ae
293a4ec
267aaa1
dbde994
fefec52
0dad968
1b09094
4326f02
49cf77a
1e96dd7
b4494b2
20f5f4e
d5078db
c1ab594
878fdfc
aca2cb6
e164e81
4feecf1
6303f89
ecf2b5c
67ce2f0
eb2c6e2
c840813
7ecde2e
5cb605d
ad868c8
1c48dc1
2dbf9d4
8318174
82246b2
ba0f85c
8b0bb1e
5d36d03
84a73b9
b4e04ad
c4c512f
c6f9e65
b95d537
c720bd2
d65ab57
9749db1
68c50ed
4321c6b
fccff03
1d03160
de5a18a
e7f6e8d
a5ed4ce
885a2e8
1a400d9
fe2d684
b511a53
883a3dc
8fc2704
df12bff
5ec19d2
17be029
19ebafa
de00626
1d7216c
b396dc2
b5ddcdc
70a9faa
53f1d36
6477d84
8d379cf
c3ca92b
9d2dc74
9effbe6
aee020e
3381152
c8b7445
cf65b8f
03fd943
c0fa3e0
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
| @@ -0,0 +1,102 @@ | ||||||
| <!-- | ||||||
|
|
||||||
| @license Apache-2.0 | ||||||
|
|
||||||
| Copyright (c) 2018 The Stdlib Authors. | ||||||
|
|
||||||
| Licensed under the Apache License, Version 2.0 (the "License"); | ||||||
| you may not use this file except in compliance with the License. | ||||||
| You may obtain a copy of the License at | ||||||
|
|
||||||
| http://www.apache.org/licenses/LICENSE-2.0 | ||||||
|
|
||||||
| Unless required by applicable law or agreed to in writing, software | ||||||
| distributed under the License is distributed on an "AS IS" BASIS, | ||||||
| WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||||||
| See the License for the specific language governing permissions and | ||||||
| limitations under the License. | ||||||
|
|
||||||
| --> | ||||||
|
|
||||||
| # cosc | ||||||
|
|
||||||
| > Compute the derivative of the [cardinal sine][sinc] of a number. | ||||||
|
|
||||||
| <section class="intro"> | ||||||
|
|
||||||
| The derivative of the normalized [cardinal sine][sinc] function is defined as | ||||||
|
||||||
| The derivative of the normalized [cardinal sine][sinc] function is defined as | |
| The derivative of the normalized [cardinal sine][sinc] is defined as |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed
Outdated
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This should be formatted as a TeX equation. We have a specific way of doing this. For an example, see https://raw.githubusercontent.com/stdlib-js/stdlib/develop/lib/node_modules/%40stdlib/math/base/special/acovercos/README.md.
So for this package, we would do
<!-- <equation class="equation" label="eq:cosc" align="center" raw=""\operatorname{cosc}(x) = \begin{cases} \frac{\cos(\pi x)}{x} - \frac{\sin( \pi x)}{\pi x^2} & \textrm{if}\ x \neq 0 \\ 0 & \textrm{if}\ x = 0\end{cases}" alt="Derivative of the normalized cardinal sine."> -->
<div class="equation" align="center" data-raw-text="\operatorname{cosc}(x) = \begin{cases} \frac{\cos(\pi x)}{x} - \frac{\sin( \pi x)}{\pi x^2} & \textrm{if}\ x \neq 0 \\ 0 & \textrm{if}\ x = 0\end{cases}" data-equation="eq:cosc">
<img src="" alt="Derivative of the normalized cardinal sine.">
<br>
</div>
<!-- </equation> -->
If you ever need to quickly preview how a TeX equation will render, you can use codecogs.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed, and added an SVG of the equation.
Outdated
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
| // returns ~-1.273 |
We use // returns immediately after statements in order to perform documentation testing which serves a similar role as unit testing and also helps ensure that our documentation is never out-of-date. See doctest.
So all the // Return commends need to be converted to // returns.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed
Outdated
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
| var x = cosc( -1.2 ); | |
| v = cosc( -1.2 ); |
It is okay to reassign to variables within an example.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed (the linter was killing me here but now it didn't seem to mind anymore :))
Outdated
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
| var y = cosc( 0.0 ); | |
| v = cosc( 0.0 ); |
Outdated
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
| var z = cosc( NaN ); | |
| v = cosc( NaN ); |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,51 @@ | ||
| /** | ||
| * @license Apache-2.0 | ||
| * | ||
| * Copyright (c) 2018 The Stdlib Authors. | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
|
|
||
| 'use strict'; | ||
|
|
||
| // MODULES // | ||
|
|
||
| var bench = require( '@stdlib/bench' ); | ||
| var randu = require( '@stdlib/random/base/randu' ); | ||
| var isnan = require( '@stdlib/math/base/assert/is-nan' ); | ||
| var pkg = require( './../package.json' ).name; | ||
| var cosc = require( './../lib' ); | ||
|
|
||
|
|
||
| // MAIN // | ||
|
|
||
| bench( pkg, function benchmark( b ) { | ||
| var x; | ||
| var y; | ||
| var i; | ||
|
|
||
| b.tic(); | ||
| for ( i = 0; i < b.iterations; i++ ) { | ||
| x = ( randu() * 2.0 ) - 2.0; | ||
| y = cosc( x ); | ||
| if ( isnan( y ) ) { | ||
| b.fail( 'should not return NaN' ); | ||
| } | ||
| } | ||
| b.toc(); | ||
| if ( isnan( y ) ) { | ||
| b.fail( 'should not return NaN' ); | ||
| } | ||
| b.pass( 'benchmark finished' ); | ||
| b.end(); | ||
| }); |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,48 @@ | ||
| /* | ||
| * @license Apache-2.0 | ||
| * | ||
| * Copyright (c) 2019 The Stdlib Authors. | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
|
|
||
| // TypeScript Version: 2.0 | ||
|
|
||
| /** | ||
| * Computes the derivative of the normalized cardinal sine of a number. | ||
| * | ||
| * @param x - input value | ||
| * @returns derivative of cardinal sine | ||
| * | ||
| * @example | ||
| * var v = cosc( 0.5 ); | ||
| * // returns ~-1.273 | ||
| * | ||
| * @example | ||
| * var v = cosc( -1.2 ); | ||
| * // returns ~0.544 | ||
| * | ||
| * @example | ||
| * var v = cosc( 0.0 ); | ||
| * // returns 0.0 | ||
| * | ||
| * @example | ||
| * var v = cosc( NaN ); | ||
| * // returns NaN | ||
| */ | ||
| declare function cosc( x: number ): number; | ||
|
|
||
|
|
||
| // EXPORTS // | ||
|
|
||
| export = cosc; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,44 @@ | ||
| /* | ||
| * @license Apache-2.0 | ||
| * | ||
| * Copyright (c) 2019 The Stdlib Authors. | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
|
|
||
| import cosc = require( './index' ); | ||
|
|
||
|
|
||
| // TESTS // | ||
|
|
||
| // The function returns a number... | ||
| { | ||
| cosc( 8 ); // $ExpectType number | ||
| } | ||
|
|
||
| // The function does not compile if provided a value other than a number... | ||
| { | ||
| cosc( true ); // $ExpectError | ||
| cosc( false ); // $ExpectError | ||
| cosc( null ); // $ExpectError | ||
| cosc( undefined ); // $ExpectError | ||
| cosc( '5' ); // $ExpectError | ||
| cosc( [] ); // $ExpectError | ||
| cosc( {} ); // $ExpectError | ||
| cosc( ( x: number ): number => x ); // $ExpectError | ||
| } | ||
|
|
||
| // The function does not compile if provided insufficient arguments... | ||
| { | ||
| cosc(); // $ExpectError | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,29 @@ | ||
| /** | ||
| * @license Apache-2.0 | ||
| * | ||
| * Copyright (c) 2018 The Stdlib Authors. | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
|
|
||
| 'use strict'; | ||
|
|
||
| var linspace = require( '@stdlib/math/utils/linspace' ); | ||
| var cosc = require( './../lib' ); | ||
|
|
||
| var x = linspace( -5.0, 5.0, 100 ); | ||
| var i; | ||
|
|
||
| for ( i = 0; i < x.length; i++ ) { | ||
| console.log( 'cosc( %d ) = %d', x[ i ], cosc( x[ i ] ) ); | ||
| } |
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
| @@ -0,0 +1,91 @@ | ||||||
| /** | ||||||
| * @license Apache-2.0 | ||||||
| * | ||||||
| * Copyright (c) 2019 The Stdlib Authors. | ||||||
| * | ||||||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||||||
| * you may not use this file except in compliance with the License. | ||||||
| * You may obtain a copy of the License at | ||||||
| * | ||||||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||||||
| * | ||||||
| * Unless required by applicable law or agreed to in writing, software | ||||||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||||||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||||||
| * See the License for the specific language governing permissions and | ||||||
| * limitations under the License. | ||||||
| */ | ||||||
|
|
||||||
| 'use strict'; | ||||||
|
|
||||||
| // MODULES // | ||||||
|
|
||||||
| var sinpi = require('@stdlib/math/base/special/sinpi'); | ||||||
| var cospi = require('@stdlib/math/base/special/cospi'); | ||||||
| var isnan = require('@stdlib/math/base/assert/is-nan'); | ||||||
| var isInfinite = require('@stdlib/math/base/assert/is-infinite'); | ||||||
| var PI = require('@stdlib/constants/math/float64-pi'); | ||||||
|
|
||||||
|
|
||||||
| // MAIN // | ||||||
|
|
||||||
| /** | ||||||
| * Computes the derivative of the normalized cardinal sine of a number. | ||||||
| * | ||||||
| * ## Method | ||||||
| * | ||||||
| * For \\( x \neq 0 \\), the derivative of \operatorname{sinc}(x) is calculated as | ||||||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
| * | ||||||
| * ```tex | ||||||
| * \operatorname{cosc}(x) = \frac{\operatorname{cos}(\pi x)}{x} - \frac{\operatorname{sin}(\pi x)}{\pi x^2} | ||||||
| * ``` | ||||||
| * | ||||||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
| * | ||||||
| * ## Special Cases | ||||||
| * | ||||||
| * ```tex | ||||||
| * \begin{align*} | ||||||
| * \operatorname{cosc}(0) &= 0 & \\ | ||||||
| * \operatorname{cosc}(\infty) &= 0 & \\ | ||||||
| * \operatorname{cosc}(-\infty) &= 0 & \\ | ||||||
| * \operatorname{cosc}(\mathrm{NaN}) &= \mathrm{NaN} | ||||||
| * \end{align*} | ||||||
| * ``` | ||||||
| * | ||||||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
| * | ||||||
| * @param {number} x - input value | ||||||
| * @returns {number} derivative of normalized sinc(x) | ||||||
| * | ||||||
| * @example | ||||||
| * var v = cosc( 0.5 ) | ||||||
| * // return ~-1.273 | ||||||
|
||||||
| * // return ~-1.273 | |
| * // returns ~-1.273 |
s/return/returns/ here and elsewhere.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For all files in this contribution, we need to update the year to
2019, as this is the year the contribution is being made.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed