[JAVA] Quellcode zum Finden einer normalen orthogonalen Basis mit der Orthogonalisierungsmethode von Gram-Schmidt

Ich habe den Quellcode implementiert, der eine normale orthogonale Basis unter Verwendung der Orthogonalisierungsmethode von Gram-Schmidt erstellt, also werde ich ihn teilen. (Dimension ist auf 3 festgelegt)

Referenzierte Site

Bedeutung und spezifische Beispiele der Orthogonalisierungsmethode von Gramschmidt http://mathtrain.jp/gramschmidt

Gram-Schmidt-Normalorthogonalisierungsmethode http://li.nu/blog/2010/07/gram-schmidt.html

Quellcode (Java)

package hoge.piyo;

import javax.vecmath.Vector3d;

public class LinearAlgebraUtil {
	
	/**
	 *Vektor a1, a2,Erstellen Sie eine normale orthogonale Basis von a3.
	 * a1, a2,a3 muss unabhängig erster Ordnung sein.
	 ** Argumentprüfung weggelassen
	 * @Parameter a1 Vektor a1
	 * @Parameter a2 Vektor a2
	 * @Parameter a3 Vektor a3
	 * @Rückgabe Normale orthogonale Basis u1, u2,Vector3d-Array in der Reihenfolge u3 gespeichert
	 */
	public static Vector3d[] createOrthonormalBasis(
			Vector3d a1, Vector3d a2, Vector3d a3) {
		
		// u1
		Vector3d u1 = createNormalizedVector(a1);
		
		// u2
		Vector3d v2 = new Vector3d(a2);
		//Schließt Komponenten in u1-Richtung von v2 aus.
		removeOrthogonalProjectionVector(v2, u1);
		//Normalisieren Sie v2
		Vector3d u2 = createNormalizedVector(v2);
		
		// u3
		Vector3d v3 = new Vector3d(a3);
		removeOrthogonalProjectionVector(v3, u1);
		removeOrthogonalProjectionVector(v3, u2);
		Vector3d u3 = createNormalizedVector(v3);
		
		return new Vector3d[] {
				u1, u2, u3
		};
	}
	
	/**
	 *Holen Sie sich den Einheitsvektor von Vektor a.
	 * @param ein Vektor a
	 * @Rückgabe des normalisierten Vektors
	 */
	private static Vector3d createNormalizedVector(Vector3d a) {
		Vector3d ret = new Vector3d(a);
		ret.normalize();
		return ret;
	}
	
	/**
	 *Ermitteln Sie den normalen Projektionsvektor des Vektors a in v-Richtung.
	 * @param ein Vektor a
	 * @param u Einheitsvektor des Vektors v
	 */
	private static Vector3d createOrthogonalProjectionVector(
			Vector3d a,
			Vector3d u) {
		Vector3d ret = new Vector3d(u);
		ret.scale( u.dot(a) );
		return ret;
	}
	
	
	/**
	 *Entfernen Sie die orthodoxe Projektionskomponente von Vektor v aus Vektor a
	 * @param ein Vektor a
	 * @param u Einheitsvektor des Vektors v
	 */
	private static void removeOrthogonalProjectionVector(
			Vector3d a,
			Vector3d u) {
		Vector3d orthoProj = createOrthogonalProjectionVector(a, u);
		a.sub(orthoProj);
	}
	

}


Testcode.

package hoge.piyo;

import javax.vecmath.Vector3d;
import org.junit.Test;

