Skip to main content
Share Your Experience: Take the 2024 Developer Survey
12 votes
Accepted

How to document math formulations in scientific computing codes?

I prefer using doxygen that supports C++ and LaTeX comments, both inline and as separate equations. This way, you will keep your comments, including, say, the rigorous mathematical formulation of the ...
Anton Menshov's user avatar
  • 8,672
11 votes
Accepted

Memory Management - Why certain initializations order are faster?

Such an effect happens because of how the data of the int** a is stored in memory (as per C/C++). This question on StackOverflow has answers with some more details (...
Anton Menshov's user avatar
  • 8,672
10 votes
Accepted

Numerical evaluation of the Exponential Integral Ei by rational Chebyshev approximations fails

There is a prime missing both in your code and in the expression in your question. In the original paper, the expression is: $$ \log(x/x_0) + (x-x_0) \frac{\sum^\prime_{0\leq j\leq n}p_j T_j^*(x/6)}{\...
Kirill's user avatar
  • 11.4k
9 votes
Accepted

C standard for computational science

In theory, as the original authors, you're free to pick and name a standard, then expect others to follow it. In practise, if you're supporting an HPC system, then your choice is likely to be ...
origimbo's user avatar
  • 2,249
8 votes
Accepted

What PRNG function is this?

A little playing with the sequence of numbers generated by the C code shows that the sequence is $z_{i+1}=5z_{i}+273 \mod 2^{16}$ This is a linear congruential generator (LCG). It's easy to show ...
Brian Borchers's user avatar
8 votes
Accepted

Suming up the series $1+1/2+...+1/n$ in C

I see several problems with your code: type error in scanf (n is an integer, not a float) i=0 and not i==0 in the for loop ...
Miyase's user avatar
  • 196
7 votes

C standard for computational science

You should definitely jump to C99, or newer(!). The C99 standard introduced the restrict keyword. Loosely speaking, with this keyword you can inform the compiler ...
wim's user avatar
  • 571
7 votes

Numerical integration giving incorrect sign

This might be an accuracy problem in computing the second term, because of those large exponentials when $x \gg 1$. I would first work on that term: gather $e^x$ out from numerator and denominator and ...
Federico Poloni's user avatar
6 votes

Looking for Runge-Kutta 8th order in C/C++

summarizing some points: If it's a long-term integration of a non-dissapative model, a symplectic integrator is what you're looking for. Otherwise, since it's an equation of motion, Runge-Kutta ...
Chris Rackauckas's user avatar
6 votes

I've developed a derivative-free optimization method, looking for comments

I would like to hear comments from users that have some practical models (e.g. black-box hyperparameter optimization) which are still needed to be solved acceptably - whether this method works or not ...
GoHokies's user avatar
  • 2,216
6 votes
Accepted

Using GSL for basic operations

Of course it makes sense to use the GSL (or another library for that matter) for several reasons: Don't reinvent the wheel. The work has been done, you can spend your time on more useful things. If ...
GertVdE's user avatar
  • 6,149
6 votes
Accepted

What language are best for creating data compression algorithms except for C/C++?

In my general experience, the following are required in order to get "reasonable" performance from a programming language: The ability to specify static types. It's OK if the language also ...
helloworld922's user avatar
5 votes
Accepted

How to deal with big numbers in intermediate calculations?

I think there's a simple way to do this. You have a rational function of identical cosh/sinh terms, where every expression is a homogeneous polynomial in cosh/sinh, and the only problem is that these ...
Kirill's user avatar
  • 11.4k
5 votes
Accepted

Recommendations for ODE solvers for stiff equations

This is a huge open ended question, but I'll copy the recommendation section from the current release of DifferentialEquations.jl: Stiff Problems For stiff problems at high tolerances (>1e-2?) it ...
Chris Rackauckas's user avatar
5 votes

float in C: Size of the exponent

$1.2\times 10^{-38}$ is a decimal value. You need to convert it to binary exponential notation to see how the bits are set. Another small mistake: $2^6+...2^1+2^0$ does not equal $2^{27}$, it equals $...
Federico Poloni's user avatar
4 votes

Jacobi iteration for finite difference: when to stop?

For solving systems you shouldn't be comparing the results between each iteration but rather computing the residual. If you consider the matrix representation to be in the standard form: $ A\cdot y ...
user3209427's user avatar
4 votes
Accepted

C or fortran library to solve linear 2D/3D elliptic PDE

Most of the widely used finite element libraries are written in C++. If all you really care for -- and if all you will ever care for -- is solving an elliptic PDE on a rectangle, then it's probably ...
Wolfgang Bangerth's user avatar
4 votes
Accepted

Number of decimal of float and double in C

Prof. Bangerth's comment is completely correct. To add more detail, we can refer to IEEE754 standard which defines the floats as ...
Abdullah Ali Sivas's user avatar
3 votes

Potential gain - Matlab vs C/C++ - assembly and eigenvalues

It is unlikely that you can benefit a lot from rewriting this particular code in C++. Main reasons: you are already assembling a sparse matrix using Matlab-specific framework for sparse matrices ...
Anton Menshov's user avatar
  • 8,672
3 votes

Efficient eigen-decomposition of covariance matrix

TL;DR: I don't think you will be able to beat any real/vendor eigensolver, but it's fun to think about. Deeper cut: One classic algorithm for symmetric eigendecomposition is tridiagonalizing A=QTQ' ...
rchilton1980's user avatar
  • 4,896
3 votes
Accepted

Simple open-source Quantum chemistry or DFT code in C/C++

Quantum Chemistry codes can get very complicated very fast. Even if you limit yourself to DFT, there are many functionals to support. There's going to be a trade off here for you. You can get a simple,...
FifthArrow's user avatar
3 votes
Accepted

How to deal with very low numerical values in C?

The best thing to do would be to re-scale your problem so that way you don't have such small values. If you can, do this and call it a day. If every number is small, then you can still be fine ...
Chris Rackauckas's user avatar
3 votes

Decrease execution time using openMP

The algorithm you are using spawns extremely cheap tasks, especially at small $n$. As you don't have a luxury of changing the algorithm, the only reasonable (relatively) way to avoid the overhead of ...
Anton Menshov's user avatar
  • 8,672
3 votes
Accepted

Linear solve using CHLOMOD in C

Here is how I call CHOLMOD from a C program. It uses as input my own C++ structure for a sparse matrix (in standard Compressed Row Storage format). There are several gotchas: understanding the ...
BrunoLevy's user avatar
  • 2,315
3 votes
Accepted

Implementing velocity verlet for harmonic oscillator in C gives error wrt conservation of energy

Your second acceleration ao in the else statement does not make sense. Since it assumes that the calculated position and ...
fibonatic's user avatar
  • 450
3 votes

Looking for Runge-Kutta 8th order in C/C++

I would like to add that while what Geoff Oxberry suggests for long-term integration (using symplectic integrators) is true, in some cases it won't work. More specifically, if you have dissipative ...
viiv's user avatar
  • 45
3 votes

MPI data broadcast or not in C

The question I think you're asking, based on your code, is: A. Better to have each node do redundant work B. Better to have one node do the work distribute the results. The answer is usually A. To ...
Richard's user avatar
  • 3,971
3 votes

How good are current implementations of rand() in C?

But these seem pretty old criticisms of the rand function This may be a nitpick, but I want to point out what I think is a flaw in this logic. Compilers are often extremely conservative about ...
Kirill's user avatar
  • 11.4k
3 votes
Accepted

Wrong result of 'ddot' from BLAS

The strange thing in your code is ddot_ being declared as extern C int, while it is actually a ...
Anton Menshov's user avatar
  • 8,672
3 votes

Finding a shortest path in a graph

Based on the provided description as well as the figure, you have an undirected graph with a single source and a sink at hand. The most famous option is to implement Dijkstra's algorithm. There are ...
Ehsan's user avatar
  • 151

Only top scored, non community-wiki answers of a minimum length are eligible