-
Notifications
You must be signed in to change notification settings - Fork 0
Cpx (class)
Cpx is a static subclass of the Mafs class.
Cpx allows for complex functions to be implemented external to the Complex class. People familiar with C++ might find this is similar to the idea of friend functions.
Cpx stands for "complex". It's intentionally short so that calls to it are quick and take up little screen real estate.
The purpose of this class is so that, if someone wants to create a math heavy function, they can create a class which extends Cpx and declare the function inside that class. That way, they can just casually say sin(inp) or exp(number) within that chunk of code. However, if for any reason, the programmer is unable to declare it in such a way (for instance, maybe they have to declare the function within another class which strictly cannot extend Cpx), the short name still gives minimal hassle in implementation.
The unlimited input add and mul functions are implemented exclusively in this class, as well as all arithmetic functions whereby all inputs are parameters (that is, no instances come before the function). Such functions might prove to be helpful, such as if the programmer wants to subtract double - Complex or divide double / Complex. The Cpx class also defines a few external constructors, such as complex (without new) or iTimes (which constructs a purely imaginary number). It also has a function which casts a String to a Complex, and a few functions to define instances of Complex equal to specific constants, such as zero(), one(), two(), and mOne() (minus one).
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 "Cpx". 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 -> Cpx.html.
Cpx has no fields.
zero, one, mOne, and two construct instances of 0+0i, 1+0i, -1+0i, and 2+0i respectively. complex(double) constructs a real number, iTimes(double) constructs an imaginary number. complex(String) casts a string to a complex number. It'll still work even if the number is formatted weird, like "2.000E1". If the cast fails, it'll construct NaN+NaNi. polar constructs a complex number in polar notation. str casts a complex number back to a string.
re, im, abs, absq, arg, and conj return the real part, imaginary part, absolute value, absolute square, polar argument, and complex conjugate, respectively.
The Cpx versions of the arithmetic functions work the same way they do in Complex. For more information on arithmetic operations, check out this wiki page.
The neg function is also implemented in this section. It returns a negated copy.
inv takes 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). log10 computes the base 10 logarithm. 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.
The trigonometric functions are all explained in this wiki page. Unlike in Complex, however, Cpx also has the trigonometric function gd, which returns the Gudermannian of the input.
The inverse trigonometric functions are all explained in this wiki page. Unlike in Complex, however, Cpx also has the function invGd, which returns the inverse Gudermannian of the input.
The function logOffset looks at a set of complex numbers, looks at the sum of their logarithms, looks at the logarithm of their product, finds the difference, and divides it by 2πi. The output is an int, because the result is always an integer. Because that's just how logarithms work. It does all this without performing any logarithms.
The function logSum looks at a set of complex numbers, then returns the sum of their logarithms. It does this only taking one logarithm. (Technically, due to my laziness, it'll actually perform 1 extra logarithm when the product of the numbers overflows or underflows).
BUG NOTIFICATION FOR VERSION 1.0: when you perform logSum on one complex input, it'll always return 0, when it's supposed to return the logarithm of that input. This'll be fixed either in the next patch or in 1.1.0. It's a very easy fix, but this is still a problem nonetheless.