Published
Edited
May 8, 2020
1 star
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
project_vector = (x) => {
let n = x.length;
let alpha;
// sort x in *decreasing* order
let x_sorted = x.slice().sort((a,b) => b-a);
let sum = 0;
// for j ranging from 0 to n-1
for (const j of x_sorted.keys()) {
sum += x_sorted[j];
alpha = (1 - sum)/(1 + j);
if (x_sorted[j+1] <= -alpha) {
break;
}
}
let y = x.map((x_j) => x_j >-alpha? x_j+alpha : 0);
return y;
}
Insert cell
// tests
[
project_vector([1,1,1]),
project_vector([0,.5,.5]),
project_vector([-1,-2,2]),
]
Insert cell
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.
Learn more