本文整理汇总了C#中Matrix.Determinant方法的典型用法代码示例。如果您正苦于以下问题:C# Matrix.Determinant方法的具体用法?C# Matrix.Determinant怎么用?C# Matrix.Determinant使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Matrix
的用法示例。
在下文中一共展示了Matrix.Determinant方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: Main
// An example main, showcasing some of the features
static void Main(string[] args)
{
Matrix A = new Matrix(new double[,] { { 77, 2, 3 }, { 5, -6, 7 }, { 9, 10, 15 } });
Matrix B = new Matrix(new double[,] { { -1, 2, 0 }, { 1, -2, -1 }, { 1, 0, 3 } });
Console.WriteLine("A:\n" + A);
Console.WriteLine("A Trasposed:\n" + ~A);
Console.WriteLine("A Minor(1,1):\n" + A.Minor(1, 1));
Console.WriteLine("B:\n" + B);
Console.WriteLine("B Determinant: " + B.Determinant() + "\n");
Console.WriteLine("B Inverse: \n" + !B);
Console.WriteLine("A+B:\n" + (A + B));
Console.WriteLine("B*A:\n" + B * A);
Vector V1 = new Vector(new double[] { 1, 2, 3 });
Vector V2 = new Vector(new double[] { 4, -2, -1 });
Console.WriteLine("V1: " + V1);
Console.WriteLine("V2: " + V2 + "\n");
Console.WriteLine("V1 + V2: " + (V1 + V2) + "\n");
Console.WriteLine("V1xV2: " + (V1 ^ V2) + "\n");
}
示例2: Determinant
public void Determinant()
{
Matrix m = new Matrix(1, 2, 3, 4, 5, 6, 7, 8, 9, 8, 7, 6, 5, 4, 3, 2);
Assert.AreEqual(0, m.Determinant(), "#1");
m = new Matrix(1, 2, 3, 2.665f, 3, 2, 31.43234f, 3, 6, 4, 2, 6, 3, 6, 532, 3);
//Assert.AreEqual(TestHelper.Approximate(-1216.07629f), TestHelper.Approximate(m.Determinant()), "#2");
Assert.AreEqual(-1216.07629f, m.Determinant(), "#2");
}
示例3: Render
/// <summary>
/// Render
/// </summary>
/// <param name="renderMatrix">Render matrix</param>
public void Render(Matrix renderMatrix)
{
// Optimization to skip smaller objects, which are very far away!
// Display 1 meter big objects only if in a distance of 250 meters!
// Scaling is guessed by the length of the first vector in our matrix,
// because we always use the same scaling for x, y, z this should be
// correct!
float maxDistance = maxViewDistance * scaling;
float distanceSquared = Vector3.DistanceSquared(
BaseGame.CameraPos, renderMatrix.Translation);
if (distanceSquared > maxDistance * maxDistance)
// Don't render, too far away!
return;
// Check out if object is behind us or not visible, then we can skip
// rendering. This is the GREATEST performance gain in the whole game!
// Object must be away at least 20 units!
if (distanceSquared > 20 * 20 &&
// And the object size must be small
distanceSquared > (10 * scaling) * (10 * scaling))
{
Vector3 objectDirection =
Vector3.Normalize(BaseGame.CameraPos - renderMatrix.Translation);
// Half field of view should be fov / 2, but because of
// the aspect ratio (1.33) and an additional offset we need
// to include to see objects at the borders.
float objAngle = Vector3Helper.GetAngleBetweenVectors(
BaseGame.CameraRotation, objectDirection);
if (objAngle > BaseGame.ViewableFieldOfView)
// Skip.
return;
} // if (distanceSquared)
// Multiply object matrix by render matrix, result is used multiple
// times here.
renderMatrix = objectMatrix * renderMatrix;
// Go through all meshes in the model
for (int meshNum = 0; meshNum < xnaModel.Meshes.Count; meshNum++)
{
ModelMesh mesh = xnaModel.Meshes[meshNum];
// Assign world matrix
Matrix worldMatrix =
transforms[mesh.ParentBone.Index] *
renderMatrix;
// Got animation?
if (animatedMesh == mesh)
{
worldMatrix =
Matrix.CreateRotationZ(
// Use pseudo number for this object for different rotations
renderMatrix.Translation.Length() * 3 +
renderMatrix.Determinant() * 5 +
(1.0f+((int)(renderMatrix.M42 * 33.3f)%100)*0.00123f)*
BaseGame.TotalTime / 0.654f) *
transforms[mesh.ParentBone.Index] *
renderMatrix;
} // if (animatedMesh)
// Just add this world matrix to our render matrices for each part.
for (int partNum = 0; partNum < mesh.MeshParts.Count; partNum++)
{
// Find mesh part in the renderableMeshes dictionary and add the
// new render matrix to be picked up in the mesh rendering later.
renderableMeshes[mesh.MeshParts[partNum]].renderMatrices.Add(
worldMatrix);
} // for (partNum)
} // foreach (mesh)
}
示例4: MethodKramera
public static double[] MethodKramera(Matrix a)
{
double[] x = new double[a.GetLength(1) - 1];
double[] b = new double[a.GetLength(0)];
Matrix c = new Matrix(a);
for (int i = 0; i < b.Length; ++i)
b[i] = c[i, c.GetLength(0)];
c.DeleteStolb(c.GetLength(1));
double d = c.Determinant();
for (int i = 0; i < x.Length; ++i)
{
Matrix f = new Matrix(a);
f.DeleteStolb(f.GetLength(1));
f.ChangeStolb(b, i);
double d1 = f.Determinant();
x[i] = d1 / d;
}
return x;
}
示例5: MatrixLUDecomposition2
public void MatrixLUDecomposition2()
{
/*
MATLAB:
mc2x2 = [1 2;3 4]
[L_mc, U_mc, P_mc] = lu(mc2x2)
P_mcv = P_mc * [0:1:length(P_mc)-1]'
det(mc2x2)
[L_mch, U_mch, P_mch] = lu(mc2x2')
P_mchv = P_mch * [0:1:length(P_mch)-1]'
det(mc2x2')
*/
Converter<int, double> int2Double = delegate(int i) { return i; };
Matrix mc2X2 = new Matrix(new double[][]
{
new double[] { 1, 2 },
new double[] { 3, 4 }
});
LUDecomposition mcLU = mc2X2.LUDecomposition;
Matrix mcL = new Matrix(new double[][] {
new double[] { 1, 0 },
new double[] { 1d/3d, 1 }
});
Matrix mcU = new Matrix(new double[][] {
new double[] { 3, 4 },
new double[] { 0, 2d/3d }
});
Matrix mcP = new Matrix(new double[][] {
new double[] { 0, 1 },
new double[] { 1, 0 }
});
Vector mcPv = new Vector(new double[] { 1, 0 });
Assert.That(mcLU.L, NumericIs.AlmostEqualTo(mcL), "real LU L-matrix");
Assert.That(mcLU.U, NumericIs.AlmostEqualTo(mcU), "real LU U-matrix");
Assert.That(mcLU.PermutationMatrix, NumericIs.AlmostEqualTo(mcP), "real LU permutation matrix");
Assert.That(mcLU.PivotVector, NumericIs.AlmostEqualTo(mcPv), "real LU pivot");
Assert.That((Vector)Array.ConvertAll(mcLU.Pivot, int2Double), NumericIs.AlmostEqualTo(mcPv), "real LU pivot II");
Assert.That(mcLU.Determinant(), NumericIs.AlmostEqualTo((double)(-2)), "real LU determinant");
Assert.That(mc2X2.Determinant(), NumericIs.AlmostEqualTo((double)(-2)), "real LU determinant II");
Assert.That(mcLU.IsNonSingular, "real LU non-singular");
Assert.That(mcLU.L * mcLU.U, NumericIs.AlmostEqualTo(mcLU.PermutationMatrix * mc2X2), "real LU product");
Matrix mc2X2H = Matrix.Transpose(mc2X2);
LUDecomposition mchLU = mc2X2H.LUDecomposition;
Matrix mchL = new Matrix(new double[][] {
new double[] { 1, 0 },
new double[] { 0.5, 1 }
});
Matrix mchU = new Matrix(new double[][] {
new double[] { 2, 4 },
new double[] { 0, 1 }
});
Matrix mchP = new Matrix(new double[][] {
new double[] { 0, 1 },
new double[] { 1, 0 }
});
Vector mchPv = new Vector(new double[] { 1, 0 });
Assert.That(mchLU.L, NumericIs.AlmostEqualTo(mchL), "real LU L-matrix (H)");
Assert.That(mchLU.U, NumericIs.AlmostEqualTo(mchU), "real LU U-matrix (H)");
Assert.That(mchLU.PermutationMatrix, NumericIs.AlmostEqualTo(mchP), "real LU permutation matrix (H)");
Assert.That(mchLU.PivotVector, NumericIs.AlmostEqualTo(mchPv), "real LU pivot (H)");
Assert.That((Vector)Array.ConvertAll(mchLU.Pivot, int2Double), NumericIs.AlmostEqualTo(mchPv), "real LU pivot II (H)");
Assert.That(mchLU.Determinant(), NumericIs.AlmostEqualTo((double)(-2)), "real LU determinant (H)");
Assert.That(mc2X2H.Determinant(), NumericIs.AlmostEqualTo((double)(-2)), "real LU determinant II (H)");
Assert.That(mchLU.IsNonSingular, "real LU non-singular (H)");
Assert.That(mchLU.L * mchLU.U, NumericIs.AlmostEqualTo(mchLU.PermutationMatrix * mc2X2H), "real LU product (H)");
}
示例6: propertyA3
public void propertyA3()
{
Matrix matrixA = new Matrix(new double[,]{
{1,-3,-2,7,9,1},
{0,4,-1,4,55,7},
{2,1,0,11,33,44},
{3,4,5,6,7,8},
{1,2,3,98,65,3},
{0,0,33,2,45,67}}, 42);
double det = matrixA.Determinant();
Matrix transpose = matrixA.Transpose();
double detT = transpose.Determinant();
Assert.AreEqual(det, detT);
}
示例7: propertyA1
public void propertyA1()
{
Matrix matrixA = new Matrix(new double[,]{
{1,1},
{2,3}}, 10);
double det = matrixA.Determinant();
Matrix transpose = matrixA.Transpose();
double detT = transpose.Determinant();
Assert.AreEqual(det, detT);
}
示例8: DeterminantTest
public void DeterminantTest()
{
double[,] targetArray = {
{1,-3,-2},
{0,4,-1},
{2,1,0}};
Matrix target = new Matrix(targetArray);
double actual = target.Determinant();
Assert.AreEqual(23, actual);
}
示例9: SingleTest
private int SingleTest(Matrix<double> matrix1, Matrix<double> matrix2, double[] means1, double[] means2, double[] vector)
{
var matrix_means1 = Matrix<double>.Build.DenseOfColumnArrays(means1);
var matrix_means2 = Matrix<double>.Build.DenseOfColumnArrays(means2);
var matrix_vector = Matrix<double>.Build.DenseOfColumnArrays(vector);
// Calculate A B C
var A = matrix2.Inverse() - matrix1.Inverse();
var B = 2 * (matrix_means1.Transpose() * matrix1.Inverse()) - matrix_means2.Transpose() * matrix2.Inverse();
var C = (matrix_means2.Transpose() * matrix2.Inverse() * matrix_means2).Determinant() - (matrix_means1.Transpose() * matrix1.Inverse() * matrix_means1).Determinant() - 2 * (Math.Log10(matrix1.Determinant() / matrix2.Determinant()));
var result = (matrix_vector.Transpose() * A * matrix_vector).Determinant() + B[0, 0] * matrix_vector[0 ,0] + B[0, 1] * matrix_vector[1, 0] + B[0, 2] * matrix_vector[2, 0] + C;
if (result > 0)
{
return 1;
}
return 2;
}
示例10: Cofactor
/// <summary>
/// Returns the cofactor value of an element of the matrix
/// </summary>
/// <param name="x"></param>
/// <param name="y"></param>
/// <returns></returns>
public double Cofactor(int x, int y)
{
Matrix temp = new Matrix(Width - 1, Height - 1);
int cX = -1;
int cY = 0;
for (int i = 0; i < Width; i++)
{
if (i == x)
continue;
cX++;
cY = 0;
for (int j = 0; j < Height; j++)
{
if (j == y)
continue;
temp[cX, cY] = field[i, j];
cY++;
}
}
return temp.Determinant();
}
示例11: Density
/// <summary>
/// Evaluates the probability density function for the Wishart distribution.
/// </summary>
/// <param name="x">The matrix at which to evaluate the density at.</param>
/// <exception cref="ArgumentOutOfRangeException">If the argument does not have the same dimensions as the scale matrix.</exception>
/// <returns>the density at <paramref name="x"/>.</returns>
public double Density(Matrix<double> x)
{
var p = _scale.RowCount;
if (x.RowCount != p || x.ColumnCount != p)
{
throw Matrix.DimensionsDontMatch<ArgumentOutOfRangeException>(x, _scale, "x");
}
var dX = x.Determinant();
var siX = _chol.Solve(x);
// Compute the multivariate Gamma function.
var gp = Math.Pow(Constants.Pi, p*(p - 1.0)/4.0);
for (var j = 1; j <= p; j++)
{
gp *= SpecialFunctions.Gamma((_degreesOfFreedom + 1.0 - j)/2.0);
}
return Math.Pow(dX, (_degreesOfFreedom - p - 1.0)/2.0)
*Math.Exp(-0.5*siX.Trace())
/Math.Pow(2.0, _degreesOfFreedom*p/2.0)
/Math.Pow(_chol.Determinant, _degreesOfFreedom/2.0)
/gp;
}
示例12: DeterminantExample
public void DeterminantExample()
{
// Set up a sample matrix
var matrix = new Matrix(3, 3);
// [ 3, 1, 8 ]
// [ 2, -5, 4 ]
// [-1, 6, -2 ]
// Determinant = 14
matrix[0, 0] = 3;
matrix[0, 1] = 1;
matrix[0, 2] = 8;
matrix[1, 0] = 2;
matrix[1, 1] = -5;
matrix[1, 2] = 4;
matrix[2, 0] = -1;
matrix[2, 1] = 6;
matrix[2, 2] = -2;
// Calculate the determinant - the answer is 14.
Assert.AreEqual(matrix.Determinant(), 14, 0.000000001);
}
示例13: Determinant
/// <summary>
///
/// </summary>
/// <returns></returns>
/// <exception cref="InvalidOperationException"></exception>
public double Determinant()
{
//make sure current matrix is a square matrix
var colNum = _matrixContent[0].Length;
if (_matrixContent.Length != colNum)
throw new InvalidOperationException("A square matrix is required to calculate determinant");
double detNum = 0;
if (colNum == 2)
detNum = this[0][0] * this[1][1] - this[0][1] * this[1][0];
else
{
for (var i = 0; i < colNum; i++)
{
var x = new Matrix(this, 0, i);
detNum += (this[0][i]) * x.Determinant() * (int)Math.Pow(-1, i);
}
}
return detNum;
}
示例14: Cofactor
/// <summary>
/// </summary>
/// <returns></returns>
/// <exception cref="Exception"></exception>
/// <exception cref="InvalidOperationException"></exception>
public Matrix Cofactor()
{
if (Length != this[0].Length)
throw new Exception("A square matrix is required to calculate the cofactor");
var cf = new Matrix(Length, Length);
for (var i = 0; i < Length; i++)
{
for (var j = 0; j < Length; j++)
{
var minor = new Matrix(this, i, j);
cf[i, j] = minor.Determinant()*(int) Math.Pow(-1, i + j)*this[i, j];
}
}
return cf;
}
示例15: QuadraticSingleTest
private int QuadraticSingleTest(Matrix<double> matrix1, Matrix<double> matrix2, double[] means1, double[] means2, double[] vector)
{
var matrix_means1 = Matrix<double>.Build.DenseOfColumnArrays(means1);
var matrix_means2 = Matrix<double>.Build.DenseOfColumnArrays(means2);
var matrix_vector = Matrix<double>.Build.DenseOfColumnArrays(vector);
// Calculate A B C
var A = matrix2.Inverse() - matrix1.Inverse();
var B = 2 * (matrix_means1.Transpose() * matrix1.Inverse()) - matrix_means2.Transpose() * matrix2.Inverse();
var C = (matrix_means2.Transpose() * matrix2.Inverse() * matrix_means2).Determinant() - (matrix_means1.Transpose() * matrix1.Inverse() * matrix_means1).Determinant() - 2 * (Math.Log10(matrix1.Determinant() / matrix2.Determinant()));
var result = (matrix_vector.Transpose() * A * matrix_vector).Determinant() + C;
for (int i = 0; i < means1.Length; i++)
{
result += B[0, i] * matrix_vector[i, 0];
}
if (result > 0)
{
return 1;
}
return 2;
}