Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
We don’t allow questions seeking recommendations for books, tools, software libraries, and more. You can edit the question so it can be answered with facts and citations.
Closed 6 years ago.
Improve this question
I'm looking into absolute function minimization problem in Java. Can anyone suggest libraries and solutions?
I downloaded apache common math3, however, could not find related functions to create linear function to minimize absolute functions.
Simply, I am looking into writing below functions in Java.
f(x) = b + | x - a | + d + | x - c | + f + | x - e |
I've edited this problem a bit for my second problem, here b, d, f are also linear functions.
b = a1x + b1
d = a2x + b2
f = a3x + b3
If you are summing N terms of the form |x - a_i|, consider the gradient as x increases from negative infinity:
At negative infinity, and for most of the left-hand number line, the gradient is -N;
As you pass the minimum of the a_i values, the gradient increases slightly, to -N+2;
As you pass the next smallest of the a_i values, the gradient increases again, to -N+4.
Each further a_i passed, the gradient increases by 2;
At positive infinity, the gradient is +N.
So, the gradient starts negative, increases in steps at each of the a_i positions, and ends up positive; you're looking for the point or range where the gradient is zero. This will occur "in the middle", i.e. at the median of the values a_i.
If there are an odd number of points, the median will be at a point. This is the case in the question: the minimum is at median(a, c, e).
If there are an even number of points, the median is between two points. In this case, the function is minimised anywhere between those points.
Related
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 4 years ago.
Improve this question
I believe it is 3n+5n^2+1 but I am not 100 percent sure. If I am wrong can someone explain to me why?
3n + 5n^2 + 1 | applying Big-O notation for the single terms
= O(3n) + O(5n^2) + O(1) | leaving out constant multipliers
= O(n) + O(n^2) + O(1) | taking the one with the maximum power
= O(n^2)
=> quadratic time
So you are right, the first option is the quadratic one.
If it was 3n + 5n^(2 + 1), then it would be qubical due to 5^(2 + 1) = 5^3.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
We don’t allow questions seeking recommendations for books, tools, software libraries, and more. You can edit the question so it can be answered with facts and citations.
Closed 5 years ago.
Improve this question
Hello I an not a java programmer,
I need know what this function does:
rpcToken = Math.round(1E8 * Double.parseDouble("0." + (int) (Math.random() * Integer.MAX_VALUE)));
This just generate a random 8 number string? I need to make this exactly in PHP.
Thank you
Math.random() gives you a pseudorandom number less than 1 but at least 0.
This is multiplied by Integer.MAX_VALUE, which is 2147483647. The result is a number between 0 and 2147483646.
(int) converts this number to an integer.
"0." + converts this to a String, and adds "0." to the beginning of it.
Double.parseDouble converts this String back to a double. So now we again have a number between 0 and 1. But any number greater than 0 but less than 0.1 is impossible due to the way we created the String. "0.9" and "0.900" would both convert to 0.9.
1E8 * multiplies this by 1E8, which is 100000000.
And finally, Math.round rounds this off to the nearest integer.
So, in total, we have a supposedly random number somewhere between 0 and 99999999, but the final result is very skewed. Many numbers are impossible to generate with this code (numbers between 1 and 9999999 seem impossible, but 0 is still possible) and this code will generate certain numbers more often than other numbers (close to half the generated numbers will begin with 1, and more than average may end with 0), and I find it very unlikely that all of this is intentional (although it's impossible to say for sure without knowing the code's purpose.)
If you just need a random 8 digit number, there are easier, faster, and better ways. Why not just multiply that random number you got in the first step by 100000000, and round it off?
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 6 years ago.
Improve this question
I need to find the number of total possible different solution to an equation with more than one variable.
For example:
1x + 2y + 8z = 13
What is the total number of different combinations for the values of x, y and z?
I can't think of an algorithm to solve this. I don't need the answers printed, just the total number of different combinations. The coefficients and variables will always be positive, and the final number too.
1x + 2y + 8z = 13
Hence try (x, y,z) from (0, 0, 0) upto (13, 6, 1)
Hence at most 14*7*2 tries
Sort by the highest coefficient: z, y, x. The last variable can be inferred
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
We don’t allow questions seeking recommendations for books, tools, software libraries, and more. You can edit the question so it can be answered with facts and citations.
Closed 7 years ago.
Improve this question
I have some questions on collision detection. I am using LWJGL for my game in java. I have no experience in this subject and have been following the thin matrix OpenGL tutorials for the core of the game. I have experience in html, css, c++ and, of course, java.
I have been searching all over the web and have not found anything useful.
What is the simplest way to implement 3D collision detection in my games?
I would suggest using a 3d engine that recognizes collision for you like jMonkey. jMonkey can use LWJGL. There is an awesome tutorial here : http://wiki.jmonkeyengine.org/doku.php/jme3:beginner:hello_collision
If you want to roll your own like I do, I create a hash of the 3d points in space. If another object has the same hash it has near collision.
import static java.lang.System.out;
public class App {
public static int hash(float x, float y, float z) {
double hash = (((x * 31) + (y * 37) + (z * 41)));//31,37 and 41 are prime numbers
return (int) (hash);//multiplying the hash by 1000 for example may increase the accuracy significantly. Depends on the size of your space
}
public static void main(String[] args) {
for (float count = 0; count < 2; count += 0.01) {
out.println(hash(0, 0, count));
}
}
}
As you can see the hashes in the output show close collision as count increments by 0.01.
0
0
0
1
1
2
2
2
3
3
4
4
4
5
5
6
6
6
You can modify the prime numbers for higher accuracy.
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
How would I go about programming a gravity simulator? I am making a kind of 2d space-simulator and what I want is to have a planet (a center of gravity) to pull objects towards it. The object is a spaceship (basically just x and y-coordinates).
Use Newton's laws - the forces two objects feel are attractive (from one objects mass center point to the other's), equal to each other, and in value equal to g*m1*m2/(d*d) (where g is a constant, m1 and m2 are the masses and d is the distance of the center points.
However, if you have a planet, the effect of the spaceship's attraction to the planet is negligible, so one usually does not do the computations for the planet; it's just stationary.
Keep in mind that F=m*a, where F is the force calculated above, m the mass of the spaceship and a is the acceleration of the object. Based on the acceleration you calculate the speed, and based on the speed the position.
Check out Princeton's N-Body assignment. It describes what you want.
However, in the interest of quick summaries, you can derive the equations from basic trigonometry and Newton's Law of Universal Gravitation:
F = GMm/(r^2)
where F = force between two objects, G = gravitational constant, M and m are the relevant masses, and r is the distance between them.
A little mathemagic and you get the following results:
F_x = F(x_2 - x_1)/r
F_y = F(y_2 - y_1)/r
where F_x is the gravitational force in the x direction (same for F_y, but in y direction), x_2 and y_2 is the position of one of your objects, x_1 and y_1 the position of the other, F is as defined above, and r is the distance between them.