C
C#2y ago
youtubebin

❔ Need help making a chemical equation balancer in C#

Hi guys. As the title suggests, I need some help writing a chemical equation balancer in C#. To be exact, in Unity (the game engine). I need it for the project I'm making (a virtual lab) and I have no idea how to make it work. It seems to be immensely complex and I can't go forward without it. The algorithm must: 1) be compatible with the unity game engine, 2) be able to operate on any chemical equation, no matter the amount of reactants and products, 3) the input must be a string. Thanks in advance, Bin.
32 Replies
Klarth
Klarth2y ago
It's certainly not trivial given constraints 1 and 2.
youtubebin
youtubebinOP2y ago
Well, I put these only as a precaution just in case
Klarth
Klarth2y ago
.NET is not particularly strong with math and Unity even less so.
youtubebin
youtubebinOP2y ago
Well, I will be heading off now Goodnight
Google
Google2y ago
Why not just Simplex?
Klarth
Klarth2y ago
Google
Google2y ago
It all reduces to a simple integer programming problem.
youtubebin
youtubebinOP2y ago
ok, seriously, I'm heading off to sleep Goodnight Hello again @JamesK.Polk So... Any ideas?
Google
Google2y ago
I already gave you my full idea.
youtubebin
youtubebinOP2y ago
Fair enough
LPeter1997
LPeter19972y ago
@JamesK.Polk Why the hell would you turn this into an IP problem, which by the way has nothing to do with Simplex, which is an LP solver
Google
Google2y ago
because it's simple
LPeter1997
LPeter19972y ago
Balancing a chemical equation should be the same as balancing any equation IIRC. You can solve a system of linear equations, which is just a matter of Gauss-elimination
Google
Google2y ago
Sure, however you want to solve it is fine.
LPeter1997
LPeter19972y ago
IP problem? Simple? Are you out of your mind
Google
Google2y ago
alglib.net non-commercial license could get it done quickly.
LPeter1997
LPeter19972y ago
It's a famous NP-hard problem For something that can be done in linear time, it's a horrible recommendation
Google
Google2y ago
Direct your comments towards them, not me. Not really used to having language like this directed at me. I gave a recommendation, and it sounds like you have very strong feelings about it.
Google
Google2y ago
Instructables
Balancing Chemical Equations With Spreadsheets
Balancing Chemical Equations With Spreadsheets: Balancing Chemical Equations with Spreadsheet Matrix Algebra Given the products and reactants of a chemical reaction, we want to find the stoichiometrically balanced equation for the reaction. This can be done with the matrix functions of a spread…
LPeter1997
LPeter19972y ago
@Bin You can represent the problem as a series of linear equations that you can solve using Gauss-elimination. There's prolly linear algebra libs already out there, likely targeting ns 2.0 that you'll be able to use in Unity too I'm sorry for the strong language, it's nothing personal. I just hate when people seemingly talk out of their asses.
Google
Google2y ago
The solver used in that tutorial is Simplex. I've done this exact problem before in a few hours. I'm not sure where you get that I'm talking out my ass. I also just noted it could be reduced to IP problem if they cared about performance.
LPeter1997
LPeter19972y ago
If they reduce it to an IP problem, it becomes slower
Google
Google2y ago
You could also go the LU decomposition route if you wanted.
LPeter1997
LPeter19972y ago
IP solving is hard and way more inefficient than Gauss-elimination No need for matrix triangulation either, that's also way more complex than it has to be
Google
Google2y ago
That's right. There are many ways to skin this cat. I just provided my two cents. Sorry I did.
LPeter1997
LPeter19972y ago
Then please, stop recommending horribly complex ways to incredibly simple problems
Google
Google2y ago
You can get both of those out of the box with existing Github links. The actual implementation doesn't need to be observed. Please don't tag me anymore just to do this. Focus on helping users, not flaming other users.
ero
ero2y ago
wait i remember this guy, they asked for help on this in the unity discord like many many months ago and refused to take any advice that would have made approaching this a million times easier
youtubebin
youtubebinOP2y ago
key word: many many months ago
Accord
Accord2y ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.
youtubebin
youtubebinOP2y ago
Well I am working on it but uh something's wrong it *should( give me: [ [ 1, 0, 0, -2 ], [ 0, 2, -1, 0 ], [ 0, 4, 0, -3 ] ] instead some numbers are wrong it gives me [ [1, 0, -1, -2], [0, 2, -1, -1], [0, 4, -1, -3] ], I believe the issue lies somewhere here:
foreach (string molecule in righ)
{
int index = molecule.IndexOf(elem);
if (index == -1) buildArr.Add(-1);
else
{
index += elem.Length;
Match numberAfterElement = Regex.Match(molecule.Substring(index), @"^\d+");
if (numberAfterElement == null || !numberAfterElement.Success)
{
buildArr.Add(-1);
}
else
{
buildArr.Add(-1 * Int32.Parse(numberAfterElement.Value));
string test = "";
foreach (var z in buildArr) test += $"{z}, ";
Debug.Log(test);
}
}
}
rrefArray.Add(buildArr);
foreach (string molecule in righ)
{
int index = molecule.IndexOf(elem);
if (index == -1) buildArr.Add(-1);
else
{
index += elem.Length;
Match numberAfterElement = Regex.Match(molecule.Substring(index), @"^\d+");
if (numberAfterElement == null || !numberAfterElement.Success)
{
buildArr.Add(-1);
}
else
{
buildArr.Add(-1 * Int32.Parse(numberAfterElement.Value));
string test = "";
foreach (var z in buildArr) test += $"{z}, ";
Debug.Log(test);
}
}
}
rrefArray.Add(buildArr);
I simply can't find it (righ is an string array) elem is also a string
Accord
Accord2y ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.
Want results from more Discord servers?
Add your server