2using System.Collections.Generic;
19 private ICollection<IElement> elements;
20 private string[] columnNames =
null;
21 private readonly
int rows;
22 private readonly
int columns;
32 this.rows =
Values.GetLength(0);
33 this.columns =
Values.GetLength(1);
45 for (i = 0; i < c; i++)
47 for (j = 0; j < d; j++)
51 this.values = Values2;
53 this.rows =
Values.GetLength(0);
54 this.columns =
Values.GetLength(1);
78 if (this.values is
null)
87 if (x >= this.columns)
113 if (this.elements is
null)
118 for (y = 0; y < this.rows; y++)
120 for (x = 0; x < this.columns; x++)
121 v[i++] = this.values[y, x];
127 return this.elements;
146 get => this.columnNames;
149 if (!(value is
null) && value.Length !=
this.columns)
150 throw new ArgumentException(
"Number of columns does not match actual number of columns.", nameof(value));
152 this.columnNames = value;
171 if (!(this.columnNames is
null))
173 int i, c = this.columnNames.Length;
175 for (i = 0; i < c; i++)
177 if (
string.Compare(this.columnNames[i], ColumnName,
true) == 0)
182 throw new ArgumentException(
"No column named " + ColumnName +
" found.");
190 StringBuilder sb =
null;
194 for (y = 0; y < this.rows; y++)
197 sb =
new StringBuilder(
"[[");
199 sb.Append(
",\r\n [");
202 for (x = 0; x < this.columns; x++)
216 sb =
new StringBuilder(
"[[]]");
220 return sb.ToString();
230 if (this.associatedMatrixSpace is
null)
231 this.associatedMatrixSpace =
new ObjectMatrices(this.rows, this.columns);
233 return this.associatedMatrixSpace;
258 v =
new IElement[this.rows, this.columns];
260 for (y = 0; y < this.rows; y++)
262 for (x = 0; x < this.columns; x++)
270 if (Matrix.Columns !=
this.rows)
273 IElement[,] Values2 = Matrix.MatrixElements;
275 v =
new IElement[Matrix.Rows, this.columns];
277 for (y = 0; y < Matrix.Rows; y++)
279 for (x = 0; x < this.columns; x++)
283 for (z = 0; z < this.rows; z++)
289 n = Operators.Arithmetics.Add.EvaluateAddition(n,
318 v =
new IElement[this.rows, this.columns];
320 for (y = 0; y < this.rows; y++)
322 for (x = 0; x < this.columns; x++)
330 if (this.columns != Matrix.Rows)
333 IElement[,] Values2 = Matrix.MatrixElements;
335 v =
new IElement[this.rows, Matrix.Columns];
337 for (y = 0; y < this.rows; y++)
339 for (x = 0; x < Matrix.Columns; x++)
343 for (z = 0; z < this.columns; z++)
349 n = Operators.Arithmetics.Add.EvaluateAddition(n,
370 if (this.rows != this.columns)
374 int c2 = this.columns << 1;
379 for (y = 0; y < this.rows; y++)
381 for (x = 0; x < this.columns; x++)
383 v[y, x] = E =
Values[y, x];
387 v[y, x + this.columns] = (x == y ? E2.One : E2.Zero);
391 if (
Reduce(v,
true,
true, out _) < 0)
396 for (y = 0; y < this.rows; y++)
398 for (x = 0; x < this.columns; x++)
399 v2[y, x] = v[y, x + this.columns];
419 Rank =
Reduce(M, Eliminate, BreakIfZero, out Factor);
437 int Rows = Matrix.GetLength(0);
438 int Columns = Matrix.GetLength(1);
447 for (x = 0; x < MinCount; x++)
453 if (a.Equals(a.
Zero))
456 for (y = x + 1; y <
Rows; y++)
462 if (!b.Equals(b.
Zero))
475 Matrix[x, u] = Matrix[z, u];
507 for (y = Eliminate ? 0 : x + 1; y <
Rows; y++)
515 if (!a.Equals(a.
Zero))
553 v =
new IElement[this.rows, this.columns];
555 for (y = 0; y < this.rows; y++)
557 for (x = 0; x < this.columns; x++)
558 v[y, x] = Operators.Arithmetics.Add.EvaluateAddition(
Element,
this.values[y, x],
null);
565 if (this.columns != Matrix.Columns ||
this.rows != Matrix.Rows)
568 IElement[,] Values2 = Matrix.MatrixElements;
570 v =
new IElement[this.rows, this.columns];
571 for (y = 0; y < this.rows; y++)
573 for (x = 0; x < this.columns; x++)
574 v[y, x] = Operators.Arithmetics.Add.EvaluateAddition(
Values[y, x], Values2[y, x],
null);
590 IElement[,] v =
new IElement[this.rows, this.columns];
593 for (y = 0; y < this.rows; y++)
595 for (x = 0; x < this.columns; x++)
596 v[y, x] = Operators.Arithmetics.Negate.EvaluateNegation(
Values[y, x]);
612 if (this.columns != Matrix.Columns ||
this.rows != Matrix.Rows)
616 IElement[,] V2 = Matrix.MatrixElements;
619 for (y = 0; y < this.rows; y++)
621 for (x = 0; x < this.columns; x++)
623 if (V1[y, x] != V2[y, x])
640 for (y = 0; y < this.rows; y++)
642 for (x = 0; x < this.columns; x++)
675 get {
throw new ScriptException(
"Zero element not defined for generic object matrices."); }
690 if (!(this.rowVectors is
null))
691 return this.rowVectors;
693 LinkedList<IElement>
Rows =
new LinkedList<IElement>();
696 if (!(this.values is
null))
698 for (y = 0; y < this.rows; y++)
700 LinkedList<IElement> Row =
new LinkedList<IElement>();
702 for (x = 0; x < this.columns; x++)
703 Row.AddLast(
this.values[y, x]);
705 Rows.AddLast(Operators.Vectors.VectorDefinition.Encapsulate(Row,
false,
null));
710 LinkedList<IElement> Row =
null;
716 Row =
new LinkedList<IElement>();
720 if (x >= this.columns)
722 Rows.AddLast(Operators.Vectors.VectorDefinition.Encapsulate(Row,
false,
null));
729 Rows.AddLast(Operators.Vectors.VectorDefinition.Encapsulate(Row,
false,
null));
732 this.rowVectors =
Rows;
737 private LinkedList<IElement> rowVectors =
null;
749 for (y = 0; y < this.rows; y++)
751 for (x = 0; x < this.columns; x++)
774 if (Index < 0 || Index >= this.rows)
778 IElement[] V =
new IElement[this.columns];
781 for (i = 0; i < this.columns; i++)
784 return Operators.Vectors.VectorDefinition.
Encapsulate(V,
false,
null);
794 if (Index < 0 || Index >= this.rows)
800 if (V.Dimension !=
this.columns)
804 this.elements =
null;
820 if (Column < 0 || Column >= this.columns || Row < 0 || Row >= this.rows)
823 return this.
Values[Row, Column];
834 if (Column < 0 || Column >= this.columns || Row < 0 || Row >= this.rows)
838 this.elements =
null;
840 M[Row, Column] = Value;
850 if (Row < 0 || Row >= this.rows)
854 IElement[] V =
new IElement[this.columns];
857 for (i = 0; i < this.columns; i++)
860 return Operators.Vectors.VectorDefinition.
Encapsulate(V,
false,
null);
870 if (Column < 0 || Column >= this.columns)
874 IElement[] V =
new IElement[this.rows];
877 for (i = 0; i < this.rows; i++)
880 return Operators.Vectors.VectorDefinition.
Encapsulate(V,
false,
null);
890 if (Row < 0 || Row >= this.rows)
894 throw new ScriptException(
"Vector dimension does not match number of columns");
897 this.elements =
null;
900 foreach (
IElement E
in Vector.VectorElements)
911 if (Column < 0 || Column >= this.columns)
915 throw new ScriptException(
"Vector dimension does not match number of rows");
918 this.elements =
null;
921 foreach (
IElement E
in Vector.VectorElements)
Base class for all types of elements.
virtual bool IsScalar
If the element represents a scalar value.
Element()
Base class for all types of elements.
Base class for all types of ring elements.
Base class for script exceptions.
Class managing a script expression.
static IElement Encapsulate(object Value)
Encapsulates an object.
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 Object-valued matrices.
IVector GetColumn(int Column)
Gets a column vector from the matrix.
override string ToString()
IElement GetElement(int Index)
Gets an element of the vector.
IMatrix ConjugateTranspose()
Returns a conjugate transposed matrix.
void SetElement(int Column, int Row, IElement Value)
Sets an element in the matrix.
override IRing AssociatedRing
Associated Ring.
override IRingElement MultiplyRight(IRingElement Element)
Tries to multiply an element to the current element, from the right.
bool HasColumnNames
If the matrix has column names defined.
ObjectMatrix(IElement[,] Values)
Object-valued matrix.
override IAbelianGroupElement Add(IAbelianGroupElement Element)
Tries to add an element to the current element.
override IElement Encapsulate(ICollection< IElement > Elements, ScriptNode Node)
Encapsulates a set of elements into a similar structure as that provided by the current element.
IElement GetElement(int Column, int Row)
Gets an element of the matrix.
override object AssociatedObjectValue
Associated object value.
ICollection< IElement > Elements
Matrix elements.
void SetColumn(int Column, IVector Vector)
Gets a column vector from the matrix.
IMatrix Reduce(bool Eliminate, bool BreakIfZero, out int Rank, out ICommutativeRingWithIdentityElement Factor)
Reduces a matrix.
IVector GetRow(int Row)
Gets a row vector from the matrix.
static int Reduce(IElement[,] Matrix, bool Eliminate, bool BreakIfZero, out ICommutativeRingWithIdentityElement Factor)
Reduces a matrix.
ObjectMatrix(object[,] Values)
Object-valued matrix.
override IRingElement Invert()
Inverts the element, if possible.
IElement[,] MatrixElements
Matrix elements
override int GetHashCode()
Calculates a hash code of the element.
ObjectMatrix(int Rows, int Columns, ICollection< IElement > Elements)
Object-valued vector.
override IRingElement MultiplyLeft(IRingElement Element)
Tries to multiply an element to the current element, from the left.
override IAbelianGroupElement Zero
Returns the zero element of the group.
int Columns
Number of columns.
void SetElement(int Index, IElement Value)
Sets an element in the vector.
IElement[,] Values
Matrix element values.
ICollection< IElement > VectorElements
Vector of row vectors.
override bool Equals(object obj)
Compares the element to another.
override ICollection< IElement > ChildElements
An enumeration of child elements. If the element is a scalar, this property will return null.
string[] ColumnNames
Contains optional column names.
override IGroupElement Negate()
Negates the element.
override bool IsScalar
If the element represents a scalar value.
int Dimension
Dimension of matrix, if seen as a vector of row vectors.
IMatrix Transpose()
Returns a transposed matrix.
void SetRow(int Row, IVector Vector)
Gets a row vector from the matrix.
static IElement EvaluateDivision(IElement Left, IElement Right, ScriptNode Node)
Divides the right operand from the left one.
static IElement EvaluateMultiplication(IElement Left, IElement Right, ScriptNode Node)
Multiplies two operands.
static IElement EvaluateSubtraction(IElement Left, IElement Right, ScriptNode Node)
Subtracts the right operand from the left one.
static IMatrix Encapsulate(ICollection< IElement > Rows, ScriptNode Node)
Encapsulates the elements of a matrix.
Basic interface for all types of abelian group elements.
IAbelianGroupElement Zero
Returns the zero element of the group.
Basic interface for all types of commutative ring with identity elements.
ICommutativeRingWithIdentityElement One
Returns the identity element of the commutative ring with identity.
Basic interface for all types of elements.
ICollection< IElement > ChildElements
An enumeration of child elements. If the element is a scalar, this property will return null.
IElement Encapsulate(ICollection< IElement > Elements, ScriptNode Node)
Encapsulates a set of elements into a similar structure as that provided by the current element.
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.