您的位置:首页 > 编程语言 > C#

【c#】 5. Tensor数据(用于Excel中不同sheet中都有矩阵的情况)

2015-02-28 20:40 411 查看
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace UserDefinedDataEXP
{
class Tensor<T>
{
private int m_rows;
private int m_columns;
private int m_depth;
private Array<NumericMatrix<T>> m_tensor;

// Constructor with #rows, columns and depth, all startindexes = 1.
//每个矩阵都是rows*columns,在这个数组中nthird个
public Tensor(int rows, int columns, int nthird)
{
m_tensor = new Array<NumericMatrix<T>>(nthird, 1);
for (int i = m_tensor.MinIndex; i <= m_tensor.MaxIndex; i++)
{
m_tensor[i] = new NumericMatrix<T>(rows, columns, 1, 1);
}
m_rows = rows;
m_columns = columns;
m_depth = nthird;
}

public  NumericMatrix<T>this[int depth]
{
get { return m_tensor[depth]; }
set { m_tensor[depth] = value; }
}

//MinThirdIndex属性
public int MinThirdIndex
{
get { return 1; }
}

//MaxThirdIndex属性
public int MaxThirdIndex
{
get { return m_depth; }
}
}
}
using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Diagnostics;namespace UserDefinedDataEXP{    class test    {            static void Main(string[] args)            {                // Tensors, calculate powers of a matrix and print                int nrows = 2;                int ncols = 2;                int ndepth = 100;                NumericMatrix<double> T = new NumericMatrix<double>(nrows, ncols);                T[1, 1] = 1.0; T[2, 2] = 1.0;                T[1, 2] = .001; T[2, 1] = 0.0;                Tensor<double> myT = new Tensor<double>(nrows, ncols, ndepth);                myT[myT.MinThirdIndex] = T;                for (int j = myT.MinThirdIndex + 1; j <= myT.MaxThirdIndex; j++)                {                    myT[j] = T * myT[j - 1];                }                for (int j = myT.MinThirdIndex + 1; j <= myT.MaxThirdIndex; j++)                {                    // Print every tenth matrix in tensor                    if ((j / 10) * 10 == j)                    {                        myT[j].print();                        Console.WriteLine();                    }                }                Console.Read();            }       }
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: