2016 Update
This commit is contained in:
parent
948b86eafa
commit
dcf5d15da3
7965 changed files with 139854 additions and 31002 deletions
|
|
@ -1,7 +1,24 @@
|
|||
A '''[[wp:Complex number|complex number]]''' is a number which can be written as "<math>a + b \times i</math>" (sometimes shown as "<math>b + a \times i</math>") where a and b are real numbers and [[wp:Imaginary_unit|<math>i</math> 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 <math>i</math>.
|
||||
A '''[[wp:Complex number|complex number]]''' is a number which can be written as:
|
||||
<big><math>a + b \times i</math></big>
|
||||
(sometimes shown as:
|
||||
<big><math>b + a \times i</math></big>
|
||||
where <big><math>a</math></big> and <big><math>b</math></big> are real numbers, and [[wp:Imaginary_unit|<big><math>i</math></big>]] is <big>√{{overline| -1 }}</big>
|
||||
|
||||
* 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 [[wp:complex conjugate|complex conjugate]] of <math>a + bi</math> is <math>a - bi</math>.
|
||||
|
||||
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.
|
||||
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 <big><math>i</math></big>.
|
||||
|
||||
|
||||
;Task:
|
||||
* 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.
|
||||
|
||||
<br>
|
||||
By definition, the [[wp:complex conjugate|complex conjugate]] of
|
||||
<big><math>a + bi</math></big>
|
||||
is
|
||||
<big><math>a - bi</math></big>
|
||||
|
||||
<br>
|
||||
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.
|
||||
<br><br>
|
||||
|
|
|
|||
85
Task/Arithmetic-Complex/Elixir/arithmetic-complex.elixir
Normal file
85
Task/Arithmetic-Complex/Elixir/arithmetic-complex.elixir
Normal file
|
|
@ -0,0 +1,85 @@
|
|||
defmodule Complex do
|
||||
import Kernel, except: [abs: 1, div: 2]
|
||||
|
||||
defstruct real: 0, imag: 0
|
||||
|
||||
def new(real, imag) do
|
||||
%__MODULE__{real: real, imag: imag}
|
||||
end
|
||||
|
||||
def add(a, b) do
|
||||
{a, b} = convert(a, b)
|
||||
new(a.real + b.real, a.imag + b.imag)
|
||||
end
|
||||
|
||||
def sub(a, b) do
|
||||
{a, b} = convert(a, b)
|
||||
new(a.real - b.real, a.imag - b.imag)
|
||||
end
|
||||
|
||||
def mul(a, b) do
|
||||
{a, b} = convert(a, b)
|
||||
new(a.real*b.real - a.imag*b.imag, a.imag*b.real + a.real*b.imag)
|
||||
end
|
||||
|
||||
def div(a, b) do
|
||||
{a, b} = convert(a, b)
|
||||
divisor = abs2(b)
|
||||
new((a.real*b.real + a.imag*b.imag) / divisor,
|
||||
(a.imag*b.real - a.real*b.imag) / divisor)
|
||||
end
|
||||
|
||||
def neg(a) do
|
||||
a = convert(a)
|
||||
new(-a.real, -a.imag)
|
||||
end
|
||||
|
||||
def inv(a) do
|
||||
a = convert(a)
|
||||
divisor = abs2(a)
|
||||
new(a.real / divisor, -a.imag / divisor)
|
||||
end
|
||||
|
||||
def conj(a) do
|
||||
a = convert(a)
|
||||
new(a.real, -a.imag)
|
||||
end
|
||||
|
||||
def abs(a) do
|
||||
:math.sqrt(abs2(a))
|
||||
end
|
||||
|
||||
defp abs2(a) do
|
||||
a = convert(a)
|
||||
a.real*a.real + a.imag*a.imag
|
||||
end
|
||||
|
||||
defp convert(a) when is_number(a), do: new(a, 0)
|
||||
defp convert(%__MODULE__{} = a), do: a
|
||||
|
||||
defp convert(a, b), do: {convert(a), convert(b)}
|
||||
|
||||
def task do
|
||||
a = new(1, 3)
|
||||
b = new(5, 2)
|
||||
IO.puts "a = #{a}"
|
||||
IO.puts "b = #{b}"
|
||||
IO.puts "add(a,b): #{add(a, b)}"
|
||||
IO.puts "sub(a,b): #{sub(a, b)}"
|
||||
IO.puts "mul(a,b): #{mul(a, b)}"
|
||||
IO.puts "div(a,b): #{div(a, b)}"
|
||||
IO.puts "div(b,a): #{div(b, a)}"
|
||||
IO.puts "neg(a) : #{neg(a)}"
|
||||
IO.puts "inv(a) : #{inv(a)}"
|
||||
IO.puts "conj(a) : #{conj(a)}"
|
||||
end
|
||||
end
|
||||
|
||||
defimpl String.Chars, for: Complex do
|
||||
def to_string(%Complex{real: real, imag: imag}) do
|
||||
if imag >= 0, do: "#{real}+#{imag}j",
|
||||
else: "#{real}#{imag}j"
|
||||
end
|
||||
end
|
||||
|
||||
Complex.task
|
||||
|
|
@ -1,10 +1,12 @@
|
|||
x=: 1j1
|
||||
y=: 3.14159j1.2
|
||||
x+y
|
||||
x+y NB. addition
|
||||
4.14159j2.2
|
||||
x*y
|
||||
x*y NB. multiplication
|
||||
1.94159j4.34159
|
||||
%x
|
||||
%x NB. inversion
|
||||
0.5j_0.5
|
||||
-x
|
||||
-x NB. negation
|
||||
_1j_1
|
||||
+x NB. (complex) conjugation
|
||||
1j_1
|
||||
|
|
|
|||
|
|
@ -1,44 +1,52 @@
|
|||
public class Complex{
|
||||
public final double real;
|
||||
public final double imag;
|
||||
public class Complex {
|
||||
public final double real;
|
||||
public final double imag;
|
||||
|
||||
public Complex(){this(0,0)}//default values to 0...force of habit
|
||||
public Complex(double r, double i){real = r; imag = i;}
|
||||
public Complex() {
|
||||
this(0, 0);
|
||||
}
|
||||
|
||||
public Complex add(Complex b){
|
||||
return new Complex(this.real + b.real, this.imag + b.imag);
|
||||
}
|
||||
public Complex(double r, double i) {
|
||||
real = r;
|
||||
imag = i;
|
||||
}
|
||||
|
||||
public Complex mult(Complex b){
|
||||
//FOIL of (a+bi)(c+di) with i*i = -1
|
||||
return new Complex(this.real * b.real - this.imag * b.imag, this.real * b.imag + this.imag * b.real);
|
||||
}
|
||||
public Complex add(Complex b) {
|
||||
return new Complex(this.real + b.real, this.imag + b.imag);
|
||||
}
|
||||
|
||||
public Complex inv(){
|
||||
//1/(a+bi) * (a-bi)/(a-bi) = 1/(a+bi) but it's more workable
|
||||
double denom = real * real + imag * imag;
|
||||
return new Complex(real/denom,-imag/denom);
|
||||
}
|
||||
public Complex mult(Complex b) {
|
||||
// FOIL of (a+bi)(c+di) with i*i = -1
|
||||
return new Complex(this.real * b.real - this.imag * b.imag,
|
||||
this.real * b.imag + this.imag * b.real);
|
||||
}
|
||||
|
||||
public Complex neg(){
|
||||
return new Complex(-real, -imag);
|
||||
}
|
||||
public Complex inv() {
|
||||
// 1/(a+bi) * (a-bi)/(a-bi) = 1/(a+bi) but it's more workable
|
||||
double denom = real * real + imag * imag;
|
||||
return new Complex(real / denom, -imag / denom);
|
||||
}
|
||||
|
||||
public Complex conj(){
|
||||
return new Complex(real, -imag);
|
||||
}
|
||||
public Complex neg() {
|
||||
return new Complex(-real, -imag);
|
||||
}
|
||||
|
||||
public String toString(){ //override Object's toString
|
||||
return real + " + " + imag + " * i";
|
||||
}
|
||||
public Complex conj() {
|
||||
return new Complex(real, -imag);
|
||||
}
|
||||
|
||||
public static void main(String[] args){
|
||||
Complex a = new Complex(Math.PI, -5) //just some numbers
|
||||
Complex b = new Complex(-1, 2.5);
|
||||
System.out.println(a.neg());
|
||||
System.out.println(a.add(b));
|
||||
System.out.println(a.inv());
|
||||
System.out.println(a.mult(b));
|
||||
System.out.println(a.conj());
|
||||
}
|
||||
@Override
|
||||
public String toString() {
|
||||
return real + " + " + imag + " * i";
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
Complex a = new Complex(Math.PI, -5); //just some numbers
|
||||
Complex b = new Complex(-1, 2.5);
|
||||
System.out.println(a.neg());
|
||||
System.out.println(a.add(b));
|
||||
System.out.println(a.inv());
|
||||
System.out.println(a.mult(b));
|
||||
System.out.println(a.conj());
|
||||
}
|
||||
}
|
||||
|
|
|
|||
39
Task/Arithmetic-Complex/PowerShell/arithmetic-complex-1.psh
Normal file
39
Task/Arithmetic-Complex/PowerShell/arithmetic-complex-1.psh
Normal file
|
|
@ -0,0 +1,39 @@
|
|||
class Complex {
|
||||
[Double]$x
|
||||
[Double]$y
|
||||
Complex() {
|
||||
$this.x = 0
|
||||
$this.y = 0
|
||||
}
|
||||
Complex([Double]$x, [Double]$y) {
|
||||
$this.x = $x
|
||||
$this.y = $y
|
||||
}
|
||||
[Double]abs2() {return $this.x*$this.x + $this.y*$this.y}
|
||||
[Double]abs() {return [math]::sqrt($this.abs2())}
|
||||
static [Complex]add([Complex]$m,[Complex]$n) {return [Complex]::new($m.x+$n.x, $m.y+$n.y)}
|
||||
static [Complex]mul([Complex]$m,[Complex]$n) {return [Complex]::new($m.x*$n.x - $m.y*$n.y, $m.x*$n.y + $n.x*$m.y)}
|
||||
[Complex]mul([Double]$k) {return [Complex]::new($k*$this.x, $k*$this.y)}
|
||||
[Complex]negate() {return $this.mul(-1)}
|
||||
[Complex]conjugate() {return [Complex]::new($this.x, -$this.y)}
|
||||
[Complex]inverse() {return $this.conjugate().mul(1/$this.abs2())}
|
||||
[String]show() {
|
||||
if(0 -ge $this.y) {
|
||||
return "$($this.x)+$($this.y)i"
|
||||
} else {
|
||||
return "$($this.x)$($this.y)i"
|
||||
}
|
||||
}
|
||||
static [String]show([Complex]$other) {
|
||||
return $other.show()
|
||||
}
|
||||
}
|
||||
$m = [complex]::new(3, 4)
|
||||
$n = [complex]::new(7, 6)
|
||||
"`$m: $($m.show())"
|
||||
"`$n: $($n.show())"
|
||||
"`$m + `$n: $([complex]::show([complex]::add($m,$n)))"
|
||||
"`$m * `$n: $([complex]::show([complex]::mul($m,$n)))"
|
||||
"negate `$m: $($m.negate().show())"
|
||||
"1/`$m: $([complex]::show($m.inverse()))"
|
||||
"conjugate `$m: $([complex]::show($m.conjugate()))"
|
||||
16
Task/Arithmetic-Complex/PowerShell/arithmetic-complex-2.psh
Normal file
16
Task/Arithmetic-Complex/PowerShell/arithmetic-complex-2.psh
Normal file
|
|
@ -0,0 +1,16 @@
|
|||
function show([System.Numerics.Complex]$c) {
|
||||
if(0 -ge $c.Imginary) {
|
||||
return "$($c.Real)+$($c.Imaginary)i"
|
||||
} else {
|
||||
return "$($c.Real)$($c.Imaginary)i"
|
||||
}
|
||||
}
|
||||
$m = [System.Numerics.Complex]::new(3, 4)
|
||||
$n = [System.Numerics.Complex]::new(7, 6)
|
||||
"`$m: $(show $m)"
|
||||
"`$n: $(show $n)"
|
||||
"`$m + `$n: $(show ([System.Numerics.Complex]::Add($m,$n)))"
|
||||
"`$m * `$n: $(show ([System.Numerics.Complex]::Multiply($m,$n)))"
|
||||
"negate `$m: $(show ([System.Numerics.Complex]::Negate($m)))"
|
||||
"1/`$m: $(show ([System.Numerics.Complex]::Reciprocal($m)))"
|
||||
"conjugate `$m: $(show ([System.Numerics.Complex]::Conjugate($m)))"
|
||||
|
|
@ -1,23 +1,23 @@
|
|||
/*REXX pgm 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 " " " " " " " */
|
||||
/*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 " " " " " " " */
|
||||
|
||||
say ' addition: ' x " + " y ' = ' Cadd(x,y)
|
||||
say ' subtraction: ' x " - " y ' = ' Csub(x,y)
|
||||
say 'multiplication: ' x " * " y ' = ' Cmul(x,y)
|
||||
say ' division: ' x " ÷ " y ' = ' Cdiv(x,y)
|
||||
say ' inverse: ' x " = " Cinv(x,y)
|
||||
say ' conjugate of: ' x " = " Conj(x,y)
|
||||
say ' negation of: ' x " = " Cneg(x,y)
|
||||
exit /*stick a fork in it, we're all done. */
|
||||
/*──────────────────────────────────one─liner subroutines─────────────────────*/
|
||||
Conj: procedure; arg a ',' b,c ',' d; call C#; return C$( a, -b)
|
||||
Cadd: procedure; arg a ',' b,c ',' d; call C#; return C$(a+c, b+d)
|
||||
Csub: procedure; arg a ',' b,c ',' d; call C#; return C$(a-c, b-d)
|
||||
Cmul: procedure; arg a ',' b,c ',' d; call C#; return C$(ac-bd, bc+ad)
|
||||
Cdiv: procedure; arg a ',' b,c ',' d; call C#; return C$((ac+bd)/s, (bc-ad)/s)
|
||||
say ' addition: ' x " + " y ' = ' Cadd(x, y)
|
||||
say ' subtraction: ' x " - " y ' = ' Csub(x, y)
|
||||
say 'multiplication: ' x " * " y ' = ' Cmul(x, y)
|
||||
say ' division: ' x " ÷ " y ' = ' Cdiv(x, y)
|
||||
say ' inverse: ' x " = " Cinv(x, y)
|
||||
say ' conjugate of: ' x " = " Conj(x, y)
|
||||
say ' negation of: ' x " = " Cneg(x, y)
|
||||
exit /*stick a fork in it, we're all done. */
|
||||
/*──────────────────────────────────────────────────────────────────────────────────────*/
|
||||
Conj: procedure; parse arg a ',' b,c ',' d; call C#; return C$( a , -b )
|
||||
Cadd: procedure; parse arg a ',' b,c ',' d; call C#; return C$( a+c , b+d )
|
||||
Csub: procedure; parse arg a ',' b,c ',' d; call C#; return C$( a-c , b-d )
|
||||
Cmul: procedure; parse arg a ',' b,c ',' d; call C#; return C$( ac-bd , bc+ad)
|
||||
Cdiv: procedure; parse arg a ',' b,c ',' d; call C#; return C$((ac+bd)/s, (bc-ad)/s)
|
||||
Cinv: return Cdiv(1, arg(1))
|
||||
Cneg: return Cmul(arg(1), -1)
|
||||
C_: arg __; return word(translate(__, , '{[(JI)]}') 0, 1) /*get # or 0*/
|
||||
C#: a=C_(a);b=C_(b);c=C_(c);d=C_(d);ac=a*c;ad=a*d;bc=b*c;bd=b*d;s=c*c+d*d;return
|
||||
C$: parse arg r,c;_='['r; if c\=0 then _=_','c"j"; return _']' /*uses j*/
|
||||
C_: return word(translate(arg(1), , '{[(JjIi)]}') 0, 1) /*get # or 0*/
|
||||
C#: a=C_(a); b=C_(b); c=C_(c); d=C_(d); ac=a*c; ad=a*d; bc=b*c; bd=b*d;s=c*c+d*d; return
|
||||
C$: parse arg r,c; _='['r; if c\=0 then _=_","c'j'; return _"]" /*uses j */
|
||||
|
|
|
|||
|
|
@ -0,0 +1,23 @@
|
|||
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
|
||||
30 DIM o(complex)
|
||||
40 REM add
|
||||
50 LET o(r)=a(r)+b(r)
|
||||
60 LET o(i)=a(i)+b(i)
|
||||
70 PRINT "Result of addition is:": GO SUB 1000
|
||||
80 REM mult
|
||||
90 LET o(r)=a(r)*b(r)-a(i)*b(i)
|
||||
100 LET o(i)=a(i)*b(r)+a(r)*b(i)
|
||||
110 PRINT "Result of multiplication is:": GO SUB 1000
|
||||
120 REM neg
|
||||
130 LET o(r)=-a(r)
|
||||
140 LET o(i)=-a(i)
|
||||
150 PRINT "Result of negation is:": GO SUB 1000
|
||||
160 LET denom=a(r)^2+a(i)^2
|
||||
170 LET o(r)=a(r)/denom
|
||||
180 LET o(i)=-a(i)/denom
|
||||
190 PRINT "Result of inversion is:": GO SUB 1000
|
||||
200 STOP
|
||||
1000 IF o(i)>=0 THEN PRINT o(r);" + ";o(i);"i": RETURN
|
||||
1010 PRINT o(r);" - ";-o(i);"i": RETURN
|
||||
Loading…
Add table
Add a link
Reference in a new issue