kill(all)$

maxima : chemical equation ,"simple chemical reaction :math ",version 1.0,part 3
peter.vlasschaert@gmail.com,10/02/2021

ref: see website 
maxima : chemical equation ,string display version, 'product reaction ',version 1.0a,part 1
maxima : chemical equation ,string display version, 'product reaction,function ',version 1.0,part 2

load packages,option
=================

load(lrats)$ ratprint:false $  load(stringproc)$

chemical reaction 
==============

what :base + acid  = salt + aq

1e) step : reaction  : example 
-----------------------------------------

→ products :before reaction .

base : acceptor (=H(+)) → coef before x1

p1a:[Na,1,O,1,H,1];

acid : donor (=H(+))→ coef before x2

p1b:[H,2,S,1,O,4];

→ products :after reaction .

salt :→ coef after x3

p2a:[Na,2,S,1,O,4];

aq:→ coef after x4.

p2b:[H,2,O,1];

2e) step : display reaction.
------------------------------------

p:print(x1,"*",p1a,"+",x2,"*",p1b, "=",x3,"*",
    p2a,"+",x4,"*",p2b)$

→ how to find : x1,x2,x3,x4

px:atom(x1);

px1:stringp(p);

3e) step :chemical elements in 2e)
------------------------------------------------
to find: list of variables , before 'or' after

before (reaction): list

p3a:append(p1a,p1b);

p3b:listofvars(p3a);

p3b1:sort(p3b);

after (reaction): list

p4a:append(p2a,p2b);

p4b:listofvars(p4a);

p4b1:sort(p4b);

check : before 'or' after: same ?

-----------------------------------------------------------------------------------------------------------

is(p3b1=p4b1);

p5:length(p3b1);

print("number of equations =",p5 )$

p5a1:makelist(concat(x,i),i,1,p5);
p5a2:makelist(sconcat(x,i),i,1,p5);

info:
===
rem : xv , changed ( symbol → integer)

-----------------------------------------------------------------------

xv:b;
atom(xv);
integerp(xv);
xv*string(args(p1a));
xv:2;
atom(xv);
integerp(xv);
xv*string(args(p1a));

------------------------------------------------------------------------

p5b:x1*string(args(p1a))+x2*string(args(p1b))=x3*string(args(p2a))+x4*string(args(p2b));

4e) step: find :{xi}, i=1,2,3,4  (= same p5,see above)
-------------------------------------------------------------------------

p7:p4b1;

manual method : use p5b.
====================

p7a:2*x2+1*x1=2*x4;           /* H  equations */
p7b:x1=2*x3;                  /* Na equations */
p7c:4*x2 +1*x1 = 1*x4+4*x3;   /* O  equations */
p7d:1*x2 = 1*x3;              /* S  equations  */
p8a:solve([p7a,p7b,p7c,p7d],p5a1)[1];/* concat, see:above */
p8b:solve([p7a,p7b,p7c,p7d],p5a2)   ;/* sconcat,see:above */

p9a:sort(listofvars(p8a));
p9b:p9a[1]=2;/* select any integer */

p10:subst([p9b],p8a);

result : chemical equation 
------------------------------------

p11:lratsubst(p10,p5b);

info :

-----------------------------

p5a1;
p5a2;

-------------------------------

symbolic method : use p5b.
=====================

p12:p7;/*four equations */

before : reaction: x1,x2
--------------------------------

p12a:[p1a,p1b];

p12a1:x1*p12a[1];

p12a2:x2*p12a[2];

p12a3:[p12a1,p12a2];

→ relation between : p12a &  p12a3  (to get lhs of the equations)

after   : reaction:x3,x4
------------------------------

p12b:[p2a,p2b];

p12b1:x3*p12b[1];

p12b2:x4*p12b[2];

p12b3:[p12b1,p12b2];

→ relation between : p12b &  p12b3 (to get rhs of the equations)

1e) step : found equations H = p12[1], first equations .
---------------------------------------------------------------------------

[p12a[1][1],p12a[1][2]];

→ lhs : H
=======

→ found  H into p12a[1]

eq1a:0;

for i:1 thru length(p12a[1])/2 do(
if p12[1] = p12a[1][2*(i-1)+1] then 
    eq1a:eq1a+p12a3[1][2*(i-1)+2]
)$

eq1a;

→ found  H into p12a[1] &  p12a[2]

eq1aa:0; /*init sum of lhs: equation H*/

for k:1 thru length(p12a) do(
for i:1 thru length(p12a[k])/2 do(
     if p12[1] = p12a[k][2*(i-1)+1] then 
        eq1aa:eq1aa+p12a3[k][2*(i-1)+2]
                                 )
                            )$

eq1aa;

→ rhs : H
=======

eq1bb:0; /*init sum of lhs: equation H*/

for k:1 thru length(p12b) do(
for i:1 thru length(p12b[k])/2 do(
     if p12[1] = p12b[k][2*(i-1)+1] then 
        eq1bb:eq1bb+p12b3[k][2*(i-1)+2]
                                 )
                            )$

eq1bb;

→ H equations (lhs =rhs)
===================

p13:eq1aa=eq1bb;

→ check  : p7a & p13
=================

p13a:is(rhs(p13)=rhs(p7a));
p13b:is(lhs(p13)=lhs(p7a));

2e) step : found all equations {H,Na,O,S.}
---------------------------------------------------------
j = index , for  four equations=1,2,3,4

eq1aa:0;
eq1bb:0;

j:3; /* generate second equation::p7b  */

for k:1 thru length(p12a) do(
for i:1 thru length(p12a[k])/2 do(
     if p12[j] = p12a[k][2*(i-1)+1] then 
        eq1aa:eq1aa+p12a3[k][2*(i-1)+2]
                                 )
                            )$

for k:1 thru length(p12b) do(
for i:1 thru length(p12b[k])/2 do(
     if p12[j] = p12b[k][2*(i-1)+1] then 
        eq1bb:eq1bb+p12b3[k][2*(i-1)+2]
                                 )
                            )$

eq1aa =eq1bb;

3e) step : found all equations {H,Na,O,S.}
---------------------------------------------------------
function : generate equation 

funceq(ii):=block([],
eqxa:0,eqxb:0,
for k:1 thru length(p12a) do(
for i:1 thru length(p12a[k])/2 do(
     if p12[ii] = p12a[k][2*(i-1)+1] then 
        eqxa:eqxa+p12a3[k][2*(i-1)+2]
                                 )
                            ),        

for k:1 thru length(p12b) do(
for i:1 thru length(p12b[k])/2 do(
     if p12[ii] = p12b[k][2*(i-1)+1] then 
        eqxb:eqxb+p12b3[k][2*(i-1)+2]
                                 )
                            )

)$

→ optimized :code.

grind(funceq)$

→use  funceq  'function with block statement'

funceq(3)$$

eqxa=eqxb;

funceq(4)$

eqxa=eqxb;

ss:[];/* empty list of equations*/

→ all four equations : build

for j:1 thru length(p12) do(
s1:funceq(j),
ss:endcons(eqxa=eqxb,ss)
);

forget(x1,x2,x3,x4);

ss;

4e) step : get solutions of all equations {H,Na,O,S.}
----------------------------------------------------------------------

h1:listofvars(ss);

h2:solve(ss,h1)[1];

h3:sort(listofvars(h2));

h4:h3[1]=1;/* select any integer =1,depeznt var. */

h5:subst([h4],h2);

use :p5b, reaction display.

h6:lratsubst(h5,p5b);

--------------------------------------------------------------------------------------------------------------------------