Show HN: Algebra.js – Build, display, and solve algebraic equations
algebra.js.org
algebra.js.org
I was learning JavaScript by completing challenges on CoderByte[0], where one of the hard challenges was to find the point where two lines intersect. The lines were defined by two sets of points, and you had to return your answers as rationals, not floats. The challenge isn't timed, so I got a little carried away: ended up making a Fraction class, Equation class, etc. This first iteration could only solve linear equations, but I decided to expand it out into a library that could solve higher order polynomials and manipulate expressions.
On a related note, solving cubics is actually kind of hard. This guy Cardano[1] figured it out[2] in the 1500s, but his solution was incomplete because he wasn't aware of imaginary numbers at the time. I then found you could solve cubics with some trig[3] and decided to go that route. Anyway, hope you enjoy, and of course I am interested in your feedback.
[1] https://en.wikipedia.org/wiki/Gerolamo_Cardano
[2] https://en.wikipedia.org/wiki/Cubic_function#Cardano.27s_met...
So in order to implement qubic equations you'd need (a) support for complex numbers, even though the final solution might be real and (b) support for reducing (square and higher order) roots. (b) would be a very valuable addition, as it also allows you to incorporate equations like x^2 - 2 = 0 which solves to x = sqrt(2) or x = -sqrt(2).
> At this point, Cardano, who did not know complex numbers, supposed that the roots of this equation were real, that is that q^2/4 + p^3/27 > 0.
I actually had initial plans to do it Cardano's way: I added a Complex class for the very situation you described[0], but ended up abandoning it as the trig solution seemed more straightforward.
[0] https://github.com/nicolewhite/algebra.js/commit/47b6cbba375...
Jason Long's Cayman Theme was unfamiliar to me until I saw your citation. Kudos to your good taste.
Documentation is pretty good. It probably skews more towards developers with experience using other libraries, but I think that's fine for a first version. If you want to kick it up some more, I'd love to see more practical examples that might inspire me to use it out in the wild. Also, it took me awhile to figure out what to do with the keyboard shortcut. Something more explicit or even a small screencast would be nice.
I see that you also work on the RNeo4j library, which looks super cool. Queueing that up in the future to play with...since R is a much better place for me to feed that graphing library data. Thank you so much for sharing this!
Glad to hear you're interested in RNeo4j; let me know what you think.
console.log("Type the following in this order pressing enter each time you see a semi-colon: \n var expr = new Expression('x'); \n expr = expr.subtract(3); \n expr = expr.add('x'); \n console.log(expr.toString()); \n var eq = new Equation(expr, 4); \n console.log(eq.toString()); var x = eq.solveFor('x'); \n console.log('x = '' + x.toString());");
Very cool project by the way.I work on a similar library for C# (Symbolism [1]).
Consider allowing for variable elimination in sets of equations. (See this problem for an example: https://gist.github.com/dharmatech/a14d1a29a7d4c0728d37)
If you want to look up how the big time guys do it, I believe Mathematica's 'Reduce' performs full on quantifier elimination via cylindrical algebraic decomposition. There aren't many open-source implementations of CAD that I know of besides the venerable C-based QEPCAD.
PS: Big fan of your Neo4J tutorials. :-)
it's not a feature request because I don't have a use case for this quite yet, but how complex would it be to parse a string that contains a formula? for example, it seems useful to be able to load equations by just sending the string "2x - 3 = 4" to a function.
that sounds like a fun problem to solve as well. maybe useful to the library too. just a thought! great work on this library and thanks for publishing.
Parsing strings into expressions is something that math.js offers currently with math.parse and expression trees[0]; I was discussing it with the author the other day[1].
[0] http://mathjs.org/docs/expressions/expression_trees.html
[1] http://www.mathworks.com/help/symbolic/equation-solving.html
I definitely want to expand the library's functionality; this project has been my go-to for when I'm burnt out on other projects so I don't plan on putting it down anytime soon. Some basic calculus seems like a natural next step, though that might betray the name algebra.js. :)
( Whiteboard ?
A place to do math, obviously, and you did say you go to it when bored of other projects. )
E.g. Sage math (http://www.sagemath.org/)
I clicked the "Download .min.js" tab on the Algebra.js.org page but was given this error message :
new CompoundExpression( "x^2 - xy + 5" )
or even new PolishSquarefree("+")("+")("^")("x")(2)("-xy")(5)
or even new Polish("+")("-")("^")("x")(2)(".")("x")("y")(5)But nice work, it looks really professional for an out of hand learning project!
That's exactly right. You could build a quintic equation, but attempting to solve will return undefined.