If you would rather(or in the mean time) check out that class I was talking about, follow these instructions:
- New Project >> Add Reference >> System.Numerics
- Add using System.Numerics; to the top of your class file.
- The class is called Complex: Complex i = new Complex();
- ...new Complex(2, 3); would look something like this in mathematical notation: 2 + 3i
[Important Note]
I recently read this article: to understand how I would go about finding the root of an imaginary number. Now, unless you wish to spend much time reading and figuring out what they are trying to describe and explain(like I did), the simplified answer goes as follows:
√i = cos(90[1/2]) + sin(90[1/2])i
OR
n√i = cos(90n) + sin(90n)i
OR
n√i = cos(90n) + sin(90n)i
If this doesn't explain or satisfy what you are looking for, then I recommend reading on in that article.
The code is pretty explanatory and I have added comments to things that may not so easily be seen...
Alright, enough talk, here is the [C#] code:
The code is pretty explanatory and I have added comments to things that may not so easily be seen...
Alright, enough talk, here is the [C#] code:
namespace Math
{
public sealed class i
{
// set value to default
private double val = 1; // i^0
public string Value
{
get
{
String final;
if (double.IsPositiveInfinity(val))
final = "i";
else if (double.IsNegativeInfinity(val))
final = "-i";
else final = val.ToString();
return final;
}
// only set value through operators
// private set
{
String test = value;
if (!double.TryParse(test, out val))
val = String.Equals(test, "-i") ?
double.NegativeInfinity :
double.PositiveInfinity;
}
}
// main targeted desire for this class
public static object Pow(double x)
{
object ans = null;
// root powers
if (System.Math.Ceiling(x) > x)
ans = System.Math.Cos(90 * x) + " + " +
System.Math.Sin(90 * x) + "i";
else
// returns real numbers if power is a multiple
// of 2
if (x % 2 == 0) ans = System.Math.Pow(-1, x/2);
// returns a string representation if
// power is odd
else ans = (System.Math.Pow(-1, (x-1)/2) == 1
? "i" : "-i");
return ans;
}
// Allows for there to be 'i * i * i'
// instead of just i.Pow(#);
// This is also the only way to set the
// 'Value' property.
public static i operator *(i complex, i complex2)
{
// this section may not be entirely accurate
i c = new i();
short tag = 0;
// i^1
if (double.IsPositiveInfinity(complex.val))
tag = 1;
else if (double.IsNegativeInfinity(complex.val))
tag = 3; // i^3
else if (complex.val == -1) tag = 2; // i^2
if (double.IsPositiveInfinity(complex2.val))
tag += 1; // i^1
else if (double.IsNegativeInfinity(complex2.val))
tag += 3;// i^3
else if (complex2.val == -1) tag += 2; // i^2
c.Value = (string)Pow((double)tag);
return c;
}
}
}
{
public sealed class i
{
// set value to default
private double val = 1; // i^0
public string Value
{
get
{
String final;
if (double.IsPositiveInfinity(val))
final = "i";
else if (double.IsNegativeInfinity(val))
final = "-i";
else final = val.ToString();
return final;
}
// only set value through operators
// private set
{
String test = value;
if (!double.TryParse(test, out val))
val = String.Equals(test, "-i") ?
double.NegativeInfinity :
double.PositiveInfinity;
}
}
// main targeted desire for this class
public static object Pow(double x)
{
object ans = null;
// root powers
if (System.Math.Ceiling(x) > x)
ans = System.Math.Cos(90 * x) + " + " +
System.Math.Sin(90 * x) + "i";
else
// returns real numbers if power is a multiple
// of 2
if (x % 2 == 0) ans = System.Math.Pow(-1, x/2);
// returns a string representation if
// power is odd
else ans = (System.Math.Pow(-1, (x-1)/2) == 1
? "i" : "-i");
return ans;
}
// Allows for there to be 'i * i * i'
// instead of just i.Pow(#);
// This is also the only way to set the
// 'Value' property.
public static i operator *(i complex, i complex2)
{
// this section may not be entirely accurate
i c = new i();
short tag = 0;
// i^1
if (double.IsPositiveInfinity(complex.val))
tag = 1;
else if (double.IsNegativeInfinity(complex.val))
tag = 3; // i^3
else if (complex.val == -1) tag = 2; // i^2
if (double.IsPositiveInfinity(complex2.val))
tag += 1; // i^1
else if (double.IsNegativeInfinity(complex2.val))
tag += 3;// i^3
else if (complex2.val == -1) tag += 2; // i^2
c.Value = (string)Pow((double)tag);
return c;
}
}
}
Interesting fact: as I developed this I realized I could have just rewritten the equation:
[(i2n)-1]/-2 as (-1)n
Edit: I also recently gained experience with extensions in C# allowing no need to create an additional separate class - just an extensions class.
Edit: I also recently gained experience with extensions in C# allowing no need to create an additional separate class - just an extensions class.
No comments:
Post a Comment