public ResultForm(MainControl inControl, Matrix[,] inResult) { InitializeComponent(); this.control = inControl; this.Result = inResult; Initialise(); }
public Matrix[,] Multy(Matrix[,] M1, Matrix[,] M2) { Matrix[,] Rezult = new Matrix[0, 0]; if (M1.GetLength(1) == M2.GetLength(0))//Если форма матриц согласована { Rezult = new Matrix[M1.GetLength(0), M2.GetLength(1)]; for (int i = 0; i < M1.GetLength(0); i++) { for (int j = 0; j < M2.GetLength(1); j++) { List <float> s = new List <float>(); for (int k = 0; k < M1.GetLength(1); k++) { s.Add(Math.Min(M1[i, k].Koef, M2[k, j].Koef)); } Rezult[i, j] = new Matrix(); Rezult[i, j].Koef = s.Max(); //??? Rezult[i, j].RowName = M1[i, 0].RowName; Rezult[i, j].ColName = M2[0, j].ColName; } } } return(Rezult); }
/// <summary> /// 计算误差系数A /// 系数参考书P84(5-28),注意k1与k2和PPT不同 /// </summary> /// <param name="aux">辅助空间坐标系的数值</param> /// <returns>n*5</returns> private Matrix CalcErrorA(Matrix[,] aux) { double[,] res = new double[this._data.Count, 5]; var f = this._cam.f; for (int i = 0; i < this._data.Count; i++) { var l = aux[0, i].Data; var r = aux[1, i].Data; double u1 = l[0, 0], v1 = l[1, 0], w1 = l[2, 0], u2 = r[0, 0], v2 = r[1, 0], w2 = r[2, 0]; res[i, 0] = u1 * v2 / w2; res[i, 2] = -u2 * v1 / w1; res[i, 3] = f * (1 + (v1 * v2) / (w1 * w2)); res[i, 1] = -u1; res[i, 4] = u2; } return(new Matrix(res, true)); }
/*----------------------------------------------------------------------------------------- * * Animation stuff : * * -----------------------------------------------------------------------------------------*/ /// <summary> /// Deletes all animation data. /// </summary> public void DeleteAnimation() { animData = null; firstFrame = 0; lastFrame = 0; trackCount = 0; }
public UpdateU(Matrix[,] U, Matrix [,] A, ref Matrix invL, int _k, int _iter) { UBlock = U; inv = invL; k = _k; iter = _iter; ABlock = A; }
public UpdateL(Matrix[,] L, Matrix[,] A, ref Matrix invU, int _k, int _iter) { LBlock = L; inv = invU; k = _k; iter = _iter; ABlock = A; }
private void b_Algorithm_Click(object sender, EventArgs e) { this.fuzzyRelation = new FuzzyRelations(this.control); Matrix[,] Result = this.fuzzyRelation.Algorithm(); ResultForm rForm = new ResultForm(this.control, Result); rForm.Show(); }
// 输入一个嵌套矩阵,输出一个List Matrix集合 public static List <Matrix> GetMatrixList(Matrix[,] MyBaseM, Location[] Lvector) { // 通道数量 int nVector = Lvector.Length; // 因为是12个通道 所以暂时固定为4 const int C_Length = 4; // 计算针对同一个通道的矩阵 Height和Width int iRow = MyBaseM.GetLength(0) - C_Length; int iCol = MyBaseM.GetLength(1) - C_Length; // 12个46*46矩阵 List <Matrix> Mlist = new List <Matrix>(); // 遍历12个通道, 组成为12个 Matrix数组 46*46 // 3,3起始位置是个很特殊的值 int baseRow = 2; int baseColumn = 2; for (int z = 0; z < nVector; z++) { // 为每一个通道做一层,根据输入矩阵大小 - 一个常量来计算 Matrix eachM = new Matrix(iRow, iCol); // 遍历12个通道时,对46*46 相对于通道的矩阵做处理 for (int i = 0; i < iRow; i++) { for (int j = 0; j < iCol; j++) { // 取得当前row column // 在大矩阵中获得相应矩阵 int curRow = i + baseRow; int curCol = j + baseColumn; Matrix curM = MyBaseM[curRow, curCol]; // 参考值矩阵 Location curL = Lvector[z]; int calRow = curRow + curL.row; int calCol = curCol + curL.column; Matrix calM = MyBaseM[calRow, calCol]; // 当前矩阵和参考矩阵做运算 得到一个值,放到i,j这个 // 位置, double Rtn = Calucate1(curM, calM); eachM.data[i, j] = Rtn; } } // 加入到一个List里 Mlist.Add(eachM); } return(Mlist); }
void InitMatrix2DArray(ref Matrix[,] Mat, int dim1, int dim2, int matrixSize) { Mat = new Matrix[dim1, dim2]; for (int i = 0; i < dim1; i++) { for (int j = 0; j < dim2; j++) { Mat[i, j] = new Matrix(matrixSize, matrixSize); } } }
public void writeRGB(Matrix[,] images, string path, string imageFormat, int numberChannels) { string newPath = path; int number = images.Length / numberChannels; for (int i = 0; i < number; i++) { for (int j = 0; j < numberChannels; j++) { switch (j) { case 0: newPath = path + "\\" + i + "r" + imageFormat; break; case 1: newPath = path + "\\" + i + "g" + imageFormat; break; case 2: newPath = path + "\\" + i + "b" + imageFormat; break; } Bitmap myBitmap = new Bitmap(images[i, j].CountRow, images[i, j].CountColumn); for (int k = 0; k < images[i, j].CountRow; k++) { for (int h = 0; h < images[i, j].CountColumn; h++) { Color color = new Color(); switch (j) { case 0: color = Color.FromArgb((byte)images[i, j][k, h], 0, 0); break; case 1: color = Color.FromArgb(0, (byte)images[i, j][k, h], 0); break; case 2: color = Color.FromArgb(0, 0, (byte)images[i, j][k, h]); break; } myBitmap.SetPixel(k, h, color); } } myBitmap.Save(newPath, ImageFormat.Bmp); newPath = path; } } }
/// <summary> /// Максимум из двух случайных величин с фазовым распределением /// </summary> /// <param name="A"></param> /// <param name="B"></param> /// <returns></returns> public static PhaseTypeVarible Max(PhaseTypeVarible A, PhaseTypeVarible B) { int n = A.NumberOfPhases; int m = B.NumberOfPhases; var alpha = A.InitialDistribution; var betta = B.InitialDistribution; var a = -A.SubGenerator * Computation.OnesColumn(n); var b = -B.SubGenerator * Computation.OnesColumn(m); var alpha0 = 1 - alpha.Sum(); var betta0 = 1 - betta.Sum(); //Генерация вектора gamma double[] gamma = new double[n * m + n + m]; int offset = 0; for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { gamma[offset] = alpha[i] * betta[j]; offset++; } } for (int i = 0; i < n; i++) { gamma[offset] = betta0 * alpha[i]; offset++; } for (int i = 0; i < m; i++) { gamma[offset] = alpha0 * betta[i]; offset++; } //Закончили генерировать вектор gamma // Computation.KroneckerProduct(Computation.Eye(n), b); Matrix[,] C = { { Computation.KroneckerSum(A.SubGenerator, B.SubGenerator), Computation.KroneckerProduct(Computation.Eye(n), b), Computation.KroneckerProduct(a, Computation.Eye(m)) }, { new Matrix(n, n * m), A.SubGenerator, new Matrix(n, m) }, { new Matrix(m, n * m), new Matrix(m, n), B.SubGenerator } }; return(new PhaseTypeVarible(new Matrix(C), gamma)); }
public void PreBuild(Matrix[,] matrix) { //часть 1: поставить нули, где группы не помещаются в аудитории for (int i = 0; i < Classes.Count; i++) { for (int j = 0; j < Orders.Count; j++) { if (Classes[i].Roominess < Orders[j].NumberStud) { matrix[i, j] = new Matrix { m = false }; } } } }
public Board(int sizeX, int sizeY, bool twoSided) { this.twoSided = twoSided; this.width = sizeX; this.height = sizeY; camera = new Camera((float)Math.Max(sizeX, sizeY) * 400.0f / 5.0f); nextPictureSet = PictureDatabase.GetNextPictureSet(twoSided ? 2 : 1); this.layout = new Chip[Width, Height]; this.boardPositionMatrices = new Matrix[Width, Height]; // Create all the chips for (int y = 0; y < Height; y++) { for (int x = 0; x < Width; x++) { Chip chip = new Chip(); chips.Add(chip); chip.XPosition = x; chip.YPosition = y; layout[x, y] = chip; Vector2 chipTexCoordFactor = new Vector2(1.0f / Width, 1.0f / Height); chip.TexCoordScale = chipTexCoordFactor; chip.TexCoordTranslationFront = new Vector2((x * chipTexCoordFactor.X), ((Height - 1) - y) * chipTexCoordFactor.Y); chip.TexCoordTranslationBack = new Vector2(((Width - 1) - x) * chipTexCoordFactor.X, ((Height - 1) - y) * chipTexCoordFactor.Y); } } // Remove one random chip emptyX = RandomHelper.Random.Next(0, Width); emptyY = RandomHelper.Random.Next(0, Height); Chip removed = layout[emptyX, emptyY]; chips.Remove(removed); layout[emptyX, emptyY] = null; }
/// <summary> /// Creates animation data /// </summary> /// <param name="firstFrame">Inclusive first frame</param> /// <param name="lastFrame"></param> /// <param name="nodeCount"></param> public void CreateAnimation(int firstFrame, int lastFrame, int nodeCount) { if (firstFrame > lastFrame) { throw new ArgumentException("firstFrame > lastFrame"); } if (nodeCount <= 0) { throw new ArgumentException("nodeCount must be positive"); } this.firstFrame = firstFrame; this.lastFrame = lastFrame; this.trackCount = nodeCount; animData = new Matrix[lastFrame - firstFrame + 1, nodeCount]; }
public void loadInput() { //string path = Path.Combine(AppDomain.CurrentDomain.BaseDirectory) + @"..\\..\\trainData"; DirectoryInfo dir = new DirectoryInfo(folderPath); output = new Matrix[dir.GetFiles().Length, numberChannels]; //Matrix[,] result = new Matrix[dir.GetFiles().Length, numberChannels]; int i = 0; foreach (var item in dir.GetFiles()) { Matrix[] load = loadImage(item.FullName); for (int j = 0; j < numberChannels; j++) { output[i, j] = load[j]; } i++; } }
static Matrix ToMatrix(Matrix[,] m) { Matrix matrix = new Matrix(m.GetLength(0) * 6, m.GetLength(1) * 6); for (int rows = 0; rows < m.GetLength(0); ++rows) { for (int matrixRow = 0; matrixRow < 6; ++matrixRow) { for (int cols = 0; cols < m.GetLength(1); ++cols) { for (int matrixCols = 0; matrixCols < 6; ++matrixCols) { matrix[6 * rows + matrixRow, 6 * cols + matrixCols] = m[rows, cols][matrixRow, matrixCols]; } } } } return(matrix); }
/// <summary> /// 计算误差常数 /// 系数参考书P84(5-29) /// </summary> /// <param name="aux">辅助空间坐标系的数值</param> /// <returns>n*1</returns> private Matrix CalcErrorL(Matrix[,] aux) { double[,] res = new double[this._data.Count, 1]; var f = this._cam.f; for (int i = 0; i < this._data.Count; i++) { var l = aux[0, i].Data; var r = aux[1, i].Data; double v1 = l[1, 0], w1 = l[2, 0], v2 = r[1, 0], w2 = r[2, 0]; res[i, 0] = -f * v1 / w1 + f * v2 / w2; } return(new Matrix(res, true)); }
public static Vector[] BlockTridiagonalGauss(Matrix[,] A, Vector[] F) { int N = F.Length; Vector[] res = new Vector[N]; //Прямий хід Matrix restMatrix = new Matrix(); Vector restVector = new Vector(); for (int i = 0; i < N - 1; i++) { A[i, 1] = A[i, 1] + restMatrix; F[i] = F[i] + restVector; Matrix A11 = (Matrix)A[i, 1].Clone(); Matrix A12 = (Matrix)A[i, 2].Clone(); Matrix A21 = (Matrix)A[i + 1, 0].Clone(); Vector V1 = (Vector)F[i].Clone(); Vector V2 = (Vector)F[i + 1].Clone(); A[i, 1] = new Matrix(1, 0, 0, 1); A[i + 1, 0] = new Matrix(); A[i, 2] = A11.InvertedMatrix() * A12; restMatrix = (-A21 * (A11.InvertedMatrix())) * A12; F[i] = A11.InvertedMatrix() * V1; restVector = (-A21 * (A11.InvertedMatrix())) * V1; } A[N - 1, 1] = A[N - 1, 1] + restMatrix; F[N - 1] = F[N - 1] + restVector; //Обернений хід res[N - 1] = (A[N - 1, 1].InvertedMatrix()) * F[N - 1]; for (int i = N - 2; i >= 0; --i) { res[i] = F[i] - A[i, 2] * res[i + 1]; } return(res); }
/// <summary> /// Создает блочную матрицу из массива матриц (костыль) /// </summary> /// <param name="Blocks"></param> public Matrix(Matrix[,] Blocks) { int n = 0; int m = 0; for (int i = 0; i < Blocks.GetLength(0); i++) { n += Blocks[i, 0].CountRow; } for (int i = 0; i < Blocks.GetLength(1); i++) { m += Blocks[0, i].CountColumn; } double[,] res = new double[n, m]; int offseti = 0; int offsetj = 0; for (int i = 0; i < Blocks.GetLength(0); i++) { offsetj = 0; for (int j = 0; j < Blocks.GetLength(1); j++) { for (int i1 = 0; i1 < Blocks[i, j].CountRow; i1++) { for (int j1 = 0; j1 < Blocks[i, j].CountColumn; j1++) { res[i1 + offseti, j1 + offsetj] = Blocks[i, j][i1, j1]; } } offsetj += Blocks[i, j].CountColumn; } offseti += Blocks[i, 0].CountRow; } this.n = n; this.m = m; this.matrix = res; }
static void write(Matrix[,] m, string filePath) { using (StreamWriter writer = new StreamWriter(filePath)) { for (int rows = 0; rows < m.GetLength(0); ++rows) { for (int matrixRow = 0; matrixRow < 6; ++matrixRow) { for (int cols = 0; cols < m.GetLength(1); ++cols) { for (int matrixCols = 0; matrixCols < 6; ++matrixCols) { writer.Write(m[rows, cols][matrixRow, matrixCols].ToString().PadRight(30)); } } writer.WriteLine(); } } } }
public static void PreBuild() { Orders = ser.OrderTable(); Rooms = ser.SelectRoom(); Matrix = new Matrix[Classes.Count, Orders.Count]; fillClasses(); //часть 1: поставить нули, где группы не помещаются в аудитории for (int i = 0; i < Classes.Count; i++) { for (int j = 0; j < Orders.Count; j++) { if (Classes[i].Roominess < Orders[j].Group_number) { Matrix[i, j] = new Matrix { m = false }; } } } }
public override void Initialize() { base.Initialize(); // Initialize output this.Input = InputLayer.Output; this.Output = new Matrix[filters]; this.Output_d_E = new Matrix[filters]; int in_r = this.Input[0].rows; int in_c = this.Input[0].cols; int f = Kernel_Size; int p = Padding; int s = Stride; int out_size_r = (in_r - f + 2 * p) / s + 1; int out_size_c = (in_c - f + 2 * p) / s + 1; for (int i = 0; i < filters; i++) { this.Output[i] = new Matrix(out_size_r, out_size_c); this.Output_d_E[i] = new Matrix(out_size_r, out_size_c); } // Initialize kernel kernels = new Matrix[filters, Input.Length]; for (int i = 0; i < filters; i++) { for (int j = 0; j < Input.Length; j++) { kernels[i, j] = new Matrix(kernel_size, kernel_size); kernels[i, j].Randomize(); } } // Initalize biases biases = new Matrix(filters, 1); biases.Randomize(); }
static void Main(string[] args) { double R = 0.1; MatrixBuilder mb = new MatrixBuilder(new CoordinateSystemConfig(0, 0.4, 0, System.Math.PI / 25, 4, 1), new CylindricalShellConfig(e: 80.1e10, v: 0.3, h: 0.005, r: R)); double[,] gNode = mb.GaussNodes; double phi = new Functions().phiBasis(0, gNode[0, 0], gNode[0, 1]); Matrix cl = mb.BuildClMatrix(0, 1, 0); double[] gaussWeights = mb.GaussWeights; double[,] gaussNodes = mb.GaussNodes; double detJ = mb.GetJacobian(0, gaussNodes[0, 0], gaussNodes[0, 1]); // PRINT // SUPER GLOBAL Matrix[,] matrix = mb.BuildGlobalMatrix(); Matrix left = ToMatrix(matrix); Matrix.Write(left, "LEFT.txt"); Matrix[] vector = mb.BuildGlobalVector(); Matrix right = ToVector(vector); Matrix.Write(right, "RIGHT.txt"); GaussSole gauss = new GaussSole(aMatrix: left, bVector: right, eps: 1e-50); Matrix result = gauss.Run(); Matrix.Write(result, "RESULT.txt"); System.Console.WriteLine("done"); System.Console.Read(); }
// 输入一个大矩阵,返回以8*8基本矩阵为基础的嵌套矩阵 public static Matrix[,] SplitMatrix(double[,] PicBase, mySize SmallBaseSize) { Matrix[,] mRtn = null; // 判断需要开辟多大的矩阵 int iBaseSize = SmallBaseSize.ROW; int iBigSize = PicBase.GetLength(0); int nNeed = iBigSize / iBaseSize; mRtn = new Matrix[nNeed, nNeed]; // 切割矩阵的控制代码,先写死 速度做出来 Location baseL = new Location(0, 0); for (int i = 0; i < nNeed; i++) { // 在外循环中,首地址一直向下移动8个单元 // 便利循环下一列 baseL.row = i * iBaseSize; for (int j = 0; j < nNeed; j++) { // 在内循环中,首地址一直向右移动8个单元 // 在每一行中,一列一列的循环 // 偏移指针, 首地址向右根据当前小矩阵j来移动 // 比如 第一个地址为(0,0) 第二个为(0,8) baseL.column = j * 8; Matrix eachM = new Matrix(PicBase, baseL, SmallBaseSize); mRtn[i, j] = eachM; } } return(mRtn); }
public Cards FillCards(int i, Matrix[,] mymatrix, int y, int x) { Cards mycards = new Cards(); if (mymatrix[y, x].Type == 'X') { mycards.cost = 1000; mycards.location_number = mymatrix[y, x].Location_N; mycards.typeC = mymatrix[y, x].Type; } if (mymatrix[y, x].Type == 'O') { mycards.cost = 0; mycards.location_number = mymatrix[y, x].Location_N; mycards.typeC = mymatrix[y, x].Type; } if (mymatrix[y, x].Type == '.') { mycards.cost = 1000; mycards.location_number = mymatrix[y, x].Location_N; mycards.typeC = mymatrix[y, x].Type; } return(mycards); }
/// <summary> /// Writes a 2D array of matrix structs to the output. /// </summary> /// <param name="name">Name of the value</param> /// <param name="value">2D array of matrices</param> public void Write(string name, Matrix[,] value) { if (value == null) { _output.Write(NULL_OBJECT); return; } else { _output.Write(A_OK); } int row = value.GetUpperBound(0); int col = value.GetUpperBound(1); _output.Write(row); _output.Write(col); for (int i = 0; i < row; i++) { for (int j = 0; j < col; j++) { this.Write(null, value[i, j]); } } }
public Board( int sizeX, int sizeY, bool twoSided ) { this.twoSided = twoSided; this.width = sizeX; this.height = sizeY; camera = new Camera( (float)Math.Max( sizeX, sizeY ) * 400.0f / 5.0f ); nextPictureSet = PictureDatabase.GetNextPictureSet( twoSided ? 2 : 1 ); this.layout = new Chip[ Width, Height ]; this.boardPositionMatrices = new Matrix[ Width, Height ]; for( int y = 0; y < Height; y++ ) { for( int x = 0; x < Width; x++ ) { Chip chip = new Chip(); chips.Add( chip ); chip.XPosition = x; chip.YPosition = y; layout[ x, y ] = chip; Vector2 chipTexCoordFactor = new Vector2( 1.0f / Width, 1.0f / Height ); chip.TexCoordScale = chipTexCoordFactor; chip.TexCoordTranslationFront = new Vector2( ( x * chipTexCoordFactor.X ), ( ( Height - 1 ) - y ) * chipTexCoordFactor.Y ); chip.TexCoordTranslationBack = new Vector2( ( ( Width - 1 ) - x ) * chipTexCoordFactor.X, ( ( Height - 1 ) - y ) * chipTexCoordFactor.Y ); } } emptyX = RandomHelper.Random.Next( 0, Width ); emptyY = RandomHelper.Random.Next( 0, Height ); Chip removed = layout[ emptyX, emptyY ]; chips.Remove( removed ); layout[ emptyX, emptyY ] = null; }
// function for square matrix 2^N x 2^N private static void StrassenMultiplyRun(Matrix A, Matrix B, Matrix C, int l, Matrix[,] f) // A * B into C, level of recursion, matrix field { int size = A.rows; int h = size / 2; if (size < 32) { for (int i = 0; i < C.rows; i++) { for (int j = 0; j < C.cols; j++) { C[i, j] = 0; for (int k = 0; k < A.cols; k++) { C[i, j] += A[i, k] * B[k, j]; } } } return; } AplusBintoC(A, 0, 0, A, h, h, f[l, 0], h); AplusBintoC(B, 0, 0, B, h, h, f[l, 1], h); StrassenMultiplyRun(f[l, 0], f[l, 1], f[l, 1 + 1], l + 1, f); // (A11 + A22) * (B11 + B22); AplusBintoC(A, 0, h, A, h, h, f[l, 0], h); ACopytoC(B, 0, 0, f[l, 1], h); StrassenMultiplyRun(f[l, 0], f[l, 1], f[l, 1 + 2], l + 1, f); // (A21 + A22) * B11; ACopytoC(A, 0, 0, f[l, 0], h); AminusBintoC(B, h, 0, B, h, h, f[l, 1], h); StrassenMultiplyRun(f[l, 0], f[l, 1], f[l, 1 + 3], l + 1, f); //A11 * (B12 - B22); ACopytoC(A, h, h, f[l, 0], h); AminusBintoC(B, 0, h, B, 0, 0, f[l, 1], h); StrassenMultiplyRun(f[l, 0], f[l, 1], f[l, 1 + 4], l + 1, f); //A22 * (B21 - B11); AplusBintoC(A, 0, 0, A, h, 0, f[l, 0], h); ACopytoC(B, h, h, f[l, 1], h); StrassenMultiplyRun(f[l, 0], f[l, 1], f[l, 1 + 5], l + 1, f); //(A11 + A12) * B22; AminusBintoC(A, 0, h, A, 0, 0, f[l, 0], h); AplusBintoC(B, 0, 0, B, h, 0, f[l, 1], h); StrassenMultiplyRun(f[l, 0], f[l, 1], f[l, 1 + 6], l + 1, f); //(A21 - A11) * (B11 + B12); AminusBintoC(A, h, 0, A, h, h, f[l, 0], h); AplusBintoC(B, 0, h, B, h, h, f[l, 1], h); StrassenMultiplyRun(f[l, 0], f[l, 1], f[l, 1 + 7], l + 1, f); // (A12 - A22) * (B21 + B22); /// C11 for (int i = 0; i < h; i++) // rows { for (int j = 0; j < h; j++) // cols { C[i, j] = f[l, 1 + 1][i, j] + f[l, 1 + 4][i, j] - f[l, 1 + 5][i, j] + f[l, 1 + 7][i, j]; } } /// C12 for (int i = 0; i < h; i++) // rows { for (int j = h; j < size; j++) // cols { C[i, j] = f[l, 1 + 3][i, j - h] + f[l, 1 + 5][i, j - h]; } } /// C21 for (int i = h; i < size; i++) // rows { for (int j = 0; j < h; j++) // cols { C[i, j] = f[l, 1 + 2][i - h, j] + f[l, 1 + 4][i - h, j]; } } /// C22 for (int i = h; i < size; i++) // rows { for (int j = h; j < size; j++) // cols { C[i, j] = f[l, 1 + 1][i - h, j - h] - f[l, 1 + 2][i - h, j - h] + f[l, 1 + 3][i - h, j - h] + f[l, 1 + 6][i - h, j - h]; } } }
// function for square matrix 2^N x 2^N private static void StrassenMultiplyRun(Matrix a, Matrix b, Matrix c, int l, Matrix[,] f) { // A * B into C, level of recursion, matrix field var size = a.Rows; var h = size / 2; if (size < 32) { for (var i = 0; i < c.Rows; i++) { for (var j = 0; j < c.Columns; j++) { c[i, j] = 0; for (var k = 0; k < a.Columns; k++) { c[i, j] += a[i, k] * b[k, j]; } } } return; } // (A11 + A22) * (B11 + B22); AplusBintoC(a, 0, 0, a, h, h, f[l, 0], h); AplusBintoC(b, 0, 0, b, h, h, f[l, 1], h); StrassenMultiplyRun(f[l, 0], f[l, 1], f[l, 1 + 1], l + 1, f); // (A21 + A22) * B11; AplusBintoC(a, 0, h, a, h, h, f[l, 0], h); ACopytoC(b, 0, 0, f[l, 1], h); StrassenMultiplyRun(f[l, 0], f[l, 1], f[l, 1 + 2], l + 1, f); //A11 * (B12 - B22); ACopytoC(a, 0, 0, f[l, 0], h); AminusBintoC(b, h, 0, b, h, h, f[l, 1], h); StrassenMultiplyRun(f[l, 0], f[l, 1], f[l, 1 + 3], l + 1, f); //A22 * (B21 - B11); ACopytoC(a, h, h, f[l, 0], h); AminusBintoC(b, 0, h, b, 0, 0, f[l, 1], h); StrassenMultiplyRun(f[l, 0], f[l, 1], f[l, 1 + 4], l + 1, f); //(A11 + A12) * B22; AplusBintoC(a, 0, 0, a, h, 0, f[l, 0], h); ACopytoC(b, h, h, f[l, 1], h); StrassenMultiplyRun(f[l, 0], f[l, 1], f[l, 1 + 5], l + 1, f); //(A21 - A11) * (B11 + B12); AminusBintoC(a, 0, h, a, 0, 0, f[l, 0], h); AplusBintoC(b, 0, 0, b, h, 0, f[l, 1], h); StrassenMultiplyRun(f[l, 0], f[l, 1], f[l, 1 + 6], l + 1, f); // (A12 - A22) * (B21 + B22); AminusBintoC(a, h, 0, a, h, h, f[l, 0], h); AplusBintoC(b, 0, h, b, h, h, f[l, 1], h); StrassenMultiplyRun(f[l, 0], f[l, 1], f[l, 1 + 7], l + 1, f); // C11 for (var i = 0; i < h; i++) { for (var j = 0; j < h; j++) { c[i, j] = f[l, 1 + 1][i, j] + f[l, 1 + 4][i, j] - f[l, 1 + 5][i, j] + f[l, 1 + 7][i, j]; } } // C12 for (var i = 0; i < h; i++) { for (var j = h; j < size; j++) { c[i, j] = f[l, 1 + 3][i, j - h] + f[l, 1 + 5][i, j - h]; } } // C21 for (var i = h; i < size; i++) { for (var j = 0; j < h; j++) { c[i, j] = f[l, 1 + 2][i - h, j] + f[l, 1 + 4][i - h, j]; } } // C22 for (var i = h; i < size; i++) { for (var j = h; j < size; j++) { c[i, j] = f[l, 1 + 1][i - h, j - h] - f[l, 1 + 2][i - h, j - h] + f[l, 1 + 3][i - h, j - h] + f[l, 1 + 6][i - h, j - h]; } } }
// Allows the game to perform any initialization it needs to before starting to run. // This is where it can query for any required services and load any non-graphic // related content. Calling base.Initialize will enumerate through any components // and initialize them as well. protected override void Initialize() { // Add your initialization logic here // Create the arrays representing the playing field // A PlayfieldWidth by PlayfieldWidth array representing the walls. // A 0 in the array will mean there is no wall, a 1 will mean there is a wall. Walls = new bool[PlayfieldWidth, PlayfieldWidth]; // An array of transformation matrices that can be used to move an object to the center of the corresponding tile. WallMatrices = new Matrix[PlayfieldWidth, PlayfieldWidth]; //Initialize the walls array, so that it is empty at the start //Also initialize our WallMatrices array, so it contains wall positions used for drawing them for (int y = 0; y < PlayfieldWidth; y = y + 1) { for (int x = 0; x < PlayfieldWidth; x = x + 1) { // initialize to 0, meaning empty Walls[x, y] = false; //Create a transformation matrix that places the object in the right spot for the tile. //This means a translation to the center of the tile. WallMatrices[x, y] = Matrix.CreateTranslation(new Vector3((x+0.5f) * TileWidth, (y+0.5f) * TileWidth, 0.0f)); } } // place a box in two corners, to give an ide of the orientation of the field Walls[0, 0] = true; Walls[PlayfieldWidth-2, 0] = true; //Create a transformation matrix for the ground plane. //Compute the scale for the playing field: number of tiles times the width of a tile. float Scale = (float) PlayfieldWidth * TileWidth; //First do a scaling to make the ground plane the right size, then a translation to the center of the playing field. //Note that matrix multiplication has the effect of performing the operations in sequence, from left to right, in this case, //first scale, then translate. PlaneMatrix = Matrix.CreateScale(Scale) * Matrix.CreateTranslation(new Vector3(Scale / 2, Scale / 2, 0)); //Create the camera that will be looking onto the scene, using the Camera class that is defined in file Camera.cs //The first vector is the location of the camera, above the far corner of the playing field //the second vector contains the angle the camera looks at in the vertical plane and in the horizontal plane //look 20 degrees down and 135 degrees from the positive y axis. //Note that the angles in degrees need to be converted to angels in radians (by dividing by 180 and multiplying with Pi). camera = new Camera( //new Vector3((float)PlayfieldWidth * TileWidth, (float)PlayfieldWidth * TileWidth, 100.0f), new Vector3(400.0f, 0.0f, 240.0f), new Vector3(120.0f, 120.0f, 0.0f) ); // Allow the mouse pointer to be visible in the game window. IsMouseVisible = true; //Set the grid size and tile size in class MovableObject //The class MovableObject is defined in file MovableObject.cs MovableObject.Initalize(24, (int)TileWidth); //Create a movable object for the Admoveo character, positioned in the center of tile (1,1) Character = new MovableObject(new Vector2(0.5f * TileWidth, 0.5f * TileWidth)); //Call inherited initialization //Do not change this one, or everything will break! base.Initialize(); }
// function for square matrix 2^N x 2^N // A * B into C private static void StrassenMultiplyRun(Matrix A, Matrix B, Matrix C, int l, Matrix[,] tempMatrix) { int aSize = A.rows; int split = aSize / 2; if (aSize < 32) { for (int i = 0; i < C.rows; i++) { for (int j = 0; j < C.columns; j++) { C[i, j] = 0; for (int k = 0; k < A.columns; k++) { C[i, j] += A[i, k] * B[k, j]; } } } return; } AplusBintoC(A, 0, 0, A, split, split, tempMatrix[l, 0], split); AplusBintoC(B, 0, 0, B, split, split, tempMatrix[l, 1], split); StrassenMultiplyRun(tempMatrix[l, 0], tempMatrix[l, 1], tempMatrix[l, 1 + 1], l + 1, tempMatrix); // (A11 + A22) * (B11 + B22); AplusBintoC(A, 0, split, A, split, split, tempMatrix[l, 0], split); ACopytoC(B, 0, 0, tempMatrix[l, 1], split); StrassenMultiplyRun(tempMatrix[l, 0], tempMatrix[l, 1], tempMatrix[l, 1 + 2], l + 1, tempMatrix); // (A21 + A22) * B11; ACopytoC(A, 0, 0, tempMatrix[l, 0], split); AminusBintoC(B, split, 0, B, split, split, tempMatrix[l, 1], split); StrassenMultiplyRun(tempMatrix[l, 0], tempMatrix[l, 1], tempMatrix[l, 1 + 3], l + 1, tempMatrix); //A11 * (B12 - B22); ACopytoC(A, split, split, tempMatrix[l, 0], split); AminusBintoC(B, 0, split, B, 0, 0, tempMatrix[l, 1], split); StrassenMultiplyRun(tempMatrix[l, 0], tempMatrix[l, 1], tempMatrix[l, 1 + 4], l + 1, tempMatrix); //A22 * (B21 - B11); AplusBintoC(A, 0, 0, A, split, 0, tempMatrix[l, 0], split); ACopytoC(B, split, split, tempMatrix[l, 1], split); StrassenMultiplyRun(tempMatrix[l, 0], tempMatrix[l, 1], tempMatrix[l, 1 + 5], l + 1, tempMatrix); //(A11 + A12) * B22; AminusBintoC(A, 0, split, A, 0, 0, tempMatrix[l, 0], split); AplusBintoC(B, 0, 0, B, split, 0, tempMatrix[l, 1], split); StrassenMultiplyRun(tempMatrix[l, 0], tempMatrix[l, 1], tempMatrix[l, 1 + 6], l + 1, tempMatrix); //(A21 - A11) * (B11 + B12); AminusBintoC(A, split, 0, A, split, split, tempMatrix[l, 0], split); AplusBintoC(B, 0, split, B, split, split, tempMatrix[l, 1], split); StrassenMultiplyRun(tempMatrix[l, 0], tempMatrix[l, 1], tempMatrix[l, 1 + 7], l + 1, tempMatrix); // (A12 - A22) * (B21 + B22); /// C11 for (int i = 0; i < split; i++) // rows { for (int j = 0; j < split; j++) // cols { C[i, j] = tempMatrix[l, 1 + 1][i, j] + tempMatrix[l, 1 + 4][i, j] - tempMatrix[l, 1 + 5][i, j] + tempMatrix[l, 1 + 7][i, j]; } } /// C12 for (int i = 0; i < split; i++) // rows { for (int j = split; j < aSize; j++) // cols { C[i, j] = tempMatrix[l, 1 + 3][i, j - split] + tempMatrix[l, 1 + 5][i, j - split]; } } /// C21 for (int i = split; i < aSize; i++) // rows { for (int j = 0; j < split; j++) // cols { C[i, j] = tempMatrix[l, 1 + 2][i - split, j] + tempMatrix[l, 1 + 4][i - split, j]; } } /// C22 for (int i = split; i < aSize; i++) // rows { for (int j = split; j < aSize; j++) // cols { C[i, j] = tempMatrix[l, 1 + 1][i - split, j - split] - tempMatrix[l, 1 + 2][i - split, j - split] + tempMatrix[l, 1 + 3][i - split, j - split] + tempMatrix[l, 1 + 6][i - split, j - split]; } } }
/// <summary> ///Creates a square matrix with the specified number of rows and columns ///the constructor that is called when you say new MatricesMatrix(numberOfRows, numberOfColumns) /// </summary> public MatricesMatrix(int numRowsAndColumns) { _matrices = new Matrix[numRowsAndColumns, numRowsAndColumns]; }
private static void BuildRotations() { rotations = new Matrix[6, 4]; float threePiOverTwo = (MathHelper.Pi * 3f) / 2f; rotations[0, 0] = Matrix.Identity; rotations[0, 1] = Matrix.CreateRotationX(MathHelper.PiOver2); rotations[0, 2] = Matrix.CreateRotationX(MathHelper.Pi); rotations[0, 3] = Matrix.CreateRotationX(threePiOverTwo); rotations[1, 0] = Matrix.CreateRotationY(-MathHelper.PiOver2); rotations[1, 1] = Matrix.CreateRotationY(-MathHelper.PiOver2) * Matrix.CreateRotationZ(MathHelper.PiOver2); rotations[1, 2] = Matrix.CreateRotationY(-MathHelper.PiOver2) * Matrix.CreateRotationZ(MathHelper.Pi); rotations[1, 3] = Matrix.CreateRotationY(-MathHelper.PiOver2) * Matrix.CreateRotationZ(threePiOverTwo); rotations[2, 0] = Matrix.CreateRotationY(-MathHelper.Pi); rotations[2, 1] = Matrix.CreateRotationY(-MathHelper.Pi) * Matrix.CreateRotationX(-MathHelper.PiOver2); rotations[2, 2] = Matrix.CreateRotationY(-MathHelper.Pi) * Matrix.CreateRotationX(-MathHelper.Pi); rotations[2, 3] = Matrix.CreateRotationY(-MathHelper.Pi) * Matrix.CreateRotationX(-threePiOverTwo); rotations[3, 0] = Matrix.CreateRotationY(-threePiOverTwo); rotations[3, 1] = Matrix.CreateRotationY(-threePiOverTwo) * Matrix.CreateRotationZ(-MathHelper.PiOver2); rotations[3, 2] = Matrix.CreateRotationY(-threePiOverTwo) * Matrix.CreateRotationZ(-MathHelper.Pi); rotations[3, 3] = Matrix.CreateRotationY(-threePiOverTwo) * Matrix.CreateRotationZ(-threePiOverTwo); rotations[4, 0] = Matrix.CreateRotationZ(MathHelper.PiOver2); rotations[4, 1] = Matrix.CreateRotationZ(MathHelper.PiOver2) * Matrix.CreateRotationY(MathHelper.PiOver2); rotations[4, 2] = Matrix.CreateRotationZ(MathHelper.PiOver2) * Matrix.CreateRotationY(MathHelper.Pi); rotations[4, 3] = Matrix.CreateRotationZ(MathHelper.PiOver2) * Matrix.CreateRotationY(threePiOverTwo); rotations[5, 0] = Matrix.CreateRotationZ(-MathHelper.PiOver2); rotations[5, 1] = Matrix.CreateRotationZ(-MathHelper.PiOver2) * Matrix.CreateRotationY(MathHelper.PiOver2); rotations[5, 2] = Matrix.CreateRotationZ(-MathHelper.PiOver2) * Matrix.CreateRotationY(MathHelper.Pi); rotations[5, 3] = Matrix.CreateRotationZ(-MathHelper.PiOver2) * Matrix.CreateRotationY(threePiOverTwo); inverseRotations = new Matrix[6, 4]; for (int i = 0; i < 6; i++) { for (int j = 0; j < 4; j++) { inverseRotations[i, j] = Matrix.Invert(rotations[i, j]); } } }
public MatricesMatrix(int numRows, int numCols) { _matrices = new Matrix[numRows, numCols]; }
// function for square matrix 2^N x 2^N private static void StrassenMultiplyRun(Matrix _a, Matrix _b, Matrix _c, int _l, Matrix[,] _f) // _a * _b into _c, level of recursion, matrix field { int size = _a.rows; int h = size / 2; if (size < 32) { for (int i = 0; i < _c.rows; i++) { for (int j = 0; j < _c.cols; j++) { _c[i, j] = 0; for (int k = 0; k < _a.cols; k++) { _c[i, j] += _a[i, k] * _b[k, j]; } } } return; } AplusBintoC(_a, 0, 0, _a, h, h, _f[_l, 0], h); AplusBintoC(_b, 0, 0, _b, h, h, _f[_l, 1], h); StrassenMultiplyRun(_f[_l, 0], _f[_l, 1], _f[_l, 1 + 1], _l + 1, _f); // (A11 + A22) * (B11 + B22); AplusBintoC(_a, 0, h, _a, h, h, _f[_l, 0], h); ACopytoC(_b, 0, 0, _f[_l, 1], h); StrassenMultiplyRun(_f[_l, 0], _f[_l, 1], _f[_l, 1 + 2], _l + 1, _f); // (A21 + A22) * B11; ACopytoC(_a, 0, 0, _f[_l, 0], h); AminusBintoC(_b, h, 0, _b, h, h, _f[_l, 1], h); StrassenMultiplyRun(_f[_l, 0], _f[_l, 1], _f[_l, 1 + 3], _l + 1, _f); //A11 * (B12 - B22); ACopytoC(_a, h, h, _f[_l, 0], h); AminusBintoC(_b, 0, h, _b, 0, 0, _f[_l, 1], h); StrassenMultiplyRun(_f[_l, 0], _f[_l, 1], _f[_l, 1 + 4], _l + 1, _f); //A22 * (B21 - B11); AplusBintoC(_a, 0, 0, _a, h, 0, _f[_l, 0], h); ACopytoC(_b, h, h, _f[_l, 1], h); StrassenMultiplyRun(_f[_l, 0], _f[_l, 1], _f[_l, 1 + 5], _l + 1, _f); //(A11 + A12) * B22; AminusBintoC(_a, 0, h, _a, 0, 0, _f[_l, 0], h); AplusBintoC(_b, 0, 0, _b, h, 0, _f[_l, 1], h); StrassenMultiplyRun(_f[_l, 0], _f[_l, 1], _f[_l, 1 + 6], _l + 1, _f); //(A21 - A11) * (B11 + B12); AminusBintoC(_a, h, 0, _a, h, h, _f[_l, 0], h); AplusBintoC(_b, 0, h, _b, h, h, _f[_l, 1], h); StrassenMultiplyRun(_f[_l, 0], _f[_l, 1], _f[_l, 1 + 7], _l + 1, _f); // (A12 - A22) * (B21 + B22); /// C11 for (int i = 0; i < h; i++) // rows { for (int j = 0; j < h; j++) // cols { _c[i, j] = _f[_l, 1 + 1][i, j] + _f[_l, 1 + 4][i, j] - _f[_l, 1 + 5][i, j] + _f[_l, 1 + 7][i, j]; } } /// C12 for (int i = 0; i < h; i++) // rows { for (int j = h; j < size; j++) // cols { _c[i, j] = _f[_l, 1 + 3][i, j - h] + _f[_l, 1 + 5][i, j - h]; } } /// C21 for (int i = h; i < size; i++) // rows { for (int j = 0; j < h; j++) // cols { _c[i, j] = _f[_l, 1 + 2][i - h, j] + _f[_l, 1 + 4][i - h, j]; } } /// C22 for (int i = h; i < size; i++) // rows { for (int j = h; j < size; j++) // cols { _c[i, j] = _f[_l, 1 + 1][i - h, j - h] - _f[_l, 1 + 2][i - h, j - h] + _f[_l, 1 + 3][i - h, j - h] + _f[_l, 1 + 6][i - h, j - h]; } } }