❔ 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
It's certainly not trivial given constraints 1 and 2.
Well, I put these only as a precaution
just in case
.NET is not particularly strong with math and Unity even less so.
Well, I will be heading off now
Goodnight
Why not just Simplex?
I'd do a quick look over https://python.plainenglish.io/balancing-chemical-equations-using-python-4b9086a92a7c for brute force or maybe something newer than https://www.sciencedirect.com/science/article/pii/S0895717706000367 if you really mean "any".
It all reduces to a simple integer programming problem.
ok, seriously, I'm heading off to sleep
Goodnight
Hello again
@JamesK.Polk
So...
Any ideas?
I already gave you my full idea.
Fair enough
@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
because it's simple
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
Sure, however you want to solve it is fine.
IP problem? Simple? Are you out of your mind
alglib.net non-commercial license could get it done quickly.
It's a famous NP-hard problem
For something that can be done in linear time, it's a horrible recommendation
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.
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…
@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.
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.
If they reduce it to an IP problem, it becomes slower
You could also go the LU decomposition route if you wanted.
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
That's right. There are many ways to skin this cat.
I just provided my two cents. Sorry I did.
Then please, stop recommending horribly complex ways to incredibly simple problems
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.
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
key word: many many months 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.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:
I simply can't find it
(righ is an string array)
elem is also a string
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.