-
-
Notifications
You must be signed in to change notification settings - Fork 153
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(vectors): add mean, minBounds, maxBounds
- add ensureInputs() assertion helper
- Loading branch information
1 parent
d646104
commit 640877f
Showing
6 changed files
with
82 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
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,9 @@ | ||
import { assert } from "@thi.ng/api"; | ||
|
||
/** | ||
* Asserts that `src` has at least 1 item. | ||
* | ||
* @internal | ||
*/ | ||
export const ensureInputs = (src: any[]) => | ||
assert(src.length > 0, `no inputs given`); |
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,19 @@ | ||
import type { ReadonlyVec, Vec } from "./api"; | ||
import { ensureInputs } from "./internal/ensure"; | ||
import { max } from "./max"; | ||
import { setN } from "./setn"; | ||
import { vecOf } from "./vec-of"; | ||
|
||
/** | ||
* Takes an array of vectors and computes componentwise maximum. Writes result | ||
* to `out` (or a new vector). | ||
* | ||
* @param out | ||
* @param src | ||
*/ | ||
export const maxBounds = (out: Vec | null, src: ReadonlyVec[]) => { | ||
ensureInputs(src); | ||
out = out ? setN(out, -Infinity) : vecOf(src[0].length, -Infinity); | ||
for (let i = src.length; --i >= 0; ) max(out, out, src[i]); | ||
return out; | ||
}; |
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,30 @@ | ||
import { add } from "./add"; | ||
import type { ReadonlyVec, Vec } from "./api"; | ||
import { ensureInputs } from "./internal/ensure"; | ||
import { mulN } from "./muln"; | ||
import { set } from "./set"; | ||
|
||
/** | ||
* Takes an array of vectors (of uniform dimensions) and computes the | ||
* componentwise mean. Writes result to `out` (or a new vector). | ||
* | ||
* @remarks | ||
* Also see {@link median}. | ||
* | ||
* @example | ||
* ```ts | ||
* mean([], [[3, 10, 400], [4, 30, 100], [1, 40, 200], [2, 20, 300]]) | ||
* // [ 2.5, 25, 250 ] | ||
* ``` | ||
* | ||
* @param out | ||
* @param src | ||
*/ | ||
export const mean = (out: Vec | null, src: ReadonlyVec[]) => { | ||
ensureInputs(src); | ||
out = set(out || [], src[0]); | ||
for (let i = src.length; --i >= 1; ) { | ||
add(out, out, src[i]); | ||
} | ||
return mulN(out, out, 1 / src.length); | ||
}; |
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
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,19 @@ | ||
import type { ReadonlyVec, Vec } from "./api"; | ||
import { ensureInputs } from "./internal/ensure"; | ||
import { min } from "./min"; | ||
import { setN } from "./setn"; | ||
import { vecOf } from "./vec-of"; | ||
|
||
/** | ||
* Takes an array of vectors and computes componentwise minimum. Writes result | ||
* to `out` (or a new vector). | ||
* | ||
* @param out | ||
* @param src | ||
*/ | ||
export const minBounds = (out: Vec | null, src: ReadonlyVec[]) => { | ||
ensureInputs(src); | ||
out = out ? setN(out, Infinity) : vecOf(src[0].length, Infinity); | ||
for (let i = src.length; --i >= 0; ) min(out, out, src[i]); | ||
return out; | ||
}; |