2using System.Collections.Generic;
18 private double[,] values;
20 private ICollection<IElement> elements;
21 private readonly
int rows;
22 private readonly
int columns;
32 this.matrixElements =
null;
33 this.rows =
Values.GetLength(0);
34 this.columns =
Values.GetLength(1);
46 this.matrixElements =
null;
59 if (this.values is
null)
61 double[,] v =
new double[this.rows, this.columns];
65 foreach (
IElement E
in this.elements)
71 if (x >= this.columns)
92 if (this.elements is
null)
97 for (y = 0; y < this.rows; y++)
99 for (x = 0; x < this.columns; x++)
106 return this.elements;
117 if (this.matrixElements is
null)
126 if (x >= this.columns)
133 this.matrixElements = v;
136 return this.matrixElements;
153 double[,] v = this.
Values;
154 StringBuilder sb =
null;
158 for (y = 0; y < this.rows; y++)
161 sb =
new StringBuilder(
"[[");
163 sb.Append(
",\r\n [");
166 for (x = 0; x < this.columns; x++)
180 sb =
new StringBuilder(
"[[]]");
184 return sb.ToString();
194 if (this.associatedMatrixSpace is
null)
195 this.associatedMatrixSpace =
new DoubleMatrices(this.rows, this.columns);
197 return this.associatedMatrixSpace;
222 v =
new double[this.rows, this.columns];
224 for (y = 0; y < this.rows; y++)
226 for (x = 0; x < this.columns; x++)
227 v[y, x] = n *
Values[y, x];
234 if (Matrix.columns !=
this.rows)
238 double[,] Values2 = Matrix.Values;
240 v =
new double[Matrix.rows, this.columns];
241 for (y = 0; y < Matrix.rows; y++)
243 for (x = 0; x < this.columns; x++)
247 for (z = 0; z < this.rows; z++)
248 n += Values2[y, z] *
Values[z, x];
276 v =
new double[this.rows, this.columns];
278 for (y = 0; y < this.rows; y++)
280 for (x = 0; x < this.columns; x++)
281 v[y, x] = n *
Values[y, x];
288 if (this.columns != Matrix.rows)
292 double[,] Values2 = Matrix.Values;
294 v =
new double[this.rows, Matrix.columns];
295 for (y = 0; y < this.rows; y++)
297 for (x = 0; x < Matrix.columns; x++)
301 for (z = 0; z < this.columns; z++)
302 n +=
Values[y, z] * Values2[z, x];
322 if (this.rows != this.columns)
326 int c2 = this.columns << 1;
327 double[,] v =
new double[this.rows, c2];
330 for (y = 0; y < this.rows; y++)
332 for (x = 0; x < this.columns; x++)
335 v[y, x + this.columns] = (x == y ? 1 : 0);
339 if (
Reduce(v,
true,
true, out _) < 0)
342 double[,] v2 =
new double[this.rows, this.columns];
344 for (y = 0; y < this.rows; y++)
346 for (x = 0; x < this.columns; x++)
347 v2[y, x] = v[y, x + this.columns];
366 double[,] M = (
double[,])this.
Values.Clone();
367 Rank =
Reduce(M, Eliminate, BreakIfZero, out
double c);
383 public static int Reduce(
double[,] Matrix,
bool Eliminate,
bool BreakIfZero,
387 int Rows = Matrix.GetLength(0);
388 int Columns = Matrix.GetLength(1);
395 for (x = 0; x < MinCount; x++)
397 a = Math.Abs(Matrix[x, x]);
399 for (y = x + 1; y <
Rows; y++)
401 b = Math.Abs(Matrix[y, x]);
414 Matrix[x, u] = Matrix[z, u];
439 for (y = Eliminate ? 0 : x + 1; y <
Rows; y++)
441 if (y != x && (a = Matrix[y, x]) != 0)
444 Matrix[y, u] -= a * Matrix[x, u];
467 v =
new double[this.rows, this.columns];
469 for (y = 0; y < this.rows; y++)
471 for (x = 0; x < this.columns; x++)
472 v[y, x] = d +
Values[y, x];
479 if (this.columns != Matrix.columns ||
this.rows != Matrix.rows)
483 double[,] Values2 = Matrix.Values;
485 v =
new double[this.rows, this.columns];
486 for (y = 0; y < this.rows; y++)
488 for (x = 0; x < this.columns; x++)
489 v[y, x] =
Values[y, x] + Values2[y, x];
507 double[,] v =
new double[this.rows, this.columns];
510 for (y = 0; y < this.rows; y++)
512 for (x = 0; x < this.columns; x++)
528 if (this.columns != Matrix.columns ||
this.rows != Matrix.rows)
531 double[,] V1 = this.
Values;
532 double[,] V2 = Matrix.Values;
535 for (y = 0; y < this.rows; y++)
537 for (x = 0; x < this.columns; x++)
539 if (V1[y, x] != V2[y, x])
562 for (y = 0; y < this.rows; y++)
564 for (x = 0; x < this.columns; x++)
599 if (this.zero is
null)
600 this.zero =
new DoubleMatrix(
new double[this.rows, this.columns]);
620 if (!(this.rowVectors is
null))
621 return this.rowVectors;
623 double[,] v = this.
Values;
624 LinkedList<IElement>
Rows =
new LinkedList<IElement>();
628 for (y = 0; y < this.rows; y++)
630 r =
new double[this.columns];
632 for (x = 0; x < this.columns; x++)
638 this.rowVectors =
Rows;
643 private LinkedList<IElement> rowVectors =
null;
651 double[,] v =
new double[this.columns, this.rows];
655 for (y = 0; y < this.rows; y++)
657 for (x = 0; x < this.columns; x++)
680 if (Index < 0 || Index >= this.rows)
683 double[,] M = this.
Values;
684 double[] V =
new double[this.columns];
687 for (i = 0; i < this.columns; i++)
700 if (Index < 0 || Index >= this.rows)
704 throw new ScriptException(
"Row vectors in a double matrix are required to be double vectors.");
706 if (V.Dimension !=
this.columns)
709 double[] V2 = V.Values;
710 double[,] M = this.
Values;
711 this.elements =
null;
715 for (i = 0; i < this.columns; i++)
727 if (Column < 0 || Column >= this.columns || Row < 0 || Row >= this.rows)
741 if (Column < 0 || Column >= this.columns || Row < 0 || Row >= this.rows)
745 throw new ScriptException(
"Elements in a double matrix must be double values.");
747 double[,] M = this.
Values;
748 this.elements =
null;
760 if (Row < 0 || Row >= this.rows)
763 double[,] M = this.
Values;
764 double[] V =
new double[this.columns];
767 for (i = 0; i < this.columns; i++)
780 if (Column < 0 || Column >= this.columns)
783 double[,] M = this.
Values;
784 double[] V =
new double[this.rows];
787 for (i = 0; i < this.rows; i++)
800 if (Row < 0 || Row >= this.rows)
804 throw new ScriptException(
"Vector dimension does not match number of columns");
807 throw new ScriptException(
"Row vectors in a double matrix must be double vectors.");
809 double[] V2 = V.Values;
810 double[,] M = this.
Values;
811 this.elements =
null;
814 for (i = 0; i < this.columns; i++)
825 if (Column < 0 || Column >= this.columns)
829 throw new ScriptException(
"Vector dimension does not match number of rows");
832 throw new ScriptException(
"Column vectors in a double matrix must be double vectors.");
834 double[] V2 = V.Values;
835 double[,] M = this.
Values;
836 this.elements =
null;
839 for (i = 0; i < this.rows; i++)
840 M[i, Column] = V2[i];
Base class for all types of elements.
abstract object AssociatedObjectValue
Associated object value.
Base class for all types of ring elements.
Base class for script exceptions.
Class managing a script expression.
static string ToString(double Value)
Converts a value to a string, that can be parsed as part of an expression.
Base class for all nodes in a parsed script tree.
Pseudo-ring of Double-valued matrices.
IElement GetElement(int Column, int Row)
Gets an element of the matrix.
IMatrix Reduce(bool Eliminate, bool BreakIfZero, out int Rank, out ICommutativeRingWithIdentityElement Factor)
Reduces a matrix.
void SetColumn(int Column, IVector Vector)
Gets a column vector from the matrix.
override ICollection< IElement > ChildElements
An enumeration of child elements. If the element is a scalar, this property will return null.
static int Reduce(double[,] Matrix, bool Eliminate, bool BreakIfZero, out double Factor)
Reduces a matrix.
IElement GetElement(int Index)
Gets an element of the vector.
DoubleMatrix(int Rows, int Columns, ICollection< IElement > Elements)
Double-valued vector.
override IGroupElement Negate()
Negates the element.
override bool IsScalar
If the element represents a scalar value.
override string ToString()
void SetElement(int Index, IElement Value)
Sets an element in the vector.
int Dimension
Dimension of matrix, if seen as a vector of row vectors.
IMatrix Transpose()
Returns a transposed matrix.
override bool Equals(object obj)
Compares the element to another.
IElement[,] MatrixElements
Matrix elements
double[,] Values
Matrix element values.
override object AssociatedObjectValue
Associated object value.
IVector GetRow(int Row)
Gets a row vector from the matrix.
void SetElement(int Column, int Row, IElement Value)
Sets an element in the matrix.
int Columns
Number of columns.
override IRing AssociatedRing
Associated Ring.
ICollection< IElement > VectorElements
Vector of row vectors.
DoubleMatrix(double[,] Values)
Double-valued matrix.
override IRingElement MultiplyLeft(IRingElement Element)
Tries to multiply an element to the current element, from the left.
IMatrix ConjugateTranspose()
Returns a conjugate transposed matrix.
override IElement Encapsulate(ICollection< IElement > Elements, ScriptNode Node)
Encapsulates a set of elements into a similar structure as that provided by the current element.
override IRingElement MultiplyRight(IRingElement Element)
Tries to multiply an element to the current element, from the right.
override IRingElement Invert()
Inverts the element, if possible.
IVector GetColumn(int Column)
Gets a column vector from the matrix.
void SetRow(int Row, IVector Vector)
Gets a row vector from the matrix.
ICollection< IElement > Elements
Matrix elements.
override IAbelianGroupElement Zero
Returns the zero element of the group.
override int GetHashCode()
Calculates a hash code of the element.
override IAbelianGroupElement Add(IAbelianGroupElement Element)
Tries to add an element to the current element.
override IRingElement MultiplyRight(IRingElement Element)
Tries to multiply an element to the current element, from the right.
override IAbelianGroupElement Add(IAbelianGroupElement Element)
Tries to add an element to the current element.
override IRingElement MultiplyLeft(IRingElement Element)
Tries to multiply an element to the current element, from the left.
override bool Equals(object obj)
Compares the element to another.
static IMatrix Encapsulate(ICollection< IElement > Rows, ScriptNode Node)
Encapsulates the elements of a matrix.
Basic interface for all types of abelian group elements.
Basic interface for all types of commutative ring with identity elements.
Basic interface for all types of elements.
object AssociatedObjectValue
Associated object value.
Basic interface for all types of group elements.
Basic interface for matrices.
Basic interface for all types of ring elements.
Basic interface for vectors.
int Dimension
Dimension of vector.
Basic interface for all types of rings.