FFT division Complex, Java - java

I see this in matlab file. freqz.m file
h = dividenowarn(fft([b zeros(1,s*nfft-nb)]),...
fft([a zeros(1,s*nfft-na)])).';
example:
x = fft([1.5,0,1,0,0,0,1,3]')
x =
6.5000
3.6213 + 2.1213i
-0.5000 + 3.0000i
-0.6213 + 2.1213i
0.5000
-0.6213 - 2.1213i
-0.5000 - 3.0000i
3.6213 - 2.1213i
now
y = fft([1,1,2,3,1,0,9,3]')
y =
20.0000
0.7071 + 6.2929i
-9.0000 + 5.0000i
-0.7071 - 7.7071i
6.0000
-0.7071 + 7.7071i
-9.0000 - 5.0000i
0.7071 - 6.2929i
not really matter the fft's I need how to perfom this operation..
z = (x./y)
z =
0.3250
0.3968 - 0.5309i
0.1840 - 0.2311i
-0.2656 - 0.1050i
0.0833
-0.2656 + 0.1050i
0.1840 + 0.2311i
0.3968 + 0.5309i
I need an algorythm (no matlab code), I need something Java, or step by step calculus...
» a
a =
1.0000 + 2.0000i 3.0000 + 4.0000i 5.0000 + 6.0000i 0
» b
b =
5.0000 + 2.0000i 1.0000 - 2.0000i 0 0
» c = a./b
Warning: Divide by zero.
c =
0.3103 + 0.2759i -1.0000 + 2.0000i Inf + Infi NaN - NaNi
»

The ./ operator performs element-wise division. You can tell it's an element-wise operator from the . before the division sign. This means that the result will be a vector with elements that are obtained using the rule x[i] / y[i].
If you want to do this in Java you will either need to implement your own Complex number class writing the division code yourself, or you can use the Apache commons math Complex class.
Assuming you use apache commons, the element-wise division in Java would look like this:
List<Complex> elementWiseDivision(List<Complex> dividend, List<Complex> divisor)
{
if (dividend.size() != divisor.size())
{
throw new IllegalArgumentException("Must have same size");
}
List<Complex> result = new ArrayList<>();
// using iterators to get O(n) with both LinkedList and ArrayList inputs
for (Iterator<Complex> xit = dividend.iterator(), yit = divisor.iterator(); xit.hasNext();)
{
result.add(xit.next().divide(yit.next()));
}
return result;
}

Related

Converting user input from binary to decimal using Modulus Operator in Java

I am having a hard time figuring out the answer to a homework assignment for Programming 1 class. The assignment is prompt a user for input (up to 4 bits) in binary, and convert it to the decimal equivalent. Using loops, conditional statements, ParseInt, and anything other than the modulus operator and other math operators are not allowed.
I am having trouble with the mathematical aspect, I think once I understand how to use the modulus operator to answer the question I would be able to write the code for it.
I have searched and have not been able to find anything that was able to help.
You should be getting the number values of each position and add them using the power of 2 to get back the original number.
double num = 1110;
double ones = Math.floor(num % 10);
double tens = Math.floor(num/10 % 10);
double hundreds = Math.floor(num/100 % 10);
double thousands = Math.floor(num %10000 /1000);
double tenThousands = Math.floor(num / 10000 % 10);
double original = (ones * 1) +
(tens * 2) +
(hundreds * 4) +
(thousands * 8);
System.out.println(num);
System.out.println("ones: " +ones);
System.out.println("tens: " +tens);
System.out.println("hundreds: " +hundreds);
System.out.println("thousands: " + thousands);
System.out.println("original number : " + original);

Analyzing runtime of a short algorithm

I got the following code given:
public class alg
{
public static int hmm (int x)
{
if (x == 1)
{
return 2;
}
return 2*x + hmm(x-1);
}
public static void main (String[] args)
{
int x = Integer.parseInt(args[0]);
System.out.println(hmm(x));
}
}
So first question is, what does this algorithm count?
I have just typed and runned it in eclipse
so I can see better what it does (it was pseudocode before, I couldn't type it here so I typed the code). I have realized that this algorithm does following: It will take the input and multiply it by its following number.
So as examples:
input = 3, output = 12 because 3*4 = 12.
Or Input = 6, output 42 because 6*7 = 42.
Alright, the next question is my problem. I'm asked to analyze the runtime of this algorithm but I have no idea where to start.
I would say, at the beginning, when we define x, we have already got time = 1
The if loop gives time = 1 too I believe.
Last part, return 2x + alg(x-1) should give "something^x" or..?
So in the end we got something like "something^x" + 2, I doubt thats right : /
edit, managed to type pseudocode too :)
Input: Integer x with x > 1
if x = 1 then
return 2;
end if
return 2x + hmm(x-1);
When you have trouble, try to walk through the code with a (small) number.
What does this calculate?
Let's take hmm(3) as an example:
3 != 1, so we calculate 2 * 3 + hmm(3-1). Down a recursion level.
2 != 1, so we calculate 2 * 2 + hmm(2-1). Down a recursion level.
1 == 1, so we return 2. No more recursions, thus hmm(2-1) == hmm(1) == 2.
Back up one recursion level, we get 2 * 2 + hmm(1) = 2 * 2 + 2 = 4 + 2 = 6. Thus hmm(2) = 6
Another level back up, we get 2 * 3 + hmm(2) = 6 + 6 = 12
If you look closely, the algorithm calculates:
2*x + ... + 4 + 2
We can reverse this and factor out 2 and get
2 * (1 + 2 + ... + x).
Which is an arithmetic progression, for which we have a well-known formula (namely x² + x)
How long does it take?
The asymptotic running time is O(n).
There are no loops, so we only have to count the number of recursions. One might be tempted to count the individual steps of calculation, but those a are constant with every step, so we usually combine them into a constant factor k.
What does O(n) mean?
Well ... we make x - 1 recursion steps, decreasing x by 1 in every step until we reach x == 1. From x = n to x = 1 there are n - 1 such steps. We thus need k * (n - 1) operations.
If you think n to be very large, - 1 becomes negligible, so we drop it. We also drop the constant factor, because for large n, O(nk) and O(n) aren't that much different, either.
The function calculates
f(x) = 2(x + x-1 + x-2 + ... + 1)
it will run in O(x), i.e. x times will be called for constant time O(1).

Why does not java cast int to double in expression automatically?

My problem:
int a = 30, b = 12, c = 2, d = 5, e = 1;
double result = (double) (a - b / (c * d) + e);
System.out.print("Result = " + (double) result + " - " + result);
Result:
Result = 30.0 - 30.0
What I want to see is: 29.8 !
I have integers but I want to evaluate an expression which I need to have in double precision.
Is there a simple way to do what I tried?
You have to cast one of the integers in your expression to double. Actually, you can't cast any of the integers. The important one to cast is one of the operands of the division, since integer division is the cause of the precision loss you experience).
For example :
double result = (a - (double)b / (c * d) + e);
or
double result = (a - b / (double)(c * d) + e);
This will ensure the division is done using floating point arithmetic.
why does not java cast int to double in expression automatically?
Because the expression where you're not getting a fractional value is evaluated based purely on the operands you give it, not the greater context (which would be horribly complicated). So a - b, c * d, and ... + e all work with int values. Only once you have a final result do you cast it to double, and the compiler doesn't look at the greater context and guess that you wanted to do that earlier (which is a Good Thing(tm)).
If you want the operations to happen with double values, you have to tell the compiler that. In this case, you can put that cast in any of several places, which will then "bubble up" to any expressions the result is used in. Here's one place you can put it:
double result = (a - b / (double)(c * d) + e);
Live example
The result of the operation between int is int. So the 12/10 is the int one 1, if you want to get the double one 1.2, you must cast one of the expression to double, like double result = (a - (double)b / (c * d) + e);.

Spline Interpolation Performance in Scala vs Java

I translated this spline interpolation algorithm from apache.commons.math from Java into Scala in the most straightforward way I could think of (see below). The function I ended up with runs 2 to 3 times slower than the original Java code. My guess is that the problem stems from the extra loops coming from the calls to Array.fill, but I can't think of a straightforward way to get rid of them. Any suggestions on how to make this code perform better? (It would also be nice to write it in a more concise and/or functional way -- suggestions on that front would be appreciated as well.)
type Real = Double
def mySplineInterpolate(x: Array[Real], y: Array[Real]) = {
if (x.length != y.length)
throw new DimensionMismatchException(x.length, y.length)
if (x.length < 3)
throw new NumberIsTooSmallException(x.length, 3, true)
// Number of intervals. The number of data points is n + 1.
val n = x.length - 1
// Differences between knot points
val h = Array.tabulate(n)(i => x(i+1) - x(i))
var mu: Array[Real] = Array.fill(n)(0)
var z: Array[Real] = Array.fill(n+1)(0)
var i = 1
while (i < n) {
val g = 2.0 * (x(i+1) - x(i-1)) - h(i-1) * mu(i-1)
mu(i) = h(i) / g
z(i) = (3.0 * (y(i+1) * h(i-1) - y(i) * (x(i+1) - x(i-1))+ y(i-1) * h(i)) /
(h(i-1) * h(i)) - h(i-1) * z(i-1)) / g
i += 1
}
// cubic spline coefficients -- b is linear, c quadratic, d is cubic (original y's are constants)
var b: Array[Real] = Array.fill(n)(0)
var c: Array[Real] = Array.fill(n+1)(0)
var d: Array[Real] = Array.fill(n)(0)
var j = n-1
while (j >= 0) {
c(j) = z(j) - mu(j) * c(j + 1)
b(j) = (y(j+1) - y(j)) / h(j) - h(j) * (c(j+1) + 2.0 * c(j)) / 3.0
d(j) = (c(j+1) - c(j)) / (3.0 * h(j))
j -= 1
}
Array.tabulate(n)(i => Polynomial(Array(y(i), b(i), c(i), d(i))))
}
You can get rid of all the Array.fill since a new array is always initialized with 0 or null, depending on whether it is a value or a reference (booleans are initialized with false, and characters with \0).
You might be able to simplify the loops by zipping arrays, but you'll only make it slower. The only way functional programming (on the JVM anyway) is going to help you make this faster is if you make it non-strict, such as with a Stream or a view, and then you go ahead and not use all of it.

Arithmetic operator confusion

Why I'm getting two different values while using the arithmetic operators for the same value of variables. I've just altered little bit my second program, which is resulted in giving me the different output. Could anyone please tell me why?
int number=113;
int rot=0;
rot=number%10;
rot*=100+number/10;
System.out.println(rot);//333
int number=113;
int rot=0;
rot=number%10;
rot=rot*100+number/10;
System.out.println(rot);//311
In the first part you compute
rot *= 100 + number/10
which is
rot = rot * (100 + number/10)
And in the second part:
rot = rot*100 + number/10
Note that multiplication and division goes before addition and substraction.
the problem is that *= has different (lower) precedence than * and +
rot *= 100 + number/10;
is equavalent to
rot = rot * (100 + number /10);
operator precdence can be found here
It seems like the problem is operator precedence.
What this means is that num * 10 + 13 is treated like (num * 10) + 13, i.e. the () are automatically added according to the rules of the language.
The difference then, in your example, is that the first one means the following:
rot*=100+number/10;
// Is the same as this:
rot = rot * (100 + (number / 10));
Whereas the second one means the following:
rot=rot*100+number/10;
// Is the same as this:
rot = (rot * 100) + (number / 10);
Since the parenthesis are in different places, these probably evaluate to different numbers.
in the second code because of the high precedence of *. rot*100 will be calculated and to that (number/10) will be added so its (300 + 11 = 311).

Categories

Resources