/// <summary> /// Initializes a square <see cref="DiagonalMatrix"/> with all zero's except for ones on the diagonal. /// </summary> /// <param name="order">the size of the square matrix.</param> /// <returns>A diagonal identity matrix.</returns> /// <exception cref="ArgumentException"> /// If <paramref name="order"/> is less than one. /// </exception> public static DiagonalMatrix Identity(int order) { var m = new DiagonalMatrix(order); for (var i = 0; i < order; i++) { m._data[i] = 1.0; } return(m); }
/// <summary> /// Returns the transpose of this matrix. /// </summary> /// <returns>The transpose of this matrix.</returns> public override Matrix<Complex> Transpose() { var ret = new DiagonalMatrix(ColumnCount, RowCount); Array.Copy(_data, ret._data, _data.Length); return ret; }
public void MatrixFrom1DArrayIsReference() { var data = new[] {new Complex(1.0, 1), new Complex(2.0, 1), new Complex(3.0, 1), new Complex(4.0, 1), new Complex(5.0, 1)}; var matrix = new DiagonalMatrix(5, 5, data); matrix[0, 0] = new Complex(10.0, 1); Assert.AreEqual(new Complex(10.0, 1), data[0]); }
public void CanCreateMatrixWithUniformValues() { var matrix = new DiagonalMatrix(10, 10, new Complex(10.0, 1)); for (var i = 0; i < matrix.RowCount; i++) { Assert.AreEqual(matrix[i, i], new Complex(10.0, 1)); } }
/// <summary> /// Adds another matrix to this matrix. /// </summary> /// <param name="other">The matrix to add to this matrix.</param> /// <returns>The result of the addition.</returns> /// <exception cref="ArgumentNullException">If the other matrix is <see langword="null"/>.</exception> /// <exception cref="ArgumentOutOfRangeException">If the two matrices don't have the same dimensions.</exception> public override Matrix<Complex> Add(Matrix<Complex> other) { if (other == null) { throw new ArgumentNullException("other"); } if (other.RowCount != RowCount || other.ColumnCount != ColumnCount) { throw new ArgumentOutOfRangeException("other", Resources.ArgumentMatrixDimensions); } Matrix<Complex> result; if (other is DiagonalMatrix) { result = new DenseMatrix(RowCount, ColumnCount); } else { result = new DiagonalMatrix(RowCount, ColumnCount); } Add(other, result); return result; }
/// <summary> /// Initializes a square <see cref="DiagonalMatrix"/> with all zero's except for ones on the diagonal. /// </summary> /// <param name="order">the size of the square matrix.</param> /// <returns>A diagonal identity matrix.</returns> /// <exception cref="ArgumentException"> /// If <paramref name="order"/> is less than one. /// </exception> public static DiagonalMatrix Identity(int order) { var m = new DiagonalMatrix(order); for (var i = 0; i < order; i++) { m.Data[i] = 1.0; } return m; }
/// <summary> /// Subtracts another matrix from this matrix. /// </summary> /// <param name="other">The matrix to subtract.</param> /// <returns>The result of the subtraction.</returns> /// <exception cref="ArgumentNullException">If the other matrix is <see langword="null"/>.</exception> /// <exception cref="ArgumentOutOfRangeException">If the two matrices don't have the same dimensions.</exception> public override Matrix<Complex> Subtract(Matrix<Complex> other) { if (other == null) { throw new ArgumentNullException("other"); } if (other.RowCount != RowCount || other.ColumnCount != ColumnCount) { throw DimensionsDontMatch<ArgumentOutOfRangeException>(this, other, "other"); } Matrix<Complex> result; if (other is DiagonalMatrix) { result = new DenseMatrix(RowCount, ColumnCount); } else { result = new DiagonalMatrix(RowCount, ColumnCount); } Subtract(other, result); return result; }