kill(all)$

appendix: chemical  components : oxidation state,version 1.0
peter.vlasschaert@gmail.com,09/02/2021

ref:Appendix :How import chemical elements : Maxima (csv file),version 1.0  (1)
     Appendix :chemical components : molecular mass ,version 1.0.                  (2)

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

load(lrats)$ratprint:false $

1e) step: given (data structure).
--------------------------------------------

p1:matrix(
        [1, H,  Hydrogen],
        [2, He, Helium],
        [3, Li, Lithium],
        [4, Be, Beryllium],
        [5, B,  Boron],
        [6, C,  Carbon],
        [7, N,  Nitrogen],
        [8, O,  Oxygen],
        [9, F,  Fluorine],
        [10,    Ne, Neon],
        [11,    Na, Sodium],
        [12,    Mg, Magnesium],
        [13,    Al, Aluminum],
        [14,    Si, Silicon],
        [15,    P,  Phosphorus],
        [16,    S,  Sulfur]
    );

edit : copy from (2) ,paste in p1.

p1a:args(p1);

2e) step : add column (with oxidation state.).
-------------------------------------------------------------

p2:[+1,y,+1,x,x,+4,x,-2,-1,y,+1,+2,+3,+4,x,x];

p2a:length(p2);

3e) step : 1e) & 2e)
---------------------------

A:addcol(p1,p2);

Matrix A : DataBase.
================

column 1 : Atomic Number
column 2 : Chemical Element Symbol
column 3 : Name (= column 2)
column 4 : A,  oxidation state ( rules ,see : below)

rem : only get oxidation state for simple anorganic products.
          =========================================


example :              Na2S2O3,H2SO4,NaH,CaF2,Al(OH)3,H2CO3
==================================================

rules :
--------
==================================================
a)  y : suppose no components  exit  :(advanced study : not true*).
b)  exception : LiH NaH,CaH2  :  oxidation state ( H =-1 ).
c)  exception : H2O2                  :  oxidation state ( O = -1 ).
d) x : oxidation state  ( follows from :    pos state + neg state = 0)
==================================================

----------------------------------------------------------
* research : (= advanced study,more rules)
----------------------------------------------------------


book : inorganic.
=============

Inorganic Chemistry (Keith F.Purcell, Holt Saunders Int.Editions)
-----------------------------------------------------------------------------------------

wikipedia: organic  & anorganic. 
=========================
https://en.wikipedia.org/wiki/Oxidation_state

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

computation : x - oxidation state  ( rule = d)
================================

1e) step : molecule given. (see :examples above).
--------------------------------------------------------------------

q1:[Na,2,S,2,O,3];/*exception :[Na,1,H,1]*/;

2e) step  : rule b) & c) .
--------------------------------

q2a:[[Li,1,H,1],[Na,1,H,1],[Ca,1,H,2]];
q2b:[[H,2,O,2]];

→ exception : data base ( Matrix  B)

list : data structure.
===============

q2:append(q2a,q2b);/*see above: exception :matrix(A),column 4 */

q22:q2[1];

matrix : data structure.
=================

B:apply(matrix,q2);/* see above :oxidation state {H,O} */

3e) step : check of the molecule=q1 not in data base : B ( or q2)
---------------------------------------------------------------------------------------

for i:1 thru length(q2) do
(
 if q1=q2[i] then     
   print("exception = ",q1)
    else 
   (s1:0) 
                          )$

→ s1=0 ,not in database : B

s1;

testB(vv,ww):=block([ii],
kill(s1), /* kill(s1) ,can be set outside : testB ,check it*/ 
/* print("test",s1) */   
for ii:1 thru length(ww) do
(
 if vv=ww[ii] then     
   print("exception(B) = ",vv)        
    else 
   (s1:0) 
                          ),
    return(s1)
)$

testB(q1,q2);

s1;

4e) step : found chemical symbol: with x 'fourth column of A'
----------------------------------------------------------------------------------

matsize(AA):=[length(AA),length(transpose(AA))];

matsize(A);

programming: check two column at same time from : 'and'  ,operator:
-------------------------------------------------------------------------------------------------
reason :(column 2 of A) and (column 4 of A), because column 4 found x

foundx(vv,AA1):=block([ii,kk,m1],
m1:length(vv)/2,       
for kk:1 thru m1 do(   
for ii:1 thru matsize(AA1)[1] do(
if vv[2*(kk-1)+1] = AA1[ii][2] and AA1[ii][4]=x then
   ( x1:vv[2*(kk-1)+1] ,x2:vv[2*(kk-1)+2]),x3:kk           
       )
    ),
xx:[x1,x2,x3],return(xx)
)$

foundx(q1,A);/* see above : matrix(A)*/

output : xx → (x1,x2,x3)

xx;

x1: chemical symbol 
x2: number of atoms of x1
x3: position in list :q1 with x : fourth column of(A)

ii;
kk;

5e) step : oxidation state (foundx : chemical element symbol :  x : fourth column of(A)  )
----------------------------------------------------------------------------------------------------------------------

chemoxstate(vvo,AAo):=block([ii,kk,m2],
m2:length(vvo)/2,
somx:0,   
for kk:1 thru m2 do(          
for ii:1 thru matsize(AAo)[1] do(
if vvo[2*(kk-1)+1] = AAo[ii][2] then
  ( somx:somx+AAo[ii][4]*vvo[2*(kk-1)+2])           
                                 )
                     ),
return(somx)
)$

chemoxstate(q1,A);

foundx → xx[1] ,use rule d), 'see above '

q3:subst([x=xx[1]],chemoxstate(q1,A))=0;

6e) step final result
---------------------------

→ found oxidation state : xx[1]  see : q3,above.

q4:solve(q3,xx[1])[1];

→ formal output

for jj:1 thru matsize(A)[1] do(
if xx[1]= A[jj][2] then
 print("oxidation state =",rhs(q4),"of",A[jj][3],"into",q1) 
)$

rem : Xe,Ne are not allowed , 'version 1.0'.

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