Instruction stringlengths 45 106 | input_code stringlengths 1 13.7k | output_code stringlengths 1 13.7k |
|---|---|---|
Port the provided F# code into Python while preserving the original functionality. | > open Microsoft.FSharp.Math;;
> let a = complex 1.0 1.0;;
val a : complex = 1r+1i
> let b = complex 3.14159 1.25;;
val b : complex = 3.14159r+1.25i
> a + b;;
val it : Complex = 4.14159r+2.25i {Conjugate = 4.14159r-2.25i;
ImaginaryPart = 2.25;
Magnitude = 4.713307515;
Phase = 0.497661247;
RealPart = 4.14159;
i = 2.25;
r = 4.14159;}
> a * b;;
val it : Complex = 1.89159r+4.39159i {Conjugate = 1.89159r-4.39159i;
ImaginaryPart = 4.39159;
Magnitude = 4.781649868;
Phase = 1.164082262;
RealPart = 1.89159;
i = 4.39159;
r = 1.89159;}
> a / b;;
val it : Complex =
0.384145932435901r+0.165463215905043i
{Conjugate = 0.384145932435901r-0.165463215905043i;
ImaginaryPart = 0.1654632159;
Magnitude = 0.418265673;
Phase = 0.4067140652;
RealPart = 0.3841459324;
i = 0.1654632159;
r = 0.3841459324;}
> -a;;
val it : complex = -1r-1i {Conjugate = -1r+1i;
ImaginaryPart = -1.0;
Magnitude = 1.414213562;
Phase = -2.35619449;
RealPart = -1.0;
i = -1.0;
r = -1.0;}
| >>> z1 = 1.5 + 3j
>>> z2 = 1.5 + 1.5j
>>> z1 + z2
(3+4.5j)
>>> z1 - z2
1.5j
>>> z1 * z2
(-2.25+6.75j)
>>> z1 / z2
(1.5+0.5j)
>>> - z1
(-1.5-3j)
>>> z1.conjugate()
(1.5-3j)
>>> abs(z1)
3.3541019662496847
>>> z1 ** z2
(-1.1024829553277784-0.38306415117199333j)
>>> z1.real
1.5
>>> z1.imag
3.0
>>>
|
Keep all operations the same but rewrite the snippet in Go. | > open Microsoft.FSharp.Math;;
> let a = complex 1.0 1.0;;
val a : complex = 1r+1i
> let b = complex 3.14159 1.25;;
val b : complex = 3.14159r+1.25i
> a + b;;
val it : Complex = 4.14159r+2.25i {Conjugate = 4.14159r-2.25i;
ImaginaryPart = 2.25;
Magnitude = 4.713307515;
Phase = 0.497661247;
RealPart = 4.14159;
i = 2.25;
r = 4.14159;}
> a * b;;
val it : Complex = 1.89159r+4.39159i {Conjugate = 1.89159r-4.39159i;
ImaginaryPart = 4.39159;
Magnitude = 4.781649868;
Phase = 1.164082262;
RealPart = 1.89159;
i = 4.39159;
r = 1.89159;}
> a / b;;
val it : Complex =
0.384145932435901r+0.165463215905043i
{Conjugate = 0.384145932435901r-0.165463215905043i;
ImaginaryPart = 0.1654632159;
Magnitude = 0.418265673;
Phase = 0.4067140652;
RealPart = 0.3841459324;
i = 0.1654632159;
r = 0.3841459324;}
> -a;;
val it : complex = -1r-1i {Conjugate = -1r+1i;
ImaginaryPart = -1.0;
Magnitude = 1.414213562;
Phase = -2.35619449;
RealPart = -1.0;
i = -1.0;
r = -1.0;}
| package main
import (
"fmt"
"math/cmplx"
)
func main() {
a := 1 + 1i
b := 3.14159 + 1.25i
fmt.Println("a: ", a)
fmt.Println("b: ", b)
fmt.Println("a + b: ", a+b)
fmt.Println("a * b: ", a*b)
fmt.Println("-a: ", -a)
fmt.Println("1 / a: ", 1/a)
fmt.Println("a̅: ", cmplx.Conj(a))
}
|
Port the provided Factor code into C while preserving the original functionality. | USING: combinators kernel math math.functions prettyprint ;
C{ 1 2 } C{ 0.9 -2.78 } {
[ + . ]
[ - . ]
[ * . ]
[ / . ]
[ ^ . ]
} 2cleave
C{ 1 2 } {
[ neg . ]
[ recip . ]
[ conjugate . ]
[ sin . ]
[ log . ]
[ sqrt . ]
} cleave
| #include <complex.h>
#include <stdio.h>
void cprint(double complex c)
{
printf("%f%+fI", creal(c), cimag(c));
}
void complex_operations() {
double complex a = 1.0 + 1.0I;
double complex b = 3.14159 + 1.2I;
double complex c;
printf("\na="); cprint(a);
printf("\nb="); cprint(b);
c = a + b;
printf("\na+b="); cprint(c);
c = a * b;
printf("\na*b="); cprint(c);
c = 1.0 / a;
printf("\n1/c="); cprint(c);
c = -a;
printf("\n-a="); cprint(c);
c = conj(a);
printf("\nconj a="); cprint(c); printf("\n");
}
|
Produce a functionally identical C# code for the snippet given in Factor. | USING: combinators kernel math math.functions prettyprint ;
C{ 1 2 } C{ 0.9 -2.78 } {
[ + . ]
[ - . ]
[ * . ]
[ / . ]
[ ^ . ]
} 2cleave
C{ 1 2 } {
[ neg . ]
[ recip . ]
[ conjugate . ]
[ sin . ]
[ log . ]
[ sqrt . ]
} cleave
| 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);
}
}
}
}
|
Can you help me rewrite this code in C++ instead of Factor, keeping it the same logically? | USING: combinators kernel math math.functions prettyprint ;
C{ 1 2 } C{ 0.9 -2.78 } {
[ + . ]
[ - . ]
[ * . ]
[ / . ]
[ ^ . ]
} 2cleave
C{ 1 2 } {
[ neg . ]
[ recip . ]
[ conjugate . ]
[ sin . ]
[ log . ]
[ sqrt . ]
} cleave
| #include <iostream>
#include <complex>
using std::complex;
void complex_operations() {
complex<double> a(1.0, 1.0);
complex<double> b(3.14159, 1.25);
std::cout << a + b << std::endl;
std::cout << a * b << std::endl;
std::cout << 1.0 / a << std::endl;
std::cout << -a << std::endl;
std::cout << std::conj(a) << std::endl;
}
|
Write the same code in Java as shown below in Factor. | USING: combinators kernel math math.functions prettyprint ;
C{ 1 2 } C{ 0.9 -2.78 } {
[ + . ]
[ - . ]
[ * . ]
[ / . ]
[ ^ . ]
} 2cleave
C{ 1 2 } {
[ neg . ]
[ recip . ]
[ conjugate . ]
[ sin . ]
[ log . ]
[ sqrt . ]
} cleave
| public class Complex {
public final double real;
public final double imag;
public Complex() {
this(0, 0);
}
public Complex(double r, double i) {
real = r;
imag = i;
}
public Complex add(Complex b) {
return new Complex(this.real + b.real, this.imag + b.imag);
}
public Complex mult(Complex b) {
return new Complex(this.real * b.real - this.imag * b.imag,
this.real * b.imag + this.imag * b.real);
}
public Complex inv() {
double denom = real * real + imag * imag;
return new Complex(real / denom, -imag / denom);
}
public Complex neg() {
return new Complex(-real, -imag);
}
public Complex conj() {
return new Complex(real, -imag);
}
@Override
public String toString() {
return real + " + " + imag + " * i";
}
public static void main(String[] args) {
Complex a = new Complex(Math.PI, -5);
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());
}
}
|
Transform the following Factor implementation into Python, maintaining the same output and logic. | USING: combinators kernel math math.functions prettyprint ;
C{ 1 2 } C{ 0.9 -2.78 } {
[ + . ]
[ - . ]
[ * . ]
[ / . ]
[ ^ . ]
} 2cleave
C{ 1 2 } {
[ neg . ]
[ recip . ]
[ conjugate . ]
[ sin . ]
[ log . ]
[ sqrt . ]
} cleave
| >>> z1 = 1.5 + 3j
>>> z2 = 1.5 + 1.5j
>>> z1 + z2
(3+4.5j)
>>> z1 - z2
1.5j
>>> z1 * z2
(-2.25+6.75j)
>>> z1 / z2
(1.5+0.5j)
>>> - z1
(-1.5-3j)
>>> z1.conjugate()
(1.5-3j)
>>> abs(z1)
3.3541019662496847
>>> z1 ** z2
(-1.1024829553277784-0.38306415117199333j)
>>> z1.real
1.5
>>> z1.imag
3.0
>>>
|
Generate a Go translation of this Factor snippet without changing its computational steps. | USING: combinators kernel math math.functions prettyprint ;
C{ 1 2 } C{ 0.9 -2.78 } {
[ + . ]
[ - . ]
[ * . ]
[ / . ]
[ ^ . ]
} 2cleave
C{ 1 2 } {
[ neg . ]
[ recip . ]
[ conjugate . ]
[ sin . ]
[ log . ]
[ sqrt . ]
} cleave
| package main
import (
"fmt"
"math/cmplx"
)
func main() {
a := 1 + 1i
b := 3.14159 + 1.25i
fmt.Println("a: ", a)
fmt.Println("b: ", b)
fmt.Println("a + b: ", a+b)
fmt.Println("a * b: ", a*b)
fmt.Println("-a: ", -a)
fmt.Println("1 / a: ", 1/a)
fmt.Println("a̅: ", cmplx.Conj(a))
}
|
Transform the following Forth implementation into C, maintaining the same output and logic. | S" fsl-util.fs" REQUIRED
S" complex.fs" REQUIRED
zvariable x
zvariable y
1e 1e x z!
pi 1.2e y z!
x z@ y z@ z+ z.
x z@ y z@ z* z.
1e 0e zconstant 1+0i
1+0i x z@ z/ z.
x z@ znegate z.
| #include <complex.h>
#include <stdio.h>
void cprint(double complex c)
{
printf("%f%+fI", creal(c), cimag(c));
}
void complex_operations() {
double complex a = 1.0 + 1.0I;
double complex b = 3.14159 + 1.2I;
double complex c;
printf("\na="); cprint(a);
printf("\nb="); cprint(b);
c = a + b;
printf("\na+b="); cprint(c);
c = a * b;
printf("\na*b="); cprint(c);
c = 1.0 / a;
printf("\n1/c="); cprint(c);
c = -a;
printf("\n-a="); cprint(c);
c = conj(a);
printf("\nconj a="); cprint(c); printf("\n");
}
|
Keep all operations the same but rewrite the snippet in C#. | S" fsl-util.fs" REQUIRED
S" complex.fs" REQUIRED
zvariable x
zvariable y
1e 1e x z!
pi 1.2e y z!
x z@ y z@ z+ z.
x z@ y z@ z* z.
1e 0e zconstant 1+0i
1+0i x z@ z/ z.
x z@ znegate z.
| 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);
}
}
}
}
|
Write a version of this Forth function in C++ with identical behavior. | S" fsl-util.fs" REQUIRED
S" complex.fs" REQUIRED
zvariable x
zvariable y
1e 1e x z!
pi 1.2e y z!
x z@ y z@ z+ z.
x z@ y z@ z* z.
1e 0e zconstant 1+0i
1+0i x z@ z/ z.
x z@ znegate z.
| #include <iostream>
#include <complex>
using std::complex;
void complex_operations() {
complex<double> a(1.0, 1.0);
complex<double> b(3.14159, 1.25);
std::cout << a + b << std::endl;
std::cout << a * b << std::endl;
std::cout << 1.0 / a << std::endl;
std::cout << -a << std::endl;
std::cout << std::conj(a) << std::endl;
}
|
Generate an equivalent Java version of this Forth code. | S" fsl-util.fs" REQUIRED
S" complex.fs" REQUIRED
zvariable x
zvariable y
1e 1e x z!
pi 1.2e y z!
x z@ y z@ z+ z.
x z@ y z@ z* z.
1e 0e zconstant 1+0i
1+0i x z@ z/ z.
x z@ znegate z.
| public class Complex {
public final double real;
public final double imag;
public Complex() {
this(0, 0);
}
public Complex(double r, double i) {
real = r;
imag = i;
}
public Complex add(Complex b) {
return new Complex(this.real + b.real, this.imag + b.imag);
}
public Complex mult(Complex b) {
return new Complex(this.real * b.real - this.imag * b.imag,
this.real * b.imag + this.imag * b.real);
}
public Complex inv() {
double denom = real * real + imag * imag;
return new Complex(real / denom, -imag / denom);
}
public Complex neg() {
return new Complex(-real, -imag);
}
public Complex conj() {
return new Complex(real, -imag);
}
@Override
public String toString() {
return real + " + " + imag + " * i";
}
public static void main(String[] args) {
Complex a = new Complex(Math.PI, -5);
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());
}
}
|
Change the following Forth code into Python without altering its purpose. | S" fsl-util.fs" REQUIRED
S" complex.fs" REQUIRED
zvariable x
zvariable y
1e 1e x z!
pi 1.2e y z!
x z@ y z@ z+ z.
x z@ y z@ z* z.
1e 0e zconstant 1+0i
1+0i x z@ z/ z.
x z@ znegate z.
| >>> z1 = 1.5 + 3j
>>> z2 = 1.5 + 1.5j
>>> z1 + z2
(3+4.5j)
>>> z1 - z2
1.5j
>>> z1 * z2
(-2.25+6.75j)
>>> z1 / z2
(1.5+0.5j)
>>> - z1
(-1.5-3j)
>>> z1.conjugate()
(1.5-3j)
>>> abs(z1)
3.3541019662496847
>>> z1 ** z2
(-1.1024829553277784-0.38306415117199333j)
>>> z1.real
1.5
>>> z1.imag
3.0
>>>
|
Rewrite the snippet below in Go so it works the same as the original Forth code. | S" fsl-util.fs" REQUIRED
S" complex.fs" REQUIRED
zvariable x
zvariable y
1e 1e x z!
pi 1.2e y z!
x z@ y z@ z+ z.
x z@ y z@ z* z.
1e 0e zconstant 1+0i
1+0i x z@ z/ z.
x z@ znegate z.
| package main
import (
"fmt"
"math/cmplx"
)
func main() {
a := 1 + 1i
b := 3.14159 + 1.25i
fmt.Println("a: ", a)
fmt.Println("b: ", b)
fmt.Println("a + b: ", a+b)
fmt.Println("a * b: ", a*b)
fmt.Println("-a: ", -a)
fmt.Println("1 / a: ", 1/a)
fmt.Println("a̅: ", cmplx.Conj(a))
}
|
Preserve the algorithm and functionality while converting the code from Fortran to C#. | program cdemo
complex :: a = (5,3), b = (0.5, 6.0)
complex :: absum, abprod, aneg, ainv
absum = a + b
abprod = a * b
aneg = -a
ainv = 1.0 / a
end program cdemo
| 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);
}
}
}
}
|
Convert this Fortran snippet to C++ and keep its semantics consistent. | program cdemo
complex :: a = (5,3), b = (0.5, 6.0)
complex :: absum, abprod, aneg, ainv
absum = a + b
abprod = a * b
aneg = -a
ainv = 1.0 / a
end program cdemo
| #include <iostream>
#include <complex>
using std::complex;
void complex_operations() {
complex<double> a(1.0, 1.0);
complex<double> b(3.14159, 1.25);
std::cout << a + b << std::endl;
std::cout << a * b << std::endl;
std::cout << 1.0 / a << std::endl;
std::cout << -a << std::endl;
std::cout << std::conj(a) << std::endl;
}
|
Generate a C translation of this Fortran snippet without changing its computational steps. | program cdemo
complex :: a = (5,3), b = (0.5, 6.0)
complex :: absum, abprod, aneg, ainv
absum = a + b
abprod = a * b
aneg = -a
ainv = 1.0 / a
end program cdemo
| #include <complex.h>
#include <stdio.h>
void cprint(double complex c)
{
printf("%f%+fI", creal(c), cimag(c));
}
void complex_operations() {
double complex a = 1.0 + 1.0I;
double complex b = 3.14159 + 1.2I;
double complex c;
printf("\na="); cprint(a);
printf("\nb="); cprint(b);
c = a + b;
printf("\na+b="); cprint(c);
c = a * b;
printf("\na*b="); cprint(c);
c = 1.0 / a;
printf("\n1/c="); cprint(c);
c = -a;
printf("\n-a="); cprint(c);
c = conj(a);
printf("\nconj a="); cprint(c); printf("\n");
}
|
Produce a functionally identical Java code for the snippet given in Fortran. | program cdemo
complex :: a = (5,3), b = (0.5, 6.0)
complex :: absum, abprod, aneg, ainv
absum = a + b
abprod = a * b
aneg = -a
ainv = 1.0 / a
end program cdemo
| public class Complex {
public final double real;
public final double imag;
public Complex() {
this(0, 0);
}
public Complex(double r, double i) {
real = r;
imag = i;
}
public Complex add(Complex b) {
return new Complex(this.real + b.real, this.imag + b.imag);
}
public Complex mult(Complex b) {
return new Complex(this.real * b.real - this.imag * b.imag,
this.real * b.imag + this.imag * b.real);
}
public Complex inv() {
double denom = real * real + imag * imag;
return new Complex(real / denom, -imag / denom);
}
public Complex neg() {
return new Complex(-real, -imag);
}
public Complex conj() {
return new Complex(real, -imag);
}
@Override
public String toString() {
return real + " + " + imag + " * i";
}
public static void main(String[] args) {
Complex a = new Complex(Math.PI, -5);
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());
}
}
|
Rewrite the snippet below in Python so it works the same as the original Fortran code. | program cdemo
complex :: a = (5,3), b = (0.5, 6.0)
complex :: absum, abprod, aneg, ainv
absum = a + b
abprod = a * b
aneg = -a
ainv = 1.0 / a
end program cdemo
| >>> z1 = 1.5 + 3j
>>> z2 = 1.5 + 1.5j
>>> z1 + z2
(3+4.5j)
>>> z1 - z2
1.5j
>>> z1 * z2
(-2.25+6.75j)
>>> z1 / z2
(1.5+0.5j)
>>> - z1
(-1.5-3j)
>>> z1.conjugate()
(1.5-3j)
>>> abs(z1)
3.3541019662496847
>>> z1 ** z2
(-1.1024829553277784-0.38306415117199333j)
>>> z1.real
1.5
>>> z1.imag
3.0
>>>
|
Maintain the same structure and functionality when rewriting this code in C. | class Complex {
final Number real, imag
static final Complex i = [0,1] as Complex
Complex(Number r, Number i = 0) { (real, imag) = [r, i] }
Complex(Map that) { (real, imag) = [that.real ?: 0, that.imag ?: 0] }
Complex plus (Complex c) { [real + c.real, imag + c.imag] as Complex }
Complex plus (Number n) { [real + n, imag] as Complex }
Complex minus (Complex c) { [real - c.real, imag - c.imag] as Complex }
Complex minus (Number n) { [real - n, imag] as Complex }
Complex multiply (Complex c) { [real*c.real - imag*c.imag , imag*c.real + real*c.imag] as Complex }
Complex multiply (Number n) { [real*n , imag*n] as Complex }
Complex div (Complex c) { this * c.recip() }
Complex div (Number n) { this * (1/n) }
Complex negative () { [-real, -imag] as Complex }
Complex bitwiseNegate () { [real, -imag] as Complex }
Number getAbs() { Math.sqrt( real*real + imag*imag ) }
Number abs() { this.abs }
Complex getRecip() { (~this) / (ρ**2) }
Complex recip() { this.recip }
Number getTheta() {
def θ = Math.atan2(imag,real)
θ = θ < 0 ? θ + 2 * Math.PI : θ
}
Number getΘ() { this.theta }
Number getRho() { this.abs }
Number getΡ() { this.abs }
static Complex fromPolar(Number ρ, Number θ) {
[ρ * Math.cos(θ), ρ * Math.sin(θ)] as Complex
}
Complex withTheta(Number θ) { fromPolar(this.rho, θ) }
Complex withΘ(Number θ) { fromPolar(this.rho, θ) }
Complex withRho(Number ρ) { fromPolar(ρ, this.θ) }
Complex withΡ(Number ρ) { fromPolar(ρ, this.θ) }
static Complex exp(Complex c) { fromPolar(Math.exp(c.real), c.imag) }
static Complex log(Complex c) { [Math.log(c.rho), c.theta] as Complex }
Complex power(Complex c) {
def zero = [0] as Complex
(this == zero && c != zero) \
? zero \
: c == 1 \
? this \
: exp( log(this) * c )
}
Complex power(Number n) { this ** ([n, 0] as Complex) }
boolean equals(that) {
that != null && (that instanceof Complex \
? [this.real, this.imag] == [that.real, that.imag] \
: that instanceof Number && [this.real, this.imag] == [that, 0])
}
int hashCode() { [real, imag].hashCode() }
String toString() {
def realPart = "${real}"
def imagPart = imag.abs() == 1 ? "i" : "${imag.abs()}i"
real == 0 && imag == 0 \
? "0" \
: real == 0 \
? (imag > 0 ? '' : "-") + imagPart \
: imag == 0 \
? realPart \
: realPart + (imag > 0 ? " + " : " - ") + imagPart
}
}
| #include <complex.h>
#include <stdio.h>
void cprint(double complex c)
{
printf("%f%+fI", creal(c), cimag(c));
}
void complex_operations() {
double complex a = 1.0 + 1.0I;
double complex b = 3.14159 + 1.2I;
double complex c;
printf("\na="); cprint(a);
printf("\nb="); cprint(b);
c = a + b;
printf("\na+b="); cprint(c);
c = a * b;
printf("\na*b="); cprint(c);
c = 1.0 / a;
printf("\n1/c="); cprint(c);
c = -a;
printf("\n-a="); cprint(c);
c = conj(a);
printf("\nconj a="); cprint(c); printf("\n");
}
|
Produce a language-to-language conversion: from Groovy to C#, same semantics. | class Complex {
final Number real, imag
static final Complex i = [0,1] as Complex
Complex(Number r, Number i = 0) { (real, imag) = [r, i] }
Complex(Map that) { (real, imag) = [that.real ?: 0, that.imag ?: 0] }
Complex plus (Complex c) { [real + c.real, imag + c.imag] as Complex }
Complex plus (Number n) { [real + n, imag] as Complex }
Complex minus (Complex c) { [real - c.real, imag - c.imag] as Complex }
Complex minus (Number n) { [real - n, imag] as Complex }
Complex multiply (Complex c) { [real*c.real - imag*c.imag , imag*c.real + real*c.imag] as Complex }
Complex multiply (Number n) { [real*n , imag*n] as Complex }
Complex div (Complex c) { this * c.recip() }
Complex div (Number n) { this * (1/n) }
Complex negative () { [-real, -imag] as Complex }
Complex bitwiseNegate () { [real, -imag] as Complex }
Number getAbs() { Math.sqrt( real*real + imag*imag ) }
Number abs() { this.abs }
Complex getRecip() { (~this) / (ρ**2) }
Complex recip() { this.recip }
Number getTheta() {
def θ = Math.atan2(imag,real)
θ = θ < 0 ? θ + 2 * Math.PI : θ
}
Number getΘ() { this.theta }
Number getRho() { this.abs }
Number getΡ() { this.abs }
static Complex fromPolar(Number ρ, Number θ) {
[ρ * Math.cos(θ), ρ * Math.sin(θ)] as Complex
}
Complex withTheta(Number θ) { fromPolar(this.rho, θ) }
Complex withΘ(Number θ) { fromPolar(this.rho, θ) }
Complex withRho(Number ρ) { fromPolar(ρ, this.θ) }
Complex withΡ(Number ρ) { fromPolar(ρ, this.θ) }
static Complex exp(Complex c) { fromPolar(Math.exp(c.real), c.imag) }
static Complex log(Complex c) { [Math.log(c.rho), c.theta] as Complex }
Complex power(Complex c) {
def zero = [0] as Complex
(this == zero && c != zero) \
? zero \
: c == 1 \
? this \
: exp( log(this) * c )
}
Complex power(Number n) { this ** ([n, 0] as Complex) }
boolean equals(that) {
that != null && (that instanceof Complex \
? [this.real, this.imag] == [that.real, that.imag] \
: that instanceof Number && [this.real, this.imag] == [that, 0])
}
int hashCode() { [real, imag].hashCode() }
String toString() {
def realPart = "${real}"
def imagPart = imag.abs() == 1 ? "i" : "${imag.abs()}i"
real == 0 && imag == 0 \
? "0" \
: real == 0 \
? (imag > 0 ? '' : "-") + imagPart \
: imag == 0 \
? realPart \
: realPart + (imag > 0 ? " + " : " - ") + imagPart
}
}
| 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);
}
}
}
}
|
Transform the following Groovy implementation into C++, maintaining the same output and logic. | class Complex {
final Number real, imag
static final Complex i = [0,1] as Complex
Complex(Number r, Number i = 0) { (real, imag) = [r, i] }
Complex(Map that) { (real, imag) = [that.real ?: 0, that.imag ?: 0] }
Complex plus (Complex c) { [real + c.real, imag + c.imag] as Complex }
Complex plus (Number n) { [real + n, imag] as Complex }
Complex minus (Complex c) { [real - c.real, imag - c.imag] as Complex }
Complex minus (Number n) { [real - n, imag] as Complex }
Complex multiply (Complex c) { [real*c.real - imag*c.imag , imag*c.real + real*c.imag] as Complex }
Complex multiply (Number n) { [real*n , imag*n] as Complex }
Complex div (Complex c) { this * c.recip() }
Complex div (Number n) { this * (1/n) }
Complex negative () { [-real, -imag] as Complex }
Complex bitwiseNegate () { [real, -imag] as Complex }
Number getAbs() { Math.sqrt( real*real + imag*imag ) }
Number abs() { this.abs }
Complex getRecip() { (~this) / (ρ**2) }
Complex recip() { this.recip }
Number getTheta() {
def θ = Math.atan2(imag,real)
θ = θ < 0 ? θ + 2 * Math.PI : θ
}
Number getΘ() { this.theta }
Number getRho() { this.abs }
Number getΡ() { this.abs }
static Complex fromPolar(Number ρ, Number θ) {
[ρ * Math.cos(θ), ρ * Math.sin(θ)] as Complex
}
Complex withTheta(Number θ) { fromPolar(this.rho, θ) }
Complex withΘ(Number θ) { fromPolar(this.rho, θ) }
Complex withRho(Number ρ) { fromPolar(ρ, this.θ) }
Complex withΡ(Number ρ) { fromPolar(ρ, this.θ) }
static Complex exp(Complex c) { fromPolar(Math.exp(c.real), c.imag) }
static Complex log(Complex c) { [Math.log(c.rho), c.theta] as Complex }
Complex power(Complex c) {
def zero = [0] as Complex
(this == zero && c != zero) \
? zero \
: c == 1 \
? this \
: exp( log(this) * c )
}
Complex power(Number n) { this ** ([n, 0] as Complex) }
boolean equals(that) {
that != null && (that instanceof Complex \
? [this.real, this.imag] == [that.real, that.imag] \
: that instanceof Number && [this.real, this.imag] == [that, 0])
}
int hashCode() { [real, imag].hashCode() }
String toString() {
def realPart = "${real}"
def imagPart = imag.abs() == 1 ? "i" : "${imag.abs()}i"
real == 0 && imag == 0 \
? "0" \
: real == 0 \
? (imag > 0 ? '' : "-") + imagPart \
: imag == 0 \
? realPart \
: realPart + (imag > 0 ? " + " : " - ") + imagPart
}
}
| #include <iostream>
#include <complex>
using std::complex;
void complex_operations() {
complex<double> a(1.0, 1.0);
complex<double> b(3.14159, 1.25);
std::cout << a + b << std::endl;
std::cout << a * b << std::endl;
std::cout << 1.0 / a << std::endl;
std::cout << -a << std::endl;
std::cout << std::conj(a) << std::endl;
}
|
Preserve the algorithm and functionality while converting the code from Groovy to Java. | class Complex {
final Number real, imag
static final Complex i = [0,1] as Complex
Complex(Number r, Number i = 0) { (real, imag) = [r, i] }
Complex(Map that) { (real, imag) = [that.real ?: 0, that.imag ?: 0] }
Complex plus (Complex c) { [real + c.real, imag + c.imag] as Complex }
Complex plus (Number n) { [real + n, imag] as Complex }
Complex minus (Complex c) { [real - c.real, imag - c.imag] as Complex }
Complex minus (Number n) { [real - n, imag] as Complex }
Complex multiply (Complex c) { [real*c.real - imag*c.imag , imag*c.real + real*c.imag] as Complex }
Complex multiply (Number n) { [real*n , imag*n] as Complex }
Complex div (Complex c) { this * c.recip() }
Complex div (Number n) { this * (1/n) }
Complex negative () { [-real, -imag] as Complex }
Complex bitwiseNegate () { [real, -imag] as Complex }
Number getAbs() { Math.sqrt( real*real + imag*imag ) }
Number abs() { this.abs }
Complex getRecip() { (~this) / (ρ**2) }
Complex recip() { this.recip }
Number getTheta() {
def θ = Math.atan2(imag,real)
θ = θ < 0 ? θ + 2 * Math.PI : θ
}
Number getΘ() { this.theta }
Number getRho() { this.abs }
Number getΡ() { this.abs }
static Complex fromPolar(Number ρ, Number θ) {
[ρ * Math.cos(θ), ρ * Math.sin(θ)] as Complex
}
Complex withTheta(Number θ) { fromPolar(this.rho, θ) }
Complex withΘ(Number θ) { fromPolar(this.rho, θ) }
Complex withRho(Number ρ) { fromPolar(ρ, this.θ) }
Complex withΡ(Number ρ) { fromPolar(ρ, this.θ) }
static Complex exp(Complex c) { fromPolar(Math.exp(c.real), c.imag) }
static Complex log(Complex c) { [Math.log(c.rho), c.theta] as Complex }
Complex power(Complex c) {
def zero = [0] as Complex
(this == zero && c != zero) \
? zero \
: c == 1 \
? this \
: exp( log(this) * c )
}
Complex power(Number n) { this ** ([n, 0] as Complex) }
boolean equals(that) {
that != null && (that instanceof Complex \
? [this.real, this.imag] == [that.real, that.imag] \
: that instanceof Number && [this.real, this.imag] == [that, 0])
}
int hashCode() { [real, imag].hashCode() }
String toString() {
def realPart = "${real}"
def imagPart = imag.abs() == 1 ? "i" : "${imag.abs()}i"
real == 0 && imag == 0 \
? "0" \
: real == 0 \
? (imag > 0 ? '' : "-") + imagPart \
: imag == 0 \
? realPart \
: realPart + (imag > 0 ? " + " : " - ") + imagPart
}
}
| public class Complex {
public final double real;
public final double imag;
public Complex() {
this(0, 0);
}
public Complex(double r, double i) {
real = r;
imag = i;
}
public Complex add(Complex b) {
return new Complex(this.real + b.real, this.imag + b.imag);
}
public Complex mult(Complex b) {
return new Complex(this.real * b.real - this.imag * b.imag,
this.real * b.imag + this.imag * b.real);
}
public Complex inv() {
double denom = real * real + imag * imag;
return new Complex(real / denom, -imag / denom);
}
public Complex neg() {
return new Complex(-real, -imag);
}
public Complex conj() {
return new Complex(real, -imag);
}
@Override
public String toString() {
return real + " + " + imag + " * i";
}
public static void main(String[] args) {
Complex a = new Complex(Math.PI, -5);
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());
}
}
|
Rewrite the snippet below in Python so it works the same as the original Groovy code. | class Complex {
final Number real, imag
static final Complex i = [0,1] as Complex
Complex(Number r, Number i = 0) { (real, imag) = [r, i] }
Complex(Map that) { (real, imag) = [that.real ?: 0, that.imag ?: 0] }
Complex plus (Complex c) { [real + c.real, imag + c.imag] as Complex }
Complex plus (Number n) { [real + n, imag] as Complex }
Complex minus (Complex c) { [real - c.real, imag - c.imag] as Complex }
Complex minus (Number n) { [real - n, imag] as Complex }
Complex multiply (Complex c) { [real*c.real - imag*c.imag , imag*c.real + real*c.imag] as Complex }
Complex multiply (Number n) { [real*n , imag*n] as Complex }
Complex div (Complex c) { this * c.recip() }
Complex div (Number n) { this * (1/n) }
Complex negative () { [-real, -imag] as Complex }
Complex bitwiseNegate () { [real, -imag] as Complex }
Number getAbs() { Math.sqrt( real*real + imag*imag ) }
Number abs() { this.abs }
Complex getRecip() { (~this) / (ρ**2) }
Complex recip() { this.recip }
Number getTheta() {
def θ = Math.atan2(imag,real)
θ = θ < 0 ? θ + 2 * Math.PI : θ
}
Number getΘ() { this.theta }
Number getRho() { this.abs }
Number getΡ() { this.abs }
static Complex fromPolar(Number ρ, Number θ) {
[ρ * Math.cos(θ), ρ * Math.sin(θ)] as Complex
}
Complex withTheta(Number θ) { fromPolar(this.rho, θ) }
Complex withΘ(Number θ) { fromPolar(this.rho, θ) }
Complex withRho(Number ρ) { fromPolar(ρ, this.θ) }
Complex withΡ(Number ρ) { fromPolar(ρ, this.θ) }
static Complex exp(Complex c) { fromPolar(Math.exp(c.real), c.imag) }
static Complex log(Complex c) { [Math.log(c.rho), c.theta] as Complex }
Complex power(Complex c) {
def zero = [0] as Complex
(this == zero && c != zero) \
? zero \
: c == 1 \
? this \
: exp( log(this) * c )
}
Complex power(Number n) { this ** ([n, 0] as Complex) }
boolean equals(that) {
that != null && (that instanceof Complex \
? [this.real, this.imag] == [that.real, that.imag] \
: that instanceof Number && [this.real, this.imag] == [that, 0])
}
int hashCode() { [real, imag].hashCode() }
String toString() {
def realPart = "${real}"
def imagPart = imag.abs() == 1 ? "i" : "${imag.abs()}i"
real == 0 && imag == 0 \
? "0" \
: real == 0 \
? (imag > 0 ? '' : "-") + imagPart \
: imag == 0 \
? realPart \
: realPart + (imag > 0 ? " + " : " - ") + imagPart
}
}
| >>> z1 = 1.5 + 3j
>>> z2 = 1.5 + 1.5j
>>> z1 + z2
(3+4.5j)
>>> z1 - z2
1.5j
>>> z1 * z2
(-2.25+6.75j)
>>> z1 / z2
(1.5+0.5j)
>>> - z1
(-1.5-3j)
>>> z1.conjugate()
(1.5-3j)
>>> abs(z1)
3.3541019662496847
>>> z1 ** z2
(-1.1024829553277784-0.38306415117199333j)
>>> z1.real
1.5
>>> z1.imag
3.0
>>>
|
Please provide an equivalent version of this Groovy code in Go. | class Complex {
final Number real, imag
static final Complex i = [0,1] as Complex
Complex(Number r, Number i = 0) { (real, imag) = [r, i] }
Complex(Map that) { (real, imag) = [that.real ?: 0, that.imag ?: 0] }
Complex plus (Complex c) { [real + c.real, imag + c.imag] as Complex }
Complex plus (Number n) { [real + n, imag] as Complex }
Complex minus (Complex c) { [real - c.real, imag - c.imag] as Complex }
Complex minus (Number n) { [real - n, imag] as Complex }
Complex multiply (Complex c) { [real*c.real - imag*c.imag , imag*c.real + real*c.imag] as Complex }
Complex multiply (Number n) { [real*n , imag*n] as Complex }
Complex div (Complex c) { this * c.recip() }
Complex div (Number n) { this * (1/n) }
Complex negative () { [-real, -imag] as Complex }
Complex bitwiseNegate () { [real, -imag] as Complex }
Number getAbs() { Math.sqrt( real*real + imag*imag ) }
Number abs() { this.abs }
Complex getRecip() { (~this) / (ρ**2) }
Complex recip() { this.recip }
Number getTheta() {
def θ = Math.atan2(imag,real)
θ = θ < 0 ? θ + 2 * Math.PI : θ
}
Number getΘ() { this.theta }
Number getRho() { this.abs }
Number getΡ() { this.abs }
static Complex fromPolar(Number ρ, Number θ) {
[ρ * Math.cos(θ), ρ * Math.sin(θ)] as Complex
}
Complex withTheta(Number θ) { fromPolar(this.rho, θ) }
Complex withΘ(Number θ) { fromPolar(this.rho, θ) }
Complex withRho(Number ρ) { fromPolar(ρ, this.θ) }
Complex withΡ(Number ρ) { fromPolar(ρ, this.θ) }
static Complex exp(Complex c) { fromPolar(Math.exp(c.real), c.imag) }
static Complex log(Complex c) { [Math.log(c.rho), c.theta] as Complex }
Complex power(Complex c) {
def zero = [0] as Complex
(this == zero && c != zero) \
? zero \
: c == 1 \
? this \
: exp( log(this) * c )
}
Complex power(Number n) { this ** ([n, 0] as Complex) }
boolean equals(that) {
that != null && (that instanceof Complex \
? [this.real, this.imag] == [that.real, that.imag] \
: that instanceof Number && [this.real, this.imag] == [that, 0])
}
int hashCode() { [real, imag].hashCode() }
String toString() {
def realPart = "${real}"
def imagPart = imag.abs() == 1 ? "i" : "${imag.abs()}i"
real == 0 && imag == 0 \
? "0" \
: real == 0 \
? (imag > 0 ? '' : "-") + imagPart \
: imag == 0 \
? realPart \
: realPart + (imag > 0 ? " + " : " - ") + imagPart
}
}
| package main
import (
"fmt"
"math/cmplx"
)
func main() {
a := 1 + 1i
b := 3.14159 + 1.25i
fmt.Println("a: ", a)
fmt.Println("b: ", b)
fmt.Println("a + b: ", a+b)
fmt.Println("a * b: ", a*b)
fmt.Println("-a: ", -a)
fmt.Println("1 / a: ", 1/a)
fmt.Println("a̅: ", cmplx.Conj(a))
}
|
Translate the given Haskell code snippet into C without altering its behavior. | import Data.Complex
main = do
let a = 1.0 :+ 2.0
let b = 4
putStrLn $ "Add: " ++ show (a + b)
putStrLn $ "Subtract: " ++ show (a - b)
putStrLn $ "Multiply: " ++ show (a * b)
putStrLn $ "Divide: " ++ show (a / b)
putStrLn $ "Negate: " ++ show (-a)
putStrLn $ "Inverse: " ++ show (recip a)
putStrLn $ "Conjugate:" ++ show (conjugate a)
| #include <complex.h>
#include <stdio.h>
void cprint(double complex c)
{
printf("%f%+fI", creal(c), cimag(c));
}
void complex_operations() {
double complex a = 1.0 + 1.0I;
double complex b = 3.14159 + 1.2I;
double complex c;
printf("\na="); cprint(a);
printf("\nb="); cprint(b);
c = a + b;
printf("\na+b="); cprint(c);
c = a * b;
printf("\na*b="); cprint(c);
c = 1.0 / a;
printf("\n1/c="); cprint(c);
c = -a;
printf("\n-a="); cprint(c);
c = conj(a);
printf("\nconj a="); cprint(c); printf("\n");
}
|
Please provide an equivalent version of this Haskell code in C#. | import Data.Complex
main = do
let a = 1.0 :+ 2.0
let b = 4
putStrLn $ "Add: " ++ show (a + b)
putStrLn $ "Subtract: " ++ show (a - b)
putStrLn $ "Multiply: " ++ show (a * b)
putStrLn $ "Divide: " ++ show (a / b)
putStrLn $ "Negate: " ++ show (-a)
putStrLn $ "Inverse: " ++ show (recip a)
putStrLn $ "Conjugate:" ++ show (conjugate a)
| 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);
}
}
}
}
|
Generate a C++ translation of this Haskell snippet without changing its computational steps. | import Data.Complex
main = do
let a = 1.0 :+ 2.0
let b = 4
putStrLn $ "Add: " ++ show (a + b)
putStrLn $ "Subtract: " ++ show (a - b)
putStrLn $ "Multiply: " ++ show (a * b)
putStrLn $ "Divide: " ++ show (a / b)
putStrLn $ "Negate: " ++ show (-a)
putStrLn $ "Inverse: " ++ show (recip a)
putStrLn $ "Conjugate:" ++ show (conjugate a)
| #include <iostream>
#include <complex>
using std::complex;
void complex_operations() {
complex<double> a(1.0, 1.0);
complex<double> b(3.14159, 1.25);
std::cout << a + b << std::endl;
std::cout << a * b << std::endl;
std::cout << 1.0 / a << std::endl;
std::cout << -a << std::endl;
std::cout << std::conj(a) << std::endl;
}
|
Write a version of this Haskell function in Java with identical behavior. | import Data.Complex
main = do
let a = 1.0 :+ 2.0
let b = 4
putStrLn $ "Add: " ++ show (a + b)
putStrLn $ "Subtract: " ++ show (a - b)
putStrLn $ "Multiply: " ++ show (a * b)
putStrLn $ "Divide: " ++ show (a / b)
putStrLn $ "Negate: " ++ show (-a)
putStrLn $ "Inverse: " ++ show (recip a)
putStrLn $ "Conjugate:" ++ show (conjugate a)
| public class Complex {
public final double real;
public final double imag;
public Complex() {
this(0, 0);
}
public Complex(double r, double i) {
real = r;
imag = i;
}
public Complex add(Complex b) {
return new Complex(this.real + b.real, this.imag + b.imag);
}
public Complex mult(Complex b) {
return new Complex(this.real * b.real - this.imag * b.imag,
this.real * b.imag + this.imag * b.real);
}
public Complex inv() {
double denom = real * real + imag * imag;
return new Complex(real / denom, -imag / denom);
}
public Complex neg() {
return new Complex(-real, -imag);
}
public Complex conj() {
return new Complex(real, -imag);
}
@Override
public String toString() {
return real + " + " + imag + " * i";
}
public static void main(String[] args) {
Complex a = new Complex(Math.PI, -5);
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());
}
}
|
Transform the following Haskell implementation into Python, maintaining the same output and logic. | import Data.Complex
main = do
let a = 1.0 :+ 2.0
let b = 4
putStrLn $ "Add: " ++ show (a + b)
putStrLn $ "Subtract: " ++ show (a - b)
putStrLn $ "Multiply: " ++ show (a * b)
putStrLn $ "Divide: " ++ show (a / b)
putStrLn $ "Negate: " ++ show (-a)
putStrLn $ "Inverse: " ++ show (recip a)
putStrLn $ "Conjugate:" ++ show (conjugate a)
| >>> z1 = 1.5 + 3j
>>> z2 = 1.5 + 1.5j
>>> z1 + z2
(3+4.5j)
>>> z1 - z2
1.5j
>>> z1 * z2
(-2.25+6.75j)
>>> z1 / z2
(1.5+0.5j)
>>> - z1
(-1.5-3j)
>>> z1.conjugate()
(1.5-3j)
>>> abs(z1)
3.3541019662496847
>>> z1 ** z2
(-1.1024829553277784-0.38306415117199333j)
>>> z1.real
1.5
>>> z1.imag
3.0
>>>
|
Convert this Haskell snippet to Go and keep its semantics consistent. | import Data.Complex
main = do
let a = 1.0 :+ 2.0
let b = 4
putStrLn $ "Add: " ++ show (a + b)
putStrLn $ "Subtract: " ++ show (a - b)
putStrLn $ "Multiply: " ++ show (a * b)
putStrLn $ "Divide: " ++ show (a / b)
putStrLn $ "Negate: " ++ show (-a)
putStrLn $ "Inverse: " ++ show (recip a)
putStrLn $ "Conjugate:" ++ show (conjugate a)
| package main
import (
"fmt"
"math/cmplx"
)
func main() {
a := 1 + 1i
b := 3.14159 + 1.25i
fmt.Println("a: ", a)
fmt.Println("b: ", b)
fmt.Println("a + b: ", a+b)
fmt.Println("a * b: ", a*b)
fmt.Println("-a: ", -a)
fmt.Println("1 / a: ", 1/a)
fmt.Println("a̅: ", cmplx.Conj(a))
}
|
Rewrite the snippet below in C so it works the same as the original Icon code. | procedure main()
SetupComplex()
a := complex(1,2)
b := complex(3,4)
c := complex(&pi,1.5)
d := complex(1)
e := complex(,1)
every v := !"abcde" do write(v," := ",cpxstr(variable(v)))
write("a+b := ", cpxstr(cpxadd(a,b)))
write("a-b := ", cpxstr(cpxsub(a,b)))
write("a*b := ", cpxstr(cpxmul(a,b)))
write("a/b := ", cpxstr(cpxdiv(a,b)))
write("neg(a) := ", cpxstr(cpxneg(a)))
write("inv(a) := ", cpxstr(cpxinv(a)))
write("conj(a) := ", cpxstr(cpxconj(a)))
write("abs(a) := ", cpxabs(a))
write("neg(1) := ", cpxstr(cpxneg(1)))
end
| #include <complex.h>
#include <stdio.h>
void cprint(double complex c)
{
printf("%f%+fI", creal(c), cimag(c));
}
void complex_operations() {
double complex a = 1.0 + 1.0I;
double complex b = 3.14159 + 1.2I;
double complex c;
printf("\na="); cprint(a);
printf("\nb="); cprint(b);
c = a + b;
printf("\na+b="); cprint(c);
c = a * b;
printf("\na*b="); cprint(c);
c = 1.0 / a;
printf("\n1/c="); cprint(c);
c = -a;
printf("\n-a="); cprint(c);
c = conj(a);
printf("\nconj a="); cprint(c); printf("\n");
}
|
Keep all operations the same but rewrite the snippet in C#. | procedure main()
SetupComplex()
a := complex(1,2)
b := complex(3,4)
c := complex(&pi,1.5)
d := complex(1)
e := complex(,1)
every v := !"abcde" do write(v," := ",cpxstr(variable(v)))
write("a+b := ", cpxstr(cpxadd(a,b)))
write("a-b := ", cpxstr(cpxsub(a,b)))
write("a*b := ", cpxstr(cpxmul(a,b)))
write("a/b := ", cpxstr(cpxdiv(a,b)))
write("neg(a) := ", cpxstr(cpxneg(a)))
write("inv(a) := ", cpxstr(cpxinv(a)))
write("conj(a) := ", cpxstr(cpxconj(a)))
write("abs(a) := ", cpxabs(a))
write("neg(1) := ", cpxstr(cpxneg(1)))
end
| 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);
}
}
}
}
|
Generate a C++ translation of this Icon snippet without changing its computational steps. | procedure main()
SetupComplex()
a := complex(1,2)
b := complex(3,4)
c := complex(&pi,1.5)
d := complex(1)
e := complex(,1)
every v := !"abcde" do write(v," := ",cpxstr(variable(v)))
write("a+b := ", cpxstr(cpxadd(a,b)))
write("a-b := ", cpxstr(cpxsub(a,b)))
write("a*b := ", cpxstr(cpxmul(a,b)))
write("a/b := ", cpxstr(cpxdiv(a,b)))
write("neg(a) := ", cpxstr(cpxneg(a)))
write("inv(a) := ", cpxstr(cpxinv(a)))
write("conj(a) := ", cpxstr(cpxconj(a)))
write("abs(a) := ", cpxabs(a))
write("neg(1) := ", cpxstr(cpxneg(1)))
end
| #include <iostream>
#include <complex>
using std::complex;
void complex_operations() {
complex<double> a(1.0, 1.0);
complex<double> b(3.14159, 1.25);
std::cout << a + b << std::endl;
std::cout << a * b << std::endl;
std::cout << 1.0 / a << std::endl;
std::cout << -a << std::endl;
std::cout << std::conj(a) << std::endl;
}
|
Translate this program into Java but keep the logic exactly as in Icon. | procedure main()
SetupComplex()
a := complex(1,2)
b := complex(3,4)
c := complex(&pi,1.5)
d := complex(1)
e := complex(,1)
every v := !"abcde" do write(v," := ",cpxstr(variable(v)))
write("a+b := ", cpxstr(cpxadd(a,b)))
write("a-b := ", cpxstr(cpxsub(a,b)))
write("a*b := ", cpxstr(cpxmul(a,b)))
write("a/b := ", cpxstr(cpxdiv(a,b)))
write("neg(a) := ", cpxstr(cpxneg(a)))
write("inv(a) := ", cpxstr(cpxinv(a)))
write("conj(a) := ", cpxstr(cpxconj(a)))
write("abs(a) := ", cpxabs(a))
write("neg(1) := ", cpxstr(cpxneg(1)))
end
| public class Complex {
public final double real;
public final double imag;
public Complex() {
this(0, 0);
}
public Complex(double r, double i) {
real = r;
imag = i;
}
public Complex add(Complex b) {
return new Complex(this.real + b.real, this.imag + b.imag);
}
public Complex mult(Complex b) {
return new Complex(this.real * b.real - this.imag * b.imag,
this.real * b.imag + this.imag * b.real);
}
public Complex inv() {
double denom = real * real + imag * imag;
return new Complex(real / denom, -imag / denom);
}
public Complex neg() {
return new Complex(-real, -imag);
}
public Complex conj() {
return new Complex(real, -imag);
}
@Override
public String toString() {
return real + " + " + imag + " * i";
}
public static void main(String[] args) {
Complex a = new Complex(Math.PI, -5);
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());
}
}
|
Can you help me rewrite this code in Python instead of Icon, keeping it the same logically? | procedure main()
SetupComplex()
a := complex(1,2)
b := complex(3,4)
c := complex(&pi,1.5)
d := complex(1)
e := complex(,1)
every v := !"abcde" do write(v," := ",cpxstr(variable(v)))
write("a+b := ", cpxstr(cpxadd(a,b)))
write("a-b := ", cpxstr(cpxsub(a,b)))
write("a*b := ", cpxstr(cpxmul(a,b)))
write("a/b := ", cpxstr(cpxdiv(a,b)))
write("neg(a) := ", cpxstr(cpxneg(a)))
write("inv(a) := ", cpxstr(cpxinv(a)))
write("conj(a) := ", cpxstr(cpxconj(a)))
write("abs(a) := ", cpxabs(a))
write("neg(1) := ", cpxstr(cpxneg(1)))
end
| >>> z1 = 1.5 + 3j
>>> z2 = 1.5 + 1.5j
>>> z1 + z2
(3+4.5j)
>>> z1 - z2
1.5j
>>> z1 * z2
(-2.25+6.75j)
>>> z1 / z2
(1.5+0.5j)
>>> - z1
(-1.5-3j)
>>> z1.conjugate()
(1.5-3j)
>>> abs(z1)
3.3541019662496847
>>> z1 ** z2
(-1.1024829553277784-0.38306415117199333j)
>>> z1.real
1.5
>>> z1.imag
3.0
>>>
|
Transform the following Icon implementation into Go, maintaining the same output and logic. | procedure main()
SetupComplex()
a := complex(1,2)
b := complex(3,4)
c := complex(&pi,1.5)
d := complex(1)
e := complex(,1)
every v := !"abcde" do write(v," := ",cpxstr(variable(v)))
write("a+b := ", cpxstr(cpxadd(a,b)))
write("a-b := ", cpxstr(cpxsub(a,b)))
write("a*b := ", cpxstr(cpxmul(a,b)))
write("a/b := ", cpxstr(cpxdiv(a,b)))
write("neg(a) := ", cpxstr(cpxneg(a)))
write("inv(a) := ", cpxstr(cpxinv(a)))
write("conj(a) := ", cpxstr(cpxconj(a)))
write("abs(a) := ", cpxabs(a))
write("neg(1) := ", cpxstr(cpxneg(1)))
end
| package main
import (
"fmt"
"math/cmplx"
)
func main() {
a := 1 + 1i
b := 3.14159 + 1.25i
fmt.Println("a: ", a)
fmt.Println("b: ", b)
fmt.Println("a + b: ", a+b)
fmt.Println("a * b: ", a*b)
fmt.Println("-a: ", -a)
fmt.Println("1 / a: ", 1/a)
fmt.Println("a̅: ", cmplx.Conj(a))
}
|
Preserve the algorithm and functionality while converting the code from J to C. | x=: 1j1
y=: 3.14159j1.2
x+y
4.14159j2.2
x*y
1.94159j4.34159
%x
0.5j_0.5
-x
_1j_1
+x
1j_1
| #include <complex.h>
#include <stdio.h>
void cprint(double complex c)
{
printf("%f%+fI", creal(c), cimag(c));
}
void complex_operations() {
double complex a = 1.0 + 1.0I;
double complex b = 3.14159 + 1.2I;
double complex c;
printf("\na="); cprint(a);
printf("\nb="); cprint(b);
c = a + b;
printf("\na+b="); cprint(c);
c = a * b;
printf("\na*b="); cprint(c);
c = 1.0 / a;
printf("\n1/c="); cprint(c);
c = -a;
printf("\n-a="); cprint(c);
c = conj(a);
printf("\nconj a="); cprint(c); printf("\n");
}
|
Can you help me rewrite this code in C# instead of J, keeping it the same logically? | x=: 1j1
y=: 3.14159j1.2
x+y
4.14159j2.2
x*y
1.94159j4.34159
%x
0.5j_0.5
-x
_1j_1
+x
1j_1
| 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);
}
}
}
}
|
Can you help me rewrite this code in C++ instead of J, keeping it the same logically? | x=: 1j1
y=: 3.14159j1.2
x+y
4.14159j2.2
x*y
1.94159j4.34159
%x
0.5j_0.5
-x
_1j_1
+x
1j_1
| #include <iostream>
#include <complex>
using std::complex;
void complex_operations() {
complex<double> a(1.0, 1.0);
complex<double> b(3.14159, 1.25);
std::cout << a + b << std::endl;
std::cout << a * b << std::endl;
std::cout << 1.0 / a << std::endl;
std::cout << -a << std::endl;
std::cout << std::conj(a) << std::endl;
}
|
Produce a language-to-language conversion: from J to Java, same semantics. | x=: 1j1
y=: 3.14159j1.2
x+y
4.14159j2.2
x*y
1.94159j4.34159
%x
0.5j_0.5
-x
_1j_1
+x
1j_1
| public class Complex {
public final double real;
public final double imag;
public Complex() {
this(0, 0);
}
public Complex(double r, double i) {
real = r;
imag = i;
}
public Complex add(Complex b) {
return new Complex(this.real + b.real, this.imag + b.imag);
}
public Complex mult(Complex b) {
return new Complex(this.real * b.real - this.imag * b.imag,
this.real * b.imag + this.imag * b.real);
}
public Complex inv() {
double denom = real * real + imag * imag;
return new Complex(real / denom, -imag / denom);
}
public Complex neg() {
return new Complex(-real, -imag);
}
public Complex conj() {
return new Complex(real, -imag);
}
@Override
public String toString() {
return real + " + " + imag + " * i";
}
public static void main(String[] args) {
Complex a = new Complex(Math.PI, -5);
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());
}
}
|
Transform the following J implementation into Python, maintaining the same output and logic. | x=: 1j1
y=: 3.14159j1.2
x+y
4.14159j2.2
x*y
1.94159j4.34159
%x
0.5j_0.5
-x
_1j_1
+x
1j_1
| >>> z1 = 1.5 + 3j
>>> z2 = 1.5 + 1.5j
>>> z1 + z2
(3+4.5j)
>>> z1 - z2
1.5j
>>> z1 * z2
(-2.25+6.75j)
>>> z1 / z2
(1.5+0.5j)
>>> - z1
(-1.5-3j)
>>> z1.conjugate()
(1.5-3j)
>>> abs(z1)
3.3541019662496847
>>> z1 ** z2
(-1.1024829553277784-0.38306415117199333j)
>>> z1.real
1.5
>>> z1.imag
3.0
>>>
|
Change the following J code into Go without altering its purpose. | x=: 1j1
y=: 3.14159j1.2
x+y
4.14159j2.2
x*y
1.94159j4.34159
%x
0.5j_0.5
-x
_1j_1
+x
1j_1
| package main
import (
"fmt"
"math/cmplx"
)
func main() {
a := 1 + 1i
b := 3.14159 + 1.25i
fmt.Println("a: ", a)
fmt.Println("b: ", b)
fmt.Println("a + b: ", a+b)
fmt.Println("a * b: ", a*b)
fmt.Println("-a: ", -a)
fmt.Println("1 / a: ", 1/a)
fmt.Println("a̅: ", cmplx.Conj(a))
}
|
Can you help me rewrite this code in C instead of Julia, keeping it the same logically? | julia> z1 = 1.5 + 3im
julia> z2 = 1.5 + 1.5im
julia> z1 + z2
3.0 + 4.5im
julia> z1 - z2
0.0 + 1.5im
julia> z1 * z2
-2.25 + 6.75im
julia> z1 / z2
1.5 + 0.5im
julia> - z1
-1.5 - 3.0im
julia> conj(z1), z1'
(1.5 - 3.0im,1.5 - 3.0im)
julia> abs(z1)
3.3541019662496847
julia> z1^z2
-1.102482955327779 - 0.38306415117199305im
julia> real(z1)
1.5
julia> imag(z1)
3.0
| #include <complex.h>
#include <stdio.h>
void cprint(double complex c)
{
printf("%f%+fI", creal(c), cimag(c));
}
void complex_operations() {
double complex a = 1.0 + 1.0I;
double complex b = 3.14159 + 1.2I;
double complex c;
printf("\na="); cprint(a);
printf("\nb="); cprint(b);
c = a + b;
printf("\na+b="); cprint(c);
c = a * b;
printf("\na*b="); cprint(c);
c = 1.0 / a;
printf("\n1/c="); cprint(c);
c = -a;
printf("\n-a="); cprint(c);
c = conj(a);
printf("\nconj a="); cprint(c); printf("\n");
}
|
Write a version of this Julia function in C# with identical behavior. | julia> z1 = 1.5 + 3im
julia> z2 = 1.5 + 1.5im
julia> z1 + z2
3.0 + 4.5im
julia> z1 - z2
0.0 + 1.5im
julia> z1 * z2
-2.25 + 6.75im
julia> z1 / z2
1.5 + 0.5im
julia> - z1
-1.5 - 3.0im
julia> conj(z1), z1'
(1.5 - 3.0im,1.5 - 3.0im)
julia> abs(z1)
3.3541019662496847
julia> z1^z2
-1.102482955327779 - 0.38306415117199305im
julia> real(z1)
1.5
julia> imag(z1)
3.0
| 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);
}
}
}
}
|
Ensure the translated C++ code behaves exactly like the original Julia snippet. | julia> z1 = 1.5 + 3im
julia> z2 = 1.5 + 1.5im
julia> z1 + z2
3.0 + 4.5im
julia> z1 - z2
0.0 + 1.5im
julia> z1 * z2
-2.25 + 6.75im
julia> z1 / z2
1.5 + 0.5im
julia> - z1
-1.5 - 3.0im
julia> conj(z1), z1'
(1.5 - 3.0im,1.5 - 3.0im)
julia> abs(z1)
3.3541019662496847
julia> z1^z2
-1.102482955327779 - 0.38306415117199305im
julia> real(z1)
1.5
julia> imag(z1)
3.0
| #include <iostream>
#include <complex>
using std::complex;
void complex_operations() {
complex<double> a(1.0, 1.0);
complex<double> b(3.14159, 1.25);
std::cout << a + b << std::endl;
std::cout << a * b << std::endl;
std::cout << 1.0 / a << std::endl;
std::cout << -a << std::endl;
std::cout << std::conj(a) << std::endl;
}
|
Port the following code from Julia to Java with equivalent syntax and logic. | julia> z1 = 1.5 + 3im
julia> z2 = 1.5 + 1.5im
julia> z1 + z2
3.0 + 4.5im
julia> z1 - z2
0.0 + 1.5im
julia> z1 * z2
-2.25 + 6.75im
julia> z1 / z2
1.5 + 0.5im
julia> - z1
-1.5 - 3.0im
julia> conj(z1), z1'
(1.5 - 3.0im,1.5 - 3.0im)
julia> abs(z1)
3.3541019662496847
julia> z1^z2
-1.102482955327779 - 0.38306415117199305im
julia> real(z1)
1.5
julia> imag(z1)
3.0
| public class Complex {
public final double real;
public final double imag;
public Complex() {
this(0, 0);
}
public Complex(double r, double i) {
real = r;
imag = i;
}
public Complex add(Complex b) {
return new Complex(this.real + b.real, this.imag + b.imag);
}
public Complex mult(Complex b) {
return new Complex(this.real * b.real - this.imag * b.imag,
this.real * b.imag + this.imag * b.real);
}
public Complex inv() {
double denom = real * real + imag * imag;
return new Complex(real / denom, -imag / denom);
}
public Complex neg() {
return new Complex(-real, -imag);
}
public Complex conj() {
return new Complex(real, -imag);
}
@Override
public String toString() {
return real + " + " + imag + " * i";
}
public static void main(String[] args) {
Complex a = new Complex(Math.PI, -5);
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());
}
}
|
Convert this Julia snippet to Python and keep its semantics consistent. | julia> z1 = 1.5 + 3im
julia> z2 = 1.5 + 1.5im
julia> z1 + z2
3.0 + 4.5im
julia> z1 - z2
0.0 + 1.5im
julia> z1 * z2
-2.25 + 6.75im
julia> z1 / z2
1.5 + 0.5im
julia> - z1
-1.5 - 3.0im
julia> conj(z1), z1'
(1.5 - 3.0im,1.5 - 3.0im)
julia> abs(z1)
3.3541019662496847
julia> z1^z2
-1.102482955327779 - 0.38306415117199305im
julia> real(z1)
1.5
julia> imag(z1)
3.0
| >>> z1 = 1.5 + 3j
>>> z2 = 1.5 + 1.5j
>>> z1 + z2
(3+4.5j)
>>> z1 - z2
1.5j
>>> z1 * z2
(-2.25+6.75j)
>>> z1 / z2
(1.5+0.5j)
>>> - z1
(-1.5-3j)
>>> z1.conjugate()
(1.5-3j)
>>> abs(z1)
3.3541019662496847
>>> z1 ** z2
(-1.1024829553277784-0.38306415117199333j)
>>> z1.real
1.5
>>> z1.imag
3.0
>>>
|
Convert this Julia block to Go, preserving its control flow and logic. | julia> z1 = 1.5 + 3im
julia> z2 = 1.5 + 1.5im
julia> z1 + z2
3.0 + 4.5im
julia> z1 - z2
0.0 + 1.5im
julia> z1 * z2
-2.25 + 6.75im
julia> z1 / z2
1.5 + 0.5im
julia> - z1
-1.5 - 3.0im
julia> conj(z1), z1'
(1.5 - 3.0im,1.5 - 3.0im)
julia> abs(z1)
3.3541019662496847
julia> z1^z2
-1.102482955327779 - 0.38306415117199305im
julia> real(z1)
1.5
julia> imag(z1)
3.0
| package main
import (
"fmt"
"math/cmplx"
)
func main() {
a := 1 + 1i
b := 3.14159 + 1.25i
fmt.Println("a: ", a)
fmt.Println("b: ", b)
fmt.Println("a + b: ", a+b)
fmt.Println("a * b: ", a*b)
fmt.Println("-a: ", -a)
fmt.Println("1 / a: ", 1/a)
fmt.Println("a̅: ", cmplx.Conj(a))
}
|
Rewrite this program in C while keeping its functionality equivalent to the Lua version. |
complex = setmetatable({
__add = function(u, v) return complex(u.real + v.real, u.imag + v.imag) end,
__sub = function(u, v) return complex(u.real - v.real, u.imag - v.imag) end,
__mul = function(u, v) return complex(u.real * v.real - u.imag * v.imag, u.real * v.imag + u.imag * v.real) end,
__div = function(u, v) return u * complex(v.real / v.norm, -v.imag / v.norm) end,
__unm = function(u) return complex(-u.real, -u.imag) end,
__concat = function(u, v)
if type(u) == "table" then return u.real .. " + " .. u.imag .. "i" .. v
elseif type(u) == "string" or type(u) == "number" then return u .. v.real .. " + " .. v.imag .. "i"
end end,
__index = function(u, index)
local operations = {
norm = function(u) return u.real ^ 2 + u.imag ^ 2 end,
conj = function(u) return complex(u.real, -u.imag) end,
}
return operations[index] and operations[index](u)
end,
__newindex = function() error() end
}, {
__call = function(z, realpart, imagpart) return setmetatable({real = realpart, imag = imagpart}, complex) end
} )
local i, j = complex(2, 3), complex(1, 1)
print(i .. " + " .. j .. " = " .. (i+j))
print(i .. " - " .. j .. " = " .. (i-j))
print(i .. " * " .. j .. " = " .. (i*j))
print(i .. " / " .. j .. " = " .. (i/j))
print("|" .. i .. "| = " .. math.sqrt(i.norm))
print(i .. "* = " .. i.conj)
| #include <complex.h>
#include <stdio.h>
void cprint(double complex c)
{
printf("%f%+fI", creal(c), cimag(c));
}
void complex_operations() {
double complex a = 1.0 + 1.0I;
double complex b = 3.14159 + 1.2I;
double complex c;
printf("\na="); cprint(a);
printf("\nb="); cprint(b);
c = a + b;
printf("\na+b="); cprint(c);
c = a * b;
printf("\na*b="); cprint(c);
c = 1.0 / a;
printf("\n1/c="); cprint(c);
c = -a;
printf("\n-a="); cprint(c);
c = conj(a);
printf("\nconj a="); cprint(c); printf("\n");
}
|
Change the following Lua code into C# without altering its purpose. |
complex = setmetatable({
__add = function(u, v) return complex(u.real + v.real, u.imag + v.imag) end,
__sub = function(u, v) return complex(u.real - v.real, u.imag - v.imag) end,
__mul = function(u, v) return complex(u.real * v.real - u.imag * v.imag, u.real * v.imag + u.imag * v.real) end,
__div = function(u, v) return u * complex(v.real / v.norm, -v.imag / v.norm) end,
__unm = function(u) return complex(-u.real, -u.imag) end,
__concat = function(u, v)
if type(u) == "table" then return u.real .. " + " .. u.imag .. "i" .. v
elseif type(u) == "string" or type(u) == "number" then return u .. v.real .. " + " .. v.imag .. "i"
end end,
__index = function(u, index)
local operations = {
norm = function(u) return u.real ^ 2 + u.imag ^ 2 end,
conj = function(u) return complex(u.real, -u.imag) end,
}
return operations[index] and operations[index](u)
end,
__newindex = function() error() end
}, {
__call = function(z, realpart, imagpart) return setmetatable({real = realpart, imag = imagpart}, complex) end
} )
local i, j = complex(2, 3), complex(1, 1)
print(i .. " + " .. j .. " = " .. (i+j))
print(i .. " - " .. j .. " = " .. (i-j))
print(i .. " * " .. j .. " = " .. (i*j))
print(i .. " / " .. j .. " = " .. (i/j))
print("|" .. i .. "| = " .. math.sqrt(i.norm))
print(i .. "* = " .. i.conj)
| 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);
}
}
}
}
|
Rewrite the snippet below in C++ so it works the same as the original Lua code. |
complex = setmetatable({
__add = function(u, v) return complex(u.real + v.real, u.imag + v.imag) end,
__sub = function(u, v) return complex(u.real - v.real, u.imag - v.imag) end,
__mul = function(u, v) return complex(u.real * v.real - u.imag * v.imag, u.real * v.imag + u.imag * v.real) end,
__div = function(u, v) return u * complex(v.real / v.norm, -v.imag / v.norm) end,
__unm = function(u) return complex(-u.real, -u.imag) end,
__concat = function(u, v)
if type(u) == "table" then return u.real .. " + " .. u.imag .. "i" .. v
elseif type(u) == "string" or type(u) == "number" then return u .. v.real .. " + " .. v.imag .. "i"
end end,
__index = function(u, index)
local operations = {
norm = function(u) return u.real ^ 2 + u.imag ^ 2 end,
conj = function(u) return complex(u.real, -u.imag) end,
}
return operations[index] and operations[index](u)
end,
__newindex = function() error() end
}, {
__call = function(z, realpart, imagpart) return setmetatable({real = realpart, imag = imagpart}, complex) end
} )
local i, j = complex(2, 3), complex(1, 1)
print(i .. " + " .. j .. " = " .. (i+j))
print(i .. " - " .. j .. " = " .. (i-j))
print(i .. " * " .. j .. " = " .. (i*j))
print(i .. " / " .. j .. " = " .. (i/j))
print("|" .. i .. "| = " .. math.sqrt(i.norm))
print(i .. "* = " .. i.conj)
| #include <iostream>
#include <complex>
using std::complex;
void complex_operations() {
complex<double> a(1.0, 1.0);
complex<double> b(3.14159, 1.25);
std::cout << a + b << std::endl;
std::cout << a * b << std::endl;
std::cout << 1.0 / a << std::endl;
std::cout << -a << std::endl;
std::cout << std::conj(a) << std::endl;
}
|
Convert this Lua block to Java, preserving its control flow and logic. |
complex = setmetatable({
__add = function(u, v) return complex(u.real + v.real, u.imag + v.imag) end,
__sub = function(u, v) return complex(u.real - v.real, u.imag - v.imag) end,
__mul = function(u, v) return complex(u.real * v.real - u.imag * v.imag, u.real * v.imag + u.imag * v.real) end,
__div = function(u, v) return u * complex(v.real / v.norm, -v.imag / v.norm) end,
__unm = function(u) return complex(-u.real, -u.imag) end,
__concat = function(u, v)
if type(u) == "table" then return u.real .. " + " .. u.imag .. "i" .. v
elseif type(u) == "string" or type(u) == "number" then return u .. v.real .. " + " .. v.imag .. "i"
end end,
__index = function(u, index)
local operations = {
norm = function(u) return u.real ^ 2 + u.imag ^ 2 end,
conj = function(u) return complex(u.real, -u.imag) end,
}
return operations[index] and operations[index](u)
end,
__newindex = function() error() end
}, {
__call = function(z, realpart, imagpart) return setmetatable({real = realpart, imag = imagpart}, complex) end
} )
local i, j = complex(2, 3), complex(1, 1)
print(i .. " + " .. j .. " = " .. (i+j))
print(i .. " - " .. j .. " = " .. (i-j))
print(i .. " * " .. j .. " = " .. (i*j))
print(i .. " / " .. j .. " = " .. (i/j))
print("|" .. i .. "| = " .. math.sqrt(i.norm))
print(i .. "* = " .. i.conj)
| public class Complex {
public final double real;
public final double imag;
public Complex() {
this(0, 0);
}
public Complex(double r, double i) {
real = r;
imag = i;
}
public Complex add(Complex b) {
return new Complex(this.real + b.real, this.imag + b.imag);
}
public Complex mult(Complex b) {
return new Complex(this.real * b.real - this.imag * b.imag,
this.real * b.imag + this.imag * b.real);
}
public Complex inv() {
double denom = real * real + imag * imag;
return new Complex(real / denom, -imag / denom);
}
public Complex neg() {
return new Complex(-real, -imag);
}
public Complex conj() {
return new Complex(real, -imag);
}
@Override
public String toString() {
return real + " + " + imag + " * i";
}
public static void main(String[] args) {
Complex a = new Complex(Math.PI, -5);
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());
}
}
|
Port the following code from Lua to Python with equivalent syntax and logic. |
complex = setmetatable({
__add = function(u, v) return complex(u.real + v.real, u.imag + v.imag) end,
__sub = function(u, v) return complex(u.real - v.real, u.imag - v.imag) end,
__mul = function(u, v) return complex(u.real * v.real - u.imag * v.imag, u.real * v.imag + u.imag * v.real) end,
__div = function(u, v) return u * complex(v.real / v.norm, -v.imag / v.norm) end,
__unm = function(u) return complex(-u.real, -u.imag) end,
__concat = function(u, v)
if type(u) == "table" then return u.real .. " + " .. u.imag .. "i" .. v
elseif type(u) == "string" or type(u) == "number" then return u .. v.real .. " + " .. v.imag .. "i"
end end,
__index = function(u, index)
local operations = {
norm = function(u) return u.real ^ 2 + u.imag ^ 2 end,
conj = function(u) return complex(u.real, -u.imag) end,
}
return operations[index] and operations[index](u)
end,
__newindex = function() error() end
}, {
__call = function(z, realpart, imagpart) return setmetatable({real = realpart, imag = imagpart}, complex) end
} )
local i, j = complex(2, 3), complex(1, 1)
print(i .. " + " .. j .. " = " .. (i+j))
print(i .. " - " .. j .. " = " .. (i-j))
print(i .. " * " .. j .. " = " .. (i*j))
print(i .. " / " .. j .. " = " .. (i/j))
print("|" .. i .. "| = " .. math.sqrt(i.norm))
print(i .. "* = " .. i.conj)
| >>> z1 = 1.5 + 3j
>>> z2 = 1.5 + 1.5j
>>> z1 + z2
(3+4.5j)
>>> z1 - z2
1.5j
>>> z1 * z2
(-2.25+6.75j)
>>> z1 / z2
(1.5+0.5j)
>>> - z1
(-1.5-3j)
>>> z1.conjugate()
(1.5-3j)
>>> abs(z1)
3.3541019662496847
>>> z1 ** z2
(-1.1024829553277784-0.38306415117199333j)
>>> z1.real
1.5
>>> z1.imag
3.0
>>>
|
Rewrite this program in Go while keeping its functionality equivalent to the Lua version. |
complex = setmetatable({
__add = function(u, v) return complex(u.real + v.real, u.imag + v.imag) end,
__sub = function(u, v) return complex(u.real - v.real, u.imag - v.imag) end,
__mul = function(u, v) return complex(u.real * v.real - u.imag * v.imag, u.real * v.imag + u.imag * v.real) end,
__div = function(u, v) return u * complex(v.real / v.norm, -v.imag / v.norm) end,
__unm = function(u) return complex(-u.real, -u.imag) end,
__concat = function(u, v)
if type(u) == "table" then return u.real .. " + " .. u.imag .. "i" .. v
elseif type(u) == "string" or type(u) == "number" then return u .. v.real .. " + " .. v.imag .. "i"
end end,
__index = function(u, index)
local operations = {
norm = function(u) return u.real ^ 2 + u.imag ^ 2 end,
conj = function(u) return complex(u.real, -u.imag) end,
}
return operations[index] and operations[index](u)
end,
__newindex = function() error() end
}, {
__call = function(z, realpart, imagpart) return setmetatable({real = realpart, imag = imagpart}, complex) end
} )
local i, j = complex(2, 3), complex(1, 1)
print(i .. " + " .. j .. " = " .. (i+j))
print(i .. " - " .. j .. " = " .. (i-j))
print(i .. " * " .. j .. " = " .. (i*j))
print(i .. " / " .. j .. " = " .. (i/j))
print("|" .. i .. "| = " .. math.sqrt(i.norm))
print(i .. "* = " .. i.conj)
| package main
import (
"fmt"
"math/cmplx"
)
func main() {
a := 1 + 1i
b := 3.14159 + 1.25i
fmt.Println("a: ", a)
fmt.Println("b: ", b)
fmt.Println("a + b: ", a+b)
fmt.Println("a * b: ", a*b)
fmt.Println("-a: ", -a)
fmt.Println("1 / a: ", 1/a)
fmt.Println("a̅: ", cmplx.Conj(a))
}
|
Convert the following code from Mathematica to C, ensuring the logic remains intact. | x=1+2I
y=3+4I
x+y => 4 + 6 I
x-y => -2 - 2 I
y x => -5 + 10 I
y/x => 11/5 - (2 I)/5
x^3 => -11 - 2 I
y^4 => -527 - 336 I
x^y => (1 + 2 I)^(3 + 4 I)
N[x^y] => 0.12901 + 0.0339241 I
| #include <complex.h>
#include <stdio.h>
void cprint(double complex c)
{
printf("%f%+fI", creal(c), cimag(c));
}
void complex_operations() {
double complex a = 1.0 + 1.0I;
double complex b = 3.14159 + 1.2I;
double complex c;
printf("\na="); cprint(a);
printf("\nb="); cprint(b);
c = a + b;
printf("\na+b="); cprint(c);
c = a * b;
printf("\na*b="); cprint(c);
c = 1.0 / a;
printf("\n1/c="); cprint(c);
c = -a;
printf("\n-a="); cprint(c);
c = conj(a);
printf("\nconj a="); cprint(c); printf("\n");
}
|
Change the programming language of this snippet from Mathematica to C# without modifying what it does. | x=1+2I
y=3+4I
x+y => 4 + 6 I
x-y => -2 - 2 I
y x => -5 + 10 I
y/x => 11/5 - (2 I)/5
x^3 => -11 - 2 I
y^4 => -527 - 336 I
x^y => (1 + 2 I)^(3 + 4 I)
N[x^y] => 0.12901 + 0.0339241 I
| 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);
}
}
}
}
|
Produce a language-to-language conversion: from Mathematica to C++, same semantics. | x=1+2I
y=3+4I
x+y => 4 + 6 I
x-y => -2 - 2 I
y x => -5 + 10 I
y/x => 11/5 - (2 I)/5
x^3 => -11 - 2 I
y^4 => -527 - 336 I
x^y => (1 + 2 I)^(3 + 4 I)
N[x^y] => 0.12901 + 0.0339241 I
| #include <iostream>
#include <complex>
using std::complex;
void complex_operations() {
complex<double> a(1.0, 1.0);
complex<double> b(3.14159, 1.25);
std::cout << a + b << std::endl;
std::cout << a * b << std::endl;
std::cout << 1.0 / a << std::endl;
std::cout << -a << std::endl;
std::cout << std::conj(a) << std::endl;
}
|
Maintain the same structure and functionality when rewriting this code in Java. | x=1+2I
y=3+4I
x+y => 4 + 6 I
x-y => -2 - 2 I
y x => -5 + 10 I
y/x => 11/5 - (2 I)/5
x^3 => -11 - 2 I
y^4 => -527 - 336 I
x^y => (1 + 2 I)^(3 + 4 I)
N[x^y] => 0.12901 + 0.0339241 I
| public class Complex {
public final double real;
public final double imag;
public Complex() {
this(0, 0);
}
public Complex(double r, double i) {
real = r;
imag = i;
}
public Complex add(Complex b) {
return new Complex(this.real + b.real, this.imag + b.imag);
}
public Complex mult(Complex b) {
return new Complex(this.real * b.real - this.imag * b.imag,
this.real * b.imag + this.imag * b.real);
}
public Complex inv() {
double denom = real * real + imag * imag;
return new Complex(real / denom, -imag / denom);
}
public Complex neg() {
return new Complex(-real, -imag);
}
public Complex conj() {
return new Complex(real, -imag);
}
@Override
public String toString() {
return real + " + " + imag + " * i";
}
public static void main(String[] args) {
Complex a = new Complex(Math.PI, -5);
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());
}
}
|
Transform the following Mathematica implementation into Python, maintaining the same output and logic. | x=1+2I
y=3+4I
x+y => 4 + 6 I
x-y => -2 - 2 I
y x => -5 + 10 I
y/x => 11/5 - (2 I)/5
x^3 => -11 - 2 I
y^4 => -527 - 336 I
x^y => (1 + 2 I)^(3 + 4 I)
N[x^y] => 0.12901 + 0.0339241 I
| >>> z1 = 1.5 + 3j
>>> z2 = 1.5 + 1.5j
>>> z1 + z2
(3+4.5j)
>>> z1 - z2
1.5j
>>> z1 * z2
(-2.25+6.75j)
>>> z1 / z2
(1.5+0.5j)
>>> - z1
(-1.5-3j)
>>> z1.conjugate()
(1.5-3j)
>>> abs(z1)
3.3541019662496847
>>> z1 ** z2
(-1.1024829553277784-0.38306415117199333j)
>>> z1.real
1.5
>>> z1.imag
3.0
>>>
|
Change the programming language of this snippet from Mathematica to Go without modifying what it does. | x=1+2I
y=3+4I
x+y => 4 + 6 I
x-y => -2 - 2 I
y x => -5 + 10 I
y/x => 11/5 - (2 I)/5
x^3 => -11 - 2 I
y^4 => -527 - 336 I
x^y => (1 + 2 I)^(3 + 4 I)
N[x^y] => 0.12901 + 0.0339241 I
| package main
import (
"fmt"
"math/cmplx"
)
func main() {
a := 1 + 1i
b := 3.14159 + 1.25i
fmt.Println("a: ", a)
fmt.Println("b: ", b)
fmt.Println("a + b: ", a+b)
fmt.Println("a * b: ", a*b)
fmt.Println("-a: ", -a)
fmt.Println("1 / a: ", 1/a)
fmt.Println("a̅: ", cmplx.Conj(a))
}
|
Rewrite the snippet below in C so it works the same as the original MATLAB code. | >> a = 1+i
a =
1.000000000000000 + 1.000000000000000i
>> b = 3+7i
b =
3.000000000000000 + 7.000000000000000i
>> a+b
ans =
4.000000000000000 + 8.000000000000000i
>> a-b
ans =
-2.000000000000000 - 6.000000000000000i
>> a*b
ans =
-4.000000000000000 +10.000000000000000i
>> a/b
ans =
0.172413793103448 - 0.068965517241379i
>> -a
ans =
-1.000000000000000 - 1.000000000000000i
>> a'
ans =
1.000000000000000 - 1.000000000000000i
>> a^b
ans =
0.000808197112874 - 0.011556516327187i
>> norm(a)
ans =
1.414213562373095
| #include <complex.h>
#include <stdio.h>
void cprint(double complex c)
{
printf("%f%+fI", creal(c), cimag(c));
}
void complex_operations() {
double complex a = 1.0 + 1.0I;
double complex b = 3.14159 + 1.2I;
double complex c;
printf("\na="); cprint(a);
printf("\nb="); cprint(b);
c = a + b;
printf("\na+b="); cprint(c);
c = a * b;
printf("\na*b="); cprint(c);
c = 1.0 / a;
printf("\n1/c="); cprint(c);
c = -a;
printf("\n-a="); cprint(c);
c = conj(a);
printf("\nconj a="); cprint(c); printf("\n");
}
|
Produce a language-to-language conversion: from MATLAB to C#, same semantics. | >> a = 1+i
a =
1.000000000000000 + 1.000000000000000i
>> b = 3+7i
b =
3.000000000000000 + 7.000000000000000i
>> a+b
ans =
4.000000000000000 + 8.000000000000000i
>> a-b
ans =
-2.000000000000000 - 6.000000000000000i
>> a*b
ans =
-4.000000000000000 +10.000000000000000i
>> a/b
ans =
0.172413793103448 - 0.068965517241379i
>> -a
ans =
-1.000000000000000 - 1.000000000000000i
>> a'
ans =
1.000000000000000 - 1.000000000000000i
>> a^b
ans =
0.000808197112874 - 0.011556516327187i
>> norm(a)
ans =
1.414213562373095
| 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);
}
}
}
}
|
Transform the following MATLAB implementation into C++, maintaining the same output and logic. | >> a = 1+i
a =
1.000000000000000 + 1.000000000000000i
>> b = 3+7i
b =
3.000000000000000 + 7.000000000000000i
>> a+b
ans =
4.000000000000000 + 8.000000000000000i
>> a-b
ans =
-2.000000000000000 - 6.000000000000000i
>> a*b
ans =
-4.000000000000000 +10.000000000000000i
>> a/b
ans =
0.172413793103448 - 0.068965517241379i
>> -a
ans =
-1.000000000000000 - 1.000000000000000i
>> a'
ans =
1.000000000000000 - 1.000000000000000i
>> a^b
ans =
0.000808197112874 - 0.011556516327187i
>> norm(a)
ans =
1.414213562373095
| #include <iostream>
#include <complex>
using std::complex;
void complex_operations() {
complex<double> a(1.0, 1.0);
complex<double> b(3.14159, 1.25);
std::cout << a + b << std::endl;
std::cout << a * b << std::endl;
std::cout << 1.0 / a << std::endl;
std::cout << -a << std::endl;
std::cout << std::conj(a) << std::endl;
}
|
Generate a Java translation of this MATLAB snippet without changing its computational steps. | >> a = 1+i
a =
1.000000000000000 + 1.000000000000000i
>> b = 3+7i
b =
3.000000000000000 + 7.000000000000000i
>> a+b
ans =
4.000000000000000 + 8.000000000000000i
>> a-b
ans =
-2.000000000000000 - 6.000000000000000i
>> a*b
ans =
-4.000000000000000 +10.000000000000000i
>> a/b
ans =
0.172413793103448 - 0.068965517241379i
>> -a
ans =
-1.000000000000000 - 1.000000000000000i
>> a'
ans =
1.000000000000000 - 1.000000000000000i
>> a^b
ans =
0.000808197112874 - 0.011556516327187i
>> norm(a)
ans =
1.414213562373095
| public class Complex {
public final double real;
public final double imag;
public Complex() {
this(0, 0);
}
public Complex(double r, double i) {
real = r;
imag = i;
}
public Complex add(Complex b) {
return new Complex(this.real + b.real, this.imag + b.imag);
}
public Complex mult(Complex b) {
return new Complex(this.real * b.real - this.imag * b.imag,
this.real * b.imag + this.imag * b.real);
}
public Complex inv() {
double denom = real * real + imag * imag;
return new Complex(real / denom, -imag / denom);
}
public Complex neg() {
return new Complex(-real, -imag);
}
public Complex conj() {
return new Complex(real, -imag);
}
@Override
public String toString() {
return real + " + " + imag + " * i";
}
public static void main(String[] args) {
Complex a = new Complex(Math.PI, -5);
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());
}
}
|
Transform the following MATLAB implementation into Python, maintaining the same output and logic. | >> a = 1+i
a =
1.000000000000000 + 1.000000000000000i
>> b = 3+7i
b =
3.000000000000000 + 7.000000000000000i
>> a+b
ans =
4.000000000000000 + 8.000000000000000i
>> a-b
ans =
-2.000000000000000 - 6.000000000000000i
>> a*b
ans =
-4.000000000000000 +10.000000000000000i
>> a/b
ans =
0.172413793103448 - 0.068965517241379i
>> -a
ans =
-1.000000000000000 - 1.000000000000000i
>> a'
ans =
1.000000000000000 - 1.000000000000000i
>> a^b
ans =
0.000808197112874 - 0.011556516327187i
>> norm(a)
ans =
1.414213562373095
| >>> z1 = 1.5 + 3j
>>> z2 = 1.5 + 1.5j
>>> z1 + z2
(3+4.5j)
>>> z1 - z2
1.5j
>>> z1 * z2
(-2.25+6.75j)
>>> z1 / z2
(1.5+0.5j)
>>> - z1
(-1.5-3j)
>>> z1.conjugate()
(1.5-3j)
>>> abs(z1)
3.3541019662496847
>>> z1 ** z2
(-1.1024829553277784-0.38306415117199333j)
>>> z1.real
1.5
>>> z1.imag
3.0
>>>
|
Convert this MATLAB snippet to Go and keep its semantics consistent. | >> a = 1+i
a =
1.000000000000000 + 1.000000000000000i
>> b = 3+7i
b =
3.000000000000000 + 7.000000000000000i
>> a+b
ans =
4.000000000000000 + 8.000000000000000i
>> a-b
ans =
-2.000000000000000 - 6.000000000000000i
>> a*b
ans =
-4.000000000000000 +10.000000000000000i
>> a/b
ans =
0.172413793103448 - 0.068965517241379i
>> -a
ans =
-1.000000000000000 - 1.000000000000000i
>> a'
ans =
1.000000000000000 - 1.000000000000000i
>> a^b
ans =
0.000808197112874 - 0.011556516327187i
>> norm(a)
ans =
1.414213562373095
| package main
import (
"fmt"
"math/cmplx"
)
func main() {
a := 1 + 1i
b := 3.14159 + 1.25i
fmt.Println("a: ", a)
fmt.Println("b: ", b)
fmt.Println("a + b: ", a+b)
fmt.Println("a * b: ", a*b)
fmt.Println("-a: ", -a)
fmt.Println("1 / a: ", 1/a)
fmt.Println("a̅: ", cmplx.Conj(a))
}
|
Write a version of this Nim function in C with identical behavior. | import complex
var a: Complex = (1.0,1.0)
var b: Complex = (3.1415,1.2)
echo("a : " & $a)
echo("b : " & $b)
echo("a + b: " & $(a + b))
echo("a * b: " & $(a * b))
echo("1/a : " & $(1/a))
echo("-a : " & $(-a))
| #include <complex.h>
#include <stdio.h>
void cprint(double complex c)
{
printf("%f%+fI", creal(c), cimag(c));
}
void complex_operations() {
double complex a = 1.0 + 1.0I;
double complex b = 3.14159 + 1.2I;
double complex c;
printf("\na="); cprint(a);
printf("\nb="); cprint(b);
c = a + b;
printf("\na+b="); cprint(c);
c = a * b;
printf("\na*b="); cprint(c);
c = 1.0 / a;
printf("\n1/c="); cprint(c);
c = -a;
printf("\n-a="); cprint(c);
c = conj(a);
printf("\nconj a="); cprint(c); printf("\n");
}
|
Write the same algorithm in C# as shown in this Nim implementation. | import complex
var a: Complex = (1.0,1.0)
var b: Complex = (3.1415,1.2)
echo("a : " & $a)
echo("b : " & $b)
echo("a + b: " & $(a + b))
echo("a * b: " & $(a * b))
echo("1/a : " & $(1/a))
echo("-a : " & $(-a))
| 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);
}
}
}
}
|
Can you help me rewrite this code in C++ instead of Nim, keeping it the same logically? | import complex
var a: Complex = (1.0,1.0)
var b: Complex = (3.1415,1.2)
echo("a : " & $a)
echo("b : " & $b)
echo("a + b: " & $(a + b))
echo("a * b: " & $(a * b))
echo("1/a : " & $(1/a))
echo("-a : " & $(-a))
| #include <iostream>
#include <complex>
using std::complex;
void complex_operations() {
complex<double> a(1.0, 1.0);
complex<double> b(3.14159, 1.25);
std::cout << a + b << std::endl;
std::cout << a * b << std::endl;
std::cout << 1.0 / a << std::endl;
std::cout << -a << std::endl;
std::cout << std::conj(a) << std::endl;
}
|
Keep all operations the same but rewrite the snippet in Java. | import complex
var a: Complex = (1.0,1.0)
var b: Complex = (3.1415,1.2)
echo("a : " & $a)
echo("b : " & $b)
echo("a + b: " & $(a + b))
echo("a * b: " & $(a * b))
echo("1/a : " & $(1/a))
echo("-a : " & $(-a))
| public class Complex {
public final double real;
public final double imag;
public Complex() {
this(0, 0);
}
public Complex(double r, double i) {
real = r;
imag = i;
}
public Complex add(Complex b) {
return new Complex(this.real + b.real, this.imag + b.imag);
}
public Complex mult(Complex b) {
return new Complex(this.real * b.real - this.imag * b.imag,
this.real * b.imag + this.imag * b.real);
}
public Complex inv() {
double denom = real * real + imag * imag;
return new Complex(real / denom, -imag / denom);
}
public Complex neg() {
return new Complex(-real, -imag);
}
public Complex conj() {
return new Complex(real, -imag);
}
@Override
public String toString() {
return real + " + " + imag + " * i";
}
public static void main(String[] args) {
Complex a = new Complex(Math.PI, -5);
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());
}
}
|
Generate a Python translation of this Nim snippet without changing its computational steps. | import complex
var a: Complex = (1.0,1.0)
var b: Complex = (3.1415,1.2)
echo("a : " & $a)
echo("b : " & $b)
echo("a + b: " & $(a + b))
echo("a * b: " & $(a * b))
echo("1/a : " & $(1/a))
echo("-a : " & $(-a))
| >>> z1 = 1.5 + 3j
>>> z2 = 1.5 + 1.5j
>>> z1 + z2
(3+4.5j)
>>> z1 - z2
1.5j
>>> z1 * z2
(-2.25+6.75j)
>>> z1 / z2
(1.5+0.5j)
>>> - z1
(-1.5-3j)
>>> z1.conjugate()
(1.5-3j)
>>> abs(z1)
3.3541019662496847
>>> z1 ** z2
(-1.1024829553277784-0.38306415117199333j)
>>> z1.real
1.5
>>> z1.imag
3.0
>>>
|
Preserve the algorithm and functionality while converting the code from Nim to Go. | import complex
var a: Complex = (1.0,1.0)
var b: Complex = (3.1415,1.2)
echo("a : " & $a)
echo("b : " & $b)
echo("a + b: " & $(a + b))
echo("a * b: " & $(a * b))
echo("1/a : " & $(1/a))
echo("-a : " & $(-a))
| package main
import (
"fmt"
"math/cmplx"
)
func main() {
a := 1 + 1i
b := 3.14159 + 1.25i
fmt.Println("a: ", a)
fmt.Println("b: ", b)
fmt.Println("a + b: ", a+b)
fmt.Println("a * b: ", a*b)
fmt.Println("-a: ", -a)
fmt.Println("1 / a: ", 1/a)
fmt.Println("a̅: ", cmplx.Conj(a))
}
|
Translate this program into C but keep the logic exactly as in OCaml. | open Complex
let print_complex z =
Printf.printf "%f + %f i\n" z.re z.im
let () =
let a = { re = 1.0; im = 1.0 }
and b = { re = 3.14159; im = 1.25 } in
print_complex (add a b);
print_complex (mul a b);
print_complex (inv a);
print_complex (neg a);
print_complex (conj a)
| #include <complex.h>
#include <stdio.h>
void cprint(double complex c)
{
printf("%f%+fI", creal(c), cimag(c));
}
void complex_operations() {
double complex a = 1.0 + 1.0I;
double complex b = 3.14159 + 1.2I;
double complex c;
printf("\na="); cprint(a);
printf("\nb="); cprint(b);
c = a + b;
printf("\na+b="); cprint(c);
c = a * b;
printf("\na*b="); cprint(c);
c = 1.0 / a;
printf("\n1/c="); cprint(c);
c = -a;
printf("\n-a="); cprint(c);
c = conj(a);
printf("\nconj a="); cprint(c); printf("\n");
}
|
Translate the given OCaml code snippet into C# without altering its behavior. | open Complex
let print_complex z =
Printf.printf "%f + %f i\n" z.re z.im
let () =
let a = { re = 1.0; im = 1.0 }
and b = { re = 3.14159; im = 1.25 } in
print_complex (add a b);
print_complex (mul a b);
print_complex (inv a);
print_complex (neg a);
print_complex (conj a)
| 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);
}
}
}
}
|
Produce a functionally identical C++ code for the snippet given in OCaml. | open Complex
let print_complex z =
Printf.printf "%f + %f i\n" z.re z.im
let () =
let a = { re = 1.0; im = 1.0 }
and b = { re = 3.14159; im = 1.25 } in
print_complex (add a b);
print_complex (mul a b);
print_complex (inv a);
print_complex (neg a);
print_complex (conj a)
| #include <iostream>
#include <complex>
using std::complex;
void complex_operations() {
complex<double> a(1.0, 1.0);
complex<double> b(3.14159, 1.25);
std::cout << a + b << std::endl;
std::cout << a * b << std::endl;
std::cout << 1.0 / a << std::endl;
std::cout << -a << std::endl;
std::cout << std::conj(a) << std::endl;
}
|
Convert this OCaml snippet to Java and keep its semantics consistent. | open Complex
let print_complex z =
Printf.printf "%f + %f i\n" z.re z.im
let () =
let a = { re = 1.0; im = 1.0 }
and b = { re = 3.14159; im = 1.25 } in
print_complex (add a b);
print_complex (mul a b);
print_complex (inv a);
print_complex (neg a);
print_complex (conj a)
| public class Complex {
public final double real;
public final double imag;
public Complex() {
this(0, 0);
}
public Complex(double r, double i) {
real = r;
imag = i;
}
public Complex add(Complex b) {
return new Complex(this.real + b.real, this.imag + b.imag);
}
public Complex mult(Complex b) {
return new Complex(this.real * b.real - this.imag * b.imag,
this.real * b.imag + this.imag * b.real);
}
public Complex inv() {
double denom = real * real + imag * imag;
return new Complex(real / denom, -imag / denom);
}
public Complex neg() {
return new Complex(-real, -imag);
}
public Complex conj() {
return new Complex(real, -imag);
}
@Override
public String toString() {
return real + " + " + imag + " * i";
}
public static void main(String[] args) {
Complex a = new Complex(Math.PI, -5);
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());
}
}
|
Ensure the translated Python code behaves exactly like the original OCaml snippet. | open Complex
let print_complex z =
Printf.printf "%f + %f i\n" z.re z.im
let () =
let a = { re = 1.0; im = 1.0 }
and b = { re = 3.14159; im = 1.25 } in
print_complex (add a b);
print_complex (mul a b);
print_complex (inv a);
print_complex (neg a);
print_complex (conj a)
| >>> z1 = 1.5 + 3j
>>> z2 = 1.5 + 1.5j
>>> z1 + z2
(3+4.5j)
>>> z1 - z2
1.5j
>>> z1 * z2
(-2.25+6.75j)
>>> z1 / z2
(1.5+0.5j)
>>> - z1
(-1.5-3j)
>>> z1.conjugate()
(1.5-3j)
>>> abs(z1)
3.3541019662496847
>>> z1 ** z2
(-1.1024829553277784-0.38306415117199333j)
>>> z1.real
1.5
>>> z1.imag
3.0
>>>
|
Preserve the algorithm and functionality while converting the code from OCaml to Go. | open Complex
let print_complex z =
Printf.printf "%f + %f i\n" z.re z.im
let () =
let a = { re = 1.0; im = 1.0 }
and b = { re = 3.14159; im = 1.25 } in
print_complex (add a b);
print_complex (mul a b);
print_complex (inv a);
print_complex (neg a);
print_complex (conj a)
| package main
import (
"fmt"
"math/cmplx"
)
func main() {
a := 1 + 1i
b := 3.14159 + 1.25i
fmt.Println("a: ", a)
fmt.Println("b: ", b)
fmt.Println("a + b: ", a+b)
fmt.Println("a * b: ", a*b)
fmt.Println("-a: ", -a)
fmt.Println("1 / a: ", 1/a)
fmt.Println("a̅: ", cmplx.Conj(a))
}
|
Rewrite this program in C while keeping its functionality equivalent to the Pascal version. | Program ComplexDemo;
uses
ucomplex;
var
a, b, absum, abprod, aneg, ainv, acong: complex;
function complex(const re, im: real): ucomplex.complex; overload;
begin
complex.re := re;
complex.im := im;
end;
begin
a := complex(5, 3);
b := complex(0.5, 6.0);
absum := a + b;
writeln ('(5 + i3) + (0.5 + i6.0): ', absum.re:3:1, ' + i', absum.im:3:1);
abprod := a * b;
writeln ('(5 + i3) * (0.5 + i6.0): ', abprod.re:5:1, ' + i', abprod.im:4:1);
aneg := -a;
writeln ('-(5 + i3): ', aneg.re:3:1, ' + i', aneg.im:3:1);
ainv := 1.0 / a;
writeln ('1/(5 + i3): ', ainv.re:3:1, ' + i', ainv.im:3:1);
acong := cong(a);
writeln ('conj(5 + i3): ', acong.re:3:1, ' + i', acong.im:3:1);
end.
| #include <complex.h>
#include <stdio.h>
void cprint(double complex c)
{
printf("%f%+fI", creal(c), cimag(c));
}
void complex_operations() {
double complex a = 1.0 + 1.0I;
double complex b = 3.14159 + 1.2I;
double complex c;
printf("\na="); cprint(a);
printf("\nb="); cprint(b);
c = a + b;
printf("\na+b="); cprint(c);
c = a * b;
printf("\na*b="); cprint(c);
c = 1.0 / a;
printf("\n1/c="); cprint(c);
c = -a;
printf("\n-a="); cprint(c);
c = conj(a);
printf("\nconj a="); cprint(c); printf("\n");
}
|
Keep all operations the same but rewrite the snippet in C#. | Program ComplexDemo;
uses
ucomplex;
var
a, b, absum, abprod, aneg, ainv, acong: complex;
function complex(const re, im: real): ucomplex.complex; overload;
begin
complex.re := re;
complex.im := im;
end;
begin
a := complex(5, 3);
b := complex(0.5, 6.0);
absum := a + b;
writeln ('(5 + i3) + (0.5 + i6.0): ', absum.re:3:1, ' + i', absum.im:3:1);
abprod := a * b;
writeln ('(5 + i3) * (0.5 + i6.0): ', abprod.re:5:1, ' + i', abprod.im:4:1);
aneg := -a;
writeln ('-(5 + i3): ', aneg.re:3:1, ' + i', aneg.im:3:1);
ainv := 1.0 / a;
writeln ('1/(5 + i3): ', ainv.re:3:1, ' + i', ainv.im:3:1);
acong := cong(a);
writeln ('conj(5 + i3): ', acong.re:3:1, ' + i', acong.im:3:1);
end.
| 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);
}
}
}
}
|
Transform the following Pascal implementation into C++, maintaining the same output and logic. | Program ComplexDemo;
uses
ucomplex;
var
a, b, absum, abprod, aneg, ainv, acong: complex;
function complex(const re, im: real): ucomplex.complex; overload;
begin
complex.re := re;
complex.im := im;
end;
begin
a := complex(5, 3);
b := complex(0.5, 6.0);
absum := a + b;
writeln ('(5 + i3) + (0.5 + i6.0): ', absum.re:3:1, ' + i', absum.im:3:1);
abprod := a * b;
writeln ('(5 + i3) * (0.5 + i6.0): ', abprod.re:5:1, ' + i', abprod.im:4:1);
aneg := -a;
writeln ('-(5 + i3): ', aneg.re:3:1, ' + i', aneg.im:3:1);
ainv := 1.0 / a;
writeln ('1/(5 + i3): ', ainv.re:3:1, ' + i', ainv.im:3:1);
acong := cong(a);
writeln ('conj(5 + i3): ', acong.re:3:1, ' + i', acong.im:3:1);
end.
| #include <iostream>
#include <complex>
using std::complex;
void complex_operations() {
complex<double> a(1.0, 1.0);
complex<double> b(3.14159, 1.25);
std::cout << a + b << std::endl;
std::cout << a * b << std::endl;
std::cout << 1.0 / a << std::endl;
std::cout << -a << std::endl;
std::cout << std::conj(a) << std::endl;
}
|
Port the provided Pascal code into Java while preserving the original functionality. | Program ComplexDemo;
uses
ucomplex;
var
a, b, absum, abprod, aneg, ainv, acong: complex;
function complex(const re, im: real): ucomplex.complex; overload;
begin
complex.re := re;
complex.im := im;
end;
begin
a := complex(5, 3);
b := complex(0.5, 6.0);
absum := a + b;
writeln ('(5 + i3) + (0.5 + i6.0): ', absum.re:3:1, ' + i', absum.im:3:1);
abprod := a * b;
writeln ('(5 + i3) * (0.5 + i6.0): ', abprod.re:5:1, ' + i', abprod.im:4:1);
aneg := -a;
writeln ('-(5 + i3): ', aneg.re:3:1, ' + i', aneg.im:3:1);
ainv := 1.0 / a;
writeln ('1/(5 + i3): ', ainv.re:3:1, ' + i', ainv.im:3:1);
acong := cong(a);
writeln ('conj(5 + i3): ', acong.re:3:1, ' + i', acong.im:3:1);
end.
| public class Complex {
public final double real;
public final double imag;
public Complex() {
this(0, 0);
}
public Complex(double r, double i) {
real = r;
imag = i;
}
public Complex add(Complex b) {
return new Complex(this.real + b.real, this.imag + b.imag);
}
public Complex mult(Complex b) {
return new Complex(this.real * b.real - this.imag * b.imag,
this.real * b.imag + this.imag * b.real);
}
public Complex inv() {
double denom = real * real + imag * imag;
return new Complex(real / denom, -imag / denom);
}
public Complex neg() {
return new Complex(-real, -imag);
}
public Complex conj() {
return new Complex(real, -imag);
}
@Override
public String toString() {
return real + " + " + imag + " * i";
}
public static void main(String[] args) {
Complex a = new Complex(Math.PI, -5);
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());
}
}
|
Keep all operations the same but rewrite the snippet in Python. | Program ComplexDemo;
uses
ucomplex;
var
a, b, absum, abprod, aneg, ainv, acong: complex;
function complex(const re, im: real): ucomplex.complex; overload;
begin
complex.re := re;
complex.im := im;
end;
begin
a := complex(5, 3);
b := complex(0.5, 6.0);
absum := a + b;
writeln ('(5 + i3) + (0.5 + i6.0): ', absum.re:3:1, ' + i', absum.im:3:1);
abprod := a * b;
writeln ('(5 + i3) * (0.5 + i6.0): ', abprod.re:5:1, ' + i', abprod.im:4:1);
aneg := -a;
writeln ('-(5 + i3): ', aneg.re:3:1, ' + i', aneg.im:3:1);
ainv := 1.0 / a;
writeln ('1/(5 + i3): ', ainv.re:3:1, ' + i', ainv.im:3:1);
acong := cong(a);
writeln ('conj(5 + i3): ', acong.re:3:1, ' + i', acong.im:3:1);
end.
| >>> z1 = 1.5 + 3j
>>> z2 = 1.5 + 1.5j
>>> z1 + z2
(3+4.5j)
>>> z1 - z2
1.5j
>>> z1 * z2
(-2.25+6.75j)
>>> z1 / z2
(1.5+0.5j)
>>> - z1
(-1.5-3j)
>>> z1.conjugate()
(1.5-3j)
>>> abs(z1)
3.3541019662496847
>>> z1 ** z2
(-1.1024829553277784-0.38306415117199333j)
>>> z1.real
1.5
>>> z1.imag
3.0
>>>
|
Produce a functionally identical Go code for the snippet given in Pascal. | Program ComplexDemo;
uses
ucomplex;
var
a, b, absum, abprod, aneg, ainv, acong: complex;
function complex(const re, im: real): ucomplex.complex; overload;
begin
complex.re := re;
complex.im := im;
end;
begin
a := complex(5, 3);
b := complex(0.5, 6.0);
absum := a + b;
writeln ('(5 + i3) + (0.5 + i6.0): ', absum.re:3:1, ' + i', absum.im:3:1);
abprod := a * b;
writeln ('(5 + i3) * (0.5 + i6.0): ', abprod.re:5:1, ' + i', abprod.im:4:1);
aneg := -a;
writeln ('-(5 + i3): ', aneg.re:3:1, ' + i', aneg.im:3:1);
ainv := 1.0 / a;
writeln ('1/(5 + i3): ', ainv.re:3:1, ' + i', ainv.im:3:1);
acong := cong(a);
writeln ('conj(5 + i3): ', acong.re:3:1, ' + i', acong.im:3:1);
end.
| package main
import (
"fmt"
"math/cmplx"
)
func main() {
a := 1 + 1i
b := 3.14159 + 1.25i
fmt.Println("a: ", a)
fmt.Println("b: ", b)
fmt.Println("a + b: ", a+b)
fmt.Println("a * b: ", a*b)
fmt.Println("-a: ", -a)
fmt.Println("1 / a: ", 1/a)
fmt.Println("a̅: ", cmplx.Conj(a))
}
|
Generate an equivalent C version of this Perl code. | use Math::Complex;
my $a = 1 + 1*i;
my $b = 3.14159 + 1.25*i;
print "$_\n" foreach
$a + $b,
$a * $b,
-$a,
1 / $a,
~$a;
| #include <complex.h>
#include <stdio.h>
void cprint(double complex c)
{
printf("%f%+fI", creal(c), cimag(c));
}
void complex_operations() {
double complex a = 1.0 + 1.0I;
double complex b = 3.14159 + 1.2I;
double complex c;
printf("\na="); cprint(a);
printf("\nb="); cprint(b);
c = a + b;
printf("\na+b="); cprint(c);
c = a * b;
printf("\na*b="); cprint(c);
c = 1.0 / a;
printf("\n1/c="); cprint(c);
c = -a;
printf("\n-a="); cprint(c);
c = conj(a);
printf("\nconj a="); cprint(c); printf("\n");
}
|
Translate the given Perl code snippet into C# without altering its behavior. | use Math::Complex;
my $a = 1 + 1*i;
my $b = 3.14159 + 1.25*i;
print "$_\n" foreach
$a + $b,
$a * $b,
-$a,
1 / $a,
~$a;
| 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);
}
}
}
}
|
Ensure the translated C++ code behaves exactly like the original Perl snippet. | use Math::Complex;
my $a = 1 + 1*i;
my $b = 3.14159 + 1.25*i;
print "$_\n" foreach
$a + $b,
$a * $b,
-$a,
1 / $a,
~$a;
| #include <iostream>
#include <complex>
using std::complex;
void complex_operations() {
complex<double> a(1.0, 1.0);
complex<double> b(3.14159, 1.25);
std::cout << a + b << std::endl;
std::cout << a * b << std::endl;
std::cout << 1.0 / a << std::endl;
std::cout << -a << std::endl;
std::cout << std::conj(a) << std::endl;
}
|
Convert the following code from Perl to Java, ensuring the logic remains intact. | use Math::Complex;
my $a = 1 + 1*i;
my $b = 3.14159 + 1.25*i;
print "$_\n" foreach
$a + $b,
$a * $b,
-$a,
1 / $a,
~$a;
| public class Complex {
public final double real;
public final double imag;
public Complex() {
this(0, 0);
}
public Complex(double r, double i) {
real = r;
imag = i;
}
public Complex add(Complex b) {
return new Complex(this.real + b.real, this.imag + b.imag);
}
public Complex mult(Complex b) {
return new Complex(this.real * b.real - this.imag * b.imag,
this.real * b.imag + this.imag * b.real);
}
public Complex inv() {
double denom = real * real + imag * imag;
return new Complex(real / denom, -imag / denom);
}
public Complex neg() {
return new Complex(-real, -imag);
}
public Complex conj() {
return new Complex(real, -imag);
}
@Override
public String toString() {
return real + " + " + imag + " * i";
}
public static void main(String[] args) {
Complex a = new Complex(Math.PI, -5);
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());
}
}
|
Maintain the same structure and functionality when rewriting this code in Python. | use Math::Complex;
my $a = 1 + 1*i;
my $b = 3.14159 + 1.25*i;
print "$_\n" foreach
$a + $b,
$a * $b,
-$a,
1 / $a,
~$a;
| >>> z1 = 1.5 + 3j
>>> z2 = 1.5 + 1.5j
>>> z1 + z2
(3+4.5j)
>>> z1 - z2
1.5j
>>> z1 * z2
(-2.25+6.75j)
>>> z1 / z2
(1.5+0.5j)
>>> - z1
(-1.5-3j)
>>> z1.conjugate()
(1.5-3j)
>>> abs(z1)
3.3541019662496847
>>> z1 ** z2
(-1.1024829553277784-0.38306415117199333j)
>>> z1.real
1.5
>>> z1.imag
3.0
>>>
|
Preserve the algorithm and functionality while converting the code from Perl to Go. | use Math::Complex;
my $a = 1 + 1*i;
my $b = 3.14159 + 1.25*i;
print "$_\n" foreach
$a + $b,
$a * $b,
-$a,
1 / $a,
~$a;
| package main
import (
"fmt"
"math/cmplx"
)
func main() {
a := 1 + 1i
b := 3.14159 + 1.25i
fmt.Println("a: ", a)
fmt.Println("b: ", b)
fmt.Println("a + b: ", a+b)
fmt.Println("a * b: ", a*b)
fmt.Println("-a: ", -a)
fmt.Println("1 / a: ", 1/a)
fmt.Println("a̅: ", cmplx.Conj(a))
}
|
Port the provided PowerShell code into C while preserving the original functionality. | 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()))"
| #include <complex.h>
#include <stdio.h>
void cprint(double complex c)
{
printf("%f%+fI", creal(c), cimag(c));
}
void complex_operations() {
double complex a = 1.0 + 1.0I;
double complex b = 3.14159 + 1.2I;
double complex c;
printf("\na="); cprint(a);
printf("\nb="); cprint(b);
c = a + b;
printf("\na+b="); cprint(c);
c = a * b;
printf("\na*b="); cprint(c);
c = 1.0 / a;
printf("\n1/c="); cprint(c);
c = -a;
printf("\n-a="); cprint(c);
c = conj(a);
printf("\nconj a="); cprint(c); printf("\n");
}
|
Rewrite this program in C# while keeping its functionality equivalent to the PowerShell version. | 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()))"
| 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);
}
}
}
}
|
Produce a functionally identical C++ code for the snippet given in PowerShell. | 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()))"
| #include <iostream>
#include <complex>
using std::complex;
void complex_operations() {
complex<double> a(1.0, 1.0);
complex<double> b(3.14159, 1.25);
std::cout << a + b << std::endl;
std::cout << a * b << std::endl;
std::cout << 1.0 / a << std::endl;
std::cout << -a << std::endl;
std::cout << std::conj(a) << std::endl;
}
|
Ensure the translated Java code behaves exactly like the original PowerShell snippet. | 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()))"
| public class Complex {
public final double real;
public final double imag;
public Complex() {
this(0, 0);
}
public Complex(double r, double i) {
real = r;
imag = i;
}
public Complex add(Complex b) {
return new Complex(this.real + b.real, this.imag + b.imag);
}
public Complex mult(Complex b) {
return new Complex(this.real * b.real - this.imag * b.imag,
this.real * b.imag + this.imag * b.real);
}
public Complex inv() {
double denom = real * real + imag * imag;
return new Complex(real / denom, -imag / denom);
}
public Complex neg() {
return new Complex(-real, -imag);
}
public Complex conj() {
return new Complex(real, -imag);
}
@Override
public String toString() {
return real + " + " + imag + " * i";
}
public static void main(String[] args) {
Complex a = new Complex(Math.PI, -5);
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());
}
}
|
Port the provided PowerShell code into Python while preserving the original functionality. | 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()))"
| >>> z1 = 1.5 + 3j
>>> z2 = 1.5 + 1.5j
>>> z1 + z2
(3+4.5j)
>>> z1 - z2
1.5j
>>> z1 * z2
(-2.25+6.75j)
>>> z1 / z2
(1.5+0.5j)
>>> - z1
(-1.5-3j)
>>> z1.conjugate()
(1.5-3j)
>>> abs(z1)
3.3541019662496847
>>> z1 ** z2
(-1.1024829553277784-0.38306415117199333j)
>>> z1.real
1.5
>>> z1.imag
3.0
>>>
|
Convert this PowerShell block to Go, preserving its control flow and logic. | 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()))"
| package main
import (
"fmt"
"math/cmplx"
)
func main() {
a := 1 + 1i
b := 3.14159 + 1.25i
fmt.Println("a: ", a)
fmt.Println("b: ", b)
fmt.Println("a + b: ", a+b)
fmt.Println("a * b: ", a*b)
fmt.Println("-a: ", -a)
fmt.Println("1 / a: ", 1/a)
fmt.Println("a̅: ", cmplx.Conj(a))
}
|
Can you help me rewrite this code in C instead of Racket, keeping it the same logically? | #lang racket
(define a 3+4i)
(define b 8+0i)
(+ a b)
(- a b)
(/ a b)
(* a b)
(- a)
(/ 1 a)
(conjugate a)
| #include <complex.h>
#include <stdio.h>
void cprint(double complex c)
{
printf("%f%+fI", creal(c), cimag(c));
}
void complex_operations() {
double complex a = 1.0 + 1.0I;
double complex b = 3.14159 + 1.2I;
double complex c;
printf("\na="); cprint(a);
printf("\nb="); cprint(b);
c = a + b;
printf("\na+b="); cprint(c);
c = a * b;
printf("\na*b="); cprint(c);
c = 1.0 / a;
printf("\n1/c="); cprint(c);
c = -a;
printf("\n-a="); cprint(c);
c = conj(a);
printf("\nconj a="); cprint(c); printf("\n");
}
|
Write the same code in C# as shown below in Racket. | #lang racket
(define a 3+4i)
(define b 8+0i)
(+ a b)
(- a b)
(/ a b)
(* a b)
(- a)
(/ 1 a)
(conjugate a)
| 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);
}
}
}
}
|
Write the same algorithm in C++ as shown in this Racket implementation. | #lang racket
(define a 3+4i)
(define b 8+0i)
(+ a b)
(- a b)
(/ a b)
(* a b)
(- a)
(/ 1 a)
(conjugate a)
| #include <iostream>
#include <complex>
using std::complex;
void complex_operations() {
complex<double> a(1.0, 1.0);
complex<double> b(3.14159, 1.25);
std::cout << a + b << std::endl;
std::cout << a * b << std::endl;
std::cout << 1.0 / a << std::endl;
std::cout << -a << std::endl;
std::cout << std::conj(a) << std::endl;
}
|
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.