How to divide two complex numbers in C# - c#

I am trying to divide two complex numbers in C# but can't get it to work! I'm pretty sure it is my formula that is wrong, but I do not understand what the problem is with it.
I have tried to modify the formula a few times but with no success. This is because I have never studied Complex numbers (or any math similar to it) and am therefore pretty lost.
I tried this formula: http://www.mesacc.edu/~scotz47781/mat120/notes/complex/dividing/dividing_complex.html but had trouble converting it into code.
I am grateful for any responses! Thank you
public Komplex div(Komplex a, Komplex b)
{
Komplex resDiv = new Komplex();
resDiv.re = a.re / b.re - a.im / a.im;
resDiv.im = a.re / b.im + a.im / b.re;
return resDiv;
}
EDIT: The program is supposed to take inputs like this (example): (3+2i) / (1-4i)

This is how .NET's Complex class does it (adjusted for your variable and type names):
public static Komplex div(Komplex a, Komplex b)
{
// Division : Smith's formula.
double a = a.re;
double b = a.im;
double c = b.re;
double d = b.im;
Komplex resDiv = new Komplex();
// Computing c * c + d * d will overflow even in cases where the actual result of the division does not overflow.
if (Math.Abs(d) < Math.Abs(c))
{
double doc = d / c;
resDiv.re = (a + b * doc) / (c + d * doc);
resDiv.im = (b - a * doc) / (c + d * doc);
}
else
{
double cod = c / d;
resDiv.re = (b + a * cod) / (d + c * cod);
resDiv.im = (-a + b * cod) / (d + c * cod);
}
return resDiv;
}
Why aren't you using .NET's Complex type though?
var a = new Complex(3, -1);
var b = new Complex(5, -3);
Console.WriteLine(a / b);

Related

What is wrong in this math equation in C#?

I have a math equation and I want use it in my program
but I got an error. The result from this equation is non number ..
the math equation i:
note: n is the number in the textbox
((298/2)^2*ACOS(((298/2)-n)/(298/2))-((298/2)-n)*((2*(298/2)*n-n^2))^(0.5))*1213/1000
in button code
double a = Math.Pow((298.00 / 2), 2);
double b = (Math.Acos(((298.00 / 2) - Convert.ToDouble(textBox1.Text)) / (298.00 / 2)));
double c = ((298.00 / 2) - Convert.ToDouble(textBox1.Text));
double d = (2 * (298.00 / 2) * Convert.ToDouble(textBox1.Text));
double f = Math.Pow(Convert.ToDouble(textBox1.Text), 2);
double r = ((a * b) - (c * d) )- f;
double result = Math.Pow(r, (0.5));
double h = result * 1213.00 / 1000;
textBox1.Text = Convert.ToString(h );
}
If anyone knows what is wrong here, tell me please.
The error is in variable r because the result from r is less that 0!
Lets rewrite given math expression in more convenient way:
( (298/2)^2 * ACOS(((298/2)-n)/(298/2)) - ((298/2)-n) * ( (2*(298/2)*n - n^2 ) ) ^ (0.5) ) * 1213/1000
--------- ------------------------- ----------- ------------ ---
a b c d f
--------------------------------
r = (d - f) ^ 0.5
------------------------------------------------------------------------------------------------------
(a * b - c * r) * 1213/1000
Now it is easier to see that you have an error in formula:
double r = ((a * b) - (c * d) )- f;
r must be calculated using the next formula:
double r = Math.Sqrt(d - f); // It is better to use Math.Sqrt(x) instead of Math.Pow(x, 0.5).
Now knowing where the error is we can fix it:
double n = Convert.ToDouble(textBox1.Text);
double a = Math.Pow(298.00 / 2, 2);
double b = Math.Acos( (298.00 / 2 - n) / (298.00 / 2) );
double c = 298.00 / 2 - n;
double d = 2 * 298.00 / 2 * n;
double f = Math.Pow(n, 2);
double r = Math.Sqrt(d - f);
double result = a * b - c * r;
double h = result * 1213.00 / 1000;
textBox1.Text = Convert.ToString(h);
But for values of n > 298 you will still get r = NaN because for such values of n expression d - f is negative.

How would i work out magnitude quickly for 3 values?

