Public
Edited
Dec 29, 2022
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
convert = (x, y) => {
const c = new complex(x, y);
return c.add(new complex(2, 3)).power(power);
}
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
{
const lines = Plot.line(data, {
x: "real",
y: "imag",
stroke: "name" //(d) => dataColor.filter((e) => e.name === d.name)[0].color
});
const plt = Plot.plot({
y: {
grid: true,
label: "↑ Imag"
},
x: {
grid: true,
label: "Real"
},
color: {
legend: true,
scheme: "category10"
},
marks: [Plot.ruleY([0]), Plot.ruleX([0]), lines]
});

return plt;
}
Insert cell
data
Type Table, then Shift-Enter. Ctrl-space for more options.

Insert cell
dataColor = Array(...new Set(data.map((d) => d.name))).map((d, i) => {
return { name: d, color: d3.schemeCategory10[i % 10] };
})
Insert cell
data = {
const n = Math.pow(10, pointsFactor);

const xValues = d3
.range(n)
.map((i) => form.centerX + (i / n) * 2 * form.rangeX - form.rangeX);

const data = [];

FUNCTIONS.map((func) => {
const { fun, name } = func;

const yValues = xValues.map((x) => eval(fun));

xValues.map((x, i) => {
data.push({ x, y: yValues[i], fun, name, n });
});
});

data.map((d) => {
const { x, y } = d;
const c = convert(x, y);
Object.assign(d, c);
});

return data;
}
Insert cell
Inputs.table(FUNCTIONS)
Insert cell
FUNCTIONS = {
const lines = textareaFunction.split("\n").map((d) => d);

const data = [];

var split;
lines.map((line) => {
split = line.split(",");
data.push({ fun: split[0], name: split[1].trim() });
});

return data;
}
Insert cell
d3 = require("d3")
Insert cell
{
// Example usage:
var c1 = new complex(3, 4);
var c2 = new complex(1, 2);
return [
c1.add(c2), // prints "4 + 6i"
c1.subtract(c2), // prints "2 + 2i"
c1.multiply(c2), // prints "-5 + 10i"
c1.divide(c2), // prints "2.2 - 0.4i"
c1.power(3) // prints "-117 + 44i"
];
}
Insert cell
complex = {
function complex(real, imag) {
this.real = real;
this.imag = imag;
}

complex.prototype.add = function (c) {
return new complex(this.real + c.real, this.imag + c.imag);
};

complex.prototype.subtract = function (c) {
return new complex(this.real - c.real, this.imag - c.imag);
};

complex.prototype.multiply = function (c) {
return new complex(
this.real * c.real - this.imag * c.imag,
this.real * c.imag + this.imag * c.real
);
};

complex.prototype.divide = function (c) {
var denom = c.real * c.real + c.imag * c.imag;
return new complex(
(this.real * c.real + this.imag * c.imag) / denom,
(this.imag * c.real - this.real * c.imag) / denom
);
};

complex.prototype.power = function (n) {
var r = Math.sqrt(this.real * this.real + this.imag * this.imag);
var theta = Math.atan2(this.imag, this.real);
var real = Math.pow(r, n) * Math.cos(n * theta);
var imag = Math.pow(r, n) * Math.sin(n * theta);
return new complex(real, imag);
};

return complex;
}
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