Arithmetic/Complex

Arithmetic/Complex
You are encouraged to solve this task according to the task description, using any language you may know.

A complex number is a number which can be written as "" (sometimes shown as "") where a and b are real numbers and i is the square root of -1.
Typically, complex numbers are represented as a pair of real numbers called the "imaginary part" and "real part", where the imaginary part is the number to be multiplied by i.

Show addition, multiplication, negation, and inversion of complex numbers in separate functions. (Subtraction and division operations can be made with pairs of these operations.) Print the results for each operation tested.

Optional: Show complex conjugation. By definition, the complex conjugate of a + bi is a − bi.

Some languages have complex number libraries available. If your language does, show the operations. If your language does not, also show the definition of this type.

Bracmat recognizes the symbol i as the square root of -1. The results of the functions below are not necessarily of the form a+b*i, but as the last example shows, Bracmat nevertheless can work out that two different representations of the same mathematical object, when subtracted from each other, give zero. You may wonder why in the functions multiply and negate there are terms 1 and -1. These terms are a trick to force Bracmat to expand the products. As it is more costly to factorize a sum than to expand a product into a sum, Bracmat retains isolated products. However, when in combination with a non-zero term, the product is expanded.

The more recent C99 standard has built-in complex number primitive types, which can be declared with float, double, or long double precision. To use these types and their associated library functions, you must include the <complex.h> header. (Note: this is a different header than the <complex> templates that are defined by C++.) [1][2]

Complex numbers are a built-in numeric type in Common Lisp. The literal syntax for a complex number is #C(realimaginary). The components of a complex number may be integers, ratios, or floating-point. Arithmetic operations automatically return complex (or real) numbers when appropriate:

>(sqrt -1)#C(0.01.0)

>(expt #c(01)2)-1

Here are some arithmetic operations on complex numbers:

>(+ #c(01) #c(10))#C(11)

>(* #c(11)2)#C(22)

>(* #c(11) #c(02))#C(-22)

>(- #c(11))#C(-1 -1)

>(/ #c(02))#C(0 -1/2)

>(conjugate #c(11))#C(1 -1)

Complex numbers can be constructed from real and imaginary parts using the complex function, and taken apart using the realpart and imagpart functions.

There is no standard syntax or mechanism for complex numbers. The FSL provides several implementations suitable for different uses. This example uses the existing floating point stack, but other libraries define a separate complex stack and/or a fixed-point implementation suitable for microcontrollers and DSPs.

In ANSI FORTRAN 66 or later, COMPLEX is a built-in data type with full access to intrinsic arithmetic operations. Putting each native operation in a function is horribly inefficient, so I will simply demonstrate the operations. This example shows usage for Fortran 90 or later:

Groovy does not provide any built-in facility for complex arithmetic. However, it does support arithmetic operator overloading. Thus it is not too hard to build a fairly robust, complete, and intuitive complex number class, such as the following:

Complex numbers are parameterized in their base type, so you can
have Complex Integer for the Gaussian Integers, Complex Float, Complex Double, etc. The operations are just the usual overloaded numeric operations.

For speed and for conformance with the complex plane interpretation, x+iy is represented as [x,y]; for flexibility, all the functions defined here will accept both real and complex numbers; and for uniformity, they are implemented as functions that ignore their input.

Recent versions of jq support modules, so these functions could all be placed in a module to avoid name conflicts, and thus no special prefix is used here.

Powering to a complex power can in general not be written shorter, so Mathematica leaves it unevaluated if the numbers are exact. An approximation can be acquired using the function N.
However Mathematica goes much further, basically all functions can handle complex numbers to arbitrary precision, including (but not limited to!):

Mathematica has fundamental support for both explicit complex numbers and symbolic complex variables. All applicable mathematical functions support arbitrary-precision evaluation for complex values of all parameters, and symbolic operations automatically treat complex variables with full generality.

::method divideuse strict arg other-- this is easier if everything is a complex numberif\other~isA(.complex)then other = .complex~new(other)-- division is multiplication with the inversionreturn self * other~inv

::method "=="expose r iuse strict arg other

if\other~isa(.complex)thenreturn.false-- Note: these are numeric comparisons, so we're using the "="-- method so those are handled correctlyreturn r = other~r & i = other~i

::method "\=="use strict arg otherreturn\self~"\=="(other)

::method "="-- this is equivalent of "=="forward message("==")

::method "\="-- this is equivalent of "\=="forward message("\==")

::method "<>"-- this is equivalent of "\=="forward message("\==")

::method "><"-- this is equivalent of "\=="forward message("\==")

-- some operator overrides -- these only work if the left-hand-side of the-- subexpression is a quaternion::method "*"forward message("TIMES")

::method "/"forward message("DIVIDE")

::method "-"-- need to check if this is a prefix minus or a subtractifarg() == 0thenforward message("NEGATIVE")elseforward message("SUBTRACT")

::method "+"-- need to check if this is a prefix plus or an additionifarg() == 0thenreturn self -- we can return this copy since it is imutableelseforward message("ADD")

Complex numbers are a built-in data type in Pop11. Real and
imaginary part of complex numbers can be floating point or
exact (integer or rational) value (both part must be of the same
type). Operations on floating point complex numbers always produce
complex numbers. Operations on exact complex numbers give
real result (integer or rational) if imaginary part of the result
is 0. The '+:' and '-:' operators create complex numbers:
'1 -: 3' is '1 - 3i' in mathematical notation.

Scheme implementations are not required to support complex numbers, but if they do, they are required to support complex number literals in one of the following standard formats[3]:

rectangular coordinates: real+imagi (or real-imagi), where real is the real part and imag is the imaginary part. For a pure-imaginary number, the real part may be omitted but the sign of the imaginary part is mandatory (even if it is "+"): +imagi (or -imagi). If the imaginary part is 1 or -1, the imaginary part can be omitted, leaving only the +i or -i at the end.

polar coordinates: r@theta, where r is the absolute value (magnitude) and theta is the angle

TI-83 BASIC has built in complex number support; the normal arithmetic operators + - * / are used.

The method complex numbers are displayed can be chosen in the "MODE" menu.
Real: Does not show complex numbers, gives an error if a number is imaginary.
a+bi: The classic display for imaginary numbers with the real and imaginary components
re^Θi: Displays imaginary numbers in Polar Coordinates.

Character set note: the symbol for the imaginary unit is not the normal "i" but a different character (Unicode: U+F02F "" (private use area); this character should display with the "TI Uni" font). Also, U+3013 EN DASH “–”, displayed on the TI as a superscript minus, is used for the minus sign on numbers, distinct from ASCII "-" used for subtraction.

Complex numbers can also be entered and displayed in polar form. (This example shows input in polar form while the complex display mode is rectangular and the angle mode is radians).

■ (1∠π/4)
√(2)/2 + √(2)/2*

Note that the parentheses around ∠ notation are required. It has a related use in vectors: (1∠π/4) is a complex number, [1,∠π/4] is a vector in two dimensions in polar notation, and [(1∠π/4)] is a complex number in a vector.

Complex numbers are a primitive type that can be parsed in
fixed or exponential formats, with either i or j notation as shown.
The usual complex arithmetic and transcendental functions are callable
using the syntax libname..funcname or a recognizable truncation (e.g.,
c..add or ..csin). Real operands are promoted to complex.