forked from tsai/mathnet-numerics
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
340 lines
11 KiB
340 lines
11 KiB
// <copyright file="Matrix.cs" company="Math.NET">
|
|
// Math.NET Numerics, part of the Math.NET Project
|
|
// http://mathnet.opensourcedotnet.info
|
|
// Copyright (c) 2009 Math.NET
|
|
// Permission is hereby granted, free of charge, to any person
|
|
// obtaining a copy of this software and associated documentation
|
|
// files (the "Software"), to deal in the Software without
|
|
// restriction, including without limitation the rights to use,
|
|
// copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
// copies of the Software, and to permit persons to whom the
|
|
// Software is furnished to do so, subject to the following
|
|
// conditions:
|
|
// The above copyright notice and this permission notice shall be
|
|
// included in all copies or substantial portions of the Software.
|
|
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
|
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
|
|
// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
|
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
|
|
// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
|
|
// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
|
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
|
|
// OTHER DEALINGS IN THE SOFTWARE.
|
|
// </copyright>
|
|
namespace MathNet.Numerics.LinearAlgebra.Double
|
|
{
|
|
using System;
|
|
using System.Text;
|
|
|
|
using MathNet.Numerics.Properties;
|
|
|
|
/// <summary>
|
|
/// A matrix class.
|
|
/// </summary>
|
|
[Serializable]
|
|
public abstract class Matrix : IFormattable, ICloneable, IEquatable<Matrix>
|
|
{
|
|
/// <summary>
|
|
/// Initializes a new instance of the <see cref="Matrix"/> class.
|
|
/// </summary>
|
|
/// <param name="rows">
|
|
/// The number of rows.
|
|
/// </param>
|
|
/// <param name="columns">
|
|
/// The number of columns.
|
|
/// </param>
|
|
protected Matrix(int rows, int columns)
|
|
{
|
|
RowCount = rows;
|
|
ColumnCount = columns;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Initializes a new instance of the <see cref="Matrix"/> class.
|
|
/// </summary>
|
|
/// <param name="order">
|
|
/// The order of the matrix.
|
|
/// </param>
|
|
protected Matrix(int order)
|
|
{
|
|
RowCount = order;
|
|
ColumnCount = order;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Gets the number of columns.
|
|
/// </summary>
|
|
/// <value>The number of columns.</value>
|
|
public virtual int ColumnCount { get; private set; }
|
|
|
|
/// <summary>
|
|
/// Gets the number of rows.
|
|
/// </summary>
|
|
/// <value>The number of rows.</value>
|
|
public virtual int RowCount { get; private set; }
|
|
|
|
/// <summary>
|
|
/// Gets or sets the value at the given row and column.
|
|
/// </summary>
|
|
/// <param name="row">
|
|
/// The row of the element.
|
|
/// </param>
|
|
/// <param name="column">
|
|
/// The column of the element.
|
|
/// </param>
|
|
/// <value>The double value to get or set.</value>
|
|
/// <remarks>This method is ranged checked. <see cref="At(int,int)"/> and <see cref="At(int,int,double)"/>
|
|
/// to get and set values without range checking.</remarks>
|
|
public virtual double this[int row, int column]
|
|
{
|
|
get
|
|
{
|
|
RangeCheck(row, column);
|
|
return At(row, column);
|
|
}
|
|
|
|
set
|
|
{
|
|
RangeCheck(row, column);
|
|
At(row, column, value);
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Retrieves the requested element without range checking.
|
|
/// </summary>
|
|
/// <param name="row">
|
|
/// The row of the element.
|
|
/// </param>
|
|
/// <param name="column">
|
|
/// The column of the element.
|
|
/// </param>
|
|
/// <returns>
|
|
/// The requested element.
|
|
/// </returns>
|
|
public abstract double At(int row, int column);
|
|
|
|
/// <summary>
|
|
/// Sets the value of the given element.
|
|
/// </summary>
|
|
/// <param name="row">
|
|
/// The row of the element.
|
|
/// </param>
|
|
/// <param name="column">
|
|
/// The column of the element.
|
|
/// </param>
|
|
/// <param name="value">
|
|
/// The value to set the element to.
|
|
/// </param>
|
|
public abstract void At(int row, int column, double value);
|
|
|
|
/// <summary>
|
|
/// Creates a clone of this instance.
|
|
/// </summary>
|
|
/// <returns>
|
|
/// A clone of the instance.
|
|
/// </returns>
|
|
public virtual Matrix Clone()
|
|
{
|
|
var result = CreateMatrix(RowCount, ColumnCount);
|
|
CopyTo(result);
|
|
return result;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Copies the elements of this matrix to the given matrix.
|
|
/// </summary>
|
|
/// <param name="target">
|
|
/// The matrix to copy values into.
|
|
/// </param>
|
|
/// <exception cref="ArgumentNullException">
|
|
/// If target is <see langword="null"/>.
|
|
/// </exception>
|
|
/// <exception cref="ArgumentException">
|
|
/// If this and the target matrix do not have the same dimensions..
|
|
/// </exception>
|
|
public virtual void CopyTo(Matrix target)
|
|
{
|
|
if (target == null)
|
|
{
|
|
throw new ArgumentNullException("target");
|
|
}
|
|
|
|
if (ReferenceEquals(this, target))
|
|
{
|
|
return;
|
|
}
|
|
|
|
if (RowCount != target.RowCount || ColumnCount != target.ColumnCount)
|
|
{
|
|
throw new ArgumentException("target", Resources.ArgumentMatrixSameDimensions);
|
|
}
|
|
|
|
throw new NotImplementedException();
|
|
|
|
/*
|
|
foreach (KeyValuePair<int, Vector> column in GetColumnEnumerator())
|
|
{
|
|
foreach (KeyValuePair<int, double> element in column.Value.GetIndexedEnumerator())
|
|
{
|
|
target.ValueAt(element.Key, column.Key, element.Value);
|
|
}
|
|
}*/
|
|
}
|
|
|
|
/// <summary>
|
|
/// Creates a <strong>Matrix</strong> for the given number of rows and columns.
|
|
/// </summary>
|
|
/// <param name="numberOfRows">
|
|
/// The number of rows.
|
|
/// </param>
|
|
/// <param name="numberOfColumns">
|
|
/// The number of columns.
|
|
/// </param>
|
|
/// <returns>
|
|
/// A <strong>Matrix</strong> with the given dimensions.
|
|
/// </returns>
|
|
/// <remarks>
|
|
/// Creates a matrix of the same matrix type as the current matrix.
|
|
/// </remarks>
|
|
public abstract Matrix CreateMatrix(int numberOfRows, int numberOfColumns);
|
|
|
|
/// <summary>
|
|
/// Returns a <see cref="System.String"/> that represents this instance.
|
|
/// </summary>
|
|
/// <returns>
|
|
/// A <see cref="System.String"/> that represents this instance.
|
|
/// </returns>
|
|
public override string ToString()
|
|
{
|
|
return this.ToString(null, null);
|
|
}
|
|
|
|
#region Implemented Interfaces
|
|
|
|
#region ICloneable
|
|
|
|
/// <summary>
|
|
/// Creates a new object that is a copy of the current instance.
|
|
/// </summary>
|
|
/// <returns>
|
|
/// A new object that is a copy of this instance.
|
|
/// </returns>
|
|
object ICloneable.Clone()
|
|
{
|
|
return Clone();
|
|
}
|
|
|
|
#endregion
|
|
|
|
#region IEquatable<Matrix>
|
|
|
|
/// <summary>
|
|
/// Indicates whether the current object is equal to another object of the same type.
|
|
/// </summary>
|
|
/// <param name="other">
|
|
/// An object to compare with this object.
|
|
/// </param>
|
|
/// <returns>
|
|
/// true if the current object is equal to the <paramref name="other"/> parameter; otherwise, false.
|
|
/// </returns>
|
|
public bool Equals(Matrix other)
|
|
{
|
|
// Reject equality when the argument is null or has a different shape.
|
|
if (other == null)
|
|
{
|
|
return false;
|
|
}
|
|
|
|
if (ColumnCount != other.ColumnCount || RowCount != other.RowCount)
|
|
{
|
|
return false;
|
|
}
|
|
|
|
// Accept if the argument is the same object as this.
|
|
if (ReferenceEquals(this, other))
|
|
{
|
|
return true;
|
|
}
|
|
|
|
// If all else fails, perform element wise comparison.
|
|
for (var row = 0; row < RowCount; row++)
|
|
{
|
|
for (var column = 0; column < ColumnCount; column++)
|
|
{
|
|
if (At(row, column) != other.At(row, column))
|
|
{
|
|
return false;
|
|
}
|
|
}
|
|
}
|
|
|
|
return true;
|
|
}
|
|
|
|
#endregion
|
|
|
|
#region IFormattable
|
|
|
|
/// <summary>
|
|
/// Returns a <see cref="System.String"/> that represents this instance.
|
|
/// </summary>
|
|
/// <param name="format">
|
|
/// The format to use.
|
|
/// </param>
|
|
/// <param name="formatProvider">
|
|
/// The format provider to use.
|
|
/// </param>
|
|
/// <returns>
|
|
/// A <see cref="System.String"/> that represents this instance.
|
|
/// </returns>
|
|
public string ToString(string format, IFormatProvider formatProvider)
|
|
{
|
|
var stringBuilder = new StringBuilder();
|
|
for (var row = 0; row < RowCount; row++)
|
|
{
|
|
for (var column = 0; column < ColumnCount; column++)
|
|
{
|
|
stringBuilder.Append(At(row, column).ToString(format, formatProvider));
|
|
if (column != ColumnCount - 1)
|
|
{
|
|
stringBuilder.Append(",");
|
|
}
|
|
}
|
|
|
|
if (row != RowCount - 1)
|
|
{
|
|
stringBuilder.Append(Environment.NewLine);
|
|
}
|
|
}
|
|
|
|
return stringBuilder.ToString();
|
|
}
|
|
|
|
#endregion
|
|
|
|
#endregion
|
|
|
|
/// <summary>
|
|
/// Ranges the check.
|
|
/// </summary>
|
|
/// <param name="row">
|
|
/// The row of the element.
|
|
/// </param>
|
|
/// <param name="column">
|
|
/// The column of the element.
|
|
/// </param>
|
|
private void RangeCheck(int row, int column)
|
|
{
|
|
if (row < 0 || row >= RowCount)
|
|
{
|
|
throw new ArgumentOutOfRangeException("row");
|
|
}
|
|
|
|
if (column < 0 || column >= ColumnCount)
|
|
{
|
|
throw new ArgumentOutOfRangeException("column");
|
|
}
|
|
}
|
|
}
|
|
}
|