Published
Edited
Oct 29, 2019
Importers
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
starting_assignments = ({
degree_expander: 8,
degree_base: 6,
epsilon: 0.025,
block_size: 32,
lambda: 80,
})
Insert cell
Insert cell
viewof assignments = report(model, starting_assignments)
Insert cell
viewof unsolved_constraints = report_unsolved(model, starting_assignments, ['layers', 'proofgen_time', 'nodes', 'snark_circuit'])
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
parse(`
Component:
a + b = c
`)
Insert cell
Insert cell
parse(await fetch_model('https://gist.githubusercontent.com/nicola/90f8e37648043118721dc8c4e9fb8542/raw/9370ccd059524186de892848c2f97488f454ee1b/ubercalc-zigzag-model.orient'))
Insert cell
Insert cell
Insert cell
solve(
model,
{
degree: 15,
degree_base: 5
}
)
Insert cell
Insert cell
Insert cell
assignment_set = [
{a:1},
[
{c:1},
{c:2}
]
]
Insert cell
unroll_assignments(assignment_set)
Insert cell
solve_multiple(model, assignment_set)
Insert cell
Insert cell
md`You can use webworkers for more complex calculations`
Insert cell
(await solve_webworker(
model,
{
degree: 12,
degree_base: 5
}
)).assignments
Insert cell
solve_constraints(["a = b + 1"], {a: 1})
Insert cell
solve_constraints(["a = b + c", "c = b + 1"], {a: 1}).assignments
Insert cell
solve_constraints(["a = b + c", "c = b + 1"], {a: 1}).constraints
Insert cell
solve_constraints(["a = b + c", "c = b + 1"], {a: 1}, ['b']).constraints
Insert cell
Insert cell
report(model, starting_assignments)
Insert cell
Insert cell
report_unsolved(model, starting_assignments)
Insert cell
Insert cell
range(model, ['soundness'])[0]
Insert cell
Insert cell
viewof conf = params(model, { degree: 11, lambda: 10 })
Insert cell
conf
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
(await solve_webworker(`
Component:
a = b + 1
`, {a: 1, b: 15})).assignments
Insert cell
solver(parse(model))
Insert cell
solver({constraints: ["a = 1", "declare(a, type)", "declare(a, type2)", "declare(b, type2)"]})
Insert cell
function solver (args) {
// The current solver is very simple:
// 1. it tries to substitute all the known terms
// 2. then tries to reduce (simplify) the terms,
// 3. Repeat from 1 until no new substitutions can be made
const {constraints, assignments} = args
let scope = Object.assign({}, assignments)
let filtered = constraints.filter(d => !d.includes('assume') && !d.includes('declare') && !d.includes('describe'))
const vars = {}
constraints.filter(d => d.includes("declare")).forEach(d => {
const tuple = d.replace('declare', '').replace('(', '').replace(')', '').split(',')
if (tuple.length == 2) {
const v = tuple[0].trim()
const val = tuple[1].trim()
vars[v] = val
}
})

// substitute current known terms
const subst = d => {
Object.keys(scope).forEach(v => {
if (d.variables().includes(v)) {
d = d.sub(v, scope[v])
}
})
return d
}

const reduce = (unsolved, d) => {
const vars = d.variables()
if (vars.length == 1) {
const solFound = d.solveFor(vars[0])
if (solFound.length == 0) {
throw(`${d.toString()} cannot be solved`)
}
const sol = solFound[0].evaluate().text() // TODO eval
if (scope[vars[0]] && scope[vars[0]] !== sol ) {
throw ("we are in trouble", scope[vars[0]], sol)
}
scope[vars[0]] = sol
} else {
unsolved.push(d)
}
return unsolved
}

const step = array => array.map(subst).reduce(reduce, [])

const run = (array) => {
let curr_resolved = 0
let i = 0;
let last = Object.keys(scope).length-1
while (Object.keys(scope).length !== last) {
last = Object.keys(scope).length
i++
array = step(array)
// if (simplify_terms) {
// array = simplifyConstraints(array, simplify_terms)
// array = step(array)
// }
}
return array
}

let res = run( filtered.map(d => nerdamer(d)) )

return {constraints: res.map(d => d.text()).filter( d => d != "0"), assignments: scope, vars: vars}
}
Insert cell
report_from_result(solve(`
Component:
declare(a, gb)
b = 1
a = 2
`), {})
Insert cell
function report_from_result(result, starting_assignments, simplify_terms) {
const html = md`

| name | val | type |
| ---- | --- | ---- |
${Object.keys(result.assignments).sort()
.map(d => `| ${!starting_assignments[d] ? `**${d}**` : d} | \`${result.assignments[d]}\` | \`${result.vars[d] || '?'}\` |\n`)}
`
html.value = result.assignments
return html
}

Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
{
const model = `
Component:
a = b
`

return table_constraints([
solve(model, {a: 2}).assignments,
solve(model, {a: 1}).assignments
], ['a'], ['b'])
}
Insert cell
{
const model = `
Component:
a = b
`

return table_constraints([
solve(model, {a: 2.002}).assignments,
solve(model, {a: 1}).assignments
], ['a', 'b'], [])
}
Insert cell
table_constraints = (solutions, filter, group_by, sort_by) => {
let results = multiple_solutions(solutions, group_by, filter)
if (sort_by) {
results = results.sort((a, b) => +a[sort_by] > +b[sort_by])
}
const header = `
Sorted by: ${sort_by}

