-
-
Notifications
You must be signed in to change notification settings - Fork 56
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add functionality to solve quadratic equation #397
Comments
|
We do not have support for lists/tuples so far (see #261), but you can already do this today, by adding code like this to your # Solve the quadratic equation: a x² + b x + c == 0
fn quadratic_equation(a: Scalar, b: Scalar, c: Scalar) -> Scalar =
(-b + sqrt(b^2 - 4 a c)) / 2 a This will only show you one of the solutions, but you can add a fourth parameter to toggle the sign or add a second function to show you the second solution. Or you do something like this: fn _qe_solution<A, B>(a: A, b: B, c: B²/A, sign: Scalar) -> B/A =
(-b + sign × sqrt(b² - 4 a c)) / 2 a
# Solve the quadratic equation: a x² + b x + c == 0
fn quadratic_equation<A2, B2>(a: A2, b: B2, c: B2²/A2) -> String =
"x₁ = {_qe_solution(a, b, c, 1)}; x₂ = {_qe_solution(a, b, c, -1)}" which shows you both solutions (and even allows for dimensionful a, b, c coefficients):
Of course, this needs error handling for the |
Wouldn't it be nicer to instead add more general symbolics support?
|
To be completely honest, I am also not sure we want specialized functions like And also, I'm not sure if it's the right approach to have specialized functions to solve quadratic equations, linear equations, cubic equations, … On the other hand:
Yes, it would be nice. But that is completely out of scope for Numbat. I don't think we can just casually implement a "small CAS". That is a huge undertaking. See also the "non features" section in our README (https://github.com/sharkdp/numbat). Something that seems in scope for Numbat, however, would be a numerical equation solver. Once we have closures (#347) and lambdas (#374, #261), we could implement a function similar to Mathematics nsolve(f: Fn[(Scalar) -> Scalar], x0: Scalar) -> Scalar in this way: let a = 9
let b = 10
let c = 2
fn f(x) = a * x^2 + b * x + c # or any other complicated expression
nsolve(f, 0) # would return one of the two solutions -0.261583 or -0.849528, probably -0.261583
nsolve(f, -10) # would return -0.849528 |
We now fully support lists (#443), and I have changed
This is now also supported: #451 |
No description provided.
The text was updated successfully, but these errors were encountered: