isolate real number in complex number in java
There is a formula i need to use for my ap开发者_StackOverflowp : here
the part Sqrt(5-25) , can be positive or negative , and of course when negative we got a imaginary part that java cant handle.
i've searched around to find a complex class to handle that , but found only basic operation (+-*/).
how can i solve this in java knowing i only need to get the real part ?(imaginary have no importance)
i precise that i develop on android platform
(i post on stack because it's concerning application in java , but if its belong to math.se , tell me)
You can simply compute verything before:
plot 25-20+((2Pi0.3²)/(Pi10²)Sqrt[2*980(1+(Pi10²)/(Pi10²))]t)² from 0 to 38
or
plot 25-20+((2*0.3²)/(10²)Sqrt[2*980(1+1)]t)² from 0 to 38
or
25 - 20 + 4 * 0.0000081 * 3920*t^2 from 0 to 38 (I have some factor wrong, but you get the idea)
just apply basic math to the constants and remove the middle (imaginary part) after applying the 2nd binomic formula.
There is nothing to do with complex numbers.
Taking the Square root of a general complex number can be done with the basic arithmetic operations on real numbers (plus taking square root of reals): http://www.mathpropress.com/stan/bibliography/complexSquareRoot.pdf (one technique is to utilise De Moivre's theorem: Any complex number a + bi can be written as r(cos θ + i sin θ)
where
r = sqrt(a^2 + b^2), cos θ = a/r, sin θ = b/r
Update: the formula r(cos θ + i sin θ)
is originaly due to Euler, whereas De Moivre's theorem is
(a + ib)ⁿ = rⁿ(cos nθ + i sin nθ)
You are confused about the math. Square root of -25 is square root of 25*(-1) and that is square root of 25 * square root of -1 and that is 5i. Real part of that number is 0.
If you want the 5, just check for the sign of the number to be "rooted" and change it if it is negative.
It's not right to say that "Java can't handle it". No language that returns a double from a square root can handle it, but if you have a Complex
class it's not an issue. Python has one built in; it's easy to write one in Java.
The square root of an integer is going to be an integer or a complex number whose real part is zero. The real part of the square root of a negative integer is zero. Always.
So ...
public double realPartOfSquareRoot(int i) {
return (i > 0) Math.sqrt(i) : 0;
}
But how I solve this? If I replace the squareroot by 0, I don't get a good result. Do I suppose the imaginary part do something on the real with the rest of the formula.
I expect that's so! (The idea of discarding the imaginary part didn't make much sense to me ... but I assumed you had a sound reason to do this.)
The real answer is to find a Java library that will do complex arithmetic. I've never needed to use it, but the first one to examine should be the Apache Commons Maths library.
精彩评论