20 private Complex[,] values;
22 private ICollection<IElement> elements;
23 private readonly
int rows;
24 private readonly
int columns;
34 this.matrixElements =
null;
35 this.rows =
Values.GetLength(0);
36 this.columns =
Values.GetLength(1);
49 this.matrixElements =
null;
61 if (this.values is
null)
63 Complex[,] v =
new Complex[this.rows, this.columns];
72 while (!(Loop is
null))
74 for (i = Loop.
Start, c = Loop.
Pos; i < c; i++)
80 if (x >= this.columns)
92 foreach (
IElement E
in this.elements)
98 if (x >= this.columns)
120 if (this.elements is
null)
125 for (y = 0; y < this.rows; y++)
127 for (x = 0; x < this.columns; x++)
134 return this.elements;
145 if (this.matrixElements is
null)
154 if (x >= this.columns)
161 this.matrixElements = v;
164 return this.matrixElements;
181 Complex[,] v = this.
Values;
182 StringBuilder sb =
null;
186 for (y = 0; y < this.rows; y++)
189 sb =
new StringBuilder(
"[[");
191 sb.Append(
",\r\n [");
194 for (x = 0; x < this.columns; x++)
208 sb =
new StringBuilder(
"[[]]");
212 return sb.ToString();
222 if (this.associatedMatrixSpace is
null)
223 this.associatedMatrixSpace =
new ComplexMatrices(this.rows, this.columns);
225 return this.associatedMatrixSpace;
250 v =
new Complex[this.rows, this.columns];
252 for (y = 0; y < this.rows; y++)
254 for (x = 0; x < this.columns; x++)
255 v[y, x] = n *
Values[y, x];
262 if (Matrix.columns !=
this.rows)
266 Complex[,] Values2 = Matrix.Values;
268 v =
new Complex[Matrix.rows, this.columns];
269 for (y = 0; y < Matrix.rows; y++)
271 for (x = 0; x < this.columns; x++)
275 for (z = 0; z < this.rows; z++)
276 n += Values2[y, z] *
Values[z, x];
304 v =
new Complex[this.rows, this.columns];
306 for (y = 0; y < this.rows; y++)
308 for (x = 0; x < this.columns; x++)
309 v[y, x] = n *
Values[y, x];
316 if (this.columns != Matrix.rows)
320 Complex[,] Values2 = Matrix.Values;
322 v =
new Complex[this.rows, Matrix.columns];
323 for (y = 0; y < this.rows; y++)
325 for (x = 0; x < Matrix.columns; x++)
329 for (z = 0; z < this.columns; z++)
330 n +=
Values[y, z] * Values2[z, x];
350 if (this.rows != this.columns)
354 int c2 = this.columns << 1;
355 Complex[,] v =
new Complex[this.rows, c2];
358 for (y = 0; y < this.rows; y++)
360 for (x = 0; x < this.columns; x++)
363 v[y, x + this.columns] = (x == y ? 1 : 0);
367 if (
Reduce(v,
true,
true, out
_) < 0)
370 Complex[,] v2 =
new Complex[this.rows, this.columns];
372 for (y = 0; y < this.rows; y++)
374 for (x = 0; x < this.columns; x++)
375 v2[y, x] = v[y, x + this.columns];
394 Complex[,] M = (Complex[,])this.
Values.Clone();
395 Rank =
Reduce(M, Eliminate, BreakIfZero, out Complex c);
411 public static int Reduce(Complex[,] Matrix,
bool Eliminate,
bool BreakIfZero,
415 int Rows = Matrix.GetLength(0);
416 int Columns = Matrix.GetLength(1);
424 for (x = 0; x < MinCount; x++)
426 a = Matrix[x, x].Magnitude;
428 for (y = x + 1; y <
Rows; y++)
430 b = Matrix[y, x].Magnitude;
443 Matrix[x, u] = Matrix[z, u];
468 for (y = Eliminate ? 0 : x + 1; y <
Rows; y++)
470 if (y != x && (w = Matrix[y, x]) != 0)
473 Matrix[y, u] -= w * Matrix[x, u];
496 v =
new Complex[this.rows, this.columns];
498 for (y = 0; y < this.rows; y++)
500 for (x = 0; x < this.columns; x++)
501 v[y, x] = n +
Values[y, x];
508 if (this.columns != Matrix.columns ||
this.rows != Matrix.rows)
512 Complex[,] Values2 = Matrix.Values;
514 v =
new Complex[this.rows, this.columns];
515 for (y = 0; y < this.rows; y++)
517 for (x = 0; x < this.columns; x++)
518 v[y, x] =
Values[y, x] + Values2[y, x];
536 Complex[,] v =
new Complex[this.rows, this.columns];
539 for (y = 0; y < this.rows; y++)
541 for (x = 0; x < this.columns; x++)
557 if (this.columns != Matrix.columns ||
this.rows != Matrix.rows)
560 Complex[,] V1 = this.
Values;
561 Complex[,] V2 = Matrix.Values;
564 for (y = 0; y < this.rows; y++)
566 for (x = 0; x < this.columns; x++)
568 if (V1[y, x] != V2[y, x])
591 for (y = 0; y < this.rows; y++)
593 for (x = 0; x < this.columns; x++)
639 if (this.zero is
null)
640 this.zero =
new ComplexMatrix(
new Complex[this.rows, this.columns]);
660 if (!(this.rowVectors is
null))
661 return this.rowVectors;
663 Complex[,] v = this.
Values;
668 for (y = 0; y < this.rows; y++)
670 r =
new Complex[this.columns];
672 for (x = 0; x < this.columns; x++)
678 this.rowVectors =
Rows;
691 Complex[,] v =
new Complex[this.columns, this.rows];
695 for (y = 0; y < this.rows; y++)
697 for (x = 0; x < this.columns; x++)
710 Complex[,] v =
new Complex[this.columns, this.rows];
714 for (y = 0; y < this.rows; y++)
716 for (x = 0; x < this.columns; x++)
717 v[x, y] = Complex.Conjugate(
Values[y, x]);
730 if (Index < 0 || Index >= this.rows)
733 Complex[,] M = this.
Values;
734 Complex[] V =
new Complex[this.columns];
737 for (i = 0; i < this.columns; i++)
750 if (Index < 0 || Index >= this.rows)
754 throw new ScriptException(
"Row vectors in a Complex matrix are required to be Complex vectors.");
756 if (V.Dimension !=
this.columns)
759 Complex[] V2 = V.Values;
760 Complex[,] M = this.
Values;
761 this.elements =
null;
765 for (i = 0; i < this.columns; i++)
777 if (Column < 0 || Column >= this.columns || Row < 0 || Row >= this.rows)
791 if (Column < 0 || Column >= this.columns || Row < 0 || Row >= this.rows)
795 throw new ScriptException(
"Elements in a Complex matrix must be Complex values.");
797 Complex[,] M = this.
Values;
798 this.elements =
null;
810 if (Row < 0 || Row >= this.rows)
813 Complex[,] M = this.
Values;
814 Complex[] V =
new Complex[this.columns];
817 for (i = 0; i < this.columns; i++)
830 if (Column < 0 || Column >= this.columns)
833 Complex[,] M = this.
Values;
834 Complex[] V =
new Complex[this.rows];
837 for (i = 0; i < this.rows; i++)
850 if (Row < 0 || Row >= this.rows)
854 throw new ScriptException(
"Vector dimension does not match number of columns");
857 throw new ScriptException(
"Row vectors in a Complex matrix must be Complex vectors.");
859 Complex[] V2 = V.Values;
860 Complex[,] M = this.
Values;
861 this.elements =
null;
864 for (i = 0; i < this.columns; i++)
875 if (Column < 0 || Column >= this.columns)
879 throw new ScriptException(
"Vector dimension does not match number of rows");
882 throw new ScriptException(
"Column vectors in a Complex matrix must be Complex vectors.");
884 Complex[] V2 = V.Values;
885 Complex[,] M = this.
Values;
886 this.elements =
null;
889 for (i = 0; i < this.rows; i++)
890 M[i, Column] = V2[i];
903 return this.
TryFind(Element, 0, 0, out Column, out Row);
919 return this.
TryFind(Z.Value, FromColumn, FromRow, out Column, out Row);
938 public bool TryFind(Complex
Element,
int FromColumn,
int FromRow, out
int Column, out
int Row)
942 while (FromRow < this.rows)
944 while (FromColumn < this.columns)
977 return this.
TryFindLast(Element, this.columns - 1, this.rows - 1, out Column, out Row);
993 return this.
TryFindLast(Z.Value, FromColumn, FromRow, out Column, out Row);
1016 while (FromRow >= 0)
1018 while (FromColumn >= 0)
1022 Column = FromColumn;
1031 FromColumn = this.columns - 1;
Node referencing a chunk in a ChunkedList<T>
ChunkNode< T > Next
Next chunk
int Pos
Index after the last element in chunk.
int Start
Index of first element in chunk.
A chunked list is a linked list of chunks of objects of type T .
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.
override IElement Encapsulate(ChunkedList< IElement > Elements, ScriptNode Node)
Encapsulates a set of elements into a similar structure as that provided by the current element.
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.
bool TryFindLast(Complex Element, int FromColumn, int FromRow, out int Column, out int Row)
Tries to find the last element in the matrix, continuing search from a given position in the matrix....
void SetRow(int Row, IVector Vector)
Gets a row vector from the matrix.
bool TryFind(Complex Element, int FromColumn, int FromRow, out int Column, out int Row)
Tries to find an element in the matrix, continuing search from a given position in the matrix....
bool TryFindLast(IElement Element, int FromColumn, int FromRow, out int Column, out int Row)
Tries to find the last element in the matrix, continuing search from a given position in the matrix....
bool TryFind(IElement Element, int FromColumn, int FromRow, out int Column, out int Row)
Tries to find an element in the matrix, continuing search from a given position in 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.
bool TryFindLast(IElement Element, out int Column, out int Row)
Tries to find the last element in the matrix. Search is done, right to left, bottom to top.
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.
bool TryFind(IElement Element, out int Column, out int Row)
Tries to find an element in the matrix. Search is done, left to right, top to bottom.
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.