/// <summary> /// 計算Tsquare /// </summary> /// <param name="item">a observation</param> /// <param name="mean">mean vector</param> /// <param name="invS">inverse of covariance matrix</param> /// <returns></returns> public static double CalculateTSquare(LinearAlgebra.Matrix <double> item, LinearAlgebra.Matrix <double> mean, LinearAlgebra.Matrix <double> invS) { if (item.ColumnCount != mean.ColumnCount || item.ColumnCount != invS.ColumnCount || mean.ColumnCount != invS.ColumnCount) { throw new ArgumentException("矩陣的維度無法計算"); } LinearAlgebra.Matrix <double> diff = item - mean; var t2 = diff.Multiply(invS).Multiply(diff.Transpose()); return(t2.At(0, 0)); }
/// <summary> /// 計算數列各元素的累積和 /// </summary> /// <param name="x">要處理的陣列,合法的輸入是 double[]</param> /// <returns></returns> public static double[] PartialSum(double[] x) { LinearAlgebra.Vector <double> vx = LinearAlgebra.Double.DenseVector.OfArray(x); int row = vx.Count; int col = row; LinearAlgebra.Matrix <double> lmat = LinearAlgebra.Matrix <double> .Build.Dense(row, col, 1); lmat = lmat.LowerTriangle(); LinearAlgebra.Vector <double> result = lmat.Multiply(vx); return(result.ToArray()); }
public LinearAlgebra.Matrix <double> ComputeStiffnessMatrix() { LinearAlgebra.Matrix <double> K = LinearAlgebra.Matrix <double> .Build.Dense(2, 2); double x1 = Nodes[0].Point.X; double y1 = Nodes[0].Point.Y; double z1 = Nodes[0].Point.Z; double x2 = Nodes[1].Point.X; double y2 = Nodes[1].Point.Y; double z2 = Nodes[1].Point.Z; ElemLength = Math.Sqrt((x2 - x1) * (x2 - x1) + (y2 - y1) * (y2 - y1) + (z2 - z1) * (z2 - z1)); K[0, 0] = Area * StiffnessModulus / ElemLength; K[0, 1] = -Area * StiffnessModulus / ElemLength; K[1, 0] = -Area * StiffnessModulus / ElemLength; K[1, 1] = Area * StiffnessModulus / ElemLength; // Tranform to global element stiffness matrix using transformation matrix G double nxx = (x2 - x1) / ElemLength; double nyx = (y2 - y1) / ElemLength; double nzx = (z2 - z1) / ElemLength; G[0, 0] = nxx; G[0, 1] = nyx; G[0, 2] = nzx; G[1, 3] = nxx; G[1, 4] = nyx; G[1, 5] = nzx; LinearAlgebra.Matrix <double> GT = G.Transpose(); LinearAlgebra.Matrix <double> KGlobal = (GT.Multiply(K)).Multiply(G); return(KGlobal); }
public double ComputeStress(List <double> elemDisplacements) { LinearAlgebra.Vector <double> elemDispVector = LinearAlgebra.Vector <double> .Build.Dense(elemDisplacements.ToArray()); double N = StiffnessModulus * Area / ElemLength * LinearAlgebra.Vector <double> .Build.Dense(new double[] { -1, 1 }).DotProduct(G.Multiply(elemDispVector)); return(N / Area); }
public LinearAlgebra.Vector <double> solveEquations(LinearAlgebra.Matrix <double> stiffnessMatrix, LinearAlgebra.Vector <double> forceVector, List <int> boundaryDofs, List <double> boundaryConstraints) { int nDof = forceVector.Count; // Find all dofs where force is known List <int> allDofs = Enumerable.Range(0, nDof).ToList(); List <int> unknownDofs = allDofs.Except(boundaryDofs).ToList(); // Add the know displacements to the result LinearAlgebra.Vector <double> displacementVector = LinearAlgebra.Vector <double> .Build.Dense(nDof); for (int i = 0; i < boundaryDofs.Count; i++) { displacementVector[boundaryDofs[i]] = boundaryConstraints[i]; } // Pick out part of matrix corresponding to known forces int nrUnknownDofs = unknownDofs.Count; LinearAlgebra.Matrix <double> unknownK = LinearAlgebra.Matrix <double> .Build.Dense(nrUnknownDofs, nrUnknownDofs); LinearAlgebra.Vector <double> knownForces = LinearAlgebra.Vector <double> .Build.Dense(unknownDofs.Count); for (int i = 0; i < unknownDofs.Count; i++) { for (int j = 0; j < unknownDofs.Count; j++) { unknownK[i, j] = stiffnessMatrix[unknownDofs[i], unknownDofs[j]]; } knownForces[i] = forceVector[unknownDofs[i]]; } LinearAlgebra.Matrix <double> unkownKnownK = LinearAlgebra.Matrix <double> .Build.Dense(nrUnknownDofs, boundaryDofs.Count); for (int i = 0; i < unknownDofs.Count; i++) { for (int j = 0; j < boundaryDofs.Count; j++) { unkownKnownK[i, j] = stiffnessMatrix[unknownDofs[i], boundaryDofs[j]]; } knownForces[i] = forceVector[unknownDofs[i]]; } // Solve for the unknown displacements LinearAlgebra.Vector <double> unknownDisplacements = unknownK.Inverse().Multiply(knownForces.Subtract(unkownKnownK.Multiply(LinearAlgebra.Vector <double> .Build.Dense(boundaryConstraints.ToArray())))); // Insert the calculated displacements for (int i = 0; i < unknownDofs.Count; i++) { displacementVector[unknownDofs[i]] = unknownDisplacements[i]; } return(displacementVector); }