Add calculations

This commit is contained in:
jaseg 2026-01-05 17:01:11 +01:00
commit 5939ce5de5
4 changed files with 3306 additions and 0 deletions

1304
Network Optimization.ipynb Normal file

File diff suppressed because it is too large Load diff

View file

@ -0,0 +1,153 @@
{
"cells": [
{
"cell_type": "code",
"execution_count": 5,
"id": "3936f121-1d67-439f-ac1b-4266c342b07a",
"metadata": {},
"outputs": [],
"source": [
"import math\n",
"import itertools\n"
]
},
{
"cell_type": "code",
"execution_count": 17,
"id": "0a55efe3-e46c-4e6e-a5ca-4e58084552e9",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
" 0.1 0.1 +0.0 % \n",
" 0.2 0.2 +0.0 % \n",
" 0.4 0.4 +0.0 % \n",
" 0.8 0.8 +0.0 % \n",
" 1.6 1.6 +0.0 % \n",
" 3.2 3.2 +0.0 % \n",
" 6.4 6.4 +0.0 % \n",
" 12.8 12.8 +0.0 % \n",
" 25.6 25.6 +0.0 % \n",
" 51.2 51.2 +0.0 % \n",
" 102.4 102.4 +0.0 % \n",
" 204.8 205.0 +0.1 % \n",
" 409.6 410.0 +0.1 % \n",
" 819.2 820.0 +0.1 % \n",
"1638.4 1650.0 +0.7 % \n",
"3276.8 3280.0 +0.1 % \n",
"6553.6 6550.0 -0.1 % \n"
]
}
],
"source": [
"C_dict = {\n",
" # capacitance [nF]: (tolerance [%], cost [USD])\n",
" 0.1: (1, 0.01),\n",
" 1.0: (1, 0.01),\n",
" 10.0: (5, 0.01),\n",
" 100.0: (5, 0.10),\n",
" 1000.0: (5, 0.50),\n",
" 1500.0: (5, 0.70),\n",
" 4700.0: (10, 1.00)}\n",
"\n",
"def series(*caps):\n",
" if any(math.isclose(c, 0) for c in caps):\n",
" return 0\n",
" return 1/sum(1/c for c in caps)\n",
"\n",
"def parallel(*caps):\n",
" return sum(caps)\n",
"\n",
"target_values = [0.1 * 2**i for i in range(17)] # in nF\n",
" \n",
"def evaluate(program):\n",
" stack = []\n",
" for op in program:\n",
" if op == '+':\n",
" stack.append(parallel(stack.pop(), stack.pop()))\n",
" elif op == '*':\n",
" stack.append(series(stack.pop(), stack.pop()))\n",
" else:\n",
" stack.append(op)\n",
" return stack.pop()\n",
"\n",
"def parse_program(program):\n",
" return [\n",
" e if e in '+*' else float(e) for e in program.strip().split()\n",
" ]\n",
"\n",
"programs = {\n",
" 0.1: '0.1',\n",
" 0.2: '0.1 0.1 +',\n",
" 0.4: '0.1 0.1 0.1 0.1 + + +',\n",
" 0.8: '1.0 1.0 * 0.1 0.1 0.1 + + +',\n",
" 1.6: '1.0 1.0 * 1.0 + 0.1 +',\n",
" 3.2: '1.0 1.0 1.0 + + 0.1 0.1 + +',\n",
" 6.4: '10 10 * 1 + 0.1 0.1 0.1 0.1 + + + +',\n",
" 12.8: '10 1 1 + + 1 1 * + 0.1 0.1 0.1 + + +',\n",
" 25.6: '10 10 + 10 10 * + 1 1 * + 0.1 +',\n",
" 51.2: '100 100 * 1 + 0.1 0.1 + +',\n",
" 102.4: '100 1 1 + + 0.1 0.1 0.1 0.1 + + + +',\n",
" 204.8: '100 100 + 10 10 * +',\n",
" 409.6: '100 100 100 100 + + + 10 +',\n",
" 819.2: '100 100 100 100 100 100 100 100 + + + + + + + 10 10 + +',\n",
" 1638.4: '1500 100 + 100 100 * +',\n",
" 3276.8: '1500 1500 + 100 100 + + 100 100 * + 10 10 10 + + +',\n",
" 6553.6: '4700 1500 + 100 100 100 + + + 100 100 * +',\n",
"}\n",
"\n",
"for target, program in programs.items():\n",
" if program:\n",
" res = evaluate(parse_program(program))\n",
" dev_percent = 100*((res - target) / target)\n",
" print(f'{target:>6.1f} {res:>6.1f} {dev_percent:+.1f} %', 'ERROR' if abs(target/res - 1) > 0.01 else '')"
]
},
{
"cell_type": "code",
"execution_count": 29,
"id": "d76a1d52-14c0-470a-969b-e8d768d85929",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"700.0"
]
},
"execution_count": 29,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# Offset caps:\n",
"evaluate(parse_program('10 10 10 10 + + + 10 10 * +'))\n",
"evaluate(parse_program('100 100 100 100 100 100 100 + + + + + +'))"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "decade-box",
"language": "python",
"name": "decade-box"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.13.11"
}
},
"nbformat": 4,
"nbformat_minor": 5
}

16
pyproject.toml Normal file
View file

@ -0,0 +1,16 @@
[project]
name = "decade-box"
version = "0.1.0"
description = "Add your description here"
readme = "README.md"
requires-python = ">=3.13"
dependencies = [
"pulp>=3.3.0",
]
[dependency-groups]
dev = [
"jupyter>=1.1.1",
"matplotlib>=3.10.8",
"tqdm>=4.67.1",
]

1833
uv.lock generated Normal file

File diff suppressed because it is too large Load diff