A comprehensive electrical engineering calculation library for circuit analysis and power systems.
test "dc circuit" {
// Ohm's law: V = I * R
// V = 2A * 10Ω = 20V
let v = ohm_law_voltage(2.0, 10.0)
assert_eq(v, 20.0)
// Current: I = V / R = 20V / 10Ω = 2A
let i = ohm_law_current(20.0, 10.0)
assert_eq(i, 2.0)
// Power: P = V * I = 20V * 2A = 40W
let p = power_vi(20.0, 2.0)
assert_eq(p, 40.0)
// Power: P = I² * R = 2² * 10 = 40W
let p2 = power_resistance(2.0, 10.0)
assert_eq(p2, 40.0)
// Resistor series: R = 10 + 20 = 30Ω
let rs = resistance_series(10.0, 20.0)
assert_eq(rs, 30.0)
// Resistor parallel: R = 1/(1/10 + 1/20) = 6.67Ω
let rp = resistance_parallel(10.0, 20.0)
assert_eq(approx_equal(rp, 6.67, 0.1), true)
// Voltage divider: V_out = 10V * 20/(10+20) = 6.67V
let vout = voltage_divider(10.0, 10.0, 20.0)
assert_eq(approx_equal(vout, 6.67, 0.1), true)
}test "ac circuit" {
// Angular frequency: ω = 2πf = 2π*50 = 314.16 rad/s
let omega = angular_frequency(50.0)
assert_eq(approx_equal(omega, 314.16, 1.0), true)
// Inductive reactance: X_L = 2πfL = 2π*50*0.1 = 31.42Ω
let xl = inductive_reactance(50.0, 0.1)
assert_eq(approx_equal(xl, 31.42, 0.5), true)
// Capacitive reactance: X_C = 1/(2πfC) = 1/(2π*50*100e-6) = 31.83Ω
let xc = capacitive_reactance(50.0, 100.0e-6)
assert_eq(approx_equal(xc, 31.83, 0.5), true)
// Resonance frequency: f_0 = 1/(2π√(LC)) = 1/(2π√(0.1*100e-6)) = 50.33Hz
let f0 = resonance_frequency(0.1, 100.0e-6)
assert_eq(approx_equal(f0, 50.33, 1.0), true)
}test "ac power" {
// Apparent power: S = V * I = 220V * 10A = 2200VA
let s = apparent_power(220.0, 10.0)
assert_eq(s, 2200.0)
// Active power: P = V*I*cos(φ) = 220*10*0.85 = 1870W
let phi = power_factor_angle(0.85)
let p = active_power(220.0, 10.0, phi)
assert_eq(approx_equal(p, 1870.0, 10.0), true)
// Power factor: PF = P/S = 1870/2200 = 0.85
let pf = power_factor(1870.0, 2200.0)
assert_eq(approx_equal(pf, 0.85, 0.01), true)
}test "transformer" {
// Turns ratio: n = N1/N2 = 1000/100 = 10
let n = transformer_turns_ratio(1000, 100)
assert_eq(n, 10.0)
// Voltage ratio: V2 = V1*N2/N1 = 220*100/1000 = 22V
let v2 = transformer_voltage_ratio(220.0, 1000, 100)
assert_eq(v2, 22.0)
// Current ratio: I2 = I1*N1/N2 = 1*1000/100 = 10A
let i2 = transformer_current_ratio(1.0, 1000, 100)
assert_eq(i2, 10.0)
}test "motor" {
// Synchronous speed: n_s = 60*f/p = 60*50/2 = 1500 rpm
let ns = synchronous_speed(50.0, 2)
assert_eq(ns, 1500.0)
// Slip: s = (1500-1440)/1500 = 0.04
let s = slip(1500.0, 1440.0)
assert_eq(approx_equal(s, 0.04, 0.001), true)
// Actual speed: n = 1500*(1-0.04) = 1440 rpm
let n = induction_motor_speed(1500.0, 0.04)
assert_eq(n, 1440.0)
}test "three phase" {
// Three-phase power: P = √3*V_L*I_L*PF = √3*380*10*0.85 = 5594W
let p = three_phase_power_star(380.0, 10.0, 0.85)
assert_eq(approx_equal(p, 5594.0, 10.0), true)
// Line to phase voltage: V_ph = 380/√3 = 219.4V
let vph = line_to_phase_voltage_star(380.0)
assert_eq(approx_equal(vph, 219.4, 0.5), true)
}test "complex" {
// Complex number: z = 3 + 4i
let z = complex(3.0, 4.0)
assert_eq(z.real, 3.0)
assert_eq(z.imag, 4.0)
// Magnitude: |z| = √(3²+4²) = 5
let mag = complex_magnitude(z)
assert_eq(mag, 5.0)
// Phase angle: φ = arctan(4/3) = 53.13°
let phase = complex_phase(z)
assert_eq(approx_equal(rad_to_deg(phase), 53.13, 1.0), true)
// Complex addition: (3+4i) + (1+2i) = 4+6i
let z1 = complex(3.0, 4.0)
let z2 = complex(1.0, 2.0)
let z_sum = complex_add(z1, z2)
assert_eq(z_sum.real, 4.0)
assert_eq(z_sum.imag, 6.0)
}test "signal processing" {
// RMS value: RMS = A/√2 = 10/√2 = 7.07V
let rms = rms_sine(10.0)
assert_eq(approx_equal(rms, 7.07, 0.1), true)
// Peak to RMS: 10V -> 7.07V
let rms2 = peak_to_rms(10.0)
assert_eq(approx_equal(rms2, 7.07, 0.1), true)
// RMS to peak: 7.07V -> 10V
let peak = rms_to_peak(7.07)
assert_eq(approx_equal(peak, 10.0, 0.1), true)
}test "filter" {
// Low-pass cutoff frequency: f_c = 1/(2πRC) = 1/(2π*1000*1e-6) = 159.15Hz
let fc = lowpass_cutoff_frequency(1000.0, 1.0e-6)
assert_eq(approx_equal(fc, 159.15, 1.0), true)
// Band-pass center frequency: f_0 = 1/(2π√(LC)) = 1/(2π√(0.001*1e-6)) = 5033Hz
let f0 = bandpass_center_frequency(0.001, 1.0e-6)
assert_eq(approx_equal(f0, 5033.0, 10.0), true)
}test "electronics" {
// Diode forward voltage: Silicon diode 0.7V
let vf = diode_forward_voltage(true)
assert_eq(vf, 0.7)
// Op-amp inverting gain: A_v = -R_f/R_in = -10k/1k = -10
let av = opamp_inverting_gain(10000.0, 1000.0)
assert_eq(av, -10.0)
// Op-amp non-inverting gain: A_v = 1 + R_f/R_in = 1 + 10k/1k = 11
let av2 = opamp_non_inverting_gain(10000.0, 1000.0)
assert_eq(av2, 11.0)
}test "control" {
// First-order system time constant: τ = RC = 1000*1e-6 = 0.001s
let tau = first_order_time_constant(1000.0, 1.0e-6)
assert_eq(tau, 0.001)
// First-order step response: y(0.001) = 1*(1-exp(-1)) = 0.632
let y = first_order_step_response(1.0, 0.001, 0.001)
assert_eq(approx_equal(y, 0.632, 0.01), true)
}test "signal analysis" {
// Decibel to voltage ratio: 20dB -> 10^(20/20) = 10
let ratio = db_to_voltage_ratio(20.0)
assert_eq(approx_equal(ratio, 10.0, 0.1), true)
// Voltage ratio to decibel: 10 -> 20*log10(10) = 20dB
let db = voltage_ratio_to_db(10.0)
assert_eq(approx_equal(db, 20.0, 0.1), true)
}test "capacitor inductor" {
// Capacitor charging: V(τ) = V0*(1-exp(-1)) = 10*0.632 = 6.32V
let vc = capacitor_charging_voltage(10.0, 0.001, 1000.0, 1.0e-6)
assert_eq(approx_equal(vc, 6.32, 0.1), true)
// Capacitor discharging: V(τ) = V0*exp(-1) = 10*0.368 = 3.68V
let vd = capacitor_discharging_voltage(10.0, 0.001, 1000.0, 1.0e-6)
assert_eq(approx_equal(vd, 3.68, 0.1), true)
}moon testfn active_power(voltage : Double, current : Double, phase_angle_rad : Double) -> Doublefn apparent_power_from_pq(active_power : Double, reactive_power : Double) -> Doublefn bandpass_bandwidth(r : Double, l : Double) -> Doublefn bandpass_center_frequency(l : Double, c : Double) -> Doublefn capacitive_reactance(freq : Double, capacitance : Double) -> Doublefn capacitor_charging_voltage(v0 : Double, t : Double, r : Double, c : Double) -> Doublefn capacitor_discharging_voltage(v0 : Double, t : Double, r : Double, c : Double) -> Doublefn characteristic_impedance(inductance : Double, capacitance : Double) -> Doublefn current_divider(itotal : Double, r1 : Double, r2 : Double) -> Doublefn diode_forward_voltage(is_silicon : Bool) -> Doublefn diode_reverse_saturation_current(i0 : Double, eg : Double, temp : Double) -> Doublefn first_order_step_response(k : Double, t : Double, tau : Double) -> Doublefn first_order_time_constant(r : Double, c_or_l : Double) -> Doublefn fourier_coefficient_square_wave(amplitude : Double, n : Int) -> Doublefn highpass_cutoff_frequency(r : Double, c : Double) -> Doublefn impedance_rlc_series(resistance : Double, freq : Double, inductance : Double, capacitance : Double) -> Complexfn induction_motor_speed(sync_speed : Double, slip_ratio : Double) -> Doublefn inductive_reactance(freq : Double, inductance : Double) -> Doublefn inductor_charging_current(v : Double, r : Double, l : Double, t : Double) -> Doublefn inductor_discharging_current(i0 : Double, r : Double, l : Double, t : Double) -> Doublefn line_reactance(freq : Double, inductance_per_unit : Double, length : Double) -> Doublefn line_resistance(rho : Double, l : Double, a : Double) -> Doublefn line_susceptance(freq : Double, capacitance_per_unit : Double, length : Double) -> Doublefn line_to_phase_current_delta(il : Double) -> Doublefn line_to_phase_current_star(il : Double) -> Doublefn line_to_phase_voltage_star(vl : Double) -> Doublefn lowpass_cutoff_frequency(r : Double, c : Double) -> Doublefn motor_efficiency(p_out : Double, p_in : Double) -> Doublefn motor_input_power(voltage : Double, current : Double, pf : Double) -> Doublefn motor_output_power(torque : Double, omega : Double) -> Doublefn ohm_law_current(voltage : Double, resistance : Double) -> Doublefn ohm_law_resistance(voltage : Double, current : Double) -> Doublefn ohm_law_voltage(current : Double, resistance : Double) -> Doublefn opamp_inverting_gain(rf : Double, rin : Double) -> Doublefn opamp_non_inverting_gain(rf : Double, rin : Double) -> Doublefn phase_to_line_voltage_star(vph : Double) -> Doublefn pid_controller_output(kp : Double, ki : Double, kd : Double, error : Double, integral : Double, derivative : Double) -> Doublefn power_factor(active_power : Double, apparent_power : Double) -> Doublefn power_ratio_to_db(p_ratio : Double) -> Doublefn power_resistance(current : Double, resistance : Double) -> Doublefn power_resistance_voltage(voltage : Double, resistance : Double) -> Doublefn quality_factor_rlc(freq : Double, inductance : Double, resistance : Double) -> Doublefn reactive_power(voltage : Double, current : Double, phase_angle_rad : Double) -> Doublefn resistance_parallel(r1 : Double, r2 : Double) -> Doublefn resistance_series(r1 : Double, r2 : Double) -> Doublefn resistance_temperature(r0 : Double, alpha : Double, t : Double, t0 : Double) -> Doublefn resonance_frequency(inductance : Double, capacitance : Double) -> Doublefn rms_sine(amplitude : Double) -> Doublefn second_order_damping_ratio(c_or_r : Double, k_or_l : Double, m_or_c : Double) -> Doublefn second_order_natural_frequency(k_or_l : Double, m_or_c : Double) -> Doublefn short_circuit_current(voltage : Double, impedance : Double) -> Doublefn signal_bandwidth(f_high : Double, f_low : Double) -> Doublefn signal_to_noise_ratio_db(v_signal : Double, v_noise : Double) -> Doublefn slip(sync_speed : Double, actual_speed : Double) -> Doublefn synchronous_speed(freq : Double, pole_pairs : Int) -> Doublefn three_phase_power_delta(vl : Double, il : Double, pf : Double) -> Doublefn three_phase_power_star(vl : Double, il : Double, pf : Double) -> Doublefn transformer_current_ratio(i1 : Double, n1 : Int, n2 : Int) -> Doublefn transformer_efficiency(p_out : Double, p_in : Double) -> Doublefn transformer_impedance_transformation(z1 : Double, n1 : Int, n2 : Int) -> Doublefn transformer_loss(p_in : Double, p_out : Double) -> Doublefn transformer_turns_ratio(n1 : Int, n2 : Int) -> Doublefn transformer_voltage_ratio(v1 : Double, n1 : Int, n2 : Int) -> Doublefn transistor_beta(ic : Double, ib : Double) -> Doublefn transistor_collector_current(beta : Double, ib : Double) -> Doublefn voltage_divider(vin : Double, r1 : Double, r2 : Double) -> Doublefn voltage_drop_percentage(voltage_drop : Double, voltage : Double) -> Doublefn voltage_ratio_to_db(v_ratio : Double) -> DoubleInstall
Download zipA comprehensive electrical engineering calculation library for circuit analysis and power systems.