Files
CPY_AlgorithmGateway/algorithm/math_utils.py
T
2026-07-30 18:46:32 +08:00

154 lines
5.0 KiB
Python

import numpy as np
def gauss(s, k):
n = np.arange(-(k - 1) / 2, (k - 1) / 2 + 1)
g = np.exp(-n ** 2 / (2 * s ** 2))
g = g / np.sum(g)
return g
def ind(x, dx, xshift=0, N=None):
Index = np.round(((x - xshift) / dx) + 1)
if N is not None:
if N == 0:
return np.array([])
if isinstance(Index, np.ndarray):
Index[Index < 1] = 1
Index[Index > N] = N
else:
if Index < 1:
Index = 1
elif Index > N:
Index = N
return Index.astype(int) if isinstance(Index, np.ndarray) else int(Index)
def equal(a, b):
tol = b / 10000.0
if np.isscalar(tol):
if tol < 1e-15:
tol = 1e-15
else:
tol[tol < 1e-15] = 1e-15
return np.abs(a - b) <= tol
def is_pe(cableType):
cableType = float(cableType)
if (equal(cableType, 1) or equal(cableType, 7) or equal(cableType, 44) or
equal(cableType, 60) or (73.99 <= cableType <= 78.01) or
(100.99 <= cableType <= 105.01) or (130.99 <= cableType <= 140.01) or
equal(cableType, 111) or equal(cableType, 151) or
equal(cableType, 62340000) or equal(cableType, 62434100) or
equal(cableType, 115) or equal(cableType, 62) or
equal(cableType, 123) or equal(cableType, 116)):
return 1
else:
return 0
def polyfit2(x, y, n):
x = np.asarray(x, dtype=np.float64)
y = np.asarray(y, dtype=np.float64)
if x.shape != y.shape:
raise ValueError('polyfit2: X and Y vectors must be the same size.')
x_flat = x.ravel()
y_flat = y.ravel()
if n == 1:
A = np.column_stack([x_flat, np.ones(len(x_flat))])
elif n == 2:
A = np.column_stack([x_flat ** 2, x_flat, np.ones(len(x_flat))])
else:
raise ValueError('polyfit2: n must be 1 or 2!')
p = np.linalg.lstsq(A, y_flat, rcond=None)[0]
return p.ravel()
def lininter(x, y, xi):
x = np.asarray(x, dtype=np.float64).ravel()
y = np.asarray(y, dtype=np.float64).ravel()
xi = np.asarray(xi, dtype=np.float64).ravel()
if len(x) != len(y):
raise ValueError('length(x) must be == length(y)')
if len(x) < 2:
raise ValueError('length(x) must be >= 2')
yi = np.zeros(len(xi))
N = len(x)
k = 0
for i in range(len(xi)):
foundxi = 0
while foundxi == 0:
if (xi[i] >= x[k]) and (xi[i] < x[k + 1]):
m = (y[k + 1] - y[k]) / (x[k + 1] - x[k])
yi[i] = m * (xi[i] - x[k]) + y[k]
foundxi = 1
elif xi[i] <= x[0]:
yi[i] = y[0]
foundxi = 1
elif xi[i] >= x[N - 1]:
yi[i] = y[N - 1]
foundxi = 1
else:
if k != (N - 2):
k = k + 1
else:
raise ValueError('Linint3: No solution, because no pos. step xi')
return yi
def linint3(x, y, xi):
x = np.asarray(x, dtype=np.float64).ravel()
y = np.asarray(y, dtype=np.float64).ravel()
xi = np.asarray(xi, dtype=np.float64).ravel()
if len(x) != len(y):
raise ValueError('length(x) must be == length(y)')
if len(x) < 2:
raise ValueError('length(x) must be >= 2')
sort_idx = np.argsort(xi)
xi_sorted = xi[sort_idx]
yi_sorted = np.zeros(len(xi_sorted))
N = len(x)
k = 0
for i in range(len(xi_sorted)):
foundxi = 0
while foundxi == 0:
if (xi_sorted[i] >= x[k]) and (xi_sorted[i] < x[k + 1]):
m = (y[k + 1] - y[k]) / (x[k + 1] - x[k])
yi_sorted[i] = m * (xi_sorted[i] - x[k]) + y[k]
foundxi = 1
elif xi_sorted[i] <= x[0]:
m = (y[1] - y[0]) / (x[1] - x[0])
yi_sorted[i] = m * (xi_sorted[i] - x[0]) + y[0]
foundxi = 1
elif xi_sorted[i] >= x[N - 1]:
m = (y[N - 1] - y[N - 2]) / (x[N - 1] - x[N - 2])
yi_sorted[i] = m * (xi_sorted[i] - x[N - 2]) + y[N - 2]
foundxi = 1
else:
if k != (N - 2):
k = k + 1
else:
raise ValueError('Linint3: No solution, because no pos. step xi')
yi = np.zeros_like(yi_sorted)
yi[sort_idx] = yi_sorted
return yi
def linreg(IN, Dl, Dr, dx, alles=0, xshift=0):
ll = ind(Dl, dx, xshift)
rr = ind(Dr, dx, xshift)
rechtsx = (ll - 1) * dx + xshift
N = 2 * (int(np.floor((rr - ll + 1) / 2)))
Indiffv = np.zeros(N)
x_arr = np.zeros(N)
Indiffv[:N] = IN[:N + ll - 1]
x_arr[:N] = np.arange(N) * dx
A = polyfit2(x_arr, Indiffv, 1)
reg = np.zeros(N)
Indiff = np.zeros(N)
reg[:N] = A[1] + A[0] * dx * np.arange(N)
Indiff[:N] = Indiffv[:N] - reg[:N]
Ref = Indiffv[int(N / 2)]
return Indiff, rechtsx, Ref