Comments (6)
To be completely honest, I am also not sure we want specialized functions like solve_quadratic_equation
in the Numbat prelude at the current point in time. First, we don't even support for lists and can only return strings. Which is okay for a hack in someones personal prelude, but not for the "standard library".
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:
Wouldn't it be nicer to instead add more general symbolics support?
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
. Maybe first restricted to a single unknown. Users might be required to rewrite their equation in the form f(x)==0
(which is completely trivial). And then they could call the function nsolve
with signature
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
from numbat.
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 init.nbt
file:
# 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):
>>> quadratic_equation(1, 1, -1)
= x₁ = 0.618034; x₂ = -1.61803
Of course, this needs error handling for the b² - 4 a c < 0
case.
from numbat.
we don't even support for lists and can only return strings
We now fully support lists (#443), and I have changed quadratic_equation
to return a list of solutions (#450).
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
. Maybe first restricted to a single unknown. Users might be required to rewrite their equation in the formf(x)==0
(which is completely trivial). And then they could call the functionnsolve
with signaturensolve(f: Fn[(Scalar) -> Scalar], x0: Scalar) -> Scalar
This is now also supported: #451
from numbat.
It'd be nice if users could do this.
from numbat.
$ git pull && time cargo clippy -- --allow clippy::result_large_err --deny warnings && time cargo run
Already up to date.
Checking numbat-exchange-rates v0.5.0 (/home/hamir/numbat/numbat-exchange-rates)
Checking numbat v1.11.0 (/home/hamir/numbat/numbat)
Checking numbat-cli v1.11.0 (/home/hamir/numbat/numbat-cli)
Finished dev [unoptimized + debuginfo] target(s) in 2.49s
real 0m2.620s
user 0m2.412s
sys 0m0.209s
Finished dev [unoptimized + debuginfo] target(s) in 0.10s
Running `target/debug/numbat`
█▄░█ █░█ █▀▄▀█ █▄▄ ▄▀█ ▀█▀ Numbat 1.11.0
█░▀█ █▄█ █░▀░█ █▄█ █▀█ ░█░ https://numbat.dev/
>>> quadratic_equation(9.0, -126.0, 441.0)
quadratic_equation(9, -126, 441)
= (7, 7)
>>>
from numbat.
Wouldn't it be nicer to instead add more general symbolics support?
- Allow functions to take in raw unevaluated expressions, including unbound symbols. Users could just pass expressions like
solve(a x^2 + b x + c = 0, [x])
. - If not already present, add more advanced structural pattern matching to the language.
- Implement an optional small CAS library in Numbat, allowing to easily expand from solving basic equations to symbolic trigonometry, differential calculus, etc.
from numbat.
Related Issues (20)
- why is creating a function that has raising a power in it so complicated? HOT 2
- Allow for "forced let" HOT 1
- Random distribution sampling HOT 4
- Suggest correct casing if the entered unit doesn't exist HOT 3
- Add info for functions HOT 1
- Binary operations with callables
- Conversion of rotational speeds is not correct HOT 1
- Feature request: Option to disable color HOT 3
- Type system extensions HOT 1
- Add glibc (libc6) as dependency to debian package HOT 2
- New dimensions HOT 2
- Typed holes
- Currency Providers HOT 4
- Add lowercase aliases for currencies? HOT 1
- Add support for raw strings HOT 4
- layout shifts after terminal loads HOT 1
- Type inference: follow-up tasks
- Lists are embarrassingly slow
- Give `quadratic_equation` a `List<B/A>` return type.
- Generic structs
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from numbat.