Notebooks 2.0 is here.
Read the preview announcement
Platform
Resources
Pricing
Sign in
Get started
Harry Stevens
News and graphics at @ClimateLab, a project of @PostGraphics.
Workspace
Fork
Public
Linear Algebra
By
Harry Stevens
Edited
Feb 7
Importers
4 stars
Insert cell
Insert cell
vecmath
=
(
{
abs
,
add
,
sub
,
mult
,
div
,
limit
,
normalize
,
setMag
,
getMag
,
dot
,
project
,
ang
,
dist
,
trans
,
invalid
}
)
;
Insert cell
Insert cell
abs
=
v
=>
{
const
out
=
[
]
;
for
(
let
i
=
0
;
i
<
v
.
length
;
i
++
)
{
out
[
i
]
=
Math
.
abs
(
v
[
i
]
)
;
}
return
out
;
}
Insert cell
// Add vector w to vector v
add
=
(
v
,
w
)
=>
{
let
out
=
[
]
;
for
(
let
i
=
0
;
i
<
v
.
length
;
i
++
)
{
out
[
i
]
=
v
[
i
]
+
w
[
i
]
;
}
return
out
;
}
Insert cell
// Subtract vector w from vector v
sub
=
(
v
,
w
)
=>
{
let
out
=
[
]
;
for
(
let
i
=
0
;
i
<
v
.
length
;
i
++
)
{
out
[
i
]
=
v
[
i
]
-
w
[
i
]
;
}
return
out
;
}
Insert cell
// Multiply vector v by w, either a vector of equal length or a scalar
mult
=
(
v
,
w
)
=>
{
let
that
=
[
]
;
if
(
typeof
w
===
"number"
)
{
for
(
let
i
=
0
;
i
<
v
.
length
;
i
++
)
{
that
[
i
]
=
w
;
}
}
else
{
that
=
w
;
}
let
out
=
[
]
;
for
(
let
i
=
0
;
i
<
v
.
length
;
i
++
)
{
out
[
i
]
=
v
[
i
]
*
that
[
i
]
;
}
return
out
;
}
Insert cell
// Divide vector v by w, either a vector of equal length or a scalar
div
=
(
v
,
w
)
=>
{
let
that
=
[
]
;
if
(
typeof
w
===
"number"
)
{
for
(
let
i
=
0
;
i
<
v
.
length
;
i
++
)
{
that
[
i
]
=
w
;
}
}
else
{
that
=
w
;
}
let
out
=
[
]
;
for
(
let
i
=
0
;
i
<
v
.
length
;
i
++
)
{
out
[
i
]
=
v
[
i
]
/
that
[
i
]
;
}
return
out
;
}
Insert cell
// Limit the magnitude of this vector to the value used for the n parameter.
limit
=
(
v
,
n
)
=>
{
let
out
=
v
;
const
sq
=
Math
.
pow
(
getMag
(
v
)
,
2
)
;
if
(
sq
>
n
*
n
)
{
out
=
div
(
out
,
Math
.
sqrt
(
sq
)
)
;
out
=
mult
(
out
,
n
)
;
}
return
out
;
}
Insert cell
// Normalize the vector to length 1 (make it a unit vector).
normalize
=
v
=>
{
const
m
=
getMag
(
v
)
,
l
=
v
.
length
;
return
m
?
mult
(
v
,
1
/
m
)
:
v
.
map
(
d
=>
1
/
l
)
;
}
Insert cell
// Get the magnitude of a vector
getMag
=
v
=>
{
let
l
=
v
.
length
,
sums
=
0
;
for
(
let
i
=
0
;
i
<
l
;
i
++
)
{
sums
+=
v
[
i
]
*
v
[
i
]
;
}
return
Math
.
sqrt
(
sums
)
;
}
Insert cell
// Set the magnitude of this vector to the value used for the n parameter.
setMag
=
(
v
,
n
)
=>
{
return
mult
(
normalize
(
v
)
,
n
)
;
}
Insert cell
// Compute the dot product or inner product
dot
=
(
v1
,
v2
)
=>
{
let
o
=
0
;
for
(
let
i
=
0
,
l
=
v1
.
length
;
i
<
l
;
i
++
)
{
o
+=
v1
[
i
]
*
v2
[
i
]
;
}
return
o
;
}
Insert cell
// Projects vector v1 onto vector v2
project
=
(
v1
,
v2
)
=>
{
return
mult
(
v2
,
dot
(
v1
,
v2
)
/
dot
(
v2
,
v2
)
)
}
Insert cell
Insert cell
// Angle from vector v to vector w in radians
// Only works for 2d vectors
ang
=
(
v
,
w
)
=>
Math
.
atan2
(
w
[
1
]
-
v
[
1
]
,
w
[
0
]
-
v
[
0
]
)
;
Insert cell
dist
(
[
2
,
1
]
,
project
(
[
2
,
1
]
,
[
6
,
8
]
)
)
Insert cell
Math
.
pow
(
2
-
6
/
5
,
2
)
+
Math
.
pow
(
1
-
8
/
5
,
2
)
Insert cell
// Distance from position of vector v to position of vector w in pixels
dist
=
(
v
,
w
)
=>
{
let
sum
=
0
;
for
(
let
i
=
0
,
l
=
v
.
length
;
i
<
l
;
i
++
)
{
sum
+=
Math
.
pow
(
w
[
i
]
-
v
[
i
]
,
2
)
;
}
return
Math
.
sqrt
(
sum
)
;
}
Insert cell
// Translate position of vector v by an angle in radians and a distance in pixels
trans
=
(
v
,
ang
,
dist
)
=>
[
v
[
0
]
+
dist
*
Math
.
cos
(
ang
)
,
v
[
1
]
+
dist
*
Math
.
sin
(
ang
)
]
;
Insert cell
Insert cell
invalid
=
v
=>
{
let
out
=
false
;
for
(
let
i
=
0
,
l
=
v
.
length
;
i
<
l
;
i
++
)
{
const
d
=
v
[
i
]
;
if
(
typeof
d
!==
"number"
||
!
isFinite
(
d
)
)
{
out
=
true
;
break
;
}
}
return
out
;
}
Insert cell
Insert cell
sqrt
=
Math
.
sqrt
Insert cell
Purpose-built for displays of data
Observable is your go-to platform for exploring data and creating expressive data visualizations. Use reactive JavaScript notebooks for prototyping and a collaborative canvas for visual data exploration and dashboard creation.
Try it for free
Learn more
Fork
View
Export
Edit
Add comment
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
vecmath
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
abs
Add comment
Copy import
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
add
Add comment
Copy import
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
sub
Add comment
Copy import
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
mult
Add comment
Copy import
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
div
Add comment
Copy import
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
limit
Add comment
Copy import
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
normalize
Add comment
Copy import
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
getMag
Add comment
Copy import
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
setMag
Add comment
Copy import
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
dot
Add comment
Copy import
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
project
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
ang
Add comment
Copy import
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
Add comment
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
Add comment
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
dist
Add comment
Copy import
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
trans
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
invalid
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
sqrt
Add comment
Copy import
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML