# PolymathPlus LEQ Specification ## What This Program Type Solves LEQ is the PolymathPlus Linear Equations program type. It solves systems of linear algebraic equations where each equation relates variables with linear terms only. Common rules that always apply: - `#` starts a comment. Comments are ignored by validation. - Blank lines are allowed and ignored. - Validation is line-oriented: each non-comment line is checked as one candidate equation. - The final consistency check is global: total equations must match the number of unique variables across the whole program. - A valid LEQ program must stay in one consistent style. Do not mix LEQ with nonlinear-root, differential-equation, NLP, regression, or table-based program styles. ## User Rules For Writing A Valid LEQ Program ### 1. Keep the program in LEQ style only Allowed style: - Linear equation lines such as `a + 2*b = 10 - 3*a`. - Valid variable names must start with a letter and then use only letters, digits, or underscore. - Valid variable-name examples: `x`, `ab`, `a_1`, `a12`, `abc_34`, `y1_cd_4`. - Do not use built-in function names or conditional keywords as variable names. Examples to avoid include `sin`, `sqrt`, `log`, `if`, `then`, and `else`. - Also avoid variable names that begin with `and` or `or`, because those prefixes are tokenized as logical operators by the validator. Not allowed in the same LEQ program: - Nonlinear root-definition lines such as `f(x)=...`. - Differential-equation lines such as `d(x)/d(t)=...` or `x'=...`. - Data tables using `[` and `]`. - Nonlinear explicit-assignment style, such as an assignment using `sin(...)`, `sqrt(...)`, `/`, `^`, or parentheses. ### 2. Use exactly one `=` per equation line Each active line must contain exactly one equals sign splitting left and right expressions. Both sides of `=` must be non-empty expressions. Invalid examples: ```polymathplus x + y x = y = 3 x = ``` ### 3. Use only LEQ-accepted expression content On each side of `=` use only: - Variable names - Numeric constants, including decimal and scientific notation - Operators: `+`, `-`, `*` Do not use: - `/` - `^` - `(` or `)` - functions such as `sin`, `sqrt`, `log`, or `exp` - comparisons such as `>`, `<`, `>=`, or `<=` - comma decimal notation ### 4. Use coefficient multiplication carefully LEQ accepts both explicit multiplication and coefficient shorthand. Valid examples: ```polymathplus 2*x + 3*y = 12 2x + 3y = 12 2 x + 3 y = 12 -3.5a + 2b = 0 1.2e-3*x + y = 5 ``` Important limits: - Coefficient shorthand is intended for number-to-variable multiplication only. - The validator accepts both compact and spaced coefficient shorthand, such as `2x` and `2 x`. - Variable-to-variable multiplication is nonlinear and invalid. - Parentheses are not allowed, so forms like `2(x+y)` are invalid. - Variable followed by number is invalid, for example `x2` is one variable name, but `x 2` is two adjacent tokens and is invalid. ### 5. Avoid invalid token sequences These patterns are rejected: - Two consecutive operators - Two consecutive numbers - Variable followed by number as a separate token - Variable followed by variable as a separate token - Expression ending with an operator Note: number followed by variable is accepted when it is coefficient shorthand, such as `2x`. ### 6. Keep equations linear A product of two variables is rejected as nonlinear. Invalid pattern: ```polymathplus var * var ``` Invalid example: ```polymathplus b = 0.25*c*c ``` Repair: ```polymathplus b = 0.25*c ``` If the model truly needs products of variables, it is not an LEQ program. Use an NLE or NLP program type instead. ### 7. Match equation count to variable count Across the full LEQ program: - Number of equations must equal number of unique variables found in expressions. If they do not match, the program is rejected. Example invalid structure: ```polymathplus x + y = 4 x - y = 1 z = 2 ``` This has three equations and three variables, so it is structurally OK. This is invalid: ```polymathplus x + y = 4 x - y = 1 z + w = 2 ``` It has three equations but four variables. ### 8. Practical checklist before solve - The program contains at least one linear equation line. - Every active line has exactly one `=`. - Only `+`, `-`, and `*` operators are used. - No `/`, `^`, parentheses, functions, comparisons, or data tables are used. - No nonlinear terms are used, such as `x*y`. - Equation count equals unique-variable count. - The program uses LEQ style only. ## Examples Of Valid LEQ Programs ### Example A: 2 equations, 2 variables, coefficient shorthand ```polymathplus 2x + 3y = 12 4x = 5 + y ``` ### Example B: 5 equations, 5 variables ```polymathplus c + e = 1 3*b - 2*c - 2*d = -2 2*a - 0.27*c - d - 2*e = -1 b = 0.25*c 1.5*a = e ``` ### Example C: Linear material balance with decimal coefficients ```polymathplus .07*D1 + .18*B1 + .15*D2 + .24*B2 = 0.15*70 .04*D1 + .24*B1 + .1*D2 + .65*B2 = 0.25*70 .54*D1 + .42*B1 + .54*D2 + .1*B2 = 0.40*70 .35*D1 + .16*B1 + .21*D2 + .01*B2 = 0.20*70 ``` ### Example D: Scientific notation coefficients ```polymathplus 1.2e-3*x + y = 5 3*x - 2.5e+1*y = 10 ``` ## Invalid Examples And Repairs ### Nonlinear variable product Invalid: ```polymathplus x + y = 10 x*y = 20 ``` Repair by changing the model to a linear form, or use another program type if the variable product is required. ```polymathplus x + y = 10 2*x + 3*y = 20 ``` ### Mixed LEQ and nonlinear assignment Invalid: ```polymathplus x + y = 10 2*x - y = 4 rate = sin(x) ``` Repair by keeping only LEQ lines, or use NLE if nonlinear expressions are required. ```polymathplus x + y = 10 2*x - y = 4 ``` ### Equation count does not match variable count Invalid: ```polymathplus x + y = 4 x - y = 1 z + w = 2 ``` Repair by adding/removing equations or variables so the counts match. ```polymathplus x + y = 4 x - y = 1 z = 2 ``` ### Unsupported operator Invalid: ```polymathplus x/2 + y = 10 x - y = 4 ``` Repair by rewriting division as multiplication by a numeric coefficient. ```polymathplus 0.5*x + y = 10 x - y = 4 ``` ## Verification Notes This hardened repo-local copy was checked against the LEQ behavior in: ```txt C:\dev\js\solver_precheck\solver_precheck.js C:\dev\js\solver_precheck\solver_precheckTests.js ``` The current examples were selected or adapted from the test corpus and validator behavior. Test-only comments were removed, and example variable names/comments were adjusted for documentation use.