Published
Edited
Mar 13, 2021
1 fork
Importers
15 stars
Insert cell
Insert cell
Insert cell
viewof A = pt([[0], [1]])
Insert cell
viewof B = pt(new Array(10).fill(1).map(Math.random))
Insert cell
viewof C = pt([[0, 1], [1, 0]], { title: "C=" })
Insert cell
viewof D = pt([["\\alpha", 0, 1], ["\\beta", 1, 0]])
Insert cell
viewof E = pt(new Array(1000000).fill(1.01), { maxcols: 9 })
Insert cell
viewof F = pt(math.matrix([[1, 0, 10], [0, 1, 10], [0, 0, 1]]))
Insert cell
viewof G = pt(
tf.tensor2d(Array.from({ length: 400 }).map((_, i) => i), [20, 20]),
{ maxrows: 8, maxcols: 10 }
)
Insert cell
viewof H = pt(tf.tensor1d(Array.from({ length: 100 }).map((_, i) => i)))
Insert cell
viewof I = pt(H.mul(tf.scalar(0.1)).sin())
Insert cell
viewof J = pt(I.sum())
Insert cell
// for 3D tensors it shows only the first slice
viewof K = pt(
tf.tensor3d(Array.from({ length: 1000 }).map((_, i) => i), [10, 10, 10])
)
Insert cell
viewof L = pt(mlMatrix.Matrix.eye(3, 4), { title: "L=" })
Insert cell
pt(Math.PI, { title: "π = " })
Insert cell
pt(nd.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]]).mapElems(Math.sin))
Insert cell
Insert cell
pt(mlMatrix.Matrix.eye(3, 4))
Insert cell
async function pt(matrix, opts = {}) {
const maxrows = opts.maxrows || 13;
const maxcols = opts.maxcols || maxrows;

const format =
opts.format ||
function(x) {
return +x >= +x ? +(Math.round(x * 1000) / 1000) : x;
};

let values = matrix;
let desc = opts.title || "";

// matrix.js
if (typeof values.toArray === "function") {
values = values.toArray();
}

// tfjs
if (typeof values.shape === "object" && typeof values.data === "function") {
values.__data = values.data;
}

// ndjs
if (typeof values.shape === "object" && typeof values.data === "object") {
values = Object.assign({ __data: () => values.data }, values);
}

// ml-matrix@^6.6
if (values.rows && values.columns && typeof values.data === "object") {
values = values.data;
}

// tfjs && ndjs
if (typeof values.shape === "object" && typeof values.__data === "function") {
let data = await values.__data(),
shape = values.shape;
if (shape.length === 0) shape = [1]; // scalar
if (shape.length > 2 || shape[0] > maxrows || shape[1] > maxcols) {
desc = `t(${values.shape})`;
}
values = [];
let lastrow = shape[1] || 1;
let lastcol = shape[0] || 1;
for (let i = 0; i < lastrow; i++) {
if (i < maxrows || i === lastrow - 1) {
let line = [];
for (let j = 0; j < lastcol; j++) {
if (j < maxcols || j === lastcol - 1) {
line.push(data[shape[0] * i + j]);
}
}
values.push(line);
}
}
}

// simple value
if (typeof values !== "object") return md`${desc}${values}`;

// single row
if (typeof values[0] !== "object") values = [values];

const nrows = values.length,
ncols = values[0].length;

if (ncols > maxcols) {
values = values.map((line, j) => {
return [
...line.slice(0, maxcols - 2),
j === 0 ? "\\cdots" : "\\space",
line[line.length - 1]
];
});
}

if (nrows > maxrows) {
values = [
...values.slice(0, maxrows - 2),
values[0].map((d, i) =>
i === 0
? "\\vdots"
: ncols > maxcols && i === maxcols - 2
? "\\ddots"
: "\\space"
),
values[values.length - 1]
];
}

const data = values
.map(row =>
Array.from(row)
.map(format)
.join(" & ")
)
.join(" \\\\ ");
const el = tex`\small{
${desc}\\
\left(\begin{matrix}
${data}
\end{matrix}\right)}
`;
el.value = matrix;
return el;
}
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
nd = require("https://raw.githack.com/DirkToewe/ndjs/master/dist/nd.min.js")
Insert cell
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