numerical-analysis

What algorithm is R using to calculate mean?

元气小坏坏 提交于 2019-12-18 11:45:44
问题 I am curious to know what algorithm R's mean function uses. Is there some reference to the numerical properties of this algorithm? I found the following C code in summary.c:do_summary(): case REALSXP: PROTECT(ans = allocVector(REALSXP, 1)); for (i = 0; i < n; i++) s += REAL(x)[i]; s /= n; if(R_FINITE((double)s)) { for (i = 0; i < n; i++) t += (REAL(x)[i] - s); s += t/n; } REAL(ans)[0] = s; break; It seems to do a straight up mean: for (i = 0; i < n; i++) s += REAL(x)[i]; s /= n; Then it adds

How do I generate points that match a histogram?

纵然是瞬间 提交于 2019-12-17 22:42:59
问题 I am working on a simulation system. I will soon have experimental data (histograms) for the real-world distribution of values for several simulation inputs. When the simulation runs, I would like to be able to produce random values that match the measured distribution. I'd prefer to do this without storing the original histograms. What are some good ways of Mapping a histogram to a set of parameters representing the distribution? Generating values that based on those parameters at runtime?

Numerical Precision in Fortran 95:

别说谁变了你拦得住时间么 提交于 2019-12-17 14:54:08
问题 I have the following Fortran code: Program Strange Real(Kind=8)::Pi1=3.1415926535897932384626433832795028841971693993751058209; Real(Kind=8)::Pi2=3.1415926535897932384626433832795028841971693993751058209_8; Print*, "Pi1=", Pi1; Print*, "Pi2=", Pi2; End Program Strange I compile with gfortran, and the output is: Pi1= 3.1415927410125732 Pi2= 3.1415926535897931 Of course the second is correct, but should this be the case? It seems like Pi1 is being input to memory as a single precision number,

Numerical Precision in Fortran 95:

﹥>﹥吖頭↗ 提交于 2019-12-17 14:53:22
问题 I have the following Fortran code: Program Strange Real(Kind=8)::Pi1=3.1415926535897932384626433832795028841971693993751058209; Real(Kind=8)::Pi2=3.1415926535897932384626433832795028841971693993751058209_8; Print*, "Pi1=", Pi1; Print*, "Pi2=", Pi2; End Program Strange I compile with gfortran, and the output is: Pi1= 3.1415927410125732 Pi2= 3.1415926535897931 Of course the second is correct, but should this be the case? It seems like Pi1 is being input to memory as a single precision number,

How accurate/precise is java.lang.Math.pow(x, n) for large n?

家住魔仙堡 提交于 2019-12-14 02:18:25
问题 I would like to calculate (1.0-p)^n where p is a double between 0 and 1 (often very close to 0) and n is a positive integer that might be on the order of hundreds or thousands (perhaps larger; I'm not sure yet). If possible I would love to just use Java's built in java.lang.Math.pow(1.0-p, n) for this, but I'm slightly concerned that there might be a gigantic loss of accuracy/precision in doing so with the range of values that I'm interested in. Does anybody have a rough idea of what kind of

How to use nested for loops for brute forcing combinations - Python

三世轮回 提交于 2019-12-11 17:56:28
问题 This question was migrated from Computer Science Stack Exchange because it can be answered on Stack Overflow. Migrated 12 months ago . I wish to do the following: 1) Try a variety of input combinations to search for a best result 2) Reset all arrays as they were before each loop of the code Every variable I am working with is in an array such as f[0,1,2,3,...] The issue is likely in the resetting variables after each pass part, as the first pass works fine, but the residuals of the first pass

Souriau method for Characteristic Polynomial

放肆的年华 提交于 2019-12-11 11:23:41
问题 Does anyone know the Souriau method for finding the characteristic polynomial of any n × n matrix? I found out the first coefficient, is obvious, but how can I find out the other coefficients? After I need to inverse the matrix but I know how. #include <iostream> #include <fstream> using namespace std; double trace(double a[5][5],int n){ int i; double trace=0; for(i=0;i<n;i++) trace+=a[i][i]; return trace; } double prod(double a[5][5],double b[5][5],int n) { double c[5][5]; int i,j,k; cout <<

Why is Matlab's inv slow and inaccurate?

纵然是瞬间 提交于 2019-12-11 03:58:15
问题 I read at a few places (in the doc and in this blog post : http://blogs.mathworks.com/loren/2007/05/16/purpose-of-inv/ ) that the use of inv in Matlab is not recommended because it is slow and inaccurate. I am trying to find the reason of this inaccuracy. As of now, Google did not give m interesting result, so I thought someone here could guide me. Thanks ! 回答1: The inaccuracy I mentioned is with the method INV, not MATLAB's implementation of it. You should be using QR, LU, or other methods

Modelica Evaluation Order

无人久伴 提交于 2019-12-11 03:34:17
问题 I can't really find any answer in the Modelica specification so ill ask you guys. The specification states that A tool is free to solve equations, reorder expressions and to not evaluate expressions if their values do not influence the result (e.g. short-circuit evaluation of Boolean expressions). If-statements and if-expressions guarantee that their clauses are only evaluated if the appropriate condition is true, but relational operators generating state or time events will during continuous

Returning 'traditional' notations of functions in the context of fourier interpolation

為{幸葍}努か 提交于 2019-12-10 17:39:40
问题 in numerical analysis we students are obligated to implement code in R that given a function f(x) finds its Fourier interpolation tN(x) and computes the interpolation error $||f(x)-t_{N}(x)||=\int_{0}^{2\pi}$ $|f(x)-t_{N}(x)|^2$ or a variety of different $N$ I first tried to compute the d-coefficients according to this formular: $d = \frac 1N M y$ with M denoting the DFT matrix and y denoting a series of equidistant function values with $y_j = f(x_j)$ and $x_j = e^{\frac{2*pi*i}N*j}$ for $j =