26
votes
Accepted
Approximating constant π² to within error
You don't need to compare prev and new during each iteration.
The difference between the new and the previous sum is simply the ...
19
votes
Approximating constant π² to within error
So, you have a polynomial sequence and want to sum its terms while they are greater than a provided tolerance (i.e.: the tolerance is lower than the current computed term).
This is easily expressed ...
19
votes
Accepted
IEEE 754 square root with Newton-Raphson
This answer uses pointer-casting for type-punning just to save space. In practice keep using your union (safe in ISO C99, and in C++ as a GNU and MSVC extension) or memcpy (safe in C and C++). This ...
17
votes
Accepted
Definite Integral Approximation using the Trapezoidal Method
If you are writing mathematical code in Python, then it is worth looking at NumPy, which is a library implementing (among other things) fast arithmetic operations on arrays of floating-point numbers.
...
15
votes
Approximating constant π² to within error
Please fix the indentation (probably just a copy-pasting issue).
In Python, you don't need parenthesis around the expressions for block statements like if and ...
15
votes
Accepted
Numerical integration in C++: Newton-Cotes formulas
I wonder whether the #include "NewtonCotesFormulas.cpp" at the end of the NewtonCotesFormulas.h header file could be somehow avoided. I used it because without I'd get linking errors.
Yea, ...
14
votes
Accepted
Mean π: Archimedes vs. Gauss - π computation through generalized means
Convergence testing
if pi == piold:
break
This is not usually done, because float equality has a lot of gotchas. In this case it's possible due to ...
13
votes
Accepted
Python π = 1 + (1/2) + (1/3) + (1/4) - (1/5) + (1/6) + (1/7) + (1/8) + (1/9) - (1/10) ...1748 Euler
What you are approximating is
$$ \pi =\sum_{m=1}^{\infty}\frac{(-1)^{s(m)}}{m},$$
where \$s(m)\$ counts the number of appearances of primes of the form \$4k+1\$ in the prime decomposition of \$m\$, ...
12
votes
Accepted
Simulating a two-body collision problem to find digits of Pi
First, this is an awesome video! Upvoted for that reason alone. :)
If n=2, the program finishes within milliseconds, whereas for n=3, it takes a whopping 115 seconds.
Do you know about big-O ...
12
votes
Numerical integration in C++: Newton-Cotes formulas
Use an enum to give names to choices
Consider using an enum, or even better an enum class, ...
11
votes
AVX assembly for fast atan2 approximation
So, just for fun, I've converted your code to intrinsics (and probably made a bunch of mistakes in the process):
...
11
votes
Implementing numerical integration
I see a few things that may help you improve your program.
Avoid pow for float
The use of ...
11
votes
Accepted
Calculate Pi using Monte Carlo
One could consider at least the following points:
Instead of including <stdlib.h>, I'd include <cstdlib>.
In ...
11
votes
IEEE 754 square root with Newton-Raphson
FYI, in IEEE754 sqrt is a "basic" operation that's required to be correctly-rounded (rounding error <= 0.5ulp), same as + - * /. Hardware FPUs (I think) always ...
9
votes
AVX assembly for fast atan2 approximation
Functional problems.
Sign test
Rather than x < 0, use signbit(x).
Although I suspect this is faster, it is to allow code to ...
9
votes
pure Python Bézier curve implementation
Unnecessary Generator
You've got an unnecessary generator expression here:
...
9
votes
A probability distribution function, to be called repeatedly during numerical integration
This is a possible solution to harolds open point about exp in his answer.
Note that exp(x+y) = exp(x) * exp(y) holds for any ...
8
votes
Accepted
Finding the root of a function by Bisection Method
Headers and namespaces
The <cmath> header is referenced, but never used; it can be removed.
If we're to use std::vector, ...
8
votes
Finding the root of a function by Bisection Method
A std::vector<T> vec is an arbitrary collection of Ts. vec might contain none, one or ...
8
votes
Definite Integral Approximation using the Trapezoidal Method
You can use sum with a comprehension to create part_2.
You can move all the maths together if you think it looks nicer.
...
8
votes
IEEE 754 square root with Newton-Raphson
I don't see why you define this constant yourself:
#define MANTISSA_SIZE 52
Given we already assume that FLT_RADIX is 2, we can ...
8
votes
Accepted
C++ class to create and evaluate Chebyshev approximations of arbitrary functions
Naming things
You have a bounds checking evaluation function, operator(), and one that doesn't do bounds checking named ...
8
votes
Accepted
Genetic algorithm to guess coefficient of a polynomial
This is not the best algorithm
If the goal is to get the best coefficients for a polynomial so it fits the given points, then a polynomial regression algorithm such as ...
7
votes
Calculating Maclaurin series for sin(x)
I am not a Haskell expert. Few words from a numerical analyst point of view.
Your concern about recalculating factorial is very well founded. I recommend to take one more step and realize that ...
7
votes
Implementing numerical integration
Improve robustness and readability
Don't using namespace with namespaces not designed for it.
Use const and ...
7
votes
Accepted
A simple definite integrator class of a single variable in C++
You made multiple non-trivial edits while I wrote my answer, so there might be some divergence. (Personal annotation: Code should be (mostly) self explanatory. Don’t add a wall of text beforehand that ...
7
votes
Integrator 2.0: A Simple Integrator in C++17
#pragma once is non-standard.
size_t is never defined. If it's a misspelling of std::size_t...
7
votes
A probability distribution function, to be called repeatedly during numerical integration
A typical way to reduce such cosines with an angle that is steadily counting up by the same increment, is to take a vector and rotate it step by step. That way, one cosine and one sine are calculated, ...
7
votes
Accepted
Implementing a joint differential equation and eigenvalue solver
My primary goal here is to verify the correctness of my method and its implementation in code
You've done some things well - you have seemingly descriptive variable names and comments.
You need to ...
6
votes
Accepted
Multithreaded Monte-Carlo Integration
Since C++17 (and its now 2018) You don;t need to do this ugly thing
namespace boost { namespace math { namespace quadrature {
}}}
You can simply do this:
...
Only top scored, non community-wiki answers of a minimum length are eligible
Related Tags
numerical-methods × 219python × 64
performance × 55
c++ × 53
numpy × 24
algorithm × 23
mathematics × 22
java × 21
c × 20
beginner × 19
python-3.x × 19
random × 18
scipy × 12
reinventing-the-wheel × 10
statistics × 10
simulation × 9
machine-learning × 8
c# × 7
rust × 7
c++11 × 6
c++17 × 6
floating-point × 6
lisp × 6
boost × 6
functional-programming × 5