public static Vector Flatten(this MatrixByArr mat) { #region self-test if (HDebug.IsDebuggerAttached && selftest_Flatten) { selftest_Flatten = false; MatrixByArr tmat = new double[, ] { { 1, 2 }, { 3, 4 } }; Vector vec0 = new double[] { 1, 2, 3, 4 }; Vector vec1 = tmat.Flatten(); HDebug.AssertAnd(vec0 == vec1); } #endregion int ColSize = mat.ColSize; int RowSize = mat.RowSize; Vector vec = new Vector(ColSize * RowSize); for (int i = 0; i < vec.Size; i++) { int c = i / ColSize; int r = i % ColSize; vec[i] = mat[c, r]; } return(vec); }
public static Vector ClosestPointOnLine(Vector A, Vector B, Vector P, bool segmentClamp) { // http://www.gamedev.net/community/forums/topic.asp?topic_id=444154 HDebug.AssertAnd(A.Size == B.Size, B.Size == P.Size); Vector AP = P - A; Vector AB = B - A; double ab2 = LinAlg.VtV(AB, AB);// AB.x*AB.x + AB.y*AB.y; if (ab2 == 0) { return(A.Clone()); } double ap_ab = LinAlg.VtV(AP, AB); //AP.x*AB.x + AP.y*AB.y; double t0 = ap_ab / ab2; double t = t0; if (segmentClamp) { if (t < 0.0) { t = 0.0; } else if (t > 1.0) { t = 1.0; } } Vector Closest = A + AB * t; HDebug.AssertTolerance(0.000000001, LinAlg.VtV(B - A, P - (A + AB * t0))); return(Closest); }
public virtual Vector GetRowVector(int col) { HDebug.AssertAnd(0 <= col, col < ColSize); double[] vec = new double[RowSize]; for (int row = 0; row < RowSize; row++) { vec[row] = this[col, row]; } return(vec); }
public virtual Vector GetColVector(int row) { HDebug.AssertAnd(0 <= row, row < RowSize); double[] vec = new double[ColSize]; for (int col = 0; col < ColSize; col++) { vec[col] = this[col, row]; } return(vec); }
public static Vector[] ClosestPointOnLine(IList <Vector> As, IList <Vector> Bs, IList <Vector> Ps, bool segmentClamp) { HDebug.AssertAnd(As.Count == Bs.Count, Bs.Count == Ps.Count); Vector[] closests = new Vector[As.Count]; for (int i = 0; i < As.Count; i++) { closests[i] = ClosestPointOnLine(As[i], Bs[i], Ps[i], segmentClamp); } return(closests); }
public static List <Vector> ClosestPointOnLine2(IList <Vector> LineBase, IList <Vector> LineDirect, IList <Vector> Points, bool segmentClamp) { HDebug.AssertAnd(LineBase.Count == LineDirect.Count, LineDirect.Count == Points.Count); List <Vector> closests = new List <Vector>(LineBase.Count); for (int i = 0; i < LineBase.Count; i++) { closests.Add(ClosestPointOnLine(LineBase[i], LineBase[i] + LineDirect[i], Points[i], segmentClamp)); } HDebug.Assert(closests.Count == LineBase.Count); return(closests); }
public VectorSparse <T> GetRowVector(int col) { HDebug.AssertAnd(0 <= col, col < RowSize); VectorSparse <T> vec = new VectorSparse <T>(RowSize, GetDefault: GetDefault); if (diagonal[col] != null) { vec[col] = diagonal[col]; } foreach (var r_val in offdiagonal[col]) { int r = r_val.Key; T val = r_val.Value; vec[r] = val; } return(vec); }
public static Vector HReshapeToColVector(this IMatrix <double> _this) { if (HDebug.IsDebuggerAttached && selftest_ToColVector) #region self-test { selftest_ToColVector = false; MatrixByArr mat = new double[, ] { { 1, 2 }, { 3, 4 } }; Vector colvec0 = new double[] { 1, 3, 2, 4 }; Vector colvec1 = mat.HReshapeToColVector(); HDebug.AssertAnd(colvec0 == colvec1); } #endregion Vector vec = new double[_this.ColSize * _this.RowSize]; for (int c = 0; c < _this.ColSize; c++) { for (int r = 0; r < _this.RowSize; r++) { vec[c + r * _this.ColSize] = _this[c, r]; } } return(vec); }