MolecularWeightCalculator 1.0.5

dotnet add package MolecularWeightCalculator --version 1.0.5
NuGet\Install-Package MolecularWeightCalculator -Version 1.0.5
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="MolecularWeightCalculator" Version="1.0.5" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add MolecularWeightCalculator --version 1.0.5
#r "nuget: MolecularWeightCalculator, 1.0.5"
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install MolecularWeightCalculator as a Cake Addin
#addin nuget:?package=MolecularWeightCalculator&version=1.0.5

// Install MolecularWeightCalculator as a Cake Tool
#tool nuget:?package=MolecularWeightCalculator&version=1.0.5

Molecular Mass Weight Calculator

This is a C# library for calculating the molecular mass of chemical compounds. It allows you to input a chemical formula and computes the total molecular weight by summing up the atomic weights of the constituent elements.

Features

  • Calculate the molecular weight of chemical compounds.
  • Support for standard atomic weights of elements.
  • Simple and intuitive DLL for integration into your projects.

Installation

You can install the Molecular Mass Calculator library via NuGet Package Manager Console:

NuGet\Install-Package MolecularWeightCalculator

Usage

Here's how you can use the library to calculate the molecular weight of a chemical compound:

var molecularMath = new MolecularMath();
string exp1 = "CO2";
DisplayExpressionInfo(exp1);


string exp2 = "CaCO3";
DisplayExpressionInfo(exp2);

string exp3 = "CO2 / CaCO3";
DisplayExpressionInfo(exp3);

string exp4 = "C2H2 +2.5 * O2";
DisplayExpressionInfo(exp4);


string exp5 = "2*CO2/C2H2";
DisplayExpressionInfo(exp5);

string exp6 = "1*2*3*4";
DisplayExpressionInfo(exp6);

string exp7 = "CaCO3 * A2 + B3";
DisplayExpressionInfo(exp7);
//KeyNotFoundException: 'A' was not present in the Periodic Table

//calculate only contain C (Carbon)

Console.WriteLine($"===== calculate only contain C (Carbon) =====");
string filterMoleculars = "C";
string exp8 = "CaO + CO2";
DisplayExpressionInfo(exp8, filterMoleculars);

string exp9 = "C2H2 +2.5*O2";
DisplayExpressionInfo(exp9, filterMoleculars);

string expA = "2*CO2 + H2O";
DisplayExpressionInfo(expA, filterMoleculars);




Console.WriteLine($"Press any key to exit.....");
Console.ReadKey();

void DisplayExpressionInfo(string expression, string filterMoleculars="")
{
    Console.WriteLine($"==={expression}, filter Moleculars:({filterMoleculars}){new String('=', 10)}");
    string[] filterMolecularsArray = filterMoleculars.Split(',', StringSplitOptions.RemoveEmptyEntries);
    try
    {
        var parameters = molecularMath.GetParameters(expression);
        Console.WriteLine($"==={expression}:Parameters({parameters.Count}),{new String('=', 10)}");
        foreach (var parameter in parameters)
        {
            Console.WriteLine(parameter);
        }
        Console.WriteLine(new String('=', 30));
        var result = molecularMath.ComputeMass(expression, filterMolecularsArray);
        Console.WriteLine($"{expression}=>{result}");
    }
    catch (Exception ex)
    {
        Console.WriteLine(ex);
    }
    
    Console.WriteLine(new String('*', 50));
}

Execute Result:

