Arithmetic/Complex: Difference between revisions

m
Automated syntax highlighting fixup (second round - minor fixes)
m (syntax highlighting fixup automation)
m (Automated syntax highlighting fixup (second round - minor fixes))
Line 29:
{{trans|Python}}
 
<syntaxhighlight lang="11l">V z1 = 1.5 + 3i
V z2 = 1.5 + 1.5i
print(z1 + z2)
Line 58:
=={{header|Action!}}==
{{libheader|Action! Tool Kit}}
<syntaxhighlight lang=Action"action!">INCLUDE "D2:REAL.ACT" ;from the Action! Tool Kit
 
DEFINE R_="+0"
Line 206:
 
=={{header|Ada}}==
<syntaxhighlight lang="ada">with Ada.Numerics.Generic_Complex_Types;
with Ada.Text_IO.Complex_IO;
 
Line 259:
{{works with|ALGOL 68G|Any - tested with release [http://sourceforge.net/projects/algol68/files/algol68g/algol68g-1.18.0/algol68g-1.18.0-9h.tiny.el5.centos.fc11.i386.rpm/download 1.18.0-9h.tiny]}}
{{wont work with|ELLA ALGOL 68|Any (with appropriate job cards) - tested with release [http://sourceforge.net/projects/algol68/files/algol68toc/algol68toc-1.8.8d/algol68toc-1.8-8d.fc9.i386.rpm/download 1.8-8d] - due to extensive use of FORMATted transput}}
<syntaxhighlight lang="algol68">main:(
FORMAT compl fmt = $g(-7,5)"⊥"g(-7,5)$;
Line 298:
=={{header|ALGOL W}}==
Complex is a built-in type in Algol W.
<syntaxhighlight lang="algolw">begin
% show some complex arithmetic %
% returns c + d, using the builtin complex + operator %
Line 335:
 
=={{header|APL}}==
<syntaxhighlight lang="text">
x←1j1 ⍝assignment
y←5.25j1.5
Line 355:
=={{header|Arturo}}==
 
<syntaxhighlight lang="rebol">a: to :complex [1 1]
b: to :complex @[pi 1.2]
 
Line 379:
=={{header|AutoHotkey}}==
contributed by Laszlo on the ahk [http://www.autohotkey.com/forum/post-276431.html#276431 forum]
<syntaxhighlight lang=AutoHotkey"autohotkey">Cset(C,1,1)
MsgBox % Cstr(C) ; 1 + i*1
Cneg(C,C)
Line 429:
=={{header|AWK}}==
contributed by af
<syntaxhighlight lang="awk"># simulate a struct using associative arrays
function complex(arr, re, im) {
arr["re"] = re
Line 488:
=={{header|BASIC}}==
{{works with|QuickBasic|4.5}}
<syntaxhighlight lang="qbasic">TYPE complex
real AS DOUBLE
imag AS DOUBLE
Line 579:
=={{header|BBC BASIC}}==
{{works with|BBC BASIC for Windows}}
<syntaxhighlight lang="bbcbasic"> DIM Complex{r, i}
DIM a{} = Complex{} : a.r = 1.0 : a.i = 1.0
Line 627:
=={{header|Bracmat}}==
Bracmat recognizes the symbol <code>i</code> as the square root of <code>-1</code>. The results of the functions below are not necessarily of the form <code>a+b*i</code>, 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 <code>multiply</code> and <code>negate</code> there are terms <code>1</code> and <code>-1</code>. 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.
<syntaxhighlight lang="bracmat"> (add=a b.!arg:(?a,?b)&!a+!b)
& ( multiply
= a b.!arg:(?a,?b)&1+!a*!b+-1
Line 669:
{{works with|C99}}
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++]].) [http://www.opengroup.org/onlinepubs/009695399/basedefs/complex.h.html] [http://publib.boulder.ibm.com/infocenter/pseries/v5r3/index.jsp?topic=/com.ibm.vacpp7a.doc/language/ref/clrc03complex_types.htm]
<syntaxhighlight lang="c">#include <complex.h>
#include <stdio.h>
 
Line 704:
{{works with|C89}}
User-defined type:
<syntaxhighlight lang="c">typedef struct{
double real;
double imag;
Line 768:
=={{header|C sharp|C#}}==
{{works with|C sharp|C#|4.0}}
<syntaxhighlight lang="csharp">namespace RosettaCode.Arithmetic.Complex
{
using System;
Line 786:
}</syntaxhighlight>
{{works with|C sharp|C#|1.2}}
<syntaxhighlight lang="csharp">using System;
 
public struct ComplexNumber
Line 993:
 
=={{header|C++}}==
<syntaxhighlight lang="cpp">#include <iostream>
#include <complex>
using std::complex;
Line 1,017:
Therefore, we use defrecord and the multimethods in
clojure.algo.generic.arithmetic to make a Complex number type.
<syntaxhighlight lang="clojure">(ns rosettacode.arithmetic.cmplx
(:require [clojure.algo.generic.arithmetic :as ga])
(:import [java.lang Number]))
Line 1,061:
===.NET Complex class===
{{trans|C#}}
<syntaxhighlight lang="cobol"> $SET SOURCEFORMAT "FREE"
$SET ILUSING "System"
$SET ILUSING "System.Numerics"
Line 1,077:
 
===Implementation===
<syntaxhighlight lang="cobol"> $SET SOURCEFORMAT "FREE"
class-id Prog.
method-id. Main static.
Line 1,177:
 
=={{header|CoffeeScript}}==
<syntaxhighlight lang="coffeescript">
# create an immutable Complex type
class Complex
Line 1,255:
Complex numbers are a built-in numeric type in Common Lisp. The literal syntax for a complex number is <tt>#C(<var>real</var> <var>imaginary</var>)</tt>. The components of a complex number may be integers, ratios, or floating-point. Arithmetic operations automatically return complex (or real) numbers when appropriate:
 
<syntaxhighlight lang="lisp">> (sqrt -1)
#C(0.0 1.0)
 
Line 1,263:
Here are some arithmetic operations on complex numbers:
 
<syntaxhighlight lang="lisp">> (+ #c(0 1) #c(1 0))
#C(1 1)
 
Line 1,283:
Complex numbers can be constructed from real and imaginary parts using the <tt>complex</tt> function, and taken apart using the <tt>realpart</tt> and <tt>imagpart</tt> functions.
 
<syntaxhighlight lang="lisp">> (complex 64 (/ 3 4))
#C(64 3/4)
 
Line 1,294:
=={{header|Component Pascal}}==
BlackBox Component Builder
<syntaxhighlight lang="oberon2">
MODULE Complex;
IMPORT StdLog;
Line 1,397:
=={{header|D}}==
Built-in complex numbers are now deprecated in D, to simplify the language.
<syntaxhighlight lang="d">import std.stdio, std.complex;
 
void main() {
Line 1,415:
 
=={{header|Dart}}==
<syntaxhighlight lang="dart">
 
class complex {
Line 1,471:
{{libheader| System.SysUtils}}
{{libheader| System.VarCmplx}}
<syntaxhighlight lang=Delphi"delphi">
program Arithmetic_Complex;
 
Line 1,507:
=={{header|EchoLisp}}==
Complex numbers are part of the language. No special library is needed.
<syntaxhighlight lang="lisp">
(define a 42+666i) → a
(define b 1+i) → b
Line 1,521:
 
=={{header|Elixir}}==
<syntaxhighlight lang="elixir">defmodule Complex do
import Kernel, except: [abs: 1, div: 2]
Line 1,622:
 
=={{header|Erlang}}==
<syntaxhighlight lang=Erlang"erlang">%% Task: Complex Arithmetic
%% Author: Abhay Jain
 
Line 1,681:
end. </syntaxhighlight>
{{out}}
<syntaxhighlight lang=Erlang"erlang">Ans = 6+5i
Ans = -1+17i
Ans = -1-3i
Line 1,688:
 
=={{header|ERRE}}==
<syntaxhighlight lang="text">
PROGRAM COMPLEX_ARITH
 
Line 1,747:
=={{header|Euler Math Toolbox}}==
 
<syntaxhighlight lang=Euler"euler Mathmath Toolboxtoolbox">
>a=1+4i; b=5-3i;
>a+b
Line 1,764:
 
=={{header|Euphoria}}==
<syntaxhighlight lang="euphoria">constant REAL = 1, IMAG = 2
type complex(sequence s)
return length(s) = 2 and atom(s[REAL]) and atom(s[IMAG])
Line 1,835:
 
C1:
<syntaxhighlight lang="excel">
=IMSUM(A1;B1)
</syntaxhighlight>
 
D1:
<syntaxhighlight lang="excel">
=IMPRODUCT(A1;B1)
</syntaxhighlight>
 
E1:
<syntaxhighlight lang="excel">
=IMSUB(0;D1)
</syntaxhighlight>
 
F1:
<syntaxhighlight lang="excel">
=IMDIV(1;E28)
</syntaxhighlight>
 
G1:
<syntaxhighlight lang="excel">
=IMCONJUGATE(C28)
</syntaxhighlight>
Line 1,861:
E1 will have the negation of D1's value
 
<syntaxhighlight lang="text">
1+2i 3+5i 4+7i -7+11i 7-11i 0,0411764705882353+0,0647058823529412i 4-7i
</syntaxhighlight>
Line 1,867:
=={{header|F Sharp|F#}}==
Entered into an interactive session to show the results:
<syntaxhighlight lang="fsharp">
> open Microsoft.FSharp.Math;;
 
Line 1,916:
 
=={{header|Factor}}==
<syntaxhighlight lang="factor">USING: combinators kernel math math.functions prettyprint ;
 
C{ 1 2 } C{ 0.9 -2.78 } {
Line 1,938:
{{libheader|Forth Scientific Library}}
Historically, there was no standard syntax or mechanism for complex numbers and several implementations suitable for different uses were provided. However later a wordset ''was'' standardised as "Algorithm #60".
<syntaxhighlight lang="forth">S" fsl-util.fs" REQUIRED
S" complex.fs" REQUIRED
 
Line 1,954:
=={{header|Fortran}}==
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:
<syntaxhighlight lang="fortran">program cdemo
complex :: a = (5,3), b = (0.5, 6.0) ! complex initializer
complex :: absum, abprod, aneg, ainv
Line 1,965:
 
And, although you did not ask, here are demonstrations of some other common complex number operations
<syntaxhighlight lang="fortran">program cdemo2
complex :: a = (5,3), b = (0.5, 6) ! complex initializer
real, parameter :: pi = 3.141592653589793 ! The constant "pi"
Line 1,995:
 
=={{header|FreeBASIC}}==
<syntaxhighlight lang="freebasic">' FB 1.05.0 Win64
 
Type Complex
Line 2,076:
=={{header|Free Pascal}}==
FreePascal has a complex units. Example of usage:
<syntaxhighlight lang=Pascal"pascal">Program ComplexDemo;
 
uses
Line 2,108:
=={{header|Frink}}==
Frink's operations handle complex numbers naturally. The real and imaginary parts of complex numbers can be arbitrary-sized integers, arbitrary-sized rational numbers, or arbitrary-precision floating-point numbers.
<syntaxhighlight lang="frink">
add[x,y] := x + y
multiply[x,y] := x * y
Line 2,135:
=={{header|Futhark}}==
{{incorrect|Futhark|Futhark's syntax has changed, so "fun" should be "let"}}
<syntaxhighlight lang=Futhark"futhark">
type complex = (f64,f64)
 
Line 2,166:
 
=={{header|GAP}}==
<syntaxhighlight lang="gap"># GAP knows gaussian integers, gaussian rationals (i.e. Q[i]), and cyclotomic fields. Here are some examples.
# E(n) is an nth primitive root of 1
i := Sqrt(-1);
Line 2,188:
=={{header|Go}}==
Go has complex numbers built in, with the complex conjugate in the standard library.
<syntaxhighlight lang="go">package main
 
import (
Line 2,219:
=={{header|Groovy}}==
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:
<syntaxhighlight lang="groovy">class Complex {
final Number real, imag
Line 2,322:
 
The following ''ComplexCategory'' class allows for modification of regular ''Number'' behavior when interacting with ''Complex''.
<syntaxhighlight lang="groovy">import org.codehaus.groovy.runtime.DefaultGroovyMethods
class ComplexCategory {
Line 2,342:
 
Test Program (mixes the ComplexCategory methods into the Number class):
<syntaxhighlight lang="groovy">import static Complex.*
Number.metaClass.mixin ComplexCategory
Line 2,440:
 
=={{header|Hare}}==
<syntaxhighlight lang="hare">use fmt;
use math::complex::{c128,addc128,mulc128,divc128,negc128,conjc128};
 
Line 2,469:
have ''Complex Integer'' for the Gaussian Integers, ''Complex Float'', ''Complex Double'', etc. The operations are just the usual overloaded numeric operations.
 
<syntaxhighlight lang="haskell">import Data.Complex
 
main = do
Line 2,497:
{{improve|Unicon|This could be better implemented as an object i n Unicon. Note, however, that Unicon doesn't allow for operator overloading at the current time.}}
Icon doesn't provide native support for complex numbers. Support is included in the IPL.
<syntaxhighlight lang=Icon"icon">procedure main()
 
SetupComplex()
Line 2,523:
{{libheader|Icon Programming Library}}
[http://www.cs.arizona.edu/icon/library/src/procs/complex.icn provides complex number support] supplemented by the code below.
<syntaxhighlight lang=Icon"icon">
link complex # for complex number support
 
Line 2,576:
<tt>complex</tt> (and <tt>dcomplex</tt> for double-precision) is a built-in data type in IDL:
 
<syntaxhighlight lang="idl">x=complex(1,1)
y=complex(!pi,1.2)
print,x+y
Line 2,589:
=={{header|J}}==
Complex numbers are a native numeric data type in J. Although the examples shown here are performed on scalars, all numeric operations naturally apply to arrays of complex numbers.
<syntaxhighlight lang="j"> x=: 1j1
y=: 3.14159j1.2
x+y NB. addition
Line 2,604:
 
=={{header|Java}}==
<syntaxhighlight lang="java">public class Complex {
public final double real;
public final double imag;
Line 2,658:
 
=={{header|JavaScript}}==
<syntaxhighlight lang="javascript">function Complex(r, i) {
this.r = r;
this.i = i;
Line 2,717:
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.<syntaxhighlight lang="jq">def real(z): if (z|type) == "number" then z else z[0] end;
 
def imag(z): if (z|type) == "number" then 0 else z[1] end;
Line 2,782:
test( [1,1]; [0,1] )</syntaxhighlight>
{{Out}}
<syntaxhighlight lang="jq">$ jq -n -f complex.jq
"x = [1,1]"
"y = [0,1]"
Line 2,795:
=={{header|Julia}}==
Julia has built-in support for complex arithmetic with arbitrary real types.
<syntaxhighlight lang="julia">julia> z1 = 1.5 + 3im
julia> z2 = 1.5 + 1.5im
julia> z1 + z2
Line 2,819:
 
=={{header|Kotlin}}==
<syntaxhighlight lang="scala">class Complex(private val real: Double, private val imag: Double) {
operator fun plus(other: Complex) = Complex(real + other.real, imag + other.imag)
 
Line 2,873:
 
=={{header|Lambdatalk}}==
<syntaxhighlight lang="scheme">
{require lib_complex}
 
Line 2,904:
 
A convenient data structure for a complex number is the record:
<syntaxhighlight lang="lisp">
(defrecord complex
real
Line 2,912:
Here are the required functions:
 
<syntaxhighlight lang="lisp">
(defun add
(((match-complex real r1 img i1)
Line 2,934:
Bonus:
 
<syntaxhighlight lang="lisp">
(defun conj
(((match-complex real r img i))
Line 2,942:
The functions above are built using the following supporting functions:
 
<syntaxhighlight lang="lisp">
(defun new (r i)
(make-complex real r img i))
Line 2,959:
Finally, we have some functions for use in the conversion and display of our complex number data structure:
 
<syntaxhighlight lang="lisp">
(defun ->str
(((match-complex real r img i)) (when (>= i 0))
Line 2,998:
 
=={{header|Liberty BASIC}}==
<syntaxhighlight lang="lb">mainwin 50 10
 
print " Adding"
Line 3,051:
 
=={{header|Lua}}==
<syntaxhighlight lang="lua">
 
--defines addition, subtraction, negation, multiplication, division, conjugation, norms, and a conversion to strgs.
Line 3,090:
Maple has <code>I</code> (the square root of -1) built-in. Thus:
 
<syntaxhighlight lang="maple">x := 1+I;
y := Pi+I*1.2;</syntaxhighlight>
 
By itself, it will perform mathematical operations symbolically, i.e. it will not try to perform computational evaluation unless specifically asked to do so. Thus:
 
<syntaxhighlight lang="maple">x*y;
==> (1 + I) (Pi + 1.2 I)
simplify(x*y);
Line 3,102:
Other than that, the task merely asks for
 
<syntaxhighlight lang="maple">x+y;
x*y;
-x;
Line 3,109:
=={{header|Mathematica}} / {{header|Wolfram Language}}==
Mathematica has fully implemented support for complex numbers throughout the software. Addition, subtraction, division, multiplications and powering need no further syntax than for real numbers:
<syntaxhighlight lang=Mathematica"mathematica">x=1+2I
y=3+4I
 
Line 3,122:
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!):
<syntaxhighlight lang=Mathematica"mathematica">Exp Log
Sin Cos Tan Csc Sec Cot
ArcSin ArcCos ArcTan ArcCsc ArcSec ArcCot
Line 3,138:
Complex numbers are a primitive data type in MATLAB. All the typical complex operations can be performed. There are two keywords that specify a number as complex: "i" and "j".
 
<syntaxhighlight lang=MATLAB"matlab">>> a = 1+i
 
a =
Line 3,199:
 
=={{header|Maxima}}==
<syntaxhighlight lang="maxima">z1: 5 + 2 * %i;
2*%i+5
 
Line 3,253:
Division: С/П; multiplication: БП 36 С/П; addition: БП 54 С/П; subtraction: БП 63 С/П.
 
<syntaxhighlight lang="text">ПA С/П ПB С/П ПC С/П ПD С/П ИПC x^2
ИПD x^2 + П3 ИПA ИПC * ИПB ИПD *
+ ИП3 / П1 ИПB ИПC * ИПA ИПD *
Line 3,263:
 
=={{header|Modula-2}}==
<syntaxhighlight lang="modula2">MODULE complex;
 
IMPORT InOut;
Line 3,343:
{{trans|Java}}
This is a translation of the Java version, but it uses operator redefinition where possible.
<syntaxhighlight lang="nanoquery">import math
 
class Complex
Line 3,401:
 
=={{header|Nemerle}}==
<syntaxhighlight lang=Nemerle"nemerle">using System;
using System.Console;
using System.Numerics;
Line 3,435:
 
=={{header|Nim}}==
<syntaxhighlight lang="nim">
import complex
var a: Complex = (1.0,1.0)
Line 3,460:
=={{header|Oberon-2}}==
Oxford Oberon Compiler
<syntaxhighlight lang="oberon2">
MODULE Complex;
IMPORT Files,Out;
Line 3,561:
=={{header|OCaml}}==
The [http://caml.inria.fr/pub/docs/manual-ocaml/libref/Complex.html Complex] module from the standard library provides the functionality of complex numbers:
<syntaxhighlight lang="ocaml">open Complex
 
let print_complex z =
Line 3,576:
 
Using [http://forge.ocamlcore.org/projects/pa-do/ Delimited Overloading], the syntax can be made closer to the usual one:
<syntaxhighlight lang="ocaml">let () =
Complex.(
let print txt z = Printf.printf "%s = %s\n" txt (to_string z) in
Line 3,593:
=={{header|Octave}}==
GNU Octave handles naturally complex numbers:
<syntaxhighlight lang="octave">z1 = 1.5 + 3i;
z2 = 1.5 + 1.5i;
disp(z1 + z2); % 3.0 + 4.5i
Line 3,610:
=={{header|Oforth}}==
 
<syntaxhighlight lang=Oforth"oforth">Object Class new: Complex(re, im)
Complex method: re @re ;
Line 3,652:
Usage :
 
<syntaxhighlight lang=Oforth"oforth">3.2 >complex I * 2 >complex + .cr
2 3 Complex new 1.2 >complex + .cr
2 3 Complex new 1.2 >complex * .cr
Line 3,668:
Ol supports complex numbers by default. Numbers must be entered manually in form A+Bi without spaces between elements, where A and B - numbers (can be rational), i - imaginary unit; or in functional form using function `complex`.
 
<syntaxhighlight lang="scheme">
(define A 0+1i) ; manually entered numbers
(define B 1+0i)
Line 3,695:
 
=={{header|ooRexx}}==
<syntaxhighlight lang=ooRexx"oorexx">c1 = .complex~new(1, 2)
c2 = .complex~new(3, 4)
r = 7
Line 3,852:
=={{header|OxygenBasic}}==
Implementation of a complex numbers class with arithmetical operations, and powers using DeMoivre's theorem (polar conversion).
<syntaxhighlight lang="oxygenbasic">
'COMPLEX OPERATIONS
'=================
Line 3,989:
=={{header|PARI/GP}}==
To use, type, e.g., inv(3 + 7*I).
<syntaxhighlight lang="parigp">add(a,b)=a+b;
mult(a,b)=a*b;
neg(a)=-a;
Line 3,997:
{{works with|Extended Pascal}}
The simple data type <tt>complex</tt> is part of Extended Pascal, ISO standard 10206.
<syntaxhighlight lang="pascal">program complexDemo(output);
 
const
Line 4,077:
=={{header|Perl}}==
The <code>Math::Complex</code> module implements complex arithmetic.
<syntaxhighlight lang="perl">use Math::Complex;
my $a = 1 + 1*i;
my $b = 3.14159 + 1.25*i;
Line 4,089:
 
=={{header|Phix}}==
<!--<syntaxhighlight lang=Phix"phix">(phixonline)-->
<span style="color: #000080;font-style:italic;">-- demo\rosetta\ArithComplex.exw</span>
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span>
Line 4,127:
 
=={{header|PicoLisp}}==
<syntaxhighlight lang=PicoLisp"picolisp">(load "@lib/math.l")
 
(de addComplex (A B)
Line 4,178:
 
=={{header|PL/I}}==
<syntaxhighlight lang="pli">/* PL/I complex numbers may be integer or floating-point. */
/* In this example, the variables are floating-pint. */
/* For integer variables, change 'float' to 'fixed binary' */
Line 4,211:
'1 -: 3' is '1 - 3i' in mathematical notation.
 
<syntaxhighlight lang="pop11">lvars a = 1.0 +: 1.0, b = 2.0 +: 5.0 ;
a+b =>
a*b =>
Line 4,233:
=={{header|PostScript}}==
Complex numbers can be represented as 2 element vectors ( arrays ). Thus, a+bi can be written as [a b] in PostScript.
<syntaxhighlight lang="text">
%Adding two complex numbers
/addcomp{
Line 4,286:
=={{header|PowerShell}}==
===Implementation===
<syntaxhighlight lang=PowerShell"powershell">
class Complex {
[Double]$x
Line 4,338:
</pre>
===Library===
<syntaxhighlight lang=PowerShell"powershell">
function show([System.Numerics.Complex]$c) {
if(0 -le $c.Imaginary) {
Line 4,368:
 
=={{header|PureBasic}}==
<syntaxhighlight lang=PureBasic"purebasic">Structure Complex
real.d
imag.d
Line 4,436:
=={{header|Python}}==
 
<syntaxhighlight lang="python">>>> z1 = 1.5 + 3j
>>> z2 = 1.5 + 1.5j
>>> z1 + z2
Line 4,463:
{{trans|Octave}}
 
<syntaxhighlight lang="rsplus">z1 <- 1.5 + 3i
z2 <- 1.5 + 1.5i
print(z1 + z2) # 3+4.5i
Line 4,479:
=={{header|Racket}}==
 
<syntaxhighlight lang="racket">
#lang racket
 
Line 4,498:
{{works with|Rakudo|2015.12}}
 
<syntaxhighlight lang=perl6"raku" line>my $a = 1 + i;
my $b = pi + 1.25i;
 
Line 4,518:
=={{header|REXX}}==
The REXX language has no complex type numbers, but most complex arithmetic functions can easily be written.
<syntaxhighlight lang="rexx">/*REXX program demonstrates how to support some math functions for complex numbers. */
x = '(5,3i)' /*define X ─── can use I i J or j */
y = "( .5, 6j)" /*define Y " " " " " " " */
Line 4,554:
=={{header|RLaB}}==
 
<syntaxhighlight lang=RLaB"rlab">
>> x = sqrt(-1)
0 + 1i
Line 4,566:
 
=={{header|Ruby}}==
<syntaxhighlight lang="ruby">
# Four ways to write complex numbers:
a = Complex(1, 1) # 1. call Kernel#Complex
Line 4,585:
* All of these operations are safe with other numeric types. For example, <code>42.conjugate</code> returns 42.
 
<syntaxhighlight lang="ruby"># Other ways to find the multiplicative inverse:
puts 1.quo a # always works
puts 1.0 / a # works, but forces floating-point math
Line 4,591:
 
=={{header|Rust}}==
<syntaxhighlight lang="rust">extern crate num;
use num::complex::Complex;
 
Line 4,612:
Scala doesn't come with a Complex library, but one can be made:
 
<syntaxhighlight lang="scala">package org.rosettacode
 
package object ArithmeticComplex {
Line 4,650:
Usage example:
 
<syntaxhighlight lang="scala">scala> import org.rosettacode.ArithmeticComplex._
import org.rosettacode.ArithmeticComplex._
 
Line 4,682:
* rectangular coordinates: <code>''real''+''imag''i</code> (or <code>''real''-''imag''i</code>), 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 "+"): <code>+''imag''i</code> (or <code>-''imag''i</code>). If the imaginary part is 1 or -1, the imaginary part can be omitted, leaving only the <code>+i</code> or <code>-i</code> at the end.
* polar coordinates: <code>''r''@''theta''</code>, where ''r'' is the absolute value (magnitude) and ''theta'' is the angle
<syntaxhighlight lang="scheme">(define a 1+i)
(define b 3.14159+1.25i)
 
Line 4,692:
=={{header|Seed7}}==
 
<syntaxhighlight lang="seed7">$ include "seed7_05.s7i";
include "float.s7i";
include "complex.s7i";
Line 4,714:
 
=={{header|Sidef}}==
<syntaxhighlight lang="ruby">var a = 1:1 # Complex(1, 1)
var b = 3.14159:1.25 # Complex(3.14159, 1.25)
Line 4,740:
=={{header|Slate}}==
 
<syntaxhighlight lang="slate">[| a b |
a: 1 + 1 i.
b: Pi + 1.2 i.
Line 4,754:
=={{header|Smalltalk}}==
{{works with|GNU Smalltalk}}
<syntaxhighlight lang="smalltalk">PackageLoader fileInPackage: 'Complex'.
|a b|
a := 1 + 1 i.
Line 4,769:
{{works with|Smalltalk/X}}
Complex is already in the basic class library. Multiples of imaginary are created by sending an "i" message to a number, which can be added to another number. Thus 5i => (0+5i), 1+(1/3)I => (1+1/3i) and (1.0+2i) => (1.0+2i). Notice that the real and imaginary components can be arbitrary integers, fractions or floating point numbers. And the results will be exact (i.e. have fractions or integer) if possible.
<syntaxhighlight lang="smalltalk">
|a b|
a := 1 + 1i.
Line 4,817:
<b>Original author unknown {:o(</b>
 
<syntaxhighlight lang="qbasic">' complex numbers are native for "smart BASIC"
A=1+2i
B=3-5i
Line 4,841:
{{works with|CSnobol}}
 
<syntaxhighlight lang=SNOBOL4"snobol4">* # Define complex datatype
data('complex(r,i)')
 
Line 4,889:
 
=={{header|Standard ML}}==
<syntaxhighlight lang=Standard"standard MLml">
(* Signature for complex numbers *)
signature COMPLEX = sig
Line 4,937:
=={{header|Stata}}==
 
<syntaxhighlight lang="stata">mata
C(2,3)
2 + 3i
Line 4,985:
Use a struct to create a complex number type in Swift. Math Operations can be added using operator overloading
 
<syntaxhighlight lang="swift">
public struct Complex {
Line 5,036:
Make the Complex Number struct printable and easier to debug by adding making it conform to CustomStringConvertible
 
<syntaxhighlight lang="swift">
 
extension Complex : CustomStringConvertible {
Line 5,067:
Explicitly support subtraction and division
 
<syntaxhighlight lang="swift">
public func - (left:Complex, right:Complex) -> Complex {
return left + -right
Line 5,082:
=={{header|Tcl}}==
{{tcllib|math::complexnumbers}}
<syntaxhighlight lang="tcl">package require math::complexnumbers
namespace import math::complexnumbers::*
 
Line 5,128:
=={{header|UNIX Shell}}==
{{works with|ksh93}}
<syntaxhighlight lang="bash">typeset -T Complex_t=(
float real=0
float imag=0
Line 5,212:
c..add or ..csin). Real operands are promoted to complex.
 
<syntaxhighlight lang=Ursala"ursala">u = 3.785e+00-1.969e+00i
v = 9.545e-01-3.305e+00j
 
Line 5,233:
 
=={{header|VBA}}==
<syntaxhighlight lang=VBA"vba">
Public Type Complex
re As Double
Line 5,339:
 
=={{header|Vlang}}==
<syntaxhighlight lang="vlang">import math.complex
fn main() {
a := complex.complex(1, 1)
Line 5,363:
=={{header|Wortel}}==
{{trans|CoffeeScript}}
<syntaxhighlight lang="wortel">@class Complex {
&[r i] @: {
^r || r 0
Line 5,403:
=={{header|Wren}}==
{{libheader|Wren-complex}}
<syntaxhighlight lang="ecmascript">import "/complex" for Complex
 
var x = Complex.new(1, 3)
Line 5,431:
 
=={{header|XPL0}}==
<syntaxhighlight lang=XPL0"xpl0">include c:\cxpl\codes;
 
func real CAdd(A, B, C); \Return complex sum of two complex numbers
Line 5,499:
 
=={{header|Yabasic}}==
<syntaxhighlight lang=Yabasic"yabasic">rem ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
rem CADDI/CADDR addition of complex numbers Z1 + Z2 with Z1 = a1 + b1 *i Z2 = a2 + b2*i
rem CADDI returns imaginary part and CADDR the real part
Line 5,549:
 
=={{header|zkl}}==
<syntaxhighlight lang="zkl">var [const] GSL=Import("zklGSL"); // libGSL (GNU Scientific Library)
(GSL.Z(3,4) + GSL.Z(1,2)).println(); // (4.00+6.00i)
(GSL.Z(3,4) - GSL.Z(1,2)).println(); // (2.00+2.00i)
Line 5,569:
 
=={{header|zonnon}}==
<syntaxhighlight lang="zonnon">
module Numbers;
type
Line 5,677:
=={{header|ZX Spectrum Basic}}==
{{trans|BBC BASIC}}
<syntaxhighlight lang="zxbasic">5 LET complex=2: LET r=1: LET i=2
10 DIM a(complex): LET a(r)=1.0: LET a(i)=1.0
20 DIM b(complex): LET b(r)=PI: LET b(i)=1.2
10,333

edits