/// <summary> /// Creates an analyis from preprocessed spectra and preprocessed concentrations. /// </summary> /// <param name="matrixX">The spectral matrix (each spectrum is a row in the matrix). They must at least be centered.</param> /// <param name="matrixY">The matrix of concentrations (each experiment is a row in the matrix). They must at least be centered.</param> /// <param name="maxFactors">Maximum number of factors for analysis.</param> /// <returns>A regression object, which holds all the loads and weights neccessary for further calculations.</returns> protected override void AnalyzeFromPreprocessedWithoutReset(IROMatrix matrixX, IROMatrix matrixY, int maxFactors) { int numberOfFactors = _calib.NumberOfFactors = Math.Min(matrixX.Columns, maxFactors); IMatrix helperY = new MatrixMath.BEMatrix(matrixY.Rows, 1); _PRESS = null; for (int i = 0; i < matrixY.Columns; i++) { MatrixMath.Submatrix(matrixY, helperY, 0, i); PLS2Regression r = PLS2Regression.CreateFromPreprocessed(matrixX, helperY, maxFactors); IPLS2CalibrationModel cal = r.CalibrationModel; _calib.NumberOfFactors = Math.Min(_calib.NumberOfFactors, cal.NumberOfFactors); _calib.XLoads[i] = cal.XLoads; _calib.YLoads[i] = cal.YLoads; _calib.XWeights[i] = cal.XWeights; _calib.CrossProduct[i] = cal.CrossProduct; if (_PRESS == null) { _PRESS = VectorMath.CreateExtensibleVector(r.PRESS.Length); } VectorMath.Add(_PRESS, r.PRESS, _PRESS); } }
/// <summary> /// Translates the vertices with the specified vector. /// </summary> /// <param name="vector">The vector.</param> public void Translate(ref Vector2 vector) { for (int i = 0; i < Count; i++) { this[i] = VectorMath.Add(this[i], vector); } }
static void Main(string[] args) { double[] vector1 = { 8, 11 }; double[] vector2 = { -4, 9 }; Console.WriteLine($"{VectorToString(vector1)} + {VectorToString(vector2)}"); Console.WriteLine($"{VectorToString(VectorMath.Add(vector1, vector2))}"); double[] vector3 = { 14, -2, 0 }; double[] vector4 = { -3, 23, 50 }; Console.WriteLine($"{VectorToString(vector3)} + {VectorToString(vector4)}"); Console.WriteLine($"{VectorToString(VectorMath.Add(vector3, vector4))}"); Console.WriteLine($"{VectorToString(vector2)} + {VectorToString(vector4)}"); Console.WriteLine($"{VectorToString(VectorMath.Add(vector2, vector4))}"); double[] vector5 = { 14, -2, 0, 2 }; double[] vector6 = { -3, 23, 50, 9 }; Console.WriteLine($"{VectorToString(vector5)} + {VectorToString(vector6)}"); Console.WriteLine($"{VectorToString(VectorMath.Add(vector5, vector6))}"); double[] vector7 = { 14 }; double[] vector8 = { -3 }; Console.WriteLine($"{VectorToString(vector7)} + {VectorToString(vector8)}"); Console.WriteLine($"{VectorToString(VectorMath.Add(vector7, vector8))}"); }
public static float DistanceBetweenPointAndLineSegment(ref Vector2 point, ref Vector2 lineEndPoint1, ref Vector2 lineEndPoint2) { Vector2 v = VectorMath.Subtract(lineEndPoint2, lineEndPoint1); Vector2 w = VectorMath.Subtract(point, lineEndPoint1); float c1 = Vector2.Dot(w, v); if (c1 <= 0) { return(DistanceBetweenPointAndPoint(ref point, ref lineEndPoint1)); } float c2 = Vector2.Dot(v, v); if (c2 <= c1) { return(DistanceBetweenPointAndPoint(ref point, ref lineEndPoint2)); } float b = c1 / c2; Vector2 pointOnLine = VectorMath.Add(lineEndPoint1, VectorMath.Multiply(v, b)); return(DistanceBetweenPointAndPoint(ref point, ref pointOnLine)); }
/// <summary> /// Translates the control points by the specified vector. /// </summary> /// <param name="vector">The vector.</param> public void Translate(ref Vector2 vector) { for (int i = 0; i < ControlPoints.Count; i++) { ControlPoints[i] = VectorMath.Add(ControlPoints[i], vector); } }
static void Main() { double[] vector1 = new double[] { 1.0, 2.0 }; double[] vector2 = new double[] { -3.0, -4.0 }; Console.WriteLine("{0}", VectorMath.Add(vector1, vector2)); }
public void AddReturnsVectorSum(double ax, double ay, double az, double bx, double by, double bz, double cx, double cy, double cz) { Vector3 a = new Vector3(ax, ay, az); Vector3 b = new Vector3(bx, by, bz); Vector3 c = VectorMath.Add(a, b); Assert.Equal(cx, c.X); Assert.Equal(cy, c.Y); Assert.Equal(cz, c.Z); }
static void Main(string[] args) { double[] vector1 = { 2.3, 1.5, 2 }; double[] vector2 = { 1, 3, 1 }; double[] new_vector = VectorMath.Add(vector1, vector2); for (int i = 0; i < new_vector.Length; i++) { Console.WriteLine(new_vector[i]); } }
public static void Main(string[] args) { double[] vector1 = { 14, -2, 0 }; double[] vector2 = { -3, 23, 50 }; double[] result = VectorMath.Add(vector1, vector2); Console.WriteLine("{0}", result[0]); Console.WriteLine("{0}", result[1]); Console.WriteLine("{0}", result[2]); }
static void Main(string[] args) { double[] vector1 = { 14, -2, 0 }; double[] vector2 = { -3, 23, 50 }; double[] result = VectorMath.Add(vector1, vector2); Console.WriteLine("(" + String.Join(", ", result) + ")"); double[] vector3 = { 14, -2 }; double[] vector4 = { -3, 23 }; result = VectorMath.Add(vector3, vector4); Console.WriteLine("(" + String.Join(", ", result) + ")"); }
static void Main(string[] args) { Console.WriteLine("Hello Vectors!"); Vector3 a = new Vector3(10, 10, 10); Vector3 b = new Vector3(4, 5, 6); Vector3 c = VectorMath.Add(a, b); Console.WriteLine("The vector <" + c.X + "," + c.Y + "," + c.Z + ">"); a.X = -10; c = VectorMath.Subtract(a, b); Console.WriteLine($"The vector <{c.X},{c.Y},{c.Z}>"); }
private static List <IVec3> Chaikin(List <IVec3> pts) { IVec3[] newPts = new IVec3[(pts.Count - 2) * 2 + 2]; newPts[0] = pts[0]; newPts[newPts.Length - 1] = pts[pts.Count - 1]; int j = 1; for (int i = 0; i < pts.Count - 2; i++) { newPts[j] = VectorMath.Add(pts[i], VectorMath.Scale(VectorMath.Subtract(pts[i + 1], pts[i]), 0.75f)); newPts[j + 1] = VectorMath.Add(pts[i + 1], VectorMath.Scale(VectorMath.Subtract(pts[i + 2], pts[i + 1]), 0.25f)); j += 2; } return(newPts.ToList()); }
static void Main(string[] args) { double[] vector = { 14, -2, 0 }; double[] vector1 = { -3, 23, 50 }; double[] vector2 = { 8, -11 }; double[] vector3 = { -4, 9 }; double[] res; double[] res1; res = VectorMath.Add(vector, vector1); res1 = VectorMath.Add(vector2, vector3); foreach (double x in res) { Console.WriteLine(x); } Console.WriteLine(); foreach (double y in res1) { Console.WriteLine(y); } }
public static void Main() { double[] vector1 = new double[] { 3.0, 9.0, -12 }; double[] vector2 = new double[] { 7.0, -3.0, 9.0 }; double[] sumVector = {}; sumVector = VectorMath.Add(vector1, vector2); Console.Write("Resulting vector is: { "); for (int i = 0; i < sumVector.Length; i++) { Console.Write($"{sumVector[i]} "); } Console.WriteLine("}"); double[] vector3 = new double[] { 7.0, -3.0 }; sumVector = VectorMath.Add(vector1, vector3); Console.WriteLine($"It's not the same sizes: \n {{ {sumVector[0]} }}"); double[] vector4 = new double[] { 7.0, -3.0, 1, 4 }; double[] vector5 = new double[] { 10.0, -13.0, 3, 2 }; sumVector = VectorMath.Add(vector1, vector3); Console.WriteLine($"It's not a 2D or 3D vector: \n {{ {sumVector[0]} }}"); }
static void Main(string[] args) { var s = VectorMath.Add(new double[] { 1, 1, 1 }, new double[] { 1, 1, 1 }); Console.WriteLine("{0},{1},{2}", s[0], s[1], s[2]); }
public static IVec3 Lerp(IVec3 from, IVec3 to, float t) { IVec3 dir = VectorMath.Subtract(to, from); return(VectorMath.Add(from, VectorMath.Scale(dir, t))); }
static void Main(string[] args) { double[] sum = VectorMath.Add(new double[] { 3, 9 }, new double[] { 1, 1 }); Console.WriteLine("Sum is ({0}, {1})", sum[0], sum[1]); }
/// <summary> /// Execute predictor-corrector scheme for Nordsieck's method /// </summary> /// <param name="flag"></param> /// <param name="f">Evaluation of the deriatives. First argument is time, second arg are the state variables, and 3rd arg is the array to accomodate the derivatives.</param> /// <param name="denseJacobianEvaluation">Evaluation of the jacobian.</param> /// <param name="sparseJacobianEvaluation">Evaluation of the jacobian as a sparse matrix. Either this or the previous arg must be valid.</param> /// <param name="opts">current options</param> /// <returns>en - current error vector</returns> internal void PredictorCorrectorScheme( ref bool flag, Action <double, double[], double[]> f, Func <double, double[], IROMatrix <double> > denseJacobianEvaluation, Func <double, double[], SparseDoubleMatrix> sparseJacobianEvaluation, GearsBDFOptions opts ) { NordsieckState currstate = this; NordsieckState newstate = this; int n = currstate._xn.Length; VectorMath.Copy(currstate._en, ecurr); VectorMath.Copy(currstate._xn, xcurr); var x0 = currstate._xn; MatrixMath.Copy(currstate._zn, zcurr); // zcurr now is old nordsieck matrix var qcurr = currstate._qn; // current degree var qmax = currstate._qmax; // max degree var dt = currstate._dt; var t = currstate._tn; MatrixMath.Copy(currstate._zn, z0); // save Nordsieck matrix //Tolerance computation factors double Cq = Math.Pow(qcurr + 1, -1.0); double tau = 1.0 / (Cq * Factorial(qcurr) * l[qcurr - 1][qcurr]); int count = 0; double Dq = 0.0, DqUp = 0.0, DqDown = 0.0; double delta = 0.0; //Scaling factors for the step size changing //with new method order q' = q, q + 1, q - 1, respectively double rSame, rUp, rDown; if (null != denseJacobianEvaluation) { var J = denseJacobianEvaluation(t + dt, xcurr); if (J.GetType() != P?.GetType()) { AllocatePMatrixForJacobian(J); } do { MatrixMath.MapIndexed(J, dt * b[qcurr - 1], (i, j, aij, factor) => (i == j ? 1 : 0) - aij * factor, P, Zeros.AllowSkip); // P = Identity - J*dt*b[qcurr-1] VectorMath.Copy(xcurr, xprev); f(t + dt, xcurr, ftdt); MatrixMath.CopyColumn(z0, 1, colExtract); // 1st derivative/dt VectorMath.Map(ftdt, colExtract, ecurr, dt, (ff, c, e, local_dt) => local_dt * ff - c - e, gm); // gm = dt * f(t + dt, xcurr) - z0.GetColumn(1) - ecurr; gaussSolver.SolveDestructive(P, gm, tmpVec1); VectorMath.Add(ecurr, tmpVec1, ecurr); // ecurr = ecurr + P.SolveGE(gm); VectorMath.Map(x0, ecurr, b[qcurr - 1], (x, e, local_b) => x + e * local_b, xcurr); // xcurr = x0 + b[qcurr - 1] * ecurr; //Row dimension is smaller than zcurr has int M_Rows = ecurr.Length; int M_Columns = l[qcurr - 1].Length; //So, "expand" the matrix MatrixMath.MapIndexed(z0, (i, j, z) => z + (i < M_Rows && j < M_Columns ? ecurr[i] * l[qcurr - 1][j] : 0.0d), zcurr); Dq = ToleranceNorm(ecurr, opts.RelativeTolerance, opts.AbsoluteTolerance, xprev); var factor_deltaE = (1.0 / (qcurr + 2) * l[qcurr - 1][qcurr - 1]); VectorMath.Map(ecurr, currstate._en, factor_deltaE, (e, c, factor) => (e - c) * factor, deltaE); // deltaE = (ecurr - currstate.en)*(1.0 / (qcurr + 2) * l[qcurr - 1][qcurr - 1]) DqUp = ToleranceNorm(deltaE, opts.RelativeTolerance, opts.AbsoluteTolerance, xcurr); zcurr.CopyColumn(qcurr - 1, colExtract); DqDown = ToleranceNorm(colExtract, opts.RelativeTolerance, opts.AbsoluteTolerance, xcurr); delta = Dq / (tau / (2 * (qcurr + 2))); count++; } while (delta > 1.0d && count < opts.NumberOfIterations); } else if (null != sparseJacobianEvaluation) { SparseDoubleMatrix J = sparseJacobianEvaluation(t + dt, xcurr); var P = new SparseDoubleMatrix(J.RowCount, J.ColumnCount); do { J.MapSparseIncludingDiagonal((x, i, j) => (i == j ? 1 : 0) - x * dt * b[qcurr - 1], P); VectorMath.Copy(xcurr, xprev); f(t + dt, xcurr, ftdt); MatrixMath.CopyColumn(z0, 1, colExtract); VectorMath.Map(ftdt, colExtract, ecurr, (ff, c, e) => dt * ff - c - e, gm); // gm = dt * f(t + dt, xcurr) - z0.GetColumn(1) - ecurr; gaussSolver.SolveDestructive(P, gm, tmpVec1); VectorMath.Add(ecurr, tmpVec1, ecurr); // ecurr = ecurr + P.SolveGE(gm); VectorMath.Map(x0, ecurr, (x, e) => x + e * b[qcurr - 1], xcurr); // xcurr = x0 + b[qcurr - 1] * ecurr; //Row dimension is smaller than zcurr has int M_Rows = ecurr.Length; int M_Columns = l[qcurr - 1].Length; //So, "expand" the matrix MatrixMath.MapIndexed(z0, (i, j, z) => z + (i < M_Rows && j < M_Columns ? ecurr[i] * l[qcurr - 1][j] : 0.0d), zcurr); Dq = ToleranceNorm(ecurr, opts.RelativeTolerance, opts.AbsoluteTolerance, xprev); var factor_deltaE = (1.0 / (qcurr + 2) * l[qcurr - 1][qcurr - 1]); VectorMath.Map(ecurr, currstate._en, (e, c) => (e - c) * factor_deltaE, deltaE); // deltaE = (ecurr - currstate.en)*(1.0 / (qcurr + 2) * l[qcurr - 1][qcurr - 1]) DqUp = ToleranceNorm(deltaE, opts.RelativeTolerance, opts.AbsoluteTolerance, xcurr); DqDown = ToleranceNorm(zcurr.GetColumn(qcurr - 1), opts.RelativeTolerance, opts.AbsoluteTolerance, xcurr); delta = Dq / (tau / (2 * (qcurr + 2))); count++; } while (delta > 1.0d && count < opts.NumberOfIterations); } else // neither denseJacobianEvaluation nor sparseJacobianEvaluation valid { throw new ArgumentNullException(nameof(denseJacobianEvaluation), "Either denseJacobianEvaluation or sparseJacobianEvaluation must be set!"); } //====================================== var nsuccess = count < opts.NumberOfIterations ? currstate._nsuccess + 1 : 0; if (count < opts.NumberOfIterations) { flag = false; MatrixMath.Copy(zcurr, newstate._zn); zcurr.CopyColumn(0, newstate._xn); VectorMath.Copy(ecurr, newstate._en); } else { flag = true; // MatrixMath.Copy(currstate.zn, newstate.zn); // null operation since currstate and newstate are identical currstate._zn.CopyColumn(0, newstate._xn); VectorMath.Copy(currstate._en, newstate._en); // null operation since currstate and newstate are identical } //Compute step size scaling factors rUp = 0.0; if (currstate._qn < currstate._qmax) { rUp = rUp = 1.0 / 1.4 / (Math.Pow(DqUp, 1.0 / (qcurr + 2)) + 1e-6); } rSame = 1.0 / 1.2 / (Math.Pow(Dq, 1.0 / (qcurr + 1)) + 1e-6); rDown = 0.0; if (currstate._qn > 1) { rDown = 1.0 / 1.3 / (Math.Pow(DqDown, 1.0 / (qcurr)) + 1e-6); } //====================================== _nsuccess = nsuccess >= _qn ? 0 : nsuccess; //Step size scale operations if (rSame >= rUp) { if (rSame <= rDown && nsuccess >= _qn && _qn > 1) { _qn = _qn - 1; _Dq = DqDown; for (int i = 0; i < n; i++) { for (int j = newstate._qn + 1; j < qmax + 1; j++) { _zn[i, j] = 0.0; } } nsuccess = 0; _rFactor = rDown; } else { // _qn = _qn; _Dq = Dq; _rFactor = rSame; } } else { if (rUp >= rDown) { if (rUp >= rSame && nsuccess >= _qn && _qn < _qmax) { _qn = _qn + 1; _Dq = DqUp; _rFactor = rUp; nsuccess = 0; } else { // _qn = _qn; _Dq = Dq; _rFactor = rSame; } } else { if (nsuccess >= _qn && _qn > 1) { _qn = _qn - 1; _Dq = DqDown; for (int i = 0; i < n; i++) { for (int j = newstate._qn + 1; j < qmax + 1; j++) { _zn[i, j] = 0.0; } } nsuccess = 0; _rFactor = rDown; } else { // _qn = _qn; _Dq = Dq; _rFactor = rSame; } } } _dt = dt; _tn = t; }