p-norms, Vector

kdex kdex at kdex.de
Fri Jun 2 22:13:45 UTC 2017


In mathematics, the p-norm of a vector is defined as in [1].

For p = 1, we get the taxicab norm, for p = 2 we get the Euclidean norm, and 
for the limit as p → ±∞ we get the maximum/minimum norm.

Particularly the Euclidean norm is pretty common in graphics-related code.

This could be implemented as an addition to`TypedArray.prototype`.

A rough sketch might look like so:

```js
TypedArray.prototype.norm = function(p) {
	if (Number.isNaN(p)) {
		return p;
	}
	const { abs, min, max, sign } = Math;
	if (Number.isFinite(p)) {
		return this.map(x => abs(x) ** p).reduce((a, b) => a + b, 0) ** (1 / p);
	}
	return abs((sign(p) === 1 ? max : min)(...this));
};
```

Since norms are a little too specific for `Array` (as they don't necessarily 
contain numerical values), it might be worth pondering about a native`Vector` 
implementation.

Any opinions?

[1] https://en.wikipedia.org/wiki/Norm_(mathematics)#p-norm
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 833 bytes
Desc: This is a digitally signed message part.
URL: <http://mail.mozilla.org/pipermail/es-discuss/attachments/20170603/0a9656e4/attachment.sig>


More information about the es-discuss mailing list