kill(all)$

maxima .appendix :'formula', 'version 1.0'
peter.vlasschaert@gmail.com, 02/11/2019

find : H balance , 'reaction',before & after
===============================
H :  => el[3]

el:(e)lement (l)ist

el :[Na,O,H,S];

qqa :
qqb
a : ' from (a)fter     reaction'
b: '  from (b)efore reaction'

before : reaction. {x1,x2,.....}
============

number of products
--------------------------

npb: 2;

products : 
=======

qqb[1]:[[Na,1],[O,1],[H,1]];
qqb[2]:[[H,2],[S,1],[O,4]];

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

mb[1];
mb[2];

lb:combine : mb[i]  , i=1...npb
======================
lb : list

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

lb:flatten(lb);

sb :[] $

balance : H, before reaction
=====================

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

sb;

sb : apply("+",sb);

all balances :el :[Na,O,H,S];
==========================
j = 3 , balance : H, before reaction

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
    
)$

balance : H, before reaction
=====================

mfb[3];

mfb[3]:apply("+",mfb[3]);

after : reaction.    {y1,y2,.....}
===========

number of products 
---------------------------

npa:2;

products : 
=======

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

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

qqa[1][1];

qqa[1][1][2];

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

ma[1];

ma[2];

la:combine : mb[i]  , i=1...npa
======================
la : list

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

la:flatten(la);

all balances :el :[Na,O,H,S];
==========================
j = 3 , balance : H,after the reaction

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
    
)$

balance : H, after the reaction
=======================

mfa[3];
mfa[3]:apply("+",mfa[3]);

final :(lhs & rhs) 'reaction'  , balance H
=============================

mfb[3] = mfa[3];