Public
Edited
Oct 1, 2023
Insert cell
Insert cell
class Matrix {
constructor(data) {
this.data = data;
this.rows = data.length;
this.cols = data[0].length;
}

toString() {
return this.data.map((row) => row.join(" ")).join("\n");
}

add(other) {
if (this.rows !== other.rows || this.cols !== other.cols) {
throw new Error("Matrices must have the same dimensions for addition");
}

const result = [];
for (let i = 0; i < this.rows; i++) {
result.push([]);
for (let j = 0; j < this.cols; j++) {
result[i][j] = this.data[i][j] + other.data[i][j];
}
}

return new Matrix(result);
}

subtract(other) {
if (this.rows !== other.rows || this.cols !== other.cols) {
throw new Error("Matrices must have the same dimensions for subtraction");
}

const result = [];
for (let i = 0; i < this.rows; i++) {
result.push([]);
for (let j = 0; j < this.cols; j++) {
result[i][j] = this.data[i][j] - other.data[i][j];
}
}

return new Matrix(result);
}

multiply(scalar) {
const result = [];
for (let i = 0; i < this.rows; i++) {
result.push([]);
for (let j = 0; j < this.cols; j++) {
result[i][j] = this.data[i][j] * scalar;
}
}

return new Matrix(result);
}

divide(scalar) {
if (scalar === 0) {
throw new Error("Division by zero is not allowed");
}

const result = [];
for (let i = 0; i < this.rows; i++) {
result.push([]);
for (let j = 0; j < this.cols; j++) {
result[i][j] = this.data[i][j] / scalar;
}
}

return new Matrix(result);
}

transpose() {
const result = [];
for (let i = 0; i < this.cols; i++) {
result.push([]);
for (let j = 0; j < this.rows; j++) {
result[i][j] = this.data[j][i];
}
}

return new Matrix(result);
}

static identity(size) {
const data = Array.from({ length: size }, (_, i) =>
Array.from({ length: size }, (_, j) => (i === j ? 1 : 0))
);
return new Matrix(data);
}

static listToMatrix(lst) {
return new Matrix(lst);
}

static matrixMultiply(matrix1, matrix2) {
if (matrix1.cols !== matrix2.rows) {
throw new Error(
"Number of columns in the first matrix must be equal to the number of rows in the second matrix"
);
}

const result = [];
for (let i = 0; i < matrix1.rows; i++) {
result.push([]);
for (let j = 0; j < matrix2.cols; j++) {
let sum = 0;
for (let k = 0; k < matrix1.cols; k++) {
sum += matrix1.data[i][k] * matrix2.data[k][j];
}
result[i][j] = sum;
}
}

return new Matrix(result);
}
}
Insert cell
function randomInt(min, max) {
return Math.floor(Math.random() * (max - min + 1)) + min;
}
Insert cell
class MagicSquare {
static toMagicSquare(coords) {
if (coords.length !== 8) {
throw new Error(
"Incomplete coordinates. You must set all 8 coordinates."
);
}

const m1 = new Matrix([
[1, 1, 1, 1],
[1, 1, 1, 1],
[1, 1, 1, 1],
[1, 1, 1, 1]
]).multiply(coords[0]);

const m2 = new Matrix([
[1, 0, 0, -1],
[0, -1, 1, 0],
[0, 1, -1, 0],
[-1, 0, 0, 1]
]).multiply(coords[1]);

const m3 = new Matrix([
[1, -1, 0, 0],
[-1, 1, 0, 0],
[0, 0, -1, 1],
[0, 0, 1, -1]
]).multiply(coords[2]);

const m4 = new Matrix([
[1, 0, -1, 0],
[0, -1, 0, 1],
[-1, 0, 1, 0],
[0, 1, 0, -1]
]).multiply(coords[3]);

const m5 = new Matrix([
[0, 0, -1, 1],
[0, 0, 1, -1],
[1, -1, 0, 0],
[-1, 1, 0, 0]
]).multiply(coords[4]);

const m6 = new Matrix([
[0, -1, 0, 1],
[1, 0, -1, 0],
[0, 1, 0, -1],
[-1, 0, 1, 0]
]).multiply(coords[5]);

const m7 = new Matrix([
[0, 1, -1, 0],
[0, 0, 0, 0],
[0, 0, 0, 0],
[0, -1, 1, 0]
]).multiply(coords[6]);

const m8 = new Matrix([
[0, 0, 0, 0],
[1, 0, 0, -1],
[-1, 0, 0, 1],
[0, 0, 0, 0]
]).multiply(coords[7]);

const result = m1.add(m2).add(m3).add(m4).add(m5).add(m6).add(m7).add(m8);

return result;
}

static fromTarget(target) {
const n = 4;
target = Math.floor(target);

// Generar coordenadas aleatorias, asegurándose de que sumen al objetivo
const coords = Array.from({ length: 8 }, () => 0);

while (coords.reduce((acc, val) => acc + val, 0) !== target) {
coords.forEach((_, index) => {
const randomValue = randomInt(-target, target);
coords[index] = randomValue;
});
}

return coords;
}
}
Insert cell
coords = MagicSquare.fromTarget(3)
Insert cell
data = Array(10)
.fill(0)
.map(() => MagicSquare.toMagicSquare(coords))
Insert cell
Insert cell
viewof rawCoords = Inputs.text({
placeholder: "Coords...",
value: "21, 32, 4, 21, 3, 12, 3, 1"
})
Insert cell
Insert cell
m = MagicSquare.toMagicSquare(coords$)
Insert cell
mData = {
const numberCounts = _.countBy(_.flattenDeep(m.data));
return _.map(numberCounts, (count, number) => ({
number: parseInt(number),
frequency: count
}));
}
Insert cell
mData
X
number
Y
frequency
Color
frequency
Size
Facet X
Facet Y
Mark
bar
Type Chart, then Shift-Enter. Ctrl-space for more options.

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