-
-
Notifications
You must be signed in to change notification settings - Fork 151
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(matrices): add outerProduct for vec 2/3/4
- Loading branch information
1 parent
3134ce6
commit 2a9d076
Showing
2 changed files
with
64 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,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 | ||
) | ||
); |