Introduction to Matrix algorithm

Source: Internet
Author: User

Summary

This paper introduces the programming representation of Matrix, elementary transformation of matrix, the step matrix and the determinant of square.

Matrix Introduction

The matrix is an MXN plane table, with a two-D array can be expressed, for the convenience of input, we use a special format of the string to initialize the matrix, with "|" Split each row, "," split each column, and add a show method to print out the matrix, in order to test and debug the need to rewrite the ToString () method.

Code

public class Matrix {
public int M {get; private set;}
public int N {get; private set;}
Private readonly double[,] num = null;
#region Constructor/show
Public Matrix (int m, int n, string input) {
m = m;
n = n;
num = new double[m, n];
if (!string. IsNullOrEmpty (input))
Parseinput (input);
}
private void Parseinput (string input) {
string[] rows = input. Split (new[] {' | '});
If rows. Length!= M)
throw new ArgumentException ("Row count err");
for (int i = 0; i < M; i++) {
String row = Rows[i];
String[] cells = row. Split (new[] {', '});
if (cells. Length!= N)
throw new ArgumentException (string. Format ("Cells Counte err:{0}", row));
for (int j = 0; J < N; J + +) {
int cellvalue;
if (!int. TryParse (Cells[j], out Cellvalue))
throw new ArgumentException (string. Format ("Cell error:{0}", Cells[j]);
Num[i, j] = Cellvalue;
}
}
}
public void Show () {
for (int i = 0; i < M; i++) {
for (int j = 0; J < N; + +)
Console.Write ("{0}\t", Num[i, j]);
Console.WriteLine ();
}
}

public override string ToString () {
StringBuilder sb = new StringBuilder ();
for (int i = 0; i < M; i++) {
for (int j = 0; J < N; J + +) {
Sb. Append (Num[i, j]);
if (J!= N-1)
Sb. Append (', ');
}
if (i!= M-1)
Sb. Append (' | ');
}
Return SB. ToString ();
}
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.