Skip to content

Commit

Permalink
feat(matrices): add outerProduct for vec 2/3/4
Browse files Browse the repository at this point in the history
  • Loading branch information
postspectacular committed May 5, 2019
1 parent 3134ce6 commit 2a9d076
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 0 deletions.
1 change: 1 addition & 0 deletions packages/matrices/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ export * from "./mulq";
export * from "./mulv";
export * from "./normal-mat";
export * from "./ortho";
export * from "./outer-product";
export * from "./perspective";
export * from "./project";
export * from "./quat-axis-angle";
Expand Down
63 changes: 63 additions & 0 deletions packages/matrices/src/outer-product.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
import {
MultiVecOpVV,
ReadonlyVec,
setC,
setC4,
vop
} from "@thi.ng/vectors";
import { Mat } from "./api";

/**
* Computes outer/tensor product of vectors `u` and `v`. Returns square
* matrix of same dimensions as vectors, e.g. 3x3 matrix for 3D vectors.
*
* https://en.wikipedia.org/wiki/Outer_product
*/
export const outerProduct: MultiVecOpVV = vop(1);

export const outerProduct2 = outerProduct.add(
2,
(out: Mat, [ux, uy]: ReadonlyVec, [vx, vy]: ReadonlyVec) =>
setC4(out || [], ux * vx, uy * vx, ux * vy, uy * vy)
);

export const outerProduct3 = outerProduct.add(
3,
(out: Mat, [ux, uy, uz]: ReadonlyVec, [vx, vy, vz]: ReadonlyVec) =>
setC(
out || [],
ux * vx,
uy * vx,
uz * vx,
ux * vy,
uy * vy,
uz * vy,
ux * vz,
uy * vz,
uz * vz
)
);

export const outerProduct4 = outerProduct.add(
4,
(out: Mat, [ux, uy, uz, uw]: ReadonlyVec, [vx, vy, vz, vw]: ReadonlyVec) =>
setC(
out || [],
ux * vx,
uy * vx,
uz * vx,
uw * vx,
ux * vy,
uy * vy,
uz * vy,
uw * vy,
ux * vz,
uy * vz,
uz * vz,
uw * vz,
ux * vw,
uy * vw,
uz * vw,
uw * vw
)
);

0 comments on commit 2a9d076

Please sign in to comment.