Skip to content

Complex (class)

vmathmachine edited this page May 22, 2022 · 3 revisions

The Complex class is the main component of the Complex Number Library.

It is a subclass of the class Mafs, which in itself is a static subclass of java.lang.Object.

Each instance of Complex has two attributes, re and im (representing the real and imaginary parts). Both are of the primitive type double (double floating point). The Complex class inherits fields and methods from Mafs to make it easier to program.

To read my official documentation on the fields and methods in Complex, please see the reference javadocs included with the Complex Number Library. You can access these by opening Processing, then at the top of the window, click Help -> Libraries Reference -> Complex Numbers (assuming you have the Complex Number Library downloaded). Then, from there, press the link to "Complex". You do not need an internet connection for this to work. :) Alternatively, if you want to access them manually, you can simply go into your Processing sketchbook folder, then open libraries -> ComplexNumberLibrary -> reference -> complexnumbers -> Complex.html.

Fields

The fields re and im are public double floating point members of each instance of Complex. re is of course the real part, im is of course the imaginary part. Please note that +0 is treated no differently than -0. Also, when one or more members is ∞, -∞, or NaN, the number is treated as a weird special case.

The fields cbrt_Option and omit_Option are public, static booleans. cbrt_Option determines if the cube root of a negative number is another negative number (true), or a complex number 60° from the positive real axis (false). omit_Option determines if extremely small components are omitted from toString conversion due to roundoff, or if they're kept to maintain precision. When omit_Option is true, the rule of thumb is that a component is omitted if this component is less than 1E-12 and the other isn't, or if it's less than 1E-11 times the other and they're both less than 1E-12.

Methods

Constructors / Psuedo-Constructors

The default constructor creates 0+0i. The other two either construct x+0i or x+yi. The method compCheck acts as a pseudo constructor, first constructing the number x+yi, then performing a check to prevent an undesirable permutation. See validate in the javadoc for more information.

Basic Functions

The methods clone and copy both create a deep copy. A variety of set functions exist, each mutators on the original instance. Some of them set the whole instance to something completely different, while others set just one thing (re, im, abs, or arg) without changing anything else. A variety of equals functions also exist, which, you guessed it, test for equality. And with the equals function also comes the hashCode function, which basically just does the same thing as PVector.hashCode but with extra steps. Read the documentation for more information. Please note if two numbers are NaN, they will equals each other. This is to maintain the convention that all non-null Objects in java must equal themselves. The validate function performs a check to prevent an undesirable permuation. See validate in the javadoc for more information.

Number Classification

All numbers fall into different categories. Real numbers are a subset of the complex numbers, rational and irrational numbers are a subset of the real numbers, integers are a subset of the rational numbers, whole numbers are a subset of the integers, and natural numbers are a subset of the whole numbers. The methods isReal, isImag, isInt, isWhole, isNatural serve to classify the input as such. We also have isInf to determine if a number is infinite (asking if either component is ±∞), and isNaN to determine if a number is undefined (asking if either component doesn't equal itself).

Cast to String

toString casts a complex number to a string the way a human would write it. No leading 0s, only in scientific notation if necessary, all that jazz. If you throw in an integer as a parameter, you can specify how many digits you want. toPolarString is the exact same way, but it writes the answer in polar notation rather than rectangular form.

Obscure Yet Really Useful Functions

lazyabs returns what I call the "lazy absolute value". This is not an official mathematical function, but it's still useful nonetheless. It returns whichever of the two components, re or im, has the largest absolute value. More specifically, it returns the absolute value of that component; the result is never negative. The purpose of this is to determine the approximate size of the input without using square roots (as with absolute value) or risking the overflow/underflow limit (as with the absolute square). It also differs from the absolute square in that it doesn't require multiplications. It is very useful for scaling a number up or down to a size at which certain operations are easier to perform.

isRoot returns true if and only if the instance z is equal to √(z²). That is, it returns true if re>0 || re==0 && im>=0. csgn returns 1 if isRoot is true, and -1 otherwise. mulcsgn and muleqcsgn multiply the instance by the csgn of the parameter. Surprisingly, this is actually pretty useful, and is slightly more efficient than actually multiplying by the csgn since it doesn't actually use multiplication. abs2 returns the instance times its csgn. In other words, it returns √(z²). rotate and rotateEq rotates a number around in the complex plane.

Basic Arithmetic

The basic arithmetic functions are all explained in this wiki page.

Negation And Other Simple Operations

neg and negeq perform negation (multiplication by -1). conj and conjeq perform complex conjugation (negating the imaginary part). mulI and muleqI multiply by i (x+yi -> -y+xi), while divI and diveqI divide by i (x+yi -> y-xi).

Complex Parts

re returns the real part, im returns the imaginary part, abs returns the absolute value, absq returns the absolute square (absolute value squared), arg returns the polar argument, and sgn returns the instance divided by its absolute value (or 0, if the instance is 0).

Reciprocal, Square Root, and Other Important Functions

inv calculates the reciprocal, details here. sq and cub find the square and cube. sqrt computes the (principal) square root, details here. cbrt computes the cube root, details here. exp computes e raised to the power of the instance, equal to exp(re)*(cos(im)+i*sin(im)). ln and log compute the (principal) natural logarithm, equal to ln|this|+i*arg(this). pow raises a complex number to a power. There are 3 variations: one for an integer exponent (which uses exponentiation by squaring), one for a double exponent, and one for a complex exponent.

Rounding and Modulos

It should be noted that rounding and modulos are not officially defined for non-real numbers (heck, they're not even officially defined for negative numbers). This library uses an implementation that, while seemingly makeshift, is actually somewhat practical, and is rather useful in certain fields, such as extending the Riemann Siegel algorithm for the Riemann Zeta function to numbers not on the critical strip.

floor, ceil, and round do just that to the real part. mod returns the modulo (see javadoc for more info).

mod_v2 is an alteration of the modulo, whereby instead of removing the largest integer multiple that fits in the instance, you instead subtract whichever integer multiple is closest to the instance (with certain technicalities about when two multiples are equally close). This is particularly useful in the application of finding the principal logarithm of something. Since all complex numbers have infinitely many natural logs, each a distance 2πi away from each other, performing this operation on an un-reduced logarithm, with the divisor set to 2πi, will convert it into a principal logarithm, who's imaginary part falls within the range (-π,π]. It should be noted that this functions noticeably different from the traditional modulo, when performed on two real numbers. For instance, 4 MOD 3 is still 1, but 5 MOD 3 is now -1, and 4.49 MOD 3 is still 1.49, but 4.51 MOD 3 is now -1.49. Since we subtract the closest integer multiple, that means instead of getting the smallest positive remainder, we now get whichever remainder is closest to 0.

Trigonometry

The trigonometric functions are all explained in this wiki page.

Inverse Trigonometry

The inverse trigonometric functions are all explained in this wiki page.

Clone this wiki locally