${group_by.length ? `| name ` : ``}| ${filter.join(' | ')} |`
const divider = `${group_by.length ? `| --- ` : ``}| ${filter.map(f => '--:').join(' | ')} |`
const rows = results.map(r => {
return `${group_by.length ? `| ${r.name} ` : ``}| ${filter.map(f => `\`${_f(r[f])}\``).join(' | ')}`
})
const table = [header, divider, rows.join('\n')].join('\n')

return md`${table}`
}
Insert cell
table(
solve_multiple(`
Component:
a = b
c = 2*z
`, [[{a: 1}, {a: 4}], [{c:3}, {c:4}]]),
['a', 'b', 'c'],
['a', 'c']
)
Insert cell
table = (solved_multiple, filter, group_by, sort_by) => {
const solutions = solved_multiple.map(d => d.assignments)
return table_constraints(solutions, filter, group_by, sort_by)
}
Insert cell
graph(
solve_multiple(`
Component:
a = b
`, [[{a: 1}, {a: 2}, {a: 3}]]),
'a',
'b',
['']
)
Insert cell
graph = (solved_multiple, x, y, group_by) => {
const solutions = solved_multiple.map(d => d.assignments)
return graph_constraints(solutions, x, y, group_by)
}
Insert cell
graph_constraints = (solutions, x, y, group_by) => {
const results = multiple_solutions(solutions, group_by, [x, y])
return vl({
"title": `Plotting: ${x} vs ${y}`,
"width": 600,
"data": {"values": results},
"mark": {"type": "line"},
"encoding": {
"x": {
"field": x,
"type": "quantitative",
},
"y": {
"field": y,
"type": "quantitative",
},
"color": {
"field": "name",
"type": "nominal",
"scale": {"scheme": "category10"}
},
},
})
}
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
vl = require('@observablehq/vega-lite')
Insert cell
Insert cell
Insert cell
Insert cell
orient`
Component:
a+2+b=c
`.solve({a: 2}).solve({b: 3}).assignments
Insert cell
orient`
Component:
a + b = c
c + 2 = d
`
Insert cell
nerdamer('c=a+1', {a: '1/1208925819614629174706176'}).solveFor('c').toString()
Insert cell
nerdamer('ceil(b/2)= a').sub('b', 1).solveFor('a').toString()
Insert cell
nerdamer('ceil(a/2)= b').sub('b', 1).solveFor('a')
Insert cell
nerdamer('a/2= ceil(b)').sub('b', 1).solveFor('a')[0].text()
Insert cell
parse(`
Component:
tree_depth = log2(nodes)
`)
Insert cell
solve(`
Component:
tree_depth = log(nodes)/log(2)
`, {
'nodes': 1073741823.9999982
})
Insert cell
nerdamer('tree_depth = log(nodes)/log(2)').sub('tree_depth', 30).solveFor('nodes')[0].text()
Insert cell
s = nerdamer('tree_depth = log(nodes)/log(2)').sub('nodes', 1073741823.9999982).solveFor('tree_depth')[0].text()
Insert cell
nerdamer('tree_depth = log(nodes)/log(2)').sub('nodes', 1073741823.9999982).solveFor('tree_depth')[0].text('decimals')
Insert cell
nerdamer('tree_depth = log(nodes)/log(2)').sub('nodes', 1073741823.9999982).solveFor('tree_depth')[0].evaluate().text()
Insert cell
unroll_assignments([{a: 1}, {c: 2}, {c:3}, [{b: 2}, {b: 1}], [{d: 4}, {d: 1}]])
Insert cell
d3 = require('d3')
Insert cell
_f = (d) => typeof d == 'number' || !Number.isNaN(+d) ? d3.format('0.3~f')(d) : d
Insert cell
_f("0.0222")
Insert cell
Number.isNaN(+"sdsds")
Insert cell
_f(0.00033)
Insert cell
_f("hello")
Insert cell
_f(0.3)
Insert cell

One platform to build and deploy the best data apps

Experiment and prototype by building visualizations in live JavaScript notebooks. Collaborate with your team and decide which concepts to build out.
Use Observable Framework to build data apps locally. Use data loaders to build in any language or library, including Python, SQL, and R.
Seamlessly deploy to Observable. Test before you ship, use automatic deploy-on-commit, and ensure your projects are always up-to-date.
Learn more