Numerische MAtrizen auf numpy umgestellt

This commit is contained in:
2025-12-31 16:03:38 +01:00
parent 59b7f33cf1
commit 01df1d55bf
11 changed files with 14361 additions and 5467 deletions

View File

@@ -1,4 +1,5 @@
import sympy as sp
import numpy as np
from dataclasses import dataclass, field
from typing import Dict, Tuple, Iterable
from Export import Export
@@ -21,34 +22,54 @@ class StochastischesModell:
self.gruppe_beob = [1] * int(self.n_beob)
# In SymPy-Spaltenvektoren umwandeln
self.sigma_beob = sp.Matrix(list(self.sigma_beob))
self.gruppe_beob = sp.Matrix(list(self.gruppe_beob))
#self.sigma_beob = sp.Matrix(list(self.sigma_beob))
#self.gruppe_beob = sp.Matrix(list(self.gruppe_beob))
# Dimension prüfen
if self.sigma_beob.rows != self.gruppe_beob.rows:
#if self.sigma_beob.rows != self.gruppe_beob.rows:
# raise ValueError("sigma_beob und gruppe_beob müssen gleich viele Einträge haben.")
#if self.sigma_beob.rows != int(self.n_beob):
# raise ValueError("n_beob passt nicht zur Länge von sigma_beob / gruppe_beob.")
# Fehlende Gruppen mit sigma0_sq = 1.0 ergänzen
#unique_groups = sorted({int(g) for g in self.gruppe_beob})
#for g in unique_groups:
# if g not in self.sigma0_gruppe:
# self.sigma0_gruppe[g] = 1.0
# In NumPy-Spaltenvektoren umwandeln
self.sigma_beob = np.asarray(list(self.sigma_beob), dtype=float).reshape(-1, 1)
self.gruppe_beob = np.asarray(list(self.gruppe_beob), dtype=int).reshape(-1, 1)
# Dimension prüfen
if self.sigma_beob.shape[0] != self.gruppe_beob.shape[0]:
raise ValueError("sigma_beob und gruppe_beob müssen gleich viele Einträge haben.")
if self.sigma_beob.rows != int(self.n_beob):
if self.sigma_beob.shape[0] != int(self.n_beob):
raise ValueError("n_beob passt nicht zur Länge von sigma_beob / gruppe_beob.")
# Fehlende Gruppen mit sigma0_sq = 1.0 ergänzen
unique_groups = sorted({int(g) for g in self.gruppe_beob})
unique_groups = sorted({int(g) for g in self.gruppe_beob.flatten()})
for g in unique_groups:
if g not in self.sigma0_gruppe:
self.sigma0_gruppe[g] = 1.0
self.func_Qll_numerisch = None
self.liste_symbole_lambdify = None
def berechne_Qll(self) -> Tuple[sp.Matrix, sp.Matrix]:
n = self.n_beob
Q_ll = sp.zeros(n, n)
P = sp.zeros(n, n)
for i in range(self.n_beob):
sigma_i = self.sigma_beob[i, 0] #σ-Wert der i-ten Beobachtung holen
g = int(self.gruppe_beob[i, 0]) #Gruppenzugehörigkeit der Beobachtung bestimmen
sigma0_sq = self.sigma0_gruppe[g] #Den Varianzfaktor der Gruppe holen
q_ii = sigma_i**2 #σ² berechnen
Q_ll[i, i] = q_ii #Diagonale
return Q_ll
#def berechne_Qll(self) -> Tuple[sp.Matrix, sp.Matrix]:
# n = self.n_beob
# Q_ll = sp.zeros(n, n)
# P = sp.zeros(n, n)
# for i in range(self.n_beob):
# sigma_i = self.sigma_beob[i, 0] #σ-Wert der i-ten Beobachtung holen
# g = int(self.gruppe_beob[i, 0]) #Gruppenzugehörigkeit der Beobachtung bestimmen
# sigma0_sq = self.sigma0_gruppe[g] #Den Varianzfaktor der Gruppe holen
# q_ii = sigma_i**2 #σ² berechnen
# Q_ll[i, i] = q_ii #Diagonale
# return Q_ll
def Qll_symbolisch(self, pfad_datenbank, liste_beobachtungen_symbolisch):
liste_standardabweichungen_symbole = []
@@ -155,7 +176,18 @@ class StochastischesModell:
if distanz is not None:
substitutionen[sp.Symbol(f"SD_{beobachtungenID}")] = float(distanz)
Qll_numerisch = Qll_Matrix_Symbolisch.xreplace(substitutionen)
#Qll_numerisch = Qll_Matrix_Symbolisch.xreplace(substitutionen)
if (self.func_Qll_numerisch is None) or (set(self.liste_symbole_lambdify) != set(substitutionen.keys())):
self.liste_symbole_lambdify = sorted(substitutionen.keys(), key=lambda s: str(s))
self.func_Qll_numerisch = sp.lambdify(
self.liste_symbole_lambdify,
Qll_Matrix_Symbolisch,
modules="numpy",
cse=True
)
liste_werte = [substitutionen[s] for s in self.liste_symbole_lambdify]
Qll_numerisch = np.asarray(self.func_Qll_numerisch(*liste_werte), dtype=float)
Export.matrix_to_csv(
r"Zwischenergebnisse\Qll_Numerisch.csv",
@@ -167,31 +199,22 @@ class StochastischesModell:
return Qll_numerisch
def berechne_P(Q_ll: sp.Matrix) -> sp.Matrix:
P = Q_ll.inv()
return P
@staticmethod
def berechne_P(Q_ll):
return np.linalg.inv(Q_ll)
def berechne_Q_xx(N: sp.Matrix) -> sp.Matrix:
if N.rows != N.cols:
@staticmethod
def berechne_Q_xx(N):
if N.shape[0] != N.shape[1]:
raise ValueError("N muss eine quadratische Matrix sein")
Q_xx = N.inv()
return Q_xx
return np.linalg.inv(N)
def berechne_Qvv(self, A, P, Q_xx):
Q_vv = np.linalg.inv(P) - A @ Q_xx @ A.T
return Q_vv
def berechne_Qvv(self, A: sp.Matrix, P: sp.Matrix, Q_xx: sp.Matrix) -> sp.Matrix:
Q_vv = P.inv() - A * Q_xx * A.T
return Q_vv #Kofaktormatrix der Beobachtungsresiduen
def berechne_R(self, Q_vv, P):
return Q_vv @ P #Redundanzmatrix
def berechne_R(self, Q_vv: sp.Matrix, P: sp.Matrix) -> sp.Matrix:
R = Q_vv * P
return R #Redundanzmatrix
def berechne_r(self, R: sp.Matrix) -> sp.Matrix:
n = R.rows
r = sp.zeros(n, 1)
for i in range(n):
r[i, 0] = R[i, i]
return r #Redundanzanteile
def berechne_r(self, R):
return np.diag(R).reshape(-1, 1) #Redundanzanteile