public class LinearAlgebraUtilTest {
	
	
	@Test
	public void testCreateOrthonormalBasis() throws Exception {
		
		//Überprüfen Sie die Antwort.
		{
			//Einfaches Beispiel
			Vector3d[] S = new Vector3d[] {
					new Vector3d(0, 0, 10),
					new Vector3d(10, 0, 0),
					new Vector3d(0, 20, 0)
			};
			Vector3d[] T = LinearAlgebraUtil.createOrthonormalBasis(S[0], S[1], S[2]);
			dumpResult(S, T);
		}
		
		{
			//Einfaches Beispiel 2
			Vector3d[] S = new Vector3d[] {
					new Vector3d(0, 0, 10),
					new Vector3d(10, 10, 0),
					new Vector3d(5, 20, 0)
			};
			Vector3d[] T = LinearAlgebraUtil.createOrthonormalBasis(S[0], S[1], S[2]);
			dumpResult(S, T);
		}
		
		//Auszug aus der Berechnungsergebnisprobe aus dem Ergebnis, das anhand eines orthonormalen Basisbeispiels gesucht wurde
		
		{
			// http://www.math4all.in/public_html/linear%20algebra/chapter8.2.html
			// 8.2.5 Examples:
			Vector3d[] S = new Vector3d[] {
					new Vector3d(1, 1, 1),
					new Vector3d(-1, 0, -1),
					new Vector3d(-1, 2, 3)
			};
			Vector3d[] T = LinearAlgebraUtil.createOrthonormalBasis(S[0], S[1], S[2]);
			dumpResult(S, T);
		}

		{
			// http://lyle.smu.edu/emis/8371/book/chap3/node12.html
			// Examples:
			Vector3d[] S = new Vector3d[] {
					new Vector3d(1, 2, 2),
					new Vector3d(1, 1, 0),
					new Vector3d(1, -1, 1)
			};
			Vector3d[] T = LinearAlgebraUtil.createOrthonormalBasis(S[0], S[1], S[2]);
			dumpResult(S, T);
		}
		
		{
			// http://www.mcu.edu.tw/department/management/stat/ch_web/etea/linear/CH6.8%20--%206.9%20Orthonormal%20Bases%20in%20R%5En.pdf
			// Example p6 - p12
			// ->Da die Antwort hier orthogonal ist, ist es notwendig, zu normalisieren und darüber nachzudenken.
			Vector3d[] S = new Vector3d[] {
					new Vector3d(1, 0, 0),
					new Vector3d(1, 2, 0),
					new Vector3d(0, 0, 3)
			};
			Vector3d[] T = LinearAlgebraUtil.createOrthonormalBasis(S[0], S[1], S[2]);
			dumpResult(S, T);
		}
		
	}
	
	private static void dumpResult(Vector3d[] S, Vector3d[] T) {
		System.out.println(String.format("S:%s", toString(S)));
		System.out.println(String.format("T:%s", toString(T)));
		System.out.println();
	}
	
	private static String toString(Vector3d[] vs) {
		StringBuilder sb = new StringBuilder();
		for (Vector3d v : vs) {
			sb.append(String.format("(%f, %f, %f) ", v.x, v.y, v.z)).append(" ");
		}
		return sb.toString();
	}
}

Ergebnis der Testcode-Ausgabe

S:(0.000000, 0.000000, 10.000000)  (10.000000, 0.000000, 0.000000)  (0.000000, 20.000000, 0.000000)  
T:(0.000000, 0.000000, 1.000000)  (1.000000, 0.000000, 0.000000)  (0.000000, 1.000000, 0.000000)  

S:(0.000000, 0.000000, 10.000000)  (10.000000, 10.000000, 0.000000)  (5.000000, 20.000000, 0.000000)  
T:(0.000000, 0.000000, 1.000000)  (0.707107, 0.707107, 0.000000)  (-0.707107, 0.707107, 0.000000)  

S:(1.000000, 1.000000, 1.000000)  (-1.000000, 0.000000, -1.000000)  (-1.000000, 2.000000, 3.000000)  
T:(0.577350, 0.577350, 0.577350)  (-0.408248, 0.816497, -0.408248)  (-0.707107, -0.000000, 0.707107)  

S:(1.000000, 2.000000, 2.000000)  (1.000000, 1.000000, 0.000000)  (1.000000, -1.000000, 1.000000)  
T:(0.333333, 0.666667, 0.666667)  (0.666667, 0.333333, -0.666667)  (0.666667, -0.666667, 0.333333)  

S:(1.000000, 0.000000, 0.000000)  (1.000000, 2.000000, 0.000000)  (0.000000, 0.000000, 3.000000)  
T:(1.000000, 0.000000, 0.000000)  (0.000000, 1.000000, 0.000000)  (0.000000, 0.000000, 1.000000)  

Recommended Posts

Quellcode zum Finden einer normalen orthogonalen Basis mit der Orthogonalisierungsmethode von Gram-Schmidt
Code zum Lösen einer Zufallszahlenmatrix mit der Pivot-Gauß-Eliminierungsmethode
Beispielquellcode zum Ermitteln des minimalen gemeinsamen Vielfachen mehrerer Werte in Java
Schreiben Sie den Code für java.io.File mit java.nio.Path und java.nio.Files neu
Geben Sie beim Erstellen mit Maven den Zeichencode der Quelle an
[Java] So suchen Sie mit der Methode includes nach Werten in einem Array (oder einer Liste)
Oracle Live für den Code
Beim Lesen des Quellcodes
Verwenden Sie eine Beispieldomäne für den Paketnamen im Beispielcode
Lassen Sie uns den Berechtigungscode-Grant-Flow mit Spring Security OAuth-Teil 2: Erstellen einer App vorerst erleben