===CO2, filter Moleculars:()==========
===CO2:Parameters(1),==========
CO2
==============================
CO2=>44.009
**************************************************
===CaCO3, filter Moleculars:()==========
===CaCO3:Parameters(1),==========
CaCO3
==============================
CaCO3=>100.086
**************************************************
===CO2 / CaCO3, filter Moleculars:()==========
===CO2 / CaCO3:Parameters(2),==========
CO2
CaCO3
==============================
CO2 / CaCO3=>0.43971184781088263
**************************************************
===C2H2 +2.5 * O2, filter Moleculars:()==========
===C2H2 +2.5 * O2:Parameters(2),==========
C2H2
O2
==============================
C2H2 +2.5 * O2=>106.033
**************************************************
===2*CO2/C2H2, filter Moleculars:()==========
===2*CO2/C2H2:Parameters(2),==========
CO2
C2H2
==============================
2*CO2/C2H2=>3.3803671556955224
**************************************************
===1*2*3*4, filter Moleculars:()==========
===1*2*3*4:Parameters(0),==========
==============================
1*2*3*4=>24
**************************************************
===CaCO3 * A2 + B3, filter Moleculars:()==========
===CaCO3 * A2 + B3:Parameters(3),==========
CaCO3
A2
B3
==============================
System.Collections.Generic.KeyNotFoundException: 'A' was not present in the Periodic Table
**************************************************
===== calculate only contain C (Carbon) =====
===CaO + CO2, filter Moleculars:(C)==========
===CaO + CO2:Parameters(2),==========
CaO
CO2
==============================
CaO + CO2=>44.009
**************************************************
===C2H2 +2.5*O2, filter Moleculars:(C)==========
===C2H2 +2.5*O2:Parameters(2),==========
C2H2
O2
==============================
C2H2 +2.5*O2=>26.037999999999997
**************************************************
===2*CO2 + H2O, filter Moleculars:(C)==========
===2*CO2 + H2O:Parameters(2),==========
CO2
H2O
==============================
2*CO2 + H2O=>88.018
**************************************************

ChangeLog

1.0.2

  1. fix: change ComputeMass method return type from double to object

1.0.3

  1. change Periodic Table from https://iupac.org/what-we-do/periodic-table-of-elements/

1.0.4

  1. Add filtering to only calculate the molecular weight of compounds with certain chemical elements, such as only contain C (Carbon)

1.0.5

  1. fix Provide analytical expressions and obtain parameter information issue
  2. Add MolecularWeightCalculator.Tests.csproj

Contributing

Contributions are welcome! If you find any issues or have suggestions for improvements, feel free to open an issue or submit a pull request.

License

License: MIT

Product Compatible and additional computed target framework versions.
.NET net5.0 was computed.  net5.0-windows was computed.  net6.0 was computed.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 was computed.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed. 
.NET Core netcoreapp2.0 was computed.  netcoreapp2.1 was computed.  netcoreapp2.2 was computed.  netcoreapp3.0 was computed.  netcoreapp3.1 was computed. 
.NET Standard netstandard2.0 is compatible.  netstandard2.1 was computed. 
.NET Framework net461 was computed.  net462 was computed.  net463 was computed.  net47 was computed.  net471 was computed.  net472 was computed.  net48 was computed.  net481 was computed. 
MonoAndroid monoandroid was computed. 
MonoMac monomac was computed. 
MonoTouch monotouch was computed. 
Tizen tizen40 was computed.  tizen60 was computed. 
Xamarin.iOS xamarinios was computed. 
Xamarin.Mac xamarinmac was computed. 
Xamarin.TVOS xamarintvos was computed. 
Xamarin.WatchOS xamarinwatchos was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
1.0.5 76 3/6/2024
1.0.4 90 3/5/2024
1.0.3 92 3/4/2024
1.0.2 116 3/1/2024
1.0.1 113 3/1/2024
1.0.0 109 3/1/2024

### 1.0.2

 1. fix: change ComputeMass method return type from double to object

 ### 1.0.3

 1. change Periodic Table from https://iupac.org/what-we-do/periodic-table-of-elements/

 ### 1.0.4

 1. Add filtering to only calculate the molecular weight of compounds with certain chemical elements, such as only contain C (Carbon)
 ### 1.0.5
 1. fix [Provide analytical expressions and obtain parameter information](https://github.com/rainmakerho/MolecularWeightCalculator/issues/1) issue