Examples#
The examples/ directory contains a collection of scripts demonstrating various applications of spicex.
current_source_with_resistor#
See on GitHub: examples/current_source_with_resistor
README:
# Current Source with Resistor
Philip Mocz (2026)
Simple current source with resistor example
```
n1+------------+
| |
[ I=1 mA ] [ R=1 kOhm ]
| |
n0+------------+
```
## Usage
```console
python current_source_with_resistor.py
```
Script:
import jax
import spicex
# switch on for double precision
jax.config.update("jax_enable_x64", True)
"""
Current source driving a resistor.
Philip Mocz (2026)
Usage:
python current_source_with_resistor.py
"""
def main():
circuit = spicex.Circuit(n_nodes=2)
circuit.add_current_source(0, 1, 1e-3) # 1 mA from ground to node 1
circuit.add_resistor(1, 0, 1e3) # 1 kΩ from node 1 to ground
v_nodes, i_vsrc = circuit.solve()
print(f"Node voltages: {v_nodes}")
print(f"Voltage source currents: {i_vsrc}")
return v_nodes, i_vsrc
if __name__ == "__main__":
main()
maximum_power_transfer#
See on GitHub: examples/maximum_power_transfer
README:
# Maximum Power Transfer
Philip Mocz (2026)
Autodiff through the circuit solver to find the load resistance R_L
that maximizes power delivered to it from a voltage source
```
n1+----[ R_s=1 kOhm ]----+n2
| |
[ V=10V ] [ R_L (optimized) ]
| |
n0+-----------------------+
```
Analytic result: max power transfer when `R_L = R_s`
Maximum power: `P_max = V_s^2 / (4 * R_s) = 25 mW`
## Usage
```console
python maximum_power_transfer.py
```
## Reference
https://en.wikipedia.org/wiki/Maximum_power_transfer_theorem
Script:
import jax
import jax.numpy as jnp
import spicex
# switch on for double precision
jax.config.update("jax_enable_x64", True)
"""
Maximum Power Transfer
Philip Mocz (2026)
Usage:
python maximum_power_transfer.py
"""
V_S = 10.0 # source voltage (V)
R_S = 1e3 # source resistance (Ohm)
def power_in_load(log_R_L):
"""Power delivered to the load resistor R_L."""
R_L = jnp.exp(log_R_L)
circuit = spicex.Circuit(n_nodes=3)
circuit.add_voltage_source(0, 1, V_S)
circuit.add_resistor(1, 2, R_S)
circuit.add_resistor(2, 0, R_L)
v_nodes, _ = circuit.solve()
return v_nodes[2] ** 2 / R_L
def main():
@jax.jit
def loss_fn(log_R_L):
return -power_in_load(log_R_L)
log_R_L = jnp.log(100.0)
log_R_L_opt, _ = spicex.optimize(log_R_L, loss_fn, max_iter=100, tol=1e-8)
R_L_opt = jnp.exp(log_R_L_opt)
P_opt = power_in_load(log_R_L_opt)
P_analytic = V_S**2 / (4.0 * R_S)
print(f"Optimal R_L: {float(R_L_opt):.2f} Ohm (analytic: {R_S:.0f} Ohm)")
print(
f"Max power: {float(P_opt) * 1e3:.4f} mW (analytic: {P_analytic * 1e3:.0f} mW)"
)
return R_L_opt, P_opt
if __name__ == "__main__":
main()
resistors_in_parallel#
See on GitHub: examples/resistors_in_parallel
README:
# Resistors in Parallel
Philip Mocz (2026)
Simple resistors in parallel example
```
n1+----------+----------+
| | |
[ V=5V ] [ R1=1 kOhm ] [ R2=2 kOhm ]
| | |
n0+----------+----------+
```
## Usage
```console
python resistors_in_parallel.py
```
Script:
import jax
import spicex
# switch on for double precision
jax.config.update("jax_enable_x64", True)
"""
Two resistors in parallel with a voltage source
Philip Mocz (2026)
Usage:
python resistors_in_parallel.py
"""
def main():
circuit = spicex.Circuit(n_nodes=2)
circuit.add_voltage_source(0, 1, 5.0) # 5 V source: ground --> node 1
circuit.add_resistor(1, 0, 1e3) # 1 kΩ: node 1 --> ground
circuit.add_resistor(1, 0, 2e3) # 2 kΩ: node 1 --> ground
v_nodes, i_vsrc = circuit.solve()
print("Node voltages:", v_nodes)
print("Current through voltage source:", i_vsrc)
return v_nodes, i_vsrc
if __name__ == "__main__":
main()
resistors_in_series#
See on GitHub: examples/resistors_in_series
README:
# Resistors in Series
Philip Mocz (2026)
Simple resistors in series example
```
n1+----[ R1=1 kOhm ]----+n2
| |
[ V=5V ] [ R2=2 kOhm ]
| |
n0+---------------------+
```
## Usage
```console
python resistors_in_series.py
```
Script:
import jax
import spicex
# switch on for double precision
jax.config.update("jax_enable_x64", True)
"""
Two resistors in parallel with a voltage source
Philip Mocz (2026)
Usage:
python resistors_in_series.py
"""
def main():
circuit = spicex.Circuit(n_nodes=3)
circuit.add_voltage_source(0, 1, 5.0) # 5 V source: ground --> node 1
circuit.add_resistor(1, 2, 1e3) # 1 kΩ: node 1 --> node 2
circuit.add_resistor(2, 0, 2e3) # 2 kΩ: node 2 --> ground
v_nodes, i_vsrc = circuit.solve()
print("Node voltages:", v_nodes)
print("Current through voltage source:", i_vsrc)
return v_nodes, i_vsrc
if __name__ == "__main__":
main()