Use ilnumerics to execute computing in. net

Source: Internet
Author: User

1. download and install the ilnumerics function library

Download ilnumerics at http://ilnumerics.net/$editions.htmlCommunityVersion. The current version is v2.13, which is about 27.3 MB. The decompressed directory structure 1 is shown in.

Figure 1 unzipping the ilnumerics source code package

Now you need to compile the ilnumerics function library, use visualstudio to open the project file ilnumerics. csproj, and compile it again. The generated ilnumerics. Dill library is in the debug/release directory, as shown in 2.

Figure 2 Compilation result

The following is how to add the ilnumerics function library to your project.

First, add a reference to ilnumerics. DLL to the project reference, and addLibiomp5md. dll,Mkl_custom32.dll,Opentk. dllFile to add content files to the ProjectRoot directoryIf it is a Linux platform, the *. So file should be added, as shown in 3.
.

Figure 3 add ilnumerics to the Project

Ii. Use ilnumerics for a small example

The following uses ilnumerics to solve linear equations. This example is from the official website. Note that the ilnumerics namespace is referenced first. In addition, the main class program inherits the ilnumerics. ilmath class to facilitate the use of its static functions, but it may not be used in reality.

Using system; using system. collections. generic; using system. LINQ; using system. text; using ilnumerics; namespace consoleapplication1 {// It is recommended to derive from ilmath class program: ilnumerics. ilmath {static void main (string [] ARGs) {// create a matrix A, give values explicitely ilarray <double> A = array <double> (New Double, ,},); // use a creation function for B ilarray <double> B = counter ); // use a function of the base class: ilmath. linsolve ilarray <double> result = linsolve (a, B); //. tostring () gives formated output console. out. writeline ("A:" + environment. newline +. tostring (); console. out. writeline ("B:" + environment. newline + B. tostring (); console. out. writeline ("A * [Result] = B:" + environment. newline + result. tostring (); // check result: // uses norm, multiply, EPS and binary operators if (norm (multiply (A, result)-B) <= EPS) {console. out. writeline ("result OK");} else {console. out. writeline ("result false");} console. readkey ();}}}

Output result:

 
A: <double> [4, 4] 1 1 1 1 1 2 2 3 4 1 3 6 10 1 4 10 20 B: <double> [] 1 5 2 6 3 4 8 A * [Result] = B: <double> [] 0 4 1 1 0 0 0 result OK

Iii. Considerations

To obtain the ilnumerics in. net, which optimizes these aspects: 1. function parameters use the value type. 2. once you exit the function, garbage collection starts immediately. 3. the memory is used to allocate arrays cyclically; 4. delayed array replication-use memory only for writing; 5. all operations on the array are done in the same place at any time.

These optimizations are not free of charge, so there are more restrictions when using ilnumerics than when using a general math library. For example, in C,The VaR keyword cannot be used for any types defined in ilnumerics..Conformity operators, such as + =,-=,/=, and * =, cannot be used for any types defined in ilnumerics.. Strictly speaking, these operators cannot be used for ilnumerics array indexes. For example, a [0] + = 1 is incorrect, and a + = 1 is supported. Official suggestion: to avoid errors, all types of ilnumerics do not use the conformity operator.
In addition, if you want to achieve more performance improvements, you need to follow more rules when defining functions. Figure 4 shows the rule.

Figure 4 ilnumerics define the rules of a function (image link: http://ilnumerics.net/img/NiceCodeExample_FreqPeaksOverview2.png)

A. The most common array types in ilnumerics include ilarray <t>, ilcell, and illogical. However, the list of function-defined parameters uses specialized input, output, and returned array types, such as Il.InArray <double>, ilOutArray <int>, ilRETArray <double>. They comply with the unified naming rules:

Il [ | In | Out | RET ] [ Array < T > | Cell | Logical ]

Again, these in | Out | RET arrays are only used in the function parameter declaration. Also, the ref and out keywords in C # are no longer used.

B. Function body rules. The function body needs to be wrapped in the following using block:

 
Using (ilscope. Enter (in1, in2,...) {// function body ...}

The using block creates a function range. All arrays created in the using block are cleared immediately after the block is released, and the input parameters are also cleared. Therefore, ilscope. enter () needs to receive input parameters declared in all functions (that is, ilinarray, ilincell, or ilinlogical parameters, excluding C # normal value variables ). Figure 5 shows an example of function definition.

Figure 5 function definition example (image URL: http://ilnumerics.net/img/NiceCodeExample_FreqPeaksHeadBody.png)

Note that the statement used to assign values to the frequencies array in Figure 5 uses.The setter does not directly assign values with equal signs. In addition, the check function in the using block checks the input parameters. Here, it mainly checks whether the value is null andThe check function should be the only place to reference input parameters.. Of course, you can also use other overloaded versions of check for more checks, such:

 
B = check (INB, (B) => {// checks on column vector also if (! B. isvector) throw new ilargumentexception ("INB must be a Vector! "); Return (B. isrowvector )? B. C: B. t ;});

As mentioned above, the optimization of ilnumerics uses the value type for function parameters, so itsFunction input parameters are unchangeable.. Therefore, for each input parameter, the corresponding local variable must be created, as shown in 5.

Finally, it is an example of Function Definition and usage. In ilnumerics, there is no direct inverse method for the matrix, so this method is used to reverse the input matrix.

 
Public static ilretarray <double> inverse (ilinarray <double> INA) {using (ilscope. enter (INA) {ilarray <double> A = ilmath. check (INA, (B) => {If (B. s. numberofdimensions! = 2) | (B. S [0]! = B. S [1]) throw new ilnumerics. Exceptions. ilargumentexception ("INA must be a square matrix! "); Return B;}); int size = A. S. Longest; return ilmath. linsolve (A, ilmath. Eye (size, size ));}}

The call method is as follows:

 
Ilarray <double> M = ilmath. Rand (2, 2); ilarray <double> I = inverse (m );

Iv. User Manual

See the online help documentation http://ilnumerics.net/support_documentation?arrays.html.

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.