kill(all)$

maxima : chemical equation ,"simple chemical reaction :disp(equation) ",version 1.0a,part 4
peter.vlasschaert@gmail.com,09/03/2021

what about :version 1.0a, 'remove "1" in the given chemical equation '.

ref : input va,vb (read)
-------------------------------
appendix :chemical equations, input & output :reaction,version 1.0
peter.vlasschaert @gmail.com,18/02/2021

1e) step : before reaction = after reaction. (products)
-------------------------------------------------------------------------

ref :
appendix : chemical equations,single or double symbol:function,version 1.0
peter.vlasschaert@gmail.com,16/02/2021

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

copy : second line : 'disp'

vbb:copy(vb);
vaa:copy(va);

simplode(vb);

vb[1][1];

2e) step : construct first line
---------------------------------------

construct : first line: (back ground)
==============

for k:1 thru length(vb) do(
for i:1 thru length(vb[k])/2 do(
if integerp(vb[k][2*i]) then vb[k][2*i]:ascii(32)  
    )
)$

vb;

for k:1 thru length(va) do(
for i:1 thru length(va[k])/2 do(
if integerp(va[k][2*i]) then va[k][2*i]:ascii(32)  
    )
)$

va;

rem : integerp ,' integer(p)= (p)redicate'

info: construct 'xx'  from {va,vb}  → 'get xxo', 'datastructure'

"----------------------------------------------------------------------------------------------------------------------------"

xx:flatten([vb[1],["+"],vb[2],["=",ascii(32)],va[1],["+"],va[2]]);

xxo:simplode(%);

1e) number "+" =  'length(vb) -1,length(va) -1',between products 'see :example 'lhs(xx),rhs(xx)'
2e) structure      =  ["=",ascii(32)]
=======================================================================
ascii(32)  = space ,see : structure 'xxo'

"----------------------------------------------------------------------------------------------------------------------------"

construct : first line: (code = function,line1(va,vb))
==============

line1(vva,vvb):= block([rb:[],ra:[],ii,jj,k1,k2,k3],
 /* init : after */   
for k1:1 thru length(vva) do(
for ii:1 thru length(vva[k1])/2 do(
if integerp(vva[k1][2*ii]) then vva[k1][2*ii]:ascii(32)  
    )
),   
 /* init: before */   
for k2:1 thru length(vvb) do(
for ii:1 thru length(vvb[k2])/2 do(
if integerp(vvb[k2][2*ii]) then vvb[k2][2*ii]:ascii(32)  
    )
),  
 /* combine : before,"+" */   
 for jj:1 thru length(vvb) do(      
 rb:endcons([vvb[jj],"+"],rb),  
 rb1:flatten(rb), 
 rb2:makelist(rb1[k3],k3,1,length(rb1)-1)                     
    ),
 /* combine : after,"+" */    
 for jj:1 thru length(vva) do(      
 ra:endcons([vva[jj],"+"],ra),  
 ra1:flatten(ra), 
 ra2:makelist(ra1[k3],k3,1,length(ra1)-1)                     
    ),  
 /* combination reaction ( before & after) */            
rba1:[rb2,["="],ra2],
rba: simplode(flatten(rba1)),
return(rba)       
)$

dp1:line1(va,vb);

check : dp1 & xxo
==============

c1a:is(lhs(xxo)=lhs(dp1));
c1b:is(rhs(xxo)=rhs(dp1));
cab1:is(c1a=c1b);

3e) step : construct second line
-------------------------------------------
need :{vbb,vaa},'see above'

construct : second line: (back ground)
=================

see : website
appendix : chemical equations,single or double symbol:function,version 1.0
peter.vlasschaert@gmail.com,16/02/2021

rem : chemical element : single (example : H) or double symbol (example : Na)

 function : sba,'see :appendix
--------------------------------------------

sba(zi,nup):= block([i,k,ss:[]],
for i:1 thru nup do(
ss:endcons(makelist(length(charlist
  (simplode([zi[i][2*(k-1)+1]]))),
            k,1,length(zi[i])/2),ss)),
return(ss)   
)$

pb:sba(vbb,length(vbb));
makelist(pb[1][i],i,1,3);
pa:sba(vaa,length(vaa));

pca:[ascii(32),ascii(32),ascii(32),1,ascii(32),ascii(32),1,ascii(32),ascii(32),1,
    ascii(32),ascii(32),ascii(32),2,ascii(32),ascii(32),1,ascii(32),ascii(32),4];

pcb:flatten([pca,pca]);

pc1:simplode(pcb);

disp(dp1,pc1);

construct : second line: (code = function,line2(vaa,vbb))
=================
rem : copy , 'Maxima'

→ input list

vaa;
vbb;

→ function :second line

line2(vbx,vax,kb,ka):=block([qb,qa,k1,k2,i,j,npb:[],npa:[]],
/* integerp(kb) = true ,integerp(ka)= true */   
qb:sba(vbx,length(vbx)),
qa:sba(vax,length(vax)), 
    /* part :before reaction */
 for k1:1 thru length(vbx) do(
  for i:1 thru length(vbx[k1])/2 do(
      if qb[k1][i] = 1 then 
    npb:endcons([ascii(32),ascii(32),vbx[k1][2*i]],npb),
      if qb[k1][i] = 2 then 
    npb:endcons([ascii(32),ascii(32),ascii(32),vbx[k1][2*i]],npb)                
                    )               
    ),  
  /* part :after reaction */   
for k2:1 thru length(vax) do(
  for j:1 thru length(vax[k2])/2 do(
if qa[k2][j] = 1 then 
    npa:endcons([ascii(32),ascii(32),vax[k2][2*j]],npa),
if qa[k2][j] = 2 then 
    npa:endcons([ascii(32),ascii(32),ascii(32),vax[k2][2*j]],npa)                
                    )               
    ), 
  /* kb ,ka → integer , change position of the indices */  
  kbb:makelist(ascii(32),i,1,kb),
  kaa:makelist(ascii(32),i,1,ka),     
    /* combination (before,after), reaction */
  nba1:[kbb,
        npb,kaa,npa],
  nba2:flatten(nba1),
  nba3:simplode(nba2),
  /* update version : will remove = "1" */
  nba: ssubst(ascii(32),"1",nba3),
  return(nba)
)$

dp2:line2(vbb,vaa,3,4);

4e) step :combination (first line, second line)
--------------------------------------------------------------

→ display2d: on

display2d;

≥  result

disp(dp1,dp2)$

or

dp1;
dp2;

→ display2d: off

display2d:false$

≥ result

disp(dp1,dp2)$

or

dp1;
dp2;

"-----------------------------------------------------------------------------------------------------------------------------------"