p-norms, Vector

classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

p-norms, Vector

kdex
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
_______________________________________________
es-discuss mailing list
[hidden email]
https://mail.mozilla.org/listinfo/es-discuss

signature.asc (849 bytes) Download Attachment
Reply | Threaded
Open this post in threaded view
|

Re: p-norms, Vector

peter miller
Hi,

> In mathematics, the p-norm of a vector is defined as in [1].
>
> For p = 1, we get the taxicab norm,

`array.reduce( (a,b)=>a+b )` (Although I wouldn't object to `Math.sum()`)

> for p = 2 we get the Euclidean norm,

`Math.hypot(...array)`

> and
> for the limit as p → ±∞ we get the maximum/minimum norm.

`Math.max(...array)`


> 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.
>

Will DOMPoint solve your use cases? (Personal annoyance: you can't index  
DOMPoint numerically.)


Peter


--
"There were drawings, and sheets of paper with writing on them, and it  
seemed that they were the sustenance of life, that here were the warlocks,  
almost the vehicles of destruction of man's life, but at the same time the  
very reason for his living." --- Maeve Gilmore/Titus Awakes.
_______________________________________________
es-discuss mailing list
[hidden email]
https://mail.mozilla.org/listinfo/es-discuss
Reply | Threaded
Open this post in threaded view
|

Re: p-norms, Vector

kdex
Thanks for the pointers!

> `array.reduce( (a,b)=>a+b )` (Although I wouldn't object to `Math.sum()`)
Yes; no objections here.

> `Math.hypot(...array)`
I'm aware of `Math.hypot`, but it only covers `p = 2`. The important thing
about `Math.hypot` is that it doesn't demolish numbers with high magnitudes in
the process of raising them to the power of two.

It would be great to have the same safety for other cases (i.e. `p = 3`) as
well. The idea was to generalize the concept for every `p`.

> Will DOMPoint solve your use cases? (Personal annoyance: you can't index
> DOMPoint numerically.)

Unfortunately, `DOMPoint` would be a Web API, which might (and probably will)
not be available in other environments.

Essentially, what I'm suggesting is an environment-indepdendent `Vector`
class, though I'm reluctant to go into more detail, as I feel that hardly
anyone would want a `Vector` without proper operator overloading.

On Monday, June 5, 2017 1:09:21 PM CEST peter miller wrote:

> Hi,
>
> > In mathematics, the p-norm of a vector is defined as in [1].
> >
> > For p = 1, we get the taxicab norm,
>
> `array.reduce( (a,b)=>a+b )` (Although I wouldn't object to `Math.sum()`)
>
> > for p = 2 we get the Euclidean norm,
>
> `Math.hypot(...array)`
>
> > and
> > for the limit as p → ±∞ we get the maximum/minimum norm.
>
> `Math.max(...array)`
>
> > 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.
>
> Will DOMPoint solve your use cases? (Personal annoyance: you can't index
> DOMPoint numerically.)
>
>
> Peter
_______________________________________________
es-discuss mailing list
[hidden email]
https://mail.mozilla.org/listinfo/es-discuss

signature.asc (849 bytes) Download Attachment
Reply | Threaded
Open this post in threaded view
|

Re: p-norms, Vector

Isiah Meadows-2

This *really* seems like a good use case for WASM + SIMD (and a small static heap), given the number-heavy nature of it.


On Mon, Jun 5, 2017, 10:47 kdex <[hidden email]> wrote:
Thanks for the pointers!

> `array.reduce( (a,b)=>a+b )` (Although I wouldn't object to `Math.sum()`)
Yes; no objections here.

> `Math.hypot(...array)`
I'm aware of `Math.hypot`, but it only covers `p = 2`. The important thing
about `Math.hypot` is that it doesn't demolish numbers with high magnitudes in
the process of raising them to the power of two.

It would be great to have the same safety for other cases (i.e. `p = 3`) as
well. The idea was to generalize the concept for every `p`.

> Will DOMPoint solve your use cases? (Personal annoyance: you can't index
> DOMPoint numerically.)

Unfortunately, `DOMPoint` would be a Web API, which might (and probably will)
not be available in other environments.

Essentially, what I'm suggesting is an environment-indepdendent `Vector`
class, though I'm reluctant to go into more detail, as I feel that hardly
anyone would want a `Vector` without proper operator overloading.

On Monday, June 5, 2017 1:09:21 PM CEST peter miller wrote:
> Hi,
>
> > In mathematics, the p-norm of a vector is defined as in [1].
> >
> > For p = 1, we get the taxicab norm,
>
> `array.reduce( (a,b)=>a+b )` (Although I wouldn't object to `Math.sum()`)
>
> > for p = 2 we get the Euclidean norm,
>
> `Math.hypot(...array)`
>
> > and
> > for the limit as p → ±∞ we get the maximum/minimum norm.
>
> `Math.max(...array)`
>
> > 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.
>
> Will DOMPoint solve your use cases? (Personal annoyance: you can't index
> DOMPoint numerically.)
>
>
> Peter_______________________________________________
es-discuss mailing list
[hidden email]
https://mail.mozilla.org/listinfo/es-discuss

_______________________________________________
es-discuss mailing list
[hidden email]
https://mail.mozilla.org/listinfo/es-discuss