Skip to content

Commit

Permalink
feat: #155 math related functions
Browse files Browse the repository at this point in the history
  • Loading branch information
Soontao committed Nov 9, 2020
1 parent 34aae2e commit e5a4469
Show file tree
Hide file tree
Showing 6 changed files with 116 additions and 0 deletions.
16 changes: 16 additions & 0 deletions src/math/mean.ts
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;
12 changes: 12 additions & 0 deletions src/math/stdDeviation.ts
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));
}
13 changes: 13 additions & 0 deletions src/math/sum.ts
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;
23 changes: 23 additions & 0 deletions src/math/util.ts
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`);
}
});
}
16 changes: 16 additions & 0 deletions src/math/variance.ts
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;
36 changes: 36 additions & 0 deletions test/math.test.ts
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);

});


});

0 comments on commit e5a4469

Please sign in to comment.