How can I use a Fast Magnitude calculation for 3 values (instead of using square root)? (+/- 3% is good enough)
public void RGBToComparison(Color32[] color)
{
DateTime start = DateTime.Now;
foreach (Color32 i in color)
{
var r = PivotRgb(i.r / 255.0);
var g = PivotRgb(i.g / 255.0);
var b = PivotRgb(i.b / 255.0);
var X = r * 0.4124 + g * 0.3576 + b * 0.1805;
var Y = r * 0.2126 + g * 0.7152 + b * 0.0722;
var Z = r * 0.0193 + g * 0.1192 + b * 0.9505;
var LB = PivotXyz(X / 95.047);
var AB = PivotXyz(Y / 100);
var BB = PivotXyz(Z / 108.883);
var L = Math.Max(0, 116 * AB - 16);
var A = 500 * (LB - AB);
var B = 200 * (AB - BB);
totalDifference += Math.Sqrt((L-LT)*(L-LT) + (A-AT)*(A-AT) + (B-BT)*(B-BT));
}
totalDifference = totalDifference / color.Length;
text.text = "Amount of Pixels: " + color.Length + " Time(MilliSeconds):" + DateTime.Now.Subtract(start).TotalMilliseconds + " Score (0 to 100)" + (totalDifference).ToString();
RandomOrNot();
}
private static double PivotRgb(double n)
{
return (n > 0.04045 ? Math.Pow((n + 0.055) / 1.055, 2.4) : n / 12.92) * 100.0;
}
private static double PivotXyz(double n)
{
return n > 0.008856 ? CubicRoot(n) : (903.3 * n + 16) / 116;
}
private static double CubicRoot(double n)
{
return Math.Pow(n, 1.0 / 3.0);
}
This is the important part: totalDifference += Math.Sqrt((L-LT)*(L-LT) + (A-AT)*(A-AT) + (B-BT)*(B-BT));
I know there are FastMagnitude calculations online, but all the ones online are for two values, not three. For example, could i use the difference between the values to get a precise answer? (By implementing the difference value into the equation, and if the difference percentage-wise is big, falling back onto square root?)
Adding up the values and iterating the square root every 4 pixels is a last resort that I could do. But firstly, I want to find out if it is possible to have a good FastMagnitude calculation for 3 values.
I know I can multi-thread and parllelize it, but I want to optimize my code before I do that.
If you just want to compare the values, why not leave the square root out and work with the length squared?
Or use the taylor series of the square root of 1+x and cut off early :)

Data type that rounds .(9) to 1 automatically?

I'm trying to find solutions to a simple MDAS (i.e. Multiplication, Division...) problem using C#, and while it mostly gets correct solutions, I have problems when the variables add up to x.99999... so I get wrong answers because it doesn't compute to 1.
For example, if I have:
decimal a = 1M;
decimal b = 2M;
decimal c = 6M;
decimal d = 4M;
decimal e = 7M;
decimal f = 8M;
decimal g = 3M;
decimal h = 5M;
decimal i = 9M;
Console.WriteLine(a + 13 * b / c + d + 12 * e - f - 11);
Console.WriteLine(g * h / i);
Console.WriteLine(a + 13 * b / c + d + 12 * e - f - 11 + g * h / i);
Which gives me:
74.33333333333333333333333333
1.6666666666666666666666666667
75.999999999999999999999999997
But I want:
74.33333333333333333333333333
1.6666666666666666666666666667
76
Is there a way that I can always get a precise answer to .(6)+.(3) = 1 without needing to check and modify the values? If not what is the best way to go about it?
The following will round your double value to within a precision of 0.1:
// parameters
double d = -7.9;
double precision = 0.1;
//the conversion
double v = (Math.Abs(Math.Round(d)-d)) < precision ? Math.Round(d) : d;
//output
int i = (int) v;
Console.WriteLine(i);
I went with #tia and Dai's suggestion to use a Rational data type. I got tompazourek's Rationals NuGet package and used it like this:
Rational a = 1;
Rational b = 2;
Rational c = 6;
Rational d = 4;
Rational e = 7;
Rational f = 8;
Rational g = 3;
Rational h = 5;
Rational i = 9;
Console.WriteLine(a + (Rational)13 * b / c + d + (Rational)12 * e - f - (Rational)11);
Console.WriteLine(g * h / i);
Rational toTest = a + (Rational)13 * b / c + d + (Rational)12 * e - f - (Rational)11 + g * h / i;
Console.WriteLine(toTest);
Console.WriteLine(toTest.Equals(76));
Which gives me:
446/6
15/9
4104/54
True

Solving arithmetic operations

