1. 软件版本信息
- Windows 10
- Visual Studio 2015 Professional
2. 相关文件链接
2.1 Jama包
Introduction:
JAMA is a basic linear algebra package for Java. It provides user-level classes for constructing and manipulating real, dense matrices. It is meant to provide sufficient functionality for routine problems, packaged in a way that is natural and understandable to non-experts. It is intended to serve as the standard matrix class for Java, and will be proposed as such to the Java Grande Forum and then to Sun. A straightforward public-domain reference implementation has been developed by the MathWorks and NIST as a strawman for such a class. We are releasing this version in order to obtain public comment. There is no guarantee that future versions of JAMA will be compatible with this one.
From Jama Official Website https://math.nist.gov/javanumerics/jama/
Download Link:
官方链接:https://math.nist.gov/javanumerics/jama/
百度网盘:https://pan.baidu.com/s/1LOsXGN3E5IJ8iuOC6gwDdQ 密码:2sfz
2.2 IKVM
Introduction:
IKVM.NET is an implementation of Java for Mono and the Microsoft .NET Framework. It includes the following components:
A Java Virtual Machine implemented in .NETA .NET implementation of the Java class libraries
Tools that enable Java and .NET interoperability
From IKVM Official Website https://www.ikvm.net/#Introduction
Download Link:
官方链接:http://www.ikvm.net/download.html 或 https://sourceforge.net/projects/ikvm/files/
百度网盘:https://pan.baidu.com/s/1nX0P4RgzBT5Z8I7ft2tn5Q
3. Jar包到动态链接库的编译
3.1 设置路径
解压ikvm-7.2.4630.5.zip, 并将%IKVM_HOME%/bin添加到path中。此处,%IKVM_HOME%是指解压后ikvm的主目录。具体如下:
Step 1:
Step 2:
3.2 执行编译
进入dos终端,一路输入以命令"cd"方式进入Jama-1.0.3包含的Jama-1.0.3.Jar包所在文件位置,输入编译命令:ikvmc -out:Jama103.dll Jama-1.0.3.jar,此后会在此Jar包所在位置生成一个名为Jama-1.0.3.dll的文件。此文件即为后面所需的动态链接库文件。为便于理解,编译设置也已图片形式呈现,如下:
Step 1:
Step 2:
4. 代码
4.1 新建C#项目
注意添加四个动态链接库文件,即第3.2步编译的Jama103.dll,以及此前下载的IKVM所在bin目录下的IKVM.OpenJDK.Core.dll, IKVM.Runtime.dll, 和IKVM.Runtime.JNI.dll.
随后,注意在项目中添加对此吃个动态链接库的引用,如下:
4.1 图片形式
4.2 代码形式:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Jama;
using IKVM;
namespace TestInvokeJamaInCSharp
{
class Program
{
static void Main(string[] args)
{
double[][] data = new double[3][];
data[0] = new double[3];
data[1] = new double[3];
data[2] = new double[3];
data[0][0] = 1;data[0][1] = 2;data[0][2] = 1;
data[1][0] = 2;data[1][1] = 3;data[1][2] = 1;
data[2][0] = 4;data[2][1] = 5;data[2][2] = 3;
var dataInv = InverseFunctionFromJama(data);
Console.ReadKey();
}
/// <summary>
/// Inverse A Matrix
/// </summary>
/// <param name="dataIn"></param>
/// <returns></returns>
public static double[][] InverseFunctionFromJama(double[][] dataIn)
{
Jama.Matrix A = new Jama.Matrix(dataIn);
Jama.Matrix invA = A.inverse();
double[][] rst = invA.getArray();
return rst;
}
/// <summary>
/// 显示double[,]两位数组信息
/// </summary>
/// <param name="dataIn"></param>
public void DisplayInvInfs(double[][] dataIn)
{
for(int g1 = 0; g1 < dataIn.GetLength(0); g1++)
{
for(int g2 = 0; g2 < dataIn.GetLength(1); g2++)
{
Console.WriteLine(dataIn[g1][g2]);
}
}
}
}
}
5. 结果
C#调用Jama包计算结果:
Matlab计算结果:
6. 重要链接