kill(all)$

appendix :chemical components : molecular mass ,version 1.0.
peter.vlasschaert@gmail.com,08/07/2021

ref :Appendix :How import chemical elements : Maxima (csv file),version 1.0

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

load(lrats)$ratprint:false $

chemical element data : data structure.
==============================

given: 
=====

p1:[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,17,Cl,Chlorine,18,Ar,Argon,19,K,Potassium,20,Ca,Calcium,21,Sc,Scandium
    ,22,Ti,Titanium,23,V,Vanadium,24,Cr,Chromium,25,Mn,Manganese,26,Fe,Iron,27,Co,Cobalt,28,
    Ni,Nickel,29,Cu,Copper,30,Zn,Zinc,31,Ga,Gallium,32,Ge,Germanium,33,As,Arsenic,34,
    Se,Selenium,35,Br,Bromine,36,Kr,Krypton,37,Rb,Rubidium,38,Sr,Strontium,39,Y,Yttrium,40,
    Zr,Zirconium,41,Nb,Niobium,42,Mo,Molybdenum,43,Tc,Technetium,44,Ru,Ruthenium,45,Rh,Rhodium,
    46,Pd,Palladium,47,Ag,Silver,48,Cd,Cadmium,49,In,Indium,50,Sn,Tin,51,Sb,Antimony,52,
    Te,Tellurium,53,I,Iodine,54,Xe,Xenon,55,Cs,Cesium,56,Ba,Barium,57,La,Lanthanum,58,Ce,Cerium,
    59,Pr,Praseodymium,60,Nd,Neodymium,61,Pm,Promethium,62,Sm,Samarium,63,Eu,Europium,64,Gd,Gadolinium,
    65,Tb,Terbium,66,Dy,Dysprosium,67,Ho,Holmium,68,Er,Erbium,69,Tm,Thulium,70,Yb,Ytterbium,71,
    Lu,Lutetium,72,Hf,Hafnium,73,Ta,Tantalum,74,W,Tungsten,75,Re,Rhenium,76,Os,Osmium,77,Ir,Iridium,78,
    Pt,Platinum,79,Au,Gold,80,Hg,Mercury,81,Tl,Thallium,82,Pb,Lead,83,Bi,Bismuth,84,Po,Polonium,85,
    At,Astatine,86,Rn,Radon,87,Fr,Francium,88,Ra,Radium,89,Ac,Actinium,90,Th,Thorium,91,Pa,
    Protactinium,92,U,Uranium,93,Np,Neptunium,94,Pu,Plutonium,95,Am,Americium,96,Cm,Curium,
    97,Bk,Berkelium,98,Cf,Californium,99,Es,Einsteinium,100,Fm,Fermium,101,Md,Mendelevium,102,
    No,Nobelium,103,Lr,Lawrencium,104,Rf,Rutherfordium,105,Db,Dubnium,106,Sg,Seaborgium,107,Bh,Bohrium,
    108,Hs,Hassium,109,Mt,Meitnerium,110,Ds,Darmstadtium,
    111,Rg,Roentgenium,112,Cn,Copernicium,113,Nh,Nihonium,114,Fl,Flerovium,115,Mc,Moscovium,116,
    Lv,Livermorium,117,Ts,Tennessine,118,Og,Oganesson]$

molecular : formula
===============

p2:[Na,2,S,2,O,3];

1e) step :convert (p1 = list to matrix =A)
-------------------------------------------------------

→ 1e element, periodic system

p3a:[p1[1],p1[2],p1[3]];

→ 2e) data structure: needed , p2
------------------------------------------------

p3b:makelist([p1[1+(i-1)*3],p1[2+(i-1)*3],p1[3+(i-1)*3]],i,1,16)$

p3c:apply('matrix,p3b);

=>  atomic mass = approx(2* atomic number 'first column, p3c')
=======================================================
reason : mass nucleus : neutron + protron  = 2*atomic number. (approx.)
=======================================================

→ 3e) step: build forth column.
------------------------------------------

p4a:makelist([p1[1+(i-1)*3],p1[2+(i-1)*3],p1[3+(i-1)*3],2*
    p1[1+(i-1)*3]],i,1,16)$

A:apply(matrix,p4a);

→4e) step first element :'p2'
---------------------------------------

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

matsize(A);

number of rows:

p5a:matsize(A)[1];

number of columns :

p5b:matsize(A)[2];

final result : first element.

for i:1 thru p5a do(
if p2[1]=A[i][2] then
result1:print("atomc mass (",p2[1+1]*p2[1],") =",p2[1+1]*A[i][4])
)$

result1;

→5e) step: (m)olecular (m)ass 'p2'
-----------------------------------------------

→ a) init :result 

result:makelist(0,i,1,length(p2)/2);

→ b)  k: 1 to length(p2)/2

for k:1 thru length(p2)/2 do(
for i:1 thru p5a do(
if p2[2*(k-1)+1]=A[i][2] then
result[k]:print("atomc mass (",p2[2*(k-1)+2]*A[i][2],") ="
          ,p2[2*(k-1)+2]*A[i][4])
)
)$

result;

→ c) (m)olecular (m)ass = mm(p2)

mm:sum(result[k],k,1,length(p2)/2);

print("molecular mass(",p2,")=",mm)$

molecularmass(BB,rr):= block([jj,kk,ii,ll],
resultrr:makelist(0,jj,1,length(rr)/2),
for kk:1 thru length(rr)/2 do(
for ii:1 thru matsize(BB)[1] do(
if rr[2*(kk-1)+1]=BB[ii][2] then 
resultrr[kk]:rr[2*(kk-1)+2]*BB[ii][4]
                               )
                              ),        
q1:sum(resultrr[ll],ll,1,length(rr)/2),
return([q1,rr])        
)$

→ optimized : code

grind(molecularmass)$

→ output :molecularmass

molecularmass(A,p2);

molecularmass(A,p2)[1];
molecularmass(A,p2)[2];

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