C # mathematical operations

Source: Internet
Author: User
Tags acos asin

If you are interested in mathematics, read this article.
We know the problem of root seeking for Equations or numerical points. Even if the algorithm is compiled, We need to compile a function for root seeking or numerical points each time. This is really tedious and inefficient. I used C #. Net (beta2) to compile a one-dimensional real function class, which simplifies the problem a lot. For example, a function such as f (x) = cos (x) ^ 3 can be expressed as function f = function. Cos ^ 3 in my method. For F (1), follow the following method F. Fun (1. Is it okay.
We know that elementary functions can be expressed in a subexpression by using a limited number of addition, subtraction, multiplication, division, and compound steps. In this class, these six basic elementary functions are provided. They are added with + representation, represented by minus-, represented by *, represented by/, and represented by ^, composite is represented by &, and all operators are overloaded.
Habitual expression function expression
Add: f (x) = sin (x) + cos (x) function f = function. Sin + function. Cos
Subtraction: f (x) = exp (x)-Ln (x) function f = function. Exp-Function.Log
Multiplication: f (x) = sin (x) * ln (x) funciton F = function. sin * function. ln
Except: f (x) = sin (X)/cos (x) function f = function. Sin/function. Cos
Multiplication Side: f (x) = exp (x) ^ 3 function f = function. Exp ^ 3
Compound: f (x) = exp (sin (x) function f = function. Exp & function. Sin

 

Note: (1) Because C # Is case sensitive, please note the case sensitivity of letters.
(2) comments about classes in the source code. I believe you can understand them.
// ================================================ =====
// Source code
// ================================================ =====
Namespace maths
{
Using system;
Public Delegate double func (double );
// One-dimensional real function class
Public class function
{
Private funcnode [] fnode; // node Array
// Constructor 1 to generate a C-constant function
Public Function (double C)
{
This. fnode = new funcnode [1];
This. fnode [0] = new func (function. Unit );
Fnode [0]. yfactor = C;
}
// Constructor 2
Public Function (funcnode node)
{
This. fnode = new funcnode [1];
Fnode [0] = node;
}
// Constructor 3
Public Function (funcnode [] fnode)
{
This. fnode = new funcnode [fnode. Length];
For (INT I = 0; I <fnode. length; I ++)
This. fnode [I] = fnode [I];
}
// Sum of the two functions
Public static function operator + (function F1, function F2)
{
Funcnode [] fnode = new funcnode [f1.fnode. Length + f2.fnode. Length + 1];
Fnode [0] = Operator. CADD;
For (INT I = 1; I <= f1.fnode. length; I ++)
Fnode [I] = f1.fnode [I-1];
For (INT I = f1.fnode. Length + 1; I <fnode. length; I ++)
Fnode [I] = f2.fnode [i-f1.fnode.Length-1];
Return new function (fnode );
}
// Difference between the two functions
Public static function operator-(function F1, function F2)
{
Funcnode [] fnode = new funcnode [f1.fnode. Length + f2.fnode. Length + 1];
Fnode [0] = Operator. csub;
For (INT I = 1; I <= f1.fnode. length; I ++)
Fnode [I] = f1.fnode [I-1];
For (INT I = f1.fnode. Length + 1; I <fnode. length; I ++)
Fnode [I] = f2.fnode [i-f1.fnode.Length-1];
Return new function (fnode );
}
// Product of two functions
Public static function operator * (function F1, function F2)
{
Funcnode [] fnode = new funcnode [f1.fnode. Length + f2.fnode. Length + 1];
Fnode [0] = Operator. cmul;
For (INT I = 1; I <= f1.fnode. length; I ++)
Fnode [I] = f1.fnode [I-1];
For (INT I = f1.fnode. Length + 1; I <fnode. length; I ++)
Fnode [I] = f2.fnode [i-f1.fnode.Length-1];
Return new function (fnode );
}
// Operator of two functions
Public static function operator/(function F1, function F2)
{
Funcnode [] fnode = new funcnode [f1.fnode. Length + f2.fnode. Length + 1];
Fnode [0] = Operator. cdiv;
For (INT I = 1; I <= f1.fnode. length; I ++)
Fnode [I] = f1.fnode [I-1];
For (INT I = f1.fnode. Length + 1; I <fnode. length; I ++)
Fnode [I] = f2.fnode [i-f1.fnode.Length-1];
Return new function (fnode );
}
// Multiplication of two functions
Public static function operator ^ (function F1, function F2)
{
Funcnode [] fnode = new funcnode [f1.fnode. Length + f2.fnode. Length + 1];
Fnode [0] = Operator. cpow;
For (INT I = 1; I <= f1.fnode. length; I ++)
Fnode [I] = f1.fnode [I-1];
For (INT I = f1.fnode. Length + 1; I <fnode. length; I ++)
Fnode [I] = f2.fnode [i-f1.fnode.Length-1];
Return new function (fnode );
}
// F & g (x) = f (g (x ))
Public static function operator & (function F1, function F2)
{
Funcnode [] fnode = new funcnode [f1.fnode. Length + f2.fnode. Length + 1];
Fnode [0] = Operator. ccom;
For (INT I = 1; I <= f1.fnode. length; I ++)
Fnode [I] = f1.fnode [I-1];
For (INT I = f1.fnode. Length + 1; I <fnode. length; I ++)
Fnode [I] = f2.fnode [i-f1.fnode.Length-1];
Return new function (fnode );
}
// Calculate the value of a complex function
Public double fun (Double X)
{
Return this. funv (x, 0, fnode. Length-1 );
}
Public double funv (Double X, int low, int high)
{
If (Low = high)
Return fnode [low]. Fun (X );
Else
{
Int signnum = 0;
Int funcnum = 0;
Int mid = 0;
For (INT I = low; I <= high; I ++)
{
If (fnode [I]. isfunction) funcnum ++;
Else
Signnum ++;
If (funcnum = signnum)
{
Mid = I;
Break;
}
}
Double V = 0;
Switch (fnode [low]. Slow)
{
Case operator. CADD: V = This. funv (x, low + 1, mid) + this. funv (x, Mid + 1, high); break;
Case operator. csub: V = This. funv (x, low + 1, mid)-This. funv (x, Mid + 1, high); break;
Case operator. cmul: V = This. funv (x, low + 1, mid) * This. funv (x, Mid + 1, high); break;
Case operator. cdiv: V = This. funv (x, low + 1, mid)/This. funv (x, Mid + 1, high); break;
Case operator. cpow: V = math. Pow (this. funv (x, low + 1, mid), this. funv (x, Mid + 1, high); break;
Case operator. ccom: V = This. funv (this. funv (x, Mid + 1, high), low + 1, mid); break;
}
Return V;
}
}
//--
// Below are the function classes of the six basic elementary functions
//--
// Unit Function
Public static function unit
{
Get
{
Funcnode node = new func (function. Unit );
Return new function (node );
}
}
// Constant function C
Public static function constfunc (double C)
{
Funcnode node = new func (function. Unit );
Node. yfactor = C;
Return new function (node );
}
// Power Function x ^
Public static function POW (double)
{
Funcnode node = new func (function. funcx );
Function F1 = new function (node );
Return F1 ^ function. constfunc ();
}
// Exponential function
Public static Function exp
{
Get
{
Funcnode node = new func (math. exp );
Return new function (node );
}
}
// Logarithm function
Public static function log
{
Get
{
Funcnode node = new func (math. Log );
Return new function (node );
}
}
// Sine function
Public static function sin
{
Get
{
Funcnode node = new func (math. Sin );
Return new function (node );
}
}
// Cosine function
Public static function cos
{
Get
{
Funcnode node = new func (math. Cos );
Return new function (node );
}
}
// Tangent function
Public static function Tan
{
Get
{
Funcnode node = new func (math. Tan );
Return new function (node );
}
}
// Cotangent Function
Public static function cot
{
Get
{
Funcnode node = new func (function. ctan );
Return new function (node );
}
}
// Supplement the cotangent Function
Private Static double ctan (Double X)
{
Return 1.0/Math. Tan (X );
}
// Arcsin Function
Public static function Asin
{
Get
{
Funcnode node = new func (math. asin );
Return new function (node );
}
}
// Arccosine Function
Public static function ACOs
{
Get
{
Funcnode node = new func (math. ACOs );
Return new function (node );
}
}
// Arctangent Function
Public static function atan
{
Get
{
Funcnode node = new func (math. atan );
Return new function (node );
}
}
// Anti-cotangent Function
Public static function ACO
{
Get
{
Funcnode node = new func (function. actan );
Return new function (node );
}
}
// Complementary function of the inverse cotangent Function
Private Static double actan (Double X)
{
Return math. PI/2-math.atan (X );
}
//
// Unit function 1
Private Static double unit (Double X)
{
Return 1;
}
Private Static double funcx (Double X)
{
Return X;
}
// Reload
Public override string tostring ()
{
Return this. tostring (0, fnode. Length-1 );
}
// Implicit conversion from double to this class
Public Static Implicit operator function (double)
{
Return function. constfunc ();
}
// Function expression
Private string tostring (INT low, int high)
{
If (Low = high)
Return "(" + fnode [low]. tostring () + ")";
Else
{
Int signnum = 0;
Int funcnum = 0;
Int mid = 0;
For (INT I = low; I <= high; I ++)
{
If (fnode [I]. isfunction) funcnum ++;
Else
Signnum ++;
If (funcnum = signnum)
{
Mid = I;
Break;
}
}
String STR = "";
Switch (fnode [low]. Slow)
{
Case operator. CADD: Str = "(" + this. tostring (low + 1, mid) + "+" + this. tostring (Mid + 1, high) + ")"; break;
Case operator. csub: Str = "(" + this. tostring (low + 1, mid) + "-" + this. tostring (Mid + 1, high) + ")"; break;
Case operator. cmul: Str = "(" + this. tostring (low + 1, mid) + "*" + this. tostring (Mid + 1, high) + ")"; break;
Case operator. cdiv: Str = "(" + this. tostring (low + 1, mid) + "/" + this. tostring (Mid + 1, high) + ")"; break;
Case operator. cpow: Str = "(" + this. tostring (low + 1, mid) + "^" + this. tostring (Mid + 1, high) + ")"; break;
Case operator. ccom: Str = "(" + this. tostring (low + 1, mid) + "&" + this. tostring (Mid + 1, high) + ")"; break;
}
Return STR;
}
}
}
//---
// Function node class
//---
Public class funcnode
{
Private operator;
Private func F;
Private double yfactor; // scaling factor of Y
Private double xfactor; // The scaling factor of X
Private double addconst; // Translation after yfactor * y
Private double xshift; // Translation after xfactor * x
// Constructor 1
Public funcnode (operator nodes)
{
If (operator = Operator. Empty)
Throw new exceptionfuncnode ("the input parameter of the struct constructor funcnode (operator) cannot be operator. Empty ");
This. Enabled = enabled;
This. f = NULL;
This. yfactor = 1;
This. xfactor = 1;
This. addconst = 0;
This. xshift = 0;
}
// Constructor 2
Public funcnode (func F)
{
If (F = NULL)
Throw new exceptionfuncnode ("the input parameter of the struct constructor funcnode (func) cannot be null ");
This. Operator = Operator. empty;
This. f = F;
This. yfactor = 1;
This. xfactor = 1;
This. addconst = 0;
This. xshift = 0;
}
// Delegates func to implicit conversion of this class
Public Static Implicit operator funcnode (func F)
{
Funcnode fnode = new funcnode (f );
Return fnode;
}
Public Static Implicit operator funcnode (operator nodes)
{
Return new funcnode (callback );
}
// Attributes
Public bool isfunction
{
Get
{
If (this. enabled! = Operator. Empty) return false;
Else
Return true;
}
}
// If the node represents a function, you can evaluate it.
Public double fun (Double X)
{
If (! This. isfunction) throw new exceptionfuncnode ("this node is not a function and cannot be used as a function value ");
Return yfactor * F (xfactor * x + xshift) + addconst;
}
// Attributes
Public operator extends
{
Get
{
If (this. Operator = Operator. Empty) throw new exceptionfuncnode ("this node is a function ");
Return this. success;
}
Set
{
If (this. F! = NULL) This. Sums = value;
Else
Throw new exceptionfuncnode ("the current node is a function ");
}
}
// Attributes
Public func F
{
Get
{
If (this. enabled! = Operator. Empty) throw new exceptionfuncnode ("this node is an operator ");
Return this. F;
}
Set
{
If (this. enabled! = Operator. Empty) throw new exceptionfuncnode ("this node is an operator ");
This. f = value;
}
}
// Attributes
Public double yfactor
{
Get
{
If (this. enabled! = Operator. Empty) throw new exceptionfuncnode ("this node is an operator ");
Return this. yfactor;
}
Set
{
If (this. enabled! = Operator. Empty) throw new exceptionfuncnode ("this node is an operator ");
This. yfactor = value;
}
}
// Attributes
Public double xfactor
{
Get
{
If (this. enabled! = Operator. Empty) throw new exceptionfuncnode ("this node is an operator ");
Return this. xfactor;
}
Set
{
If (this. enabled! = Operator. Empty) throw new exceptionfuncnode ("this node is an operator ");
If (value = 0) throw new exceptionfuncnode ("X's scaling volume cannot be 0 ");
This. xfactor = value;
}
}
// Attributes
Public double addconst
{
Get
{
If (this. enabled! = Operator. Empty) throw new exceptionfuncnode ("this node is an operator ");
Return this. addconst;
}
Set
{
If (this. enabled! = Operator. Empty) throw new exceptionfuncnode ("this node is an operator ");
This. addconst = value;
}
}
// Attributes
Public double xshift
{
Get
{
If (this. enabled! = Operator. Empty) throw new exceptionfuncnode ("this node is an operator ");
Return this. xshift;
}
Set
{
If (this. enabled! = Operator. Empty) throw new exceptionfuncnode ("this node is an operator ");
This. xshift = value;
}
}
// Reload
Public override string tostring ()
{
If (this. enabled! = Operator. Empty)
{
String STR = "";
Switch (this. enabled)
{
Case operator. CADD: Str = "+"; break;
Case operator. csub: Str = "-"; break;
Case operator. cmul: Str = "*"; break;
Case operator. cdiv: Str = "/"; break;
Case operator. cpow: Str = "^"; break;
Case operator. ccom: Str = "&"; break;
}
Return STR;
}
Else
{
String STR;
STR = This. f. method. tostring (). Remove (0, 7 );
STR = Str. Remove (Str. Length-7, 6 );
If (this. yfactor = 0)
{
If (this. addconst <0) Return "(" + this. addconst + ")";
Return this. addconst. tostring ();
}
Else
{
If (this. yfactor! = 1) STR = This. yfactor + "*" + STR;
If (this. xfactor! = 1) STR = Str. insert (Str. Length-1, this. xfactor + "* X ");
Else
STR = Str. insert (Str. Length-1, "x ");
If (this. xshift> 0)
STR = Str. insert (Str. Length-1, "+" + this. xshift );
Else if (this. xshift <0)
STR = Str. insert (Str. Length-1, this. xshift. tostring ());
If (this. addconst> 0)
STR = STR + "+" + this. addconst;
Else if (this. addconst <0)
STR = STR + this. addconst. tostring ();
}
Return STR;
}
}
}
Public class exceptionfuncnode: system. Exception
{
Private string errormessage;
Public String errormessage
{
Get
{
Return this. errormessage;
}
}
// Constructor
Public exceptionfuncnode (string errormessage)
{
This. errormessage = errormessage;
}
Public override string tostring ()
{
Return this. errormessage;
}
}
Public Enum Operator
{
CADD,
Csub,
Cmul,
Cdiv,
Cpow,
Ccom,
Empty
}
}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.