numerical-methods

Making C code plot a graph automatically

北城以北 提交于 2019-11-27 03:25:50
I have written a program which writes a list of data to a '.dat' file with the intention of then plotting it separately using gnuplot. Is there a way of making my code plot it automatically? My output is of the form: x-coord analytic approximation x-coord analytic approximation x-coord analytic approximation x-coord analytic approximation x-coord analytic approximation .... Ideally, when I run the code the graph would also be printed with an x-label, y-label and title (which could be changed from my C code). Many thanks. I came across this while searching for something else regarding gnuplot.

sample random point in triangle [closed]

这一生的挚爱 提交于 2019-11-27 01:59:27
问题 Suppose you have an arbitrary triangle with vertices A , B , and C . This paper (section 4.2) says that you can generate a random point, P , uniformly from within triangle ABC by the following convex combination of the vertices: P = (1 - sqrt(r1)) * A + (sqrt(r1) * (1 - r2)) * B + (sqrt(r1) * r2) * C where r1 and r2 are uniformly drawn from [0, 1] , and sqrt is the square root function. How do you justify that the sampled points that are uniformly distributed within triangle ABC ? EDIT As

Implementing the derivative in C/C++

走远了吗. 提交于 2019-11-26 22:39:21
问题 How is the derivative of a f(x) typically calculated programmatically to ensure maximum accuracy? I am implementing the Newton-Raphson method, and it requires taking of the derivative of a function. 回答1: I agree with @erikkallen that (f(x + h) - f(x - h)) / 2 * h is the usual approach for numerically approximating derivatives. However, getting the right step size h is a little subtle. The approximation error in ( f(x + h) - f(x - h)) / 2 * h decreases as h gets smaller, which says you should

How can I use numpy.correlate to do autocorrelation?

痞子三分冷 提交于 2019-11-26 19:24:57
I need to do auto-correlation of a set of numbers, which as I understand it is just the correlation of the set with itself. I've tried it using numpy's correlate function, but I don't believe the result, as it almost always gives a vector where the first number is not the largest, as it ought to be. So, this question is really two questions: What exactly is numpy.correlate doing? How can I use it (or something else) to do auto-correlation? A. Levy To answer your first question, numpy.correlate(a, v, mode) is performing the convolution of a with the reverse of v and giving the results clipped

argsort for a multidimensional ndarray

强颜欢笑 提交于 2019-11-26 18:20:06
问题 I'm trying to get the indices to sort a multidimensional array by the last axis, e.g. >>> a = np.array([[3,1,2],[8,9,2]]) And I'd like indices i such that, >>> a[i] array([[1, 2, 3], [2, 8, 9]]) Based on the documentation of numpy.argsort I thought it should do this, but I'm getting the error: >>> a[np.argsort(a)] IndexError: index 2 is out of bounds for axis 0 with size 2 Edit: I need to rearrange other arrays of the same shape (e.g. an array b such that a.shape == b.shape ) in the same way.

Sampling a random subset from an array

ε祈祈猫儿з 提交于 2019-11-26 17:43:34
What is a clean way of taking a random sample, without replacement from an array in javascript? So suppose there is an array x = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15] and I want to randomly sample 5 unique values; i.e. generate a random subset of length 5. To generate one random sample one could do something like: x[Math.floor(Math.random()*x.length)]; But if this is done multiple times, there is a risk of a grabbing the same entry multiple times. I suggest shuffling a copy of the array using the Fisher-Yates shuffle and taking a slice: function getRandomSubarray(arr, size) { var shuffled =

__builtin_prefetch, How much does it read?

限于喜欢 提交于 2019-11-26 14:44:31
I'm trying to optimize some C++ ( RK4 ) by using __builtin_prefetch I can't figure out how to prefetch a whole structure. I don't understand how much of the const void *addr is read. I want to have the next values of from and to loaded. for (int i = from; i < to; i++) { double kv = myLinks[i].kv; particle* from = con[i].Pfrom; particle* to = con[i].Pto; //Prefetch values at con[i++].Pfrom & con[i].Pto; double pos = to->px- from->px; double delta = from->r + to->r - pos; double k1 = axcel(kv, delta, from->mass) * dt; //axcel is an inlined function double k2 = axcel(kv, delta + 0.5 * k1, from-

Making C code plot a graph automatically

∥☆過路亽.° 提交于 2019-11-26 10:34:41
问题 I have written a program which writes a list of data to a \'.dat\' file with the intention of then plotting it separately using gnuplot. Is there a way of making my code plot it automatically? My output is of the form: x-coord analytic approximation x-coord analytic approximation x-coord analytic approximation x-coord analytic approximation x-coord analytic approximation .... Ideally, when I run the code the graph would also be printed with an x-label, y-label and title (which could be

Check if a varchar is a number (TSQL)

一个人想着一个人 提交于 2019-11-26 09:37:03
问题 is there an easy way to figure out if a varchar is a number? Examples: abc123 --> no number 123 --> yes, its a number Thanks :) 回答1: ISNUMERIC will do Check the NOTES section too in the article. 回答2: ISNUMERIC will not do - it tells you that the string can be converted to any of the numeric types, which is almost always a pointless piece of information to know. For example, all of the following are numeric, according to ISNUMERIC: £, $, 0d0 If you want to check for digits and only digits, a

Sampling a random subset from an array

风格不统一 提交于 2019-11-26 05:33:34
问题 What is a clean way of taking a random sample, without replacement from an array in javascript? So suppose there is an array x = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15] and I want to randomly sample 5 unique values; i.e. generate a random subset of length 5. To generate one random sample one could do something like: x[Math.floor(Math.random()*x.length)]; But if this is done multiple times, there is a risk of a grabbing the same entry multiple times. 回答1: I suggest shuffling a copy of the array