Skip to content

Commit a85d004

Browse files
committed
Add xlogy function
1 parent 5274ac8 commit a85d004

File tree

13 files changed

+528
-0
lines changed

13 files changed

+528
-0
lines changed
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
# xlogy
2+
3+
> Compute `x * ln(y)` so that the result is `0` if `x = 0`.
4+
5+
6+
<section class="usage">
7+
8+
## Usage
9+
10+
``` javascript
11+
var xlogy = require( '@stdlib/math/base/special/xlogy' );
12+
```
13+
14+
#### xlogy( x, y )
15+
16+
Computes `x * ln(y)` so that the result is `0` if `x = 0`.
17+
18+
``` javascript
19+
var out = xlogy( 3.0, 2.0 );
20+
// returns ~2.079
21+
22+
out = xlogy( 1.5, 5.9 );
23+
// returns ~2.662
24+
25+
out = xlogy( 0.9, 1.0 );
26+
// returns 0.0
27+
28+
out = xlogy( 0.0, -2.0 );
29+
// returns 0.0
30+
31+
out = xlogy( 1.5, NaN );
32+
// returns NaN
33+
34+
out = xlogy( 0.0, NaN );
35+
// returns NaN
36+
37+
out = xlogy( NaN, 2.3 );
38+
// returns NaN
39+
```
40+
41+
</section>
42+
43+
<!-- /.usage -->
44+
45+
46+
<section class="examples">
47+
48+
## Examples
49+
50+
``` javascript
51+
var randu = require( '@stdlib/math/base/random/randu' );
52+
var xlogy = require( '@stdlib/math/base/special/xlogy' );
53+
54+
var x;
55+
var y;
56+
var i;
57+
58+
for ( i = 0; i < 100; i++ ) {
59+
x = randu();
60+
x = x < 0.5 ? 0.0 : x;
61+
y = ( randu() * 20.0 ) - 5.0;
62+
console.log( 'xlogy(%d,%d) = %d', x, y, xlogy( x, y ) );
63+
}
64+
```
65+
66+
</section>
67+
68+
<!-- /.examples -->
69+
70+
71+
<section class="links">
72+
73+
</section>
74+
75+
<!-- /.links -->
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
'use strict';
2+
3+
// MODULES //
4+
5+
var bench = require( '@stdlib/bench' );
6+
var randu = require( '@stdlib/math/base/random/randu' );
7+
var isnan = require( '@stdlib/math/base/assert/is-nan' );
8+
var pkg = require( './../package.json' ).name;
9+
var xlogy = require( './../lib' );
10+
11+
12+
// MAIN //
13+
14+
bench( pkg, function benchmark( b ) {
15+
var out;
16+
var x;
17+
var y;
18+
var i;
19+
20+
b.tic();
21+
for ( i = 0; i < b.iterations; i++ ) {
22+
x = randu() * 10.0;
23+
y = randu() * 10.0;
24+
out = xlogy( x, y );
25+
if ( isnan( out ) ) {
26+
b.fail( 'should not return NaN' );
27+
}
28+
}
29+
b.toc();
30+
if ( isnan( out ) ) {
31+
b.fail( 'should not return NaN' );
32+
}
33+
b.pass( 'benchmark finished' );
34+
b.end();
35+
});
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
2+
{{alias}}( x, y )
3+
Computes `x * ln(y)` so that the result is `0` if `x = 0`.
4+
5+
Parameters
6+
----------
7+
x: number
8+
Input value.
9+
10+
y: number
11+
Input value.
12+
13+
Returns
14+
-------
15+
out: number
16+
Function value.
17+
18+
Examples
19+
--------
20+
> var out = {{alias}}( 3.0, 2.0 )
21+
~2.079
22+
> out = {{alias}}( 1.5, 5.9 )
23+
~2.662
24+
> out = {{alias}}( 0.9, 1.0 )
25+
0.0
26+
> out = {{alias}}( 0.0, -2.0 )
27+
0.0
28+
> out = {{alias}}( 1.5, NaN )
29+
NaN
30+
> out = {{alias}}( 0.0, NaN )
31+
NaN
32+
> out = {{alias}}( NaN, 2.3 )
33+
NaN
34+
35+
See Also
36+
--------
37+
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
'use strict';
2+
3+
var randu = require( '@stdlib/math/base/random/randu' );
4+
var xlogy = require( './../lib' );
5+
6+
var x;
7+
var y;
8+
var i;
9+
10+
for ( i = 0; i < 100; i++ ) {
11+
x = randu();
12+
x = x < 0.5 ? 0.0 : x;
13+
y = ( randu() * 20.0 ) - 5.0;
14+
console.log( 'xlogy(%d,%d) = %d', x, y, xlogy( x, y ) );
15+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
'use strict';
2+
3+
/**
4+
* Compute `x * ln(y)` so that the result is `0` if `x = 0`.
5+
*
6+
* @module @stdlib/math/base/special/xlogy
7+
*
8+
* @example
9+
* var xlogy = require( '@stdlib/math/base/special/xlogy' );
10+
*
11+
* var out = xlogy( 3.0, 2.0 );
12+
* // returns ~2.079
13+
*
14+
* out = xlogy( 1.5, 5.9 );
15+
* // returns ~2.662
16+
*
17+
* out = xlogy( 0.9, 1.0 );
18+
* // returns 0.0
19+
*
20+
* out = xlogy( 0.0, -2.0 );
21+
* // returns 0.0
22+
*
23+
* out = xlogy( 1.5, NaN );
24+
* // returns NaN
25+
*
26+
* out = xlogy( 0.0, NaN );
27+
* // returns NaN
28+
*
29+
* out = xlogy( NaN, 2.3 );
30+
* // returns NaN
31+
*/
32+
33+
// MODULES //
34+
35+
var xlogy = require( './xlogy.js' );
36+
37+
38+
// EXPORTS //
39+
40+
module.exports = xlogy;
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
'use strict';
2+
3+
// MODULES //
4+
5+
var isnan = require( '@stdlib/math/base/assert/is-nan' );
6+
var ln = require( '@stdlib/math/base/special/ln' );
7+
8+
9+
// MAIN //
10+
11+
/**
12+
* Computes `x * ln(y)` so that the result is `0` if `x = 0`.
13+
*
14+
* @param {number} x - input value
15+
* @param {number} y - input value
16+
* @returns {number} function value
17+
*
18+
* @example
19+
* var out = xlogy( 3.0, 2.0 );
20+
* // returns ~2.079
21+
*
22+
* @example
23+
* var out = xlogy( 1.5, 5.9 );
24+
* // returns ~2.662
25+
*
26+
* @example
27+
* var out = xlogy( 0.9, 1.0 );
28+
* // returns 0.0
29+
*
30+
* @example
31+
* var out = xlogy( 0.0, -2.0 );
32+
* // returns 0.0
33+
*
34+
* @example
35+
* var out = xlogy( 1.5, NaN );
36+
* // returns NaN
37+
*
38+
* @example
39+
* var out = xlogy( 0.0, NaN );
40+
* // returns NaN
41+
*
42+
* @example
43+
* var out = xlogy( NaN, 2.3 );
44+
* // returns NaN
45+
*/
46+
function xlogy( x, y ) {
47+
if ( x === 0.0 && !isnan( y ) ) {
48+
return 0.0;
49+
}
50+
return x * ln( y );
51+
} // end FUNCTION xlogy()
52+
53+
54+
// EXPORTS //
55+
56+
module.exports = xlogy;
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
{
2+
"name": "@stdlib/math/base/special/xlogy",
3+
"version": "0.0.0",
4+
"description": "Compute `x * ln(y)` so that the result is `0` if `x = 0`.",
5+
"author": {
6+
"name": "The Stdlib Authors",
7+
"url": "https://github.com/stdlib-js/stdlib/graphs/contributors"
8+
},
9+
"contributors": [
10+
{
11+
"name": "The Stdlib Authors",
12+
"url": "https://github.com/stdlib-js/stdlib/graphs/contributors"
13+
}
14+
],
15+
"scripts": {},
16+
"main": "./lib",
17+
"repository": {
18+
"type": "git",
19+
"url": "git://github.com/stdlib-js/stdlib.git"
20+
},
21+
"homepage": "https://github.com/stdlib-js/stdlib",
22+
"keywords": [
23+
"stdlib",
24+
"stdmath",
25+
"mathematics",
26+
"math",
27+
"natural",
28+
"logarithm",
29+
"log",
30+
"ln",
31+
"special"
32+
],
33+
"bugs": {
34+
"url": "https://github.com/stdlib-js/stdlib/issues"
35+
},
36+
"dependencies": {},
37+
"devDependencies": {},
38+
"engines": {
39+
"node": ">=0.10.0",
40+
"npm": ">2.7.0"
41+
},
42+
"license": "Apache-2.0"
43+
}

lib/node_modules/@stdlib/math/base/special/xlogy/test/fixtures/python/large_large.json

Lines changed: 1 addition & 0 deletions
Large diffs are not rendered by default.

lib/node_modules/@stdlib/math/base/special/xlogy/test/fixtures/python/large_small.json

Lines changed: 1 addition & 0 deletions
Large diffs are not rendered by default.
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
#!/usr/bin/env python
2+
"""Generate fixtures."""
3+
4+
import os
5+
import json
6+
import numpy as np
7+
from scipy.special import xlogy
8+
9+
# Get the file path:
10+
FILE = os.path.realpath(__file__)
11+
12+
# Extract the directory in which this file resides:
13+
DIR = os.path.dirname(FILE)
14+
15+
16+
def gen(x, y, name):
17+
"""Generate fixture data and writes them to file.
18+
19+
# Arguments
20+
21+
* `x`: first parameter
22+
* `y`: second parameter
23+
* `name::str`: output filename
24+
25+
# Examples
26+
27+
``` python
28+
python> x = random(500) * 5.0
29+
python> y = random(500) * 5.0
30+
python> gen(x, y, './data.json')
31+
```
32+
"""
33+
out = xlogy(x,y)
34+
35+
# Store data to be written to file as a dictionary:
36+
data = {
37+
"x": x.tolist(),
38+
"y": y.tolist(),
39+
"expected": out.tolist()
40+
}
41+
42+
# Based on the script directory, create an output filepath:
43+
filepath = os.path.join(DIR, name)
44+
45+
# Write the data to the output filepath as JSON:
46+
with open(filepath, "w") as outfile:
47+
json.dump(data, outfile)
48+
49+
50+
def main():
51+
"""Generate fixture data."""
52+
x = np.random.random(500) * 5.0
53+
y = np.random.random(500) * 5.0
54+
gen(x, y, "small_small.json")
55+
56+
x = np.random.random(500) * 5.0
57+
y = np.random.random(500) * 100.0
58+
gen(x, y, "small_large.json")
59+
60+
x = np.random.random(500) * 100.0
61+
y = np.random.random(500) * 5.0
62+
gen(x, y, "large_small.json")
63+
64+
x = np.random.random(500) * 100.0
65+
y = np.random.random(500) * 100.0
66+
gen(x, y, "large_large.json")
67+
68+
if __name__ == "__main__":
69+
main()

0 commit comments

Comments
 (0)