Skip to content

Commit

Permalink
Smath v1.5.1 (#91)
Browse files Browse the repository at this point in the history
+stdevp
+stdevs
  • Loading branch information
nicfv authored Mar 26, 2024
1 parent 0c7c67e commit f5f5c52
Show file tree
Hide file tree
Showing 7 changed files with 48 additions and 12 deletions.
2 changes: 1 addition & 1 deletion datafit/src/test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ X.eq(data[8].y, 7);
// Values should be within 20%
// of actual, but this could
// fail due to randomness.
const tolerance: number = 0.10;
const tolerance: number = 0.20;
X.le(Math.abs((a[0] - summary.params[0]) / a[0]), tolerance);
X.le(Math.abs((a[1] - summary.params[1]) / a[1]), tolerance);
X.le(Math.abs((a[2] - summary.params[2]) / a[2]), tolerance);
Expand Down
7 changes: 7 additions & 0 deletions smath/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
# Changelog

## 1.5.1

- Add `stdevp()` and `stdevs()`
- Minor fixes in tsdoc
- Fix bug in `npx` where it would crash with 0 arguments
- Warn for missing arguments in `npx`

## 1.5.0

- Change spread syntax parameters to a single array parameter
Expand Down
2 changes: 1 addition & 1 deletion smath/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,4 @@ This example command returns the value 0.4.

```shell
npx smath normalize 4 0 10
```
```
2 changes: 1 addition & 1 deletion smath/examples/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@
"clean": "rm -rf node_modules package-lock.json"
},
"dependencies": {
"smath": "file:smath-1.5.0.tgz"
"smath": "file:smath-1.5.1.tgz"
}
}
2 changes: 1 addition & 1 deletion smath/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "smath",
"version": "1.5.0",
"version": "1.5.1",
"description": "Small math function library",
"homepage": "https://npm.nicfv.com/smath",
"bin": "dist/bin.js",
Expand Down
19 changes: 16 additions & 3 deletions smath/src/bin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import { SMath } from '.';

const func: string = process.argv[2].toLowerCase(),
const func: string = (process.argv[2] ?? '').toLowerCase(),
nums: Array<number> = process.argv.slice(3).map((arg, i) => {
const num: number = Number.parseFloat(arg);
if (Number.isFinite(num)) {
Expand All @@ -22,13 +22,14 @@ if (func.includes('help')) {
console.log(' avg <c0> [c1] ... [cn] : Take an average of `n` numbers');
console.log(' varp <c0> [c1] ... [cn] : Compute the population variance of `n` numbers');
console.log(' vars <c0> [c1] ... [cn] : Compute the sample variance of `n` numbers');
console.log(' stdevp <c0> [c1] ... [cn]: Compute the population standard deviation of `n` numbers');
console.log(' stdevs <c0> [c1] ... [cn]: Compute the sample standard deviation of `n` numbers');
console.log(' approx <a> <b> [eps] : Check if `a` and `b` are approximately equal');
console.log(' clamp <n> <min> <max> : Clamp `n` between `min` and `max`');
console.log(' expand <n> <min> <max> : Expand normalized `n` between `min` and `max`');
console.log(' linspace <min> <max> <n> : Generate `n` linearly spaced numbers between `min` and `max`');
console.log(' logspace <min> <max> <n> : Generate `n` logarithmically spaced numbers between `min` and `max`');
console.log(' normalize <n> <min> <max>');
console.log(' : Normalize `n` between `min` and `max`');
console.log(' normalize <n> <min> <max>: Normalize `n` between `min` and `max`');
console.log(' translate <n> <min1> <max1> <min2> <max2>');
console.log(' : Linearly interpolate `n` from `min1`, `max1` to `min2`, `max2`');
console.log(' factorial <n> : Compute `n!` (factorial)');
Expand Down Expand Up @@ -57,6 +58,14 @@ switch (func) {
console.log(SMath.vars(nums));
break;
}
case ('stdevp'): {
console.log(SMath.stdevp(nums));
break;
}
case ('stdevs'): {
console.log(SMath.stdevs(nums));
break;
}
case ('approx'): {
console.log(SMath.approx(nums[0], nums[1], nums[2] ?? 1e-6));
break;
Expand Down Expand Up @@ -93,6 +102,10 @@ switch (func) {
console.log(SMath.error(nums[0], nums[1]));
break;
}
case (''): {
console.error('Missing argument. Use with "help" for a list of commands.');
process.exit(1);
}
default: {
console.error('Unknown argument "' + func + '". Use with "help" for a list of commands.');
process.exit(1);
Expand Down
26 changes: 21 additions & 5 deletions smath/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ export abstract class SMath {
/**
* Add up all the inputs.
* If none are present, returns 0.
* @param n Any amount of numeric inputs
* @param n An array of numeric inputs
* @returns The sum total
* @example
* ```js
Expand All @@ -25,7 +25,7 @@ export abstract class SMath {
/**
* Multiply all the inputs.
* If none are present, returns 1.
* @param n Any amount of numeric inputs
* @param n An array of numeric inputs
* @returns The product
* @example
* ```js
Expand All @@ -37,7 +37,7 @@ export abstract class SMath {
}
/**
* Compute the average, or mean, of a set of numbers.
* @param n Any amount of numeric inputs
* @param n An array of numeric inputs
* @returns The average, or mean
* @example
* ```js
Expand All @@ -49,7 +49,7 @@ export abstract class SMath {
}
/**
* Compute the variance of a **complete population**.
* @param n Any amount of numeric inputs
* @param n An array of numeric inputs
* @returns The population variance
* @example
* ```js
Expand All @@ -63,7 +63,7 @@ export abstract class SMath {
}
/**
* Compute the variance of a **sample**.
* @param n Any amount of numeric inputs
* @param n An array of numeric inputs
* @returns The sample variance
* @example
* ```js
Expand All @@ -75,6 +75,22 @@ export abstract class SMath {
squares: Array<number> = n.map(x => (x - mean) ** 2);
return this.sum(squares) / (n.length - 1);
}
/**
* Compute the standard deviation of a **complete population**.
* @param n An array of numeric inputs
* @returns The population standard deviation
*/
public static stdevp(n: Array<number>): number {
return Math.sqrt(this.varp(n));
}
/**
* Compute the standard deviation of a **sample**.
* @param n An array of numeric inputs
* @returns The sample standard deviation
*/
public static stdevs(n: Array<number>): number {
return Math.sqrt(this.vars(n));
}
/**
* Check if two numbers are approximately equal with a maximum abolute error.
* @param a Any number
Expand Down

0 comments on commit f5f5c52

Please sign in to comment.