Run Code
|
API
|
Code Wall
|
Misc
|
Feedback
|
Login
|
Theme
|
Privacy
|
Patreon
Complex Variable Implementation in C#
//Rextester.Program.Main is the entry point for your code. Don't change it. //Compiler version 4.0.30319.17929 for Microsoft (R) .NET Framework 4.5 using System; using System.Collections.Generic; using System.Linq; using System.Text.RegularExpressions; namespace Rextester { public class ComplexOpCode { public double x1 { get; set; } //first x value public double y1 { get; set; } // first y value public double x2{ get; set; } //second x value public double y2 { get; set; } // second y value public double Power { get; set; } } public class ComplexValue { public double Real { get; set; } public double Imaginary { get; set; } public ComplexValue Conjugate { get; set; } public double Modulus { get; set; } public double Theta { get; set; } public ComplexValue Inverse { get; set; } public ComplexValue PolarValue { get; set; } public double Length { get; set; } public ComplexValue Pow { get; set; } public bool DoubleIsSet { get; set; } //Return double value. Used in ToString. If true, then returning double value else ComplexValue public override string ToString() { string ret = "0"; if(this.DoubleIsSet) { return string.Format("{0:0.0000}", Math.Abs(this.Real)); } if (this.Real == 0) { if (this.Imaginary < 0) { ret = "- i" + string.Format("{0:0.0000}", Math.Abs(this.Imaginary)); } else { ret = "i" + string.Format("{0:0.0000}", this.Imaginary); } } else if (this.Imaginary == 0 && this.Real != 0) { ret = string.Format("{0:0.0000}", this.Real); } else if (this.Imaginary != 0 && this.Real != 0) { if (this.Imaginary < 0) { ret = string.Format("{0:0.0000}", this.Real) + " - i" + string.Format("{0:0.0000}", Math.Abs(this.Imaginary)); } else { ret = string.Format("{0:0.0000}", this.Real) + " + i" + string.Format("{0:0.0000}", this.Imaginary); } } return ret; } } public class ComplexVariable { public Func<ComplexOpCode, ComplexValue> ComplexFunction = null; //Func delegate takes a ComplexOpCode object as input and outputs a ComplexValue object public ComplexVariable(Func<ComplexOpCode, ComplexValue> cf) { ComplexFunction = cf; } } public class Tester { public static void TestComplexVariable() { //f(z) = x + iy ComplexVariable z = new ComplexVariable(co => { return new ComplexValue { Real = co.x1, Imaginary = co.y1, DoubleIsSet = false }; }); Console.WriteLine("f(z) = z = x + iy = {0}", z.ComplexFunction(new ComplexOpCode { x1 = 1, y1 = 2, Power = 0 })); //output is f(z) = z = x + iy = 1.0000 + i2.0000 //End of f(z) = x + iy //f(z) = x - iy ComplexVariable Conjugate = new ComplexVariable(co => { return new ComplexValue { Real = co.x1, Imaginary = -co.y1, DoubleIsSet = false }; }); Console.WriteLine("f(z) = z = x - iy = {0}", Conjugate.ComplexFunction(new ComplexOpCode { x1 = 1, y1 = 2, Power = 0 })); //output is f(z) = z = x - iy = 1.0000 - i2.0000 //End of f(z) = x - iy //f(z) = |z| = Sqrt(x^2 + y^2) ComplexVariable Modulus = new ComplexVariable(co => { double mod = Math.Sqrt(Math.Pow(co.x1, 2) + Math.Pow(co.y1, 2)); return new ComplexValue { Real = mod, Imaginary = 0, Modulus = mod, DoubleIsSet = true }; }); Console.WriteLine("f(z) = |z| = Sqrt(x^2 + y^2) = {0}", Modulus.ComplexFunction(new ComplexOpCode { x1 = 1, y1 = 2, Power = 0 })); //output is f(z) = |z| = Sqrt(/x^2 + y^2) = 2.2361 //End of f(z) = |z| = Sqrt(x^2 + y^2) //f(z) = z^-1 = Conjuate(z)/|z|^2 ComplexVariable Inverse = new ComplexVariable(co => { double modsq = Math.Pow(co.x1, 2) + Math.Pow(co.y1, 2); return new ComplexValue { Real = co.x1/modsq, Imaginary = -co.y1/modsq, DoubleIsSet = false }; }); Console.WriteLine("f(z) = z^-1 = Conjuate(z)/|z|^2 = {0}", Inverse.ComplexFunction(new ComplexOpCode { x1 = 1, y1 = 2, Power = 0 })); //output is f(z) = z^-1 = Conjuate(z)/|z|^2 = 0.2000 - i0.4000 //End of f(z) = Conjuate(z)/Mod^2fs //z + w = x1 + iy1 + x2 + iy2 = (x1 + x2) + i(y1 + y2) ComplexVariable Add = new ComplexVariable(co => { return new ComplexValue { Real = co.x1 + co.x2, Imaginary = co.y1 + co.y2, DoubleIsSet = false }; }); Console.WriteLine("w + z = x1 + iy1 + x2 + iy2 = (x1 + x2) + i(y1 + y2) = {0}", Add.ComplexFunction(new ComplexOpCode { x1 = 1, y1 = 2, x2 = 5, y2 = 7, Power = 0 })); //output is w + z = x1 + iy1 + x2 + iy2 = (x1 + x2) + i(y1 + y2) = 6.0000 + i9.0000 //End of z + w = x1 + iy1 + x2 + iy2 = (x1 + x2) + i(y1 + y2) //z - w = x1 + iy1 - x2 + iy2 = (x1 - x2) + i(y1 - y2) ComplexVariable Subtract = new ComplexVariable(co => { return new ComplexValue { Real = co.x1 - co.x2, Imaginary = co.y1 - co.y2, DoubleIsSet = false }; }); Console.WriteLine("w - z = x1 + iy1 - x2 + iy2 = (x1 - x2) + i(y1 - y2) = {0}", Subtract.ComplexFunction(new ComplexOpCode { x1 = 1, y1 = 2, x2 = 5, y2 = 7, Power = 0 })); //output is w - z = x1 + iy1 - x2 + iy2 = (x1 - x2) + i(y1 - y2) = -4.0000 - i5.0000 //End of z - w = x1 + iy1 - x2 + iy2 = (x1 - x2) + i(y1 - y2) //zw = ( x1 + iy1)(x2 + iy2) = (x1x2 - y1y2) + i(x1y2 + y1x2) ComplexVariable Multiply = new ComplexVariable(co => { return new ComplexValue { Real = (co.x1 * co.x2 - co.y1 * co.y2), Imaginary = (co.x1 * co.y2 + co.y1 * co.x2), DoubleIsSet = false }; }); Console.WriteLine("zw = ( x1 + iy1)(x2 + iy2) = (x1x2 - y1y2) + i(x1y2 + y1x2) = {0}", Multiply.ComplexFunction(new ComplexOpCode { x1 = 1, y1 = 2, x2 = 5, y2 = 7, Power = 0 })); //output is zw = ( x1 + iy1)(x2 + iy2) = (x1x2 - y1y2) + i(x1y2 + y1x2) = -9.0000 + i17.0000 //End of zw = ( x1 + iy1)(x2 + iy2) = (x1x2 - y1y2) + i(x1y2 + y1x2) } } public class Program { public static void Main(string[] args) { //Your code goes here Tester.TestComplexVariable(); } } }
run
|
edit
|
history
|
help
0
pared 1
Main5.1
test2
c# string abbreviation
xml into dictionary
número poderoso
Enumeration
Test equality...1
matching parenthesis
tokenExchange