Skip to content

Commit

Permalink
feat(vectors): add mean, minBounds, maxBounds
Browse files Browse the repository at this point in the history
- add ensureInputs() assertion helper
  • Loading branch information
postspectacular committed Aug 17, 2021
1 parent d646104 commit 640877f
Show file tree
Hide file tree
Showing 6 changed files with 82 additions and 0 deletions.
3 changes: 3 additions & 0 deletions packages/vectors/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -101,8 +101,11 @@ export * from "./major";
export * from "./map";
export * from "./map-vectors";
export * from "./max";
export * from "./max-bounds";
export * from "./mean";
export * from "./median";
export * from "./min";
export * from "./min-bounds";
export * from "./minor";
export * from "./mix-bilinear";
export * from "./mix-cubic";
Expand Down
9 changes: 9 additions & 0 deletions packages/vectors/src/internal/ensure.ts
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`);
19 changes: 19 additions & 0 deletions packages/vectors/src/max-bounds.ts
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;
};
30 changes: 30 additions & 0 deletions packages/vectors/src/mean.ts
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);
};
2 changes: 2 additions & 0 deletions packages/vectors/src/median.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import type { ReadonlyVec, Vec } from "./api";
import { ensureInputs } from "./internal/ensure";

/**
* Takes an array of vectors (of uniform dimensions) and computes the
Expand All @@ -15,6 +16,7 @@ import type { ReadonlyVec, Vec } from "./api";
* @param src
*/
export const median = (out: Vec | null, src: ReadonlyVec[]) => {
ensureInputs(src);
out = out || [];
const m = src.length >> 1;
for (let i = src[0].length; --i >= 0; ) {
Expand Down
19 changes: 19 additions & 0 deletions packages/vectors/src/min-bounds.ts
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;
};

0 comments on commit 640877f

Please sign in to comment.