kill(all)$

Maxima : reaction  Formula  'final reaction: find coef.,solve ', 'version 1.0' ,part: '3e',final,test2
peter.vlasschaert@gmail.com, 02/11/2019

rem : 'error' in solve for coef. of the equation ' z = 1'

load  packages
============

load(lrats)$

see website:
==========
---------------------------------------------------------
part 3a,3b,3c,3d
maxima .appendix :'formula', 'version 1.0'
---------------------------------------------------------

 = > begin definition

definition : formula.
==============

formula(v):=block([],
kaa:makelist(v[i][1],i,1,length(v)),
kbb:makelist(v[i][2],i,1,length(v)),
kbb1:makelist(if  v[i][2] =1 
    then  v[i][2] : "" else  v[i][2] : v[i][2],i,1,length(v)),
kcc1:maplist(concat,kaa,kbb1),
mpp:concat(kcc1[1],kcc1[2]),
for i:3 thru length(v) do(
mpp: concat(mpp,kcc1[i])),
    return(mpp)
)$         

definition : bproducts.
================

bproducts(pl,el,cf):=block([],
bp:[],
for i:1 thru length(el)  do(
if pl =el[i][1]  then
bp:endcons(cf*el[i][2],bp)),
return(bp))$

 =>  end definition  

reaction :
=======

x2*H2SO4+x1*NaOH=y2*Na2SO4+y1*H2O

rem : remove ' read ' use  the key : 'Backspace'

general info : find coef of the  reaction
=============================
{x1,x2,.....}  &  {y1,y2,....}

print( "How to enter elements = [Na,O,H,S]")$

el : read( "elements in the reaction   :'shift' & 'enter'  = ")$

products : before reaction
====================

print(" number of products : integer")$

npb : read( "number products (before reaction)   :'shift' & 'enter'       = ")$

 => enter product  ( before reaction )
products : {NaOH,H2SO4}

qb[1]:[[Na,z],[O,z],[H,z],x1];
qb[2]:[[H,2],[S,z],[O,4],x2];

see : website
==========
part  3a,3b : definition : formula,bproducts

for i:1 thru npb do(
    qqb[i] :delete(last(qb[i]) ,qb[i])
);

qqb[1];
qqb[2];

use : definition
=>  formula,bproducts

br:makelist([formula(qqb[j]),last(qb[j])],j,1,npb);

br1:[sum(br[j][2]*br[j][1],j,1,npb)];

products : after reaction
==================

print(" number of products : integer")$

npa : read( "number products (after reaction)   :'shift' & 'enter'       = ")$

 => enter product  ( after reaction )
products : {H2O,Na2SO4}

qa[1]:[[H,2],[O,z],y1];
qa[2]:[[Na,2],[S,z],[O,4],y2];

see : website
==========
part  3a,3b : definition : formula,bproducts

for i:1 thru npa do(
    qqa[i] :delete(last(qa[i]) ,qa[i])
);

qqa[1];
qqa[2];

use : definition
=>  formula,bproducts

ar:makelist([formula(qqa[j]),last(qa[j])],j,1,npa);

ar1:[sum(ar[j][2]*ar[j][1],j,1,npa)];

final reaction
==========

use : definition 'formula' : see above

g1:br1[1] =ar1[1];

to find : coef {x1,x2,.....} & {y1,y2,....} :reaction : g1
======================================

before: reaction :' see appendix'
=============

combi : list =>  products
---------------------------------

mb[i]:=makelist([qqb[i][j][1],qqb[i][j][2]*1*ev(concat(x,i))],j,1,length(qqb[i]))$

lb:[]$
for i:1  thru npb do(
lb:endcons(mb[i],lb)
    )$

lb:flatten(lb);

lhs :  equation  (list)
---------------------------

for j:1 thru length(el) do(
    sbb:[],         
for i:1  thru length(lb) do(
    if el[j] = lb[i] then  
        (
        sbb:endcons(lb[i+1],sbb)
        )                   
)  ,
    mfb[j]:sbb
    
)$

lhs => equation, [Na,O,H,S],sum
=========================

for i:1 thru  length(el) do(
    mfb[i]:apply("+",mfb[i])
    )$

after : reaction : ' see appendix'
=============

combi : list =>  reaction products
--------------------------------------------

ma[i]:=makelist([qqa[i][j][1],qqa[i][j][2]*1*ev(concat(y,i))],j,1,length(qqa[i]))$

la:[]$
for i:1  thru npa do(
la:endcons(ma[i],la)
    )$

la;

la:flatten(la);

rhs :  equation  (list)
---------------------------

for j:1 thru length(el) do(
    saa:[],
for i:1  thru length(la) do(
    if el[j] = la[i] then  
        (
        saa:endcons(la[i+1],saa)
        )                   
)  ,
    mfa[j]:saa
    
)$

rhs => equation, [Na,O,H,S],sum
=====================

for i:1 thru  length(el) do(
    mfa[i]:apply("+",mfa[i])
    )$

final : element balancce: [Na,O,H,S]={p1,p2,p3,p4}
---------------------------------------------------------------------

p1:mfb[1]=mfa[1];
p2:mfb[2]=mfa[2];
p3:mfb[3]=mfa[3];
p4:mfb[4]=mfa[4];

for i:1 thru length(el) do(
    fer[i]:mfb[i]=mfa[i]
);

find coef : {x1,x2,...} & {y1,y2,...}
--------------------------------------------
solve : ?

list : [x1,x2,....,y1,y2,]
----------------------------

ta:makelist(concat(x,i),i,1,npa)$
tb:makelist(concat(y,i),i,1,npb)$
tt:[ta,tb]$
tt:flatten(tt);

list : equations  'p1...pi'  ( example : 2)
-------------------------------
 [Na,O,H,S]

ss:makelist(ev(fer[i]),i,1,length(el));

ss : subst([z=1],ss);

solve =
x2*H2SO4+x1*NaOH=y2*Na2SO4+y1*H2O

ratprint:false;

frsm:solve(ss,tt);

frsm1:subst([%r1=1],frsm)[1];

coef : final reaction
===============
use :'g1'  above 

output  :  z=1

print (" z = 1 ,reaction below   =>  ")$

frsm2:subst(frsm1,g1);