C# & Jama 混合编程

本文介绍如何使用IKVM.NET将Java的Jama线性代数包转换为C#可用的动态链接库,并演示了一个具体的逆矩阵计算实例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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 .NET
A .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. 重要链接

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值