Platform
Solutions
Resources
Pricing
Sign in
Sign up
Ricky Reusser
Workspace
Fork
Published
WebGL
By
Ricky Reusser
Edited
Jul 16, 2019
Importers
WebGL
Ueda's Attractor
Jacobi Elliptic Functions in the Complex Plane
SDF Points with regl
Clifford and de Jong Attractors
Drawing Instanced Lines with regl
Instanced Line Rendering
Lawson's Klein Bottle
Interactive Multi-scale Turing Patterns
Selecting the Right Opacity for 2D Point Clouds
Adaptive Contouring in Fragment Shaders
Baker's Map
Fake Transparency for 3D Surfaces
GPU Boids
Double Compound Pendulums
3D Reaction-Diffusion
Mathematical Easter Egg Coloring
Toiletpaperfullerenes and Charmin Nanotubes
Quasicrystals
Observable + regl
2D (Non-physical) N-body Gravity with Poisson's Equation
Periodic Planar Three-Body Orbits
Quick (miterless) Lines in WebGL
Spin WebGL
icosphere
regl-camera
Finding Roots in the Complex Plane
glsl-complex
glsl-complex-auto-differentiation
Domain Coloring with Adaptive Contouring
gl-mat3
gl-mat4
gl-vec4
gl-quat
gl-mat2
gl-vec3
gl-vec2
Instanced WebGL Circles
regl-tools
Domain Coloring for Complex Functions
Contour Plots with regl Observable Plot
Drawing indexed mesh data as screen-space normals without duplicating data
GPUs, FFTs, and the 1-D Schrödinger Equation
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
a
=
mat2
.
create
(
)
Insert cell
Insert cell
b
=
mat2create
(
)
Insert cell
Insert cell
mat2
=
(
{
determinant
:
mat2determinant
,
transpose
:
mat2transpose
,
multiply
:
mat2multiply
,
identity
:
mat2identity
,
adjoint
:
mat2adjoint
,
rotate
:
mat2rotate
,
invert
:
mat2invert
,
create
:
mat2create
,
scale
:
mat2scale
,
copy
:
mat2copy
,
frob
:
mat2frob
,
ldu
:
mat2ldu
}
)
Insert cell
/**
* Calculates the adjugate of a mat2
*
* @alias mat2.adjoint
* @param {mat2} out the receiving matrix
* @param {mat2} a the source matrix
* @returns {mat2} out
*/
function
mat2adjoint
(
out
,
a
)
{
// Caching this value is nessecary if out == a
var
a0
=
a
[
0
]
out
[
0
]
=
a
[
3
]
out
[
1
]
=
-
a
[
1
]
out
[
2
]
=
-
a
[
2
]
out
[
3
]
=
a0
return
out
}
Insert cell
/**
* Copy the values from one mat2 to another
*
* @alias mat2.copy
* @param {mat2} out the receiving matrix
* @param {mat2} a the source matrix
* @returns {mat2} out
*/
function
mat2copy
(
out
,
a
)
{
out
[
0
]
=
a
[
0
]
out
[
1
]
=
a
[
1
]
out
[
2
]
=
a
[
2
]
out
[
3
]
=
a
[
3
]
return
out
}
Insert cell
/**
* Creates a new identity mat2
*
* @alias mat2.create
* @returns {mat2} a new 2x2 matrix
*/
function
mat2create
(
)
{
var
out
=
new
Float32Array
(
4
)
out
[
0
]
=
1
out
[
1
]
=
0
out
[
2
]
=
0
out
[
3
]
=
1
return
out
}
Insert cell
/**
* Calculates the determinant of a mat2
*
* @alias mat2.determinant
* @param {mat2} a the source matrix
* @returns {Number} determinant of a
*/
function
mat2determinant
(
a
)
{
return
a
[
0
]
*
a
[
3
]
-
a
[
2
]
*
a
[
1
]
}
Insert cell
/**
* Returns Frobenius norm of a mat2
*
* @alias mat2.frob
* @param {mat2} a the matrix to calculate Frobenius norm of
* @returns {Number} Frobenius norm
*/
function
mat2frob
(
a
)
{
return
Math
.
sqrt
(
Math
.
pow
(
a
[
0
]
,
2
)
+
Math
.
pow
(
a
[
1
]
,
2
)
+
Math
.
pow
(
a
[
2
]
,
2
)
+
Math
.
pow
(
a
[
3
]
,
2
)
)
}
Insert cell
/**
* Set a mat2 to the identity matrix
*
* @alias mat2.identity
* @param {mat2} out the receiving matrix
* @returns {mat2} out
*/
function
mat2identity
(
out
)
{
out
[
0
]
=
1
out
[
1
]
=
0
out
[
2
]
=
0
out
[
3
]
=
1
return
out
}
Insert cell
/**
* Inverts a mat2
*
* @alias mat2.invert
* @param {mat2} out the receiving matrix
* @param {mat2} a the source matrix
* @returns {mat2} out
*/
function
mat2invert
(
out
,
a
)
{
var
a0
=
a
[
0
]
var
a1
=
a
[
1
]
var
a2
=
a
[
2
]
var
a3
=
a
[
3
]
var
det
=
a0
*
a3
-
a2
*
a1
if
(
!
det
)
return
null
det
=
1.0
/
det
out
[
0
]
=
a3
*
det
out
[
1
]
=
-
a1
*
det
out
[
2
]
=
-
a2
*
det
out
[
3
]
=
a0
*
det
return
out
}
Insert cell
/**
* Returns L, D and U matrices (Lower triangular, Diagonal and Upper triangular) by factorizing the input matrix
*
* @alias mat2.ldu
* @param {mat2} L the lower triangular matrix
* @param {mat2} D the diagonal matrix
* @param {mat2} U the upper triangular matrix
* @param {mat2} a the input matrix to factorize
*/
function
mat2ldu
(
L
,
D
,
U
,
a
)
{
L
[
2
]
=
a
[
2
]
/
a
[
0
]
U
[
0
]
=
a
[
0
]
U
[
1
]
=
a
[
1
]
U
[
3
]
=
a
[
3
]
-
L
[
2
]
*
U
[
1
]
return
[
L
,
D
,
U
]
}
Insert cell
/**
* Multiplies two mat2's
*
* @alias mat2.multiply
* @param {mat2} out the receiving matrix
* @param {mat2} a the first operand
* @param {mat2} b the second operand
* @returns {mat2} out
*/
function
mat2multiply
(
out
,
a
,
b
)
{
var
a0
=
a
[
0
]
,
a1
=
a
[
1
]
,
a2
=
a
[
2
]
,
a3
=
a
[
3
]
var
b0
=
b
[
0
]
,
b1
=
b
[
1
]
,
b2
=
b
[
2
]
,
b3
=
b
[
3
]
out
[
0
]
=
a0
*
b0
+
a2
*
b1
out
[
1
]
=
a1
*
b0
+
a3
*
b1
out
[
2
]
=
a0
*
b2
+
a2
*
b3
out
[
3
]
=
a1
*
b2
+
a3
*
b3
return
out
}
Insert cell
/**
* Rotates a mat2 by the given angle
*
* @alias mat2.rotate
* @param {mat2} out the receiving matrix
* @param {mat2} a the matrix to rotate
* @param {Number} rad the angle to rotate the matrix by
* @returns {mat2} out
*/
function
mat2rotate
(
out
,
a
,
rad
)
{
var
a0
=
a
[
0
]
,
a1
=
a
[
1
]
,
a2
=
a
[
2
]
,
a3
=
a
[
3
]
var
s
=
Math
.
sin
(
rad
)
var
c
=
Math
.
cos
(
rad
)
out
[
0
]
=
a0
*
c
+
a2
*
s
out
[
1
]
=
a1
*
c
+
a3
*
s
out
[
2
]
=
a0
*
-
s
+
a2
*
c
out
[
3
]
=
a1
*
-
s
+
a3
*
c
return
out
}
Insert cell
/**
* Scales the mat2 by the dimensions in the given vec2
*
* @alias mat2.scale
* @param {mat2} out the receiving matrix
* @param {mat2} a the matrix to rotate
* @param {vec2} v the vec2 to scale the matrix by
* @returns {mat2} out
**/
function
mat2scale
(
out
,
a
,
v
)
{
var
a0
=
a
[
0
]
,
a1
=
a
[
1
]
,
a2
=
a
[
2
]
,
a3
=
a
[
3
]
var
v0
=
v
[
0
]
,
v1
=
v
[
1
]
out
[
0
]
=
a0
*
v0
out
[
1
]
=
a1
*
v0
out
[
2
]
=
a2
*
v1
out
[
3
]
=
a3
*
v1
return
out
}
Insert cell
/**
* Transpose the values of a mat2
*
* @alias mat2.transpose
* @param {mat2} out the receiving matrix
* @param {mat2} a the source matrix
* @returns {mat2} out
*/
function
mat2transpose
(
out
,
a
)
{
// If we are transposing ourselves we can skip a few steps but have to cache some values
if
(
out
===
a
)
{
var
a1
=
a
[
1
]
out
[
1
]
=
a
[
2
]
out
[
2
]
=
a1
}
else
{
out
[
0
]
=
a
[
0
]
out
[
1
]
=
a
[
2
]
out
[
2
]
=
a
[
1
]
out
[
3
]
=
a
[
3
]
}
return
out
}
Insert cell
One platform
to build and deploy the best data apps
Experiment and prototype by building visualizations in live JavaScript notebooks. Collaborate with your team and decide which concepts to build out.
Use Observable Framework to build data apps locally. Use data loaders to build in any language or library, including Python, SQL, and R.
Seamlessly deploy to Observable. Test before you ship, use automatic deploy-on-commit, and ensure your projects are always up-to-date.
Try it for free
Learn more
Fork
View
Export
Edit
Add comment
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
Edit
Add comment
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
Edit
Add comment
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
Edit
Add comment
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
a
Add comment
Copy import
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
Edit
Add comment
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
b
Add comment
Copy import
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
Edit
Add comment
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
mat2
Add comment
Copy import
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
mat2adjoint
Add comment
Copy import
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
mat2copy
Add comment
Copy import
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
mat2create
Add comment
Copy import
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
mat2determinant
Add comment
Copy import
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
mat2frob
Add comment
Copy import
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
mat2identity
Add comment
Copy import
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
mat2invert
Add comment
Copy import
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
mat2ldu
Add comment
Copy import
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
mat2multiply
Add comment
Copy import
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
mat2rotate
Add comment
Copy import
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
mat2scale
Add comment
Copy import
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
mat2transpose
Add comment
Copy import
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML