public Molecule(string empiricalFormula) { if (empiricalFormula.Contains(".")){ string[] q = empiricalFormula.Split('.'); Molecule[] m = new Molecule[q.Length]; int[] w = new int[q.Length]; for (int i = 0; i < q.Length; i++){ m[i] = new Molecule(q[i]); w[i] = 1; } Molecule x = Sum(m, w); AtomType = x.AtomType; AtomCount = x.AtomCount; CalcMasses(); } else{ int[][] tc = ProcessEmpiricalFormula(empiricalFormula); AtomType = tc[0]; AtomCount = tc[1]; CalcMasses(); } }
protected bool Equals(Molecule other) { return ArrayUtils.EqualArrays(AtomType, other.AtomType) && ArrayUtils.EqualArrays(AtomCount, other.AtomCount); }
public static Molecule Max(Molecule x, Molecule y) { int[] counts1 = new int[ChemElement.Elements.Length]; for (int j = 0; j < x.AtomType.Length; j++){ counts1[x.AtomType[j]] = x.AtomCount[j]; } int[] counts2 = new int[ChemElement.Elements.Length]; for (int j = 0; j < y.AtomType.Length; j++){ counts2[y.AtomType[j]] = y.AtomCount[j]; } int[] counts = new int[ChemElement.Elements.Length]; for (int i = 0; i < ChemElement.Elements.Length; i++){ counts[i] = Math.Max(counts1[i], counts2[i]); } int[] types = new int[counts.Length]; int count = 0; for (int i = 0; i < counts.Length; i++){ if (counts[i] > 0){ types[count++] = i; } } Array.Resize(ref types, count); return new Molecule(types, ArrayUtils.SubArray(counts, types)); }
public static Molecule Sum(Molecule molecule1, Molecule molecule2) { return Sum(new[]{molecule1, molecule2}); }
public static Tuple<Molecule, Molecule> GetDifferences(Molecule molecule1, Molecule molecule2) { int[] counts1 = new int[ChemElement.Elements.Length]; for (int j = 0; j < molecule1.AtomType.Length; j++){ counts1[molecule1.AtomType[j]] = molecule1.AtomCount[j]; } int[] counts2 = new int[ChemElement.Elements.Length]; for (int j = 0; j < molecule2.AtomType.Length; j++){ counts2[molecule2.AtomType[j]] = molecule2.AtomCount[j]; } for (int i = 0; i < counts1.Length; i++){ if (counts1[i] > counts2[i]){ counts1[i] -= counts2[i]; counts2[i] = 0; } else{ counts2[i] -= counts1[i]; counts1[i] = 0; } } int[] types1 = new int[counts1.Length]; int count1 = 0; for (int i = 0; i < counts1.Length; i++){ if (counts1[i] > 0){ types1[count1++] = i; } } Array.Resize(ref types1, count1); Molecule diff1 = new Molecule(types1, ArrayUtils.SubArray(counts1, types1)); int[] types2 = new int[counts2.Length]; int count2 = 0; for (int i = 0; i < counts2.Length; i++){ if (counts2[i] > 0){ types2[count2++] = i; } } Array.Resize(ref types2, count2); Molecule diff2 = new Molecule(types2, ArrayUtils.SubArray(counts2, types2)); return new Tuple<Molecule, Molecule>(diff1, diff2); }