Skip to main content

Exp

Enum Exp 

Source
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

Source

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
Hide additional 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}
Source

pub fn val(v: f64) -> Self

Examples found in repository?
examples/cad_circle_intersection.rs (line 29)
28fn circle_eq(x: &Exp, y: &Exp, cx: f64, cy: f64, r: f64) -> Exp {
29    let dx = Exp::sub(x.clone(), Exp::val(cx));
30    let dy = Exp::sub(y.clone(), Exp::val(cy));
31    Exp::sub(
32        Exp::add(Exp::power(dx, 2.0), Exp::power(dy, 2.0)),
33        Exp::val(r * r),
34    )
35}
More examples
Hide additional 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}
Source

pub fn add(lhs: Exp, rhs: Exp) -> Self

Examples found in repository?
examples/cad_circle_intersection.rs (line 32)
28fn circle_eq(x: &Exp, y: &Exp, cx: f64, cy: f64, r: f64) -> Exp {
29    let dx = Exp::sub(x.clone(), Exp::val(cx));
30    let dy = Exp::sub(y.clone(), Exp::val(cy));
31    Exp::sub(
32        Exp::add(Exp::power(dx, 2.0), Exp::power(dy, 2.0)),
33        Exp::val(r * r),
34    )
35}
More examples
Hide additional 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}
Source

pub fn sub(lhs: Exp, rhs: Exp) -> Self

Examples found in repository?
examples/cad_circle_intersection.rs (line 29)
28fn circle_eq(x: &Exp, y: &Exp, cx: f64, cy: f64, r: f64) -> Exp {
29    let dx = Exp::sub(x.clone(), Exp::val(cx));
30    let dy = Exp::sub(y.clone(), Exp::val(cy));
31    Exp::sub(
32        Exp::add(Exp::power(dx, 2.0), Exp::power(dy, 2.0)),
33        Exp::val(r * r),
34    )
35}
More examples
Hide additional 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}
Source

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}
Source

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
Hide additional 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}
Source

pub fn power(base: Exp, exp: f64) -> Self

Examples found in repository?
examples/cad_circle_intersection.rs (line 32)
28fn circle_eq(x: &Exp, y: &Exp, cx: f64, cy: f64, r: f64) -> Exp {
29    let dx = Exp::sub(x.clone(), Exp::val(cx));
30    let dy = Exp::sub(y.clone(), Exp::val(cy));
31    Exp::sub(
32        Exp::add(Exp::power(dx, 2.0), Exp::power(dy, 2.0)),
33        Exp::val(r * r),
34    )
35}
More examples
Hide additional 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}
Source

pub fn neg(exp: Exp) -> Self

Source

pub fn sin(exp: Exp) -> Self

Source

pub fn cos(exp: Exp) -> Self

Source

pub fn ln(exp: Exp) -> Self

Source

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}
Source

pub fn evaluate_checked( &self, vars: &HashMap<String, f64>, ) -> Result<f64, MissingVarError>

Source

pub fn evaluate(&self, vars: &HashMap<String, f64>) -> f64

Source

pub fn differentiate(&self, var_name: &str) -> Exp

Source

pub fn simplify(&self) -> Exp

Trait Implementations§

Source§

impl Clone for Exp

Source§

fn clone(&self) -> Exp

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for Exp

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Display for Exp

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl PartialEq for Exp

Source§

fn eq(&self, other: &Exp) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

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> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T> ToString for T
where T: Display + ?Sized,

Source§

fn to_string(&self) -> String

Converts the given value to a String. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.