CDE
This commit is contained in:
parent
518da4a923
commit
764da6cbbb
6144 changed files with 83610 additions and 11 deletions
19
Task/Arithmetic-Complex/C++/arithmetic-complex.cpp
Normal file
19
Task/Arithmetic-Complex/C++/arithmetic-complex.cpp
Normal file
|
|
@ -0,0 +1,19 @@
|
|||
#include <iostream>
|
||||
#include <complex>
|
||||
using std::complex;
|
||||
|
||||
void complex_operations() {
|
||||
complex<double> a(1.0, 1.0);
|
||||
complex<double> b(3.14159, 1.25);
|
||||
|
||||
// addition
|
||||
std::cout << a + b << std::endl;
|
||||
// multiplication
|
||||
std::cout << a * b << std::endl;
|
||||
// inversion
|
||||
std::cout << 1.0 / a << std::endl;
|
||||
// negation
|
||||
std::cout << -a << std::endl;
|
||||
// conjugate
|
||||
std::cout << std::conj(a) << std::endl;
|
||||
}
|
||||
17
Task/Arithmetic-Complex/C-sharp/arithmetic-complex-1.cs
Normal file
17
Task/Arithmetic-Complex/C-sharp/arithmetic-complex-1.cs
Normal file
|
|
@ -0,0 +1,17 @@
|
|||
namespace RosettaCode.Arithmetic.Complex
|
||||
{
|
||||
using System;
|
||||
using System.Numerics;
|
||||
|
||||
internal static class Program
|
||||
{
|
||||
private static void Main()
|
||||
{
|
||||
var number = Complex.ImaginaryOne;
|
||||
foreach (var result in new[] { number + number, number * number, -number, 1 / number, Complex.Conjugate(number) })
|
||||
{
|
||||
Console.WriteLine(result);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
205
Task/Arithmetic-Complex/C-sharp/arithmetic-complex-2.cs
Normal file
205
Task/Arithmetic-Complex/C-sharp/arithmetic-complex-2.cs
Normal file
|
|
@ -0,0 +1,205 @@
|
|||
using System;
|
||||
|
||||
public struct ComplexNumber
|
||||
{
|
||||
public static readonly ComplexNumber i = new ComplexNumber(0.0, 1.0);
|
||||
public static readonly ComplexNumber Zero = new ComplexNumber(0.0, 0.0);
|
||||
|
||||
public double Re;
|
||||
public double Im;
|
||||
|
||||
public ComplexNumber(double re)
|
||||
{
|
||||
this.Re = re;
|
||||
this.Im = 0;
|
||||
}
|
||||
|
||||
public ComplexNumber(double re, double im)
|
||||
{
|
||||
this.Re = re;
|
||||
this.Im = im;
|
||||
}
|
||||
|
||||
public static ComplexNumber operator *(ComplexNumber n1, ComplexNumber n2)
|
||||
{
|
||||
return new ComplexNumber(n1.Re * n2.Re - n1.Im * n2.Im,
|
||||
n1.Im * n2.Re + n1.Re * n2.Im);
|
||||
}
|
||||
|
||||
public static ComplexNumber operator *(double n1, ComplexNumber n2)
|
||||
{
|
||||
return new ComplexNumber(n1 * n2.Re, n1 * n2.Im);
|
||||
}
|
||||
|
||||
public static ComplexNumber operator /(ComplexNumber n1, ComplexNumber n2)
|
||||
{
|
||||
double n2Norm = n2.Re * n2.Re + n2.Im * n2.Im;
|
||||
return new ComplexNumber((n1.Re * n2.Re + n1.Im * n2.Im) / n2Norm,
|
||||
(n1.Im * n2.Re - n1.Re * n2.Im) / n2Norm);
|
||||
}
|
||||
|
||||
public static ComplexNumber operator /(ComplexNumber n1, double n2)
|
||||
{
|
||||
return new ComplexNumber(n1.Re / n2, n1.Im / n2);
|
||||
}
|
||||
|
||||
public static ComplexNumber operator +(ComplexNumber n1, ComplexNumber n2)
|
||||
{
|
||||
return new ComplexNumber(n1.Re + n2.Re, n1.Im + n2.Im);
|
||||
}
|
||||
|
||||
public static ComplexNumber operator -(ComplexNumber n1, ComplexNumber n2)
|
||||
{
|
||||
return new ComplexNumber(n1.Re - n2.Re, n1.Im - n2.Im);
|
||||
}
|
||||
|
||||
public static ComplexNumber operator -(ComplexNumber n)
|
||||
{
|
||||
return new ComplexNumber(-n.Re, -n.Im);
|
||||
}
|
||||
|
||||
public static implicit operator ComplexNumber(double n)
|
||||
{
|
||||
return new ComplexNumber(n, 0.0);
|
||||
}
|
||||
|
||||
public static explicit operator double(ComplexNumber n)
|
||||
{
|
||||
return n.Re;
|
||||
}
|
||||
|
||||
public static bool operator ==(ComplexNumber n1, ComplexNumber n2)
|
||||
{
|
||||
return n1.Re == n2.Re && n1.Im == n2.Im;
|
||||
}
|
||||
|
||||
public static bool operator !=(ComplexNumber n1, ComplexNumber n2)
|
||||
{
|
||||
return n1.Re != n2.Re || n1.Im != n2.Im;
|
||||
}
|
||||
|
||||
public override bool Equals(object obj)
|
||||
{
|
||||
return this == (ComplexNumber)obj;
|
||||
}
|
||||
|
||||
public override int GetHashCode()
|
||||
{
|
||||
return Re.GetHashCode() ^ Im.GetHashCode();
|
||||
}
|
||||
|
||||
public override string ToString()
|
||||
{
|
||||
return String.Format("{0}+{1}*i", Re, Im);
|
||||
}
|
||||
}
|
||||
|
||||
public static class ComplexMath
|
||||
{
|
||||
public static double Abs(ComplexNumber a)
|
||||
{
|
||||
return Math.Sqrt(Norm(a));
|
||||
}
|
||||
|
||||
public static double Norm(ComplexNumber a)
|
||||
{
|
||||
return a.Re * a.Re + a.Im * a.Im;
|
||||
}
|
||||
|
||||
public static double Arg(ComplexNumber a)
|
||||
{
|
||||
return Math.Atan2(a.Im, a.Re);
|
||||
}
|
||||
|
||||
public static ComplexNumber Inverse(ComplexNumber a)
|
||||
{
|
||||
double norm = Norm(a);
|
||||
return new ComplexNumber(a.Re / norm, -a.Im / norm);
|
||||
}
|
||||
|
||||
public static ComplexNumber Conjugate(ComplexNumber a)
|
||||
{
|
||||
return new ComplexNumber(a.Re, -a.Im);
|
||||
|
||||
}
|
||||
|
||||
public static ComplexNumber Exp(ComplexNumber a)
|
||||
{
|
||||
double e = Math.Exp(a.Re);
|
||||
return new ComplexNumber(e * Math.Cos(a.Im), e * Math.Sin(a.Im));
|
||||
}
|
||||
|
||||
public static ComplexNumber Log(ComplexNumber a)
|
||||
{
|
||||
|
||||
return new ComplexNumber(0.5 * Math.Log(Norm(a)), Arg(a));
|
||||
}
|
||||
|
||||
public static ComplexNumber Power(ComplexNumber a, ComplexNumber power)
|
||||
{
|
||||
return Exp(power * Log(a));
|
||||
}
|
||||
|
||||
public static ComplexNumber Power(ComplexNumber a, int power)
|
||||
{
|
||||
bool inverse = false;
|
||||
if (power < 0)
|
||||
{
|
||||
inverse = true; power = -power;
|
||||
}
|
||||
|
||||
ComplexNumber result = 1.0;
|
||||
ComplexNumber multiplier = a;
|
||||
while (power > 0)
|
||||
{
|
||||
if ((power & 1) != 0) result *= multiplier;
|
||||
multiplier *= multiplier;
|
||||
power >>= 1;
|
||||
}
|
||||
|
||||
if (inverse)
|
||||
return Inverse(result);
|
||||
else
|
||||
return result;
|
||||
}
|
||||
|
||||
public static ComplexNumber Sqrt(ComplexNumber a)
|
||||
{
|
||||
return Exp(0.5 * Log(a));
|
||||
}
|
||||
|
||||
public static ComplexNumber Sin(ComplexNumber a)
|
||||
{
|
||||
return Sinh(ComplexNumber.i * a) / ComplexNumber.i;
|
||||
}
|
||||
|
||||
public static ComplexNumber Cos(ComplexNumber a)
|
||||
{
|
||||
return Cosh(ComplexNumber.i * a);
|
||||
}
|
||||
|
||||
public static ComplexNumber Sinh(ComplexNumber a)
|
||||
{
|
||||
return 0.5 * (Exp(a) - Exp(-a));
|
||||
}
|
||||
|
||||
public static ComplexNumber Cosh(ComplexNumber a)
|
||||
{
|
||||
return 0.5 * (Exp(a) + Exp(-a));
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
class Program
|
||||
{
|
||||
static void Main(string[] args)
|
||||
{
|
||||
// usage
|
||||
ComplexNumber i = 2;
|
||||
ComplexNumber j = new ComplexNumber(1, -2);
|
||||
Console.WriteLine(i * j);
|
||||
Console.WriteLine(ComplexMath.Power(j, 2));
|
||||
Console.WriteLine((double)ComplexMath.Sin(i) + " vs " + Math.Sin(2));
|
||||
Console.WriteLine(ComplexMath.Power(j, 0) == 1.0);
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,5 @@
|
|||
> (sqrt -1)
|
||||
#C(0.0 1.0)
|
||||
|
||||
> (expt #c(0 1) 2)
|
||||
-1
|
||||
|
|
@ -0,0 +1,17 @@
|
|||
> (+ #c(0 1) #c(1 0))
|
||||
#C(1 1)
|
||||
|
||||
> (* #c(1 1) 2)
|
||||
#C(2 2)
|
||||
|
||||
> (* #c(1 1) #c(0 2))
|
||||
#C(-2 2)
|
||||
|
||||
> (- #c(1 1))
|
||||
#C(-1 -1)
|
||||
|
||||
> (/ #c(0 2))
|
||||
#C(0 -1/2)
|
||||
|
||||
> (conjugate #c(1 1))
|
||||
#C(1 -1)
|
||||
|
|
@ -0,0 +1,8 @@
|
|||
> (complex 64 (/ 3 4))
|
||||
#C(64 3/4)
|
||||
|
||||
> (realpart #c(5 5))
|
||||
5
|
||||
|
||||
> (imagpart (complex 0 pi))
|
||||
3.141592653589793d0
|
||||
11
Task/Arithmetic-Complex/D/arithmetic-complex.d
Normal file
11
Task/Arithmetic-Complex/D/arithmetic-complex.d
Normal file
|
|
@ -0,0 +1,11 @@
|
|||
import std.stdio, std.complex;
|
||||
|
||||
void main() {
|
||||
auto x = complex(1, 1); // complex of doubles on default
|
||||
auto y = complex(3.14159, 1.2);
|
||||
|
||||
writeln(x + y); // addition
|
||||
writeln(x * y); // multiplication
|
||||
writeln(1.0 / x); // inversion
|
||||
writeln(-x); // negation
|
||||
}
|
||||
|
|
@ -0,0 +1,13 @@
|
|||
>a=1+4i; b=5-3i;
|
||||
>a+b
|
||||
6+1i
|
||||
>a-b
|
||||
-4+7i
|
||||
>a*b
|
||||
17+17i
|
||||
>a/b
|
||||
-0.205882352941+0.676470588235i
|
||||
>fraction a/b
|
||||
-7/34+23/34i
|
||||
>conj(a)
|
||||
1-4i
|
||||
58
Task/Arithmetic-Complex/Euphoria/arithmetic-complex.euphoria
Normal file
58
Task/Arithmetic-Complex/Euphoria/arithmetic-complex.euphoria
Normal file
|
|
@ -0,0 +1,58 @@
|
|||
constant REAL = 1, IMAG = 2
|
||||
type complex(sequence s)
|
||||
return length(s) = 2 and atom(s[REAL]) and atom(s[IMAG])
|
||||
end type
|
||||
|
||||
function add(complex a, complex b)
|
||||
return a + b
|
||||
end function
|
||||
|
||||
function mult(complex a, complex b)
|
||||
return {a[REAL] * b[REAL] - a[IMAG] * b[IMAG],
|
||||
a[REAL] * b[IMAG] + a[IMAG] * b[REAL]}
|
||||
end function
|
||||
|
||||
function inv(complex a)
|
||||
atom denom
|
||||
denom = a[REAL] * a[REAL] + a[IMAG] * a[IMAG]
|
||||
return {a[REAL] / denom, -a[IMAG] / denom}
|
||||
end function
|
||||
|
||||
function neg(complex a)
|
||||
return -a
|
||||
end function
|
||||
|
||||
function scomplex(complex a)
|
||||
sequence s
|
||||
if a[REAL] != 0 then
|
||||
s = sprintf("%g",a)
|
||||
else
|
||||
s = {}
|
||||
end if
|
||||
|
||||
if a[IMAG] != 0 then
|
||||
if a[IMAG] = 1 then
|
||||
s &= "+i"
|
||||
elsif a[IMAG] = -1 then
|
||||
s &= "-i"
|
||||
else
|
||||
s &= sprintf("%+gi",a[IMAG])
|
||||
end if
|
||||
end if
|
||||
|
||||
if length(s) = 0 then
|
||||
return "0"
|
||||
else
|
||||
return s
|
||||
end if
|
||||
end function
|
||||
|
||||
complex a, b
|
||||
a = { 1.0, 1.0 }
|
||||
b = { 3.14159, 1.2 }
|
||||
printf(1,"a = %s\n",{scomplex(a)})
|
||||
printf(1,"b = %s\n",{scomplex(b)})
|
||||
printf(1,"a+b = %s\n",{scomplex(add(a,b))})
|
||||
printf(1,"a*b = %s\n",{scomplex(mult(a,b))})
|
||||
printf(1,"1/a = %s\n",{scomplex(inv(a))})
|
||||
printf(1,"-a = %s\n",{scomplex(neg(a))})
|
||||
Loading…
Add table
Add a link
Reference in a new issue