FreeTextHost.in host your text & codes anonymously


FREE TEXT HOST

You can save codes, scripts, sources & general debugging text and share / access / use them at any time (anonymously).
You can even set yourself a password if you want to keep it just for yourself.

Posted by "Anonymous" on Wed, September 8th, 2010, 11:55 AM - Never Expires
Download | New paste

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5.  
  6. namespace Math2
  7. {
  8.     unsafe public class Math2
  9.     {
  10.         public class SquareEquation
  11.         {
  12.             private object[] sol = new object[0];
  13.             private bool isComplex = false;
  14.  
  15.             public object[] Solutions { get { return sol; } }
  16.             public bool SolutionIsComplex { get { return isComplex; } }
  17.  
  18.             public SquareEquation(double* a, double* b, double* c, bool allowcomplex)
  19.             {
  20.                 if (*a == 0)
  21.                 {
  22.                     sol = new object[] { -*c / *b };
  23.                 }
  24.                 else
  25.                 {
  26.                     double discr = *b * *b - 4 * *a * *c;
  27.  
  28.                     if (discr < 0)
  29.                     {
  30.                         isComplex = true;
  31.  
  32.                         if (allowcomplex)
  33.                         {
  34.                             double p1 = -1 * *b;
  35.                             double discr2 = -1 * discr;
  36.                             double p2 = 2 * *a;
  37.  
  38.                             Complex.Z z1 = new Complex.Z(&p1, &discr);
  39.                             Complex.Z z2 = new Complex.Z(&p1, &discr2);
  40.  
  41.                             Complex.Z zdivisor = new Complex.Z(&p2);
  42.  
  43.                             z1.Divide(&zdivisor);
  44.                             z2.Divide(&zdivisor);
  45.  
  46.                             sol = new object[] { z1.ToString(false, 'j', 3), z2.ToString(false, 'j', 3) };
  47.                         }
  48.                     }
  49.                     else if (discr == 0)
  50.                     {
  51.                         sol = new object[] { -*b / 2 * *a };
  52.                     }
  53.                     else
  54.                     {
  55.                         sol = new object[] { getresult(a, b, &discr, 1), getresult(a, b, &discr, -1) };
  56.                     }
  57.                 }
  58.             }
  59.  
  60.             private static double getresult(double* a, double* b, double* discr, sbyte mod)
  61.             {
  62.                 return (-*b + mod * *discr) / 2 * *a;
  63.             }
  64.         }
  65.  
  66.         class Complex
  67.         {
  68.             public struct Z
  69.             {
  70.                 private double _a, _b, _r;
  71.                 private float _p;
  72.  
  73.                 public double A
  74.                 {
  75.                     get { return _a; }
  76.                     set
  77.                     {
  78.                         _a = value;
  79.  
  80.                         fixed (double* _B = &_b)
  81.                         {
  82.                             _r = rFromCart(&value, _B);
  83.                             _p = phiFromCart(&value, _B);
  84.                         }
  85.                     }
  86.                 }
  87.  
  88.                 public double B
  89.                 {
  90.                     get { return _b; }
  91.                     set
  92.                     {
  93.                         _b = value;
  94.  
  95.                         fixed (double* _A = &_a)
  96.                         {
  97.                             _r = rFromCart(_A, &value);
  98.                             _p = phiFromCart(_A, &value);
  99.                         }
  100.                     }
  101.                 }
  102.  
  103.                 public double R
  104.                 {
  105.                     get { return _r; }
  106.                     set
  107.                     {
  108.                         _r = value;
  109.  
  110.                         fixed (float* _P = &_p)
  111.                         {
  112.                             _a = aFromPol(&value, _P);
  113.                             _b = bFromPol(&value, _P);
  114.                         }
  115.                     }
  116.                 }
  117.  
  118.                 public float Phi
  119.                 {
  120.                     get { return _p; }
  121.                     set
  122.                     {
  123.                         _p = value;
  124.  
  125.                         fixed (double* _R = &_r)
  126.                         {
  127.                             _a = aFromPol(_R, &value);
  128.                             _b = bFromPol(_R, &value);
  129.                         }
  130.                     }
  131.                 }
  132.  
  133.                 public string ToString(bool PolarForm, char iChar, byte digits)
  134.                 {
  135.                     fixed (Z* z = &this)
  136.                     {
  137.                         return stringify(z, PolarForm, iChar, digits);
  138.                     }
  139.                 }
  140.  
  141.                 public string ToString(bool PolarForm, char iChar)
  142.                 {
  143.                     return ToString(PolarForm, iChar, 2);
  144.                 }
  145.  
  146.                 public string ToString(bool PolarForm)
  147.                 {
  148.                     return ToString(PolarForm, 'i');
  149.                 }
  150.  
  151.                 public Z(double* a, double* b)
  152.                 {
  153.                     _a = *a;
  154.                     _b = *b;
  155.  
  156.                     _r = rFromCart(a, b);
  157.                     _p = phiFromCart(a, b);
  158.                 }
  159.  
  160.                 public Z(double* r, float* p)
  161.                 {
  162.                     _r = *r;
  163.                     _p = *p;
  164.  
  165.                     _a = aFromPol(r, p);
  166.                     _b = bFromPol(r, p);
  167.                 }
  168.  
  169.                 public Z(double* n)
  170.                 {
  171.                     _a = *n;
  172.                     _b = 0;
  173.                     _r = *n;
  174.                     _p = 0;
  175.                 }
  176.  
  177.                 public void Set(double* a, double* b)
  178.                 {
  179.                     _a = *a;
  180.                     _b = *b;
  181.  
  182.                     _r = rFromCart(a, b);
  183.                     _p = phiFromCart(a, b);
  184.                 }
  185.  
  186.                 public void Set(double* r, float* phi)
  187.                 {
  188.                     _r = *r;
  189.                     _p = *phi;
  190.  
  191.                     _a = aFromPol(r, phi);
  192.                     _b = bFromPol(r, phi);
  193.                 }
  194.  
  195.                 public void Divide(Z* Divisor)
  196.                 {
  197.                     fixed (Z* dividend = &this)
  198.                     {
  199.                         this = divide(dividend, Divisor, true);
  200.                     }
  201.                 }
  202.  
  203.                 public void Multiply(Z* Operand2)
  204.                 {
  205.                     fixed (Z* op1 = &this)
  206.                     {
  207.                         this = multiply(op1, Operand2, true);
  208.                     }
  209.                 }
  210.  
  211.                 public void Add(Z* Operand2)
  212.                 {
  213.                     fixed (Z* op1 = &this)
  214.                     {
  215.                         this = add(op1, Operand2, 1);
  216.                     }
  217.                 }
  218.  
  219.                 public void Sub(Z* Operand2)
  220.                 {
  221.                     fixed (Z* op1 = &this)
  222.                     {
  223.                         this = add(op1, Operand2, -1);
  224.                     }
  225.                 }
  226.  
  227.                 public void ValidateAngle()
  228.                 {
  229.                     fixed (float* f = &_p)
  230.                     {
  231.                         Angle.ValidateDeg(f);
  232.                     }
  233.                 }
  234.             }
  235.  
  236.             private static Z add(Z* op1, Z* op2, sbyte mod)
  237.             {
  238.                 double a = op1->A + mod * (op2->A);
  239.                 double b = op1->B + mod * (op2->B);
  240.  
  241.                 return new Z(&a, &b);
  242.             }
  243.  
  244.             private static Z divide(Z* dividend, Z* divisor, bool anglecheck)
  245.             {
  246.                 double r = dividend->R / divisor->R;
  247.                 float p = dividend->Phi - divisor->Phi;
  248.  
  249.                 Z z = new Z(&r, &p);
  250.  
  251.                 if (anglecheck) z.ValidateAngle();
  252.  
  253.                 return z;
  254.             }
  255.  
  256.             private static Z multiply(Z* op1, Z* op2, bool anglecheck)
  257.             {
  258.                 double r = op1->R * op2->R;
  259.                 float p = op1->Phi + op1->Phi;
  260.  
  261.                 Z z = new Z(&r, &p);
  262.  
  263.                 if (anglecheck) z.ValidateAngle();
  264.  
  265.                 return z;
  266.             }
  267.  
  268.             private static string stringify(Z* c, bool polar, char imag, byte digits)
  269.             {
  270.                 string result = "";
  271.  
  272.                 if (polar)
  273.                 {
  274.                     result += c->R + " <" + c->Phi + "°";
  275.                 }
  276.                 else
  277.                 {
  278.                     result += c->A + " ";
  279.  
  280.                     if (c->B < 0)
  281.                     {
  282.                         string s = c->B.ToString().Remove(0, 1);
  283.                         result += "- " + s;
  284.                     }
  285.                     else
  286.                     {
  287.                         result += "+ " + c->B.ToString();
  288.                     }
  289.  
  290.                     result += imag;
  291.                 }
  292.  
  293.                 return result;
  294.             }
  295.  
  296.             private static double aFromPol(double* r, float* phi)
  297.             {
  298.                 return *r * Math2.CosDeg(phi);
  299.             }
  300.  
  301.             private static double bFromPol(double* r, float* phi)
  302.             {
  303.                 return *r * Math2.SinDeg(phi);
  304.             }
  305.  
  306.             private static double rFromCart(double* a, double* b)
  307.             {
  308.                 return Math.Sqrt(*a * *a + *b * *b);
  309.             }
  310.  
  311.             private static float phiFromCart(double* a, double* b)
  312.             {
  313.                 float f = (float)(*b / *a);
  314.  
  315.                 f = (float)Math2.ATanDeg(&f);
  316.  
  317.                 correctangle(&f, a, b);
  318.  
  319.                 return f;
  320.             }
  321.  
  322.             private static void correctangle(float* targetAngle, double* a, double* b)
  323.             {
  324.                 switch (Coord2D.GetQuadrantXY(a, b))
  325.                 {
  326.                     case 2: *targetAngle += 180f; break;
  327.                     case 3: goto case 2;
  328.                 }
  329.  
  330.                 Angle.ValidateDeg(targetAngle);
  331.             }
  332.         }
  333.  
  334.         public class Coord2D
  335.         {
  336.             public static byte GetQuadrantDeg(float* degAnglePtr)
  337.             {
  338.                 float f = *degAnglePtr;
  339.                 Math2.Angle.ValidateDeg(&f);
  340.  
  341.                 for (byte b = 0; b < 4; b++)
  342.                 {
  343.                     if (f >= 90 * b) return (byte)(b + 1);
  344.                 }
  345.  
  346.                 return 0;
  347.             }
  348.  
  349.             public static byte GetQuadrantXY(double* x, double* y)
  350.             {
  351.                 if (*x > 0)
  352.                 {
  353.                     if (*y >= 0) return 1;
  354.                     if (*y < 0) return 4;
  355.                 }
  356.                 else if (*x < 0)
  357.                 {
  358.                     if (*y >= 0) return 2;
  359.                     if (*y < 0) return 3;
  360.                 }
  361.                 else
  362.                 {
  363.                     if (*y > 0) return 1;
  364.                     if (*y < 0) return 3;
  365.                 }
  366.  
  367.                 return 0;
  368.             }
  369.         }
  370.  
  371.         public class Angle
  372.         {
  373.             public static float DegToRad(float* deg)
  374.             {
  375.                 return (float)((Math.PI * *deg) / 180);
  376.             }
  377.  
  378.             public static float RadToDeg(float* rad)
  379.             {
  380.                 return (float)((180 * *rad) / Math.PI);
  381.             }
  382.  
  383.             public static void ValidateDeg(float* angle)
  384.             {
  385.                 if (*angle > 360)
  386.                 {
  387.                     *angle -= 360;
  388.                     ValidateDeg(angle);
  389.                     return;
  390.                 }
  391.  
  392.                 if (*angle < 0)
  393.                 {
  394.                     *angle += 360;
  395.                     ValidateDeg(angle);
  396.                     return;
  397.                 }
  398.             }
  399.         }
  400.  
  401.         public static double SinDeg(float* f)
  402.         {
  403.             return Math.Sin(Angle.DegToRad(f));
  404.         }
  405.  
  406.         public static double CosDeg(float* f)
  407.         {
  408.             return Math.Cos(Angle.DegToRad(f));
  409.         }
  410.  
  411.         public static double TanDeg(float* f)
  412.         {
  413.             return Math.Tan(Angle.DegToRad(f));
  414.         }
  415.  
  416.         public static double ASinDeg(float* f)
  417.         {
  418.             return Math.Asin(Angle.DegToRad(f));
  419.         }
  420.  
  421.         public static double ACosDeg(float* f)
  422.         {
  423.             return Math.Acos(Angle.DegToRad(f));
  424.         }
  425.  
  426.         public static double ATanDeg(float* f)
  427.         {
  428.             return Math.Atan(Angle.DegToRad(f));
  429.         }
  430.     }
  431. }
Language:
To highlight particular lines, prefix each line with @@








freetexthost.in © 2010-2013 - Powered by PASTE 1.0   website by GandhiG.com