public SquareMatrix(SquareMatrix m) { #if DEBUG foreach (Vector col in m.Columns) Debug.Assert(m.Columns.Length == col.Length); // Only sqare matrices allowed. #endif Columns = new Vector[m.Columns.Length]; for (int c = 0; c < Columns.Length; ++c) Columns[c] = new Vector(m.Columns[c]); }
private float Lagrange(out SquareMatrix clipped) { if (Length == 2) { clipped = null; return this[0][0] * this[1][1] - this[0][1] * this[1][0]; } else { throw new NotImplementedException(); } }
public SquareMatrix Transposed() { SquareMatrix mat = new SquareMatrix(Length); for (int x = 0; x < Length; ++x) for (int y = 0; y < Length; ++y) { mat[x][y] = this[y][x]; //float tmp = this[x][y]; //this[x][y] = this[y][x]; //this[y][x] = tmp; } return mat; }
public SquareMatrix ToMat2x2() { SquareMatrix mat = new SquareMatrix(2); mat[0] = this[0].ToVec2(); mat[1] = (this.Length > 1) ? this[1].ToVec2() : new Vec2(0); return mat; }
public static SquareMatrix operator -(SquareMatrix a, SquareMatrix b) { Debug.Assert(a.Length == b.Length); SquareMatrix c = new SquareMatrix(a.Length); for (int col = 0; col < c.Length; ++col) c[col] = a[col] - b[col]; return c; }
public void Eigenanalysis(out SquareMatrix eigenvalues, out SquareMatrix eigenvectors) { Debug.Assert(Length == 2, "Only 2D eigenanalysis implemented so far."); eigenvectors = new SquareMatrix(2); eigenvalues = new SquareMatrix(2); float a = this[0][0]; float b = this[1][0]; float c = this[0][1]; float d = this[1][1]; // Computing eigenvalues. float Th = (a + d) * 0.5f; float D = a * d - b * c; float root = Th * Th - D; float complex = 0; if (root < 0) { complex = -root; root = 0; } root = (float)Math.Sqrt(root); float l0 = Th + root; float l1 = Th - root; // Save directional information. eigenvalues[0] = new Vec2(l0, complex); eigenvalues[1] = new Vec2(l1, -complex); // Computing eigenvectors. if (c != 0) { eigenvectors[0] = new Vec2(l0 - d, c); eigenvectors[1] = new Vec2(l1 - d, c); } else if (b != 0) { eigenvectors[0] = new Vec2(b, l0 - a); eigenvectors[1] = new Vec2(b, l1 - a); } else { eigenvectors[0] = new Vec2(1, 0); eigenvectors[1] = new Vec2(0, 1); } }
public static SquareMatrix operator *(SquareMatrix a, SquareMatrix b) { Debug.Assert(a.Length == b.Length); SquareMatrix prod = new SquareMatrix(a.Length); for(int x = 0; x < a.Length; ++x) { Vector row = a.Row(x); for(int y = 0; y < b.Length; ++y) { prod[x][y] = Vector.Dot(row, b[y]); } } return prod; }
public static SquareMatrix operator *(SquareMatrix a, float b) { SquareMatrix c = new SquareMatrix(a.Length); for (int col = 0; col < c.Length; ++col) c[col] = a[col] * b; return c; }
public CriticalPoint2D(Vector3 position, SquareMatrix J) { Position = position; Debug.Assert(J.Length == 2); Eigenvectors = new SquareMatrix(2); Eigenvalues = new ComplexDirection[2]; float a = J[0][0]; float b = J[1][0]; float c = J[0][1]; float d = J[1][1]; // Computing eigenvalues. float Th = (a - d) * 0.5f; float D = a * d - b * c; float root = Th * Th - D; bool complex = false; if (root < 0) { complex = true; root = 0; } else { complex = false; } root = (float)Math.Sqrt(root); float l0 = Th + root; float l1 = Th - root; // Save directional information. if (l0 >= 0) { Eigenvalues[0] = complex ? ComplexDirection.POSITIVE_COMPLEX : ComplexDirection.POSITIVE; } else { Eigenvalues[0] = complex ? ComplexDirection.NEGATIVE_COMPLEX : ComplexDirection.NEGATIVE; } if (l1 >= 0) { Eigenvalues[1] = complex ? ComplexDirection.POSITIVE_COMPLEX : ComplexDirection.POSITIVE; } else { Eigenvalues[1] = complex ? ComplexDirection.NEGATIVE_COMPLEX : ComplexDirection.NEGATIVE; } // Computing eigenvectors. if (c != 0) { Eigenvectors[0] = new Vec2(l0 - d, c); Eigenvectors[1] = new Vec2(l1 - d, c); } else if (b != 0) { Eigenvectors[0] = new Vec2(b, l0 - a); Eigenvectors[1] = new Vec2(b, l1 - a); } else { Eigenvectors[0] = new Vec2(1, 0); Eigenvectors[1] = new Vec2(0, 1); } // Derive the critical points type now. SetType(); }
public FloatCP2D(Vector3 position, SquareMatrix J, float value) : base(position, J) { Value = value; }
/// <summary> /// Get the derivative at a data point. Not checking for InvalidValue. /// </summary> /// <param name="pos"></param> /// <returns></returns> public SquareMatrix SampleDerivative(Index pos) { //Debug.Assert(NumVectorDimensions == Size.Length); int size = Math.Max(Size.Length, NumVectorDimensions); SquareMatrix jacobian = new SquareMatrix(size); // For all dimensions, so please reset each time. Index samplePos = new Index(pos); for (int dim = 0; dim < Size.Length; ++dim) { // Just to be sure, check thst no value was overwritten. int posCpy = samplePos[dim]; // See whether a step to the right/left is possible. samplePos[dim]++; bool rightValid = (samplePos[dim] < Size[dim]) && Scalars[0].Sample(samplePos) != InvalidValue; samplePos[dim] -= 2; bool leftValid = (samplePos[dim] >= 0) && Scalars[0].Sample(samplePos) != InvalidValue; samplePos[dim]++; if (rightValid) { if (leftValid) { // Regular case. Interpolate. samplePos[dim]++; jacobian[dim] = Sample(samplePos).ToVec(size); samplePos[dim] -= 2; jacobian[dim] -= Sample(samplePos).ToVec(size); jacobian[dim] *= 0.5f; samplePos[dim]++; } else { // Left border. samplePos[dim]++; jacobian[dim] = Sample(samplePos).ToVec(size); samplePos[dim]--; jacobian[dim] -= Sample(samplePos).ToVec(size); } } else { if (leftValid) { // Right border. jacobian[dim] = Sample(samplePos).ToVec(size); samplePos[dim]--; jacobian[dim] -= Sample(samplePos).ToVec(size); samplePos[dim]++; } else { // Weird case. jacobian[dim] = new Vector(0, size); } } Debug.Assert(posCpy == samplePos[dim]); } return(jacobian); }