SuanShu in .NET environment
We provide a version of SuanShu that can be used in Microsoft .NET environment. The following explains how to set up Visual Studio to use SuanShu.NET.
Please note that SuanShu is primarily a Java library, of which the .NET version is a conversion. Hence there are some benefits gained by using the Java version, which will be described at the bottom of this page.
Obtaining the Distribution
You can download the most recent version of the distribution from SuanShu’s ​product website.
Included in the distribution are:
- The converted suanshu.dll
- A converted version of the JodaTime jar
- The parts of IKVM required to use the converted assemblies.
Importing the Assemblies
In order to use SuanShu.NET in your application, you will need to add it and its dependencies as references in Visual Studio. All of the files that you need to add are included in the distribution.
Visual Studio 2010
These directions are for Visual Studio 2010, although the procedure is very similar for other recent versions.
To add the references, right-click your project in the ‘Solution Explorer’ and click ‘Add Reference’.
[one_half_last][/one_half_last]
Alternatively you can also select ‘Add Reference’ from the ‘Project’ menu. This should open the ‘Add Reference’ dialog. In the dialog, please select the ‘Browse’ tab and navigate to the directory in which you downloaded the SuanShu.NET distribution. Select all files and click ‘OK’. You should then see them being listed as References in the ‘Solution Explorer’.
Placing the license file
For SuanShu to run, it has to have access to the license file. In Java it will look for the license file on the classpath of your current project. In .NET it will only check the directory of your executable.
When running your project straight from Visual Studio, this would be the bin\Debug and bin\Release folders inside your project folder. For projects using Office integration features, the license file has to be in your ‘My Documents’ folder. To manually change the license file location, see the tips and tricks section below.
Examples
Using SuanShu with .NET is largely very similar to using it in Java. In the following I will give brief demonstrations on how to do some basic operations in C#.
Simple Example
In the blank project there is a simple example that shows how to call SuanShu code from C# to do matrix multiplication.
After importing the necessary classes (this can be done automatically by Visual Studio):
using com.numericalmethod.suanshu.matrix.doubles;
using com.numericalmethod.suanshu.matrix.doubles.matrixtype.dense;
using com.numericalmethod.suanshu.license;
we can do matrix multiplication with the following code:
Matrix A = new DenseMatrix(new double[][] {
new double[] { 1.0, 2.0, 3.0 },
new double[] { 3.0, 5.0, 6.0 },
new double[] { 7.0, 8.0, 9.0 }
});
Matrix B = new DenseMatrix(new double[][] {
new double[] { 2.0, 0.0, 0.0 },
new double[] { 0.0, 2.0, 0.0 },
new double[] { 0.0, 0.0, 2.0 }
});
Matrix AB = A.multiply(B);
Console.Write(AB.ToString());
Further tips on how to use SuanShu with .NET can be found at the bottom of this page.
Code Examples
https://github.com/nmltd/SuanShu/tree/master/SuanShu.NET
Documentation
Included in the distribution in an XML file containing XML comments, which can be viewed in Visual Studio. However since it is a conversion from Java’s JavaDoc comments, it is possible that documentation for some parts are missing or incomplete. Furthermore documentation for classes from the Java library is not included.
In either of those cases, please check the latest JavaDoc.
Tips and Tricks
Setting the location of the license file
You can manually set the location of the license file before calling any SuanShu code. This can be done by calling:
com.numericalmethod.suanshu.license.License.setLicenseFile()
Datatypes
Since the library uses converted Java datatypes instead of their .NET equivalents. Hence for the SuanShu code to interact with other .NET code you may need to convert the datatypes. For example for lists, this can be accomplished as follows (in C#):
List cSharpList = new List();
java.util.List javaList = java.util.Arrays.asList(cSharpList.ToArray());
List
Unfortunately due to Java’s type erasure at compile time, the converted Java collections are not generic.
Jagged arrays
In .NET we have multi-dimensional arrays ([,] in C#) and jagged arrays ([][], e.g. arrays of arrays). Since Java only supports the latter, functions for which a multi-dimensional array would have been appropriate, will use jagged arrays. To see how a jagged array is defined, see the matrix example above.
Benefits of using the Java version
Even though the .NET version of SuanShu is fully featured, there are a few benefits of using the Java version:
- Performance is about 2x better
- The JavaDoc documentation is more complete than the XML comments
- There is no need to convert between .NET and Java datatypes
- Types are generic
- The naming conventions of SuanShu are consistent with Java conventions