I need to solve millions to tens of millions of independent 2-variable linear systems represented in augmented matrix form. The input consists of coefficient vectors A, B, C, A', B', C' (each containing millions to hundreds of millions of elements). At each index i, the system is defined as:
A[i]x + B[i]y = -C[i]
A'[i]x + B'[i]y = -C'[i]
Current Python Approach (Inefficient): Using a for loop with Sympy to solve each system sequentially:
from sympy import symbols, Eq, solve
x = []
y = []
for i in range(len(A)):
sol = solve([
Eq(A[i]*x_sym + B[i]*y_sym, -C[i]),
Eq(A_prime[i]*x_sym + B_prime[i]*y_sym, -C_prime[i])
], [x_sym, y_sym])
x.append(sol[x_sym])
y.append(sol[y_sym])
This approach becomes impractical for large datasets (e.g., 10M+ entries) due to excessive computation time.
How can I write a DolphinDB script to:
A
, B
, C
, A_prime
, B_prime
, C_prime
.x
and y
using vectorized/parallelized operations?i think you're better off using something like numpy for this
as i don't have your data i'm just going to provide a toy example here but i hope it helps you find a solution
a = [
[# system 1 LHS
[1,0],
[0,2],
],
[# system 2 LHS
[2,0],
[0,1],
]
]
b = [
[# system 1 RHS
[2],
[4],
],
[# system 2 RHS
[6],
[1],
]
]
np.linalg.solve(a,b)
results in
array([[[2.], # solution to system 1
[2.]],
[[3.], # solution to system 2
[1.]]])
just a little more compressed:
a = [[[1, 0], [0, 2]], [[2, 0], [0, 1]]]
b = [[[2], [4]], [[6], [1]]]
print(np.linalg.solve(a,b)).tolist()
>>> [[[2.0], [2.0]], [[3.0], [1.0]]]