C#による 最近傍相関行列の計算

C#によるサンプルソースコード : 使用関数名:g02aa

Keyword: 最近傍相関行列

概要

本サンプルは最近傍相関行列の計算を行うC#によるサンプルプログラムです。 本サンプルは以下に示される行列に対して最近傍相関行列の計算を行います。

最近傍相関行列のデータ 

※本サンプルはnAG Library for .NETに含まれる関数 g02aa() のExampleコードです。本サンプル及び関数の詳細情報は g02aa のマニュアルページをご参照ください。
ご相談やお問い合わせはこちらまで

出力結果

(本関数の詳細はg02aa のマニュアルページを参照)

この出力例をダウンロード
g02aa Example Program Results

      Nearest Correlation Matrix

     1.00000    -0.80841     0.19159     0.10678
    -0.80841     1.00000    -0.65623     0.19159
     0.19159    -0.65623     1.00000    -0.80841
     0.10678     0.19159    -0.80841     1.00000


 Number of Newton steps taken:          3
 Number of function evaluations:        4

  • 3〜8行目に最近傍相関行列が出力されています。
  • 11行目に実行されたニュートンステップの数が出力されています。
  • 12行目に関数評価の数が出力されています。

ソースコード

(本関数の詳細はg02aa のマニュアルページを参照)

※本サンプルソースコードは .NET環境用の科学技術・統計計算ライブラリである「nAG Library for .NET」の関数を呼び出します。
サンプルのコンパイル及び実行方法


このソースコードをダウンロード
//      g02aa Example Program Text
//      C# version, nAG Copyright 2008
using System;
using NagLibrary;
using System.IO;
namespace NagDotNetExamples
{
  public class G02AAE
  {
    static void Main(String[] args)
    {
      StartExample();
    }
    public static void StartExample()
    {
      try
      {
        double errtol,   nrmgrd; int feval,  i,  iter,  j,  maxit,  maxits,  n;
        int ifail;
        Console.WriteLine("g02aa Example Program Results");
        Console.WriteLine("");
        // 
        //      Set up matrix G
        n = 4;
        double[,] g = new double[n, n];
        double[,] x = new double[n, n];
        for (j = 1; j <= n; j++)
        {
          for (i = 1; i <= n; i++)
          {
            g[i - 1, j - 1] = 0.0;
          }
          g[j - 1, j - 1] = 2.00e0;
        }
        for (j = 2; j <= n; j++)
        {
          g[j - 1 - 1, j - 1] = -1.00e0;
          g[j - 1, j - 1 - 1] = -1.00e0;
        }
        // 
        //      Set up method parameters
        errtol = 1.00e-7;
        maxits = 200;
        maxit = 10;
        // 
        G02.g02aa(g, n, errtol, maxits, maxit, x, out iter, out feval, out nrmgrd, out ifail);
        // 
        if (ifail == 0)
        {
          Console.WriteLine(" {0}", "     Nearest Correlation Matrix\r\n");
          for (i = 1; i <= n; i++)
          {
            for (j = 1; j <= n; j++)
            {
              Console.Write(" {0,11:f5}{1}", x[i - 1, j - 1],j%4==0? "\n":"");
            }
          }
          Console.WriteLine("");
          Console.WriteLine("");
          Console.WriteLine("{0}{1,11}", " Number of Newton steps taken:", iter);
          Console.WriteLine("{0}{1,9}", " Number of function evaluations:", feval);
          if (nrmgrd > errtol)
          {
            Console.WriteLine("{0}{1,11:e3}", " Norm of gradient of last Newton step:", nrmgrd);
          }
        }
        else
        {
          Console.WriteLine("** g02aa failed with ifail = {0,5}", ifail);
        }
        Console.WriteLine("");
        // 
      }
      catch (Exception e)
      {
        Console.WriteLine(e.Message);
        Console.WriteLine("Exception Raised");
      }
    }
  }
}



関連情報
Privacy Policy  /  Trademarks