2using System.Collections.Generic;
19 private Complex[,] values;
21 private ICollection<IElement> elements;
22 private readonly
int rows;
23 private readonly
int columns;
33 this.matrixElements =
null;
34 this.rows =
Values.GetLength(0);
35 this.columns =
Values.GetLength(1);
48 this.matrixElements =
null;
60 if (this.values is
null)
62 Complex[,] v =
new Complex[this.rows, this.columns];
66 foreach (
IElement E
in this.elements)
72 if (x >= this.columns)
93 if (this.elements is
null)
98 for (y = 0; y < this.rows; y++)
100 for (x = 0; x < this.columns; x++)
107 return this.elements;
118 if (this.matrixElements is
null)
127 if (x >= this.columns)
134 this.matrixElements = v;
137 return this.matrixElements;
154 Complex[,] v = this.
Values;
155 StringBuilder sb =
null;
159 for (y = 0; y < this.rows; y++)
162 sb =
new StringBuilder(
"[[");
164 sb.Append(
",\r\n [");
167 for (x = 0; x < this.columns; x++)
181 sb =
new StringBuilder(
"[[]]");
185 return sb.ToString();
195 if (this.associatedMatrixSpace is
null)
196 this.associatedMatrixSpace =
new ComplexMatrices(this.rows, this.columns);
198 return this.associatedMatrixSpace;
223 v =
new Complex[this.rows, this.columns];
225 for (y = 0; y < this.rows; y++)
227 for (x = 0; x < this.columns; x++)
228 v[y, x] = n *
Values[y, x];
235 if (Matrix.columns !=
this.rows)
239 Complex[,] Values2 = Matrix.Values;
241 v =
new Complex[Matrix.rows, this.columns];
242 for (y = 0; y < Matrix.rows; y++)
244 for (x = 0; x < this.columns; x++)
248 for (z = 0; z < this.rows; z++)
249 n += Values2[y, z] *
Values[z, x];
277 v =
new Complex[this.rows, this.columns];
279 for (y = 0; y < this.rows; y++)
281 for (x = 0; x < this.columns; x++)
282 v[y, x] = n *
Values[y, x];
289 if (this.columns != Matrix.rows)
293 Complex[,] Values2 = Matrix.Values;
295 v =
new Complex[this.rows, Matrix.columns];
296 for (y = 0; y < this.rows; y++)
298 for (x = 0; x < Matrix.columns; x++)
302 for (z = 0; z < this.columns; z++)
303 n +=
Values[y, z] * Values2[z, x];
323 if (this.rows != this.columns)
327 int c2 = this.columns << 1;
328 Complex[,] v =
new Complex[this.rows, c2];
331 for (y = 0; y < this.rows; y++)
333 for (x = 0; x < this.columns; x++)
336 v[y, x + this.columns] = (x == y ? 1 : 0);
340 if (
Reduce(v,
true,
true, out _) < 0)
343 Complex[,] v2 =
new Complex[this.rows, this.columns];
345 for (y = 0; y < this.rows; y++)
347 for (x = 0; x < this.columns; x++)
348 v2[y, x] = v[y, x + this.columns];
367 Complex[,] M = (Complex[,])this.
Values.Clone();
368 Rank =
Reduce(M, Eliminate, BreakIfZero, out Complex c);
384 public static int Reduce(Complex[,] Matrix,
bool Eliminate,
bool BreakIfZero,
388 int Rows = Matrix.GetLength(0);
389 int Columns = Matrix.GetLength(1);
397 for (x = 0; x < MinCount; x++)
399 a = Matrix[x, x].Magnitude;
401 for (y = x + 1; y <
Rows; y++)
403 b = Matrix[y, x].Magnitude;
416 Matrix[x, u] = Matrix[z, u];
441 for (y = Eliminate ? 0 : x + 1; y <
Rows; y++)
443 if (y != x && (w = Matrix[y, x]) != 0)
446 Matrix[y, u] -= w * Matrix[x, u];
469 v =
new Complex[this.rows, this.columns];
471 for (y = 0; y < this.rows; y++)
473 for (x = 0; x < this.columns; x++)
474 v[y, x] = n +
Values[y, x];
481 if (this.columns != Matrix.columns ||
this.rows != Matrix.rows)
485 Complex[,] Values2 = Matrix.Values;
487 v =
new Complex[this.rows, this.columns];
488 for (y = 0; y < this.rows; y++)
490 for (x = 0; x < this.columns; x++)
491 v[y, x] =
Values[y, x] + Values2[y, x];
509 Complex[,] v =
new Complex[this.rows, this.columns];
512 for (y = 0; y < this.rows; y++)
514 for (x = 0; x < this.columns; x++)
530 if (this.columns != Matrix.columns ||
this.rows != Matrix.rows)
533 Complex[,] V1 = this.
Values;
534 Complex[,] V2 = Matrix.Values;
537 for (y = 0; y < this.rows; y++)
539 for (x = 0; x < this.columns; x++)
541 if (V1[y, x] != V2[y, x])
564 for (y = 0; y < this.rows; y++)
566 for (x = 0; x < this.columns; x++)
601 if (this.zero is
null)
602 this.zero =
new ComplexMatrix(
new Complex[this.rows, this.columns]);
622 if (!(this.rowVectors is
null))
623 return this.rowVectors;
625 Complex[,] v = this.
Values;
626 LinkedList<IElement>
Rows =
new LinkedList<IElement>();
630 for (y = 0; y < this.rows; y++)
632 r =
new Complex[this.columns];
634 for (x = 0; x < this.columns; x++)
640 this.rowVectors =
Rows;
645 private LinkedList<IElement> rowVectors =
null;
653 Complex[,] v =
new Complex[this.columns, this.rows];
657 for (y = 0; y < this.rows; y++)
659 for (x = 0; x < this.columns; x++)
672 Complex[,] v =
new Complex[this.columns, this.rows];
676 for (y = 0; y < this.rows; y++)
678 for (x = 0; x < this.columns; x++)
679 v[x, y] = Complex.Conjugate(
Values[y, x]);
692 if (Index < 0 || Index >= this.rows)
695 Complex[,] M = this.
Values;
696 Complex[] V =
new Complex[this.columns];
699 for (i = 0; i < this.columns; i++)
712 if (Index < 0 || Index >= this.rows)
716 throw new ScriptException(
"Row vectors in a Complex matrix are required to be Complex vectors.");
718 if (V.Dimension !=
this.columns)
721 Complex[] V2 = V.Values;
722 Complex[,] M = this.
Values;
723 this.elements =
null;
727 for (i = 0; i < this.columns; i++)
739 if (Column < 0 || Column >= this.columns || Row < 0 || Row >= this.rows)
753 if (Column < 0 || Column >= this.columns || Row < 0 || Row >= this.rows)
757 throw new ScriptException(
"Elements in a Complex matrix must be Complex values.");
759 Complex[,] M = this.
Values;
760 this.elements =
null;
772 if (Row < 0 || Row >= this.rows)
775 Complex[,] M = this.
Values;
776 Complex[] V =
new Complex[this.columns];
779 for (i = 0; i < this.columns; i++)
792 if (Column < 0 || Column >= this.columns)
795 Complex[,] M = this.
Values;
796 Complex[] V =
new Complex[this.rows];
799 for (i = 0; i < this.rows; i++)
812 if (Row < 0 || Row >= this.rows)
816 throw new ScriptException(
"Vector dimension does not match number of columns");
819 throw new ScriptException(
"Row vectors in a Complex matrix must be Complex vectors.");
821 Complex[] V2 = V.Values;
822 Complex[,] M = this.
Values;
823 this.elements =
null;
826 for (i = 0; i < this.columns; i++)
837 if (Column < 0 || Column >= this.columns)
841 throw new ScriptException(
"Vector dimension does not match number of rows");
844 throw new ScriptException(
"Column vectors in a Complex matrix must be Complex vectors.");
846 Complex[] V2 = V.Values;
847 Complex[,] M = this.
Values;
848 this.elements =
null;
851 for (i = 0; i < this.rows; i++)
852 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 Complex-valued matrices.
static int Reduce(Complex[,] Matrix, bool Eliminate, bool BreakIfZero, out Complex Factor)
Reduces a matrix.
override ICollection< IElement > ChildElements
An enumeration of child elements. If the element is a scalar, this property will return null.
IElement[,] MatrixElements
Matrix elements
IElement GetElement(int Column, int Row)
Gets an element of the matrix.
IMatrix Transpose()
Returns a transposed matrix.
ICollection< IElement > VectorElements
Vector of row vectors.
void SetRow(int Row, IVector Vector)
Gets a row vector from the matrix.
override IAbelianGroupElement Add(IAbelianGroupElement Element)
Tries to add an element to the current element.
override string ToString()
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 IAbelianGroupElement Zero
Returns the zero element of the group.
Complex[,] Values
Matrix element values.
override IRingElement MultiplyRight(IRingElement Element)
Tries to multiply an element to the current element, from the right.
ComplexMatrix(Complex[,] Values)
Complex-valued matrix.
override bool Equals(object obj)
Compares the element to another.
override IRingElement MultiplyLeft(IRingElement Element)
Tries to multiply an element to the current element, from the left.
override IGroupElement Negate()
Negates the element.
override IRingElement Invert()
Inverts the element, if possible.
IMatrix ConjugateTranspose()
Returns a conjugate transposed matrix.
IElement GetElement(int Index)
Gets an element of the vector.
IMatrix Reduce(bool Eliminate, bool BreakIfZero, out int Rank, out ICommutativeRingWithIdentityElement Factor)
Reduces a matrix.
override bool IsScalar
If the element represents a scalar value.
ICollection< IElement > Elements
Matrix elements.
void SetElement(int Column, int Row, IElement Value)
Sets an element in the matrix.
IVector GetRow(int Row)
Gets a row vector from the matrix.
int Columns
Number of columns.
IVector GetColumn(int Column)
Gets a column vector from the matrix.
int Dimension
Dimension of matrix, if seen as a vector of row vectors.
void SetElement(int Index, IElement Value)
Sets an element in the vector.
override int GetHashCode()
Calculates a hash code of the element.
override object AssociatedObjectValue
Associated object value.
void SetColumn(int Column, IVector Vector)
Gets a column vector from the matrix.
override IRing AssociatedRing
Associated Ring.
ComplexMatrix(int Rows, int Columns, ICollection< IElement > Elements)
Complex-valued vector.
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.