本文整理汇总了C#中DenseMatrix类的典型用法代码示例。如果您正苦于以下问题:C# DenseMatrix类的具体用法?C# DenseMatrix怎么用?C# DenseMatrix使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
DenseMatrix类属于命名空间,在下文中一共展示了DenseMatrix类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: Main
public static void Main(string[] args)
{
Matrix recipe = new DenseMatrix(recipe_data);
using (Model M = new Model("Recipe"))
{
// "production" defines the amount of each product to bake.
Variable production = M.Variable("production",
new StringSet(productnames),
Domain.GreaterThan(0.0));
// The objective is to maximize the total revenue.
M.Objective("revenue",
ObjectiveSense.Maximize,
Expr.Dot(revenue, production));
// The prodoction is constrained by stock:
M.Constraint(Expr.Mul(recipe, production), Domain.LessThan(stock));
M.SetLogHandler(Console.Out);
// We solve and fetch the solution:
M.Solve();
double[] res = production.Level();
Console.WriteLine("Solution:");
for (int i = 0; i < res.Length; ++i)
{
Console.WriteLine(" Number of {0} : {1}", productnames[i], res[i]);
}
Console.WriteLine(" Revenue : ${0}",
res[0] * revenue[0] + res[1] * revenue[1]);
}
}
示例2: Simulate
public DenseMatrix Simulate(DenseMatrix inputs) //rows: t, cols: inputcount
{
states = new Vector(states.Elements.Length);
DenseMatrix temp = inputs.MatrixMultiply(inputWeights);
DenseMatrix ret = new DenseMatrix(inputs.Rows, states.Elements.Length);
for (int t = 0; t < temp.Rows; ++t)
{
Vector v = temp.GetRow(t);
Vector states2 = innerConnections.MatrixMultiplyRight(states);
states2.Add(v);
states2.Add(biasWeights);
for (int i = 0; i < states2.Elements.Length; ++i)
{
states2.Elements[i] = (double)Math.Tanh(states2.Elements[i]);
}
states = states2;
for (int i = 0; i < states.Elements.Length; ++i)
{
ret[t, i] = states.Elements[i];
}
}
return ret;
}
示例3: Main
public static void Main(string[] args)
{
using (Model M = new Model("sdo1"))
{
// Setting up the variables
Variable X = M.Variable("X", Domain.InPSDCone(3));
Variable x = M.Variable("x", Domain.InQCone(3));
DenseMatrix C = new DenseMatrix ( new double[][] { new double[] {2,1,0}, new double[] {1,2,1}, new double[] {0,1,2}} );
DenseMatrix A1 = new DenseMatrix ( new double[][] { new double[] {1,0,0}, new double[] {0,1,0}, new double[] {0,0,1}} );
DenseMatrix A2 = new DenseMatrix ( new double[][] { new double[] {1,1,1}, new double[] {1,1,1}, new double[] {1,1,1}} );
// Objective
M.Objective(ObjectiveSense.Minimize, Expr.Add(Expr.Dot(C, X), x.Index(0)));
// Constraints
M.Constraint("c1", Expr.Add(Expr.Dot(A1, X), x.Index(0)), Domain.EqualsTo(1.0));
M.Constraint("c2", Expr.Add(Expr.Dot(A2, X), Expr.Sum(x.Slice(1,3))), Domain.EqualsTo(0.5));
M.Solve();
Console.WriteLine("[{0}]", (new Utils.StringBuffer()).A(X.Level()).ToString());
Console.WriteLine("[{0}]", (new Utils.StringBuffer()).A(x.Level()).ToString());
}
}
示例4: EstimatePolynomial
// Computes coefficients of real polynomial (or estimates if values are noised) using Svd
// Each row of matrix should contain [x, P(x)], at least rank+1 rows
// Supplied x-es best have magintude in range [1-2], so resulting coefficient matrix is well conditioned
public static Polynomial EstimatePolynomial(Matrix<float> values, int rank)
{
// 1) Create equation Xa = b
// | x1^n x1^n-1 ... x1 1 | | a0 | | P(x1) |
// | | | ...| = | |
// | xk^n xk^n-1 ... xk 1 | | an | | P(xk) |
Matrix<float> X = new DenseMatrix(values.RowCount, rank + 1);
Vector<float> P = new DenseVector(values.RowCount);
for(int i = 0; i < values.RowCount; ++i)
{
X[i, rank] = 1.0f;
for(int c = rank - 1; c >= 0; --c)
{
X[i, c] = X[i, c + 1] * values.At(i, 0);
}
P[i] = values[i, 1];
}
return new Polynomial()
{
Coefficents = SvdSolver.Solve(X, P),
Rank = rank
};
}
示例5: Main
public static void Main()
{
Matrix matrix = new DenseMatrix(5, 6);
for (int i = 0; i < matrix.Rows; i++)
{
for (int j = 0; j < matrix.Columns; j++)
{
matrix[i, j] = j;
}
}
//the enumerator returns a KeyValuePair, where the key is the column number
//and the value is the column as a Vector.
foreach (KeyValuePair<int, Vector> column in matrix.GetColumnEnumerator())
{
Console.WriteLine("Column: {0}", column.Key);
//the Vector enumerator also returns a KeyValuePair with the key
//being the element's position in the Vector (the row in this case)
//and the value being the element's value.
foreach (double element in column.Value)
{
Console.WriteLine(element);
}
}
}
示例6: Create
/// <summary>
/// Initializes a new instance of the <see cref="DenseQR"/> class. This object will compute the
/// QR factorization when the constructor is called and cache it's factorization.
/// </summary>
/// <param name="matrix">The matrix to factor.</param>
/// <param name="method">The QR factorization method to use.</param>
/// <exception cref="ArgumentNullException">If <paramref name="matrix"/> is <c>null</c>.</exception>
/// <exception cref="ArgumentException">If <paramref name="matrix"/> row count is less then column count</exception>
public static DenseQR Create(DenseMatrix matrix, QRMethod method = QRMethod.Full)
{
if (matrix.RowCount < matrix.ColumnCount)
{
throw Matrix.DimensionsDontMatch<ArgumentException>(matrix);
}
var tau = new float[Math.Min(matrix.RowCount, matrix.ColumnCount)];
Matrix<float> q;
Matrix<float> r;
if (method == QRMethod.Full)
{
r = matrix.Clone();
q = new DenseMatrix(matrix.RowCount);
Control.LinearAlgebraProvider.QRFactor(((DenseMatrix) r).Values, matrix.RowCount, matrix.ColumnCount, ((DenseMatrix) q).Values, tau);
}
else
{
q = matrix.Clone();
r = new DenseMatrix(matrix.ColumnCount);
Control.LinearAlgebraProvider.ThinQRFactor(((DenseMatrix) q).Values, matrix.RowCount, matrix.ColumnCount, ((DenseMatrix) r).Values, tau);
}
return new DenseQR(q, r, method, tau);
}
示例7: ValidateMatrixFactoryParse
public void ValidateMatrixFactoryParse()
{
denseMatObj = GetDenseMatrix();
MatrixFactory<String, String, Double> mfObj =
MatrixFactory<String, String, Double>.GetInstance();
ParallelOptions poObj = new ParallelOptions();
TryParseMatrixDelegate<string, string, double> a =
new TryParseMatrixDelegate<string, string, double>(this.TryParseMatrix);
mfObj.RegisterMatrixParser(a);
// Writes the text file
denseMatObj.WritePaddedDouble(Constants.FastQTempTxtFileName, poObj);
Matrix<string, string, double> newMatObj =
mfObj.Parse(Constants.FastQTempTxtFileName, double.NaN, poObj);
Assert.AreEqual(denseMatObj.RowCount, newMatObj.RowCount);
Assert.AreEqual(denseMatObj.RowKeys.Count, newMatObj.RowKeys.Count);
Assert.AreEqual(denseMatObj.ColCount, newMatObj.ColCount);
Assert.AreEqual(denseMatObj.ColKeys.Count, newMatObj.ColKeys.Count);
Assert.AreEqual(denseMatObj.Values.Count(), newMatObj.Values.Count());
ApplicationLog.WriteLine(
"MatrixFactory BVT : Successfully validated Parse() method");
}
示例8: MatrixFrom1DArrayIsReference
public void MatrixFrom1DArrayIsReference()
{
var data = new double[] { 1, 1, 1, 1, 1, 1, 2, 2, 2 };
var matrix = new DenseMatrix(3, 3, data);
matrix[0, 0] = 10.0;
Assert.AreEqual(10.0, data[0]);
}
示例9: Create
/// <summary>
/// Initializes a new instance of the <see cref="DenseEvd"/> class. This object will compute the
/// the eigenvalue decomposition when the constructor is called and cache it's decomposition.
/// </summary>
/// <param name="matrix">The matrix to factor.</param>
/// <param name="symmetricity">If it is known whether the matrix is symmetric or not the routine can skip checking it itself.</param>
/// <exception cref="ArgumentNullException">If <paramref name="matrix"/> is <c>null</c>.</exception>
/// <exception cref="ArgumentException">If EVD algorithm failed to converge with matrix <paramref name="matrix"/>.</exception>
public static DenseEvd Create(DenseMatrix matrix, Symmetricity symmetricity)
{
if (matrix.RowCount != matrix.ColumnCount)
{
throw new ArgumentException(Resources.ArgumentMatrixSquare);
}
var order = matrix.RowCount;
// Initialize matrices for eigenvalues and eigenvectors
var eigenVectors = new DenseMatrix(order);
var blockDiagonal = new DenseMatrix(order);
var eigenValues = new LinearAlgebra.Complex.DenseVector(order);
bool isSymmetric;
switch (symmetricity)
{
case Symmetricity.Symmetric:
case Symmetricity.Hermitian:
isSymmetric = true;
break;
case Symmetricity.Asymmetric:
isSymmetric = false;
break;
default:
isSymmetric = matrix.IsSymmetric();
break;
}
Control.LinearAlgebraProvider.EigenDecomp(isSymmetric, order, matrix.Values, eigenVectors.Values, eigenValues.Values, blockDiagonal.Values);
return new DenseEvd(eigenVectors, eigenValues, blockDiagonal, isSymmetric);
}
示例10: DenseQR
/// <summary>
/// Initializes a new instance of the <see cref="DenseQR"/> class. This object will compute the
/// QR factorization when the constructor is called and cache it's factorization.
/// </summary>
/// <param name="matrix">The matrix to factor.</param>
/// <param name="method">The QR factorization method to use.</param>
/// <exception cref="ArgumentNullException">If <paramref name="matrix"/> is <c>null</c>.</exception>
/// <exception cref="ArgumentException">If <paramref name="matrix"/> row count is less then column count</exception>
public DenseQR(DenseMatrix matrix, QRMethod method = QRMethod.Full)
{
if (matrix == null)
{
throw new ArgumentNullException("matrix");
}
if (matrix.RowCount < matrix.ColumnCount)
{
throw Matrix.DimensionsDontMatch<ArgumentException>(matrix);
}
Tau = new Complex32[Math.Min(matrix.RowCount, matrix.ColumnCount)];
if (method == QRMethod.Full)
{
MatrixR = matrix.Clone();
MatrixQ = new DenseMatrix(matrix.RowCount);
Control.LinearAlgebraProvider.QRFactor(((DenseMatrix)MatrixR).Values, matrix.RowCount, matrix.ColumnCount,
((DenseMatrix)MatrixQ).Values, Tau);
}
else
{
MatrixQ = matrix.Clone();
MatrixR = new DenseMatrix(matrix.ColumnCount);
Control.LinearAlgebraProvider.ThinQRFactor(((DenseMatrix)MatrixQ).Values, matrix.RowCount, matrix.ColumnCount,
((DenseMatrix)MatrixR).Values, Tau);
}
}
示例11: DenseEvd
/// <summary>
/// Initializes a new instance of the <see cref="DenseEvd"/> class. This object will compute the
/// the eigenvalue decomposition when the constructor is called and cache it's decomposition.
/// </summary>
/// <param name="matrix">The matrix to factor.</param>
/// <exception cref="ArgumentNullException">If <paramref name="matrix"/> is <c>null</c>.</exception>
/// <exception cref="ArgumentException">If EVD algorithm failed to converge with matrix <paramref name="matrix"/>.</exception>
public DenseEvd(DenseMatrix matrix)
{
if (matrix == null)
{
throw new ArgumentNullException("matrix");
}
if (matrix.RowCount != matrix.ColumnCount)
{
throw new ArgumentException(Resources.ArgumentMatrixSquare);
}
var order = matrix.RowCount;
// Initialize matrices for eigenvalues and eigenvectors
MatrixEv = DenseMatrix.Identity(order);
MatrixD = matrix.CreateMatrix(order, order);
VectorEv = new Complex.DenseVector(order);
IsSymmetric = true;
for (var i = 0; IsSymmetric && i < order; i++)
{
for (var j = 0; IsSymmetric && j < order; j++)
{
IsSymmetric &= matrix.At(i, j) == matrix.At(j, i).Conjugate();
}
}
Control.LinearAlgebraProvider.EigenDecomp(IsSymmetric, order, matrix.Values, ((DenseMatrix) MatrixEv).Values,
((Complex.DenseVector) VectorEv).Values, ((DenseMatrix) MatrixD).Values);
}
示例12: Create
/// <summary>
/// Initializes a new instance of the <see cref="DenseEvd"/> class. This object will compute the
/// the eigenvalue decomposition when the constructor is called and cache it's decomposition.
/// </summary>
/// <param name="matrix">The matrix to factor.</param>
/// <exception cref="ArgumentNullException">If <paramref name="matrix"/> is <c>null</c>.</exception>
/// <exception cref="ArgumentException">If EVD algorithm failed to converge with matrix <paramref name="matrix"/>.</exception>
public static DenseEvd Create(DenseMatrix matrix)
{
if (matrix.RowCount != matrix.ColumnCount)
{
throw new ArgumentException(Resources.ArgumentMatrixSquare);
}
var order = matrix.RowCount;
// Initialize matrices for eigenvalues and eigenvectors
var eigenVectors = DenseMatrix.Identity(order);
var blockDiagonal = new DenseMatrix(order);
var eigenValues = new DenseVector(order);
var isSymmetric = true;
for (var i = 0; isSymmetric && i < order; i++)
{
for (var j = 0; isSymmetric && j < order; j++)
{
isSymmetric &= matrix.At(i, j) == matrix.At(j, i).Conjugate();
}
}
Control.LinearAlgebraProvider.EigenDecomp(isSymmetric, order, matrix.Values, eigenVectors.Values, eigenValues.Values, blockDiagonal.Values);
return new DenseEvd(eigenVectors, eigenValues, blockDiagonal, isSymmetric);
}
示例13: ReadFile
public bool ReadFile(string fileName)
{
TextReader tr = null;
try {
tr = new StreamReader(fileName);
} catch (Exception e) {
return false;
}
string buffer = tr.ReadLine();
string[] numbers = buffer.Split(' ');
N = int.Parse(numbers[0]);
P = int.Parse(numbers[1]);
A = new DenseMatrix(P, N);
for (int i = 0; i < P; i++) {
buffer = tr.ReadLine();
numbers = buffer.Split(' ');
for (int j = 0; j < N; j++) {
A[i,j] = int.Parse(numbers[j]);
}
}
B = new DenseVector(P);
for (int i = 0; i < P; i++) {
buffer = tr.ReadLine();
B[i] = int.Parse(buffer);
}
return true;
}
示例14: FilterRowsBy
public static DenseMatrix FilterRowsBy(this Matrix m, int[] filter)
{
var result = new DenseMatrix(filter.Length, m.Columns);
for (int i = 0; i < filter.Length; ++i) {
result.SetRow(i, m.GetRow(filter[i]));
}
return result;
}
示例15: FilterColumnsBy
public static DenseMatrix FilterColumnsBy(this Matrix m, int[] filter)
{
var result = new DenseMatrix(m.Rows, filter.Length);
for (int j = 0; j < filter.Length; ++j) {
result.SetColumn(j, m.GetColumn(j));
}
return result;
}