Skip to content

Instantly share code, notes, and snippets.

@kodai100
Created June 27, 2017 02:32
Show Gist options
  • Save kodai100/6a39366023b5af0847b97c536a37f4e9 to your computer and use it in GitHub Desktop.
Save kodai100/6a39366023b5af0847b97c536a37f4e9 to your computer and use it in GitHub Desktop.
using Vector = MathNet.Numerics.LinearAlgebra.Vector<float>;
using Matrix = MathNet.Numerics.LinearAlgebra.Matrix<float>;
using UnityEngine;
public class ConfigureNumerics : MonoBehaviour {
public Vector3 v1;
public Vector3 v2;
public Vector3 v3;
void Start() {
// 行列の定義
Matrix A = Matrix.Build.DenseOfArray(new float[,] { { v1.x, v1.y, v1.z }, { v2.x, v2.y, v2.z }, { v3.x, v3.y, v3.z } });
Vector v = Vector.Build.DenseOfArray(new float[] { v1.x, v1.y, v1.z });
// 各行列の要素の四則演算
Debug.Log("A + A = " + (A + A));
Debug.Log("A - A = " + (A - A));
Debug.Log("A * A = " + Matrix.op_DotMultiply(A, A));
Debug.Log("A / A = " + Matrix.op_DotDivide(A, A));
// 行列演算
Debug.Log("A * A = " + (A * A)); // 積
Debug.Log("invA = " + A.Inverse()); // 逆行列
Debug.Log("A * invA = " + (A * A.Inverse())); // 行列とその逆行列の積 (= 単位行列)
Debug.Log("det(A) = " + A.Determinant()); // 行列式
Matrix B = Matrix.Build.DenseIdentity(3);
A.CopyTo(B);
B += B;
Debug.Log("A += A = " + B);
// ベクトル演算
Debug.Log("A * v = " + (A * v)); //
Debug.Log("v * A = " + (v * A)); //
Debug.Log("v * v = " + (v * v)); // 内積
// 行列の特殊な演算
// 特異値分解
var svd = A.Svd(true);
Debug.Log("U = " + svd.U);
Debug.Log("s = " + svd.S);
Debug.Log("VT = " + svd.VT);
Debug.Log("U * s * VT = " + (svd.U * Matrix.Build.DenseOfDiagonalArray(new float[] { svd.S[0], svd.S[1], svd.S[2] }) * svd.VT));
// 行列の固有値分解
var eig = A.Evd();
Debug.Log("P = " + eig.EigenVectors);
Debug.Log("e = " + eig.EigenValues);
Debug.Log("PAP_inv" + (eig.EigenVectors.Inverse() * Matrix.Build.DiagonalOfDiagonalArray(new float[] { (float)eig.EigenValues[0].Real, (float)eig.EigenValues[1].Real, (float)eig.EigenValues[2].Real } ) * eig.EigenVectors));
var lu = A.LU();
Debug.Log("L = " + lu.L);
Debug.Log("U = " + lu.U);
}
void VectorToVector3() {
}
void Update () {
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment