implement Horner's scheme in java for two variable polynomials - java

I have already understood the Horner's scheme for one variable polynomials like (2x^3+x+1) but I have not found a clear explenation for the two variable polynomials like (2x^6+3y+9) and i want to create a program in java to calcualte the scheme for me.

Use that A[x,Y] = A[x][Y]. In other words, consider your polynomials in x and Y with coefficients in some set A as polynomials in Y, whose coefficients are in turn polynomials, this time in A[x]. For example, rewrite
x^3+x^2Y+xY^2+xY+x^2+x+Y^3+Y^2+Y+1
as
Y^3 + (x+1)Y^2 + (x^2+x+1)Y + (x^3+x^2+x+1)
and then apply Horn first in A[x][Y] using it again for every one of the coefficients 1, x+1, xˆ2+x+1and x^3+x^2+x+1 in A[x].
Note that this will require sorting first the monomials according to their Y-degree and after grouping the coefficients, sorting their monomials according to the x-degree.

For two variables you could separate the two and apply the rule to each group:
x^3+x^2y+xy^2+xy+x^2+x+1+y^3+y^2+y+1=
=[1+x(1+y+y^2+x(1+y+x))+]+[1+y(1+y(1+y))]
So the algorithm would be:
Put x and xy terms in one group
treat y as a constant and apply
put y-only terms in another
apply

Related

Proving chicken nugget numbers with varying coin pairs without the use of loops

Apologies for the stupid question title. I've been given a non-graded challenge in Java to essentially determine if there exists two positive integers x and y such that ax+by=c, where a, b, and c are provided.
The context for this is that you're given two coins of value a and b, and you must determine whether they can be combined in some fashion to equal the amount c. The catch to this is your code must not include loops of any kind or recursive functions.
So far I've determined Euclid's Algorithm seems useful to this kind of problem; as finding the greatest common divisor of a and b can prove the existence of x and y as integers that completes the equation. The problem there is that x and y could still be negative, as is the case with the set [3,7,11]. With the greatest common divisor between 3 and 7 being 1, they can be combined to make 11 through 3(-1)+7(2).
Since you obviously can't use a negative amount of coins, 11 wouldn't be a "chicken nugget number" (in reference to the Chicken McNugget problem, which I feel is relevant). So what I'm looking for is either a completely different method of attempting this without loops of any kind, or advice on how to actually determine the values of x and y to see if either are negative.
I'm not necessarily looking for code for this, just any advice or guides would help. If you don't have a solution but are interested in thinking about this, recommended reading is Extended Euclidean Algorithm, Bézout's Identity, Diophantine Equations, and the Chicken McNugget Theorem.

Weka Euclidean Distance

I have two Weka instances which, when printed, look as follows:
0.44,0.34,0.48,0.5,0.3,0.33,0.43,cp
0.51,0.37,0.48,0.5,0.35,0.36,0.45,cp
I am trying to obtain their distance using the in-built Euclidean Distance function. My code:
EuclideanDistance e = new EuclideanDistance(neighbours);
double x = e.distance(neighbours.instance(0), neighbours.instance(1));
Where neighbours is an object of type Instances and the objects at indexes 0 and 1 are the two instances I referred to.
I am slightly confused because x is returned with value 1.5760032627255223 although, by doing the calculation separately, I was expecting 0.09798. cp is the class label, but earlier in my code I did specify data.setClassIndex(data.numAttributes() - 1);
Any advice?
By default, Weka's EuclideanDistance metric normalizes the ranges to compute the distance. If you don't want that, call e.setDontNormalize(true).

How to generate some identically distributed pseudo-random vectors with a fixed length r in a programming language?

For example, for the dimension d=2, it means that we could generate a random angle 0<=a<2*pi, and then we could just use
(x_1,x_2)=(r*cos(a),r*sin(a)) as the random vector.
However, for the dimension d>=3, we could not just generate an angle and use it to represent the vector. Then how could we generate such a vector (x_1,...,x_d), which is identically distributed on x_1^2+x_2^2+...+x_d^2=r^2?
I have just come up with a new idea, that we could generate a vector (x_1,...,x_d) such that -r<=x_i<r for all i, normalize it if x_1^2+x_2^2+...+x_d^2<=r^2 and abondon it if x_1^2+x_2^2+...+x_d^2>r^2.
However, there is a drawback that the probability that x_1^2+x_2^2+...+x_d^2<=r^2 would become very small if d is very large. Does there exist some better solutions?
Generate random variables (X_1, X_2, ... X_d) that are independent and have standard normal distributions, and then normalize by dividing by sqrt(X_1^2+...+X_d^2)/r.
That the joint distribution of independent normal distributions is rotationally symmetric is not just true, it characterizes normal distributions.
You can generate pairs of independent variables with a standard normal distribution efficiently from uniform random variables using the Box-Muller transform.
I see two ways around it.
The straightforward way is, in pseudo-code:
1. build n-dimensional vector x[0] through x[n-1] with random components
2. find radius
3. go to step 1 if radius > r; otherwise, normalize to radius r
This is non-deterministic, because there is no way to know how many times you will need to cycle before you find an acceptable sphere. Additionally, the probability of finding a bad point goes up with the number of dimensions.
To understand why (thanks commenters!), imagine a 1x1 square. Add a r=1 circle inside. Fill the square with random points. All the points between the center and the circle are evenly distributed when projected on the circle. All the points between the circle and the square's border are not - because there's too many at, say, 45º; and none at, say, 90º.
The non-straightforward version is a generalization of your 2-dimensional approach:
1. assume that we are on an n-sphere; generate angles phi[0], ...phi[n-2]
for a polar-coordinates point
2. convert to cartesian coordinates x[0] through x[n-1]
According to the n-sphere page in wikipedia, the formula is
x[0] = r*cos(phi[0]);
x[1] = r*sin(phi[0])*cos(phi[1]);
x[2] = r*sin(phi[0])*sin(phi[1]);
...
x[n-2] = r*sin(phi[0])*sin(phi[1])* /*...*/ sin(phi[n-3])*sin(phi[n-2])
x[n-1] = r*sin(phi[0])*cos(phi[1])* /*...*/ sin(phi[n-3])*cos(phi[n-2])
The actual algorithm can be implemented a lot more efficiently (sin(phi[0]) is getting calculated a lot, for example)
To avoid non-determinism, I recommend the second approach.
Edit
The recommended approach, not listed above, is in Douglas's answer and many reference sites:
https://mathoverflow.net/questions/136314/what-is-a-good-method-to-find-random-points-on-the-n-sphere-when-n-is-large
http://en.wikipedia.org/wiki/Box%E2%80%93Muller_transform
http://mathworld.wolfram.com/HyperspherePointPicking.html

Max absolute sum in a array

I am came across this question recently but didn't get any idea about solving this. Can you some one help with pseudo code.
Given an array with four integers A, B, C, D, shuffle them in some order. If the integers are unique then there are 24 shuffles. My task is get the best shuffle such that
F(S) = abs(s[0]-s[1]) + abs(s[1]-s[2])+ abs(s[2]-s[3])
is maximum
For example consider this example
A=5, B= 3, C=-1, D =5
s[0]=5, s[1]=-1, s[2]= 5, s[3] =3
will give me maximum sum which is
F[s] =14
The time and space complexity are O(1).
Since your array has a bounded size, any algorithm you use that terminates will have time and space complexity O(1). Therefore, the simple algorithm of "try all permutations and find the best one" will solve the problem in the appropriate time bounds. I don't mean to say that this is by any stretch of the imagination the ideal algorithm, but if all you need is something that works in time/space O(1), then you've got your answer.
Hope this helps!
Algorithm
Consider laying out your points in sorted order:
A B C D
Let x be the distance AB
Let y be the distance BC
Let z be the distance CD
An order which will always give the best score is BDAC with score 2x+3y+2z.
Example
In your example, the sorted points are:
A=-1 B= 3 C=5 D=5
x=4, y=2, z=0
So the best order will be BDAC=3->5->-1->5 with score 14.
Hints towards Proof
You can prove this result be simply considering all permutations of the path between the 4 points, and computing the score in terms of x,y,z.
e.g.
ABCD -> x+y+z
ACBD -> x+3y+z
ADBC -> x+3y+2z
etc.
In any permutation, the score will use x at most twice (because A is on the end so the route can only go to or from A twice). Similarly, z is used at most twice because D is on the end. y can be used at most three times because there are three things being added.
The permutation BDAC uses x twice, z twice, and y three times so can never be beaten.
If array is sorted this solution also works:
F(S)= 2*abs(s[0]-s[3]) + abs(s[1]-s[2])
where s[0]=A, s[1]=B, s[2]=C and s[3]=D.

How to implement equivalence class in Java?

What would be the simple way to implement equivalence class in Java? Is there any library for that purpose?
The bothering part is how to write an efficient and non-naive "equal" operator.
Let S = {x,y,z,w,h}. If we use a mapping x->1, y->1, z->1, w->2, h->2 for the equivalence class of S, one has to consider the mapping x->10, y->10, z->10, w->20, h->20 as the same equivalence class.
Naive "equal" operator can quickly become time-consuming when the cardinal of the set S becomes large.
What would be the simple way? Any idea?
[EDITED] To clarify, the specific problem can be formalized as follows:
Let S be a non-empty set. We denote by M a set of partial mappings from V to integers. It is relatively easy to show that the binary relation \sim defined below derives an equivalence relation on M.
For m1 and m2 two partial mappings of M, m1 \sim m2 if and only if,
for any a of V, m1(a) is defined if and only if m2(a) is defined
for any a,b of V, m1(a) and m1(b) are both defined to be the same
integer value 'z1' if and only if m2(a) and m2(b) are both defined
to the same integer value 'z2' (which may or may not differ from
'z1')
Example.
a->9,b->9,w->1 \sim a->10,b->10,w->0
But it is not correct to say
a->5 \sim b->9
Thanks.
From what I understand from your question you can find the greatest common divisor (Euclid's algorithm recursively) for a set once and map the quotients with that instead - if they're exactly equal with another set it's equal, else not. This will only work if the sets are equal in size and mappings.
If I understand you correct, you could apply vector normalization. A 3d vector for example is normalized to a length of 1 by dividing all of its components separately with the vectors length. If two normalized vector's components are equal, their original (non-normalized) vectors point in the same direction (which is what I think you define as 'equal')
x,y,z,w,h would in your case be a 5-dimensional vector. They belong to the same class when the show into the same direction, but may have an arbitrary length.
Aside: I assume that the set S is actually the set V in your definition.
I think Uli is on the right track, although I would not assume that Set(Set(E)).equals() is efficient for your purposes. (Sorry, I couldn't get the lt or gt symbols to come through)
The default implementation of Set(E).equals() is likely O(nlog n) or O(n^2). Set(E).equals() almost certainly involves sorting; O(nlog n) is as good as it gets. I suggest you look at radix sort. It's O(n*log n), but grows very slowly.

Categories