Home  Introduction  Complex numbers  Newton's method  Newton's method with complex numbers  Fractals  Optimisations  Performance

Search this site:
Threads Database Profiling Regular expressions Random numbers Compression Exceptions C Equivalents in Java
 Got a question about Java? Java discussion forum

The cost of Java object orientation in mathematical calculations (ctd)

This next section gives some background information on the problem that we take as our example for investigating the cost of object orientation in Java. If you're mathematically enclined, you may wish to skip this section.

Complex numbers

If we stick to real numbers (essentially, "normal" numbers such as 4, 716.22 or -245.8902), then there are certain mathematical problems that either cannot be solved at all, or are difficult to solve. In particular, equations such as x2 + 16 = 0 have no solution with real numbers: the solution of the latter would be the square root of -4, and there's no real number we can multiply by itself to get -16 (or indeed any negative number).

However, it turns out to be sometimes useful to "pretend" that it is possible to take the square root of a negative number. We define an "imaginary" constant, i, equal to the square root of -1. Then, we can express square roots of other negative numbers as multiples of i. For example, we can say that the square root of -16 is 4i, since 4i multiplied by itself would be 16i2, and if i is the square root of -1 it follows that i2 is equal to -1. (Strictly speaking, -4i would be the second root, since -4 * -4 is also equal to 16.) This then leads to two main ways in which we can use complex numbers:

  • we can perform calculations which involve multiples of i, but in which i always ends up being squared with itself (and hence converted into a real number) before we evaluate the final answer;
  • we can perform calculations whose result contains a multiple of i, and we can treat such numbers as "two-dimensional" numbers.

As an example of what we mean by "two-dimensional" numbers, we can plot the number 10 + 4.5i on a two-dimensional graph, with 10 (the "real" component) and 4.5 (the "imaginary" component) being the values along the x and y axes respectively. In certain calculations, the real and imaginary components have particular interpretations: for example, in a fourier transform used in various signal processing applications, the magnitude of the complex component (i.e. 4.5 in this example) represents the phase of a particular frequency component in a signal.

Calculations on complex numbers essentially fall out of standard algebra. For example, to add two complex numbers, we simply sum the real and complex components. So, (2 + 3.5i) + (1 + 1.5i) = (3 + 5i).

At first sight, division of complex numbers seems more tricky, but we can still form a rule with a little bit of basic algebra. Consider, for example, that we want to divide (a + bi) by (c + di). In any fraction, we can multiply both the numerator and denominator by any value (provided it is the same value in both cases), and the result of the fraction will be the same. So we choose to multiply both the numerator (a + bi) and the denominator (c + di) by the same value, (c - di). This means that we have a fraction whose denominator is now (c + di)(c - di), which evaluates to c2 - d2i2. Remembering that i2 is equal to minus one, the denominator is now a real number, c2 + d2 and evaluating the division is straightforward. We finally end up with code to divide two complex numbers which goes something as follows:

public ComplexNumber divide(ComplexNumber other) {
  double a = this.real;
  double b = this.imag;
  double c = other.real;
  double d = other.imag;
  double denom = c * c + d * d;
  return new ComplexNumber((a * c + b * d) / denom,
                           (b * c - a * d) / denom);
}

Next: introduction to Newton's method

On the next page, we look at Newton's method, which we can use with complex numbers.


Unless otherwise stated, the Java programming articles and tutorials on this site are written by Neil Coffey. Suggestions are always welcome if you wish to suggest topics for Java tutorials or programming articles, or if you simply have a programming question that you would like to see answered on this site. Most topics will be considered. But in particular, the site aims to provide tutorials and information on topics that aren't well covered elsewhere, or on Java performance information that is poorly described or understood. Suggestions may be made via the Javamex blog (see the site's front page for details).
Copyright © Javamex UK 2010. All rights reserved.