本文整理汇总了C#中Matrix.IsSquare方法的典型用法代码示例。如果您正苦于以下问题:C# Matrix.IsSquare方法的具体用法?C# Matrix.IsSquare怎么用?C# Matrix.IsSquare使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Matrix
的用法示例。
在下文中一共展示了Matrix.IsSquare方法的8个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: LinearSolve
/// <summary>
/// Solves a linear system through inversing a matrix
/// </summary>
/// <param name="paramM"></param>
/// <param name="yV"></param>
/// <returns></returns>
public Vector LinearSolve(Matrix paramM, Vector yV)
{
if (!paramM.IsSquare() || paramM.Height != yV.Dim)
throw new Exception("Dim Error");
Vector xV = paramM.Inverse() * yV;
return xV;
}
示例2: Matrix_IsSquareFalseTest
public void Matrix_IsSquareFalseTest()
{
Matrix matrix1 = new Matrix(3, 2);
double[] matrix1Column1 = { 2, 3, 1 };
double[] matrix1Column2 = { 4, 5, 1 };
matrix1.SetColumn(0, matrix1Column1);
matrix1.SetColumn(1, matrix1Column2);
matrix1.IsSquare().Should().BeFalse();
}
示例3: Matrix_IsSquareZeroTest
public void Matrix_IsSquareZeroTest()
{
Matrix matrix1 = new Matrix(3, 3);
matrix1.IsSquare().Should().BeTrue();
}
示例4: Matrix_IsSquareTrueTest
public void Matrix_IsSquareTrueTest()
{
Matrix matrix1 = new Matrix(2, 2);
double[] matrix1Column1 = { 2, 3 };
double[] matrix1Column2 = { 4, 5 };
matrix1.SetColumn(0, matrix1Column1);
matrix1.SetColumn(1, matrix1Column2);
matrix1.IsSquare().Should().BeTrue();
}
示例5: Multiply
private static Matrix Multiply(Matrix m1, Matrix m2) // Matrix multiplication
{
if (m1.cols != m2.rows) throw new MException("Wrong dimension of matrix!");
int msize = Math.Max(Math.Max(m1.rows, m1.cols), Math.Max(m2.rows, m2.cols));
// stupid multiplication faster for small matrices
if (msize < 32)
{
return StupidMultiply(m1, m2);
}
// stupid multiplication faster for non square matrices
if (!m1.IsSquare() || !m2.IsSquare()) {
return StupidMultiply(m1, m2);
}
// Strassen multiplication is faster for large square matrix 2^N x 2^N
// NOTE because of previous checks msize == m1.cols == m1.rows == m2.cols == m2.cols
double exponent = Math.Log(msize) / Math.Log(2);
if (Math.Pow(2,exponent) == msize) {
return StrassenMultiply(m1, m2);
} else {
return StupidMultiply(m1, m2);
}
}
示例6: FloydPath
/// <summary>
/// Returns the shortest path between two given vertices i and j as
/// int array.
/// </summary>
/// <param name="P">Path matrix as returned from Floyd().</param>
/// <param name="i">One-based index of start vertex.</param>
/// <param name="j">One-based index of end vertex.</param>
/// <returns></returns>
public static ArrayList FloydPath(Matrix P, int i, int j)
{
if (!P.IsSquare())
throw new ArgumentException("Path matrix must be square.");
else if (!P.IsReal())
throw new ArgumentException("Adjacence matrices are expected to be real.");
ArrayList path = new ArrayList();
path.Add(i);
//int borderliner = 0;
//int n = P.Size()[0] + 1; // shortest path cannot have more than n vertices!
while (P[i, j] != 0)
{
i = Convert.ToInt32(P[i, j]);
path.Add(i);
//borderliner++;
//if (borderliner == n)
// throw new FormatException("P was not a Floyd path matrix.");
}
path.Add(j);
return path;
}
示例7: Floyd
/// <summary>
/// Computes all shortest distance between any vertices in a given graph.
/// </summary>
/// <param name="adjacence_matrix">Square adjacence matrix. The main diagonal
/// is expected to consist of zeros, any non-existing edges should be marked
/// positive infinity.</param>
/// <returns>Two matrices D and P, where D[u,v] holds the distance of the shortest
/// path between u and v, and P[u,v] holds the shortcut vertex on the way from
/// u to v.</returns>
public static Matrix[] Floyd(Matrix adjacence_matrix)
{
if (!adjacence_matrix.IsSquare())
throw new ArgumentException("Expected square matrix.");
else if (!adjacence_matrix.IsReal())
throw new ArgumentException("Adjacence matrices are expected to be real.");
int n = adjacence_matrix.RowCount;
Matrix D = adjacence_matrix.Clone(); // distance matrix
Matrix P = new Matrix(n);
double buf;
for (int k = 1; k <= n; k++)
for (int i = 1; i <= n; i++)
for (int j = 1; j <= n; j++)
{
buf = D[i, k].Re + D[k, j].Re;
if (buf < D[i, j].Re)
{
D[i, j].Re = buf;
P[i, j].Re = k;
}
}
return new Matrix[] { D, P };
}
示例8: DFS
/// <summary>
/// Performs depth-first search for a graph given by its adjacence matrix.
/// </summary>
/// <param name="adjacence_matrix">A[i,j] = 0 or +infty, if there is no edge from i to j; any non-zero value otherwise.</param>
/// <param name="root">The vertex to begin the search.</param>
/// <returns>Adjacence matrix of the computed spanning tree.</returns>
public static Matrix DFS(Matrix adjacence_matrix, int root)
{
if (!adjacence_matrix.IsSquare())
throw new ArgumentException("Adjacence matrices are expected to be square.");
else if (!adjacence_matrix.IsReal())
throw new ArgumentException("Adjacence matrices are expected to be real.");
int n = adjacence_matrix.RowCount;
if (root < 1 || root > n)
throw new ArgumentException("Root must be a vertex of the graph, e.i. in {1, ..., n}.");
Matrix spanTree = new Matrix(n);
bool[] marked = new bool[n + 1];
Stack todo = new Stack();
todo.Push(root);
marked[root] = true;
// adajacence lists for each vertex
ArrayList[] A = new ArrayList[n + 1];
for (int i = 1; i <= n; i++)
{
A[i] = new ArrayList();
for (int j = 1; j <= n; j++)
if (adjacence_matrix[i, j].Re != 0 && adjacence_matrix[i, j].Im != double.PositiveInfinity)
A[i].Add(j);
}
int v, w;
while (todo.Count > 0)
{
v = (int)todo.Peek();
if (A[v].Count > 0)
{
w = (int)A[v][0];
if (!marked[w])
{
marked[w] = true; // mark w
spanTree[v, w].Re = 1; // mark vw
todo.Push(w); // one more to search
}
A[v].RemoveAt(0);
}
else
todo.Pop();
}
return spanTree;
}