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.
Related
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).
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.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I'm actually working on a board game which is a variant of the TIC-TAC-TOE game. The specifics of the game are the followings :
1. The game is played on a nxn board, with n variable.
2. A player wins if he succeeds in placing k alignments the first, k is variable.
3. An alignment is constituted of l marks (X or O) in a horizontal, vertical or diagonal. l is fixed.
4. If the nxn grid is full (no player can add a mark either X or O) and no player succeeded in placing k alignments so the game is drawn.
I'm using an minmax with alpha-beta prunning algorithm. This is my first program with artificial intelligence and I don't know exactly how to create the evaluation function to be used by the algorithm. I saw some examples on the net which use a material weighting to evaluate a position but I can't apply that in my case. Actually, I'm using a radom evaluation function which returns a value between -100 and 100.
float Conf_eval(Configuration c)
{
return (rand()%201)-100;
}
Any idea on how can I evaluate a given board configuration ?
This is thoroughly discussed in the book Artificial Intelligence - A Modern Approach
There are also excellent implementations available (this is java, there is also python, you can Google for more) based on the book series. Including for tic-tac-toe (and alpha-beta pruning agents).
If you're using the min-max algorithm with alpha-beta prunning, you can use a sorted "Actions" list to perform better in addition to your heuristic function (a trivial utility-function would be assigning 1 to a victory, 0 to a tie and -1 to a loss - these are all leaf nodes of the min-max expanded tree).
To sort the actions you can, for say, prefer actions that add your symbol(X, O) to a clear-to-victory path. This should eventually lead to better prunning.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions must demonstrate a minimal understanding of the problem being solved. Tell us what you've tried to do, why it didn't work, and how it should work. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I recently came across an interview question and was wondering what could be the solution. Any ideas to solve it are greatly appreciated.
Given an array A[N] containing N numbers. Create an array Output[N] where Output[i] is equal to the product of all the elements of A[N] except A[i].
For example Output[0] is the product of A1 to A[N-1] and Output1 is the product of A[0] and from A[2] to A[N-1].
Do this without using the division operator. Do it in O(n).
Tip: do two iterations over the array - on the first one put in each cell the product of all preceding elements and on the second one multiply this by the product of all succeding elements.
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.