code wiki / (root) / nx_calc_chem_test.nx

nx_calc_chem_test.nx source

↩ module page · 182 lines · 7332 B

1// nx_calc_chem_test.nx -- exercise integrate engine + extended chem. 2 3import "nx_calc_integrate.nx" 4import "nx_chem_extended.nx" 5 6// ===== T1: integrate(c) = c * x ================================== 7func t1_integrate_const() -> nx_int { 8 let c: *Term = nx_calc_const_int(7) 9 let r: *Term = nx_calc_integrate(c, NX_CALC_SYM_X) 10 if r.kind != NX_TERM_APP { return 1 } 11 if r.sym != NX_CALC_SYM_MUL { return 1 } 12 return 0 13} 14 15// ===== T2: integrate(x) = x^2 / 2 ================================= 16func t2_integrate_x() -> nx_int { 17 let x: *Term = nx_term_var(NX_CALC_SYM_X) 18 let r: *Term = nx_calc_integrate(x, NX_CALC_SYM_X) 19 if r.kind != NX_TERM_APP { return 2 } 20 if r.sym != NX_CALC_SYM_DIV { return 2 } 21 let num: *Term = nx_term_arg(r, 0) 22 if num.sym != NX_CALC_SYM_POW { return 2 } 23 return 0 24} 25 26// ===== T3: integrate(x^3) = x^(3+1) / (3+1) ======================= 27func t3_integrate_power() -> nx_int { 28 let x: *Term = nx_term_var(NX_CALC_SYM_X) 29 let three: *Term = nx_calc_const_int(3) 30 let x3: *Term = nx_calc_pow(x, three) 31 let r: *Term = nx_calc_integrate(x3, NX_CALC_SYM_X) 32 if r.kind != NX_TERM_APP { return 3 } 33 if r.sym != NX_CALC_SYM_DIV { return 3 } 34 return 0 35} 36 37// ===== T4: integrate(sin x) = -cos(x) ============================= 38func t4_integrate_sin() -> nx_int { 39 let x: *Term = nx_term_var(NX_CALC_SYM_X) 40 let s: *Term = nx_calc_sin(x) 41 let r: *Term = nx_calc_integrate(s, NX_CALC_SYM_X) 42 if r.kind != NX_TERM_APP { return 4 } 43 if r.sym != NX_CALC_SYM_NEG { return 4 } 44 let inner: *Term = nx_term_arg(r, 0) 45 if inner.sym != NX_CALC_SYM_COS { return 4 } 46 return 0 47} 48 49// ===== T5: integrate(cos x) = sin(x) ============================== 50func t5_integrate_cos() -> nx_int { 51 let x: *Term = nx_term_var(NX_CALC_SYM_X) 52 let c: *Term = nx_calc_cos(x) 53 let r: *Term = nx_calc_integrate(c, NX_CALC_SYM_X) 54 if r.kind != NX_TERM_APP { return 5 } 55 if r.sym != NX_CALC_SYM_SIN { return 5 } 56 return 0 57} 58 59// ===== T6: integrate(exp x) = exp(x) ============================== 60func t6_integrate_exp() -> nx_int { 61 let x: *Term = nx_term_var(NX_CALC_SYM_X) 62 let e: *Term = nx_calc_exp(x) 63 let r: *Term = nx_calc_integrate(e, NX_CALC_SYM_X) 64 if r.kind != NX_TERM_APP { return 6 } 65 if r.sym != NX_CALC_SYM_EXP { return 6 } 66 return 0 67} 68 69// ===== T7: integrate(a + b) distributes ========================== 70func t7_integrate_sum() -> nx_int { 71 let x: *Term = nx_term_var(NX_CALC_SYM_X) 72 let s: *Term = nx_calc_add(nx_calc_sin(x), nx_calc_cos(x)) 73 let r: *Term = nx_calc_integrate(s, NX_CALC_SYM_X) 74 if r.kind != NX_TERM_APP { return 7 } 75 if r.sym != NX_CALC_SYM_ADD { return 7 } 76 return 0 77} 78 79// ===== T8: extended periodic table covers 1..36 =================== 80func t8_periodic_36() -> nx_int { 81 let table: *Element = nx_chem_periodic_table_36() 82 let h: *Element = nx_chem_element_by_z_36(table, 1) 83 if h.z != 1 { return 8 } 84 let fe: *Element = nx_chem_element_by_z_36(table, 26) 85 if fe.z != 26 { return 8 } 86 if fe.mass_q3 != 55845 { return 8 } 87 let kr: *Element = nx_chem_element_by_z_36(table, 36) 88 if kr.z != 36 { return 8 } 89 if kr.mass_q3 != 83798 { return 8 } 90 let bad: *Element = nx_chem_element_by_z_36(table, 37) 91 if (bad as nx_int) != 0 { return 8 } // out of range -> null 92 return 0 93} 94 95// ===== T9: reaction balancer -- 2 H2 + O2 -> 2 H2O is balanced == 96func t9_reaction_balanced() -> nx_int { 97 let h2: *Molecule = nx_chem_h2() 98 let o2: *Molecule = nx_chem_o2() 99 let h2o: *Molecule = nx_chem_water() 100 let ok: nx_int = nx_chem_reaction_check(2, h2, 1, o2, 2, h2o) 101 if ok != 1 { return 9 } 102 return 0 103} 104 105// ===== T10: reaction balancer -- 1 H2 + 1 O2 -> 1 H2O is NOT ==== 106// (atoms don't balance: 2 H + 2 O on LHS vs 2 H + 1 O on RHS) 107func t10_reaction_unbalanced() -> nx_int { 108 let h2: *Molecule = nx_chem_h2() 109 let o2: *Molecule = nx_chem_o2() 110 let h2o: *Molecule = nx_chem_water() 111 let ok: nx_int = nx_chem_reaction_check(1, h2, 1, o2, 1, h2o) 112 if ok != 0 { return 10 } 113 return 0 114} 115 116// ===== T11: 2 Fe + O2 -> 2 FeO is balanced ===================== 117func t11_iron_oxide() -> nx_int { 118 let fe: *Molecule = nx_chem_iron() 119 let o2: *Molecule = nx_chem_o2() 120 let feo: *Molecule = nx_chem_feo() 121 let ok: nx_int = nx_chem_reaction_check(2, fe, 1, o2, 2, feo) 122 if ok != 1 { return 11 } 123 return 0 124} 125 126func main() -> nx_exit { 127 println("=== nx_calc_chem -- integrate engine + extended chem smoke ===" as *u8) 128 129 let r1: nx_int = t1_integrate_const() 130 if r1 != 0 { println("T1 integrate_const FAIL" as *u8); return r1 } 131 println("T1 integrate_const PASS integrate(7) = 7 * x" as *u8) 132 133 let r2: nx_int = t2_integrate_x() 134 if r2 != 0 { println("T2 integrate_x FAIL" as *u8); return r2 } 135 println("T2 integrate_x PASS integrate(x) = x^2 / 2" as *u8) 136 137 let r3: nx_int = t3_integrate_power() 138 if r3 != 0 { println("T3 integrate_power FAIL" as *u8); return r3 } 139 println("T3 integrate_power PASS integrate(x^3) = x^(3+1) / (3+1)" as *u8) 140 141 let r4: nx_int = t4_integrate_sin() 142 if r4 != 0 { println("T4 integrate_sin FAIL" as *u8); return r4 } 143 println("T4 integrate_sin PASS integrate(sin x) = -cos(x)" as *u8) 144 145 let r5: nx_int = t5_integrate_cos() 146 if r5 != 0 { println("T5 integrate_cos FAIL" as *u8); return r5 } 147 println("T5 integrate_cos PASS integrate(cos x) = sin(x)" as *u8) 148 149 let r6: nx_int = t6_integrate_exp() 150 if r6 != 0 { println("T6 integrate_exp FAIL" as *u8); return r6 } 151 println("T6 integrate_exp PASS integrate(exp x) = exp(x)" as *u8) 152 153 let r7: nx_int = t7_integrate_sum() 154 if r7 != 0 { println("T7 integrate_sum FAIL" as *u8); return r7 } 155 println("T7 integrate_sum PASS integrate(sin+cos) distributes" as *u8) 156 157 let r8: nx_int = t8_periodic_36() 158 if r8 != 0 { println("T8 periodic_36 FAIL" as *u8); return r8 } 159 println("T8 periodic_36 PASS H..Kr (1..36) atomic masses table" as *u8) 160 161 let r9: nx_int = t9_reaction_balanced() 162 if r9 != 0 { println("T9 reaction_balanced FAIL" as *u8); return r9 } 163 println("T9 reaction_balanced PASS 2 H2 + O2 -> 2 H2O atoms conserve" as *u8) 164 165 let r10: nx_int = t10_reaction_unbalanced() 166 if r10 != 0 { println("T10 reaction_unbalanced FAIL" as *u8); return r10 } 167 println("T10 unbalanced PASS 1 H2 + 1 O2 -> 1 H2O REJECTED (correct)" as *u8) 168 169 let r11: nx_int = t11_iron_oxide() 170 if r11 != 0 { println("T11 iron_oxide FAIL" as *u8); return r11 } 171 println("T11 iron_oxide PASS 2 Fe + O2 -> 2 FeO balanced via period-4 element" as *u8) 172 173 println("" as *u8) 174 println("=== Updated comparison vs Mathematica ===" as *u8) 175 println(" symbolic_calculus : was LOSE_BIG -> moved to LOSE (need Series + Solve)" as *u8) 176 println(" Integrate now ships 9 patterns (const/x/x^n/sin/cos/" as *u8) 177 println(" exp/1//x/sum/diff/scalar-mul) returning kernel Term" as *u8) 178 println(" chemistry_substrate: was LOSE_BIG -> moved to LOSE (need 37..118 + ions)" as *u8) 179 println(" periodic table ships 1..36 (H..Kr) + reaction" as *u8) 180 println(" balancer for binary synthesis reactions" as *u8) 181 return 0 182}