pub enum Exp {
Val(f64),
Var(String),
Add(Box<Exp>, Box<Exp>),
Sub(Box<Exp>, Box<Exp>),
Mul(Box<Exp>, Box<Exp>),
Div(Box<Exp>, Box<Exp>),
Power(Box<Exp>, f64),
Neg(Box<Exp>),
Sin(Box<Exp>),
Cos(Box<Exp>),
Ln(Box<Exp>),
Exp(Box<Exp>),
}Variants§
Val(f64)
Var(String)
Add(Box<Exp>, Box<Exp>)
Sub(Box<Exp>, Box<Exp>)
Mul(Box<Exp>, Box<Exp>)
Div(Box<Exp>, Box<Exp>)
Power(Box<Exp>, f64)
Neg(Box<Exp>)
Sin(Box<Exp>)
Cos(Box<Exp>)
Ln(Box<Exp>)
Exp(Box<Exp>)
Implementations§
Source§impl Exp
impl Exp
Sourcepub fn var(name: impl Into<String>) -> Self
pub fn var(name: impl Into<String>) -> Self
Examples found in repository?
examples/cad_circle_intersection.rs (line 39)
37fn main() {
38 // Two circles with radius 3 centered at (0, 0) and (4, 0).
39 let x = Exp::var("x");
40 let y = Exp::var("y");
41
42 let eq1 = circle_eq(&x, &y, 0.0, 0.0, 3.0);
43 let eq2 = circle_eq(&x, &y, 4.0, 0.0, 3.0);
44
45 let compiled = Compiler::compile(&[eq1, eq2]).expect("compile failed");
46 let solver = NewtonRaphsonSolver::new(compiled);
47
48 let mut initial = HashMap::new();
49 initial.insert("x".to_string(), 2.0);
50 initial.insert("y".to_string(), 1.0);
51
52 let solution = solver.solve(initial).expect("solve failed");
53 let x_sol = solution.values.get("x").copied().unwrap();
54 let y_sol = solution.values.get("y").copied().unwrap();
55
56 println!("intersection: x={:.6}, y={:.6}", x_sol, y_sol);
57}More examples
examples/circuit_voltage_divider.rs (line 30)
28fn main() {
29 // Simple resistive divider with explicit currents.
30 let vout = Exp::var("vout");
31 let i1 = Exp::var("i1");
32 let i2 = Exp::var("i2");
33
34 let vin = 12.0;
35 let r1 = 1_000.0;
36 let r2 = 2_000.0;
37
38 // (Vin - Vout) / R1 = I1
39 let eq1 = Exp::sub(
40 Exp::div(Exp::sub(Exp::val(vin), vout.clone()), Exp::val(r1)),
41 i1.clone(),
42 );
43 // Vout / R2 = I2
44 let eq2 = Exp::sub(Exp::div(vout.clone(), Exp::val(r2)), i2.clone());
45 // KCL: I1 = I2
46 let eq3 = Exp::sub(i1.clone(), i2.clone());
47
48 let compiled = Compiler::compile(&[eq1, eq2, eq3]).expect("compile failed");
49 let solver = NewtonRaphsonSolver::new(compiled);
50
51 let mut initial = HashMap::new();
52 initial.insert("vout".to_string(), 8.0);
53 initial.insert("i1".to_string(), 0.004);
54 initial.insert("i2".to_string(), 0.004);
55
56 let solution = solver.solve(initial).expect("solve failed");
57 let vout_sol = solution.values.get("vout").copied().unwrap();
58 let i_sol = solution.values.get("i1").copied().unwrap();
59
60 println!("vout={:.6} V, current={:.6} A", vout_sol, i_sol);
61}examples/circuit_diode.rs (line 30)
28fn main() {
29 // Series resistor + diode with Shockley equation.
30 let i = Exp::var("i");
31 let vd = Exp::var("vd");
32
33 let vs = 5.0;
34 let r = 1_000.0;
35 let isat = 1e-12;
36 let n = 1.0;
37 let vt = 0.02585;
38
39 // KVL: Vs - I*R - Vd = 0
40 let kvl = Exp::sub(Exp::sub(Exp::val(vs), Exp::mul(i.clone(), Exp::val(r))), vd.clone());
41
42 // I - Is * (exp(Vd / (n*Vt)) - 1) = 0
43 let exp_arg = Exp::div(vd.clone(), Exp::val(n * vt));
44 let diode_i = Exp::mul(
45 Exp::val(isat),
46 Exp::sub(Exp::exp(exp_arg), Exp::val(1.0)),
47 );
48 let diode_eq = Exp::sub(i.clone(), diode_i);
49
50 let compiled = Compiler::compile(&[kvl, diode_eq]).expect("compile failed");
51 let solver = NewtonRaphsonSolver::new(compiled);
52
53 let mut initial = HashMap::new();
54 initial.insert("i".to_string(), 0.005);
55 initial.insert("vd".to_string(), 0.7);
56
57 let solution = solver.solve(initial).expect("solve failed");
58 let i_sol = solution.values.get("i").copied().unwrap();
59 let vd_sol = solution.values.get("vd").copied().unwrap();
60
61 println!("diode: i={:.6} A, vd={:.6} V", i_sol, vd_sol);
62}examples/cad_segment_horizontal.rs (line 30)
28fn main() {
29 // Find point B given fixed A, fixed length, and horizontal constraint.
30 let ax = Exp::var("ax");
31 let ay = Exp::var("ay");
32 let bx = Exp::var("bx");
33 let by = Exp::var("by");
34
35 let length = 5.0;
36 let y_target = 2.0;
37
38 let dx = Exp::sub(bx.clone(), ax.clone());
39 let dy = Exp::sub(by.clone(), ay.clone());
40 let length_eq = Exp::sub(
41 Exp::add(Exp::power(dx, 2.0), Exp::power(dy, 2.0)),
42 Exp::val(length * length),
43 );
44 let horizontal_eq = Exp::sub(by.clone(), Exp::val(y_target));
45
46 let compiled = Compiler::compile(&[length_eq, horizontal_eq]).expect("compile failed");
47 let solver = NewtonRaphsonSolver::new_with_variables(compiled, &["bx", "by"])
48 .expect("solver init failed");
49
50 let mut initial = HashMap::new();
51 initial.insert("ax".to_string(), 1.0);
52 initial.insert("ay".to_string(), 2.0);
53 initial.insert("bx".to_string(), 6.0);
54 initial.insert("by".to_string(), 2.0);
55
56 let solution = solver.solve(initial).expect("solve failed");
57 let bx_sol = solution.values.get("bx").copied().unwrap();
58 let by_sol = solution.values.get("by").copied().unwrap();
59
60 println!("B = ({:.6}, {:.6})", bx_sol, by_sol);
61}Sourcepub fn val(v: f64) -> Self
pub fn val(v: f64) -> Self
Examples found in repository?
More examples
examples/circuit_voltage_divider.rs (line 40)
28fn main() {
29 // Simple resistive divider with explicit currents.
30 let vout = Exp::var("vout");
31 let i1 = Exp::var("i1");
32 let i2 = Exp::var("i2");
33
34 let vin = 12.0;
35 let r1 = 1_000.0;
36 let r2 = 2_000.0;
37
38 // (Vin - Vout) / R1 = I1
39 let eq1 = Exp::sub(
40 Exp::div(Exp::sub(Exp::val(vin), vout.clone()), Exp::val(r1)),
41 i1.clone(),
42 );
43 // Vout / R2 = I2
44 let eq2 = Exp::sub(Exp::div(vout.clone(), Exp::val(r2)), i2.clone());
45 // KCL: I1 = I2
46 let eq3 = Exp::sub(i1.clone(), i2.clone());
47
48 let compiled = Compiler::compile(&[eq1, eq2, eq3]).expect("compile failed");
49 let solver = NewtonRaphsonSolver::new(compiled);
50
51 let mut initial = HashMap::new();
52 initial.insert("vout".to_string(), 8.0);
53 initial.insert("i1".to_string(), 0.004);
54 initial.insert("i2".to_string(), 0.004);
55
56 let solution = solver.solve(initial).expect("solve failed");
57 let vout_sol = solution.values.get("vout").copied().unwrap();
58 let i_sol = solution.values.get("i1").copied().unwrap();
59
60 println!("vout={:.6} V, current={:.6} A", vout_sol, i_sol);
61}examples/circuit_diode.rs (line 40)
28fn main() {
29 // Series resistor + diode with Shockley equation.
30 let i = Exp::var("i");
31 let vd = Exp::var("vd");
32
33 let vs = 5.0;
34 let r = 1_000.0;
35 let isat = 1e-12;
36 let n = 1.0;
37 let vt = 0.02585;
38
39 // KVL: Vs - I*R - Vd = 0
40 let kvl = Exp::sub(Exp::sub(Exp::val(vs), Exp::mul(i.clone(), Exp::val(r))), vd.clone());
41
42 // I - Is * (exp(Vd / (n*Vt)) - 1) = 0
43 let exp_arg = Exp::div(vd.clone(), Exp::val(n * vt));
44 let diode_i = Exp::mul(
45 Exp::val(isat),
46 Exp::sub(Exp::exp(exp_arg), Exp::val(1.0)),
47 );
48 let diode_eq = Exp::sub(i.clone(), diode_i);
49
50 let compiled = Compiler::compile(&[kvl, diode_eq]).expect("compile failed");
51 let solver = NewtonRaphsonSolver::new(compiled);
52
53 let mut initial = HashMap::new();
54 initial.insert("i".to_string(), 0.005);
55 initial.insert("vd".to_string(), 0.7);
56
57 let solution = solver.solve(initial).expect("solve failed");
58 let i_sol = solution.values.get("i").copied().unwrap();
59 let vd_sol = solution.values.get("vd").copied().unwrap();
60
61 println!("diode: i={:.6} A, vd={:.6} V", i_sol, vd_sol);
62}examples/cad_segment_horizontal.rs (line 42)
28fn main() {
29 // Find point B given fixed A, fixed length, and horizontal constraint.
30 let ax = Exp::var("ax");
31 let ay = Exp::var("ay");
32 let bx = Exp::var("bx");
33 let by = Exp::var("by");
34
35 let length = 5.0;
36 let y_target = 2.0;
37
38 let dx = Exp::sub(bx.clone(), ax.clone());
39 let dy = Exp::sub(by.clone(), ay.clone());
40 let length_eq = Exp::sub(
41 Exp::add(Exp::power(dx, 2.0), Exp::power(dy, 2.0)),
42 Exp::val(length * length),
43 );
44 let horizontal_eq = Exp::sub(by.clone(), Exp::val(y_target));
45
46 let compiled = Compiler::compile(&[length_eq, horizontal_eq]).expect("compile failed");
47 let solver = NewtonRaphsonSolver::new_with_variables(compiled, &["bx", "by"])
48 .expect("solver init failed");
49
50 let mut initial = HashMap::new();
51 initial.insert("ax".to_string(), 1.0);
52 initial.insert("ay".to_string(), 2.0);
53 initial.insert("bx".to_string(), 6.0);
54 initial.insert("by".to_string(), 2.0);
55
56 let solution = solver.solve(initial).expect("solve failed");
57 let bx_sol = solution.values.get("bx").copied().unwrap();
58 let by_sol = solution.values.get("by").copied().unwrap();
59
60 println!("B = ({:.6}, {:.6})", bx_sol, by_sol);
61}Sourcepub fn add(lhs: Exp, rhs: Exp) -> Self
pub fn add(lhs: Exp, rhs: Exp) -> Self
Examples found in repository?
More examples
examples/cad_segment_horizontal.rs (line 41)
28fn main() {
29 // Find point B given fixed A, fixed length, and horizontal constraint.
30 let ax = Exp::var("ax");
31 let ay = Exp::var("ay");
32 let bx = Exp::var("bx");
33 let by = Exp::var("by");
34
35 let length = 5.0;
36 let y_target = 2.0;
37
38 let dx = Exp::sub(bx.clone(), ax.clone());
39 let dy = Exp::sub(by.clone(), ay.clone());
40 let length_eq = Exp::sub(
41 Exp::add(Exp::power(dx, 2.0), Exp::power(dy, 2.0)),
42 Exp::val(length * length),
43 );
44 let horizontal_eq = Exp::sub(by.clone(), Exp::val(y_target));
45
46 let compiled = Compiler::compile(&[length_eq, horizontal_eq]).expect("compile failed");
47 let solver = NewtonRaphsonSolver::new_with_variables(compiled, &["bx", "by"])
48 .expect("solver init failed");
49
50 let mut initial = HashMap::new();
51 initial.insert("ax".to_string(), 1.0);
52 initial.insert("ay".to_string(), 2.0);
53 initial.insert("bx".to_string(), 6.0);
54 initial.insert("by".to_string(), 2.0);
55
56 let solution = solver.solve(initial).expect("solve failed");
57 let bx_sol = solution.values.get("bx").copied().unwrap();
58 let by_sol = solution.values.get("by").copied().unwrap();
59
60 println!("B = ({:.6}, {:.6})", bx_sol, by_sol);
61}Sourcepub fn sub(lhs: Exp, rhs: Exp) -> Self
pub fn sub(lhs: Exp, rhs: Exp) -> Self
Examples found in repository?
More examples
examples/circuit_voltage_divider.rs (lines 39-42)
28fn main() {
29 // Simple resistive divider with explicit currents.
30 let vout = Exp::var("vout");
31 let i1 = Exp::var("i1");
32 let i2 = Exp::var("i2");
33
34 let vin = 12.0;
35 let r1 = 1_000.0;
36 let r2 = 2_000.0;
37
38 // (Vin - Vout) / R1 = I1
39 let eq1 = Exp::sub(
40 Exp::div(Exp::sub(Exp::val(vin), vout.clone()), Exp::val(r1)),
41 i1.clone(),
42 );
43 // Vout / R2 = I2
44 let eq2 = Exp::sub(Exp::div(vout.clone(), Exp::val(r2)), i2.clone());
45 // KCL: I1 = I2
46 let eq3 = Exp::sub(i1.clone(), i2.clone());
47
48 let compiled = Compiler::compile(&[eq1, eq2, eq3]).expect("compile failed");
49 let solver = NewtonRaphsonSolver::new(compiled);
50
51 let mut initial = HashMap::new();
52 initial.insert("vout".to_string(), 8.0);
53 initial.insert("i1".to_string(), 0.004);
54 initial.insert("i2".to_string(), 0.004);
55
56 let solution = solver.solve(initial).expect("solve failed");
57 let vout_sol = solution.values.get("vout").copied().unwrap();
58 let i_sol = solution.values.get("i1").copied().unwrap();
59
60 println!("vout={:.6} V, current={:.6} A", vout_sol, i_sol);
61}examples/circuit_diode.rs (line 40)
28fn main() {
29 // Series resistor + diode with Shockley equation.
30 let i = Exp::var("i");
31 let vd = Exp::var("vd");
32
33 let vs = 5.0;
34 let r = 1_000.0;
35 let isat = 1e-12;
36 let n = 1.0;
37 let vt = 0.02585;
38
39 // KVL: Vs - I*R - Vd = 0
40 let kvl = Exp::sub(Exp::sub(Exp::val(vs), Exp::mul(i.clone(), Exp::val(r))), vd.clone());
41
42 // I - Is * (exp(Vd / (n*Vt)) - 1) = 0
43 let exp_arg = Exp::div(vd.clone(), Exp::val(n * vt));
44 let diode_i = Exp::mul(
45 Exp::val(isat),
46 Exp::sub(Exp::exp(exp_arg), Exp::val(1.0)),
47 );
48 let diode_eq = Exp::sub(i.clone(), diode_i);
49
50 let compiled = Compiler::compile(&[kvl, diode_eq]).expect("compile failed");
51 let solver = NewtonRaphsonSolver::new(compiled);
52
53 let mut initial = HashMap::new();
54 initial.insert("i".to_string(), 0.005);
55 initial.insert("vd".to_string(), 0.7);
56
57 let solution = solver.solve(initial).expect("solve failed");
58 let i_sol = solution.values.get("i").copied().unwrap();
59 let vd_sol = solution.values.get("vd").copied().unwrap();
60
61 println!("diode: i={:.6} A, vd={:.6} V", i_sol, vd_sol);
62}examples/cad_segment_horizontal.rs (line 38)
28fn main() {
29 // Find point B given fixed A, fixed length, and horizontal constraint.
30 let ax = Exp::var("ax");
31 let ay = Exp::var("ay");
32 let bx = Exp::var("bx");
33 let by = Exp::var("by");
34
35 let length = 5.0;
36 let y_target = 2.0;
37
38 let dx = Exp::sub(bx.clone(), ax.clone());
39 let dy = Exp::sub(by.clone(), ay.clone());
40 let length_eq = Exp::sub(
41 Exp::add(Exp::power(dx, 2.0), Exp::power(dy, 2.0)),
42 Exp::val(length * length),
43 );
44 let horizontal_eq = Exp::sub(by.clone(), Exp::val(y_target));
45
46 let compiled = Compiler::compile(&[length_eq, horizontal_eq]).expect("compile failed");
47 let solver = NewtonRaphsonSolver::new_with_variables(compiled, &["bx", "by"])
48 .expect("solver init failed");
49
50 let mut initial = HashMap::new();
51 initial.insert("ax".to_string(), 1.0);
52 initial.insert("ay".to_string(), 2.0);
53 initial.insert("bx".to_string(), 6.0);
54 initial.insert("by".to_string(), 2.0);
55
56 let solution = solver.solve(initial).expect("solve failed");
57 let bx_sol = solution.values.get("bx").copied().unwrap();
58 let by_sol = solution.values.get("by").copied().unwrap();
59
60 println!("B = ({:.6}, {:.6})", bx_sol, by_sol);
61}Sourcepub fn mul(lhs: Exp, rhs: Exp) -> Self
pub fn mul(lhs: Exp, rhs: Exp) -> Self
Examples found in repository?
examples/circuit_diode.rs (line 40)
28fn main() {
29 // Series resistor + diode with Shockley equation.
30 let i = Exp::var("i");
31 let vd = Exp::var("vd");
32
33 let vs = 5.0;
34 let r = 1_000.0;
35 let isat = 1e-12;
36 let n = 1.0;
37 let vt = 0.02585;
38
39 // KVL: Vs - I*R - Vd = 0
40 let kvl = Exp::sub(Exp::sub(Exp::val(vs), Exp::mul(i.clone(), Exp::val(r))), vd.clone());
41
42 // I - Is * (exp(Vd / (n*Vt)) - 1) = 0
43 let exp_arg = Exp::div(vd.clone(), Exp::val(n * vt));
44 let diode_i = Exp::mul(
45 Exp::val(isat),
46 Exp::sub(Exp::exp(exp_arg), Exp::val(1.0)),
47 );
48 let diode_eq = Exp::sub(i.clone(), diode_i);
49
50 let compiled = Compiler::compile(&[kvl, diode_eq]).expect("compile failed");
51 let solver = NewtonRaphsonSolver::new(compiled);
52
53 let mut initial = HashMap::new();
54 initial.insert("i".to_string(), 0.005);
55 initial.insert("vd".to_string(), 0.7);
56
57 let solution = solver.solve(initial).expect("solve failed");
58 let i_sol = solution.values.get("i").copied().unwrap();
59 let vd_sol = solution.values.get("vd").copied().unwrap();
60
61 println!("diode: i={:.6} A, vd={:.6} V", i_sol, vd_sol);
62}Sourcepub fn div(lhs: Exp, rhs: Exp) -> Self
pub fn div(lhs: Exp, rhs: Exp) -> Self
Examples found in repository?
examples/circuit_voltage_divider.rs (line 40)
28fn main() {
29 // Simple resistive divider with explicit currents.
30 let vout = Exp::var("vout");
31 let i1 = Exp::var("i1");
32 let i2 = Exp::var("i2");
33
34 let vin = 12.0;
35 let r1 = 1_000.0;
36 let r2 = 2_000.0;
37
38 // (Vin - Vout) / R1 = I1
39 let eq1 = Exp::sub(
40 Exp::div(Exp::sub(Exp::val(vin), vout.clone()), Exp::val(r1)),
41 i1.clone(),
42 );
43 // Vout / R2 = I2
44 let eq2 = Exp::sub(Exp::div(vout.clone(), Exp::val(r2)), i2.clone());
45 // KCL: I1 = I2
46 let eq3 = Exp::sub(i1.clone(), i2.clone());
47
48 let compiled = Compiler::compile(&[eq1, eq2, eq3]).expect("compile failed");
49 let solver = NewtonRaphsonSolver::new(compiled);
50
51 let mut initial = HashMap::new();
52 initial.insert("vout".to_string(), 8.0);
53 initial.insert("i1".to_string(), 0.004);
54 initial.insert("i2".to_string(), 0.004);
55
56 let solution = solver.solve(initial).expect("solve failed");
57 let vout_sol = solution.values.get("vout").copied().unwrap();
58 let i_sol = solution.values.get("i1").copied().unwrap();
59
60 println!("vout={:.6} V, current={:.6} A", vout_sol, i_sol);
61}More examples
examples/circuit_diode.rs (line 43)
28fn main() {
29 // Series resistor + diode with Shockley equation.
30 let i = Exp::var("i");
31 let vd = Exp::var("vd");
32
33 let vs = 5.0;
34 let r = 1_000.0;
35 let isat = 1e-12;
36 let n = 1.0;
37 let vt = 0.02585;
38
39 // KVL: Vs - I*R - Vd = 0
40 let kvl = Exp::sub(Exp::sub(Exp::val(vs), Exp::mul(i.clone(), Exp::val(r))), vd.clone());
41
42 // I - Is * (exp(Vd / (n*Vt)) - 1) = 0
43 let exp_arg = Exp::div(vd.clone(), Exp::val(n * vt));
44 let diode_i = Exp::mul(
45 Exp::val(isat),
46 Exp::sub(Exp::exp(exp_arg), Exp::val(1.0)),
47 );
48 let diode_eq = Exp::sub(i.clone(), diode_i);
49
50 let compiled = Compiler::compile(&[kvl, diode_eq]).expect("compile failed");
51 let solver = NewtonRaphsonSolver::new(compiled);
52
53 let mut initial = HashMap::new();
54 initial.insert("i".to_string(), 0.005);
55 initial.insert("vd".to_string(), 0.7);
56
57 let solution = solver.solve(initial).expect("solve failed");
58 let i_sol = solution.values.get("i").copied().unwrap();
59 let vd_sol = solution.values.get("vd").copied().unwrap();
60
61 println!("diode: i={:.6} A, vd={:.6} V", i_sol, vd_sol);
62}Sourcepub fn power(base: Exp, exp: f64) -> Self
pub fn power(base: Exp, exp: f64) -> Self
Examples found in repository?
More examples
examples/cad_segment_horizontal.rs (line 41)
28fn main() {
29 // Find point B given fixed A, fixed length, and horizontal constraint.
30 let ax = Exp::var("ax");
31 let ay = Exp::var("ay");
32 let bx = Exp::var("bx");
33 let by = Exp::var("by");
34
35 let length = 5.0;
36 let y_target = 2.0;
37
38 let dx = Exp::sub(bx.clone(), ax.clone());
39 let dy = Exp::sub(by.clone(), ay.clone());
40 let length_eq = Exp::sub(
41 Exp::add(Exp::power(dx, 2.0), Exp::power(dy, 2.0)),
42 Exp::val(length * length),
43 );
44 let horizontal_eq = Exp::sub(by.clone(), Exp::val(y_target));
45
46 let compiled = Compiler::compile(&[length_eq, horizontal_eq]).expect("compile failed");
47 let solver = NewtonRaphsonSolver::new_with_variables(compiled, &["bx", "by"])
48 .expect("solver init failed");
49
50 let mut initial = HashMap::new();
51 initial.insert("ax".to_string(), 1.0);
52 initial.insert("ay".to_string(), 2.0);
53 initial.insert("bx".to_string(), 6.0);
54 initial.insert("by".to_string(), 2.0);
55
56 let solution = solver.solve(initial).expect("solve failed");
57 let bx_sol = solution.values.get("bx").copied().unwrap();
58 let by_sol = solution.values.get("by").copied().unwrap();
59
60 println!("B = ({:.6}, {:.6})", bx_sol, by_sol);
61}pub fn neg(exp: Exp) -> Self
pub fn sin(exp: Exp) -> Self
pub fn cos(exp: Exp) -> Self
pub fn ln(exp: Exp) -> Self
Sourcepub fn exp(exp: Exp) -> Self
pub fn exp(exp: Exp) -> Self
Examples found in repository?
examples/circuit_diode.rs (line 46)
28fn main() {
29 // Series resistor + diode with Shockley equation.
30 let i = Exp::var("i");
31 let vd = Exp::var("vd");
32
33 let vs = 5.0;
34 let r = 1_000.0;
35 let isat = 1e-12;
36 let n = 1.0;
37 let vt = 0.02585;
38
39 // KVL: Vs - I*R - Vd = 0
40 let kvl = Exp::sub(Exp::sub(Exp::val(vs), Exp::mul(i.clone(), Exp::val(r))), vd.clone());
41
42 // I - Is * (exp(Vd / (n*Vt)) - 1) = 0
43 let exp_arg = Exp::div(vd.clone(), Exp::val(n * vt));
44 let diode_i = Exp::mul(
45 Exp::val(isat),
46 Exp::sub(Exp::exp(exp_arg), Exp::val(1.0)),
47 );
48 let diode_eq = Exp::sub(i.clone(), diode_i);
49
50 let compiled = Compiler::compile(&[kvl, diode_eq]).expect("compile failed");
51 let solver = NewtonRaphsonSolver::new(compiled);
52
53 let mut initial = HashMap::new();
54 initial.insert("i".to_string(), 0.005);
55 initial.insert("vd".to_string(), 0.7);
56
57 let solution = solver.solve(initial).expect("solve failed");
58 let i_sol = solution.values.get("i").copied().unwrap();
59 let vd_sol = solution.values.get("vd").copied().unwrap();
60
61 println!("diode: i={:.6} A, vd={:.6} V", i_sol, vd_sol);
62}pub fn evaluate_checked( &self, vars: &HashMap<String, f64>, ) -> Result<f64, MissingVarError>
pub fn evaluate(&self, vars: &HashMap<String, f64>) -> f64
pub fn differentiate(&self, var_name: &str) -> Exp
pub fn simplify(&self) -> Exp
Trait Implementations§
impl StructuralPartialEq for Exp
Auto Trait Implementations§
impl Freeze for Exp
impl RefUnwindSafe for Exp
impl Send for Exp
impl Sync for Exp
impl Unpin for Exp
impl UnwindSafe for Exp
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more