Calculate elements in a array that are linear [closed] - arrays

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I have a large array of a tuple (data, time)
Part of the array contains data that has a linear slope.
Other data in the array is non-linear.
What algorithm can I use to determine
Where does the linear slope start?
Where does the liner slope end?

Seems straightforward:
Sort tuples by time
Let (d1, t1), (d2, t2), (d3, t3), (d4, t4), etc be consecutive tuples
Calculate the gradient (slope) between each tuple: (d2 - d1) / (t2 - t1), (d3 - d2) / (t3 - t2), etc.
If the gradient is the same between multiple consecutive tuples (within a margin of error depending on your data), then those consecutive tuples must be on a linear line.

Look up RANSAC algorithm for linear regression robust to outliers. Using a randomized approach, RANSAC will essentially find you an (approximately) largest set of points that are within a prescribed distance to a common line.

Related

Partial sine data fit code C [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
I have 8 data points that form the peak of a partial sine wave. I am trying to fit these to get an equation so I discover the point of the true maximum position (which most likely lies between the data points). The coding will be in C. Does anyone have any info on algorithms or ideally code samples?
Since the data points are all near a maximum, the wave y = A*sin(B*x + C) + D can be approximated as a parabola much like the first 2 terms of cos(x) = (1.0 - x*x/2! + ...).
So find the best fit parabola for the 8 data points and calculate the maximum.
C- Peak detection via quadratic fit
Lots of google examples exist. Example
Provided your sample-values form a "hump", i.e. increasing followed by decreasing samples, you could try viewing the samplevalues as "weights" and compute the "center of gravity":
float cog = 0f;
for (i=0; i<num_samples; ii+) {
cog += i * samples[i];
}
cog /= num_samples;
I've used that in similar cases in the past.
NOTE: This scheme only works if the set of samples used contain a single peak, which the question phrasing certainly made me think was the case. Finding locations of interest can easily be done by monitoring, if sample values are increasing or decreasing, selecting an "interesting" range of samples and computing the peak location as described.
Also note, that if the actual goal is to determine the sine wave phase or frequency of an input signal, it would be a lot better to correlate the signal against reference set of sine-waves (in other words, do a Fourier transform).

How to find coefficient of x^m in the polynomial? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about programming within the scope defined in the help center.
Closed 8 years ago.
Improve this question
How to find coefficient of
x^m (m<=n)
in the polynomial of type
(a1+b1x)(a2+b2x)...(an+bnx)? O(n^2)
Algorithm needed!
(a1+b1x)(a2+b2x)...(an+bnx)=b1*b2*...*bn*(a1/b1+x)*(a2/b2+x)...(a/bn+x)
Right part is polynomial with roots (-a1/b1,-a2/b2...-an/bn)
There is O(N^2) algorithm to find coefficients of this polynomial, implemented here
(Don't forget to multiply coefficients by product of b[i])
Speaking personally I would use an inductive application of the Binomial Theorem.
http://en.wikipedia.org/wiki/Binomial_theorem
This will solve your base case of two binomials. Then the rest is just repeating the application using the associativity of multiplication.
I don't know enough C to write this though, I'm sorry.
Coefficient of m-th element is sum of all possible products of (a or b)[i] for all i between 0 and m such that there are exactly m selections of b (and n - m selections of a).
More procedurally, generate all combinations of integers between 0 and m, multiply elements of a at those indices, get a complement of each combination and multiply the obtained product further by the elements of b at those indices. Add all obtained products together.

Optimize SUMIFS formula [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I basically have a super long formula that I need to shrink. So basically I have a pivot table that allows a manager to select two work locations or both and one to twelve months.
This is how the formula looks. Only thing that changes are cell I and J->K. Would I need to set this up in a array?
=SUMIFS('owssvr(1)'!C:C,'owssvr(1)'!$IR:$IR,$I$3,'owssvr(1)'!$IQ:$IQ,'Summary KPI'!$J$2)+SUMIFS('owssvr(1)'!C:C,'owssvr(1)'!$IR:$IR,$I$4,'owssvr(1)'!$IQ:$IQ,'Summary KPI'!$J$2)+SUMIFS('owssvr(1)'!C:C,'owssvr(1)'!$IR:$IR,$I$5,'owssvr(1)'!$IQ:$IQ,'Summary KPI'!$J$2)+SUMIFS('owssvr(1)'!C:C,'owssvr(1)'!$IR:$IR,$I$6,'owssvr(1)'!$IQ:$IQ,'Summary KPI'!$J$2)+SUMIFS('owssvr(1)'!C:C,'owssvr(1)'!$IR:$IR,$I$7,'owssvr(1)'!$IQ:$IQ,'Summary KPI'!$J$2)+SUMIFS('owssvr(1)'!C:C,'owssvr(1)'!$IR:$IR,$I$8,'owssvr(1)'!$IQ:$IQ,'Summary KPI'!$J$2)+SUMIFS('owssvr(1)'!C:C,'owssvr(1)'!$IR:$IR,$I$9,'owssvr(1)'!$IQ:$IQ,'Summary KPI'!$J$2)+SUMIFS('owssvr(1)'!C:C,'owssvr(1)'!$IR:$IR,$I$10,'owssvr(1)'!$IQ:$IQ,'Summary KPI'!$J$2)+SUMIFS('owssvr(1)'!C:C,'owssvr(1)'!$IR:$IR,$I$11,'owssvr(1)'!$IQ:$IQ,'Summary KPI'!$J$2)+SUMIFS('owssvr(1)'!C:C,'owssvr(1)'!$IR:$IR,$I$12,'owssvr(1)'!$IQ:$IQ,'Summary KPI'!$J$2)+SUMIFS('owssvr(1)'!C:C,'owssvr(1)'!$IR:$IR,$I$13,'owssvr(1)'!$IQ:$IQ,'Summary KPI'!$J$2)+SUMIFS('owssvr(1)'!C:C,'owssvr(1)'!$IR:$IR,$I$14,'owssvr(1)'!$IQ:$IQ,'Summary KPI'!$J$2)+SUMIFS('owssvr(1)'!C:C,'owssvr(1)'!$IR:$IR,$I$3,'owssvr(1)'!$IQ:$IQ,'Summary KPI'!$K$2)+SUMIFS('owssvr(1)'!C:C,'owssvr(1)'!$IR:$IR,$I$4,'owssvr(1)'!$IQ:$IQ,'Summary KPI'!$K$2)+SUMIFS('owssvr(1)'!C:C,'owssvr(1)'!$IR:$IR,$I$5,'owssvr(1)'!$IQ:$IQ,'Summary KPI'!$K$2)+SUMIFS('owssvr(1)'!C:C,'owssvr(1)'!$IR:$IR,$I$6,'owssvr(1)'!$IQ:$IQ,'Summary KPI'!$K$2)+SUMIFS('owssvr(1)'!C:C,'owssvr(1)'!$IR:$IR,$I$7,'owssvr(1)'!$IQ:$IQ,'Summary KPI'!$K$2)+SUMIFS('owssvr(1)'!C:C,'owssvr(1)'!$IR:$IR,$I$8,'owssvr(1)'!$IQ:$IQ,'Summary KPI'!$K$2)+SUMIFS('owssvr(1)'!C:C,'owssvr(1)'!$IR:$IR,$I$9,'owssvr(1)'!$IQ:$IQ,'Summary KPI'!$K$2)+SUMIFS('owssvr(1)'!C:C,'owssvr(1)'!$IR:$IR,$I$10,'owssvr(1)'!$IQ:$IQ,'Summary KPI'!$K$2)+SUMIFS('owssvr(1)'!C:C,'owssvr(1)'!$IR:$IR,$I$11,'owssvr(1)'!$IQ:$IQ,'Summary KPI'!$K$2)+SUMIFS('owssvr(1)'!C:C,'owssvr(1)'!$IR:$IR,$I$12,'owssvr(1)'!$IQ:$IQ,'Summary KPI'!$K$2)+SUMIFS('owssvr(1)'!C:C,'owssvr(1)'!$IR:$IR,$I$13,'owssvr(1)'!$IQ:$IQ,'Summary KPI'!$K$2)+SUMIFS('owssvr(1)'!C:C,'owssvr(1)'!$IR:$IR,$I$14,'owssvr(1)'!$IQ:$IQ,'Summary KPI'!$K$2)
This formula should give the same result
=SUMPRODUCT(SUMIFS('owssvr(1)'!C:C,'owssvr(1)'!$IR:$IR,$I$3:$I$14,'owssvr(1)'!$IQ:$IQ,'Summary KPI'!$J$2:$K$2))
Assuming you want to sum for all combinations then you can have at most one vertical and one horizontal range as criteria in SUMIFS (so $I$3:$I$14 and 'Summary KPI'!$J$2:$K$2 qualify as those).
SUMIFS then returns an array of 12x2 values (the same dimensions as your criteria) and SUMPRODUCT sums all those to get the correct result (used instead of SUM so that "array entry" is not required)

Number of Integers less than x in a subarray [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
Given an array A[1..n] and Q queries on that array.
Each query is of the format (x a b), and for every query I need to find out, how many elements in the sub-array (a,b) of main array denoted by A[a..b] are less than x.
How to perform this job efficiently ?
OR If required what kind of data structures to build on top of array, to perform the job efficiently ?
Limits :
n <= 10^6
Q <= 10^4
HINTS
Use a Fenwick tree to store a cumulative histogram of which values you have seen.
Turn your queries (x a b) into a F(x,a)-F(x,b-1) where F(x,a) counts numbers less than x in the range 0..a.
Sort these F(x,a) queries by a.
Then iterate over the array index i, inserting element A[i] into the Fenwick tree, and answering any F(x,a) queries that have a==i.

Generating non-uniform random numbers [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 3 years ago.
Improve this question
Can you tell me any ways to generate non-uniform random numbers?
I am using Java but the code examples can be in whatever you want.
One way is to create a skewed distribution by adding two uniform random numbers together (i.e. rolling 2 dice).
Try generating uniformly distributed random numbers, then applying your inverted non-uniform cumulative distribution function to each of them.
What distribution of deviates do you want?
Here is a technique which always works, but isn't always the most efficient. The cumulative distrubtion function P(x) gives the fraction of the time that values fall below x. Thus P(x)=0 at the lowest possible value of x and P(x)=1 at the highest possible value of x. Every distribution has a unique CDF, which encodes all the properties of the distrubtion in the way that P(x) rises from 0 to 1. If y is a uniform deviate on the interval [0,1], then x satisfying P(x)=y will be disributed according to your distribution. To make this work comuptationally, you just need a way computing the inverse of P(x) for your distribution.
The Meta.Numerics library defines a large number of commonly used distrubtions (e.g. normal, lognormal, exponential, chi squared, etc.) and has functions for computing the CDF (Distribution.LeftProbability) and the inverse CDF (Distribution.InverseLeftProbability) of each.
For specialized techniques that are fast for particular distrubtions, e.g. the Box-Muller technique for normaly distributed deviates, see the book Numerical Recipies.
If you are using Java then my Uncommons Maths library may be of interest. It includes classes for generating random numbers for Uniform, Gaussian, Poisson, Binomial and Exponential distributions. This article shows how you might use these distributions.

Resources