-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
116 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import { sum } from './sum'; | ||
|
||
|
||
/** | ||
* | ||
* Mean (avg) values | ||
* @since 5.18.0 | ||
* @category Math | ||
* @param values | ||
* | ||
*/ | ||
export function mean(values: Float64Array) { | ||
return sum(values) / values.length; | ||
} | ||
|
||
export default mean; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
import variance from './variance'; | ||
|
||
|
||
/** | ||
* Standard Deviation | ||
* @since 5.18.0 | ||
* @category Math | ||
* @param values | ||
*/ | ||
export function stdDeviation(values: Float64Array): number { | ||
return Math.sqrt(variance(values)); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
|
||
|
||
/** | ||
* Math sum | ||
* @category Math | ||
* @since 5.18.0 | ||
* @param values | ||
*/ | ||
export function sum(values: Float64Array): number { | ||
return values.reduce((pre, value) => pre + value, 0); | ||
} | ||
|
||
export default sum; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
import forEach from '../forEach'; | ||
|
||
|
||
/** | ||
* check array all are numbers | ||
* | ||
* @private | ||
* @internal | ||
* @ignore | ||
* @since 5.18.0 | ||
* @param values | ||
*/ | ||
export function checkArray(values: ArrayLike<any>) { | ||
forEach(values, (value, idx) => { | ||
const tValue = typeof value; | ||
if (tValue !== 'number' && tValue !== 'bigint') { | ||
throw new TypeError(`array[${idx}] is ${tValue}, all items must be number`); | ||
} | ||
if (value === NaN) { | ||
throw new TypeError(`array[${idx}] is NaN, all items must be number`); | ||
} | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import mean from './mean'; | ||
import sum from './sum'; | ||
|
||
/** | ||
* Variance | ||
* @since 5.18.0 | ||
* @category Math | ||
* | ||
* @param values | ||
*/ | ||
export function variance(values: Float64Array): number { | ||
const mValue = mean(values); | ||
return sum(values.map((value) => (value - mValue) ** 2)) / values.length; | ||
} | ||
|
||
export default variance; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
import { mean } from '../src/math/mean'; | ||
import { stdDeviation } from '../src/math/stdDeviation'; | ||
import { sum } from '../src/math/sum'; | ||
import { variance } from '../src/math/variance'; | ||
|
||
|
||
describe('math', () => { | ||
|
||
it('should support sum', () => { | ||
|
||
expect(sum(Float64Array.from([1, 23, 4]))).toBe(28); | ||
expect(sum(Float64Array.from([1.2, 3.1, 0.3]))).toBe(4.6); | ||
|
||
}); | ||
|
||
it('should support mean', () => { | ||
|
||
expect(mean(Float64Array.from([1, 7, 4]))).toBe(4); | ||
expect(mean(Float64Array.from([1.2, 3, 0.3]))).toBe(1.5); | ||
|
||
}); | ||
|
||
it('should support variance', () => { | ||
|
||
expect(variance(Float64Array.from([600, 470, 170, 430, 300]))).toBe(21704); | ||
|
||
}); | ||
|
||
it('should support standard deviation', () => { | ||
|
||
expect(stdDeviation(Float64Array.from([600, 470, 170, 430, 300]))).toBe(147.32277488562318); | ||
|
||
}); | ||
|
||
|
||
}); |