/ Published in: Python
it's a assessement from a lecture about numerics...have to code it in C...but just for trying out my algo, was python just fine.
Expand |
Embed | Plain Text
Copy this code and paste it in your HTML
A=[3,-5,1,0,0,0.5,2,-9,0,0,-1,3,0,0,0,9] b=[5,22,-8,-18] x=b n=4 # think of the dictionary as two dimensional array # i = \[0..n-1\], j=\[i..n-1\] # A\[i,j\] (2.dim) \hat{=} A\[i*4+j\] (1.dim) # to save memory, we calculate it directly in x # we could save more memory by leaving out x or b :) # the inner loop is running through: n < (n(n+1)/2)+1 < n^2 for i in range(n-1,-1,-1): for j in range(n-1,i,-1): x[i] -= A[i*n+j]*x[j] x[i] /= A[i*n+i] print x