int a = 3, b = 9, c = 2;
double e = 11, f = 0.1;
e = (b + c) / a * a;
Now, I want to now what the result of e is. When I do the math in my head, I get the result to be 11/9 = 1,22222222
BUT
when I run the program in the compiler, I simply get 9. Which way to think is right?
There are 2 thing going wrong,
your misunderstanding your order of operations
your integers are truncating
so, what the compiler is doing is
9+2 = 11
11/3 = 3.666 truncated to 3
3*3 = 9
Use parenthesis, and don't use ints, use all doubles.
double a = 3, b = 9, c = 2, d;
double e = 11, f = 0.1;
string s = "AB", t = "BA", v;
e = (b + c) / (a * a);
if a, b, and c all have to be ints, than you can cast them
int a = 3, b = 9, c = 2, d;
double e = 11, f = 0.1;
string s = "AB", t = "BA", v;
e = ((double)b + (double)c) / ((double)a * (double)a);
now this could just easily be done with
e = (double)(b + c) / (double)(a * a);
and that's because both operations in parenthesis leave no remainder, but it is a bad practice to rely on those kind of coincidences.
All your input variables are integers, so the arithmetic is done as integer arithmetic. Also all arithemtic operators in C# are evaluated left to right, so
e = (b + c) / a * a is equivalent to e = ((b + c) / a ) * a
(b + c ) / a * a = ((9 + 2) / 3) * 3
= (11 / 3) * 3
= 3 * 3
= 9
The expression that I think you want is
e = (double)(b + c) / (a * a);
For details on operator precendence in C# see this .NET documentation
The compiler is quite correct - you may want to review how operator precedence in C# works.
You have two choice.Use doubles instead of integers, or use explicit cast and convert one of your values to double
e = (b + c) / (double)(a * a);
This statement :
(b + c) / a * a
evaluated like:
(int) / (int)
If you divide two integers, how could you get double results ? Then your result converting double with implicit type conversion.But result is already '1' without the floating points,and conversion is meaningless in this case.
But when you do something like this:
e = (b + c) / (double)(a * a);
Returning value from b + c promoted to double and then (double / double) division is performing.So you get the correct results.

c# math formula output inconsistency

here is my formula:
y = b*exp(-((x-c)^2)/2(d^2)) if x < c, else y = b*exp(-(x-c)^2/2g^2);
this is how i coded it:
for (double x = 0; x <= 7D; x += .01D)
{
b = 6.0410638; c = 2.1344769; d = 0.59183047; g = 0.77384504;
if (x < c)
y = b * Math.Exp(-Math.Pow((x - c), 2)) / (2 * Math.Pow(d, 2D));
else
y = b * Math.Exp(-Math.Pow((x - c), 2)) / (2 * Math.Pow(g, 2D));
qResults.Rows.Add(x, y);
}
the output:
0.0529826172
0.05528786
0.05768187
0.0601675063
0.0627477
0.06542546
0.06820385
0.07108601
0.074075155
0.0771745443
0.08038754
0.08371756
0.08716809
0.0907426849
0.0944449753
0.0982786641
0.102247514
0.106355369
0.110606134
0.115003787
0.119552381
0.124256022
0.1291189
0.13414526
0.139339417
0.144705743
0.150248691
0.155972764
0.16188252
0.167982608
0.1742777
0.180772528
0.187471911
0.1943807
0.201503783
0.2088461
0.2164127
0.224208578
0.232238829
0.2405086
0.24902302
0.257787317
0.2668067
is there a problem with my syntax?
the output should be:
0.009332048
0.009915393
0.010532198
0.011184179
0.011873133
0.012600931
0.013369526
0.014180954
0.015037339
0.015940891
0.016893914
0.017898806
0.01895806
0.020074273
0.021250142
0.022488471
0.023792173
0.025164271
0.026607905
0.028126332
0.029722928
0.031401194
0.033164757
0.035017372
0.036962928
0.039005447
0.041149089
0.043398156
0.045757092
0.048230485
0.050823073
0.053539744
0.05638554
0.059365657
0.062485449
0.065750429
0.069166271
0.072738815
0.076474061
0.08037818
0.084457508
0.088718551
0.093167983
0.097812651
0.102659571
0.107715931
0.112989091
0.118486582
0.124216105
0.130185532
0.136402905
0.142876432
0.149614489
0.156625615
0.163918513
0.171502045
0.17938523
0.187577239
0.196087395
0.204925165
0.21410016
0.223622125
0.233500937
0.2437466
0.254369235
0.265379079
0.276786473
0.288601856
0.30083576
0.313498797
0.326601652
0.340155077
0.354169874
0.368656891
0.383627009
0.39909113
0.415060167
0.431545027
0.448556606
0.466105768
0.484203337
0.502860078
0.522086688
0.541893773
0.562291841
0.583291279
0.604902341
0.627135126
0.649999569
0.673505413
0.697662199
0.722479244
0.747965622
0.774130146
0.800981347
0.828527458
0.856776387
0.885735705
0.915412619
0.945813957
0.976946142
1.008815173
1.041426608
1.074785537
1.108896564
1.143763787
1.179390775
1.21578055
Based on your formula, the code should be:
for (double x = 0; x <= 7D; x += .01D)
{
b = 6.0410638; c = 2.1344769; d = 0.59183047; g = 0.77384504;
if (x < c)
y = b * Math.Exp(-Math.Pow(x - c, 2) / (2 * Math.Pow(d, 2D)));
else
y = b * Math.Exp(-Math.Pow(x - c, 2) / (2 * Math.Pow(g, 2D)));
qResults.Rows.Add(x, y);
}
Basically, the Math.Exp function should be applied to the result of the division, not just to the result of -Math.Pow(x - c, 2)
Also, it's not very clear based on the formula and your code example whether you need to divide by 2 in the exponent computation, then multiply the result by d^2, or actually divide by 2 * d^2 as you are doing in the implementation.

Categories