math - Lua module for calculating the molar mass of chemical compounds -


i have been using (or attempting to, of modules have have borrowed other wikis understanding of lua limited @ best) lua-based modules , invoking templates (i.e., ones invoke them {{#invoke:}}) on wiki time , have infobox chemical compounds (my chembox) includes inputs numbers of atoms of each chemical element present in compound (i.e., numbers used in compound's molecular formula) , molar mass of compound. thinking, since possible calculate molar mass of compound based on molecular formula might possible create lua module can automatically me, eliminating need me input molar mass myself these infoboxes.

so question basically, how do this?

research

my background in mathematics, felt straight-forward way of doing set 2 vectors, , b, , perform dot-product between them. contain user-defined variables, namely, provided template invokes module. example, talking ethanol (c2h6o) template may like:

{{molar mass calculator |c = 2 |h = 6 |o = 1 }} 

. b contain average atomic mass of each element in grams per mol (g/mol). vector self-provided (i.e., me, have list of 84 elements found in sufficient quantities in nature these standard atomic weights available, standard atomic weights listed. provide them in lua code if show me add them code.).

i have looked dot products in lua see if possible , how them , found code (http://rosettacode.org/wiki/dot_product#lua):

function dotprod(a, b)   local ret = 0   = 1, #a     ret = ret + a[i] * b[i]   end   return ret end  print(dotprod({1, 3, -5}, {4, -2, 1})) 

i believe looking :)

populate "atomicweightlookup" table own values.

there example call "calculate" function @ end of program.

local atomicweightlookup = {  c = 12.01,  h = 1.001,  o = 16 }  local function calculate(input)  -- input example: {c = 2, h = 6, o = 1}  local result = 0  -- iterate through input table  element,quantity in next,input   -- if element not found in table, assume 0 weight.   local atomicweight = atomicweightlookup[element] or 0   -- multiply   result = result + quantity * atomicweight  end  return result end  -- example print(calculate({c = 2, h = 6, o = 1})) 

Comments