XLPack API: C#使用例

C#からXLPackを呼び出す

C#からXLPackを呼び出すためにはDLLを直接使う必要があります.
SDKにはDLLインターフェースプログラムxlpack.csが入っています. これを使うと, 呼び出しプログラムは “using static XLPack;” と宣言するだけでDLLインターフェースの詳細を気にせず組込み関数のように簡単にXLPackを呼び出すことができます.
係数行列aは2次元配列, 右辺行列bは1次元配列を使います.

C#プログラム例 (例題(1))

using System;
using System.IO;
using static XLPack;

public class Test
{

static void Test_dgesv()
{
    const int n = 3;
    double[,] a =
        {{ 0.2, -0.32, -0.8 },
         { -0.11, 0.81, -0.92 },
         { -0.93, 0.37, -0.29 }};
    double[] b = { -0.3727, 0.4319, -1.4247 };
    int[] ipiv = new int[n];
    int info;

    dgesv(n, a, ipiv, b, out info);
    Console.Write("x = {0}  {1}  {2}\n", b[0], b[1], b[2]);
    Console.Write("info = {0}\n", info);
}

public static int Main(string[] args)
{
    Test_dgesv();
    return 0;
}

}

実行結果

x = 0.86  0.64  0.51
info = 0

C#プログラム例 (例題(2))

例題(2)ではf(x)の積分をqk15を使って求めます. qk15はf(x)を定義する外部関数を必要とします. 外部関数はC#で作成することができ, qk15は必要なときにそれを呼び出します.

using System;
using System.IO;
using static XLPack;

public class Test
{

static double f_qk15(double x)
{
	return 1/(1 + x*x);
}

static void Test_qk15()
{
	double a, b, result, abserr;

	a = 0; b = 4;
	qk15(f_qk15, a, b, out result, out abserr);
	Console.Write("result = {0}, abserr = {1}\n", result, abserr);
}

public static int Main(string[] args)
{
    Test_qk15();
    return 0;
}

}

実行結果

result = 1.32581766136379, abserr = 0.00148272394121622