19 private double[,] 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);
47 this.matrixElements =
null;
60 if (this.values is
null)
62 double[,] v =
new double[this.rows, this.columns];
71 while (!(Loop is
null))
73 for (i = Loop.
Start, c = Loop.
Pos; i < c; i++)
79 if (x >= this.columns)
91 foreach (
IElement E
in this.elements)
97 if (x >= this.columns)
119 if (this.elements is
null)
124 for (y = 0; y < this.rows; y++)
126 for (x = 0; x < this.columns; x++)
133 return this.elements;
144 if (this.matrixElements is
null)
153 if (x >= this.columns)
160 this.matrixElements = v;
163 return this.matrixElements;
180 double[,] v = this.
Values;
181 StringBuilder sb =
null;
185 for (y = 0; y < this.rows; y++)
188 sb =
new StringBuilder(
"[[");
190 sb.Append(
",\r\n [");
193 for (x = 0; x < this.columns; x++)
207 sb =
new StringBuilder(
"[[]]");
211 return sb.ToString();
221 if (this.associatedMatrixSpace is
null)
222 this.associatedMatrixSpace =
new DoubleMatrices(this.rows, this.columns);
224 return this.associatedMatrixSpace;
249 v =
new double[this.rows, this.columns];
251 for (y = 0; y < this.rows; y++)
253 for (x = 0; x < this.columns; x++)
254 v[y, x] = n *
Values[y, x];
261 if (Matrix.columns !=
this.rows)
265 double[,] Values2 = Matrix.Values;
267 v =
new double[Matrix.rows, this.columns];
268 for (y = 0; y < Matrix.rows; y++)
270 for (x = 0; x < this.columns; x++)
274 for (z = 0; z < this.rows; z++)
275 n += Values2[y, z] *
Values[z, x];
303 v =
new double[this.rows, this.columns];
305 for (y = 0; y < this.rows; y++)
307 for (x = 0; x < this.columns; x++)
308 v[y, x] = n *
Values[y, x];
315 if (this.columns != Matrix.rows)
319 double[,] Values2 = Matrix.Values;
321 v =
new double[this.rows, Matrix.columns];
322 for (y = 0; y < this.rows; y++)
324 for (x = 0; x < Matrix.columns; x++)
328 for (z = 0; z < this.columns; z++)
329 n +=
Values[y, z] * Values2[z, x];
349 if (this.rows != this.columns)
353 int c2 = this.columns << 1;
354 double[,] v =
new double[this.rows, c2];
357 for (y = 0; y < this.rows; y++)
359 for (x = 0; x < this.columns; x++)
362 v[y, x + this.columns] = (x == y ? 1 : 0);
366 if (
Reduce(v,
true,
true, out
_) < 0)
369 double[,] v2 =
new double[this.rows, this.columns];
371 for (y = 0; y < this.rows; y++)
373 for (x = 0; x < this.columns; x++)
374 v2[y, x] = v[y, x + this.columns];
393 double[,] M = (
double[,])this.
Values.Clone();
394 Rank =
Reduce(M, Eliminate, BreakIfZero, out
double c);
410 public static int Reduce(
double[,] Matrix,
bool Eliminate,
bool BreakIfZero,
414 int Rows = Matrix.GetLength(0);
415 int Columns = Matrix.GetLength(1);
422 for (x = 0; x < MinCount; x++)
424 a = Math.Abs(Matrix[x, x]);
426 for (y = x + 1; y <
Rows; y++)
428 b = Math.Abs(Matrix[y, x]);
441 Matrix[x, u] = Matrix[z, u];
466 for (y = Eliminate ? 0 : x + 1; y <
Rows; y++)
468 if (y != x && (a = Matrix[y, x]) != 0)
471 Matrix[y, u] -= a * Matrix[x, u];
494 v =
new double[this.rows, this.columns];
496 for (y = 0; y < this.rows; y++)
498 for (x = 0; x < this.columns; x++)
499 v[y, x] = d +
Values[y, x];
506 if (this.columns != Matrix.columns ||
this.rows != Matrix.rows)
510 double[,] Values2 = Matrix.Values;
512 v =
new double[this.rows, this.columns];
513 for (y = 0; y < this.rows; y++)
515 for (x = 0; x < this.columns; x++)
516 v[y, x] =
Values[y, x] + Values2[y, x];
534 double[,] v =
new double[this.rows, this.columns];
537 for (y = 0; y < this.rows; y++)
539 for (x = 0; x < this.columns; x++)
555 if (this.columns != Matrix.columns ||
this.rows != Matrix.rows)
558 double[,] V1 = this.
Values;
559 double[,] V2 = Matrix.Values;
562 for (y = 0; y < this.rows; y++)
564 for (x = 0; x < this.columns; x++)
566 if (V1[y, x] != V2[y, x])
589 for (y = 0; y < this.rows; y++)
591 for (x = 0; x < this.columns; x++)
637 if (this.zero is
null)
638 this.zero =
new DoubleMatrix(
new double[this.rows, this.columns]);
658 if (!(this.rowVectors is
null))
659 return this.rowVectors;
661 double[,] v = this.
Values;
666 for (y = 0; y < this.rows; y++)
668 r =
new double[this.columns];
670 for (x = 0; x < this.columns; x++)
676 this.rowVectors =
Rows;
689 double[,] v =
new double[this.columns, this.rows];
693 for (y = 0; y < this.rows; y++)
695 for (x = 0; x < this.columns; x++)
718 if (Index < 0 || Index >= this.rows)
721 double[,] M = this.
Values;
722 double[] V =
new double[this.columns];
725 for (i = 0; i < this.columns; i++)
738 if (Index < 0 || Index >= this.rows)
742 throw new ScriptException(
"Row vectors in a double matrix are required to be double vectors.");
744 if (V.Dimension !=
this.columns)
747 double[] V2 = V.Values;
748 double[,] M = this.
Values;
749 this.elements =
null;
753 for (i = 0; i < this.columns; i++)
765 if (Column < 0 || Column >= this.columns || Row < 0 || Row >= this.rows)
779 if (Column < 0 || Column >= this.columns || Row < 0 || Row >= this.rows)
783 throw new ScriptException(
"Elements in a double matrix must be double values.");
785 double[,] M = this.
Values;
786 this.elements =
null;
798 if (Row < 0 || Row >= this.rows)
801 double[,] M = this.
Values;
802 double[] V =
new double[this.columns];
805 for (i = 0; i < this.columns; i++)
818 if (Column < 0 || Column >= this.columns)
821 double[,] M = this.
Values;
822 double[] V =
new double[this.rows];
825 for (i = 0; i < this.rows; i++)
838 if (Row < 0 || Row >= this.rows)
842 throw new ScriptException(
"Vector dimension does not match number of columns");
845 throw new ScriptException(
"Row vectors in a double matrix must be double vectors.");
847 double[] V2 = V.Values;
848 double[,] M = this.
Values;
849 this.elements =
null;
852 for (i = 0; i < this.columns; i++)
863 if (Column < 0 || Column >= this.columns)
867 throw new ScriptException(
"Vector dimension does not match number of rows");
870 throw new ScriptException(
"Column vectors in a double matrix must be double vectors.");
872 double[] V2 = V.Values;
873 double[,] M = this.
Values;
874 this.elements =
null;
877 for (i = 0; i < this.rows; i++)
878 M[i, Column] = V2[i];
891 return this.
TryFind(Element, 0, 0, out Column, out Row);
907 return this.
TryFind(D.Value, FromColumn, FromRow, out Column, out Row);
926 public bool TryFind(
double Element,
int FromColumn,
int FromRow, out
int Column, out
int Row)
930 while (FromRow < this.rows)
932 while (FromColumn < this.columns)
965 return this.
TryFindLast(Element, this.columns - 1, this.rows - 1, out Column, out Row);
981 return this.
TryFindLast(D.Value, FromColumn, FromRow, out Column, out Row);
1004 while (FromRow >= 0)
1006 while (FromColumn >= 0)
1010 Column = FromColumn;
1019 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 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.
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....
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.
override IElement Encapsulate(ChunkedList< IElement > Elements, ScriptNode Node)
Encapsulates a set of elements into a similar structure as that provided by the current element.
ICollection< IElement > VectorElements
Vector of row vectors.
DoubleMatrix(double[,] Values)
Double-valued 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....
override IRingElement MultiplyLeft(IRingElement Element)
Tries to multiply an element to the current element, from the left.
IMatrix ConjugateTranspose()
Returns a conjugate transposed matrix.
bool TryFind(double 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 IElement Encapsulate(ICollection< IElement > Elements, ScriptNode Node)
Encapsulates a set of elements into a similar structure as that provided by the current element.
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.
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.
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.
override IAbelianGroupElement Zero
Returns the zero element of the group.
bool TryFindLast(double 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....
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.